Ring3: Added support for AARCH64 EL2&0 translation regime.

This commit is contained in:
Mikhail Krichanov 2024-10-21 17:51:52 +03:00
parent d12742da3d
commit a071d15784
7 changed files with 66 additions and 39 deletions

View File

@ -133,6 +133,15 @@ ASM_FUNC(ArmInvalidateTlb)
isb
ret
ASM_FUNC(ArmWriteCptr)
EL1_OR_EL2_OR_EL3(x1)
1:ret
2:msr cptr_el2, x0
b 4f
3:msr cptr_el3, x0 // EL3 Coprocessor Trap Reg (CPTR)
4:isb
ret
ASM_FUNC(ArmCallWFE)
wfe
ret

View File

@ -63,6 +63,7 @@ ArmMemoryAttributeToPageAttribute (
}
break;
default:
Permissions = 0;
break;
@ -479,29 +480,24 @@ GcdAttributeToPageAttribute (
PageAttributes |= TT_AF;
}
if ((GcdAttributes & EFI_MEMORY_USER) != 0) {
PageAttributes |= TT_PXN_MASK;
if ((GcdAttributes & EFI_MEMORY_RO) != 0) {
PageAttributes |= TT_AP_RO_RO;
} else {
PageAttributes |= TT_AP_RW_RW;
}
} else {
if (ArmReadCurrentEL () == AARCH64_EL1) {
//
// TODO: Add EL2&0 support.
//
PageAttributes |= TT_UXN_MASK;
}
if ((GcdAttributes & EFI_MEMORY_RO) != 0) {
PageAttributes |= TT_AP_NO_RO;
} else {
PageAttributes |= TT_AP_NO_RW;
}
}
if ((GcdAttributes & EFI_MEMORY_USER) != 0) {
PageAttributes |= TT_PXN_MASK;
if ((GcdAttributes & EFI_MEMORY_RO) != 0) {
PageAttributes |= TT_AP_RO_RO;
} else {
PageAttributes |= TT_AP_RW_RW;
}
} else {
PageAttributes |= TT_UXN_MASK;
if ((GcdAttributes & EFI_MEMORY_RO) != 0) {
PageAttributes |= TT_AP_NO_RO;
} else {
PageAttributes |= TT_AP_NO_RW;
}
}
return PageAttributes;
}
@ -603,6 +599,7 @@ ArmConfigureMmu (
UINTN RootTableEntryCount;
UINT64 TCR;
EFI_STATUS Status;
UINTN Hcr;
ASSERT (ArmReadCurrentEL () < AARCH64_EL3);
if (ArmReadCurrentEL () == AARCH64_EL3) {
@ -627,6 +624,19 @@ ArmConfigureMmu (
T0SZ = 64 - MaxAddressBits;
RootTableEntryCount = GetRootTableEntryCount (T0SZ);
if (ArmReadCurrentEL () == AARCH64_EL2) {
//
// Switch to EL2&0 translation regime.
//
Hcr = ArmReadHcr ();
Hcr |= ARM_HCR_E2H | ARM_HCR_TGE;
ArmWriteHcr (Hcr);
//
// Allow access to the Advanced SIMD and floating-point registers.
//
ArmWriteCptr (AARCH64_CPTR_FPEN);
}
//
// Set TCR that allows us to retrieve T0SZ in the subsequent functions
//

View File

@ -107,7 +107,7 @@
CpuExceptionHandlerLib|ArmPkg/Library/ArmExceptionLib/ArmExceptionLib.inf
ArmSmcLib|ArmPkg/Library/ArmSmcLib/ArmSmcLib.inf
ArmHvcLib|ArmPkg/Library/ArmHvcLib/ArmHvcLib.inf
ArmGenericTimerCounterLib|ArmPkg/Library/ArmGenericTimerVirtCounterLib/ArmGenericTimerVirtCounterLib.inf
ArmGenericTimerCounterLib|ArmPkg/Library/ArmGenericTimerPhyCounterLib/ArmGenericTimerPhyCounterLib.inf
PlatformPeiLib|ArmVirtPkg/Library/PlatformPeiLib/PlatformPeiLib.inf
MemoryInitPeiLib|ArmVirtPkg/Library/ArmVirtMemoryInitPeiLib/ArmVirtMemoryInitPeiLib.inf

View File

@ -88,24 +88,23 @@ ASM_FUNC(ArmCallRing3)
// Disable interrupts.
msr daifset, #0xf
isb
// Copy PSTATE to SPSR.
mrs x6, nzcv
mrs x7, pan
orr x6, x6, x7
// Prepare Ring3 SP and EntryPoint.
msr sp_el0, x1
msr elr_el1, x2
EL1_OR_EL2(x1)
1:msr elr_el1, x2
msr spsr_el1, x6
b 3f
2:msr elr_el2, x2
msr spsr_el2, x6
// Save Core SP and switch to CoreSysCall Stack.
mov x5, sp
3:mov x5, sp
str x5, [x4]
mov sp, x3
// Copy PSTATE to SPSR.
mrs x1, nzcv
mrs x2, pan
orr x1, x1, x2
//
// M[3:0], bits [3:0] AArch64 Exception level and selected Stack Pointer.
// 0b0000 - EL0.
// 0b0100 - EL1 with SP_EL0 (ELt).
// 0b0101 - EL1 with SP_EL1 (EL1h).
//
msr spsr_el1, x1
isb
eret

View File

@ -97,6 +97,7 @@ InitializeMsr (
)
{
UINTN Tcr;
UINTN Sctlr;
//
// If HCR_EL2.NV is 1 and the current Exception level is EL1,
// then EL1 read accesses to the CurrentEL register return a value of 0x2 in bits[3:2].
@ -115,6 +116,10 @@ InitializeMsr (
//
// Enable Privileged Access Never feature.
//
Sctlr = ArmReadSctlr ();
Sctlr |= SCTLR_EPAN;
ArmWriteSctlr (Sctlr);
ArmSetPan ();
}

View File

@ -25,6 +25,7 @@
#define AARCH64_CPTR_TFP (1 << 10)
#define AARCH64_CPTR_RES1 0x33ff
#define AARCH64_CPTR_DEFAULT AARCH64_CPTR_RES1
#define AARCH64_CPTR_FPEN (3 << 20)
// ID_AA64MMFR1 - AArch64 Memory Model Feature Register 0 definitions
#define AARCH64_MMFR1_VH (0xF << 8)
@ -46,6 +47,9 @@
#define SCR_FW (1 << 4)
#define SCR_AW (1 << 5)
// SCTLR - System Control Register definitions
#define SCTLR_EPAN BIT57
// MIDR - Main ID Register definitions
#define ARM_CPU_TYPE_SHIFT 4
#define ARM_CPU_TYPE_MASK 0xFFF

View File

@ -497,7 +497,7 @@ ArmEnableVFP (
VOID
);
UINT32
UINTN
EFIAPI
ArmReadSctlr (
VOID
@ -506,7 +506,7 @@ ArmReadSctlr (
VOID
EFIAPI
ArmWriteSctlr (
IN UINT32 Value
IN UINTN Value
);
UINTN