Subversion Repositories Kolibri OS

Rev

Rev 9976 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2288 clevermous 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
9715 Doczom 3
;; Copyright (C) KolibriOS team 2004-2022. All rights reserved. ;;
6793 pathoswith 4
;;  Distributed under terms of the GNU General Public License.  ;;
2288 clevermous 5
;;                                                              ;;
6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7
 
8
$Revision: 10002 $
9
 
9975 Jurgen 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
 
2288 clevermous 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
  @@:
8050 rgimad 25
        movsw   ; low word of code-entry
26
        stosd   ; interrupt gate type : os_code selector
27
        movsw   ; high word of code-entry
2288 clevermous 28
        loop    @b
8050 rgimad 29
        movsd   ; copy low  dword of trap gate for int 0x40
30
        movsd   ; copy high dword of trap gate for int 0x40
7733 dunkaist 31
        mov     ecx, 23
32
        mov     eax, (10001110b shl 24) + os_code
33
  @@:
8050 rgimad 34
        movsw   ; low word of code-entry
35
        stosd   ; interrupt gate type : os_code selector
36
        movsw   ; high word of code-entry
7733 dunkaist 37
        loop    @b
2288 clevermous 38
        lidt    [esi]
39
        ret
40
 
41
iglobal
42
  align 4
43
  sys_int:
8050 rgimad 44
    ; exception handlers addresses (for interrupt gate construction)
2288 clevermous 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
 
8050 rgimad 50
    ; interrupt handlers addresses (for interrupt gate construction)
7733 dunkaist 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\}
2288 clevermous 56
 
7733 dunkaist 57
        ; int_0x40 gate trap (for directly copied)
58
        dw i40 and 0xFFFF, os_code, 11101111b shl 8, i40 shr 16
2288 clevermous 59
 
7733 dunkaist 60
        rept 23 irqn:33 \{dd irq_serv.irq_\#irqn\}
61
 
2288 clevermous 62
  idtreg: ; data for LIDT instruction (!!! must be immediately below sys_int data)
63
        dw      2*($-sys_int-4)-1
8050 rgimad 64
        dd      idts ; 0x8000B100
65
        dw      0    ; alignment
2288 clevermous 66
 
67
  msg_fault_sel dd  msg_exc_8,msg_exc_u,msg_exc_a,msg_exc_b
6817 dunkaist 68
                dd  msg_exc_c,msg_exc_d,msg_exc_e,msg_exc_u
69
                dd  msg_exc_u,msg_exc_11
2288 clevermous 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
6817 dunkaist 78
  msg_exc_11    db "Alignment Check", 0
2288 clevermous 79
 
3342 yogev_ezra 80
  if lang eq sp
3344 yogev_ezra 81
    include 'core/sys32-sp.inc'
3342 yogev_ezra 82
  else
83
    msg_sel_ker   db "kernel", 0
84
    msg_sel_app   db "application", 0
3309 esevece 85
  end if
2288 clevermous 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
 
8050 rgimad 115
page_fault_exc:                   ; foolproof: selectors are clobbered ...
116
        pop     [ss:pf_err_code]  ; actually, until the next #PF
10002 Jurgen 117
 
118
        cmp     edi, CONTROL_EXCEPTION ; equ 'EXPT'
119
        jne     .no_ctrl_exc
120
        bt      dword [esp], 31
121
        jc      .setret
122
        test    esi, esi
123
        jl      .no_ctrl_exc
124
.setret:
125
        mov     [esp], esi
126
        iret
127
 
128
.no_ctrl_exc:
2288 clevermous 129
        save_ring3_context
130
        mov     bl, 14
131
 
8050 rgimad 132
exc_c:                            ; exceptions (all but 7th - #NM)
133
 ; stack frame when exception/interrupt from ring3 + pushad (i.e right here)
2288 clevermous 134
  reg_ss        equ esp+0x30
135
  reg_esp3      equ esp+0x2C
136
  reg_eflags    equ esp+0x28
137
  reg_cs3       equ esp+0x24
138
  reg_eip       equ esp+0x20
8050 rgimad 139
 ; this if frame from pushad
2288 clevermous 140
  reg_eax       equ esp+0x1C
141
  reg_ecx       equ esp+0x18
142
  reg_edx       equ esp+0x14
143
  reg_ebx       equ esp+0x10
144
  reg_esp0      equ esp+0x0C
145
  reg_ebp       equ esp+0x08
146
  reg_esi       equ esp+0x04
147
  reg_edi       equ esp+0x00
148
 
8050 rgimad 149
        mov     ax, app_data       ; exception
150
        mov     ds, ax             ; load proper values
151
        mov     es, ax             ; to registers
8051 rgimad 152
        cld                        ; clear the direction flag
2288 clevermous 153
        movzx   ebx, bl
154
; redirect to V86 manager? (EFLAGS & 0x20000) != 0?
155
        test    byte[reg_eflags+2], 2
156
        jnz     v86_exc_c
8050 rgimad 157
        cmp     bl, 14             ; #PF
2288 clevermous 158
        jne     @f
159
        call    page_fault_handler ; SEE: core/memory.inc
160
  @@:
161
        mov     esi, [current_slot]
9715 Doczom 162
        btr     [esi + APPDATA.except_mask], ebx
2288 clevermous 163
        jnc     @f
9715 Doczom 164
        mov     eax, [esi + APPDATA.exc_handler]
2288 clevermous 165
        test    eax, eax
166
        jnz     IRetToUserHook
167
  @@:
168
        cli
9715 Doczom 169
        mov     eax, [esi + APPDATA.debugger_slot]
2288 clevermous 170
        test    eax, eax
171
        jnz     .debug
6793 pathoswith 172
; not debuggee => say error and terminate
9709 Doczom 173
        call    show_error_parameters  ; this function output in edx = current_slot
2288 clevermous 174
        sti
9709 Doczom 175
        mov     [edx + APPDATA.state], TSTATE_TERMINATING
3534 clevermous 176
        call    wakeup_osloop
3615 clevermous 177
        call    change_task
178
; If we're here, then the main OS thread has crashed before initializing IDLE thread.
179
; Or they both have crashed. Anyway, things are hopelessly broken.
180
        hlt
181
        jmp     $-1
2288 clevermous 182
.debug:
183
; we are debugged process, notify debugger and suspend ourself
184
; eax=debugger PID
185
        mov     ecx, 1          ; debug_message code=other_exception
186
        cmp     bl, 1           ; #DB
187
        jne     .notify         ; notify debugger and suspend ourself
188
        mov     ebx, dr6        ; debug_message data=DR6_image
189
        xor     edx, edx
190
        mov     dr6, edx
191
        mov     edx, dr7
192
        mov     cl, not 8
193
  .l1:
194
        shl     dl, 2
195
        jc      @f
196
        and     bl, cl
197
  @@:
198
        sar     cl, 1
199
        jc      .l1
200
        mov     cl, 3           ; debug_message code=debug_exception
201
.notify:
202
        push    ebx             ; debug_message data
9692 Doczom 203
        mov     ebx, [current_slot]
204
        push    [ebx + APPDATA.tid] ; PID
2288 clevermous 205
        push    ecx             ; debug_message code ((here: ecx==1/3))
206
        mov     cl, 12          ; debug_message size
207
        call    debugger_notify ;; only ONE using, inline ??? SEE: core/debug.inc
208
        add     esp, 12
9709 Doczom 209
        mov     edx, [current_slot]
210
        mov     [edx + APPDATA.state], TSTATE_RUN_SUSPENDED
2288 clevermous 211
        call    change_task     ; SEE: core/shed.inc
212
        restore_ring3_context
213
        iretd
214
 
215
IRetToUserHook:
9976 Jurgen 216
        cmp     ebx, 12
217
        je      .ex_stack
218
        cmp     ebx, 14
219
        jne     .nostack