Making a toy OS on hifive1

Hello everybody,
I have an hifive1 board and I recently had an idea to program myself a tiny minimal OS that runs on the microcontroller. (and can communicate via serial or something similar)
I tried to google for some resources on the topic, but found very few results which were really confusing.
I don’t really understand how the boot and memory really work - What exactly is the flash memory and how it should be used? Where do the instructions get fetched from? What other memory do I have? What exactly does the toolchain do?
And apart from that, I struggle with understanding the details of how to compile/assemble my code, how to define certain regions of memory (for example the stack), and generally how do I approach this?

I know that probably most of these questions are beginner questions and might be annoying, but I would really appreciate it if someone could help me understand what’s going on and point me to good resources.

Why don’t you look at something like FreeRTOS which can be quite minimal and is already ported for RISC-V?

Thank you for the idea, but I’m not looking to use an operating system for the HiFive1, but to make one from scratch “just for fun”, not for any reason other than that.

Flash is for code storage. Are you using the HiFive1 or HiFive1 Rev B? The rev b has 4M of flash and 16k of ram. The ram is used for heap and stack memory. All processing happens in the core which has 32-bit registers. These are the program counter (pc), stack pointer (sp), global pointer (gp), thread pointer (tp), and a few more. instructions and such are loaded to these registers for computations. I highly recommend reading “RISC-V Assembly Language” by Anthony J. Dos Reis. This will answer a lot of your questions and understand RISC-V at a low level. Then try looking through this blog

I’m really interested in doing the same thing. I’d like to learn about writing operating systems by doing it on completely open hardware like the HiFive. If you’re interested in working together with someone send me a note. My HiFive is gathering dust at the moment; the last time I tried to get everything working I ran into some compatibility problems but there has been another release of the SDK since then so the problem may be resolved now.

One thing I’m a bit hesitant about is the lack of a memory management unit in the HiFive. I think this means that the operating system can’t protect itself from user code; like the old computers of the 1980’s. Also my guess is that the operating system has to either only allow 1 user process or swap all the memory out to storage when swapping user processes - cumbersome and slow, but viable.

If you want to learn about some RTOS system primitives, you can also take a look at:

It is still work in progress, but for the parts not yet covered by the manual, you can take a look at the source code.

Thank you for the learning resources!

I have a background on the RISC-V ISA, but my main problem currently is that I don’t know much about booting and the actual “stuff” that happens or needs to happen at the memory-level on the OS startup - like what needs to be set to what (on the hardware and software levels) and how to actually set up those things in assembly and go to C. To sum it up, I don’t really know where to start…

1 Like

This is a great resource for what you are asking, I haven’t gone through it all myself but I use it as a reference. I’m not an expert by any means but I would also research “startup” code.

here is another good link I came across