Coverage Report

Created: 2026-12-03 06:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xen/tools/tests/x86_emulator/x86-emulate.h
Line
Count
Source
1
#ifndef X86_EMULATE_H
2
#define X86_EMULATE_H
3
4
#include <assert.h>
5
#include <stdbool.h>
6
#include <stddef.h>
7
#include <stdint.h>
8
#include <stdlib.h>
9
/*
10
 * Use of sse registers must be disabled prior to the definition of
11
 * always_inline functions that would use them (memcpy, memset, etc),
12
 * so do this as early as possible, aiming to be before any always_inline
13
 * functions that are used are declared.
14
 * Unfortunately, this cannot be done prior to inclusion of <stdlib.h>
15
 * due to functions such as 'atof' that have SSE register return declared,
16
 * so do so here, immediately after that.
17
 */
18
#if __GNUC__ >= 6
19
# pragma GCC target("no-sse")
20
#endif
21
 /*
22
 * Attempt detection of unwanted prior inclusion of some headers known to use
23
 * always_inline with SSE registers in some library / compiler / optimization
24
 * combinations.
25
 */
26
#ifdef _STRING_H
27
# error "Must not include <string.h> before x86-emulate.h"
28
#endif
29
#include <string.h>
30
31
/* EOF is a standard macro defined in <stdio.h> so use it for detection */
32
#ifdef EOF
33
# error "Must not include <stdio.h> before x86-emulate.h"
34
#endif
35
#include <stdio.h>
36
37
#include <xen/xen.h>
38
39
#include <xen/asm/msr-index.h>
40
#include <xen/asm/x86-defns.h>
41
#include <xen/asm/x86-event.h>
42
#include <xen/asm/x86-types.h>
43
#include <xen/asm/x86-vendors.h>
44
45
#include <xen-tools/common-macros.h>
46
47
#define CONFIG_DEBUG
48
49
3.66M
#define ASSERT assert
50
0
#define ASSERT_UNREACHABLE() assert(!__LINE__)
51
52
#define DEFINE_PER_CPU(type, var) type per_cpu_##var
53
0
#define this_cpu(var) per_cpu_##var
54
55
#define __init
56
#define __maybe_unused __attribute__((__unused__))
57
58
105k
#define likely(x)   __builtin_expect(!!(x), true)
59
4.12M
#define unlikely(x) __builtin_expect(!!(x), false)
60
61
#define cf_check /* No Control Flow Integriy checking */
62
63
/*
64
 * Pseudo keyword 'fallthrough' to make explicit the fallthrough intention at
65
 * the end of a case statement block.
66
 */
67
#if !defined(__clang__) && (__GNUC__ >= 7)
68
# define fallthrough        __attribute__((__fallthrough__))
69
#else
70
5.21k
# define fallthrough        do {} while (0)  /* fallthrough */
71
#endif
72
73
#ifdef __GCC_ASM_FLAG_OUTPUTS__
74
# define ASM_FLAG_OUT(yes, no) yes
75
#else
76
# define ASM_FLAG_OUT(yes, no) no
77
#endif
78
79
#define hweight32 __builtin_popcount
80
0
#define hweight64 __builtin_popcountll
81
82
78
#define is_canonical_address(x) (((int64_t)(x) >> 47) == ((int64_t)(x) >> 63))
83
84
static inline void *place_ret(void *ptr)
85
194k
{
86
194k
    *(uint8_t *)ptr = 0xc3;
87
194k
    return ptr + 1;
88
194k
}
Unexecuted instantiation: fuzz-emul.c:place_ret
x86-emulate.c:place_ret
Line
Count
Source
85
131k
{
86
131k
    *(uint8_t *)ptr = 0xc3;
87
131k
    return ptr + 1;
88
131k
}
Unexecuted instantiation: 0f01.c:place_ret
Unexecuted instantiation: 0fae.c:place_ret
Unexecuted instantiation: 0fc7.c:place_ret
Unexecuted instantiation: decode.c:place_ret
fpu.c:place_ret
Line
Count
Source
85
62.7k
{
86
62.7k
    *(uint8_t *)ptr = 0xc3;
87
62.7k
    return ptr + 1;
88
62.7k
}
Unexecuted instantiation: wrappers.c:place_ret
89
90
extern uint32_t mxcsr_mask;
91
extern struct cpu_policy cpu_policy;
92
93
4
#define MMAP_SZ 16384
94
bool emul_test_init(void);
95
96
/* Must save and restore FPU state between any call into libc. */
97
void emul_save_fpu_state(void);
98
void emul_restore_fpu_state(void);
99
100
struct x86_fxsr *get_fpu_save_area(void);
101
102
/*
103
 * In order to reasonably use the above, wrap library calls we use and which we
104
 * think might access any of the FPU state into wrappers saving/restoring state
105
 * around the actual function.
106
 */
