Subversion Repositories Kolibri OS

Rev

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