Infrastructure for running tests under Linux in primary VM.

Includes an initial test of simply inserting and removing the Hafnium
kernel module.

Change-Id: I832a30d902f58ca71f89374300ab39b2ba3ab877
diff --git a/build/image/generate_initrd.py b/build/image/generate_initrd.py
index a49865a..6fb76de 100644
--- a/build/image/generate_initrd.py
+++ b/build/image/generate_initrd.py
@@ -39,15 +39,14 @@
     parser.add_argument("--staging", required=True)
     parser.add_argument("--output", required=True)
     args = parser.parse_args()
+    staged_files = ["vmlinuz", "initrd.img"]
     # Prepare the primary VM image.
-    staged_files = ["vmlinuz"]
     shutil.copyfile(args.primary_vm, os.path.join(args.staging, "vmlinuz"))
-    # Prepare the primary VM's initrd. Currently, it just makes an empty one.
+    # Prepare the primary VM's initrd.
     if args.primary_vm_initrd:
-        raise NotImplementedError(
-            "This doesn't copy the primary VM's initrd yet")
-    with open(os.path.join(args.staging, "initrd.img"), "w") as vms_txt:
-        staged_files.append("initrd.img")
+        shutil.copyfile(args.primary_vm_initrd, os.path.join(args.staging, "initrd.img"))
+    else:
+        open(os.path.join(args.staging, "initrd.img"), "w").close()
     # Prepare the secondary VMs.
     with open(os.path.join(args.staging, "vms.txt"), "w") as vms_txt:
         staged_files.append("vms.txt")
diff --git a/build/image/generate_linux_initrd.py b/build/image/generate_linux_initrd.py
new file mode 100644
index 0000000..06f6502
--- /dev/null
+++ b/build/image/generate_linux_initrd.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python
+#
+# Copyright 2019 The Hafnium Authors.
+#
+# 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.
+
+"""Generate an initial RAM disk for a Linux VM."""
+
+import argparse
+import os
+import shutil
+import subprocess
+import sys
+
+def Main():
+    parser = argparse.ArgumentParser()
+    parser.add_argument("--staging", required=True)
+    parser.add_argument("--output", required=True)
+    args = parser.parse_args()
+    # Package files into an initial RAM disk.
+    with open(args.output, "w") as initrd:
+        # Move into the staging directory so the file names taken by cpio don't
+        # include the path.
+        os.chdir(args.staging)
+        staged_files = [os.path.join(root, filename)
+          for (root, dirs, files) in os.walk(".") for filename in files + dirs]
+        cpio = subprocess.Popen(
+            ["cpio", "--create", "--format=newc"],
+            stdin=subprocess.PIPE,
+            stdout=initrd,
+            stderr=subprocess.PIPE)
+        cpio.communicate(input="\n".join(staged_files).encode("utf-8"))
+    return 0
+
+
+if __name__ == "__main__":
+    sys.exit(Main())
diff --git a/build/image/image.gni b/build/image/image.gni
index 67af5a3..61a407a 100644
--- a/build/image/image.gni
+++ b/build/image/image.gni
@@ -112,10 +112,47 @@
   }
 }
 
