2
0

kvmvapic.c 23 KB

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