| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- I N T E R F A C E S . C H E R I -- |
| 6 |
-- -- |
| 7 |
-- S p e c -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2023-2025, AdaCore -- |
| 10 |
-- -- |
| 11 |
-- GNAT is free software; you can redistribute it and/or modify it under -- |
| 12 |
-- terms of the GNU General Public License as published by the Free Soft- -- |
| 13 |
-- ware Foundation; either version 3, or (at your option) any later ver- -- |
| 14 |
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
| 15 |
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- |
| 16 |
-- or FITNESS FOR A PARTICULAR PURPOSE. -- |
| 17 |
-- -- |
| 18 |
-- As a special exception under Section 7 of GPL version 3, you are granted -- |
| 19 |
-- additional permissions described in the GCC Runtime Library Exception, -- |
| 20 |
-- version 3.1, as published by the Free Software Foundation. -- |
| 21 |
-- -- |
| 22 |
-- You should have received a copy of the GNU General Public License and -- |
| 23 |
-- a copy of the GCC Runtime Library Exception along with this program; -- |
| 24 |
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see -- |
| 25 |
-- <http://www.gnu.org/licenses/>. -- |
| 26 |
-- -- |
| 27 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 28 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 29 |
-- -- |
| 30 |
------------------------------------------------------------------------------ |
| 31 |
|
| 32 |
-- This package provides bindings to CHERI intrinsics and some common |
| 33 |
-- operations on capabilities. |
| 34 |
|
| 35 |
with System; |
| 36 |
with System.Storage_Elements; |
| 37 |
|
| 38 |
package Interfaces.CHERI with |
| 39 |
Preelaborate, |
| 40 |
No_Elaboration_Code_All |
| 41 |
is |
| 42 |
|
| 43 |
use type System.Storage_Elements.Integer_Address; |
| 44 |
|
| 45 |
subtype Capability is System.Address; |
| 46 |
|
| 47 |
type Bounds_Length is range 0 .. System.Memory_Size - 1 with |
| 48 |
Size => System.Word_Size; |
| 49 |
|
| 50 |
---------------------------- |
| 51 |
-- Capability Permissions -- |
| 52 |
---------------------------- |
| 53 |
|
| 54 |
type Permissions_Mask is mod System.Memory_Size with |
| 55 |
Size => System.Word_Size; |
| 56 |
|
| 57 |
Global : constant Permissions_Mask := 16#0001#; |
| 58 |
Executive : constant Permissions_Mask := 16#0002#; |
| 59 |
Mutable_Load : constant Permissions_Mask := 16#0040#; |
| 60 |
Compartment_Id : constant Permissions_Mask := 16#0080#; |
| 61 |
Branch_Sealed_Pair : constant Permissions_Mask := 16#0100#; |
| 62 |
Access_System_Registers : constant Permissions_Mask := 16#0200#; |
| 63 |
Permit_Unseal : constant Permissions_Mask := 16#0400#; |
| 64 |
Permit_Seal : constant Permissions_Mask := 16#0800#; |
| 65 |
Permit_Store_Local : constant Permissions_Mask := 16#1000#; |
| 66 |
Permit_Store_Capability : constant Permissions_Mask := 16#2000#; |
| 67 |
Permit_Load_Capability : constant Permissions_Mask := 16#4000#; |
| 68 |
Permit_Execute : constant Permissions_Mask := 16#8000#; |
| 69 |
Permit_Store : constant Permissions_Mask := 16#1_0000#; |
| 70 |
Permit_Load : constant Permissions_Mask := 16#2_0000#; |
| 71 |
|
| 72 |
function "and" |
| 73 |
(Cap : Capability; |
| 74 |
Mask : Permissions_Mask) |
| 75 |
return Capability |
| 76 |
with |
| 77 |
Import, Convention => Intrinsic, |
| 78 |
External_Name => "__builtin_cheri_perms_and"; |
| 79 |
-- Perform a bitwise-AND of a capability permissions and the specified |
| 80 |
-- mask, returning the new capability. |
| 81 |
|
| 82 |
function Get_Permissions (Cap : Capability) return Permissions_Mask with |
| 83 |
Import, Convention => Intrinsic, |
| 84 |
External_Name => "__builtin_cheri_perms_get"; |
| 85 |
-- Get the permissions of a capability |
| 86 |
|
| 87 |
function Clear_Permissions |
| 88 |
(Cap : Capability; |
| 89 |
Mask : Permissions_Mask) |
| 90 |
return Capability is |
| 91 |
(Cap and not Mask); |
| 92 |
-- Clear the specified permissions of a capability, returning the new |
| 93 |
-- capability. |
| 94 |
|
| 95 |
function Has_All_Permissions |
| 96 |
(Cap : Capability; |
| 97 |
Permissions : Permissions_Mask) |
| 98 |
return Boolean is |
| 99 |
((Get_Permissions (Cap) and Permissions) = Permissions); |
| 100 |
-- Query whether all of the specified permission bits are set in a |
| 101 |
-- capability's permissions flags. |
| 102 |
|
| 103 |
----------------------- |
| 104 |
-- Common Intrinsics -- |
| 105 |
----------------------- |
| 106 |
|
| 107 |
function Capability_With_Address |
| 108 |
(Cap : Capability; |
| 109 |
Addr : System.Storage_Elements.Integer_Address) |
| 110 |
return Capability |
| 111 |
with |
| 112 |
Import, Convention => Intrinsic, |
| 113 |
External_Name => "__builtin_cheri_address_set"; |
| 114 |
-- Return a new capability with the same bounds and permissions as Cap, |
| 115 |
-- with the address set to Addr. |
| 116 |
|
| 117 |
function Get_Address |
| 118 |
(Cap : Capability) |
| 119 |
return System.Storage_Elements.Integer_Address |
| 120 |
with |
| 121 |
Import, Convention => Intrinsic, |
| 122 |
External_Name => "__builtin_cheri_address_get"; |
| 123 |
-- Get the address of a capability |
| 124 |
|
| 125 |
procedure Set_Address |
| 126 |
(Cap : in out Capability; |
| 127 |
Addr : System.Storage_Elements.Integer_Address) |
| 128 |
with |
| 129 |
Import, Convention => Intrinsic, |
| 130 |
External_Name => "__builtin_cheri_address_set"; |
| 131 |
-- Set the address of a capability |
| 132 |
|
| 133 |
function Get_Base |
| 134 |
(Cap : Capability) |
| 135 |
return System.Storage_Elements.Integer_Address |
| 136 |
with |
| 137 |
Import, Convention => Intrinsic, |
| 138 |
External_Name => "__builtin_cheri_base_get"; |
| 139 |
-- Get the lower bound of a capability |
| 140 |
|
| 141 |
function Get_Offset (Cap : Capability) return Bounds_Length with |
| 142 |
Import, Convention => Intrinsic, |
| 143 |
External_Name => "__builtin_cheri_offset_get"; |
| 144 |
-- Get the difference between the address and the lower bound of a |
| 145 |
-- capability. |
| 146 |
|
| 147 |
procedure Set_Offset |
| 148 |
(Cap : in out Capability; |
| 149 |
Offset : Bounds_Length) |
| 150 |
with |
| 151 |
Import, Convention => Intrinsic, |
| 152 |
External_Name => "__builtin_cheri_offset_set"; |
| 153 |
-- Set the address relative to the lower bound of a capability |
| 154 |
|
| 155 |
function Capability_With_Offset |
| 156 |
(Cap : Capability; |
| 157 |
Value : Bounds_Length) |
| 158 |
return Capability |
| 159 |
with |
| 160 |
Import, Convention => Intrinsic, |
| 161 |
External_Name => "__builtin_cheri_offset_set"; |
| 162 |
-- Set the address relative to the lower bound of a capability, returning |
| 163 |
-- the new capability. |
| 164 |
|
| 165 |
function Increment_Offset |
| 166 |
(Cap : Capability; |
| 167 |
Value : Bounds_Length) |
| 168 |
return Capability |
| 169 |
with |
| 170 |
Import, Convention => Intrinsic, |
| 171 |
External_Name => "__builtin_cheri_offset_increment"; |
| 172 |
-- Increment the address of a capability by the specified amount, |
| 173 |
-- returning the new capability. |
| 174 |
|
| 175 |
function Get_Length (Cap : Capability) return Bounds_Length with |
| 176 |
Import, Convention => Intrinsic, |
| 177 |
External_Name => "__builtin_cheri_length_get"; |
| 178 |
-- Get the length of a capability's bounds |
| 179 |
|
| 180 |
function Clear_Tag (Cap : Capability) return Capability with |
| 181 |
Import, Convention => Intrinsic, |
| 182 |
External_Name => "__builtin_cheri_tag_clear"; |
| 183 |
-- Clear the capability validity tag, returning the new capability |
| 184 |
|
| 185 |
function Get_Tag (Cap : Capability) return Boolean with |
| 186 |
Import, Convention => Intrinsic, |
| 187 |
External_Name => "__builtin_cheri_tag_get"; |
| 188 |
-- Get the validty tag of a capability |
| 189 |
|
| 190 |
function Is_Valid (Cap : Capability) return Boolean is (Get_Tag (Cap)); |
| 191 |
-- Check whether a capability is valid |
| 192 |
|
| 193 |
function Is_Invalid (Cap : Capability) return Boolean is |
| 194 |
(not Is_Valid (Cap)); |
| 195 |
-- Check whether a capability is invalid |
| 196 |
|
| 197 |
function Is_Equal_Exact (A, B : Capability) return Boolean with |
| 198 |
Import, Convention => Intrinsic, |
| 199 |
External_Name => "__builtin_cheri_equal_exact"; |
| 200 |
-- Check for bit equality between two capabilities |
| 201 |
|
| 202 |
function Is_Subset (A, B : Capability) return Boolean with |
| 203 |
Import, Convention => Intrinsic, |
| 204 |
External_Name => "__builtin_cheri_subset_test"; |
| 205 |
-- Returns True if capability A is a subset or equal to capability B |
| 206 |
|
| 207 |
-------------------- |
| 208 |
-- Bounds Setting -- |
| 209 |
-------------------- |
| 210 |
|
| 211 |
function Capability_With_Bounds |
| 212 |
(Cap : Capability; |
| 213 |
Length : Bounds_Length) |
| 214 |
return Capability |
| 215 |
with |
| 216 |
Import, Convention => Intrinsic, |
| 217 |
External_Name => "__builtin_cheri_bounds_set"; |
| 218 |
-- Narrow the bounds of a capability so that the lower bound is the |
| 219 |
-- current address and the upper bound is suitable for the Length, |
| 220 |
-- returning the new capability. |
| 221 |
-- |
| 222 |
-- Note that the effective bounds of the returned capability may be wider |
| 223 |
-- than the range Get_Address (Cap) .. Get_Address (Cap) + Length - 1 due |
| 224 |
-- to capability compression, but they will always be a subset of the |
| 225 |
-- original bounds. |
| 226 |
|
| 227 |
function Capability_With_Exact_Bounds |
| 228 |
(Cap : Capability; |
| 229 |
Length : Bounds_Length) |
| 230 |
return Capability |
| 231 |
with |
| 232 |
Import, Convention => Intrinsic, |
| 233 |
External_Name => "__builtin_cheri_bounds_set_exact"; |
| 234 |
-- Narrow the bounds of a capability so that the lower bound is the |
| 235 |
-- current address and the upper bound is suitable for the Length, |
| 236 |
-- returning the new capability. |
| 237 |
-- |
| 238 |
-- This is similar to Capability_With_Bounds but will clear the capability |
| 239 |
-- tag in the returned capability if the bounds cannot be set exactly, due |
| 240 |
-- to capability compression. |
| 241 |
|
| 242 |
function Capability_With_Address_And_Bounds |
| 243 |
(Cap : Capability; |
| 244 |
Address : System.Storage_Elements.Integer_Address; |
| 245 |
Length : Bounds_Length) |
| 246 |
return Capability is |
| 247 |
(Capability_With_Bounds |
| 248 |
(Capability_With_Address (Cap, Address), Length)); |
| 249 |
-- Set the address and narrow the bounds of the capability so that the |
| 250 |
-- lower bound is Address and the upper bound is Address + Length, |
| 251 |
-- returning the new capability. |
| 252 |
-- |
| 253 |
-- Note that the effective bounds of the returned capability may be wider |
| 254 |
-- than the range Address .. Address + Length - 1 due to capability |
| 255 |
-- compression, but they will always be a subset of the original bounds. |
| 256 |
|
| 257 |
function Capability_With_Address_And_Exact_Bounds |
| 258 |
(Cap : Capability; |
| 259 |
Address : System.Storage_Elements.Integer_Address; |
| 260 |
Length : Bounds_Length) |
| 261 |
return Capability is |
| 262 |
(Capability_With_Exact_Bounds |
| 263 |
(Capability_With_Address (Cap, Address), Length)); |
| 264 |
-- Set the address and narrow the bounds of the capability so that the |
| 265 |
-- lower bound is Address and the upper bound is Address + Length, |
| 266 |
-- returning the new capability. |
| 267 |
-- |
| 268 |
-- This is similar to Capability_With_Address_And_Bounds but will clear the |
| 269 |
-- capability tag in the returned capability if the bounds cannot be set |
| 270 |
-- exactly, due to capability compression. |
| 271 |
|
| 272 |
procedure Set_Bounds |
| 273 |
(Cap : in out Capability; |
| 274 |
Length : Bounds_Length) |
| 275 |
with |
| 276 |
Import, Convention => Intrinsic, |
| 277 |
External_Name => "__builtin_cheri_bounds_set"; |
| 278 |
-- Narrow the bounds of a capability so that the lower bound is the |
| 279 |
-- current address and the upper bound is suitable for the Length. |
| 280 |
-- |
| 281 |
-- Note that the effective bounds of the output capability may be wider |
| 282 |
-- than the range Get_Address (Cap) .. Get_Address (Cap) + Length - 1 due |
| 283 |
-- to capability compression, but they will always be a subset of the |
| 284 |
-- original bounds. |
| 285 |
|
| 286 |
procedure Set_Exact_Bounds |
| 287 |
(Cap : in out Capability; |
| 288 |
Length : Bounds_Length) |
| 289 |
with |
| 290 |
Import, Convention => Intrinsic, |
| 291 |
External_Name => "__builtin_cheri_bounds_set_exact"; |
| 292 |
-- Narrow the bounds of a capability so that the lower bound is the |
| 293 |
-- current address and the upper bound is suitable for the Length. |
| 294 |
-- |
| 295 |
-- This is similar to Set_Bounds but will clear the capability tag if the |
| 296 |
-- bounds cannot be set exactly, due to capability compression. |
| 297 |
|
| 298 |
procedure Set_Address_And_Bounds |
| 299 |
(Cap : in out Capability; |
| 300 |
Address : System.Storage_Elements.Integer_Address; |
| 301 |
Length : Bounds_Length) |
| 302 |
with |
| 303 |
Inline_Always; |
| 304 |
-- Set the address and narrow the bounds of the capability so that the |
| 305 |
-- lower bound is Address and the upper bound is Address + Length. |
| 306 |
-- |
| 307 |
-- Note that the effective bounds of the output capability may be wider |
| 308 |
-- than the range Address .. Address + Length - 1 due to capability |
| 309 |
-- compression, but they will always be a subset of the original bounds. |
| 310 |
|
| 311 |
procedure Set_Address_And_Exact_Bounds |
| 312 |
(Cap : in out Capability; |
| 313 |
Address : System.Storage_Elements.Integer_Address; |
| 314 |
Length : Bounds_Length) |
| 315 |
with |
| 316 |
Inline_Always; |
| 317 |
-- Set the address and narrow the bounds of the capability so that the |
| 318 |
-- lower bound is Address and the upper bound is Address + Length. |
| 319 |
-- |
| 320 |
-- This is similar to Set_Address_And_Bounds but will clear the capability |
| 321 |
-- tag if the bounds cannot be set exactly, due to capability compression. |
| 322 |
|
| 323 |
function Representable_Length (Length : Bounds_Length) return Bounds_Length |
| 324 |
with |
| 325 |
Import, Convention => Intrinsic, |
| 326 |
External_Name => "__builtin_cheri_round_representable_length"; |
| 327 |
-- Returns the length that a capability would have after using Set_Bounds |
| 328 |
-- to set the Length (assuming appropriate alignment of the base). |
| 329 |
|
| 330 |
function Representable_Alignment_Mask |
| 331 |
(Length : Bounds_Length) |
| 332 |
return System.Storage_Elements.Integer_Address |
| 333 |
with |
| 334 |
Import, Convention => Intrinsic, |
| 335 |
External_Name => "__builtin_cheri_representable_alignment_mask"; |
| 336 |
-- Returns a bitmask that can be used to align an address downwards such |
| 337 |
-- that it is sufficiently aligned to create a precisely bounded |
| 338 |
-- capability. |
| 339 |
|
| 340 |
function Align_Address_Down |
| 341 |
(Address : System.Storage_Elements.Integer_Address; |
| 342 |
Length : Bounds_Length) |
| 343 |
return System.Storage_Elements.Integer_Address is |
| 344 |
(Address and Representable_Alignment_Mask (Length)); |
| 345 |
-- Align an address such that it is sufficiently aligned to create a |
| 346 |
-- precisely bounded capability, rounding down if necessary. |
| 347 |
-- |
| 348 |
-- Due to capability compression, the upper and lower bounds of a |
| 349 |
-- capability must be aligned based on the length of the bounds to ensure |
| 350 |
-- that the capability is representable. This function aligns an address |
| 351 |
-- down to the next boundary if it is not already aligned. |
| 352 |
|
| 353 |
function Capability_With_Address_Aligned_Down |
| 354 |
(Cap : Capability; |
| 355 |
Length : Bounds_Length) |
| 356 |
return Capability is |
| 357 |
(Capability_With_Address |
| 358 |
(Cap, Align_Address_Down (Get_Address (Cap), Length))); |
| 359 |
-- Align a capability's address such that it is sufficiently aligned to |
| 360 |
-- create a precisely bounded capability, rounding down if necessary. |
| 361 |
-- |
| 362 |
-- Due to capability compression, the upper and lower bounds of a |
| 363 |
-- capability must be aligned based on the length of the bounds to ensure |
| 364 |
-- that the capability is representable. This function aligns an address |
| 365 |
-- down to the next boundary if it is not already aligned. |
| 366 |
|
| 367 |
function Align_Address_Up |
| 368 |
(Address : System.Storage_Elements.Integer_Address; |
| 369 |
Length : Bounds_Length) |
| 370 |
return System.Storage_Elements.Integer_Address |
| 371 |
with |
| 372 |
Inline; |
| 373 |
-- Align an address such that it is sufficiently aligned to create a |
| 374 |
-- precisely bounded capability, rounding upwards if necessary. |
| 375 |
-- |
| 376 |
-- Due to capability compression, the upper and lower bounds of a |
| 377 |
-- capability must be aligned based on the length of the bounds to ensure |
| 378 |
-- that the capability is representable. This function aligns an address up |
| 379 |
-- to the next boundary if it is not already aligned. |
| 380 |
|
| 381 |
function Capability_With_Address_Aligned_Up |
| 382 |
(Cap : Capability; |
| 383 |
Length : Bounds_Length) |
| 384 |
return Capability is |
| 385 |
(Capability_With_Address |
| 386 |
(Cap, Align_Address_Up (Get_Address (Cap), Length))); |
| 387 |
-- Align a capability's address such that it is sufficiently aligned to |
| 388 |
-- create a precisely bounded capability, rounding upwards if necessary. |
| 389 |
-- |
| 390 |
-- Due to capability compression, the upper and lower bounds of a |
| 391 |
-- capability must be aligned based on the length of the bounds to ensure |
| 392 |
-- that the capability is representable. This function aligns an address up |
| 393 |
-- to the next boundary if it is not already aligned. |
| 394 |
|
| 395 |
------------------------------------------ |
| 396 |
-- Object Types, Sealing, and Unsealing -- |
| 397 |
------------------------------------------ |
| 398 |
|
| 399 |
type Object_Type is |
| 400 |
range -2**(System.Word_Size - 1) .. +2**(System.Word_Size - 1) - 1 with |
| 401 |
Size => System.Word_Size; |
| 402 |
|
| 403 |
Type_Unsealed : constant Object_Type := 0; |
| 404 |
Type_Sentry : constant Object_Type := 1; |
| 405 |
|
| 406 |
function Get_Object_Type (Cap : Capability) return Object_Type with |
| 407 |
Import, Convention => Intrinsic, |
| 408 |
External_Name => "__builtin_cheri_type_get"; |
| 409 |
-- Get the object type of a capability |
| 410 |
|
| 411 |
function Is_Sealed (Cap : Capability) return Boolean with |
| 412 |
Import, Convention => Intrinsic, |
| 413 |
External_Name => "__builtin_cheri_sealed_get"; |
| 414 |
-- Check whether a capability is sealed |
| 415 |
|
| 416 |
function Is_Unsealed (Cap : Capability) return Boolean is |
| 417 |
(not Is_Sealed (Cap)); |
| 418 |
-- Check whether a capability is unsealed |
| 419 |
|
| 420 |
function Is_Sentry (Cap : Capability) return Boolean is |
| 421 |
(Get_Object_Type (Cap) = Type_Sentry); |
| 422 |
-- Check whether a capability is a sealed entry |
| 423 |
|
| 424 |
function Create_Sentry (Cap : Capability) return Capability with |
| 425 |
Import, Convention => Intrinsic, |
| 426 |
External_Name => "__builtin_cheri_seal_entry"; |
| 427 |
-- Create a sealed entry and return the new capability. |
| 428 |
|
| 429 |
function Seal |
| 430 |
(Cap : Capability; |
| 431 |
Sealing_Cap : Capability) |
| 432 |
return Capability |
| 433 |
with |
| 434 |
Import, Convention => Intrinsic, |
| 435 |
External_Name => "__builtin_cheri_seal"; |
| 436 |
-- Seal a capability with a sealing capability, by setting the object type |
| 437 |
-- of the capability to the Sealing_Cap's value, returning the sealed |
| 438 |
-- capability. |
| 439 |
|
| 440 |
function Unseal |
| 441 |
(Cap : Capability; |
| 442 |
Unsealing_Cap : Capability) |
| 443 |
return Capability |
| 444 |
with |
| 445 |
Import, Convention => Intrinsic, |
| 446 |
External_Name => "__builtin_cheri_unseal"; |
| 447 |
-- Unseal a capability with an unsealing capability, by checking the object |
| 448 |
-- type of the capability against the Sealing_Cap's value, returning the |
| 449 |
-- unsealed capability. |
| 450 |
|
| 451 |
----------------------------------- |
| 452 |
-- Capability Register Accessors -- |
| 453 |
----------------------------------- |
| 454 |
|
| 455 |
function Get_PCC return System.Address with |
| 456 |
Import, Convention => Intrinsic, |
| 457 |
External_Name => "__builtin_cheri_program_counter_get"; |
| 458 |
-- Get the Program Counter Capablity (PCC) |
| 459 |
|
| 460 |
function Get_DDC return System.Address with |
| 461 |
Import, Convention => Intrinsic, |
| 462 |
External_Name => "__builtin_cheri_global_data_get"; |
| 463 |
-- Get the Default Data Capability (DDC) |
| 464 |
|
| 465 |
function Get_CSP return System.Address with |
| 466 |
Import, Convention => Intrinsic, |
| 467 |
External_Name => "__builtin_cheri_stack_get"; |
| 468 |
-- Get the Capability Stack Pointer (CSP) |
| 469 |
|
| 470 |
end Interfaces.CHERI; |