Subversion Repositories Kolibri OS

Rev

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