![]() |
AKOS
v1.0.0
Documentation
|
Porting is the work needed to make AKOS run on a specific CPU architecture and board. The kernel provides the scheduling and runtime logic, while the port layer supplies the low-level CPU and startup support that the kernel depends on.
For the current tree, the Cortex-M3 port is the reference implementation.
ARM Cortex-M3 is the CPU architecture AKOS targets in this repository. It uses the ARM exception model, a separate system tick timer, and a nested interrupt controller to handle hardware events and kernel scheduling work.
For AKOS, the most important Cortex-M3 concepts are the register model, processor modes, interrupt controller, memory layout, instruction set, and exception flow.
The Cortex-M3 register set defines the CPU state that the port layer must create, save, and restore during startup and context switching.
The most important registers for AKOS are:
For a new thread, the port layer builds an initial stack frame that matches the register state the CPU expects to restore on exception return.
The Cortex-M3 has two related concepts:
These are not the same thing.
The execution modes are:
The privilege relationship is:
That means normal program code usually runs in Thread mode, while exceptions run in Handler mode. If Thread mode is unprivileged, it cannot execute some privileged instructions or directly access some protected system resources.
AKOS relies on this split so application code runs in Thread mode while the port layer performs startup, tick handling, and context switching in Handler mode.
On Cortex-M3, interrupts are handled through the exception system. The CPU uses exceptions for both external interrupts and internal events, so the port layer works through that model when it starts threads and requests scheduling work.
For AKOS, the important parts are:
These features let the kernel keep normal thread execution separate from the low-level exception flow that the port layer manages.
SysTick is a built-in Cortex-M3 system timer. It is a 24-bit down-counter that can generate a periodic exception when it reaches zero.
For AKOS, SysTick is important because it provides the regular time base used by the kernel.
The typical SysTick flow is:
In AKOS, this periodic exception is used to:
That is why SysTick is one of the core hardware blocks the port layer must configure during startup.
SVC is a system exception triggered by software through the SVC instruction. It is used when software wants to enter a controlled supervisor service path through the exception mechanism.
In AKOS, the purpose of SVC is to start the first runnable thread after the kernel has finished its startup work. The port triggers SVC, enters the SVC handler, restores the first thread context, and then returns into thread execution through the normal exception-return path.
PendSV is a system exception used for deferred service work. Software can set it to pending and let the CPU handle it later through the normal exception mechanism.
In AKOS, the purpose of PendSV is to perform context switching. When the kernel decides that another thread should run, it pends PendSV, enters the PendSV handler, saves the current thread context, restores the next thread context, and then returns to thread execution.
AKOS uses the port layer to control the interrupt state around kernel-critical work. The kernel depends on the port to temporarily mask interrupts when it needs to protect shared runtime data.
The reference port provides the basic interrupt helpers:
These helpers are used by kernel critical sections so the kernel can update ready lists, message queues, and other shared structures safely.
The port layer also configures exception priorities for scheduler-related exceptions. In the current Cortex-M3 port, PendSV is assigned a very low priority so context switching can be deferred safely, while SysTick is configured as the periodic kernel tick source.
The port layer sits between the kernel and the hardware. It is responsible for the pieces that are too architecture-specific for the kernel itself:
AKOS uses a reference CPU port in this repository. It lives under akos/port/arm/cortex-m3/ and provides the CPU-facing hooks that the kernel calls during startup and scheduling.
The main port APIs are:
The port layer also exposes helper macros such as:
The reference port also provides exception handlers that connect the CPU's exception model to the kernel runtime:
Before a thread can run, the port layer prepares the stack so the CPU can enter the thread entry function using the normal exception return path.
akos_port_task_stack_init() does three important things:
The prepared frame includes values for:
The stack layout is prepared to match the normal Cortex-M exception model:
That means a new thread starts with:
When the port later restores this stack, the CPU can continue as if the thread had already been running and was simply being resumed after an exception.
That gives a newly created thread the same kind of starting state that a restored thread would have after a context switch. In other words, the first thread start and a later thread restore both follow the same Cortex-M return model.
AKOS starts the first runnable thread through akos_port_start_first_task(). That function sets up the processor state and then triggers SVC 0 so the port layer can restore the initial thread context.
The startup flow is:
When the kernel decides a different thread should run, it asks the port layer to perform a switch.
In the current reference port, that work is handled through PendSV. The kernel updates tcb_high_rdy_ptr and then pends the exception, and the handler performs the CPU-level context save and restore.
The switch flow is:
The Cortex-M hardware already stacks R0 to R3, R12, LR, PC, and xPSR on exception entry. The PendSV handler only has to save and restore the remaining software-saved registers R4 to R11.
PendSV is a good fit for this role because it runs at very low priority and lets the CPU defer the actual switch until it is safe to do so.
The CPU port is only part of the job. A new board still needs its own startup, clock, memory map, and peripheral setup around AKOS.
For a new target, the board-side work usually includes:
In AKOS, that board-specific work lives mostly in the example and platform files, while the port layer stays focused on CPU behavior such as stack-frame layout, exception handling, and SysTick register programming.
The Cortex-M3 port is implemented in:
For scheduler behavior, see Scheduler. For thread behavior, see Threads management.