Subversion Repositories Kolibri OS

Rev

Rev 5836 | Rev 6817 | 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: 6793 $
  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
  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.  
  71.   if lang eq sp
  72.     include 'core/sys32-sp.inc'
  73.   else
  74.     msg_sel_ker   db "kernel", 0
  75.     msg_sel_app   db "application", 0
  76.   end if
  77.  
  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:                 ; дуракоусточивость: селекторы испорчены...
  107.         pop     [ss:pf_err_code]; действительно до следующего #PF
  108.         save_ring3_context
  109.         mov     bl, 14
  110.  
  111. exc_c:                          ; исключения (все, кроме 7-го - #NM)
  112. ; Фрэйм стека при исключении/прерывании из 3-го кольца + pushad (т.е., именно здесь)
  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.  ; это фрэйм от 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        ;исключение
  129.         mov     ds, ax                  ;загрузим правильные значения
  130.         mov     es, ax                  ;в регистры
  131.         cld                     ; и приводим DF к стандарту
  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], byte 4 ; terminate
  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     byte [edx+TASKDATA.state], 1 ; 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, 0x0e
  225.         jbe     .l1
  226.   .l0:
  227.         mov     bl, 0x09
  228.   .l1:
  229.         mov     eax, [msg_fault_sel+ebx*4 - 0x08*4]
  230.         DEBUGF  1, "K : %s\n", eax
  231.         mov     eax, [reg_cs3+4]
  232.         mov     edi, msg_sel_app
  233.         mov     ebx, [reg_esp3+4]
  234.         cmp     eax, app_code
  235.         je      @f
  236.         mov     edi, msg_sel_ker
  237.         mov     ebx, [reg_esp0+4]
  238.     @@:
  239.         DEBUGF  1, "K : EAX : %x EBX : %x ECX : %x\n", [reg_eax+4], [reg_ebx+4], [reg_ecx+4]
  240.         DEBUGF  1, "K : EDX : %x ESI : %x EDI : %x\n", [reg_edx+4], [reg_esi+4], [reg_edi+4]
  241.         DEBUGF  1, "K : EBP : %x EIP : %x ESP : %x\n", [reg_ebp+4], [reg_eip+4], ebx
  242.         DEBUGF  1, "K : Flags : %x CS : %x (%s)\n", [reg_eflags+4], eax, edi
  243.  
  244.         DEBUGF  1, "K : Stack dump:\n"
  245.         push    eax ebx ecx edx
  246.         call    .check_ESP
  247.         test    eax, eax
  248.         jnz     .error_ESP
  249.         DEBUGF  1, "K : [ESP+00]: %x",[ebx]
  250.         add     ebx, 4
  251.         call    .check_ESP
  252.         test    eax, eax
  253.         jnz     .error_ESP
  254.         DEBUGF  1, " [ESP+04]: %x",[ebx]
  255.         add     ebx, 4
  256.         call    .check_ESP
  257.         test    eax, eax
  258.         jnz     .error_ESP
  259.         DEBUGF  1, " [ESP+08]: %x\n",[ebx]
  260.         add     ebx, 4
  261.         call    .check_ESP
  262.         test    eax, eax
  263.         jnz     .error_ESP
  264.         DEBUGF  1, "K : [ESP+12]: %x",[ebx]
  265.         add     ebx, 4
  266.         call    .check_ESP
  267.         test    eax, eax
  268.         jnz     .error_ESP
  269.         DEBUGF  1, " [ESP+16]: %x",[ebx]
  270.         add     ebx, 4
  271.         call    .check_ESP
  272.         test    eax, eax
  273.         jnz     .error_ESP
  274.         DEBUGF  1, " [ESP+20]: %x\n",[ebx]
  275.         add     ebx, 4
  276.         call    .check_ESP
  277.         test    eax, eax
  278.         jnz     .error_ESP
  279.         DEBUGF  1, "K : [ESP+24]: %x",[ebx]
  280.         add     ebx, 4
  281.         call    .check_ESP
  282.         test    eax, eax
  283.         jnz     .error_ESP
  284.         DEBUGF  1, " [ESP+28]: %x",[ebx]
  285.         add     ebx, 4
  286.         call    .check_ESP
  287.         test    eax, eax
  288.         jnz     .error_ESP
  289.         DEBUGF  1, " [ESP+32]: %x\n",[ebx]
  290.         pop     edx ecx ebx eax
  291.         ret
  292. .error_ESP:
  293.         pop     edx ecx ebx eax
  294.         DEBUGF  1, "\n"
  295.         DEBUGF  1, "K : Unexpected end of the stack\n"
  296.         ret
  297. ;--------------------------------------
  298. .check_ESP:
  299.         push    ebx
  300.         shr     ebx, 12
  301.         mov     ecx, ebx
  302.         shr     ecx, 10
  303.         mov     edx, [master_tab+ecx*4]
  304.         test    edx, PG_READ
  305.         jz      .fail             ;page table is not created
  306.                                   ;incorrect address in the program
  307.  
  308.         mov     eax, [page_tabs+ebx*4]
  309.         test    eax, 2
  310.         jz      .fail             ;address not reserved for use. error
  311.  
  312.         pop     ebx
  313.         xor     eax, eax
  314.         ret
  315.  
  316. .fail:
  317.         pop     ebx
  318.         xor     eax, eax
  319.         dec     eax
  320.         ret
  321. ;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  322.  
  323.   restore  reg_ss
  324.   restore  reg_esp3
  325.   restore  reg_eflags
  326.   restore  reg_cs
  327.   restore  reg_eip
  328.   restore  reg_eax
  329.   restore  reg_ecx
  330.   restore  reg_edx
  331.   restore  reg_ebx
  332.   restore  reg_esp0
  333.   restore  reg_ebp
  334.   restore  reg_esi
  335.   restore  reg_edi
  336.  
  337.  
  338. align 4
  339. lock_application_table:
  340.         push    eax ecx edx
  341.         mov     ecx, application_table_mutex
  342.         call    mutex_lock
  343.  
  344.         mov     eax, [CURRENT_TASK]
  345.         shl     eax, 5
  346.         add     eax, CURRENT_TASK+TASKDATA.pid
  347.         mov     eax, [eax]
  348.  
  349.         mov     [application_table_owner], eax
  350.  
  351.         pop     edx ecx eax
  352.  
  353.         ret
  354.  
  355. align 4
  356. unlock_application_table:
  357.         push    eax ecx edx
  358.  
  359.         mov     [application_table_owner], 0
  360.         mov     ecx, application_table_mutex
  361.         call    mutex_unlock
  362.  
  363.         pop     edx ecx eax
  364.  
  365.         ret
  366.  
  367. ;  * eax = 64 - номер функции
  368. ;  * ebx = 1 - единственная подфункция
  369. ;  * ecx = новый размер памяти
  370. ;Возвращаемое значение:
  371. ;  * eax = 0 - успешно
  372. ;  * eax = 1 - недостаточно памяти
  373.  
  374. align 4
  375. sys_resize_app_memory:
  376.         ; ebx = 1 - resize
  377.         ; ecx = new amount of memory
  378.  
  379. ;        cmp    eax,1
  380.         dec     ebx
  381.         jnz     .no_application_mem_resize
  382.  
  383.         mov     eax, [pg_data.pages_free]
  384.         shl     eax, 12
  385.         cmp     eax, ecx
  386.         jae     @f
  387.  
  388.         xor     eax, eax
  389.         inc     eax
  390.         jmp     .store_result
  391. @@:
  392.         stdcall new_mem_resize, ecx
  393. .store_result:
  394.         mov     [esp+32], eax
  395. .no_application_mem_resize:
  396.         ret
  397.  
  398. iglobal
  399. ;  process_terminating  db 'K : Process - terminating',13,10,0
  400. ;  process_terminated   db 'K : Process - done',13,10,0
  401.   msg_obj_destroy       db 'K : destroy app object',13,10,0
  402. endg
  403.  
  404. ; param
  405. ;  esi= slot
  406.  
  407. align 4
  408. terminate: ; terminate application
  409. destroy_thread:
  410.  
  411.         .slot     equ esp+4             ;locals
  412.         .process  equ esp               ;ptr to parent process
  413.  
  414.  
  415.         push    esi        ;save .slot
  416.  
  417.         shl     esi, 8
  418.         mov     edx, [SLOT_BASE+esi+APPDATA.process]
  419.         test    edx, edx
  420.         jnz     @F
  421.         pop     esi
  422.         shl     esi, 5
  423.         mov     [CURRENT_TASK+esi+TASKDATA.state], 9
  424.         ret
  425. @@:
  426.         push    edx                     ;save .process
  427.         lea     edx, [SLOT_BASE+esi]
  428.         call    scheduler_remove_thread
  429.         call    lock_application_table
  430.  
  431. ; if the process is in V86 mode...
  432.         mov     eax, [.slot]
  433.         shl     eax, 8
  434.         mov     esi, [eax+SLOT_BASE+APPDATA.pl0_stack]
  435.         add     esi, RING0_STACK_SIZE
  436.         cmp     [eax+SLOT_BASE+APPDATA.saved_esp0], esi
  437.         jz      .nov86
  438. ; ...it has page directory for V86 mode
  439.         mov     esi, [eax+SLOT_BASE+APPDATA.saved_esp0]
  440.         mov     ecx, [esi+4]
  441.         mov     [eax+SLOT_BASE+APPDATA.process], ecx
  442. ; ...and I/O permission map for V86 mode
  443.         mov     ecx, [esi+12]
  444.         mov     [eax+SLOT_BASE+APPDATA.io_map], ecx
  445.         mov     ecx, [esi+8]
  446.         mov     [eax+SLOT_BASE+APPDATA.io_map+4], ecx
  447. .nov86:
  448. ;destroy per-thread kernel objects
  449.         mov     esi, [.slot]
  450.         shl     esi, 8
  451.         add     esi, SLOT_BASE+APP_OBJ_OFFSET
  452. @@:
  453.         mov     eax, [esi+APPOBJ.fd]
  454.         test    eax, eax
  455.         jz      @F
  456.  
  457.         cmp     eax, esi
  458.         je      @F
  459.  
  460.         push    esi
  461.         call    [eax+APPOBJ.destroy]
  462.            DEBUGF 1,"%s",msg_obj_destroy
  463.         pop     esi
  464.         jmp     @B
  465. @@:
  466.         mov     esi, [.slot]
  467.         cmp     [fpu_owner], esi ; if user fpu last -> fpu user = 2
  468.         jne     @F
  469.  
  470.         mov     [fpu_owner], 2
  471.         mov     eax, [256*2+SLOT_BASE+APPDATA.fpu_state]
  472.         clts
  473.         bt      [cpu_caps], CAPS_SSE
  474.         jnc     .no_SSE
  475.         fxrstor [eax]
  476.         jmp     @F
  477. .no_SSE:
  478.         fnclex
  479.         frstor  [eax]
  480. @@:
  481.  
  482.         mov     [KEY_COUNT], byte 0    ; empty keyboard buffer
  483.         mov     [BTN_COUNT], byte 0    ; empty button buffer
  484.  
  485.  
  486. ; remove defined hotkeys
  487.         mov     eax, hotkey_list
  488. .loop:
  489.         cmp     [eax+8], esi
  490.         jnz     .cont
  491.         mov     ecx, [eax]
  492.         jecxz   @f
  493.         push    dword [eax+12]
  494.         pop     dword [ecx+12]
  495. @@:
  496.         mov     ecx, [eax+12]
  497.         push    dword [eax]
  498.         pop     dword [ecx]
  499.         xor     ecx, ecx
  500.         mov     [eax], ecx
  501.         mov     [eax+4], ecx
  502.         mov     [eax+8], ecx
  503.         mov     [eax+12], ecx
  504. .cont:
  505.         add     eax, 16
  506.         cmp     eax, hotkey_list+256*16
  507.         jb      .loop
  508. ; get process PID
  509.         mov     eax, esi
  510.         shl     eax, 5
  511.         mov     eax, [eax+CURRENT_TASK+TASKDATA.pid]
  512. ; compare current lock input with process PID
  513.         cmp     eax, [PID_lock_input]
  514.         jne     @f
  515.  
  516.         xor     eax, eax
  517.         mov     [PID_lock_input], eax
  518. @@:
  519. ; remove hotkeys in buffer
  520.         mov     eax, hotkey_buffer
  521. .loop2:
  522.         cmp     [eax], esi
  523.         jnz     .cont2
  524.         and     dword [eax+4], 0
  525.         and     dword [eax], 0
  526. .cont2:
  527.         add     eax, 8
  528.         cmp     eax, hotkey_buffer+120*8
  529.         jb      .loop2
  530.  
  531.         mov     ecx, esi          ; remove buttons
  532.   bnewba2:
  533.         mov     edi, [BTN_ADDR]
  534.         mov     eax, edi
  535.         cld
  536.         movzx   ebx, word [edi]
  537.         inc     bx
  538.   bnewba:
  539.         dec     bx
  540.         jz      bnmba
  541.         add     eax, 0x10
  542.         cmp     cx, [eax]
  543.         jnz     bnewba
  544.         pusha
  545.         mov     ecx, ebx
  546.         inc     ecx
  547.         shl     ecx, 4
  548.         mov     ebx, eax
  549.         add     eax, 0x10
  550.         call    memmove
  551.         dec     dword [edi]
  552.         popa
  553.         jmp     bnewba2
  554.   bnmba:
  555.  
  556.         pusha   ; save window coordinates for window restoring
  557.         cld
  558.         shl     esi, 5
  559.         add     esi, window_data
  560.         mov     eax, [esi+WDATA.box.left]
  561.         mov     [draw_limits.left], eax
  562.         add     eax, [esi+WDATA.box.width]
  563.         mov     [draw_limits.right], eax
  564.         mov     eax, [esi+WDATA.box.top]
  565.         mov     [draw_limits.top], eax
  566.         add     eax, [esi+WDATA.box.height]
  567.         mov     [draw_limits.bottom], eax
  568.  
  569.         xor     eax, eax
  570.         mov     [esi+WDATA.box.left], eax
  571.         mov     [esi+WDATA.box.width], eax
  572.         mov     [esi+WDATA.box.top], eax
  573.         mov     [esi+WDATA.box.height], eax
  574.         mov     [esi+WDATA.cl_workarea], eax
  575.         mov     [esi+WDATA.cl_titlebar], eax
  576.         mov     [esi+WDATA.cl_frames], eax
  577.         mov     dword [esi+WDATA.z_modif], eax; clear all flags: z_modif, wstate, redraw, wdrawn
  578.         lea     edi, [esi-window_data+draw_data]
  579.         mov     ecx, 32/4
  580.         rep stosd
  581.         popa
  582.  
  583. ; debuggee test
  584.         pushad
  585.         mov     edi, esi
  586.         shl     edi, 5
  587.         mov     eax, [SLOT_BASE+edi*8+APPDATA.debugger_slot]
  588.         test    eax, eax
  589.         jz      .nodebug
  590.         movi    ecx, 8
  591.         push    dword [CURRENT_TASK+edi+TASKDATA.pid]; PID
  592.         push    2
  593.         call    debugger_notify
  594.         pop     ecx
  595.         pop     ecx
  596. .nodebug:
  597.         popad
  598.  
  599.         mov     ebx, [.slot]
  600.         shl     ebx, 8
  601.         push    ebx
  602.         mov     ebx, [SLOT_BASE+ebx+APPDATA.pl0_stack]
  603.  
  604.         stdcall kernel_free, ebx
  605.  
  606.         pop     ebx
  607.         mov     ebx, [SLOT_BASE+ebx+APPDATA.cur_dir]
  608.         stdcall kernel_free, ebx
  609.  
  610.         mov     edi, [.slot]
  611.         shl     edi, 8
  612.         add     edi, SLOT_BASE
  613.  
  614.         mov     eax, [edi+APPDATA.io_map]
  615.         cmp     eax, [SLOT_BASE+256+APPDATA.io_map]
  616.         je      @F
  617.         call    free_page
  618. @@:
  619.         mov     eax, [edi+APPDATA.io_map+4]
  620.         cmp     eax, [SLOT_BASE+256+APPDATA.io_map+4]
  621.         je      @F
  622.         call    free_page
  623. @@:
  624.         lea     ebx, [edi+APPDATA.list]
  625.         list_del ebx                    ;destroys edx, ecx
  626.  
  627.         mov     eax, 0x20202020
  628.         stosd
  629.         stosd
  630.         stosd
  631.         mov     ecx, 244/4
  632.         xor     eax, eax
  633.         rep stosd
  634.  
  635.   ; activate window
  636.         movzx   eax, word [WIN_STACK + esi*2]
  637.         cmp     eax, [TASK_COUNT]
  638.         jne     .dont_activate
  639.         pushad
  640.  .check_next_window:
  641.         dec     eax
  642.         cmp     eax, 1
  643.         jbe     .nothing_to_activate
  644.         lea     esi, [WIN_POS+eax*2]
  645.         movzx   edi, word [esi]              ; edi = process
  646.         shl     edi, 5
  647.         cmp     [CURRENT_TASK + edi + TASKDATA.state], byte 9 ; skip dead slots
  648.         je      .check_next_window
  649.         add     edi, window_data
  650. ; \begin{diamond}[19.09.2006]
  651. ; skip minimized windows
  652.         test    [edi + WDATA.fl_wstate], WSTATE_MINIMIZED
  653.         jnz     .check_next_window
  654. ; \end{diamond}
  655.         call    waredraw
  656.  .nothing_to_activate:
  657.         popad
  658.  .dont_activate:
  659.  
  660.         push    esi     ; remove hd1 & cd & flp reservation
  661.         shl     esi, 5
  662.         mov     esi, [esi+CURRENT_TASK+TASKDATA.pid]
  663.         cmp     [cd_status], esi
  664.         jnz     @f
  665.         call    free_cd_channel
  666.         and     [cd_status], 0
  667. @@:
  668.         pop     esi
  669.         cmp     [bgrlockpid], esi
  670.         jnz     @f
  671.         and     [bgrlockpid], 0
  672.         and     [bgrlock], 0
  673. @@:
  674.  
  675.         pusha                 ; remove all port reservations
  676.         mov     edx, esi
  677.         shl     edx, 5
  678.         add     edx, CURRENT_TASK
  679.         mov     edx, [edx+TASKDATA.pid]
  680.  
  681.   rmpr0:
  682.  
  683.         mov     esi, [RESERVED_PORTS]
  684.  
  685.         test    esi, esi
  686.         jz      rmpr9
  687.  
  688.   rmpr3:
  689.  
  690.         mov     edi, esi
  691.         shl     edi, 4
  692.         add     edi, RESERVED_PORTS
  693.  
  694.         cmp     edx, [edi]
  695.         je      rmpr4
  696.  
  697.         dec     esi
  698.         jnz     rmpr3
  699.  
  700.         jmp     rmpr9
  701.  
  702.   rmpr4:
  703.  
  704.         mov     ecx, 256
  705.         sub     ecx, esi
  706.         shl     ecx, 4
  707.  
  708.         mov     esi, edi
  709.         add     esi, 16
  710.         cld
  711.         rep movsb
  712.  
  713.         dec     dword [RESERVED_PORTS]
  714.  
  715.         jmp     rmpr0
  716.  
  717.   rmpr9:
  718.  
  719.         popa
  720.         mov     edi, esi ; do not run this process slot
  721.         shl     edi, 5
  722.         mov     [edi+CURRENT_TASK + TASKDATA.state], byte 9
  723. ; debugger test - terminate all debuggees
  724.         mov     eax, 2
  725.         mov     ecx, SLOT_BASE+2*0x100+APPDATA.debugger_slot
  726. .xd0:
  727.         cmp     eax, [TASK_COUNT]
  728.         ja      .xd1
  729.         cmp     dword [ecx], esi
  730.         jnz     @f
  731.         and     dword [ecx], 0
  732.         pushad
  733.         xchg    eax, ecx
  734.         mov     ebx, 2
  735.         call    sys_system
  736.         popad
  737. @@:
  738.         inc     eax
  739.         add     ecx, 0x100
  740.         jmp     .xd0
  741. .xd1:
  742. ;release slot
  743.  
  744.         bts     [thr_slot_map], esi
  745.  
  746.         mov     ecx, [.process]
  747.         lea     eax, [ecx+PROC.thr_list]
  748.         cmp     eax, [eax+LHEAD.next]
  749.         jne     @F
  750.  
  751.         call    destroy_process.internal
  752. @@:
  753.         sti     ; .. and life goes on
  754.  
  755.         mov     eax, [draw_limits.left]
  756.         mov     ebx, [draw_limits.top]
  757.         mov     ecx, [draw_limits.right]
  758.         mov     edx, [draw_limits.bottom]
  759.         call    calculatescreen
  760.         xor     eax, eax
  761.         xor     esi, esi
  762.         call    redrawscreen
  763.  
  764.         call    unlock_application_table
  765.     ;mov   esi,process_terminated
  766.     ;call  sys_msg_board_str
  767.         add     esp, 8
  768.         ret
  769. restore .slot
  770. restore .process
  771.  
  772. ; Three following procedures are used to guarantee that
  773. ; some part of kernel code will not be terminated from outside
  774. ; while it is running.
  775. ; Note: they do not protect a thread from terminating due to errors inside
  776. ; the thread; accessing a nonexisting memory would still terminate it.
  777.  
  778. ; First two procedures must be used in pair by thread-to-be-protected
  779. ; to signal the beginning and the end of an important part.
  780. ; It is OK to have nested areas.
  781.  
  782. ; The last procedure must be used by outside wanna-be-terminators;
  783. ; if it is safe to terminate the given thread immediately, it returns eax=1;
  784. ; otherwise, it returns eax=0 and notifies the target thread that it should
  785. ; terminate itself when leaving a critical area (the last critical area if
  786. ; they are nested).
  787.  
  788. ; Implementation. Those procedures use one dword in APPDATA for the thread,
  789. ; APPDATA.terminate_protection.
  790. ; * The upper bit is 1 during normal operations and 0 when terminate is requested.
  791. ; * Other bits form a number = depth of critical regions,
  792. ;   plus 1 if the upper bit is 1.
  793. ; * When this dword goes to zero, the thread should be destructed,
  794. ;   and the procedure in which it happened becomes responsible for destruction.
  795.  
  796. ; Enter critical area. Called by thread which wants to be protected.
  797. proc protect_from_terminate
  798.         mov     edx, [current_slot]
  799. ; Atomically increment depth of critical areas and get the old value.
  800.         mov     eax, 1
  801.         lock xadd [edx+APPDATA.terminate_protection], eax
  802. ; If the old value was zero, somebody has started to terminate us,
  803. ; so we are destructing and cannot do anything protected.
  804. ; Otherwise, return to the caller.
  805.         test    eax, eax
  806.         jz      @f
  807.         ret
  808. @@:
  809. ; Wait for somebody to finish us.
  810.         call    change_task
  811.         jmp     @b
  812. endp
  813.  
  814. ; Leave critical area. Called by thread which wants to be protected.
  815. proc unprotect_from_terminate
  816.         mov     edx, [current_slot]
  817. ; Atomically decrement depth of critical areas.
  818.         lock dec [edx+APPDATA.terminate_protection]
  819. ; If the result of decrement is zero, somebody has requested termination,
  820. ; but at that moment we were inside a critical area; terminate now.
  821.         jz      sys_end
  822. ; Otherwise, return to the caller.
  823.         ret
  824. endp
  825.  
  826. ; Request termination of thread identified by edx = SLOT_BASE + slot*256.
  827. ; Called by anyone.
  828. proc request_terminate
  829.         xor     eax, eax        ; set return value
  830. ; Atomically clear the upper bit. If it was already zero, then
  831. ; somebody has requested termination before us, so just exit.
  832.         lock btr [edx+APPDATA.terminate_protection], 31
  833.         jnc     .unsafe
  834. ; Atomically decrement depth of critical areas.
  835.         lock dec [edx+APPDATA.terminate_protection]
  836. ; If the result of decrement is nonzero, the target thread is inside a
  837. ; critical area; leave termination to leaving that area.
  838.         jnz     .unsafe
  839. ; Otherwise, it is safe to kill the target now and the caller is responsible
  840. ; for this. Return eax=1.
  841.         inc     eax
  842. .unsafe:
  843.         ret
  844. endp
  845.  
  846.