Subversion Repositories Kolibri OS

Rev

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