Software APIs
macros.h
Go to the documentation of this file.
1 // Copyright lowRISC contributors (OpenTitan project).
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 struct member.
231  *
232  * Identical to `OT_ASSERT_MEMBER_SIZE`, except the size parameter is provided
233  * as an enum constant.
234  *
235  * @param type A struct type.
236  * @param member A member of the struct.
237  * @param size Expected size of the type as an enum constant.
238  */
239 #define OT_ASSERT_MEMBER_SIZE_AS_ENUM(type, member, enum_size) \
240  static_assert(sizeof(((type){0}).member) == enum_size, \
241  "Unexpected size for " #type)
242 
243 /**
244  * A macro that expands to an assertion for the size of a type.
245  *
246  * @param type A type.
247  * @param size Expected size of the type.
248  */
249 #define OT_ASSERT_SIZE(type, size) \
250  static_assert(sizeof(type) == UINT32_C(size), "Unexpected size for " #type)
251 
252 /**
253  * A macro that expands to an assertion for an expected enum value.
254  *
255  * @param var An enum entry.
256  * @param expected_value Expected enum value.
257  */
258 #define OT_ASSERT_ENUM_VALUE(var, expected_value) \
259  static_assert(var == expected_value, "Unexpected value for " #var)
260 
261 /**
262  * A macro representing the OpenTitan execution platform.
263  */
264 #if __riscv_xlen == 32
265 #define OT_PLATFORM_RV32 1
266 #endif
267 
268 /**
269  * A macro indicating whether software should assume reduced hardware
270  * support (for the `top_englishbreakafst` toplevel).
271  */
272 #ifdef OT_IS_ENGLISH_BREAKFAST_REDUCED_SUPPORT_FOR_INTERNAL_USE_ONLY_
273 #define OT_IS_ENGLISH_BREAKFAST 1
274 #endif
275 
276 /**
277  * Attribute for functions which return errors that must be acknowledged.
278  *
279  * This attribute must be used to mark all DIFs which return an error value of
280  * some kind, to ensure that callers do not accidentally drop the error on the
281  * ground.
282  *
283  * Normally, the standard way to drop such a value on the ground explicitly is
284  * with the syntax `(void)expr;`, in analogy with the behavior of C++'s
285  * `[[nodiscard]]` attribute.
286  * However, GCC does not implement this, so the idiom `if (expr) {}` should be
287  * used instead, for the time being.
288  * See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=25509.
289  */
290 #define OT_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
291 
292 /**
293  * Attribute for weak functions that can be overridden, e.g., ISRs.
294  */
295 #define OT_WEAK __attribute__((weak))
296 
297 /**
298  * Attribute to construct functions without prologue/epilogue sequences.
299  *
300  * Only basic asm statements can be safely included in naked functions.
301  *
302  * See https://gcc.gnu.org/onlinedocs/gcc/RISC-V-Function-Attributes.html
303  * #RISC-V-Function-Attributes
304  */
305 #define OT_NAKED __attribute__((naked))
306 
307 /**
308  * Attribute to place symbols into particular sections.
309  *
310  * See https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html
311  * #Common-Function-Attributes
312  */
313 #define OT_SECTION(name) __attribute__((section(name)))
314 
315 /**
316  * Pragma meant to place symbols into a NOBITS section with a specified name.
317  *
318  * This should only be used for zero-initialized globals (including
319  * implicitly zero-initialized ones, when the initializer is missing). The
320  * pragma won't affect variables that do not go into the bss section.
321  *
322  * Example:
323  *
324  * OT_SET_BSS_SECTION(".foo",
325  * uint32_t x; // emitted in section .foo instead of .bss
326  * uint32_t y = 42; // emitted in regular .data section (but don't do this)
327  * )
328  */
329 #define OT_SET_BSS_SECTION(name, ...) \
330  OT_SET_BSS_SECTION_(clang section bss = name) \
331  __VA_ARGS__ \
332  _Pragma("clang section bss = \"\"")
333 #define OT_SET_BSS_SECTION_(section) _Pragma(#section)
334 
335 /**
336  * Attribute to suppress the inlining of a function at its call sites.
337  *
338  * See https://clang.llvm.org/docs/AttributeReference.html#noinline.
339  */
340 #define OT_NOINLINE __attribute__((noinline))
341 
342 /**
343  * Returns the address of the current function stack frame.
344  *
345  * See https://gcc.gnu.org/onlinedocs/gcc/Return-Address.html.
346  */
347 #define OT_FRAME_ADDR() __builtin_frame_address(0)
348 
349 /**
350  * Hints to the compiler that some point is not reachable.
351  *
352  * One use case could be a function that never returns.
353  *
354  * Please not that if the control flow reaches the point of the
355  * __builtin_unreachable, the program is undefined.
356  *
357  * See https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html.
358  */
359 #define OT_UNREACHABLE() __builtin_unreachable()
360 
361 /**
362  * Attribute for weak alias that can be overridden, e.g., Mock overrides in
363  * DIFs.
364  */
365 #define OT_ALIAS(name) __attribute__((alias(name)))
366 
367 /**
368  * Defines a local symbol named `kName_` whose address resolves to the
369  * program counter value an inline assembly block at this location would
370  * see.
371  *
372  * The primary intention of this macro is to allow for peripheral tests to be
373  * written that want to assert that a specific program counter reported by the
374  * hardware corresponds to actual code that executed.
375  *
376  * For example, suppose that we're waiting for an interrupt to fire, and want
377  * to verify that it fired in a specific region. We have two volatile globals:
378  * `irq_happened`, which the ISR sets before returning, and `irq_pc`, which
379  * the ISR sets to whatever PC the hardware reports it came from. The
380  * code below tests that the reported PC matches expectations.
381  *
382  * ```
383  * OT_ADDRESSABLE_LABEL(kIrqWaitStart);
384  * enable_interrupts();
385  * while(!irq_happened) {
386  * wait_for_interrupt();
387  * }
388  * OT_ADDRESSABLE_LABEL(kIrqWaitEnd);
389  *
390  * CHECK(irq_pc >= &kIrqWaitStart && irq_pc < &IrqWaitEnd);
391  * ```
392  *
393  * Note that this only works if all functions called between the two labels
394  * are actually inlined; if the interrupt fires inside of one of those two
395  * functions, it will appear to not have the right PC, even though it is
396  * logically inside the pair of labels.
397  *
398  * # Volatile Semantics
399  *
400  * This has the same semantics as a `volatile` inline assembly block: it
401  * may be reordered with respect to all operations except other volatile
402  * operations (i.e. volatile assembly and volatile read/write, such as
403  * MMIO). For example, in the following code, `kBefore` and `kAfter` can
404  * wind up having the same address:
405  *
406  * ```
407  * OT_ADDRESSABLE_LABEL(kBefore);
408  * x += 5;
409  * OT_ADDRESSABLE_LABEL(kAfter);
410  * ```
411  *
412  * Because it can be reordered and the compiler is free to emit whatever
413  * instructions it likes, comparing a program counter value obtained from
414  * the hardware with a symbol emitted by this macro is brittle (and,
415  * ultimately, futile). Instead, it should be used in pairs to create a
416  * range of addresses that a value can be checked for being within.
417  *
418  * For this primitive to work correctly there must be something that counts as
419  * a volatile operation between the two labels. These include:
420  *
421  * - Direct reads or writes to MMIO registers or CSRs.
422  * - A volatile assembly block with at least one instruction.
423  * - Any DIF or driver call that may touch an MMIO register or a CSR.
424  * - `LOG`ging, `printf`ing, or `CHECK`ing.
425  * - `wait_for_interrupt()`.
426  * - `barrier32()`, but NOT `launder32()`.
427  *
428  * This macro should not be used on the host side. It will compile, but will
429  * probably not provide any meaningful values. In the future, it may start
430  * producing a garbage value on the host side.
431  *
432  * # Symbol Access
433  *
434  * The symbol reference `kName_` will only be scoped to the current block
435  * in a function, but it can be redeclared in the same file with
436  *
437  * ```
438  * extern const char kName_[];
439  * ```
440  *
441  * if needed elsewhere (although this should be avoided for readability's sake).
442  * The name of the constant is global to the current `.c` file only.
443  *
444  * # Example Uses
445  */
446 #define OT_ADDRESSABLE_LABEL(kName_) \
447  extern const char kName_[]; \
448  asm volatile(".local " #kName_ "; " #kName_ ":;"); \
449  /* Force this to be at function scope. It could go at global scope, but it \
450  * would give a garbage value dependent on the whims of the linker. */ \
451  do { \
452  } while (false)
453 
454 /**
455  * Evaluates and discards `expr_`.
456  *
457  * This is needed because ‘(void)expr;` does not work for gcc.
458  */
459 #define OT_DISCARD(expr_) \
460  if (expr_) { \
461  }
462 
463 /**
464  * An attribute used for static variables indicating that they should be
465  * retained in the object file, even if they are seemingly unreferenced.
466  */
467 #define OT_USED __attribute__((used))
468 
469 /**
470  * OT_BUILD_FOR_STATIC_ANALYZER indicates whether we are compiling for the
471  * purpose of static analysis. Currently, this macro only detects
472  * Clang-Analyzer, which is used as a backend by Clang-Tidy.
473  */
474 #ifdef __clang_analyzer__
475 #define OT_BUILD_FOR_STATIC_ANALYZER 1
476 #else
477 #define OT_BUILD_FOR_STATIC_ANALYZER 0
478 #endif
479 
480 /**
481  * This macro is used to align an offset to point to a 32b value.
482  */
483 #define OT_ALIGN_MEM(x) (uint32_t)(4 + (((uintptr_t)(x)-1) & ~3u))
484 
485 #if !defined(__ASSEMBLER__) && !defined(NOSTDINC) && \
486  !defined(RUST_PREPROCESSOR_EMIT)
487 #ifndef __cplusplus
489  char err;
490 };
491 
492 /**
493  * This macro converts a given unsigned integer value to its signed counterpart.
494  */
495 #define OT_SIGNED(value) \
496  _Generic((value), \
497  uint8_t: (int8_t)(value), \
498  uint16_t: (int16_t)(value), \
499  uint32_t: (int32_t)(value), \
500  uint64_t: (int64_t)(value), \
501  default: (struct OtSignConversionUnsupportedType){.err = 1})
502 
503 /**
504  * This macro converts a given signed integer value to its unsigned counterpart.
505  */
506 #define OT_UNSIGNED(value) \
507  _Generic((value), \
508  int8_t: (uint8_t)(value), \
509  int16_t: (uint16_t)(value), \
510  int32_t: (uint32_t)(value), \
511  int64_t: (uint64_t)(value), \
512  default: (struct OtSignConversionUnsupportedType){.err = 1})
513 #else // __cplusplus
514 // Templates require "C++" linkage. Even though this block is only reachable
515 // when __cplusplus is defined, it's possible that we are in the middle of an
516 // `extern "C"` block. In case that is true, we explicitly set the linkage to
517 // "C++" for the coming C++ templates.
518 extern "C++" {
519 namespace {
520 template <typename T>
521 class SignConverter {
522  public:
523  using SignedT = typename std::make_signed<T>::type;
524  using UnsignedT = typename std::make_unsigned<T>::type;
525  static constexpr SignedT as_signed(T value) {
526  return static_cast<SignedT>(value);
527  }
528  static constexpr UnsignedT as_unsigned(T value) {
529  return static_cast<UnsignedT>(value);
530  }
531 };
532 } // namespace
533 }
534 #define OT_SIGNED(value) (SignConverter<typeof(value)>::as_signed((value)))
535 #define OT_UNSIGNED(value) (SignConverter<typeof(value)>::as_unsigned((value)))
536 #endif // __cplusplus
537 #endif // !defined(__ASSEMBLER__) && !defined(NOSTDINC) &&
538  // !defined(RUST_PREPROCESSOR_EMIT)
539 
540 // This routine makes sure that a condition that can be changed by IRQs
541 // is evaluated inside a critical session. It executes a `wfi` between
542 // evaluations.
543 #define ATOMIC_WAIT_FOR_INTERRUPT(_volatile_condition) \
544  while (true) { \
545  irq_global_ctrl(false); \
546  if ((_volatile_condition)) { \
547  break; \
548  } \
549  wait_for_interrupt(); \
550  irq_global_ctrl(true); \
551  } \
552  irq_global_ctrl(true)
553 #endif // OPENTITAN_SW_DEVICE_LIB_BASE_MACROS_H_