Let’s write a simple Kernel (2014)
Bare metal projects are always a fun hobby. I've done some projects like this in the past [0], let me share some things I learned the hard way.<p>If the author is reading, here are a few corrections.<p>> char * vidptr = (char * )0xb8000;<p>This needs "volatile" qualifier or all the writes to video memory will be dropped when optimization is enabled.<p>Memory mapped I/O like this is one of the few use cases where you need "volatile" in C.<p>> gcc -m32 -c kernel.c -o kc.o<p>You need to build a cross-compiler for bare metal projects. Using the system gcc will not work in the long run. The compiler packaged with your operating system is intended for building binaries for use with your operating system. It may have downstream patches or configured to a target in a way that will cause problems.<p>The worst part is that it may <i>seem</i> to work for a while, until it doesn't. I ran into some hard-to-debug issues with my past projects and learned this the hard way. If I recall correctly, the issues were related to redzones and ABI conventions.<p>You need to build GNU binutils and GCC for the "i686-pc-elf" target. I documented this process for someone else's bare metal project here [1].<p>It really pays off to do this right from the start. Once you have a cross-compiler and a build system that can produce debuggable elf images (you also need to build gdb for the target), things get much easier. Using the built-in debuggers in QEMU or Bochs can only get you so far. Having a proper debugger with symbols and source view will make working much easier.<p>For a build system, plain old Makefiles work best, CMake and other high level build systems are a pain in the ass with bare metal projects that require linker scripts, etc.<p>[0] <a href="https://github.com/rikusalminen/danjeros" rel="nofollow">https://github.com/rikusalminen/danjeros</a>
[1] <a href="https://github.com/Overv/MineAssemble/blob/master/README.md" rel="nofollow">https://github.com/Overv/MineAssemble/blob/master/README.md</a>