// embedded systems project
FreeRTOS on RISC-V
FreeRTOS brought up on 64-bit RISC-V and booted on QEMU with no firmware, from the startup code and linker script up.
A bare-metal bring-up of the FreeRTOS kernel on QEMU's RV64 virt machine: startup in assembly, a linker script that lays out RAM from 0x80000000, the CLINT machine timer driving a 1 kHz tick through the stock RISC-V port, and two preemptive tasks that prove the scheduler. The whole image is 30 KB. Built for the CENG507 embedded systems course.
- year
- 2026
- type
- embedded systems project
- group
- systems
//in plain words
A real-time operating system is the small program that lets a microcontroller juggle several timed jobs at once. This project starts one up on a simulated open-standard processor with no manufacturer firmware helping it: the first instruction that runs is ours, memory is laid out by hand, and a hardware timer ticks a thousand times a second so two jobs can take turns.
for students meeting an RTOS for the first time, and anyone curious what runs before the kernel
the problem
//01the problem
A real-time OS hides the hardware from applications, but first someone has to tame the hardware: start the core with no firmware, lay out memory, take the timer interrupt and hand the kernel a context to switch. The goal was to do that by hand on an open instruction set.
how it works
//02how it works
- 01
Bare metal, from the reset vector
An assembly startup file sets up a 4 KB boot stack, zeroes .bss, points mtvec at the FreeRTOS trap handler, enables machine interrupts and jumps to C with no BIOS at all. A linker script places code, data and a 64 KB interrupt stack in RAM from 0x80000000 on QEMU's virt machine.
- 02
The timer drives the scheduler
The CLINT machine timer, through mtime and mtimecmp, raises a 1 kHz tick: 10,000 counts at 10 MHz. Every trap, timer or ecall, enters the upstream RISC-V port's handler, which saves the context in assembly, lets the kernel pick a task, and returns with mret to whichever one it chose.
- 03
Proven by tasks, checked in the disassembly
Two tasks with 500 ms and 1000 ms periods interleave under preemption, using vTaskDelayUntil so they never drift, with stack overflow checking on. The build emits an ELF with symbols, a raw binary and a full disassembly listing, targeting rv64imac_zicsr with the lp64 ABI and the medany code model.
- 04
Small-data sections, found the hard way
RISC-V compilers put small globals in .sbss, which the first startup code did not zero. The linker script now folds the small-data sections into .rodata, .data and .bss so one loop clears everything, and the interrupt stack is aligned to 16 bytes because the port asserts it.
measured
//03measured
- 1 kHz
- scheduler tick
- clint machine timer at 10 mhz, 10,000 counts per tick
- 30 KB
- whole image
- 13,014 bytes of code; most of the rest is the 16 kb heap
- 0
- lines of bios
- qemu starts with -bios none; _start is the first code to run
- 303
- lines written for the bring-up
- startup, linker script, config, makefile, run script and the demo; the port is upstream
decisions
//04decisions
what was chosen, and what it cost
- 01
chose starting with -bios none
over booting through OpenSBI
With no firmware, the ELF's first instruction runs in machine mode at the reset address, which is the point of the exercise. The cost is owning the stack, .bss and the trap vector yourself.
- 02
chose the upstream RISC-V port unmodified
over writing the context switch
The port's trap handler and context save are the kernel's own, kept byte-identical for reference. The work of the project is everything around it: startup, memory map, configuration and the timer.
- 03
chose vTaskDelayUntil for both tasks
over vTaskDelay
A delay from now drifts by the task's own run time every period. Delaying until the next absolute tick keeps the 500 ms and 1000 ms schedules exact, which makes the interleaving in the output predictable.
try it
//05try it
//install qemu and a bare-metal toolchain on macos
linux and windows setup in the readme; distro linux-targeting compilers will not link
brew install qemu && brew tap riscv-software-src/riscv && brew install riscv-toolslinux and windows setup in the readme; distro linux-targeting compilers will not link
//build and run
quit qemu with ctrl+a then x
make && ./run.shquit qemu with ctrl+a then x
highlights
//06highlights
- Boots with no firmware on QEMU's virt machine
- Preemptive scheduling on a 1 kHz tick, stack overflow checks on
- RV64IMAC with Zicsr, medany code model, 30 KB image
- Setup documented for Linux, macOS and Windows, with GDB over QEMU
- Course project for CENG507, Embedded Systems
built with
- C
- RISC-V assembly
- FreeRTOS
- QEMU
- GNU toolchain
- Make
on githubCENG507-Project
- language
- C
- last push
- oct 9, 2026
printings
//07printings
2 entries
First commit
Trap vector setup and machine-mode interrupt enable, the two tasks and the first README.
Fixes and a full README
Small-data sections zeroed at boot, the interrupt stack aligned, and diagrams of the boot, memory map, trap path and scheduling.
questions
//08questions
- Why does task 2 print twice as often, and first when both wake?
- Its period is 500 ms against 1000 ms, and its priority is higher, so on a tick where both are due it preempts.
- How does a context switch happen?
- Every trap enters the port's handler, which saves all registers to the current task's stack. A switch is a change of the current task pointer, and mret restores whichever task it points to.
- Which toolchain do I need?
- A bare-metal RISC-V GCC with newlib: the Homebrew riscv-tools or the xPack toolchain, passed as the PREFIX. Linux-targeting cross compilers from distribution packages fail at link time.
- Can I debug it?
- Yes. Start QEMU with -s -S, connect GDB on port 1234 and break on main, a task or the trap handler.
related sheets
//09related sheets
//10contact
Say hello.I read everything.
Email is the fastest way to reach me. For anything that needs a conversation, book a call.
to
- name
- Khaled Saeed
- country
- Lebanon