sys32 - concentration all (except #NM) exception handlers in ecx_c for optimization purpose

debug  - moved handler to sys32::exc_c
fpu    - moved except_16/19 to sys32::exc_c
memory - changing interface for page_fault_handler
v86    - changing interface for handler v86_exc_c
kernel - small optimization

git-svn-id: svn://kolibrios.org@1056 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
Galkov 2009-04-04 19:58:42 +00:00
parent 3be0aa5c21
commit 82da211b9b
7 changed files with 799 additions and 887 deletions

View File

@ -415,59 +415,28 @@ debugger_notify:
.ret:
ret
debug_exc:
test byte [esp+8+2], 2
jnz v86_debug_exc
; int 1 = #DB
save_ring3_context
cld
mov ax, app_data ;os_data
mov ds, ax
mov es, ax
mov eax, dr6
push eax
xor eax, eax
mov dr6, eax
; test if debugging
cli
mov eax, [current_slot]
mov eax, [eax+APPDATA.debugger_slot]
test eax, eax
jnz .debug
sti
; not debuggee => say error and terminate
add esp, 0x20+4
mov [error_interrupt], 1
call show_error_parameters
mov edx, [TASK_BASE]
mov byte [edx+TASKDATA.state], 4
jmp change_task
.debug:
debug_ex:
; we are debugged process, notify debugger and suspend ourself
; eax=debugger PID
pop edx
mov edx, dr6 ; debug_message data=DR6_image
xor ebx, ebx
mov dr6, ebx
mov ebx, dr7
mov cl, not 1
.l1:
test bl, 1
jnz @f
mov cl, not 8
.l1: shl bl,2
jc @f
and dl, cl
@@:
shr ebx, 2
add cl, cl
inc ecx
cmp cl, not 10h
jnz .l1
push edx ; DR6 image
mov ecx, [TASK_BASE]
push dword [ecx+TASKDATA.pid] ; PID
push 12
pop ecx
push 3 ; 3 = debug exception
call debugger_notify
pop ecx
pop ecx
pop ecx
@@: sar cl,1
jc .l1
mov ecx,3 ; debug_message code=debug_exception
.notify:
push edx ; debug_message data
mov ebx, [TASK_BASE]
push [ebx+TASKDATA.pid] ; PID
push ecx ; debug_message code
mov ecx,12 ; debug_message size
call debugger_notify ;; only ONE using, inline ???
add esp,12
mov edx, [TASK_BASE]
mov byte [edx+TASKDATA.state], 1 ; suspended
call change_task

View File

@ -142,7 +142,7 @@ fpu_restore:
ret
align 4
e7: ;#NM exception handler
except_7: ;#NM exception handler
save_ring3_context
clts
mov ax, app_data ;
@ -180,7 +180,7 @@ e7: ;#NM exception handler
iglobal
fpu_owner dd 0
endg
endg
reg_eip equ ebp+4
reg_cs equ ebp+8
@ -188,8 +188,8 @@ reg_eflags equ ebp+12
reg_esp equ ebp+16
reg_ss equ ebp+20
align 4
except_16: ;fpu native exceptions handler
align 4 ;not used now
proc except_16 ;fpu native exceptions handler
test byte [esp+8+2], 2
jnz v86_except_16
push ebp
@ -233,9 +233,10 @@ except_16: ;fpu native exceptions handler
mov bl, 16
jmp exc_c
endp
align 4
except_19: ;sse exceptions handler
align 4 ;not used now
proc except_19 ;sse exceptions handler
test byte [esp+8+2], 2
jnz v86_except_19
push ebp
@ -278,6 +279,7 @@ except_19: ;sse exceptions handler
mov bl, 19
jmp exc_c
endp
restore reg_eip
restore reg_cs

View File

@ -544,23 +544,14 @@ get_pg_addr:
align 4
; Now it is called from sys32::exc_c (see stack frame there)
proc page_fault_handler
test byte [esp+12+2], 2
jnz v86_page_fault
.err_code equ ebp+32
.err_addr equ ebp-4
pushad
mov ebp, esp
mov eax, cr2
push eax
mov ax, app_data
mov ds, ax
mov es, ax
mov ebx, cr2
push ebx ; that is locals: .err_addr = cr2
inc [pg_data.pages_faults]
; push eax
@ -571,10 +562,9 @@ proc page_fault_handler
; pop edx
; pop eax
mov ebx, [.err_addr]
mov eax, [.err_code]
mov eax, [pf_err_code]
cmp ebx, OS_BASE
cmp ebx, OS_BASE ;ebx == .err_addr
jb .user_space ;страница в памяти приложения ;
cmp ebx, page_tabs
@ -583,16 +573,21 @@ proc page_fault_handler
cmp ebx, kernel_tabs
jb .alloc;.app_tabs ;таблицы страниц приложения ;
;просто создадим одну
if 0 ;ïîêà ýòî ïðîñòî ëèøíåå
cmp ebx, LFB_BASE
jb .core_tabs ;таблицы страниц ядра
;Ошибка
.lfb:
.lfb:
;область LFB
;Ошибка
jmp .fail
end if
.core_tabs:
.fail: ;simply return to caller
mov esp, ebp
mov bl, 14 ;#PF
ret
align 4
.user_space:
test eax, PG_MAP
jnz .err_access ;Страница присутствует
@ -615,33 +610,32 @@ align 4
test eax, eax
jz .fail
stdcall map_page,[ebp-4],eax,dword PG_UW
stdcall map_page,[.err_addr],eax,dword PG_UW
mov edi, [ebp-4]
mov edi, [.err_addr]
and edi, 0xFFFFF000
mov ecx, 1024
xor eax, eax
cld
;cld ;caller is duty for this
rep stosd
.exit:
mov esp, ebp
popad
add esp, 4
.exit: ;iret with repeat fault instruction
add esp,8 ; clear in stack: locals(.err_addr) + ret_to_caller
restore_ring3_context
iretd
.err_access:
.err_access = .fail
;никогда не происходит
jmp .fail
;jmp .fail
.kernel_space:
test eax, PG_MAP
jz .fail ;страница не присутствует
test eax, 4 ;U/S
test eax,12 ;U/S (+below)
jnz .fail ;приложение обратилось к памяти
;ядра
test eax, 8
jnz .fail ;óñòàíîâëåí çàðåçåðâèðîâàííûé áèò
;test eax, 8
;jnz .fail ;óñòàíîâëåí çàðåçåðâèðîâàííûé áèò
;в таблицах страниц. добавлено в P4/Xeon
;попытка записи в защищённую страницу ядра
@ -660,7 +654,7 @@ align 4
jz .fail
push eax
stdcall map_page,[ebp-4],eax,dword PG_SW
stdcall map_page,[.err_addr],eax,dword PG_SW
pop eax
mov edi, [.err_addr]
and edi, -4096
@ -674,26 +668,9 @@ align 4
add esi, [default_io_map]
mov ecx, 4096/4
cld
;cld ;caller is duty for this
rep movsd
jmp .exit
;íå îáðàáàòûâàåì. Îøèáêà
.core_tabs:
.fail:
mov esp, ebp
popad
add esp, 4
; iretd
save_ring3_context ;debugger support
mov bl, 14
jmp exc_c
iretd
endp
align 4
@ -1346,6 +1323,3 @@ proc create_ring_buffer stdcall, size:dword, flags:dword
.fail:
ret
endp

View File

@ -14,212 +14,206 @@
$Revision$
align 4
idtreg:
dw 8*0x41-1
dd idts+8
align 4 ;3A08
build_interrupt_table:
mov edi, idts+8
mov edi, idts
mov esi, sys_int
mov ecx, 0x40
@@:
lodsd
mov [edi], ax ; lower part of offset
mov [edi+2], word os_code ; segment selector
mov ax, word 10001110b shl 8 ; type: interrupt gate
mov [edi+4], eax
add edi, 8
mov eax, (10001110b shl 24) + os_code
@@: movsw ;low word of code-entry
stosd ;interrupt gate type : os_code selector
movsw ;high word of code-entry
loop @b
;mov edi,8*0x40+idts+8
mov dword [edi], (i40 and 0xFFFF) or (os_code shl 16)
mov dword [edi+4], (11101111b shl 8) or (i40 and 0xFFFF0000)
; type: trap gate
movsd ;copy low dword of trap gate for int 0x40
movsd ;copy high dword of trap gate for int 0x40
lidt [esi]
ret
iglobal
align 4
sys_int:
;exception handlers addresses (for interrupt gate construction)
dd e0,e1,e2,e3,e4,e5,e6,except_7
dd e8,e9,e10,e11,e12,e13,page_fault_exc,e15
dd e16, e17,e18, e19
times 12 dd unknown_interrupt ;int_20..int_31
;interrupt handlers addresses (for interrupt gate construction)
dd irq0, irq_serv.irq_1, irq_serv.irq_2
if USE_COM_IRQ
dd irq_serv.irq_3, irq_serv.irq_4
else
dd p_irq3, p_irq4 ;??? íåñòûêîâêà
end if
dd irq_serv.irq_5, p_irq6, irq_serv.irq_7
dd irq_serv.irq_8, irq_serv.irq_9, irq_serv.irq_10
dd irq_serv.irq_11, irq_serv.irq_12, irqD,p_irq14,p_irq15
times 16 dd unknown_interrupt ;int_0x30..int_0x3F
;int_0x40 gate trap (for directly copied)
dw i40 and 0xFFFF, os_code, 11101111b shl 8, i40 shr 16
idtreg: ; data for LIDT instruction (must be immediately below sys_int data)
dw 2*($-sys_int-4)-1
dd idts ;0x8000B100
dw 0 ;ïðîñòî âûðàâíèâàíèå
msg_fault_sel dd msg_exc_8,msg_exc_u,msg_exc_a,msg_exc_b
dd msg_exc_c,msg_exc_d,msg_exc_e
msg_exc_8 db "Double fault", 0
msg_exc_u db "Undefined Exception", 0
msg_exc_a db "Invalid TSS", 0
msg_exc_b db "Segment not present", 0
msg_exc_c db "Stack fault", 0
msg_exc_d db "General protection fault", 0
msg_exc_e db "Page fault", 0
msg_sel_ker db "kernel", 0
msg_sel_app db "application", 0
sys_int:
dd e0,debug_exc,e2,e3
dd e4,e5,e6,e7
dd e8,e9,e10,e11
dd e12,e13,page_fault_handler,e15
dd except_16, e17,e18, except_19
times 12 dd unknown_interrupt
dd irq0, irq_serv.irq_1, irq_serv.irq_2
if USE_COM_IRQ
dd irq_serv.irq_3, irq_serv.irq_4
else
dd p_irq3, p_irq4
end if
dd irq_serv.irq_5, p_irq6, irq_serv.irq_7
dd irq_serv.irq_8, irq_serv.irq_9, irq_serv.irq_10
dd irq_serv.irq_11, irq_serv.irq_12,irqD ,p_irq14,p_irq15
times 16 dd unknown_interrupt
dd i40
endg
macro save_ring3_context
{
macro save_ring3_context {
pushad
}
macro restore_ring3_context
{
macro restore_ring3_context {
popad
}
; simply return control to interrupted process
unknown_interrupt:
iret
macro exc_wo_code [num]
{
forward
macro exc_wo_code [num] {
e#num :
save_ring3_context
mov bl, num
jmp exc_c
}
} exc_wo_code 0,1,2,3,4,5,6,15,16,19
macro exc_w_code [num]
{
forward
macro exc_w_code [num] {
e#num :
add esp, 4
save_ring3_context
mov bl, num
jmp exc_c
}
} exc_w_code 8,9,10,11,12,13,17,18
exc_wo_code 0, 2, 3, 4, 5, 6, 9, 15, 18
exc_w_code 8, 10, 11, 12, 13, 14, 17
exc_c:
mov ax, app_data ;èñêëþ÷åíèå
mov ds, ax ;çàãðóçèì ïðàâèëüíûå çíà÷åíè
mov es, ax ;â ðåãèñòðû
uglobal
pf_err_code dd ?
endg
page_fault_exc:
pop [pf_err_code] ;äåéñòâèòåëüíî äî ñëåäóþùåãî #PF
save_ring3_context
mov bl,14
exc_c: ;èñêëþ÷åíèå
; Ôðýéì ñòåêà ïðè èñêëþ÷åíèè/ïðåðûâàíèè èç 3-ãî êîëüöà + pushad (ò.å., èìåííî çäåñü)
reg_ss equ esp+0x30
reg_esp3 equ esp+0x2C
reg_eflags equ esp+0x28
reg_cs3 equ esp+0x24
reg_eip equ esp+0x20
; ýòî ôðýéì îò pushad
reg_eax equ esp+0x1C
reg_ecx equ esp+0x18
reg_edx equ esp+0x14
reg_ebx equ esp+0x10
reg_esp0 equ esp+0x0C
reg_ebp equ esp+0x08
reg_esi equ esp+0x04
reg_edi equ esp+0x00
Mov ds,ax,app_data ; çàãðóçèì ïðàâèëüíûå çíà÷åíèÿ
mov es,ax ; â ñåãìåíòíûå ðåãèñòðû
cld ; è ïðèâîäèì DF ê ñòàíäàðòó
; redirect to V86 manager? (EFLAGS & 0x20000) != 0?
test byte [esp+20h+8+2], 2
test byte[reg_eflags+2],2
jnz v86_exc_c
; test if debugging
cli
mov eax, [current_slot]
mov eax, [eax+APPDATA.debugger_slot]
cmp bl,14 ; #PF
jne .l0
call page_fault_handler
.l0: mov esi, [current_slot]
mov eax,[esi+APPDATA.fpu_handler]
cmp bl,16 ; #MF
je .l1
cmp bl,19 ; #XF
jne .l2
mov eax,[esi+APPDATA.sse_handler]
.l1: test eax, eax
jnz IRetToUserHook
.l2: cli
mov eax, [esi+APPDATA.debugger_slot]
test eax, eax
jnz .debug
sti
; not debuggee => say error and terminate
movzx eax, bl
mov [error_interrupt], eax
call show_error_parameters
add esp, 0x20
mov edx, [TASK_BASE]
mov [edx + TASKDATA.state], byte 4
jmp change_task
call show_error_parameters ;; only ONE using, inline ???
;mov edx, [TASK_BASE]
mov [edx + TASKDATA.state], byte 4 ; terminate
jmp change_task ; stack - here it does not matter at all
.debug:
; we are debugged process, notify debugger and suspend ourself
; eax=debugger PID
cld
movzx ecx, bl
push ecx
mov ecx, [TASK_BASE]
push dword [ecx+TASKDATA.pid] ; PID of current process
push 12
pop ecx
push 1 ; 1=exception
call debugger_notify
pop ecx
pop ecx
pop ecx
mov edx, [TASK_BASE]
mov byte [edx+TASKDATA.state], 1 ; suspended
call change_task
restore_ring3_context
iretd
cmp bl,1 ; #DB
je debug_ex
movzx edx, bl ; debug_message data=Number_Of_Exception
mov ecx,1 ; debug_message code=other_exception
jmp debug_ex.notify ; notify debugger and suspend ourself
iglobal
hexletters db '0123456789ABCDEF'
error_interrupt dd -1
endg
IRetToUserHook:
xchg eax, [reg_eip]
sub dword[reg_esp3], 4
mov edi, [reg_esp3]
stosd
restore_ring3_context
unknown_interrupt:
iretd
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
show_error_parameters:
mov eax,[CURRENT_TASK]
shl eax, 5
DEBUGF 1, "K : Process - forced terminate PID: %x\n", [CURRENT_TASK + TASKDATA.pid + eax]
mov eax, [error_interrupt]
cmp al, 0x08
jne @f
DEBUGF 1, "K : Double fault\n"
jmp defined_error
@@:
cmp al, 0x0a
jne @f
DEBUGF 1, "K : Invalid TSS\n"
jmp defined_error
@@:
cmp al, 0x0b
jne @f
DEBUGF 1, "K : Segment not present\n"
jmp defined_error
@@:
cmp al, 0x0c
jne @f
DEBUGF 1, "K : Stack fault\n"
jmp defined_error
@@:
cmp al, 0x0d
jne @f
DEBUGF 1, "K : General protection fault\n"
jmp defined_error
@@:
cmp al, 0x0e
jne @f
DEBUGF 1, "K : Page fault\n"
jmp defined_error
@@:
DEBUGF 1, "K : Undefined Exception\n"
defined_error:
DEBUGF 1, "K : EAX : %x EBX : %x ECX : %x\n", [esp + 0x20], [esp - 12 + 0x20], [esp - 4 + 0x20]
DEBUGF 1, "K : EDX : %x ESI : %x EDI : %x\n", [esp - 8 + 0x20], [esp - 24 + 0x20], [esp - 28 + 0x20]
DEBUGF 1, "K : EBP : %x EIP : %x ", [esp - 20 + 0x20], [esp + 4 + 0x20]
mov eax, [esp + 8 + 0x20]
mov edx,[TASK_BASE] ;not scratched below
DEBUGF 1, "K : Process - forced terminate PID: %x\n", [edx+TASKDATA.pid]
cmp bl, 0x08
jb .l0
cmp bl, 0x0e
jbe .l1
.l0: mov bl, 0x09
.l1: movzx eax,bl
mov eax,[msg_fault_sel+eax*4 - 0x08*4]
DEBUGF 1, "K : %s\n", eax
mov eax, [reg_cs3+4]
mov edi, msg_sel_app
mov ebx, [esp + 16 + 0x20]
mov ebx, [reg_esp3+4]
cmp eax, app_code
je @f
mov edi, msg_sel_ker
mov ebx, [esp - 16 + 0x20]
@@:
DEBUGF 1, "ESP : %x\nK : Flags : %x CS : %x (%s)\n", ebx, [esp + 12 + 0x20], eax, edi
mov ebx, [reg_esp0+4]
@@: DEBUGF 1, "K : EAX : %x EBX : %x ECX : %x\n", [reg_eax+4], [reg_ebx+4], [reg_ecx+4]
DEBUGF 1, "K : EDX : %x ESI : %x EDI : %x\n", [reg_edx+4], [reg_esi+4], [reg_edi+4]
DEBUGF 1, "K : EBP : %x EIP : %x ESP : %x\n", [reg_ebp+4], [reg_eip+4], ebx
DEBUGF 1, "K : Flags : %x CS : %x (%s)\n", [reg_eflags+4], eax, edi
ret
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
restore reg_ss
restore reg_esp3
restore reg_eflags
restore reg_cs
restore reg_eip
restore reg_eax
restore reg_ecx
restore reg_edx
restore reg_ebx
restore reg_esp0
restore reg_ebp
restore reg_esi
restore reg_edi
; irq1 -> hid/keyboard.inc
macro irqh [num]
{
forward
macro irqh [num] {
p_irq#num :
mov edi, num
jmp irqhandler
}
irqh 2,3,4,5,7,8,9,10,11
p_irq6:
@ -277,26 +271,18 @@ ready_for_next_irq_1:
ret
irqD:
save_ring3_context
mov ax, app_data ;os_data
mov ds, ax
mov es, ax
mov dx,0xf0
push eax
mov al,0
out dx,al
mov dx,0xa0
out 0xf0,al
mov al,0x20
out dx,al
mov dx,0x20
out dx,al
restore_ring3_context
out 0xa0,al
out 0x20,al
pop eax
iret
irqh 2,3,4,5,7,8,9,10,11
irqhandler:
mov esi,edi ; 1

View File

@ -351,47 +351,27 @@ endg
; a protected-mode interrupt handler (typically the general-protection
; exception handler, which in turn calls the virtual 8086-mode monitor).
v86_debug_exc:
pushad
xor eax, eax
mov dr6, eax
mov bl, 1
jmp v86_exc_c
v86_page_fault:
add esp, 4
pushad
mov bl, 14
jmp v86_exc_c
v86_except_16:
pushad
mov bl, 16
jmp v86_exc_c
v86_except_19:
pushad
mov bl, 19
iglobal
v86_exc_str1 db 'V86 : unexpected exception ',0
v86_exc_str2 db ' at ',0
v86_exc_str3 db ':',0
v86_exc_str4 db 13,10,'V86 : faulted code:',0
v86_exc_str5 db ' (unavailable)',0
v86_newline db 13,10,0
v86_io_str1 db 'V86 : access to disabled i/o port ',0
v86_io_byte db ' (byte)',13,10,0
v86_io_word db ' (word)',13,10,0
v86_io_dword db ' (dword)',13,10,0
v86_irqerr db 'V86 : IRQ already hooked',13,10,0
v86_exc_str1 db 'V86 : unexpected exception ',0
v86_exc_str2 db ' at ',0
v86_exc_str3 db ':',0
v86_exc_str4 db 13,10,'V86 : faulted code:',0
v86_exc_str5 db ' (unavailable)',0
v86_newline db 13,10,0
v86_io_str1 db 'V86 : access to disabled i/o port ',0
v86_io_byte db ' (byte)',13,10,0
v86_io_word db ' (word)',13,10,0
v86_io_dword db ' (dword)',13,10,0
v86_irqerr db 'V86 : IRQ already hooked',13,10,0
endg
v86_exc_c:
mov ax, app_data
mov ds, ax
mov es, ax
; Did we all that we have wanted to do?
mov eax, [esp+v86_regs.size+10h+18h]
cmp bl,1
jne @f
xor eax, eax
mov dr6, eax
@@: mov eax, [esp+v86_regs.size+10h+18h]
cmp word [esp+v86_regs.eip], ax
jnz @f
shr eax, 16

View File

@ -454,8 +454,8 @@ high_code:
; LOAD IDT
call build_interrupt_table
lidt [idtreg]
call build_interrupt_table ;lidt is executed
;lidt [idtreg]
call init_kernel_heap
stdcall kernel_alloc, RING0_STACK_SIZE+512
@ -1296,6 +1296,7 @@ display_number_force:
xor edx,edx
call division_64_bits
div ebx
hexletters = __fdo_hexdigits
add edx,hexletters
mov dl,[edx]
mov [edi],dl

View File

@ -70,9 +70,9 @@
;
; A400 -> B0FF free
; B100 -> B2FF IDT
; B100 -> B307 IDT for int_0x00..int_0x40
; B300 -> BFFF free
; B308 -> BFFF free
; C000 -> C3FF window stack C000 no of windows - all in words
; C402 -> C7FF window position in stack