Software APIs
macros.h
Go to the documentation of this file.
1 // Copyright lowRISC contributors.
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #ifndef OPENTITAN_SW_DEVICE_LIB_BASE_MACROS_H_
6 #define OPENTITAN_SW_DEVICE_LIB_BASE_MACROS_H_
7 
8 // This file may be used in .S files, in which case standard library includes
9 // should be elided.
10 #if !defined(__ASSEMBLER__) && !defined(NOSTDINC)
11 #include <assert.h>
12 #include <stddef.h>
13 #include <stdint.h>
14 
15 #ifdef __cplusplus
16 // The <type_traits> header is required for the C++-only `SignConverter` class.
17 // See `SignConverter` for an explanation of this `extern "C++"` block.
18 extern "C++" {
19 #include <type_traits>
20 }
21 #endif
22 #endif
23 
24 /**
25  * @file
26  * @brief Generic preprocessor macros that don't really fit anywhere else.
27  */
28 
29 /**
30  * Computes the number of elements in the given array.
31  *
32  * Note that this can only determine the length of *fixed-size* arrays. Due to
33  * limitations of C, it will incorrectly compute the size of an array passed as
34  * a function argument, because those automatically decay into pointers. This
35  * function can only be used correctly with:
36  * - Arrays declared as stack variables.
37  * - Arrays declared at global scope.
38  * - Arrays that are members of a struct or union.
39  *
40  * @param array The array expression to measure.
41  * @return The number of elements in the array, as a `size_t`.
42  */
43 // This is a sufficiently well-known macro that we don't really need to bother
44 // with a prefix like the others, and a rename will cause churn.
45 #define ARRAYSIZE(array) (sizeof(array) / sizeof(array[0]))
46 
47 /**
48  * An annotation that a switch/case fallthrough is the intended behavior.
49  */
50 #define OT_FALLTHROUGH_INTENDED __attribute__((fallthrough))
51 
52 /**
53  * A directive to force the compiler to inline a function.
54  */
55 #define OT_ALWAYS_INLINE __attribute__((always_inline)) inline
56 
57 /**
58  * The `restrict` keyword is C specific, so we provide a C++-portable wrapper
59  * that uses the GCC name.
60  *
61  * It only needs to be used in headers; .c files can use `restrict` directly.
62  */
63 #define OT_RESTRICT __restrict__
64 
65 /**
66  * An argument stringification macro.
67  */
68 #define OT_STRINGIFY(a) OT_STRINGIFY_(a)
69 #define OT_STRINGIFY_(a) #a
70 
71 /**
72  * A variable-argument macro that expands to the number of arguments passed into
73  * it, between 0 and 31 arguments.
74  *
75  * This macro is based off of a well-known preprocessor trick. This
76  * StackOverflow post expains the trick in detail:
77  * https://stackoverflow.com/questions/2308243/macro-returning-the-number-of-arguments-it-is-given-in-c
78  * TODO #2026: a dummy token is required for this to work correctly.
79  *
80  * @param dummy a dummy token that is required to be passed for the calculation
81  * to work correctly.
82  * @param ... the variable args list.
83  */
84 #define OT_VA_ARGS_COUNT(dummy, ...) \
85  OT_SHIFT_N_VARIABLE_ARGS_(dummy, ##__VA_ARGS__, 31, 30, 29, 28, 27, 26, 25, \
86  24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, \
87  12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
88 
89 // Implementation details for `OT_VA_ARGS_COUNT()`.
90 #define OT_SHIFT_N_VARIABLE_ARGS_(...) OT_GET_NTH_VARIABLE_ARG_(__VA_ARGS__)
91 #define OT_GET_NTH_VARIABLE_ARG_(x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, \
92  x11, x12, x13, x14, x15, x16, x17, x18, x19, \
93  x20, x21, x22, x23, x24, x25, x26, x27, x28, \
94  x29, x30, x31, n, ...) \
95  n
96 
97 /**
98  * An argument concatenation macro.
99  *
100  * Because the `##` operator inhibits expansion, we use a level of indirection
101  * to create a macro which concatenates without inhibition.
102  */
103 #define OT_CAT(a, ...) OT_PRIMITIVE_CAT(a, __VA_ARGS__)
104 #define OT_PRIMITIVE_CAT(a, ...) a##__VA_ARGS__
105 
106 /**
107  * A macro which gets the nth arg from a __VA_LIST__
108  */
109 #define OT_GET_ARG(n, ...) OT_CAT(OT_CAT(OT_GET_ARG_, n), _)(__VA_ARGS__)
110 
111 /**
112  * A macro which gets the last arg from a __VA_LIST__
113  *
114  * Note: we leave out the dummy argument because we want count to
115  * give us the number of args minus one so that the created
116  * OT_GET_ARG_n token will contain the correct integer.
117  */
118 #define OT_GET_LAST_ARG(...) \
119  OT_GET_ARG(OT_VA_ARGS_COUNT(__VA_ARGS__), ##__VA_ARGS__)
120 
121 /**
122  * The following collection of `OT_GET_ARG` macros are used to construct the
123  * generic "get nth arg" macro.
124  */
125 #define OT_GET_ARG_0_(x0, ...) x0
126 #define OT_GET_ARG_1_(x1, x0, ...) x0
127 #define OT_GET_ARG_2_(x2, x1, x0, ...) x0
128 #define OT_GET_ARG_3_(x3, x2, x1, x0, ...) x0
129 #define OT_GET_ARG_4_(x4, x3, x2, x1, x0, ...) x0
130 #define OT_GET_ARG_5_(x5, x4, x3, x2, x1, x0, ...) x0
131 #define OT_GET_ARG_6_(x6, x5, x4, x3, x2, x1, x0, ...) x0
132 #define OT_GET_ARG_7_(x7, x6, x5, x4, x3, x2, x1, x0, ...) x0
133 #define OT_GET_ARG_8_(x8, x7, x6, x5, x4, x3, x2, x1, x0, ...) x0
134 #define OT_GET_ARG_9_(x9, x8, x7, x6, x5, x4, x3, x2, x1, x0, ...) x0
135 #define OT_GET_ARG_10_(x10, x9, x8, x7, x6, x5, x4, x3, x2, x1, x0, ...) x0
136 #define OT_GET_ARG_11_(x11, x10, x9, x8, x7, x6, x5, x4, x3, x2, x1, x0, ...) x0
137 #define OT_GET_ARG_12_(x12, x11, x10, x9, x8, x7, x6, x5, x4, x3, x2, x1, x0, \
138  ...) \
139  x0
140 #define OT_GET_ARG_13_(x13, x12, x11, x10, x9, x8, x7, x6, x5, x4, x3, x2, x1, \
141  x0, ...) \
142  x0
143 #define OT_GET_ARG_14_(x14, x13, x12, x11, x10, x9, x8, x7, x6, x5, x4, x3, \
144  x2, x1, x0, ...) \
145  x0
146 #define OT_GET_ARG_15_(x15, x14, x13, x12, x11, x10, x9, x8, x7, x6, x5, x4, \
147  x3, x2, x1, x0, ...) \
148  x0
149 #define OT_GET_ARG_16_(x16, x15, x14, x13, x12, x11, x10, x9, x8, x7, x6, x5, \
150  x4, x3, x2, x1, x0, ...) \
151  x0
152 #define OT_GET_ARG_17_(x17, x16, x15, x14, x13, x12, x11, x10, x9, x8, x7, x6, \
153  x5, x4, x3, x2, x1, x0, ...) \
154  x0
155 #define OT_GET_ARG_18_(x18, x17, x16, x15, x14, x13, x12, x11, x10, x9, x8, \
156  x7, x6, x5, x4, x3, x2, x1, x0, ...) \
157  x0
158 #define OT_GET_ARG_19_(x19, x18, x17, x16, x15, x14, x13, x12, x11, x10, x9, \
159  x8, x7, x6, x5, x4, x3, x2, x1, x0, ...) \
160  x0
161 #define OT_GET_ARG_20_(x20, x19, x18, x17, x16, x15, x14, x13, x12, x11, x10, \
162  x9, x8, x7, x6, x5, x4, x3, x2, x1, x0, ...) \
163  x0
164 #define OT_GET_ARG_21_(x21, x20, x19, x18, x17, x16, x15, x14, x13, x12, x11, \
165  x10, x9, x8, x7, x6, x5, x4, x3, x2, x1, x0, ...) \
166  x0
167 #define OT_GET_ARG_22_(x22, x21, x20, x19, x18, x17, x16, x15, x14, x13, x12, \
168  x11, x10, x9, x8, x7, x6, x5, x4, x3, x2, x1, x0, ...) \
169  x0
170 #define OT_GET_ARG_23_(x23, x22, x21, x20, x19, x18, x17, x16, x15, x14, x13, \
171  x12, x11, x10, x9, x8, x7, x6, x5, x4, x3, x2, x1, x0, \
172  ...) \
173  x0
174 #define OT_GET_ARG_24_(x24, x23, x22, x21, x20, x19, x18, x17, x16, x15, x14, \
175  x13, x12, x11, x10, x9, x8, x7, x6, x5, x4, x3, x2, x1, \
176  x0, ...) \
177  x0
178 #define OT_GET_ARG_25_(x25, x24, x23, x22, x21, x20, x19, x18, x17, x16, x15, \
179  x14, x13, x12, x11, x10, x9, x8, x7, x6, x5, x4, x3, \
180  x2, x1, x0, ...) \
181  x0
182 #define OT_GET_ARG_26_(x26, x25, x24, x23, x22, x21, x20, x19, x18, x17, x16, \
183  x15, x14, x13, x12, x11, x10, x9, x8, x7, x6, x5, x4, \
184  x3, x2, x1, x0, ...) \
185  x0
186 #define OT_GET_ARG_27_(x27, x26, x25, x24, x23, x22, x21, x20, x19, x18, x17, \
187  x16, x15, x14, x13, x12, x11, x10, x9, x8, x7, x6, x5, \
188  x4, x3, x2, x1, x0, ...) \
189  x0
190 #define OT_GET_ARG_28_(x28, x27, x26, x25, x24, x23, x22, x21, x20, x19, x18, \
191  x17, x16, x15, x14, x13, x12, x11, x10, x9, x8, x7, x6, \
192  x5, x4, x3, x2, x1, x0, ...) \
193  x0
194 #define OT_GET_ARG_29_(x29, x28, x27, x26, x25, x24, x23, x22, x21, x20, x19, \
195  x18, x17, x16, x15, x14, x13, x12, x11, x10, x9, x8, \
196  x7, x6, x5, x4, x3, x2, x1, x0, ...) \
197  x0
198 #define OT_GET_ARG_30_(x30, x29, x28, x27, x26, x25, x24, x23, x22, x21, x20, \
199  x19, x18, x17, x16, x15, x14, x13, x12, x11, x10, x9, \
200  x8, x7, x6, x5, x4, x3, x2, x1, x0, ...) \
201  x0
202 #define OT_GET_ARG_31_(x31, x30, x29, x28, x27, x26, x25, x24, x23, x22, x21, \
203  x20, x19, x18, x17, x16, x15, x14, x13, x12, x11, x10, \
204  x9, x8, x7, x6, x5, x4, x3, x2, x1, x0, ...) \
205  x0
206 
207 /**
208  * A macro that expands to an assertion for the offset of a struct member.
209  *
210  * @param type A struct type.
211  * @param member A member of the struct.
212  * @param offset Expected offset of the member.
213  */
214 #define OT_ASSERT_MEMBER_OFFSET(type, member, offset) \
215  static_assert(offsetof(type, member) == UINT32_C(offset), \
216  "Unexpected offset for " #type "." #member)
217 
218 /**
219  * A macro that expands to an assertion for the size of a struct member.
220  *
221  * @param type A struct type.
222  * @param member A member of the struct.
223  * @param size Expected size of the type.
224  */
225 #define OT_ASSERT_MEMBER_SIZE(type, member, size) \
226  static_assert(sizeof(((type){0}).member) == UINT32_C(size), \
227  "Unexpected size for " #type)
228 
229 /**
230  * A macro that expands to an assertion for the size of a type.
231  *
232  * @param type A type.
233  * @param size Expected size of the type.
234  */
235 #define OT_ASSERT_SIZE(type, size) \
236  static_assert(sizeof(type) == UINT32_C(size), "Unexpected size for " #type)
237 
238 /**
239  * A macro that expands to an assertion for an expected enum value.
240  *
241  * @param var An enum entry.
242  * @param expected_value Expected enum value.
243  */
244 #define OT_ASSERT_ENUM_VALUE(var, expected_value) \
245  static_assert(var == expected_value, "Unexpected value for " #var)
246 
247 /**
248  * A macro representing the OpenTitan execution platform.
249  */
250 #if __riscv_xlen == 32
251 #define OT_PLATFORM_RV32 1
252 #endif
253 
254 /**
255  * A macro indicating whether software should assume reduced hardware
256  * support (for the `top_englishbreakafst` toplevel).
257  */
258 #ifdef OT_IS_ENGLISH_BREAKFAST_REDUCED_SUPPORT_FOR_INTERNAL_USE_ONLY_
259 #define OT_IS_ENGLISH_BREAKFAST 1
260 #endif
261 
262 /**
263  * Attribute for functions which return errors that must be acknowledged.
264  *
265  * This attribute must be used to mark all DIFs which return an error value of
266  * some kind, to ensure that callers do not accidentally drop the error on the
267  * ground.
268  *
269  * Normally, the standard way to drop such a value on the ground explicitly is
270  * with the syntax `(void)expr;`, in analogy with the behavior of C++'s
271  * `[[nodiscard]]` attribute.
272  * However, GCC does not implement this, so the idiom `if (expr) {}` should be
273  * used instead, for the time being.
274  * See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=25509.
275  */
276 #define OT_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
277 
278 /**
279  * Attribute for weak functions that can be overridden, e.g., ISRs.
280  */
281 #define OT_WEAK __attribute__((weak))
282 
283 /**
284  * Attribute to construct functions without prologue/epilogue sequences.
285  *
286  * Only basic asm statements can be safely included in naked functions.
287  *
288  * See https://gcc.gnu.org/onlinedocs/gcc/RISC-V-Function-Attributes.html
289  * #RISC-V-Function-Attributes
290  */
291 #define OT_NAKED __attribute__((naked))
292 
293 /**
294  * Attribute to place symbols into particular sections.
295  *
296  * See https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html
297  * #Common-Function-Attributes
298  */
299 #define OT_SECTION(name) __attribute__((section(name)))
300 
301 /**
302  * Pragma meant to place symbols into a NOBITS section with a specified name.
303  *
304  * This should only be used for zero-initialized globals (including
305  * implicitly zero-initialized ones, when the initializer is missing). The
306  * pragma won't affect variables that do not go into the bss section.
307  *
308  * Example:
309  *
310  * OT_SET_BSS_SECTION(".foo",
311  * uint32_t x; // emitted in section .foo instead of .bss
312  * uint32_t y = 42; // emitted in regular .data section (but don't do this)
313  * )
314  */
315 #define OT_SET_BSS_SECTION(name, ...) \
316  OT_SET_BSS_SECTION_(clang section bss = name) \
317  __VA_ARGS__ \
318  _Pragma("clang section bss = \"\"")
319 #define OT_SET_BSS_SECTION_(section) _Pragma(#section)
320 
321 /**
322  * Attribute to suppress the inlining of a function at its call sites.
323  *
324  * See https://clang.llvm.org/docs/AttributeReference.html#noinline.
325  */
326 #define OT_NOINLINE __attribute__((noinline))
327 
328 /**
329  * Returns the address of the current function stack frame.
330  *
331  * See https://gcc.gnu.org/onlinedocs/gcc/Return-Address.html.
332  */
333 #define OT_FRAME_ADDR() __builtin_frame_address(0)
334 
335 /**
336  * Hints to the compiler that some point is not reachable.
337  *
338  * One use case could be a function that never returns.
339  *
340  * Please not that if the control flow reaches the point of the
341  * __builtin_unreachable, the program is undefined.
342  *
343  * See https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html.
344  */
345 #define OT_UNREACHABLE() __builtin_unreachable()
346 
347 /**
348  * Attribute for weak alias that can be overridden, e.g., Mock overrides in
349  * DIFs.
350  */
351 #define OT_ALIAS(name) __attribute__((alias(name)))
352 
353 /**
354  * Defines a local symbol named `kName_` whose address resolves to the
355  * program counter value an inline assembly block at this location would
356  * see.
357  *
358  * The primary intention of this macro is to allow for peripheral tests to be
359  * written that want to assert that a specific program counter reported by the
360  * hardware corresponds to actual code that executed.
361  *
362  * For example, suppose that we're waiting for an interrupt to fire, and want
363  * to verify that it fired in a specific region. We have two volatile globals:
364  * `irq_happened`, which the ISR sets before returning, and `irq_pc`, which
365  * the ISR sets to whatever PC the hardware reports it came from. The
366  * code below tests that the reported PC matches expectations.
367  *
368  * ```
369  * OT_ADDRESSABLE_LABEL(kIrqWaitStart);
370  * enable_interrupts();
371  * while(!irq_happened) {
372  * wait_for_interrupt();
373  * }
374  * OT_ADDRESSABLE_LABEL(kIrqWaitEnd);
375  *
376  * CHECK(irq_pc >= &kIrqWaitStart && irq_pc < &IrqWaitEnd);
377  * ```
378  *
379  * Note that this only works if all functions called between the two labels
380  * are actually inlined; if the interrupt fires inside of one of those two
381  * functions, it will appear to not have the right PC, even though it is
382  * logically inside the pair of labels.
383  *
384  * # Volatile Semantics
385  *
386  * This has the same semantics as a `volatile` inline assembly block: it
387  * may be reordered with respect to all operations except other volatile
388  * operations (i.e. volatile assembly and volatile read/write, such as
389  * MMIO). For example, in the following code, `kBefore` and `kAfter` can
390  * wind up having the same address:
391  *
392  * ```
393  * OT_ADDRESSABLE_LABEL(kBefore);
394  * x += 5;
395  * OT_ADDRESSABLE_LABEL(kAfter);
396  * ```
397  *
398  * Because it can be reordered and the compiler is free to emit whatever
399  * instructions it likes, comparing a program counter value obtained from
400  * the hardware with a symbol emitted by this macro is brittle (and,
401  * ultimately, futile). Instead, it should be used in pairs to create a
402  * range of addresses that a value can be checked for being within.
403  *
404  * For this primitive to work correctly there must be something that counts as
405  * a volatile operation between the two labels. These include:
406  *
407  * - Direct reads or writes to MMIO registers or CSRs.
408  * - A volatile assembly block with at least one instruction.
409  * - Any DIF or driver call that may touch an MMIO register or a CSR.
410  * - `LOG`ging, `printf`ing, or `CHECK`ing.
411  * - `wait_for_interrupt()`.
412  * - `barrier32()`, but NOT `launder32()`.
413  *
414  * This macro should not be used on the host side. It will compile, but will
415  * probably not provide any meaningful values. In the future, it may start
416  * producing a garbage value on the host side.
417  *
418  * # Symbol Access
419  *
420  * The symbol reference `kName_` will only be scoped to the current block
421  * in a function, but it can be redeclared in the same file with
422  *
423  * ```
424  * extern const char kName_[];
425  * ```
426  *
427  * if needed elsewhere (although this should be avoided for readability's sake).
428  * The name of the constant is global to the current `.c` file only.
429  *
430  * # Example Uses
431  */
432 #define OT_ADDRESSABLE_LABEL(kName_) \
433  extern const char kName_[]; \
434  asm volatile(".local " #kName_ "; " #kName_ ":;"); \
435  /* Force this to be at function scope. It could go at global scope, but it \
436  * would give a garbage value dependent on the whims of the linker. */ \
437  do { \
438  } while (false)
439 
440 /**
441  * Evaluates and discards `expr_`.
442  *
443  * This is needed because ‘(void)expr;` does not work for gcc.
444  */
445 #define OT_DISCARD(expr_) \
446  if (expr_) { \
447  }
448 
449 /**
450  * An attribute used for static variables indicating that they should be
451  * retained in the object file, even if they are seemingly unreferenced.
452  */
453 #define OT_USED __attribute__((used))
454 
455 /**
456  * OT_BUILD_FOR_STATIC_ANALYZER indicates whether we are compiling for the
457  * purpose of static analysis. Currently, this macro only detects
458  * Clang-Analyzer, which is used as a backend by Clang-Tidy.
459  */
460 #ifdef __clang_analyzer__
461 #define OT_BUILD_FOR_STATIC_ANALYZER 1
462 #else
463 #define OT_BUILD_FOR_STATIC_ANALYZER 0
464 #endif
465 
466 /**
467  * This macro is used to align an offset to point to a 32b value.
468  */
469 #define OT_ALIGN_MEM(x) (uint32_t)(4 + (((uintptr_t)(x)-1) & ~3u))
470 
471 #if !defined(__ASSEMBLER__) && !defined(NOSTDINC) && \
472  !defined(RUST_PREPROCESSOR_EMIT)
473 #ifndef __cplusplus
475  char err;
476 };
477 
478 /**
479  * This macro converts a given unsigned integer value to its signed counterpart.
480  */
481 #define OT_SIGNED(value) \
482  _Generic((value), \
483  uint8_t: (int8_t)(value), \
484  uint16_t: (int16_t)(value), \
485  uint32_t: (int32_t)(value), \
486  uint64_t: (int64_t)(value), \
487  default: (struct OtSignConversionUnsupportedType){.err = 1})
488 
489 /**
490  * This macro converts a given signed integer value to its unsigned counterpart.
491  */
492 #define OT_UNSIGNED(value) \
493  _Generic((value), \
494  int8_t: (uint8_t)(value), \
495  int16_t: (uint16_t)(value), \
496  int32_t: (uint32_t)(value), \
497  int64_t: (uint64_t)(value), \
498  default: (struct OtSignConversionUnsupportedType){.err = 1})
499 #else // __cplusplus
500 // Templates require "C++" linkage. Even though this block is only reachable
501 // when __cplusplus is defined, it's possible that we are in the middle of an
502 // `extern "C"` block. In case that is true, we explicitly set the linkage to
503 // "C++" for the coming C++ templates.
504 extern "C++" {
505 namespace {
506 template <typename T>
507 class SignConverter {
508  public:
509  using SignedT = typename std::make_signed<T>::type;
510  using UnsignedT = typename std::make_unsigned<T>::type;
511  static constexpr SignedT as_signed(T value) {
512  return static_cast<SignedT>(value);
513  }
514  static constexpr UnsignedT as_unsigned(T value) {
515  return static_cast<UnsignedT>(value);
516  }
517 };
518 } // namespace
519 }
520 #define OT_SIGNED(value) (SignConverter<typeof(value)>::as_signed((value)))
521 #define OT_UNSIGNED(value) (SignConverter<typeof(value)>::as_unsigned((value)))
522 #endif // __cplusplus
523 #endif // !defined(__ASSEMBLER__) && !defined(NOSTDINC) &&
524  // !defined(RUST_PREPROCESSOR_EMIT)
525 
526 // This routine makes sure that a condition that can be changed by IRQs
527 // is evaluated inside a critical session. It executes a `wfi` between
528 // evaluations.
529 #define ATOMIC_WAIT_FOR_INTERRUPT(_volatile_condition) \
530  while (true) { \
531  irq_global_ctrl(false); \
532  if ((_volatile_condition)) { \
533  break; \
534  } \
535  wait_for_interrupt(); \
536  irq_global_ctrl(true); \
537  } \
538  irq_global_ctrl(true)
539 #endif // OPENTITAN_SW_DEVICE_LIB_BASE_MACROS_H_