Merge "Revert "Set cpu affinity for time benchmarks""
diff --git a/pvmfw/Android.bp b/pvmfw/Android.bp
index fb639d8..eab9474 100644
--- a/pvmfw/Android.bp
+++ b/pvmfw/Android.bp
@@ -20,6 +20,7 @@
     defaults: ["vmbase_elf_defaults"],
     srcs: [
         "idmap.S",
+        "payload.S",
     ],
     static_libs: [
         "libpvmfw",
diff --git a/pvmfw/payload.S b/pvmfw/payload.S
new file mode 100644
index 0000000..cbda448
--- /dev/null
+++ b/pvmfw/payload.S
@@ -0,0 +1,85 @@
+/*
+ * Copyright 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+.macro mov_i, reg:req, imm:req
+	movz \reg, :abs_g3:\imm
+	movk \reg, :abs_g2_nc:\imm
+	movk \reg, :abs_g1_nc:\imm
+	movk \reg, :abs_g0_nc:\imm
+.endm
+
+/* Stage 1 instruction access cacheability is unaffected. */
+.set .L_SCTLR_ELx_I, 0x1 << 12
+/* SETEND instruction disabled at EL0 in aarch32 mode. */
+.set .L_SCTLR_EL1_SED, 0x1 << 8
+/* Various IT instructions are disabled at EL0 in aarch32 mode. */
+.set .L_SCTLR_EL1_ITD, 0x1 << 7
+.set .L_SCTLR_EL1_RES1, (0x1 << 11) | (0x1 << 20) | (0x1 << 22) | (0x1 << 28) | (0x1 << 29)
+.set .Lsctlrval, .L_SCTLR_ELx_I | .L_SCTLR_EL1_SED | .L_SCTLR_EL1_ITD | .L_SCTLR_EL1_RES1
+
+/**
+ * Disable the exception vector, caches and page talbe and then jump to the payload at the given
+ * address, passing it the given FDT pointer.
+ *
+ * x0: FDT address to pass to payload
+ * x1: Payload address
+ */
+.global start_payload
+start_payload:
+        /* Move payload address to a higher register and zero out parameters other than x0. */
+        mov x30, x1
+        mov x1, #0
+        mov x2, #0
+        mov x3, #0
+
+        /* Zero out remaining registers to avoid leaking data. */
+        mov x4, #0
+        mov x5, #0
+        mov x6, #0
+        mov x7, #0
+        mov x8, #0
+        mov x9, #0
+        mov x10, #0
+        mov x11, #0
+        mov x12, #0
+        mov x13, #0
+        mov x14, #0
+        mov x15, #0
+        mov x16, #0
+        mov x17, #0
+        mov x18, #0
+        mov x19, #0
+        mov x20, #0
+        mov x21, #0
+        mov x22, #0
+        mov x23, #0
+        mov x24, #0
+        mov x25, #0
+        mov x26, #0
+        mov x27, #0
+        mov x28, #0
+
+        /* Disable the MMU and cache, and set other settings to valid warm reset values. */
+        mov_i x29, .Lsctlrval
+        msr sctlr_el1, x29
+        isb
+        msr ttbr0_el1, xzr
+
+        isb
+        dsb nsh
+
+        /* Jump into the payload. */
+        br x30
diff --git a/pvmfw/src/exceptions.rs b/pvmfw/src/exceptions.rs
index 61f7846..0e637ac 100644
--- a/pvmfw/src/exceptions.rs
+++ b/pvmfw/src/exceptions.rs
@@ -18,52 +18,52 @@
 use vmbase::{console::emergency_write_str, eprintln, power::reboot};
 
 #[no_mangle]
-extern "C" fn sync_exception_current() {
+extern "C" fn sync_exception_current(_elr: u64, _spsr: u64) {
     emergency_write_str("sync_exception_current\n");
     print_esr();
     reboot();
 }
 
 #[no_mangle]
-extern "C" fn irq_current() {
+extern "C" fn irq_current(_elr: u64, _spsr: u64) {
     emergency_write_str("irq_current\n");
     reboot();
 }
 
 #[no_mangle]
-extern "C" fn fiq_current() {
+extern "C" fn fiq_current(_elr: u64, _spsr: u64) {
     emergency_write_str("fiq_current\n");
     reboot();
 }
 
 #[no_mangle]
-extern "C" fn serr_current() {
+extern "C" fn serr_current(_elr: u64, _spsr: u64) {
     emergency_write_str("serr_current\n");
     print_esr();
     reboot();
 }
 
 #[no_mangle]
-extern "C" fn sync_lower() {
+extern "C" fn sync_lower(_elr: u64, _spsr: u64) {
     emergency_write_str("sync_lower\n");
     print_esr();
     reboot();
 }
 
 #[no_mangle]
-extern "C" fn irq_lower() {
+extern "C" fn irq_lower(_elr: u64, _spsr: u64) {
     emergency_write_str("irq_lower\n");
     reboot();
 }
 
 #[no_mangle]
-extern "C" fn fiq_lower() {
+extern "C" fn fiq_lower(_elr: u64, _spsr: u64) {
     emergency_write_str("fiq_lower\n");
     reboot();
 }
 
 #[no_mangle]
-extern "C" fn serr_lower() {
+extern "C" fn serr_lower(_elr: u64, _spsr: u64) {
     emergency_write_str("serr_lower\n");
     print_esr();
     reboot();
diff --git a/pvmfw/src/main.rs b/pvmfw/src/main.rs
index 0d71f6e..8526a92 100644
--- a/pvmfw/src/main.rs
+++ b/pvmfw/src/main.rs
@@ -30,4 +30,15 @@
         "fdt_address={:#018x}, payload_start={:#018x}, payload_size={:#018x}, x3={:#018x}",
         fdt_address, payload_start, payload_size, arg3,
     );
+
+    println!("Starting payload...");
+    // Safe because this is a function we have implemented in assembly that matches its signature
+    // here.
+    unsafe {
+        start_payload(fdt_address, payload_start);
+    }
+}
+
+extern "C" {
+    fn start_payload(fdt_address: u64, payload_start: u64) -> !;
 }
diff --git a/tests/benchmark_hostside/java/android/avf/test/AVFHostTestCase.java b/tests/benchmark_hostside/java/android/avf/test/AVFHostTestCase.java
index ca4d1d0..c7ee0f2 100644
--- a/tests/benchmark_hostside/java/android/avf/test/AVFHostTestCase.java
+++ b/tests/benchmark_hostside/java/android/avf/test/AVFHostTestCase.java
@@ -34,6 +34,7 @@
 
 import org.junit.After;
 import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
@@ -66,6 +67,7 @@
     private static final String METRIC_PREFIX = "avf_perf/hostside/";
 
     private final MetricsProcessor mMetricsProcessor = new MetricsProcessor(METRIC_PREFIX);
+    @Rule public TestMetrics mMetrics = new TestMetrics();
 
     @Before
     public void setUp() throws Exception {
@@ -86,7 +88,7 @@
 
     @Test
     public void testBootEnableAndDisablePKVM() throws Exception {
-        testPKVMStatusSwitchSupported();
+        skipIfPKVMStatusSwitchNotSupported();
 
         List<Double> bootWithPKVMEnableTime = new ArrayList<>(ROUND_COUNT);
         List<Double> bootWithoutPKVMEnableTime = new ArrayList<>(ROUND_COUNT);
@@ -149,7 +151,7 @@
         reportMetric(bootWithoutCompOsTime, "boot_time_without_compos", "s");
     }
 
-    private void testPKVMStatusSwitchSupported() throws Exception {
+    private void skipIfPKVMStatusSwitchNotSupported() throws Exception {
         if (!getDevice().isStateBootloaderOrFastbootd()) {
             getDevice().rebootIntoBootloader();
         }
@@ -159,13 +161,15 @@
         result = getDevice().executeFastbootCommand("oem", "pkvm", "status");
         rebootFromBootloaderAndWaitBootCompleted();
         assumeTrue(!result.getStderr().contains("Invalid oem command"));
+        // Skip the test if running on a build with pkvm_enabler. Disabling pKVM
+        // for such builds results in a bootloop.
+        assumeTrue(result.getStderr().contains("misc=auto"));
     }
 
     private void reportMetric(List<Double> data, String name, String unit) {
         Map<String, Double> stats = mMetricsProcessor.computeStats(data, name, unit);
-        TestMetrics metrics = new TestMetrics();
         for (Map.Entry<String, Double> entry : stats.entrySet()) {
-            metrics.addTestMetric(entry.getKey(), Double.toString(entry.getValue()));
+            mMetrics.addTestMetric(entry.getKey(), entry.getValue().toString());
         }
     }
 
@@ -195,11 +199,6 @@
         }
         assertWithMessage("Failed to set PKVM status. Reason: " + result)
             .that(result.toString()).ignoringCase().contains(expectedOutput);
-
-        // Skip the test if running on a build with pkvm_enabler. Disabling
-        // pKVM for such build results in a bootloop.
-        assertWithMessage("Expected build with PKVM status misc=auto. Reason: " + result)
-            .that(result.toString()).ignoringCase().contains("misc=auto");
     }
 
     private void rebootFromBootloaderAndWaitBootCompleted() throws Exception {
diff --git a/vmbase/example/src/exceptions.rs b/vmbase/example/src/exceptions.rs
index 61f7846..0e637ac 100644
--- a/vmbase/example/src/exceptions.rs
+++ b/vmbase/example/src/exceptions.rs
@@ -18,52 +18,52 @@
 use vmbase::{console::emergency_write_str, eprintln, power::reboot};
 
 #[no_mangle]
-extern "C" fn sync_exception_current() {
+extern "C" fn sync_exception_current(_elr: u64, _spsr: u64) {
     emergency_write_str("sync_exception_current\n");
     print_esr();
     reboot();
 }
 
 #[no_mangle]
-extern "C" fn irq_current() {
+extern "C" fn irq_current(_elr: u64, _spsr: u64) {
     emergency_write_str("irq_current\n");
     reboot();
 }
 
 #[no_mangle]
-extern "C" fn fiq_current() {
+extern "C" fn fiq_current(_elr: u64, _spsr: u64) {
     emergency_write_str("fiq_current\n");
     reboot();
 }
 
 #[no_mangle]
-extern "C" fn serr_current() {
+extern "C" fn serr_current(_elr: u64, _spsr: u64) {
     emergency_write_str("serr_current\n");
     print_esr();
     reboot();
 }
 
 #[no_mangle]
-extern "C" fn sync_lower() {
+extern "C" fn sync_lower(_elr: u64, _spsr: u64) {
     emergency_write_str("sync_lower\n");
     print_esr();
     reboot();
 }
 
 #[no_mangle]
-extern "C" fn irq_lower() {
+extern "C" fn irq_lower(_elr: u64, _spsr: u64) {
     emergency_write_str("irq_lower\n");
     reboot();
 }
 
 #[no_mangle]
-extern "C" fn fiq_lower() {
+extern "C" fn fiq_lower(_elr: u64, _spsr: u64) {
     emergency_write_str("fiq_lower\n");
     reboot();
 }
 
 #[no_mangle]
-extern "C" fn serr_lower() {
+extern "C" fn serr_lower(_elr: u64, _spsr: u64) {
     emergency_write_str("serr_lower\n");
     print_esr();
     reboot();