Subversion Repositories Kolibri OS

Rev

Rev 3534 | Rev 3558 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2288 clevermous 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
2455 mario79 3
;; Copyright (C) KolibriOS team 2004-2011. All rights reserved. ;;
2288 clevermous 4
;; Distributed under terms of the GNU General Public License    ;;
5
;;                                                              ;;
6
;;                                                              ;;
7
;;  MenuetOS process management, protected ring3                ;;
8
;;                                                              ;;
9
;;  Distributed under GPL. See file COPYING for details.        ;;
10
;;  Copyright 2003 Ville Turjanmaa                              ;;
11
;;                                                              ;;
12
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13
 
14
$Revision: 3539 $
15
 
16
align 4 ;3A08
17
build_interrupt_table:
18
        mov     edi, idts
19
        mov     esi, sys_int
20
        mov     ecx, 0x40
21
        mov     eax, (10001110b shl 24) + os_code
22
  @@:
23
        movsw   ;low word of code-entry
24
        stosd   ;interrupt gate type : os_code selector
25
        movsw   ;high word of code-entry
26
        loop    @b
27
        movsd   ;copy low  dword of trap gate for int 0x40
28
        movsd   ;copy high dword of trap gate for int 0x40
29
        lidt    [esi]
30
        ret
31
 
32
iglobal
33
  align 4
34
  sys_int:
35
    ;exception handlers addresses (for interrupt gate construction)
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
38
        dd      e16, e17,e18, e19
39
        times   12 dd unknown_interrupt ;int_20..int_31
40
 
41
    ;interrupt handlers addresses (for interrupt gate construction)
42
        ; 0x20 .. 0x2F - IRQ handlers
43
        dd      irq0, irq_serv.irq_1, irq_serv.irq_2
44
        dd      irq_serv.irq_3, irq_serv.irq_4
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
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
49
        dd irq_serv.irq_17
50
        dd irq_serv.irq_18
51
        dd irq_serv.irq_19
52
        dd irq_serv.irq_20
53
        dd irq_serv.irq_21
54
        dd irq_serv.irq_22
55
        dd irq_serv.irq_23
56
 
57
    times 32 - IRQ_RESERVED dd unknown_interrupt
58
    ;int_0x40 gate trap (for directly copied)
59
        dw      i40 and 0xFFFF, os_code, 11101111b shl 8, i40 shr 16
60
 
61
  idtreg: ; data for LIDT instruction (!!! must be immediately below sys_int data)
62
        dw      2*($-sys_int-4)-1
63
        dd      idts ;0x8000B100
3539 clevermous 64
        dw      0    ;просто выравнивание
2288 clevermous 65
 
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
68
 
69
  msg_exc_8     db "Double fault", 0
70
  msg_exc_u     db "Undefined Exception", 0
71
  msg_exc_a     db "Invalid TSS", 0
72
  msg_exc_b     db "Segment not present", 0
73
  msg_exc_c     db "Stack fault", 0
74
  msg_exc_d     db "General protection fault", 0
75
  msg_exc_e     db "Page fault", 0
76
 
3342 yogev_ezra 77
  if lang eq sp
3344 yogev_ezra 78
    include 'core/sys32-sp.inc'
3342 yogev_ezra 79
  else
80
    msg_sel_ker   db "kernel", 0
81
    msg_sel_app   db "application", 0
3309 esevece 82
  end if
2288 clevermous 83
 
84
endg
85
 
86
macro save_ring3_context {
87
        pushad
88
}
89
macro restore_ring3_context {
90
        popad
91
}
92
macro exc_wo_code [num] {
93
  e#num :
94
        save_ring3_context
95
        mov     bl, num
96
        jmp     exc_c
97
} exc_wo_code   0,1,2,3,4,5,6,15,16,19
98
 
99
macro exc_w_code [num] {
100
  e#num :
101
        add     esp, 4
102
        save_ring3_context
103
        mov     bl, num
104
        jmp     exc_c
105
} exc_w_code    8,9,10,11,12,13,17,18
106
 
107
 
108
uglobal
109
  pf_err_code   dd ?
110
endg
111
 
3539 clevermous 112
page_fault_exc:                 ; дуракоусточивость: селекторы испорчены...
113
        pop     [ss:pf_err_code]; действительно до следующего #PF
2288 clevermous 114
        save_ring3_context
115
        mov     bl, 14
116
 
