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