Subversion Repositories Kolibri OS

Rev

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

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