[Writeup] Shared pseudocode: part 10 of 36 ========================================== Section: Silicon & Architectures Started: 2026-11-28 17:44 UTC by admin Replies: 0, last post 2026-11-28 17:44 UTC Page: 1 of 1 Web: https://romdump.net/t/102 ------------------------------------------------------------------------------ #122 admin, 2026-11-28 17:44 UTC [OP] Source: https://romdump.net/raw/122

Shared pseudocode from the offline Arm documentation snapshot. Part 10 of 36.

 AArch64_TakeException(PSTATE.EL, except,
preferred_exception_return, vect_offset);
    elsif route_to_el2 then
        AArch64_TakeException(EL2, except, preferred_exception_return, vect_offset);
    else
        AArch64_TakeException(EL1, except, preferred_exception_return, vect_offset);
    end;
end;

end;


## Library pseudocode for aarch64/exceptions/traps/AArch64\_MonitorModeTrap

// AArch64_MonitorModeTrap // ========================= // Trapped use of Monitor mode features in a Secure EL1 AArch32 mode

func AArch64_MonitorModeTrap begin let preferred_exception_return : bits(64) = ThisInstrAddr{}(); let vect_offset : integer = 0x0;

let except : ExceptionRecord =
ExceptionSyndrome(Exception_Uncategorized);

if IsSecureEL2Enabled() then
    AArch64_TakeException(EL2, except, preferred_exception_return, vect_offset);
end;
AArch64_TakeException(EL3, except, preferred_exception_return, vect_offset);

end;


## Library pseudocode for aarch64/exceptions/traps/AArch64\_OtherInstrTrap

// AArch64_OtherInstrTrap // ======================== // Trapped access to the following instructions: // - LD64B // - ST64B // - ST64BV // - ST64BV0 // - PSB // - TSB

func AArch64_OtherInstrTrap(target_el : bits(2), iss : bits(25)) begin let preferred_exception_return : bits(64) = ThisInstrAddr{}(); let vect_offset : integer = 0x0;

var except : ExceptionRecord =
ExceptionSyndrome(Exception_OtherTrap);
except.syndrome.iss = iss;
AArch64_TakeException(target_el, except, preferred_exception_return,
vect_offset);

end;


## Library pseudocode for aarch64/exceptions/traps/AArch64\_SystemAccessTrap

// AArch64_SystemAccessTrap // ========================== // Trapped access to AArch64 System register or system instruction.

func AArch64_SystemAccessTrap(target_el : bits(2), ec : integer) begin assert HaveEL(target_el) && target_el != EL0 && UInt(target_el) >= UInt(PSTATE.EL);

let preferred_exception_return : bits(64) = ThisInstrAddr{}();
let vect_offset : integer = 0x0;

let except : ExceptionRecord = AArch64_SystemAccessTrapSyndrome(ThisInstr(),
ec);
AArch64_TakeException(target_el, except, preferred_exception_return,
vect_offset);

end;


## Library pseudocode for
aarch64/exceptions/traps/AArch64\_SystemAccessTrapSyndrome

// AArch64_SystemAccessTrapSyndrome // ================================== // Returns the syndrome information for traps on AArch64 MSR/MRS instructions.

func AArch64_SystemAccessTrapSyndrome(instr_in : bits(32), ec : integer) => ExceptionRecord begin var except : ExceptionRecord; var instr : bits(32) = instr_in; case ec of when 0x0 => // Trapped access due to unknown reason. except = ExceptionSyndrome(Exception_Uncategorized); when 0x7 => // Trapped access to SVE, Advance SIMD&FP System register. except = ExceptionSyndrome(Exception_AdvSIMDFPAccessTrap); except.syndrome.iss[24:20] = ConditionSyndrome(); when 0x14 => // Trapped access to 128-bit System register or // 128-bit System instruction. except = ExceptionSyndrome(Exception_SystemRegister128Trap); instr = ThisInstr(); except.syndrome.iss[21:20] = instr[20:19]; // Op0 except.syndrome.iss[19:17] = instr[7:5]; // Op2 except.syndrome.iss[16:14] = instr[18:16]; // Op1 except.syndrome.iss[13:10] = instr[15:12]; // CRn except.syndrome.iss[9:6] = instr[4:1]; // Rt except.syndrome.iss[4:1] = instr[11:8]; // CRm except.syndrome.iss[0] = instr[21]; // Direction when 0x18 => // Trapped access to System register or system instruction. except = ExceptionSyndrome(Exception_SystemRegisterTrap); instr = ThisInstr(); except.syndrome.iss[21:20] = instr[20:19]; // Op0 except.syndrome.iss[19:17] = instr[7:5]; // Op2 except.syndrome.iss[16:14] = instr[18:16]; // Op1 except.syndrome.iss[13:10] = instr[15:12]; // CRn except.syndrome.iss[9:5] = instr[4:0]; // Rt except.syndrome.iss[4:1] = instr[11:8]; // CRm except.syndrome.iss[0] = instr[21]; // Direction when 0x19 => // Trapped access to SVE System register except = ExceptionSyndrome(Exception_SVEAccessTrap); when 0x1D => // Trapped access to SME System register except = ExceptionSyndrome(Exception_SMEAccessTrap); otherwise => unreachable; end;

return except;

end;


## Library pseudocode for
aarch64/exceptions/traps/AArch64\_SystemAccessTraptoEL1orEL2

// AArch64_SystemAccessTraptoEL1orEL2 // ==================================== // Trapped access to AArch64 System register or system instruction from EL0. // The trap is taken to EL1, or to EL2 if HCR_EL2().TGE is 1.

func AArch64_SystemAccessTraptoEL1orEL2(ec : integer) begin assert PSTATE.EL == EL0; let target_el : bits(2) = if EL2Enabled() && HCR_EL2().TGE == '1' then EL2 else EL1;

AArch64_SystemAccessTrap(target_el, ec);

end;


## Library pseudocode for aarch64/exceptions/traps/AArch64\_Undefined

// AArch64_Undefined // ===================

noreturn func AArch64_Undefined begin

let route_to_el2 : boolean = PSTATE.EL == EL0 &&
EL2Enabled() && HCR_EL2().TGE == '1';
let preferred_exception_return : bits(64) = ThisInstrAddr{}();
let vect_offset : integer = 0x0;

let except : ExceptionRecord = ExceptionSyndrome(Exception_Uncategorized);

if UInt(PSTATE.EL) > UInt(EL1) then
    AArch64_TakeException(PSTATE.EL, except, preferred_exception_return, vect_offset);
elsif route_to_el2 then
    AArch64_TakeException(EL2, except, preferred_exception_return, vect_offset);
else
    AArch64_TakeException(EL1, except, preferred_exception_return, vect_offset);
end;

end;


## Library pseudocode for aarch64/exceptions/traps/AArch64\_WFxTrap

// AArch64_WFxTrap // ================= // Generate an exception for a trapped WFE, WFI, WFET or WFIT instruction.

