Skip to content

elfloader: add support for PSCI functions via HVC - #239

Merged
Indanz merged 3 commits into
seL4:masterfrom
Ivan-Velickovic:hvc_support
Nov 26, 2026
Merged

Indanz merged 3 commits into
seL4:masterfrom
Ivan-Velickovic:hvc_support

Conversation

@Ivan-Velickovic

Copy link
Copy Markdown
Contributor

My understanding is the HVC ABI is the exact same as the SMC one, it's just a difference with exception levels.

Also adds a commit to automatically add the right -smp flags with the generated simulate scripts.

Draft because I did this very quickly and need to review my own changes more.

So far I have tested that SMP/uni-core QEMU works and that I haven't broken PSCI SVC functionality by testing SMP on the Odroid-C4.

help="QEMU CPU", default="@QEMU_SIM_CPU@")
parser.add_argument('-o', '--cpu-opt', dest='qemu_sim_cpu_opt', type=str,
help="QEMU CPU Options", default="@QEMU_SIM_CPU_OPT@")
parser.add_argument('--smp', dest='qemu_sim_smp', type=str,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the other main arguments have a single letter variant, -s is already taken though so I didn't know what to put here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe -j, like make does?

Comment thread elfloader-tool/src/arch-arm/psci.c Resolved
@Ivan-Velickovic
Ivan-Velickovic force-pushed the hvc_support branch 2 times, most recently from 8a53cb9 to cea7513 Compare October 23, 2025 13:07

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should investigate into HVC on 32-bit ARM.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to be the same as for 64-bit Arm.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially tried that but it failed for 32-bit ARM on QEMU. Will have to debug.

Comment thread cmake-tool/helpers/simulation.cmake Resolved
@Indanz

Indanz commented Oct 23, 2025

Copy link
Copy Markdown
Contributor

Considering you want to turn on a real CPU core, I don't see why you would need to use a HVC call instead of a SMC call for that. HCV is to call to a hypervisor running in EL2, SMC is to call into EL3. So except if for some reason SMC just fails and HVC is redirected to EL3, I don't see how using HVC can work while SMC doesn't. Is this some Qemu quirk or am I missing something?

@midnightveil

midnightveil commented Oct 23, 2025 •

Copy link
Copy Markdown
Contributor

Yeah, I think this might be a weird QEMU quirk. But given how little was needed to fix it...

However there are a few (but not many) boards with device trees that specify that they want HVC calls for PSCI, e.g. https://elixir.bootlin.com/linux/v6.17.4/source/arch/arm64/boot/dts/qcom/msm8994.dtsi#L178. So it's not unreasonable for the device tree to ask for this...

@Ivan-Velickovic

Copy link
Copy Markdown
Contributor Author

Is this some Qemu quirk or am I missing something?

I don't know why just doing an smc instead does not work, I tried it but it failed to turn on any other CPUs.

I do not know why QEMU has this restriction, I am yet to look into it.

@Ivan-Velickovic

Copy link
Copy Markdown
Contributor Author

Perhaps this clears things up:
https://github.com/qemu/qemu/blob/88b1716a407459c8189473e4667653cb8e4c3df7/hw/arm/virt.c#L2265-L2281

@Ivan-Velickovic
Ivan-Velickovic marked this pull request as ready for review November 4, 2025 08:55
@Ivan-Velickovic

Copy link
Copy Markdown
Contributor Author

This is ready for review now. I have added more details in the commit message for the motivation and why this is needed in the first place.

I attempted to get 32-bit ARM working but ran into two problems:

  1. At least for the PSCI version that QEMU expects, the SMC/HVC function IDs are incorrect for 32-bit. E.g for SMC_FID_CPU_ON it is 0x80000003 instead of 0x84000003.
  2. The function IDs were easy to fix, which then lead to all the CPUs being booted and the kernel started executing, but never dropped to user space.

This is the log I got:

ELF-loader started on CPU: ARM Ltd. Cortex-A15 r2p1
  paddr=[40454000..4076104b]
No DTB passed in from boot loader.
Looking for DTB in CPIO archive...found at 4049886c.
Loaded DTB from 4049886c.
   paddr=[4004c000..4004dfff]
ELF-loading image 'kernel' to 40000000
  paddr=[40000000..4004bfff]
  vaddr=[a0000000..a004bfff]
  virt_entry=a0000000
ELF-loading image 'rootserver' to 4004e000
  paddr=[4004e000..40433fff]
  vaddr=[10000..3f5fff]
  virt_entry=1d77c
Boot cpu id = 0x0, index=0
Core 1 is up with logic id 1
Core 2 is up with logic id 2
Core 3 is up with logic id 3
Enabling MMU and paging
Jumping to kernel-image entry point...

Bootstrapping kernel
available phys memory regions: 1
  [40000000..80000000)
reserved virt address space regions: 4
  [a0000000..a004c000)
  [a004c000..a004de8e)
  [a004e000..a0434000)
  [ff000000..ff100000)

I don't know if I have time to debug the 32-bit ARM case, I can type up more details and post it in an issue.

Is it possible to get this merged without 32-bit support?

@Indanz Indanz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer working arm32 support, but otherwise looks fine.

help="QEMU CPU", default="@QEMU_SIM_CPU@")
parser.add_argument('-o', '--cpu-opt', dest='qemu_sim_cpu_opt', type=str,
help="QEMU CPU Options", default="@QEMU_SIM_CPU_OPT@")
parser.add_argument('--smp', dest='qemu_sim_smp', type=str,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe -j, like make does?

Comment thread cmake-tool/helpers/simulation.cmake Resolved
@Indanz

Indanz commented Nov 5, 2025 •

Copy link
Copy Markdown
Contributor

I attempted to get 32-bit ARM working but ran into two problems:

1. At least for the PSCI version that QEMU expects, the SMC/HVC function IDs are incorrect for 32-bit. E.g for `SMC_FID_CPU_ON` it is `0x80000003` instead of `0x84000003`.

That doesn't make any sense, is this another Qemu quirk?

Please just add the 32-bit support and ignore the problem for now (so no FID work-arounds either). Maybe add a printf that it seems broken on Qemu, but trying anyway.

@Ivan-Velickovic

Copy link
Copy Markdown
Contributor Author

That doesn't make any sense, is this another Qemu quirk?

I was looking at this PSCI specification (DEN0022F.b_Power_State_Coordination_Interface). Section 5.1.4 says the function ID for SMC32 is 0x84000003, but the elfloader code has it as 0x80000003.

Please just add the 32-bit support and ignore the problem for now (so no FID work-arounds either). Maybe add a printf that it seems broken on Qemu, but trying anyway.

Sure.

@Indanz

Indanz commented Nov 6, 2025

Copy link
Copy Markdown
Contributor

That doesn't make any sense, is this another Qemu quirk?

I was looking at this PSCI specification (DEN0022F.b_Power_State_Coordination_Interface). Section 5.1.4 says the function ID for SMC32 is 0x84000003, but the elfloader code has it as 0x80000003.

Oh, that's just wrong. Bit 30 determines whether the calling convention is 32 or 64 bits, but Elfloader is using the 32 bit calling convention on 64-bit, so all codes should be the same.

Ivan-Velickovic added a commit to Ivan-Velickovic/seL4_tools that referenced this pull request Nov 6, 2025
Note that this does not work for 32-bit ARM, at least when
tested on QEMU with:

../init-build.sh -DPLATFORM=qemu-arm-virt -DAARCH32=1 -DSMP=1

See [1] for details.

[1]: seL4#239 (comment).

Signed-off-by: Ivan Velickovic <i.velickovic@unsw.edu.au>
@timretout

Copy link
Copy Markdown

I tested this PR (8c7a9b4, merged onto master 6493e0c) with sel4test on two cores:

  • QEMU virt with GICv3, booted through UEFI: both cores up, 136 tests pass, including MULTICORE0001 to 0005.
  • Google Compute Engine c4a-standard-2: both cores up, MULTICORE0001 to 0005 pass.
  • AWS EC2 t4g.small: both cores up. The run stopped in a cache test before reaching the multicore tests, for reasons unrelated to PSCI.

It isn’t only QEMU that needs this: the Arm VMs on both EC2 and Compute Engine boot at EL1 and their device trees ask for PSCI over HVC, so without this PR the elfloader can’t bring up a second core on either.

I think the checks just need kicking?

@Indanz Indanz added the hw-test enable sel4test hardware builds + runs label Nov 16, 2026
@Indanz

Indanz commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Thanks for testing, I'll merge it when the tests pass.

@Indanz

Indanz commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

The 32-bit Arm builds fail with "/github/workspace/tools/seL4/elfloader-tool/src/arch-arm/armv/armv7-a/32/smc.S:31: Error: selected processor does not support `hvc #0' in ARM mode".

Drop 32-bit HVC support or what should we do?

@midnightveil

Copy link
Copy Markdown
Contributor

We could encode the bytes manually? Using .inst.

@Indanz

Indanz commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

We could encode the bytes manually? Using .inst.

That assumes that the error is wrong though. To me it is a sign we compile with the wrong options, e.g. we need armv7ve but don't have that or something.

@midnightveil

Copy link
Copy Markdown
Contributor

clang reports:

2026-11-16T13:24:16.1072947Z /usr/bin/ccache /usr/bin/clang -target arm-linux-gnueabi  -I/github/workspace/tools/seL4/elfloader-tool/include -I/github/workspace/tools/seL4/elfloader-tool/include/plat/imx6 -I/github/workspace/tools/seL4/elfloader-tool/include/arch-arm -I/github/workspace/tools/seL4/elfloader-tool/include/arch-arm/32 -I/github/workspace/build/elfloader/gen_headers -I/github/workspace/build/elfloader -I/github/workspace/tools/seL4/elfloader-tool/include/arch-arm/armv/armv7-a -I/github/workspace/tools/seL4/elfloader-tool/include/arch-arm/armv/armv7-a/32 -I/github/workspace/projects/util_libs/libcpio/include -I/github/workspace/build/elfloader/gen_config -I/github/workspace/build/libsel4/autoconf -I/github/workspace/build/kernel/gen_config -I/github/workspace/build/libsel4/gen_config -march=armv7-a  -D__KERNEL_32__ -g -D_XOPEN_SOURCE=700 -ffreestanding -Wall -Werror -Wextra -mno-unaligned-access -fno-common -fno-pic -fno-pie -MD -MT elfloader/CMakeFiles/elfloader.dir/src/arch-arm/armv/armv7-a/32/smc.S.obj -MF elfloader/CMakeFiles/elfloader.dir/src/arch-arm/armv/armv7-a/32/smc.S.obj.d -o elfloader/CMakeFiles/elfloader.dir/src/arch-arm/armv/armv7-a/32/smc.S.obj -c /github/workspace/tools/seL4/elfloader-tool/src/arch-arm/armv/armv7-a/32/smc.S
2026-11-16T13:24:16.1098618Z /github/workspace/tools/seL4/elfloader-tool/src/arch-arm/armv/armv7-a/32/smc.S:31:5: error: invalid instruction, any one of the following would fix this:
2026-11-16T13:24:16.1100111Z     hvc #0
2026-11-16T13:24:16.1100621Z     ^
2026-11-16T13:24:16.1101534Z /github/workspace/tools/seL4/elfloader-tool/src/arch-arm/armv/armv7-a/32/smc.S:31:5: note: instruction requires: thumb2
2026-11-16T13:24:16.1102605Z     hvc #0
2026-11-16T13:24:16.1103084Z     ^
2026-11-16T13:24:16.1104179Z /github/workspace/tools/seL4/elfloader-tool/src/arch-arm/armv/armv7-a/32/smc.S:31:5: note: instruction requires: virtualization-extensions
2026-11-16T13:24:16.1105466Z     hvc #0
2026-11-16T13:24:16.1105908Z     ^

Which I guess is not on by default for armv7 (we also don't seem to specify a CPU, which might be related)

@nspin did this for rust-seL4: google/smccc#33

llvm/llvm-project#185356 (thanks LLM for your bug that I'd argue is actually a feature)

For hyp mode we compile for armv7ve, but for non-hyp only for armv7a. But we need this even for non-hyp mode seL4 that is SMP. Apparently Cortex-A9 has no virt extensions (but it has SMP, so how does that work?). So we'd need to compile this code only for the CPUs that need it and pass flags or just encode the instruction directly :)

@timretout

Copy link
Copy Markdown

AI was previously suggesting this change to me but I've not had time to get my head around it - I think it switches this code on only on the CPUs with hypervisor extensions, as you said:

--- a/elfloader-tool/src/arch-arm/armv/armv7-a/32/smc.S
+++ b/elfloader-tool/src/arch-arm/armv/armv7-a/32/smc.S
@@ -25,7 +25,7 @@
 END_FUNC(psci_smc_func)
 
 BEGIN_FUNC(psci_hvc_func)
-    .arch_extension sec
+    .arch_extension virt
     stmfd    sp!, {r3-r11, lr}
     dsb
     hvc #0

@Indanz Indanz removed the hw-test enable sel4test hardware builds + runs label Nov 26, 2026
@Indanz Indanz added the hw-build enable all sel4test hardware builds label Nov 26, 2026
@Indanz

Indanz commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

If .arch_extension virt works, then that would be the cleanest.

Indanz pushed a commit to Ivan-Velickovic/seL4_tools that referenced this pull request Nov 26, 2026
Note that this does not work for 32-bit ARM, at least when
tested on QEMU with:

../init-build.sh -DPLATFORM=qemu-arm-virt -DAARCH32=1 -DSMP=1

See [1] for details.

[1]: seL4#239 (comment).

Signed-off-by: Ivan Velickovic <i.velickovic@unsw.edu.au>
@Indanz

Indanz commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

For hyp mode we compile for armv7ve, but for non-hyp only for armv7a. But we need this even for non-hyp mode seL4 that is SMP. Apparently Cortex-A9 has no virt extensions (but it has SMP, so how does that work?).

Surely that would use SMC instead of HVC?

The motivation for this change is that currently if the elfloader
is trying to boot multiple CPUs for an SMP configuration of seL4,
it will fail unless the PSCI method is SVC. The only other possible
PSCI method that is possible is HVC, which can occur when the system
is being virtualised by something like QEMU and is executing at EL1.

For QEMU, unless it is running with virtualisation explicitly enabled
(`virtualization=on`), it will default to accepting only the HVC PSCI
method. More details are available in comments in the QEMU source [1].

My understanding is the HVC ABI is the exact same as the SMC
one, it's just a difference with exception levels.

[1]: https://github.com/qemu/qemu/blob/88b171/hw/arm/virt.c#L2265-L2281

Signed-off-by: Ivan Velickovic <i.velickovic@unsw.edu.au>
Indanz pushed a commit to Ivan-Velickovic/seL4_tools that referenced this pull request Nov 26, 2026
Note that this does not work for 32-bit ARM, at least when
tested on QEMU with:

../init-build.sh -DPLATFORM=qemu-arm-virt -DAARCH32=1 -DSMP=1

See [1] for details.

[1]: seL4#239 (comment)

Signed-off-by: Ivan Velickovic <i.velickovic@unsw.edu.au>
Add a --smp argument to the simulate script which specifies the
number of cores to emulate. The value will be passed on to QEMU
via -smp.

Signed-off-by: Ivan Velickovic <i.velickovic@unsw.edu.au>
Note that this does not work for 32-bit ARM, at least when
tested on QEMU with:

../init-build.sh -DPLATFORM=qemu-arm-virt -DAARCH32=1 -DSMP=1

See [1] for details.

[1]: seL4#239 (comment)

Signed-off-by: Ivan Velickovic <i.velickovic@unsw.edu.au>
@Indanz
Indanz merged commit 6481cc9 into seL4:master Nov 26, 2026
39 of 40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hw-build enable all sel4test hardware builds

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants