CHERIoT RTOS
A compartmentalised RTOS for CHERIoT hardware
Loading...
Searching...
No Matches
cheri-builtins.h
1// Copyright Microsoft and CHERIoT Contributors.
2// SPDX-License-Identifier: MIT
3
4#pragma once
5
6#include <__cheri_sealed.h>
7
8#define CHERI_PERM_GLOBAL (1U << 0)
9#define CHERI_PERM_LOAD_GLOBAL (1U << 1)
10#define CHERI_PERM_STORE (1U << 2)
11#define CHERI_PERM_LOAD_MUTABLE (1U << 3)
12#define CHERI_PERM_STORE_LOCAL (1U << 4)
13#define CHERI_PERM_LOAD (1U << 5)
14#define CHERI_PERM_LOAD_STORE_CAP (1U << 6)
15#define CHERI_PERM_ACCESS_SYS (1U << 7)
16#define CHERI_PERM_EXECUTE (1U << 8)
17#define CHERI_PERM_UNSEAL (1U << 9)
18#define CHERI_PERM_SEAL (1U << 10)
19#define CHERI_PERM_USER0 (1U << 11)
20
21#define CHERI_OTYPE_BITS 3
22
23#ifdef __cplusplus
24# include <cdefs.h>
25# include <stddef.h>
26# include <type_traits>
27
28static inline __always_inline ptraddr_t cheri_address_get(void *x)
29{
30 return __builtin_cheri_address_get(x);
31}
32
33static inline __always_inline auto cheri_address_set(auto *x, ptrdiff_t y)
34{
35 return __builtin_cheri_address_set(x, y);
36}
37
38static inline __always_inline auto cheri_address_increment(auto *x, ptrdiff_t y)
39{
40 return __builtin_cheri_address_increment(x, y);
41}
42
43static inline __always_inline ptraddr_t cheri_base_get(void *x)
44{
45 return __builtin_cheri_base_get(x);
46}
47
48static inline __always_inline ptraddr_t cheri_top_get(void *x)
49{
50 return __builtin_cheri_top_get(x);
51}
52
53static inline __always_inline ptraddr_t cheri_length_get(void *x)
54{
55 return __builtin_cheri_length_get(x);
56}
57
58static inline __always_inline auto cheri_tag_clear(void *x)
59{
60 return __builtin_cheri_tag_clear(x);
61}
62
63static inline __always_inline auto cheri_tag_get(void *x)
64{
65 return __builtin_cheri_tag_get(x);
66}
67
68static inline __always_inline bool cheri_is_valid(void *x)
69{
70 return cheri_tag_get(x);
71}
72
73static inline __always_inline bool cheri_is_invalid(void *x)
74{
75 return !cheri_tag_get(x);
76}
77
78static inline __always_inline bool cheri_is_equal_exact(void *x, void *y)
79{
80 return __builtin_cheri_equal_exact(x, y);
81}
82
83static inline __always_inline bool cheri_is_subset(void *x, void *y)
84{
85 return __builtin_cheri_subset_test(x, y);
86}
87
88static inline __always_inline auto cheri_permissions_get(void *x)
89{
90 return __builtin_cheri_perms_get(x);
91}
92
93static inline __always_inline auto cheri_permissions_and(void *x, unsigned y)
94{
95 return __builtin_cheri_perms_and(x, y);
96}
97
98static inline __always_inline auto cheri_type_get(void *x)
99{
100 return __builtin_cheri_type_get(x);
101}
102
103static inline __always_inline auto cheri_seal(auto *x, auto *y)
104{
105 return __builtin_cheri_seal(x, y);
106}
107
108static inline __always_inline auto cheri_unseal(CHERI_SEALED(auto *) x, auto *y)
109{
110 return __builtin_cheri_unseal(x, y);
111}
112
113static inline __always_inline auto cheri_bounds_set(auto *a, size_t b)
114{
115 return __builtin_cheri_bounds_set(a, b);
116}
117
118static inline __always_inline auto cheri_bounds_set_exact(auto *a, size_t b)
119{
120 return __builtin_cheri_bounds_set_exact(a, b);
121}
122
123static inline __always_inline auto cheri_subset_test(void *a, void *b)
124{
125 return __builtin_cheri_subset_test(a, b);
126}
127
128static inline __always_inline auto
129cheri_representable_alignment_mask(size_t len)
130{
131 return __builtin_cheri_representable_alignment_mask(len);
132}
133
134static inline __always_inline auto cheri_round_representable_length(size_t len)
135{
136 return __builtin_cheri_round_representable_length(len);
137}
138
139static inline __always_inline auto cheri_without_global(auto pointer)
140{
141 // Remove and reapply __sealed_capability if necessary
142 return reinterpret_cast<decltype(pointer)>(cheri_permissions_and(
143 reinterpret_cast<std::remove_reference_t<decltype(*pointer)> *>(pointer),
144 ~CHERI_PERM_GLOBAL));
145}
146#else
147# ifndef __ASSEMBLER__
148
149# include <stddef.h>
150# include <stdint.h>
151
152// Old deprecated macros. Here for compatibility, hopefully we can remove them
153// soon.
154# define cgetlen(foo) __builtin_cheri_length_get(foo)
155# pragma clang deprecated(cgetlen, "use cheri_length_get instead")
156
157# define cgetperms(foo) __builtin_cheri_perms_get(foo)
158# pragma clang deprecated(cgetperms, "use cheri_permissions_get instead")
159
160# define cgettype(foo) __builtin_cheri_type_get(foo)
161# pragma clang deprecated(cgettype, "use cheri_type_get instead")
162
163# define cgettag(foo) __builtin_cheri_tag_get(foo)
164# pragma clang deprecated(cgettag, "use cheri_tag_get instead")
165
166# define cincoffset(a, b) __builtin_cheri_offset_increment((a), (b))
167# pragma clang deprecated(cincoffset, \
168 "use cheri_address_increment instead")
169
170# define cgetaddr(a) __builtin_cheri_address_get(a)
171# pragma clang deprecated(cgetaddr, "use cheri_address_get instead")
172
173# define csetaddr(a, b) __builtin_cheri_address_set((a), (b))
174# pragma clang deprecated(csetaddr, "use cheri_address_set instead")
175
176# define cgetbase(foo) __builtin_cheri_base_get(foo)
177# pragma clang deprecated(cgetbase, "use cheri_base_get instead")
178
179# define candperms(a, b) __builtin_cheri_perms_and((a), (b))
180# pragma clang deprecated(candperms, "use cheri_permissions_and instead")
181
182# define cseal(a, b) __builtin_cheri_seal((a), (b))
183# pragma clang deprecated(cseal, "use cheri_seal instead")
184
185# define cunseal(a, b) __builtin_cheri_unseal((a), (b))
186# pragma clang deprecated(cunseal, "use cheri_unseal instead")
187
188# define csetbounds(a, b) __builtin_cheri_bounds_set((a), (b))
189# pragma clang deprecated(csetbounds, "use cheri_bounds_set instead")
190
191# define csetboundsext(a, b) __builtin_cheri_bounds_set_exact((a), (b))
192# pragma clang deprecated(csetboundsext, \
193 "use cheri_bounds_set_exact instead")
194
195# define cequalexact(a, b) __builtin_cheri_equal_exact((a), (b))
196# pragma clang deprecated(cequalexact, \
197 "use cheri_is_equal_exact instead")
198
199# define ctestsubset(a, b) __builtin_cheri_subset_test(a, b)
200# pragma clang deprecated(ctestsubset, "use cheri_subset_test instead")
201
202# define creplenalignmask(len) \
203 __builtin_cheri_representable_alignment_mask(len)
204# pragma clang deprecated( \
205 creplenalignmask, "use cheri_representable_alignment_mask instead")
206
207# define croundreplen(len) \
208 __builtin_cheri_round_representable_length(len)
209# pragma clang deprecated( \
210 croundreplen, "use cheri_round_representable_length instead")
211
212# define cheri_address_get(x) __builtin_cheri_address_get(x)
213# define cheri_address_set(x, y) __builtin_cheri_address_set((x), (y))
214# define cheri_address_increment(x, y) \
215 __builtin_cheri_offset_increment((x), (y))
216# define cheri_base_get(x) __builtin_cheri_base_get(x)
217# define cheri_top_get(x) __builtin_cheri_top_get(x)
218# define cheri_length_get(x) __builtin_cheri_length_get(x)
219# define cheri_tag_clear(x) __builtin_cheri_tag_clear(x)
220# define cheri_tag_get(x) __builtin_cheri_tag_get(x)
221# define cheri_is_valid(x) __builtin_cheri_tag_get(x)
222# define cheri_is_invalid(x) (!__builtin_cheri_tag_get(x))
223# define cheri_is_equal_exact(x, y) __builtin_cheri_equal_exact((x), (y))
224# define cheri_is_subset(x, y) __builtin_cheri_subset_test((x), (y))
225# define cheri_permissions_get(x) __builtin_cheri_perms_get(x)
226# define cheri_permissions_and(x, y) __builtin_cheri_perms_and((x), (y))
227# define cheri_type_get(x) __builtin_cheri_type_get(x)
228# define cheri_seal(a, b) \
229 (CHERI_SEALED((__typeof__((a)))) __builtin_cheri_seal((a), (b)))
230# define cheri_unseal(a, b) \
231 ((__typeof__(*(a)) *)__builtin_cheri_unseal((a), (b)))
232# define cheri_bounds_set(a, b) __builtin_cheri_bounds_set((a), (b))
233# define cheri_bounds_set_exact(a, b) \
234 __builtin_cheri_bounds_set_exact((a), (b))
235# define cheri_subset_test(a, b) __builtin_cheri_subset_test(a, b)
236# define cheri_representable_alignment_mask(len) \
237 __builtin_cheri_representable_alignment_mask(len)
238# define cheri_round_representable_length(len) \
239 __builtin_cheri_round_representable_length(len)
240# define cheri_without_global(pointer) \
241 ((__typeof__(pointer))(cheri_permissions_and( \
242 (__typeof__(*pointer) *)(pointer), ~CHERI_PERM_GLOBAL)))
243
244# endif // __ASSEMBLER__
245
246#endif