1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
/******************************************************************************
* util-xen.c
*
* Generic x86 (32-bit and 64-bit) instruction decoder and emulator hypervisor-
* only utility functions.
*/
#include "private.h"
#include <xen/nospec.h>
#include <xen/sched.h>
#include <asm/debugreg.h>
#include <asm/xstate.h>
#ifndef NDEBUG
void x86_emulate_free_state(struct x86_emulate_state *s)
{
check_state(s);
s->caller = NULL;
}
#endif
unsigned int x86_insn_opsize(const struct x86_emulate_state *s)
{
check_state(s);
return s->op_bytes << 3;
}
int x86_insn_modrm(const struct x86_emulate_state *s,
unsigned int *rm, unsigned int *reg)
{
check_state(s);
if ( unlikely(s->modrm_mod > 3) )
{
if ( rm )
*rm = ~0U;
if ( reg )
*reg = ~0U;
return -EINVAL;
}
if ( rm )
*rm = s->modrm_rm;
if ( reg )
*reg = s->modrm_reg;
return s->modrm_mod;
}
unsigned long x86_insn_operand_ea(const struct x86_emulate_state *s,
enum x86_segment *seg)
{
*seg = s->ea.type == OP_MEM ? s->ea.mem.seg : x86_seg_none;
check_state(s);
return s->ea.mem.off;
}
bool cf_check x86_insn_is_portio(const struct x86_emulate_state *s,
const struct x86_emulate_ctxt *ctxt)
{
switch ( ctxt->opcode )
{
case 0x6c ... 0x6f: /* INS / OUTS */
case 0xe4 ... 0xe7: /* IN / OUT imm8 */
case 0xec ... 0xef: /* IN / OUT %dx */
return true;
}
return false;
}
bool cf_check x86_insn_is_cr_access(const struct x86_emulate_state *s,
const struct x86_emulate_ctxt *ctxt)
{
switch ( ctxt->opcode )
{
unsigned int ext;
case X86EMUL_OPC(0x0f, 0x01):
if ( x86_insn_modrm(s, NULL, &ext) >= 0
&& (ext & 5) == 4 ) /* SMSW / LMSW */
return true;
break;
case X86EMUL_OPC(0x0f, 0x06): /* CLTS */
case X86EMUL_OPC(0x0f, 0x20): /* MOV from CRn */
case X86EMUL_OPC(0x0f, 0x22): /* MOV to CRn */
return true;
}
return false;
}
unsigned long x86_insn_immediate(const struct x86_emulate_state *s,
unsigned int nr)
{
check_state(s);
switch ( nr )
{
case 0:
return s->imm1;
case 1:
return s->imm2;
}
return 0;
}
int cf_check x86emul_read_xcr(unsigned int reg, uint64_t *val,
struct x86_emulate_ctxt *ctxt)
{
switch ( reg )
{
case 0:
*val = current->arch.xcr0;
return X86EMUL_OKAY;
case 1:
if ( current->domain->arch.cpuid->xstate.xgetbv1 )
break;
/* fall through */
default:
x86_emul_hw_exception(X86_EXC_GP, 0, ctxt);
return X86EMUL_EXCEPTION;
}
*val = xgetbv(reg);
return X86EMUL_OKAY;
}
/* Note: May be called with ctxt=NULL. */
int cf_check x86emul_write_xcr(unsigned int reg, uint64_t val,
struct x86_emulate_ctxt *ctxt)
{
switch ( reg )
{
case 0:
break;
default:
gp_fault:
if ( ctxt )
x86_emul_hw_exception(X86_EXC_GP, 0, ctxt);
return X86EMUL_EXCEPTION;
}
if ( unlikely(handle_xsetbv(reg, val) != 0) )
goto gp_fault;
return X86EMUL_OKAY;
}
#ifdef CONFIG_PV
/* Called with NULL ctxt in hypercall context. */
int cf_check x86emul_read_dr(unsigned int reg, unsigned long *val,
struct x86_emulate_ctxt *ctxt)
{
struct vcpu *curr = current;
/* HVM support requires a bit more plumbing before it will work. */
ASSERT(is_pv_vcpu(curr));
switch ( reg )
{
case 0 ... 3:
*val = array_access_nospec(curr->arch.dr, reg);
break;
case 4:
if ( curr->arch.pv.ctrlreg[4] & X86_CR4_DE )
goto ud_fault;
/* Fallthrough */
case 6:
*val = curr->arch.dr6;
break;
case 5:
if ( curr->arch.pv.ctrlreg[4] & X86_CR4_DE )
goto ud_fault;
/* Fallthrough */
case 7:
*val = curr->arch.dr7 | curr->arch.pv.dr7_emul;
break;
ud_fault:
default:
if ( ctxt )
x86_emul_hw_exception(X86_EXC_UD, X86_EVENT_NO_EC, ctxt);
return X86EMUL_EXCEPTION;
}
return X86EMUL_OKAY;
}
int cf_check x86emul_write_dr(unsigned int reg, unsigned long val,
struct x86_emulate_ctxt *ctxt)
{
struct vcpu *curr = current;
/* HVM support requires a bit more plumbing before it will work. */
ASSERT(is_pv_vcpu(curr));
switch ( set_debugreg(curr, reg, val) )
{
case 0:
return X86EMUL_OKAY;
case -ENODEV:
x86_emul_hw_exception(X86_EXC_UD, X86_EVENT_NO_EC, ctxt);
return X86EMUL_EXCEPTION;
default:
x86_emul_hw_exception(X86_EXC_GP, 0, ctxt);
return X86EMUL_EXCEPTION;
}
}
#endif /* CONFIG_PV */
int cf_check x86emul_cpuid(uint32_t leaf, uint32_t subleaf,
struct cpuid_leaf *res,
struct x86_emulate_ctxt *ctxt)
{
guest_cpuid(current, leaf, subleaf, res);
return X86EMUL_OKAY;
}
|