Mate Toth-Pal | 83a45bd | 2023-09-01 11:17:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-License-Identifier: BSD-3-Clause |
| 3 | * SPDX-FileCopyrightText: Copyright TF-RMM Contributors. |
| 4 | */ |
| 5 | |
| 6 | #ifndef TB_COMMON_H |
| 7 | #define TB_COMMON_H |
| 8 | |
Mate Toth-Pal | 0da5811 | 2024-01-10 11:49:58 +0100 | [diff] [blame] | 9 | #include "granule.h" |
Mate Toth-Pal | 83a45bd | 2023-09-01 11:17:19 +0200 | [diff] [blame] | 10 | #include "host_defs.h" |
| 11 | #include "stdbool.h" |
| 12 | #include "stdint.h" |
| 13 | #include "string.h" |
| 14 | |
| 15 | /* Forces the model-checker to only consider executions where `p` holds. */ |
| 16 | #define __ASSUME(p) \ |
| 17 | do { \ |
| 18 | __CPROVER_assume((p)); \ |
| 19 | } while (false) |
| 20 | |
| 21 | #define ASSUME(p) __ASSUME(p) |
| 22 | |
| 23 | /* |
| 24 | * Challenges the model checker to show that `p` holds in the current execution |
| 25 | * (in which case this behaves like a no-op), or otherwise come up with a |
| 26 | * counterexample trace of states and assignments to variables which refutes |
| 27 | * `p`, associating the name `name` to this trace. |
| 28 | */ |
| 29 | #define __ASSERT(p, name) \ |
| 30 | do { \ |
| 31 | __CPROVER_assert((p), (name)); \ |
| 32 | } while (false) |
| 33 | |
| 34 | #define ASSERT(p, name) __ASSERT((p), (name)) |
| 35 | |
| 36 | /* |
| 37 | * A marker for reachability: running CBMC with `--cover cover` will check that |
| 38 | * all instances of `COVER` are reachable from the program entry point. |
| 39 | */ |
| 40 | #define __COVER(p) \ |
| 41 | do { \ |
| 42 | __CPROVER_cover(p); \ |
| 43 | } while (false) |
| 44 | |
| 45 | #define COVER(p) __COVER(p) |
| 46 | |
| 47 | #define REACHABLE COVER(true) |
| 48 | |
| 49 | /* |
| 50 | * An assertion that always fails, useful for checking code-paths are never |
| 51 | * reachable. |
| 52 | */ |
| 53 | #define UNREACHABLE ASSERT(false, "UNREACHABLE") |
| 54 | |
| 55 | /* |
| 56 | * Declare nondet_* functions for primitive types. |
| 57 | * Ref: CBMC automatically returns nondet values, or symbolic values, matching |
| 58 | * the return types. However, enum is treated as integer hence the value might |
| 59 | * be out of range. |
| 60 | */ |
| 61 | bool nondet_bool(void); |
| 62 | unsigned char nondet_unsigned_char(void); |
| 63 | unsigned short nondet_unsigned_short(void); |
| 64 | unsigned int nondet_unsigned_int(void); |
| 65 | unsigned long nondet_unsigned_long(void); |
| 66 | char nondet_char(void); |
| 67 | short nondet_short(void); |
| 68 | int nondet_int(void); |
| 69 | long nondet_long(void); |
| 70 | uint32_t nondet_uint32_t(void); |
| 71 | uint64_t nondet_uint64_t(void); |
| 72 | int32_t nondet_int32_t(void); |
| 73 | int64_t nondet_int64_t(void); |
| 74 | size_t nondet_size_t(void); |
| 75 | |
| 76 | struct tb_regs { |
| 77 | uint64_t X0; |
| 78 | uint64_t X1; |
| 79 | uint64_t X2; |
| 80 | uint64_t X3; |
| 81 | uint64_t X4; |
| 82 | uint64_t X5; |
| 83 | uint64_t X6; |
| 84 | }; |
| 85 | |
| 86 | struct tb_regs __tb_arb_regs(void); |
| 87 | |
| 88 | bool ResultEqual_2(unsigned int code, unsigned int expected); |
| 89 | bool ResultEqual_3(unsigned int code, unsigned int expected, unsigned int walk_level); |
| 90 | uint64_t Zeros(void); |
| 91 | |
| 92 | /* Some autogen contains this function. */ |
| 93 | bool __tb_arb_bool(void); |
| 94 | |
| 95 | struct tb_lock_status { |
| 96 | uint64_t something; |
| 97 | }; |
| 98 | void __tb_lock_invariant(struct tb_lock_status *lock_status); |
| 99 | |
| 100 | struct tb_lock_status __tb_lock_status(void); |
| 101 | |
| 102 | /* |
| 103 | * Functions that manipulates internal states, |
| 104 | * including PA, granule metadata and granule buffer, or content. |
| 105 | */ |
| 106 | bool valid_pa(uint64_t addr); |
| 107 | bool valid_granule_metadata_ptr(struct granule *p); |
| 108 | struct granule *pa_to_granule_metadata_ptr(uint64_t addr); |
Mate Toth-Pal | 70ae89d | 2024-01-11 13:47:28 +0100 | [diff] [blame] | 109 | void *granule_metadata_ptr_to_buffer_ptr(struct granule *g_ptr); |
Mate Toth-Pal | 83a45bd | 2023-09-01 11:17:19 +0200 | [diff] [blame] | 110 | uint64_t granule_metadata_ptr_to_pa(struct granule *g_ptr); |
Mate Toth-Pal | c751c0d | 2023-11-14 16:56:41 +0100 | [diff] [blame] | 111 | void *pa_to_granule_buffer_ptr(uint64_t addr); |
| 112 | |
Mate Toth-Pal | 83a45bd | 2023-09-01 11:17:19 +0200 | [diff] [blame] | 113 | /* TODO change the function name */ |
| 114 | void init_pa_page(const void *content, size_t size); |
| 115 | |
| 116 | |
| 117 | /* |
| 118 | * Return an unused continuous index to both `granules` and `granules_buffer` |
| 119 | * arrays. |
| 120 | */ |
| 121 | size_t next_index(void); |
| 122 | bool unused_index(size_t index); |
| 123 | /* |
| 124 | * Initialise granule at an non-deterministic granule. It updates both granule |
| 125 | * metadata array and physical page array. |
| 126 | */ |
| 127 | struct granule *inject_granule_at(const struct granule *granule_metadata, |
| 128 | const void *src_page, |
| 129 | size_t src_size, |
| 130 | size_t idx); |
| 131 | struct granule *inject_granule(const struct granule *granule_metadata, |
| 132 | const void *src_page, |
| 133 | size_t src_size); |
| 134 | |
Soby Mathew | bdd30ee | 2023-11-28 09:50:28 +0000 | [diff] [blame] | 135 | /* Returns the status of the granule in the GPT. */ |
| 136 | enum granule_gpt get_granule_gpt(uint64_t addr); |
| 137 | /* Set the status of the granule in GPT. */ |
| 138 | void set_granule_gpt(uint64_t addr, enum granule_gpt granule_gpt); |
Mate Toth-Pal | a18676e | 2023-10-27 17:05:01 +0200 | [diff] [blame] | 139 | |
Mate Toth-Pal | 83a45bd | 2023-09-01 11:17:19 +0200 | [diff] [blame] | 140 | #endif /* !TB_COMMON_H */ |