5 #ifndef OPENTITAN_SW_DEVICE_LIB_BASE_STATUS_H_
6 #define OPENTITAN_SW_DEVICE_LIB_BASE_STATUS_H_
16 #include "sw/device/silicon_creator/lib/error.h"
18 #define USING_INTERNAL_STATUS
19 #include "sw/device/lib/base/internal/status.h"
20 #undef USING_INTERNAL_STATUS
63 #define RECORD_STATUS_CREATE(code, mod_id, file) \
64 _RECORD_STATUS_CREATE(code, mod_id, file)
75 enum ot_status_create_record_magic {
76 OT_SCR_UNKNOWN_MOD_ID = 0xffffffff,
84 #define _RECORD_STATUS_CREATE(...)
86 #define _RECORD_STATUS_CREATE(code_val, mod_id, file) \
90 __builtin_choose_expr(code_val != kOk, \
91 __RECORD_STATUS_CREATE(code_val, mod_id, file), ({}))
93 #define __RECORD_STATUS_CREATE(code_val, mod_id, file) \
95 OT_SECTION(".ot.status_create_record") \
97 static const ot_status_create_record_t kOtStatusRecord = { \
100 .module_id = __builtin_constant_p(mod_id) ? mod_id : OT_SCR_UNKNOWN_MOD_ID, \
106 #define DIF_RESULT_INTO_STATUS(expr_) \
108 typeof(expr_) _val = (expr_); \
109 absl_status_t code; \
110 memcpy(&code, &_val, sizeof(code)); \
111 status_create(code, MODULE_ID, __FILE__, code == kOk ? 0 : __LINE__); \
114 #define ROM_ERROR_INTO_STATUS(expr_) \
116 typeof(expr_) ex_ = (expr_); \
118 memcpy(&val, &ex_, sizeof(val)); \
119 absl_status_t code = \
120 val == kErrorOk ? 0 \
121 : bitfield_field32_read(val, ROM_ERROR_FIELD_STATUS); \
122 int32_t arg = (int32_t)bitfield_field32_read(val, ROM_ERROR_FIELD_ERROR); \
123 uint32_t mod = bitfield_field32_read(val, ROM_ERROR_FIELD_MODULE); \
124 uint32_t module = (mod & 0x1F) << 16 | (mod & 0x1F00) << (21 - 8); \
125 status_create(code, module, __FILE__, code == kOk ? kErrorOk : arg); \
139 #define INTO_STATUS(expr_) _Generic((expr_), \
141 rom_error_t: ROM_ERROR_INTO_STATUS(expr_), \
142 dif_result_t: DIF_RESULT_INTO_STATUS(expr_))
162 #define STATUS_REPORT_HERE(status) \
164 absl_status_t err = status_err(status); \
165 status_t report = status_create(err, MODULE_ID, __FILE__, __LINE__); \
166 status_report(report); \
182 status_t status_ = INTO_STATUS(expr_); \
183 if (status_.value < 0) { \
184 STATUS_REPORT_HERE(status_); \
194 extern const uint32_t MODULE_ID;
207 status_t status_create(absl_status_t code, uint32_t mod_id,
const char *file,
221 bool status_extract(
status_t s,
const char **code, int32_t *arg,
char *mod_id);
248 #define STATUS_CREATE(s_, ...) \
250 static_assert(OT_VA_ARGS_COUNT(_, __VA_ARGS__) <= 2, \
251 "status macros take 0 or 1 arguments"); \
252 RECORD_STATUS_CREATE(s_, MODULE_ID, __FILE__); \
253 status_create(s_, MODULE_ID, __FILE__, OT_GET_LAST_ARG(__VA_ARGS__)); \
258 #define OK_STATUS(...) STATUS_CREATE(kOk, 0, ##__VA_ARGS__)
259 #define CANCELLED(...) STATUS_CREATE(kCancelled, __LINE__, ##__VA_ARGS__)
260 #define UNKNOWN(...) STATUS_CREATE(kUnknown, __LINE__, ##__VA_ARGS__)
261 #define INVALID_ARGUMENT(...) STATUS_CREATE(kInvalidArgument, __LINE__, ##__VA_ARGS__)
262 #define DEADLINE_EXCEEDED(...) STATUS_CREATE(kDeadlineExceeded, __LINE__, ##__VA_ARGS__)
263 #define NOT_FOUND(...) STATUS_CREATE(kNotFound, __LINE__, ##__VA_ARGS__)
264 #define ALREADY_EXISTS(...) STATUS_CREATE(kAlreadyExists, __LINE__, ##__VA_ARGS__)
265 #define PERMISSION_DENIED(...) STATUS_CREATE(kPermissionDenied, __LINE__, ##__VA_ARGS__)
266 #define RESOURCE_EXHAUSTED(...) STATUS_CREATE(kResourceExhausted, __LINE__, ##__VA_ARGS__)
267 #define FAILED_PRECONDITION(...) STATUS_CREATE(kFailedPrecondition, __LINE__, ##__VA_ARGS__)
268 #define ABORTED(...) STATUS_CREATE(kAborted, __LINE__, ##__VA_ARGS__)
269 #define OUT_OF_RANGE(...) STATUS_CREATE(kOutOfRange, __LINE__, ##__VA_ARGS__)
270 #define UNIMPLEMENTED(...) STATUS_CREATE(kUnimplemented, __LINE__, ##__VA_ARGS__)
271 #define INTERNAL(...) STATUS_CREATE(kInternal, __LINE__, ##__VA_ARGS__)
272 #define UNAVAILABLE(...) STATUS_CREATE(kUnavailable, __LINE__, ##__VA_ARGS__)
273 #define DATA_LOSS(...) STATUS_CREATE(kDataLoss, __LINE__, ##__VA_ARGS__)
274 #define UNAUTHENTICATED(...) STATUS_CREATE(kUnauthenticated, __LINE__, ##__VA_ARGS__)
280 #endif // OPENTITAN_SW_DEVICE_LIB_BASE_STATUS_H_