aboutsummaryrefslogtreecommitdiff
path: root/accel/mshv/msr.c
blob: e6e5baef507bf5586fdfe9fa7c5f857b7b65476e (plain)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
 * QEMU MSHV support
 *
 * Copyright Microsoft, Corp. 2025
 *
 * Authors: Magnus Kulke  <magnuskulke@microsoft.com>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "qemu/osdep.h"
#include "system/mshv.h"
#include "system/mshv_int.h"
#include "hw/hyperv/hvgdk_mini.h"
#include "linux/mshv.h"
#include "qemu/error-report.h"

static uint32_t supported_msrs[64] = {
    IA32_MSR_TSC,
    IA32_MSR_EFER,
    IA32_MSR_KERNEL_GS_BASE,
    IA32_MSR_APIC_BASE,
    IA32_MSR_PAT,
    IA32_MSR_SYSENTER_CS,
    IA32_MSR_SYSENTER_ESP,
    IA32_MSR_SYSENTER_EIP,
    IA32_MSR_STAR,
    IA32_MSR_LSTAR,
    IA32_MSR_CSTAR,
    IA32_MSR_SFMASK,
    IA32_MSR_MTRR_DEF_TYPE,
    IA32_MSR_MTRR_PHYSBASE0,
    IA32_MSR_MTRR_PHYSMASK0,
    IA32_MSR_MTRR_PHYSBASE1,
    IA32_MSR_MTRR_PHYSMASK1,
    IA32_MSR_MTRR_PHYSBASE2,
    IA32_MSR_MTRR_PHYSMASK2,
    IA32_MSR_MTRR_PHYSBASE3,
    IA32_MSR_MTRR_PHYSMASK3,
    IA32_MSR_MTRR_PHYSBASE4,
    IA32_MSR_MTRR_PHYSMASK4,
    IA32_MSR_MTRR_PHYSBASE5,
    IA32_MSR_MTRR_PHYSMASK5,
    IA32_MSR_MTRR_PHYSBASE6,
    IA32_MSR_MTRR_PHYSMASK6,
    IA32_MSR_MTRR_PHYSBASE7,
    IA32_MSR_MTRR_PHYSMASK7,
    IA32_MSR_MTRR_FIX64K_00000,
    IA32_MSR_MTRR_FIX16K_80000,
    IA32_MSR_MTRR_FIX16K_A0000,
    IA32_MSR_MTRR_FIX4K_C0000,
    IA32_MSR_MTRR_FIX4K_C8000,
    IA32_MSR_MTRR_FIX4K_D0000,
    IA32_MSR_MTRR_FIX4K_D8000,
    IA32_MSR_MTRR_FIX4K_E0000,
    IA32_MSR_MTRR_FIX4K_E8000,
    IA32_MSR_MTRR_FIX4K_F0000,
    IA32_MSR_MTRR_FIX4K_F8000,
    IA32_MSR_TSC_AUX,
    IA32_MSR_DEBUG_CTL,
    HV_X64_MSR_GUEST_OS_ID,
    HV_X64_MSR_SINT0,
    HV_X64_MSR_SINT1,
    HV_X64_MSR_SINT2,
    HV_X64_MSR_SINT3,
    HV_X64_MSR_SINT4,
    HV_X64_MSR_SINT5,
    HV_X64_MSR_SINT6,
    HV_X64_MSR_SINT7,
    HV_X64_MSR_SINT8,
    HV_X64_MSR_SINT9,
    HV_X64_MSR_SINT10,
    HV_X64_MSR_SINT11,
    HV_X64_MSR_SINT12,
    HV_X64_MSR_SINT13,
    HV_X64_MSR_SINT14,
    HV_X64_MSR_SINT15,
    HV_X64_MSR_SCONTROL,
    HV_X64_MSR_SIEFP,
    HV_X64_MSR_SIMP,
    HV_X64_MSR_REFERENCE_TSC,
    HV_X64_MSR_EOM,
};
static const size_t msr_count = ARRAY_SIZE(supported_msrs);

static int compare_msr_index(const void *a, const void *b)
{
    return *(uint32_t *)a - *(uint32_t *)b;
}

