[SOLVED] Fault Occures on Key Press

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
FunnyGuy9796
Member
Member
Posts: 61
Joined: Tue Sep 13, 2022 9:29 pm
Libera.chat IRC: FunnyGuy9796

[SOLVED] Fault Occures on Key Press

Post by FunnyGuy9796 »

I have enabled interrupts and setup the keyboard correctly (I think). However, whenever a key is pressed a fault is triggered.

Code: Select all

#ifndef IDT_H_INCLUDED
#define IDT_H_INCLUDED

#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "../byte.h"
#include "../itoa.h"
#include "../terminal.h"

#define IDT_MAX_DESCRIPTORS 256

#define KEYBOARD_DATA_PORT 0x60
#define PIC1_COMMAND_PORT 0x20
#define PIC1_DATA_PORT 0x21
#define PIC_EOI 0x20

typedef struct {
	uint16_t    isr_low;
	uint16_t    kernel_cs;
	uint8_t	    ist;
	uint8_t     attributes;
	uint16_t    isr_mid;
	uint32_t    isr_high;
	uint32_t    reserved;
} __attribute__((packed)) idt_entry_t;

__attribute__((aligned(0x10))) 
static idt_entry_t idt[IDT_MAX_DESCRIPTORS];

typedef struct {
	uint16_t	limit;
	uint64_t	base;
} __attribute__((packed)) idtr_t;

static idtr_t idtr;

static bool vectors[IDT_MAX_DESCRIPTORS];

__attribute__((noreturn))
void exception_handler(void) {
    terminal_write("An exception occured\n");
    __asm__ volatile ("cli; hlt");
}

void idt_set_descriptor(uint8_t vector, void* isr, uint8_t flags) {
    idt_entry_t* descriptor = &idt[vector];
    descriptor->isr_low     = (uint64_t)isr & 0xFFFF;
    descriptor->kernel_cs   = 0x100000;
    descriptor->ist         = 0;
    descriptor->attributes  = flags;
    descriptor->isr_mid     = ((uint64_t)isr >> 16) & 0xFFFF;
    descriptor->isr_high    = ((uint64_t)isr >> 32) & 0xFFFFFFFF;
    descriptor->reserved    = 0;
}

extern void* isr_stub_table[];

void keyboard_interrupt_handler() {
    uint8_t scancode = inb(KEYBOARD_DATA_PORT);
    outb(PIC1_COMMAND_PORT, PIC_EOI);
    char str[100];
    terminal_write("Key pressed: ");
    terminal_write(itoa(scancode, str, 10));
}

void keyboard_init() {
    outb(PIC1_DATA_PORT, inb(PIC1_DATA_PORT) & ~(1 << 1));
    uint16_t mask = inb(PIC1_DATA_PORT) & ~(1 << 1);
    outb(PIC1_DATA_PORT, mask);
    idt_set_descriptor(33, keyboard_interrupt_handler, 0x8E);
    terminal_write("Keyboard: Initialized\n");
}

void idt_init(void) {
    idtr.base = (uintptr_t)&idt[0];
    idtr.limit = (uint16_t)sizeof(idt_entry_t) * IDT_MAX_DESCRIPTORS - 1;
    for (uint8_t vector = 0; vector < 32; vector++) {
        idt_set_descriptor(vector, isr_stub_table[vector], 0x8E);
        vectors[vector] = true;
    }
    __asm__ volatile ("lidt %0" : : "m"(idtr));
    __asm__ volatile ("sti");
    terminal_write("IDT: Initialized\n");
}

#endif
Help would be much appreciated.
Last edited by FunnyGuy9796 on Sun Mar 12, 2023 10:45 am, edited 1 time in total.
MichaelPetch
Member
Member
Posts: 797
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: Fault Occures on Key Press

Post by MichaelPetch »

Code: Select all

descriptor->kernel_cs   = 0x100000;
kernel_cs is a code segment selector fields that should reference a code segment descriptor in the current GDT/LDT. It is not supposed to be an address.
MichaelPetch
Member
Member
Posts: 797
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: Fault Occures on Key Press

Post by MichaelPetch »

Another concern will be this:

Code: Select all

