Subversion Repositories Kolibri OS

Rev

Rev 2385 | Blame | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2004-2012. All rights reserved. ;;
  4. ;; Distributed under terms of the GNU General Public License    ;;
  5. ;;                                                              ;;
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7.  
  8. $Revision $
  9.  
  10.  
  11. GREEDY_KERNEL  equ 0
  12.  
  13. struct  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. ends
  21.  
  22. struct  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. ends
  32.  
  33.  
  34. struct  APP_PARAMS
  35.         app_cmdline     dd ?    ;0x00
  36.         app_path        dd ?    ;0x04
  37.         app_eip         dd ?    ;0x08
  38.         app_esp         dd ?    ;0x0C
  39.         app_mem         dd ?    ;0x10
  40. ends
  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.          handle        dd ? ;temp. for default cursor handle for curr. thread
  76.                       ;app header data
  77.          hdr_cmdline   dd ? ;0x00
  78.          hdr_path      dd ? ;0x04
  79.          hdr_eip       dd ? ;0x08
  80.          hdr_esp       dd ? ;0x0C
  81.          hdr_mem       dd ? ;0x10
  82.          hdr_i_end     dd ? ;0x14
  83.        endl
  84.  
  85.         pushad
  86.  
  87.         pushad
  88.         stdcall set_cursor, [def_cursor_clock]
  89.         mov     [handle], eax
  90.         mov     [redrawmouse_unconditional], 1
  91.         call    __sys_draw_pointer
  92.         popad
  93.  
  94.         mov     [flags], edx
  95.  
  96. ; [ebp]  pointer to filename
  97.  
  98.         lea     edi, [filename]
  99.         lea     ecx, [edi+1024]
  100.         mov     al, '/'
  101.         stosb
  102. @@:
  103.         cmp     edi, ecx
  104.         jae     .bigfilename
  105.         lodsb
  106.         stosb
  107.         test    al, al
  108.         jnz     @b
  109.         mov     esi, [ebp]
  110.         test    esi, esi
  111.         jz      .namecopied
  112.         mov     byte [edi-1], '/'
  113. @@:
  114.         cmp     edi, ecx
  115.         jae     .bigfilename
  116.         lodsb
  117.         stosb
  118.         test    al, al
  119.         jnz     @b
  120.         jmp     .namecopied
  121. .bigfilename:
  122.         popad
  123.         mov     eax, -ERROR_FILE_NOT_FOUND
  124.  
  125.         jmp     .final
  126.  
  127. .namecopied:
  128.  
  129.         mov     [cmdline], ebx
  130.         test    ebx, ebx
  131.         jz      @F
  132.  
  133.         lea     eax, [cmdline]
  134.         mov     dword [eax+252], 0
  135.         stdcall strncpy, eax, ebx, 255
  136. @@:
  137.         lea     eax, [filename]
  138.         stdcall load_file, eax
  139.  
  140.         mov     esi, -ERROR_FILE_NOT_FOUND
  141.         test    eax, eax
  142.         jz      .err_file
  143.  
  144.         mov     [file_base], eax
  145.         mov     [file_size], ebx
  146.  
  147.         lea     ebx, [hdr_cmdline]
  148.         call    test_app_header
  149.         mov     esi, -0x1F
  150.         test    eax, eax
  151.         jz      .err_hdr
  152.  
  153. .wait_lock:
  154.         cmp     [application_table_status], 0
  155.         je      .get_lock
  156.         call    change_task
  157.         jmp     .wait_lock
  158.  
  159. .get_lock:
  160.         mov     eax, 1
  161.         xchg    eax, [application_table_status]
  162.         test    eax, eax
  163.         jnz     .wait_lock
  164.  
  165.         call    set_application_table_status
  166.  
  167.         call    get_new_process_place
  168.         test    eax, eax
  169.         mov     esi, -0x20 ; too many processes
  170.         jz      .err
  171.  
  172.         mov     [slot], eax
  173.         shl     eax, 8
  174.         add     eax, SLOT_BASE
  175.         mov     [slot_base], eax
  176.         mov     edi, eax
  177.        _clear_ 256     ;clean extended information about process
  178.  
  179. ; write application name
  180.         lea     eax, [filename]
  181.         stdcall strrchr, eax, '/'  ; now eax points to name without path
  182.  
  183.         lea     esi, [eax+1]
  184.         test    eax, eax
  185.         jnz     @F
  186.         lea     esi, [filename]
  187. @@:
  188.         mov     ecx, 8; 8 chars for name
  189.         mov     edi, [slot_base]
  190. .copy_process_name_loop:
  191.         lodsb
  192.         cmp     al, '.'
  193.         jz      .copy_process_name_done
  194.         test    al, al
  195.         jz      .copy_process_name_done
  196.         stosb
  197.         loop    .copy_process_name_loop
  198. .copy_process_name_done:
  199.  
  200.         mov     ebx, cr3
  201.         mov     [save_cr3], ebx
  202.  
  203.         stdcall create_app_space, [hdr_mem], [file_base], [file_size]
  204.         mov     esi, -30; no memory
  205.         test    eax, eax
  206.         jz      .failed
  207.  
  208.         mov     ebx, [slot_base]
  209.         mov     [ebx+APPDATA.dir_table], eax
  210.         mov     eax, [hdr_mem]
  211.         mov     [ebx+APPDATA.mem_size], eax
  212.  
  213.         xor     edx, edx
  214.         cmp     word [6], '02'
  215.         jne     @f
  216.  
  217.         not     edx
  218. @@:
  219.         mov     [ebx+APPDATA.tls_base], edx
  220.  
  221. if GREEDY_KERNEL
  222. else
  223.         mov     ecx, [hdr_mem]
  224.         mov     edi, [file_size]
  225.         add     edi, 4095
  226.         and     edi, not 4095
  227.         sub     ecx, edi
  228.         jna     @F
  229.  
  230.         xor     eax, eax
  231.         cld
  232.         rep stosb
  233. @@:
  234. end if
  235.  
  236. ; release only virtual space, not phisical memory
  237.  
  238.         stdcall free_kernel_space, [file_base]
  239.         lea     eax, [hdr_cmdline]
  240.         lea     ebx, [cmdline]
  241.         lea     ecx, [filename]
  242.         stdcall set_app_params , [slot], eax, ebx, ecx, [flags]
  243.  
  244.         mov     eax, [save_cr3]
  245.         call    set_cr3
  246.  
  247.         xor     ebx, ebx
  248.         mov     [application_table_status], ebx;unlock application_table_status mutex
  249.         mov     eax, [process_number];set result
  250.  
  251.         jmp     .final
  252.  
  253. .failed:
  254.         mov     eax, [save_cr3]
  255.         call    set_cr3
  256. .err:
  257. .err_hdr:
  258.         stdcall kernel_free, [file_base]
  259. .err_file:
  260.         xor     eax, eax
  261.         mov     [application_table_status], eax
  262.         mov     eax, esi
  263. .final:
  264.         pushad
  265.         stdcall set_cursor, [handle]
  266.         mov     [redrawmouse_unconditional], 1
  267.         call    __sys_draw_pointer
  268.         popad
  269.         ret
  270. endp
  271.  
  272. align 4
  273. test_app_header:
  274.        virtual at eax
  275.          APP_HEADER_00 APP_HEADER_00_
  276.        end virtual
  277.        virtual at eax
  278.          APP_HEADER_01 APP_HEADER_01_
  279.        end virtual
  280.  
  281.         cmp     dword [eax], 'MENU'
  282.         jne     .fail
  283.         cmp     word [eax+4], 'ET'
  284.         jne     .fail
  285.  
  286.         cmp     [eax+6], word '00'
  287.         jne     .check_01_header
  288.  
  289.         mov     ecx, [APP_HEADER_00.start]
  290.         mov     [ebx+0x08], ecx             ;app_eip
  291.         mov     edx, [APP_HEADER_00.mem_size]
  292.         mov     [ebx+0x10], edx             ;app_mem
  293.         shr     edx, 1
  294.         sub     edx, 0x10
  295.         mov     [ebx+0x0C], edx             ;app_esp
  296.         mov     ecx, [APP_HEADER_00.i_param]
  297.         mov     [ebx], ecx                  ;app_cmdline
  298.         mov     [ebx+4], dword 0            ;app_path
  299.         mov     edx, [APP_HEADER_00.i_end]
  300.         mov     [ebx+0x14], edx
  301.         ret
  302.  
  303.  .check_01_header:
  304.  
  305.         cmp     [eax+6], word '01'
  306.         je      @f
  307.         cmp     [eax+6], word '02'
  308.         jne     .fail
  309. @@:
  310.         mov     ecx, [APP_HEADER_01.start]
  311.         mov     [ebx+0x08], ecx             ;app_eip
  312.         mov     edx, [APP_HEADER_01.mem_size]
  313.  
  314. ; \begin{diamond}[20.08.2006]
  315. ; sanity check (functions 19,58 load app_i_end bytes and that must
  316. ; fit in allocated memory to prevent kernel faults)
  317.         cmp     edx, [APP_HEADER_01.i_end]
  318.         jb      .fail
  319. ; \end{diamond}[20.08.2006]
  320.  
  321.         mov     [ebx+0x10], edx             ;app_mem
  322.         mov     ecx, [APP_HEADER_01.stack_top]
  323.         mov     [ebx+0x0C], ecx             ;app_esp
  324.         mov     edx, [APP_HEADER_01.i_param]
  325.         mov     [ebx], edx                  ;app_cmdline
  326.         mov     ecx, [APP_HEADER_01.i_icon]
  327.         mov     [ebx+4], ecx                ;app_path
  328.         mov     edx, [APP_HEADER_01.i_end]
  329.         mov     [ebx+0x14], edx
  330.         ret
  331. .fail:
  332.         xor     eax, eax
  333.         ret
  334.  
  335. align 4
  336. proc get_new_process_place
  337. ;input:
  338. ;  none
  339. ;result:
  340. ;  eax=[new_process_place]<>0 - ok
  341. ;      0 - failed.
  342. ;This function find least empty slot.
  343. ;It doesn't increase [TASK_COUNT]!
  344.         mov     eax, CURRENT_TASK
  345.         mov     ebx, [TASK_COUNT]
  346.         inc     ebx
  347.         shl     ebx, 5
  348.         add     ebx, eax    ;ebx - address of process information for (last+1) slot
  349. .newprocessplace:
  350. ;eax = address of process information for current slot
  351.         cmp     eax, ebx
  352.         jz      .endnewprocessplace ;empty slot after high boundary
  353.         add     eax, 0x20
  354.         cmp     word [eax+0xa], 9;check process state, 9 means that process slot is empty
  355.         jnz     .newprocessplace
  356. .endnewprocessplace:
  357.         mov     ebx, eax
  358.         sub     eax, CURRENT_TASK
  359.         shr     eax, 5      ;calculate slot index
  360.         cmp     eax, 256
  361.         jge     .failed     ;it should be <256
  362.         mov     word [ebx+0xa], 9;set process state to 9 (for slot after hight boundary)
  363.         ret
  364. .failed:
  365.         xor     eax, eax
  366.         ret
  367. endp
  368.  
  369. align 4
  370. proc create_app_space stdcall, app_size:dword,img_base:dword,img_size:dword
  371.        locals
  372.          app_pages   dd ?
  373.          img_pages   dd ?
  374.          dir_addr    dd ?
  375.          app_tabs    dd ?
  376.        endl
  377.  
  378.         mov     ecx, pg_data.mutex
  379.         call    mutex_lock
  380.  
  381.         xor     eax, eax
  382.         mov     [dir_addr], eax
  383.  
  384.         mov     eax, [app_size]
  385.         add     eax, 4095
  386.         and     eax, NOT(4095)
  387.         mov     [app_size], eax
  388.         mov     ebx, eax
  389.         shr     eax, 12
  390.         mov     [app_pages], eax
  391.  
  392.         add     ebx, 0x3FFFFF
  393.         and     ebx, NOT(0x3FFFFF)
  394.         shr     ebx, 22
  395.         mov     [app_tabs], ebx
  396.  
  397.         mov     ecx, [img_size]
  398.         add     ecx, 4095
  399.         and     ecx, NOT(4095)
  400.  
  401.         mov     [img_size], ecx
  402.         shr     ecx, 12
  403.         mov     [img_pages], ecx
  404.  
  405. if GREEDY_KERNEL
  406.         lea     eax, [ecx+ebx+2];only image size
  407. else
  408.         lea     eax, [eax+ebx+2];all requested memory
  409. end if
  410.         cmp     eax, [pg_data.pages_free]
  411.         ja      .fail
  412.  
  413.         call    alloc_page
  414.         test    eax, eax
  415.         jz      .fail
  416.         mov     [dir_addr], eax
  417.         stdcall map_page, [tmp_task_pdir], eax, dword PG_SW
  418.  
  419.         mov     edi, [tmp_task_pdir]
  420.         mov     ecx, (OS_BASE shr 20)/4
  421.         xor     eax, eax
  422.         cld
  423.         rep stosd
  424.  
  425.         mov     ecx, (OS_BASE shr 20)/4
  426.         mov     esi, sys_pgdir+(OS_BASE shr 20)
  427.         rep movsd
  428.  
  429.         mov     eax, [dir_addr]
  430.         or      eax, PG_SW
  431.         mov     [edi-4096+(page_tabs shr 20)], eax
  432.  
  433.         and     eax, -4096
  434.         call    set_cr3
  435.  
  436.         mov     edx, [app_tabs]
  437.         mov     edi, new_app_base
  438. @@:
  439.         call    alloc_page
  440.         test    eax, eax
  441.         jz      .fail
  442.  
  443.         stdcall map_page_table, edi, eax
  444.         add     edi, 0x00400000
  445.         dec     edx
  446.         jnz     @B
  447.  
  448.         mov     edi, new_app_base
  449.         shr     edi, 10
  450.         add     edi, page_tabs
  451.  
  452.         mov     ecx, [app_tabs]
  453.         shl     ecx, 10
  454.         xor     eax, eax
  455.         rep stosd
  456.  
  457.         mov     ecx, [img_pages]
  458.         mov     ebx, PG_UW
  459.         mov     edx, new_app_base
  460.         mov     esi, [img_base]
  461.         mov     edi, new_app_base
  462.         shr     esi, 10
  463.         shr     edi, 10
  464.         add     esi, page_tabs
  465.         add     edi, page_tabs
  466. .remap:
  467.         lodsd
  468.         or      eax, ebx; force user level r/w access
  469.         stosd
  470.         add     edx, 0x1000
  471.         dec     [app_pages]
  472.         dec     ecx
  473.         jnz     .remap
  474.  
  475.         mov     ecx, [app_pages]
  476.         test    ecx, ecx
  477.         jz      .done
  478.  
  479. if GREEDY_KERNEL
  480.         mov     eax, 0x02
  481.         rep stosd
  482. else
  483.  
  484. .alloc:
  485.         call    alloc_page
  486.         test    eax, eax
  487.         jz      .fail
  488.  
  489.         stdcall map_page, edx, eax, dword PG_UW
  490.         add     edx, 0x1000
  491.         dec     [app_pages]
  492.         jnz     .alloc
  493. end if
  494.  
  495. .done:
  496.         stdcall map_page, [tmp_task_pdir], dword 0, dword PG_UNMAP
  497.  
  498.         mov     ecx, pg_data.mutex
  499.         call    mutex_unlock
  500.         mov     eax, [dir_addr]
  501.         ret
  502. .fail:
  503.         mov     ecx, pg_data.mutex
  504.         call    mutex_unlock
  505.         cmp     [dir_addr], 0
  506.         je      @f
  507.         stdcall destroy_app_space, [dir_addr], 0
  508. @@:
  509.         xor     eax, eax
  510.         ret
  511. endp
  512.  
  513. align 4
  514. set_cr3:
  515.  
  516.         mov     ebx, [current_slot]
  517.         mov     [ebx+APPDATA.dir_table], eax
  518.         mov     cr3, eax
  519.         ret
  520.  
  521. align 4
  522. proc destroy_page_table stdcall, pg_tab:dword
  523.  
  524.         push    esi
  525.  
  526.         mov     esi, [pg_tab]
  527.         mov     ecx, 1024
  528. .free:
  529.         mov     eax, [esi]
  530.         test    eax, 1
  531.         jz      .next
  532.         test    eax, 1 shl 9
  533.         jnz     .next                     ;skip shared pages
  534.         call    free_page
  535. .next:
  536.         add     esi, 4
  537.         dec     ecx
  538.         jnz     .free
  539.         pop     esi
  540.         ret
  541. endp
  542.  
  543. align 4
  544. proc destroy_app_space stdcall, pg_dir:dword, dlls_list:dword
  545.  
  546.         xor     edx, edx
  547.         push    edx
  548.         mov     eax, 0x2
  549.         mov     ebx, [pg_dir]
  550. .loop:
  551. ;eax = current slot of process
  552.         mov     ecx, eax
  553.         shl     ecx, 5
  554.         cmp     byte [CURRENT_TASK+ecx+0xa], 9;if process running?
  555.         jz      @f           ;skip empty slots
  556.         shl     ecx, 3
  557.         add     ecx, SLOT_BASE
  558.         cmp     [ecx+APPDATA.dir_table], ebx;compare page directory addresses
  559.         jnz     @f
  560.         mov     [ebp-4], ecx
  561.         inc     edx             ;thread found
  562. @@:
  563.         inc     eax
  564.         cmp     eax, [TASK_COUNT]   ;exit loop if we look through all processes
  565.         jle     .loop
  566.  
  567. ;edx = number of threads
  568. ;our process is zombi so it isn't counted
  569.         pop     ecx
  570.         cmp     edx, 1
  571.         jg      .ret
  572. ;if there isn't threads then clear memory.
  573.         mov     esi, [dlls_list]
  574.         call    destroy_all_hdlls;ecx=APPDATA
  575.  
  576.         mov     ecx, pg_data.mutex
  577.         call    mutex_lock
  578.  
  579.         mov     eax, [pg_dir]
  580.         and     eax, not 0xFFF
  581.         stdcall map_page, [tmp_task_pdir], eax, PG_SW
  582.         mov     esi, [tmp_task_pdir]
  583.         mov     edi, (OS_BASE shr 20)/4
  584. .destroy:
  585.         mov     eax, [esi]
  586.         test    eax, 1
  587.         jz      .next
  588.         and     eax, not 0xFFF
  589.         stdcall map_page, [tmp_task_ptab], eax, PG_SW
  590.         stdcall destroy_page_table, [tmp_task_ptab]
  591.         mov     eax, [esi]
  592.         call    free_page
  593. .next:
  594.         add     esi, 4
  595.         dec     edi
  596.         jnz     .destroy
  597.  
  598.         mov     eax, [pg_dir]
  599.         call    free_page
  600. .exit:
  601.         stdcall map_page, [tmp_task_ptab], 0, PG_UNMAP
  602.         stdcall map_page, [tmp_task_pdir], 0, PG_UNMAP
  603.         mov     ecx, pg_data.mutex
  604.         call    mutex_unlock
  605. .ret:
  606.         ret
  607. endp
  608.  
  609. align 4
  610. get_pid:
  611.         mov     eax, [TASK_BASE]
  612.         mov     eax, [eax+TASKDATA.pid]
  613.         ret
  614.  
  615. pid_to_slot:
  616. ;Input:
  617. ;  eax - pid of process
  618. ;Output:
  619. ;  eax - slot of process or 0 if process don't exists
  620. ;Search process by PID.
  621.         push    ebx
  622.         push    ecx
  623.         mov     ebx, [TASK_COUNT]
  624.         shl     ebx, 5
  625.         mov     ecx, 2*32
  626.  
  627. .loop:
  628. ;ecx=offset of current process info entry
  629. ;ebx=maximum permitted offset
  630.         cmp     byte [CURRENT_TASK+ecx+0xa], 9
  631.         jz      .endloop ;skip empty slots
  632.         cmp     [CURRENT_TASK+ecx+0x4], eax;check PID
  633.         jz      .pid_found
  634. .endloop:
  635.         add     ecx, 32
  636.         cmp     ecx, ebx
  637.         jle     .loop
  638.  
  639.         pop     ecx
  640.         pop     ebx
  641.         xor     eax, eax
  642.         ret
  643.  
  644. .pid_found:
  645.         shr     ecx, 5
  646.         mov     eax, ecx ;convert offset to index of slot
  647.         pop     ecx
  648.         pop     ebx
  649.         ret
  650.  
  651. check_region:
  652. ;input:
  653. ;  esi - start of buffer
  654. ;  edx - size of buffer
  655. ;result:
  656. ;  eax = 1 region lays in app memory
  657. ;  eax = 0 region don't lays in app memory
  658.         mov     eax, [CURRENT_TASK]
  659. ;     jmp  check_process_region
  660. ;-----------------------------------------------------------------------------
  661. ;check_process_region:
  662. ;input:
  663. ;  eax - slot
  664. ;  esi - start of buffer
  665. ;  edx - size of buffer
  666. ;result:
  667. ;  eax = 1 region lays in app memory
  668. ;  eax = 0 region don't lays in app memory
  669.  
  670.         test    edx, edx
  671.         jle     .ok
  672.         shl     eax, 5
  673.         cmp     word [CURRENT_TASK+eax+0xa], 0
  674.         jnz     .failed
  675.         shl     eax, 3
  676.         mov     eax, [SLOT_BASE+eax+0xb8]
  677.         test    eax, eax
  678.         jz      .failed
  679.  
  680.         mov     eax, 1
  681.         ret
  682.  
  683.  
  684. ;    call MEM_Get_Linear_Address
  685. ;    push ebx
  686. ;    push ecx
  687. ;    push edx
  688. ;    mov  edx,ebx
  689. ;    and  edx,not (4096-1)
  690. ;    sub  ebx,edx
  691. ;    add  ecx,ebx
  692. ;    mov  ebx,edx
  693. ;    add  ecx,(4096-1)
  694. ;    and  ecx,not (4096-1)
  695. ;.loop:
  696. ;;eax - linear address of page directory
  697. ;;ebx - current page
  698. ;;ecx - current size
  699. ;    mov  edx,ebx
  700. ;    shr  edx,22
  701. ;    mov  edx,[eax+4*edx]
  702. ;    and  edx,not (4096-1)
  703. ;    test edx,edx
  704. ;    jz   .failed1
  705. ;    push eax
  706. ;    mov  eax,edx
  707. ;    call MEM_Get_Linear_Address
  708. ;    mov  edx,ebx
  709. ;    shr  edx,12
  710. ;    and  edx,(1024-1)
  711. ;    mov  eax,[eax+4*edx]
  712. ;    and  eax,not (4096-1)
  713. ;    test eax,eax
  714. ;    pop  eax
  715. ;    jz   .failed1
  716. ;    add  ebx,4096
  717. ;    sub  ecx,4096
  718. ;    jg   .loop
  719. ;    pop  edx
  720. ;    pop  ecx
  721. ;    pop  ebx
  722. .ok:
  723.         mov     eax, 1
  724.         ret
  725. ;
  726. ;.failed1:
  727. ;    pop  edx
  728. ;    pop  ecx
  729. ;    pop  ebx
  730. .failed:
  731.         xor     eax, eax
  732.         ret
  733.  
  734. align 4
  735. proc read_process_memory
  736. ;Input:
  737. ;  eax - process slot
  738. ;  ecx - buffer address
  739. ;  edx - buffer size
  740. ;  esi - start address in other process
  741. ;Output:
  742. ;  eax - number of bytes read.
  743.        locals
  744.          slot   dd ?
  745.          buff   dd ?
  746.          r_count    dd ?
  747.          offset dd ?
  748.          tmp_r_cnt  dd ?
  749.        endl
  750.  
  751.         mov     [slot], eax
  752.         mov     [buff], ecx
  753.         and     [r_count], 0
  754.         mov     [tmp_r_cnt], edx
  755.         mov     [offset], esi
  756.  
  757.         pushad
  758. .read_mem:
  759.         mov     edx, [offset]
  760.         mov     ebx, [tmp_r_cnt]
  761.  
  762.         mov     ecx, 0x400000
  763.         and     edx, 0x3FFFFF
  764.         sub     ecx, edx
  765.         cmp     ecx, ebx
  766.         jbe     @f
  767.         mov     ecx, ebx
  768. @@:
  769.         cmp     ecx, 0x8000
  770.         jna     @F
  771.         mov     ecx, 0x8000
  772. @@:
  773.         mov     ebx, [offset]
  774.  
  775.         push    ecx
  776.         stdcall map_memEx, [proc_mem_map], \
  777.                 [slot], ebx, ecx, PG_MAP
  778.         pop     ecx
  779.  
  780.         mov     esi, [offset]
  781.         and     esi, 0xfff
  782.         sub     eax, esi
  783.         jbe     .ret
  784.         cmp     ecx, eax
  785.         jbe     @f
  786.         mov     ecx, eax
  787.         mov     [tmp_r_cnt], eax
  788. @@:
  789.         add     esi, [proc_mem_map]
  790.         mov     edi, [buff]
  791.         mov     edx, ecx
  792.         rep movsb
  793.         add     [r_count], edx
  794.  
  795.         add     [offset], edx
  796.         sub     [tmp_r_cnt], edx
  797.         jnz     .read_mem
  798. .ret:
  799.         popad
  800.         mov     eax, [r_count]
  801.         ret
  802. endp
  803.  
  804. align 4
  805. proc write_process_memory
  806. ;Input:
  807. ;  eax - process slot
  808. ;  ecx - buffer address
  809. ;  edx - buffer size
  810. ;  esi - start address in other process
  811. ;Output:
  812. ;  eax - number of bytes written
  813.  
  814.        locals
  815.          slot   dd ?
  816.          buff   dd ?
  817.          w_count    dd ?
  818.          offset dd ?
  819.          tmp_w_cnt  dd ?
  820.        endl
  821.  
  822.         mov     [slot], eax
  823.         mov     [buff], ecx
  824.         and     [w_count], 0
  825.         mov     [tmp_w_cnt], edx
  826.         mov     [offset], esi
  827.  
  828.         pushad
  829. .read_mem:
  830.         mov     edx, [offset]
  831.         mov     ebx, [tmp_w_cnt]
  832.  
  833.         mov     ecx, 0x400000
  834.         and     edx, 0x3FFFFF
  835.         sub     ecx, edx
  836.         cmp     ecx, ebx
  837.         jbe     @f
  838.         mov     ecx, ebx
  839. @@:
  840.         cmp     ecx, 0x8000
  841.         jna     @F
  842.         mov     ecx, 0x8000
  843. @@:
  844.         mov     ebx, [offset]
  845.       ;     add ebx, new_app_base
  846.         push    ecx
  847.         stdcall map_memEx, [proc_mem_map], \
  848.                 [slot], ebx, ecx, PG_SW
  849.         pop     ecx
  850.  
  851.         mov     edi, [offset]
  852.         and     edi, 0xfff
  853.         sub     eax, edi
  854.         jbe     .ret
  855.         cmp     ecx, eax
  856.         jbe     @f
  857.         mov     ecx, eax
  858.         mov     [tmp_w_cnt], eax
  859. @@:
  860.         add     edi, [proc_mem_map]
  861.         mov     esi, [buff]
  862.         mov     edx, ecx
  863.         rep movsb
  864.  
  865.         add     [w_count], edx
  866.         add     [offset], edx
  867.         sub     [tmp_w_cnt], edx
  868.         jnz     .read_mem
  869. .ret:
  870.         popad
  871.         mov     eax, [w_count]
  872.         ret
  873. endp
  874.  
  875. align 4
  876. proc new_sys_threads
  877.        locals
  878.          slot      dd ?
  879.          app_cmdline   dd ? ;0x00
  880.          app_path      dd ? ;0x04
  881.          app_eip       dd ? ;0x08
  882.          app_esp       dd ? ;0x0C
  883.          app_mem       dd ? ;0x10
  884.        endl
  885.  
  886.         cmp     ebx, 1
  887.         jne     .failed     ;other subfunctions
  888.  
  889.         xor     eax, eax
  890.         mov     [app_eip], ecx
  891.         mov     [app_cmdline], eax
  892.         mov     [app_esp], edx
  893.         mov     [app_path], eax
  894.        ;mov    esi,new_process_loading
  895.        ;call   sys_msg_board_str
  896. .wait_lock:
  897.         cmp     [application_table_status], 0
  898.         je      .get_lock
  899.         call    change_task
  900.         jmp     .wait_lock
  901.  
  902. .get_lock:
  903.         mov     eax, 1
  904.         xchg    eax, [application_table_status]
  905.         test    eax, eax
  906.         jnz     .wait_lock
  907.  
  908.         call    set_application_table_status
  909.  
  910.         call    get_new_process_place
  911.         test    eax, eax
  912.         jz      .failed
  913.  
  914.         mov     [slot], eax
  915.  
  916.         mov     esi, [current_slot]
  917.         mov     ebx, esi      ;ebx=esi - pointer to extended information about current thread
  918.  
  919.         mov     edi, eax
  920.         shl     edi, 8
  921.         add     edi, SLOT_BASE
  922.         mov     edx, edi      ;edx=edi - pointer to extended infomation about new thread
  923.         mov     ecx, 256/4
  924.         xor     eax, eax
  925.         cld
  926.         rep stosd             ;clean extended information about new thread
  927.         mov     esi, ebx
  928.         mov     edi, edx
  929.         mov     ecx, 11
  930.         rep movsb             ;copy process name
  931.  
  932.         mov     eax, [ebx+APPDATA.heap_base]
  933.         mov     [edx+APPDATA.heap_base], eax
  934.  
  935.         mov     ecx, [ebx+APPDATA.heap_top]
  936.         mov     [edx+APPDATA.heap_top], ecx
  937.  
  938.         mov     eax, [ebx+APPDATA.mem_size]
  939.         mov     [edx+APPDATA.mem_size], eax
  940.  
  941.         mov     ecx, [ebx+APPDATA.dir_table]
  942.         mov     [edx+APPDATA.dir_table], ecx;copy page directory
  943.  
  944.         mov     eax, [ebx+APPDATA.dlls_list_ptr]
  945.         mov     [edx+APPDATA.dlls_list_ptr], eax
  946.  
  947.         mov     eax, [ebx+APPDATA.tls_base]
  948.         test    eax, eax
  949.         jz      @F
  950.  
  951.         push    edx
  952.         stdcall user_alloc, 4096
  953.         pop     edx
  954.         test    eax, eax
  955.         jz      .failed1;eax=0
  956. @@:
  957.         mov     [edx+APPDATA.tls_base], eax
  958.  
  959.         lea     eax, [app_cmdline]
  960.         stdcall set_app_params , [slot], eax, dword 0, \
  961.                 dword 0,dword 0
  962.  
  963.        ;mov    esi,new_process_running
  964.        ;call   sys_msg_board_str                ;output information about succefull startup
  965.         xor     eax, eax
  966.         mov     [application_table_status], eax ;unlock application_table_status mutex
  967.         mov     eax, [process_number]           ;set result
  968.         ret
  969. .failed:
  970.         xor     eax, eax
  971. .failed1:
  972.         mov     [application_table_status], eax
  973.         dec     eax     ;-1
  974.         ret
  975. endp
  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.