1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Unit tests for PDX compression.
4  *
5  * Copyright (C) 2025 Cloud Software Group
6  */
7 
8 #ifndef _TEST_HARNESS_
9 #define _TEST_HARNESS_
10 
11 #include <assert.h>
12 #include <stdbool.h>
13 #include <stdint.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 
18 #include <xen-tools/common-macros.h>
19 
20 #define __init
21 #define __initdata
22 #define __ro_after_init
23 #define cf_check
24 
25 #define printk printf
26 #define XENLOG_INFO
27 #define XENLOG_DEBUG
28 #define XENLOG_WARNING
29 #define KERN_INFO
30 
31 #define BITS_PER_LONG (unsigned int)(sizeof(unsigned long) * 8)
32 
33 #define PAGE_SHIFT    12
34 /* Some libcs define PAGE_SIZE in limits.h. */
35 #undef  PAGE_SIZE
36 #define PAGE_SIZE     (1L << PAGE_SHIFT)
37 #define MAX_ORDER     18 /* 2 * PAGETABLE_ORDER (9) */
38 
39 #define PFN_DOWN(x)   ((x) >> PAGE_SHIFT)
40 #define PFN_UP(x)     (((x) + PAGE_SIZE-1) >> PAGE_SHIFT)
41 
42 #define pfn_to_paddr(pfn) ((paddr_t)(pfn) << PAGE_SHIFT)
43 #define paddr_to_pfn(pa)  ((unsigned long)((pa) >> PAGE_SHIFT))
44 
45 #define MAX_RANGES 16
46 #define MAX_PFN_RANGES MAX_RANGES
47 #define CONFIG_PDX_OFFSET_TBL_ORDER 6
48 
49 #define ASSERT assert
50 #define ASSERT_UNREACHABLE() assert(0)
51 
52 #define CONFIG_DEBUG
53 
find_next(const unsigned long * addr,unsigned int size,unsigned int off,bool value)54 static inline unsigned int find_next(
55     const unsigned long *addr, unsigned int size, unsigned int off, bool value)
56 {
57     unsigned int i;
58 
59     ASSERT(size <= BITS_PER_LONG);
60 
61     for ( i = off; i < size; i++ )
62         if ( !!(*addr & (1UL << i)) == value )
63             return i;
64 
65     return size;
66 }
67 
68 #define find_next_zero_bit(a, s, o) find_next(a, s, o, false)
69 #define find_next_bit(a, s, o)      find_next(a, s, o, true)
70 
71 #define flsl(x) ((x) ? BITS_PER_LONG - __builtin_clzl(x) : 0)
72 #define ffsl(x) __builtin_ffsl(x)
73 
74 #define boolean_param(name, func)
75 
76 typedef uint64_t paddr_t;
77 
78 #define SWAP(a, b) \
79    do { typeof(a) t_ = (a); (a) = (b); (b) = t_; } while ( 0 )
80 
81 #define sort(elem, nr, size, cmp, swp) ({                               \
82     /* Consume swp() so compiler doesn't complain it's unused. */       \
83     (void)(swp);                                                        \
84     qsort(elem, nr, size, cmp);                                         \
85 })
86 
87 #include "pdx.h"
88 
89 #endif
90 
91 /*
92  * Local variables:
93  * mode: C
94  * c-file-style: "BSD"
95  * c-basic-offset: 4
96  * indent-tabs-mode: nil
97  * End:
98  */
99