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
27static inline __always_inline ptraddr_t cheri_address_get(void *x)
28{
29 return __builtin_cheri_address_get(x);
30}
31
32static inline __always_inline auto cheri_address_set(auto *x, ptrdiff_t y)
33{
34 return __builtin_cheri_address_set(x, y);
35}
36
37static inline __always_inline auto cheri_address_increment(auto *x, ptrdiff_t y)
38{
39 return __builtin_cheri_address_increment(x, y);
40}
41
42static inline __always_inline ptraddr_t cheri_base_get(void *x)
43{
44 return __builtin_cheri_base_get(x);
45}
46
47static inline __always_inline ptraddr_t cheri_top_get(void *x)
48{
49 return __builtin_cheri_top_get(x);
50}
51
52static inline __always_inline ptraddr_t cheri_length_get(void *x)
53{
54 return __builtin_cheri_length_get(x);
55}
56
57static inline __always_inline auto cheri_tag_clear(void *x)
58{
59 return __builtin_cheri_tag_clear(x);
60}
61
62static inline __always_inline auto cheri_tag_get(void *x)
63{
64 return __builtin_cheri_tag_get(x);
65}
66
67static inline __always_inline bool cheri_is_valid(void *x)
68{
69 return cheri_tag_get(x);
70}
71
72static inline __always_inline bool cheri_is_invalid(void *x)
73{
74 return !cheri_tag_get(x);
75}
76
77static inline __always_inline bool cheri_is_equal_exact(void *x, void *y)
78{
79 return __builtin_cheri_equal_exact(x, y);
80}
81
82static inline __always_inline bool cheri_is_subset(void *x, void *y)
83{
84 return __builtin_cheri_subset_test(x, y);
85}
86
87static inline __always_inline auto cheri_permissions_get(void *x)
88{
89 return __builtin_cheri_perms_get(x);
90}
91
92static inline __always_inline auto cheri_permissions_and(void *x, unsigned y)
93{
94 return __builtin_cheri_perms_and(x, y);
95}
96
97static inline __always_inline auto cheri_type_get(void *x)
98{
99 return __builtin_cheri_type_get(x);
100}
101
102static inline __always_inline auto cheri_seal(auto *x, auto *y)
103{
104 return __builtin_cheri_seal(x, y);
105}
106
107static inline __always_inline auto cheri_unseal(CHERI_SEALED(auto *) x, auto *y)
108{
109 return __builtin_cheri_unseal(x, y);
110}
111
112static inline __always_inline auto cheri_bounds_set(auto *a, size_t b)
113{
114 return __builtin_cheri_bounds_set(a, b);
115}
116
117static inline __always_inline auto cheri_bounds_set_exact(auto *a, size_t b)
118{
119 return __builtin_cheri_bounds_set_exact(a, b);
120}
121
122static inline __always_inline auto cheri_subset_test(void *a, void *b)
123{
124 return __builtin_cheri_subset_test(a, b);
125}
126
127static inline __always_inline auto
128cheri_representable_alignment_mask(size_t len)
129{
130 return __builtin_cheri_representable_alignment_mask(len);
131}
132
133static inline __always_inline auto cheri_round_representable_length(size_t len)
134{
135 return __builtin_cheri_round_representable_length(len);
136}
137
138#else
139# ifndef __ASSEMBLER__
140
141# include <stddef.h>
142# include <stdint.h>
143
144// Old deprecated macros. Here for compatibility, hopefully we can remove them
145// soon.
146# define cgetlen(foo) __builtin_cheri_length_get(foo)
147# pragma clang deprecated(cgetlen, "use cheri_length_get instead")
148
149# define cgetperms(foo) __builtin_cheri_perms_get(foo)
150# pragma clang deprecated(cgetperms, "use cheri_permissions_get instead")
151
152# define cgettype(foo) __builtin_cheri_type_get(foo)
153# pragma clang deprecated(cgettype, "use cheri_type_get instead")
154
155# define cgettag(foo) __builtin_cheri_tag_get(foo)
156# pragma clang deprecated(cgettag, "use cheri_tag_get instead")
157
158# define cincoffset(a, b) __builtin_cheri_offset_increment((a), (b))
159# pragma clang deprecated(cincoffset, \
160 "use cheri_address_increment instead")
161
162# define cgetaddr(a) __builtin_cheri_address_get(a)
163# pragma clang deprecated(cgetaddr, "use cheri_address_get instead")
164
165# define csetaddr(a, b) __builtin_cheri_address_set((a), (b))
166# pragma clang deprecated(csetaddr, "use cheri_address_set instead")
167
168# define cgetbase(foo) __builtin_cheri_base_get(foo)
169# pragma clang deprecated(cgetbase, "use cheri_base_get instead")
170
171# define candperms(a, b) __builtin_cheri_perms_and((a), (b))
172# pragma clang deprecated(candperms, "use cheri_permissions_and instead")
173
174# define cseal(a, b) __builtin_cheri_seal((a), (b))
175# pragma clang deprecated(cseal, "use cheri_seal instead")
176
177# define cunseal(a, b) __builtin_cheri_unseal((a), (b))
178# pragma clang deprecated(cunseal, "use cheri_unseal instead")
179
180# define csetbounds(a, b) __builtin_cheri_bounds_set((a), (b))
181# pragma clang deprecated(csetbounds, "use cheri_bounds_set instead")
182
183# define csetboundsext(a, b) __builtin_cheri_bounds_set_exact((a), (b))
184# pragma clang deprecated(csetboundsext, \
185 "use cheri_bounds_set_exact instead")
186
187# define cequalexact(a, b) __builtin_cheri_equal_exact((a), (b))
188# pragma clang deprecated(cequalexact, \
189 "use cheri_is_equal_exact instead")
190
191# define ctestsubset(a, b) __builtin_cheri_subset_test(a, b)
192# pragma clang deprecated(ctestsubset, "use cheri_subset_test instead")
193
194# define creplenalignmask(len) \
195 __builtin_cheri_representable_alignment_mask(len)
196# pragma clang deprecated( \
197 creplenalignmask, "use cheri_representable_alignment_mask instead")
198
199# define croundreplen(len) \
200 __builtin_cheri_round_representable_length(len)
201# pragma clang deprecated( \
202 croundreplen, "use cheri_round_representable_length instead")
203
204# define cheri_address_get(x) __builtin_cheri_address_get(x)
205# define cheri_address_set(x, y) __builtin_cheri_address_set((x), (y))
206# define cheri_address_increment(x, y) \
207 __builtin_cheri_offset_increment((x), (y))
208# define cheri_base_get(x) __builtin_cheri_base_get(x)
209# define cheri_top_get(x) __builtin_cheri_top_get(x)
210# define cheri_length_get(x) __builtin_cheri_length_get(x)
211# define cheri_tag_clear(x) __builtin_cheri_tag_clear(x)
212# define cheri_tag_get(x) __builtin_cheri_tag_get(x)
213# define cheri_is_valid(x) __builtin_cheri_tag_get(x)
214# define cheri_is_invalid(x) (!__builtin_cheri_tag_get(x))
215# define cheri_is_equal_exact(x, y) __builtin_cheri_equal_exact((x), (y))
216# define cheri_is_subset(x, y) __builtin_cheri_subset_test((x), (y))
217# define cheri_permissions_get(x) __builtin_cheri_perms_get(x)
218# define cheri_permissions_and(x, y) __builtin_cheri_perms_and((x), (y))
219# define cheri_type_get(x) __builtin_cheri_type_get(x)
220# define cheri_seal(a, b) \
221 (CHERI_SEALED((__typeof__((a)))) __builtin_cheri_seal((a), (b)))
222# define cheri_unseal(a, b) \
223 ((__typeof__(*(a)) *)__builtin_cheri_unseal((a), (b)))
224# define cheri_bounds_set(a, b) __builtin_cheri_bounds_set((a), (b))
225# define cheri_bounds_set_exact(a, b) \
226 __builtin_cheri_bounds_set_exact((a), (b))
227# define cheri_subset_test(a, b) __builtin_cheri_subset_test(a, b)
228# define cheri_representable_alignment_mask(len) \
229 __builtin_cheri_representable_alignment_mask(len)
230# define cheri_round_representable_length(len) \
231 __builtin_cheri_round_representable_length(len)
232
233# endif // __ASSEMBLER__
234
235#endif