I am extending my kernel by remapping IRQs to IDT slots 0x20 and the slave PIC to 0x28. The problem is that when the IDT is set and everything is done, when a key is pressed, Trap #6 in my IDT is called. can anybody tell me what I am doing wrong? Below is the code for the kernel:
VIDEOSEGMENT EQU 0x000B8000
VIDEOBYTECOUNT EQU (80*25) << 1
KERNELENTRY EQU 0x00100000
KERNEL_SIZE_IN_KB EQU 0x04
IDT_ENTRY_COUNT EQU 0x30
; --------------------------------------------------
%IDEFINE OFFSET
%IDEFINE PTR
; --------------------------------------------------
%MACRO SETIDTENTRY 2 ; (int IDTNumber , void IDTProcedure)
MOV EBX , OFFSET IDT + (%1 * 0x08)
MOV EAX , %2
MOV WORD PTR , AX
MOV WORD PTR , 0x0008
MOV WORD PTR , 0x8E00
SHR EAX , 0x10
MOV WORD PTR , AX
%ENDMACRO
; --------------------------------------------------
PIC1 EQU 0x20
PIC2 EQU 0xA0
PIC1_COMMAND EQU PIC1
PIC1_DATA EQU (PIC1+1)
PIC2_COMMAND EQU PIC2
PIC2_DATA EQU (PIC2+1)
PIC_EOI EQU 0x20
ICW1_ICW4 EQU 0x01
ICW1_INIT EQU 0x10
ICW4_8086 EQU 0x01
; --------------------------------------------------
START:
CLI
CALL __ClearScreen
; Fill all the IDT slots with zero
MOV ECX , IDT_ENTRY_COUNT
MOV EBX , OFFSET IDT
XOR EAX , EAX
@@__FillIDTWithZero:
MOV DWORD PTR , EAX
MOV DWORD PTR , EAX
ADD EBX , 0x08
DEC ECX
JNZ @@__FillIDTWithZero
; Initialize the 8259A PIC
IN AL , PIC1_DATA
MOV DL , AL
IN AL , PIC2_DATA
MOV DH , AL
MOV AL , ICW1_INIT | ICW1_ICW4
OUT PIC1_COMMAND , AL
OUT PIC2_COMMAND , AL
MOV AL , 0x20
OUT PIC1_DATA , AL
MOV AL , 0x28
OUT PIC2_DATA , AL
MOV AL , 0x04
OUT PIC1_DATA , AL
MOV AL , 0x02
OUT PIC2_DATA , AL
MOV AL , ICW4_8086
OUT PIC1_DATA , AL
OUT PIC2_DATA , AL
MOV AL , DL
OUT PIC1_DATA , AL
MOV AL , DH
OUT PIC2_DATA , AL
; Set IDT entries for the first 32 slots
SETIDTENTRY 0x00 , __Trap0Handler
SETIDTENTRY 0x01 , __Trap1Handler
SETIDTENTRY 0x02 , __Trap2Handler
SETIDTENTRY 0x03 , __Trap3Handler
SETIDTENTRY 0x04 , __Trap4Handler
SETIDTENTRY 0x05 , __Trap5Handler
SETIDTENTRY 0x06 , __Trap6Handler
SETIDTENTRY 0x07 , __Trap7Handler
SETIDTENTRY 0x08 , __Trap8Handler
SETIDTENTRY 0x09 , __Trap9Handler
SETIDTENTRY 0x0A , __Trap10Handler
SETIDTENTRY 0x0B , __Trap11Handler
SETIDTENTRY 0x0C , __Trap12Handler
SETIDTENTRY 0x0D , __Trap13Handler
SETIDTENTRY 0x0E , __Trap14Handler
SETIDTENTRY 0x0F , __Trap15Handler
SETIDTENTRY 0x10 , __Trap16Handler
SETIDTENTRY 0x11 , __Trap17Handler
SETIDTENTRY 0x12 , __Trap18Handler
SETIDTENTRY 0x13 , __Trap19To31Handler
SETIDTENTRY 0x14 , __Trap19To31Handler
SETIDTENTRY 0x15 , __Trap19To31Handler
SETIDTENTRY 0x16 , __Trap19To31Handler
SETIDTENTRY 0x17 , __Trap19To31Handler
SETIDTENTRY 0x18 , __Trap19To31Handler
SETIDTENTRY 0x19 , __Trap19To31Handler
SETIDTENTRY 0x1A , __Trap19To31Handler
SETIDTENTRY 0x1B , __Trap19To31Handler
SETIDTENTRY 0x1C , __Trap19To31Handler
SETIDTENTRY 0x1D , __Trap19To31Handler
SETIDTENTRY 0x1E , __Trap19To31Handler
SETIDTENTRY 0x1F , __Trap19To31Handler
; Fill IRQ gates in the IDT for the first 8 IRQs
MOV EBX , OFFSET IDT + (0x20 * 0x08)
MOV ECX , 0x08
MOV EAX , OFFSET __MasterPICHandler
@@__FillIRQSlots1to8:
MOV WORD PTR , AX
MOV WORD PTR , 0x0008
MOV WORD PTR , 0x8F00
ROR EAX , 0x10
MOV WORD PTR , AX
ADD EBX , 0x08
DEC ECX
JNZ @@__FillIRQSlots1to8
; Fill IRQ gates in the IDT for the last 8 IRQs
MOV ECX , 0x08
MOV EAX , OFFSET __SlavePICHandler
@@__FillIRQSlots9to16:
MOV WORD PTR , AX
MOV WORD PTR , 0x0008
MOV WORD PTR , 0x8F00
ROR EAX , 0x10
MOV WORD PTR , AX
ADD EBX , 0x08
DEC ECX
JNZ @@__FillIRQSlots9to16
LIDT
STI
HLT
%INCLUDE "Procs.asm"
; --------------------------------------------------
__MasterPICHandler:
MOV AL , PIC_EOI
OUT PIC1_COMMAND , AL
IRET
__SlavePICHandler:
MOV AL , PIC_EOI
OUT PIC2_COMMAND , AL
OUT PIC1_COMMAND , AL
IRET
; --------------------------------------------------
Trap0Msg DB 'Division by zero', 0x00
Trap1Msg DB 'Debug Exception', 0x00
Trap2Msg DB 'Non-maskable Interrupt Exception', 0x00
Trap3Msg DB 'Breakpoint Exception', 0x00
Trap4Msg DB 'Into Detected Overflow Exception', 0x00
Trap5Msg DB 'Out of Bounds Exception', 0x00
Trap6Msg DB 'Invalid Opcode Exception', 0x00
Trap7Msg DB 'No Coprocessor Exception', 0x00
Trap8Msg DB 'Double Fault Exception', 0x00
Trap9Msg DB 'Coprocessor Segment Overrun Exception', 0x00
Trap10Msg DB 'Bad TSS Exception', 0x00
Trap11Msg DB 'Segment Not Present Exception', 0x00
Trap12Msg DB 'Stack Fault Exception', 0x00
Trap13Msg DB 'General Protection Fault', 0x00
Trap14Msg DB 'Page Fault Exception', 0x00
Trap15Msg DB 'Unknown Interrupt Exception', 0x00
Trap16Msg DB 'Coprocessor Fault Exception', 0x00
Trap17Msg DB 'Alignment Check Exception', 0x00
Trap18Msg DB 'Machine Check Exception', 0x00
Trap19Msg DB 'Reserved Exception', 0x00
; --------------------------------------------------
__Trap0Handler:
MOV EAX , Trap0Msg
CALL __WriteStr
JMP $
; --------------------
__Trap1Handler:
MOV EAX , Trap1Msg
CALL __WriteStr
JMP $
; --------------------
__Trap2Handler:
MOV EAX , Trap2Msg
CALL __WriteStr
JMP $
; --------------------
__Trap3Handler:
MOV EAX , Trap3Msg
CALL __WriteStr
JMP $
; --------------------
__Trap4Handler:
MOV EAX , Trap4Msg
CALL __WriteStr
JMP $
; --------------------
__Trap5Handler:
MOV EAX , Trap5Msg
CALL __WriteStr
JMP $
; --------------------
__Trap6Handler:
MOV EAX , Trap6Msg
CALL __WriteStr
JMP $
; --------------------
__Trap7Handler:
MOV EAX , Trap7Msg
CALL __WriteStr
JMP $
; --------------------
__Trap8Handler:
MOV EAX , Trap8Msg
CALL __WriteStr
JMP $
; --------------------
__Trap9Handler:
MOV EAX , Trap9Msg
CALL __WriteStr
JMP $
; --------------------
__Trap10Handler:
MOV EAX , Trap10Msg
CALL __WriteStr
JMP $
; --------------------
__Trap11Handler:
MOV EAX , Trap11Msg
CALL __WriteStr
JMP $
; --------------------
__Trap12Handler:
MOV EAX , Trap12Msg
CALL __WriteStr
JMP $
; --------------------
__Trap13Handler:
MOV EAX , Trap13Msg
CALL __WriteStr
JMP $
; --------------------
__Trap14Handler:
MOV EAX , Trap14Msg
CALL __WriteStr
JMP $
; --------------------
__Trap15Handler:
MOV EAX , Trap15Msg
CALL __WriteStr
JMP $
; --------------------
__Trap16Handler:
MOV EAX , Trap16Msg
CALL __WriteStr
JMP $
; --------------------
__Trap17Handler:
MOV EAX , Trap17Msg
CALL __WriteStr
JMP $
; --------------------
__Trap18Handler:
MOV EAX , Trap18Msg
CALL __WriteStr
JMP $
; --------------------
__Trap19To31Handler:
MOV EAX , Trap19Msg
CALL __WriteStr
JMP $
; --------------------------------------------------
ALIGN 8, NOP
IDT:
RESD (IDT_ENTRY_COUNT << 1)
IDT_END:
;------------------------------
IDTR:
DW (IDT_END - IDT) - 1
DD IDT
String1 DB 'Kernel', 0x00
String2 DB 'A key is pressed', 0
VideoCursor DD 0x00000000
TIMES (KERNEL_SIZE_IN_KB << 10)-($-$$) DB 0x00
Shooting from the hip, your INT 0x21 (IRQ 1/Keyboard) entry may be pointing to a section of data (hence the invalid opcode) or is perhaps corrupt.
I'll look into more later, I'm busy at the moment.
I'll look into more later, I'm busy at the moment.
Okay I fixed the problem. There were a lot of stuff wrong with the code. Thanks spook. Appreciations.
Okay I fixed the problem. There were a lot of stuff wrong with the code. Thanks spook. Appreciations.
Yeah, sorry I didn't get time to look at it further. I kind of expected you to catch it though, as you are more familiar with your own code :)
Actually it was your comment that made me realize that some data was being executed instead of code. For now I really am just testing stuff so I'm going to be needing your help every now and then.
Actually it was your comment that made me realize that some data was being executed instead of code. For now I really am just testing stuff so I'm going to be needing your help every now and then.
Yeah, I use things like Bochs to keep me in check. There are times that I think there is a "bug in Bochs" but eventually I realize it is something I did when I should have been sleeping :)