mkeykernel icon indicating copy to clipboard operation
mkeykernel copied to clipboard

Various bug fixes as well as a change to provide our own GDT.

Open mkilgore opened this issue 9 years ago • 5 comments

I would take a look at the commit list for better information on what all this pull request entails, each commit has a description describing it (Most of them are fairly small/simple fixes).

The bigger changes are the addition of a Makefile and the GDT. The Makefile just executes the same instructions as in the README.md - Though, I set CFLAGS to provide -ffreestanding, since this is kernel code.

For the GDT, it's extremely simple and just does a flat 4GB for the kernel code and data. The reason it's necessary to provide one is because GRUB doesn't guarentee that the GDT is actually usable when our kernel starts. Since interrupts cause the segment registers to be reloaded, the code would fault on an interrupt if we don't have a valid GDT. It's simple enough to provide one. It's worth noting, I modeled the GDT code off of my own kernel's GDT code, to make it easier. You can see it here, the file ./arch/x86/include/arch/gdt.h. I mostly just pulled the GDT_ENTRY macro, which makes creating GDT entries much easier.

The other small changes are removing the stdio.h include (I suspect that was just left in accidentally), removing a redundent cli instruction from start, fixing the end of start so that we guarentee execution will never continue beyond it, saving and restoring the registers in the keyboard interrupt handler, and make-sure the IDT and now GDT structures are packed by the compiler to guarentee we don't have any padding inside them.

I'd be happy to separate out some of the commits if you'd prefer.

mkilgore avatar May 08 '15 19:05 mkilgore

Thanks for your contribution. The article and code is reduced to the bare minimum intentionally so that it's accessible for beginners, and is brief. I have resisted the temptation to add a Makefile to let the users do each of the steps themselves.

Oh stdio.h .. oh I can't believe how I botched it up and how that crept in. As for the GDT, the article is predicated on the assumption that GRUB provides a GDT that could be put to use. If that isn't true, I will need to rewrite the article before I provide one independently.

arjun024 avatar May 09 '15 09:05 arjun024

That's fair enough, a little later today I'll remove the Makefile, and change the names back. You will want to add -ffreestanding to the gcc line.

See the multiboot spec here. Basically, while GRUB provides segments in a specefic configuration, it may also clear the GDTR register. When that's cleared, you can't reload any of the segment registers or else it will try to read the invalid GDTR - The solution is of course to propvide your own GDT as a replacement. You can also see the note in EFLAGS about IF always being cleared, so interrupts are always off.

I think I may have a bug though - load_gdt should reload the segment registers as well I think. I'll look into that.

mkilgore avatar May 09 '15 15:05 mkilgore

Yes, it's anyway better to have a GDT to ourselves. I will try and incorporate the changes once I get the time to add the GDT part to the article.

arjun024 avatar May 09 '15 19:05 arjun024

I have gone ahead and removed the stdio header. It's very misleading to let it stay there. thanks

arjun024 avatar May 10 '15 18:05 arjun024

FWIW, I think this is a really good PR @mkilgore as it adds several realistic features even the most barebones interrupt example requires.

FrankRay78 avatar Jun 22 '24 15:06 FrankRay78