blob: 9a2947436fda8d855630ac8e665029b0ccb720bd [file] [log] [blame]
Mate Toth-Pal83a45bd2023-09-01 11:17:19 +02001/*
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-Pal0da58112024-01-10 11:49:58 +01009#include "granule.h"
Mate Toth-Pal83a45bd2023-09-01 11:17:19 +020010#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 */
61bool nondet_bool(void);
62unsigned char nondet_unsigned_char(void);
63unsigned short nondet_unsigned_short(void);
64unsigned int nondet_unsigned_int(void);
65unsigned long nondet_unsigned_long(void);
66char nondet_char(void);
67short nondet_short(void);
68int nondet_int(void);
69long nondet_long(void);
70uint32_t nondet_uint32_t(void);
71uint64_t nondet_uint64_t(void);
72int32_t nondet_int32_t(void);
73int64_t nondet_int64_t(void);
74size_t nondet_size_t(void);
75
76struct 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
86struct tb_regs __tb_arb_regs(void);
87
88bool ResultEqual_2(unsigned int code, unsigned int expected);
89bool ResultEqual_3(unsigned int code, unsigned int expected, unsigned int walk_level);
90uint64_t Zeros(void);
91
92/* Some autogen contains this function. */
93bool __tb_arb_bool(void);
94
95struct tb_lock_status {
96 uint64_t something;
97};
98void __tb_lock_invariant(struct tb_lock_status *lock_status);
99
100struct 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 */
106bool valid_pa(uint64_t addr);
107bool valid_granule_metadata_ptr(struct granule *p);
108struct granule *pa_to_granule_metadata_ptr(uint64_t addr);
Mate Toth-Pal70ae89d2024-01-11 13:47:28 +0100109void *granule_metadata_ptr_to_buffer_ptr(struct granule *g_ptr);
Mate Toth-Pal83a45bd2023-09-01 11:17:19 +0200110uint64_t granule_metadata_ptr_to_pa(struct granule *g_ptr);
Mate Toth-Palc751c0d2023-11-14 16:56:41 +0100111void *pa_to_granule_buffer_ptr(uint64_t addr);
112
Mate Toth-Pal83a45bd2023-09-01 11:17:19 +0200113/* TODO change the function name */
114void 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 */
121size_t next_index(void);
122bool 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 */
127struct granule *inject_granule_at(const struct granule *granule_metadata,
128 const void *src_page,
129 size_t src_size,
130 size_t idx);
131struct granule *inject_granule(const struct granule *granule_metadata,
132 const void *src_page,
133 size_t src_size);
134
Soby Mathewbdd30ee2023-11-28 09:50:28 +0000135/* Returns the status of the granule in the GPT. */
136enum granule_gpt get_granule_gpt(uint64_t addr);
137/* Set the status of the granule in GPT. */
138void set_granule_gpt(uint64_t addr, enum granule_gpt granule_gpt);
Mate Toth-Pala18676e2023-10-27 17:05:01 +0200139
Mate Toth-Pal83a45bd2023-09-01 11:17:19 +0200140#endif /* !TB_COMMON_H */