func AArch64_WFxTrap(wfxtype : WFxType, target_el : bits(2)) begin assert UInt(target_el) > UInt(PSTATE.EL); let preferred_exception_return : bits(64) = ThisInstrAddr{}(); let vect_offset : integer = 0x0; var except : ExceptionRecord = ExceptionSyndrome(Exception_WFxTrap); except.syndrome.iss[24:20] = ConditionSyndrome(); case wfxtype of when WFxType_WFI => except.syndrome.iss[1:0] = '00'; when WFxType_WFE => except.syndrome.iss[1:0] = '01'; when WFxType_WFIT => except.syndrome.iss[1:0] = '10'; except.syndrome.iss[2] = '1'; // Register field is valid except.syndrome.iss[9:5] = ThisInstr()[4:0]; when WFxType_WFET => except.syndrome.iss[1:0] = '11'; except.syndrome.iss[2] = '1'; // Register field is valid except.syndrome.iss[9:5] = ThisInstr()[4:0]; end;

if target_el == EL1 && EL2Enabled() &&
HCR_EL2().TGE == '1' then
    AArch64_TakeException(EL2, except, preferred_exception_return, vect_offset);
else
    AArch64_TakeException(target_el, except, preferred_exception_return, vect_offset);
end;

end;


## Library pseudocode for aarch64/exceptions/traps/CheckLDST64BEnabled

// CheckLDST64BEnabled // ===================== // Checks for trap on ST64B, LD64B, STL64B, and LDA64B instructions

func CheckLDST64BEnabled(acqrel : boolean) begin var trap : boolean = FALSE; let iss : bits(25) = if acqrel then 0x8[24:0] else 0x2[24:0]; var target_el : bits(2);

if PSTATE.EL == EL0 then
    if () then
        trap = SCTLR_EL1().EnALS == '0';
        target_el = if EL2Enabled() && HCR_EL2().TGE == '1' then EL2 else EL1;
    else
        trap = SCTLR_EL2().EnALS == '0';
        target_el = EL2;
    end;
else
    target_el = EL1;
end;

if !trap && EL2Enabled() && ((PSTATE.EL == EL0 && ())
|| PSTATE.EL == EL1) then
    trap = () || HCRX_EL2().EnALS == '0';
    target_el = EL2;
end;

if trap then AArch64_OtherInstrTrap(target_el, iss); end;

end;


## Library pseudocode for aarch64/exceptions/traps/CheckST64BV0Enabled

// CheckST64BV0Enabled // ===================== // Checks for trap on ST64BV0 and STL64BV0 instructions

func CheckST64BV0Enabled(acqrel : boolean) begin var trap : boolean = FALSE; let iss : bits(25) = if acqrel then 0x7[24:0] else 0x1[24:0]; var target_el : bits(2);

if PSTATE.EL != EL3 && HaveEL(EL3) &&
SCR_EL3().EnAS0 == '0' && EL3SDDUndefPriority() then
    Undefined();
end;

if PSTATE.EL == EL0 then
    if () then
        trap = SCTLR_EL1().EnAS0 == '0';
        target_el = if EL2Enabled() && HCR_EL2().TGE == '1' then EL2 else EL1;
    else
        trap = SCTLR_EL2().EnAS0 == '0';
        target_el = EL2;
    end;
end;

if !trap && EL2Enabled() && ((PSTATE.EL == EL0 && ())
|| PSTATE.EL == EL1) then
    trap = () || HCRX_EL2().EnAS0 == '0';
    target_el = EL2;
end;

if !trap && PSTATE.EL != EL3 then
    trap = HaveEL(EL3) && SCR_EL3().EnAS0 == '0';
    target_el = EL3;
end;

if trap then
    if target_el == EL3 && EL3SDDUndef() then
        Undefined();
    else
        AArch64_OtherInstrTrap(target_el, iss);
    end;
end;

end;


## Library pseudocode for aarch64/exceptions/traps/CheckST64BVEnabled

// CheckST64BVEnabled // ==================== // Checks for trap on ST64BV and STL64BV instructions

func CheckST64BVEnabled(acqrel : boolean) begin var trap : boolean = FALSE; let iss : bits(25) = if acqrel then 0x6[24:0] else 0x0[24:0]; var target_el : bits(2);

if PSTATE.EL == EL0 then
    if () then
        trap = SCTLR_EL1().EnASR == '0';
        target_el = if EL2Enabled() && HCR_EL2().TGE == '1' then EL2 else EL1;
    else
        trap = SCTLR_EL2().EnASR == '0';
        target_el = EL2;
    end;
end;

if !trap && EL2Enabled() && ((PSTATE.EL == EL0 && ())
|| PSTATE.EL == EL1) then
    trap = () || HCRX_EL2().EnASR == '0';
    target_el = EL2;
end;

if trap then AArch64_OtherInstrTrap(target_el, iss); end;

end;


## Library pseudocode for aarch64/exceptions/traps/WFETrapDelay

// WFETrapDelay // ============== // Returns TRUE when delay in trap to WFE is enabled with value to amount of delay, // FALSE otherwise.

func WFETrapDelay(target_el : bits(2)) => (boolean, integer) begin var delay_enabled : boolean; var delay : integer; case target_el of when EL1 => if () then delay_enabled = SCTLR_EL1().TWEDEn == '1'; delay = 1 << (UInt(SCTLR_EL1().TWEDEL) + 8); else delay_enabled = SCTLR_EL2().TWEDEn == '1'; delay = 1 << (UInt(SCTLR_EL2().TWEDEL) + 8); end; when EL2 => assert EL2Enabled(); delay_enabled = HCR_EL2().TWEDEn == '1'; delay = 1 << (UInt(HCR_EL2().TWEDEL) + 8); when EL3 => delay_enabled = SCR_EL3().TWEDEn == '1'; delay = 1 << (UInt(SCR_EL3().TWEDEL) + 8); end; return (delay_enabled, delay); end;


## Library pseudocode for aarch64/exceptions/traps/WaitForEventUntilDelay

// WaitForEventUntilDelay // ======================== // Returns TRUE if WaitForEvent() returns before WFE trap delay expires, including potentially // some IMPLEMENTATION SPECIFIC additional delay, FALSE otherwise.

impdef func WaitForEventUntilDelay(delay_enabled : boolean, delay : integer) => boolean begin return FALSE; end;


## Library pseudocode for aarch64/functions/aborts/AArch64\_FaultSyndrome

// AArch64_FaultSyndrome // ======================= // Creates an exception syndrome value and updates the virtual address for Abort and Watchpoint // exceptions taken to an Exception level using AArch64.

func AArch64_FaultSyndrome(exceptype : Exception, fault : FaultRecord, target_el : bits(2)) => IssType begin assert fault.statuscode != Fault_None;

var isstype : IssType;
isstype.iss  = Zeros{25};
isstype.iss2 = Zeros{24};

let d_side : boolean = exceptype IN {Exception_DataAbort,
Exception_NV2DataAbort,
                                     Exception_Watchpoint, Exception_NV2Watchpoint};
if IsFeatureImplemented(FEAT_RAS) && fault.statuscode ==
Fault_SyncExternal then
    let errstate : ErrorState = PEErrorState(fault);
    isstype.iss[12:11] = AArch64_EncodeSyncErrorSyndrome(errstate);  // SET
end;

