Subversion Repositories Kolibri OS

Rev

Rev 3742 | Rev 3911 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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