107
#ifndef WRAP
108
# define WRAP(x) typeof(x) __wrap_ ## x
109
#endif
110
111
WRAP(fwrite);
112
WRAP(memcmp);
113
WRAP(memcpy);
114
WRAP(memset);
115
WRAP(printf);
116
WRAP(putchar);
117
WRAP(puts);
118
WRAP(snprintf);
119
WRAP(strstr);
120
WRAP(vprintf);
121
WRAP(vsnprintf);
122
123
#undef WRAP
124
125
#include "x86_emulate/x86_emulate.h"
126
127
void evex_disp8_test(void *instr, struct x86_emulate_ctxt *ctxt,
128
                     const struct x86_emulate_ops *ops);
129
void predicates_test(void *instr, struct x86_emulate_ctxt *ctxt,
130
                     int (*fetch)(unsigned long offset,
131
                                  void *p_data,
132
                                  unsigned int bytes,
133
                                  struct x86_emulate_ctxt *ctxt));
134
135
static inline uint64_t xgetbv(uint32_t xcr)
136
54.4k
{
137
54.4k
    uint32_t lo, hi;
138
139
54.4k
    asm ( ".byte 0x0f, 0x01, 0xd0" : "=a" (lo), "=d" (hi) : "c" (xcr) );
140
141
54.4k
    return ((uint64_t)hi << 32) | lo;
142
54.4k
}
Unexecuted instantiation: fuzz-emul.c:xgetbv
x86-emulate.c:xgetbv
Line
Count
Source
136
54.4k
{
137
54.4k
    uint32_t lo, hi;
138
139
54.4k
    asm ( ".byte 0x0f, 0x01, 0xd0" : "=a" (lo), "=d" (hi) : "c" (xcr) );
140
141
54.4k
    return ((uint64_t)hi << 32) | lo;
142
54.4k
}
Unexecuted instantiation: 0f01.c:xgetbv
Unexecuted instantiation: 0fae.c:xgetbv
Unexecuted instantiation: 0fc7.c:xgetbv
Unexecuted instantiation: decode.c:xgetbv
Unexecuted instantiation: fpu.c:xgetbv
Unexecuted instantiation: wrappers.c:xgetbv
143
144
/* Intentionally checking OSXSAVE here. */
145
146k
#define cpu_has_xsave     (cpu_policy.basic.raw[1].c & (1u << 27))
146
147
static inline bool xcr0_mask(uint64_t mask)
148
54.4k
{
149
54.4k
    return cpu_has_xsave && ((xgetbv(0) & mask) == mask);
150
54.4k
}
Unexecuted instantiation: fuzz-emul.c:xcr0_mask
x86-emulate.c:xcr0_mask
Line
Count
Source
148
54.4k
{
149
54.4k
    return cpu_has_xsave && ((xgetbv(0) & mask) == mask);
150
54.4k
}
Unexecuted instantiation: 0f01.c:xcr0_mask
Unexecuted instantiation: 0fae.c:xcr0_mask
Unexecuted instantiation: 0fc7.c:xcr0_mask
Unexecuted instantiation: decode.c:xcr0_mask
Unexecuted instantiation: fpu.c:xcr0_mask
Unexecuted instantiation: wrappers.c:xcr0_mask
151
152
unsigned int rdpkru(void);
153
void wrpkru(unsigned int val);
154
155
#define cache_line_size()           (cpu_policy.basic.clflush_size * 8)
156
#define cpu_has_fpu                  cpu_policy.basic.fpu
157
20.3k
#define cpu_has_mmx                  cpu_policy.basic.mmx
158
#define cpu_has_fxsr                 cpu_policy.basic.fxsr
159
54.5k
#define cpu_has_sse                  cpu_policy.basic.sse
160
#define cpu_has_sse2                 cpu_policy.basic.sse2
161
#define cpu_has_sse3                 cpu_policy.basic.sse3
162
#define cpu_has_pclmulqdq            cpu_policy.basic.pclmulqdq
163
#define cpu_has_ssse3                cpu_policy.basic.ssse3
164
#define cpu_has_fma                 (cpu_policy.basic.fma && xcr0_mask(6))
165
#define cpu_has_sse4_1               cpu_policy.basic.sse4_1
166
#define cpu_has_sse4_2               cpu_policy.basic.sse4_2
167
#define cpu_has_popcnt               cpu_policy.basic.popcnt
168
#define cpu_has_aesni                cpu_policy.basic.aesni
169
53.6k
#define cpu_has_avx                 (cpu_policy.basic.avx  && xcr0_mask(6))
170
#define cpu_has_f16c                (cpu_policy.basic.f16c && xcr0_mask(6))
171
172
#define cpu_has_avx2                (cpu_policy.feat.avx2 && xcr0_mask(6))
173
#define cpu_has_bmi1                 cpu_policy.feat.bmi1
174
#define cpu_has_bmi2                 cpu_policy.feat.bmi2
175
0
#define cpu_has_avx512f             (cpu_policy.feat.avx512f && \
176
0
                                     xcr0_mask(0xe6))
177
#define cpu_has_avx512dq            (cpu_policy.feat.avx512dq && \
178
                                     xcr0_mask(0xe6))
