Move std.c to arch to avoid conflicts with libc for tests.

Change-Id: I0fc966962413ee12f84058d702e7396e38460f27
diff --git a/inc/hf/std.h b/inc/hf/arch/std.h
similarity index 98%
rename from inc/hf/std.h
rename to inc/hf/arch/std.h
index 6609978..c8a03a5 100644
--- a/inc/hf/std.h
+++ b/inc/hf/arch/std.h
@@ -64,6 +64,7 @@
 #define align_down(v, a) ((uintptr_t)(v) & ~(a - 1))
 #endif
 
+#ifndef be16toh
 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
 
 #define be16toh(v) __builtin_bswap16(v)
@@ -110,3 +111,4 @@
 #error "Unsupported byte order"
 
 #endif
+#endif
diff --git a/src/BUILD.gn b/src/BUILD.gn
index d3069ae..cef8a7f 100644
--- a/src/BUILD.gn
+++ b/src/BUILD.gn
@@ -55,10 +55,10 @@
   ]
 
   deps = [
-    ":common",
     ":fdt",
     ":memiter",
     "//src/arch/${plat_arch}",
+    "//src/arch/${plat_arch}:std",
   ]
 
   if (is_debug) {
@@ -66,13 +66,6 @@
   }
 }
 
-# Code that is not specific to a certain image so can be shared.
-source_set("common") {
-  sources = [
-    "std.c",
-  ]
-}
-
 # Debug code that is not specific to a certain image so can be shared.
 source_set("dlog") {
   sources = [
@@ -80,8 +73,8 @@
   ]
 
   deps = [
-    ":common",
     "//src/arch/${plat_arch}:putchar",
+    "//src/arch/${plat_arch}:std",
   ]
 }
 
@@ -92,7 +85,7 @@
   ]
 
   deps = [
-    ":common",
+    "//src/arch/${plat_arch}:std",
   ]
 
   if (is_debug) {
diff --git a/src/api.c b/src/api.c
index ba89203..a155bb6 100644
--- a/src/api.c
+++ b/src/api.c
@@ -19,12 +19,12 @@
 #include <assert.h>
 
 #include "hf/arch/cpu.h"
+#include "hf/arch/std.h"
 #include "hf/arch/timer.h"
 
 #include "hf/dlog.h"
 #include "hf/mm.h"
 #include "hf/spinlock.h"
-#include "hf/std.h"
 #include "hf/vm.h"
 
 #include "vmapi/hf/call.h"
diff --git a/src/arch/aarch64/BUILD.gn b/src/arch/aarch64/BUILD.gn
index 2d6fdf9..b7e6461 100644
--- a/src/arch/aarch64/BUILD.gn
+++ b/src/arch/aarch64/BUILD.gn
@@ -45,6 +45,12 @@
   ]
 }
 
