Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2004-2016. All rights reserved. ;;
  4. ;;  Distributed under terms of the GNU General Public License.  ;;
  5. ;;                                                              ;;
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7.  
  8. $Revision: 6817 $
  9.  
  10. align 4 ;3A08
  11. build_interrupt_table:
  12.         mov     edi, idts
  13.         mov     esi, sys_int
  14.         mov     ecx, 0x40
  15.         mov     eax, (10001110b shl 24) + os_code
  16.   @@:
  17.         movsw   ;low word of code-entry
  18.         stosd   ;interrupt gate type : os_code selector
  19.         movsw   ;high word of code-entry
  20.         loop    @b
  21.         movsd   ;copy low  dword of trap gate for int 0x40
  22.         movsd   ;copy high dword of trap gate for int 0x40
  23.         lidt    [esi]
  24.         ret
  25.  
  26. iglobal
  27.   align 4
  28.   sys_int:
  29.     ;exception handlers addresses (for interrupt gate construction)
  30.         dd      e0,e1,e2,e3,e4,e5,e6,except_7 ; SEE: core/fpu.inc
  31.         dd      e8,e9,e10,e11,e12,e13,page_fault_exc,e15
  32.         dd      e16, e17,e18, e19
  33.         times   12 dd unknown_interrupt ;int_20..int_31
  34.  
  35.     ;interrupt handlers addresses (for interrupt gate construction)
  36.         ; 0x20 .. 0x2F - IRQ handlers
  37.         dd      irq0, irq_serv.irq_1, irq_serv.irq_2
  38.         dd      irq_serv.irq_3, irq_serv.irq_4
  39.         dd      irq_serv.irq_5,  irq_serv.irq_6,  irq_serv.irq_7
  40.         dd      irq_serv.irq_8,  irq_serv.irq_9,  irq_serv.irq_10
  41.         dd      irq_serv.irq_11, irq_serv.irq_12, irqD, irq_serv.irq_14, irq_serv.irq_15
  42.         dd irq_serv.irq_16
  43.         dd irq_serv.irq_17
  44.         dd irq_serv.irq_18
  45.         dd irq_serv.irq_19
  46.         dd irq_serv.irq_20
  47.         dd irq_serv.irq_21
  48.         dd irq_serv.irq_22
  49.         dd irq_serv.irq_23
  50.  
  51.     times 32 - IRQ_RESERVED dd unknown_interrupt
  52.     ;int_0x40 gate trap (for directly copied)
  53.         dw      i40 and 0xFFFF, os_code, 11101111b shl 8, i40 shr 16
  54.  
  55.   idtreg: ; data for LIDT instruction (!!! must be immediately below sys_int data)
  56.         dw      2*($-sys_int-4)-1
  57.         dd      idts ;0x8000B100
  58.         dw      0    ;просто выравнивание
  59.  
  60.   msg_fault_sel dd  msg_exc_8,msg_exc_u,msg_exc_a,msg_exc_b
  61.                 dd  msg_exc_c,msg_exc_d,msg_exc_e,msg_exc_u
  62.                 dd  msg_exc_u,msg_exc_11
  63.  
  64.   msg_exc_8     db "Double fault", 0
  65.   msg_exc_u     db "Undefined Exception", 0
  66.   msg_exc_a     db "Invalid TSS", 0
  67.   msg_exc_b     db "Segment not present", 0
  68.   msg_exc_c     db "Stack fault", 0
  69.   msg_exc_d     db "General protection fault", 0
  70.   msg_exc_e     db "Page fault", 0
  71.   msg_exc_11    db "Alignment Check", 0
  72.  
  73.   if lang eq sp
  74.     include 'core/sys32-sp.inc'
  75.   else
  76.     msg_sel_ker   db "kernel", 0
  77.     msg_sel_app   db "application", 0
  78.   end if
  79.  
  80. endg
  81.  
  82. macro save_ring3_context {
  83.         pushad
  84. }
  85. macro restore_ring3_context {
  86.         popad
  87. }
  88. macro exc_wo_code [num] {
  89.   e#num :
  90.         save_ring3_context
  91.         mov     bl, num
  92.         jmp     exc_c
  93. } exc_wo_code   0,1,2,3,4,5,6,15,16,19
  94.  
  95. macro exc_w_code [num] {
  96.   e#num :
  97.         add     esp, 4
  98.         save_ring3_context
  99.         mov     bl, num
  100.         jmp     exc_c
  101. } exc_w_code    8,9,10,11,12,13,17,18
  102.  
  103.  
  104. uglobal
  105.   pf_err_code   dd ?
  106. endg
  107.  
  108. page_fault_exc:                 ; дуракоусточивость: селекторы испорчены...
  109.         pop     [ss:pf_err_code]; действительно до следующего #PF
  110.         save_ring3_context
  111.         mov     bl, 14
  112.  
  113. exc_c:                          ; исключения (все, кроме 7-го - #NM)
  114. ; Фрэйм стека при исключении/прерывании из 3-го кольца + pushad (т.е., именно здесь)
  115.   reg_ss        equ esp+0x30
  116.   reg_esp3      equ esp+0x2C
  117.   reg_eflags    equ esp+0x28
  118.   reg_cs3       equ esp+0x24
  119.   reg_eip       equ esp+0x20
  120.  ; это фрэйм от pushad
  121.   reg_eax       equ esp+0x1C
  122.   reg_ecx       equ esp+0x18
  123.   reg_edx       equ esp+0x14
  124.   reg_ebx       equ esp+0x10
  125.   reg_esp0      equ esp+0x0C
  126.   reg_ebp       equ esp+0x08
  127.   reg_esi       equ esp+0x04
  128.   reg_edi       equ esp+0x00
  129.  
  130.         mov     ax, app_data        ;исключение
  131.         mov     ds, ax                  ;загрузим правильные значения
  132.         mov     es, ax                  ;в регистры
  133.         cld                     ; и приводим DF к стандарту
  134.         movzx   ebx, bl
  135. ; redirect to V86 manager? (EFLAGS & 0x20000) != 0?
  136.         test    byte[reg_eflags+2], 2
  137.         jnz     v86_exc_c
  138.         cmp     bl, 14          ; #PF
  139.         jne     @f
  140.         call    page_fault_handler ; SEE: core/memory.inc
  141.   @@:
  142.         mov     esi, [current_slot]
  143.         btr     [esi+APPDATA.except_mask], ebx
  144.         jnc     @f
  145.         mov     eax, [esi+APPDATA.exc_handler]
  146.         test    eax, eax
  147.         jnz     IRetToUserHook
  148.   @@:
  149.         cli
  150.         mov     eax, [esi+APPDATA.debugger_slot]
  151.         test    eax, eax
  152.         jnz     .debug
  153. ; not debuggee => say error and terminate
  154.         call    show_error_parameters
  155.         sti
  156.         mov     [edx + TASKDATA.state], byte 4 ; terminate
  157.         call    wakeup_osloop
  158.         call    change_task
  159. ; If we're here, then the main OS thread has crashed before initializing IDLE thread.
  160. ; Or they both have crashed. Anyway, things are hopelessly broken.
  161.         hlt
  162.         jmp     $-1
  163. .debug:
  164. ; we are debugged process, notify debugger and suspend ourself
  165. ; eax=debugger PID
  166.         mov     ecx, 1          ; debug_message code=other_exception
  167.         cmp     bl, 1           ; #DB
  168.         jne     .notify         ; notify debugger and suspend ourself
  169.         mov     ebx, dr6        ; debug_message data=DR6_image
  170.         xor     edx, edx
  171.         mov     dr6, edx
  172.         mov     edx, dr7
  173.         mov     cl, not 8
  174.   .l1:
  175.         shl     dl, 2
  176.         jc      @f
  177.         and     bl, cl
  178.   @@:
  179.         sar     cl, 1
  180.         jc      .l1
  181.         mov     cl, 3           ; debug_message code=debug_exception
  182. .notify:
  183.         push    ebx             ; debug_message data
  184.         mov     ebx, [TASK_BASE]
  185.         push    [ebx+TASKDATA.pid] ; PID
  186.         push    ecx             ; debug_message code ((here: ecx==1/3))
  187.         mov     cl, 12          ; debug_message size
  188.         call    debugger_notify ;; only ONE using, inline ??? SEE: core/debug.inc
  189.         add     esp, 12
  190.         mov     edx, [TASK_BASE]
  191.         mov     byte [edx+TASKDATA.state], 1 ; suspended
  192.         call    change_task     ; SEE: core/shed.inc
  193.         restore_ring3_context
  194.         iretd
  195.  
  196. IRetToUserHook:
  197.         xchg    eax, [reg_eip]
  198.         sub     dword[reg_esp3], 8
  199.         mov     edi, [reg_esp3]
  200.         stosd
  201.         mov     [edi], ebx
  202.         restore_ring3_context
  203. ; simply return control to interrupted process
  204. unknown_interrupt:
  205.         iretd
  206.  
  207. ;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  208. ; bl - error vector
  209. show_error_parameters:
  210.         cmp     bl, 0x06
  211.         jnz     .no_ud
  212.         push    ebx
  213.         mov     ebx, ud_user_message
  214.         mov     ebp, notifyapp
  215.         call    fs_execute_from_sysdir_param
  216.         pop     ebx
  217. .no_ud:
  218.         mov     edx, [TASK_BASE];not scratched below
  219.         if lang eq sp
  220.         DEBUGF  1, "K : Proceso - terminado forzado PID: %x [%s]\n", [edx+TASKDATA.pid], [current_slot]
  221.         else
  222.         DEBUGF  1, "K : Process - forced terminate PID: %x [%s]\n", [edx+TASKDATA.pid], [current_slot]
  223.         end if
  224.         cmp     bl, 0x08
  225.         jb      .l0
  226.         cmp     bl, 0x11
  227.         jbe     .l1
  228.   .l0:
  229.         mov     bl, 0x09
  230.   .l1:
  231.         mov     eax, [msg_fault_sel+ebx*4 - 0x08*4]
  232.         DEBUGF  1, "K : %s\n", eax
  233.         mov     eax, [reg_cs3+4]
  234.         mov     edi, msg_sel_app
  235.         mov     ebx, [reg_esp3+4]
  236.         cmp     eax, app_code
  237.         je      @f
  238.         mov     edi, msg_sel_ker
  239.         mov     ebx, [reg_esp0+4]
  240.     @@:
  241.         DEBUGF  1, "K : EAX : %x EBX : %x ECX : %x\n", [reg_eax+4], [reg_ebx+4], [reg_ecx+4]
  242.         DEBUGF  1, "K : EDX : %x ESI : %x EDI : %x\n", [reg_edx+4], [reg_esi+4], [reg_edi+4]
  243.         DEBUGF  1, "K : EBP : %x EIP : %x ESP : %x\n", [reg_ebp+4], [reg_eip+4], ebx
  244.         DEBUGF  1, "K : Flags : %x CS : %x (%s)\n", [reg_eflags+4], eax, edi
  245.  
  246.         DEBUGF  1, "K : Stack dump:\n"
  247.         push    eax ebx ecx edx
  248.         call    .check_ESP
  249.         test    eax, eax
  250.         jnz     .error_ESP
  251.         DEBUGF  1, "K : [ESP+00]: %x",[ebx]
  252.         add     ebx, 4
  253.         call    .check_ESP
  254.         test    eax, eax
  255.         jnz     .error_ESP
  256.         DEBUGF  1, " [ESP+04]: %x",[ebx]
  257.         add     ebx, 4
  258.         call    .check_ESP
  259.         test    eax, eax
  260.         jnz     .error_ESP
  261.         DEBUGF  1, " [ESP+08]: %x\n",[ebx]
  262.         add     ebx, 4
  263.         call    .check_ESP
  264.         test    eax, eax
  265.         jnz     .error_ESP
  266.         DEBUGF  1, "K : [ESP+12]: %x",[ebx]
  267.         add     ebx, 4
  268.         call    .check_ESP
  269.         test    eax, eax
  270.         jnz     .error_ESP
  271.         DEBUGF  1, " [ESP+16]: %x",[ebx]
  272.         add     ebx, 4
  273.         call    .check_ESP
  274.         test    eax, eax
  275.         jnz     .error_ESP
  276.         DEBUGF  1, " [ESP+20]: %x\n",[ebx]
  277.         add     ebx, 4
  278.         call    .check_ESP
  279.         test    eax, eax
  280.         jnz     .error_ESP
  281.         DEBUGF  1, "K : [ESP+24]: %x",[ebx]
  282.         add     ebx, 4
  283.         call    .check_ESP
  284.         test    eax, eax
  285.         jnz     .error_ESP
  286.         DEBUGF  1, " [ESP+28]: %x",[ebx]
  287.         add     ebx, 4
  288.         call    .check_ESP
  289.         test    eax, eax
  290.         jnz     .error_ESP
  291.         DEBUGF  1, " [ESP+32]: %x\n",[ebx]
  292.         pop     edx ecx ebx eax
  293.         ret
  294. .error_ESP:
  295.         pop     edx ecx ebx eax
  296.         DEBUGF  1, "\n"
  297.         DEBUGF  1, "K : Unexpected end of the stack\n"
  298.         ret
  299. ;--------------------------------------
  300. .check_ESP:
  301.         push    ebx
  302.         shr     ebx, 12
  303.         mov     ecx, ebx
  304.         shr     ecx, 10
  305.         mov     edx, [master_tab+ecx*4]
  306.         test    edx, PG_READ
  307.         jz      .fail             ;page table is not created
  308.                                   ;incorrect address in the program
  309.  
  310.         mov     eax, [page_tabs+ebx*4]
  311.         test    eax, 2
  312.         jz      .fail             ;address not reserved for use. error
  313.  
  314.         pop     ebx
  315.         xor     eax, eax
  316.         ret
  317.  
  318. .fail:
  319.         pop     ebx
  320.         xor     eax, eax
  321.         dec     eax
  322.         ret
  323. ;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  324.  
  325.   restore  reg_ss
  326.   restore  reg_esp3
  327.   restore  reg_eflags
  328.   restore  reg_cs
  329.   restore  reg_eip
  330.   restore  reg_eax
  331.   restore  reg_ecx
  332.   restore  reg_edx
  333.   restore  reg_ebx
  334.   restore  reg_esp0
  335.   restore  reg_ebp
  336.   restore  reg_esi
  337.   restore  reg_edi
  338.  
  339.  
  340. align 4
  341. lock_application_table:
  342.         push    eax ecx edx
  343.         mov     ecx, application_table_mutex
  344.         call    mutex_lock
  345.  
  346.         mov     eax, [CURRENT_TASK]
  347.         shl     eax, 5
  348.         add     eax, CURRENT_TASK+TASKDATA.pid
  349.         mov     eax, [eax]
  350.  
  351.         mov     [application_table_owner], eax
  352.  
  353.         pop     edx ecx eax
  354.  
  355.         ret
  356.  
  357. align 4
  358. unlock_application_table:
  359.         push    eax ecx edx
  360.  
  361.         mov     [application_table_owner], 0
  362.         mov     ecx, application_table_mutex
  363.         call    mutex_unlock
  364.  
  365.         pop     edx ecx eax
  366.  
  367.         ret
  368.  
  369. ;  * eax = 64 - номер функции
  370. ;  * ebx = 1 - единственная подфункция
  371. ;  * ecx = новый размер памяти
  372. ;Возвращаемое значение:
  373. ;  * eax = 0 - успешно
  374. ;  * eax = 1 - недостаточно памяти
  375.  
  376. align 4
  377. sys_resize_app_memory:
  378.         ; ebx = 1 - resize
  379.         ; ecx = new amount of memory
  380.  
  381. ;        cmp    eax,1
  382.         dec     ebx
  383.         jnz     .no_application_mem_resize
  384.  
  385.         mov     eax, [pg_data.pages_free]
  386.         shl     eax, 12
  387.         cmp     eax, ecx
  388.         jae     @f
  389.  
  390.         xor     eax, eax
  391.         inc     eax
  392.         jmp     .store_result
  393. @@:
  394.         stdcall new_mem_resize, ecx
  395. .store_result:
  396.         mov     [esp+32], eax
  397. .no_application_mem_resize:
  398.         ret
  399.  
  400. iglobal
  401. ;  process_terminating  db 'K : Process - terminating',13,10,0
  402. ;  process_terminated   db 'K : Process - done',13,10,0
  403.   msg_obj_destroy       db 'K : destroy app object',13,10,0
  404. endg
  405.  
  406. ; param
  407. ;  esi= slot
  408.  
  409. align 4
  410. terminate: ; terminate application
  411. destroy_thread:
  412.  
  413.         .slot     equ esp+4             ;locals
  414.         .process  equ esp               ;ptr to parent process
  415.  
  416.  
  417.         push    esi        ;save .slot
  418.  
  419.         shl     esi, 8
  420.         mov     edx, [SLOT_BASE+esi+APPDATA.process]
  421.         test    edx, edx
  422.         jnz     @F
  423.         pop     esi
  424.         shl     esi, 5
  425.         mov     [CURRENT_TASK+esi+TASKDATA.state], 9
  426.         ret
  427. @@:
  428.         push    edx                     ;save .process
  429.         lea     edx, [SLOT_BASE+esi]
  430.         call    scheduler_remove_thread
  431.         call    lock_application_table
  432.  
  433. ; if the process is in V86 mode...
  434.         mov     eax, [.slot]
  435.         shl     eax, 8
  436.         mov     esi, [eax+SLOT_BASE+APPDATA.pl0_stack]
  437.         add     esi, RING0_STACK_SIZE
  438.         cmp     [eax+SLOT_BASE+APPDATA.saved_esp0], esi
  439.         jz      .nov86
  440. ; ...it has page directory for V86 mode
  441.         mov     esi, [eax+SLOT_BASE+APPDATA.saved_esp0]
  442.         mov     ecx, [esi+4]
  443.         mov     [eax+SLOT_BASE+APPDATA.process], ecx
  444. ; ...and I/O permission map for V86 mode
  445.         mov     ecx, [esi+12]
  446.         mov     [eax+SLOT_BASE+APPDATA.io_map], ecx
  447.         mov     ecx, [esi+8]
  448.         mov     [eax+SLOT_BASE+APPDATA.io_map+4], ecx
  449. .nov86:
  450. ;destroy per-thread kernel objects
  451.         mov     esi, [.slot]
  452.         shl     esi, 8
  453.         add     esi, SLOT_BASE+APP_OBJ_OFFSET
  454. @@:
  455.         mov     eax, [esi+APPOBJ.fd]
  456.         test    eax, eax
  457.         jz      @F
  458.  
  459.         cmp     eax, esi
  460.         je      @F
  461.  
  462.         push    esi
  463.         call    [eax+APPOBJ.destroy]
  464.            DEBUGF 1,"%s",msg_obj_destroy
  465.         pop     esi
  466.         jmp     @B
  467. @@:
  468.         mov     esi, [.slot]
  469.         cmp     [fpu_owner], esi ; if user fpu last -> fpu user = 2
  470.         jne     @F
  471.  
  472.         mov     [fpu_owner], 2
  473.         mov     eax, [256*2+SLOT_BASE+APPDATA.fpu_state]
  474.         clts
  475.         bt      [cpu_caps], CAPS_SSE
  476.         jnc     .no_SSE
  477.         fxrstor [eax]
  478.         jmp     @F
  479. .no_SSE:
  480.         fnclex
  481.         frstor  [eax]
  482. @@:
  483.  
  484.         mov     [KEY_COUNT], byte 0    ; empty keyboard buffer
  485.         mov     [BTN_COUNT], byte 0    ; empty button buffer
  486.  
  487.  
  488. ; remove defined hotkeys
  489.         mov     eax, hotkey_list
  490. .loop:
  491.         cmp     [eax+8], esi
  492.         jnz     .cont
  493.         mov     ecx, [eax]
  494.         jecxz   @f
  495.         push    dword [eax+12]
  496.         pop     dword [ecx+12]
  497. @@:
  498.         mov     ecx, [eax+12]
  499.         push    dword [eax]
  500.         pop     dword [ecx]
  501.         xor     ecx, ecx
  502.         mov     [eax], ecx
  503.         mov     [eax+4], ecx
  504.         mov     [eax+8], ecx
  505.         mov     [eax+12], ecx
  506. .cont:
  507.         add     eax, 16
  508.         cmp     eax, hotkey_list+256*16
  509.         jb      .loop
  510. ; get process PID
  511.         mov     eax, esi
  512.         shl     eax, 5
  513.         mov     eax, [eax+CURRENT_TASK+TASKDATA.pid]
  514. ; compare current lock input with process PID
  515.         cmp     eax, [PID_lock_input]
  516.         jne     @f
  517.  
  518.         xor     eax, eax
  519.         mov     [PID_lock_input], eax
  520. @@:
  521. ; remove hotkeys in buffer
  522.         mov     eax, hotkey_buffer
  523. .loop2:
  524.         cmp     [eax], esi
  525.         jnz     .cont2
  526.         and     dword [eax+4], 0
  527.         and     dword [eax], 0
  528. .cont2:
  529.         add     eax, 8
  530.         cmp     eax, hotkey_buffer+120*8
  531.         jb      .loop2
  532.  
  533.         mov     ecx, esi          ; remove buttons
  534.   bnewba2:
  535.         mov     edi, [BTN_ADDR]
  536.         mov     eax, edi
  537.         cld
  538.         movzx   ebx, word [edi]
  539.         inc     bx
  540.   bnewba:
  541.         dec     bx
  542.         jz      bnmba
  543.         add     eax, 0x10
  544.         cmp     cx, [eax]
  545.         jnz     bnewba
  546.         pusha
  547.         mov     ecx, ebx
  548.         inc     ecx
  549.         shl     ecx, 4
  550.         mov     ebx, eax
  551.         add     eax, 0x10
  552.         call    memmove
  553.         dec     dword [edi]
  554.         popa
  555.         jmp     bnewba2
  556.   bnmba:
  557.  
  558.         pusha   ; save window coordinates for window restoring
  559.         cld
  560.         shl     esi, 5
  561.         add     esi, window_data
  562.         mov     eax, [esi+WDATA.box.left]
  563.         mov     [draw_limits.left], eax
  564.         add     eax, [esi+WDATA.box.width]
  565.         mov     [draw_limits.right], eax
  566.         mov     eax, [esi+WDATA.box.top]
  567.         mov     [draw_limits.top], eax
  568.         add     eax, [esi+WDATA.box.height]
  569.         mov     [draw_limits.bottom], eax
  570.  
  571.         xor     eax, eax
  572.         mov     [esi+WDATA.box.left], eax
  573.         mov     [esi+WDATA.box.width], eax
  574.         mov     [esi+WDATA.box.top], eax
  575.         mov     [esi+WDATA.box.height], eax
  576.         mov     [esi+WDATA.cl_workarea], eax
  577.         mov     [esi+WDATA.cl_titlebar], eax
  578.         mov     [esi+WDATA.cl_frames], eax
  579.         mov     dword [esi+WDATA.z_modif], eax; clear all flags: z_modif, wstate, redraw, wdrawn
  580.         lea     edi, [esi-window_data+draw_data]
  581.         mov     ecx, 32/4
  582.         rep stosd
  583.         popa
  584.  
  585. ; debuggee test
  586.         pushad
  587.         mov     edi, esi
  588.         shl     edi, 5
  589.         mov     eax, [SLOT_BASE+edi*8+APPDATA.debugger_slot]
  590.         test    eax, eax
  591.         jz      .nodebug
  592.         movi    ecx, 8
  593.         push    dword [CURRENT_TASK+edi+TASKDATA.pid]; PID
  594.         push    2
  595.         call    debugger_notify
  596.         pop     ecx
  597.         pop     ecx
  598. .nodebug:
  599.         popad
  600.  
  601.         mov     ebx, [.slot]
  602.         shl     ebx, 8
  603.         push    ebx
  604.         mov     ebx, [SLOT_BASE+ebx+APPDATA.pl0_stack]
  605.  
  606.         stdcall kernel_free, ebx
  607.  
  608.         pop     ebx
  609.         mov     ebx, [SLOT_BASE+ebx+APPDATA.cur_dir]
  610.         stdcall kernel_free, ebx
  611.  
  612.         mov     edi, [.slot]
  613.         shl     edi, 8
  614.         add     edi, SLOT_BASE
  615.  
  616.         mov     eax, [edi+APPDATA.io_map]
  617.         cmp     eax, [SLOT_BASE+256+APPDATA.io_map]
  618.         je      @F
  619.         call    free_page
  620. @@:
  621.         mov     eax, [edi+APPDATA.io_map+4]
  622.         cmp     eax, [SLOT_BASE+256+APPDATA.io_map+4]
  623.         je      @F
  624.         call    free_page
  625. @@:
  626.         lea     ebx, [edi+APPDATA.list]
  627.         list_del ebx                    ;destroys edx, ecx
  628.  
  629.         mov     eax, 0x20202020
  630.         stosd
  631.         stosd
  632.         stosd
  633.         mov     ecx, 244/4
  634.         xor     eax, eax
  635.         rep stosd
  636.  
  637.   ; activate window
  638.         movzx   eax, word [WIN_STACK + esi*2]
  639.         cmp     eax, [TASK_COUNT]
  640.         jne     .dont_activate
  641.         pushad
  642.  .check_next_window:
  643.         dec     eax
  644.         cmp     eax, 1
  645.         jbe     .nothing_to_activate
  646.         lea     esi, [WIN_POS+eax*2]
  647.         movzx   edi, word [esi]              ; edi = process
  648.         shl     edi, 5
  649.         cmp     [CURRENT_TASK + edi + TASKDATA.state], byte 9 ; skip dead slots
  650.         je      .check_next_window
  651.         add     edi, window_data
  652. ; \begin{diamond}[19.09.2006]
  653. ; skip minimized windows
  654.         test    [edi + WDATA.fl_wstate], WSTATE_MINIMIZED
  655.         jnz     .check_next_window
  656. ; \end{diamond}
  657.         call    waredraw
  658.  .nothing_to_activate:
  659.         popad
  660.  .dont_activate:
  661.  
  662.         push    esi     ; remove hd1 & cd & flp reservation
  663.         shl     esi, 5
  664.         mov     esi, [esi+CURRENT_TASK+TASKDATA.pid]
  665.         cmp     [cd_status], esi
  666.         jnz     @f
  667.         call    free_cd_channel
  668.         and     [cd_status], 0
  669. @@:
  670.         pop     esi
  671.         cmp     [bgrlockpid], esi
  672.         jnz     @f
  673.         and     [bgrlockpid], 0
  674.         and     [bgrlock], 0
  675. @@:
  676.  
  677.         pusha                 ; remove all port reservations
  678.         mov     edx, esi
  679.         shl     edx, 5
  680.         add     edx, CURRENT_TASK
  681.         mov     edx, [edx+TASKDATA.pid]
  682.  
  683.   rmpr0:
  684.  
  685.         mov     esi, [RESERVED_PORTS]
  686.  
  687.         test    esi, esi
  688.         jz      rmpr9
  689.  
  690.   rmpr3:
  691.  
  692.         mov     edi, esi
  693.         shl     edi, 4
  694.         add     edi, RESERVED_PORTS
  695.  
  696.         cmp     edx, [edi]
  697.         je      rmpr4
  698.  
  699.         dec     esi
  700.         jnz     rmpr3
  701.  
  702.         jmp     rmpr9
  703.  
  704.   rmpr4:
  705.  
  706.         mov     ecx, 256
  707.         sub     ecx, esi
  708.         shl     ecx, 4
  709.  
  710.         mov     esi, edi
  711.         add     esi, 16
  712.         cld
  713.         rep movsb
  714.  
  715.         dec     dword [RESERVED_PORTS]
  716.  
  717.         jmp     rmpr0
  718.  
  719.   rmpr9:
  720.  
  721.         popa
  722.         mov     edi, esi ; do not run this process slot
  723.         shl     edi, 5
  724.         mov     [edi+CURRENT_TASK + TASKDATA.state], byte 9
  725. ; debugger test - terminate all debuggees
  726.         mov     eax, 2
  727.         mov     ecx, SLOT_BASE+2*0x100+APPDATA.debugger_slot
  728. .xd0:
  729.         cmp     eax, [TASK_COUNT]
  730.         ja      .xd1
  731.         cmp     dword [ecx], esi
  732.         jnz     @f
  733.         and     dword [ecx], 0
  734.         pushad
  735.         xchg    eax, ecx
  736.         mov     ebx, 2
  737.         call    sys_system
  738.         popad
  739. @@:
  740.         inc     eax
  741.         add     ecx, 0x100
  742.         jmp     .xd0
  743. .xd1:
  744. ;release slot
  745.  
  746.         bts     [thr_slot_map], esi
  747.  
  748.         mov     ecx, [.process]
  749.         lea     eax, [ecx+PROC.thr_list]
  750.         cmp     eax, [eax+LHEAD.next]
  751.         jne     @F
  752.  
  753.         call    destroy_process.internal
  754. @@:
  755.         sti     ; .. and life goes on
  756.  
  757.         mov     eax, [draw_limits.left]
  758.         mov     ebx, [draw_limits.top]
  759.         mov     ecx, [draw_limits.right]
  760.         mov     edx, [draw_limits.bottom]
  761.         call    calculatescreen
  762.         xor     eax, eax
  763.         xor     esi, esi
  764.         call    redrawscreen
  765.  
  766.         call    unlock_application_table
  767.     ;mov   esi,process_terminated
  768.     ;call  sys_msg_board_str
  769.         add     esp, 8
  770.         ret
  771. restore .slot
  772. restore .process
  773.  
  774. ; Three following procedures are used to guarantee that
  775. ; some part of kernel code will not be terminated from outside
  776. ; while it is running.
  777. ; Note: they do not protect a thread from terminating due to errors inside
  778. ; the thread; accessing a nonexisting memory would still terminate it.
  779.  
  780. ; First two procedures must be used in pair by thread-to-be-protected
  781. ; to signal the beginning and the end of an important part.
  782. ; It is OK to have nested areas.
  783.  
  784. ; The last procedure must be used by outside wanna-be-terminators;
  785. ; if it is safe to terminate the given thread immediately, it returns eax=1;
  786. ; otherwise, it returns eax=0 and notifies the target thread that it should
  787. ; terminate itself when leaving a critical area (the last critical area if
  788. ; they are nested).
  789.  
  790. ; Implementation. Those procedures use one dword in APPDATA for the thread,
  791. ; APPDATA.terminate_protection.
  792. ; * The upper bit is 1 during normal operations and 0 when terminate is requested.
  793. ; * Other bits form a number = depth of critical regions,
  794. ;   plus 1 if the upper bit is 1.
  795. ; * When this dword goes to zero, the thread should be destructed,
  796. ;   and the procedure in which it happened becomes responsible for destruction.
  797.  
  798. ; Enter critical area. Called by thread which wants to be protected.
  799. proc protect_from_terminate
  800.         mov     edx, [current_slot]
  801. ; Atomically increment depth of critical areas and get the old value.
  802.         mov     eax, 1
  803.         lock xadd [edx+APPDATA.terminate_protection], eax
  804. ; If the old value was zero, somebody has started to terminate us,
  805. ; so we are destructing and cannot do anything protected.
  806. ; Otherwise, return to the caller.
  807.         test    eax, eax
  808.         jz      @f
  809.         ret
  810. @@:
  811. ; Wait for somebody to finish us.
  812.         call    change_task
  813.         jmp     @b
  814. endp
  815.  
  816. ; Leave critical area. Called by thread which wants to be protected.
  817. proc unprotect_from_terminate
  818.         mov     edx, [current_slot]
  819. ; Atomically decrement depth of critical areas.
  820.         lock dec [edx+APPDATA.terminate_protection]
  821. ; If the result of decrement is zero, somebody has requested termination,
  822. ; but at that moment we were inside a critical area; terminate now.
  823.         jz      sys_end
  824. ; Otherwise, return to the caller.
  825.         ret
  826. endp
  827.  
  828. ; Request termination of thread identified by edx = SLOT_BASE + slot*256.
  829. ; Called by anyone.
  830. proc request_terminate
  831.         xor     eax, eax        ; set return value
  832. ; Atomically clear the upper bit. If it was already zero, then
  833. ; somebody has requested termination before us, so just exit.
  834.         lock btr [edx+APPDATA.terminate_protection], 31
  835.         jnc     .unsafe
  836. ; Atomically decrement depth of critical areas.
  837.         lock dec [edx+APPDATA.terminate_protection]
  838. ; If the result of decrement is nonzero, the target thread is inside a
  839. ; critical area; leave termination to leaving that area.
  840.         jnz     .unsafe
  841. ; Otherwise, it is safe to kill the target now and the caller is responsible
  842. ; for this. Return eax=1.
  843.         inc     eax
  844. .unsafe:
  845.         ret
  846. endp
  847.  
  848.