[Writeup] Shared pseudocode: part 36 of 36 ========================================== Section: Silicon & Architectures Started: 2026-11-28 17:49 UTC by admin Replies: 0, last post 2026-11-28 17:49 UTC Page: 1 of 1 Web: https://romdump.net/t/128 ------------------------------------------------------------------------------ #148 admin, 2026-11-28 17:49 UTC [OP] Source: https://romdump.net/raw/148
Shared pseudocode from the offline Arm documentation snapshot. Part 36 of 36.
// DecodeGPTContiguous
// =====================
// Decode a GPT Contiguous descriptor.
func DecodeGPTContiguous(pgs : PGSe, gpt_entry : bits(64)) => GPTEntry
begin
assert gpt_entry[3:0] == GPT_Contig;
var result : GPTEntry;
result.gpi = gpt_entry[7:4];
case pgs of
when PGS_4KB => result.size = GPTRange_4KB;
when PGS_16KB => result.size = GPTRange_16KB;
when PGS_64KB => result.size = GPTRange_64KB;
otherwise => unreachable;
end;
case gpt_entry[9:8] of
when '01' => result.contig_size = GPTRange_2MB;
when '10' => result.contig_size = GPTRange_32MB;
when '11' => result.contig_size = GPTRange_512MB;
otherwise => unreachable;
end;
result.level = 1;
result.istable = FALSE;
return result;
end;
// DecodeGPTGranules
// ===================
// Decode a GPT Granules descriptor.
func DecodeGPTGranules(pgs : PGSe, index : integer, gpt_entry : bits(64))
=> GPTEntry
begin
var result : GPTEntry;
result.gpi = gpt_entry[index*4 +:4];
case pgs of
when PGS_4KB => result.size = GPTRange_4KB;
when PGS_16KB => result.size = GPTRange_16KB;
when PGS_64KB => result.size = GPTRange_64KB;
otherwise => unreachable;
end;
result.contig_size = result.size; // No contiguity
result.level = 1;
result.istable = FALSE;
return result;
end;
// DecodeGPTTable
// ================
// Decode a GPT Table descriptor.
func DecodeGPTTable(pgs : PGSe, gpt_entry : bits(64)) => GPTTable
begin
assert gpt_entry[3:0] == GPT_Table;
var result : GPTTable;
// Descriptor bits [L0GPTSZ-PGS-2:12] are RES0
var s : integer{30, 34, 36, 39};
case GPTL0Size() of
when GPTRange_1GB => s = 30;
when GPTRange_16GB => s = 34;
when GPTRange_64GB => s = 36;
when GPTRange_512GB => s = 39;
otherwise => unreachable;
end;
var p : integer{12, 14, 16};
case pgs of
when PGS_4KB => p = 12;
when PGS_16KB => p = 14;
when PGS_64KB => p = 16;
otherwise => unreachable;
end;
let align_bits : integer{} = (s - p) - 1;
result.address = AlignDownP2(gpt_entry[NUM_PABITS-1:0],align_bits);
return result;
end;
// DecodePGS
// ===========
pure func DecodePGS(pgs : bits(2)) => PGSe
begin
case pgs of
when '00' => return PGS_4KB;
when '10' => return PGS_16KB;
when '01' => return PGS_64KB;
otherwise => unreachable;
end;
end;
// DecodePGSRange
// ================
pure func DecodePGSRange(pgs : PGSe) => AddressSize
begin
case pgs of
when PGS_4KB => return GPTRange_4KB;
when PGS_16KB => return GPTRange_16KB;
when PGS_64KB => return GPTRange_64KB;
otherwise => unreachable;
end;
end;
// DecodePPS
// ===========
// Size of region protected by the GPT, in bits.
readonly func DecodePPS() => AddressSize
begin
case GPCCR_EL3().[PPS3, PPS] of
when '0000' => return 32;
when '0001' => return 36;
when '0010' => return 40;
when '0011' => return 42;
when '0100' => return 44;
when '0101' => return 48;
when '0110' => return 52;
when '0111' => assert IsFeatureImplemented(FEAT_RME_GPC3); return 56;
when '1000' => assert IsFeatureImplemented(FEAT_RME_GPC3); return 46;
when '1001' => assert IsFeatureImplemented(FEAT_RME_GPC3); return 47;
otherwise => unreachable;
end;
end;
// GPCBW_EL3BWSTRIDEValid
// ========================
// Returns whether the current GPCBW_EL3.BWSTRIDE value is valid
readonly func GPCBW_EL3BWSTRIDEValid() => boolean
begin
assert IsFeatureImplemented(FEAT_RME_GPC3);
return GPCBW_EL3().BWSTRIDE IN {
'00000',
'00010',
'00100',
'00110',
'00111',
'01000',
'01001',
'01010',
'10000'
};
end;
// GPCFault
// ==========
// Constructs and returns a GPCF
func GPCFault(gpf : GPCF, level : integer) => GPCFRecord
begin
var fault : GPCFRecord;
fault.gpf = gpf;
fault.level = level;
return fault;
end;
// GPCNoFault
// ============
// Returns the default properties of a GPCF that does not represent a fault
func GPCNoFault() => GPCFRecord
begin
var result : GPCFRecord;
result.gpf = GPCF_None;
return result;
end;
// GPCRegistersConsistent
// ========================
// Returns whether the GPT registers are configured correctly.
// This returns false if any fields select a Reserved value.
readonly func GPCRegistersConsistent() => boolean
begin
// Check for Reserved register values
if IsFeatureImplemented(FEAT_RME_GPC3) then
if ! GPCCR_EL3().[PPS3, PPS] IN {'0xxx', '100x'} then
return FALSE;
end;
if GPCCR_EL3().GPCBW == '1' then
if ! GPCBW_EL3().BWSIZE IN {'00x', '1x0', '010'} then
return FALSE;
end;
if () then
return FALSE;
end;
let bwstride : integer = 1 << (40 + UInt(GPCBW_EL3().BWSTRIDE));
let bwaddr : integer = UInt(GPCBW_EL3().BWADDR) << 30;
if bwstride <= bwaddr then
return FALSE;
end;
if !IsAlignedSize{26}(GPCBW_EL3().BWADDR, 1 << UInt(GPCBW_EL3().BWSIZE)) then
return FALSE;
end;
end;
else
if GPCCR_EL3().PPS == '111' then
return FALSE;
end;
end;
if DecodePPS() > AArch64_PAMax() then
return FALSE;
end;
if GPCCR_EL3().PGS == '11' then
return FALSE;
end;
if GPCCR_EL3().SH == '01' then
return FALSE;
end;
// Inner and Outer Non-cacheable requires Outer Shareable
if GPCCR_EL3().[ORGN, IRGN] == '0000' && GPCCR_EL3().SH != '10' then
return FALSE;
end;
return TRUE;
end;
// GPICheck
// ==========
// Returns whether an access to a given physical address space is permitted
// given the configured GPI value.
// paspace: Physical address space of the access
// gpi: Value read from GPT for the access
// ss: Security state of the access
readonly func GPICheck(paspace : PASpace, gpi : bits(4), ss : SecurityState)
=> boolean
begin
case gpi of
when GPT_NoAccess =>
return FALSE;
when GPT_Secure =>
assert IsFeatureImplemented(FEAT_SEL2);
return paspace == PAS_Secure;
when GPT_NonSecure =>
return paspace == PAS_NonSecure;
when GPT_Root =>
return paspace == PAS_Root;
when GPT_Realm =>
return paspace == PAS_Realm;
when GPT_NonSecureOnly =>
assert IsFeatureImplemented(FEAT_RME_GPC2);
return paspace == PAS_NonSecure && (ss IN {SS_Root, SS_NonSecure});
when GPT_SystemAgent =>
assert IsFeatureImplemented(FEAT_RME_GDI);
return paspace == PAS_SystemAgent;
when GPT_NonSecureProtected =>
assert IsFeatureImplemented(FEAT_RME_GDI);
return paspace == PAS_NonSecureProtected;
when GPT_NA6 =>
assert IsFeatureImplemented(FEAT_RME_GDI);
return FALSE;
when GPT_NA7 =>
assert IsFeatureImplemented(FEAT_RME_GDI);
return FALSE;
when GPT_Any =>
return TRUE;
otherwise =>
unreachable;
end;
end;
// GPIIndex
// ==========
func GPIIndex(pa : bits(NUM_PABITS)) => integer
begin
case DecodePGS(GPCCR_EL3().PGS) of
when PGS_4KB => return UInt(pa[15:12]);
when PGS_16KB => return UInt(pa[17:14]);
when PGS_64KB => return UInt(pa[19:16]);
otherwise => unreachable;
end;
end;
// GPIValid
// ==========
// Returns whether a given value is a valid encoding for a GPI value
readonly func GPIValid(gpi : bits(4)) => boolean
begin
case gpi of
when GPT_NoAccess =>
return TRUE;
when GPT_NonSecureProtected =>
return IsFeatureImplemented(FEAT_RME_GDI) && GPCCR_EL3().NSP == '1';
when GPT_SystemAgent =>
return IsFeatureImplemented(FEAT_RME_GDI) && GPCCR_EL3().SA == '1';
when GPT_NA6 =>
return IsFeatureImplemented(FEAT_RME_GDI) && GPCCR_EL3().NA6 == '1';
when GPT_NA7 =>
return IsFeatureImplemented(FEAT_RME_GDI) && GPCCR_EL3().NA7 == '1';
when GPT_Secure =>
return IsFeatureImplemented(FEAT_SEL2);
when GPT_NonSecure =>
return TRUE;
when GPT_Realm =>
return TRUE;
when GPT_Root =>
return TRUE;
when GPT_NonSecureOnly =>
return IsFeatureImplemented(FEAT_RME_GPC2) && GPCCR_EL3().NSO == '1';
when GPT_Any =>
return TRUE;
otherwise =>
return FALSE;
end;
end;
// GPT dimensions
// ==============
constant GPTRange_4KB : AddressSize = 12;
constant GPTRange_16KB : AddressSize = 14;
constant GPTRange_64KB : AddressSize = 16;
constant GPTRange_2MB : AddressSize = 21;
constant GPTRange_32MB : AddressSize = 25;
constant GPTRange_512MB : AddressSize = 29;
constant GPTRange_1GB : AddressSize = 30;
constant GPTRange_16GB : AddressSize = 34;
constant GPTRange_64GB : AddressSize = 36;
constant GPTRange_512GB : AddressSize = 39;
// GPTBlockDescriptorValid
// =========================
// Returns TRUE if the given GPT Block descriptor is valid, and FALSE
otherwise.
func GPTBlockDescriptorValid(level_0_entry : bits(64)) => boolean
begin
assert level_0_entry[3:0] == GPT_Block;
return IsZero(level_0_entry[63:8]) && GPIValid(level_0_entry[7:4]);
end;
// GPTContigDescriptorValid
// ==========================
// Returns TRUE if the given GPT Contiguous descriptor is valid, and FALSE
otherwise.
readonly func GPTContigDescriptorValid(level_1_entry : bits(64), pgs : PGSe)
=> boolean
begin
assert level_1_entry[3:0] == GPT_Contig;
return (IsZero(level_1_entry[63:10]) &&
!IsZero(level_1_entry[9:8]) &&
GPIValid(level_1_entry[7:4]));
end;
// GPTEntry
// ========
type GPTEntry of record {
gpi : bits(4), // GPI value for this region
size : AddressSize, // Region size
contig_size : AddressSize, // Contiguous region size
istable : boolean, // Flag a table entry
level : integer, // Level of GPT lookup
pa : bits(NUM_PABITS) // PA uniquely identifying the GPT entry
};
// GPTGranulesDescriptorValid
// ============================
// Returns TRUE if the given GPT Granules descriptor is valid, and FALSE
otherwise.
readonly func GPTGranulesDescriptorValid(level_1_entry : bits(64)) =>
boolean
begin
for i = 0 to 15 do
if (level_1_entry[i*4 +:4]) then
return FALSE;
end;
end;
return TRUE;
end;
// GPTL0Size
// ===========
// Returns number of bits covered by a level 0 GPT entry
readonly func GPTL0Size() => AddressSize
begin
case GPCCR_EL3().L0GPTSZ of
when '0000' => return GPTRange_1GB;
when '0100' => return GPTRange_16GB;
when '0110' => return GPTRange_64GB;
when '1001' => return GPTRange_512GB;
otherwise => unreachable;
end;
return 30;
end;
// GPTLevel0EntryValid
// =====================
// Returns TRUE if the given level 0 gpt descriptor is valid, and FALSE
otherwise.
func GPTLevel0EntryValid(gpt_entry : bits(64)) => boolean
begin
case gpt_entry[3:0] of
when GPT_Block => return GPTBlockDescriptorValid(gpt_entry);
when GPT_Table => return GPTTableDescriptorValid(gpt_entry);
otherwise => return FALSE;
end;
end;
// GPTLevel0Index
// ================
// Compute the level 0 index based on input PA.
func GPTLevel0Index(pa : bits(NUM_PABITS)) => integer
begin
// Input address and index bounds
let pps : integer{} = DecodePPS();
let l0sz : integer{} = GPTL0Size();
if pps <= l0sz then
return 0;
end;
return UInt(pa[pps-1:l0sz]);
end;
// GPTLevel1EntryValid
// =====================
// Returns TRUE if the given level 1 gpt descriptor is valid, and FALSE
otherwise.
func GPTLevel1EntryValid(gpt_entry : bits(64), pgs : PGSe) => boolean
begin
case gpt_entry[3:0] of
when GPT_Contig => return GPTContigDescriptorValid(gpt_entry, pgs);
otherwise => return GPTGranulesDescriptorValid(gpt_entry);
end;
end;
// GPTLevel1Index
// ================
// Compute the level 1 index based on input PA.
func GPTLevel1Index(pa : bits(NUM_PABITS)) => integer
begin
// Input address and index bounds
let l0sz : integer{} = GPTL0Size();
case DecodePGS(GPCCR_EL3().PGS) of
when PGS_4KB => return UInt(pa[l0sz-1:16]);
when PGS_16KB => return UInt(pa[l0sz-1:18]);
when PGS_64KB => return UInt(pa[l0sz-1:20]);
otherwise => unreachable;
end;
end;
// GPTTable
// ========
type GPTTable of record {
address : bits(NUM_PABITS) // Base address of next table
};
// GPTTableDescriptorValid
// =========================
// Returns TRUE if the given GPT Table descriptor is valid, and FALSE
otherwise.
readonly func GPTTableDescriptorValid(level_0_entry : bits(64)) => boolean
begin
assert level_0_entry[3:0] == GPT_Table;
let l0sz : integer{} = GPTL0Size();
let pgs : PGSe = DecodePGS(GPCCR_EL3().PGS);
let p : integer{} = DecodePGSRange(pgs);
let top : integer{} = if DecodePPS() == 56 then 56 else 52;
return IsZero(level_0_entry[63:top,11:4]) && IsZero(level_0_entry[(l0sz-p)-2:12]);
end;
// GPTWalk
// =========
// Get the GPT entry for a given physical address, pa
func GPTWalk(pa : bits(NUM_PABITS), accdesc : AccessDescriptor) =>
(GPCFRecord, GPTEntry)
begin
// GPT base address
var base : bits(NUM_PABITS);
let pgs : PGSe = DecodePGS(GPCCR_EL3().PGS);
// The level 0 GPT base address is aligned to the greater of:
// * the size of the level 0 GPT, determined by GPCCR_EL3().[PPS, L0GPTSZ].
// * 4KB
base = ZeroExtend{NUM_PABITS}(GPTBR_EL3().BADDR::Zeros{12});
let pps : AddressSize = DecodePPS();
let l0sz : integer{} = GPTL0Size();
let alignment : integer{} = Max((pps - l0sz) + 3, 12) as AddressSize;
base[alignment-1:0] = Zeros{alignment};
let gptaccdesc : AccessDescriptor = CreateAccDescGPTW(accdesc);
// Access attributes and address for GPT fetches
var gptaddrdesc : AddressDescriptor;
gptaddrdesc.memattrs = WalkMemAttrs(GPCCR_EL3().SH, GPCCR_EL3().IRGN, GPCCR_EL3().ORGN);
gptaddrdesc.fault = NoFault(gptaccdesc);
gptaddrdesc.paddress.paspace = PAS_Root;
gptaddrdesc.paddress.address = base + GPTLevel0Index(pa) * 8;
// Fetch L0GPT entry
var level_0_entry : bits(64);
var memstatus : PhysMemRetStatus;
(memstatus, level_0_entry) = PhysMemRead{64}(gptaddrdesc, gptaccdesc);
if IsFault(memstatus) then
return (GPCFault(GPCF_EABT, 0), ARBITRARY : GPTEntry);
end;
if (level_0_entry) then
return (GPCFault(GPCF_Walk, 0), ARBITRARY : GPTEntry);
end;
var result : GPTEntry;
var table : GPTTable;
case level_0_entry[3:0] of
when GPT_Block =>
// Decode the GPI value and return that
result = DecodeGPTBlock(pgs, level_0_entry);
result.pa = pa;
return (GPCNoFault(), result);
when GPT_Table =>
// Decode the table entry and continue walking
table = DecodeGPTTable(pgs, level_0_entry);
// The address must be within the range covered by the GPT
if AbovePPS(table.address) then
return (GPCFault(GPCF_AddressSize, 0), ARBITRARY : GPTEntry);
end;
otherwise =>
// An invalid encoding would be caught by GPTLevel0EntryValid
unreachable;
end;
// Must be a GPT Table entry
assert level_0_entry[3:0] == GPT_Table;
// Address of level 1 GPT entry
let offset : integer = GPTLevel1Index(pa) * 8;
var level_1_entry : bits(64);
if IsFeatureImplemented(FEAT_RME_GDI) then
// When FEAT_RME_GDI is implemented, the descriptor validation checks are performed
// on a pair of descriptors within a naturally aligned 16-byte region of memory.
gptaddrdesc.paddress.address = AlignDownSize{56}(table.address + offset, 16);
var level_1_entry_lower : bits(64);
(memstatus, level_1_entry_lower) = PhysMemRead{64}(gptaddrdesc, gptaccdesc);
if IsFault(memstatus) then
return (GPCFault(GPCF_EABT, 1), ARBITRARY : GPTEntry);
end;
gptaddrdesc.paddress.address = gptaddrdesc.paddress.address + 8;
var level_1_entry_upper : bits(64);
(memstatus, level_1_entry_upper) = PhysMemRead{64}(gptaddrdesc, gptaccdesc);
if IsFault(memstatus) then
return (GPCFault(GPCF_EABT, 1), ARBITRARY : GPTEntry);
end;
// An individual GPT descriptor is valid when both descriptors within the pair are valid.
if ((level_1_entry_upper, pgs) ||
(level_1_entry_lower, pgs)) then
return (GPCFault(GPCF_Walk, 1), ARBITRARY : GPTEntry);
end;
if offset[3] == '1' then
level_1_entry = level_1_entry_upper;
else
level_1_entry = level_1_entry_lower;
end;
else
gptaddrdesc.paddress.address = table.address + offset;
// Fetch L1GPT entry
(memstatus, level_1_entry) = PhysMemRead{64}(gptaddrdesc, gptaccdesc);
if IsFault(memstatus) then
return (GPCFault(GPCF_EABT, 1), ARBITRARY : GPTEntry);
end;
if (level_1_entry, pgs) then
return (GPCFault(GPCF_Walk, 1), ARBITRARY : GPTEntry);
end;
end;
case level_1_entry[3:0] of
when GPT_Contig =>
result = DecodeGPTContiguous(pgs, level_1_entry);
otherwise =>
let gpi_index : integer = GPIIndex(pa);
result = DecodeGPTGranules(pgs, gpi_index, level_1_entry);
end;
result.pa = pa;
return (GPCNoFault(), result);
end;
// GranuleProtectionCheck
// ========================
// Returns whether a given access is permitted, according to the
// granule protection check.
// addrdesc and accdesc describe the access to be checked.
func GranuleProtectionCheck(addrdesc : AddressDescriptor, accdesc :
AccessDescriptor) => GPCFRecord
begin
assert IsFeatureImplemented(FEAT_RME);
// The address to be checked
let address = addrdesc.paddress;
// Bypass mode - all accesses pass
if GPCCR_EL3().GPC == '0' then
return GPCNoFault();
end;
// Configuration consistency check
if () then
return GPCFault(GPCF_Walk, 0);
end;
if IsFeatureImplemented(FEAT_RME_GPC2) then
var access_disabled : boolean;
case address.paspace of
when PAS_Secure => access_disabled = GPCCR_EL3().SPAD == '1';
when PAS_NonSecure => access_disabled = GPCCR_EL3().NSPAD == '1';
when PAS_Realm => access_disabled = GPCCR_EL3().RLPAD == '1';
when PAS_Root => access_disabled = FALSE;
otherwise => unreachable;
end;
if access_disabled then
return GPCFault(GPCF_Fail, 0);
end;
end;
// Input address size check
if AbovePPS(address.address) then
if (address.paspace == PAS_NonSecure ||
(IsFeatureImplemented(FEAT_RME_GPC2) && GPCCR_EL3().APPSAA == '1')) then
return GPCNoFault();
else
return GPCFault(GPCF_Fail, 0);
end;
end;
if (IsFeatureImplemented(FEAT_RME_GPC3) && GPCCR_EL3().GPCBW == '1' &&
PAWithinGPCBypassWindow(address.address)) then
return GPCNoFault();
end;
// GPT base address size check
let gpt_base : bits(NUM_PABITS) = ZeroExtend{}(GPTBR_EL3().BADDR::Zeros{12});
if AbovePPS(gpt_base) then
return GPCFault(GPCF_AddressSize, 0);
end;
// GPT lookup
var (gpcf, gpt_entry) = GPTWalk(address.address, accdesc);
if gpcf.gpf != GPCF_None then
return gpcf;
end;
// Check input physical address space against GPI
let permitted : boolean = GPICheck(address.paspace, gpt_entry.gpi, accdesc.ss);
if !permitted then
gpcf = GPCFault(GPCF_Fail, gpt_entry.level);
return gpcf;
end;
// Check passed
return GPCNoFault();
end;
// IsGranuleProtectionCheckedAccess
// ==================================
// Check if the access should be subject to Granule protection check returns
// true if it is, false otherwise
func IsGranuleProtectionCheckedAccess(accdesc : AccessDescriptor) =>
boolean
begin
if accdesc.acctype == AccessType_DC then
return ImpDefBool("GPC Fault on DC operations");
end;
return TRUE;
end;
// PAWithinGPCBypassWindow
// =========================
// Check if the supplied address is within a GPC Bypass window.
func PAWithinGPCBypassWindow(pa_in : bits(56)) => boolean
begin
// Only check the top 26 bits as the minimum window size is 1GB
let pa : bits(26) = pa_in[55:30];
let gpcbwl : integer{} = UInt(GPCBW_EL3().BWSIZE);
let gpcbwu : integer{} = 9 + UInt(GPCBW_EL3().BWSTRIDE);
return pa[gpcbwu:gpcbwl] == GPCBW_EL3().BWADDR[gpcbwu:gpcbwl];
end;
// PGS
// ===
// Physical granule size
type PGSe of enumeration {
PGS_4KB,
PGS_16KB,
PGS_64KB
};
// Table format information
// ========================
// Granule Protection Table constants
constant GPT_NoAccess : bits(4) = '0000';
constant GPT_Table : bits(4) = '0011';
constant GPT_Block : bits(4) = '0001';
constant GPT_Contig : bits(4) = '0001';
constant GPT_SystemAgent : bits(4) = '0100';
constant GPT_NonSecureProtected : bits(4) = '0101';
constant GPT_NA6 : bits(4) = '0110';
constant GPT_NA7 : bits(4) = '0111';
constant GPT_Secure : bits(4) = '1000';
constant GPT_NonSecure : bits(4) = '1001';
constant GPT_Root : bits(4) = '1010';
constant GPT_Realm : bits(4) = '1011';
constant GPT_NonSecureOnly : bits(4) = '1101';
constant GPT_Any : bits(4) = '1111';
// S1TranslationRegime
// =====================
// Stage 1 translation regime for the given Exception level
readonly func S1TranslationRegime(el : bits(2)) => bits(2)
begin
if el != EL0 then
return el;
elsif HaveEL(EL3) && ELUsingAArch32(EL3) && SCR().NS == '0' then
return EL3;
elsif IsFeatureImplemented(FEAT_VHE) && ELIsInHost(el) then
return EL2;
else
return EL1;
end;
end;
// S1TranslationRegime
// =====================
// Returns the Exception level controlling the current Stage 1 translation
regime. For the most
// part this is unused in code because the System register accessors
(SCTLR_ELx, etc.) implicitly
// return the correct value.
readonly func S1TranslationRegime() => bits(2)
begin
return S1TranslationRegime(PSTATE.EL);
end;
// AddressDescriptor
// =================
// Descriptor used to access the underlying memory array.
type AddressDescriptor of record {
fault : FaultRecord, // fault.statuscode indicates whether the address is valid
memattrs : MemoryAttributes,
paddress : FullAddress,
s1assured : boolean, // Stage 1 Assured Translation Property
s2fs1mro : boolean, // Stage 2 MRO permission for Stage 1
mecid : bits(16), // FEAT_MEC: Memory Encryption Context ID
vaddress : bits(64)
};
constant FINAL_LEVEL : integer = 3;
// ContiguousSize
// ================
// Return the number of entries log 2 marking a contiguous output range
readonly func ContiguousSize(d128 : bit, tgx : TGx, walkstate : TTWState)
=> integer
begin
if d128 == '1' then
case tgx of
when TGx_4KB =>
assert walkstate.level IN {1, 2, 3};
return if walkstate.level == 1 then 2 else 4;
when TGx_16KB =>
assert walkstate.level IN {1, 2, 3};
if walkstate.level == 1 then
return 2;
elsif walkstate.level == 2 then
return 4;
else
return 6;
end;
when TGx_64KB =>
assert walkstate.level IN {2, 3};
return if walkstate.level == 2 then 6 else 4;
end;
end;
case tgx of
when TGx_4KB =>
assert walkstate.level IN {1, 2, 3};
return 4;
when TGx_16KB =>
assert walkstate.level IN {2, 3};
return if walkstate.level == 2 then 5 else 7;
when TGx_64KB =>
assert walkstate.level IN {2, 3};
return 5;
end;
end;
// CreateAddressDescriptor
// =========================
// Set internal members for address descriptor type to valid values
func CreateAddressDescriptor(pa : FullAddress, memattrs : MemoryAttributes,
accdesc : AccessDescriptor) => AddressDescriptor
begin
var addrdesc : AddressDescriptor;
addrdesc.paddress = pa;
addrdesc.memattrs = memattrs;
addrdesc.fault = NoFault(accdesc);
return addrdesc;
end;
// CreateAddressDescriptor
// =========================
// Set internal members for address descriptor type to valid values
func CreateAddressDescriptor(va : bits(64), pa : FullAddress,
memattrs : MemoryAttributes,
accdesc : AccessDescriptor) => AddressDescriptor
begin
var addrdesc : AddressDescriptor;
addrdesc.paddress = pa;
addrdesc.vaddress = va;
addrdesc.memattrs = memattrs;
addrdesc.fault = NoFault(accdesc);
addrdesc.s1assured = FALSE;
return addrdesc;
end;
// CreateFaultyAddressDescriptor
// ===============================
// Set internal members for address descriptor type with values indicating
error
func CreateFaultyAddressDescriptor(fault : FaultRecord) =>
AddressDescriptor
begin
var addrdesc : AddressDescriptor;
addrdesc.vaddress = fault.vaddress;
addrdesc.fault = fault;
return addrdesc;
end;
// DecodePASpace
// ===============
// Decode the target PA Space
pure func DecodePASpace(nse2 : bit, nse : bit, ns : bit) => PASpace
begin
case nse2::nse::ns of
when '000' => return PAS_Secure;
when '001' => return PAS_NonSecure;
when '010' => return PAS_Root;
when '011' => return PAS_Realm;
when '100' => return PAS_SystemAgent;
when '101' => return PAS_NonSecureProtected;
when '110' => return PAS_NA6;
when '111' => return PAS_NA7;
otherwise => unreachable;
end;
end;
// DescriptorType
// ==============
// Translation table descriptor formats
type DescriptorType of enumeration {
DescriptorType_Table,
DescriptorType_Leaf,
DescriptorType_Invalid
};
// Domains
// =======
// Short-descriptor format Domains
constant Domain_NoAccess : bits(2) = '00';
constant Domain_Client : bits(2) = '01';
constant Domain_Manager : bits(2) = '11';
// FetchDescriptor
// =================
// Fetch a translation table descriptor
func FetchDescriptor{N : integer{32, 64, 128}}(ee : bit, walkaddress :
AddressDescriptor,
walkaccess : AccessDescriptor,
fault_in : FaultRecord) => (FaultRecord, bits(N))
begin
var descriptor : bits(N);
var fault : FaultRecord = fault_in;
if IsFeatureImplemented(FEAT_RME) then
fault.gpcf = GranuleProtectionCheck(walkaddress, walkaccess);
if fault.gpcf.gpf != GPCF_None then
fault.statuscode = Fault_GPCFOnWalk;
fault.paddress = walkaddress.paddress;
fault.gpcfs2walk = fault.secondstage;
return (fault, ARBITRARY : bits(N));
end;
end;
var memstatus : PhysMemRetStatus;
(memstatus, descriptor) = PhysMemRead{N}(walkaddress, walkaccess);
if IsFault(memstatus) then
let iswrite : boolean = FALSE;
fault = HandleExternalTTWAbort(memstatus, iswrite, walkaddress,
walkaccess, N DIV 8, fault);
if IsFault(fault.statuscode) then
return (fault, ARBITRARY : bits(N));
end;
end;
if ee == '1' then
descriptor = BigEndianReverse{N}(descriptor);
end;
return (fault, descriptor);
end;
// HasUnprivileged
// =================
// Returns whether a translation regime serves EL0 as well as a higher EL
readonly func HasUnprivileged(regime : Regime) => boolean
begin
return (regime IN {
Regime_EL20,
Regime_EL30,
Regime_EL10
});
end;
// NUM_PABITS
// ==========
constant NUM_PABITS : integer{} = 56;
// Regime
// ======
// Translation regimes
type Regime of enumeration {
Regime_EL3, // EL3
Regime_EL30, // EL3&0 (PL1&0 when EL3 is AArch32)
Regime_EL2, // EL2
Regime_EL20, // EL2&0
Regime_EL10 // EL1&0
};
// RegimeUsingAArch32
// ====================
// Determine if the EL controlling the regime executes in AArch32 state
func RegimeUsingAArch32(regime : Regime) => boolean
begin
case regime of
when Regime_EL10 => return ELUsingAArch32(EL1);
when Regime_EL30 => return TRUE;
when Regime_EL20 => return FALSE;
when Regime_EL2 => return ELUsingAArch32(EL2);
when Regime_EL3 => return FALSE;
end;
end;
// S1TTWParams
// ===========
// Register fields corresponding to stage 1 translation
// For A32-VMSA, if noted, they correspond to A32-LPAE (Long descriptor
format)
type S1TTWParams of record {
// A64-VMSA exclusive parameters
ha : bit, // TCR_ELx.HA
hd : bit, // TCR_ELx.HD
tbi : bit, // TCR_ELx.TBI{x}
tbid : bit, // TCR_ELx.TBID{x}
nfd : bit, // TCR_EL1.NFDx or TCR_EL2.NFDx when HCR_EL2.E2H == '1'
e0pd : bit, // TCR_EL1.E0PDx or TCR_EL2.E0PDx when HCR_EL2.E2H == '1'
d128 : bit, // TCR_ELx.D128
aie : bit, // (TCR2_ELx/TCR_EL3).AIE
mair2 : MAIRType, // MAIR2_ELx
ds : bit, // TCR_ELx.DS
ps : bits(3), // TCR_ELx.{I}PS
txsz : bits(6), // TCR_ELx.TxSZ
epan : bit, // SCTLR_EL1.EPAN or SCTLR_EL2.EPAN when HCR_EL2.E2H == '1'
dct : bit, // HCR_EL2.DCT
nv1 : bit, // HCR_EL2.NV1
cmow : bit, // SCTLR_EL1.CMOW or SCTLR_EL2.CMOW when HCR_EL2.E2H == '1'
pnch : bit, // TCR{2}_ELx.PnCH
disch : bit, // TCR{2}_ELx.DisCH
haft : bit, // TCR{2}_ELx.HAFT
mtx : bit, // TCR_ELx.MTX{y}
skl : bits(2), // TTBRn_ELx.SKL
pie : bit, // TCR2_ELx.PIE or TCR_EL3.PIE
pir : S1PIRType, // PIR_ELx
pire0 : S1PIRType,// PIRE0_EL1 or PIRE0_EL2 when HCR_EL2.E2H == '1'
emec : bit, // SCTLR2_EL2.EMEC or SCTLR2_EL3.EMEC
amec : bit, // TCR2_EL2.AMEC0 or TCR2_EL2.AMEC1 when HCR_EL2.E2H == '1'
fng : bit, // TCR2_EL1.FNGx or TCR2_EL2.FNGx when HCR_EL2.E2H == '1'
fngna : bit, // TCR2_EL1.FNGx
// A32-VMSA exclusive parameters
t0sz : bits(3), // TTBCR.T0SZ
t1sz : bits(3), // TTBCR.T1SZ
uwxn : bit, // SCTLR.UWXN
// Parameters common to both A64-VMSA & A32-VMSA (A64/A32)
tgx : TGx, // TCR_ELx.TGx / Always TGx_4KB
irgn : bits(2), // TCR_ELx.IRGNx / TTBCR.IRGNx or HTCR.IRGN0
orgn : bits(2), // TCR_ELx.ORGNx / TTBCR.ORGNx or HTCR.ORGN0
sh : bits(2), // TCR_ELx.SHx / TTBCR.SHx or HTCR.SH0
hpd : bit, // TCR_ELx.HPD{x} / TTBCR2.HPDx or HTCR.HPD
ee : bit, // SCTLR_ELx.EE / SCTLR.EE or HSCTLR.EE
wxn : bit, // SCTLR_ELx.WXN / SCTLR.WXN or HSCTLR.WXN
ntlsmd : bit, // SCTLR_ELx.nTLSMD / SCTLR.nTLSMD or HSCTLR.nTLSMD
dc : bit, // HCR_EL2.DC / HCR.DC
sif : bit, // SCR_EL3.SIF / SCR.SIF
mair : MAIRType // MAIR_ELx / MAIR1:MAIR0 or HMAIR1:HMAIR0
};
// S2TTWParams
// ===========
// Register fields corresponding to stage 2 translation.
type S2TTWParams of record {
// A64-VMSA exclusive parameters
ha : bit, // VTCR_EL2.HA
hd : bit, // VTCR_EL2.HD
sl2 : bit, // V{S}TCR_EL2.SL2
ds : bit, // VTCR_EL2.DS
d128 : bit, // VTCR_ELx.D128
sw : bit, // VSTCR_EL2.SW
nsw : bit, // VTCR_EL2.NSW
sa : bit, // VSTCR_EL2.SA
nsa : bit, // VTCR_EL2.NSA
ps : bits(3), // VTCR_EL2.PS
txsz : bits(6), // V{S}TCR_EL2.T0SZ
fwb : bit, // HCR_EL2.FWB
cmow : bit, // HCRX_EL2.CMOW
skl : bits(2), // VTTBR_EL2.SKL
s2pie : bit, // VTCR_EL2.S2PIE
s2pir : S2PIRType, // S2PIR_EL2
tl0 : bit, // VTCR_EL2.TL0
tl1 : bit, // VTCR_EL2.TL1
assuredonly : bit, // VTCR_EL2.AssuredOnly
haft : bit, // VTCR_EL2.HAFT
emec : bit, // SCTLR2_EL2.EMEC
hdbss : bit, // VTCR_EL2.HDBSS
// A32-VMSA exclusive parameters
s : bit, // VTCR.S
t0sz : bits(4), // VTCR.T0SZ
// Parameters common to both A64-VMSA & A32-VMSA if implemented (A64/A32)
tgx : TGx, // V{S}TCR_EL2.TG0 / Always TGx_4KB
sl0 : bits(2), // V{S}TCR_EL2.SL0 / VTCR.SL0
irgn : bits(2), // VTCR_EL2.IRGN0 / VTCR.IRGN0
orgn : bits(2), // VTCR_EL2.ORGN0 / VTCR.ORGN0
sh : bits(2), // VTCR_EL2.SH0 / VTCR.SH0
ee : bit, // SCTLR_EL2.EE / HSCTLR.EE
ptw : bit, // HCR_EL2.PTW / HCR.PTW
vm : bit // HCR_EL2.VM / HCR.VM
};
// SDFType
// =======
// Short-descriptor format type
type SDFType of enumeration {
SDFType_Table,
SDFType_Invalid,
SDFType_Supersection,
SDFType_Section,
SDFType_LargePage,
SDFType_SmallPage
};
// SecurityStateForRegime
// ========================
// Return the Security State of the given translation regime
func SecurityStateForRegime(regime : Regime) => SecurityState
begin
case regime of
when Regime_EL3 => return SecurityStateAtEL(EL3);
when Regime_EL30 => return SS_Secure; // A32 EL3 is always Secure
when Regime_EL2 => return SecurityStateAtEL(EL2);
when Regime_EL20 => return SecurityStateAtEL(EL2);
when Regime_EL10 => return SecurityStateAtEL(EL1);
end;
end;
// Stage1OA
// ==========
// Given the final walk state (a page or block descriptor), map the
untranslated
// input address bits to the output address
func Stage1OA(ia : bits(64), walkparams : S1TTWParams, walkstate : TTWState)
=> FullAddress
begin
// Output Address
var oa : FullAddress;
let tsize : integer = TranslationSize(walkparams.d128, walkparams.tgx, walkstate.level);
var csize : integer;
if walkstate.contiguous == '1' then
csize = ContiguousSize(walkparams.d128, walkparams.tgx, walkstate);
else
csize = 0;
end;
let ia_msb : AddressSize = (tsize + csize) as AddressSize;
oa.paspace = walkstate.baseaddress.paspace;
oa.address = walkstate.baseaddress.address[NUM_PABITS-1:ia_msb]::ia[ia_msb-1:0];
return oa;
end;
// Stage2OA
// ==========
// Given the final walk state (a page or block descriptor), map the
untranslated
// IPA bits to the output PA.
func Stage2OA(ipa : bits(NUM_PABITS), walkparams : S2TTWParams, walkstate :
TTWState) => FullAddress
begin
// Output Address
var oa : FullAddress;
let tsize : integer = TranslationSize(walkparams.d128, walkparams.tgx, walkstate.level);
var csize : integer;
if walkstate.contiguous == '1' then
csize = ContiguousSize(walkparams.d128, walkparams.tgx, walkstate);
else
csize = 0;
end;
let ipa_msb : AddressSize = (tsize + csize) as AddressSize;
oa.paspace = walkstate.baseaddress.paspace;
oa.address = walkstate.baseaddress.address[NUM_PABITS-1:ipa_msb]::ipa[ipa_msb-1:0];
return oa;
end;
// TGx
// ===
// Translation granules sizes
type TGx of enumeration {
TGx_4KB,
TGx_16KB,
TGx_64KB
};
// TGxGranuleBits
// ================
// Retrieve the address size, in bits, of a granule
pure func TGxGranuleBits(tgx : TGx) => AddressSize
begin
case tgx of
when TGx_4KB => return 12;
when TGx_16KB => return 14;
when TGx_64KB => return 16;
end;
end;
// TLBContext
// ==========
// Translation context compared on TLB lookups and invalidations, promoting a
TLB hit on match
type TLBContext of record {
ss : SecurityState,
regime : Regime,
vmid : bits(NUM_VMIDBITS),
asid : bits(NUM_ASIDBITS),
nG : bit,
ipaspace : PASpace, // Used in stage 2 lookups & invalidations only
includes_s1 : boolean,
includes_s2 : boolean,
use_vmid : boolean,
includes_gpt : boolean,
ia : bits(64), // Input Address
tg : TGx,
cnp : bit,
level : integer, // Assist TLBI level hints (FEAT_TTL)
isd128 : boolean,
xs : bit // XS attribute (FEAT_XS)
};
// TLBRecord
// =========
// Translation output as a TLB payload
type TLBRecord of record {
context : TLBContext,
walkstate : TTWState,
blocksize : AddressSize, // Number of bits directly mapped from IA to OA
contigsize : integer, // Number of entries log 2 marking a contiguous output range
s1descriptor : bits(128), // Stage 1 leaf descriptor in memory (valid if the TLB caches stage 1)
s2descriptor : bits(128) // Stage 2 leaf descriptor in memory (valid if the TLB caches stage 2)
};
// TTWState
// ========
// Translation table walk state
type TTWState of record {
istable : boolean,
level : integer,
baseaddress : FullAddress,
contiguous : bit,
s1assured : boolean, // Stage 1 Assured Translation Property
s2assuredonly : bit, // Stage 2 AssuredOnly attribute
disch : bit, // Stage 1 Disable Contiguous Hint
nG : bit,
guardedpage : bit,
sdftype : SDFType, // AArch32 Short-descriptor format walk only
domain : bits(4), // AArch32 Short-descriptor format walk only
memattrs : MemoryAttributes,
permissions : Permissions
};
// TranslationRegime
// ===================
// Select the translation regime given the target EL and PE state
readonly func TranslationRegime(el : bits(2)) => Regime
begin
if el == EL3 then
return if ELUsingAArch32(EL3) then Regime_EL30 else Regime_EL3;
elsif el == EL2 then
return if ELIsInHost(EL2) then Regime_EL20 else Regime_EL2;
elsif el == EL1 then
return Regime_EL10;
elsif el == EL0 then
if CurrentSecurityState() == SS_Secure && ELUsingAArch32(EL3) then
return Regime_EL30;
elsif ELIsInHost(EL0) then
return Regime_EL20;
else
return Regime_EL10;
end;
else
unreachable;
end;
end;
// TranslationSize
// =================
// Compute the number of bits directly mapped from the input address
// to the output address
func TranslationSize(d128 : bit, tgx : TGx, level : integer) => AddressSize
begin
let granulebits : AddressSize = TGxGranuleBits(tgx);
let descsizelog2 : integer{} = if d128 == '1' then 4 else 3;
let blockbits : integer = (FINAL_LEVEL - level) * (granulebits - descsizelog2);
return (granulebits + blockbits) as AddressSize;
end;
// UseASID
// =========
// Determine whether the translation context for the access requires ASID or
is a global entry
func UseASID(accesscontext : TLBContext) => boolean
begin
return HasUnprivileged(accesscontext.regime);
end;
// UseVMID
// =========
// Determine whether the translation context for the access requires VMID to
match a TLB entry
func UseVMID(regime : Regime) => boolean
begin
return regime == Regime_EL10 && EL2Enabled();
end;
------------------------------------------------------------------------------
Page 1 of 1.
End of thread.