AArch64 EL2 hypervisor for QEMU virt that boots at EL2
1#ifndef HV_TYPES_H
2#define HV_TYPES_H
3
4#include <stdbool.h>
5#include <stddef.h>
6#include <stdint.h>
7
8#define HV_ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
9#define HV_BIT(n) (1ull << (n))
10#define HV_MASK(width) ((width) == 64u ? UINT64_MAX : ((1ull << (width)) - 1ull))
11#define HV_ALIGN_UP(x, a) (((x) + ((a) - 1ull)) & ~((a) - 1ull))
12#define HV_ALIGN_DOWN(x, a) ((x) & ~((a) - 1ull))
13
14typedef uint64_t paddr_t;
15typedef uint64_t vaddr_t;
16typedef uint64_t ipa_t;
17
18static inline bool hv_is_aligned_u64(uint64_t value, uint64_t alignment)
19{
20 return alignment != 0u && (value & (alignment - 1u)) == 0u;
21}
22
23static inline bool hv_add_overflow_u64(uint64_t a, uint64_t b, uint64_t *out)
24{
25 *out = a + b;
26 return *out < a;
27}
28
29static inline bool hv_range_overlaps(uint64_t a_base, uint64_t a_size,
30 uint64_t b_base, uint64_t b_size)
31{
32 uint64_t a_end;
33 uint64_t b_end;
34
35 if (a_size == 0u || b_size == 0u) {
36 return false;
37 }
38 if (hv_add_overflow_u64(a_base, a_size, &a_end) ||
39 hv_add_overflow_u64(b_base, b_size, &b_end)) {
40 return true;
41 }
42 return a_base < b_end && b_base < a_end;
43}
44
45static inline bool hv_range_contains(uint64_t base, uint64_t size, uint64_t addr, uint64_t len)
46{
47 uint64_t end;
48 uint64_t range_end;
49
50 if (len == 0u || size == 0u) {
51 return false;
52 }
53 if (hv_add_overflow_u64(addr, len, &end) ||
54 hv_add_overflow_u64(base, size, &range_end)) {
55 return false;
56 }
57 return addr >= base && end <= range_end;
58}
59
60#endif