Skip to content

Commit

Permalink
Share more code between 64r and 128r format
Browse files Browse the repository at this point in the history
  • Loading branch information
arichardson committed Feb 6, 2025
1 parent 688ea9e commit db44ebf
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 61 deletions.
31 changes: 1 addition & 30 deletions cheri_compressed_cap_128r.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,33 +154,4 @@ _CC_STATIC_ASSERT_SAME(CC128R_MANTISSA_WIDTH, CC128R_FIELD_EXP_ZERO_BOTTOM_SIZE)
#define CC128R_USES_LEN_MSB 0

#include "cheri_compressed_cap_common.h"

static inline uint8_t cc128r_get_reserved(const cc128r_cap_t* cap) {
return _CC_EXTRACT_SPLIT_FIELD(cap->cr_pesbt, RESERVED1, RESERVED0);
}

static inline bool _cc_N(bounds_malformed)(_cc_bounds_bits bounds) {
// The spec defines this check as checking for E < 0, but since we store it as an unsigned number, we compare it to
// the maximum exponent instead.
bool malformedLSB = bounds.E > _CC_MAX_EXPONENT;
bool malformedMSB = (bounds.E == _CC_MAX_EXPONENT && bounds.B != 0) ||
(bounds.E == _CC_MAX_EXPONENT - 1 && (bounds.B & (1u << (_CC_MANTISSA_WIDTH - 1))) != 0);
return bounds.IE && (malformedLSB || malformedMSB);
}

static inline bool _cc_N(compute_base_top_special_cases)(_cc_bounds_bits bounds, _cc_addr_t* base_out,
_cc_length_t* top_out, bool* valid) {
if (_cc_N(bounds_malformed)(bounds)) {
*base_out = 0;
*top_out = 0;
*valid = false;
return true;
}
return false;
}

// Check that there is no XOR mask for this encoding format (i.e. NULL encodes to all-zeroes in memory).
_CC_STATIC_ASSERT_SAME(CC128R_MEM_XOR_MASK, UINT64_C(0));

#undef CC_FORMAT_LOWER
#undef CC_FORMAT_UPPER
#include "cheri_compressed_cap_riscv_common.h"
32 changes: 1 addition & 31 deletions cheri_compressed_cap_64r.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,34 +155,4 @@ _CC_STATIC_ASSERT_SAME(CC64R_MANTISSA_WIDTH, CC64R_FIELD_EXP_ZERO_BOTTOM_SIZE);
#define CC64R_USES_LEN_MSB 1

#include "cheri_compressed_cap_common.h"

static inline uint8_t _cc_N(get_reserved)(const _cc_cap_t* cap) {
return _CC_EXTRACT_SPLIT_FIELD(cap->cr_pesbt, RESERVED1, RESERVED0);
}

static inline bool _cc_N(bounds_malformed)(_cc_bounds_bits bounds) {
// The spec defines this check as checking for E < 0, but since we store it as an unsigned number, we compare it to
// the maximum exponent instead.
// For MXLEN==32, we also report invalid bounds for IE && E == 0
bool malformedLSB = bounds.E > _CC_MAX_EXPONENT || (bounds.E == 0);
bool malformedMSB = (bounds.E == _CC_MAX_EXPONENT && bounds.B != 0) ||
(bounds.E == _CC_MAX_EXPONENT - 1 && (bounds.B & (1u << (_CC_MANTISSA_WIDTH - 1))) != 0);
return bounds.IE && (malformedLSB || malformedMSB);
}

static inline bool _cc_N(compute_base_top_special_cases)(_cc_bounds_bits bounds, _cc_addr_t* base_out,
_cc_length_t* top_out, bool* valid) {
if (_cc_N(bounds_malformed)(bounds)) {
*base_out = 0;
*top_out = 0;
*valid = false;
return true;
}
return false;
}

// Check that there is no XOR mask for this encoding format (i.e. NULL encodes to all-zeroes in memory).
_CC_STATIC_ASSERT_SAME(CC64R_MEM_XOR_MASK, UINT64_C(0));

#undef CC_FORMAT_LOWER
#undef CC_FORMAT_UPPER
#include "cheri_compressed_cap_riscv_common.h"
35 changes: 35 additions & 0 deletions cheri_compressed_cap_riscv_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: BSD-2-Clause

static inline uint8_t _cc_N(get_reserved)(const _cc_cap_t* cap) {
return _CC_EXTRACT_SPLIT_FIELD(cap->cr_pesbt, RESERVED1, RESERVED0);
}

static inline bool _cc_N(bounds_malformed)(_cc_bounds_bits bounds) {
// The spec defines this check as checking for E < 0, but since we store it as an unsigned number, we compare it to
// the maximum exponent instead.
bool malformedLSB = bounds.E > _CC_MAX_EXPONENT;
#if _CC_N(USES_LEN_MSB) != 0
// For MXLEN==32, we also report invalid bounds for IE && E == 0
malformedLSB = malformedLSB || (bounds.E == 0);
#endif
bool malformedMSB = (bounds.E == _CC_MAX_EXPONENT && bounds.B != 0) ||
(bounds.E == _CC_MAX_EXPONENT - 1 && (bounds.B & (1u << (_CC_MANTISSA_WIDTH - 1))) != 0);
return bounds.IE && (malformedLSB || malformedMSB);
}

static inline bool _cc_N(compute_base_top_special_cases)(_cc_bounds_bits bounds, _cc_addr_t* base_out,
_cc_length_t* top_out, bool* valid) {
if (_cc_N(bounds_malformed)(bounds)) {
*base_out = 0;
*top_out = 0;
*valid = false;
return true;
}
return false;
}

// Check that there is no XOR mask for this encoding format (i.e. NULL encodes to all-zeroes in memory).
_CC_STATIC_ASSERT_SAME(_CC_N(MEM_XOR_MASK), UINT64_C(0));

#undef CC_FORMAT_LOWER
#undef CC_FORMAT_UPPER

0 comments on commit db44ebf

Please sign in to comment.