]> Git Repo - qemu.git/blame - hw/i386/kvmvapic.c
Merge remote-tracking branch 'remotes/lalrae/tags/mips-20160513' into staging
[qemu.git] / hw / i386 / kvmvapic.c
CommitLineData
e5ad936b
JK
1/*
2 * TPR optimization for 32-bit Windows guests (XP and Server 2003)
3 *
4 * Copyright (C) 2007-2008 Qumranet Technologies
5 * Copyright (C) 2012 Jan Kiszka, Siemens AG
6 *
7 * This work is licensed under the terms of the GNU GPL version 2, or
8 * (at your option) any later version. See the COPYING file in the
9 * top-level directory.
10 */
b6a0aa05 11#include "qemu/osdep.h"
9c17d615
PB
12#include "sysemu/sysemu.h"
13#include "sysemu/cpus.h"
14#include "sysemu/kvm.h"
0d09e41a 15#include "hw/i386/apic_internal.h"
5f8df3ce 16#include "hw/sysbus.h"
e5ad936b 17
e5ad936b
JK
18#define VAPIC_IO_PORT 0x7e
19
20#define VAPIC_CPU_SHIFT 7
21
22#define ROM_BLOCK_SIZE 512
23#define ROM_BLOCK_MASK (~(ROM_BLOCK_SIZE - 1))
24
25typedef enum VAPICMode {
26 VAPIC_INACTIVE = 0,
27 VAPIC_ACTIVE = 1,
28 VAPIC_STANDBY = 2,
29} VAPICMode;
30
31typedef struct VAPICHandlers {
32 uint32_t set_tpr;
33 uint32_t set_tpr_eax;
34 uint32_t get_tpr[8];
35 uint32_t get_tpr_stack;
36} QEMU_PACKED VAPICHandlers;
37
38typedef struct GuestROMState {
39 char signature[8];
40 uint32_t vaddr;
41 uint32_t fixup_start;
42 uint32_t fixup_end;
43 uint32_t vapic_vaddr;
44 uint32_t vapic_size;
45 uint32_t vcpu_shift;
46 uint32_t real_tpr_addr;
47 VAPICHandlers up;
48 VAPICHandlers mp;
49} QEMU_PACKED GuestROMState;
50
51typedef struct VAPICROMState {
52 SysBusDevice busdev;
53 MemoryRegion io;
54 MemoryRegion rom;
55 uint32_t state;
56 uint32_t rom_state_paddr;
57 uint32_t rom_state_vaddr;
58 uint32_t vapic_paddr;
59 uint32_t real_tpr_addr;
60 GuestROMState rom_state;
61 size_t rom_size;
62 bool rom_mapped_writable;
a6dead43 63 VMChangeStateEntry *vmsentry;
e5ad936b
JK
64} VAPICROMState;
65
f1fc3e66
IM
66#define TYPE_VAPIC "kvmvapic"
67#define VAPIC(obj) OBJECT_CHECK(VAPICROMState, (obj), TYPE_VAPIC)
68
e5ad936b
JK
69#define TPR_INSTR_ABS_MODRM 0x1
70#define TPR_INSTR_MATCH_MODRM_REG 0x2
71
72typedef struct TPRInstruction {
73 uint8_t opcode;
74 uint8_t modrm_reg;
75 unsigned int flags;
76 TPRAccess access;
77 size_t length;
78 off_t addr_offset;
79} TPRInstruction;
80
81/* must be sorted by length, shortest first */
82static const TPRInstruction tpr_instr[] = {
83 { /* mov abs to eax */
84 .opcode = 0xa1,
85 .access = TPR_ACCESS_READ,
86 .length = 5,
87 .addr_offset = 1,
88 },
89 { /* mov eax to abs */
90 .opcode = 0xa3,
91 .access = TPR_ACCESS_WRITE,
92 .length = 5,
93 .addr_offset = 1,
94 },
95 { /* mov r32 to r/m32 */
96 .opcode = 0x89,
97 .flags = TPR_INSTR_ABS_MODRM,
98 .access = TPR_ACCESS_WRITE,
99 .length = 6,
100 .addr_offset = 2,
101 },
102 { /* mov r/m32 to r32 */
103 .opcode = 0x8b,
104 .flags = TPR_INSTR_ABS_MODRM,
105 .access = TPR_ACCESS_READ,
106 .length = 6,
107 .addr_offset = 2,
108 },
109 { /* push r/m32 */
110 .opcode = 0xff,
111 .modrm_reg = 6,
112 .flags = TPR_INSTR_ABS_MODRM | TPR_INSTR_MATCH_MODRM_REG,
113 .access = TPR_ACCESS_READ,
114 .length = 6,
115 .addr_offset = 2,
116 },
117 { /* mov imm32, r/m32 (c7/0) */
118 .opcode = 0xc7,
119 .modrm_reg = 0,
120 .flags = TPR_INSTR_ABS_MODRM | TPR_INSTR_MATCH_MODRM_REG,
121 .access = TPR_ACCESS_WRITE,
122 .length = 10,
123 .addr_offset = 2,
124 },
125};
126
127static void read_guest_rom_state(VAPICROMState *s)
128{
eb6282f2
SW
129 cpu_physical_memory_read(s->rom_state_paddr, &s->rom_state,
130 sizeof(GuestROMState));
e5ad936b
JK
131}
132
133static void write_guest_rom_state(VAPICROMState *s)
134{
eb6282f2
SW
135 cpu_physical_memory_write(s->rom_state_paddr, &s->rom_state,
136 sizeof(GuestROMState));
e5ad936b
JK
137}
138
139static void update_guest_rom_state(VAPICROMState *s)
140{
141 read_guest_rom_state(s);
142
143 s->rom_state.real_tpr_addr = cpu_to_le32(s->real_tpr_addr);
144 s->rom_state.vcpu_shift = cpu_to_le32(VAPIC_CPU_SHIFT);
145
146 write_guest_rom_state(s);
147}
148
4a8fa5dc 149static int find_real_tpr_addr(VAPICROMState *s, CPUX86State *env)
e5ad936b 150{
00b941e5 151 CPUState *cs = CPU(x86_env_get_cpu(env));
a8170e5e 152 hwaddr paddr;
e5ad936b
JK
153 target_ulong addr;
154
155 if (s->state == VAPIC_ACTIVE) {
156 return 0;
157 }
158 /*
159 * If there is no prior TPR access instruction we could analyze (which is
160 * the case after resume from hibernation), we need to scan the possible
161 * virtual address space for the APIC mapping.
162 */
163 for (addr = 0xfffff000; addr >= 0x80000000; addr -= TARGET_PAGE_SIZE) {
00b941e5 164 paddr = cpu_get_phys_page_debug(cs, addr);
e5ad936b
JK
165 if (paddr != APIC_DEFAULT_ADDRESS) {
166 continue;
167 }
168 s->real_tpr_addr = addr + 0x80;
169 update_guest_rom_state(s);
170 return 0;
171 }
172 return -1;
173}
174
175static uint8_t modrm_reg(uint8_t modrm)
176{
177 return (modrm >> 3) & 7;
178}
179
180static bool is_abs_modrm(uint8_t modrm)
181{
182 return (modrm & 0xc7) == 0x05;
183}
184
185static bool opcode_matches(uint8_t *opcode, const TPRInstruction *instr)
186{
187 return opcode[0] == instr->opcode &&
188 (!(instr->flags & TPR_INSTR_ABS_MODRM) || is_abs_modrm(opcode[1])) &&
189 (!(instr->flags & TPR_INSTR_MATCH_MODRM_REG) ||
190 modrm_reg(opcode[1]) == instr->modrm_reg);
191}
192
f17ec444 193static int evaluate_tpr_instruction(VAPICROMState *s, X86CPU *cpu,
e5ad936b
JK
194 target_ulong *pip, TPRAccess access)
195{
f17ec444 196 CPUState *cs = CPU(cpu);
e5ad936b
JK
197 const TPRInstruction *instr;
198 target_ulong ip = *pip;
199 uint8_t opcode[2];
200 uint32_t real_tpr_addr;
201 int i;
202
203 if ((ip & 0xf0000000ULL) != 0x80000000ULL &&
204 (ip & 0xf0000000ULL) != 0xe0000000ULL) {
205 return -1;
206 }
207
208 /*
209 * Early Windows 2003 SMP initialization contains a
210 *
211 * mov imm32, r/m32
212 *
213 * instruction that is patched by TPR optimization. The problem is that
214 * RSP, used by the patched instruction, is zero, so the guest gets a
215 * double fault and dies.
216 */
f17ec444 217 if (cpu->env.regs[R_ESP] == 0) {
e5ad936b
JK
218 return -1;
219 }
220
221 if (kvm_enabled() && !kvm_irqchip_in_kernel()) {
222 /*
223 * KVM without kernel-based TPR access reporting will pass an IP that
224 * points after the accessing instruction. So we need to look backward
225 * to find the reason.
226 */
227 for (i = 0; i < ARRAY_SIZE(tpr_instr); i++) {
228 instr = &tpr_instr[i];
229 if (instr->access != access) {
230 continue;
231 }
f17ec444 232 if (cpu_memory_rw_debug(cs, ip - instr->length, opcode,
e5ad936b
JK
233 sizeof(opcode), 0) < 0) {
234 return -1;
235 }
236 if (opcode_matches(opcode, instr)) {
237 ip -= instr->length;
238 goto instruction_ok;
239 }
240 }
241 return -1;
242 } else {
f17ec444 243 if (cpu_memory_rw_debug(cs, ip, opcode, sizeof(opcode), 0) < 0) {
e5ad936b
JK
244 return -1;
245 }
246 for (i = 0; i < ARRAY_SIZE(tpr_instr); i++) {
247 instr = &tpr_instr[i];
248 if (opcode_matches(opcode, instr)) {
249 goto instruction_ok;
250 }
251 }
252 return -1;
253 }
254
255instruction_ok:
256 /*
257 * Grab the virtual TPR address from the instruction
258 * and update the cached values.
259 */
f17ec444 260 if (cpu_memory_rw_debug(cs, ip + instr->addr_offset,
e5ad936b
JK
261 (void *)&real_tpr_addr,
262 sizeof(real_tpr_addr), 0) < 0) {
263 return -1;
264 }
265 real_tpr_addr = le32_to_cpu(real_tpr_addr);
266 if ((real_tpr_addr & 0xfff) != 0x80) {
267 return -1;
268 }
269 s->real_tpr_addr = real_tpr_addr;
270 update_guest_rom_state(s);
271
272 *pip = ip;
273 return 0;
274}
275
4a8fa5dc 276static int update_rom_mapping(VAPICROMState *s, CPUX86State *env, target_ulong ip)
e5ad936b 277{
00b941e5 278 CPUState *cs = CPU(x86_env_get_cpu(env));
a8170e5e 279 hwaddr paddr;
e5ad936b
JK
280 uint32_t rom_state_vaddr;
281 uint32_t pos, patch, offset;
282
283 /* nothing to do if already activated */
284 if (s->state == VAPIC_ACTIVE) {
285 return 0;
286 }
287
288 /* bail out if ROM init code was not executed (missing ROM?) */
289 if (s->state == VAPIC_INACTIVE) {
290 return -1;
291 }
292
293 /* find out virtual address of the ROM */
294 rom_state_vaddr = s->rom_state_paddr + (ip & 0xf0000000);
00b941e5 295 paddr = cpu_get_phys_page_debug(cs, rom_state_vaddr);
e5ad936b
JK
296 if (paddr == -1) {
297 return -1;
298 }
299 paddr += rom_state_vaddr & ~TARGET_PAGE_MASK;
300 if (paddr != s->rom_state_paddr) {
301 return -1;
302 }
303 read_guest_rom_state(s);
304 if (memcmp(s->rom_state.signature, "kvm aPiC", 8) != 0) {
305 return -1;
306 }
307 s->rom_state_vaddr = rom_state_vaddr;
308
309 /* fixup addresses in ROM if needed */
310 if (rom_state_vaddr == le32_to_cpu(s->rom_state.vaddr)) {
311 return 0;
312 }
313 for (pos = le32_to_cpu(s->rom_state.fixup_start);
314 pos < le32_to_cpu(s->rom_state.fixup_end);
315 pos += 4) {
eb6282f2
SW
316 cpu_physical_memory_read(paddr + pos - s->rom_state.vaddr,
317 &offset, sizeof(offset));
e5ad936b 318 offset = le32_to_cpu(offset);
eb6282f2 319 cpu_physical_memory_read(paddr + offset, &patch, sizeof(patch));
e5ad936b
JK
320 patch = le32_to_cpu(patch);
321 patch += rom_state_vaddr - le32_to_cpu(s->rom_state.vaddr);
322 patch = cpu_to_le32(patch);
eb6282f2 323 cpu_physical_memory_write(paddr + offset, &patch, sizeof(patch));
e5ad936b
JK
324 }
325 read_guest_rom_state(s);
326 s->vapic_paddr = paddr + le32_to_cpu(s->rom_state.vapic_vaddr) -
327 le32_to_cpu(s->rom_state.vaddr);
328
329 return 0;
330}
331
332/*
333 * Tries to read the unique processor number from the Kernel Processor Control
334 * Region (KPCR) of 32-bit Windows XP and Server 2003. Returns -1 if the KPCR
335 * cannot be accessed or is considered invalid. This also ensures that we are
336 * not patching the wrong guest.
337 */
f17ec444 338static int get_kpcr_number(X86CPU *cpu)
e5ad936b 339{
f17ec444 340 CPUX86State *env = &cpu->env;
e5ad936b
JK
341 struct kpcr {
342 uint8_t fill1[0x1c];
343 uint32_t self;
344 uint8_t fill2[0x31];
345 uint8_t number;
346 } QEMU_PACKED kpcr;
347
f17ec444 348 if (cpu_memory_rw_debug(CPU(cpu), env->segs[R_FS].base,
e5ad936b
JK
349 (void *)&kpcr, sizeof(kpcr), 0) < 0 ||
350 kpcr.self != env->segs[R_FS].base) {
351 return -1;
352 }
353 return kpcr.number;
354}
355
f17ec444 356static int vapic_enable(VAPICROMState *s, X86CPU *cpu)
e5ad936b 357{
f17ec444 358 int cpu_number = get_kpcr_number(cpu);
a8170e5e 359 hwaddr vapic_paddr;
e5ad936b
JK
360 static const uint8_t enabled = 1;
361
362 if (cpu_number < 0) {
363 return -1;
364 }
365 vapic_paddr = s->vapic_paddr +
a8170e5e 366 (((hwaddr)cpu_number) << VAPIC_CPU_SHIFT);
eb6282f2
SW
367 cpu_physical_memory_write(vapic_paddr + offsetof(VAPICState, enabled),
368 &enabled, sizeof(enabled));
02e51483 369 apic_enable_vapic(cpu->apic_state, vapic_paddr);
e5ad936b
JK
370
371 s->state = VAPIC_ACTIVE;
372
373 return 0;
374}
375
f17ec444 376static void patch_byte(X86CPU *cpu, target_ulong addr, uint8_t byte)
e5ad936b 377{
f17ec444 378 cpu_memory_rw_debug(CPU(cpu), addr, &byte, 1, 1);
e5ad936b
JK
379}
380
f17ec444 381static void patch_call(VAPICROMState *s, X86CPU *cpu, target_ulong ip,
e5ad936b
JK
382 uint32_t target)
383{
384 uint32_t offset;
385
386 offset = cpu_to_le32(target - ip - 5);
f17ec444
AF
387 patch_byte(cpu, ip, 0xe8); /* call near */
388 cpu_memory_rw_debug(CPU(cpu), ip + 1, (void *)&offset, sizeof(offset), 1);
e5ad936b
JK
389}
390
d77953b9 391static void patch_instruction(VAPICROMState *s, X86CPU *cpu, target_ulong ip)
e5ad936b 392{
d77953b9
AF
393 CPUState *cs = CPU(cpu);
394 CPUX86State *env = &cpu->env;
e5ad936b
JK
395 VAPICHandlers *handlers;
396 uint8_t opcode[2];
397 uint32_t imm32;
5c61afec
JK
398 target_ulong current_pc = 0;
399 target_ulong current_cs_base = 0;
89fee74a 400 uint32_t current_flags = 0;
e5ad936b
JK
401
402 if (smp_cpus == 1) {
403 handlers = &s->rom_state.up;
404 } else {
405 handlers = &s->rom_state.mp;
406 }
407
5c61afec 408 if (!kvm_enabled()) {
5c61afec
JK
409 cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
410 &current_flags);
411 }
412
e5ad936b
JK
413 pause_all_vcpus();
414
f17ec444 415 cpu_memory_rw_debug(cs, ip, opcode, sizeof(opcode), 0);
e5ad936b
JK
416
417 switch (opcode[0]) {
418 case 0x89: /* mov r32 to r/m32 */
f17ec444
AF
419 patch_byte(cpu, ip, 0x50 + modrm_reg(opcode[1])); /* push reg */
420 patch_call(s, cpu, ip + 1, handlers->set_tpr);
e5ad936b
JK
421 break;
422 case 0x8b: /* mov r/m32 to r32 */
f17ec444
AF
423 patch_byte(cpu, ip, 0x90);
424 patch_call(s, cpu, ip + 1, handlers->get_tpr[modrm_reg(opcode[1])]);
e5ad936b
JK
425 break;
426 case 0xa1: /* mov abs to eax */
f17ec444 427 patch_call(s, cpu, ip, handlers->get_tpr[0]);
e5ad936b
JK
428 break;
429 case 0xa3: /* mov eax to abs */
f17ec444 430 patch_call(s, cpu, ip, handlers->set_tpr_eax);
e5ad936b
JK
431 break;
432 case 0xc7: /* mov imm32, r/m32 (c7/0) */
f17ec444
AF
433 patch_byte(cpu, ip, 0x68); /* push imm32 */
434 cpu_memory_rw_debug(cs, ip + 6, (void *)&imm32, sizeof(imm32), 0);
435 cpu_memory_rw_debug(cs, ip + 1, (void *)&imm32, sizeof(imm32), 1);
436 patch_call(s, cpu, ip + 5, handlers->set_tpr);
e5ad936b
JK
437 break;
438 case 0xff: /* push r/m32 */
f17ec444
AF
439 patch_byte(cpu, ip, 0x50); /* push eax */
440 patch_call(s, cpu, ip + 1, handlers->get_tpr_stack);
e5ad936b
JK
441 break;
442 default:
443 abort();
444 }
445
446 resume_all_vcpus();
447
5c61afec 448 if (!kvm_enabled()) {
648f034c 449 tb_gen_code(cs, current_pc, current_cs_base, current_flags, 1);
0ea8cb88 450 cpu_resume_from_signal(cs, NULL);
5c61afec 451 }
e5ad936b
JK
452}
453
d77953b9 454void vapic_report_tpr_access(DeviceState *dev, CPUState *cs, target_ulong ip,
e5ad936b
JK
455 TPRAccess access)
456{
253eacc2 457 VAPICROMState *s = VAPIC(dev);
d77953b9
AF
458 X86CPU *cpu = X86_CPU(cs);
459 CPUX86State *env = &cpu->env;
e5ad936b 460
cb446eca 461 cpu_synchronize_state(cs);
e5ad936b 462
f17ec444 463 if (evaluate_tpr_instruction(s, cpu, &ip, access) < 0) {
e5ad936b 464 if (s->state == VAPIC_ACTIVE) {
f17ec444 465 vapic_enable(s, cpu);
e5ad936b
JK
466 }
467 return;
468 }
469 if (update_rom_mapping(s, env, ip) < 0) {
470 return;
471 }
f17ec444 472 if (vapic_enable(s, cpu) < 0) {
e5ad936b
JK
473 return;
474 }
d77953b9 475 patch_instruction(s, cpu, ip);
e5ad936b
JK
476}
477
478typedef struct VAPICEnableTPRReporting {
479 DeviceState *apic;
480 bool enable;
481} VAPICEnableTPRReporting;
482
483static void vapic_do_enable_tpr_reporting(void *data)
484{
485 VAPICEnableTPRReporting *info = data;
486
487 apic_enable_tpr_access_reporting(info->apic, info->enable);
488}
489
490static void vapic_enable_tpr_reporting(bool enable)
491{
492 VAPICEnableTPRReporting info = {
493 .enable = enable,
494 };
182735ef 495 CPUState *cs;
f100f0b3 496 X86CPU *cpu;
e5ad936b 497
bdc44640 498 CPU_FOREACH(cs) {
182735ef 499 cpu = X86_CPU(cs);
02e51483 500 info.apic = cpu->apic_state;
182735ef 501 run_on_cpu(cs, vapic_do_enable_tpr_reporting, &info);
e5ad936b
JK
502 }
503}
504
505static void vapic_reset(DeviceState *dev)
506{
253eacc2 507 VAPICROMState *s = VAPIC(dev);
e5ad936b 508
c056bc3f 509 s->state = VAPIC_INACTIVE;
4357930b 510 s->rom_state_paddr = 0;
e5ad936b
JK
511 vapic_enable_tpr_reporting(false);
512}
513
514/*
515 * Set the IRQ polling hypercalls to the supported variant:
516 * - vmcall if using KVM in-kernel irqchip
517 * - 32-bit VAPIC port write otherwise
518 */
519static int patch_hypercalls(VAPICROMState *s)
520{
a8170e5e 521 hwaddr rom_paddr = s->rom_state_paddr & ROM_BLOCK_MASK;
e5ad936b
JK
522 static const uint8_t vmcall_pattern[] = { /* vmcall */
523 0xb8, 0x1, 0, 0, 0, 0xf, 0x1, 0xc1
524 };
525 static const uint8_t outl_pattern[] = { /* nop; outl %eax,0x7e */
526 0xb8, 0x1, 0, 0, 0, 0x90, 0xe7, 0x7e
527 };
528 uint8_t alternates[2];
529 const uint8_t *pattern;
530 const uint8_t *patch;
531 int patches = 0;
532 off_t pos;
533 uint8_t *rom;
534
535 rom = g_malloc(s->rom_size);
eb6282f2 536 cpu_physical_memory_read(rom_paddr, rom, s->rom_size);
e5ad936b
JK
537
538 for (pos = 0; pos < s->rom_size - sizeof(vmcall_pattern); pos++) {
539 if (kvm_irqchip_in_kernel()) {
540 pattern = outl_pattern;
541 alternates[0] = outl_pattern[7];
542 alternates[1] = outl_pattern[7];
543 patch = &vmcall_pattern[5];
544 } else {
545 pattern = vmcall_pattern;
546 alternates[0] = vmcall_pattern[7];
547 alternates[1] = 0xd9; /* AMD's VMMCALL */
548 patch = &outl_pattern[5];
549 }
550 if (memcmp(rom + pos, pattern, 7) == 0 &&
551 (rom[pos + 7] == alternates[0] || rom[pos + 7] == alternates[1])) {
eb6282f2 552 cpu_physical_memory_write(rom_paddr + pos + 5, patch, 3);
e5ad936b
JK
553 /*
554 * Don't flush the tb here. Under ordinary conditions, the patched
555 * calls are miles away from the current IP. Under malicious
556 * conditions, the guest could trick us to crash.
557 */
558 }
559 }
560
561 g_free(rom);
562
563 if (patches != 0 && patches != 2) {
564 return -1;
565 }
566
567 return 0;
568}
569
570/*
571 * For TCG mode or the time KVM honors read-only memory regions, we need to
572 * enable write access to the option ROM so that variables can be updated by
573 * the guest.
574 */
18e5eec4 575static int vapic_map_rom_writable(VAPICROMState *s)
e5ad936b 576{
a8170e5e 577 hwaddr rom_paddr = s->rom_state_paddr & ROM_BLOCK_MASK;
e5ad936b
JK
578 MemoryRegionSection section;
579 MemoryRegion *as;
580 size_t rom_size;
581 uint8_t *ram;
582
583 as = sysbus_address_space(&s->busdev);
584
585 if (s->rom_mapped_writable) {
586 memory_region_del_subregion(as, &s->rom);
d8d95814 587 object_unparent(OBJECT(&s->rom));
e5ad936b
JK
588 }
589
590 /* grab RAM memory region (region @rom_paddr may still be pc.rom) */
591 section = memory_region_find(as, 0, 1);
592
593 /* read ROM size from RAM region */
7174e54c
JK
594 if (rom_paddr + 2 >= memory_region_size(section.mr)) {
595 return -1;
596 }
e5ad936b
JK
597 ram = memory_region_get_ram_ptr(section.mr);
598 rom_size = ram[rom_paddr + 2] * ROM_BLOCK_SIZE;
18e5eec4
JK
599 if (rom_size == 0) {
600 return -1;
601 }
e5ad936b
JK
602 s->rom_size = rom_size;
603
9512e4a9 604 /* We need to round to avoid creating subpages
e5ad936b 605 * from which we cannot run code. */
9512e4a9
AK
606 rom_size += rom_paddr & ~TARGET_PAGE_MASK;
607 rom_paddr &= TARGET_PAGE_MASK;
e5ad936b
JK
608 rom_size = TARGET_PAGE_ALIGN(rom_size);
609
1437c94b
PB
610 memory_region_init_alias(&s->rom, OBJECT(s), "kvmvapic-rom", section.mr,
611 rom_paddr, rom_size);
e5ad936b
JK
612 memory_region_add_subregion_overlap(as, rom_paddr, &s->rom, 1000);
613 s->rom_mapped_writable = true;
dfde4e6e 614 memory_region_unref(section.mr);
18e5eec4
JK
615
616 return 0;
e5ad936b
JK
617}
618
619static int vapic_prepare(VAPICROMState *s)
620{
18e5eec4
JK
621 if (vapic_map_rom_writable(s) < 0) {
622 return -1;
623 }
e5ad936b
JK
624
625 if (patch_hypercalls(s) < 0) {
626 return -1;
627 }
628
629 vapic_enable_tpr_reporting(true);
630
631 return 0;
632}
633
a8170e5e 634static void vapic_write(void *opaque, hwaddr addr, uint64_t data,
e5ad936b
JK
635 unsigned int size)
636{
e5ad936b 637 VAPICROMState *s = opaque;
4c1396cb
PP
638 X86CPU *cpu;
639 CPUX86State *env;
640 hwaddr rom_paddr;
e5ad936b 641
4c1396cb
PP
642 if (!current_cpu) {
643 return;
644 }
645
646 cpu_synchronize_state(current_cpu);
647 cpu = X86_CPU(current_cpu);
648 env = &cpu->env;
e5ad936b
JK
649
650 /*
651 * The VAPIC supports two PIO-based hypercalls, both via port 0x7E.
652 * o 16-bit write access:
653 * Reports the option ROM initialization to the hypervisor. Written
654 * value is the offset of the state structure in the ROM.
655 * o 8-bit write access:
656 * Reactivates the VAPIC after a guest hibernation, i.e. after the
657 * option ROM content has been re-initialized by a guest power cycle.
658 * o 32-bit write access:
659 * Poll for pending IRQs, considering the current VAPIC state.
660 */
661 switch (size) {
662 case 2:
663 if (s->state == VAPIC_INACTIVE) {
664 rom_paddr = (env->segs[R_CS].base + env->eip) & ROM_BLOCK_MASK;
665 s->rom_state_paddr = rom_paddr + data;
666
667 s->state = VAPIC_STANDBY;
668 }
669 if (vapic_prepare(s) < 0) {
670 s->state = VAPIC_INACTIVE;
4357930b 671 s->rom_state_paddr = 0;
e5ad936b
JK
672 break;
673 }
674 break;
675 case 1:
676 if (kvm_enabled()) {
677 /*
678 * Disable triggering instruction in ROM by writing a NOP.
679 *
680 * We cannot do this in TCG mode as the reported IP is not
681 * accurate.
682 */
683 pause_all_vcpus();
f17ec444
AF
684 patch_byte(cpu, env->eip - 2, 0x66);
685 patch_byte(cpu, env->eip - 1, 0x90);
e5ad936b
JK
686 resume_all_vcpus();
687 }
688
689 if (s->state == VAPIC_ACTIVE) {
690 break;
691 }
692 if (update_rom_mapping(s, env, env->eip) < 0) {
693 break;
694 }
695 if (find_real_tpr_addr(s, env) < 0) {
696 break;
697 }
f17ec444 698 vapic_enable(s, cpu);
e5ad936b
JK
699 break;
700 default:
701 case 4:
702 if (!kvm_irqchip_in_kernel()) {
02e51483 703 apic_poll_irq(cpu->apic_state);
e5ad936b
JK
704 }
705 break;
706 }
707}
708
0c1cd0ae
MT
709static uint64_t vapic_read(void *opaque, hwaddr addr, unsigned size)
710{
711 return 0xffffffff;
712}
713
e5ad936b
JK
714static const MemoryRegionOps vapic_ops = {
715 .write = vapic_write,
0c1cd0ae 716 .read = vapic_read,
e5ad936b
JK
717 .endianness = DEVICE_NATIVE_ENDIAN,
718};
719
c118d44b 720static void vapic_realize(DeviceState *dev, Error **errp)
e5ad936b 721{
c118d44b 722 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
f1fc3e66 723 VAPICROMState *s = VAPIC(dev);
e5ad936b 724
1437c94b 725 memory_region_init_io(&s->io, OBJECT(s), &vapic_ops, s, "kvmvapic", 2);
c118d44b
HT
726 sysbus_add_io(sbd, VAPIC_IO_PORT, &s->io);
727 sysbus_init_ioports(sbd, VAPIC_IO_PORT, 2);
e5ad936b
JK
728
729 option_rom[nb_option_roms].name = "kvmvapic.bin";
730 option_rom[nb_option_roms].bootindex = -1;
731 nb_option_roms++;
e5ad936b
JK
732}
733
734static void do_vapic_enable(void *data)
735{
736 VAPICROMState *s = data;
182735ef 737 X86CPU *cpu = X86_CPU(first_cpu);
e5ad936b 738
5a6e8ba6
PD
739 static const uint8_t enabled = 1;
740 cpu_physical_memory_write(s->vapic_paddr + offsetof(VAPICState, enabled),
741 &enabled, sizeof(enabled));
742 apic_enable_vapic(cpu->apic_state, s->vapic_paddr);
743 s->state = VAPIC_ACTIVE;
e5ad936b
JK
744}
745
a6dead43
PD
746static void kvmvapic_vm_state_change(void *opaque, int running,
747 RunState state)
e5ad936b
JK
748{
749 VAPICROMState *s = opaque;
750 uint8_t *zero;
751
a6dead43
PD
752 if (!running) {
753 return;
754 }
755
756 if (s->state == VAPIC_ACTIVE) {
757 if (smp_cpus == 1) {
758 run_on_cpu(first_cpu, do_vapic_enable, s);
759 } else {
760 zero = g_malloc0(s->rom_state.vapic_size);
761 cpu_physical_memory_write(s->vapic_paddr, zero,
762 s->rom_state.vapic_size);
763 g_free(zero);
764 }
765 }
766
767 qemu_del_vm_change_state_handler(s->vmsentry);
768}
769
770static int vapic_post_load(void *opaque, int version_id)
771{
772 VAPICROMState *s = opaque;
773
e5ad936b
JK
774 /*
775 * The old implementation of qemu-kvm did not provide the state
776 * VAPIC_STANDBY. Reconstruct it.
777 */
778 if (s->state == VAPIC_INACTIVE && s->rom_state_paddr != 0) {
779 s->state = VAPIC_STANDBY;
780 }
781
782 if (s->state != VAPIC_INACTIVE) {
783 if (vapic_prepare(s) < 0) {
784 return -1;
785 }
786 }
e5ad936b 787
5a6e8ba6
PD
788 if (!s->vmsentry) {
789 s->vmsentry =
790 qemu_add_vm_change_state_handler(kvmvapic_vm_state_change, s);
791 }
e5ad936b
JK
792 return 0;
793}
794
795static const VMStateDescription vmstate_handlers = {
796 .name = "kvmvapic-handlers",
797 .version_id = 1,
798 .minimum_version_id = 1,
e5ad936b
JK
799 .fields = (VMStateField[]) {
800 VMSTATE_UINT32(set_tpr, VAPICHandlers),
801 VMSTATE_UINT32(set_tpr_eax, VAPICHandlers),
802 VMSTATE_UINT32_ARRAY(get_tpr, VAPICHandlers, 8),
803 VMSTATE_UINT32(get_tpr_stack, VAPICHandlers),
804 VMSTATE_END_OF_LIST()
805 }
806};
807
808static const VMStateDescription vmstate_guest_rom = {
809 .name = "kvmvapic-guest-rom",
810 .version_id = 1,
811 .minimum_version_id = 1,
e5ad936b
JK
812 .fields = (VMStateField[]) {
813 VMSTATE_UNUSED(8), /* signature */
814 VMSTATE_UINT32(vaddr, GuestROMState),
815 VMSTATE_UINT32(fixup_start, GuestROMState),
816 VMSTATE_UINT32(fixup_end, GuestROMState),
817 VMSTATE_UINT32(vapic_vaddr, GuestROMState),
818 VMSTATE_UINT32(vapic_size, GuestROMState),
819 VMSTATE_UINT32(vcpu_shift, GuestROMState),
820 VMSTATE_UINT32(real_tpr_addr, GuestROMState),
821 VMSTATE_STRUCT(up, GuestROMState, 0, vmstate_handlers, VAPICHandlers),
822 VMSTATE_STRUCT(mp, GuestROMState, 0, vmstate_handlers, VAPICHandlers),
823 VMSTATE_END_OF_LIST()
824 }
825};
826
827static const VMStateDescription vmstate_vapic = {
828 .name = "kvm-tpr-opt", /* compatible with qemu-kvm VAPIC */
829 .version_id = 1,
830 .minimum_version_id = 1,
e5ad936b
JK
831 .post_load = vapic_post_load,
832 .fields = (VMStateField[]) {
833 VMSTATE_STRUCT(rom_state, VAPICROMState, 0, vmstate_guest_rom,
834 GuestROMState),
835 VMSTATE_UINT32(state, VAPICROMState),
836 VMSTATE_UINT32(real_tpr_addr, VAPICROMState),
837 VMSTATE_UINT32(rom_state_vaddr, VAPICROMState),
838 VMSTATE_UINT32(vapic_paddr, VAPICROMState),
839 VMSTATE_UINT32(rom_state_paddr, VAPICROMState),
840 VMSTATE_END_OF_LIST()
841 }
842};
843
844static void vapic_class_init(ObjectClass *klass, void *data)
845{
e5ad936b
JK
846 DeviceClass *dc = DEVICE_CLASS(klass);
847
e5ad936b
JK
848 dc->reset = vapic_reset;
849 dc->vmsd = &vmstate_vapic;
c118d44b 850 dc->realize = vapic_realize;
e5ad936b
JK
851}
852
8c43a6f0 853static const TypeInfo vapic_type = {
f1fc3e66 854 .name = TYPE_VAPIC,
e5ad936b
JK
855 .parent = TYPE_SYS_BUS_DEVICE,
856 .instance_size = sizeof(VAPICROMState),
857 .class_init = vapic_class_init,
858};
859
860static void vapic_register(void)
861{
862 type_register_static(&vapic_type);
863}
864
865type_init(vapic_register);
This page took 0.426174 seconds and 4 git commands to generate.