void keyboard_interrupt_handler() {
    uint8_t scancode = inb(KEYBOARD_DATA_PORT);
    outb(PIC1_COMMAND_PORT, PIC_EOI);
    char str[100];
    terminal_write("Key pressed: ");
    terminal_write(itoa(scancode, str, 10));
}

void keyboard_init() {
    outb(PIC1_DATA_PORT, inb(PIC1_DATA_PORT) & ~(1 << 1));
    uint16_t mask = inb(PIC1_DATA_PORT) & ~(1 << 1);
    outb(PIC1_DATA_PORT, mask);
    idt_set_descriptor(33, keyboard_interrupt_handler, 0x8E);
    terminal_write("Keyboard: Initialized\n");
}
In particular you have used `dt_set_descriptor(33, keyboard_interrupt_handler, 0x8E);` to set an IDT descriptor to point directly at `keyboard_interrupt_handler`. `keyboard_interrupt_handler` happens to be a regular C function that does a RET to return. RET from an interrupt handler is going to cause problems. It needs to be an IRET. If using GCC 7.x+ you could add `__attribute__((interrupt))` to the 'keyboard_interrupt_handler'. That would cause it to return with IRET instead of RET. More on using that attribute can be found here: https://gcc.gnu.org/onlinedocs/gcc/x86- ... butes.html . I believe though it is better to write an assembly ISR/IRQ stub that sets things up the way you want and then calls over to the 'keyboard_interrupt_handler' as a normal C function. The ISR/IRQ stub would end with an IRET.
Last edited by MichaelPetch on Sat Mar 11, 2023 2:11 pm, edited 1 time in total.
FunnyGuy9796
Member
Member
Posts: 61
Joined: Tue Sep 13, 2022 9:29 pm
Libera.chat IRC: FunnyGuy9796

Re: Fault Occures on Key Press

Post by FunnyGuy9796 »

Thank you for the replies! I had read that it’s better to set up the IRQ in assembly but didn’t think it was necessary. I am a little confused on how to find the kernel offset. I am using the Limine bootloader and can’t seem to figure out where it sets the kernel offset and it’s documentation only mentions the address it is at.
MichaelPetch
Member
Member
Posts: 797
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: Fault Occures on Key Press

Post by MichaelPetch »

It isn't a kernel offset. It is a selector. You must have a GDT. I don't know anything about Limine but according to this bit of documentation https://github.com/limine-bootloader/li ... ROTOCOL.md if you didn't set up your own GDT the one provided by Limine is guaranteed to have these entries:

The GDT register is loaded to point to a GDT, in bootloader-reclaimable memory, with at least the following entries, starting at offset 0:

- Null descriptor
- 16-bit code descriptor. Base = 0, limit = 0xffff. Readable.
- 16-bit data descriptor. Base = 0, limit = 0xffff. Writable.
- 32-bit code descriptor. Base = 0, limit = 0xffffffff. Readable.
- 32-bit data descriptor. Base = 0, limit = 0xffffffff. Writable.
- 64-bit code descriptor. Base and limit irrelevant. Readable.
- 64-bit data descriptor. Base and limit irrelevant. Writable.

Since each GDT entry is 8 bytes the Null selector is 0x0, 16-bit code selector is 0x08, 16-bit data selector is 0x10, 32-bit code selector is 0x18, 32-bit data selector is 0x20, 64-bit code selector is 0x28, and 64-bit data selector is 0x30.

This would mean that you likely have a 64-bit Code selector in your Limine supplied GDT at offset 0x28 . The CS you probably want assign to `descriptor->kernel_cs` is 0x28 since you appear to be operating in 64-bit mode.
Last edited by MichaelPetch on Sat Mar 11, 2023 2:35 pm, edited 2 times in total.
FunnyGuy9796
Member
Member
Posts: 61
Joined: Tue Sep 13, 2022 9:29 pm
Libera.chat IRC: FunnyGuy9796

Re: Fault Occures on Key Press

Post by FunnyGuy9796 »

Alright, thank you so much! Sorry for the confusion. I’m still learning and trying to teach myself and I’m not too experienced yet.
FunnyGuy9796
Member
Member
Posts: 61
Joined: Tue Sep 13, 2022 9:29 pm
Libera.chat IRC: FunnyGuy9796

Re: Fault Occures on Key Press

Post by FunnyGuy9796 »