if d_side then
    if (IsFeatureImplemented(FEAT_EAESR) && exceptype == Exception_DataAbort &&
          !fault.s2fs1walk && ! fault.accessdesc.acctype IN {AccessType_DC,
                                                             AccessType_IC,
                                                             AccessType_AT,
                                                             AccessType_NV2}) then
        // LST2 field
        if fault.accessdesc.atomicop then
            isstype.iss2[15:13] = '001';    // Data Abort is from an Atomic instruction
        elsif (fault.accessdesc.acctype == AccessType_DCZero &&
                 fault.accessdesc.cachetype IN {CacheType_Data, CacheType_Data_Tag}) then
            isstype.iss2[15:13] = '010';    // Data Abort is from a DC ZVA or DC GZVA
        end;

        // Unpriv field
        if (fault.accessdesc.el == EL0 && PSTATE.EL != EL0 &&
              fault.accessdesc.acctype IN {AccessType_GPR,
                                           AccessType_GCS,
                                           AccessType_ASIMD}) then
            isstype.iss2[12] = '1';         // Data Abort is from an unprivileged load or store
        end;
    end;

    if fault.accessdesc.acctype == AccessType_GCS then
        isstype.iss2[8] = '1';
    end;
    if exceptype IN {Exception_Watchpoint, Exception_NV2Watchpoint} then
        isstype.iss[23:0] = WatchpointRelatedSyndrome(fault);
    end;

    if AArch64_InstructionSyndromeValid(fault) then
        if fault.accessdesc.ls64 then
            (isstype.iss2, isstype.iss[24:14]) = LS64InstructionSyndrome();
        else
            isstype.iss[24:14] = LSInstructionSyndrome();
        end;
    end;

    if IsFeatureImplemented(FEAT_NV2) && fault.accessdesc.acctype == AccessType_NV2 then
        isstype.iss[13] = '1';   // Fault is generated by use of VNCR_EL2
    end;

    if (IsFeatureImplemented(FEAT_LS64) &&
          fault.statuscode IN {Fault_AccessFlag, Fault_Translation, Fault_Permission}) then
        isstype.iss[12:11] = GetLoadStoreType();
    end;

    if fault.accessdesc.acctype IN {AccessType_DC, AccessType_IC, AccessType_AT} then
        isstype.iss[8] = '1';
    end;

    if fault.accessdesc.acctype IN {AccessType_DC, AccessType_IC, AccessType_AT} then
        isstype.iss[6] = '1';
    elsif fault.statuscode IN {Fault_UnsupportedAtomicHWUpdate, Fault_Exclusive} then
        isstype.iss[6] = ARBITRARY : bit;
    elsif fault.accessdesc.atomicop && IsExternalAbort(fault) then
        isstype.iss[6] = ARBITRARY : bit;
    else
        isstype.iss[6] = if fault.write then '1' else '0';
    end;
    isstype.iss2[9] = if fault.tagaccess then '1' else '0';
    if exceptype IN {Exception_DataAbort, Exception_NV2DataAbort} then
        isstype.iss2[5] = if fault.dirtybit then '1' else '0';
        isstype.iss2[6] = if fault.overlay then '1' else '0';
        if isstype.iss[24] == '0' then
            isstype.iss[21] = if fault.toplevel then '1' else '0';
        end;
        isstype.iss2[7] = if fault.assuredonly then '1' else '0';
        isstype.iss2[10] = if fault.s1tagnotdata then '1' else '0';
    end;

else
    if fault.accessdesc.acctype == AccessType_IFETCH then
        isstype.iss2[5] = if fault.dirtybit then '1' else '0';
        isstype.iss[21] = if fault.toplevel then '1' else '0';
        isstype.iss2[7] = if fault.assuredonly then '1' else '0';
        isstype.iss2[6] = if fault.overlay then '1' else '0';
    end;
end;
if fault.hdbssf then
    isstype.iss2[11] = '1';
end;

if (IsFeatureImplemented(FEAT_PFAR) && IsExternalSyncAbort(fault)
&&
      !(EL2Enabled() &&
          (HCR_EL2().VM == '1' || (IsFeatureImplemented(FEAT_HCRMAC) && HCR_EL2().DC == '1')) &&
          target_el == EL1)) then
    isstype.iss[14] = if IsPFARValid(fault) then '1' else '0';
end;

if IsExternalAbort(fault) then isstype.iss[9] = fault.extflag; end;
isstype.iss[7] = if fault.s2fs1walk then '1' else '0';
isstype.iss[5:0] = EncodeLDFSC(fault.statuscode, fault.level);

return isstype;

end;


## Library pseudocode for
aarch64/functions/aborts/AArch64\_InstructionSyndromeValid

// AArch64_InstructionSyndromeValid // ================================== // Returns TRUE if ESR_ELx.ISV is '1' for the given Fault.

readonly func AArch64_InstructionSyndromeValid(fault : FaultRecord) => boolean begin if !fault.accessdesc.lowestaddress then return FALSE; end;

if fault.s2fs1walk then
    return FALSE;
end;

if IsFeatureImplemented(FEAT_RAS) && IsExternalSyncAbort(fault) then
    return FALSE;
end;

if fault.accessdesc.ls64 then
    return fault.statuscode IN {Fault_AccessFlag, Fault_Translation, Fault_Permission};
end;

if fault.accessdesc.acctype == AccessType_GCS then
    return FALSE;
end;

if (IsFeatureImplemented(FEAT_EAESR) && !IsSecondStage(fault)
&&
      fault.statuscode IN {Fault_AccessFlag, Fault_Translation, Fault_Permission}) then
    return TRUE;
end;

if IsSecondStage(fault) then
    return (!IsExternalAbortOnWalk(fault) ||
            ImpDefBool("ISV on second stage translation table walk"));
end;

return FALSE;

end;


## Library pseudocode for aarch64/functions/aborts/EncodeGPCSC

// EncodeGPCSC // ============= // Function that gives the GPCSC code for types of GPT Fault

func EncodeGPCSC(gpcf : GPCFRecord) => bits(6) begin assert gpcf.level IN {0,1};

case gpcf.gpf of
    when GPCF_AddressSize => return '00000'::gpcf.level[0];
    when GPCF_Walk =>        return '00010'::gpcf.level[0];
    when GPCF_Fail =>        return '00110'::gpcf.level[0];
    when GPCF_EABT =>        return '01010'::gpcf.level[0];
end;

end;


## Library pseudocode for aarch64/functions/aborts/LS64InstructionSyndrome

// LS64InstructionSyndrome // ========================= // Returns the syndrome information and LST for a Data Abort by a // ST64B, ST64BV, ST64BV0, or LD64B instruction. The syndrome information // includes the ISS2, extended syndrome field.

impdef func LS64InstructionSyndrome() => (bits(24), bits(11)) begin return (Zeros{24}, LSInstructionSyndrome()); end;


## Library pseudocode for aarch64/functions/aborts/WatchpointFARNotPrecise

// WatchpointFARNotPrecise // ========================= // Returns TRUE If the lowest watchpointed address that is higher than or equal to the address // recorded in EDWAR might not have been accessed by the instruction, other than the CONSTRAINED // UNPREDICTABLE condition of watchpoint matching a range of addresses with lowest address 16 bytes // rounded down and upper address rounded up to nearest 16 byte multiple, // FALSE otherwise.

impdef func WatchpointFARNotPrecise(fault : FaultRecord) => boolean begin if IsFeatureImplemented(FEAT_SVE) || IsFeatureImplemented(FEAT_SME) then return fault.watchptinfo.maybe_false_match; end; return FALSE; end;


## Library pseudocode for aarch64/functions/apas/AArch64\_APAS

// AArch64_APAS // ============== // Decode Xt and perform an APAS operation for the decoded record.

func AArch64_APAS(Xt : bits(64)) begin var apas : APASRecord; let nse2 : bit = '0'; apas.paspace = DecodePASpace(nse2, Xt[62], Xt[63]);

