[sw] Collect various "runtime-ey" functions into lib/runtime.
This directory is made distinct from libbase mostly for semantic
reasons:
- It needs to know timing-ey things about Ibex, which are isolated in
ibex.h.
- It's more "system-aware" than what we tend to put in libbase.
hart.h contains "execution control" functions that may be
RISC-V-flavored, but independent of ibex.h.
Signed-off-by: Miguel Young de la Sota <mcyoung@google.com>
diff --git a/sw/device/lib/runtime/README.md b/sw/device/lib/runtime/README.md
new file mode 100644
index 0000000..ee9d84a
--- /dev/null
+++ b/sw/device/lib/runtime/README.md
@@ -0,0 +1,5 @@
+# `libruntime`, the OpenTitan Runtime Library
+
+This directory contains machine-aware base libraries for OpenTitan, being more
+aware and able to control the execution environment than the "pure algorithm"
+functions in `libbase`.
diff --git a/sw/device/lib/runtime/hart.c b/sw/device/lib/runtime/hart.c
new file mode 100644
index 0000000..1f02ac9
--- /dev/null
+++ b/sw/device/lib/runtime/hart.c
@@ -0,0 +1,22 @@
+// Copyright lowRISC contributors.
+// Licensed under the Apache License, Version 2.0, see LICENSE for details.
+// SPDX-License-Identifier: Apache-2.0
+
+#include "sw/device/lib/runtime/hart.h"
+
+#include <stdbool.h>
+
+#include "sw/device/lib/runtime/ibex.h"
+
+extern void wait_for_interrupt(void);
+
+void busy_sleep_micros(size_t microseconds) {
+ size_t cycles = kIbexClockFreqHz * microseconds / 10000000;
+ ibex_busy_loop(cycles);
+}
+
+noreturn void abort(void) {
+ while (true) {
+ wait_for_interrupt();
+ }
+}
diff --git a/sw/device/lib/runtime/hart.h b/sw/device/lib/runtime/hart.h
new file mode 100644
index 0000000..2290076
--- /dev/null
+++ b/sw/device/lib/runtime/hart.h
@@ -0,0 +1,40 @@
+// Copyright lowRISC contributors.
+// Licensed under the Apache License, Version 2.0, see LICENSE for details.
+// SPDX-License-Identifier: Apache-2.0
+
+#ifndef SW_DEVICE_LIB_RUNTIME_HART_H_
+#define SW_DEVICE_LIB_RUNTIME_HART_H_
+
+#include <stddef.h>
+#include <stdnoreturn.h>
+
+#include "sw/device/lib/base/stdasm.h"
+
+/**
+ * This header provides functions for controlling the excution of a hart, such
+ * as halt-like functionality.
+ */
+
+/**
+ * Hints to the processor that we don't have anything better to be doing, and to
+ * go into low-power mode until an interrupt is serviced.
+ *
+ * This function may behave as if it is a no-op.
+ */
+inline void wait_for_interrupt(void) { asm volatile("wfi"); }
+
+/**
+ * Spin for roughly the given number of microseconds.
+ *
+ * @param microseconds the duration for which to spin.
+ */
+void busy_sleep_micros(size_t microseconds);
+
+/**
+ * Immediately halt program execution.
+ *
+ * This function conforms to the semantics defined in ISO C11 S7.22.4.1.
+ */
+noreturn void abort(void);
+
+#endif // SW_DEVICE_LIB_RUNTIME_HART_H_
diff --git a/sw/device/lib/runtime/ibex.c b/sw/device/lib/runtime/ibex.c
new file mode 100644
index 0000000..854d33d
--- /dev/null
+++ b/sw/device/lib/runtime/ibex.c
@@ -0,0 +1,13 @@
+// Copyright lowRISC contributors.
+// Licensed under the Apache License, Version 2.0, see LICENSE for details.
+// SPDX-License-Identifier: Apache-2.0
+
+#include "sw/device/lib/runtime/ibex.h"
+
+#ifdef SIMULATION
+const size_t kIbexClockFreqHz = 500 * 1000; // 500 kHz
+#else
+const size_t kIbexClockFreqHz = 50 * 1000 * 1000; // 50 MHz
+#endif
+
+extern void ibex_busy_loop(size_t);
diff --git a/sw/device/lib/runtime/ibex.h b/sw/device/lib/runtime/ibex.h
new file mode 100644
index 0000000..a31d609
--- /dev/null
+++ b/sw/device/lib/runtime/ibex.h
@@ -0,0 +1,41 @@
+// Copyright lowRISC contributors.
+// Licensed under the Apache License, Version 2.0, see LICENSE for details.
+// SPDX-License-Identifier: Apache-2.0
+
+#ifndef SW_DEVICE_LIB_RUNTIME_IBEX_H_
+#define SW_DEVICE_LIB_RUNTIME_IBEX_H_
+
+#include <stddef.h>
+
+#include "sw/device/lib/base/stdasm.h"
+
+/**
+ * This header provides Ibex-specific functions, such as the clock frequency and
+ * cycle-accurate busy loops.
+ */
+
+/**
+ * The clock frequency of the Ibex core, in hertz.
+ */
+extern const size_t kIbexClockFreqHz;
+
+/**
+ * Spins for roughly the number of |cycles| given. For best results, |cycles|
+ * should be a multiple of eight.
+ *
+ * This function should not be used for time-keeping.
+ *
+ * @param cycles the number of cycles to burn.
+ */
+inline void ibex_busy_loop(size_t cycles) {
+ size_t out; // Used to create an inout parameter below.
+ asm volatile(
+ "busy_loop%=:"
+ " nop; nop; nop; nop;" // 4 cycles.
+ " addi %1, %1, -8;" // 1 cycle.
+ " blez %1, busy_loop%=;" // 3 cycles.
+ : "=&r"(out)
+ : "0"(cycles));
+}
+
+#endif // SW_DEVICE_LIB_RUNTIME_IBEX_H_
diff --git a/sw/device/lib/runtime/meson.build b/sw/device/lib/runtime/meson.build
new file mode 100644
index 0000000..6e7e5ab
--- /dev/null
+++ b/sw/device/lib/runtime/meson.build
@@ -0,0 +1,18 @@
+# Copyright lowRISC contributors.
+# Licensed under the Apache License, Version 2.0, see LICENSE for details.
+# SPDX-License-Identifier: Apache-2.0
+
+sw_lib_runtime_ibex = declare_dependency(
+ link_with: static_library(
+ 'runtime_ibex_ot',
+ sources: ['ibex.c'],
+ )
+)
+
+sw_lib_runtime_hart = declare_dependency(
+ link_with: static_library(
+ 'runtime_hart_ot',
+ sources: ['hart.c'],
+ dependencies: [sw_lib_runtime_ibex],
+ )
+)