Ok, I have attempted to implement an ISR handler in assembly

Code: Select all

global keyboard_isr_handler
extern keyboard_interrupt_handler

keyboard_isr_handler:
    pushfq
    push rax
    push rcx
    push rdx
    push rsi
    push rdi
    in al, 0x60
    movzx edi, al
    call keyboard_interrupt_handler
    mov al, 0x20
    out 0x20, al
    pop rdi
    pop rsi
    pop rdx
    pop rcx
    pop rax
    popfq
    iretq
I have also modified the existing code to (hopefully) accommodate the ISR

Code: Select all

#ifndef IDT_H_INCLUDED
#define IDT_H_INCLUDED

#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "../limine.h"
#include "../byte.h"
#include "../itoa.h"
#include "../terminal.h"

#define IDT_MAX_DESCRIPTORS 256
#define KEYBOARD_DATA_PORT 0x60
#define PIC1_COMMAND_PORT 0x20
#define PIC1_DATA_PORT 0x21
#define PIC_EOI 0x20

typedef struct {
	uint16_t    isr_low;
	uint16_t    kernel_cs;
	uint8_t	    ist;
	uint8_t     attributes;
	uint16_t    isr_mid;
	uint32_t    isr_high;
	uint32_t    reserved;
} __attribute__((packed)) idt_entry_t;

__attribute__((aligned(0x10))) 
static idt_entry_t idt[IDT_MAX_DESCRIPTORS];

typedef struct {
	uint16_t	limit;
	uint64_t	base;
} __attribute__((packed)) idtr_t;

static idtr_t idtr;

static bool vectors[IDT_MAX_DESCRIPTORS];

__attribute__((noreturn))
void exception_handler(void) {
    print("An exception occured\n");
    __asm__ volatile ("cli; hlt");
}

void idt_set_descriptor(uint8_t vector, void* isr, uint8_t flags) {
    idt_entry_t* descriptor = &idt[vector];
    descriptor->isr_low     = (uint64_t)isr & 0xFFFF;
    descriptor->kernel_cs   = 0x28;
    descriptor->ist         = 0;
    descriptor->attributes  = flags;
    descriptor->isr_mid     = ((uint64_t)isr >> 16) & 0xFFFF;
    descriptor->isr_high    = ((uint64_t)isr >> 32) & 0xFFFFFFFF;
    descriptor->reserved    = 0;
}

extern void* isr_stub_table[];

void idt_init(void) {
    idtr.base = (uintptr_t)&idt[0];
    idtr.limit = (uint16_t)sizeof(idt_entry_t) * IDT_MAX_DESCRIPTORS - 1;
    for (uint8_t vector = 0; vector < 32; vector++) {
        idt_set_descriptor(vector, isr_stub_table[vector], 0x8E);
        vectors[vector] = true;
    }
    __asm__ volatile ("lidt %0" : : "m"(idtr));
    __asm__ volatile ("sti");
    print("IDT: Initialized\n");
}

extern void keyboard_isr_handler();

struct interrupt_frame;

void keyboard_interrupt_handler(unsigned long scancode) {
    char str[100];
    print("Key pressed: ");
    print(itoa(scancode, str, 10));
    
}

void keyboard_init() {
    outb(PIC1_DATA_PORT, inb(PIC1_DATA_PORT) & ~(1 << 1));
    uint16_t mask = inb(PIC1_DATA_PORT) & ~(1 << 1);
    outb(PIC1_DATA_PORT, mask);
    idt_set_descriptor(33, keyboard_isr_handler, 0x8E);
    print("Keyboard: Initialized\n");
}

#endif
MichaelPetch
Member
Member
Posts: 797
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: Fault Occures on Key Press

Post by MichaelPetch »

Some notes. The following are the registers and whether they are preserved across function calls:
Image

The general purposes registers that have "no" you will need to save and then restore yourself. So that would be RAX, RCX, RDX, RSI, RDI, R8, R9, R10, and R11. The floating point and XMM registers shouldn't be used in your interrupt handler code. Interrupt handlers should be built without the FPU and SIMD instructions (Which use XMM, ST?, MM?, ZMM?, YMM? registers etc). Your kernel should be built without FPU and SIMD support. With GGC 7.x+ there is a `-mgeneral-regs-only` option that prevents code being generated with those kinds of instructions. By doing this you don't need to save and restore them.