+source_set("std") {
+  sources = [
+    "std.c",
+  ]
+}
+
 # Entry code to prepare the loaded image to be run.
 source_set("entry") {
   sources = [
diff --git a/src/arch/aarch64/cpu.c b/src/arch/aarch64/cpu.c
index 4a8114a..61be67b 100644
--- a/src/arch/aarch64/cpu.c
+++ b/src/arch/aarch64/cpu.c
@@ -20,8 +20,9 @@
 #include <stddef.h>
 #include <stdint.h>
 
+#include "hf/arch/std.h"
+
 #include "hf/addr.h"
-#include "hf/std.h"
 
 void arch_irq_disable(void)
 {
diff --git a/src/std.c b/src/arch/aarch64/std.c
similarity index 98%
rename from src/std.c
rename to src/arch/aarch64/std.c
index 45263c8..94de77c 100644
--- a/src/std.c
+++ b/src/arch/aarch64/std.c
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "hf/std.h"
+#include "hf/arch/std.h"
 
 void *memset(void *s, int c, size_t n)
 {
diff --git a/src/arch/fake/BUILD.gn b/src/arch/fake/BUILD.gn
index c3c404b..a004782 100644
--- a/src/arch/fake/BUILD.gn
+++ b/src/arch/fake/BUILD.gn
@@ -26,3 +26,7 @@
     "putchar.c",
   ]
 }
+
+# Empty, as the functions are provided by libc already.
+source_set("std") {
+}
diff --git a/src/cpio.c b/src/cpio.c
index 01002d8..0a57fdd 100644
--- a/src/cpio.c
+++ b/src/cpio.c
@@ -18,7 +18,7 @@
 
 #include <stdint.h>
 
-#include "hf/std.h"
+#include "hf/arch/std.h"
 
 #pragma pack(push, 1)
 struct cpio_header {
diff --git a/src/cpu.c b/src/cpu.c
index 536fee1..a29c22d 100644
--- a/src/cpu.c
+++ b/src/cpu.c
@@ -19,10 +19,10 @@
 #include <stdalign.h>
 
 #include "hf/arch/cpu.h"
+#include "hf/arch/std.h"
 
 #include "hf/api.h"
 #include "hf/dlog.h"
-#include "hf/std.h"
 #include "hf/vm.h"
 
 #include "vmapi/hf/call.h"
diff --git a/src/dlog.c b/src/dlog.c
index 1ce5566..a0c1d15 100644
--- a/src/dlog.c
+++ b/src/dlog.c
@@ -20,9 +20,9 @@
 #include <stddef.h>
 
 #include "hf/arch/console.h"
+#include "hf/arch/std.h"
 
 #include "hf/spinlock.h"
-#include "hf/std.h"
 
 /* Keep macro alignment */
 /* clang-format off */
diff --git a/src/fdt.c b/src/fdt.c
index 3c06784..691b733 100644
--- a/src/fdt.c
+++ b/src/fdt.c
@@ -18,8 +18,9 @@
 
 #include <stdint.h>
 
+#include "hf/arch/std.h"
+
 #include "hf/dlog.h"
-#include "hf/std.h"
 
 struct fdt_header {
 	uint32_t magic;
diff --git a/src/fdt_handler.c b/src/fdt_handler.c
index f41fa8d..61b688d 100644
--- a/src/fdt_handler.c
+++ b/src/fdt_handler.c
@@ -16,13 +16,14 @@
 
 #include "hf/fdt_handler.h"
 
+#include "hf/arch/std.h"
+
 #include "hf/boot_params.h"
 #include "hf/cpu.h"
 #include "hf/dlog.h"
 #include "hf/fdt.h"
 #include "hf/layout.h"
 #include "hf/mm.h"
-#include "hf/std.h"
 
 static uint64_t convert_number(const char *data, uint32_t size)
 {
diff --git a/src/layout.c b/src/layout.c
index fe11c91..403d2d4 100644
--- a/src/layout.c
+++ b/src/layout.c
@@ -16,7 +16,7 @@
 
 #include "hf/layout.h"
 
-#include "hf/std.h"
+#include "hf/arch/std.h"
 
 /**
  * Get the address the .text section begins at.
diff --git a/src/load.c b/src/load.c
index 30de46f..d573473 100644
--- a/src/load.c
+++ b/src/load.c
@@ -19,13 +19,14 @@
 #include <assert.h>
 #include <stdbool.h>
 
+#include "hf/arch/std.h"
+
 #include "hf/api.h"
 #include "hf/boot_params.h"
 #include "hf/dlog.h"
 #include "hf/layout.h"
 #include "hf/memiter.h"
 #include "hf/mm.h"
-#include "hf/std.h"
 #include "hf/vm.h"
 
 #include "vmapi/hf/call.h"
diff --git a/src/main.c b/src/main.c
index 3457f25..90912c2 100644
--- a/src/main.c
+++ b/src/main.c
@@ -19,6 +19,7 @@
 #include <stdnoreturn.h>
 
 #include "hf/arch/init.h"
+#include "hf/arch/std.h"
 
 #include "hf/api.h"
 #include "hf/boot_params.h"
@@ -28,7 +29,6 @@
 #include "hf/load.h"
 #include "hf/mm.h"
 #include "hf/mpool.h"
-#include "hf/std.h"
 #include "hf/vm.h"
 
 #include "vmapi/hf/call.h"
diff --git a/src/memiter.c b/src/memiter.c
index 8b9854c..f6064bb 100644
--- a/src/memiter.c
+++ b/src/memiter.c
@@ -16,7 +16,7 @@
 
 #include "hf/memiter.h"
 
-#include "hf/std.h"
+#include "hf/arch/std.h"
 
 /**
  * Initialises the given memory iterator.
diff --git a/src/vm.c b/src/vm.c
index b68c219..a597e3f 100644
--- a/src/vm.c
+++ b/src/vm.c
@@ -16,9 +16,10 @@
 
 #include "hf/vm.h"
 
+#include "hf/arch/std.h"
+
 #include "hf/api.h"
 #include "hf/cpu.h"
-#include "hf/std.h"
 
 #include "vmapi/hf/call.h"
 
diff --git a/test/hftest/BUILD.gn b/test/hftest/BUILD.gn
index 51968c1..4090832 100644
--- a/test/hftest/BUILD.gn
+++ b/test/hftest/BUILD.gn
@@ -42,10 +42,10 @@
   ]
 
   deps = [
-    "//src:common",
     "//src:dlog",
     "//src:memiter",
     "//src/arch/${plat_arch}:entry",
+    "//src/arch/${plat_arch}:std",
     "//src/arch/${plat_arch}/hftest:entry",
     "//src/arch/${plat_arch}/hftest:hf_call",
     "//src/arch/${plat_arch}/hftest:power_mgmt",
@@ -72,11 +72,11 @@
   ]
 
   deps = [
-    "//src:common",
     "//src:dlog",
     "//src:fdt",
     "//src:memiter",
     "//src/arch/${plat_arch}:entry",
+    "//src/arch/${plat_arch}:std",
     "//src/arch/${plat_arch}/hftest:entry",
     "//src/arch/${plat_arch}/hftest:power_mgmt",
   ]
diff --git a/test/hftest/hftest.c b/test/hftest/hftest.c
index 04a07a6..1016a9a 100644
--- a/test/hftest/hftest.c
+++ b/test/hftest/hftest.c
@@ -19,11 +19,11 @@
 #include <stdalign.h>
 #include <stdint.h>
 
+#include "hf/arch/std.h"
 #include "hf/arch/vm/power_mgmt.h"
 
 #include "hf/fdt.h"
 #include "hf/memiter.h"
-#include "hf/std.h"
 
 alignas(4096) uint8_t kstack[4096];
 
diff --git a/test/hftest/hftest_service.c b/test/hftest/hftest_service.c
index 048d267..896d0e9 100644
--- a/test/hftest/hftest_service.c
+++ b/test/hftest/hftest_service.c
@@ -17,8 +17,9 @@
 #include <stdalign.h>
 #include <stdint.h>
 
+#include "hf/arch/std.h"
+
 #include "hf/memiter.h"
-#include "hf/std.h"
 
 #include "vmapi/hf/call.h"
 
diff --git a/test/hftest/inc/hftest_impl.h b/test/hftest/inc/hftest_impl.h
index 39e50a9..2519595 100644
--- a/test/hftest/inc/hftest_impl.h
+++ b/test/hftest/inc/hftest_impl.h
@@ -18,7 +18,7 @@
 
 #include <stdnoreturn.h>
 
-#include "hf/std.h"
+#include "hf/arch/std.h"
 
 /*
  * Log with the HFTEST_LOG_PREFIX and a new line. The zero is added so there is
diff --git a/test/vmapi/gicv3/busy_secondary.c b/test/vmapi/gicv3/busy_secondary.c
index 99117a2..14f2eb2 100644
--- a/test/vmapi/gicv3/busy_secondary.c
+++ b/test/vmapi/gicv3/busy_secondary.c
@@ -15,10 +15,10 @@
  */
 
 #include "hf/arch/cpu.h"
+#include "hf/arch/std.h"
 #include "hf/arch/vm/interrupts_gicv3.h"
 
 #include "hf/dlog.h"
-#include "hf/std.h"
 
 #include "vmapi/hf/call.h"
 
diff --git a/test/vmapi/gicv3/gicv3.c b/test/vmapi/gicv3/gicv3.c
index 91ab6e6..9868c96 100644
--- a/test/vmapi/gicv3/gicv3.c
+++ b/test/vmapi/gicv3/gicv3.c
@@ -17,11 +17,11 @@
 #include "gicv3.h"
 
 #include "hf/arch/cpu.h"
+#include "hf/arch/std.h"
 #include "hf/arch/vm/interrupts_gicv3.h"
 
 #include "hf/dlog.h"
 #include "hf/mm.h"
-#include "hf/std.h"
 
 #include "vmapi/hf/call.h"
 
diff --git a/test/vmapi/gicv3/interrupts.c b/test/vmapi/gicv3/interrupts.c
index 7b6d1b7..1e9f2b1 100644
--- a/test/vmapi/gicv3/interrupts.c
+++ b/test/vmapi/gicv3/interrupts.c
@@ -15,10 +15,10 @@
  */
 
 #include "hf/arch/cpu.h"
+#include "hf/arch/std.h"
 #include "hf/arch/vm/interrupts_gicv3.h"
 
 #include "hf/dlog.h"
-#include "hf/std.h"
 
 #include "vmapi/hf/call.h"
 
diff --git a/test/vmapi/gicv3/services/timer.c b/test/vmapi/gicv3/services/timer.c
index b469a75..dc5ca3f 100644
--- a/test/vmapi/gicv3/services/timer.c
+++ b/test/vmapi/gicv3/services/timer.c
@@ -17,10 +17,10 @@
 #include "hf/arch/vm/timer.h"
 
 #include "hf/arch/cpu.h"
+#include "hf/arch/std.h"
 #include "hf/arch/vm/interrupts_gicv3.h"
 
 #include "hf/dlog.h"
-#include "hf/std.h"
 
 #include "vmapi/hf/call.h"
 
diff --git a/test/vmapi/primary_with_secondaries/interrupts.c b/test/vmapi/primary_with_secondaries/interrupts.c
index ad61833..5dab1d8 100644
--- a/test/vmapi/primary_with_secondaries/interrupts.c
+++ b/test/vmapi/primary_with_secondaries/interrupts.c
@@ -16,7 +16,7 @@
 
 #include <stdint.h>
 
-#include "hf/std.h"
+#include "hf/arch/std.h"
 
 #include "vmapi/hf/call.h"
 
diff --git a/test/vmapi/primary_with_secondaries/mailbox.c b/test/vmapi/primary_with_secondaries/mailbox.c
index bd5b588..f9ae00c 100644
--- a/test/vmapi/primary_with_secondaries/mailbox.c
+++ b/test/vmapi/primary_with_secondaries/mailbox.c
@@ -16,7 +16,7 @@
 
 #include <stdint.h>
 
-#include "hf/std.h"
+#include "hf/arch/std.h"
 
 #include "vmapi/hf/call.h"
 
diff --git a/test/vmapi/primary_with_secondaries/memory_sharing.c b/test/vmapi/primary_with_secondaries/memory_sharing.c
index 6f8a389..0a397b9 100644
--- a/test/vmapi/primary_with_secondaries/memory_sharing.c
+++ b/test/vmapi/primary_with_secondaries/memory_sharing.c
@@ -16,8 +16,9 @@
 
 #include <stdint.h>
 
+#include "hf/arch/std.h"
+
 #include "hf/mm.h"
-#include "hf/std.h"
 
 #include "vmapi/hf/call.h"
 
diff --git a/test/vmapi/primary_with_secondaries/no_services.c b/test/vmapi/primary_with_secondaries/no_services.c
index 13adca0..2cbf642 100644
--- a/test/vmapi/primary_with_secondaries/no_services.c
+++ b/test/vmapi/primary_with_secondaries/no_services.c
@@ -18,8 +18,9 @@
 #include <stdalign.h>
 #include <stdint.h>
 
+#include "hf/arch/std.h"
+
 #include "hf/mm.h"
-#include "hf/std.h"
 
 #include "vmapi/hf/call.h"
 
diff --git a/test/vmapi/primary_with_secondaries/run_race.c b/test/vmapi/primary_with_secondaries/run_race.c
index dc7a6a4..ff21cec 100644
--- a/test/vmapi/primary_with_secondaries/run_race.c
+++ b/test/vmapi/primary_with_secondaries/run_race.c
@@ -18,10 +18,10 @@
 #include <stdalign.h>
 #include <stdint.h>
 
+#include "hf/arch/std.h"
 #include "hf/arch/vm/power_mgmt.h"
 
 #include "hf/mm.h"
-#include "hf/std.h"
 
 #include "vmapi/hf/call.h"
 
diff --git a/test/vmapi/primary_with_secondaries/services/abort.c b/test/vmapi/primary_with_secondaries/services/abort.c
index 8652caf..e1de6c8 100644
--- a/test/vmapi/primary_with_secondaries/services/abort.c
+++ b/test/vmapi/primary_with_secondaries/services/abort.c
@@ -14,8 +14,9 @@
  * limitations under the License.
  */
 
+#include "hf/arch/std.h"
+
 #include "hf/mm.h"
-#include "hf/std.h"
 
 #include "vmapi/hf/call.h"
 
diff --git a/test/vmapi/primary_with_secondaries/services/check_state.c b/test/vmapi/primary_with_secondaries/services/check_state.c
index 10063c5..e3a9d64 100644
--- a/test/vmapi/primary_with_secondaries/services/check_state.c
+++ b/test/vmapi/primary_with_secondaries/services/check_state.c
@@ -14,10 +14,9 @@
  * limitations under the License.
  */
 
+#include "hf/arch/std.h"
 #include "hf/arch/vm/state.h"
 
-#include "hf/std.h"
-
 #include "vmapi/hf/call.h"
 
 #include "hftest.h"
diff --git a/test/vmapi/primary_with_secondaries/services/echo.c b/test/vmapi/primary_with_secondaries/services/echo.c
index 3c38f3f..288b198 100644
--- a/test/vmapi/primary_with_secondaries/services/echo.c
+++ b/test/vmapi/primary_with_secondaries/services/echo.c
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "hf/std.h"
+#include "hf/arch/std.h"
 
 #include "vmapi/hf/call.h"
 
diff --git a/test/vmapi/primary_with_secondaries/services/echo_with_notification.c b/test/vmapi/primary_with_secondaries/services/echo_with_notification.c
index 80c4324..5f6502f 100644
--- a/test/vmapi/primary_with_secondaries/services/echo_with_notification.c
+++ b/test/vmapi/primary_with_secondaries/services/echo_with_notification.c
@@ -15,10 +15,9 @@
  */
 
 #include "hf/arch/cpu.h"
+#include "hf/arch/std.h"
 #include "hf/arch/vm/interrupts_gicv3.h"
 
-#include "hf/std.h"
-
 #include "vmapi/hf/call.h"
 
 #include "../msr.h"
diff --git a/test/vmapi/primary_with_secondaries/services/interruptible.c b/test/vmapi/primary_with_secondaries/services/interruptible.c
index adcffbd..264601c 100644
--- a/test/vmapi/primary_with_secondaries/services/interruptible.c
+++ b/test/vmapi/primary_with_secondaries/services/interruptible.c
@@ -15,10 +15,10 @@
  */
 
 #include "hf/arch/cpu.h"
+#include "hf/arch/std.h"
 #include "hf/arch/vm/interrupts_gicv3.h"
 
 #include "hf/dlog.h"
-#include "hf/std.h"
 
 #include "vmapi/hf/call.h"
 
diff --git a/test/vmapi/primary_with_secondaries/services/memory.c b/test/vmapi/primary_with_secondaries/services/memory.c
index 938ef4a..1392332 100644
--- a/test/vmapi/primary_with_secondaries/services/memory.c
+++ b/test/vmapi/primary_with_secondaries/services/memory.c
@@ -14,8 +14,9 @@
  * limitations under the License.
  */
 
+#include "hf/arch/std.h"
+
 #include "hf/mm.h"
-#include "hf/std.h"
 
 #include "vmapi/hf/call.h"
 
diff --git a/test/vmapi/primary_with_secondaries/services/relay.c b/test/vmapi/primary_with_secondaries/services/relay.c
index d90e029..b96db5b 100644
--- a/test/vmapi/primary_with_secondaries/services/relay.c
+++ b/test/vmapi/primary_with_secondaries/services/relay.c
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "hf/std.h"
+#include "hf/arch/std.h"
 
 #include "vmapi/hf/call.h"