Subversion Repositories Kolibri OS

Rev

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

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