News for hackers. Stuff that matters.
Re:How would you build an operating system? (SerenityOS with... by skizm on Wednesday February 07, 2024 @11:55AM (#39290921)
How does one get started on a toy OS? I don’t mean like concepts, but is there like a minimum viable OS .c file somewhere I can look at, with instructions for how to run it on “bare metal”? I assume I need a bootloader of some kind first?

I’m just totally unsure where I’d even begin in a practical sense.

Re:How would you build an operating system? (SerenityOS with... by tonetegeatinst on Wednesday February 07, 2024 @11:58AM (#39290962)
I think tinycore would be your best bet.

Its linux but stripped down to the very bare essentials

Re:How would you build an operating system? (SerenityOS with... by filleduchaos on Wednesday February 07, 2024 @12:08PM (#39291081)
Tiny core Linux is far too heavy for what they're asking for. It's the very bare essentials for a user, not a bare metal programmer.

The OSDev wiki has lots of actually minimal examples for different architectures and boards

Re:How would you build an operating system? (SerenityOS with... by adhamsalama on Wednesday February 07, 2024 @12:04PM (#39291039)
xv6 is a small one built for operating systems courses. We added functionality to it in my college operating systems course as a project.
Re:How would you build an operating system? (SerenityOS with... by gjvc on Wednesday February 07, 2024 @12:35PM (#39291471)
Re:How would you build an operating system? (SerenityOS with... by eric__cartman on Wednesday February 07, 2024 @02:11PM (#39292850)
That wiki was invaluable to me when I took computer organization courses. My recommendation would be to learn x86 assembly and C (also familiarize yourself with gdb!) by writing programs that run on a Linux system and only after target bare metal programming by using something like QEMU + gdb or Bochs for emulating bare metal hardware that's also debuggable.

(I used x86 assembly as an example assuming you wanted to boot your OS on a PC. An alternative could be to target a well documented SBC like the BeagleBone Black)

Re:How would you build an operating system? (SerenityOS with... by hnthrowaway0328 on Wednesday February 07, 2024 @12:45PM (#39291608)
Checkout Linux 0.01. The kernel is just a handful of files.

https://elixir.bootlin.com/linux/0.01/source

Re:How would you build an operating system? (SerenityOS with... by lieks on Wednesday February 07, 2024 @02:17PM (#39292917)
I started with the osdev wiki's Barebones Tutorial: https://wiki.osdev.org/Bare_Bones

It teaches how to do a hello world, with proper setup.

It's a bit more than a single C file, with some assembly/linker script/custom GCC toolchain required.

From there, that wiki is probably the most helpful resource you can find, along with the CPU manuals.

Re:How would you build an operating system? (SerenityOS with... by Dessesaf on Thursday February 08, 2024 @09:01AM (#39301987)
If you're not looking to build a bootloader, but will rely on GRUB then it's extremely simple. You just need to write a very simple multiboot header. It's literally just some numbers that you hardcode at the beginning of your program in assembly.

Then you compile this with GCC flags that tell GCC to not include a standard library (e.g. compile for bare metal).

Then you link with a very simple linker script that tells ld to put the multiboot header before your actual program.

Then, if you want to run on your own hardware, you change the GRUB config file to load your binary and reboot. Or if you want to run in QEMU, you just load the binary with QEMU. Using QEMU is probably simpler, since you're less likely to run into hardware weirdness at the beginning.

Then, you'd start with just writing to a text buffer, which has a hardcoded address on old architectures (it makes sense to start with an architecture where that's the case). That way, you can print stuff to the screen. That's enough to do a lot of very cool stuff already. After that, you configure interrupts, which you do by hard-coding some function pointers in a table using assembly. Then you use some special instructions to load these interrupt handler tables on startup. Now you can do all kinds of interactive things.

Re:How would you build an operating system? (SerenityOS with... by franknstein on Thursday February 08, 2024 @04:12PM (#39307773)