179
#define cpu_has_avx512_ifma         (cpu_policy.feat.avx512_ifma && \
180
                                     xcr0_mask(0xe6))
181
#define cpu_has_avx512cd            (cpu_policy.feat.avx512cd && \
182
                                     xcr0_mask(0xe6))
183
#define cpu_has_sha                  cpu_policy.feat.sha
184
0
#define cpu_has_avx512bw            (cpu_policy.feat.avx512bw && \
185
0
                                     xcr0_mask(0xe6))
186
#define cpu_has_avx512vl            (cpu_policy.feat.avx512vl && \
187
                                     xcr0_mask(0xe6))
188
#define cpu_has_avx512_vbmi         (cpu_policy.feat.avx512_vbmi && \
189
                                     xcr0_mask(0xe6))
190
#define cpu_has_avx512_vbmi2        (cpu_policy.feat.avx512_vbmi2 && \
191
                                     xcr0_mask(0xe6))
192
#define cpu_has_gfni                 cpu_policy.feat.gfni
193
#define cpu_has_vaes                (cpu_policy.feat.vaes && xcr0_mask(6))
194
#define cpu_has_vpclmulqdq          (cpu_policy.feat.vpclmulqdq && xcr0_mask(6))
195
#define cpu_has_avx512_vnni         (cpu_policy.feat.avx512_vnni && \
196
                                     xcr0_mask(0xe6))
197
#define cpu_has_avx512_bitalg       (cpu_policy.feat.avx512_bitalg && \
198
                                     xcr0_mask(0xe6))
199
#define cpu_has_avx512_vpopcntdq    (cpu_policy.feat.avx512_vpopcntdq && \
200
                                     xcr0_mask(0xe6))
201
#define cpu_has_movdiri              cpu_policy.feat.movdiri
202
#define cpu_has_movdir64b            cpu_policy.feat.movdir64b
203
#define cpu_has_avx512_vp2intersect (cpu_policy.feat.avx512_vp2intersect && \
204
                                     xcr0_mask(0xe6))
205
#define cpu_has_serialize            cpu_policy.feat.serialize
206
#define cpu_has_avx512_fp16         (cpu_policy.feat.avx512_fp16 && \
207
                                     xcr0_mask(0xe6))
208
#define cpu_has_sha512              (cpu_policy.feat.sha512 && xcr0_mask(6))
209
#define cpu_has_sm3                 (cpu_policy.feat.sm3 && xcr0_mask(6))
210
#define cpu_has_sm4                 (cpu_policy.feat.sm4 && xcr0_mask(6))
211
#define cpu_has_avx_vnni            (cpu_policy.feat.avx_vnni && xcr0_mask(6))
212
#define cpu_has_avx512_bf16         (cpu_policy.feat.avx512_bf16 && \
213
                                     xcr0_mask(0xe6))
214
#define cpu_has_cmpccxadd            cpu_policy.feat.cmpccxadd
215
#define cpu_has_avx_ifma            (cpu_policy.feat.avx_ifma && xcr0_mask(6))
216
#define cpu_has_avx_vnni_int8       (cpu_policy.feat.avx_vnni_int8 && \
217
                                     xcr0_mask(6))
218
#define cpu_has_avx_ne_convert      (cpu_policy.feat.avx_ne_convert && \
219
                                     xcr0_mask(6))
220
#define cpu_has_avx_vnni_int16      (cpu_policy.feat.avx_vnni_int16 && \
221
                                     xcr0_mask(6))
222
223
1
#define cpu_has_xgetbv1             (cpu_has_xsave && cpu_policy.xstate.xgetbv1)
224
225
#define cpu_has_3dnow_ext            cpu_policy.extd._3dnowext
226
#define cpu_has_sse4a                cpu_policy.extd.sse4a
227
#define cpu_has_xop                 (cpu_policy.extd.xop  && xcr0_mask(6))
228
#define cpu_has_fma4                (cpu_policy.extd.fma4 && xcr0_mask(6))
229
#define cpu_has_tbm                  cpu_policy.extd.tbm
230
#define cpu_has_avx512_bmm          (cpu_policy.extd.avx512_bmm && \
231
                                     xcr0_mask(0xe6))
232
233
int emul_test_cpuid(
234
    uint32_t leaf,
235
    uint32_t subleaf,
236
    struct cpuid_leaf *res,
237
    struct x86_emulate_ctxt *ctxt);
238
239
int emul_test_read_cr(
240
    unsigned int reg,
241
    unsigned long *val,
242
    struct x86_emulate_ctxt *ctxt);
243
244
int emul_test_read_xcr(
245
    unsigned int reg,
246
    uint64_t *val,
247
    struct x86_emulate_ctxt *ctxt);
248
249
int emul_test_get_fpu(
250
    enum x86_emulate_fpu_type type,
251
    struct x86_emulate_ctxt *ctxt);
252
253
void emul_test_put_fpu(
254
    struct x86_emulate_ctxt *ctxt,
255
    enum x86_emulate_fpu_type backout,
256
    const struct x86_emul_fpu_aux *aux);
257
258
#endif /* X86_EMULATE_H */