3539 clevermous 117
exc_c:                          ; исключения (все, кроме 7-го - #NM)
118
; Фрэйм стека при исключении/прерывании из 3-го кольца + pushad (т.е., именно здесь)
2288 clevermous 119
  reg_ss        equ esp+0x30
120
  reg_esp3      equ esp+0x2C
121
  reg_eflags    equ esp+0x28
122
  reg_cs3       equ esp+0x24
123
  reg_eip       equ esp+0x20
3539 clevermous 124
 ; это фрэйм от pushad
2288 clevermous 125
  reg_eax       equ esp+0x1C
126
  reg_ecx       equ esp+0x18
127
  reg_edx       equ esp+0x14
128
  reg_ebx       equ esp+0x10
129
  reg_esp0      equ esp+0x0C
130
  reg_ebp       equ esp+0x08
131
  reg_esi       equ esp+0x04
132
  reg_edi       equ esp+0x00
133
 
3539 clevermous 134
        mov     ax, app_data        ;исключение
135
        mov     ds, ax                  ;загрузим правильные значения
136
        mov     es, ax                  ;в регистры
137
        cld                     ; и приводим DF к стандарту
2288 clevermous 138
        movzx   ebx, bl
139
; redirect to V86 manager? (EFLAGS & 0x20000) != 0?
140
        test    byte[reg_eflags+2], 2
141
        jnz     v86_exc_c
142
        cmp     bl, 14          ; #PF
143
        jne     @f
144
        call    page_fault_handler ; SEE: core/memory.inc
145
  @@:
146
        mov     esi, [current_slot]
147
        btr     [esi+APPDATA.except_mask], ebx
148
        jnc     @f
149
        mov     eax, [esi+APPDATA.exc_handler]
150
        test    eax, eax
151
        jnz     IRetToUserHook
152
  @@:
153
        cli
154
        mov     eax, [esi+APPDATA.debugger_slot]
155
        test    eax, eax
156
        jnz     .debug
157
        sti
158
; not debuggee => say error and terminate
159
        call    show_error_parameters ;; only ONE using, inline ???
160
       ;mov     edx, [TASK_BASE]
161
        mov     [edx + TASKDATA.state], byte 4 ; terminate
3534 clevermous 162
        call    wakeup_osloop
2288 clevermous 163
        jmp     change_task     ; stack - here it does not matter at all, SEE: core/shed.inc
164
.debug:
165
; we are debugged process, notify debugger and suspend ourself
166
; eax=debugger PID
167
        mov     ecx, 1          ; debug_message code=other_exception
168
        cmp     bl, 1           ; #DB
169
        jne     .notify         ; notify debugger and suspend ourself
170
        mov     ebx, dr6        ; debug_message data=DR6_image
171
        xor     edx, edx
172
        mov     dr6, edx
173
        mov     edx, dr7
174
        mov     cl, not 8
175
  .l1:
176
        shl     dl, 2
177
        jc      @f
178
        and     bl, cl
179
  @@:
180
        sar     cl, 1
181
        jc      .l1
182
        mov     cl, 3           ; debug_message code=debug_exception
183
.notify:
184
        push    ebx             ; debug_message data
185
        mov     ebx, [TASK_BASE]
186
        push    [ebx+TASKDATA.pid] ; PID
187
        push    ecx             ; debug_message code ((here: ecx==1/3))
188
        mov     cl, 12          ; debug_message size
189
        call    debugger_notify ;; only ONE using, inline ??? SEE: core/debug.inc
190
        add     esp, 12
191
        mov     edx, [TASK_BASE]
192
        mov     byte [edx+TASKDATA.state], 1 ; suspended
193
        call    change_task     ; SEE: core/shed.inc
194
        restore_ring3_context
195
        iretd
196
 
197
IRetToUserHook:
198
        xchg    eax, [reg_eip]
199
        sub     dword[reg_esp3], 8
200
        mov     edi, [reg_esp3]
201
        stosd
202
        mov     [edi], ebx
203
        restore_ring3_context
204
; simply return control to interrupted process
205
unknown_interrupt:
206
        iretd
207
 
208
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
209
; bl - error vector
210
show_error_parameters:
211
        cmp     bl, 0x06
212
        jnz     .no_ud
213
        push    ebx
214
        mov     ebx, ud_user_message
215
        mov     ebp, notifyapp
216
        call    fs_execute_from_sysdir_param
217
        pop     ebx
218
.no_ud:
219
        mov     edx, [TASK_BASE];not scratched below
3309 esevece 220
        if lang eq sp
221
        DEBUGF  1, "K : Proceso - terminado forzado PID: %x [%s]\n", [edx+TASKDATA.pid], [current_slot]
222
        else
3294 clevermous 223
        DEBUGF  1, "K : Process - forced terminate PID: %x [%s]\n", [edx+TASKDATA.pid], [current_slot]
3309 esevece 224
        end if
2288 clevermous 225
        cmp     bl, 0x08
226
        jb      .l0
227
        cmp     bl, 0x0e
228
        jbe     .l1
229
  .l0:
230
        mov     bl, 0x09
231
  .l1:
232
        mov     eax, [msg_fault_sel+ebx*4 - 0x08*4]
233
        DEBUGF  1, "K : %s\n", eax
234
        mov     eax, [reg_cs3+4]
235
        mov     edi, msg_sel_app
236
        mov     ebx, [reg_esp3+4]
237
        cmp     eax, app_code
238
        je      @f
239
        mov     edi, msg_sel_ker
240
        mov     ebx, [reg_esp0+4]
241
    @@:
242
        DEBUGF  1, "K : EAX : %x EBX : %x ECX : %x\n", [reg_eax+4], [reg_ebx+4], [reg_ecx+4]
243
        DEBUGF  1, "K : EDX : %x ESI : %x EDI : %x\n", [reg_edx+4], [reg_esi+4], [reg_edi+4]