Module 10 · Lesson 72 of 112

SMP and the boot hart

Explain what RISC-V harts share, why a single boot hart enters the course kernel first, and how OpenSBI lets the kernel start secondary harts.

  • Beginner to advanced
  • 8 min read
  • 10 min lab
What you will need

Ubuntu 24.04 in WSL2 or a VM for the written exercises, plus familiarity with the module's QEMU virt and OpenSBI boot setup

Your place in the module
By the end you can 4 objectives
  • Identify which resources belong to each hart and which resources harts share.
  • Distinguish the harts configured in QEMU from the harts ready for kernel work.
  • Explain why the boot hart initializes shared kernel state before secondary harts use it.
  • Describe the roles of the kernel and OpenSBI HSM in secondary startup.
Video coming soonSMP and the boot hart
The video for this lesson is being recorded. Read the lesson below in the meantime.
Recall first from Atomics and read-write locks
  • fetch_add and fetch_sub return the previous value; compare_exchange returns Result and is the building block of every lock
  • A read-write lock allows many readers or one writer, never both, and wins only on read-mostly data
  • Acquire on lock plus Release on unlock keeps the critical section inside the lock; Relaxed is safe for a standalone monotonic counter
01

What a multi-hart machine shares

4 min read

The last lesson made shared state safe with locks. Now you bring up the other harts that will contend on those locks, starting with what harts are and how the boot hart is chosen.

The hart that boots first is called the boot hart, and it is not always hart 0. This lesson shows why, with a run you can repeat.

On QEMU's RISC-V virt machine, you can configure several harts and still have only one running your kernel. The distinction matters during boot. Linux's RISC-V boot documentation describes an ordered boot method in which firmware initially gives control to a single hart, and Linux requests startup of the others later. The course kernel follows the same order. A successful boot message from one hart tells you that the initial entry ran. To establish that the other harts are working for the kernel, you need evidence from their startup too. RISC-V Linux boot requirements

A has its own architectural register state. Each hart has its own program counter, which identifies where it is executing, and its own general-purpose registers. If hart 0 changes a temporary register, it changes that register on hart 0. Hart 1 can have a different value in the register with the same name. The two program counters can point at different instructions or at the same instruction in the shared kernel image. Even when the harts execute the same code, each executes it using its own registers. A hart is a hardware execution context. A task is a unit of work the kernel can schedule on one.

Registers and the memory they refer to have different ownership. Suppose both harts hold the address of the same physical RAM location in a register. There are two copies of the address in registers and one location in RAM. If that location holds an allocator entry, both harts can operate on the same entry. The stack pointer gives another concrete example. Each hart has its own stack pointer register, but if both point into the same stack area, their stores can overwrite each other's stack contents. Separate registers give the kernel a way to select separate memory areas where it needs them. The kernel must make that selection itself.

The same distinction applies to . If two harts access the same UART transmit register, the writes reach the same UART. Starting additional harts gives the machine additional instruction streams while leaving that UART shared. So when you read kernel code, ask which hart executes the instruction and which resource the instruction accesses. The first question identifies the register state. The second tells you whether another hart can access the same object or device.

In , the harts can run work concurrently under one kernel. The same kernel image and shared memory support that work. A hart may have a special role during startup and then run ordinary kernel work alongside the others. This is how an ordered boot fits an SMP system. The startup roles differ so the kernel can initialize the machine in a defined order. Once the harts are ready, the kernel can use them for ordinary work.

Registers and shared resourcesHart 0Hart 1Program counterOwn counterOwn counterGeneral-purposeregistersOwn registersOwn registersPhysical memoryShared RAMShared RAMUART MMIOSame UARTSame UART
Each hart has its own registers and program counter, while both can access the same RAM and UART.
02

Why one boot hart enters the kernel first

3 min read

