←back to thread

309 points LorenDB | 1 comments | | HN request time: 0.256s | source
Show context
siws ◴[] No.42641548[source]
Reading this post makes me think, how can someone start to get into the drivers and OSes world? This seems so complicated I really don’t know where to start.
replies(6): >>42641944 #>>42642133 #>>42643359 #>>42644209 #>>42645600 #>>42651669 #
1. ultimaweapon ◴[] No.42644209[source]
Actually it is very simple to communicate with the modern hardware (both x86 and ARM). All you need to do is read/write the hardware memory. This called Memory-mapped I/O (or MMIO in short). Of course you cannot do this from the application that run on the OS since it is a job of the kernel to prevent the application to direct access the hardware memory. Here are the roughly steps if you want to get into this field:

1. You need a programming language that capable of output the machine code for your target CPU. Better to be a language that does not have GC or runtime like Rust/C++/C/Zig. If you are not familiar with low-level language I recommend C as a first step since it is easy to find examples on the internet.

2. Learn the basic of assembly language of your target CPU. You need this because the above language may not provide the intrinsic functions for some instructions.

3. Start writing a hello world kernel. You can find a bunch of tutorial on the internet, especially for x86.

4. With step 3 you should already learned how the CPU start your kernel and how many mode and privilege level available on the CPU. Usually the CPU will give you the highest privilege level when jumping into your code.

5. Now you need to setup the CPU into whatever you want. For example, you likely need to switch the CPU to long mode on x86 so you can use 64-bits instructions. In this step you likely need to setup a virtual memory. You can find a reference for your CPU from their website.

6. If you have reached this step then congratulations! You should already have some idea how the CPU is working with the OS and how to enumerate available devices to discover its memory location. There are a ton of works that need to be done once you are here like implementing filesystem, scheduler, etc.

Remember that the only different between the software that run on the OS and the OS kernel is mode of the CPU currently executing its code. With highest privilege level you can use some instructions that are not available to the normal application.