Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2004-2021. All rights reserved. ;;
  4. ;;  Distributed under terms of the GNU General Public License.  ;;
  5. ;;                                                              ;;
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7.  
  8. $Revision: 9248 $
  9.  
  10.  
  11. GREEDY_KERNEL  = 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. struct  APP_HDR
  34.         cmdline         rd 1    ;0x00
  35.         path            rd 1    ;0x04
  36.         eip             rd 1    ;0x08
  37.         esp             rd 1    ;0x0C
  38.         _edata          rd 1    ;0x10
  39.         _emem           rd 1    ;0x14
  40.         img_base        rd 1    ;0x18
  41.         img_size        rd 1
  42.         filename_size   rd 1
  43.         cmdline_size    rd 1
  44.         path_string     rd 1
  45. ends
  46.  
  47. macro _clear_ op
  48. {  mov ecx, op/4
  49.         xor     eax, eax
  50.         cld
  51.         rep stosd
  52. }
  53.  
  54. align 4
  55. _strnlen:
  56.         mov     edx, ecx
  57.         xor     eax, eax
  58.         repne scasb
  59.         jne     @F
  60.         inc     ecx
  61. @@:
  62.         mov     eax, edx
  63.         sub     eax, ecx
  64.         retn
  65.  
  66. fs_execute_from_sysdir:
  67.         xor     ebx, ebx
  68. fs_execute_from_sysdir_param:
  69.         stdcall kernel_alloc, maxPathLength
  70.         push    eax ebx
  71.         mov     esi, ebp
  72.         mov     edi, eax
  73.         xor     eax, eax
  74.         call    getFullPath
  75.         pop     ecx ebx
  76.         xor     edx, edx
  77. ; @brief Executes a program
  78. ; @param edx Flags
  79. ; @param ecx Commandline
  80. ; @param ebx Absolute file path
  81. ; @param eax String length
  82. ; @returns Negated error code or new process number
  83. proc fs_execute
  84.     locals
  85.         cmdline         rd  1
  86.         flags           rd  1
  87.         slot            rd  1  ; index of new thread slot
  88.         slot_base       rd  1  ; base address of it
  89. ; app header data
  90.         hdr_cmdline     rd  1
  91.         hdr_path        rd  1
  92.         hdr_eip         rd  1
  93.         hdr_esp         rd  1
  94.         hdr_edata       rd  1
  95.         hdr_emem        rd  1
  96.         file_base       rd  1
  97.         file_size       rd  1
  98.         filename_size   rd  1
  99.         cmdline_size    rd  1
  100.         path_string     rd  1
  101.     endl
  102.  
  103.         and     edx, 1  ;clear flags user. TODO: add api for start apps with user flags
  104.         mov     [flags], edx
  105.         mov     [cmdline], ecx
  106.         mov     [path_string], ebx
  107.         mov     [filename_size], eax
  108.         mov     esi, -ERROR_FILE_NOT_FOUND
  109.         test    eax, eax
  110.         jz      .err_file
  111.         stdcall load_file, ebx
  112.         test    eax, eax
  113.         jz      .err_file
  114.  
  115.         mov     [file_base], eax
  116.         mov     [file_size], ebx
  117.         lea     ebx, [hdr_cmdline]
  118.         call    test_app_header  ; fill our app header data locals with values from header of given program (if its correct)
  119.         mov     esi, -TASKMAN_ERROR_NOT_A_EXECUTABLE
  120.         test    eax, eax
  121.         jz      .err_hdr
  122.  
  123.         call    lock_application_table
  124.         call    alloc_thread_slot   ; create a slot for new thread
  125.         mov     esi, -TASKMAN_ERROR_TOO_MANY_PROCESSES
  126.         test    eax, eax
  127.         jz      .err_0
  128.  
  129.         mov     [slot], eax
  130.         shl     eax, 8
  131.         lea     edi, [SLOT_BASE+eax]
  132.         mov     [slot_base], edi
  133. ; clean extended information about process
  134.         mov     ecx, sizeof.APPDATA/4
  135.         xor     eax, eax
  136.         cld
  137.         rep stosd
  138. ; write application name ( APPDATA.appname )
  139.         stdcall strrchr, [path_string], '/'
  140.         lea     esi, [eax+1]    ; -> name without path
  141.         mov     ecx, 11
  142.         mov     edi, [slot_base]
  143. @@:
  144.         call    utf8to16
  145.         call    uni2ansi_char
  146.         cmp     al, '.'
  147.         jz      @f
  148.         test    al, al
  149.         jz      @f
  150.         stosb
  151.         loop    @b
  152. @@:
  153.         mov     edi, [cmdline]
  154.         xor     eax, eax
  155.         test    edi, edi
  156.         jz      @f
  157.         mov     ecx, 65535
  158.         call    _strnlen
  159.         cmp     eax, 256
  160.         jb      @f
  161. ; if cmdline length >= 256 then increase needed memory size by this length
  162.         lea     ebx, [eax+1]
  163.         add     [hdr_emem], ebx
  164. @@:
  165.         mov     [cmdline_size], eax
  166.         stdcall create_process, [hdr_emem]  ; create a new process
  167.         mov     esi, -TASKMAN_ERROR_OUT_OF_MEMORY
  168.         test    eax, eax
  169.         jz      .err_hdr
  170.  
  171. ; add new process to the list
  172.         mov     ebx, [sys_proc+LHEAD.prev]
  173.         __list_add eax, ebx, sys_proc
  174. ; fill the structure fields:
  175.         mov     ebx, [hdr_emem]
  176.         mov     [eax+PROC.mem_used], ebx
  177.  
  178. ; write that main thread of app belongs to new process
  179.         mov     ebx, [slot_base]
  180.         mov     [ebx+APPDATA.process], eax
  181.  
  182. ; initialize the thread list of process: at this moment it consists only of one main thread
  183.         lea     edx, [ebx+APPDATA.list]
  184.         lea     ecx, [eax+PROC.thr_list]
  185.         list_add_tail edx, ecx
  186.  
  187. ; allocate space and copy app header data locals and cmdline string there, put pointer to exec_params of new thread
  188.         mov     eax, [cmdline_size]
  189.         add     eax, sizeof.APP_HDR
  190.         stdcall kernel_alloc, eax
  191.         mov     [ebx+APPDATA.exec_params], eax
  192.         mov     edi, eax
  193.         lea     esi, [hdr_cmdline]
  194.         mov     ecx, sizeof.APP_HDR/4
  195.         rep movsd
  196.         mov     ecx, [cmdline_size]
  197.         mov     esi, [cmdline]
  198.         rep movsb
  199. ; set other parameters of application
  200.         lea     eax, [hdr_cmdline]
  201.         stdcall set_app_params , [slot], eax, [flags]
  202.         mov     eax, [process_number]   ; return process number
  203.         call    unlock_application_table
  204.         ret
  205.  
  206. .err_0:
  207.         call    unlock_application_table
  208. .err_hdr:
  209.         stdcall kernel_free, [file_base]
  210. .err_file:
  211.         stdcall kernel_free, [path_string]
  212.         mov     eax, esi
  213.         ret
  214. endp
  215.  
  216. align 4
  217. test_app_header:
  218.        virtual at eax
  219.          APP_HEADER_00 APP_HEADER_00_
  220.        end virtual
  221.        virtual at eax
  222.          APP_HEADER_01 APP_HEADER_01_
  223.        end virtual
  224.  
  225.         cmp     dword [eax], 'MENU'
  226.         jne     .fail
  227.         cmp     word [eax+4], 'ET'
  228.         jne     .fail
  229.  
  230.         cmp     [eax+6], word '00'
  231.         jne     .check_01_header
  232.  
  233.         mov     ecx, [APP_HEADER_00.start]
  234.         mov     [ebx+APP_HDR.eip], ecx
  235.         mov     edx, [APP_HEADER_00.mem_size]
  236.         mov     [ebx+APP_HDR._emem], edx
  237.         shr     edx, 1
  238.         sub     edx, 0x10
  239.         mov     [ebx+APP_HDR.esp], edx
  240.         mov     ecx, [APP_HEADER_00.i_param]
  241.         mov     [ebx+APP_HDR.cmdline], ecx
  242.         mov     [ebx+APP_HDR.path], 0
  243.         mov     edx, [APP_HEADER_00.i_end]
  244.         mov     [ebx+APP_HDR._edata], edx
  245.         ret
  246.  
  247.  .check_01_header:
  248.  
  249.         cmp     [eax+6], word '01'
  250.         je      @f
  251.         cmp     [eax+6], word '02'
  252.         jne     .fail
  253. @@:
  254.         mov     ecx, [APP_HEADER_01.start]
  255.         mov     [ebx+0x08], ecx
  256.         mov     edx, [APP_HEADER_01.mem_size]
  257.  
  258. ; \begin{diamond}[20.08.2006]
  259. ; sanity check (functions 19,58 load app_i_end bytes and that must
  260. ; fit in allocated memory to prevent kernel faults)
  261.         cmp     edx, [APP_HEADER_01.i_end]
  262.         jb      .fail
  263. ; \end{diamond}[20.08.2006]
  264.  
  265.         mov     [ebx+APP_HDR._emem], edx
  266.         mov     ecx, [APP_HEADER_01.stack_top]
  267.         mov     [ebx+APP_HDR.esp], ecx
  268.         mov     edx, [APP_HEADER_01.i_param]
  269.         mov     [ebx+APP_HDR.cmdline], edx
  270.         mov     ecx, [APP_HEADER_01.i_icon]
  271.         mov     [ebx+APP_HDR.path], ecx
  272.         mov     edx, [APP_HEADER_01.i_end]
  273.         mov     [ebx+APP_HDR._edata], edx
  274.         ret
  275. .fail:
  276.         xor     eax, eax
  277.         ret
  278.  
  279. align 4
  280. alloc_thread_slot:
  281. ;input:
  282. ;  none
  283. ;result:
  284. ;  eax=[new_thread_slot]<>0 - ok
  285. ;      0 - failed.
  286. ;This function find least empty slot.
  287. ;It doesn't increase [thread_count]!
  288.  
  289.  
  290.         mov     edx, thr_slot_map
  291.         pushfd
  292.         cli
  293. .l1:
  294.         bsf     eax, [edx]
  295.         jnz     .found
  296.         add     edx, 4
  297.         cmp     edx, thr_slot_map+32
  298.         jb      .l1
  299.  
  300.         popfd
  301.         xor     eax, eax
  302.         ret
  303. .found:
  304.         btr     [edx], eax
  305.         sub     edx, thr_slot_map
  306.         lea     eax, [eax+edx*8]
  307.         popfd
  308.         ret
  309.  
  310. align 4
  311. proc create_process stdcall, app_size:dword
  312.        locals
  313.          process     dd ?
  314.          app_tabs    dd ?
  315.        endl
  316.  
  317.         push    ebx
  318.         push    esi
  319.         push    edi
  320.  
  321.         xor     eax, eax
  322.         mov     [process], eax
  323.  
  324.         mov     eax, [app_size]
  325.         add     eax, 0x3FFFFF
  326.         shr     eax, 22
  327.         mov     [app_tabs], eax
  328.  
  329.         stdcall kernel_alloc, 0x2000
  330.         test    eax, eax
  331.         jz      .fail
  332.         mov     [process], eax
  333.  
  334.         lea     edi, [eax+PROC.heap_lock]
  335.         mov     ecx, (PROC.ht_free-PROC.heap_lock)/4
  336.  
  337.         list_init eax
  338.         add     eax, PROC.thr_list
  339.         list_init eax
  340.  
  341.         xor     eax, eax
  342.         cld
  343.         rep stosd
  344.  
  345.         mov     [edi], dword (PROC.pdt_0 - PROC.htab)/4 - 3
  346.         mov     [edi+4], dword 3           ;reserve handles for stdin stdout and stderr
  347.         mov     ecx, (PROC.pdt_0 - PROC.htab)/4
  348.         add     edi, 8
  349.         inc     eax
  350. @@:
  351.         stosd
  352.         inc     eax
  353.         cmp     eax, ecx
  354.         jbe     @B
  355.  
  356.         mov     eax, edi
  357.         call    get_pg_addr
  358.         mov     [edi-4096+PROC.pdt_0_phys], eax
  359.  
  360.         mov     ecx, (OS_BASE shr 20)/4
  361.         xor     eax, eax
  362.         rep stosd
  363.  
  364.         mov     ecx, (OS_BASE shr 20)/4
  365.         mov     esi, sys_proc+PROC.pdt_0+(OS_BASE shr 20)
  366.         rep movsd
  367.  
  368.         mov     eax, [edi-8192+PROC.pdt_0_phys]
  369.         or      eax, PG_SWR
  370.         mov     [edi-4096+(page_tabs shr 20)], eax
  371.  
  372.         lea     edx, [edi-4096]
  373.         mov     esi, [app_tabs]
  374.  
  375. .alloc_page_dir:
  376.         call    alloc_page
  377.         test    eax, eax
  378.         jz      .fail
  379.         or      eax, PG_UWR
  380.         mov     [edx], eax
  381.  
  382.         mov     edi, [tmp_task_ptab]
  383.         stdcall map_page, edi, eax, PG_SWR
  384.         mov     ecx, 1024
  385.         xor     eax, eax
  386.         rep stosd
  387.  
  388.         add     edx, 4
  389.         dec     esi
  390.         jnz     .alloc_page_dir
  391.  
  392.         stdcall map_page, [tmp_task_ptab], 0, PG_UNMAP
  393.         mov     eax, [process]
  394.  
  395.         pop     edi
  396.         pop     esi
  397.         pop     ebx
  398.         ret
  399. .fail:
  400.         mov     ecx, [process]
  401.         jcxz    @F
  402.  
  403.         call    destroy_process
  404. @@:
  405.         xor     eax, eax
  406.         pop     edi
  407.         pop     esi
  408.         pop     ebx
  409.         ret
  410. endp
  411.  
  412. align 4
  413. proc destroy_page_table stdcall, pg_tab:dword
  414.  
  415.         push    esi
  416.  
  417.         mov     esi, [pg_tab]
  418.         mov     ecx, 1024
  419. .free:
  420.         mov     eax, [esi]
  421.         test    eax, 1
  422.         jz      .next
  423.         test    eax, 2
  424.         jz      .next
  425.         test    eax, 1 shl 9
  426.         jnz     .next                     ;skip shared pages
  427.         call    free_page
  428. .next:
  429.         add     esi, 4
  430.         dec     ecx
  431.         jnz     .free
  432.         pop     esi
  433.         ret
  434. endp
  435.  
  436. align 4
  437. destroy_process: ;fastcall ecx= ptr to process
  438.  
  439.         lea     eax, [ecx+PROC.thr_list]
  440.         cmp     eax, [eax+LHEAD.next]
  441.         jne     .exit
  442.  
  443. align 4
  444. .internal:
  445.         push    ecx
  446.  
  447.         mov     esi, ecx
  448.         list_del esi
  449.  
  450.         mov     esi, [esi+PROC.dlls_list_ptr]
  451.         call    destroy_all_hdlls
  452.  
  453.         mov     esi, [esp]
  454.         add     esi, PROC.pdt_0
  455.         mov     edi, (0x80000000 shr 20)/4
  456. .destroy:
  457.         mov     eax, [esi]
  458.         test    eax, 1
  459.         jz      .next
  460.         and     eax, not 0xFFF
  461.         stdcall map_page, [tmp_task_ptab], eax, PG_SWR
  462.         stdcall destroy_page_table, [tmp_task_ptab]
  463.         mov     eax, [esi]
  464.         call    free_page
  465. .next:
  466.         add     esi, 4
  467.         dec     edi
  468.         jnz     .destroy
  469.  
  470.         call    kernel_free     ;ecx still in stack
  471.         stdcall map_page, [tmp_task_ptab], 0, PG_UNMAP
  472. .exit:
  473.         ret
  474.  
  475. align 4
  476. get_pid:
  477.         mov     eax, [TASK_BASE]
  478.         mov     eax, [eax+TASKDATA.pid]
  479.         ret
  480.  
  481. pid_to_slot:
  482. ;Input:
  483. ;  eax - pid of process
  484. ;Output:
  485. ;  eax - slot of process or 0 if process don't exists
  486. ;Search process by PID.
  487.         push    ebx
  488.         push    ecx
  489.         mov     ebx, [thread_count]
  490.         shl     ebx, BSF sizeof.TASKDATA ; multiply by size
  491.         ; add 2*32 cause:
  492.         ; [TASK_TABLE; TASK_TABLE + 32) isnt a task actually
  493.         ; skip first process in the task table
  494.         mov     ecx, 2*32
  495.  
  496. .loop:
  497. ;ecx = offset of current process info entry
  498. ;ebx = maximum permitted offset
  499.         cmp     [TASK_TABLE+ecx+TASKDATA.state], TSTATE_FREE
  500.         jz      .endloop ;skip empty slots
  501.         cmp     [TASK_TABLE+ecx+TASKDATA.pid], eax;check PID
  502.         jz      .pid_found
  503. .endloop:
  504.         add     ecx, sizeof.TASKDATA
  505.         cmp     ecx, ebx
  506.         jle     .loop
  507.  
  508.         pop     ecx
  509.         pop     ebx
  510.         xor     eax, eax
  511.         ret
  512.  
  513. .pid_found:
  514.         shr     ecx, BSF sizeof.TASKDATA ; divide by size
  515.         mov     eax, ecx ;convert offset to index of slot
  516.         pop     ecx
  517.         pop     ebx
  518.         ret
  519.  
  520.  
  521. align 4
  522. proc read_process_memory
  523. ;Input:
  524. ;  eax - process slot
  525. ;  ecx - buffer address
  526. ;  edx - buffer size
  527. ;  esi - start address in other process
  528. ;Output:
  529. ;  eax - number of bytes read.
  530.        locals
  531.          slot   dd ?
  532.          buff   dd ?
  533.          r_count    dd ?
  534.          offset dd ?
  535.          tmp_r_cnt  dd ?
  536.        endl
  537.  
  538.         mov     [slot], eax
  539.         mov     [buff], ecx
  540.         and     [r_count], 0
  541.         mov     [tmp_r_cnt], edx
  542.         mov     [offset], esi
  543.  
  544.         pushad
  545. .read_mem:
  546.         mov     edx, [offset]
  547.         mov     ebx, [tmp_r_cnt]
  548.  
  549.         mov     ecx, 0x400000
  550.         and     edx, 0x3FFFFF
  551.         sub     ecx, edx
  552.         cmp     ecx, ebx
  553.         jbe     @f
  554.         mov     ecx, ebx
  555. @@:
  556.         cmp     ecx, 0x8000
  557.         jna     @F
  558.         mov     ecx, 0x8000
  559. @@:
  560.         mov     ebx, [offset]
  561.  
  562.         push    ecx
  563.         stdcall map_memEx, [proc_mem_map], \
  564.                 [slot], ebx, ecx, PG_READ
  565.         pop     ecx
  566.  
  567.         mov     esi, [offset]
  568.         and     esi, 0xfff
  569.         sub     eax, esi
  570.         jbe     .ret
  571.         cmp     ecx, eax
  572.         jbe     @f
  573.         mov     ecx, eax
  574.         mov     [tmp_r_cnt], eax
  575. @@:
  576.         add     esi, [proc_mem_map]
  577.         mov     edi, [buff]
  578.         mov     edx, ecx
  579.         rep movsb
  580.         add     [r_count], edx
  581.  
  582.         add     [offset], edx
  583.         sub     [tmp_r_cnt], edx
  584.         jnz     .read_mem
  585. .ret:
  586.         popad
  587.         mov     eax, [r_count]
  588.         ret
  589. endp
  590.  
  591. align 4
  592. proc write_process_memory
  593. ;Input:
  594. ;  eax - process slot
  595. ;  ecx - buffer address
  596. ;  edx - buffer size
  597. ;  esi - start address in other process
  598. ;Output:
  599. ;  eax - number of bytes written
  600.  
  601.        locals
  602.          slot   dd ?
  603.          buff   dd ?
  604.          w_count    dd ?
  605.          offset dd ?
  606.          tmp_w_cnt  dd ?
  607.        endl
  608.  
  609.         mov     [slot], eax
  610.         mov     [buff], ecx
  611.         and     [w_count], 0
  612.         mov     [tmp_w_cnt], edx
  613.         mov     [offset], esi
  614.  
  615.         pushad
  616. .read_mem:
  617.         mov     edx, [offset]
  618.         mov     ebx, [tmp_w_cnt]
  619.  
  620.         mov     ecx, 0x400000
  621.         and     edx, 0x3FFFFF
  622.         sub     ecx, edx
  623.         cmp     ecx, ebx
  624.         jbe     @f
  625.         mov     ecx, ebx
  626. @@:
  627.         cmp     ecx, 0x8000
  628.         jna     @F
  629.         mov     ecx, 0x8000
  630. @@:
  631.         mov     ebx, [offset]
  632.         push    ecx
  633.         stdcall map_memEx, [proc_mem_map], \
  634.                 [slot], ebx, ecx, PG_SWR
  635.         pop     ecx
  636.  
  637.         mov     edi, [offset]
  638.         and     edi, 0xfff
  639.         sub     eax, edi
  640.         jbe     .ret
  641.         cmp     ecx, eax
  642.         jbe     @f
  643.         mov     ecx, eax
  644.         mov     [tmp_w_cnt], eax
  645. @@:
  646.         add     edi, [proc_mem_map]
  647.         mov     esi, [buff]
  648.         mov     edx, ecx
  649.         rep movsb
  650.  
  651.         add     [w_count], edx
  652.         add     [offset], edx
  653.         sub     [tmp_w_cnt], edx
  654.         jnz     .read_mem
  655. .ret:
  656.         popad
  657.         mov     eax, [w_count]
  658.         ret
  659. endp
  660.  
  661. ;ebx = 1 - kernel thread
  662. ;ecx=thread entry point
  663. ;edx=thread stack pointer
  664. ;creation flags  0x01 - debugged
  665. ;                0x02 - kernel
  666.  
  667. align 4
  668. proc new_sys_threads
  669.        locals
  670.          slot          dd ?
  671.          flags         dd ?
  672.          app_cmdline   dd ? ;0x00
  673.          app_path      dd ? ;0x04
  674.          app_eip       dd ? ;0x08
  675.          app_esp       dd ? ;0x0C
  676.          app_mem       dd ? ;0x10
  677.        endl
  678.  
  679.         shl     ebx, 1
  680.         mov     [flags], ebx
  681.  
  682.         xor     eax, eax
  683.         mov     [app_eip], ecx
  684.         mov     [app_cmdline], eax
  685.         mov     [app_esp], edx
  686.         mov     [app_path], eax
  687.  
  688.         call    lock_application_table
  689.  
  690.         call    alloc_thread_slot
  691.         test    eax, eax
  692.         jz      .failed
  693.  
  694.         mov     [slot], eax
  695.  
  696.         mov     esi, [current_slot]
  697.         mov     ebx, esi      ;ebx=esi - pointer to extended information about current thread
  698.  
  699.         mov     edi, eax
  700.         shl     edi, 8
  701.         add     edi, SLOT_BASE
  702.         mov     edx, edi      ;edx=edi - pointer to extended infomation about new thread
  703.         mov     ecx, sizeof.APPDATA/4
  704.         xor     eax, eax
  705.         cld
  706.         rep stosd             ;clean extended information about new thread
  707.         mov     esi, ebx
  708.         mov     edi, edx
  709.         mov     ecx, 11
  710.         rep movsb             ;copy process name
  711.  
  712.  
  713.         mov     eax, [ebx+APPDATA.tls_base]
  714.         test    eax, eax
  715.         jz      @F
  716.  
  717.         push    edx
  718.         stdcall user_alloc, 4096
  719.         pop     edx
  720.         test    eax, eax
  721.         jz      .failed1;eax=0
  722. @@:
  723.         mov     [edx+APPDATA.tls_base], eax
  724.  
  725.         mov     eax, [ebx+APPDATA.process]
  726.         mov     [edx+APPDATA.process], eax
  727.  
  728.         lea     ebx, [edx+APPDATA.list]
  729.         lea     ecx, [eax+PROC.thr_list]
  730.         list_add_tail ebx, ecx               ;add thread to process child's list
  731.  
  732.         lea     eax, [app_cmdline]
  733.         stdcall set_app_params , [slot], eax, [flags]
  734.  
  735.         mov     eax, [process_number]           ;set result
  736.         call    unlock_application_table
  737.         ret
  738. .failed:
  739.         xor     eax, eax
  740. .failed1:
  741.         call    unlock_application_table
  742.         dec     eax     ;-1
  743.         ret
  744. endp
  745.  
  746. proc map_process_image stdcall, img_size:dword, file_base:dword, file_size:dword
  747.  
  748.         mov     edx, [img_size]
  749.         mov     esi, [file_base]
  750.         mov     ecx, [file_size]
  751.         add     edx, 4095
  752.         add     ecx, 4095
  753.         shr     edx, 12        ; total pages
  754.         shr     ecx, 12        ; image pages
  755.  
  756.         mov     edi, page_tabs
  757.         shr     esi, 10
  758.         add     esi, edi
  759.  
  760. .map_image:
  761.         lodsd
  762.         and     eax, -4096
  763.         or      eax, PG_UWR
  764.         stosd
  765.         dec     edx
  766.         loop    .map_image
  767.  
  768.         test    edx, edx
  769.         jz      .done
  770. .map_bss:
  771.         call    alloc_page
  772.         test    eax, eax
  773.         jz      .fail
  774.  
  775.         or      eax, PG_UWR
  776.         stosd
  777.         dec     edx
  778.         jnz     .map_bss
  779.  
  780.         mov     edi, [file_size]
  781.         mov     ecx, [img_size]
  782.         add     edi, 4095
  783.         and     edi, -4096
  784.         add     ecx, 4095
  785.         and     ecx, -4096
  786.         sub     ecx, edi
  787.         shr     ecx, 2
  788.         xor     eax, eax
  789.         rep stosd
  790. .done:
  791. .fail:
  792.         ret
  793. endp
  794.  
  795. align 4
  796. common_app_entry:
  797.         mov     ebp, [current_slot]
  798.         mov     ebp, [ebp+APPDATA.exec_params]
  799.         test    ebp, ebp
  800.         jz      .exit
  801. ; APPDATA.exec_params have first thread only,
  802. ; so second and next threads don't get here (they jump to .exit)
  803.         stdcall map_process_image, [ebp+APP_HDR._emem],\
  804.                 [ebp+APP_HDR.img_base], [ebp+APP_HDR.img_size]
  805.         mov     esi, [ebp+APP_HDR.path_string]
  806.         mov     edi, [ebp+APP_HDR.path]
  807.         mov     ecx, [ebp+APP_HDR.filename_size]
  808.         cmp     ecx, 1023
  809.         jc      @f
  810.         mov     ecx, 1022
  811. @@:
  812.         push    esi
  813.         test    edi, edi
  814.         jz      @f
  815.         stdcall is_region_userspace, edi, [ebp+APP_HDR.filename_size]
  816.         jnz     @f
  817.         mov     al, '/'
  818.         stosb
  819.         rep movsb
  820.         mov     byte [edi], 0
  821. @@:
  822.         call    kernel_free
  823.         mov     edi, [ebp+APP_HDR.cmdline]
  824.         test    edi, edi
  825.         jz      .check_tls_header
  826.         lea     esi, [ebp+sizeof.APP_HDR]
  827.         mov     ecx, [ebp+APP_HDR.cmdline_size]
  828.         cmp     ecx, 256
  829.         jb      .copy_cmdline
  830.         mov     edi, [ebp+APP_HDR._emem]
  831.         add     edi, 4095
  832.         and     edi, -4096
  833.         sub     edi, ecx
  834.         dec     edi
  835.         cmp     word [6], '00'
  836.         jne     @f
  837.         mov     [APP_HEADER_00_.i_param], edi
  838.         jmp     .copy_cmdline
  839. @@:
  840.         mov     [APP_HEADER_01_.i_param], edi
  841. .copy_cmdline:
  842.         inc     ecx  ; keep in mind about 0 in the end
  843.         stdcall is_region_userspace, edi, ecx
  844.         jnz     .check_tls_header
  845.         dec     ecx
  846.         rep movsb
  847.         mov     byte [edi], 0
  848. .check_tls_header:
  849.         cmp     word [6], '02'
  850.         jne     .try_load_dll ;.cleanup
  851.         call    init_heap
  852.         stdcall user_alloc, 4096
  853.         mov     edx, [current_slot]
  854.         mov     [edx+APPDATA.tls_base], eax
  855.         mov     [tls_data_l+2], ax
  856.         shr     eax, 16
  857.         mov     [tls_data_l+4], al
  858.         mov     [tls_data_l+7], ah
  859.         mov     dx, app_tls
  860.         mov     fs, dx      
  861. ; { Patch by Coldy, For DLL autoload    
  862. .try_load_dll:        
  863. ; Test app header version
  864.         mov     ecx, dword[ebp+APP_HDR.img_base]
  865.         cmp     dword[ecx+8], 2
  866.         jne     .cleanup
  867. ;if APP_HEADER.version = 2 => load lib/dll.obj & change eip to APP_STARTUP_THUNK
  868.         DEBUGF 1, 'K : App header version 2\n'
  869.         stdcall load_library, dll_lib_path, 0
  870.         cmp     eax, 0
  871.         jne     @f
  872. ; Something went wrong (TODO: Next 2 line is code copy after .cleanup)  
  873.         stdcall free_kernel_space, [ebp+APP_HDR.img_base]
  874.         stdcall kernel_free, ebp
  875.         DEBUGF 1, 'K : DLL.OBJ not found! Terminate application!\n'
  876.         mov     ebx, dll_error_msg
  877.         mov     ebp, notifyapp
  878.         call    fs_execute_from_sysdir_param
  879. ; Terminate process (TODO: Need jump to .cleanup after sys_end ?)
  880.         call    sys_end
  881.        
  882. @@:      
  883. ; Find APP_STARTUP_THUNK in DLL.OBJ
  884.         sub     eax, 4
  885.         mov     eax, [eax]
  886.        
  887. ;.change_eip:
  888.         mov     ecx, [current_slot]
  889.         mov     ecx, [ecx+APPDATA.pl0_stack]
  890.         mov     [ecx+REG_EIP], eax
  891.        
  892. ; } End patch by Coldy, For DLL autoload
  893. .cleanup:
  894.         stdcall free_kernel_space, [ebp+APP_HDR.img_base]
  895.         stdcall kernel_free, ebp
  896.         mov     ebx, [current_slot]
  897.         cmp     [ebx+APPDATA.debugger_slot], 0
  898.         je      .exit
  899.         mov     eax, [TASK_BASE]
  900.         mov     [eax+TASKDATA.state], TSTATE_RUN_SUSPENDED
  901.         call    change_task
  902. .exit:
  903.         popad
  904.         iretd
  905.  
  906. EFL_IF      = 0x0200
  907. EFL_IOPL1   = 0x1000
  908. EFL_IOPL2   = 0x2000
  909. EFL_IOPL3   = 0x3000
  910.  
  911. align 4
  912. proc set_app_params stdcall,slot:dword, params:dword, flags:dword
  913.  
  914.        locals
  915.          pl0_stack dd ?
  916.        endl
  917.  
  918.         mov     eax, [xsave_area_size]
  919.         add     eax, RING0_STACK_SIZE
  920.         stdcall kernel_alloc, eax
  921.         mov     [pl0_stack], eax
  922.  
  923.         lea     edi, [eax+RING0_STACK_SIZE]
  924.  
  925.         mov     eax, [slot]
  926.         mov     ebx, eax
  927.  
  928.         shl     eax, 8
  929.         mov     [eax+SLOT_BASE+APPDATA.fpu_state], edi
  930.         mov     [eax+SLOT_BASE+APPDATA.exc_handler], 0
  931.         mov     [eax+SLOT_BASE+APPDATA.except_mask], 0
  932.         mov     [eax+SLOT_BASE+APPDATA.terminate_protection], 80000001h
  933.  
  934. ;set default io permission map
  935.         mov     ecx, [SLOT_BASE+sizeof.APPDATA+APPDATA.io_map]
  936.         mov     [eax+SLOT_BASE+APPDATA.io_map], ecx
  937.         mov     ecx, [SLOT_BASE+sizeof.APPDATA+APPDATA.io_map+4]
  938.         mov     [eax+SLOT_BASE+APPDATA.io_map+4], ecx
  939.  
  940.         mov     esi, fpu_data
  941.         mov     ecx, [xsave_area_size]
  942.         add     ecx, 3
  943.         shr     ecx, 2
  944.         rep movsd
  945.  
  946.         cmp     [thread_count], ebx
  947.         adc     [thread_count], 0   ; update number of processes
  948.         shl     ebx, 8
  949.         lea     edx, [ebx+SLOT_BASE+APP_EV_OFFSET]
  950.         mov     [SLOT_BASE+APPDATA.fd_ev+ebx], edx
  951.         mov     [SLOT_BASE+APPDATA.bk_ev+ebx], edx
  952.  
  953.         add     edx, APP_OBJ_OFFSET-APP_EV_OFFSET
  954.         mov     [SLOT_BASE+APPDATA.fd_obj+ebx], edx
  955.         mov     [SLOT_BASE+APPDATA.bk_obj+ebx], edx
  956.  
  957.         mov     ecx, [def_cursor]
  958.         mov     [SLOT_BASE+APPDATA.cursor+ebx], ecx
  959.         mov     eax, [pl0_stack]
  960.         mov     [SLOT_BASE+APPDATA.pl0_stack+ebx], eax
  961.         add     eax, RING0_STACK_SIZE
  962.         mov     [SLOT_BASE+APPDATA.saved_esp0+ebx], eax
  963.  
  964.         push    ebx
  965.         stdcall kernel_alloc, maxPathLength
  966.         pop     ebx
  967.         mov     esi, [current_slot]
  968.         mov     esi, [esi+APPDATA.cur_dir]
  969.         mov     ecx, maxPathLength/4
  970.         mov     edi, eax
  971.         mov     [ebx+SLOT_BASE+APPDATA.cur_dir], eax
  972.         rep movsd
  973.  
  974.         shr     ebx, 3
  975.         mov     dword [TASK_TABLE+ebx+TASKDATA.mem_start], 0
  976.  
  977.         mov     ebx, [slot]
  978.         mov     eax, ebx
  979.         shl     ebx, 5
  980.         lea     ecx, [draw_data+ebx];ecx - pointer to draw data
  981.  
  982. ; set window state to 'normal' (non-minimized/maximized/rolled-up) state
  983.         mov     [ebx+window_data+WDATA.fl_wstate], WSTATE_NORMAL
  984.         mov     [ebx+window_data+WDATA.fl_redraw], 1
  985.         add     ebx, TASK_TABLE     ;ebx - pointer to information about process
  986.         mov     [ebx+TASKDATA.wnd_number], al;set window number on screen = process slot
  987.  
  988.         mov     [ebx+TASKDATA.event_mask], dword 1+2+4;set default event flags (see 40 function)
  989.  
  990.         inc     dword [process_number]
  991.         mov     eax, [process_number]
  992.         mov     [ebx+TASKDATA.pid], eax    ;set PID
  993.  
  994. ;set draw data to full screen
  995.         xor     eax, eax
  996.         mov     [ecx+0], dword eax
  997.         mov     [ecx+4], dword eax
  998.         mov     eax, [screen_workarea.right]
  999.         mov     [ecx+8], eax
  1000.         mov     eax, [screen_workarea.bottom]
  1001.         mov     [ecx+12], eax
  1002.  
  1003.         mov     ebx, [pl0_stack]
  1004.         mov     esi, [params]
  1005.         lea     ecx, [ebx+REG_EIP]
  1006.         xor     eax, eax
  1007.  
  1008.         mov     [ebx+REG_RET], dword common_app_entry
  1009.         mov     [ebx+REG_EDI], eax
  1010.         mov     [ebx+REG_ESI], eax
  1011.         mov     [ebx+REG_EBP], eax
  1012.         mov     [ebx+REG_ESP], ecx;ebx+REG_EIP
  1013.         mov     [ebx+REG_EBX], eax
  1014.         mov     [ebx+REG_EDX], eax
  1015.         mov     [ebx+REG_ECX], eax
  1016.         mov     [ebx+REG_EAX], eax
  1017.  
  1018.         mov     eax, [esi+APP_HDR.eip]
  1019.         mov     [ebx+REG_EIP], eax
  1020.         mov     [ebx+REG_CS], dword app_code
  1021.         mov     ecx, USER_PRIORITY
  1022.  
  1023.         test    byte [flags], 2
  1024.         jz      @F
  1025.  
  1026.         mov     [ebx+REG_CS], dword os_code ; kernel thread
  1027.         mov     ecx, MAX_PRIORITY
  1028. @@:
  1029.         mov     [ebx+REG_EFLAGS], dword EFL_IOPL1+EFL_IF
  1030.  
  1031.         mov     eax, [esi+APP_HDR.esp]
  1032.         mov     [ebx+REG_APP_ESP], eax
  1033.         mov     [ebx+REG_SS], dword app_data
  1034.  
  1035.         lea     edx, [ebx+REG_RET]
  1036.         mov     ebx, [slot]
  1037.         shl     ebx, 5
  1038.         mov     [ebx*8+SLOT_BASE+APPDATA.saved_esp], edx
  1039.  
  1040.         xor     edx, edx; process state - running
  1041. ; set if debuggee
  1042.         test    byte [flags], 1
  1043.         jz      .no_debug
  1044.         mov     eax, [current_slot_idx]
  1045.         mov     [SLOT_BASE+ebx*8+APPDATA.debugger_slot], eax
  1046. .no_debug:
  1047.         mov     [TASK_TABLE+ebx+TASKDATA.state], dl
  1048.         lea     edx, [SLOT_BASE+ebx*8]
  1049.         call    scheduler_add_thread
  1050.         ret
  1051. endp
  1052.  
  1053. align 4
  1054. get_stack_base:
  1055.         mov     eax, [current_slot]
  1056.         mov     eax, [eax+APPDATA.pl0_stack]
  1057.         ret
  1058.  
  1059.  
  1060. include "debug.inc"
  1061.