apas.pa = Xt[NUM_PABITS-1:6] :: '000000';
apas.target_attributes = Xt[2:0];

if AArch64_LocationSupportsAPAS(apas) then
    APAS_OP(apas);
end;

end;


## Library pseudocode for aarch64/functions/apas/AArch64\_LocationSupportsAPAS

// AArch64_LocationSupportsAPAS // ============================== // Returns TRUE if the given memory location supports the APAS instruction.

impdef func AArch64_LocationSupportsAPAS(apas : APASRecord) => boolean begin return TRUE; end;


## Library pseudocode for aarch64/functions/apas/APASRecord

// APASRecord // ========== // Details related to an APAS operation.

type APASRecord of record { pa : bits(NUM_PABITS), paspace : PASpace, target_attributes : bits(3) };


## Library pseudocode for aarch64/functions/apas/APAS\_OP

// APAS_OP // ========= // Sets the PA Space of the address in the APASRecord to the target PA space. If the location // does not support the APAS instruction or cannot be associated with the indicated PASpace, // then the instruction has no effect on the location and does not generate an External abort.

impdef func APAS_OP(apas : APASRecord) begin return; end;


## Library pseudocode for aarch64/functions/at/AArch64\_AT

// AArch64_AT // ============ // Perform address translation as per AT instructions.

func AArch64_AT(address : bits(64), stage : TranslationStage, el_in : bits(2), ataccess : ATAccess) begin var el : bits(2) = el_in; // For stage 1 translation, when HCR_EL2().[E2H, TGE] is {1,1} and requested EL is EL1, // the EL2&0 translation regime is used. if ELIsInHost(EL0) && el == EL1 && stage == TranslationStage_1 then el = EL2; end;

let ss : SecurityState = SecurityStateAtEL(el);

let accdesc : AccessDescriptor = CreateAccDescAT(ss, el, ataccess);
let aligned : boolean = TRUE;

var fault : FaultRecord = NoFault(accdesc, address);
let regime : Regime = TranslationRegime(el);

var addrdesc : AddressDescriptor;
if (el == EL0 && ELUsingAArch32(EL1)) || (el != EL0 &&
ELUsingAArch32(el)) then
    if regime == Regime_EL2 || TTBCR().EAE == '1' then
        (fault, addrdesc) = AArch32_S1TranslateLD(fault, regime, address[31:0], aligned,
                                                  accdesc);
    else
        (fault, addrdesc, -) = AArch32_S1TranslateSD(fault, regime, address[31:0], aligned,
                                                     accdesc);
    end;
else
    let size : integer = 1;
    (fault, addrdesc) = AArch64_S1Translate(fault, regime, address, size, aligned, accdesc);
end;

if stage == TranslationStage_12 && fault.statuscode == Fault_None then
    let s1aarch64 : boolean = TRUE;
    if ELUsingAArch32(EL1) && regime == Regime_EL10 && EL2Enabled() then
        addrdesc.vaddress = ZeroExtend{64}(address);
        (fault, addrdesc) = AArch32_S2Translate(fault, addrdesc, aligned, accdesc);
    elsif regime == Regime_EL10 && EL2Enabled() then
        (fault, addrdesc) = AArch64_S2Translate(fault, addrdesc, s1aarch64, aligned, accdesc);
    end;
end;

let is_ATS1Ex : boolean = stage != TranslationStage_12;
if fault.statuscode != Fault_None then
    addrdesc = CreateFaultyAddressDescriptor(fault);
    // Take an exception on:
    // * A Synchronous External abort occurs on translation table walk
    // * A stage 2 fault occurs on a stage 1 walk
    // * A GPC Exception (FEAT_RME)
    // * A GPF from ATS1E{1,0}* when executed from EL1 and HCR_EL2.GPF == '1' (FEAT_RME)
    if (IsExternalAbort(fault) ||
          (PSTATE.EL == EL1 && fault.s2fs1walk) ||
           (IsFeatureImplemented(FEAT_RME) && fault.gpcf.gpf != GPCF_None && (
            ReportAsGPCException(fault) ||
            (EL2Enabled() && HCR_EL2().GPF == '1' && PSTATE.EL == EL1 && el IN {EL1, EL0} &&
             is_ATS1Ex)
            ))) then
        if IsFeatureImplemented(FEAT_D128) then
            PAR_EL1()       = ARBITRARY : bits(128);
        else
            PAR_EL1()[63:0] = ARBITRARY : bits(64);
        end;
        AArch64_Abort(addrdesc.fault);
    end;
end;

AArch64_EncodePAR(regime, el, is_ATS1Ex, addrdesc);
return;

end;


## Library pseudocode for aarch64/functions/at/AArch64\_EncodePAR

// AArch64_EncodePAR // =================== // Encode PAR register with result of translation.

func AArch64_EncodePAR(regime : Regime, el : bits(2), is_ATS1Ex : boolean, addrdesc : AddressDescriptor) begin let paspace : PASpace = addrdesc.paddress.paspace; if IsFeatureImplemented(FEAT_D128) then PAR_EL1() = Zeros{128}; if AArch64_isPARFormatD128(regime, is_ATS1Ex) then PAR_EL1().D128 = '1'; else PAR_EL1().D128 = '0'; end; else PAR_EL1()[63:0] = Zeros{64}; end;

if !IsFault(addrdesc) then
    PAR_EL1().F = '0';
    if IsFeatureImplemented(FEAT_RME) then
        if regime == Regime_EL3 then
            case paspace of
                when PAS_Secure =>     PAR_EL1().[NSE,NS] = '00';
                when PAS_NonSecure =>  PAR_EL1().[NSE,NS] = '01';
                when PAS_Root =>       PAR_EL1().[NSE,NS] = '10';
                when PAS_Realm =>      PAR_EL1().[NSE,NS] = '11';
            end;

        elsif SecurityStateForRegime(regime) == SS_Secure  then
            PAR_EL1().NSE = ARBITRARY : bit;
            PAR_EL1().NS = if paspace == PAS_Secure then '0' else '1';

        elsif SecurityStateForRegime(regime) == SS_Realm then
            if regime == Regime_EL10 && is_ATS1Ex then
                PAR_EL1().NSE = ARBITRARY : bit;
                PAR_EL1().NS  = ARBITRARY : bit;
            else
                PAR_EL1().NSE = ARBITRARY : bit;
                PAR_EL1().NS  = if paspace == PAS_Realm then '0' else '1';
            end;

        else
            PAR_EL1().NSE = ARBITRARY : bit;
            PAR_EL1().NS  = ARBITRARY : bit;
        end;
    else
        PAR_EL1()[11] = '1'; // RES1
        if SecurityStateForRegime(regime) == SS_Secure then
            PAR_EL1().NS = if paspace == PAS_Secure then '0' else '1';
        else
            PAR_EL1().NS = ARBITRARY : bit;
        end;
    end;
    PAR_EL1().SH   = ReportedPARShareability(PAREncodeShareability(addrdesc.memattrs));
    if IsFeatureImplemented(FEAT_D128) && PAR_EL1().D128 == '1' then
        PAR_EL1()[119:76] = addrdesc.paddress.address[55:12];
    else
        PAR_EL1()[55:12] = addrdesc.paddress.address[55:12];
    end;
    PAR_EL1().ATTR = ReportedPARAttrs(EncodePARAttrs(addrdesc.memattrs));
    PAR_EL1()[10] = ImpDefBit("Non-Faulting PAR");
