Subversion Repositories Kolibri OS

Rev

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

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