Skip to main content

ARM64

info

This driver works only on ARM64.

We will build a driver that powers off QEMU.

Power Off​

QEMU provides the semihosting interface and the Power State Coordination Interface (PSCI) interface. Both can be used to power off QEMU, the first one is returning a value to QEMU.

A simple driver that we can write is one that powers off QEMU when it is loaded.

Using semihosting​

This is used for debugging ARM devices, it basically traps the hlt instruction with the 0xf000 argument and uses them similar to hypervisor calls. It executes actions based on the values found in the registers.

  • register x0 stores the command SYS_*
  • register x1 stores a reference (pointer) to the exit reason

We have to enable semihosting in QEMU by appending -semihosting to the command line.

unsafe {
asm!("
hlt #0xF000
";
in("x0") 0x18_u64, // SYS_EXIT
in("x1") &0x20026u64, // Reason ApplicationExit
options(noreturn)
);
};

Using PCSI​

We need to use the PSCI_SYSTEM_OFF hypervisor call.

We use the hvc instruction in inline assembly.

// kernel API (use kernel::asm)
unsafe {
asm!(
"hvc #0";
in("x0") 0x8400_0008_u64, // PSCI SYSTEM_OFF (SMC32 ID)
options(noreturn)
);
};

Exercises​

  1. Write an empty module called PowerOff
  2. Use the hvc instruction with x0 set to 0x8400_0008.