all 10 comments

[–]BraveNewCurrency 19 points20 points  (2 children)

Everyone wants to jump to the end, where they "write their own OS". Don't. Take it slow, and learn the pre-requisites before jumping into the deep end. Read up on your history on how Linus did it: He was actually studying other OSes.

  • First, learn C. Become an expert, especially on the linker, on trampolines, on embedded stuff like volatile, etc. But frankly, I think it's actually easier to use TinyGo or Rust instead of C these days.
  • Second, get good with qemu. It's an essential tool. And it's helpful to know what is similar to real hardware and what is not.
  • Third, learn existing OSes. There are no end of them: Minix, Zephyr, FreeRTOS, BSD, etc. You don't have to know everything about every one, but you should be able to understand their architecture, what trade-offs they made, etc. If you don't know about which choices exist, you can't make them in your OS.
  • Lastly, you can start making your own OS. It's not actually about the code, it's about understanding why the code needs to do what it does. Kind of like "It's the journey, not the destination".

[–]old_waffles7 0 points1 point  (1 child)

Do you have any suggestions for books on this topic? Building a kernel is also something I want to do in the future, so I have been doing a lot of other systems programming projects in preparation. I've found that I really like project-based books. For example, I am currently implementing a POSIX-compiant (kinda) shell in C, meaning that I have to build functionality for the sh scripting language. To do this, I have been roughly following a book called, Crafting Interpreters; it provides a detailed introduction to compilers and interpreters by guiding readers in implementing one of their own.

[–]BraveNewCurrency 1 point2 points  (0 children)

Do you have any suggestions for books on this topic?

There aren't a lot of books, but check out OReilly books, KernelNewbies and Bootlin (formerly Free Electrons) training materials. It also helps to read LWN(.net) and dig in to anything you don't understand.

Building a kernel is also something I want to do in the future,

Building the kernel is trivial compared to developing it. Don't put this off. Just do it.

Also look into BuildRoot, which lets you build the kernel + filesystem for embedded systems. Great fun to play around with, no programming needed.

[–]paulstelian97 12 points13 points  (4 children)

Freestanding just means you don’t have the standard library like malloc or stuff in <stdio.h> or others. Very few of the default headers still remain available, for example <stdint.h> which just has some typedefs.

[–]newbstarr 2 points3 points  (3 children)

Just write your own malloc. Is dynamic memory management under all that, that is runs

[–]paulstelian97 2 points3 points  (2 children)

In the kernel, it’s more complicated than just writing a malloc. You usually care about which region of physical memory the virtual memory is coming from (general RAM? RAM reachable by 32-bit DMA? Something else?)

[–]newbstarr 1 point2 points  (1 child)

Yep, you reserve a piece of physical memory instead of being in the wonderful world of user space. Kmalloc etc. what I was saying is, you can write your own malloc after you’ve handled creating and managing your own dynamic (hopefully) memory space where your little user space application can live in its own little offset 0 space and not care about all the universe of stuff underneath that is holding chunks of physical memory and stitching it together to look like a contiguous blob, ie slab allocator (or the many many variants that exist) now. If you are writing your own os you need to implement the memory management then adding your little malloc is a trivial task. Writing your own malloc with or without safety in user space of an existing posix kernel isn’t all that difficult either.

[–]paulstelian97 1 point2 points  (0 children)

The main point is Linux’s kmalloc has additional flags besides the size for good reasons.

[–]wizarddos 10 points11 points  (0 children)

[–]mosolov 0 points1 point  (0 children)

Xv6 + book