There is no reason you can't do the port input in C and the sending of the EOI in C as well.

No reason to use PUSHFQ/POPFQ either since the flags are automatically saved when your interrupt handler is called by the processor and restored when you do the IRETQ.

You should also do a CLD instruction before calling your C code from the assembly interrupt stub to ensure the direction flag for string instructions is set forward (required by the 64-bit System V ABI)
FunnyGuy9796
Member
Member
Posts: 61
Joined: Tue Sep 13, 2022 9:29 pm
Libera.chat IRC: FunnyGuy9796

Re: Fault Occures on Key Press

Post by FunnyGuy9796 »

I modified the assembly code to handle those registers. However, I am still getting an exception error. It appears that the error is a divide by zero error. I am aware that is caused by incorrect access of ports or access of invalid ports. However, I am struggling to see where I would have done that.
MichaelPetch
Member
Member
Posts: 797
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: Fault Occures on Key Press

Post by MichaelPetch »

Without seeing all your code it is hard to tell. If you are using QEMU to test, I think it is time to start trying and debugging. If using QEMU one thing you can do is have QEMU output all the interrupts and exceptions and their state. Adding `-d int -no-shutdown -no-reboot` to your QEMU command line would do that. You'd be able to see the interrupts, exceptions, and their state leading up to the failure. You could also attach the log to a post here on this site so we can look at it. The log may be long depending on the types of interrupts that are firing.
FunnyGuy9796
Member
Member
Posts: 61
Joined: Tue Sep 13, 2022 9:29 pm
Libera.chat IRC: FunnyGuy9796

Re: Fault Occures on Key Press

Post by FunnyGuy9796 »

Code: Select all