The is selected by OpenSBI, and it is not always hart 0. On QEMU virt with OpenSBI v1.3 (the firmware that Ubuntu 24.04's QEMU 8.2.2 package loads with -bios default), all harts start at once and compete for the cold-boot role, and the winner enters the kernel. In repeated runs of the same four-hart kernel, the OpenSBI banner reported Domain0 Boot HART as 0 in some runs and as 1 or 2 in others, and the kernel's a0 at entry matched it each time. A waits under firmware control during that first entry. Here, “boots first” means that the boot hart is first to execute the kernel's startup instructions. Firmware runs before that handoff. Never hard-code hart 0 as the boot hart. Read the hart ID that firmware passes in a0 at kernel entry and use that value to identify the boot hart, on this machine and on any other RISC-V system.

This first entry performs work the kernel needs to do once. Consider setting up the physical frame allocator. Its initial state describes which frames the kernel can allocate and which are already reserved. The boot hart prepares that shared state before any secondary hart can use the allocator. Imagine instead that a secondary independently repeats the same initialization after the boot hart has taken a frame. If the second initialization marks that frame free again, a later allocation can hand out memory that is already in use. The allocator needs a defined point at which its initial state is complete, followed by ordinary use of that state.

The same reasoning applies to zeroing the kernel's uninitialized static storage, usually called .bss. Once the kernel has cleared that memory and begun writing live values into it, repeating the clear can erase those values. One hart executes the initial startup path that prepares shared state. Secondary harts enter through a startup path intended for joining a kernel that already has shared state. Each still needs a valid execution context before it can run ordinary kernel code. Think of the two jobs in terms of what they prepare. The initial path prepares the kernel for everyone. The secondary path prepares another hart to run it.

Now consider a QEMU guest configured with four harts. At the initial kernel entry, the boot hart is executing kernel code and three secondary harts are waiting. The SBI hart status call reports this directly: in the lab run it returned state 1 (STOPPED) for each waiting hart and state 0 (STARTED) for the boot hart. The guest has four hardware execution contexts, with one running the kernel at that point. If the kernel never brings up the other three, it can continue to print messages and run tasks on the boot hart. The configured count tells you how many harts the machine has. The count of harts ready for kernel work depends on what the kernel has brought up. Linux makes a related distinction between CPUs present in the system and CPUs online and available for scheduling. CPU hotplug in the kernel

Roles during initial bootBoot hartSecondaryhartsWhich hartThe hartOpenSBI picksAll otherhartsInitial handoffEnters kernelWait underOpenSBIShared kernelsetupInitializesshared stateUse stateafter startup
At the first kernel entry, the boot hart runs initial setup while the secondary harts wait under firmware control.
03

How OpenSBI lets secondary harts start

3 min read

Once the kernel has prepared the shared state that secondary harts need, it can ask the firmware to start them. The interface is the SBI . OpenSBI implements this interface. The kernel running in supervisor mode chooses which waiting hart to start and where that hart should begin executing. OpenSBI handles the firmware side of the transition, allowing the target hart to enter supervisor-mode code at the requested starting point. Linux's RISC-V documentation identifies HSM as the mechanism used to start the remaining harts in ordered boot. RISC-V Linux boot requirements

Follow the actors in that operation. The boot hart is already executing the kernel and makes the request on behalf of the kernel. OpenSBI acts on the request for the selected secondary hart. When that secondary begins executing its entry code, it does so with its own program counter and registers. The boot hart can continue its own execution. Each request targets a particular hart, so bringing up all the secondaries in the four-hart example requires the kernel to arrange startup for each of the three. The waiting harts do not have to keep checking a kernel variable to find out whether the first kernel entry has finished. Before their own entry, their wait is managed by firmware.

There are three separate facts you can establish during startup. First, a target hart exists in the guest configuration. Second, the kernel has requested that it start through OpenSBI. Third, the target has reached the point where the kernel considers it ready for work. These facts become true at different times. HSM start is asynchronous, so a successful start request can return before the target reaches its kernel entry. The distinction matters when reading a boot log. A message printed by the boot hart after requesting startup describes what the boot hart did. Evidence that a secondary is ready must come from the secondary's progress through its own startup.

Starting a secondary hartBoot hartOpenSBITarget hartRequest hart startStart target hartExecute secondaryentry1. Boot hart → OpenSBIRequest hart start2. OpenSBI → Target hartStart target hart3. Target hart → Target hartExecute secondary entry
The boot hart requests startup through OpenSBI, and the target hart enters the kernel on its own execution context.

In “Starting secondary harts,” you will see how the kernel issues the start request and what the target hart does when it reaches its entry. That follows directly from ordered boot. The boot hart has prepared the shared kernel state, and each waiting hart now needs a way to enter the kernel and become ready for work.

Key takeaways

  • A hart is a RISC-V hardware thread with its own architectural registers and access to shared RAM and MMIO.
  • One boot hart runs the initial kernel entry while the secondary harts wait under firmware control. On QEMU `virt` with OpenSBI 1.3 that hart was 0, 1 or 2 in different runs, so the kernel reads its ID from a0.
  • OpenSBI implements the SBI Hart State Management extension, which the kernel uses to request that secondary harts start.
  • The kernel must bring up each secondary hart it intends to use before it can assign work to that hart.
Reflect
  1. Explain to a colleague, in two sentences: identify which resources belong to each hart and which resources harts share.
  2. Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
  3. What surprised you when you ran the commands yourself?
On a real board

Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.

References and further reading

Official documentation first. Links checked when the lesson was published.

Find your next step

↑ ↓ to move · Enter to open · Esc to close