Pressing 2 Keys on Keyboard results in Page Fault

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.
Post Reply
programatic
Posts: 2
Joined: Thu Oct 20, 2022 2:46 pm

Pressing 2 Keys on Keyboard results in Page Fault

Post by programatic »

Hello,

I am new to learning os development, and am working on a little hobby OS. Over the last couple days, I have finally been able to create a higher half kernel using limine as the bootloader. I am currently working on trying to get keyboard input and put it on the screen, however, I keep getting a weird page fault issue. I've spent all day on it, so I am hoping somebody else can see the issue I am unable to. I have a working GDT, IDT, and PIC, and the keystrokes will put a simple '.' on the screen. However, if I press 2 keys at the same time, the kernel halts in the page fault exception handler. I found a way around it by just reenabling interrupts in the handler, but something seems very bizzarre to me that it is even throwing the page fault exception.

This is what qemu debug displays:

Code: Select all

check_exception old: 0xffffffff new 0xe
    13: v=0e e=0010 i=0 cpl=0 IP=0028:0000000000000028 pc=0000000000000028 SP=0030:ffff800007b20de8 CR2=0000000000000028
RAX=0000000000000020 RBX=0000000000000000 RCX=0000000000000009 RDX=0000000000000020
RSI=0000000000000020 RDI=0000000000000020 RBP=ffff800007b20ff0 RSP=ffff800007b20de8
R8 =0000000000000028 R9 =0000000000000000 R10=0000000000000000 R11=0000000000000000
R12=0000000000000000 R13=0000000000000000 R14=0000000000000000 R15=0000000000000000
RIP=0000000000000028 RFL=00000216 [----AP-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0030 0000000000000000 00000fff 00a09300 DPL=0 DS   [-WA]
CS =0028 0000000000000000 20000fff 00a29a00 DPL=0 CS64 [-R-]
SS =0030 0000000000000000 00000fff 00a09300 DPL=0 DS   [-WA]
DS =0030 0000000000000000 00000fff 00a09300 DPL=0 DS   [-WA]
FS =0030 0000000000000000 00000fff 00a09300 DPL=0 DS   [-WA]
GS =0030 0000000000000000 00000fff 00a09300 DPL=0 DS   [-WA]
LDT=0000 0000000000000000 00000000 00008200 DPL=0 LDT
TR =0048 ffffffff80012560 00000068 00208900 DPL=0 TSS64-avl
GDT=     ffffffff800024a0 00000057
IDT=     ffffffff800125e0 00000fff
CR0=80010011 CR2=0000000000000028 CR3=0000000007b10000 CR4=00000020
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=0000000000000018 CCD=0000000007b28000 CCO=ADDL
EFER=0000000000000d00
Any help is appreciated! I published the code to github.

(It is a mess right now while I am trying to debug this issue)
https://github.com/Programatic/PokerOS
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: Pressing 2 Keys on Keyboard results in Page Fault

Post by Octocontrabass »

You can't put ordinary function pointers in your IDT. You need a stub written in assembly that adapts the interrupt context to meet the needs of the System V ABI.

There's a function attribute you could use instead of an assembly stub, but it's not suitable for a complete OS since it doesn't give you access to the interrupt context.
programatic
Posts: 2
Joined: Thu Oct 20, 2022 2:46 pm

Re: Pressing 2 Keys on Keyboard results in Page Fault

Post by programatic »

@Octocontrabass This was the issue. Thank you so much! So is the best way to make the interrupt handlers to create an assembly file and have it redirect to my C functions?
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: Pressing 2 Keys on Keyboard results in Page Fault

Post by Octocontrabass »

Yes.

Exactly how you do it is up to you, but it's pretty common to share most of the assembly code between interrupt vectors, including calling a single shared C function. That shared function then uses an array of function pointers to call the appropriate handler.
Post Reply