Merge "Microdroid runs on cuttlefish_x86_64"
diff --git a/apex/Android.bp b/apex/Android.bp
index 5357566..41d2f62 100644
--- a/apex/Android.bp
+++ b/apex/Android.bp
@@ -20,12 +20,26 @@
                 "authfs", // TODO(victorhsieh): move to microdroid once we can run the test in VM.
                 "crosvm",
             ],
+            filesystems: [
+                "microdroid_super",
+                "microdroid_boot-5.10",
+                "microdroid_vendor_boot-5.10",
+                "microdroid_vbmeta",
+                "microdroid_vbmeta_system",
+            ],
         },
         x86_64: {
             binaries: [
                 "authfs", // TODO(victorhsieh): move to microdroid once we can run the test in VM.
                 "crosvm",
             ],
+            filesystems: [
+                "microdroid_super",
+                "microdroid_boot-5.10",
+                "microdroid_vendor_boot-5.10",
+                "microdroid_vbmeta",
+                "microdroid_vbmeta_system",
+            ],
         },
     },
     binaries: [
@@ -34,8 +48,13 @@
         "virtmanager",
         "vm",
     ],
-    filesystems: ["microdroid"],
-    prebuilts: ["com.android.virt.init.rc"],
+    prebuilts: [
+        "com.android.virt.init.rc",
+        "microdroid_cdisk.json",
+        "microdroid_cdisk_env.json",
+        "microdroid_uboot_env",
+        "microdroid_bootloader",
+    ],
     file_contexts: ":com.android.virt-file_contexts",
 }
 
diff --git a/docs/getting_started/index.md b/docs/getting_started/index.md
index e43124c..9c06367 100644
--- a/docs/getting_started/index.md
+++ b/docs/getting_started/index.md
@@ -16,15 +16,14 @@
 [packages/modules/Virtualization](https://android.googlesource.com/platform/packages/modules/Virtualization)
 of the AOSP repository.
 
-### Host-side tests
+### Device-side tests
 
-These are tests where the test driver runs on the "host" (your computer) and it issues commands to
-the "target" (the connected device or emulator) over ADB. The tests spawn guest VMs and test
-different aspects of the architecture.
+The tests spawn guest VMs and test different aspects of the architecture.
 
 You can build and run them with:
-``` shell
-atest VirtualizationHostTestCases
+
+```shell
+atest VirtualizationTestCases
 ```
 
 If you run into problems, inspect the logs produced by `atest`. Their location is printed at the
@@ -35,36 +34,47 @@
 [CrosVM](https://android.googlesource.com/platform/external/crosvm/) is a Rust-based Virtual Machine
 Monitor (VMM) originally built for ChromeOS and ported to Android.
 
-It is not installed in regular Android builds (yet!), but it's installed in the
-VIM3L (yukawa) build, as part of the `com.android.virt` APEX.
-builds.
+It is not installed in regular Android builds (yet!), but it's installed in the VIM3L (yukawa)
+build, as part of the `com.android.virt` APEX.
 
 ### Spawning your own VMs
 
-You can spawn your own VMs by running CrosVM directly on a rooted KVM-enabled device. If your
-device is attached over ADB, you can run:
-``` shell
+You can spawn your own VMs by passing a JSON config file to the Virt Manager via the `vm` tool on a
+rooted KVM-enabled device. If your device is attached over ADB, you can run:
+
+```shell
+$ cat > vm_config.json
+{
+  "kernel": "/data/local/tmp/kernel",
+  "initrd": "/data/local/tmp/ramdisk",
+  "params": "rdinit=/bin/init"
+}
 $ adb root
 $ adb push <kernel> /data/local/tmp/kernel
 $ adb push <ramdisk> /data/local/tmp/ramdisk
-$ adb shell /apex/com.android.virt/bin/crosvm run --initrd /data/local/tmp/ramdisk /data/local/tmp/kernel
+$ adb push vm_config.json /data/local/tmp/vm_config.json
+$ adb shell "start virtmanager"
+$ adb shell "/apex/com.android.virt/bin/vm run /data/local/tmp/vm_config.json"
 ```
 
-### Building and updating CrosVM
+The `vm` command also has other subcommands for debugging; run `/apex/com.android.virt/bin/vm help`
+for details.
 
-You can update CrosVM by updating the `com.android.virt` APEX where CrosVM is
-in. If your device already has `com.android.virt` (e.g. VIM3L),
+### Building and updating CrosVM and Virt Manager
 
-``` shell
-$ m com.android.virt
-$ adb install out/target/product/<device_name>/system/apex/com.android.virt.apex
+You can update CrosVM and the Virt Manager service by updating the `com.android.virt` APEX. If your
+device already has `com.android.virt` (e.g. VIM3L):
+
+```shell
+$ TARGET_BUILD_APPS="com.android.virt" m
+$ adb install $ANDROID_PRODUCT_OUT/system/apex/com.android.virt.apex
 $ adb reboot
 ```
 
 If it doesn't have the APEX yet, you first need to place it manually to the
 system partition.
 
-``` shell
+```shell
 $ adb root
 $ adb disable-verity
 $ adb reboot
diff --git a/microdroid/Android.bp b/microdroid/Android.bp
index 08870ba..2ce435b 100644
--- a/microdroid/Android.bp
+++ b/microdroid/Android.bp
@@ -364,3 +364,8 @@
     name: "microdroid_cdisk.json",
     src: "microdroid_cdisk.json",
 }
+
+prebuilt_etc {
+    name: "microdroid_cdisk_env.json",
+    src: "microdroid_cdisk_env.json",
+}
diff --git a/microdroid/README.md b/microdroid/README.md
index ffc8302..a4bfb6e 100644
--- a/microdroid/README.md
+++ b/microdroid/README.md
@@ -10,50 +10,33 @@
 You need a VIM3L board. Instructions for building Android for the target, and
 flashing the image can be found [here](../docs/getting_started/yukawa.md).
 
-Then you build microdroid. Note that the instruction below is very likely to
-change in the future, because this is in active development. For example, the
-`microdroid_*` modules will eventually be included in the `com.android.virt`
-APEX, which is already in the `yukawa` (VIM3L) target.
+Then you install `com.android.virt` APEX. All files needed to run microdroid are
+included in the APEX, which is already in the `yukawa` (VIM3L) target. You can
+of course build and install the APEX manually.
 
 ```
 $ source build/envsetup.sh
 $ choosecombo 1 aosp_arm64 userdebug // actually, any arm64-based target is ok
-$ m microdroid_super
-$ m microdroid_boot-5.10
-$ m microdroid_vendor_boot-5.10
-$ m microdroid_bootloader
-$ m microdroid_uboot_env
-$ m microdroid_vbmeta
-$ m microdroid_vbmeta_system
-$ m microdroid_cdisk.json
-```
-
-## Installing
-
-Push the built files to the device. In addition to that, some other files have
-to be manually created, for now. In the future, you won't need these.
-
-```
-$ adb push $ANDROID_PRODUCT_OUT/system/etc/microdroid_bootloader /data/local/tmp/bootloader
-$ adb push $ANDROID_PRODUCT_OUT/system/etc/microdroid_super.img /data/local/tmp/super.img
-$ adb push $ANDROID_PRODUCT_OUT/system/etc/microdroid_boot-5.10.img /data/local/tmp/boot.img
-$ adb push $ANDROID_PRODUCT_OUT/system/etc/microdroid_vendor_boot-5.10.img /data/local/tmp/vendor_boot.img
-$ adb push $ANDROID_PRODUCT_OUT/system/etc/microdroid_vbmeta.img /data/local/tmp/vbmeta.img
-$ adb push $ANDROID_PRODUCT_OUT/system/etc/microdroid_vbmeta_system.img /data/local/tmp/vbmeta_system.img
-$ adb push $ANDROID_PRODUCT_OUT/system/etc/uboot_env.img /data/local/tmp
-$ adb push $ANDROID_PRODUCT_OUT/system/etc/microdroid_cdisk.json /data/local/tmp
-$ dd if=/dev/zero of=misc.img bs=4k count=256
-$ adb push misc.img /data/local/tmp/
+$ m com.android.virt
+$ adb install $ANDROID_PRODUCT_OUT/system/apex/com.android.virt.apex
+$ adb reboot
 ```
 
 ## Running
 
-Create the composite image using `assemble_cvd` and run it via `crosvm`. In the
-future, this shall be done via [`virtmanager`](../virtmanager/).
+Copy the artifacts to the temp directory, create the composite image using
+`mk_cdisk`, and run it via `crosvm`. For now, some other files have to be
+manually created. In the future, you won't need these, and this shall be done
+via [`virtmanager`](../virtmanager/).
 
 ```
-$ adb shell 'cd /data/local/tmp; /apex/com.android.virt/bin/mk_cdisk microdroid_cdisk.json os_composite.img'
-$ adb shell 'cd /data/local/tmp; /apex/com.android.virt/bin/crosvm run --cid=5 --disable-sandbox --bios=bootloader --serial=type=stdout --disk=os_composite.img'
+$ adb shell 'cp /apex/com.android.virt/etc/microdroid_bootloader /data/local/tmp/bootloader'
+$ adb shell 'cp /apex/com.android.virt/etc/fs/*.img /data/local/tmp'
+$ adb shell 'cp /apex/com.android.virt/etc/uboot_env.img /data/local/tmp'
+$ adb shell 'dd if=/dev/zero of=/data/local/tmp/misc.img bs=4k count=256'
+$ adb shell 'cd /data/local/tmp; /apex/com.android.virt/bin/mk_cdisk /apex/com.android.virt/etc/microdroid_cdisk.json os_composite.img'
+$ adb shell 'cd /data/local/tmp; /apex/com.android.virt/bin/mk_cdisk /apex/com.android.virt/etc/microdroid_cdisk_env.json env_composite.img'
+$ adb shell 'cd /data/local/tmp; /apex/com.android.virt/bin/crosvm run --cid=5 --disable-sandbox --bios=bootloader --serial=type=stdout --disk=os_composite.img --disk=env_composite.img'
 ```
 
 The CID in `--cid` parameter can be anything greater than 2 (`VMADDR_CID_HOST`).
diff --git a/microdroid/microdroid_cdisk.json b/microdroid/microdroid_cdisk.json
index 2bf9fcf..591e83b 100644
--- a/microdroid/microdroid_cdisk.json
+++ b/microdroid/microdroid_cdisk.json
@@ -1,58 +1,53 @@
 {
   "partitions": [
     {
-      "label": "uboot_env",
-      "path": "uboot_env.img",
-      "read_only": true
-    },
-    {
       "label": "misc",
       "path": "misc.img",
       "read_only": true
     },
     {
       "label": "boot_a",
-      "path": "boot.img",
+      "path": "microdroid_boot-5.10.img",
       "read_only": true
     },
     {
       "label": "boot_b",
-      "path": "boot.img",
+      "path": "microdroid_boot-5.10.img",
       "read_only": true
     },
     {
       "label": "vendor_boot_a",
-      "path": "vendor_boot.img",
+      "path": "microdroid_vendor_boot-5.10.img",
       "read_only": true
     },
     {
       "label": "vendor_boot_b",
-      "path": "vendor_boot.img",
+      "path": "microdroid_vendor_boot-5.10.img",
       "read_only": true
     },
     {
       "label": "vbmeta_a",
-      "path": "vbmeta.img",
+      "path": "microdroid_vbmeta.img",
       "read_only": true
     },
     {
       "label": "vbmeta_b",
-      "path": "vbmeta.img",
+      "path": "microdroid_vbmeta.img",
       "read_only": true
     },
     {
       "label": "vbmeta_system_a",
-      "path": "vbmeta_system.img",
+      "path": "microdroid_vbmeta_system.img",
       "read_only": true
     },
     {
       "label": "vbmeta_system_b",
-      "path": "vbmeta_system.img",
+      "path": "microdroid_vbmeta_system.img",
       "read_only": true
     },
     {
       "label": "super",
-      "path": "super.img",
+      "path": "microdroid_super.img",
       "read_only": true
     }
   ]
diff --git a/microdroid/microdroid_cdisk_env.json b/microdroid/microdroid_cdisk_env.json
new file mode 100644
index 0000000..f43c4b1
--- /dev/null
+++ b/microdroid/microdroid_cdisk_env.json
@@ -0,0 +1,9 @@
+{
+  "partitions": [
+    {
+      "label": "uboot_env",
+      "path": "uboot_env.img",
+      "read_only": true
+    }
+  ]
+}
diff --git a/tests/hostside/Android.bp b/tests/hostside/Android.bp
index 37a9c5b..429b737 100644
--- a/tests/hostside/Android.bp
+++ b/tests/hostside/Android.bp
@@ -7,22 +7,4 @@
     srcs: ["java/**/*.java"],
     test_suites: ["device-tests"],
     libs: ["tradefed"],
-    data: [
-        ":microdroid_cdisk.json",
-        ":microdroid_super",
-        ":microdroid_boot-5.10",
-        ":microdroid_vendor_boot-5.10",
-        ":microdroid_uboot_env",
-        ":microdroid_bootloader",
-        ":MicrodroidHostTestCase_misc",
-        ":microdroid_vbmeta",
-        ":microdroid_vbmeta_system",
-    ],
-}
-
-genrule {
-    name: "MicrodroidHostTestCase_misc",
-    out: ["misc.img"],
-    cmd: "dd if=/dev/zero of=$(out) bs=4k count=256",
-    visibility: ["//visibility:private"],
 }
diff --git a/tests/hostside/java/android/virt/test/MicrodroidTestCase.java b/tests/hostside/java/android/virt/test/MicrodroidTestCase.java
index 5bfa4b3..57c7b17 100644
--- a/tests/hostside/java/android/virt/test/MicrodroidTestCase.java
+++ b/tests/hostside/java/android/virt/test/MicrodroidTestCase.java
@@ -30,9 +30,6 @@
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
-import java.io.File;
-import java.nio.file.Path;
-import java.nio.file.Paths;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 
@@ -45,17 +42,6 @@
     private static final String MICRODROID_SERIAL = "localhost:" + TEST_VM_ADB_PORT;
     private static final long MICRODROID_BOOT_TIMEOUT_MILLIS = 15000;
 
-    private void pushFile(String localName, String remoteName) {
-        try {
-            File localFile = getTestInformation().getDependencyFile(localName, false);
-            Path remotePath = Paths.get(TEST_ROOT, remoteName);
-            getDevice().executeShellCommand("mkdir -p " + remotePath.getParent());
-            getDevice().pushFile(localFile, remotePath.toString());
-        } catch (Exception e) {
-            throw new RuntimeException(e);
-        }
-    }
-
     private String executeCommand(String cmd) {
         final long defaultCommandTimeoutMillis = 1000; // 1 sec
         return executeCommand(defaultCommandTimeoutMillis, cmd);
@@ -69,26 +55,33 @@
     @Test
     public void testMicrodroidBoots() throws Exception {
         // Prepare input files
-        pushFile("microdroid_cdisk.json", "microdroid_cdisk.json");
-        pushFile("microdroid_bootloader", "bootloader");
-        pushFile("microdroid_super.img", "super.img");
-        pushFile("microdroid_boot-5.10.img", "boot.img");
-        pushFile("microdroid_vendor_boot-5.10.img", "vendor_boot.img");
-        pushFile("uboot_env.img", "uboot_env.img");
-        pushFile("misc.img", "misc.img");
-        pushFile("microdroid_vbmeta.img", "vbmeta.img");
-        pushFile("microdroid_vbmeta_system.img", "vbmeta_system.img");
+        String prepareImagesCmd =
+                String.format(
+                        "mkdir -p %s; cd %s; "
+                                + "cp %setc/microdroid_bootloader bootloader && "
+                                + "cp %setc/fs/*.img . && "
+                                + "cp %setc/uboot_env.img . && "
+                                + "dd if=/dev/zero of=misc.img bs=4k count=256",
+                        TEST_ROOT, TEST_ROOT, VIRT_APEX, VIRT_APEX, VIRT_APEX);
+        getDevice().executeShellCommand(prepareImagesCmd);
 
-        // Create os_composite.img
+        // Create os_composite.img and env_composite.img
         String makeOsCompositeCmd =
                 String.format(
-                        "cd %s; %sbin/mk_cdisk microdroid_cdisk.json os_composite.img",
-                        TEST_ROOT, VIRT_APEX);
+                        "cd %s; %sbin/mk_cdisk %setc/microdroid_cdisk.json os_composite.img",
+                        TEST_ROOT, VIRT_APEX, VIRT_APEX);
         getDevice().executeShellCommand(makeOsCompositeCmd);
+        String makeEnvCompositeCmd =
+                String.format(
+                        "cd %s; %sbin/mk_cdisk %setc/microdroid_cdisk_env.json env_composite.img",
+                        TEST_ROOT, VIRT_APEX, VIRT_APEX);
+        getDevice().executeShellCommand(makeEnvCompositeCmd);
 
-        // Make sure that os_composite.img is created
+        // Make sure that the composite images are created
         final String compositeImg = TEST_ROOT + "/os_composite.img";
-        CommandResult result = getDevice().executeShellV2Command("du -b " + compositeImg);
+        final String envCompositeImg = TEST_ROOT + "/env_composite.img";
+        CommandResult result =
+                getDevice().executeShellV2Command("du -b " + compositeImg + " " + envCompositeImg);
         assertThat(result.getExitCode(), is(0));
         assertThat(result.getStdout(), is(not("")));
 
@@ -97,7 +90,8 @@
         String runMicrodroidCmd =
                 String.format(
                         "cd %s; %sbin/crosvm run --cid=%d --disable-sandbox --bios=bootloader"
-                                + " --serial=type=syslog --disk=os_composite.img",
+                                + " --serial=type=syslog --disk=os_composite.img"
+                                + " --disk=env_composite.img",
                         TEST_ROOT, VIRT_APEX, TEST_VM_CID);
         executor.execute(
                 () -> {
diff --git a/virtmanager/Android.bp b/virtmanager/Android.bp
index 37c3bcc..83ac3d6 100644
--- a/virtmanager/Android.bp
+++ b/virtmanager/Android.bp
@@ -11,7 +11,6 @@
     rustlibs: [
         "android.system.virtmanager-rust",
         "libandroid_logger",
-        "libbinder_rs", // TODO(dbrazdil): remove once b/182890877 is fixed
         "liblog_rust",
         "libserde_json",
         "libserde",
diff --git a/virtmanager/aidl/android/system/virtmanager/VirtualMachineDebugInfo.aidl b/virtmanager/aidl/android/system/virtmanager/VirtualMachineDebugInfo.aidl
index d1ba9a6..3a271cb 100644
--- a/virtmanager/aidl/android/system/virtmanager/VirtualMachineDebugInfo.aidl
+++ b/virtmanager/aidl/android/system/virtmanager/VirtualMachineDebugInfo.aidl
@@ -19,13 +19,16 @@
 parcelable VirtualMachineDebugInfo {
     /** The CID assigned to the VM. */
     int cid;
+
     /** The UID of the process which requested the VM. */
-    int requester_uid;
+    int requesterUid;
+
     /** The SID of the process which requested the VM. */
-    @nullable String requester_sid;
+    @nullable String requesterSid;
+
     /**
      * The PID of the process which requested the VM. Note that this process may no longer exist and
      * the PID may have been reused for a different process, so this should not be trusted.
      */
-    int requester_pid;
+    int requesterPid;
 }
diff --git a/virtmanager/src/aidl.rs b/virtmanager/src/aidl.rs
index 98af714..96ba04f 100644
--- a/virtmanager/src/aidl.rs
+++ b/virtmanager/src/aidl.rs
@@ -17,7 +17,6 @@
 use crate::config::VmConfig;
 use crate::crosvm::VmInstance;
 use crate::{Cid, FIRST_GUEST_CID};
-use ::binder::FromIBinder; // TODO(dbrazdil): remove once b/182890877 is fixed
 use android_system_virtmanager::aidl::android::system::virtmanager::IVirtManager::IVirtManager;
 use android_system_virtmanager::aidl::android::system::virtmanager::IVirtualMachine::{
     BnVirtualMachine, IVirtualMachine,
@@ -97,9 +96,9 @@
             .into_iter()
             .map(|vm| VirtualMachineDebugInfo {
                 cid: vm.cid as i32,
-                requester_uid: vm.requester_uid as i32,
-                requester_sid: vm.requester_sid.clone(),
-                requester_pid: vm.requester_pid,
+                requesterUid: vm.requester_uid as i32,
+                requesterSid: vm.requester_sid.clone(),
+                requesterPid: vm.requester_pid,
             })
             .collect();
         Ok(cids)
@@ -107,16 +106,13 @@
 
     /// Hold a strong reference to a VM in Virt Manager. This method is only intended for debug
     /// purposes, and as such is only permitted from the shell user.
-    fn debugHoldVmRef(&self, vmref: &dyn IVirtualMachine) -> binder::Result<()> {
+    fn debugHoldVmRef(&self, vmref: &Strong<dyn IVirtualMachine>) -> binder::Result<()> {
         if !debug_access_allowed() {
             return Err(StatusCode::PERMISSION_DENIED.into());
         }
 
-        // Workaround for b/182890877.
-        let vm: Strong<dyn IVirtualMachine> = FromIBinder::try_from(vmref.as_binder()).unwrap();
-
         let state = &mut *self.state.lock().unwrap();
-        state.debug_hold_vm(vm);
+        state.debug_hold_vm(vmref.clone());
         Ok(())
     }
 
diff --git a/vm/src/main.rs b/vm/src/main.rs
index 6c1052f..f47d9e8 100644
--- a/vm/src/main.rs
+++ b/vm/src/main.rs
@@ -89,7 +89,7 @@
 
     if daemonize {
         // Pass the VM reference back to Virt Manager and have it hold it in the background.
-        virt_manager.debugHoldVmRef(&*vm).context("Failed to pass VM to Virt Manager")
+        virt_manager.debugHoldVmRef(&vm).context("Failed to pass VM to Virt Manager")
     } else {
         // Wait until the VM dies. If we just returned immediately then the IVirtualMachine Binder
         // object would be dropped and the VM would be killed.