+# Build the initial RAM disk for the Linux VM.
+template("linux_initrd") {
+  initrd_base = "${target_out_dir}/${target_name}/initrd"
+  initrd_file = "${initrd_base}.img"
+  initrd_staging = "${initrd_base}"
+
+  copy("${target_name}__staging") {
+    forward_variables_from(invoker,
+                           [
+                             "testonly",
+                             "sources",
+                             "deps",
+                           ])
+    outputs = [
+      "${initrd_staging}/{{source_file_part}}",
+    ]
+  }
+
+  action(target_name) {
+    forward_variables_from(invoker, [ "testonly" ])
+    script = "//build/image/generate_linux_initrd.py"
+    args = [
+      "--staging",
+      rebase_path(initrd_staging),
+      "--output",
+      rebase_path(initrd_file),
+    ]
+    deps = [
+      ":${target_name}__staging",
+    ]
+    outputs = [
+      initrd_file,
+    ]
+  }
+}
+
 # Build the initial RAM disk for the hypervisor.
 template("initrd") {
-  assert(defined(invoker.primary_vm),
-         "initrd() must specify a \"primary_vm\" value")
+  assert(
+      defined(invoker.primary_vm) || defined(invoker.primary_vm_prebuilt),
+      "initrd() must specify a \"primary_vm\" or \"primary_vm_prebuilt\" value")
 
   action(target_name) {
     forward_variables_from(invoker, [ "testonly" ])
@@ -125,19 +162,32 @@
     initrd_file = "${initrd_base}.img"
     initrd_staging = "${initrd_base}"
 
-    deps = [
-      invoker.primary_vm,
-    ]
+    deps = []
 
-    primary_vm_outputs = get_target_outputs(invoker.primary_vm)
+    if (defined(invoker.primary_vm_prebuilt)) {
+      primary_vm_output = invoker.primary_vm_prebuilt
+    } else {
+      primary_vm_output =
+          get_label_info(invoker.primary_vm, "target_out_dir") + "/" +
+          get_label_info(invoker.primary_vm, "name") + ".bin"
+      deps += [ invoker.primary_vm ]
+    }
     args = [
       "--primary_vm",
-      rebase_path(primary_vm_outputs[0]),
+      rebase_path(primary_vm_output),
       "--staging",
       rebase_path(initrd_staging),
       "--output",
       rebase_path(initrd_file),
     ]
+    if (defined(invoker.primary_initrd)) {
+      deps += [ invoker.primary_initrd ]
+      primary_initrd_outputs = get_target_outputs(invoker.primary_initrd)
+      args += [
+        "--primary_vm_initrd",
+        rebase_path(primary_initrd_outputs[0]),
+      ]
+    }
 
     # Add the info about the secondary VMs. The information about the VMs is
     # encoded in lists with the following elements:
diff --git a/build/make.py b/build/make.py
new file mode 100644
index 0000000..d4cc0d8
--- /dev/null
+++ b/build/make.py
@@ -0,0 +1,44 @@
+#!/usr/bin/env python
+#
+# Copyright 2019 The Hafnium Authors.
+#
+# 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.
+
+"""Runs make to build a target."""
+
+import argparse
+import os
+import shutil
+import subprocess
+import sys
+
+
+def Main():
+    parser = argparse.ArgumentParser()
+    parser.add_argument("--directory", required=True)
+    parser.add_argument("--out_file", required=True)
+    parser.add_argument("--copy_out_file", required=True)
+    args, make_args = parser.parse_known_args()
+
+    os.chdir(args.directory)
+    os.environ["PWD"] = args.directory
+    status = subprocess.call(["make"] + make_args)
+    if status != 0:
+        return status
+
+    shutil.copyfile(args.out_file, args.copy_out_file)
+    return 0
+
+
+if __name__ == "__main__":
+    sys.exit(Main())
diff --git a/build/toolchain/BUILD.gn b/build/toolchain/BUILD.gn
index 861bd18..0bfc56b 100644
--- a/build/toolchain/BUILD.gn
+++ b/build/toolchain/BUILD.gn
@@ -12,8 +12,31 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import("//build/toolchain/embedded.gni")
 import("//build/toolchain/host.gni")
 
 host_toolchain("host") {
   use_platform = false
 }
+
+embedded_clang_toolchain("aarch64_linux_clang") {
+  target = "aarch64-linux-musleabi"
+
+  # TODO: Remove //inc/system if we can stop using the version of stdatomic.h
+  # from the Android prebuilt Clang.
+  extra_cflags =
+      "-nostdinc -isystem" +
+      rebase_path("//prebuilts/linux-aarch64/musl/include") + " -isystem" +
+      rebase_path("//prebuilts/linux-x64/clang/lib64/clang/8.0.4/include") +
+      " -isystem" + rebase_path("//inc/system")
+  extra_defines = "-D_LIBCPP_HAS_MUSL_LIBC=1 -D_GNU_SOURCE=1"
+  extra_ldflags = "-no-pie -lc --library-path=" +
+                  rebase_path("//prebuilts/linux-aarch64/musl/lib/") + " " +
+                  rebase_path("//prebuilts/linux-aarch64/musl/lib/crt1.o") +
+                  " " + rebase_path(
+                      "//prebuilts/linux-x64/clang/lib64/clang/8.0.4/lib/linux/libclang_rt.builtins-aarch64-android.a")
+  toolchain_args = {
+    use_platform = true
+    plat_arch = "fake"
+  }
+}