[sw/testing] add CHECK_DIF_OK macro
As DIFs are refactored to use a global error space this macro becomes
useful for checking DIF return results.
Signed-off-by: Timothy Trippel <ttrippel@google.com>
diff --git a/sw/device/lib/testing/check.h b/sw/device/lib/testing/check.h
index ffc1954..c983fa2 100644
--- a/sw/device/lib/testing/check.h
+++ b/sw/device/lib/testing/check.h
@@ -9,6 +9,7 @@
#include "sw/device/lib/base/macros.h"
#include "sw/device/lib/base/memory.h"
+#include "sw/device/lib/dif/dif_base.h"
#include "sw/device/lib/runtime/hart.h"
#include "sw/device/lib/runtime/log.h"
#include "sw/device/lib/testing/test_framework/test_status.h"
@@ -72,4 +73,34 @@
} \
} while (false)
+/**
+ * Checks that the given DIF call returns kDifOk. If the DIF call returns a
+ * different dif_result_t value (defined in sw/device/lib/dif/dif_base.h), this
+ * function logs and then aborts.
+ *
+ * @param DIF call to invoke and check its return value .
+ * @param ... arguments to a LOG_* macro, which are evaluated if the check
+ * fails.
+ */
+#define CHECK_DIF_OK(dif_call, ...) \
+ do { \
+ if (dif_call != kDifOk) { \
+ /* NOTE: because the condition in this if \
+ statement can be statically determined, \
+ only one of the below string constants \
+ will be included in the final binary.*/ \
+ if (GET_NUM_VARIABLE_ARGS(_, ##__VA_ARGS__) == 0) { \
+ LOG_ERROR("CHECK-fail: " #dif_call); \
+ } else { \
+ LOG_ERROR("CHECK-fail: " __VA_ARGS__); \
+ } \
+ /* Currently, this macro will call into \
+ the test failure code, which logs \
+ "FAIL" and aborts. In the future, \
+ we will try to condition on whether \
+ or not this is a test.*/ \
+ test_status_set(kTestStatusFailed); \
+ } \
+ } while (false)
+
#endif // OPENTITAN_SW_DEVICE_LIB_TESTING_CHECK_H_
diff --git a/sw/device/sca/lib/simple_serial.h b/sw/device/sca/lib/simple_serial.h
index 09eea2e..ffe8f75 100644
--- a/sw/device/sca/lib/simple_serial.h
+++ b/sw/device/sca/lib/simple_serial.h
@@ -5,11 +5,12 @@
#ifndef OPENTITAN_SW_DEVICE_SCA_LIB_SIMPLE_SERIAL_H_
#define OPENTITAN_SW_DEVICE_SCA_LIB_SIMPLE_SERIAL_H_
-#include "sw/device/lib/dif/dif_uart.h"
-
#include <stddef.h>
#include <stdint.h>
+#include "sw/device/lib/dif/dif_base.h"
+#include "sw/device/lib/dif/dif_uart.h"
+
/**
* @file
* @brief Simple serial protocol for side-channel analysis.
@@ -33,6 +34,17 @@
} while (false)
/**
+ * Sends an error message over UART if DIF does not return kDifOk.
+ */
+#define SS_CHECK_DIF_OK(dif_call) \
+ do { \
+ if (dif_call != kDifOk) { \
+ simple_serial_send_status(kSimpleSerialError); \
+ return; \
+ } \
+ } while (false)
+
+/**
* Simple serial status codes.
*/
typedef enum simple_serial_result {