Commit 35f50c91 authored by Michael Roth's avatar Michael Roth Committed by Sean Christopherson

KVM: selftests: Make sparsebit structs const where appropriate

Make all sparsebit struct pointers "const" where appropriate.  This will
allow adding a bitmap to track protected/encrypted physical memory that
tests can access in a read-only fashion.

Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Sean Christopherson <seanjc@google.com>
Cc: Vishal Annapurve <vannapurve@google.com>
Cc: Ackerley Tng <ackerleytng@google.com>
Cc: Andrew Jones <andrew.jones@linux.dev>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Cc: Michael Roth <michael.roth@amd.com>
Tested-by: default avatarCarlos Bilbao <carlos.bilbao@amd.com>
Signed-off-by: default avatarMichael Roth <michael.roth@amd.com>
Signed-off-by: default avatarPeter Gonda <pgonda@google.com>
[sean: massage changelog]
Link: https://lore.kernel.org/r/20240223004258.3104051-3-seanjc@google.comSigned-off-by: default avatarSean Christopherson <seanjc@google.com>
parent 12619037
...@@ -30,26 +30,26 @@ typedef uint64_t sparsebit_num_t; ...@@ -30,26 +30,26 @@ typedef uint64_t sparsebit_num_t;
struct sparsebit *sparsebit_alloc(void); struct sparsebit *sparsebit_alloc(void);
void sparsebit_free(struct sparsebit **sbitp); void sparsebit_free(struct sparsebit **sbitp);
void sparsebit_copy(struct sparsebit *dstp, struct sparsebit *src); void sparsebit_copy(struct sparsebit *dstp, const struct sparsebit *src);
bool sparsebit_is_set(struct sparsebit *sbit, sparsebit_idx_t idx); bool sparsebit_is_set(const struct sparsebit *sbit, sparsebit_idx_t idx);
bool sparsebit_is_set_num(struct sparsebit *sbit, bool sparsebit_is_set_num(const struct sparsebit *sbit,
sparsebit_idx_t idx, sparsebit_num_t num); sparsebit_idx_t idx, sparsebit_num_t num);
bool sparsebit_is_clear(struct sparsebit *sbit, sparsebit_idx_t idx); bool sparsebit_is_clear(const struct sparsebit *sbit, sparsebit_idx_t idx);
bool sparsebit_is_clear_num(struct sparsebit *sbit, bool sparsebit_is_clear_num(const struct sparsebit *sbit,
sparsebit_idx_t idx, sparsebit_num_t num); sparsebit_idx_t idx, sparsebit_num_t num);
sparsebit_num_t sparsebit_num_set(struct sparsebit *sbit); sparsebit_num_t sparsebit_num_set(const struct sparsebit *sbit);
bool sparsebit_any_set(struct sparsebit *sbit); bool sparsebit_any_set(const struct sparsebit *sbit);
bool sparsebit_any_clear(struct sparsebit *sbit); bool sparsebit_any_clear(const struct sparsebit *sbit);
bool sparsebit_all_set(struct sparsebit *sbit); bool sparsebit_all_set(const struct sparsebit *sbit);
bool sparsebit_all_clear(struct sparsebit *sbit); bool sparsebit_all_clear(const struct sparsebit *sbit);
sparsebit_idx_t sparsebit_first_set(struct sparsebit *sbit); sparsebit_idx_t sparsebit_first_set(const struct sparsebit *sbit);
sparsebit_idx_t sparsebit_first_clear(struct sparsebit *sbit); sparsebit_idx_t sparsebit_first_clear(const struct sparsebit *sbit);
sparsebit_idx_t sparsebit_next_set(struct sparsebit *sbit, sparsebit_idx_t prev); sparsebit_idx_t sparsebit_next_set(const struct sparsebit *sbit, sparsebit_idx_t prev);
sparsebit_idx_t sparsebit_next_clear(struct sparsebit *sbit, sparsebit_idx_t prev); sparsebit_idx_t sparsebit_next_clear(const struct sparsebit *sbit, sparsebit_idx_t prev);
sparsebit_idx_t sparsebit_next_set_num(struct sparsebit *sbit, sparsebit_idx_t sparsebit_next_set_num(const struct sparsebit *sbit,
sparsebit_idx_t start, sparsebit_num_t num); sparsebit_idx_t start, sparsebit_num_t num);
sparsebit_idx_t sparsebit_next_clear_num(struct sparsebit *sbit, sparsebit_idx_t sparsebit_next_clear_num(const struct sparsebit *sbit,
sparsebit_idx_t start, sparsebit_num_t num); sparsebit_idx_t start, sparsebit_num_t num);
void sparsebit_set(struct sparsebit *sbitp, sparsebit_idx_t idx); void sparsebit_set(struct sparsebit *sbitp, sparsebit_idx_t idx);
...@@ -62,9 +62,9 @@ void sparsebit_clear_num(struct sparsebit *sbitp, ...@@ -62,9 +62,9 @@ void sparsebit_clear_num(struct sparsebit *sbitp,
sparsebit_idx_t start, sparsebit_num_t num); sparsebit_idx_t start, sparsebit_num_t num);
void sparsebit_clear_all(struct sparsebit *sbitp); void sparsebit_clear_all(struct sparsebit *sbitp);
void sparsebit_dump(FILE *stream, struct sparsebit *sbit, void sparsebit_dump(FILE *stream, const struct sparsebit *sbit,
unsigned int indent); unsigned int indent);
void sparsebit_validate_internal(struct sparsebit *sbit); void sparsebit_validate_internal(const struct sparsebit *sbit);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
...@@ -202,7 +202,7 @@ static sparsebit_num_t node_num_set(struct node *nodep) ...@@ -202,7 +202,7 @@ static sparsebit_num_t node_num_set(struct node *nodep)
/* Returns a pointer to the node that describes the /* Returns a pointer to the node that describes the
* lowest bit index. * lowest bit index.
*/ */
static struct node *node_first(struct sparsebit *s) static struct node *node_first(const struct sparsebit *s)
{ {
struct node *nodep; struct node *nodep;
...@@ -216,7 +216,7 @@ static struct node *node_first(struct sparsebit *s) ...@@ -216,7 +216,7 @@ static struct node *node_first(struct sparsebit *s)
* lowest bit index > the index of the node pointed to by np. * lowest bit index > the index of the node pointed to by np.
* Returns NULL if no node with a higher index exists. * Returns NULL if no node with a higher index exists.
*/ */
static struct node *node_next(struct sparsebit *s, struct node *np) static struct node *node_next(const struct sparsebit *s, struct node *np)
{ {
struct node *nodep = np; struct node *nodep = np;
...@@ -244,7 +244,7 @@ static struct node *node_next(struct sparsebit *s, struct node *np) ...@@ -244,7 +244,7 @@ static struct node *node_next(struct sparsebit *s, struct node *np)
* highest index < the index of the node pointed to by np. * highest index < the index of the node pointed to by np.
* Returns NULL if no node with a lower index exists. * Returns NULL if no node with a lower index exists.
*/ */
static struct node *node_prev(struct sparsebit *s, struct node *np) static struct node *node_prev(const struct sparsebit *s, struct node *np)
{ {
struct node *nodep = np; struct node *nodep = np;
...@@ -273,7 +273,7 @@ static struct node *node_prev(struct sparsebit *s, struct node *np) ...@@ -273,7 +273,7 @@ static struct node *node_prev(struct sparsebit *s, struct node *np)
* subtree and duplicates the bit settings to the newly allocated nodes. * subtree and duplicates the bit settings to the newly allocated nodes.
* Returns the newly allocated copy of subtree. * Returns the newly allocated copy of subtree.
*/ */
static struct node *node_copy_subtree(struct node *subtree) static struct node *node_copy_subtree(const struct node *subtree)
{ {
struct node *root; struct node *root;
...@@ -307,7 +307,7 @@ static struct node *node_copy_subtree(struct node *subtree) ...@@ -307,7 +307,7 @@ static struct node *node_copy_subtree(struct node *subtree)
* index is within the bits described by the mask bits or the number of * index is within the bits described by the mask bits or the number of
* contiguous bits set after the mask. Returns NULL if there is no such node. * contiguous bits set after the mask. Returns NULL if there is no such node.
*/ */
static struct node *node_find(struct sparsebit *s, sparsebit_idx_t idx) static struct node *node_find(const struct sparsebit *s, sparsebit_idx_t idx)
{ {
struct node *nodep; struct node *nodep;
...@@ -393,7 +393,7 @@ static struct node *node_add(struct sparsebit *s, sparsebit_idx_t idx) ...@@ -393,7 +393,7 @@ static struct node *node_add(struct sparsebit *s, sparsebit_idx_t idx)
} }
/* Returns whether all the bits in the sparsebit array are set. */ /* Returns whether all the bits in the sparsebit array are set. */
bool sparsebit_all_set(struct sparsebit *s) bool sparsebit_all_set(const struct sparsebit *s)
{ {
/* /*
* If any nodes there must be at least one bit set. Only case * If any nodes there must be at least one bit set. Only case
...@@ -775,7 +775,7 @@ static void node_reduce(struct sparsebit *s, struct node *nodep) ...@@ -775,7 +775,7 @@ static void node_reduce(struct sparsebit *s, struct node *nodep)
/* Returns whether the bit at the index given by idx, within the /* Returns whether the bit at the index given by idx, within the
* sparsebit array is set or not. * sparsebit array is set or not.
*/ */
bool sparsebit_is_set(struct sparsebit *s, sparsebit_idx_t idx) bool sparsebit_is_set(const struct sparsebit *s, sparsebit_idx_t idx)
{ {
struct node *nodep; struct node *nodep;
...@@ -921,7 +921,7 @@ static inline sparsebit_idx_t node_first_clear(struct node *nodep, int start) ...@@ -921,7 +921,7 @@ static inline sparsebit_idx_t node_first_clear(struct node *nodep, int start)
* used by test cases after they detect an unexpected condition, as a means * used by test cases after they detect an unexpected condition, as a means
* to capture diagnostic information. * to capture diagnostic information.
*/ */
static void sparsebit_dump_internal(FILE *stream, struct sparsebit *s, static void sparsebit_dump_internal(FILE *stream, const struct sparsebit *s,
unsigned int indent) unsigned int indent)
{ {
/* Dump the contents of s */ /* Dump the contents of s */
...@@ -969,7 +969,7 @@ void sparsebit_free(struct sparsebit **sbitp) ...@@ -969,7 +969,7 @@ void sparsebit_free(struct sparsebit **sbitp)
* sparsebit_alloc(). It can though already have bits set, which * sparsebit_alloc(). It can though already have bits set, which
* if different from src will be cleared. * if different from src will be cleared.
*/ */
void sparsebit_copy(struct sparsebit *d, struct sparsebit *s) void sparsebit_copy(struct sparsebit *d, const struct sparsebit *s)
{ {
/* First clear any bits already set in the destination */ /* First clear any bits already set in the destination */
sparsebit_clear_all(d); sparsebit_clear_all(d);
...@@ -981,7 +981,7 @@ void sparsebit_copy(struct sparsebit *d, struct sparsebit *s) ...@@ -981,7 +981,7 @@ void sparsebit_copy(struct sparsebit *d, struct sparsebit *s)
} }
/* Returns whether num consecutive bits starting at idx are all set. */ /* Returns whether num consecutive bits starting at idx are all set. */
bool sparsebit_is_set_num(struct sparsebit *s, bool sparsebit_is_set_num(const struct sparsebit *s,
sparsebit_idx_t idx, sparsebit_num_t num) sparsebit_idx_t idx, sparsebit_num_t num)
{ {
sparsebit_idx_t next_cleared; sparsebit_idx_t next_cleared;
...@@ -1005,14 +1005,14 @@ bool sparsebit_is_set_num(struct sparsebit *s, ...@@ -1005,14 +1005,14 @@ bool sparsebit_is_set_num(struct sparsebit *s,
} }
/* Returns whether the bit at the index given by idx. */ /* Returns whether the bit at the index given by idx. */
bool sparsebit_is_clear(struct sparsebit *s, bool sparsebit_is_clear(const struct sparsebit *s,
sparsebit_idx_t idx) sparsebit_idx_t idx)
{ {
return !sparsebit_is_set(s, idx); return !sparsebit_is_set(s, idx);
} }
/* Returns whether num consecutive bits starting at idx are all cleared. */ /* Returns whether num consecutive bits starting at idx are all cleared. */
bool sparsebit_is_clear_num(struct sparsebit *s, bool sparsebit_is_clear_num(const struct sparsebit *s,
sparsebit_idx_t idx, sparsebit_num_t num) sparsebit_idx_t idx, sparsebit_num_t num)
{ {
sparsebit_idx_t next_set; sparsebit_idx_t next_set;
...@@ -1041,13 +1041,13 @@ bool sparsebit_is_clear_num(struct sparsebit *s, ...@@ -1041,13 +1041,13 @@ bool sparsebit_is_clear_num(struct sparsebit *s,
* value. Use sparsebit_any_set(), instead of sparsebit_num_set() > 0, * value. Use sparsebit_any_set(), instead of sparsebit_num_set() > 0,
* to determine if the sparsebit array has any bits set. * to determine if the sparsebit array has any bits set.
*/ */
sparsebit_num_t sparsebit_num_set(struct sparsebit *s) sparsebit_num_t sparsebit_num_set(const struct sparsebit *s)
{ {
return s->num_set; return s->num_set;
} }
/* Returns whether any bit is set in the sparsebit array. */ /* Returns whether any bit is set in the sparsebit array. */
bool sparsebit_any_set(struct sparsebit *s) bool sparsebit_any_set(const struct sparsebit *s)
{ {
/* /*
* Nodes only describe set bits. If any nodes then there * Nodes only describe set bits. If any nodes then there
...@@ -1070,20 +1070,20 @@ bool sparsebit_any_set(struct sparsebit *s) ...@@ -1070,20 +1070,20 @@ bool sparsebit_any_set(struct sparsebit *s)
} }
/* Returns whether all the bits in the sparsebit array are cleared. */ /* Returns whether all the bits in the sparsebit array are cleared. */
bool sparsebit_all_clear(struct sparsebit *s) bool sparsebit_all_clear(const struct sparsebit *s)
{ {
return !sparsebit_any_set(s); return !sparsebit_any_set(s);
} }
/* Returns whether all the bits in the sparsebit array are set. */ /* Returns whether all the bits in the sparsebit array are set. */
bool sparsebit_any_clear(struct sparsebit *s) bool sparsebit_any_clear(const struct sparsebit *s)
{ {
return !sparsebit_all_set(s); return !sparsebit_all_set(s);
} }
/* Returns the index of the first set bit. Abort if no bits are set. /* Returns the index of the first set bit. Abort if no bits are set.
*/ */
sparsebit_idx_t sparsebit_first_set(struct sparsebit *s) sparsebit_idx_t sparsebit_first_set(const struct sparsebit *s)
{ {
struct node *nodep; struct node *nodep;
...@@ -1097,7 +1097,7 @@ sparsebit_idx_t sparsebit_first_set(struct sparsebit *s) ...@@ -1097,7 +1097,7 @@ sparsebit_idx_t sparsebit_first_set(struct sparsebit *s)
/* Returns the index of the first cleared bit. Abort if /* Returns the index of the first cleared bit. Abort if
* no bits are cleared. * no bits are cleared.
*/ */
sparsebit_idx_t sparsebit_first_clear(struct sparsebit *s) sparsebit_idx_t sparsebit_first_clear(const struct sparsebit *s)
{ {
struct node *nodep1, *nodep2; struct node *nodep1, *nodep2;
...@@ -1151,7 +1151,7 @@ sparsebit_idx_t sparsebit_first_clear(struct sparsebit *s) ...@@ -1151,7 +1151,7 @@ sparsebit_idx_t sparsebit_first_clear(struct sparsebit *s)
/* Returns index of next bit set within s after the index given by prev. /* Returns index of next bit set within s after the index given by prev.
* Returns 0 if there are no bits after prev that are set. * Returns 0 if there are no bits after prev that are set.
*/ */
sparsebit_idx_t sparsebit_next_set(struct sparsebit *s, sparsebit_idx_t sparsebit_next_set(const struct sparsebit *s,
sparsebit_idx_t prev) sparsebit_idx_t prev)
{ {
sparsebit_idx_t lowest_possible = prev + 1; sparsebit_idx_t lowest_possible = prev + 1;
...@@ -1244,7 +1244,7 @@ sparsebit_idx_t sparsebit_next_set(struct sparsebit *s, ...@@ -1244,7 +1244,7 @@ sparsebit_idx_t sparsebit_next_set(struct sparsebit *s,
/* Returns index of next bit cleared within s after the index given by prev. /* Returns index of next bit cleared within s after the index given by prev.
* Returns 0 if there are no bits after prev that are cleared. * Returns 0 if there are no bits after prev that are cleared.
*/ */
sparsebit_idx_t sparsebit_next_clear(struct sparsebit *s, sparsebit_idx_t sparsebit_next_clear(const struct sparsebit *s,
sparsebit_idx_t prev) sparsebit_idx_t prev)
{ {
sparsebit_idx_t lowest_possible = prev + 1; sparsebit_idx_t lowest_possible = prev + 1;
...@@ -1300,7 +1300,7 @@ sparsebit_idx_t sparsebit_next_clear(struct sparsebit *s, ...@@ -1300,7 +1300,7 @@ sparsebit_idx_t sparsebit_next_clear(struct sparsebit *s,
* and returns the index of the first sequence of num consecutively set * and returns the index of the first sequence of num consecutively set
* bits. Returns a value of 0 of no such sequence exists. * bits. Returns a value of 0 of no such sequence exists.
*/ */
sparsebit_idx_t sparsebit_next_set_num(struct sparsebit *s, sparsebit_idx_t sparsebit_next_set_num(const struct sparsebit *s,
sparsebit_idx_t start, sparsebit_num_t num) sparsebit_idx_t start, sparsebit_num_t num)
{ {
sparsebit_idx_t idx; sparsebit_idx_t idx;
...@@ -1335,7 +1335,7 @@ sparsebit_idx_t sparsebit_next_set_num(struct sparsebit *s, ...@@ -1335,7 +1335,7 @@ sparsebit_idx_t sparsebit_next_set_num(struct sparsebit *s,
* and returns the index of the first sequence of num consecutively cleared * and returns the index of the first sequence of num consecutively cleared
* bits. Returns a value of 0 of no such sequence exists. * bits. Returns a value of 0 of no such sequence exists.
*/ */
sparsebit_idx_t sparsebit_next_clear_num(struct sparsebit *s, sparsebit_idx_t sparsebit_next_clear_num(const struct sparsebit *s,
sparsebit_idx_t start, sparsebit_num_t num) sparsebit_idx_t start, sparsebit_num_t num)
{ {
sparsebit_idx_t idx; sparsebit_idx_t idx;
...@@ -1583,7 +1583,7 @@ static size_t display_range(FILE *stream, sparsebit_idx_t low, ...@@ -1583,7 +1583,7 @@ static size_t display_range(FILE *stream, sparsebit_idx_t low,
* contiguous bits. This is done because '-' is used to specify command-line * contiguous bits. This is done because '-' is used to specify command-line
* options, and sometimes ranges are specified as command-line arguments. * options, and sometimes ranges are specified as command-line arguments.
*/ */
void sparsebit_dump(FILE *stream, struct sparsebit *s, void sparsebit_dump(FILE *stream, const struct sparsebit *s,
unsigned int indent) unsigned int indent)
{ {
size_t current_line_len = 0; size_t current_line_len = 0;
...@@ -1681,7 +1681,7 @@ void sparsebit_dump(FILE *stream, struct sparsebit *s, ...@@ -1681,7 +1681,7 @@ void sparsebit_dump(FILE *stream, struct sparsebit *s,
* s. On error, diagnostic information is printed to stderr and * s. On error, diagnostic information is printed to stderr and
* abort is called. * abort is called.
*/ */
void sparsebit_validate_internal(struct sparsebit *s) void sparsebit_validate_internal(const struct sparsebit *s)
{ {
bool error_detected = false; bool error_detected = false;
struct node *nodep, *prev = NULL; struct node *nodep, *prev = NULL;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment