Subversion Repositories Kolibri OS

Rev

Rev 1161 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1161 Rev 1198
Line 12... Line 12...
12
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
12
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 13... Line 13...
13
 
13
 
Line 14... Line 14...
14
$Revision: 982 $
14
$Revision: 982 $
15
 
-
 
16
 
-
 
17
align 4
-
 
18
idtreg:
-
 
19
     dw   8*0x41-1
15
 
20
     dd   idts+8
-
 
21
 
16
 
22
build_interrupt_table:
17
align 4 ;3A08
23
 
18
build_interrupt_table:
24
	mov    edi, idts+8
-
 
25
	mov    esi, sys_int
-
 
26
	mov    ecx, 0x40
19
        mov     edi, idts
27
     @@:
20
        mov     esi, sys_int
28
	lodsd
21
        mov     ecx, 0x40
29
	mov    [edi],	ax	     ; lower part of offset
22
        mov     eax, (10001110b shl 24) + os_code
30
	mov    [edi+2], word os_code ; segment selector
-
 
31
	mov	ax, word 10001110b shl 8	; type: interrupt gate
23
  @@:   movsw   ;low word of code-entry
32
	mov	[edi+4], eax
-
 
33
	add    edi, 8
-
 
34
	loop	@b
24
        stosd   ;interrupt gate type : os_code selector
35
 
25
        movsw   ;high word of code-entry
36
	;mov    edi,8*0x40+idts+8
26
        loop    @b
37
	mov	dword [edi], (i40 and 0xFFFF) or (os_code shl 16)
27
        movsd   ;copy low  dword of trap gate for int 0x40
Line 38... Line 28...
38
	mov	dword [edi+4], (11101111b shl 8) or (i40 and 0xFFFF0000)
28
        movsd   ;copy high dword of trap gate for int 0x40
39
						; type: trap gate
-
 
40
	ret
29
        lidt    [esi]
41
 
-
 
42
iglobal
-
 
43
 
30
        ret
44
  msg_sel_ker	db "kernel", 0
31
 
45
  msg_sel_app	db "application", 0
32
iglobal
46
 
-
 
47
  sys_int:
33
  align 4
48
    dd e0,debug_exc,e2,e3
-
 
49
    dd e4,e5,e6,e7
34
  sys_int:
50
    dd e8,e9,e10,e11
35
    ;exception handlers addresses (for interrupt gate construction)
Line -... Line 36...
-
 
36
        dd      e0,e1,e2,e3,e4,e5,e6,except_7 ; SEE: core/fpu.inc
51
    dd e12,e13,page_fault_handler,e15
37
        dd      e8,e9,e10,e11,e12,e13,page_fault_exc,e15
52
 
38
        dd      e16, e17,e18, e19
53
    dd except_16, e17,e18, except_19
39
        times   12 dd unknown_interrupt ;int_20..int_31
54
    times 12 dd unknown_interrupt
40
 
55
 
41
    ;interrupt handlers addresses (for interrupt gate construction)
56
    dd	 irq0, irq_serv.irq_1, irq_serv.irq_2
42
        dd      irq0, irq_serv.irq_1, irq_serv.irq_2
57
if USE_COM_IRQ
43
    if  USE_COM_IRQ
58
    dd	 irq_serv.irq_3, irq_serv.irq_4
44
        dd      irq_serv.irq_3, irq_serv.irq_4
59
else
45
    else
-
 
46
        dd      p_irq3, p_irq4 ;??? íåñòûêîâêà
-
 
47
    end if
-
 
48
        dd      irq_serv.irq_5,  p_irq6,          irq_serv.irq_7
-
 
49
        dd      irq_serv.irq_8,  irq_serv.irq_9,  irq_serv.irq_10
-
 
50
        dd      irq_serv.irq_11, irq_serv.irq_12, irqD,p_irq14,p_irq15
-
 
51
        times   16 dd unknown_interrupt ;int_0x30..int_0x3F
-
 
52
 
-
 
53
    ;int_0x40 gate trap (for directly copied)
-
 
54
        dw      i40 and 0xFFFF, os_code, 11101111b shl 8, i40 shr 16
-
 
55
 
-
 
56
  idtreg: ; data for LIDT instruction (!!! must be immediately below sys_int data)
-
 
57
        dw      2*($-sys_int-4)-1
-
 
58
        dd      idts ;0x8000B100
-
 
59
        dw      0    ;ïðîñòî âûðàâíèâàíèå
-
 
60
 
-
 
61
  msg_fault_sel dd  msg_exc_8,msg_exc_u,msg_exc_a,msg_exc_b
-
 
62
                dd  msg_exc_c,msg_exc_d,msg_exc_e
-
 
63
 
-
 
64
  msg_exc_8     db "Double fault", 0
-
 
65
  msg_exc_u     db "Undefined Exception", 0
Line -... Line 66...
-
 
66
  msg_exc_a     db "Invalid TSS", 0
60
    dd	 p_irq3, p_irq4
67
  msg_exc_b     db "Segment not present", 0
Line 61... Line -...
61
end if
-
 
62
    dd	 irq_serv.irq_5,  p_irq6,	  irq_serv.irq_7
68
  msg_exc_c     db "Stack fault", 0
Line 63... Line 69...
63
    dd	 irq_serv.irq_8,  irq_serv.irq_9, irq_serv.irq_10
69
  msg_exc_d     db "General protection fault", 0
64
    dd	 irq_serv.irq_11, irq_serv.irq_12,irqD ,p_irq14,p_irq15
-
 
65
 
70
  msg_exc_e     db "Page fault", 0
66
    times 16 dd unknown_interrupt
71
 
67
 
72
  msg_sel_ker   db "kernel", 0
68
    dd	 i40
-
 
69
endg
73
  msg_sel_app   db "application", 0
70
 
74
 
71
macro save_ring3_context
-
 
72
{
-
 
73
    pushad
-
 
74
}
-
 
75
macro restore_ring3_context
-
 
76
{
75
endg
77
    popad
-
 
78
}
-
 
79
 
76
 
80
; simply return control to interrupted process
77
macro save_ring3_context {
81
unknown_interrupt:
78
        pushad
82
     iret
79
}
83
 
-
 
-
 
80
macro restore_ring3_context {
Line 84... Line 81...
84
macro exc_wo_code [num]
81
        popad
85
{
-
 
86
  forward
-
 
87
  e#num :
82
}
88
      save_ring3_context
83
macro exc_wo_code [num] {
89
      mov bl, num
84
  e#num :
90
      jmp exc_c
85
        save_ring3_context
91
}
86
        mov     bl, num
92
 
-
 
-
 
87
        jmp     exc_c
Line 93... Line -...
93
macro exc_w_code [num]
-
 
94
{
-
 
Line 95... Line 88...
95
  forward
88
} exc_wo_code   0,1,2,3,4,5,6,15,16,19
96
  e#num :
-
 
97
      add esp, 4
-
 
98
      save_ring3_context
89
 
-
 
90
macro exc_w_code [num] {
Line -... Line 91...
-
 
91
  e#num :
-
 
92
        add     esp, 4
-
 
93
        save_ring3_context
-
 
94
        mov     bl, num
-
 
95
        jmp     exc_c
-
 
96
} exc_w_code    8,9,10,11,12,13,17,18
-
 
97
 
-
 
98
 
-
 
99
uglobal
-
 
100
  pf_err_code   dd ?
-
 
101
endg
-
 
102
 
-
 
103
page_fault_exc:                 ; äóðàêîóñòî÷èâîñòü: ñåëåêòîðû èñïîð÷åíû...
-
 
104
        pop     [ss:pf_err_code]; äåéñòâèòåëüíî äî ñëåäóþùåãî #PF
-
 
105
        save_ring3_context
-
 
106
        mov     bl,14
-
 
107
 
-
 
108
exc_c:                          ; èñêëþ÷åíèÿ (âñå, êðîìå 7-ãî - #NM)
-
 
109
; Ôðýéì ñòåêà ïðè èñêëþ÷åíèè/ïðåðûâàíèè èç 3-ãî êîëüöà + pushad (ò.å., èìåííî çäåñü)
-
 
110
  reg_ss        equ esp+0x30
-
 
111
  reg_esp3      equ esp+0x2C
-
 
112
  reg_eflags    equ esp+0x28
-
 
113
  reg_cs3       equ esp+0x24
-
 
114
  reg_eip       equ esp+0x20
-
 
115
 ; ýòî ôðýéì îò pushad
-
 
116
  reg_eax       equ esp+0x1C
99
      mov bl, num
117
  reg_ecx       equ esp+0x18
100
      jmp exc_c
118
  reg_edx       equ esp+0x14
101
}
119
  reg_ebx       equ esp+0x10
102
 
-
 
-
 
120
  reg_esp0      equ esp+0x0C
103
exc_wo_code 0, 2, 3, 4, 5, 6, 9, 15, 18
121
  reg_ebp       equ esp+0x08
104
exc_w_code 8, 10, 11, 12, 13, 14, 17
-
 
-
 
122
  reg_esi       equ esp+0x04
105
 
123
  reg_edi       equ esp+0x00
-
 
124
 
-
 
125
        Mov     ds,ax,app_data  ; çàãðóçèì ïðàâèëüíûå çíà÷åíèÿ
-
 
126
        mov     es,ax           ; â ñåãìåíòíûå ðåãèñòðû
-
 
127
        cld                     ; è ïðèâîäèì DF ê ñòàíäàðòó
-
 
128
        movzx   ebx,bl
-
 
129
; redirect to V86 manager? (EFLAGS & 0x20000) != 0?
106
exc_c:
130
        test    byte[reg_eflags+2],2
107
	mov   ax, app_data  ;èñêëþ÷åíèå
131
        jnz     v86_exc_c
108
	mov   ds, ax	    ;çàãðóçèì ïðàâèëüíûå çíà÷åíè
132
        cmp     bl,14           ; #PF
109
	mov   es, ax	    ;â ðåãèñòðû
133
        jne     @f
110
 
134
        call    page_fault_handler ; SEE: core/memory.inc
111
; redirect to V86 manager? (EFLAGS & 0x20000) != 0?
-
 
112
	test	byte [esp+20h+8+2], 2
-
 
113
	jnz	v86_exc_c
135
  @@:   mov     esi, [current_slot]
114
 
-
 
115
; test if debugging
136
        btr     [esi+APPDATA.except_mask], ebx
116
	cli
137
        jnc     @f
117
	mov   eax, [current_slot]
-
 
118
	mov   eax, [eax+APPDATA.debugger_slot]
138
        mov     eax,[esi+APPDATA.exc_handler]
119
	test  eax, eax
-
 
120
	jnz   .debug
139
        test    eax, eax
121
	sti
140
        jnz     IRetToUserHook
122
; not debuggee => say error and terminate
141
  @@:   cli
-
 
142
        mov     eax, [esi+APPDATA.debugger_slot]
123
	movzx eax, bl
143
        test    eax, eax
-
 
144
        jnz     .debug
-
 
145
        sti
124
	mov   [error_interrupt], eax
146
; not debuggee => say error and terminate
125
	call  show_error_parameters
147
        call    show_error_parameters ;; only ONE using, inline ???
126
	add   esp, 0x20
148
       ;mov     edx, [TASK_BASE]
127
	mov   edx, [TASK_BASE]
149
        mov     [edx + TASKDATA.state], byte 4 ; terminate
128
	mov   [edx + TASKDATA.state], byte 4
150
        jmp     change_task     ; stack - here it does not matter at all, SEE: core/shed.inc
129
 
151
.debug:
130
	jmp   change_task
152
; we are debugged process, notify debugger and suspend ourself
131
 
153
; eax=debugger PID
132
.debug:
154
        mov     ecx,1           ; debug_message code=other_exception
-
 
155
        cmp     bl,1            ; #DB
133
; we are debugged process, notify debugger and suspend ourself
156
        jne     .notify         ; notify debugger and suspend ourself
-
 
157
        mov     ebx, dr6        ; debug_message data=DR6_image
-
 
158
        xor     edx, edx
-
 
159
        mov     dr6, edx
-
 
160
        mov     edx, dr7
-
 
161
        mov     cl, not 8
-
 
162
  .l1:  shl     dl,2
134
; eax=debugger PID
163
        jc      @f
135
	cld
164
        and     bl, cl
136
	movzx ecx, bl
165
  @@:   sar     cl,1
137
	push  ecx
166
        jc      .l1
138
	mov   ecx, [TASK_BASE]
167
        mov     cl, 3           ; debug_message code=debug_exception
139
	push  dword [ecx+TASKDATA.pid]	  ; PID of current process
168
.notify:
Line 140... Line 169...
140
	push  12
169
        push    ebx             ; debug_message data
-
 
170
        mov     ebx, [TASK_BASE]
141
	pop   ecx
171
        push    [ebx+TASKDATA.pid] ; PID
-
 
172
        push    ecx             ; debug_message code ((here: ecx==1/3))
-
 
173
        mov     cl, 12          ; debug_message size
-
 
174
        call    debugger_notify ;; only ONE using, inline ??? SEE: core/debug.inc
-
 
175
        add     esp,12
142
	push  1        ; 1=exception
176
        mov     edx, [TASK_BASE]
143
	call  debugger_notify
177
        mov     byte [edx+TASKDATA.state], 1 ; suspended
Line 144... Line 178...
144
	pop   ecx
178
        call    change_task     ; SEE: core/shed.inc
145
	pop   ecx
179
        restore_ring3_context
146
	pop   ecx
180
        iretd
147
	mov   edx, [TASK_BASE]
-
 
148
	mov   byte [edx+TASKDATA.state], 1	  ; suspended
181
 
149
	call  change_task
-
 
150
	restore_ring3_context
182
IRetToUserHook:
151
	iretd
-
 
152
 
-
 
153
iglobal
-
 
154
	hexletters	db '0123456789ABCDEF'
-
 
155
	error_interrupt dd  -1
183
        xchg    eax, [reg_eip]
156
endg
-
 
157
 
-
 
158
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-
 
159
show_error_parameters:
-
 
160
    mov eax,[CURRENT_TASK]
184
        sub     dword[reg_esp3], 8
161
    shl eax, 5
-
 
162
    DEBUGF  1, "K : Process - forced terminate PID: %x\n", [CURRENT_TASK + TASKDATA.pid + eax]
-
 
163
    mov eax, [error_interrupt]
-
 
164
    cmp al, 0x08
-
 
165
    jne @f
185
        mov     edi, [reg_esp3]
166
    DEBUGF  1, "K : Double fault\n"
-
 
167
    jmp defined_error
-
 
168
@@:
-
 
169
    cmp al, 0x0a
-
 
170
    jne @f
186
        stosd
171
    DEBUGF  1, "K : Invalid TSS\n"
-
 
172
    jmp defined_error
187
        mov     [edi], ebx
173
@@:
-
 
174
    cmp al, 0x0b
-
 
175
    jne @f
-
 
176
    DEBUGF  1, "K : Segment not present\n"
-
 
177
    jmp defined_error
188
        restore_ring3_context
178
@@:
-
 
179
    cmp al, 0x0c
-
 
180
    jne @f
-
 
181
    DEBUGF  1, "K : Stack fault\n"
-
 
182
    jmp defined_error
-
 
183
@@:
-
 
184
    cmp al, 0x0d
-
 
185
    jne @f
-
 
186
    DEBUGF  1, "K : General protection fault\n"
189
unknown_interrupt:
187
    jmp defined_error
190
        iretd
188
@@:
191
 
189
    cmp al, 0x0e
192
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
190
    jne @f
193
show_error_parameters:
191
    DEBUGF  1, "K : Page fault\n"
194
        mov     edx,[TASK_BASE] ;not scratched below
192
    jmp defined_error
195
        DEBUGF  1, "K : Process - forced terminate PID: %x\n", [edx+TASKDATA.pid]
193
@@:
-
 
-
 
196
        cmp     bl, 0x08
-
 
197
        jb      .l0
-
 
198
        cmp     bl, 0x0e
194
    DEBUGF  1, "K : Undefined Exception\n"
199
        jbe     .l1
195
defined_error:
200
  .l0:  mov     bl, 0x09
196
    DEBUGF  1, "K : EAX : %x EBX : %x ECX : %x\n", [esp + 0x20], [esp - 12 + 0x20], [esp - 4 + 0x20]
201
  .l1:  mov     eax,[msg_fault_sel+ebx*4 - 0x08*4]
Line -... Line 202...
-
 
202
        DEBUGF  1, "K : %s\n", eax
-
 
203
        mov     eax, [reg_cs3+4]
-
 
204
        mov     edi, msg_sel_app
-
 
205
        mov     ebx, [reg_esp3+4]
-
 
206
        cmp     eax, app_code
-
 
207
        je      @f
-
 
208
        mov     edi, msg_sel_ker
-
 
209
        mov     ebx, [reg_esp0+4]
-
 
210
    @@: DEBUGF  1, "K : EAX : %x EBX : %x ECX : %x\n", [reg_eax+4], [reg_ebx+4], [reg_ecx+4]
-
 
211
        DEBUGF  1, "K : EDX : %x ESI : %x EDI : %x\n", [reg_edx+4], [reg_esi+4], [reg_edi+4]
-
 
212
        DEBUGF  1, "K : EBP : %x EIP : %x ESP : %x\n", [reg_ebp+4], [reg_eip+4], ebx
-
 
213
        DEBUGF  1, "K : Flags : %x CS : %x (%s)\n", [reg_eflags+4], eax, edi
-
 
214
        ret
Line 197... Line 215...
197
    DEBUGF  1, "K : EDX : %x ESI : %x EDI : %x\n", [esp - 8 + 0x20], [esp - 24 + 0x20], [esp - 28 + 0x20]
215
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
198
    DEBUGF  1, "K : EBP : %x EIP : %x ", [esp - 20 + 0x20], [esp + 4 + 0x20]
216
 
199
 
-
 
200
    mov eax, [esp + 8 + 0x20]
-
 
201
    mov edi, msg_sel_app
217
  restore  reg_ss
202
    mov ebx, [esp + 16 + 0x20]
218
  restore  reg_esp3
203
    cmp eax, app_code
219
  restore  reg_eflags
204
    je	@f
220
  restore  reg_cs
Line 205... Line -...
205
    mov edi, msg_sel_ker
-
 
Line 206... Line 221...
206
    mov ebx, [esp - 16 + 0x20]
221
  restore  reg_eip
207
@@:
222
  restore  reg_eax
208
    DEBUGF  1, "ESP : %x\nK : Flags : %x CS : %x (%s)\n", ebx, [esp + 12 + 0x20], eax, edi
223
  restore  reg_ecx
Line 275... Line 290...
275
     out    0xa0,al
290
        out     0xa0,al
276
     out   0x20, al
291
        out     0x20, al
277
     ret
292
        ret
Line 278... Line 293...
278
 
293
 
279
irqD:
-
 
280
     save_ring3_context
-
 
281
     mov   ax, app_data  ;os_data
-
 
282
     mov   ds, ax
294
irqD:
283
     mov   es, ax
-
 
284
 
-
 
285
     mov   dx,0xf0
295
        push  eax
286
     mov   al,0
296
        mov   al,0
287
     out   dx,al
-
 
288
 
-
 
289
     mov   dx,0xa0
297
        out   0xf0,al
290
     mov   al,0x20
298
        mov   al,0x20
291
     out   dx,al
299
        out   0xa0,al
292
     mov   dx,0x20
300
        out   0x20,al
293
     out   dx,al
-
 
294
 
-
 
295
     restore_ring3_context
-
 
296
 
301
        pop   eax
Line -... Line 302...
-
 
302
        iret
-
 
303
 
297
     iret
304
 
Line 298... Line 305...
298
 
305
irqh 2,3,4,5,7,8,9,10,11
299
 
306
 
300
irqhandler:
307
irqhandler:
Line 419... Line 426...
419
;
426
;
420
; on return : eax = pid
427
; on return : eax = pid
421
jmp new_sys_threads
428
jmp new_sys_threads
Line 422... Line 429...
422
 
429
 
423
iglobal
430
iglobal
424
  process_terminating	db 'K : Process - terminating',13,10,0
431
;  process_terminating  db 'K : Process - terminating',13,10,0
425
  process_terminated	db 'K : Process - done',13,10,0
432
;  process_terminated   db 'K : Process - done',13,10,0
426
  msg_obj_destroy	db 'K : destroy app object',13,10,0
433
  msg_obj_destroy       db 'K : destroy app object',13,10,0
Line 427... Line 434...
427
endg
434
endg
428
 
435
 
Line 443... Line 450...
443
	   mov	  [CURRENT_TASK+esi+TASKDATA.state], 9
450
           mov    [CURRENT_TASK+esi+TASKDATA.state], 9
444
	   ret
451
           ret
445
@@:
452
@@:
446
	   ;mov    esi,process_terminating
453
           ;mov    esi,process_terminating
447
	   ;call   sys_msg_board_str
454
           ;call   sys_msg_board_str
448
	   DEBUGF 1,"%s",process_terminating
-
 
449
@@:
455
@@:
450
	   cli
456
           cli
451
	   cmp	 [application_table_status],0
457
           cmp   [application_table_status],0
452
	   je	 term9
458
           je    term9
453
	   sti
459
           sti
Line 803... Line 809...
803
    mov   [DONT_DRAW_MOUSE],byte 0  ; draw mouse
809
    mov   [DONT_DRAW_MOUSE],byte 0  ; draw mouse
Line 804... Line 810...
804
 
810
 
805
    mov   [application_table_status],0
811
    mov   [application_table_status],0
806
    ;mov   esi,process_terminated
812
    ;mov   esi,process_terminated
807
    ;call  sys_msg_board_str
-
 
808
    DEBUGF 1,"%s",process_terminated
813
    ;call  sys_msg_board_str
809
    add esp, 4
814
    add esp, 4
810
    ret
815
    ret
Line 811... Line 816...
811
restore .slot
816
restore .slot