kvmvapic.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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.h"
  12. #include "cpus.h"
  13. #include "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. target_phys_addr_t 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. target_phys_addr_t 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. target_phys_addr_t 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. (((target_phys_addr_t)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. target_phys_addr_t paddr;
  345. VAPICHandlers *handlers;
  346. uint8_t opcode[2];
  347. uint32_t imm32;
  348. if (smp_cpus == 1) {
  349. handlers = &s->rom_state.up;
  350. } else {
  351. handlers = &s->rom_state.mp;
  352. }
  353. pause_all_vcpus();
  354. cpu_memory_rw_debug(env, ip, opcode, sizeof(opcode), 0);
  355. switch (opcode[0]) {
  356. case 0x89: /* mov r32 to r/m32 */
  357. patch_byte(env, ip, 0x50 + modrm_reg(opcode[1])); /* push reg */
  358. patch_call(s, env, ip + 1, handlers->set_tpr);
  359. break;
  360. case 0x8b: /* mov r/m32 to r32 */
  361. patch_byte(env, ip, 0x90);
  362. patch_call(s, env, ip + 1, handlers->get_tpr[modrm_reg(opcode[1])]);
  363. break;
  364. case 0xa1: /* mov abs to eax */
  365. patch_call(s, env, ip, handlers->get_tpr[0]);
  366. break;
  367. case 0xa3: /* mov eax to abs */
  368. patch_call(s, env, ip, handlers->set_tpr_eax);
  369. break;
  370. case 0xc7: /* mov imm32, r/m32 (c7/0) */
  371. patch_byte(env, ip, 0x68); /* push imm32 */
  372. cpu_memory_rw_debug(env, ip + 6, (void *)&imm32, sizeof(imm32), 0);
  373. cpu_memory_rw_debug(env, ip + 1, (void *)&imm32, sizeof(imm32), 1);
  374. patch_call(s, env, ip + 5, handlers->set_tpr);
  375. break;
  376. case 0xff: /* push r/m32 */
  377. patch_byte(env, ip, 0x50); /* push eax */
  378. patch_call(s, env, ip + 1, handlers->get_tpr_stack);
  379. break;
  380. default:
  381. abort();
  382. }
  383. resume_all_vcpus();
  384. paddr = cpu_get_phys_page_debug(env, ip);
  385. paddr += ip & ~TARGET_PAGE_MASK;
  386. tb_invalidate_phys_page_range(paddr, paddr + 1, 1);
  387. }
  388. void vapic_report_tpr_access(DeviceState *dev, void *cpu, target_ulong ip,
  389. TPRAccess access)
  390. {
  391. VAPICROMState *s = DO_UPCAST(VAPICROMState, busdev.qdev, dev);
  392. CPUX86State *env = cpu;
  393. cpu_synchronize_state(env);
  394. if (evaluate_tpr_instruction(s, env, &ip, access) < 0) {
  395. if (s->state == VAPIC_ACTIVE) {
  396. vapic_enable(s, env);
  397. }
  398. return;
  399. }
  400. if (update_rom_mapping(s, env, ip) < 0) {
  401. return;
  402. }
  403. if (vapic_enable(s, env) < 0) {
  404. return;
  405. }
  406. patch_instruction(s, env, ip);
  407. }
  408. typedef struct VAPICEnableTPRReporting {
  409. DeviceState *apic;
  410. bool enable;
  411. } VAPICEnableTPRReporting;
  412. static void vapic_do_enable_tpr_reporting(void *data)
  413. {
  414. VAPICEnableTPRReporting *info = data;
  415. apic_enable_tpr_access_reporting(info->apic, info->enable);
  416. }
  417. static void vapic_enable_tpr_reporting(bool enable)
  418. {
  419. VAPICEnableTPRReporting info = {
  420. .enable = enable,
  421. };
  422. CPUX86State *env;
  423. for (env = first_cpu; env != NULL; env = env->next_cpu) {
  424. info.apic = env->apic_state;
  425. run_on_cpu(env, vapic_do_enable_tpr_reporting, &info);
  426. }
  427. }
  428. static void vapic_reset(DeviceState *dev)
  429. {
  430. VAPICROMState *s = DO_UPCAST(VAPICROMState, busdev.qdev, dev);
  431. if (s->state == VAPIC_ACTIVE) {
  432. s->state = VAPIC_STANDBY;
  433. }
  434. vapic_enable_tpr_reporting(false);
  435. }
  436. /*
  437. * Set the IRQ polling hypercalls to the supported variant:
  438. * - vmcall if using KVM in-kernel irqchip
  439. * - 32-bit VAPIC port write otherwise
  440. */
  441. static int patch_hypercalls(VAPICROMState *s)
  442. {
  443. target_phys_addr_t rom_paddr = s->rom_state_paddr & ROM_BLOCK_MASK;
  444. static const uint8_t vmcall_pattern[] = { /* vmcall */
  445. 0xb8, 0x1, 0, 0, 0, 0xf, 0x1, 0xc1
  446. };
  447. static const uint8_t outl_pattern[] = { /* nop; outl %eax,0x7e */
  448. 0xb8, 0x1, 0, 0, 0, 0x90, 0xe7, 0x7e
  449. };
  450. uint8_t alternates[2];
  451. const uint8_t *pattern;
  452. const uint8_t *patch;
  453. int patches = 0;
  454. off_t pos;
  455. uint8_t *rom;
  456. rom = g_malloc(s->rom_size);
  457. cpu_physical_memory_rw(rom_paddr, rom, s->rom_size, 0);
  458. for (pos = 0; pos < s->rom_size - sizeof(vmcall_pattern); pos++) {
  459. if (kvm_irqchip_in_kernel()) {
  460. pattern = outl_pattern;
  461. alternates[0] = outl_pattern[7];
  462. alternates[1] = outl_pattern[7];
  463. patch = &vmcall_pattern[5];
  464. } else {
  465. pattern = vmcall_pattern;
  466. alternates[0] = vmcall_pattern[7];
  467. alternates[1] = 0xd9; /* AMD's VMMCALL */
  468. patch = &outl_pattern[5];
  469. }
  470. if (memcmp(rom + pos, pattern, 7) == 0 &&
  471. (rom[pos + 7] == alternates[0] || rom[pos + 7] == alternates[1])) {
  472. cpu_physical_memory_rw(rom_paddr + pos + 5, (uint8_t *)patch,
  473. 3, 1);
  474. /*
  475. * Don't flush the tb here. Under ordinary conditions, the patched
  476. * calls are miles away from the current IP. Under malicious
  477. * conditions, the guest could trick us to crash.
  478. */
  479. }
  480. }
  481. g_free(rom);
  482. if (patches != 0 && patches != 2) {
  483. return -1;
  484. }
  485. return 0;
  486. }
  487. /*
  488. * For TCG mode or the time KVM honors read-only memory regions, we need to
  489. * enable write access to the option ROM so that variables can be updated by
  490. * the guest.
  491. */
  492. static void vapic_map_rom_writable(VAPICROMState *s)
  493. {
  494. target_phys_addr_t rom_paddr = s->rom_state_paddr & ROM_BLOCK_MASK;
  495. MemoryRegionSection section;
  496. MemoryRegion *as;
  497. size_t rom_size;
  498. uint8_t *ram;
  499. as = sysbus_address_space(&s->busdev);
  500. if (s->rom_mapped_writable) {
  501. memory_region_del_subregion(as, &s->rom);
  502. memory_region_destroy(&s->rom);
  503. }
  504. /* grab RAM memory region (region @rom_paddr may still be pc.rom) */
  505. section = memory_region_find(as, 0, 1);
  506. /* read ROM size from RAM region */
  507. ram = memory_region_get_ram_ptr(section.mr);
  508. rom_size = ram[rom_paddr + 2] * ROM_BLOCK_SIZE;
  509. s->rom_size = rom_size;
  510. /* We need to round to avoid creating subpages
  511. * from which we cannot run code. */
  512. rom_size += rom_paddr & ~TARGET_PAGE_MASK;
  513. rom_paddr &= TARGET_PAGE_MASK;
  514. rom_size = TARGET_PAGE_ALIGN(rom_size);
  515. memory_region_init_alias(&s->rom, "kvmvapic-rom", section.mr, rom_paddr,
  516. rom_size);
  517. memory_region_add_subregion_overlap(as, rom_paddr, &s->rom, 1000);
  518. s->rom_mapped_writable = true;
  519. }
  520. static int vapic_prepare(VAPICROMState *s)
  521. {
  522. vapic_map_rom_writable(s);
  523. if (patch_hypercalls(s) < 0) {
  524. return -1;
  525. }
  526. vapic_enable_tpr_reporting(true);
  527. return 0;
  528. }
  529. static void vapic_write(void *opaque, target_phys_addr_t addr, uint64_t data,
  530. unsigned int size)
  531. {
  532. CPUX86State *env = cpu_single_env;
  533. target_phys_addr_t rom_paddr;
  534. VAPICROMState *s = opaque;
  535. cpu_synchronize_state(env);
  536. /*
  537. * The VAPIC supports two PIO-based hypercalls, both via port 0x7E.
  538. * o 16-bit write access:
  539. * Reports the option ROM initialization to the hypervisor. Written
  540. * value is the offset of the state structure in the ROM.
  541. * o 8-bit write access:
  542. * Reactivates the VAPIC after a guest hibernation, i.e. after the
  543. * option ROM content has been re-initialized by a guest power cycle.
  544. * o 32-bit write access:
  545. * Poll for pending IRQs, considering the current VAPIC state.
  546. */
  547. switch (size) {
  548. case 2:
  549. if (s->state == VAPIC_INACTIVE) {
  550. rom_paddr = (env->segs[R_CS].base + env->eip) & ROM_BLOCK_MASK;
  551. s->rom_state_paddr = rom_paddr + data;
  552. s->state = VAPIC_STANDBY;
  553. }
  554. if (vapic_prepare(s) < 0) {
  555. s->state = VAPIC_INACTIVE;
  556. break;
  557. }
  558. break;
  559. case 1:
  560. if (kvm_enabled()) {
  561. /*
  562. * Disable triggering instruction in ROM by writing a NOP.
  563. *
  564. * We cannot do this in TCG mode as the reported IP is not
  565. * accurate.
  566. */
  567. pause_all_vcpus();
  568. patch_byte(env, env->eip - 2, 0x66);
  569. patch_byte(env, env->eip - 1, 0x90);
  570. resume_all_vcpus();
  571. }
  572. if (s->state == VAPIC_ACTIVE) {
  573. break;
  574. }
  575. if (update_rom_mapping(s, env, env->eip) < 0) {
  576. break;
  577. }
  578. if (find_real_tpr_addr(s, env) < 0) {
  579. break;
  580. }
  581. vapic_enable(s, env);
  582. break;
  583. default:
  584. case 4:
  585. if (!kvm_irqchip_in_kernel()) {
  586. apic_poll_irq(env->apic_state);
  587. }
  588. break;
  589. }
  590. }
  591. static const MemoryRegionOps vapic_ops = {
  592. .write = vapic_write,
  593. .endianness = DEVICE_NATIVE_ENDIAN,
  594. };
  595. static int vapic_init(SysBusDevice *dev)
  596. {
  597. VAPICROMState *s = FROM_SYSBUS(VAPICROMState, dev);
  598. memory_region_init_io(&s->io, &vapic_ops, s, "kvmvapic", 2);
  599. sysbus_add_io(dev, VAPIC_IO_PORT, &s->io);
  600. sysbus_init_ioports(dev, VAPIC_IO_PORT, 2);
  601. option_rom[nb_option_roms].name = "kvmvapic.bin";
  602. option_rom[nb_option_roms].bootindex = -1;
  603. nb_option_roms++;
  604. return 0;
  605. }
  606. static void do_vapic_enable(void *data)
  607. {
  608. VAPICROMState *s = data;
  609. vapic_enable(s, first_cpu);
  610. }
  611. static int vapic_post_load(void *opaque, int version_id)
  612. {
  613. VAPICROMState *s = opaque;
  614. uint8_t *zero;
  615. /*
  616. * The old implementation of qemu-kvm did not provide the state
  617. * VAPIC_STANDBY. Reconstruct it.
  618. */
  619. if (s->state == VAPIC_INACTIVE && s->rom_state_paddr != 0) {
  620. s->state = VAPIC_STANDBY;
  621. }
  622. if (s->state != VAPIC_INACTIVE) {
  623. if (vapic_prepare(s) < 0) {
  624. return -1;
  625. }
  626. }
  627. if (s->state == VAPIC_ACTIVE) {
  628. if (smp_cpus == 1) {
  629. run_on_cpu(first_cpu, do_vapic_enable, s);
  630. } else {
  631. zero = g_malloc0(s->rom_state.vapic_size);
  632. cpu_physical_memory_rw(s->vapic_paddr, zero,
  633. s->rom_state.vapic_size, 1);
  634. g_free(zero);
  635. }
  636. }
  637. return 0;
  638. }
  639. static const VMStateDescription vmstate_handlers = {
  640. .name = "kvmvapic-handlers",
  641. .version_id = 1,
  642. .minimum_version_id = 1,
  643. .minimum_version_id_old = 1,
  644. .fields = (VMStateField[]) {
  645. VMSTATE_UINT32(set_tpr, VAPICHandlers),
  646. VMSTATE_UINT32(set_tpr_eax, VAPICHandlers),
  647. VMSTATE_UINT32_ARRAY(get_tpr, VAPICHandlers, 8),
  648. VMSTATE_UINT32(get_tpr_stack, VAPICHandlers),
  649. VMSTATE_END_OF_LIST()
  650. }
  651. };
  652. static const VMStateDescription vmstate_guest_rom = {
  653. .name = "kvmvapic-guest-rom",
  654. .version_id = 1,
  655. .minimum_version_id = 1,
  656. .minimum_version_id_old = 1,
  657. .fields = (VMStateField[]) {
  658. VMSTATE_UNUSED(8), /* signature */
  659. VMSTATE_UINT32(vaddr, GuestROMState),
  660. VMSTATE_UINT32(fixup_start, GuestROMState),
  661. VMSTATE_UINT32(fixup_end, GuestROMState),
  662. VMSTATE_UINT32(vapic_vaddr, GuestROMState),
  663. VMSTATE_UINT32(vapic_size, GuestROMState),
  664. VMSTATE_UINT32(vcpu_shift, GuestROMState),
  665. VMSTATE_UINT32(real_tpr_addr, GuestROMState),
  666. VMSTATE_STRUCT(up, GuestROMState, 0, vmstate_handlers, VAPICHandlers),
  667. VMSTATE_STRUCT(mp, GuestROMState, 0, vmstate_handlers, VAPICHandlers),
  668. VMSTATE_END_OF_LIST()
  669. }
  670. };
  671. static const VMStateDescription vmstate_vapic = {
  672. .name = "kvm-tpr-opt", /* compatible with qemu-kvm VAPIC */
  673. .version_id = 1,
  674. .minimum_version_id = 1,
  675. .minimum_version_id_old = 1,
  676. .post_load = vapic_post_load,
  677. .fields = (VMStateField[]) {
  678. VMSTATE_STRUCT(rom_state, VAPICROMState, 0, vmstate_guest_rom,
  679. GuestROMState),
  680. VMSTATE_UINT32(state, VAPICROMState),
  681. VMSTATE_UINT32(real_tpr_addr, VAPICROMState),
  682. VMSTATE_UINT32(rom_state_vaddr, VAPICROMState),
  683. VMSTATE_UINT32(vapic_paddr, VAPICROMState),
  684. VMSTATE_UINT32(rom_state_paddr, VAPICROMState),
  685. VMSTATE_END_OF_LIST()
  686. }
  687. };
  688. static void vapic_class_init(ObjectClass *klass, void *data)
  689. {
  690. SysBusDeviceClass *sc = SYS_BUS_DEVICE_CLASS(klass);
  691. DeviceClass *dc = DEVICE_CLASS(klass);
  692. dc->no_user = 1;
  693. dc->reset = vapic_reset;
  694. dc->vmsd = &vmstate_vapic;
  695. sc->init = vapic_init;
  696. }
  697. static TypeInfo vapic_type = {
  698. .name = "kvmvapic",
  699. .parent = TYPE_SYS_BUS_DEVICE,
  700. .instance_size = sizeof(VAPICROMState),
  701. .class_init = vapic_class_init,
  702. };
  703. static void vapic_register(void)
  704. {
  705. type_register_static(&vapic_type);
  706. }
  707. type_init(vapic_register);