blob: 536b53f4889a7dbfb99181fc8614c14bae7612ff [file]
// Copyright 2017 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <lib/arch/asm.h>
#include "src/virtualization/tests/hypervisor/asm.h"
#include "src/virtualization/tests/hypervisor/constants.h"
.set ICC_IAR1_EL1, S3_0_C12_C12_0
#define ID_AA64ZFR0_EL1 S3_0_C0_C4_4
#define ZCR_EL1 S3_0_C1_C2_0
.set CPACR_EL1_ZEN_NO_TRAP, (3 << 16)
.set CPACR_EL1_FPEN_NO_TRAP, (3 << 20)
// See ARM ARM Table D1.10.2 for more details.
.set CURRENT_EL_EL0_BASE, 0x000
.set CURRENT_EL_ELx_BASE, 0x200
.set LOWER_EL_ARCH64_BASE, 0x400
.set LOWER_EL_ARCH32_BASE, 0x600
.set SYNC_EXC_OFFSET, 0x000
.set IRQ_EXC_OFFSET, 0x080
.set FIQ_EXC_OFFSET, 0x100
.set SERROR_EXC_OFFSET, 0x180
.set SPSR_DAIF_AARCH64, 0x3c0
.set SPSR_AIFM_AARCH32, 0x1d0
.set GICC_BASE, 0x800001000
.set GICC_IAR_OFFSET, 0xc
.set PSCI64_SYSTEM_OFF, 0x84000008
.set PSCI64_SYSTEM_RESET, 0x84000009
.set PSCI64_CPU_ON, 0xc4000003
// TCR_EL1 for configuring the MMU.
//
// [arm/v8]: D13.2.120 TCR_EL1, Translation Control Register (EL1)
.set TCR_EL1_TG0_4KIB, (0 << 14) // TTBR0 Granule size.
.set TCR_EL1_T0SZ, ((64 - REGION_SIZE_BITS) << 0) // TTBR0 region size
.set TCR_EL1_SH0_INNER_SHAREABLE, (3 << 12) // TTBR0 Inner shareable.
.set TCR_EL1_ORGN0_WB_WA, (1 << 10) // TTBR0 Outer write-back/write-allocate
.set TCR_EL1_IRGN0_WB_WA, (1 << 8) // TTBR0 Inner write-back/write-allocate
.set GUEST_TCR_EL1, ( \
(TCR_EL1_TG0_4KIB | TCR_EL1_T0SZ | TCR_EL1_SH0_INNER_SHAREABLE | TCR_EL1_ORGN0_WB_WA | \
TCR_EL1_IRGN0_WB_WA))
.set GUEST_MAIR_EL1, (MAIR_ATTR_NORMAL_CACHED << 0)
// Bits for the SCTLR registers.
.set SCTLR_ELX_M, (1 << 0) // MMU enabled.
.set SCTLR_ELX_C, (1 << 2) // Data cache enabled.
.set SCTLR_ELX_I, (1 << 12) // Instruction cache enabled.
.set MEMORY_TEST_BYTES, (2 * 1024 * 1024) // Region size to read/write for memory tests.
// Architecture-specific callbacks called by the FUNCTION macro.
.macro arch_function_start name start end
// Each time we create a function, define a macro that adjusts the current
// text location to position at a specific exception handler.
//
// We need to reset the macro each function, because the offset needs
// to be at a fixed location after the function's start.
.macro exception_vector base offset
.skip (\base + \offset) - (. - \name)
.endm
.endm
.macro arch_function_end name start end
.purgem exception_vector
.endm
// Signals test completion by writing 0 to EXIT_TEST_ADDR.
.macro test_complete
mov x0, EXIT_TEST_ADDR
str xzr, [x0]
b .
.endm
// Signals test failure by writing 0 to EXIT_TEST_FAILURE_ADDR.
.macro test_failure
movlit x0, EXIT_TEST_FAILURE_ADDR
str xzr, [x0]
b .
.endm
// Signals test completion by writing 0 to EXIT_TEST_ADDR.
.macro test_complete_aarch32
.word 0xE3A01000 // aarch32: movw r1, 0
.word 0xE3000000 // aarch32: movw r0, [lower]EXIT_TEST_ADDR
.word 0xE34000FF // aarch32: movt r0, [upper]EXIT_TEST_ADDR
.word 0xE5801000 // aarch32: str r1, [r0]
.word 0xEAFFFFFE // aarch32: 1: b 1b
.endm
// Drop exception level from EL1 to EL0 and, if requested, switch execution
// state.
.macro drop_to_el0 arch=aarch64
.ifc \arch, aarch64
mov x0, SPSR_DAIF_AARCH64
.else
.ifc \arch, aarch32
mov x0, SPSR_AIFM_AARCH32
.else
.error "Unsupported architecture"
.endif
.endif
msr spsr_el1, x0
isb
adr x0, .Ldrop_to_el0_end\@ // resume execution at label ahead
msr elr_el1, x0 // store exception link register
eret // resume at el0 using specified arch.
.Ldrop_to_el0_end\@:
.endm
// Set up and enable the MMU.
//
// Clobbers x0 and x1.
.macro setup_and_enable_mmu
// Set up Memory Attribute Indirection Register (MAIR).
movlit x0, GUEST_MAIR_EL1
msr mair_el1, x0
// Set up Translation Control Register (TCR).
movlit x0, GUEST_TCR_EL1
msr tcr_el1, x0
// Set up page table root
movlit x0, PAGE_TABLE_PADDR
msr ttbr0_el1, x0
// Enable page table and caches.
mrs x0, sctlr_el1
movlit x1, (SCTLR_ELX_M | SCTLR_ELX_C | SCTLR_ELX_I)
orr x0, x0, x1
msr sctlr_el1, x0
dsb sy
isb
.endm
// Turn off the MMU.
//
// Clobbers x0.
.macro disable_mmu
mrs x0, sctlr_el1
bic x0, x0, SCTLR_ELX_M // Clear MMU enable bit.
msr sctlr_el1, x0
dsb sy
isb
.endm
// Write the value in `word_reg` to every word in [start, start + size).
//
// Clobbers x0, x1.
.macro write_region start, size, word_reg
movlit x0, \start // Start address
movlit x1, (\start + \size) // End address
.Lwrite_region\@:
str \word_reg, [x0], 8 // Write the word and increment the pointer.
cmp x0, x1 // Keep looping until we reach the end address.
bne .Lwrite_region\@
.endm
// Confirm the region of memory [start, start + size) contains the value in `word_reg`.
//
// On error, jumps to the label `failure_label`. Otherwise, continues execution
// below the macro.
//
// Clobbers x0, x1, x2.
.macro check_region start, size, word_reg, failure_label
movlit x0, \start // Start address
movlit x1, (\start + \size) // End address
.Lcheck_region\@:
ldr x2, [x0], 8 // Read the word and increment the pointer.
cmp x2, \word_reg // Ensure if matches
bne \failure_label
cmp x0, x1 // Otherwise, keep looping until we reach the end address.
bne .Lcheck_region\@
.endm
// Clean the cache by VA for the region [start, start + size).
//
// Clobbers x0 -- x3.
.macro clean_region start, size
movlit x0, \start
movlit x1, (\start + \size)
// Extract the cache line size in bytes from CTR_EL0
mrs x2, ctr_el0
ubfx x2, x2, #16, #4
mov x3, #4
lsl x2, x3, x2
.Lclean_region\@:
// Iterate with x0, cleaning one cache line at a time by VA.
dc cvac, x0
add x0, x0, x2
// Loop if x0 is still less than the end of the region.
cmp x0, x1
blo .Lclean_region\@
dsb sy // Ensure all cleans have taken effect...
isb // ...and that no speculation occurs until they do.
.endm
.text
// Test vcpu_enter.
FUNCTION vcpu_enter
test_complete
END_FUNCTION
FUNCTION vcpu_wait
1:
wfi
b 1b
END_FUNCTION
// Test vcpu_read_state and vcpu_write_state.
FUNCTION vcpu_read_write_state
add x1, x1, #1
add x2, x2, #2
add x3, x3, #3
add x4, x4, #4
add x5, x5, #5
add x6, x6, #6
add x7, x7, #7
add x8, x8, #8
add x9, x9, #9
add x10, x10, #10
add x11, x11, #11
add x12, x12, #12
add x13, x13, #13
add x14, x14, #14
add x15, x15, #15
add x16, x16, #16
add x17, x17, #17
add x18, x18, #18
add x19, x19, #19
add x20, x20, #20
add x21, x21, #21
add x22, x22, #22
add x23, x23, #23
add x24, x24, #24
add x25, x25, #25
add x26, x26, #26
add x27, x27, #27
add x28, x28, #28
add x29, x29, #29
add x30, x30, #30
add sp, sp, #64
cmp sp, #128 // Set ZC bits of CPSR.
test_complete
END_FUNCTION
// Test vcpu_interrupt.
FUNCTION vcpu_interrupt
msr daifclr, #2
b .
exception_vector CURRENT_EL_ELx_BASE, IRQ_EXC_OFFSET
test_complete
END_FUNCTION
// Test guest_set_trap using a memory-based trap.
FUNCTION guest_set_trap
mov x0, TRAP_ADDR
str xzr, [x0]
test_complete
END_FUNCTION
// Test wfi instruction handling.
FUNCTION vcpu_wfi
// Setup the virtual timer by:
// 1. Setting the compare value to 0.
// 2. Enabling the virtual timer.
msr cntv_cval_el0, xzr
mov x0, 1
msr cntv_ctl_el0, x0
wfi
test_complete
END_FUNCTION
// Test wfi handling with pending interrupt in LR (GICv2).
FUNCTION vcpu_wfi_pending_interrupt_gicv2
msr daifclr, #2
b .
exception_vector CURRENT_EL_ELx_BASE, IRQ_EXC_OFFSET
movlit x0, GICC_BASE
// Acknowledge interrupt by reading IAR.
ldr w1, [x0, GICC_IAR_OFFSET]
wfi
test_complete
END_FUNCTION
// Test wfi handling with pending interrupt in LR (GICv3).
FUNCTION vcpu_wfi_pending_interrupt_gicv3
msr daifclr, #2
b .
exception_vector CURRENT_EL_ELx_BASE, IRQ_EXC_OFFSET
movlit x0, GICC_BASE
// Acknowledge interrupt by reading IAR.
mrs x1, ICC_IAR1_EL1
dsb sy
wfi
test_complete
END_FUNCTION
// Test wfi instruction handling.
// Execution of WFI at EL0 on AARCH32 is propagated to EL1 / AARCH64.
FUNCTION vcpu_wfi_aarch32
drop_to_el0 aarch32
.word 0xE320F003 // aarch32: wfi
// From ARM DDI 0487C.a, Section D10.2.45: Since a WFI can complete at any
// time, even without a Wakeup event, the traps on WFI are not guaranteed to
// be taken, even if the WFI is executed when there is no Wakeup event. The
// only guarantee is that if the instruction does not complete in finite
// time in the absence of a Wakeup event, the trap will be taken.
//
// We therefore complete the test at this point.
test_complete_aarch32
exception_vector LOWER_EL_ARCH32_BASE, SYNC_EXC_OFFSET
test_complete
END_FUNCTION
// Test floating-point instruction handling with trapping between levels.
FUNCTION vcpu_fp
drop_to_el0
// Access vector registers.
mov w0, 0xff
dup v0.16b, w0
test_complete
// Handle EL1 floating-point trap.
// This is interpreted as a Lower exception level (coming from EL0)
// captured by AARCH64. See ARM ARM Table D1.10.2 for more details.
exception_vector LOWER_EL_ARCH64_BASE, SYNC_EXC_OFFSET
mov x0, CPACR_EL1_FPEN_NO_TRAP
msr cpacr_el1, x0
eret
END_FUNCTION
// Test wfi instruction handling.
FUNCTION vcpu_fp_aarch32
drop_to_el0 aarch32
// Load double precision register d0 from address 0.
// This should trigger floating point exception.
.word 0xE3A00000 // aarch32: mov r0, 0
.word 0xED900B00 // aarch32: vldr d0, [r0]
test_complete_aarch32
exception_vector LOWER_EL_ARCH32_BASE, SYNC_EXC_OFFSET
mov x0, CPACR_EL1_FPEN_NO_TRAP
msr cpacr_el1, x0
eret
END_FUNCTION
FUNCTION vcpu_psci_system_off
movlit x0, PSCI64_SYSTEM_OFF
smc #0
END_FUNCTION
FUNCTION vcpu_psci_system_reset
movlit x0, PSCI64_SYSTEM_RESET
smc #0
END_FUNCTION
FUNCTION vcpu_psci_cpu_on
movlit x0, PSCI64_CPU_ON
movlit x1, 0x1
movlit x2, 0x1000
movlit x3, 0xfeedfacedeadbeef
smc #0
cbnz x0, 1f
test_complete
1:
test_failure
END_FUNCTION
// Test EL1/EL0 system register context switching across VM exits under VHE.
FUNCTION vcpu_system_registers
// Enable the guest MMU and caches first so HCR_EL2.TVM is cleared and
// subsequent system register writes execute untrapped against *_el12 hardware.
setup_and_enable_mmu
movlit x1, 0x1111222233334444
msr sp_el0, x1
movlit x2, 0x2222333344445555
msr tpidr_el0, x2
movlit x3, 0x3333444455556666
msr tpidrro_el0, x3
movlit x4, 0x4444555566667777
msr tpidr_el1, x4
movlit x5, 0x88889999aaaabbbc
msr elr_el1, x5
movlit x6, SPSR_DAIF_AARCH64
msr spsr_el1, x6
movlit x7, 0x0000123456784000
msr vbar_el1, x7
movlit x8, 0x9999aaaa
msr contextidr_el1, x8
movlit x9, CPACR_EL1_FPEN_NO_TRAP
msr cpacr_el1, x9
mov x10, 0x3
msr cntkctl_el1, x10
movlit x11, 0x0022000000020000
msr ttbr1_el1, x11
movlit x12, 0x56000042
msr esr_el1, x12
movlit x13, 0xfeedfacecafebabe
msr far_el1, x13
movlit x15, 0xab00000012345800
msr par_el1, x15
mov x16, 0x2
msr csselr_el1, x16
isb
// Trigger a VM exit to userspace so host EL2&0 / userspace runs before re-entry.
mov x0, TRAP_ADDR
str xzr, [x0]
mrs x14, sp_el0
cmp x14, x1
bne .Lsysreg_fail
mrs x14, tpidr_el0
cmp x14, x2
bne .Lsysreg_fail
mrs x14, tpidrro_el0
cmp x14, x3
bne .Lsysreg_fail
mrs x14, tpidr_el1
cmp x14, x4
bne .Lsysreg_fail
mrs x14, elr_el1
cmp x14, x5
bne .Lsysreg_fail
mrs x14, spsr_el1
cmp x14, x6
bne .Lsysreg_fail
mrs x14, vbar_el1
cmp x14, x7
bne .Lsysreg_fail
mrs x14, contextidr_el1
cmp x14, x8
bne .Lsysreg_fail
mrs x14, cpacr_el1
cmp x14, x9
bne .Lsysreg_fail
mrs x14, cntkctl_el1
and x14, x14, #0x3
cmp x14, x10
bne .Lsysreg_fail
mrs x14, ttbr1_el1
cmp x14, x11
bne .Lsysreg_fail
mrs x14, esr_el1
cmp x14, x12
bne .Lsysreg_fail
mrs x14, far_el1
cmp x14, x13
bne .Lsysreg_fail
mrs x14, par_el1
cmp x14, x15
bne .Lsysreg_fail
mrs x14, csselr_el1
cmp x14, x16
bne .Lsysreg_fail
mrs x14, mair_el1
movlit x17, GUEST_MAIR_EL1
cmp x14, x17
bne .Lsysreg_fail
mrs x14, tcr_el1
movlit x17, GUEST_TCR_EL1
cmp x14, x17
bne .Lsysreg_fail
mrs x14, ttbr0_el1
movlit x17, PAGE_TABLE_PADDR
cmp x14, x17
bne .Lsysreg_fail
test_complete
.Lsysreg_fail:
test_failure
END_FUNCTION
// Test trapped TVM / TDOSA system register accesses and Rt == 31 (xzr) bounds checking.
FUNCTION vcpu_trapped_system_registers
// sp_el0 immediately follows x[30] in GuestState; verify Rt == 31 (xzr)
// does not index out-of-bounds and overwrite sp_el0.
movlit x19, 0xdeadbeefcafebabe
msr sp_el0, x19
// Trapped TVM writes using xzr (Rt == 31).
msr sctlr_el1, xzr
msr tcr_el1, xzr
msr ttbr0_el1, xzr
msr ttbr1_el1, xzr
msr esr_el1, xzr
msr far_el1, xzr
msr afsr0_el1, xzr
msr afsr1_el1, xzr
msr mair_el1, xzr
msr amair_el1, xzr
msr contextidr_el1, xzr
// Trapped TDOSA reads and writes using xzr (Rt == 31) and general registers.
msr oslar_el1, xzr
msr osdlr_el1, xzr
msr dbgprcr_el1, xzr
mrs xzr, oslsr_el1
mrs xzr, osdlr_el1
mrs xzr, dbgprcr_el1
movlit x1, 0x123456789abcdef0
msr osdlr_el1, x1
mrs x1, osdlr_el1
cbnz x1, .Ltrap_sysreg_fail
movlit x1, 0x123456789abcdef0
mrs x1, oslsr_el1
cbnz x1, .Ltrap_sysreg_fail
movlit x1, 0x123456789abcdef0
msr dbgprcr_el1, x1
mrs x1, dbgprcr_el1
cbnz x1, .Ltrap_sysreg_fail
// Verify trapped AFSR0_EL1, AFSR1_EL1, and AMAIR_EL1 writes are handled
// without error (these registers are RAZ/WI in hardware when no
// implementation-defined fault status or auxiliary attributes exist).
movlit x1, 0x0123456789abcdef
msr afsr0_el1, x1
msr afsr1_el1, x1
msr amair_el1, x1
// Verify 64-bit preservation across trapped TVM writes.
movlit x1, 0xfeedfacedeadbeef
msr mair_el1, x1
isb
mrs x2, mair_el1
cmp x2, x1
bne .Ltrap_sysreg_fail
movlit x1, 0x0011000000010000
msr ttbr0_el1, x1
isb
mrs x2, ttbr0_el1
cmp x2, x1
bne .Ltrap_sysreg_fail
movlit x1, 0x0022000000020000
msr ttbr1_el1, x1
isb
mrs x2, ttbr1_el1
cmp x2, x1
bne .Ltrap_sysreg_fail
movlit x1, ((1 << 32) | GUEST_TCR_EL1)
msr tcr_el1, x1
isb
mrs x2, tcr_el1
cmp x2, x1
bne .Ltrap_sysreg_fail
movlit x1, 0xfeedfacecafebabe
msr far_el1, x1
isb
mrs x2, far_el1
cmp x2, x1
bne .Ltrap_sysreg_fail
movlit x1, 0x56000042
msr esr_el1, x1
isb
mrs x2, esr_el1
cmp x2, x1
bne .Ltrap_sysreg_fail
movlit x1, 0xaabbccdd
msr contextidr_el1, x1
isb
mrs x2, contextidr_el1
cmp x2, x1
bne .Ltrap_sysreg_fail
// Verify SVE is masked out in ID_AA64PFR0_EL1 (bits [35:32]) and ID_AA64ZFR0_EL1 is RAZ.
mrs x1, id_aa64pfr0_el1
ubfx x1, x1, #32, #4
cbnz x1, .Ltrap_sysreg_fail
movlit x1, 0x123456789abcdef0
mrs x1, ID_AA64ZFR0_EL1
cbnz x1, .Ltrap_sysreg_fail
// Verify sp_el0 was not corrupted by any Rt == 31 (xzr) trapped instructions.
mrs x2, sp_el0
cmp x2, x19
bne .Ltrap_sysreg_fail
test_complete
.Ltrap_sysreg_fail:
test_failure
END_FUNCTION
// Test that SVE instructions are trapped to EL2 and not enabled for guests.
FUNCTION vcpu_sve
// Verify ID_AA64PFR0_EL1.SVE (bits [35:32]) is masked to 0 and ID_AA64ZFR0_EL1 is RAZ.
mrs x0, id_aa64pfr0_el1
ubfx x0, x0, #32, #4
cbnz x0, .Lsve_fail
mrs x0, ID_AA64ZFR0_EL1
cbnz x0, .Lsve_fail
// Enable FP and SVE in the guest's CPACR_EL1 (CPACR_EL12 under VHE).
// Because CPTR_EL2.ZEN is 0b00, executing an SVE instruction must trap to EL2
// and cause vcpu_enter to fail with ZX_ERR_NOT_SUPPORTED.
mov x0, (CPACR_EL1_FPEN_NO_TRAP | CPACR_EL1_ZEN_NO_TRAP)
msr cpacr_el1, x0
isb
.arch_extension sve
rdvl x0, #1
.arch_extension nosve
.Lsve_fail:
test_failure
exception_vector CURRENT_EL_ELx_BASE, SYNC_EXC_OFFSET
test_failure
END_FUNCTION
// Test that guest access to ZCR_EL1 is trapped to EL2 and not enabled for guests.
FUNCTION vcpu_sve_zcr
mov x0, (CPACR_EL1_FPEN_NO_TRAP | CPACR_EL1_ZEN_NO_TRAP)
msr cpacr_el1, x0
isb
mrs x0, ZCR_EL1
test_failure
exception_vector CURRENT_EL_ELx_BASE, SYNC_EXC_OFFSET
test_failure
END_FUNCTION
// Test EL1 and EL0 generic timer access and context switch under VHE.
FUNCTION vcpu_timer_registers
mrs x1, cntfrq_el0
cbz x1, .Ltimer_fail
mrs x2, cntpct_el0
cbz x2, .Ltimer_fail
mrs x3, cntvct_el0
cbz x3, .Ltimer_fail
// Program virtual timer with a masked far-future deadline and verify it
// survives a VM exit to the host.
movlit x2, 0x7fffffffffffffff
msr cntv_cval_el0, x2
mov x3, 0x3 // IMASK | ENABLE
msr cntv_ctl_el0, x3
mov x0, TRAP_ADDR
str xzr, [x0]
mrs x4, cntv_cval_el0
cmp x4, x2
bne .Ltimer_fail
mrs x4, cntv_ctl_el0
and x4, x4, #0x3
cmp x4, x3
bne .Ltimer_fail
msr cntv_ctl_el0, xzr
// Enable EL0 physical and virtual counter reads (EL0PCTEN | EL0VCTEN)
// and verify guest EL0 can read timer registers without trapping to EL2.
mov x0, 0x3
msr cntkctl_el1, x0
isb
drop_to_el0
mrs x1, cntfrq_el0
cbz x1, .Ltimer_fail
mrs x2, cntpct_el0
cbz x2, .Ltimer_fail
mrs x3, cntvct_el0
cbz x3, .Ltimer_fail
test_complete
.Ltimer_fail:
test_failure
END_FUNCTION
// Test that guest EL0 syscalls (svc) route to guest VBAR_EL1 (TGE=0) rather than host VBAR_EL2.
FUNCTION vcpu_el0_syscall
mov x19, #0
drop_to_el0
svc #0x1234
cmp x19, #0x42
bne .Lsvc_fail
test_complete
.Lsvc_fail:
test_failure
exception_vector LOWER_EL_ARCH64_BASE, SYNC_EXC_OFFSET
mrs x1, esr_el1
// EC == 0b010101 (SVC in AArch64), ISS == 0x1234.
movlit x2, ((0x15 << 26) | (1 << 25) | 0x1234)
cmp x1, x2
bne .Lsvc_fail
mov x19, #0x42
eret
END_FUNCTION
// Test set/way d-cache operations.
//
// These are trapped and emulated by the hypervisor.
FUNCTION vcpu_dc_set_way_ops
mov x0, #0
dc csw, x0 // Clean L1 cache, set/way 0.
dc cisw, x0 // Clean/Invalidate L1 cache, set/way 0.
dc isw, x0 // Invalidate L1 cache, set/way 0.
test_complete
END_FUNCTION
// Enable the MMU.
FUNCTION vcpu_enable_mmu
// Write a value to raw memory.
movlit x14, 0x0123456789abcdef
write_region GUEST_SCRATCH_ADDR, MEMORY_TEST_BYTES, x14 // clobbers x0, x1
// Turn on the MMU.
setup_and_enable_mmu // clobbers x0, x1
// Ensure we can read from memory, and we get our previously written value.
//
// Clobbers x0 -- x2, L1
check_region GUEST_SCRATCH_ADDR, MEMORY_TEST_BYTES, x14, 1f
test_complete
1:
test_failure
END_FUNCTION
// Enable and disable the MMU.
FUNCTION vcpu_enable_disable_mmu
// Turn on the MMU.
setup_and_enable_mmu // clobbers x0, x1
// Write a magic word to every word on a page of cached memory.
movlit x14, 0x1122334455667788
write_region GUEST_SCRATCH_ADDR, MEMORY_TEST_BYTES, x14 // clobbers x0, x1
// Turn off the MMU.
disable_mmu // clobbers x0
// Write the contents of the scratch space back to main memory.
clean_region GUEST_SCRATCH_ADDR, MEMORY_TEST_BYTES // clobbers x0 -- x3.
// The value written into cache should now have been cleaned to main
// memory, and should be visible even though caches are off.
check_region GUEST_SCRATCH_ADDR, MEMORY_TEST_BYTES, x14, .Lfailure // clobbers x0 -- x2
// Write out a different value to uncached RAM.
movlit x14, 0x8877665544332211
write_region GUEST_SCRATCH_ADDR, MEMORY_TEST_BYTES, x14 // clobbers x0, x1, L1
// Turn the MMU on again.
setup_and_enable_mmu
// Verify that what we wrote is visible, even with caches on.
//
// We've already invalidated caches and we are supposed to be the
// only thing running on this system, so we shouldn't need to
// invalidate any cache lines.
check_region GUEST_SCRATCH_ADDR, MEMORY_TEST_BYTES, x14, .Lfailure // clobbers x0 -- x2
test_complete
// Failure
.Lfailure:
test_failure
END_FUNCTION
// A guest that just exits each time it is resumed.
FUNCTION vcpu_always_exit
1:
mov x0, TRAP_ADDR
str xzr, [x0]
b 1b
END_FUNCTION