__attribute__((constructor))
static void init_sorted_msr_map(void)
{
    qsort(supported_msrs, msr_count, sizeof(uint32_t), compare_msr_index);
}

static int mshv_is_supported_msr(uint32_t msr)
{
    return bsearch(&msr, supported_msrs, msr_count, sizeof(uint32_t),
                   compare_msr_index) != NULL;
}

static int mshv_msr_to_hv_reg_name(uint32_t msr, uint32_t *hv_reg)
{
    switch (msr) {
    case IA32_MSR_TSC:
        *hv_reg = HV_X64_REGISTER_TSC;
        return 0;
    case IA32_MSR_EFER:
        *hv_reg = HV_X64_REGISTER_EFER;
        return 0;
    case IA32_MSR_KERNEL_GS_BASE:
        *hv_reg = HV_X64_REGISTER_KERNEL_GS_BASE;
        return 0;
    case IA32_MSR_APIC_BASE:
        *hv_reg = HV_X64_REGISTER_APIC_BASE;
        return 0;
    case IA32_MSR_PAT:
        *hv_reg = HV_X64_REGISTER_PAT;
        return 0;
    case IA32_MSR_SYSENTER_CS:
        *hv_reg = HV_X64_REGISTER_SYSENTER_CS;
        return 0;
    case IA32_MSR_SYSENTER_ESP:
        *hv_reg = HV_X64_REGISTER_SYSENTER_ESP;
        return 0;
    case IA32_MSR_SYSENTER_EIP:
        *hv_reg = HV_X64_REGISTER_SYSENTER_EIP;
        return 0;
    case IA32_MSR_STAR:
        *hv_reg = HV_X64_REGISTER_STAR;
        return 0;
    case IA32_MSR_LSTAR:
        *hv_reg = HV_X64_REGISTER_LSTAR;
        return 0;
    case IA32_MSR_CSTAR:
        *hv_reg = HV_X64_REGISTER_CSTAR;
        return 0;
    case IA32_MSR_SFMASK:
        *hv_reg = HV_X64_REGISTER_SFMASK;
        return 0;
    case IA32_MSR_MTRR_CAP:
        *hv_reg = HV_X64_REGISTER_MSR_MTRR_CAP;
        return 0;
    case IA32_MSR_MTRR_DEF_TYPE:
        *hv_reg = HV_X64_REGISTER_MSR_MTRR_DEF_TYPE;
        return 0;
    case IA32_MSR_MTRR_PHYSBASE0:
        *hv_reg = HV_X64_REGISTER_MSR_MTRR_PHYS_BASE0;
        return 0;
    case IA32_MSR_MTRR_PHYSMASK0:
        *hv_reg = HV_X64_REGISTER_MSR_MTRR_PHYS_MASK0;
        return 0;
    case IA32_MSR_MTRR_PHYSBASE1:
        *hv_reg = HV_X64_REGISTER_MSR_MTRR_PHYS_BASE1;
        return 0;
    case IA32_MSR_MTRR_PHYSMASK1:
        *hv_reg = HV_X64_REGISTER_MSR_MTRR_PHYS_MASK1;
        return 0;
    case IA32_MSR_MTRR_PHYSBASE2:
        *hv_reg = HV_X64_REGISTER_MSR_MTRR_PHYS_BASE2;
        return 0;
    case IA32_MSR_MTRR_PHYSMASK2:
        *hv_reg = HV_X64_REGISTER_MSR_MTRR_PHYS_MASK2;
        return 0;
    case IA32_MSR_MTRR_PHYSBASE3:
        *hv_reg = HV_X64_REGISTER_MSR_MTRR_PHYS_BASE3;
        return 0;
    case IA32_MSR_MTRR_PHYSMASK3:
        *hv_reg = HV_X64_REGISTER_MSR_MTRR_PHYS_MASK3;
        return 0;
    case IA32_MSR_MTRR_PHYSBASE4:
        *hv_reg = HV_X64_REGISTER_MSR_MTRR_PHYS_BASE4;
        return 0;
    case IA32_MSR_MTRR_PHYSMASK4:
        *hv_reg = HV_X64_REGISTER_MSR_MTRR_PHYS_MASK4;
        return 0;
    case IA32_MSR_MTRR_PHYSBASE5:
        *hv_reg = HV_X64_REGISTER_MSR_MTRR_PHYS_BASE5;
        return 0;
    case IA32_MSR_MTRR_PHYSMASK5:
        *hv_reg = HV_X64_REGISTER_MSR_MTRR_PHYS_MASK5;
        return 0;
    case IA32_MSR_MTRR_PHYSBASE6:
        *hv_reg = HV_X64_REGISTER_MSR_MTRR_PHYS_BASE6;
        return 0;
    case IA32_MSR_MTRR_PHYSMASK6:
        *hv_reg = HV_X64_REGISTER_MSR_MTRR_PHYS_MASK6;
        return 0;
    case IA32_MSR_MTRR_PHYSBASE7:
        *hv_reg = HV_X64_REGISTER_MSR_MTRR_PHYS_BASE7;
        return 0;
    case IA32_MSR_MTRR_PHYSMASK7:
        *hv_reg = HV_X64_REGISTER_MSR_MTRR_PHYS_MASK7;
        return 0;
    case IA32_MSR_MTRR_FIX64K_00000:
        *hv_reg = HV_X64_REGISTER_MSR_MTRR_FIX64K00000;
        return 0;
    case IA32_MSR_MTRR_FIX16K_80000:
        *hv_reg = HV_X64_REGISTER_MSR_MTRR_FIX16K80000;
        return 0;
    case IA32_MSR_MTRR_FIX16K_A0000:
        *hv_reg = HV_X64_REGISTER_MSR_MTRR_FIX16KA0000;
        return 0;
    case IA32_MSR_MTRR_FIX4K_C0000:
        *hv_reg = HV_X64_REGISTER_MSR_MTRR_FIX4KC0000;
        return 0;
    case IA32_MSR_MTRR_FIX4K_C8000:
        *hv_reg = HV_X64_REGISTER_MSR_MTRR_FIX4KC8000;
        return 0;
    case IA32_MSR_MTRR_FIX4K_D0000:
        *hv_reg = HV_X64_REGISTER_MSR_MTRR_FIX4KD0000;
        return 0;
    case IA32_MSR_MTRR_FIX4K_D8000:
        *hv_reg = HV_X64_REGISTER_MSR_MTRR_FIX4KD8000;
        return 0;
    case IA32_MSR_MTRR_FIX4K_E0000:
        *hv_reg = HV_X64_REGISTER_MSR_MTRR_FIX4KE0000;
        return 0;
    case IA32_MSR_MTRR_FIX4K_E8000:
        *hv_reg = HV_X64_REGISTER_MSR_MTRR_FIX4KE8000;
        return 0;
    case IA32_MSR_MTRR_FIX4K_F0000:
        *hv_reg = HV_X64_REGISTER_MSR_MTRR_FIX4KF0000;
        return 0;
    case IA32_MSR_MTRR_FIX4K_F8000:
        *hv_reg = HV_X64_REGISTER_MSR_MTRR_FIX4KF8000;
        return 0;
    case IA32_MSR_TSC_AUX:
        *hv_reg = HV_X64_REGISTER_TSC_AUX;
        return 0;
    case IA32_MSR_BNDCFGS:
        *hv_reg = HV_X64_REGISTER_BNDCFGS;
        return 0;
    case IA32_MSR_DEBUG_CTL:
        *hv_reg = HV_X64_REGISTER_DEBUG_CTL;
        return 0;
    case IA32_MSR_TSC_ADJUST:
        *hv_reg = HV_X64_REGISTER_TSC_ADJUST;
        return 0;
    case IA32_MSR_SPEC_CTRL:
        *hv_reg = HV_X64_REGISTER_SPEC_CTRL;
        return 0;
    case HV_X64_MSR_GUEST_OS_ID:
        *hv_reg = HV_REGISTER_GUEST_OS_ID;
        return 0;
    case HV_X64_MSR_SINT0:
        *hv_reg = HV_REGISTER_SINT0;
        return 0;
    case HV_X64_MSR_SINT1:
        *hv_reg = HV_REGISTER_SINT1;
        return 0;
    case HV_X64_MSR_SINT2:
        *hv_reg = HV_REGISTER_SINT2;
        return 0;
    case HV_X64_MSR_SINT3:
        *hv_reg = HV_REGISTER_SINT3;
        return 0;
    case HV_X64_MSR_SINT4:
        *hv_reg = HV_REGISTER_SINT4;
        return 0;
    case HV_X64_MSR_SINT5:
        *hv_reg = HV_REGISTER_SINT5;
        return 0;
    case HV_X64_MSR_SINT6:
        *hv_reg = HV_REGISTER_SINT6;
        return 0;
    case HV_X64_MSR_SINT7:
        *hv_reg = HV_REGISTER_SINT7;
        return 0;
    case HV_X64_MSR_SINT8:
        *hv_reg = HV_REGISTER_SINT8;
        return 0;
    case HV_X64_MSR_SINT9:
        *hv_reg = HV_REGISTER_SINT9;
        return 0;
    case HV_X64_MSR_SINT10:
        *hv_reg = HV_REGISTER_SINT10;
        return 0;
    case HV_X64_MSR_SINT11:
        *hv_reg = HV_REGISTER_SINT11;
        return 0;
    case HV_X64_MSR_SINT12:
        *hv_reg = HV_REGISTER_SINT12;
        return 0;
    case HV_X64_MSR_SINT13:
        *hv_reg = HV_REGISTER_SINT13;
        return 0;
    case HV_X64_MSR_SINT14:
        *hv_reg = HV_REGISTER_SINT14;
        return 0;
    case HV_X64_MSR_SINT15:
        *hv_reg = HV_REGISTER_SINT15;
        return 0;
    case IA32_MSR_MISC_ENABLE:
        *hv_reg = HV_X64_REGISTER_MSR_IA32_MISC_ENABLE;
        return 0;
    case HV_X64_MSR_SCONTROL:
        *hv_reg = HV_REGISTER_SCONTROL;
        return 0;
    case HV_X64_MSR_SIEFP:
        *hv_reg = HV_REGISTER_SIEFP;
        return 0;
    case HV_X64_MSR_SIMP:
        *hv_reg = HV_REGISTER_SIMP;
        return 0;
    case HV_X64_MSR_REFERENCE_TSC:
        *hv_reg = HV_REGISTER_REFERENCE_TSC;
        return 0;
    case HV_X64_MSR_EOM:
        *hv_reg = HV_REGISTER_EOM;
        return 0;
    default:
        error_report("failed to map MSR %u to HV register name", msr);
        return -1;
    }
}

