Move test utilities to common directory, and use them more widely.

Change-Id: Ib7397eccf811fec6a746254a613936c8466ac9e6
diff --git a/test/vmapi/primary_with_secondaries/BUILD.gn b/test/vmapi/primary_with_secondaries/BUILD.gn
index 3a38f26..8554d4b 100644
--- a/test/vmapi/primary_with_secondaries/BUILD.gn
+++ b/test/vmapi/primary_with_secondaries/BUILD.gn
@@ -43,24 +43,13 @@
   ]
 
   deps = [
-    ":util",
     "//src/arch/aarch64/hftest:registers",
     "//test/hftest:hftest_primary_vm",
+    "//test/vmapi/common",
     "//vmlib",
   ]
 }
 
-source_set("util") {
-  testonly = true
-  public_configs = [
-    ":config",
-    "//test/hftest:hftest_config",
-  ]
-  sources = [
-    "util.c",
-  ]
-}
-
 initrd("primary_with_secondaries_test") {
   testonly = true
 
diff --git a/test/vmapi/primary_with_secondaries/abort.c b/test/vmapi/primary_with_secondaries/abort.c
index dea9611..07eea62 100644
--- a/test/vmapi/primary_with_secondaries/abort.c
+++ b/test/vmapi/primary_with_secondaries/abort.c
@@ -18,7 +18,7 @@
 
 #include "primary_with_secondary.h"
 #include "test/hftest.h"
-#include "util.h"
+#include "test/vmapi/spci.h"
 
 /**
  * Accessing unmapped memory aborts the VM.
diff --git a/test/vmapi/primary_with_secondaries/boot.c b/test/vmapi/primary_with_secondaries/boot.c
index 7902659..ef97715 100644
--- a/test/vmapi/primary_with_secondaries/boot.c
+++ b/test/vmapi/primary_with_secondaries/boot.c
@@ -20,7 +20,7 @@
 
 #include "primary_with_secondary.h"
 #include "test/hftest.h"
-#include "util.h"
+#include "test/vmapi/spci.h"
 
 /**
  * The VM gets its memory size on boot, and can access it all.
diff --git a/test/vmapi/primary_with_secondaries/debug_el1.c b/test/vmapi/primary_with_secondaries/debug_el1.c
index 217b149..2dd5312 100644
--- a/test/vmapi/primary_with_secondaries/debug_el1.c
+++ b/test/vmapi/primary_with_secondaries/debug_el1.c
@@ -16,7 +16,7 @@
 
 #include "primary_with_secondary.h"
 #include "sysregs.h"
-#include "util.h"
+#include "test/vmapi/spci.h"
 
 /**
  * QEMU does not properly handle the trapping of certain system register
diff --git a/test/vmapi/primary_with_secondaries/floating_point.c b/test/vmapi/primary_with_secondaries/floating_point.c
index a12328d..1904d68 100644
--- a/test/vmapi/primary_with_secondaries/floating_point.c
+++ b/test/vmapi/primary_with_secondaries/floating_point.c
@@ -24,7 +24,7 @@
 #include "../msr.h"
 #include "primary_with_secondary.h"
 #include "test/hftest.h"
-#include "util.h"
+#include "test/vmapi/spci.h"
 
 /**
  * Test that floating point registers are saved and restored by
diff --git a/test/vmapi/primary_with_secondaries/inc/util.h b/test/vmapi/primary_with_secondaries/inc/util.h
deleted file mode 100644
index 3ecebf6..0000000
--- a/test/vmapi/primary_with_secondaries/inc/util.h
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * Copyright 2018 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.
- */
-
-#pragma once
-
-#include "vmapi/hf/spci.h"
-
-#define EXPECT_SPCI_ERROR(value, spci_error)          \
-	do {                                          \
-		EXPECT_EQ(value.func, SPCI_ERROR_32); \
-		EXPECT_EQ(value.arg2, spci_error);    \
-	} while (0)
-
-struct mailbox_buffers {
-	void *send;
-	void *recv;
-};
-
-struct mailbox_buffers set_up_mailbox(void);
diff --git a/test/vmapi/primary_with_secondaries/interrupts.c b/test/vmapi/primary_with_secondaries/interrupts.c
index ac64049..5aaf830 100644
--- a/test/vmapi/primary_with_secondaries/interrupts.c
+++ b/test/vmapi/primary_with_secondaries/interrupts.c
@@ -22,7 +22,7 @@
 
 #include "primary_with_secondary.h"
 #include "test/hftest.h"
-#include "util.h"
+#include "test/vmapi/spci.h"
 
 /**
  * Send a message to the interruptible VM, which will interrupt itself to send a
diff --git a/test/vmapi/primary_with_secondaries/mailbox.c b/test/vmapi/primary_with_secondaries/mailbox.c
index f08c696..c58115a 100644
--- a/test/vmapi/primary_with_secondaries/mailbox.c
+++ b/test/vmapi/primary_with_secondaries/mailbox.c
@@ -23,7 +23,7 @@
 
 #include "primary_with_secondary.h"
 #include "test/hftest.h"
-#include "util.h"
+#include "test/vmapi/spci.h"
 
 /**
  * Reverses the order of the elements in the given array.
diff --git a/test/vmapi/primary_with_secondaries/memory_sharing.c b/test/vmapi/primary_with_secondaries/memory_sharing.c
index e6cc222..efe0275 100644
--- a/test/vmapi/primary_with_secondaries/memory_sharing.c
+++ b/test/vmapi/primary_with_secondaries/memory_sharing.c
@@ -23,7 +23,7 @@
 
 #include "primary_with_secondary.h"
 #include "test/hftest.h"
-#include "util.h"
+#include "test/vmapi/spci.h"
 
 alignas(PAGE_SIZE) static uint8_t pages[4 * PAGE_SIZE];
 
diff --git a/test/vmapi/primary_with_secondaries/no_services.c b/test/vmapi/primary_with_secondaries/no_services.c
index 0c83a8b..2e06c78 100644
--- a/test/vmapi/primary_with_secondaries/no_services.c
+++ b/test/vmapi/primary_with_secondaries/no_services.c
@@ -25,7 +25,7 @@
 
 #include "primary_with_secondary.h"
 #include "test/hftest.h"
-#include "util.h"
+#include "test/vmapi/spci.h"
 
 static alignas(PAGE_SIZE) uint8_t send_page[PAGE_SIZE];
 static alignas(PAGE_SIZE) uint8_t recv_page[PAGE_SIZE];
diff --git a/test/vmapi/primary_with_secondaries/perfmon.c b/test/vmapi/primary_with_secondaries/perfmon.c
index c325190..4edc81b 100644
--- a/test/vmapi/primary_with_secondaries/perfmon.c
+++ b/test/vmapi/primary_with_secondaries/perfmon.c
@@ -19,7 +19,7 @@
 #include "../../src/arch/aarch64/hypervisor/sysregs.h"
 #include "primary_with_secondary.h"
 #include "sysregs.h"
-#include "util.h"
+#include "test/vmapi/spci.h"
 
 /*
  * TODO(b/132394973): Devise a way to test exhaustively read/write behavior to
diff --git a/test/vmapi/primary_with_secondaries/run_race.c b/test/vmapi/primary_with_secondaries/run_race.c
index 672815d..f06f2a7 100644
--- a/test/vmapi/primary_with_secondaries/run_race.c
+++ b/test/vmapi/primary_with_secondaries/run_race.c
@@ -26,7 +26,7 @@
 
 #include "primary_with_secondary.h"
 #include "test/hftest.h"
-#include "util.h"
+#include "test/vmapi/spci.h"
 
 /**
  * Iterates trying to run vCPU of the secondary VM. Returns when a message
diff --git a/test/vmapi/primary_with_secondaries/services/BUILD.gn b/test/vmapi/primary_with_secondaries/services/BUILD.gn
index dc395c0..728692f 100644
--- a/test/vmapi/primary_with_secondaries/services/BUILD.gn
+++ b/test/vmapi/primary_with_secondaries/services/BUILD.gn
@@ -94,7 +94,7 @@
     "//test/hftest:hftest_config",
   ]
   deps = [
-    "//test/vmapi/primary_with_secondaries:util",
+    "//test/vmapi/common",
     "//vmlib",
   ]
 
@@ -155,7 +155,7 @@
   deps = [
     "//src/arch/aarch64:arch",
     "//src/arch/aarch64/hftest:interrupts",
-    "//test/vmapi/primary_with_secondaries:util",
+    "//test/vmapi/common",
   ]
 }
 
@@ -204,7 +204,7 @@
     "//test/hftest:hftest_config",
   ]
   deps = [
-    "//test/vmapi/primary_with_secondaries:util",
+    "//test/vmapi/common",
   ]
 
   sources = [
diff --git a/test/vmapi/primary_with_secondaries/services/memory.c b/test/vmapi/primary_with_secondaries/services/memory.c
index 524f006..58015e2 100644
--- a/test/vmapi/primary_with_secondaries/services/memory.c
+++ b/test/vmapi/primary_with_secondaries/services/memory.c
@@ -21,7 +21,7 @@
 
 #include "primary_with_secondary.h"
 #include "test/hftest.h"
-#include "util.h"
+#include "test/vmapi/spci.h"
 
 alignas(PAGE_SIZE) static uint8_t page[PAGE_SIZE];
 
diff --git a/test/vmapi/primary_with_secondaries/services/receive_block.c b/test/vmapi/primary_with_secondaries/services/receive_block.c
index 1fa34ab..49ea5f2 100644
--- a/test/vmapi/primary_with_secondaries/services/receive_block.c
+++ b/test/vmapi/primary_with_secondaries/services/receive_block.c
@@ -24,7 +24,7 @@
 
 #include "primary_with_secondary.h"
 #include "test/hftest.h"
-#include "util.h"
+#include "test/vmapi/spci.h"
 
 /*
  * Secondary VM that enables an interrupt, disables interrupts globally, and
diff --git a/test/vmapi/primary_with_secondaries/services/spci_check.c b/test/vmapi/primary_with_secondaries/services/spci_check.c
index 05715d4..9e342ba 100644
--- a/test/vmapi/primary_with_secondaries/services/spci_check.c
+++ b/test/vmapi/primary_with_secondaries/services/spci_check.c
@@ -21,7 +21,7 @@
 
 #include "primary_with_secondary.h"
 #include "test/hftest.h"
-#include "util.h"
+#include "test/vmapi/spci.h"
 
 TEST_SERVICE(spci_check)
 {
diff --git a/test/vmapi/primary_with_secondaries/smp.c b/test/vmapi/primary_with_secondaries/smp.c
index 1199d83..ea1fc00 100644
--- a/test/vmapi/primary_with_secondaries/smp.c
+++ b/test/vmapi/primary_with_secondaries/smp.c
@@ -22,7 +22,7 @@
 
 #include "primary_with_secondary.h"
 #include "test/hftest.h"
-#include "util.h"
+#include "test/vmapi/spci.h"
 
 /**
  * Run a service that starts a second vCPU, and check that both the first and
diff --git a/test/vmapi/primary_with_secondaries/spci.c b/test/vmapi/primary_with_secondaries/spci.c
index 6862333..a3cfbca 100644
--- a/test/vmapi/primary_with_secondaries/spci.c
+++ b/test/vmapi/primary_with_secondaries/spci.c
@@ -24,7 +24,7 @@
 
 #include "primary_with_secondary.h"
 #include "test/hftest.h"
-#include "util.h"
+#include "test/vmapi/spci.h"
 
 /**
  * Send a message to a secondary VM which checks the validity of the received
diff --git a/test/vmapi/primary_with_secondaries/sysregs.c b/test/vmapi/primary_with_secondaries/sysregs.c
index 3b31d71..f15a692 100644
--- a/test/vmapi/primary_with_secondaries/sysregs.c
+++ b/test/vmapi/primary_with_secondaries/sysregs.c
@@ -19,7 +19,7 @@
 #include "hf/arch/vm/interrupts.h"
 
 #include "primary_with_secondary.h"
-#include "util.h"
+#include "test/vmapi/spci.h"
 
 /**
  * Tracks the number of times the exception handler has been invoked.
diff --git a/test/vmapi/primary_with_secondaries/util.c b/test/vmapi/primary_with_secondaries/util.c
deleted file mode 100644
index 19657c4..0000000
--- a/test/vmapi/primary_with_secondaries/util.c
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * Copyright 2018 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.
- */
-
-#include "util.h"
-
-#include "hf/mm.h"
-#include "hf/spci.h"
-#include "hf/static_assert.h"
-
-#include "vmapi/hf/call.h"
-
-#include "test/hftest.h"
-
-static alignas(PAGE_SIZE) uint8_t send_page[PAGE_SIZE];
-static alignas(PAGE_SIZE) uint8_t recv_page[PAGE_SIZE];
-static_assert(sizeof(send_page) == PAGE_SIZE, "Send page is not a page.");
-static_assert(sizeof(recv_page) == PAGE_SIZE, "Recv page is not a page.");
-
-static hf_ipaddr_t send_page_addr = (hf_ipaddr_t)send_page;
-static hf_ipaddr_t recv_page_addr = (hf_ipaddr_t)recv_page;
-
-struct mailbox_buffers set_up_mailbox(void)
-{
-	ASSERT_EQ(spci_rxtx_map(send_page_addr, recv_page_addr).func,
-		  SPCI_SUCCESS_32);
-	return (struct mailbox_buffers){
-		.send = send_page,
-		.recv = recv_page,
-	};
-}