SMM: enter
EAX=00000001 EBX=00000000 ECX=02000000 EDX=02000628
ESI=0000000b EDI=02000000 EBP=000a0000 ESP=00006c58
EIP=000ef1cd EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00cf9b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f7460 00000037
IDT=     000f749e 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000 
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=00000080 CCD=00000001 CCO=LOGICB
EFER=0000000000000000
SMM: after RSM
EAX=00000001 EBX=00000000 ECX=02000000 EDX=02000628
ESI=0000000b EDI=02000000 EBP=000a0000 ESP=00006c58
EIP=000ef1cd EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f7460 00000037
IDT=     000f749e 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000 
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=00000000 CCD=00000000 CCO=EFLAGS
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=000f8743 ECX=00001234 EDX=00006cff
ESI=00006cc0 EDI=befff5fd EBP=00006c80 ESP=00006c80
EIP=00008742 EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =dd80 000dd800 ffffffff 008f9300
CS =f000 000f0000 ffffffff 008f9b00
SS =0000 00000000 ffffffff 008f9300
DS =0000 00000000 ffffffff 008f9300
FS =0000 00000000 ffffffff 008f9300
GS =0000 00000000 ffffffff 008f9300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000 
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=00000000 CCD=00006c80 CCO=EFLAGS
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=000f8743 ECX=00001234 EDX=00006cff
ESI=00006cc0 EDI=befff5fd EBP=00006c80 ESP=00006c80
EIP=000f8743 EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f7460 00000037
IDT=     000f749e 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000 
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=00000000 CCD=00000000 CCO=EFLAGS
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=0000875d ECX=00005678 EDX=befeb0a0
ESI=000ecae0 EDI=befff5fd EBP=00006c80 ESP=00006c80
EIP=000f875c EFL=00000006 [-----P-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f7460 00000037
IDT=     000f749e 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000 
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=00000004 CCD=00006c6c CCO=EFLAGS
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=0000875d ECX=00005678 EDX=befeb0a0
ESI=000ecae0 EDI=befff5fd EBP=00006c80 ESP=00006c80
EIP=0000875d EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =dd80 000dd800 ffffffff 00809300
CS =f000 000f0000 ffffffff 00809b00
SS =0000 00000000 ffffffff 00809300
DS =0000 00000000 ffffffff 00809300
FS =0000 00000000 ffffffff 00809300
GS =0000 00000000 ffffffff 00809300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000 
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=00000000 CCD=00000001 CCO=EFLAGS
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=000f8743 ECX=00001234 EDX=000069ff
ESI=000069aa EDI=befff5fd EBP=0000696a ESP=0000696a
EIP=00008742 EFL=00000006 [-----P-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =dd80 000dd800 ffffffff 008f9300
CS =f000 000f0000 ffffffff 008f9b00
SS =0000 00000000 ffffffff 008f9300
DS =0000 00000000 ffffffff 008f9300
FS =0000 00000000 ffffffff 008f9300
GS =c900 000c9000 ffffffff 008f9300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000 
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=00000004 CCD=0000696a CCO=EFLAGS
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=000f8743 ECX=00001234 EDX=000069ff
ESI=000069aa EDI=befff5fd EBP=0000696a ESP=0000696a
EIP=000f8743 EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f7460 00000037
IDT=     000f749e 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000 
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=00000000 CCD=00000000 CCO=EFLAGS
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=0000875d ECX=00005678 EDX=00000005
ESI=00000000 EDI=befff5fd EBP=0000696a ESP=0000696a
EIP=000f875c EFL=00000046 [---Z-P-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f7460 00000037
IDT=     000f749e 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000 
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=00000044 CCD=00000000 CCO=EFLAGS
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=0000875d ECX=00005678 EDX=00000005
ESI=00000000 EDI=befff5fd EBP=0000696a ESP=0000696a
EIP=0000875d EFL=00000006 [-----P-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =dd80 000dd800 ffffffff 00809300
CS =f000 000f0000 ffffffff 00809b00
SS =0000 00000000 ffffffff 00809300
DS =0000 00000000 ffffffff 00809300
FS =0000 00000000 ffffffff 00809300
GS =c900 000c9000 ffffffff 00809300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000 
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=00000004 CCD=00000001 CCO=EFLAGS
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=000f8743 ECX=00001234 EDX=000069ff
ESI=000069a4 EDI=befff5fd EBP=00006964 ESP=00006964
EIP=00008742 EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =dd80 000dd800 ffffffff 00809300
CS =f000 000f0000 ffffffff 00809b00
SS =0000 00000000 ffffffff 00809300
DS =0000 00000000 ffffffff 00809300
FS =0000 00000000 ffffffff 00809300
GS =c900 000c9000 ffffffff 00809300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000 
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=00000000 CCD=00006964 CCO=EFLAGS
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=000f8743 ECX=00001234 EDX=000069ff
ESI=000069a4 EDI=befff5fd EBP=00006964 ESP=00006964
EIP=000f8743 EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f7460 00000037
IDT=     000f749e 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000 
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=00000000 CCD=00000000 CCO=EFLAGS
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=0000875d ECX=00005678 EDX=00000003
ESI=befd3490 EDI=befff5fd EBP=00006964 ESP=00006964
EIP=000f875c EFL=00000016 [----AP-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f7460 00000037
IDT=     000f749e 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000 
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=00000014 CCD=00006950 CCO=EFLAGS
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=0000875d ECX=00005678 EDX=00000003
ESI=befd3490 EDI=befff5fd EBP=00006964 ESP=00006964
EIP=0000875d EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =dd80 000dd800 ffffffff 00809300
CS =f000 000f0000 ffffffff 00809b00
SS =0000 00000000 ffffffff 00809300
DS =0000 00000000 ffffffff 00809300
FS =0000 00000000 ffffffff 00809300
GS =c900 000c9000 ffffffff 00809300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000 
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=00000000 CCD=00000001 CCO=EFLAGS
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=000f8743 ECX=00001234 EDX=000069ff
ESI=000069aa EDI=befff5fd EBP=0000696a ESP=0000696a
EIP=00008742 EFL=00000006 [-----P-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =dd80 000dd800 ffffffff 00809300
CS =f000 000f0000 ffffffff 00809b00
SS =0000 00000000 ffffffff 00809300
DS =0000 00000000 ffffffff 00809300
FS =0000 00000000 ffffffff 00809300
GS =c900 000c9000 ffffffff 00809300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000 
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=00000004 CCD=0000696a CCO=EFLAGS
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=000f8743 ECX=00001234 EDX=000069ff
ESI=000069aa EDI=befff5fd EBP=0000696a ESP=0000696a
EIP=000f8743 EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f7460 00000037
IDT=     000f749e 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000 
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=00000000 CCD=00000000 CCO=EFLAGS
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=0000875d ECX=00005678 EDX=00000005
ESI=00000000 EDI=befff5fd EBP=0000696a ESP=0000696a
EIP=000f875c EFL=00000046 [---Z-P-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f7460 00000037
IDT=     000f749e 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000 
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=00000044 CCD=00000000 CCO=EFLAGS
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=0000875d ECX=00005678 EDX=00000005
ESI=00000000 EDI=befff5fd EBP=0000696a ESP=0000696a
EIP=0000875d EFL=00000006 [-----P-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =dd80 000dd800 ffffffff 00809300
CS =f000 000f0000 ffffffff 00809b00
SS =0000 00000000 ffffffff 00809300
DS =0000 00000000 ffffffff 00809300
FS =0000 00000000 ffffffff 00809300
GS =c900 000c9000 ffffffff 00809300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000 
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=00000004 CCD=00000001 CCO=EFLAGS
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=000f8743 ECX=00001234 EDX=000069ff
ESI=000069a4 EDI=befff5fd EBP=00006964 ESP=00006964
EIP=00008742 EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =dd80 000dd800 ffffffff 00809300
CS =f000 000f0000 ffffffff 00809b00
SS =0000 00000000 ffffffff 00809300
DS =0000 00000000 ffffffff 00809300
FS =0000 00000000 ffffffff 00809300
GS =c900 000c9000 ffffffff 00809300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000 
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=00000000 CCD=00006964 CCO=EFLAGS
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=000f8743 ECX=00001234 EDX=000069ff
ESI=000069a4 EDI=befff5fd EBP=00006964 ESP=00006964
EIP=000f8743 EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f7460 00000037
IDT=     000f749e 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000 
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=00000000 CCD=00000000 CCO=EFLAGS
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=0000875d ECX=00005678 EDX=00000003
ESI=bef33490 EDI=befff5fd EBP=00006964 ESP=00006964
EIP=000f875c EFL=00000016 [----AP-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f7460 00000037
IDT=     000f749e 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000 
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=00000014 CCD=00006950 CCO=EFLAGS
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=0000875d ECX=00005678 EDX=00000003
ESI=bef33490 EDI=befff5fd EBP=00006964 ESP=00006964
EIP=0000875d EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =dd80 000dd800 ffffffff 00809300
CS =f000 000f0000 ffffffff 00809b00
SS =0000 00000000 ffffffff 00809300
DS =0000 00000000 ffffffff 00809300
FS =0000 00000000 ffffffff 00809300
GS =c900 000c9000 ffffffff 00809300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000 
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=00000000 CCD=00000001 CCO=EFLAGS
EFER=0000000000000000
Servicing hardware INT=0x08
Servicing hardware INT=0x08
Servicing hardware INT=0x08
Servicing hardware INT=0x08
Servicing hardware INT=0x08
Servicing hardware INT=0x09
     0: v=09 e=0000 i=0 cpl=0 IP=0028:ffffffff80000184 pc=ffffffff80000184 SP=0030:ffff8000bfb1ffe8 env->regs[R_EAX]=0000000000000000
RAX=0000000000000000 RBX=0000000000000000 RCX=0000000000000000 RDX=0000000000000010
RSI=00000000800029b5 RDI=00000000bfb27000 RBP=0000000000000000 RSP=ffff8000bfb1ffe8
R8 =ffff8000bfb1fefc R9 =0000000000000002 R10=0000000000000007 R11=00000000fffffff9
R12=0000000000000000 R13=0000000000000000 R14=0000000000000000 R15=0000000000000000
RIP=ffffffff80000184 RFL=00000246 [---Z-P-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0030 0000000000000000 00000000 00009300 DPL=0 DS   [-WA]
CS =0028 0000000000000000 00000000 00209a00 DPL=0 CS64 [-R-]
SS =0030 0000000000000000 00000000 00009300 DPL=0 DS   [-WA]
DS =0030 0000000000000000 00000000 00009300 DPL=0 DS   [-WA]
FS =0030 0000000000000000 00000000 00009300 DPL=0 DS   [-WA]
GS =0030 0000000000000000 00000000 00009300 DPL=0 DS   [-WA]
LDT=0000 0000000000000000 00000000 00008200 DPL=0 LDT
TR =0000 0000000000000000 0000ffff 00008b00 DPL=0 TSS64-busy
GDT=     ffff800000014ae8 00000037
IDT=     ffffffff80003da0 00000fff
CR0=80010011 CR2=0000000000000000 CR3=00000000bfb0f000 CR4=00000020
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000 
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=0000000000000044 CCD=0000000000000000 CCO=LOGICL
EFER=0000000000000d00
There was indeed a fair amount of debug info. I'm unsure what it all means but it includes the interrupts debugging info.
Octocontrabass
Member
Member
Posts: 5562
Joined: Mon Mar 25, 2013 7:01 pm

Re: Fault Occures on Key Press

Post by Octocontrabass »

FunnyGuy9796 wrote:It appears that the error is a divide by zero error. I am aware that is caused by incorrect access of ports or access of invalid ports.
Divide errors are only caused by division. It has nothing to do with port access. Either you're actually performing invalid division, or your exception handler is broken and doesn't tell you the real exception.

You should fix your exception handlers before you worry about hardware interrupts. Debugging is a lot more difficult when your debug information is wrong.
FunnyGuy9796 wrote:

Code: Select all

     0: v=09 e=0000 i=0 cpl=0 IP=0028:ffffffff80000184 pc=ffffffff80000184 SP=0030:ffff8000bfb1ffe8 env->regs[R_EAX]=0000000000000000
You need to initialize the interrupt controllers before you enable interrupts.
FunnyGuy9796
Member
Member
Posts: 61
Joined: Tue Sep 13, 2022 9:29 pm
Libera.chat IRC: FunnyGuy9796

Re: Fault Occures on Key Press

Post by FunnyGuy9796 »

Thank you so much for pointing that out. That kind of makes me feel stupid that I didn’t catch that. Learn from my mistakes I guess. Thanks again!
MichaelPetch
Member
Member
Posts: 797
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: Fault Occures on Key Press

Post by MichaelPetch »

This is telling:

Servicing hardware INT=0x08
Servicing hardware INT=0x08
Servicing hardware INT=0x08
Servicing hardware INT=0x08
Servicing hardware INT=0x08
Servicing hardware INT=0x09
0: v=09 e=0000 i=0 cpl=0 IP=0028:ffffffff80000184 pc=ffffffff80000184 SP=0030:ffff8000bfb1ffe8 env->regs[R_EAX]=0000000000000000

It appears that the timer interrupts are coming in on interrupt 0x08 (IRQ0) and the keyboard interrupt is int 0x09 (IRQ1). This tells me you have likely not remapped the PICs. Because of a oversight by IBM and their engineers when making the IBM PC/XT they mapped the PIC0 onto interrupts 0x08 to 0x0f. Intel had reserved these interrupts for future use. On the IBM PC this wasn't a big deal because there weren't any exceptions above 0x07, but this changed on the IBM PC/AT (80286).

The end result is this, on modern OSes you need to move PIC0 (and PIC1 if you want) somewhere other than their default. Many OSes move PIC0 to interrupt 0x20-0x27 and PIC1 to interrupt 0x28-0x2f. This seems to be the case int the code you are using since the keyboard handler (IRQ1) is setup with `idt_set_descriptor(33, keyboard_isr_handler, 0x8E);` 33 = 0x21 which suggests your code intended to have PIC0 mapped to 0x20 and PIC1 to 0x2f.

How to do this can be found here: https://wiki.osdev.org/8259_PIC#Initialisation . You'd create a PIC_remap function and call it with the values 0x20 and 0x28 like `PIC_remap(0x20, 0x28)`
Last edited by MichaelPetch on Sat Mar 11, 2023 7:09 pm, edited 2 times in total.
FunnyGuy9796
Member
Member
Posts: 61
Joined: Tue Sep 13, 2022 9:29 pm
Libera.chat IRC: FunnyGuy9796

Re: Fault Occures on Key Press

Post by FunnyGuy9796 »

Indeed I did not remap the PIC. I, being new to this, accidentally overlooked the fact that the PIC needs to be initialized before the APIC. Thanks guy again for the help! (I know my questions and lack of understanding can be annoying)
Post Reply