Subversion Repositories Kolibri OS

Rev

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