Subversion Repositories Kolibri OS

Rev

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

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