Move common VM functionality into a library.

Bug: 223166344
Test: Ran unprotected VM under crosvm.
Change-Id: I3b027c3a5de037c2adc75cf816518d8db5e10587
diff --git a/pvmfw/Android.bp b/pvmfw/Android.bp
index 3d77b2c..7385288 100644
--- a/pvmfw/Android.bp
+++ b/pvmfw/Android.bp
@@ -13,8 +13,7 @@
         "libcore.rust_sysroot",
     ],
     rustlibs: [
-        "libpsci",
-        "libspin_nostd",
+        "libvmbase",
     ],
     enabled: false,
     target: {
diff --git a/pvmfw/src/exceptions.rs b/pvmfw/src/exceptions.rs
index f67a524..61f7846 100644
--- a/pvmfw/src/exceptions.rs
+++ b/pvmfw/src/exceptions.rs
@@ -14,61 +14,59 @@
 
 //! Exception handlers.
 
-use crate::console::emergency_write_str;
-use crate::eprintln;
 use core::arch::asm;
-use psci::system_reset;
+use vmbase::{console::emergency_write_str, eprintln, power::reboot};
 
 #[no_mangle]
 extern "C" fn sync_exception_current() {
     emergency_write_str("sync_exception_current\n");
     print_esr();
-    system_reset().unwrap();
+    reboot();
 }
 
 #[no_mangle]
 extern "C" fn irq_current() {
     emergency_write_str("irq_current\n");
-    system_reset().unwrap();
+    reboot();
 }
 
 #[no_mangle]
 extern "C" fn fiq_current() {
     emergency_write_str("fiq_current\n");
-    system_reset().unwrap();
+    reboot();
 }
 
 #[no_mangle]
 extern "C" fn serr_current() {
     emergency_write_str("serr_current\n");
     print_esr();
-    system_reset().unwrap();
+    reboot();
 }
 
 #[no_mangle]
 extern "C" fn sync_lower() {
     emergency_write_str("sync_lower\n");
     print_esr();
-    system_reset().unwrap();
+    reboot();
 }
 
 #[no_mangle]
 extern "C" fn irq_lower() {
     emergency_write_str("irq_lower\n");
-    system_reset().unwrap();
+    reboot();
 }
 
 #[no_mangle]
 extern "C" fn fiq_lower() {
     emergency_write_str("fiq_lower\n");
-    system_reset().unwrap();
+    reboot();
 }
 
 #[no_mangle]
 extern "C" fn serr_lower() {
     emergency_write_str("serr_lower\n");
     print_esr();
-    system_reset().unwrap();
+    reboot();
 }
 
 #[inline]
diff --git a/pvmfw/src/main.rs b/pvmfw/src/main.rs
index 395c252..3fe3435 100644
--- a/pvmfw/src/main.rs
+++ b/pvmfw/src/main.rs
@@ -17,12 +17,9 @@
 #![no_main]
 #![no_std]
 
-mod console;
 mod exceptions;
-mod uart;
 
-use core::panic::PanicInfo;
-use psci::{system_off, system_reset};
+use vmbase::{console, power::shutdown, println};
 
 /// Entry point for pVM firmware.
 #[no_mangle]
@@ -30,14 +27,5 @@
     console::init();
     println!("Hello world");
 
-    system_off().unwrap();
-    #[allow(clippy::empty_loop)]
-    loop {}
-}
-
-#[panic_handler]
-fn panic(info: &PanicInfo) -> ! {
-    eprintln!("{}", info);
-    system_reset().unwrap();
-    loop {}
+    shutdown();
 }
diff --git a/vmbase/Android.bp b/vmbase/Android.bp
new file mode 100644
index 0000000..972cd1b
--- /dev/null
+++ b/vmbase/Android.bp
@@ -0,0 +1,18 @@
+rust_library_rlib {
+    name: "libvmbase",
+    host_supported: false,
+    crate_name: "vmbase",
+    srcs: ["src/lib.rs"],
+    edition: "2021",
+    rustlibs: [
+        "libpsci",
+        "libspin_nostd",
+    ],
+    enabled: false,
+    target: {
+        android_arm64: {
+            enabled: true,
+        },
+    },
+    apex_available: ["com.android.virt"],
+}
diff --git a/pvmfw/src/console.rs b/vmbase/src/console.rs
similarity index 100%
rename from pvmfw/src/console.rs
rename to vmbase/src/console.rs
diff --git a/vmbase/src/lib.rs b/vmbase/src/lib.rs
new file mode 100644
index 0000000..0901e04
--- /dev/null
+++ b/vmbase/src/lib.rs
@@ -0,0 +1,30 @@
+// 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
+//
+//     http://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.
+
+//! Basic functionality for bare-metal binaries to run in a VM under crosvm.
+
+#![no_std]
+
+pub mod console;
+pub mod power;
+pub mod uart;
+
+use core::panic::PanicInfo;
+use power::reboot;
+
+#[panic_handler]
+fn panic(info: &PanicInfo) -> ! {
+    eprintln!("{}", info);
+    reboot()
+}
diff --git a/vmbase/src/power.rs b/vmbase/src/power.rs
new file mode 100644
index 0000000..10a5e5d
--- /dev/null
+++ b/vmbase/src/power.rs
@@ -0,0 +1,35 @@
+// 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
+//
+//     http://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.
+
+//! Functions for shutting down the VM.
+
+use psci::{system_off, system_reset};
+
+/// Makes a `PSCI_SYSTEM_OFF` call to shutdown the VM.
+///
+/// Panics if it returns an error.
+pub fn shutdown() -> ! {
+    system_off().unwrap();
+    #[allow(clippy::empty_loop)]
+    loop {}
+}
+
+/// Makes a `PSCI_SYSTEM_RESET` call to shutdown the VM abnormally.
+///
+/// Panics if it returns an error.
+pub fn reboot() -> ! {
+    system_reset().unwrap();
+    #[allow(clippy::empty_loop)]
+    loop {}
+}
diff --git a/pvmfw/src/uart.rs b/vmbase/src/uart.rs
similarity index 100%
rename from pvmfw/src/uart.rs
rename to vmbase/src/uart.rs