else
    PAR_EL1().F   = '1';
    PAR_EL1().DirtyBit    = if addrdesc.fault.dirtybit then '1' else '0';
    PAR_EL1().Overlay     = if addrdesc.fault.overlay then '1' else '0';
    PAR_EL1().TopLevel    = if addrdesc.fault.toplevel then '1' else '0';
    PAR_EL1().AssuredOnly = if addrdesc.fault.assuredonly then '1' else '0';
    PAR_EL1().FST = AArch64_PARFaultStatus(addrdesc.fault);
    PAR_EL1().PTW = if addrdesc.fault.s2fs1walk then '1' else '0';
    PAR_EL1().S   = if addrdesc.fault.secondstage then '1' else '0';
    PAR_EL1()[11] = '1'; // RES1
    PAR_EL1()[63:48] = ImpDefBits{16}("Faulting PAR");
end;
return;

end;


## Library pseudocode for aarch64/functions/at/AArch64\_PARFaultStatus

// AArch64_PARFaultStatus // ======================== // Fault status field decoding of 64-bit PAR.

func AArch64_PARFaultStatus(fault : FaultRecord) => bits(6) begin var fst : bits(6);

if fault.statuscode == Fault_Domain then
    // Report Domain fault
    assert fault.level IN {1,2};
    fst[1:0] = if fault.level == 1 then '01' else '10';
    fst[5:2] = '1111';
else
    fst = EncodeLDFSC(fault.statuscode, fault.level);
end;
return fst;

end;


## Library pseudocode for aarch64/functions/at/AArch64\_isPARFormatD128

// AArch64_isPARFormatD128 // ========================= // Check if last stage of translation uses VMSAv9-128. // Last stage of translation is stage 2 if enabled, else it is stage 1.

func AArch64_isPARFormatD128(regime : Regime, is_ATS1Ex : boolean) => boolean begin var isPARFormatD128 : boolean; // Regime_EL2 does not support VMSAv9-128 if regime == Regime_EL2 || !IsFeatureImplemented(FEAT_D128) then isPARFormatD128 = FALSE; else isPARFormatD128 = FALSE; case regime of when Regime_EL3 => isPARFormatD128 = TCR_EL3().D128 == '1'; when Regime_EL20 => isPARFormatD128 = IsTCR2EL2Enabled() && TCR2_EL2().D128 == '1'; when Regime_EL10 => if is_ATS1Ex || () || HCR_EL2().[VM,DC] == '00' then isPARFormatD128 = IsTCR2EL1Enabled() && TCR2_EL1().D128 == '1'; else isPARFormatD128 = VTCR_EL2().D128 == '1'; end; end; end;

return isPARFormatD128;

end;


## Library pseudocode for aarch64/functions/at/GetPAR\_EL1\_D128

// GetPAR_EL1_D128 // ================= // Query the PAR_EL1.D128 field

func GetPAR_EL1_D128() => bit begin return if IsFeatureImplemented(FEAT_D128) then PAR_EL1().D128 else '0'; end;


## Library pseudocode for aarch64/functions/at/GetPAR\_EL1\_F

// GetPAR_EL1_F // ============== // Query the PAR_EL1.F field.

func GetPAR_EL1_F() => bit begin var F : bit;

F = PAR_EL1().F;
return F;

end;


## Library pseudocode for aarch64/functions/barrierop/MemBarrierOp

// MemBarrierOp // ============ // Memory barrier instruction types.

