kmu
Posts: 3
Joined: Sun Sep 17, 2017 7:17 pm

HVC instruction on RPI3

Sun Sep 17, 2017 7:42 pm

Hi,

I'm trying to make HVC instruction trap to EL2 but for some reason, it doesn't work. IIUC rpi3 CPUs start at EL2 (at least that is what I see in CurrentEL), so basically the only thing I need to do is to set vbar_el2 to point into proper exception vector table and that's it - not much space to make a mistake.

Unfortunately, when my EL1 code executes hvc #0 it doesn't trap into my EL2 handler. Interesting thing is that RPI3 doesn't crash, hangs or breaks in any other visible way and just returns execution to the caller, which makes me think that hvc traps somewhere (maybe in EL3?) just not to my EL2 handler.

I also should mention that the same code works in QEMU without any visible problems.

Any thoughts on how to approach this problem and make hvc work on RPI3?

LdB
Posts: 582
Joined: Wed Dec 07, 2016 2:29 pm

Re: HVC instruction on RPI3

Mon Sep 18, 2017 2:33 am

There is something seriously wrong with QEMU AARCH64 emulation if that is all you are doing to make it work, it isn't anywhere near accurate to physical silicon

For a start
http://infocenter.arm.com/help/index.js ... BGABF.html
HCE Hyp Call enable. This bit enables the use of HVC instructions. The possible values are:

0
The HVC instruction is undefined at all exception levels. This is the reset value.
1
The HVC instruction is enabled at EL1, EL2 or EL3.

The Pi3 boot stub does that part for you, why QEMU has it on I have no idea
https://github.com/raspberrypi/tools/bl ... armstub8.S
/* Set up SCR */
mov x0, #SCR_VAL
msr SCR_EL3, x0
SCR_VAL includes SCR_HCE which is defined as BIT(8) so it does turn the HVC on.

If you are booting the Pi3 from 0x0 using the config file (kernel_old=1) you will need to go deal with that register yourself. You said you were in EL2 so I would guess you must be using the Boot stub and picking the ARM8 up at 0x80000. So I am taking it on faith that the HVC response is turned on.

So assuming EL3 is done you will need to setup the trap registers on EL2 because they will also start off by default. So hit the HCR_EL2 etc to turn on what traps you want. QEMU is simulating that all those registers are starting on, in the real silicon they start OFF. The silicon reset behaviour is not to trap anything, you want something trapped go and set the registers.

timanu90
Posts: 60
Joined: Sat Dec 24, 2016 11:54 am

Re: HVC instruction on RPI3

Mon Sep 18, 2017 8:29 am

To enable HVC instruction you need to configure this register http://infocenter.arm.com/help/index.js ... BGABF.html.

Code: Select all

/* Enable Hypercalls */
    mrs x0, scr_el3
    orr x0, x0, #(1<<8)            /* Hyp Call enable. */
    msr scr_el3, x0
If you dont enable the HVC instruction, it work as a SMC instruction performing a monitor call. Thats why the processor hang, maybe you don't have VBAR_EL3 configured.

Hope this helps
Tiago

kmu
Posts: 3
Joined: Sun Sep 17, 2017 7:17 pm

Re: HVC instruction on RPI3

Sat Sep 23, 2017 8:50 am

There is something seriously wrong with QEMU AARCH64 emulation if that is all you are doing to make it work, it isn't anywhere near accurate to physical silicon

For a start
http://infocenter.arm.com/help/index.js ... BGABF.html
HCE Hyp Call enable. This bit enables the use of HVC instructions. The possible values are:

0
The HVC instruction is undefined at all exception levels. This is the reset value.
1
The HVC instruction is enabled at EL1, EL2 or EL3.
But AFAIK this register is only accessible in EL3, and as I mentioned RPI3 starts in EL2 so I cannot set this bit, thus the only thing that is left to do for me is to set VBAR. Moreover documentation says that instruction is undefined if HCE == 0, and I don't see any undefined instruction exceptions. So I wouldn't blame QEMU just yet, if I start QEMU at EL2 it absolutely reasonable behaviour.
So hit the HCR_EL2 etc to turn on what traps you want.
So far I only want HVC to trap to EL2 (as expected from HVC), there is no such option in HCR_EL2
Thats why the processor hang
I specifically stated that it doesn't hang.
maybe you don't have VBAR_EL3 configured.
I cannot configure it on EL2

kmu
Posts: 3
Joined: Sun Sep 17, 2017 7:17 pm

Re: HVC instruction on RPI3

Sat Sep 23, 2017 4:37 pm

I reduced the code to the bare minimum trying to isolate the problem and after that HVC instruction started to work. So setting vbar_el2 is enough to make HVC work on RPI3, as I initially thought, and the problem was something else.

Return to “Bare metal”

Who is online

Users browsing this forum: No registered users and 6 guests