Subversion Repositories Kolibri OS

Rev

Rev 1635 | Rev 2130 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2004-2007. All rights reserved. ;;
  4. ;; Distributed under terms of the GNU General Public License    ;;
  5. ;;                                                              ;;
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7.  
  8. $Revision: 2010 $
  9.  
  10.  
  11. GREEDY_KERNEL  equ 0
  12.  
  13. struc APP_HEADER_00
  14. { .banner      dq ?
  15.   .version     dd ?    ;+8
  16.   .start       dd ?    ;+12
  17.   .i_end       dd ?    ;+16
  18.   .mem_size    dd ?    ;+20
  19.   .i_param     dd ?    ;+24
  20. }
  21.  
  22. struc APP_HEADER_01
  23. { .banner      dq ?
  24.   .version     dd ?    ;+8
  25.   .start       dd ?    ;+12
  26.   .i_end       dd ?    ;+16
  27.   .mem_size    dd ?    ;+20
  28.   .stack_top   dd ?    ;+24
  29.   .i_param     dd ?    ;+28
  30.   .i_icon      dd ?    ;+32
  31. }
  32.  
  33.  
  34. struc APP_PARAMS
  35. { .app_cmdline   ;0x00
  36.   .app_path  ;0x04
  37.   .app_eip   ;0x08
  38.   .app_esp   ;0x0C
  39.   .app_mem   ;0x10
  40. }
  41.  
  42. macro _clear_ op
  43. {  mov ecx, op/4
  44.    xor eax, eax
  45.    cld
  46.    rep stosd
  47. }
  48.  
  49. fs_execute_from_sysdir:
  50.     xor ebx, ebx
  51. fs_execute_from_sysdir_param:
  52.     xor edx, edx
  53.     mov esi, sysdir_path
  54.  
  55. align 4
  56. proc fs_execute
  57.  
  58. ;fn_read:dword, file_size:dword, cluster:dword
  59.  
  60. ; ebx - cmdline
  61. ; edx - flags
  62. ; ebp - full filename
  63. ; [esp+4] = procedure DoRead, [esp+8] = filesize & [esp+12]... - arguments for it
  64.  
  65.        locals
  66.          cmdline       rd 64    ;256/4
  67.          filename      rd 256   ;1024/4
  68.          flags     dd ?
  69.  
  70.          save_cr3      dd ?
  71.          slot      dd ?
  72.          slot_base     dd ?
  73.          file_base     dd ?
  74.          file_size     dd ?
  75.                       ;app header data
  76.          hdr_cmdline   dd ? ;0x00
  77.          hdr_path      dd ? ;0x04
  78.          hdr_eip       dd ? ;0x08
  79.          hdr_esp       dd ? ;0x0C
  80.          hdr_mem       dd ? ;0x10
  81.          hdr_i_end     dd ? ;0x14
  82.        endl
  83.  
  84.        pushad
  85.  
  86.        mov [flags], edx
  87.  
  88. ; [ebp]  pointer to filename
  89.  
  90.        lea edi, [filename]
  91.        lea ecx, [edi+1024]
  92.        mov al, '/'
  93.        stosb
  94. @@:
  95.        cmp edi, ecx
  96.        jae .bigfilename
  97.        lodsb
  98.        stosb
  99.        test al, al
  100.        jnz @b
  101.        mov esi, [ebp]
  102.        test esi, esi
  103.        jz .namecopied
  104.        mov byte [edi-1], '/'
  105. @@:
  106.        cmp edi, ecx
  107.        jae .bigfilename
  108.        lodsb
  109.        stosb
  110.        test al, al
  111.        jnz @b
  112.        jmp .namecopied
  113. .bigfilename:
  114.        popad
  115.        mov eax, -ERROR_FILE_NOT_FOUND
  116.        ret
  117.  
  118. .namecopied:
  119.  
  120.        mov [cmdline], ebx
  121.        test ebx, ebx
  122.        jz @F
  123.  
  124.        lea eax, [cmdline]
  125.        mov dword [eax+252], 0
  126.        stdcall strncpy, eax, ebx, 255
  127. @@:
  128.        lea eax, [filename]
  129.        stdcall load_file, eax
  130.        mov  ecx, -ERROR_FILE_NOT_FOUND
  131.        test eax, eax
  132.        jz .err_file
  133.  
  134.        mov [file_base], eax
  135.        mov [file_size], ebx
  136.  
  137.        lea ebx, [hdr_cmdline]
  138.        call test_app_header
  139.        mov ecx, -0x1F
  140.        test eax, eax
  141.        jz .err_hdr
  142.  
  143.        ;mov esi, new_process_loading
  144.        ;call sys_msg_board_str       ; write message to message board
  145.  
  146. .wait_lock:
  147.        cmp [application_table_status],0
  148.        je .get_lock
  149.        call   change_task
  150.        jmp .wait_lock
  151.  
  152. .get_lock:
  153.        mov eax, 1
  154.        xchg eax, [application_table_status]
  155.        test eax, eax
  156.        jnz .wait_lock
  157.  
  158.        call set_application_table_status
  159.  
  160.        call get_new_process_place
  161.        test eax, eax
  162.        mov ecx, -0x20      ; too many processes
  163.        jz .err
  164.  
  165.        mov [slot], eax
  166.        shl eax, 8
  167.        add eax, SLOT_BASE
  168.        mov [slot_base], eax
  169.        mov edi, eax
  170.        _clear_ 256     ;clean extended information about process
  171.  
  172. ; write application name
  173.        lea eax, [filename]
  174.        stdcall strrchr,  eax, '/'  ; now eax points to name without path
  175.  
  176.        lea esi, [eax+1]
  177.        test eax, eax
  178.        jnz @F
  179.        lea esi, [filename]
  180. @@:
  181.        mov ecx, 8  ; 8 chars for name
  182.        mov edi, [slot_base]
  183. .copy_process_name_loop:
  184.        lodsb
  185.        cmp al, '.'
  186.        jz .copy_process_name_done
  187.        test al, al
  188.        jz .copy_process_name_done
  189.        stosb
  190.        loop .copy_process_name_loop
  191. .copy_process_name_done:
  192.  
  193.        mov ebx, cr3
  194.        mov [save_cr3], ebx
  195.  
  196.        stdcall create_app_space,[hdr_mem],[file_base],[file_size]
  197.        mov ecx, -30  ; no memory
  198.        test eax, eax
  199.        jz .failed
  200.  
  201.        mov   ebx,[slot_base]
  202.        mov   [ebx+APPDATA.dir_table],eax
  203.        mov   eax,[hdr_mem]
  204.        mov   [ebx+APPDATA.mem_size],eax
  205.  
  206.        xor edx, edx
  207.        cmp word [6], '02'
  208.        jne @f
  209.  
  210.        not edx
  211. @@:
  212.        mov   [ebx+APPDATA.tls_base],edx
  213.  
  214. if GREEDY_KERNEL
  215. else
  216.        mov ecx, [hdr_mem]
  217.        mov edi, [file_size]
  218.        add edi, 4095
  219.        and edi, not 4095
  220.        sub ecx, edi
  221.        jna @F
  222.  
  223.        xor eax, eax
  224.        cld
  225.        rep stosb
  226. @@:
  227. end if
  228.  
  229. ; release only virtual space, not phisical memory
  230.  
  231.        stdcall free_kernel_space, [file_base]
  232.        lea eax, [hdr_cmdline]
  233.        lea ebx, [cmdline]
  234.        lea ecx, [filename]
  235.        stdcall set_app_params ,[slot],eax,ebx,ecx,[flags]
  236.  
  237.        mov eax, [save_cr3]
  238.        call set_cr3
  239.  
  240.        xor ebx, ebx
  241.        mov [application_table_status],ebx ;unlock application_table_status mutex
  242.        mov eax,[process_number]  ;set result
  243.        ret
  244. .failed:
  245.        mov eax, [save_cr3]
  246.        call set_cr3
  247. .err:
  248. .err_hdr:
  249.        stdcall kernel_free,[file_base]
  250. .err_file:
  251.        xor eax, eax
  252.        mov [application_table_status],eax
  253.        mov eax, ecx
  254.        ret
  255. endp
  256.  
  257. align 4
  258. test_app_header:
  259.        virtual at eax
  260.          APP_HEADER_00 APP_HEADER_00
  261.        end virtual
  262.        virtual at eax
  263.          APP_HEADER_01 APP_HEADER_01
  264.        end virtual
  265.  
  266.        cmp dword [eax], 'MENU'
  267.        jne .fail
  268.        cmp word [eax+4],'ET'
  269.        jne .fail
  270.  
  271.        cmp [eax+6], word '00'
  272.        jne  .check_01_header
  273.  
  274.        mov  ecx,[APP_HEADER_00.start]
  275.        mov  [ebx+0x08], ecx                 ;app_eip
  276.        mov  edx,[APP_HEADER_00.mem_size]
  277.        mov  [ebx+0x10], edx                 ;app_mem
  278.        shr  edx,1
  279.        sub  edx,0x10
  280.        mov  [ebx+0x0C], edx                 ;app_esp
  281.        mov  ecx,[APP_HEADER_00.i_param]
  282.        mov  [ebx], ecx                      ;app_cmdline
  283.        mov  [ebx+4], dword 0                ;app_path
  284.        mov  edx, [APP_HEADER_00.i_end]
  285.        mov  [ebx+0x14], edx
  286.        ret
  287.  
  288.  .check_01_header:
  289.  
  290.        cmp [eax+6], word '01'
  291.        je @f
  292.        cmp [eax+6], word '02'
  293.        jne .fail
  294. @@:
  295.        mov  ecx,[APP_HEADER_01.start]
  296.        mov  [ebx+0x08], ecx                 ;app_eip
  297.        mov  edx,[APP_HEADER_01.mem_size]
  298.  
  299. ; \begin{diamond}[20.08.2006]
  300. ; sanity check (functions 19,58 load app_i_end bytes and that must
  301. ; fit in allocated memory to prevent kernel faults)
  302.        cmp  edx,[APP_HEADER_01.i_end]
  303.        jb   .fail
  304. ; \end{diamond}[20.08.2006]
  305.  
  306.        mov  [ebx+0x10], edx                 ;app_mem
  307.        mov  ecx,[APP_HEADER_01.stack_top]
  308.        mov  [ebx+0x0C], ecx                 ;app_esp
  309.        mov  edx,[APP_HEADER_01.i_param]
  310.        mov  [ebx], edx                      ;app_cmdline
  311.        mov  ecx,[APP_HEADER_01.i_icon]
  312.        mov  [ebx+4], ecx                    ;app_path
  313.        mov  edx, [APP_HEADER_01.i_end]
  314.        mov  [ebx+0x14], edx
  315.        ret
  316. .fail:
  317.        xor eax, eax
  318.        ret
  319.  
  320. align 4
  321. proc get_new_process_place
  322. ;input:
  323. ;  none
  324. ;result:
  325. ;  eax=[new_process_place]<>0 - ok
  326. ;      0 - failed.
  327. ;This function find least empty slot.
  328. ;It doesn't increase [TASK_COUNT]!
  329.        mov    eax,CURRENT_TASK
  330.        mov    ebx,[TASK_COUNT]
  331.        inc    ebx
  332.        shl    ebx,5
  333.        add    ebx,eax       ;ebx - address of process information for (last+1) slot
  334. .newprocessplace:
  335. ;eax = address of process information for current slot
  336.        cmp    eax,ebx
  337.        jz     .endnewprocessplace   ;empty slot after high boundary
  338.        add    eax,0x20
  339.        cmp    word [eax+0xa],9  ;check process state, 9 means that process slot is empty
  340.        jnz    .newprocessplace
  341. .endnewprocessplace:
  342.        mov    ebx,eax
  343.        sub    eax,CURRENT_TASK
  344.        shr    eax,5         ;calculate slot index
  345.        cmp    eax,256
  346.        jge    .failed       ;it should be <256
  347.        mov    word [ebx+0xa],9  ;set process state to 9 (for slot after hight boundary)
  348.        ret
  349. .failed:
  350.        xor    eax,eax
  351.        ret
  352. endp
  353.  
  354. align 4
  355. proc create_app_space stdcall, app_size:dword,img_base:dword,img_size:dword
  356.        locals
  357.          app_pages   dd ?
  358.          img_pages   dd ?
  359.          dir_addr    dd ?
  360.          app_tabs    dd ?
  361.        endl
  362.  
  363.        mov ebx, pg_data.pg_mutex
  364.        call wait_mutex   ;ebx
  365.  
  366.        xor eax, eax
  367.        mov [dir_addr], eax
  368.  
  369.        mov eax, [app_size]
  370.        add eax, 4095
  371.        and eax, NOT(4095)
  372.        mov [app_size], eax
  373.        mov ebx, eax
  374.        shr eax, 12
  375.        mov [app_pages], eax
  376.  
  377.        add ebx, 0x3FFFFF
  378.        and ebx, NOT(0x3FFFFF)
  379.        shr ebx, 22
  380.        mov [app_tabs], ebx
  381.  
  382.        mov ecx, [img_size]
  383.        add ecx, 4095
  384.        and ecx, NOT(4095)
  385.  
  386.        mov [img_size], ecx
  387.        shr ecx, 12
  388.        mov [img_pages], ecx
  389.  
  390.      if GREEDY_KERNEL
  391.        lea eax, [ecx+ebx+2]    ;only image size
  392.      else
  393.        lea eax, [eax+ebx+2]    ;all requested memory
  394.      end if
  395.        cmp eax, [pg_data.pages_free]
  396.        ja .fail
  397.  
  398.        call alloc_page
  399.        test eax, eax
  400.        jz .fail
  401.        mov [dir_addr], eax
  402.        stdcall map_page,[tmp_task_pdir],eax,dword PG_SW
  403.  
  404.        mov edi, [tmp_task_pdir]
  405.        mov ecx, (OS_BASE shr 20)/4
  406.        xor eax, eax
  407.        cld
  408.        rep stosd
  409.  
  410.        mov ecx, (OS_BASE shr 20)/4
  411.        mov esi, sys_pgdir+(OS_BASE shr 20)
  412.        rep movsd
  413.  
  414.        mov eax, [dir_addr]
  415.        or eax, PG_SW
  416.        mov [edi-4096+(page_tabs shr 20)], eax
  417.  
  418.        and eax, -4096
  419.        call set_cr3
  420.  
  421.        mov edx, [app_tabs]
  422.        mov edi, new_app_base
  423. @@:
  424.        call alloc_page
  425.        test eax, eax
  426.        jz .fail
  427.  
  428.        stdcall map_page_table, edi, eax
  429.        add edi, 0x00400000
  430.        dec edx
  431.        jnz @B
  432.  
  433.        mov edi, new_app_base
  434.        shr edi, 10
  435.        add edi, page_tabs
  436.  
  437.        mov ecx, [app_tabs]
  438.        shl ecx, 10
  439.        xor eax, eax
  440.        rep stosd
  441.  
  442.        mov ecx, [img_pages]
  443.        mov ebx, PG_UW
  444.        mov edx, new_app_base
  445.        mov esi, [img_base]
  446.        mov edi, new_app_base
  447.        shr esi, 10
  448.        shr edi, 10
  449.        add esi, page_tabs
  450.        add edi, page_tabs
  451. .remap:
  452.        lodsd
  453.        or eax, ebx      ; force user level r/w access
  454.        stosd
  455.        add edx, 0x1000
  456.        dec [app_pages]
  457.        dec ecx
  458.        jnz .remap
  459.  
  460.        mov ecx, [app_pages]
  461.        test ecx, ecx
  462.        jz .done
  463.  
  464. if GREEDY_KERNEL
  465.        mov eax, 0x02
  466.        rep stosd
  467. else
  468.  
  469. .alloc:
  470.        call alloc_page
  471.        test eax, eax
  472.        jz .fail
  473.  
  474.        stdcall map_page,edx,eax,dword PG_UW
  475.        add edx, 0x1000
  476.        dec [app_pages]
  477.        jnz .alloc
  478. end if
  479.  
  480. .done:
  481.        stdcall map_page,[tmp_task_pdir],dword 0,dword PG_UNMAP
  482.  
  483.        dec [pg_data.pg_mutex]
  484.        mov eax, [dir_addr]
  485.        ret
  486. .fail:
  487.        dec [pg_data.pg_mutex]
  488.        cmp [dir_addr], 0
  489.        je @f
  490.        stdcall destroy_app_space, [dir_addr], 0
  491. @@:
  492.        xor eax, eax
  493.        ret
  494. endp
  495.  
  496. align 4
  497. set_cr3:
  498.  
  499.        mov ebx, [current_slot]
  500.        mov [ebx+APPDATA.dir_table], eax
  501.        mov cr3, eax
  502.        ret
  503.  
  504. align 4
  505. proc destroy_page_table stdcall, pg_tab:dword
  506.  
  507.        push esi
  508.  
  509.        mov esi, [pg_tab]
  510.        mov ecx, 1024
  511. .free:
  512.        mov eax, [esi]
  513.        test eax, 1
  514.        jz .next
  515.            test eax, 1 shl 9
  516.            jnz .next                      ;skip shared pages
  517.        call free_page
  518. .next:
  519.        add esi, 4
  520.        dec ecx
  521.        jnz .free
  522.        pop esi
  523.        ret
  524. endp
  525.  
  526. align 4
  527. proc destroy_app_space stdcall, pg_dir:dword, dlls_list:dword
  528.  
  529.        xor   edx,edx
  530.        push  edx
  531.        mov   eax,0x2
  532.        mov ebx, [pg_dir]
  533. .loop:
  534. ;eax = current slot of process
  535.        mov   ecx,eax
  536.        shl   ecx,5
  537.        cmp   byte [CURRENT_TASK+ecx+0xa],9  ;if process running?
  538.        jz    @f              ;skip empty slots
  539.        shl   ecx,3
  540.        add   ecx,SLOT_BASE
  541.        cmp   [ecx+APPDATA.dir_table],ebx   ;compare page directory addresses
  542.        jnz   @f
  543.        mov   [ebp-4],ecx
  544.        inc   edx                ;thread found
  545. @@:
  546.        inc   eax
  547.        cmp   eax,[TASK_COUNT]       ;exit loop if we look through all processes
  548.        jle   .loop
  549.  
  550. ;edx = number of threads
  551. ;our process is zombi so it isn't counted
  552.        pop   ecx
  553.        cmp   edx,1
  554.        jg    .ret
  555. ;if there isn't threads then clear memory.
  556.        mov esi, [dlls_list]
  557.        call destroy_all_hdlls
  558.  
  559.        mov ebx, pg_data.pg_mutex
  560.        call wait_mutex   ;ebx
  561.  
  562.        mov eax, [pg_dir]
  563.        and eax, not 0xFFF
  564.        stdcall map_page,[tmp_task_pdir],eax,PG_SW
  565.        mov esi, [tmp_task_pdir]
  566.        mov edi, (OS_BASE shr 20)/4
  567. .destroy:
  568.        mov eax, [esi]
  569.        test eax, 1
  570.        jz .next
  571.        and eax, not 0xFFF
  572.        stdcall map_page,[tmp_task_ptab],eax,PG_SW
  573.        stdcall destroy_page_table, [tmp_task_ptab]
  574.        mov eax, [esi]
  575.        call free_page
  576. .next:
  577.        add esi, 4
  578.        dec edi
  579.        jnz .destroy
  580.  
  581.        mov eax, [pg_dir]
  582.        call free_page
  583. .exit:
  584.        stdcall map_page,[tmp_task_ptab],0,PG_UNMAP
  585.        stdcall map_page,[tmp_task_pdir],0,PG_UNMAP
  586.        dec [pg_data.pg_mutex]
  587. .ret:
  588.        ret
  589. endp
  590.  
  591. align 4
  592. get_pid:
  593.        mov eax, [TASK_BASE]
  594.        mov eax, [eax+TASKDATA.pid]
  595.        ret
  596.  
  597. pid_to_slot:
  598. ;Input:
  599. ;  eax - pid of process
  600. ;Output:
  601. ;  eax - slot of process or 0 if process don't exists
  602. ;Search process by PID.
  603.     push   ebx
  604.     push   ecx
  605.     mov    ebx,[TASK_COUNT]
  606.     shl    ebx,5
  607.     mov    ecx,2*32
  608.  
  609. .loop:
  610. ;ecx=offset of current process info entry
  611. ;ebx=maximum permitted offset
  612.     cmp    byte [CURRENT_TASK+ecx+0xa],9
  613.     jz     .endloop      ;skip empty slots
  614.     cmp    [CURRENT_TASK+ecx+0x4],eax ;check PID
  615.     jz     .pid_found
  616. .endloop:
  617.     add    ecx,32
  618.     cmp    ecx,ebx
  619.     jle    .loop
  620.  
  621.     pop    ecx
  622.     pop    ebx
  623.     xor    eax,eax
  624.     ret
  625.  
  626. .pid_found:
  627.     shr    ecx,5
  628.     mov    eax,ecx       ;convert offset to index of slot
  629.     pop    ecx
  630.     pop    ebx
  631.     ret
  632.  
  633. check_region:
  634. ;input:
  635. ;  esi - start of buffer
  636. ;  edx - size of buffer
  637. ;result:
  638. ;  eax = 1 region lays in app memory
  639. ;  eax = 0 region don't lays in app memory
  640.      mov  eax,[CURRENT_TASK]
  641. ;     jmp  check_process_region
  642. ;-----------------------------------------------------------------------------
  643. ;check_process_region:
  644. ;input:
  645. ;  eax - slot
  646. ;  esi - start of buffer
  647. ;  edx - size of buffer
  648. ;result:
  649. ;  eax = 1 region lays in app memory
  650. ;  eax = 0 region don't lays in app memory
  651.  
  652.      test edx,edx
  653.      jle  .ok
  654.      shl  eax,5
  655.      cmp  word [CURRENT_TASK+eax+0xa],0
  656.      jnz  .failed
  657.      shl  eax,3
  658.      mov  eax,[SLOT_BASE+eax+0xb8]
  659.      test eax,eax
  660.      jz   .failed
  661.  
  662.      mov  eax,1
  663.      ret
  664.  
  665.  
  666. ;    call MEM_Get_Linear_Address
  667. ;    push ebx
  668. ;    push ecx
  669. ;    push edx
  670. ;    mov  edx,ebx
  671. ;    and  edx,not (4096-1)
  672. ;    sub  ebx,edx
  673. ;    add  ecx,ebx
  674. ;    mov  ebx,edx
  675. ;    add  ecx,(4096-1)
  676. ;    and  ecx,not (4096-1)
  677. ;.loop:
  678. ;;eax - linear address of page directory
  679. ;;ebx - current page
  680. ;;ecx - current size
  681. ;    mov  edx,ebx
  682. ;    shr  edx,22
  683. ;    mov  edx,[eax+4*edx]
  684. ;    and  edx,not (4096-1)
  685. ;    test edx,edx
  686. ;    jz   .failed1
  687. ;    push eax
  688. ;    mov  eax,edx
  689. ;    call MEM_Get_Linear_Address
  690. ;    mov  edx,ebx
  691. ;    shr  edx,12
  692. ;    and  edx,(1024-1)
  693. ;    mov  eax,[eax+4*edx]
  694. ;    and  eax,not (4096-1)
  695. ;    test eax,eax
  696. ;    pop  eax
  697. ;    jz   .failed1
  698. ;    add  ebx,4096
  699. ;    sub  ecx,4096
  700. ;    jg   .loop
  701. ;    pop  edx
  702. ;    pop  ecx
  703. ;    pop  ebx
  704. .ok:
  705.     mov  eax,1
  706.     ret
  707. ;
  708. ;.failed1:
  709. ;    pop  edx
  710. ;    pop  ecx
  711. ;    pop  ebx
  712. .failed:
  713.     xor  eax,eax
  714.     ret
  715.  
  716. align 4
  717. proc read_process_memory
  718. ;Input:
  719. ;  eax - process slot
  720. ;  ecx - buffer address
  721. ;  edx - buffer size
  722. ;  esi - start address in other process
  723. ;Output:
  724. ;  eax - number of bytes read.
  725.        locals
  726.          slot   dd ?
  727.          buff   dd ?
  728.          r_count    dd ?
  729.          offset dd ?
  730.          tmp_r_cnt  dd ?
  731.        endl
  732.  
  733.        mov [slot], eax
  734.        mov [buff], ecx
  735.        and [r_count], 0
  736.        mov [tmp_r_cnt], edx
  737.        mov [offset], esi
  738.  
  739.        pushad
  740. .read_mem:
  741.        mov edx, [offset]
  742.        mov ebx, [tmp_r_cnt]
  743.  
  744.        mov ecx, 0x400000
  745.        and edx, 0x3FFFFF
  746.        sub ecx, edx
  747.        cmp ecx, ebx
  748.        jbe @f
  749.        mov ecx, ebx
  750. @@:
  751.        cmp ecx, 0x8000
  752.        jna @F
  753.        mov ecx, 0x8000
  754. @@:
  755.        mov ebx, [offset]
  756.  
  757.        push ecx
  758.        stdcall map_memEx, [proc_mem_map],\
  759.                   [slot], ebx, ecx, PG_MAP
  760.        pop ecx
  761.  
  762.        mov esi, [offset]
  763.        and esi, 0xfff
  764.        sub eax, esi
  765.        jbe .ret
  766.        cmp ecx, eax
  767.        jbe @f
  768.        mov ecx, eax
  769.        mov [tmp_r_cnt], eax
  770. @@:
  771.        add esi, [proc_mem_map]
  772.        mov edi, [buff]
  773.        mov edx, ecx
  774.        rep movsb
  775.        add [r_count], edx
  776.  
  777.        add [offset], edx
  778.        sub [tmp_r_cnt], edx
  779.        jnz .read_mem
  780. .ret:
  781.        popad
  782.        mov eax, [r_count]
  783.        ret
  784. endp
  785.  
  786. align 4
  787. proc write_process_memory
  788. ;Input:
  789. ;  eax - process slot
  790. ;  ecx - buffer address
  791. ;  edx - buffer size
  792. ;  esi - start address in other process
  793. ;Output:
  794. ;  eax - number of bytes written
  795.  
  796.        locals
  797.          slot   dd ?
  798.          buff   dd ?
  799.          w_count    dd ?
  800.          offset dd ?
  801.          tmp_w_cnt  dd ?
  802.        endl
  803.  
  804.        mov [slot], eax
  805.        mov [buff], ecx
  806.        and [w_count], 0
  807.        mov [tmp_w_cnt], edx
  808.        mov [offset], esi
  809.  
  810.        pushad
  811. .read_mem:
  812.        mov edx, [offset]
  813.        mov ebx, [tmp_w_cnt]
  814.  
  815.        mov ecx, 0x400000
  816.        and edx, 0x3FFFFF
  817.        sub ecx, edx
  818.        cmp ecx, ebx
  819.        jbe @f
  820.        mov ecx, ebx
  821. @@:
  822.        cmp ecx, 0x8000
  823.        jna @F
  824.        mov ecx, 0x8000
  825. @@:
  826.        mov ebx, [offset]
  827.       ;     add ebx, new_app_base
  828.        push ecx
  829.        stdcall map_memEx, [proc_mem_map],\
  830.                   [slot], ebx, ecx, PG_SW
  831.        pop ecx
  832.  
  833.        mov edi, [offset]
  834.        and edi, 0xfff
  835.        sub eax, edi
  836.        jbe .ret
  837.        cmp ecx, eax
  838.        jbe @f
  839.        mov ecx, eax
  840.        mov [tmp_w_cnt], eax
  841. @@:
  842.        add edi, [proc_mem_map]
  843.        mov esi, [buff]
  844.        mov edx, ecx
  845.        rep movsb
  846.  
  847.        add [w_count], edx
  848.        add [offset], edx
  849.        sub [tmp_w_cnt], edx
  850.        jnz .read_mem
  851. .ret:
  852.        popad
  853.        mov eax, [w_count]
  854.        ret
  855. endp
  856.  
  857. align 4
  858. proc new_sys_threads
  859.        locals
  860.          slot      dd ?
  861.          app_cmdline   dd ? ;0x00
  862.          app_path      dd ? ;0x04
  863.          app_eip       dd ? ;0x08
  864.          app_esp       dd ? ;0x0C
  865.          app_mem       dd ? ;0x10
  866.        endl
  867.  
  868.        cmp ebx,1
  869.        jne .failed          ;other subfunctions
  870.  
  871.        xor  eax,eax
  872.        mov [app_eip], ecx
  873.        mov [app_cmdline], eax
  874.        mov [app_esp], edx
  875.        mov [app_path], eax
  876.        ;mov    esi,new_process_loading
  877.        ;call   sys_msg_board_str
  878. .wait_lock:
  879.        cmp [application_table_status],0
  880.        je .get_lock
  881.        call   change_task
  882.        jmp .wait_lock
  883.  
  884. .get_lock:
  885.        mov eax, 1
  886.        xchg eax, [application_table_status]
  887.        test eax, eax
  888.        jnz .wait_lock
  889.  
  890.        call   set_application_table_status
  891.  
  892.        call get_new_process_place
  893.        test eax, eax
  894.        jz .failed
  895.  
  896.        mov [slot], eax
  897.  
  898.        mov    esi,[current_slot]
  899.        mov    ebx,esi         ;ebx=esi - pointer to extended information about current thread
  900.  
  901.        mov    edi, eax
  902.        shl    edi,8
  903.        add    edi,SLOT_BASE
  904.        mov    edx,edi         ;edx=edi - pointer to extended infomation about new thread
  905.        mov    ecx,256/4
  906.        xor eax, eax
  907.        cld
  908.        rep    stosd           ;clean extended information about new thread
  909.        mov    esi,ebx
  910.        mov    edi,edx
  911.        mov    ecx,11
  912.        rep    movsb           ;copy process name
  913.  
  914.        mov eax,[ebx+APPDATA.heap_base]
  915.        mov [edx+APPDATA.heap_base], eax
  916.  
  917.        mov ecx,[ebx+APPDATA.heap_top]
  918.        mov [edx+APPDATA.heap_top], ecx
  919.  
  920.        mov eax,[ebx+APPDATA.mem_size]
  921.        mov [edx+APPDATA.mem_size], eax
  922.  
  923.        mov ecx,[ebx+APPDATA.dir_table]
  924.        mov [edx+APPDATA.dir_table],ecx      ;copy page directory
  925.  
  926.        mov eax,[ebx+APPDATA.dlls_list_ptr]
  927.        mov [edx+APPDATA.dlls_list_ptr],eax
  928.  
  929.        mov eax, [ebx+APPDATA.tls_base]
  930.        test eax, eax
  931.        jz @F
  932.  
  933.        push edx
  934.        stdcall user_alloc, 4096
  935.        pop edx
  936.        test eax, eax
  937.        jz .failed1      ;eax=0
  938. @@:
  939.        mov [edx+APPDATA.tls_base], eax
  940.  
  941.        lea eax, [app_cmdline]
  942.        stdcall set_app_params ,[slot],eax,dword 0,\
  943.                      dword 0,dword 0
  944.  
  945.        ;mov    esi,new_process_running
  946.        ;call   sys_msg_board_str                ;output information about succefull startup
  947.        xor    eax,eax
  948.        mov    [application_table_status],eax    ;unlock application_table_status mutex
  949.        mov    eax,[process_number]              ;set result
  950.        ret
  951. .failed:
  952.        xor    eax,eax
  953. .failed1:
  954.        mov    [application_table_status],eax
  955.        dec    eax       ;-1
  956.        ret
  957. endp
  958.  
  959. ; param
  960. ;  ebx=mutex
  961.  
  962. align 4
  963. wait_mutex:
  964. ;;Maxis use atomic bts for mutex 4.4.2009
  965.        push eax
  966.        push ebx
  967. .do_wait:
  968.            bts dword [ebx],0
  969.            jnc .locked
  970.        call change_task
  971.        jmp .do_wait
  972. .locked:
  973.        pop ebx
  974.        pop eax
  975.        ret
  976.  
  977. align 4
  978. tls_app_entry:
  979.  
  980.         call init_heap
  981.         stdcall user_alloc, 4096
  982.  
  983.         mov edx, [current_slot]
  984.         mov [edx+APPDATA.tls_base], eax
  985.         mov [tls_data_l+2],ax
  986.         shr eax,16
  987.         mov [tls_data_l+4],al
  988.         mov [tls_data_l+7],ah
  989.         mov dx, app_tls
  990.         mov fs, dx
  991.         popad
  992.         iretd
  993.  
  994.  
  995. EFL_IF      equ 0x0200
  996. EFL_IOPL1   equ 0x1000
  997. EFL_IOPL2   equ 0x2000
  998. EFL_IOPL3   equ 0x3000
  999.  
  1000.  
  1001. align 4
  1002. proc set_app_params stdcall,slot:dword, params:dword,\
  1003.             cmd_line:dword, app_path:dword, flags:dword
  1004.  
  1005.        locals
  1006.          pl0_stack dd ?
  1007.        endl
  1008.  
  1009.        stdcall kernel_alloc, RING0_STACK_SIZE+512
  1010.        mov [pl0_stack], eax
  1011.  
  1012.        lea edi, [eax+RING0_STACK_SIZE]
  1013.  
  1014.        mov eax, [slot]
  1015.        mov ebx, eax
  1016.  
  1017.        shl eax, 8
  1018.        mov [eax+SLOT_BASE+APPDATA.fpu_state], edi
  1019.        mov [eax+SLOT_BASE+APPDATA.exc_handler], 0
  1020.        mov [eax+SLOT_BASE+APPDATA.except_mask], 0
  1021.  
  1022. ;set default io permission map
  1023.        mov ecx, [SLOT_BASE+256+APPDATA.io_map]
  1024.        mov [eax+SLOT_BASE+APPDATA.io_map], ecx
  1025.        mov ecx, [SLOT_BASE+256+APPDATA.io_map+4]
  1026.        mov [eax+SLOT_BASE+APPDATA.io_map+4], ecx
  1027.  
  1028.        mov esi, fpu_data
  1029.        mov ecx, 512/4
  1030.        rep movsd
  1031.  
  1032.        cmp    ebx,[TASK_COUNT]
  1033.        jle    .noinc
  1034.        inc    dword [TASK_COUNT]       ;update number of processes
  1035. .noinc:
  1036.        shl ebx,8
  1037.        lea edx, [ebx+SLOT_BASE+APP_EV_OFFSET]
  1038.        mov [SLOT_BASE+APPDATA.fd_ev+ebx],edx
  1039.        mov [SLOT_BASE+APPDATA.bk_ev+ebx],edx
  1040.  
  1041.        add edx, APP_OBJ_OFFSET-APP_EV_OFFSET
  1042.        mov [SLOT_BASE+APPDATA.fd_obj+ebx],edx
  1043.        mov [SLOT_BASE+APPDATA.bk_obj+ebx],edx
  1044.  
  1045.        mov ecx, [def_cursor]
  1046.        mov [SLOT_BASE+APPDATA.cursor+ebx],ecx
  1047.        mov eax, [pl0_stack]
  1048.        mov [SLOT_BASE+APPDATA.pl0_stack+ebx],eax
  1049.        add eax, RING0_STACK_SIZE
  1050.        mov [SLOT_BASE+APPDATA.saved_esp0+ebx], eax
  1051.  
  1052.        push ebx
  1053.        stdcall kernel_alloc, 0x1000
  1054.        pop ebx
  1055.        mov esi,[current_slot]
  1056.        mov esi,[esi+APPDATA.cur_dir]
  1057.        mov ecx,0x1000/4
  1058.        mov edi,eax
  1059.        mov [ebx+SLOT_BASE+APPDATA.cur_dir],eax
  1060.        rep movsd
  1061.  
  1062.        shr ebx,3
  1063.        mov eax, new_app_base
  1064.        mov dword [CURRENT_TASK+ebx+0x10],eax
  1065.  
  1066. .add_command_line:
  1067.        mov edx,[params]
  1068.        mov edx,[edx]       ;app_cmdline
  1069.        test edx,edx
  1070.        jz @f           ;application doesn't need parameters
  1071.  
  1072.        mov     eax, edx
  1073.        add     eax, 256
  1074.        jc      @f
  1075.  
  1076.        cmp     eax, [SLOT_BASE+APPDATA.mem_size+ebx*8]
  1077.        ja      @f
  1078.  
  1079.        mov     byte [edx], 0   ;force empty string if no cmdline given
  1080.        mov     eax, [cmd_line]
  1081.        test    eax, eax
  1082.        jz      @f
  1083.        stdcall strncpy, edx, eax, 256
  1084. @@:
  1085.        mov edx,[params]
  1086.        mov edx, [edx+4]    ;app_path
  1087.        test edx,edx
  1088.        jz @F           ;application don't need path of file
  1089.        mov     eax, edx
  1090.        add     eax, 1024
  1091.        jc      @f
  1092.        cmp     eax, [SLOT_BASE+APPDATA.mem_size+ebx*8]
  1093.        ja      @f
  1094.        stdcall strncpy, edx, [app_path], 1024
  1095. @@:
  1096.        mov    ebx,[slot]
  1097.        mov    eax,ebx
  1098.        shl    ebx,5
  1099.        lea    ecx,[draw_data+ebx]  ;ecx - pointer to draw data
  1100.  
  1101.        mov edx, irq0.return
  1102.        cmp [ebx*8+SLOT_BASE+APPDATA.tls_base], -1
  1103.        jne @F
  1104.        mov edx, tls_app_entry
  1105. @@:
  1106. ; set window state to 'normal' (non-minimized/maximized/rolled-up) state
  1107.        mov     [ebx+window_data+WDATA.fl_wstate], WSTATE_NORMAL
  1108.        mov     [ebx+window_data+WDATA.fl_redraw], 1
  1109.        add    ebx,CURRENT_TASK        ;ebx - pointer to information about process
  1110.        mov    [ebx+TASKDATA.wnd_number],al;set window number on screen = process slot
  1111.  
  1112.        mov    [ebx+TASKDATA.event_mask],dword 1+2+4 ;set default event flags (see 40 function)
  1113.  
  1114.        inc    dword [process_number]
  1115.        mov    eax,[process_number]
  1116.        mov    [ebx+4],eax       ;set PID
  1117.  
  1118. ;set draw data to full screen
  1119.        xor    eax,eax
  1120.        mov    [ecx+0],dword eax
  1121.        mov    [ecx+4],dword eax
  1122.        mov    eax,[Screen_Max_X]
  1123.        mov    [ecx+8],eax
  1124.        mov    eax,[Screen_Max_Y]
  1125.        mov    [ecx+12],eax
  1126.  
  1127.        mov ebx, [pl0_stack]
  1128.        mov esi,[params]
  1129.        lea ecx, [ebx+REG_EIP]
  1130.        xor eax, eax
  1131.  
  1132.        mov [ebx+REG_RET], edx
  1133.        mov [ebx+REG_EDI], eax
  1134.        mov [ebx+REG_ESI], eax
  1135.        mov [ebx+REG_EBP], eax
  1136.        mov [ebx+REG_ESP], ecx   ;ebx+REG_EIP
  1137.        mov [ebx+REG_EBX], eax
  1138.        mov [ebx+REG_EDX], eax
  1139.        mov [ebx+REG_ECX], eax
  1140.        mov [ebx+REG_EAX], eax
  1141.  
  1142.        mov eax, [esi+0x08]       ;app_eip
  1143.        mov [ebx+REG_EIP],  eax   ;app_entry
  1144.        mov [ebx+REG_CS], dword app_code
  1145.        mov [ebx+REG_EFLAGS], dword EFL_IOPL1+EFL_IF
  1146.  
  1147.        mov eax, [esi+0x0C]       ;app_esp
  1148.        mov [ebx+REG_APP_ESP], eax    ;app_stack
  1149.        mov [ebx+REG_SS], dword app_data
  1150.  
  1151.        lea ecx, [ebx+REG_RET]
  1152.        mov ebx, [slot]
  1153.        shl ebx, 5
  1154.        mov [ebx*8+SLOT_BASE+APPDATA.saved_esp], ecx
  1155.  
  1156.        xor  ecx, ecx    ; process state - running
  1157. ; set if debuggee
  1158.        test byte [flags], 1
  1159.        jz   .no_debug
  1160.        inc  ecx     ; process state - suspended
  1161.        mov  eax,[CURRENT_TASK]
  1162.        mov  [SLOT_BASE+ebx*8+APPDATA.debugger_slot],eax
  1163. .no_debug:
  1164.        mov  [CURRENT_TASK+ebx+TASKDATA.state], cl
  1165.        ;mov    esi,new_process_running
  1166.        ;call   sys_msg_board_str     ;output information about succefull startup
  1167.        ret
  1168. endp
  1169.  
  1170.  
  1171. align 4
  1172.  
  1173. get_stack_base:
  1174.        mov eax,[current_slot]
  1175.        mov eax,[eax+APPDATA.pl0_stack]
  1176.        ret
  1177.  
  1178.  
  1179. include "debug.inc"
  1180.