[PATCH 1/1] efi_selftest: check executing in EL2

Heinrich Schuchardt heinrich.schuchardt at canonical.com
Fri Apr 11 14:05:46 CEST 2025


On 4/11/25 12:00, Ilias Apalodimas wrote:
> Hi Heinrich,
> 
> [...]
> 
>> +
>> +/**
>> + * current_exception_level()
>> + *
>> + * Return:     current exception level, 0 - 3
>> + */
>> +static unsigned int current_exception_level(void)
>> +{
>> +       unsigned long el;
>> +
>> +       asm volatile (
>> +               "MRS %0, CurrentEL"
>> +               : "=r" (el) : : );
>> +
>> +       return (el >> 2) & 0x3;
>> +}
>> +
> 
> We have an identical function in arch/arm/include/asm/system.h, can we
> use that? It's static inline so you may be able to just include the
> header file directly. Also, that function adds a 'cc,' which tells the
> compiler that the instruction might change the condition code flags. I
> don't remember if this is needed, but it doesn't hurt to have it.

The function current_el() in arch/arm/include/asm/system.h is already 
used in switch_to_non_secure_mode() when determining if U-Boot should 
switch the exception state. Including the same function both in the code 
under test and in the test should be avoided.

https://developer.arm.com/documentation/ddi0597/2025-03/Base-Instructions/MRS--Move-Special-register-to-general-purpose-register-
does not describe any side effects when reading the CurrentEL flags?

Best regards

Heinrich

> 
> 
>> +/**
>> + * execute() - execute test
>> + *
>> + * Check that the exception level is not EL3.
>> + */
>> +static int execute(void)
>> +{
>> +       unsigned int el = current_exception_level();
>> +
>> +       efi_st_printf("Exception level EL%u\n", el);
>> +       if (el != 1 && el != 2) {
>> +               efi_st_error("EL1 or EL2 expected");
>> +               return EFI_ST_FAILURE;
>> +       }
>> +
>> +       return EFI_ST_SUCCESS;
>> +}
>> +
>> +EFI_UNIT_TEST(el) = {
>> +       .name = "exception level",
>> +       .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
>> +       .execute = execute,
>> +};
>> --
>> 2.48.1
>>
> 
> Other than that LGTM
> 
> /Ilias



More information about the U-Boot mailing list