Let’s write a simple Kernel (2014)
arjunsreedharan.org
arjunsreedharan.org
If the author is reading, here are a few corrections.
> char * vidptr = (char * )0xb8000;
This needs "volatile" qualifier or all the writes to video memory will be dropped when optimization is enabled.
Memory mapped I/O like this is one of the few use cases where you need "volatile" in C.
> gcc -m32 -c kernel.c -o kc.o
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.
The worst part is that it may seem 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.
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].
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.
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.
[0] https://github.com/rikusalminen/danjeros [1] https://github.com/Overv/MineAssemble/blob/master/README.md
coreboot has similar issues, and so we maintain our cross compiler building script for 10 years now[0] (although historically using i386-elf instead of i686-*-elf). I sometimes wonder if there's value in packaging our compilers (all 8 architectures) for other users.
[0] https://review.coreboot.org/cgit/coreboot.git/tree/util/cros...
Might you have any useful links or documentation on this that you could share? Thanks.
https://hub.docker.com/r/brett/gcc-cross-x86_64-elf/
It hosts the gcc 7.1 x86_64-elf compiler with redzone disabled.
If you don't want to use the docker container, you can view the dockerfile to see the steps required to build the cross-compiler yourself:
https://github.com/beevik/docker/tree/master/gcc-cross-x86_6...
https://github.com/lordmilko/i686-elf-tools
There's also a bit of information on OSDEV if you are wondering why you need a cross compiler:
I remember a few years back when I was trying to play with MINIX. Tanenbaum got several million EUR and hired some grad students to work on the thing. They promptly replaced much of the system with NetBSD. ("Perhaps too much", you can hear Tanenbaum say in one of his talks.) As a result of this the system compiler ACK was switched out for LLVM/clang. I complained on the mailing list about this because a full system build from source is something that used to be doable in <10 minutes—something Tanenbaum used to boast about—and it was now taking 3 hours if you decided to blow away your source/build directory and do a from-scratch build. The worst part is that the MINIX core, i.e., all the interesting parts, still only accounted for ~10 minutes of that build time, and virtually all the rest was spent compiling and then recompiling LLVM. The response I got from one of the aforementioned grad students was that this is "just how cross-compilers work". No, pal; that's how the compiler that you chose works.
Later, the Go folks fixed their compiler to be a cross-compiler by default. See https://dave.cheney.net/2015/03/03/cross-compilation-just-go...
IMO, it's unforgivable that any given mainstream toolset wouldn't make this a baseline project goal.
Was the LLVM/Clang build done for building a cross compiler to build Minix or was it to build a compiler that runs on Minix? The latter would be unavoidable, but for the former just having LLVM pre-installed on the build machine should do.
This piqued my interest, what was this project exactly? Can you elaborate on this anecdote?
It's something in the ABI (I was on x86_64, the ABI is rather complex) related to padding the stack frames when calling functions.
I don't remember if this applied to all functions or was it something special needed for interrupt handlers.
The linux kernel does not follow this part of the ABI, so the compiler needs to be told that there is no red zone or functions will randomly see their stack frame clobbered by interrupt handlers.
You're absolute right, but I would add a qualifier that it is impossible for a kernel to use the redzone optimization. Certain architectures (Like x86) will push data onto the stack when an interrupt happens and there is no way to tell them to put that data after the first 128 bytes on the stack.
I say "nobody" because in particular signal handlers (Which are basically just userland interrupts) can interrupt any part of userland code. However since they are generated entirely by the kernel it is easy enough for the kernel to obey the redzone and place the signal handler 128 bytes above the stack pointer, so in practice this isn't that big of a deal.
Shameless plug: I used bkerndev verbatim for my bare metal project - Nope OS [1] - a C64-like system that I built for my son when he was born, so that he could get to know computers the same way I did. :)
Although starting coding right away and getting tangible results is somewhat rewarding, it would be nice to see core concepts of designing a kernel mentioned in a "Kernel 101" article:
* How to choose between monolithic/micro/nano architecture?
* What are primary goals and use cases?
* What hardware should it abstract away in HAL?
* How to design userland-kernel and drivers-kernel API/ABI?
* How much isolation is needed and what are ways to provide it?
>"Most registers of the x86 CPU have well defined values after power-on. The Instruction Pointer (EIP) register holds the memory address for the instruction being executed by the processor. EIP is hardcoded to the value 0xFFFFFFF0. Thus, the x86 CPU is hardwired to begin execution at the physical address 0xFFFFFFF0. It is in fact, the last 16 bytes of the 32-bit address space. This memory address is called reset vector.
This says that the EIP is doing a JMP to an address in RAM not a memory-mapped IO address which points to ROM where the BIOS is stored.
>"Now, the chipset’s memory map makes sure that 0xFFFFFFF0 is mapped to a certain part of the BIOS, not to the RAM. Meanwhile, the BIOS copies itself to the RAM for faster access. This is called shadowing. The address 0xFFFFFFF0 will contain just a jump instruction to the address in memory where BIOS has copied itself."
The second says that the CPU is doing a JMP to an IO mapped-memory address which points to ROM.
So the first passage says that CPU is just doing a JMP to 0xFFFFFFF0 in RAM. The second passage say that the CPU is doing a JMP to memory-mapped IO address which points to ROM.
Are these not completely contradictory or am I reading this wrong?
It's always been my understanding that that reset vector always pointed to a memory-mapped address which was located in ROM. Since the BIOS' POST routines contain code to initialiZe and test memory(the BIOs will actually emit beep codes if no memory is present or memory is faulty.), this would be a chicken and egg problem.
Also the post mentions the chipset loads the BIOS into RAM as a process called "Shadowing" which was done because ROM used to be slow. But since BIOS ROM these days in generally NAND flash I don't believe this is the case any longer.
Also these two statements appear to contradict each other:
>"All x86 processors begin in a simplistic 16-bit mode called real mode. The GRUB bootloader makes the switch to 32-bit protected mode by setting the lowest bit of CR0 register to 1. Thus the kernel loads in 32-bit protected mode."
>"Do note that in case of linux kernel, GRUB detects linux boot protocol and loads linux kernel in real mode. Linux kernel itself makes the switch to protected mode."
The first states that the kernel loads in protected mode and then the following states that kernel loads in real mode.
Grub has other ways to boot kernels. I once used a method that looks for a specific signature inside the kernel blob that would tell Grub to stay in 32 bit mode, put the blob at a certain memory address and jump into it. This was a feature of Grub 1 back in the day. I do not know if it was removed in Grub 2.
>"When the boot loader invokes the 32-bit operating system, the machine must have the following state ... ‘CR0’
Bit 31 (PG) must be cleared. Bit 0 (PE) must be set. Other bits are all undefined."[1]
However it sounds like Grub also understand the Multiboot Specification so can keep protected mode set if it's booting a kernel that expects it to already be set. It's not clear to me how Grub would determine that.
http://www.gnu.org/software/grub/manual/multiboot/multiboot....
[0] (page 5)- https://firmware.intel.com/sites/default/files/resources/A_T...
Also thanks for the link.
Because it launched from DOS, many insisted to me it wasn't really an operating system. What was it if it wasn't an operating system?
So I guess both you and your friends were wrong.
Name: vmlinuz-*
Kind: DOS/Windows executableThe Windows NT line, which subsumed the consumer line starting at Windows 2000, always used its own bootloader. So has Windows CE.
Anyhow, my point was just to reinforce the GP's point that it's ok to think about DOS as a bootloader because Microsoft have done exactly this themselves.
Win95 had a full screen splash which was an RLE file called logo.sys, loaded by the win95 boot process. I don't think autoexec even ran under 95/98/me. I'm sure that Wikipedia will have the full history though.
I remember writing my own logo.sys boot splashes. If I recall correctly they were just bitmaps with a couple of values altered in a hex editor to define which sequence of colours (in the colour palette) to animate.
I wish we had more of these, not least because of the first comment: "great post. Didn't know it was this easy ;)"
People assume a lot of low level stuff like this is near magic not for mere mortals, and it's a great shame, because there's a lot of this kind of low level stuff that more people would benefit from playing with.
And because demystifying it is a great path to get people into kernel hacking even if they don't end up writing their own complete kernel.