11 ms·
Writing a “bare metal” operating system for Raspberry Pi 4
- nanis 5y agoI find the writing style tedious: The author expects a reader who does not know about `make` or cross compilers to relate to writing ARM64 assembly for the bootloader. If I am following along this material, then I don't need all the digressions with close enough descriptions of the tools. Like, if I am reading a home building tutorial, don't explain what a hammer is.
- detaro 5y agoNot that unlikely. E.g. in my university curriculum assembly programming came a bit before make, and way before needing a cross compiler.
- isometimes 5y agoThat’s a fair comment. I suppose I was just documenting my own journey. Before this project I’d written a lot of C and Assembly (albeit not Arm64), but I’d never used make, or a cross-compiler (or git for that matter!). I wrote about what was new to me, originally with the aim of not forgetting, and then later with the aim of helping others who might be in a similar place. I’m sorry if it was irksome in places - I’m learning too :)
- salawat 5y agoHey, don't take that too hard. I'm actually in tge midst of an LFS build centered on the raspi4, with shaded of learning to write boot code as well, and you can bet I'm scouring everything you wrote. No one can ever learn anything if no one writes this type of stuff down. sometimes the network of knowledge and laying out the precise line of questions to answer is the best way of charting out and communicating info for learners. So thank you for writing this!
- isometimes 5y agoYou’re very welcome :) I owe the Internet’s vast array of content creators a debt of gratitude for teaching me so much. Even those that sent me down the wrong path - it was invariably a data point that helped me to move on…
- jsjohnst 5y agoSmall suggestion that I’ve found works great: rather than rewrite the content (which I somewhat agree is tedious to skim over, but wouldn’t have commented like GP, when you know it already), link to the sources you used when learning yourself. Not only does it save you and the reader time, it also works as a handy log of the sources when you need to go back and look up a detail.
- yissp 5y agoJulia Evans pointed this out as a common problem in explanatory writing https://jvns.ca/blog/confusing-explanations/#pattern-2-having-inconsistent-expectations-of-the-reader-s-knowledge https://jvns.ca/blog/confusing-explanations/#pattern-2-havin...
- deleted 5y ago[deleted]
- hikerclimber1 5y agoEverything is subjective. Especially laws.
- mrlonglong 5y agoI can recommend https://www.giters.com/rust-embedded/rust-raspberrypi-OS-tutorials https://www.giters.com/rust-embedded/rust-raspberrypi-OS-tut... for those of you interested in using Rust. Be aware it also requires the use of Docker though but I don't need Docker and have changed my code not to need it.
- ggregoire 5y agoIs this an alternative UI for GitHub but without the files, commits history and so on? Why tho? I'm confused. Actual GitHub repo for anyone looking for the files: https://github.com/rust-embedded/rust-raspberrypi-OS-tutorials https://github.com/rust-embedded/rust-raspberrypi-OS-tutoria...
- superkuh 5y agoUnlike github or gitlab this page is actually an HTML file and does not need javascript executed to define the web components "HTML". I appreciate an accessible link, at least. I don't know if that's why he linked it.
- mrlonglong 5y agoOh, was it on GitHub? I hadn't noticed it was different. I'll do better next time.
- AQuantized 5y agoThis is perfect for me, I recently made the project OS for Nand to Tetris and have been learning systems programming with Rust. I wish there was a way to find resources like this more easily than scouring HN or trying to sort through google searches.
- jfoutz 5y agoFor a few glorious years google was amazing at this. the difference coming from altavista was unreal. at this point, I think I'd prefer boolean queries like altavista so I can search the word vectors myself. maybe some meta info so I can include/exclude based on various tags and links.
- ac42 5y agoAnd not to forget https://github.com/rsta2/circle https://github.com/rsta2/circle
- sneak 5y agoI'm surprised this doesn't start with qemu on a Real Computer for building/testing.
- deleted 5y ago[deleted]
- throwaway889900 5y agoOf all the sections in the tutorial, https://github.com/isometimes/rpi4-osdev/tree/master/part10-multicore https://github.com/isometimes/rpi4-osdev/tree/master/part10-... is probably the best one for anyone to read. I don't think a lot of people grasp that all the cores on a system start running immediately on power up and they're all running the same code from memory initially.
- SavantIdiot 5y agoThat depends on the architecture. E.g., Intel uses a wired-OR circuit and the cores race to determine who booted first, then that core becomes the boot core and executes the first instruction from boot ROM.
- throwaway889900 5y agoI'm assuming a simplistic CPU architecture aimed towards beginners, which is generally what a tutorial is aimed at. From there you can learn about all the nitty gritty details that you need to get actual chip to work.
- deleted 5y ago[deleted]
- deleted 5y ago[deleted]
- userbinator 5y agoI think the point is that on a standard x86 PC the cores don't all start running from reset; one boots up and the others are halted, waiting for the OS to start them (usually via the APIC.)
- Unklejoe 5y agoWhat happens when they all race to store to the same memory location? I guess if they all run in lockstep it doesn’t really matter? I’ve worked with some ARM SoCs from NXP and I could have sworn that one core comes up first and the others get released from reset later, with a “bringing up secondary CPUs” message printed.
- not-elite 5y agoWow, is it really this [1] easy to run a C routine? Where does the rpi4 store the firmware necessary to read from the sd card where this software is (presumably) stored? [1] https://github.com/isometimes/rpi4-osdev/blob/master/part1-bootstrapping/boot.S https://github.com/isometimes/rpi4-osdev/blob/master/part1-b...
- dragontamer 5y agoYeah, C is really easy to interface with assembly language. The hard part is the linker-script to get this working right :-) https://github.com/isometimes/rpi4-osdev/blob/master/part1-bootstrapping/link.ld https://github.com/isometimes/rpi4-osdev/blob/master/part1-b...
- deleted 5y ago[deleted]
- sigjuice 5y agoYeah, C is really easy to interface with assembly language. Doesn't necessarily have to involve assembly! https://duckduckgo.com/?q=c+interpreter https://duckduckgo.com/?q=c+interpreter
- jandrese 5y agoThe main core gets bootstrapped by the opaque binary blob in the graphics subsystem.
- Teknoman117 5y agoThere's a bootloader (u-boot) written into flash memory in the RPi4 SoC that handles the early initialization of the core and finding a kernel to boot. Think of u-boot as the UEFI equivalent for your RPi. Getting into C (or Rust) assuming the presence of some kind of system firmware (BIOS, UEFI, u-boot, coreboot, etc.) isn't too difficult in the grand scheme of things. Not to toot my own horn much, but here's an example I did of getting into Rust on a 386EX SBC i had hanging around. I actually yanked out the BIOS chip and this replaces it. Please forgive any poor Rust practices, this was written in a hurry. https://github.com/teknoman117/ts-3100-images/tree/master/rustimage https://github.com/teknoman117/ts-3100-images/tree/master/ru... I discovered a mind-melting bug where replacing the RTC clock chip / battery-backed RAM can erase the BIOS. This SBC uses the same flash chip for both user storage and the BIOS. The partition between the user area and the bios is stored in the CMOS ram, so if there is any junk in it, the BIOS might misidentify the flash boundaries and erase itself... So, I wrote this to recover the boards. Bonus points were that I only had an 8 KiB EEPROM hanging around so it had to fit in 8K initially.
- hikerclimber1 5y agoYou should only invest in the country you live in since you know what goes on politically.
- subhro 5y agoVery refreshing to see this. It is so much fucking easy to grok bare metal C compared to the <flavour of the year>-script junk that floats around these days.
- kennywinker 5y agoAh yes, nice easy to grok code like `curval &= ~(field_mask << shift);` :P But for real - I’ve had way more luck grokking embedded rust than all of the bare metal C examples i’ve looked at. C breeds dense bittwiddling and code that relies on inscrutable compiler behavior. There are easier ways to learn how these systems work at a bare-metal level.
- PaulDavisThe1st 5y agoWould you like to propose or reference a way of doing bit-twiddling that is clearer than this? Also hint: C doesn't breed bit-twiddling, writing software that actually interacts directly with hardware does.
- kennywinker 5y agoThat was just a throwaway example of a pretty write-only line of code from the op codebase, but since you asked: One operation per line. A comment for every operation. Shifts that explicitly say if they are wrapping or overflowing. Rust uses ! instead of ~ but if I had my way it’d be a named function like bitwise_invert(). // curval &= ~(field_mask << shift); // original line // pseudo-rust version let shifted_mask = FIELD_MASK.wrapping_shl(shift); // be clear about what kind of shift we’re doing let invered_mask = shifted_mask.bitwise_invert(); // use a fictional invert fn to avoid single-char operators. let shifted_val = curval & inverted_mask; // new variable instead of mutating the existing one Ideally those comments would say WHY we’re doing those ops rather than what’s notable about them - but i didn’t dig into the code enough to write explanations. And then we let the compiler crush that into an efficient lil one liner like the author of the original code did manually.
- 5y ago
- Brian_K_White 5y agoThis is a great idea, but I can't square that kind of project with WSL and brew instead of actual linux or macports. If it's meant for the youths and neophites where you don't want to scare them with strange not-windows things, perfectly fine, but then aarch64 assembly is already out of scope. If you don't know what's wrong with brew (as an os developer not a casual user) then I can't take you seriously as a system architect or os developer.
- kennywinker 5y agoI’m very familiar with many of homebrew’s faults, but none of them are dealbreakers for the casual “install latest version of tool”. If you don’t like it, just install the same tools using macports.
- vagrantJin 5y agoNeophites? > can't take you seriously as a system architect or os developer I dont think OS devs and Sys Architects are the intended audience. What might be helpful is if those rather busy people could chip in with their knowledge to improve said project rather than off-handedly dismiss it. It is afterall being made available freely and some devs who aren't low level proframmers might find it a good reason to learn something low level as an OS, don't you think so? Wouldn't it be nice?
- sylwester 5y agoOn the Pi Zero and Pi CM (maybe also others) you don't even need an SD card to boot it. You can boot it via rpi-boot https://github.com/raspberrypi/usbboot https://github.com/raspberrypi/usbboot so no need for qemu for testing. You can just test it on real hardware in no time.
- junon 5y agoWithout qemu you have to implement serial interfaces early in order to debug your own OS. Qemu is a huge benefit early on, and provides a sane environment for deterministic development as opposed to potentially quirky hardware. Usually hobbyist OS projects don't start directly on hardware. Many of them never reach the point of running on hardware.
- toast0 5y agoI don't know about qemu for a PI/arm in general, but you do have to be careful on x86, because segmentation limits aren't checked by default, and you can be lulled into doing things that don't work on hardware (I messed up secondary processor starting), and if you don't frequently test on hardware, it's easy to forget things. (See also homebrew games that only work on emulators) But, you can certainly wait to start on hardware until you've got it started on qemu. Debuggability is a lot better unless you've got specialized equipment for your hardware setup
- pm215 5y agoOn Arm a couple of only-on-hardware pitfalls are (a) cache maintenance -- QEMU doesn't model caches so won't notice if you forget to clean the dcache and flush the icache before executing code you just wrote or modified and (b) synchronization/barriers -- QEMU doesn't reorder memory accesses and generally makes writes to system registers take effect immediately, so won't notice if you forget necessary barrier insns.
- LeoPanthera 5y agoThe Pi 3 and 4 can also boot from TFTP with an NFS root. This also allows you to switch the OS your Pi is booting just by renaming a symlink on your server. All my home Pis boot that way. As a bonus, you never need to worry about an SD card going bad.
- poetaster 5y agoFor my older pies I found https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/os/ https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/os/ great. But this is arm assembly territory. I believe subsequent generations of pi have had good tutorials. OS, of course, is a very large, encompassing term. What is a minimal OS?
- isometimes 5y agoQuick thanks to the OP for linking my project: https://www.rpi4os.com https://www.rpi4os.com & https://github.com/isometimes/rpi4-osdev https://github.com/isometimes/rpi4-osdev. I'm humbled to be mentioned here. So great to read the feedback! Feel free to get in touch.
- rcarmo 5y agoNo problem. I’ve been looking into bare metal runtimes for ARM chips, and when I chanced upon this thought it was too much of a gem not to be posted here. :)
- 908B64B197 5y agoCS107E[0] might be an interesting course for those interested in bare metal programming. Enrollment is over for it as of now, so better luck next semester! [0] http://web.stanford.edu/class/cs107e/ http://web.stanford.edu/class/cs107e/
- erdo 5y agoMight sound like a strange comment, but I really like the tone of the readme. It's very welcoming and clear, you don't often find that level of professionalism in readme docs - it makes me think the author is probably quite unusual (in a good way)
- isometimes 5y agoI'm going to take that as a compliment, thank you ;-)
- rkagerer 5y agoThe Bluetooth modem is a Broadcom chip (BCM43455), and it needs to be loaded with proprietary software before it's useful to us. Are there any efforts out there to create open-source firmware for this chip?
- isometimes 5y agoI looked for a long time, but to no avail. Broadcom are notoriously tight-lipped when it comes to their intellectual property. Some have attempted to reverse-engineer... This was a good read: https://blog.quarkslab.com/reverse-engineering-broadcom-wireless-chipsets.html https://blog.quarkslab.com/reverse-engineering-broadcom-wire...
- deleted 5y ago[deleted]
- Decabytes 5y agoSetting aside any impracticalities of doing this, I've always wondered what an operating system would look like if it was tightly integrated into a specific virtual machine. Something like Java or Racket (or blub if you would like), to the point where only languages that ran on those platforms could run on the operating system. One of the biggest issues people bring up when someone asks "Why can't the OS handle garbage?" is that the OS doesn't know what is garbage and what isn't. But if you only ran languages that ran on the OSes virtual machine, then it would know the memory model. Java and Racket both have histories of languages being built on top of them, so if you don't like the base language you could create your own. I'm not saying this would be practical or useful (other than maybe as a proof of concept) but it could be fun!
- HideousKojima 5y agoThat's actually kind of been done before, at the CPU level as opposed to the OS though: https://en.m.wikipedia.org/wiki/Java_processor https://en.m.wikipedia.org/wiki/Java_processor
- Decabytes 5y agoWoah I had heard about the Jazelle instruction in arm, but full on JVM on the CPU! Thanks for the info
- grishka 5y agoThese are for traditional, stack-based JVM bytecode. But as I understand after having spent way too much time lately poking around and modifying .smali disassembly of other people's apps, Dalvik (Android JVM) bytecode was initially designed to map more-or-less directly to actual CPU registers to be easily JIT-able. But then I don't think garbage collection can be done purely in hardware. You still have to have something resembling a runtime, and it has to be written in a lower level language.
- yjftsjthsd-h 5y agoIt's been done:) https://en.m.wikipedia.org/wiki/JavaOS https://en.m.wikipedia.org/wiki/JavaOS https://en.m.wikipedia.org/wiki/Inferno_(operating_system) https://en.m.wikipedia.org/wiki/Inferno_(operating_system)
- DixieDev 5y agoThe readmes in each section are fantastic. Also, I cannot believe how straight-forward bootstrapping is on a modern ARM chip compared to x64, where things become painful quickly thanks to decades worth of backwards compatibility. Enabling the A20 line[0] is my favourite example of the nonsense you have to deal with. [0] https://wiki.osdev.org/A20_Line https://wiki.osdev.org/A20_Line
- phendrenad2 5y agoWell, on modern UEFI systems, it's very easy to bootstrap x86 also.
- musicale 5y agoAlso see https://github.com/dddrrreee/cs140e-20win/ https://github.com/dddrrreee/cs140e-20win/