Development of the ARM Cortex-M Mini-RTOS
Development log for my Mini Cortex-M RTOS kernel.
GitHub setup
I set aside the existing code as attempt-1 and created a new branch called rework.
Software installation
pacman -S mingw-w64-ucrt-x86_64-arm-none-eabi-gcc mingw-w64-ucrt-x86_64-qemu
- Installed
arm-none-eabi-gcc.- This is the ARM compiler. It also includes helper tools, such as
objdumpandreadelf. - It turns my C and assembly code into instructions that an ARM chip can understand. A normal compiler would turn the code into Intel instructions, which ARM can't run.
- This is the ARM compiler. It also includes helper tools, such as
- Installed QEMU (
qemu-system-arm).- This is a program that pretends to be an ARM chip, specifically a TI board called
lm3s6965evb. The RTOS code will run purely on this program.
- This is a program that pretends to be an ARM chip, specifically a TI board called
I already had gdb-multiarch, which is my debugger. It lets me pause the fake QEMU chip and run the program one line at a time, while viewing the registers and memory as the program progresses.
1. What the chip does when it powers on
The first thing the CPU does at reset is read the word stored at 0x00000000. The processor loads this value into the main stack pointer (MSP, register R13). After that, the CPU reads the word stored at 0x00000004 and loads it into the program counter (PC, register R15). Both 0x00000000 and 0x00000004 are in flash.
The word at 0x00000004, which gets loaded into the PC, is the reset vector: the address of Reset_Handler. Since this is an address the CPU will jump to for running code, its bit 0 needs to be 1 when it is stored. This is an old requirement for telling ARM and Thumb instructions apart. This CPU only runs Thumb instructions, but the requirement is kept for compatibility. After the CPU reads the address of Reset_Handler, it copies that word's bit 0 into the T-bit, and the rest of the word into the PC. From then on, for every instruction, the CPU checks the T-bit to know whether to run it in Thumb or ARM mode. This CPU can't run ARM mode, so a T-bit of 0 causes a fault.
Source: page 17, The Cortex-M3 Processor.
Some additional notes
The stack pointer is the first thing set on the CPU because an error or exception can happen between power-on and the first program running. When that happens, the CPU saves registers onto the stack as part of handling the interrupt. If the stack isn't set up properly, the CPU may cause a second fault on top of the first one by writing to an invalid address.
The T-bit is a single bit in the xPSR register. xPSR is a special-purpose register alongside R0–R15, and it holds the CPU's current status. The T-bit is bit 24 of that register, where 0 = ARM and 1 = Thumb.
By convention, the SP starts at the top of RAM so the stack can use the most space. SP − 4 is where the stack actually begins, since the stack grows downward from there (each push subtracts before writing). There is no automatically defined end to the stack, so it's my responsibility to define one to prevent stack overflows; otherwise the stack will just write over variables stored in RAM.
There are also two stack pointers, MSP and PSP. "SP" refers to whichever one is in use. By convention, MSP is used for the kernel and interrupts, and PSP is used for tasks.
2. GDB walkthrough of startup on the attempt-1 code
Reading symbols from firmware.elf...
(gdb) target remote :1234
Remote debugging using :1234
warning: A handler for the OS ABI "Windows" is not built into this configuration
of GDB. Attempting to continue with the default armv7 settings.
Reset_Handler () at boot.c:82
82 void Reset_Handler(void) {
(gdb) x/2wx 0
0x0 <vector_table>: 0x20010000 0x0000009d
(gdb) info registers sp pc lr
sp 0x20010000 0x20010000
pc 0x9c 0x9c <Reset_Handler>
lr 0xffffffff -1
(gdb) p/t $xpsr
$1 = 1000001000000000000000000000000
(gdb) x/3i $pc
=> 0x9c <Reset_Handler>: push {r7, lr}
0x9e <Reset_Handler+2>: sub sp, #8
0xa0 <Reset_Handler+4>: add r7, sp, #0
(gdb) stepi
0x0000009e 82 void Reset_Handler(void) {
(gdb) info registers pc sp
pc 0x9e 0x9e <Reset_Handler+2>
sp 0x2000fff8 0x2000fff8
(gdb)
- The vector table is the 2 words in flash.
- The first word was loaded into SP, and the second was loaded into PC with bit 0 cleared (
0x9d→0x9c). - LR holds its reset value.
- In the xPSR, the second 1 is the T-bit, which is correctly 1 (Thumb).
- The first
push, which the compiler generated, happens withstepi.