type MemBarrierOp of enumeration { MemBarrierOp_DSB, // Data Synchronization Barrier MemBarrierOp_DMB, // Data Memory Barrier MemBarrierOp_ISB, // Instruction Synchronization Barrier MemBarrierOp_SSBB, // Speculative Synchronization Barrier to VA MemBarrierOp_PSSBB, // Speculative Synchronization Barrier to PA MemBarrierOp_SB // Speculation Barrier };


## Library pseudocode for aarch64/functions/bfxpreferred/BFXPreferred

// BFXPreferred // ============== // // Return TRUE if UBFX or SBFX is the preferred disassembly of a // UBFM or SBFM bitfield instruction. Must exclude more specific // aliases UBFIZ, SBFIZ, UXT[BH], SXT[BHW], LSL, LSR and ASR.

func BFXPreferred(sf : bit, uns : bit, imms : bits(6), immr : bits(6)) => boolean begin

// must not match UBFIZ/SBFIX alias
if UInt(imms) < UInt(immr) then
    return FALSE;
end;

// must not match LSR/ASR/LSL alias (imms == 31 or 63)
if imms == sf::'11111' then
    return FALSE;
end;

// must not match UXTx/SXTx alias
if immr == '000000' then
    // must not match 32-bit UXT[BH] or SXT[BH]
    if sf == '0' && imms IN {'000111', '001111'} then
        return FALSE;
    end;
    // must not match 64-bit SXT[BHW]
    if sf::uns == '10' && imms IN {'000111', '001111', '011111'} then
        return FALSE;
    end;
end;

// must be UBFX/SBFX alias
return TRUE;

end;


## Library pseudocode for aarch64/functions/bitmasks/AltDecodeBitMasks

// AltDecodeBitMasks // =================== // Alternative but logically equivalent implementation of DecodeBitMasks() that // uses simpler primitives to compute tmask and wmask.

func AltDecodeBitMasks{M}(immN : bit, imms : bits(6), immr : bits(6), immediate : boolean) => (bits(M), bits(M)) begin var tmask, wmask : bits(64); var tmask_and, wmask_and : bits(6); var tmask_or, wmask_or : bits(6); var levels : bits(6);

// Compute log2 of element size
// 2^len must be in range [2, M]
if immN::NOT(imms) == '000000x' then Undefined(); end;
let len : integer{} = HighestSetBitNZ(immN::NOT(imms));
assert 2 <= (2^len) && (2^len) <= M;

// Determine s, r and s - r parameters
levels = ZeroExtend{6}(Ones{len});

// For logical immediates an all-ones value of s is reserved
// since it would generate a useless all-ones result (many times)
if immediate && (imms AND levels) == levels then Undefined(); end;

let s : integer{} = UInt(imms AND levels);
let r : integer{} = UInt(immr AND levels);
let diff : integer{} = s - r;    // 6-bit subtract with borrow

// Compute "top mask"
tmask_and = diff[5:0] OR NOT(levels);
tmask_or  = diff[5:0] AND levels;

tmask = Ones{64};
tmask = ((tmask
          AND Replicate{64, 2}(Replicate{1}(tmask_and[0]) :: Ones{1}))
           OR  Replicate{64, 2}(Zeros{1} :: Replicate{1}(tmask_or[0])));
// optimization of first step:
// tmask = Replicate{64}(tmask_and[0] :: '1');
tmask = ((tmask
          AND Replicate{64, 4}(Replicate{2}(tmask_and[1]) :: Ones{2}))
           OR  Replicate{64, 4}(Zeros{2} :: Replicate{2}(tmask_or[1])));
tmask = ((tmask
          AND Replicate{64, 8}(Replicate{4}(tmask_and[2]) :: Ones{4}))
           OR  Replicate{64, 8}(Zeros{4} :: Replicate{4}(tmask_or[2])));
tmask = ((tmask
          AND Replicate{64, 16}(Replicate{8}(tmask_and[3]) :: Ones{8}))
           OR  Replicate{64, 16}(Zeros{8} :: Replicate{8}(tmask_or[3])));
tmask = ((tmask
          AND Replicate{64, 32}(Replicate{16}(tmask_and[4]) :: Ones{16}))
           OR  Replicate{64, 32}(Zeros{16} :: Replicate{16}(tmask_or[4])));
tmask = ((tmask
          AND Replicate{64, 64}(Replicate{32}(tmask_and[5]) :: Ones{32}))
           OR  Replicate{64, 64}(Zeros{32} :: Replicate{32}(tmask_or[5])));

// Compute "wraparound mask"
wmask_and = immr OR NOT(levels);
wmask_or  = immr AND levels;

wmask = Zeros{64};
wmask = ((wmask
          AND Replicate{64, 2}(Ones{1} :: Replicate{1}(wmask_and[0])))
           OR  Replicate{64, 2}(Replicate{1}(wmask_or[0]) :: Zeros{1}));
// wmask = Replicate{64}(wmask_or[0] :: '0');
wmask = ((wmask
          AND Replicate{64, 4}(Ones{2} :: Replicate{2}(wmask_and[1])))
           OR  Replicate{64, 4}(Replicate{2}(wmask_or[1]) :: Zeros{2}));
wmask = ((wmask
          AND Replicate{64, 8}(Ones{4} :: Replicate{4}(wmask_and[2])))
           OR  Replicate{64, 8}(Replicate{4}(wmask_or[2]) :: Zeros{4}));
wmask = ((wmask
          AND Replicate{64, 16}(Ones{8} :: Replicate{8}(wmask_and[3])))
           OR  Replicate{64, 16}(Replicate{8}(wmask_or[3]) :: Zeros{8}));
wmask = ((wmask
          AND Replicate{64, 32}(Ones{16} :: Replicate{16}(wmask_and[4])))
           OR  Replicate{64, 32}(Replicate{16}(wmask_or[4]) :: Zeros{16}));
wmask = ((wmask
          AND Replicate{64, 64}(Ones{32} :: Replicate{32}(wmask_and[5])))
           OR  Replicate{64, 64}(Replicate{32}(wmask_or[5]) :: Zeros{32}));

if diff[6] != '0' then // borrow from s - r
    wmask = wmask AND tmask;
else
    wmask = wmask OR tmask;
end;

return (wmask[M-1:0], tmask[M-1:0]);

end;


## Library pseudocode for aarch64/functions/bitmasks/DecodeBitMasks

// DecodeBitMasks // ================ // Decode AArch64 bitfield and logical immediate masks which use a similar encoding structure

func DecodeBitMasks{M}(immN : bit, imms : bits(6), immr : bits(6), immediate : boolean) => (bits(M), bits(M)) begin var levels : bits(6);

// Compute log2 of element size
// 2^len must be in range [2, M]
if immN::NOT(imms) == '000000x' then Undefined(); end;
let len : integer{} = HighestSetBitNZ(immN::NOT(imms));
assert 2 <= (2^len) && (2^len) <= M;

// Determine s, r and s - r parameters
levels = ZeroExtend{6}(Ones{len});

// For logical immediates an all-ones value of s is reserved
// since it would generate a useless all-ones result (many times)
if immediate && (imms AND levels) == levels then Undefined(); end;

let s : integer{} = UInt(imms AND levels);
let r : integer{} = UInt(immr AND levels);
let diff : integer{} = s - r;    // 6-bit subtract with borrow

let esize : integer{} = 1 << len;
let d : integer{}     = UInt(diff[len-1:0]);
let welem : bits(esize) = ZeroExtend{}(Ones{s + 1});
let telem : bits(esize) = ZeroExtend{}(Ones{d + 1});
let wmask : bits(M) = Replicate{}(ROR(welem, r));
let tmask : bits(M) = Replicate{}(telem);
return (wmask, tmask);

end;


## Library pseudocode for aarch64/functions/cache/AArch64\_DataMemZero

// AArch64_DataMemZero // ===================== // Write Zero to data memory.

func AArch64_DataMemZero(regval : bits(64), vaddress : bits(64), accdesc_in : AccessDescriptor, size : integer) begin var accdesc : AccessDescriptor = accdesc_in;

// If the instruction targets tags as a payload, confer with system
register configuration
// which may override this.
if accdesc.tagaccess then
    accdesc.tagaccess = IsMTEEnabled(accdesc.el);
end;

// If the instruction encoding permits tag checking, confer with system
register configuration
// which may override this.
if accdesc.tagchecked then
    accdesc.tagchecked = AArch64_AccessIsTagChecked(vaddress, accdesc);
end;

let aligned : boolean = TRUE;
var memaddrdesc : AddressDescriptor = AArch64_TranslateAddress(vaddress,
                                                               accdesc, aligned, size);

if IsFault(memaddrdesc) then
    if !IsDebugException(memaddrdesc.fault) then
        memaddrdesc.fault.vaddress = regval;
    end;
    AArch64_Abort(memaddrdesc.fault);
end;

for i = 0 to size-1 do
    if accdesc.tagchecked then
        let ltag : bits(4)      = AArch64_LogicalAddressTag(vaddress);
        let bytes : integer = 1;
        let readcheck : boolean = FALSE;
        var fault : FaultRecord = AArch64_CheckTag(memaddrdesc, accdesc, readcheck,
                                                   bytes, ltag);
        if fault.statuscode != Fault_None then
            if ImpDefBool(
                  "DC_ZVA tag fault reported with lowest faulting address") then
                AArch64_Abort(fault);
            else
                fault.vaddress = regval;
                AArch64_Abort(fault);
            end;
        end;
    end;
    let memstatus : PhysMemRetStatus = PhysMemWrite{8}(memaddrdesc, accdesc, Zeros{8});
    if IsFault(memstatus) then
        HandleExternalWriteAbort(memstatus, memaddrdesc, 1, accdesc);
    end;

    memaddrdesc.paddress.address = memaddrdesc.paddress.address + 1;
end;
return;

end;


## Library pseudocode for aarch64/functions/cache/AArch64\_WriteTagMem

// AArch64_WriteTagMem // ===================== // Write to tag memory.

func AArch64_WriteTagMem(regval : bits(64), cachetype : CacheType, accdesc_in : AccessDescriptor, size : integer) begin assert accdesc_in.tagaccess;

var accdesc : AccessDescriptor = accdesc_in;
let vaddress : bits(64) = AlignDownSize{64}(regval, size as
integer{16..2048});
let count : integer     = size >> LOG2_TAG_GRANULE;
var tag : bits(4);
if cachetype == CacheType_TagZero then
    tag = '0000';
else
    tag = AArch64_AllocationTagFromAddress(vaddress);
end;
let aligned : boolean   = IsAlignedSize{64}(vaddress, TAG_GRANULE);
assert aligned;

accdesc.tagaccess = IsMTEEnabled(accdesc.el);

var (memtagtype, memaddrdesc) = AArch64_TranslateTagAddress(vaddress, accdesc,
aligned, size);

// Check for aborts or debug exceptions
if IsFault(memaddrdesc) then
    if IsDebugException(memaddrdesc.fault) then
        AArch64_Abort(memaddrdesc.fault);
    else
        memaddrdesc.fault.vaddress = regval;
        AArch64_Abort(memaddrdesc.fault);
    end;
end;

if !accdesc.tagaccess || memtagtype != MemTag_AllocationTagged then
    return;
end;

var memstatus : PhysMemRetStatus;

for i = 0 to count-1 do
    memstatus = PhysMemTagWrite(memaddrdesc, accdesc, tag);
    if IsFault(memstatus) then
        HandleExternalWriteAbort(memstatus, memaddrdesc, 1, accdesc);
    end;

    memaddrdesc.paddress.address = memaddrdesc.paddress.address + TAG_GRANULE;
    memaddrdesc.vaddress         = memaddrdesc.vaddress + TAG_GRANULE;
end;

return;

end;


## Library pseudocode for aarch64/functions/cflt/AArch64\_ConditionalFault

// AArch64_ConditionalFault // ========================== // Generate a Conditional fault.

noreturn func AArch64_ConditionalFault(isflt : boolean, immediate : bits(16)) begin assert (); var target_el : bits(2); if PSTATE.EL == EL0 then target_el = if EffectiveTGE() == '1' then EL2 else EL1; else target_el = PSTATE.EL; end; let preferred_exception_return : bits(64) = ThisInstrAddr{}(); let vect_offset : integer{} = 0x0;

var except : ExceptionRecord =
ExceptionSyndrome(Exception_ConditionalFault);
except.syndrome.iss[24]      = if isflt then '1' else '0';
except.syndrome.iss[15:0]    = immediate;

AArch64_TakeException(target_el, except, preferred_exception_return,
vect_offset);

end;


## Library pseudocode for aarch64/functions/cflt/CheckCFLTEnabled

// CheckCFLTEnabled // ================== // Check for EL0 access to a conditional fault instruction

func CheckCFLTEnabled begin if PSTATE.EL == EL0 && () && !(IsSCTLR2EL1Enabled() && SCTLR2_EL1().EnCF0 == '1') then Undefined(); end;

if PSTATE.EL == EL0 && IsInHost() &&
!(IsSCTLR2EL2Enabled() && SCTLR2_EL2().EnCF0 == '1') then
    Undefined();
end;

end;


## Library pseudocode for aarch64/functions/compareop/CompareOp

// CompareOp // ========= // Vector compare instruction types.

type CompareOp of enumeration {CompareOp_GT, CompareOp_GE, CompareOp_EQ, CompareOp_LE, CompareOp_LT};


## Library pseudocode for aarch64/functions/countop/CountOp

// CountOp // ======= // Bit counting instruction types.

type CountOp of enumeration {CountOp_CLZ, CountOp_CLS, CountOp_CNT};


## Library pseudocode for aarch64/functions/cpa/EffectiveCPTA

// EffectiveCPTA // =============== // Returns the CPTA bit applied to Checked Pointer Arithmetic for Addition in the given EL.

func EffectiveCPTA(el : bits(2)) => bit begin if !IsFeatureImplemented(FEAT_CPA2) then return '0'; end;

if Halted() then
    return '0';
end;

var cpta : bits(1);
let regime : Regime = TranslationRegime(el);

case regime of
    when Regime_EL3 =>
        cpta = SCTLR2_EL3().CPTA;
    when Regime_EL2 =>
        if IsSCTLR2EL2Enabled() then
            cpta = SCTLR2_EL2().CPTA;
        else
            cpta = '0';
        end;
    when Regime_EL20 =>
        if IsSCTLR2EL2Enabled() then
            cpta = if el == EL0 then SCTLR2_EL2().CPTA0 else SCTLR2_EL2().CPTA;
        else
            cpta = '0';
        end;
    when Regime_EL10 =>
        if IsSCTLR2EL1Enabled() then
            cpta = if el == EL0 then SCTLR2_EL1().CPTA0 else SCTLR2_EL1().CPTA;
        else
            cpta = '0';
        end;
    otherwise =>
        unreachable;
end;

return cpta;

end;


## Library pseudocode for aarch64/functions/cpa/EffectiveCPTM

// EffectiveCPTM // =============== // Returns the CPTM bit applied to Checked Pointer Arithmetic for Multiplication in the given EL.

func EffectiveCPTM(el : bits(2)) => bit begin if !IsFeatureImplemented(FEAT_CPA2) then return '0'; end;

if EffectiveCPTA(el) == '0' then
    return '0';
end;

if Halted() then
    return '0';
end;

var cptm : bits(1);
let regime : Regime = TranslationRegime(el);

case regime of
    when Regime_EL3 =>
        cptm = SCTLR2_EL3().CPTM;
    when Regime_EL2 =>
        if IsSCTLR2EL2Enabled() then
            cptm = SCTLR2_EL2().CPTM;
        else
            cptm = '0';
        end;
    when Regime_EL20 =>
        if IsSCTLR2EL2Enabled() then
            cptm = if el == EL0 then SCTLR2_EL2().CPTM0 else SCTLR2_EL2().CPTM;
        else
            cptm = '0';
        end;
    when Regime_EL10 =>
        if IsSCTLR2EL1Enabled() then
            cptm = if el == EL0 then SCTLR2_EL1().CPTM0 else SCTLR2_EL1().CPTM;
        else
            cptm = '0';
        end;
    otherwise =>
        unreachable;
end;

return cptm;

end;


## Library pseudocode for aarch64/functions/cpa/PointerAddCheck

// PointerAddCheck // ================= // Apply Checked Pointer Arithmetic for addition.

func PointerAddCheck(result : bits(64), base : bits(64)) => bits(64) begin return PointerCheckAtEL(PSTATE.EL, result, base, FALSE); end;


## Library pseudocode for aarch64/functions/cpa/PointerAddCheckAtEL

// PointerAddCheckAtEL // ===================== // Apply Checked Pointer Arithmetic for addition at the specified EL.

func PointerAddCheckAtEL(el : bits(2), result : bits(64), base : bits(64)) => bits(64) begin return PointerCheckAtEL(el, result, base, FALSE); end;


## Library pseudocode for aarch64/functions/cpa/PointerCheckAtEL

// PointerCheckAtEL // ================== // Apply Checked Pointer Arithmetic at the specified EL.

func PointerCheckAtEL(el : bits(2), result : bits(64), base : bits(64), cptm_detected : boolean) => bits(64) begin var rv : bits(64) = result;

let previous_detection : boolean = (base[55] != base[54]);
let cpta_detected : boolean = (result[63:56] != base[63:56] ||
previous_detection);

if ((cpta_detected && EffectiveCPTA(el) == '1') ||
      (cptm_detected && EffectiveCPTM(el) == '1')) then
    rv[63:55] = base[63:55];
    rv[54] = NOT(rv[55]);
end;

return rv;

end;


## Library pseudocode for aarch64/functions/cpa/PointerMultiplyAddCheck

// PointerMultiplyAddCheck // ========================= // Apply Checked Pointer Arithmetic for multiplication.

func PointerMultiplyAddCheck(result : bits(64), base : bits(64), cptm_detected : boolean) => bits(64) begin return PointerCheckAtEL(PSTATE.EL, result, base, cptm_detected); end;


## Library pseudocode for aarch64/functions/d128/IsD128Enabled

// IsD128Enabled // =============== // Returns true if 128-bit page descriptor is enabled

func IsD128Enabled(el : bits(2)) => boolean begin var d128enabled : boolean; if IsFeatureImplemented(FEAT_D128) then case el of when EL0 => if (EL0) then d128enabled = IsTCR2EL1Enabled() && TCR2_EL1().D128 == '1'; else d128enabled = IsTCR2EL2Enabled() && TCR2_EL2().D128 == '1'; end; when EL1 => d128enabled = IsTCR2EL1Enabled() && TCR2_EL1().D128 == '1'; when EL2 => d128enabled = IsTCR2EL2Enabled() && IsInHost() && TCR2_EL2().D128 == '1'; when EL3 => d128enabled = TCR_EL3().D128 == '1'; end; else d128enabled = FALSE; end;

return d128enabled;

end;


## Library pseudocode for aarch64/functions/dc/AArch64\_CanTrapDC

// AArch64_CanTrapDC // =================== // Determines whether the execution of the DC instruction can be trapped.

func AArch64_CanTrapDC(cachetype : CacheType, cacheop : CacheOp, opscope : CacheOpScope) => boolean begin return ((cachetype, cacheop, opscope) || ImpDefBool( "When DC is treated as NOP, data cache maintenance operations are trapped")); end;


## Library pseudocode for aarch64/functions/dc/AArch64\_DC

// AArch64_DC // ============ // Perform Data Cache Operation.

func AArch64_DC(regval : bits(64), cachetype : CacheType, cacheop : CacheOp, opscope_in : CacheOpScope) begin var opscope : CacheOpScope = opscope_in; var cache : CacheRecord;

cache.acctype   = AccessType_DC;
cache.cachetype = cachetype;
cache.cacheop   = cacheop;
cache.opscope   = opscope;

if opscope == CacheOpScope_SetWay then
    let ss : SecurityState = SecurityStateAtEL(PSTATE.EL);
    cache.cpas = CPASAtSecurityState(ss);
    cache.shareability = Shareability_NSH;
    cache.(setnum, waynum, level) = DecodeSW(regval, cachetype);
    if (cacheop == CacheOp_Invalidate && PSTATE.EL == EL1 && EL2Enabled() &&
      (HCR_EL2().SWIO == '1' || HCR_EL2().[DC,VM] != '00')) then
        cache.cacheop = CacheOp_CleanInvalidate;
    end;

    CACHE_OP(cache);
    return;
end;

if EL2Enabled() && () then
    if PSTATE.EL IN {EL0, EL1} then
        cache.is_vmid_valid = TRUE;
        cache.vmid          = VMID();
    else
        cache.is_vmid_valid = FALSE;
    end;
else
    cache.is_vmid_valid = FALSE;
end;

if PSTATE.EL == EL0 then
    cache.is_asid_valid = TRUE;
    cache.asid          = ASID();
else
    cache.is_asid_valid = FALSE;
end;

if (opscope == CacheOpScope_PoPS &&
      !ImpDefBool("Memory system supports PoPS")) then
    opscope = CacheOpScope_PoC;
end;
if (opscope == CacheOpScope_PoDP &&
      !ImpDefBool("Memory system supports PoDP")) then
    opscope = CacheOpScope_PoP;
end;
if (opscope == CacheOpScope_PoP &&
      !ImpDefBool("Memory system supports PoP")) then
    opscope = CacheOpScope_PoC;
end;
var vaddress : bits(64) = regval;

var size : integer = 0;        // by default no watchpoint address
if cacheop == CacheOp_Invalidate then
    size = DataCacheWatchpointSize();
    vaddress = AlignDownSize{64}(regval, size as AddressSize);
end;

if opscope == CacheOpScope_PoE then
    cache.shareability     = Shareability_OSH;
    cache.paddress.address = regval[NUM_PABITS-1:0];
    let nse2 : bit = if IsFeatureImplemented(FEAT_RME_GDI) then regval[61] else '0';
    cache.paddress.paspace = DecodePASpace(nse2, regval[62], regval[63]);
    cache.cpas             = CPASAtPAS(cache.paddress.paspace);

    // If a Reserved encoding is selected, the instruction is permitted to be treated as a NOP.
    if cache.paddress.paspace != PAS_Realm then
        ExecuteAsNOP();
    end;

    if ImpDefBool("Apply granule protection check on DC to PoE") then
        var memaddrdesc : AddressDescriptor;
        let accdesc : AccessDescriptor = CreateAccDescDC(cache);
        memaddrdesc.paddress     = cache.paddress;
        memaddrdesc.fault.gpcf   = GranuleProtectionCheck(memaddrdesc, accdesc);

        if memaddrdesc.fault.gpcf.gpf != GPCF_None then
            memaddrdesc.fault.statuscode = Fault_GPCFOnOutput;
            memaddrdesc.fault.paddress   = memaddrdesc.paddress;
            memaddrdesc.fault.vaddress   = ARBITRARY : bits(64);
            AArch64_Abort(memaddrdesc.fault);
        end;
    end;
elsif opscope == CacheOpScope_PoPA then
    cache.shareability = Shareability_OSH;
    cache.paddress.address = regval[NUM_PABITS-1:0];
    let nse2 : bit = if IsFeatureImplemented(FEAT_RME_GDI) then regval[61] else '0';
    cache.paddress.paspace = DecodePASpace(nse2, regval[62], regval[63]);

    // If {NSE2, NSE, NS} is reserved, then no cache entries are required
    // to be cleaned or invalidated.
    if cache.paddress.paspace IN {PAS_NA6, PAS_NA7} then
        ExecuteAsNOP();
    end;
    cache.cpas = CPASAtPAS(cache.paddress.paspace);
else
    cache.vaddress  = vaddress;
    let aligned : boolean = TRUE;
    let accdesc : AccessDescriptor = CreateAccDescDC(cache);
    var memaddrdesc : AddressDescriptor = AArch64_TranslateAddress(vaddress, accdesc,
                                                                   aligned, size);
    if IsFault(memaddrdesc) then
        memaddrdesc.fault.vaddress = regval;
        AArch64_Abort(memaddrdesc.fault);
    end;

    cache.paddress   = memaddrdesc.paddress;
    cache.cpas       = CPASAtPAS(memaddrdesc.paddress.paspace);

    if (opscope IN {
                     CacheOpScope_PoDP,
                     CacheOpScope_PoP,
                     CacheOpScope_PoC,
                     CacheOpScope_PoU
                  }) then
        cache.shareability = memaddrdesc.memattrs.shareability;
    else
        cache.shareability = Shareability_NSH;
    end;
end;

if (cacheop == CacheOp_Invalidate && PSTATE.EL == EL1 &&
EL2Enabled() &&
        HCR_EL2().[DC,VM] != '00') then
    cache.cacheop = CacheOp_CleanInvalidate;
end;

// If Secure state is not implemented, but RME is, the instruction acts as a
NOP
if cache.cpas == CPAS_Secure && () then
    return;
end;

CACHE_OP(cache);
return;

end;


## Library pseudocode for aarch64/functions/dc/AArch64\_MemZero

// AArch64_MemZero // =================

func AArch64_MemZero(regval : bits(64), cachetype : CacheType) begin let size : integer{} = (if cachetype IN {CacheType_TagWrite, CacheType_TagZero} then 4*(2^(UInt(DCZID_EL0().TBS))) else 4*(2^(UInt(DCZID_EL0().BS)))); assert size <= MAX_ZERO_BLOCK_SIZE; if IsFeatureImplemented(FEAT_MTE2) then

------------------------------------------------------------------------------ Page 1 of 1. End of thread.