RTOS From Scratch
RTOS From Scratch
Preface 3
1 Context Switching 4
1.1 The PendSV exception . . . . . . . . . . . . . . . . . . . . 4
1.2 MSP versus PSP . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3 Special Considerations . . . . . . . . . . . . . . . . . . . . 6
2 System Calls 8
2.1 Unprivileged mode and SVC calls . . . . . . . . . . . . . . . 8
2.2 The SysTick timer . . . . . . . . . . . . . . . . . . . . . . 10
References 11
Preface
This report deep-dives into the implementation of a minimal pre-
emptive RTOS kernel, from scratch. The implementation is based on the
ARM Cortex-M4 processor, using a GNU C toolchain, and utilizes all
OS-specific features built into the hardware. These include,
• The PendSV exception handler, usually employed for context switch-
ing.
• The SVC (i.e, Supervisor Call) assembly instruction, and its associated
exception handler.
• The SysTick timer peripheral, and its associated exception handler.
• Support for privileged and unprivileged operation modes.
• Support for two stack pointers, MSP (main) and PSP (process), shad-
owed behind SP, utilized by the OS and the application, respectively.
As prerequisites to this text, familiarty with the ARM architecture
and the C programming language is a must, and previous exposure to
Real-Time Operating System (RTOS) concepts, from the perspective of a
user, is recommended.
Also, check out the author’s implementation of a similarly minimalis-
tic pre-emptive RTOS kernel, with support for many OS resources (e.g:
mutexes, semaphores and queues).
> https://github.com/hazemanwer2000/BabyRTOS
Chapter 1
Context Switching
This chapter elaborates on how context switching may be implemented,
utilizing built-in hardware features such as the PendSV exception, and the
PSP stack pointer.
The writing above implies that the PendSV handler must be written in
assembly. In GNU C, use the naked compiler directive to tell the compiler
not to inject any assembly instructions in the function. Accordingly, a
naked function should be composed of assembly directives only. It is unsafe
to write C code in naked functions.
1 void __attribute__ (( naked ) )
2 PendSV_Handler ( void ) {
3 // ...
4 }
that all tasks must accomodate extra space for the maximum depth an
interrupt may require. If interrupt pre-emption is supported and enabled,
more space is required. This is, obviously, wastes much memory.
The Cortex-M4 provides two stack pointers. MSP is always used in
handler mode (i.e, when handling interrupts), while MSP or PSP may be
used in thread mode. Before triggering PendSV for the first time, PSP is
switched to, by setting a specific bit in the CONTROL special register,
System Calls
This chapter elaborates on how system calls may be implemented,
utilizing built-in hardware features such as unprivileged mode, and the
SVC assembly instruction and its associated exception handler.
Note that the SVC exception may not be pending, otherwise a hard
fault exception is triggered. This means that SVC should usually be the
highest priority interrupt in the system.