Subversion Repositories Kolibri OS

Rev

Rev 130 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. if ~defined newprocess_inc
  2. newprocess_inc_fix:
  3. newprocess_inc fix newprocess_inc_fix
  4. include "mem.inc"
  5. include "memmanag.inc"
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7. ;;Working with new types of processes.
  8. ;;Author: Khalyavin Andrey halyavin@land.ru
  9. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  10. iglobal
  11.     new_process_loading db 'K : New Process - loading',13,10,0
  12.     new_process_running db 'K : New Process - done',13,10,0
  13.     start_not_enough_memory db 'K : New Process - not enough memory',13,10,0
  14. endg
  15. ;-----------------------------------------------------------------------------
  16.  
  17. find_new_process_place:
  18. ;input:
  19. ;  none
  20. ;result:
  21. ;  eax=[new_process_place]<>0 - ok
  22. ;      0 - failed.
  23. ;This function find least empty slot.
  24. ;It doesn't increase [0x3004]!
  25.     mov    eax,0x3000+second_base_address
  26.     push   ebx
  27.     mov    ebx,[0x3004]
  28.     inc    ebx
  29.     shl    ebx,5
  30.     add    ebx,eax               ;ebx - address of process information for (last+1) slot
  31. .newprocessplace:
  32. ;eax = address of process information for current slot
  33.     cmp    eax,ebx
  34.     jz     .endnewprocessplace   ;empty slot after high boundary
  35.     add    eax,0x20
  36.     cmp    word [eax+TASKDATA.state],9      ;check process state, 9 means that process slot is empty
  37.     jnz    .newprocessplace
  38. .endnewprocessplace:
  39.     mov    ebx,eax
  40.     sub    eax,0x3000+second_base_address
  41.     shr    eax,5                 ;calculate slot index
  42.     cmp    eax,256
  43.     jge    .failed               ;it should be <256
  44.     mov    word [ebx+TASKDATA.state],9      ;set process state to 9 (for slot after hight boundary)
  45.     mov    [new_process_place],eax ;save process slot
  46.     pop    ebx
  47.     ret    
  48. .failed:
  49.     xor    eax,eax
  50.     pop    ebx
  51.     ret
  52. ;-----------------------------------------------------------------------------
  53. safe_sti:
  54.     cmp    byte [0xe000], 1
  55.     jne    @f
  56.     sti
  57.  @@:ret
  58.  
  59. new_start_application_floppy:
  60. ;input:
  61. ;  eax - pointer to filename
  62. ;  ebx - parameters to pass
  63. ;  edx - flags
  64. ;result:
  65. ;  eax - pid of new process
  66. ;        or 0 if call fails.
  67.     mov    [appl_path],edi
  68.     pushad
  69.     mov    esi,new_process_loading
  70.     call   sys_msg_board_str     ;write to debug board
  71.    
  72. ;wait application_table_status mutex
  73. .table_status:    
  74.     cli
  75.     cmp    [application_table_status],0
  76.     jz     .stf
  77.     sti
  78.     call   change_task
  79.     jmp    .table_status
  80. .stf:
  81.     call   set_application_table_status
  82. ;we can change system tables now
  83.     push   edi
  84.     push   ebx
  85.     push   eax
  86.     call   find_new_process_place ;find empty process slot
  87.     sti
  88.     test   eax,eax
  89.         mov     ecx, -0x20      ; too many processes
  90.     jz     .failed
  91.  
  92.     mov    edi,eax
  93.     shl    edi,8
  94.     add    edi,0x80000
  95.     mov    ecx,256/4
  96.     xor    eax,eax
  97.     cld
  98.     rep    stosd                 ;clean extended information about process
  99.    
  100. ;set new process name
  101.     mov    [appl_path_size],eax
  102.     pop    eax
  103.     push   eax
  104. .find_last_byte:
  105.     cmp    byte [eax],0    
  106.     jz     .find_last_byte_end
  107.     inc    eax
  108.     inc    [appl_path_size]
  109.     jmp    .find_last_byte
  110. .find_last_byte_end:
  111.     add    [appl_path_size],24    
  112.     sub    eax,11                ;last 11 bytes = application name
  113. ;    mov    eax,[esp]             ;eax - pointer to file name
  114.     mov    ebx,[new_process_place]
  115.     shl    ebx,8
  116.     add    ebx,0x80000 + APPDATA.app_name
  117.     mov    ecx,11
  118.     call   memmove
  119.      
  120. ;read header of file
  121.     mov    eax,[esp]
  122.     mov    ebx,1                 ;index of first block
  123.     mov    ecx,2                 ;number of blocks
  124.     mov    edx,0x90000           ;temp area
  125.     mov    esi,12                ;file name length
  126.     mov    edi,[esp+8]
  127. ;    cli
  128.     call   floppy_fileread       ;read file from FD
  129. ;    sti
  130.         mov     ecx, eax
  131.         neg     ecx
  132.         jnz     .cleanfailed
  133. ;check MENUET signature
  134.         mov     ecx, -0x1F      ; not Menuet/Kolibri executable
  135.     cmp    [0x90000],dword 'MENU'
  136.     jnz    .cleanfailed
  137.     cmp    [0x90004],word 'ET'
  138.     jnz    .cleanfailed
  139.    
  140.     call   get_app_params        ;parse header fields
  141.     jc     .cleanfailed
  142.    
  143.     mov    eax,[new_process_place]
  144.         inc     ecx             ; -0x1E = no memory
  145.     call   create_app_cr3_table   ;create page directory for new process
  146.     test   eax,eax
  147.     jz     .cleanfailed_mem
  148.    
  149.     call   MEM_Get_Linear_Address ;calculate linear address of it
  150.    
  151.     mov    ebx,std_application_base_address
  152.     mov    ecx,[app_mem]
  153.     add    ecx,4095
  154.     shr    ecx,12
  155.     mov    edx,eax
  156.     call   mem_alloc_specified_region ;allocate memory for application
  157.     test   eax,eax
  158.         mov     ecx, -0x1E
  159.     jz     .cleanfailed_mem1
  160.    
  161.     mov    eax,[edx+(std_application_base_address shr 20)]
  162.     and    eax,not (4096-1)      ;eax - physical address of first (for application memory) page table
  163.     call   MEM_Get_Linear_Address
  164.     mov    edx,eax
  165.  
  166. ;read file
  167.     mov    ebx,1
  168.     mov    esi,12                ;length of file name
  169. .loop1:
  170. ;edx = linear address of current page table entry
  171. ;ebx = index of current block in file
  172.     push   edx
  173.     mov    eax,[edx]
  174.     and    eax,not (4096-1)
  175.     call   MEM_Get_Linear_Address
  176.     mov    edx,eax               ;read file block to current page
  177.     mov    eax,[esp+4]           ;restore pointer to file name
  178.     mov    ecx,8                 ;number of blocks read
  179.     mov    ebp,edx               ;save buffer address for .endofimage
  180.     push   ebx
  181.     mov    edi,[esp+16]    
  182. ;    cli
  183.     call   floppy_fileread
  184. ;ebx=file size    
  185. ;    sti
  186.     pop    ecx
  187.     add    ecx,8
  188.     test   eax,eax
  189.     jnz    .endloop1             ;check io errors
  190.     mov    eax,[app_i_end]
  191.     add    eax,511
  192.     shr    eax,9
  193.     cmp    ecx,eax
  194.     jg     .endofimage           ;we have loaded whole program
  195.     add    ebx,511
  196.     shr    ebx,9
  197.     cmp    ecx,ebx
  198.     jg     .endloop1             ;if end of file?
  199.     mov    ebx,ecx
  200.     pop    edx
  201.     add    edx,4
  202.     jmp    .loop1
  203.    
  204. .endofimage:                     ;set to zero memory at end of page
  205.     mov    ecx,[app_i_end]
  206.     and    ecx,4096-1
  207.     jz     .endloop1
  208.     lea    edi,[ebp+ecx]
  209.     neg    ecx
  210.     add    ecx,4096
  211.     xor    eax,eax
  212.     cld
  213.     rep    stosb
  214. .endloop1:
  215.     add    esp,8+4                 ;pop linear address of page table entry and pointer to file name
  216.     call   new_start_application_fl.add_app_parameters
  217.     mov    [esp+28],eax
  218.     popad
  219.     ret
  220.    
  221. .cleanfailed_mem1:
  222. ;there is mem for directory entry, but there is no mem for pages
  223. ;so free directory entry
  224.     mov    eax,[new_process_place]    
  225.     shl    eax,8
  226.     mov    eax,[0x80000+eax+APPDATA.dir_table]
  227.     call   MEM_Free_Page    
  228. .cleanfailed_mem:
  229. ;there is no mem for directory entry, display message.
  230.     mov    esi,start_not_enough_memory
  231.     call   sys_msg_board_str
  232. .cleanfailed:                    ;clean process name
  233.         push    ecx     ; save error code
  234. ;can't read file, clean process name.
  235. ;this avoid problems with panel application.
  236.     mov    edi,[new_process_place]
  237.     shl    edi,8
  238.     add    edi,0x80000 + APPDATA.app_name
  239.     mov    ecx,11
  240.     mov    eax,' '
  241.     cld
  242.     rep    stosb
  243.         pop     eax
  244. .failed:
  245. ;no more slots
  246.     add    esp,8+4
  247.     mov    [application_table_status],0
  248.     mov    [esp+1Ch], eax
  249.     popad
  250.     sti
  251.     ret  
  252.  
  253. ;-----------------------------------------------------------------------------    
  254. new_start_application_fl:
  255. ;input:
  256. ;  eax - pointer to filename
  257. ;  ebx - parameters to pass
  258. ;  edx - flags
  259. ;result:
  260. ;  eax - pid of new process
  261. ;        or 0 if call fails.
  262.     mov    [appl_path],edi
  263.     mov    [appl_path_size],36
  264.     pushad
  265.     mov    esi,new_process_loading
  266.     call   sys_msg_board_str     ;write to debug board
  267.    
  268. ;wait application_table_status mutex
  269. .table_status:    
  270.     cli
  271.     cmp    [application_table_status],0
  272.     jz     .stf
  273.     sti
  274.     call   change_task
  275.     jmp    .table_status
  276. .stf:
  277.     call   set_application_table_status
  278. ;we can change system tables now    
  279.     push   ebx
  280.     push   eax
  281.     call   find_new_process_place ;find empty process slot
  282.     call   safe_sti
  283.     test   eax,eax
  284.         mov     ecx, -0x20      ; too many processes
  285.     jz     .failed
  286.  
  287.     mov    edi,eax
  288.     shl    edi,8
  289.     add    edi,0x80000
  290.     mov    ecx,256/4
  291.     xor    eax,eax
  292.     cld
  293.     rep    stosd                 ;clean extended information about process
  294.    
  295. ;set new process name
  296.     mov    eax,[esp]             ;eax - pointer to file name
  297.     mov    ebx,[new_process_place]
  298.     shl    ebx,8
  299.     add    ebx,0x80000 + APPDATA.app_name
  300.     mov    ecx,11
  301.     call   memmove
  302.      
  303. ;read header of file
  304.     mov    ebx,1                 ;index of first block
  305.     mov    ecx,2                 ;number of blocks
  306.     mov    edx,0x90000           ;temp area
  307.     mov    esi,12                ;file name length
  308.     cli
  309.     call   fileread              ;read file from RD
  310.     call   safe_sti
  311.         mov     ecx, eax
  312.         neg     ecx
  313.         jnz     .cleanfailed
  314. ;check MENUET signature
  315.         mov     ecx, -0x1F      ; not Menuet/Kolibri executable
  316.     cmp    [0x90000],dword 'MENU'
  317.     jnz    .cleanfailed
  318.     cmp    [0x90004],word 'ET'
  319.     jnz    .cleanfailed
  320.    
  321.     call   get_app_params        ;parse header fields
  322.     jc     .cleanfailed
  323.    
  324.     mov    eax,[new_process_place]
  325.     inc    ecx          ; -0x1E = no memory
  326.     call   create_app_cr3_table   ;create page directory for new process
  327.     test   eax,eax
  328.     jz     .cleanfailed_mem
  329.    
  330.     call   MEM_Get_Linear_Address ;calculate linear address of it
  331.    
  332.     mov    ebx,std_application_base_address
  333.     mov    ecx,[app_mem]
  334.     add    ecx,4095
  335.     shr    ecx,12
  336.     mov    edx,eax
  337.     call   mem_alloc_specified_region ;allocate memory for application
  338.     test   eax,eax
  339.         mov     ecx, -0x1E
  340.     jz     .cleanfailed_mem1
  341.    
  342.     mov    eax,[edx+(std_application_base_address shr 20)]
  343.     and    eax,not (4096-1)      ;eax - physical address of first (for application memory) page table
  344.     call   MEM_Get_Linear_Address
  345.     mov    edx,eax
  346.  
  347. ;read file
  348.     mov    ebx,1
  349.     mov    esi,12                ;length of file name
  350. .loop1:
  351. ;edx = linear address of current page table entry
  352. ;ebx = index of current block in file
  353.     push   edx
  354.     mov    eax,[edx]
  355.     and    eax,not (4096-1)
  356.     call   MEM_Get_Linear_Address
  357.     mov    edx,eax               ;read file block to current page
  358.     mov    eax,[esp+4]           ;restore pointer to file name
  359.     mov    ecx,8                 ;number of blocks read
  360.     mov    ebp,edx               ;save buffer address for .endofimage
  361.     push   ebx
  362.     cli
  363.     call   fileread
  364. ;ebx=file size    
  365.     call   safe_sti
  366.     pop    ecx
  367.     add    ecx,8
  368.     test   eax,eax
  369.     jnz    .endloop1             ;check io errors
  370.     mov    eax,[app_i_end]
  371.     add    eax,511
  372.     shr    eax,9
  373.     cmp    ecx,eax
  374.     jg     .endofimage           ;we have loaded whole program
  375.     add    ebx,511
  376.     shr    ebx,9
  377.     cmp    ecx,ebx
  378.     jg     .endloop1             ;if end of file?
  379.     mov    ebx,ecx
  380.     pop    edx
  381.     add    edx,4
  382.     jmp    .loop1
  383.    
  384. .endofimage:                     ;set to zero memory at end of page
  385.     mov    ecx,[app_i_end]
  386.     and    ecx,4096-1
  387.     jz     .endloop1
  388.     lea    edi,[ebp+ecx]
  389.     neg    ecx
  390.     add    ecx,4096
  391.     xor    eax,eax
  392.     cld
  393.     rep    stosb
  394. .endloop1:
  395.     add    esp,8                 ;pop linear address of page table entry and pointer to file name
  396.     call   .add_app_parameters
  397.     mov    [esp+28],eax
  398.     popad
  399.     ret
  400.    
  401. .cleanfailed_mem1:
  402. ;there is mem for directory entry, but there is no mem for pages
  403. ;so free directory entry
  404.     mov    eax,[new_process_place]    
  405.     shl    eax,8
  406.     mov    eax,[0x80000+eax+APPDATA.dir_table]
  407.     call   MEM_Free_Page
  408. .cleanfailed_mem:
  409. ;there is no mem for directory entry, display message.
  410.     mov    esi,start_not_enough_memory
  411.     call   sys_msg_board_str
  412. .cleanfailed:                    ;clean process name
  413.         push    ecx     ; save error code
  414. ;can't read file, clean process name.
  415. ;this avoid problems with panel application.
  416.     mov    edi,[new_process_place]
  417.     shl    edi,8
  418.     add    edi,0x80000+APPDATA.app_name
  419.     mov    ecx,11
  420.     mov    eax,' '
  421.     cld
  422.     rep    stosb
  423.         pop     eax
  424. .failed:
  425. ;no more slots
  426.     add    esp,8
  427.     mov    [application_table_status],0
  428.     mov    [esp+1Ch], eax
  429.     popad
  430.     call   safe_sti
  431.     ret
  432.        
  433. .add_app_parameters:
  434. ;input:
  435. ;  [esp] - pointer to parameters
  436. ;  [esp+4]-[esp+36] pushad registers.
  437. ;result
  438. ;  eax - pid of new process
  439. ;        or zero if failed
  440.     cli
  441.     mov    ebx,[new_process_place]
  442.     cmp    ebx,[0x3004]
  443.     jle    .noinc
  444.     inc    dword [0x3004]        ;update number of processes
  445. .noinc:
  446.  
  447. ;   mov    ebx,[new_process_place]
  448. ;set 0x8c field of extended information about process
  449. ;(size of application memory)
  450.     shl    ebx,8
  451.     mov    eax,[app_mem]
  452.     mov    [second_base_address+0x80000+APPDATA.mem_size+ebx],eax            
  453. ;set 0x10 field of information about process
  454. ;(application base address)    
  455. ;    mov    ebx,[new_process_place]
  456. ;    shl    ebx,5
  457.     shr    ebx,3
  458.     mov    dword [second_base_address+0x3000+ebx+TASKDATA.mem_start],std_application_base_address
  459.  
  460. ;add command line parameters
  461. .add_command_line:
  462.     mov    edx,[app_i_param]
  463.     test   edx,edx
  464.     jz     .no_command_line      ;application don't need parameters
  465.     mov    eax,[esp+4]
  466.     test   eax,eax
  467.     jz     .no_command_line      ;no parameters specified
  468. ;calculate parameter length    
  469.     mov    esi,eax
  470.     xor    ecx,ecx
  471. .command_line_len:
  472.     cmp    byte [esi],0
  473.     jz     .command_line_len_end
  474.     inc    esi
  475.     inc    ecx
  476.     cmp    ecx,256
  477.     jl     .command_line_len
  478.    
  479. .command_line_len_end:
  480. ;ecx - parameter length
  481. ;edx - address of parameters in new process address space
  482.     mov    ebx,eax               ;ebx - address of parameters in our address space
  483.     mov    eax,[new_process_place]
  484.     call   write_process_memory  ;copy parameters to new process address space
  485.    
  486. .no_command_line:
  487. ;******************************************************************
  488.     mov    edx,[app_i_icon]
  489.     test   edx,edx
  490.     jz     .no_command_line_1      ;application don't need path of file
  491.     mov    ebx,[appl_path]
  492.     mov    ecx,[appl_path_size]
  493.     mov    eax,[new_process_place]
  494.     call   write_process_memory  ;copy path of file to new process address space
  495. .no_command_line_1:
  496. ;******************************************************************
  497.     mov    ebx,[new_process_place]
  498.     mov    eax,ebx
  499.     shl    ebx,5
  500.     add    ebx,0x3000            ;ebx - pointer to information about process
  501.     mov    [ebx+TASKDATA.wnd_number],al  ;set window number on screen = process slot
  502.    
  503.     mov    [ebx+TASKDATA.event_mask],dword 1+2+4     ;set default event flags (see 40 function)
  504.    
  505.     inc    dword [process_number]
  506.     mov    eax,[process_number]
  507.     mov    [ebx+TASKDATA.pid],eax           ;set PID
  508.    
  509.     mov    ecx,ebx
  510.     add    ecx,draw_data-0x3000  ;ecx - pointer to draw data
  511. ;set draw data to full screen    
  512.     mov    [ecx+RECT.left],dword 0      
  513.     mov    [ecx+RECT.top],dword 0
  514.     mov    eax,[0xfe00]
  515.     mov    [ecx+RECT.right],eax
  516.     mov    eax,[0xfe04]
  517.     mov    [ecx+RECT.bottom],eax
  518. ;set window state to 'normal' (non-minimized/maximized/rolled-up) state
  519.     mov    [ecx+WDATA.fl_wstate],WSTATE_NORMAL
  520. ;set cr3 register in TSS of application    
  521.     mov    ecx,[new_process_place]    
  522.     shl    ecx,8
  523.     mov    eax,[0x80000+APPDATA.dir_table+ecx]
  524.     add    eax,8+16              ;add flags
  525.     mov    [l.cr3],eax
  526.    
  527.     mov    eax,[app_start]
  528.     mov    [l.eip],eax           ;set eip in TSS
  529.     mov    eax,[app_esp]
  530.     mov    [l.esp],eax           ;set stack in TSS
  531.    
  532. ;gdt
  533.     ;mov    ebx,[new_process_place]
  534.     ;shl    ebx,3
  535.     mov    ax,app_code           ;ax - selector of code segment
  536.     ;add    ax,bx
  537.     mov    [l.cs],ax
  538.     mov    ax,app_data
  539.     ;add    ax,bx                 ;ax - selector of data segment
  540.     mov    [l.ss],ax
  541.     mov    [l.ds],ax
  542.     mov    [l.es],ax
  543.     mov    [l.fs],ax
  544.     mov    ax,graph_data         ;ax - selector of graphic segment
  545.     mov    [l.gs],ax
  546.     mov    [l.io],word 128
  547.     mov    [l.eflags],dword 0x11202
  548.     mov    [l.ss0],os_data
  549.     mov    ebx,[new_process_place]
  550.     shl    ebx,12
  551.     add    ebx,sysint_stack_data+4096
  552.     mov    [l.esp0],ebx
  553.  
  554. ;copy tss to it place
  555.     mov    eax,tss_sceleton
  556.     mov    ebx,[new_process_place]
  557.     imul   ebx,tss_step
  558.     add    ebx,tss_data          ;ebx - address of application TSS
  559.     mov    ecx,120              
  560.     call   memmove
  561.    
  562. ;Add IO access table - bit array of permitted ports
  563.     or     eax,-1
  564.     mov    edi,[new_process_place]
  565.     imul   edi,tss_step
  566.     add    edi,tss_data+128
  567.     mov    ecx,2048
  568.     cld
  569.     rep    stosd                 ;full access to 2048*8=16384 ports
  570.    
  571.     mov    ecx,ebx               ;ecx - address of application TSS
  572.     mov    edi,[new_process_place]
  573.     shl    edi,3
  574. ;set TSS descriptor
  575.     mov    [edi+gdts+tss0+0],word tss_step ;limit (size)
  576.     mov    [edi+gdts+tss0+2],cx  ;part of offset
  577.     mov    eax,ecx
  578.     shr    eax,16
  579.     mov    [edi+gdts+tss0+4],al  ;part of offset
  580.     mov    [edi+gdts+tss0+7],ah  ;part of offset
  581.     mov    [edi+gdts+tss0+5],word 01010000b*256+11101001b ;system flags
  582.      
  583.  
  584. ;flush keyboard and buttons queue
  585.     mov    [0xf400],byte 0
  586.     mov    [0xf500],byte 0
  587.  
  588.     mov    edi,[new_process_place]
  589.     shl    edi,5
  590.     add    edi,window_data
  591.     mov    ebx,[new_process_place]
  592.     movzx  esi,word [0xC000+ebx*2]
  593.     lea    esi,[0xC400+esi*2]
  594.     call   windowactivate        ;gui initialization
  595.  
  596.     mov    ebx,[new_process_place]
  597.     shl    ebx,5
  598. ; set if debuggee
  599.         test    byte [esp+28], 1
  600.         jz      .no_debug
  601.         mov     [0x3000+ebx+TASKDATA.state], 1        ; set process state - suspended
  602.         mov     eax, [0x3000]
  603.         mov     [0x80000+ebx*8+APPDATA.debugger_slot], eax ;set debugger PID - current
  604.         jmp     .debug
  605. .no_debug:
  606.         mov     [0x3000+ebx+TASKDATA.state], 0        ; set process state - running
  607. .debug:
  608.    
  609.     mov    esi,new_process_running
  610.     call   sys_msg_board_str     ;output information about succefull startup
  611.    
  612. ;    add    esp,4                 ;pop pointer to parameters
  613. ;    popad
  614.     mov    eax,[process_number]  ;set result
  615.     mov    [application_table_status],0 ;unlock application_table_status mutex
  616.     call   safe_sti
  617.     ret    4
  618. ;-----------------------------------------------------------------------------    
  619. new_sys_threads:
  620. ;eax=1 - create thread
  621. ;   ebx=thread start
  622. ;   ecx=thread stack value
  623. ;result:
  624. ;   eax=pid
  625.     xor    edx,edx      ; flags=0
  626.     pushad
  627.    
  628.     cmp    eax,1
  629.     jnz    .ret                  ;other subfunctions
  630.     mov    esi,new_process_loading
  631.     call   sys_msg_board_str
  632. ;lock application_table_status mutex
  633. .table_status:    
  634.     cli
  635.     cmp    [application_table_status],0
  636.     je     .stf
  637.     sti
  638.     call   change_task
  639.     jmp    .table_status
  640. .stf:
  641.     call   set_application_table_status
  642. ;find free process slot
  643.  
  644.     call   find_new_process_place
  645.     test   eax,eax
  646.     jz     .failed
  647.    
  648. ;set parameters for thread
  649.     xor    eax,eax
  650.     mov    [app_i_param],eax
  651.     mov    [app_i_icon],eax
  652.     mov    [app_start],ebx
  653.     mov    [app_esp],ecx
  654.  
  655.     mov    esi,[0x3000]
  656.     shl    esi,8
  657.     add    esi,0x80000+APPDATA.app_name
  658.     mov    ebx,esi               ;ebx=esi - pointer to extended information about current thread
  659.    
  660.     mov    edi,[new_process_place]
  661.     shl    edi,8
  662.     add    edi,0x80000
  663.     lea    edx, [edi+APPDATA.app_name] ;edx=edi - pointer to extended infomation about new thread
  664.     mov    ecx,256/4
  665.     rep    stosd                 ;clean extended information about new thread
  666.     mov    edi,edx
  667.     mov    ecx,11
  668.     rep    movsb                 ;copy process name
  669.     mov    eax,[ebx+APPDATA.mem_size]
  670.     mov    [app_mem],eax         ;set memory size
  671.     mov    eax,[ebx+APPDATA.dir_table]
  672.     mov    dword [edx-APPDATA.app_name+APPDATA.dir_table],eax        ;copy page directory
  673. ;    mov    eax,[new_process_place]
  674. ;    mov    ebx,[0x3000]
  675. ;    call   addreference_app_cr3_table
  676.  
  677.     push   0                     ;no parameters
  678.     call    new_start_application_fl.add_app_parameters ;start thread
  679.     mov    [esp+28],eax
  680.     popad
  681.     ret
  682.    
  683. .failed:
  684.     sti
  685.     popad
  686.     mov    eax,-1
  687.     ret    
  688. .ret:
  689.     popad
  690.     ret
  691. ;-----------------------------------------------------------------------------    
  692. new_mem_resize:
  693. ;input:
  694. ;  ebx - new size
  695. ;result:
  696. ;  [esp+36]:=0 - normal
  697. ;  [esp+36]:=1 - error
  698. ;This function set new application memory size.
  699.     mov    esi,ebx               ;save new size
  700.     add    ebx,4095
  701.     and    ebx,not (4096-1)      ;round up size
  702.     mov    ecx,[0x3000]
  703.     shl    ecx,8
  704.     mov    edx,[0x80000 + APPDATA.mem_size +ecx]    
  705.     add    edx,4095
  706.     and    edx,not (4096-1)      ;old size
  707.     mov    eax,[0x80000 + APPDATA.dir_table+ecx]
  708.     call   MEM_Get_Linear_Address
  709. ;eax - linear address of page directory    
  710.     call   MEM_Heap_Lock         ;guarantee that two threads willn't
  711.                                  ;change memory size simultaneously
  712.     cmp    ebx,edx
  713. ;    mov    esi,ebx               ;save new size
  714.     jg     .expand
  715.    
  716. .free:
  717.     sub    edx,ebx
  718.     jz     .unlock               ;do nothing
  719.     mov    ecx,edx
  720.     shr    ecx,12
  721.     add    ebx,std_application_base_address
  722.     call   mem_free_specified_region  ;free unnecessary pages
  723.     jmp    .unlock
  724.  
  725. .expand:
  726.     sub    ebx,edx
  727.     mov    ecx,ebx
  728.     shr    ecx,12
  729.     mov    ebx,edx
  730.     add    ebx,std_application_base_address
  731.     call   mem_alloc_specified_region ;alloc necessary pages
  732.     test   eax,eax
  733.     jz     .failed               ;not enough memory
  734.    
  735. .unlock:
  736.     mov    ebx,esi
  737.     mov    eax,[0x3000]
  738.     shl    eax,8
  739.     mov    [eax+0x80000 + APPDATA.mem_size],ebx     ;write new memory size
  740. ;search threads and update
  741. ;application memory size infomation    
  742.     mov    ecx,[eax+0x80000 + APPDATA.dir_table]
  743.     mov    eax,2
  744.    
  745. .search_threads:
  746. ;eax = current slot
  747. ;ebx = new memory size
  748. ;ecx = page directory
  749.     cmp    eax,[0x3004]
  750.     jg     .search_threads_end
  751.     mov    edx,eax
  752.     shl    edx,5
  753.     cmp    word [0x3000+edx+TASKDATA.state],9 ;if slot empty?
  754.     jz     .search_threads_next
  755.     shl    edx,3
  756.     cmp    [edx+0x80000+APPDATA.dir_table],ecx     ;if it is our thread?
  757.     jnz    .search_threads_next
  758.     mov    [edx+0x80000+APPDATA.mem_size],ebx     ;update memory size
  759. .search_threads_next:
  760.     inc    eax
  761.     jmp    .search_threads
  762. .search_threads_end:
  763.  
  764.     call   MEM_Heap_UnLock
  765.     mov    dword [esp+36],0
  766.     ret
  767.    
  768. .failed:
  769.     call   MEM_Heap_UnLock
  770.     mov    dword [esp+36],1
  771.     ret    
  772. ;-----------------------------------------------------------------------------
  773. pid_to_slot:
  774. ;Input:
  775. ;  eax - pid of process
  776. ;Output:
  777. ;  eax - slot of process or 0 if process don't exists
  778. ;Search process by PID.
  779.     push   ebx
  780.     push   ecx
  781.     mov    ebx,[0x3004]
  782.     shl    ebx,5
  783.     mov    ecx,2*32
  784.    
  785. .loop:
  786. ;ecx=offset of current process info entry
  787. ;ebx=maximum permitted offset
  788.     cmp    byte [second_base_address+0x3000+ecx+TASKDATA.state],9
  789.     jz     .endloop              ;skip empty slots
  790.     cmp    [second_base_address+0x3000+ecx+TASKDATA.pid],eax ;check PID
  791.     jz     .pid_found
  792. .endloop:
  793.     add    ecx,32
  794.     cmp    ecx,ebx
  795.     jle    .loop
  796.    
  797.     pop    ecx
  798.     pop    ebx
  799.     xor    eax,eax
  800.     ret
  801.    
  802. .pid_found:
  803.     shr    ecx,5
  804.     mov    eax,ecx               ;convert offset to index of slot
  805.     pop    ecx
  806.     pop    ebx
  807.     ret
  808. ;-----------------------------------------------------------------------------    
  809. is_new_process:
  810. ;Input:
  811. ;  eax - process slot
  812. ;Output:
  813. ;  eax=1 - it is new process
  814. ;  eax=0 - it is old process
  815. ;    shl   eax,5
  816. ;    mov   eax,[second_base_address+0x3000+eax+0x10]
  817. ;    cmp   eax,std_application_base_address  ;check base address of application
  818. ;    jz    .new_process
  819. ;    xor   eax,eax
  820. ;    ret
  821.    
  822. ;.new_process:
  823.     mov   eax,1
  824.     ret
  825. ;-----------------------------------------------------------------------------    
  826. write_process_memory:
  827. ;Input:
  828. ;  eax - process slot
  829. ;  ebx - buffer address
  830. ;  ecx - buffer size
  831. ;  edx - start address in other process
  832. ;Output:
  833. ;  eax - number of bytes written
  834.     pushad
  835.     shl  eax,8
  836.     mov  eax,[0x80000+eax+APPDATA.dir_table]
  837.     call MEM_Get_Linear_Address
  838.     mov  ebp,eax
  839. ;ebp=linear address of page directory of other process.    
  840.     add  edx,std_application_base_address  ;convert to linear address
  841.     test ecx,ecx
  842.     jle  .ret
  843.    
  844. .write_loop:
  845. ;ebx = current buffer address
  846. ;ecx>0 = current size
  847. ;edx = current address in other process
  848. ;ebp = linear address of page directory
  849.  
  850.     call MEM_Heap_Lock           ;cli
  851.     mov  esi,edx
  852.     shr  esi,22
  853.     mov  eax,[ebp+4*esi]         ;find page directory entry
  854.     and  eax,not (4096-1)        ;clear flags
  855.     test eax,eax
  856.     jz   .page_not_found
  857.     call MEM_Get_Linear_Address  ;calculate linear address of page table
  858.     test eax,eax
  859.     jz   .page_not_found
  860.     mov  esi,edx
  861.     shr  esi,12
  862.     and  esi,1023                
  863.     mov  eax,[eax+4*esi]         ;find page table entry
  864.     and  eax,not (4096-1)
  865.     test eax,eax
  866.     jz   .page_not_found
  867.     call MEM_Get_Linear_Address  ;calculate linear address of page
  868.     test eax,eax
  869.     jz   .page_not_found
  870.     mov  edi,eax
  871.     call MEM_Add_Reference_Linear;guarantee that page willn't disappear
  872.     call MEM_Heap_UnLock         ;sti
  873.    
  874.     mov  esi,edx
  875.     and  esi,4095
  876.     add  edi,esi                 ;add offset in page
  877. ;edi = linear address corresponding edx in other process
  878.     sub  esi,4096
  879.     neg  esi                     ;esi - number of remaining bytes in page
  880.     cmp  esi,ecx
  881.     jl   .min_ecx
  882.     mov  esi,ecx
  883. .min_ecx:                        ;esi=min(ecx,esi) - number of bytes to write
  884.     sub  ecx,esi
  885.     push ecx
  886.     mov  ecx,esi                 ;ecx - number of bytes to write
  887.     mov  esi,ebx                 ;esi - source, edi - destination
  888.     add  edx,ecx                 ;move pointer in address space of other process
  889.     push edi
  890.    
  891. ;move ecx bytes    
  892.     test ecx,3
  893.     jnz  .not_aligned
  894.     shr  ecx,2
  895.     rep  movsd
  896.     jmp  .next_iter
  897. .not_aligned:
  898.     rep  movsb
  899. .next_iter:
  900.  
  901.     pop  eax                    
  902.     and  eax,not (4096-1)        ;eax - linear address of current page
  903.     call MEM_Free_Page_Linear    ;free reference
  904.     mov  ebx,esi                 ;new pointer to buffer - movsb automaticaly advance it.
  905.     pop  ecx                     ;restore number of remaining bytes
  906.     test ecx,ecx
  907.     jnz  .write_loop
  908. .ret:    
  909.     popad
  910.     mov  eax,ecx
  911.     ret
  912.    
  913. .page_not_found:
  914.     call MEM_Heap_UnLock         ;error has appeared in critical region
  915.     sub  ecx,[esp+24]            ;[esp+24]<-->ecx
  916.     neg  ecx                     ;ecx=number_of_written_bytes
  917.     mov  [esp+28],ecx            ;[esp+28]<-->eax
  918.     popad
  919.     ret    
  920. ;-----------------------------------------------------------------------------    
  921. syscall_test:
  922. ;for testing memory manager from applications.
  923.     mov  edx,ecx
  924.     mov  ecx,ebx
  925.     call trans_address
  926.     mov  ebx,eax
  927.     mov  eax,[0x3000]
  928.     call read_process_memory
  929.     ret
  930. ;-----------------------------------------------------------------------------    
  931. read_process_memory:
  932. ;Input:
  933. ;  eax - process slot
  934. ;  ebx - buffer address
  935. ;  ecx - buffer size
  936. ;  edx - start address in other process
  937. ;Output:
  938. ;  eax - number of bytes read.
  939.     pushad
  940.     shl  eax,8
  941.     mov  eax,[0x80000+eax+APPDATA.dir_table]
  942.     call MEM_Get_Linear_Address
  943.     mov  ebp,eax
  944.     add  edx,std_application_base_address
  945. .read_loop:
  946. ;ebx = current buffer address
  947. ;ecx>0 = current size
  948. ;edx = current address in other process
  949. ;ebp = linear address of page directory
  950.  
  951.     call MEM_Heap_Lock           ;cli
  952.     mov  esi,edx
  953.     shr  esi,22
  954.     mov  eax,[ebp+4*esi]         ;find page directory entry
  955.     and  eax,not (4096-1)
  956.     test eax,eax
  957.     jz   .page_not_found
  958.     call MEM_Get_Linear_Address
  959.     test eax,eax
  960.     jz   .page_not_found
  961.     mov  esi,edx
  962.     shr  esi,12
  963.     and  esi,1023
  964.     mov  eax,[eax+4*esi]         ;find page table entry
  965.     and  eax,not (4096-1)
  966.     test eax,eax
  967.     jz   .page_not_found
  968.     call MEM_Get_Linear_Address  ;calculate linear address of page
  969.     test eax,eax
  970.     jz   .page_not_found
  971.     mov  esi,eax
  972.     call MEM_Add_Reference_Linear;guarantee that page willn't disappear
  973.     call MEM_Heap_UnLock         ;sti
  974.    
  975.     mov  edi,edx
  976.     and  edi,4095                
  977.     add  esi,edi                 ;add offset in page
  978. ;esi = linear address corresponding edx in other process
  979.     sub  edi,4096
  980.     neg  edi
  981.    
  982. ;edi=min(edi,ecx) - number of bytes to copy  
  983.     cmp  edi,ecx
  984.     jl   .min_ecx
  985.     mov  edi,ecx
  986. .min_ecx:
  987.  
  988.     sub  ecx,edi                 ;update size of remaining bytes
  989.     add  edx,edi                 ;update current pointer in other address space.
  990.     push ecx
  991.     mov  ecx,edi                 ;ecx - number of bytes to read
  992.     mov  edi,ebx                 ;esi - source, edi - destination
  993.     push esi
  994. ;move ecx bytes    
  995.     test ecx,3
  996.     jnz  .not_aligned
  997.     shr  ecx,2
  998.     rep  movsd
  999.     jmp  .next_iter
  1000. .not_aligned:
  1001.     rep  movsb
  1002. .next_iter:
  1003.     pop  eax
  1004.     and  eax,not (4096-1)        ;eax - linear address of current page
  1005.     call MEM_Free_Page_Linear    ;free reference
  1006.     mov  ebx,edi                 ;new pointer to buffer - movsb automaticaly advance it.
  1007.     pop  ecx                     ;restore number of remaining bytes
  1008.     test ecx,ecx
  1009.     jnz  .read_loop
  1010.    
  1011.     popad
  1012.     mov  eax,ecx
  1013.     ret
  1014.    
  1015. .page_not_found:
  1016.     call MEM_Heap_UnLock         ;error has appeared in critical region
  1017.     sub  ecx,[esp+24]            ;[esp+24]<-->ecx
  1018.     neg  ecx                     ;ecx=number_of_read_bytes
  1019.     mov  [esp+28],ecx            ;[esp+28]<-->eax
  1020.     popad
  1021.     ret
  1022. ;-----------------------------------------------------------------------------
  1023. check_region:
  1024. ;input:
  1025. ;  ebx - start of buffer
  1026. ;  ecx - size of buffer
  1027. ;result:
  1028. ;  eax = 1 region lays in app memory
  1029. ;  eax = 0 region don't lays in app memory
  1030.     mov  eax,[0x3000]
  1031.     jmp  check_process_region
  1032. ;-----------------------------------------------------------------------------    
  1033. check_process_region:
  1034. ;input:
  1035. ;  eax - slot
  1036. ;  ebx - start of buffer
  1037. ;  ecx - size of buffer
  1038. ;result:
  1039. ;  eax = 1 region lays in app memory
  1040. ;  eax = 0 region don't lays in app memory
  1041.     test ecx,ecx
  1042.     jle  .ok
  1043.     shl  eax,5
  1044.     cmp  word [0x3000+eax+TASKDATA.state],0
  1045.     jnz  .failed
  1046.     shl  eax,3
  1047.     mov  eax,[0x80000+eax+APPDATA.dir_table]
  1048.     test eax,eax
  1049.     jz   .failed
  1050.     call MEM_Get_Linear_Address
  1051.     push ebx
  1052.     push ecx
  1053.     push edx
  1054.     mov  edx,ebx
  1055.     and  edx,not (4096-1)
  1056.     sub  ebx,edx
  1057.     add  ecx,ebx
  1058.     mov  ebx,edx
  1059.     add  ecx,(4096-1)
  1060.     and  ecx,not (4096-1)
  1061. .loop:
  1062. ;eax - linear address of page directory    
  1063. ;ebx - current page
  1064. ;ecx - current size
  1065.     mov  edx,ebx
  1066.     shr  edx,22
  1067.     mov  edx,[eax+4*edx]
  1068.     and  edx,not (4096-1)
  1069.     test edx,edx
  1070.     jz   .failed1
  1071.     push eax
  1072.     mov  eax,edx
  1073.     call MEM_Get_Linear_Address
  1074.     mov  edx,ebx
  1075.     shr  edx,12
  1076.     and  edx,(1024-1)
  1077.     mov  eax,[eax+4*edx]
  1078.     and  eax,not (4096-1)
  1079.     test eax,eax
  1080.     pop  eax
  1081.     jz   .failed1
  1082.     add  ebx,4096
  1083.     sub  ecx,4096
  1084.     jg   .loop
  1085.     pop  edx
  1086.     pop  ecx
  1087.     pop  ebx
  1088. .ok:
  1089.     mov  eax,1
  1090.     ret
  1091.    
  1092. .failed1:
  1093.     pop  edx
  1094.     pop  ecx
  1095.     pop  ebx
  1096. .failed:
  1097.     xor  eax,eax
  1098.     ret
  1099. ;-----------------------------------------------------------------------------
  1100. new_sys_ipc:
  1101. ;input:
  1102. ;  eax=1 - set ipc buffer area
  1103. ;    ebx=address of buffer
  1104. ;    ecx=size of buffer
  1105. ;  eax=2 - send message
  1106. ;    ebx=PID
  1107. ;    ecx=address of message
  1108. ;    edx=size of message
  1109.     cmp  eax,1
  1110.     jnz  .no_ipc_def
  1111. ;set ipc buffer area    
  1112.     mov  edi,[0x3000]
  1113.     shl  edi,8
  1114.     add  edi,0x80000
  1115.     cli
  1116.     mov  [edi+APPDATA.ipc_start],ebx          ;set fields in extended information area
  1117.     mov  [edi+APPDATA.ipc_size],ecx
  1118.     sti
  1119.     mov  [esp+36],dword 0        ;success
  1120.     ret
  1121.        
  1122. .no_ipc_def:
  1123.     cmp  eax,2
  1124.     jnz  .no_ipc_send
  1125. ;send message    
  1126.     cli
  1127. ;obtain slot from PID    
  1128.     mov  eax,ebx
  1129.     call pid_to_slot
  1130.     test eax,eax
  1131.     jz   .no_pid
  1132.     mov  ebp,eax
  1133. ;ebp = slot of other process    
  1134.     shl  eax,8
  1135.     mov  edi,[eax+0x80000+APPDATA.ipc_start]  ;is ipc area defined?
  1136.     test edi,edi
  1137.     jz   .no_ipc_area
  1138.     mov  esi,[eax+0x80000+APPDATA.ipc_size]  ;esi - size of buffer
  1139.     push dword -1                ;temp variable for read_process_memory
  1140.     mov  ebx,esp
  1141.     push ecx
  1142.     push edx
  1143.     mov  ecx,4                   ;read 4 bytes
  1144.     mov  eax,ebp
  1145.     mov  edx,edi                 ;from beginning of buffer.
  1146.     call read_process_memory
  1147.     mov  eax,[esp+8]
  1148.     test eax,eax
  1149.     jnz  .ipc_blocked            ;if dword [buffer]<>0 - ipc blocked now
  1150.     add  edx,4                   ;move to next 4 bytes
  1151.     mov  eax,ebp
  1152.     call read_process_memory     ;read size of occupied space in buffer
  1153.     sub  esi,8
  1154.     sub  esi,[esp]
  1155.     sub  esi,[esp+8]             ;esi=(buffer size)-(occupied size)-(message size)-(header of message size)
  1156.     js   .buffer_overflow        ;esi<0 - not enough memory in buffer
  1157.     mov  esi,[esp+8]             ;previous offset
  1158.     add  dword [esp+8],8
  1159.     mov  edi,[esp]
  1160.     add  [esp+8],edi             ;add (size of message)+(size of header of message) to [buffer+4]
  1161.     mov  eax,ebp
  1162.     call write_process_memory
  1163.     add  edx,esi                
  1164.     sub  edx,4                   ;move to beginning of place for our message
  1165.     mov  eax,[second_base_address+0x3010]
  1166.     mov  eax,[eax+TASKDATA.pid]           ;eax - our PID
  1167.     mov  [esp+8],eax
  1168.     mov  eax,ebp
  1169.     call write_process_memory    ;write PID
  1170.     mov  ebx,esp                 ;address of size of message
  1171.     mov  eax,ebp
  1172.     add  edx,4
  1173.     call write_process_memory    ;write size of message
  1174.     add  edx,4
  1175.     pop  ecx                     ;ecx - size of message
  1176.     pop  eax
  1177.     call trans_address
  1178.     mov  ebx,eax                 ;ebx - linear address of message
  1179.     add  esp,4                   ;pop temporary variable
  1180.     mov  eax,ebp
  1181.     call write_process_memory    ;write message
  1182.     sti
  1183. ;awake other process    
  1184.     shl  ebp,8
  1185.     mov  eax,ebp
  1186.     or   [eax+0x80000+APPDATA.event_mask],dword 0x40
  1187.    
  1188.     cmp  dword [check_idle_semaphore],20
  1189.     jge  .ipc_no_cis
  1190.     mov  dword [check_idle_semaphore],5
  1191. .ipc_no_cis:
  1192.     mov  dword [esp+36],0
  1193.     ret
  1194. .no_ipc_send:
  1195.     mov  dword [esp+36],-1
  1196.     ret
  1197. .no_pid:
  1198.     sti
  1199.     mov  dword [esp+36],4
  1200.     ret
  1201. .no_ipc_area:
  1202.     sti
  1203.     mov  dword [esp+36],1
  1204.     ret
  1205. .ipc_blocked:
  1206.     sti
  1207.     add  esp,12
  1208.     mov  dword [esp+36],2
  1209.     ret
  1210. .buffer_overflow:
  1211.     sti
  1212.     add  esp,12
  1213.     mov  dword [esp+36],3
  1214.     ret
  1215. ;-----------------------------------------------------------------------------    
  1216. trans_address:
  1217. ;Input
  1218. ;  eax - application address
  1219. ;Output
  1220. ;  eax - linear address for kernel      
  1221.     add   eax,std_application_base_address
  1222.     ret
  1223. ;-----------------------------------------------------------------------------    
  1224. new_start_application_hd:
  1225. ;eax - file name (kernel address)
  1226. ;ebx - file name length
  1227. ;ecx - work area (kernel address)
  1228. ;edx - flags
  1229. ;ebp - parameters
  1230.     mov    [appl_path],edi
  1231.     pushad
  1232.    
  1233.     mov    esi,new_process_loading
  1234.     call   sys_msg_board_str     ;write message to message board
  1235.    
  1236. ;lock application_table_status mutex
  1237. .table_status:
  1238.     cli
  1239.     cmp    [application_table_status],0
  1240.     jz     .stf
  1241.     sti
  1242.     call   change_task
  1243.     jmp    .table_status
  1244. .stf:
  1245.     call   set_application_table_status
  1246.    
  1247.     push   ebp
  1248.     push   ebx
  1249.     push   eax
  1250.     push   ecx
  1251.     call   find_new_process_place ;find new process slot
  1252.     sti
  1253.     test   eax,eax
  1254.         mov     ecx, -0x20      ; too many processes
  1255.     jz     .failed
  1256.    
  1257. ;write application name
  1258.     xor    eax,eax
  1259.     mov    [appl_path_size],eax    
  1260.     mov    eax,[esp+4]
  1261. .find_last_byte:
  1262.     cmp    byte [eax],0    
  1263.     jz     .find_last_byte_end
  1264.     inc    eax
  1265.     inc    [appl_path_size]
  1266.     jmp    .find_last_byte
  1267. .find_last_byte_end:
  1268.     add    [appl_path_size],24    
  1269.     lea    esi,[eax-11]          ;last 11 bytes = application name
  1270.     mov    edi,[new_process_place]
  1271.     shl    edi,8
  1272.     add    edi,0x80000+APPDATA.app_name
  1273.     mov    ecx,11
  1274.     cld
  1275.     rep    movsb                 ;copy name to extended information about process
  1276.    
  1277. ;read header    
  1278.     mov    eax,[esp+4]           ;file name
  1279.     mov    esi,[esp]             ;work area
  1280.     mov    ecx,1                 ;read from first block
  1281.     mov    edx,1                 ;read 1 block
  1282.     call   read_hd_file
  1283.         mov     ecx, eax
  1284.         neg     ecx
  1285.         jnz     .cleanfailed
  1286.  
  1287.     pop    esi
  1288.     push   esi
  1289. ;check menuet signature
  1290.         mov     ecx, -0x1F      ; not Menuet/Kolibri executable
  1291.     cmp    [esi+1024+0],dword 'MENU'  ;read_hd_file function write file to +1024 offset
  1292.     jnz    .cleanfailed
  1293.     cmp    [esi+1024+4],word 'ET'
  1294.     jnz    .cleanfailed
  1295.     add    esi,1024
  1296.     mov    edi,0x90000
  1297.     mov    ecx,512/4
  1298.     cld
  1299.     rep    movsd                 ;copy first block to 0x90000 address for get_app_params function
  1300.     call   get_app_params
  1301.         mov     ecx, -0x1F      ; not Menuet/Kolibri executable
  1302.     jc     .cleanfailed
  1303.    
  1304.     mov    eax,[new_process_place]
  1305.         inc     ecx             ; -0x1E = no memory
  1306.     call   create_app_cr3_table  ;create page directory
  1307.     test   eax,eax
  1308.     jz     .cleanfailed_mem
  1309.    
  1310.     call   MEM_Get_Linear_Address
  1311.    
  1312.     mov    ebx,std_application_base_address
  1313.     mov    ecx,[app_mem]
  1314.     add    ecx,4096-1
  1315.     shr    ecx,12
  1316.     mov    edx,eax               ;edx - linear address of page directory
  1317.     call   mem_alloc_specified_region ;allocate memory for application
  1318.         mov     ecx, -0x1E      ; no memory
  1319.     test   eax,eax
  1320.     jz     .cleanfailed_mem1
  1321.    
  1322.     add    edx,(std_application_base_address shr 20)
  1323.     mov    eax,[edx]
  1324.     and    eax,not (4096-1)
  1325.     call   MEM_Get_Linear_Address
  1326.     push   edx                   ;save pointer to first page table
  1327.     mov    edx,eax
  1328. ;read file
  1329.     mov    ecx,1
  1330.     xor    ebp,ebp
  1331. .loop1:
  1332. ;[esp] - pointer to current page directory entry
  1333. ;edx - pointer to current page table
  1334. ;ebp - offset in page
  1335. ;ecx - current cluster
  1336.     push   edx
  1337.     mov    eax,[esp+12]          ;file name
  1338.     mov    ebx,[esp+16]          ;file name length
  1339.     mov    esi,[esp+8]           ;work area
  1340.     mov    edx,1                 ;number of blocks to read
  1341.     push   ecx
  1342.     push   ebp
  1343.     cli
  1344.     call   read_hd_file
  1345.     sti
  1346.     pop    ebp
  1347.     test   eax,eax
  1348.     jnz    .endloop1             ;check io errors
  1349.    
  1350.     mov    esi,[esp+8+4]         ;work area
  1351.     add    esi,1024
  1352.     mov    eax,[esp+4]           ;current page table
  1353.     mov    eax,[eax]
  1354.     and    eax,not (4096-1)        
  1355.     call   MEM_Get_Linear_Address;calculate linear page address
  1356.     lea    edi,[eax+ebp]         ;add page offset
  1357.     mov    ecx,512/4
  1358.     cld
  1359.     rep    movsd                 ;copy data
  1360.    
  1361.     pop    ecx
  1362.     inc    ecx                   ;next block
  1363.     mov    eax,[app_i_end] ;todo: precalculate ([app_i_end]+4095)/4096
  1364.     add    eax,512-1
  1365.     shr    eax,9                 ;calculate application image size
  1366.     cmp    ecx,eax
  1367.     jg     .endloop11
  1368.     pop    edx
  1369.     add    ebp,512               ;new offset
  1370.     test   ebp,4096
  1371.     jz     .loop1
  1372.     xor    ebp,ebp
  1373.     add    edx,4                 ;go to next page
  1374.     test   edx,(4096-1)
  1375.     jnz    .loop1
  1376.     add    dword [esp],4         ;go to next directory entry
  1377.     mov    eax,[esp]
  1378.     mov    eax,[eax]
  1379.     and    eax,not (4096-1)
  1380.     call   MEM_Get_Linear_Address
  1381.     mov    edx,eax
  1382.     jmp    .loop1
  1383. .endloop1:
  1384.     add    esp,4                 ;pop ecx
  1385. .endloop11:
  1386.     add    esp,4+4               ;pop edx, pop edx
  1387.    
  1388. ;add_app_parameters
  1389.     add    esp,12                ;now pointer to parameters is on the top of the stack
  1390.     call   new_start_application_fl.add_app_parameters ;start process
  1391.     mov    [esp+28],eax
  1392.     popad
  1393.     ret
  1394.    
  1395. .cleanfailed_mem1:
  1396. ;there is mem for directory entry, but there is no mem for pages
  1397. ;so free directory entry
  1398.     mov    eax,[new_process_place]
  1399.     shl    eax,8
  1400.     mov    eax,[0x80000+eax+APPDATA.dir_table]
  1401.     call   MEM_Free_Page
  1402. .cleanfailed_mem:
  1403. ;there is no mem for directory entry, display message.
  1404.     mov    esi,start_not_enough_memory
  1405.     call   sys_msg_board_str    
  1406. .cleanfailed:                    ;clean process name
  1407.         push    ecx
  1408. ;can't read file, clean process name.
  1409. ;this avoid problems with panel application.
  1410.     mov    edi,[new_process_place]
  1411.     shl    edi,8
  1412.     add    edi,0x80000+APPDATA.app_name
  1413.     mov    ecx,11
  1414.     mov    eax,' '
  1415.     cld
  1416.     rep    stosb
  1417.         pop     eax
  1418. .failed:
  1419. ;no more slots
  1420.     add    esp,16
  1421.     mov    [esp+1Ch], eax
  1422.     popad
  1423.     mov    [application_table_status],0
  1424.     sti
  1425.     ret
  1426. end if
  1427.  
  1428. ; \begin{diamond}
  1429.         include 'debug.inc'
  1430.  
  1431. fs_execute:
  1432. ; ebx - cmdline
  1433. ; edx - flags
  1434. ; ebp - full filename
  1435. ; [esp+4] = procedure DoRead, [esp+8] = filesize & [esp+12]... - arguments for it
  1436.         pushad
  1437. ; check filename length - with terminating NULL must be no more than 1024 symbols
  1438.         mov     edi, ebp
  1439.         mov     ecx, 1024
  1440.         xor     eax, eax
  1441.         repnz   scasb
  1442.         jz      @f
  1443.         popad
  1444.         mov     eax, -ERROR_FILE_NOT_FOUND
  1445.         ret
  1446. @@:
  1447.  
  1448.         mov     esi, new_process_loading
  1449.         call    sys_msg_board_str       ; write message to message board
  1450.  
  1451. ; lock application_table_status mutex
  1452. .table_status:
  1453.         cli
  1454.         cmp     [application_table_status], 0
  1455.         jz      .stf
  1456.         sti
  1457.         call    change_task
  1458.         jmp     .table_status
  1459. .stf:
  1460.         call    set_application_table_status
  1461.         push    ebx     ; save command line pointer for add_app_parameters
  1462.  
  1463.         call    find_new_process_place  ; find new process slot
  1464.         call    safe_sti
  1465.         test    eax, eax
  1466.         mov     ecx, -0x20      ; too many processes
  1467.         jz      .failed
  1468.  
  1469. ; write application name
  1470.         push    edi
  1471.         mov     ecx, edi
  1472.         sub     ecx, ebp
  1473.         mov     [appl_path], ebp
  1474.         mov     [appl_path_size], ecx
  1475.         dec     edi
  1476.         std
  1477.         mov     al, '/'
  1478.         repnz   scasb
  1479.         cld
  1480.         jnz     @f
  1481.         inc     edi
  1482. @@:
  1483.         inc     edi
  1484. ; now edi points to name without path
  1485.         mov     esi, edi
  1486.         mov     ecx, 8  ; 8 chars for name
  1487.         mov     edi, [new_process_place]
  1488.         shl     edi, cl
  1489.         add     edi, 0x80000+APPDATA.app_name
  1490. .copy_process_name_loop:
  1491.         lodsb
  1492.         cmp     al, '.'
  1493.         jz      .copy_process_name_done
  1494.         test    al, al
  1495.         jz      .copy_process_name_done
  1496.         stosb
  1497.         loop    .copy_process_name_loop
  1498. .copy_process_name_done:
  1499.         mov     al, ' '
  1500.         rep     stosb
  1501.         pop     eax
  1502.         mov     cl, 3   ; 3 chars for extension
  1503.         dec     esi
  1504. @@:
  1505.         dec     eax
  1506.         cmp     eax, esi
  1507.         jbe     .copy_process_ext_done
  1508.         cmp     byte [eax], '.'
  1509.         jnz     @b
  1510.         lea     esi, [eax+1]
  1511. .copy_process_ext_loop:
  1512.         lodsb
  1513.         test    al, al
  1514.         jz      .copy_process_ext_done
  1515.         stosb
  1516.         loop    .copy_process_ext_loop
  1517. .copy_process_ext_done:
  1518.         mov     al, ' '
  1519.         rep     stosb
  1520.  
  1521. ; read header
  1522.         lea     eax, [esp+8+36]
  1523.         mov     edi, 0x90000
  1524.         call    dword [eax-4]
  1525.         mov     ecx, eax
  1526.         neg     ecx
  1527.         jnz     .cleanfailed
  1528. ; check menuet signature
  1529.         mov     ecx, -0x1F
  1530.         cmp     dword [0x90000], 'MENU'
  1531.         jnz     .cleanfailed
  1532.         cmp     word [0x90004], 'ET'
  1533.         jnz     .cleanfailed
  1534.         call    get_app_params
  1535.         mov     ecx, -0x1F
  1536.         jc      .cleanfailed
  1537. ; sanity check - because we will load all file,
  1538. ; file size must be not greater than memory size
  1539.         mov     eax, [esp+8+36]
  1540.         cmp     [app_mem], eax
  1541.         jb      .cleanfailed
  1542.  
  1543.         mov     eax, [new_process_place]
  1544.         inc     ecx     ; -0x1E = no memory
  1545.         call    create_app_cr3_table
  1546.         test    eax, eax
  1547.         jz      .cleanfailed_mem
  1548.  
  1549.         call    MEM_Get_Linear_Address
  1550.  
  1551.         mov     ebx, std_application_base_address
  1552.         mov     ecx, [app_mem]
  1553.         add     ecx, 4095
  1554.         shr     ecx, 12
  1555.         mov     edx, eax        ; edx - linear address of page directory
  1556.         call    mem_alloc_specified_region
  1557.         mov     ecx, -0x1E      ; no memory
  1558.         test    eax, eax
  1559.         jz      .cleanfailed_mem1
  1560.  
  1561.         add     edx, std_application_base_address shr 20
  1562.         mov     eax, [edx]
  1563.         and     eax, not 4095
  1564.         call    MEM_Get_Linear_Address
  1565.         push    edx             ; save pointer to first page table
  1566.         mov     edx, eax
  1567. ; read file
  1568. ; first block is already read to 0x90000
  1569.         mov     eax, [edx]
  1570.         and     eax, not 0xFFF
  1571.         call    MEM_Get_Linear_Address
  1572.         mov     esi, 0x90000
  1573.         mov     edi, eax
  1574.         mov     ecx, 512/4
  1575.         rep     movsd
  1576.         sub     edi, eax
  1577. .loop1:
  1578. ; [esp] = pointer to current page directory entry
  1579. ; edx = pointer to current page table
  1580. ; edi = offset in page
  1581.         mov     eax, [edx]
  1582.         and     eax, not 0xFFF
  1583.         call    MEM_Get_Linear_Address
  1584.         push    edi
  1585.         add     edi, eax
  1586.         lea     eax, [esp+8+36+8]
  1587.         call    dword [eax-4]
  1588.         pop     edi
  1589.         test    eax, eax
  1590.         jnz     .endloop1
  1591.         add     edi, 512        ; new offset
  1592.         cmp     edi, 4096
  1593.         jb      .loop1
  1594.         xor     edi, edi
  1595.         add     edx, 4          ; go to next page
  1596.         test    edx, 4096-1
  1597.         jnz     .loop1
  1598.         pop     eax
  1599.         add     eax, 4          ; go to next directory entry
  1600.         push    eax
  1601.         mov     eax, [eax]
  1602.         and     eax, not 0xFFF
  1603.         call    MEM_Get_Linear_Address
  1604.         mov     edx, eax
  1605.         jmp     .loop1
  1606. .endloop1:
  1607.         pop     edx
  1608.         cmp     eax, 6
  1609.         jnz     .cleanfailed_mem2
  1610.         call    new_start_application_fl.add_app_parameters
  1611.         mov     [esp+28], eax
  1612.         popad
  1613.         ret
  1614.  
  1615. .cleanfailed_mem2:
  1616. ; file read error; free all allocated mem
  1617.         mov     ecx, eax
  1618.         neg     ecx
  1619.         mov     eax, [new_process_place]
  1620.         call    dispose_app_cr3_table
  1621.         jmp     .cleanfailed
  1622. .cleanfailed_mem1:
  1623. ; there is mem for directory entry, but there is no mem for pages
  1624. ; so free directory entry
  1625.         mov     eax, [new_process_place]
  1626.         shl     eax, 8
  1627.         mov     eax, [0x80000+eax+0xB8]
  1628.         call    MEM_Free_Page
  1629. .cleanfailed_mem:
  1630. ; there is no mem for directory entry, display message
  1631.         mov     esi, start_not_enough_memory
  1632.         call    sys_msg_board_str
  1633. .cleanfailed:
  1634.         push    ecx
  1635. ; clean process name, this avoid problems with @panel
  1636.         mov     edi, [new_process_place]
  1637.         shl     edi, 8
  1638.         add     edi, 0x80000+APPDATA.app_name
  1639.         mov     ecx, 11
  1640.         mov     al, ' '
  1641.         rep     stosb
  1642.         pop     eax
  1643. .failed:
  1644.         pop     ebx
  1645.         mov     [esp+28], eax
  1646.         popad
  1647.         mov     [application_table_status], 0
  1648.         call    safe_sti
  1649.         ret
  1650. ; \end{diamond}
  1651.