2

I'm trying to understand how you set about writing an OS in something like C.

I've always used C to write applications - using commands like malloc and fork and so on to request things from the OS. If these methods don't exist when you're programming the OS itself (or perhaps the Kernel in particular), how do you write them?

How do you implement memory and process management and so on, specifically what functions are available and what is exposed to allows you to do this in C instead of assembler? (what do you call to interact with memory / devices / interrupts?)

I can imagine this isn't a small question, so I'm more than happy for an answer that includes some further reading but if you'd be able to summarise the main points related to each question that would be super handy for me!

Thanks in advance!

EDIT (8/3/20):

This related question has some good resources attached to it which help with answering the question:

What are some resources for getting started in operating system development?

MysteriousWaffle
  • 439
  • 1
  • 5
  • 16
  • 5
    See https://osdev.org/ and read [n1570](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf). Also study the source code of the [Linux kernel](https://kernel.org/). Some of the kernel code is not written in C – Basile Starynkevitch Mar 07 '21 at 19:18
  • See http://kernelnewbies.org/ and budget several months of full-time work. Also read https://pages.cs.wisc.edu/~remzi/OSTEP/ – Basile Starynkevitch Mar 07 '21 at 20:48
  • There is already a great answer to this question, but you might enjoy Brandon Falk's (gamozolabs) streams and videos. He has several in which he writes various kernels. He does it in Rust, but your question is about concepts that are language agnostic, and he usually explains what he is doing and why he does it in a pretty entertaining way. – icebp Mar 07 '21 at 20:49
  • 1
    @MysteriousWaffle: Please note, that Stack Overflow is a **Question/Answer** site and not a *discussion* forum. Because of that, it is bad place for "where-to-start" sort of questions, when you want to ask and get one direction, then ask again and get more detailed direction an so on. – Tsyvarev Mar 07 '21 at 21:52
  • Where is a good place to ask "where-to-start" questions? What I want is a overview (similar to what a3f has written with some pointers for further reading). I wouldn't say I have a developed enough understanding of the topic to vet anything I find on Google for the misconceptions @too_honest_for_this_site has raised but I wouldn't know where to post it either – MysteriousWaffle Mar 07 '21 at 22:01
  • 1
    @MysteriousWaffle You might want to read [ask] again. While I understand you lack directions, this is not the site, but there are certainly resources, I already gave you some hints in another comment and maybe college/university courses are best for you right now). Please leave it at that here. Feel welcome if you hav an on-topic question, of course. – too honest for this site Mar 07 '21 at 22:24

1 Answers1

5

How do you implement memory

Writing your own allocator is a common system programming exercise. Only thing you need from "outside" is a memory region to manage. If you are programming bare-metal, you'll likely have a memory map available that describes which regions are RAM. These you can manage with malloc.

and process management

Processes on a modern OS are a number of things. They usually have their own page tables, a context (registers to save while the process is suspended) and a number of other process-specific data structures.

The data structures you can set up in C, but their "activation" (switching page tables, saving/restoring context) is usually beyond what's possible in portable C. For those you either have assembly routines or some inline assembly interleaved with the C code.

and so on, specifically what functions are available and what is exposed to allows you to do this in C instead of assembler?

There are hosted and freestanding C implementations. If you write your own OS, you are usually within a freestanding C environment and you have no standard library provided functions at your disposal. You rely on your own code, write assembly routines or you use compiler intrinsics.

what do you call to interact with memory

You can cast integers to pointers and manipulate the pointees. volatile allows you to tell the compiler that you absolutely want to do reads/writes in the order written.

devices

Many devices are memory-mapped, meaning that you can communicate as if you are reading/writing memory (simplified; caches can make this a bit trickier). Other devices are addressable over dedicated I/O ports. Those require special instructions, which either the compiler has intrinsics for or you need to implement in assembly (that you can call from C).

interrupts?

Like above. Parts like enabling/disabling interrupts might require dedicated instructions. Setting an interrupt handler might be just a matter of writing a function pointer to the correct address.

I'm more than happy for an answer that includes some further reading but if you'd be able to summarise the main points related to each question that would be super handy for me!

My suggestion is getting yourself an ARM Cortex-M and start playing around with it. osdev as noted in the comments is also a very nice resource.

a3f
  • 8,517
  • 1
  • 41
  • 46
  • This is excellent, thank you! Couple of questions: - What provides the memory map? What would you call to access this? - I assume Memory Mapping isn't set up or managed by the OS? – MysteriousWaffle Mar 07 '21 at 20:14
  • The physical memory map is determined by the hardware developers. You usually get it out of a reference manual. The OS manages the MMU, which allows to remap stuff to different virtual memory addresses later on. Some buses also support configuring a base address for the memory map at runtime, which you usually do by interacting with the memory mapped bus controller. – a3f Mar 07 '21 at 20:19
  • Sure thing, another quick one on devices - how would you know that a device received the data you wrote to it's memory address? Would there be another address for the device to write back? – MysteriousWaffle Mar 07 '21 at 20:22
  • There are often status registers you can read out. – a3f Mar 07 '21 at 20:57
  • 2
    @MysteriousWaffle Plase remember this is not a tutorial site. There are books about this sobject and it won't take less to give even the details. Actually even more, because these books are typically directed to people who already have advanced programming skills and know the implementation language(s) very well. – too honest for this site Mar 07 '21 at 21:00
  • 2
    That's the problem answering far too broad (aka "unfocussed") questions. This answer contains a lot of missconceptions and (potentially dangerous) wrong information. While it might result from trying to not write a book right here, it will misslead other readers. This is one major reason why OT questions should be closed and not answered. You have enough reps to help moderating this site. Please use this privilege. – too honest for this site Mar 07 '21 at 21:20
  • I see value in nudging into the right direction. If you disagree, you can vote to close and downvote. If you see problems with this answer, feel free to edit. ;) – a3f Mar 07 '21 at 21:25
  • @toohonestforthissite I'm aware the question is broad, but that's why the end of it asks for a summary of the main points and further reading. I'd argue this is what I've been provided with by a3f's response and Basile Starynkevitch's comment. If the answers I've been provided with are wrong, can you elaborate as to what the 'potentially dangerous' misconceptions about them are? – MysteriousWaffle Mar 07 '21 at 22:03
  • @MysteriousWaffle A good start would also attend a C and then OS class at your local college/university/etc. As usual quality varies, of course, but it should at least give you starters. – too honest for this site Mar 07 '21 at 22:15
  • I would love to do the research myself, that is why I asked this question. I readily acknowleged what I was posing was pretty broad because my grasp of the topic isn't sufficient to craft a google search that returns information that directly answers my question. I'm asking for some reading resources build exactly that knowledge, so in the future I 'can' just search for it. If these kinds of questions aren't appropriate for Stack Overflow that's fine but I've seen plenty of similar ones that suggest otherwise. – MysteriousWaffle Mar 07 '21 at 23:26