static int set_msrs(const CPUState *cpu, GList *msrs)
{
    size_t n_msrs;
    GList *entries;
    MshvMsrEntry *entry;
    enum hv_register_name name;
    struct hv_register_assoc *assoc;
    int ret;
    size_t i = 0;

    n_msrs = g_list_length(msrs);
    hv_register_assoc *assocs = g_new0(hv_register_assoc, n_msrs);

    entries = msrs;
    for (const GList *elem = entries; elem != NULL; elem = elem->next) {
        entry = elem->data;
        ret = mshv_msr_to_hv_reg_name(entry->index, &name);
        if (ret < 0) {
            g_free(assocs);
            return ret;
        }
        assoc = &assocs[i];
        assoc->name = name;
        /* the union has been initialized to 0 */
        assoc->value.reg64 = entry->data;
        i++;
    }
    ret = mshv_set_generic_regs(cpu, assocs, n_msrs);
    g_free(assocs);
    if (ret < 0) {
        error_report("failed to set msrs");
        return -1;
    }
    return 0;
}


int mshv_configure_msr(const CPUState *cpu, const MshvMsrEntry *msrs,
                       size_t n_msrs)
{
    GList *valid_msrs = NULL;
    uint32_t msr_index;
    int ret;

    for (size_t i = 0; i < n_msrs; i++) {
        msr_index = msrs[i].index;
        /* check whether index of msrs is in SUPPORTED_MSRS */
        if (mshv_is_supported_msr(msr_index)) {
            valid_msrs = g_list_append(valid_msrs, (void *) &msrs[i]);
        }
    }

    ret = set_msrs(cpu, valid_msrs);
    g_list_free(valid_msrs);

    return ret;
}