Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2004-2021. All rights reserved. ;;
  4. ;;  Distributed under terms of the GNU General Public License.  ;;
  5. ;;                                                              ;;
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7.  
  8. $Revision: 9051 $
  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.         mov     ecx, 23
  24.         mov     eax, (10001110b shl 24) + os_code
  25.   @@:
  26.         movsw   ; low word of code-entry
  27.         stosd   ; interrupt gate type : os_code selector
  28.         movsw   ; high word of code-entry
  29.         loop    @b
  30.         lidt    [esi]
  31.         ret
  32.  
  33. iglobal
  34.   align 4
  35.   sys_int:
  36.     ; exception handlers addresses (for interrupt gate construction)
  37.         dd      e0,e1,e2,e3,e4,e5,e6,except_7 ; SEE: core/fpu.inc
  38.         dd      e8,e9,e10,e11,e12,e13,page_fault_exc,e15
  39.         dd      e16, e17,e18, e19
  40.         times   12 dd unknown_interrupt ;int_20..int_31
  41.  
  42.     ; interrupt handlers addresses (for interrupt gate construction)
  43.         ; 0x20+ are IRQ handlers
  44.         dd irq0
  45.         rept 12 irqn:1  \{dd irq_serv.irq_\#irqn\}
  46.         dd irqD
  47.         rept 18 irqn:14 \{dd irq_serv.irq_\#irqn\}
  48.  
  49.         ; int_0x40 gate trap (for directly copied)
  50.         dw i40 and 0xFFFF, os_code, 11101111b shl 8, i40 shr 16
  51.  
  52.         rept 23 irqn:33 \{dd irq_serv.irq_\#irqn\}
  53.  
  54.   idtreg: ; data for LIDT instruction (!!! must be immediately below sys_int data)
  55.         dw      2*($-sys_int-4)-1
  56.         dd      idts ; 0x8000B100
  57.         dw      0    ; alignment
  58.  
  59.   msg_fault_sel dd  msg_exc_8,msg_exc_u,msg_exc_a,msg_exc_b
  60.                 dd  msg_exc_c,msg_exc_d,msg_exc_e,msg_exc_u
  61.                 dd  msg_exc_u,msg_exc_11
  62.  
  63.   msg_exc_8     db "Double fault", 0
  64.   msg_exc_u     db "Undefined Exception", 0
  65.   msg_exc_a     db "Invalid TSS", 0
  66.   msg_exc_b     db "Segment not present", 0
  67.   msg_exc_c     db "Stack fault", 0
  68.   msg_exc_d     db "General protection fault", 0
  69.   msg_exc_e     db "Page fault", 0
  70.   msg_exc_11    db "Alignment Check", 0
  71.  
  72.   if lang eq sp
  73.     include 'core/sys32-sp.inc'
  74.   else
  75.     msg_sel_ker   db "kernel", 0
  76.     msg_sel_app   db "application", 0
  77.   end if
  78. endg
  79.  
  80. macro save_ring3_context {
  81.         pushad
  82. }
  83. macro restore_ring3_context {
  84.         popad
  85. }
  86. macro exc_wo_code [num] {
  87.   e#num :
  88.         save_ring3_context
  89.         mov     bl, num
  90.         jmp     exc_c
  91. } exc_wo_code   0,1,2,3,4,5,6,15,16,19
  92.  
  93. macro exc_w_code [num] {
  94.   e#num :
  95.         add     esp, 4
  96.         save_ring3_context
  97.         mov     bl, num
  98.         jmp     exc_c
  99. } exc_w_code    8,9,10,11,12,13,17,18
  100.  
  101.  
  102. uglobal
  103.   pf_err_code   dd ?
  104. endg
  105.  
  106. page_fault_exc:                   ; foolproof: selectors are clobbered ...
  107.         pop     [ss:pf_err_code]  ; actually, until the next #PF
  108.         save_ring3_context
  109.         mov     bl, 14
  110.  
  111. exc_c:                            ; exceptions (all but 7th - #NM)
  112.  ; stack frame when exception/interrupt from ring3 + pushad (i.e right here)
  113.   reg_ss        equ esp+0x30
  114.   reg_esp3      equ esp+0x2C
  115.   reg_eflags    equ esp+0x28
  116.   reg_cs3       equ esp+0x24
  117.   reg_eip       equ esp+0x20
  118.  ; this if frame from pushad
  119.   reg_eax       equ esp+0x1C
  120.   reg_ecx       equ esp+0x18
  121.   reg_edx       equ esp+0x14
  122.   reg_ebx       equ esp+0x10
  123.   reg_esp0      equ esp+0x0C
  124.   reg_ebp       equ esp+0x08
  125.   reg_esi       equ esp+0x04
  126.   reg_edi       equ esp+0x00
  127.  
  128.         mov     ax, app_data       ; exception
  129.         mov     ds, ax             ; load proper values
  130.         mov     es, ax             ; to registers
  131.         cld                        ; clear the direction flag
  132.         movzx   ebx, bl
  133. ; redirect to V86 manager? (EFLAGS & 0x20000) != 0?
  134.         test    byte[reg_eflags+2], 2
  135.         jnz     v86_exc_c
  136.         cmp     bl, 14             ; #PF
  137.         jne     @f
  138.         call    page_fault_handler ; SEE: core/memory.inc
  139.   @@:
  140.         mov     esi, [current_slot]
  141.         btr     [esi+APPDATA.except_mask], ebx
  142.         jnc     @f
  143.         mov     eax, [esi+APPDATA.exc_handler]
  144.         test    eax, eax
  145.         jnz     IRetToUserHook
  146.   @@:
  147.         cli
  148.         mov     eax, [esi+APPDATA.debugger_slot]
  149.         test    eax, eax
  150.         jnz     .debug
  151. ; not debuggee => say error and terminate
  152.         call    show_error_parameters
  153.         sti
  154.         mov     [edx + TASKDATA.state], TSTATE_TERMINATING
  155.         call    wakeup_osloop
  156.         call    change_task
  157. ; If we're here, then the main OS thread has crashed before initializing IDLE thread.
  158. ; Or they both have crashed. Anyway, things are hopelessly broken.
  159.         hlt
  160.         jmp     $-1
  161. .debug:
  162. ; we are debugged process, notify debugger and suspend ourself
  163. ; eax=debugger PID
  164.         mov     ecx, 1          ; debug_message code=other_exception
  165.         cmp     bl, 1           ; #DB
  166.         jne     .notify         ; notify debugger and suspend ourself
  167.         mov     ebx, dr6        ; debug_message data=DR6_image
  168.         xor     edx, edx
  169.         mov     dr6, edx
  170.         mov     edx, dr7
  171.         mov     cl, not 8
  172.   .l1:
  173.         shl     dl, 2
  174.         jc      @f
  175.         and     bl, cl
  176.   @@:
  177.         sar     cl, 1
  178.         jc      .l1
  179.         mov     cl, 3           ; debug_message code=debug_exception
  180. .notify:
  181.         push    ebx             ; debug_message data
  182.         mov     ebx, [TASK_BASE]
  183.         push    [ebx+TASKDATA.pid] ; PID
  184.         push    ecx             ; debug_message code ((here: ecx==1/3))
  185.         mov     cl, 12          ; debug_message size
  186.         call    debugger_notify ;; only ONE using, inline ??? SEE: core/debug.inc
  187.         add     esp, 12
  188.         mov     edx, [TASK_BASE]
  189.         mov     [edx+TASKDATA.state], TSTATE_RUN_SUSPENDED
  190.         call    change_task     ; SEE: core/shed.inc
  191.         restore_ring3_context
  192.         iretd
  193.  
  194. IRetToUserHook:
  195.         xchg    eax, [reg_eip]
  196.         sub     dword[reg_esp3], 8
  197.         mov     edi, [reg_esp3]
  198.         stosd
  199.         mov     [edi], ebx
  200.         restore_ring3_context
  201. ; simply return control to interrupted process
  202. unknown_interrupt:
  203.         iretd
  204.  
  205. ;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  206. ; bl - error vector
  207. show_error_parameters:
  208.         cmp     bl, 0x06
  209.         jnz     .no_ud
  210.         push    ebx
  211.         mov     ebx, ud_user_message
  212.         mov     ebp, notifyapp
  213.         call    fs_execute_from_sysdir_param
  214.         pop     ebx
  215. .no_ud:
  216.         mov     edx, [TASK_BASE];not scratched below
  217.         if lang eq sp
  218.         DEBUGF  1, "K : Proceso - terminado forzado PID: %x [%s]\n", [edx+TASKDATA.pid], [current_slot]
  219.         else
  220.         DEBUGF  1, "K : Process - forced terminate PID: %x [%s]\n", [edx+TASKDATA.pid], [current_slot]
  221.         end if
  222.         cmp     bl, 0x08
  223.         jb      .l0
  224.         cmp     bl, 0x11
  225.         jbe     .l1
  226.   .l0:
  227.         mov     bl, 0x09
  228.   .l1:
  229.         mov     eax, [msg_fault_sel+ebx*4 - 0x08*4]
  230.  
  231.         ; Notify show exeption message
  232.         pushad
  233.         ccall   LIBC_SPRINTF, except_notify_msg, except_notify_fstr, [current_slot], [edx+TASKDATA.pid], eax
  234.         mov     ebx, except_notify_msg       ; exception message for @notify  
  235.         mov     ebp, notifyapp               ; @notify bin file path
  236.         call    fs_execute_from_sysdir_param ; Execute a @notify to display an exception
  237.         popad
  238.  
  239.         DEBUGF  1, "K : %s\n", eax
  240.         mov     eax, [reg_cs3+4]
  241.         mov     edi, msg_sel_app
  242.         mov     ebx, [reg_esp3+4]
  243.         cmp     eax, app_code
  244.         je      @f
  245.         mov     edi, msg_sel_ker
  246.         mov     ebx, [reg_esp0+4]
  247.     @@:
  248.         DEBUGF  1, "K : EAX : %x EBX : %x ECX : %x\n", [reg_eax+4], [reg_ebx+4], [reg_ecx+4]
  249.         DEBUGF  1, "K : EDX : %x ESI : %x EDI : %x\n", [reg_edx+4], [reg_esi+4], [reg_edi+4]
  250.         DEBUGF  1, "K : EBP : %x EIP : %x ESP : %x\n", [reg_ebp+4], [reg_eip+4], ebx
  251.         DEBUGF  1, "K : Flags : %x CS : %x (%s)\n", [reg_eflags+4], eax, edi
  252.  
  253.         DEBUGF  1, "K : Stack dump:\n"
  254.         push    eax ebx ecx edx
  255.         call    .check_ESP
  256.         test    eax, eax
  257.         jnz     .error_ESP
  258.         DEBUGF  1, "K : [ESP+00]: %x",[ebx]
  259.         add     ebx, 4
  260.         call    .check_ESP
  261.         test    eax, eax
  262.         jnz     .error_ESP
  263.         DEBUGF  1, " [ESP+04]: %x",[ebx]
  264.         add     ebx, 4
  265.         call    .check_ESP
  266.         test    eax, eax
  267.         jnz     .error_ESP
  268.         DEBUGF  1, " [ESP+08]: %x\n",[ebx]
  269.         add     ebx, 4
  270.         call    .check_ESP
  271.         test    eax, eax
  272.         jnz     .error_ESP
  273.         DEBUGF  1, "K : [ESP+12]: %x",[ebx]
  274.         add     ebx, 4
  275.         call    .check_ESP
  276.         test    eax, eax
  277.         jnz     .error_ESP
  278.         DEBUGF  1, " [ESP+16]: %x",[ebx]
  279.         add     ebx, 4
  280.         call    .check_ESP
  281.         test    eax, eax
  282.         jnz     .error_ESP
  283.         DEBUGF  1, " [ESP+20]: %x\n",[ebx]
  284.         add     ebx, 4
  285.         call    .check_ESP
  286.         test    eax, eax
  287.         jnz     .error_ESP
  288.         DEBUGF  1, "K : [ESP+24]: %x",[ebx]
  289.         add     ebx, 4
  290.         call    .check_ESP
  291.         test    eax, eax
  292.         jnz     .error_ESP
  293.         DEBUGF  1, " [ESP+28]: %x",[ebx]
  294.         add     ebx, 4
  295.         call    .check_ESP
  296.         test    eax, eax
  297.         jnz     .error_ESP
  298.         DEBUGF  1, " [ESP+32]: %x\n",[ebx]
  299.         pop     edx ecx ebx eax
  300.         ret
  301. .error_ESP:
  302.         pop     edx ecx ebx eax
  303.         DEBUGF  1, "\n"
  304.         DEBUGF  1, "K : Unexpected end of the stack\n"
  305.         ret
  306. ;--------------------------------------
  307. .check_ESP:
  308.         push    ebx
  309.         shr     ebx, 12
  310.         mov     ecx, ebx
  311.         shr     ecx, 10
  312.         mov     edx, [master_tab+ecx*4]
  313.         test    edx, PG_READ
  314.         jz      .fail             ; page table is not created
  315.                                   ; incorrect address in the program
  316.  
  317.         mov     eax, [page_tabs+ebx*4]
  318.         test    eax, 2
  319.         jz      .fail             ; address not reserved for use. error
  320.  
  321.         pop     ebx
  322.         xor     eax, eax
  323.         ret
  324.  
  325. .fail:
  326.         pop     ebx
  327.         xor     eax, eax
  328.         dec     eax
  329.         ret
  330. ;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  331.  
  332.   restore  reg_ss
  333.   restore  reg_esp3
  334.   restore  reg_eflags
  335.   restore  reg_cs
  336.   restore  reg_eip
  337.   restore  reg_eax
  338.   restore  reg_ecx
  339.   restore  reg_edx
  340.   restore  reg_ebx
  341.   restore  reg_esp0
  342.   restore  reg_ebp
  343.   restore  reg_esi
  344.   restore  reg_edi
  345.  
  346. align 4
  347. lock_application_table:
  348.         push    eax ecx edx
  349.         mov     ecx, application_table_mutex
  350.         call    mutex_lock
  351.  
  352.         mov     eax, [current_slot_idx]
  353.         shl     eax, BSF sizeof.TASKDATA
  354.         add     eax, TASK_TABLE+TASKDATA.pid
  355.         mov     eax, [eax]
  356.  
  357.         mov     [application_table_owner], eax
  358.  
  359.         pop     edx ecx eax
  360.  
  361.         ret
  362.  
  363. align 4
  364. unlock_application_table:
  365.         push    eax ecx edx
  366.  
  367.         mov     [application_table_owner], 0
  368.         mov     ecx, application_table_mutex
  369.         call    mutex_unlock
  370.  
  371.         pop     edx ecx eax
  372.  
  373.         ret
  374.  
  375. ; sysfn 64 implementation
  376. align 4
  377. sys_resize_app_memory:
  378. ; in:   eax = 64 - function number
  379. ;       ebx = 1 - number of its only subfunction
  380. ;       ecx = new amount of memory
  381. ; out:
  382. ;       eax = 0 - success
  383. ;       eax = 1 - out of memory
  384.  
  385. ;        cmp    eax,1
  386.         dec     ebx
  387.         jnz     .no_application_mem_resize
  388.  
  389.         mov     eax, [pg_data.pages_free]
  390.         shl     eax, 12
  391.         cmp     eax, ecx
  392.         jae     @f
  393.  
  394.         xor     eax, eax
  395.         inc     eax
  396.         jmp     .store_result
  397. @@:
  398.         stdcall new_mem_resize, ecx
  399. .store_result:
  400.         mov     [esp+32], eax
  401. .no_application_mem_resize:
  402.         ret
  403.  
  404. iglobal
  405. ;  process_terminating  db 'K : Process - terminating',13,10,0
  406. ;  process_terminated   db 'K : Process - done',13,10,0
  407.   msg_obj_destroy       db 'K : destroy app object',13,10,0
  408. endg
  409.  
  410. ; param
  411. ;  esi= slot
  412.  
  413. align 4
  414. terminate: ; terminate application
  415. destroy_thread:
  416.  
  417.         .slot     equ esp+4             ;locals
  418.         .process  equ esp               ;ptr to parent process
  419.  
  420.  
  421.         push    esi        ;save .slot
  422.  
  423.         shl     esi, 8
  424.         mov     edx, [SLOT_BASE+esi+APPDATA.process]
  425.         test    edx, edx
  426.         jnz     @F
  427.         pop     esi
  428.         shl     esi, BSF sizeof.TASKDATA
  429.         mov     [TASK_TABLE+esi+TASKDATA.state], TSTATE_FREE
  430.         ret
  431. @@:
  432.         push    edx                     ;save .process
  433.         lea     edx, [SLOT_BASE+esi]
  434.         call    scheduler_remove_thread
  435.         call    lock_application_table
  436.  
  437. ; if the process is in V86 mode...
  438.         mov     eax, [.slot]
  439.         shl     eax, 8
  440.         mov     esi, [eax+SLOT_BASE+APPDATA.pl0_stack]
  441.         add     esi, RING0_STACK_SIZE
  442.         cmp     [eax+SLOT_BASE+APPDATA.saved_esp0], esi
  443.         jz      .nov86
  444. ; ...it has page directory for V86 mode
  445.         mov     esi, [eax+SLOT_BASE+APPDATA.saved_esp0]
  446.         mov     ecx, [esi+4]
  447.         mov     [eax+SLOT_BASE+APPDATA.process], ecx
  448. ; ...and I/O permission map for V86 mode
  449.         mov     ecx, [esi+12]
  450.         mov     [eax+SLOT_BASE+APPDATA.io_map], ecx
  451.         mov     ecx, [esi+8]
  452.         mov     [eax+SLOT_BASE+APPDATA.io_map+4], ecx
  453. .nov86:
  454. ; destroy per-thread kernel objects
  455.         mov     esi, [.slot]
  456.         shl     esi, 8
  457.         add     esi, SLOT_BASE+APP_OBJ_OFFSET
  458. @@:
  459.         mov     eax, [esi+APPOBJ.fd]
  460.         test    eax, eax
  461.         jz      @F
  462.  
  463.         cmp     eax, esi
  464.         je      @F
  465.  
  466.         push    esi
  467.         call    [eax+APPOBJ.destroy]
  468.            DEBUGF 1,"%s",msg_obj_destroy
  469.         pop     esi
  470.         jmp     @B
  471. @@:
  472.         mov     esi, [.slot]
  473.         cmp     [fpu_owner], esi ; if user fpu last -> fpu user = 2
  474.         jne     @F
  475.  
  476.         mov     [fpu_owner], 2
  477.         mov     eax, [sizeof.APPDATA*2+SLOT_BASE+APPDATA.fpu_state]
  478.         clts
  479.         bt      [cpu_caps], CAPS_SSE
  480.         jnc     .no_SSE
  481.         fxrstor [eax]
  482.         jmp     @F
  483. .no_SSE:
  484.         fnclex
  485.         frstor  [eax]
  486. @@:
  487.  
  488.         mov     [KEY_COUNT], byte 0    ; empty keyboard buffer
  489.         mov     [BTN_COUNT], byte 0    ; empty button buffer
  490.  
  491.  
  492. ; remove defined hotkeys
  493.         mov     eax, hotkey_list
  494. .loop:
  495.         cmp     [eax+8], esi
  496.         jnz     .cont
  497.         mov     ecx, [eax]
  498.         jecxz   @f
  499.         push    dword [eax+12]
  500.         pop     dword [ecx+12]
  501. @@:
  502.         mov     ecx, [eax+12]
  503.         push    dword [eax]
  504.         pop     dword [ecx]
  505.         xor     ecx, ecx
  506.         mov     [eax], ecx
  507.         mov     [eax+4], ecx
  508.         mov     [eax+8], ecx
  509.         mov     [eax+12], ecx
  510. .cont:
  511.         add     eax, 16
  512.         cmp     eax, hotkey_list+256*16
  513.         jb      .loop
  514. ; get process PID
  515.         mov     eax, esi
  516.         shl     eax, BSF sizeof.TASKDATA
  517.         mov     eax, [eax+TASK_TABLE+TASKDATA.pid]
  518. ; compare current lock input with process PID
  519.         cmp     eax, [PID_lock_input]
  520.         jne     @f
  521.  
  522.         xor     eax, eax
  523.         mov     [PID_lock_input], eax
  524. @@:
  525. ; remove hotkeys in buffer
  526.         mov     eax, hotkey_buffer
  527. .loop2:
  528.         cmp     [eax], esi
  529.         jnz     .cont2
  530.         and     dword [eax+4], 0
  531.         and     dword [eax], 0
  532. .cont2:
  533.         add     eax, 8
  534.         cmp     eax, hotkey_buffer+120*8
  535.         jb      .loop2
  536.  
  537.         mov     ecx, esi          ; remove buttons
  538.   bnewba2:
  539.         mov     edi, [BTN_ADDR]
  540.         mov     eax, edi
  541.         cld
  542.         movzx   ebx, word [edi]
  543.         inc     bx
  544.   bnewba:
  545.         dec     bx
  546.         jz      bnmba
  547.         add     eax, 0x10
  548.         cmp     cx, [eax]
  549.         jnz     bnewba
  550.         pusha
  551.         mov     ecx, ebx
  552.         inc     ecx
  553.         shl     ecx, 4
  554.         mov     ebx, eax
  555.         add     eax, 0x10
  556.         call    memmove
  557.         dec     dword [edi]
  558.         popa
  559.         jmp     bnewba2
  560.   bnmba:
  561.  
  562.         pusha   ; save window coordinates for window restoring
  563.         cld
  564.         shl     esi, BSF sizeof.WDATA
  565.         add     esi, window_data
  566.         mov     eax, [esi+WDATA.box.left]
  567.         mov     [draw_limits.left], eax
  568.         add     eax, [esi+WDATA.box.width]
  569.         mov     [draw_limits.right], eax
  570.         mov     eax, [esi+WDATA.box.top]
  571.         mov     [draw_limits.top], eax
  572.         add     eax, [esi+WDATA.box.height]
  573.         mov     [draw_limits.bottom], eax
  574.  
  575.         xor     eax, eax
  576.         mov     edi, esi
  577.         mov     ecx, sizeof.WDATA/4
  578.         rep stosd
  579.  
  580.         lea     edi, [esi-window_data+draw_data]
  581.         mov     ecx, sizeof.WDATA/4
  582.         rep stosd
  583.         popa
  584.  
  585. ; debuggee test
  586.         pushad
  587.         mov     edi, esi
  588.         shl     edi, BSF sizeof.TASKDATA
  589.         mov     eax, [SLOT_BASE+edi*8+APPDATA.debugger_slot]
  590.         test    eax, eax
  591.         jz      .nodebug
  592.         movi    ecx, 8
  593.         push    dword [TASK_TABLE+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+sizeof.APPDATA+APPDATA.io_map]
  618.         je      @F
  619.         call    free_page
  620. @@:
  621.         mov     eax, [edi+APPDATA.io_map+4]
  622.         cmp     eax, [SLOT_BASE+sizeof.APPDATA+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, [thread_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, BSF sizeof.TASKDATA
  649.         cmp     [TASK_TABLE + edi + TASKDATA.state], TSTATE_FREE ; skip free 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, BSF sizeof.TASKDATA
  664.         mov     esi, [esi+TASK_TABLE+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, BSF sizeof.TASKDATA
  680.         add     edx, TASK_TABLE
  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, BSF sizeof.TASKDATA
  724.         mov     [edi+TASK_TABLE + TASKDATA.state], TSTATE_FREE
  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, [thread_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*sizeof.APPDATA.
  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.