lapic.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. /*
  2. * Local APIC virtualization
  3. *
  4. * Copyright (C) 2006 Qumranet, Inc.
  5. * Copyright (C) 2007 Novell
  6. * Copyright (C) 2007 Intel
  7. *
  8. * Authors:
  9. * Dor Laor <dor.laor@qumranet.com>
  10. * Gregory Haskins <ghaskins@novell.com>
  11. * Yaozu (Eddie) Dong <eddie.dong@intel.com>
  12. *
  13. * Based on Xen 3.1 code, Copyright (c) 2004, Intel Corporation.
  14. *
  15. * This work is licensed under the terms of the GNU GPL, version 2. See
  16. * the COPYING file in the top-level directory.
  17. */
  18. #include <linux/kvm_host.h>
  19. #include <linux/kvm.h>
  20. #include <linux/mm.h>
  21. #include <linux/highmem.h>
  22. #include <linux/smp.h>
  23. #include <linux/hrtimer.h>
  24. #include <linux/io.h>
  25. #include <linux/module.h>
  26. #include <linux/math64.h>
  27. #include <asm/processor.h>
  28. #include <asm/msr.h>
  29. #include <asm/page.h>
  30. #include <asm/current.h>
  31. #include <asm/apicdef.h>
  32. #include <asm/atomic.h>
  33. #include <asm/apicdef.h>
  34. #include "kvm_cache_regs.h"
  35. #include "irq.h"
  36. #include "trace.h"
  37. #include "x86.h"
  38. #ifndef CONFIG_X86_64
  39. #define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
  40. #else
  41. #define mod_64(x, y) ((x) % (y))
  42. #endif
  43. #define PRId64 "d"
  44. #define PRIx64 "llx"
  45. #define PRIu64 "u"
  46. #define PRIo64 "o"
  47. #define APIC_BUS_CYCLE_NS 1
  48. /* #define apic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg) */
  49. #define apic_debug(fmt, arg...)
  50. #define APIC_LVT_NUM 6
  51. /* 14 is the version for Xeon and Pentium 8.4.8*/
  52. #define APIC_VERSION (0x14UL | ((APIC_LVT_NUM - 1) << 16))
  53. #define LAPIC_MMIO_LENGTH (1 << 12)
  54. /* followed define is not in apicdef.h */
  55. #define APIC_SHORT_MASK 0xc0000
  56. #define APIC_DEST_NOSHORT 0x0
  57. #define APIC_DEST_MASK 0x800
  58. #define MAX_APIC_VECTOR 256
  59. #define VEC_POS(v) ((v) & (32 - 1))
  60. #define REG_POS(v) (((v) >> 5) << 4)
  61. static inline u32 apic_get_reg(struct kvm_lapic *apic, int reg_off)
  62. {
  63. return *((u32 *) (apic->regs + reg_off));
  64. }
  65. static inline void apic_set_reg(struct kvm_lapic *apic, int reg_off, u32 val)
  66. {
  67. *((u32 *) (apic->regs + reg_off)) = val;
  68. }
  69. static inline int apic_test_and_set_vector(int vec, void *bitmap)
  70. {
  71. return test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  72. }
  73. static inline int apic_test_and_clear_vector(int vec, void *bitmap)
  74. {
  75. return test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  76. }
  77. static inline void apic_set_vector(int vec, void *bitmap)
  78. {
  79. set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  80. }
  81. static inline void apic_clear_vector(int vec, void *bitmap)
  82. {
  83. clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  84. }
  85. static inline int apic_hw_enabled(struct kvm_lapic *apic)
  86. {
  87. return (apic)->vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE;
  88. }
  89. static inline int apic_sw_enabled(struct kvm_lapic *apic)
  90. {
  91. return apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_APIC_ENABLED;
  92. }
  93. static inline int apic_enabled(struct kvm_lapic *apic)
  94. {
  95. return apic_sw_enabled(apic) && apic_hw_enabled(apic);
  96. }
  97. #define LVT_MASK \
  98. (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
  99. #define LINT_MASK \
  100. (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
  101. APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
  102. static inline int kvm_apic_id(struct kvm_lapic *apic)
  103. {
  104. return (apic_get_reg(apic, APIC_ID) >> 24) & 0xff;
  105. }
  106. static inline int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type)
  107. {
  108. return !(apic_get_reg(apic, lvt_type) & APIC_LVT_MASKED);
  109. }
  110. static inline int apic_lvt_vector(struct kvm_lapic *apic, int lvt_type)
  111. {
  112. return apic_get_reg(apic, lvt_type) & APIC_VECTOR_MASK;
  113. }
  114. static inline int apic_lvtt_period(struct kvm_lapic *apic)
  115. {
  116. return apic_get_reg(apic, APIC_LVTT) & APIC_LVT_TIMER_PERIODIC;
  117. }
  118. static inline int apic_lvt_nmi_mode(u32 lvt_val)
  119. {
  120. return (lvt_val & (APIC_MODE_MASK | APIC_LVT_MASKED)) == APIC_DM_NMI;
  121. }
  122. void kvm_apic_set_version(struct kvm_vcpu *vcpu)
  123. {
  124. struct kvm_lapic *apic = vcpu->arch.apic;
  125. struct kvm_cpuid_entry2 *feat;
  126. u32 v = APIC_VERSION;
  127. if (!irqchip_in_kernel(vcpu->kvm))
  128. return;
  129. feat = kvm_find_cpuid_entry(apic->vcpu, 0x1, 0);
  130. if (feat && (feat->ecx & (1 << (X86_FEATURE_X2APIC & 31))))
  131. v |= APIC_LVR_DIRECTED_EOI;
  132. apic_set_reg(apic, APIC_LVR, v);
  133. }
  134. static inline int apic_x2apic_mode(struct kvm_lapic *apic)
  135. {
  136. return apic->vcpu->arch.apic_base & X2APIC_ENABLE;
  137. }
  138. static unsigned int apic_lvt_mask[APIC_LVT_NUM] = {
  139. LVT_MASK | APIC_LVT_TIMER_PERIODIC, /* LVTT */
  140. LVT_MASK | APIC_MODE_MASK, /* LVTTHMR */
  141. LVT_MASK | APIC_MODE_MASK, /* LVTPC */
  142. LINT_MASK, LINT_MASK, /* LVT0-1 */
  143. LVT_MASK /* LVTERR */
  144. };
  145. static int find_highest_vector(void *bitmap)
  146. {
  147. u32 *word = bitmap;
  148. int word_offset = MAX_APIC_VECTOR >> 5;
  149. while ((word_offset != 0) && (word[(--word_offset) << 2] == 0))
  150. continue;
  151. if (likely(!word_offset && !word[0]))
  152. return -1;
  153. else
  154. return fls(word[word_offset << 2]) - 1 + (word_offset << 5);
  155. }
  156. static inline int apic_test_and_set_irr(int vec, struct kvm_lapic *apic)
  157. {
  158. apic->irr_pending = true;
  159. return apic_test_and_set_vector(vec, apic->regs + APIC_IRR);
  160. }
  161. static inline int apic_search_irr(struct kvm_lapic *apic)
  162. {
  163. return find_highest_vector(apic->regs + APIC_IRR);
  164. }
  165. static inline int apic_find_highest_irr(struct kvm_lapic *apic)
  166. {
  167. int result;
  168. if (!apic->irr_pending)
  169. return -1;
  170. result = apic_search_irr(apic);
  171. ASSERT(result == -1 || result >= 16);
  172. return result;
  173. }
  174. static inline void apic_clear_irr(int vec, struct kvm_lapic *apic)
  175. {
  176. apic->irr_pending = false;
  177. apic_clear_vector(vec, apic->regs + APIC_IRR);
  178. if (apic_search_irr(apic) != -1)
  179. apic->irr_pending = true;
  180. }
  181. int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu)
  182. {
  183. struct kvm_lapic *apic = vcpu->arch.apic;
  184. int highest_irr;
  185. /* This may race with setting of irr in __apic_accept_irq() and
  186. * value returned may be wrong, but kvm_vcpu_kick() in __apic_accept_irq
  187. * will cause vmexit immediately and the value will be recalculated
  188. * on the next vmentry.
  189. */
  190. if (!apic)
  191. return 0;
  192. highest_irr = apic_find_highest_irr(apic);
  193. return highest_irr;
  194. }
  195. static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
  196. int vector, int level, int trig_mode);
  197. int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq)
  198. {
  199. struct kvm_lapic *apic = vcpu->arch.apic;
  200. return __apic_accept_irq(apic, irq->delivery_mode, irq->vector,
  201. irq->level, irq->trig_mode);
  202. }
  203. static inline int apic_find_highest_isr(struct kvm_lapic *apic)
  204. {
  205. int result;
  206. result = find_highest_vector(apic->regs + APIC_ISR);
  207. ASSERT(result == -1 || result >= 16);
  208. return result;
  209. }
  210. static void apic_update_ppr(struct kvm_lapic *apic)
  211. {
  212. u32 tpr, isrv, ppr;
  213. int isr;
  214. tpr = apic_get_reg(apic, APIC_TASKPRI);
  215. isr = apic_find_highest_isr(apic);
  216. isrv = (isr != -1) ? isr : 0;
  217. if ((tpr & 0xf0) >= (isrv & 0xf0))
  218. ppr = tpr & 0xff;
  219. else
  220. ppr = isrv & 0xf0;
  221. apic_debug("vlapic %p, ppr 0x%x, isr 0x%x, isrv 0x%x",
  222. apic, ppr, isr, isrv);
  223. apic_set_reg(apic, APIC_PROCPRI, ppr);
  224. }
  225. static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr)
  226. {
  227. apic_set_reg(apic, APIC_TASKPRI, tpr);
  228. apic_update_ppr(apic);
  229. }
  230. int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest)
  231. {
  232. return dest == 0xff || kvm_apic_id(apic) == dest;
  233. }
  234. int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda)
  235. {
  236. int result = 0;
  237. u32 logical_id;
  238. if (apic_x2apic_mode(apic)) {
  239. logical_id = apic_get_reg(apic, APIC_LDR);
  240. return logical_id & mda;
  241. }
  242. logical_id = GET_APIC_LOGICAL_ID(apic_get_reg(apic, APIC_LDR));
  243. switch (apic_get_reg(apic, APIC_DFR)) {
  244. case APIC_DFR_FLAT:
  245. if (logical_id & mda)
  246. result = 1;
  247. break;
  248. case APIC_DFR_CLUSTER:
  249. if (((logical_id >> 4) == (mda >> 0x4))
  250. && (logical_id & mda & 0xf))
  251. result = 1;
  252. break;
  253. default:
  254. printk(KERN_WARNING "Bad DFR vcpu %d: %08x\n",
  255. apic->vcpu->vcpu_id, apic_get_reg(apic, APIC_DFR));
  256. break;
  257. }
  258. return result;
  259. }
  260. int kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
  261. int short_hand, int dest, int dest_mode)
  262. {
  263. int result = 0;
  264. struct kvm_lapic *target = vcpu->arch.apic;
  265. apic_debug("target %p, source %p, dest 0x%x, "
  266. "dest_mode 0x%x, short_hand 0x%x\n",
  267. target, source, dest, dest_mode, short_hand);
  268. ASSERT(!target);
  269. switch (short_hand) {
  270. case APIC_DEST_NOSHORT:
  271. if (dest_mode == 0)
  272. /* Physical mode. */
  273. result = kvm_apic_match_physical_addr(target, dest);
  274. else
  275. /* Logical mode. */
  276. result = kvm_apic_match_logical_addr(target, dest);
  277. break;
  278. case APIC_DEST_SELF:
  279. result = (target == source);
  280. break;
  281. case APIC_DEST_ALLINC:
  282. result = 1;
  283. break;
  284. case APIC_DEST_ALLBUT:
  285. result = (target != source);
  286. break;
  287. default:
  288. printk(KERN_WARNING "Bad dest shorthand value %x\n",
  289. short_hand);
  290. break;
  291. }
  292. return result;
  293. }
  294. /*
  295. * Add a pending IRQ into lapic.
  296. * Return 1 if successfully added and 0 if discarded.
  297. */
  298. static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
  299. int vector, int level, int trig_mode)
  300. {
  301. int result = 0;
  302. struct kvm_vcpu *vcpu = apic->vcpu;
  303. switch (delivery_mode) {
  304. case APIC_DM_LOWEST:
  305. vcpu->arch.apic_arb_prio++;
  306. case APIC_DM_FIXED:
  307. /* FIXME add logic for vcpu on reset */
  308. if (unlikely(!apic_enabled(apic)))
  309. break;
  310. result = !apic_test_and_set_irr(vector, apic);
  311. trace_kvm_apic_accept_irq(vcpu->vcpu_id, delivery_mode,
  312. trig_mode, vector, !result);
  313. if (!result) {
  314. if (trig_mode)
  315. apic_debug("level trig mode repeatedly for "
  316. "vector %d", vector);
  317. break;
  318. }
  319. if (trig_mode) {
  320. apic_debug("level trig mode for vector %d", vector);
  321. apic_set_vector(vector, apic->regs + APIC_TMR);
  322. } else
  323. apic_clear_vector(vector, apic->regs + APIC_TMR);
  324. kvm_vcpu_kick(vcpu);
  325. break;
  326. case APIC_DM_REMRD:
  327. printk(KERN_DEBUG "Ignoring delivery mode 3\n");
  328. break;
  329. case APIC_DM_SMI:
  330. printk(KERN_DEBUG "Ignoring guest SMI\n");
  331. break;
  332. case APIC_DM_NMI:
  333. result = 1;
  334. kvm_inject_nmi(vcpu);
  335. kvm_vcpu_kick(vcpu);
  336. break;
  337. case APIC_DM_INIT:
  338. if (level) {
  339. result = 1;
  340. if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE)
  341. printk(KERN_DEBUG
  342. "INIT on a runnable vcpu %d\n",
  343. vcpu->vcpu_id);
  344. vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
  345. kvm_vcpu_kick(vcpu);
  346. } else {
  347. apic_debug("Ignoring de-assert INIT to vcpu %d\n",
  348. vcpu->vcpu_id);
  349. }
  350. break;
  351. case APIC_DM_STARTUP:
  352. apic_debug("SIPI to vcpu %d vector 0x%02x\n",
  353. vcpu->vcpu_id, vector);
  354. if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
  355. result = 1;
  356. vcpu->arch.sipi_vector = vector;
  357. vcpu->arch.mp_state = KVM_MP_STATE_SIPI_RECEIVED;
  358. kvm_vcpu_kick(vcpu);
  359. }
  360. break;
  361. case APIC_DM_EXTINT:
  362. /*
  363. * Should only be called by kvm_apic_local_deliver() with LVT0,
  364. * before NMI watchdog was enabled. Already handled by
  365. * kvm_apic_accept_pic_intr().
  366. */
  367. break;
  368. default:
  369. printk(KERN_ERR "TODO: unsupported delivery mode %x\n",
  370. delivery_mode);
  371. break;
  372. }
  373. return result;
  374. }
  375. int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
  376. {
  377. return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
  378. }
  379. static void apic_set_eoi(struct kvm_lapic *apic)
  380. {
  381. int vector = apic_find_highest_isr(apic);
  382. int trigger_mode;
  383. /*
  384. * Not every write EOI will has corresponding ISR,
  385. * one example is when Kernel check timer on setup_IO_APIC
  386. */
  387. if (vector == -1)
  388. return;
  389. apic_clear_vector(vector, apic->regs + APIC_ISR);
  390. apic_update_ppr(apic);
  391. if (apic_test_and_clear_vector(vector, apic->regs + APIC_TMR))
  392. trigger_mode = IOAPIC_LEVEL_TRIG;
  393. else
  394. trigger_mode = IOAPIC_EDGE_TRIG;
  395. if (!(apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI)) {
  396. mutex_lock(&apic->vcpu->kvm->irq_lock);
  397. kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
  398. mutex_unlock(&apic->vcpu->kvm->irq_lock);
  399. }
  400. }
  401. static void apic_send_ipi(struct kvm_lapic *apic)
  402. {
  403. u32 icr_low = apic_get_reg(apic, APIC_ICR);
  404. u32 icr_high = apic_get_reg(apic, APIC_ICR2);
  405. struct kvm_lapic_irq irq;
  406. irq.vector = icr_low & APIC_VECTOR_MASK;
  407. irq.delivery_mode = icr_low & APIC_MODE_MASK;
  408. irq.dest_mode = icr_low & APIC_DEST_MASK;
  409. irq.level = icr_low & APIC_INT_ASSERT;
  410. irq.trig_mode = icr_low & APIC_INT_LEVELTRIG;
  411. irq.shorthand = icr_low & APIC_SHORT_MASK;
  412. if (apic_x2apic_mode(apic))
  413. irq.dest_id = icr_high;
  414. else
  415. irq.dest_id = GET_APIC_DEST_FIELD(icr_high);
  416. trace_kvm_apic_ipi(icr_low, irq.dest_id);
  417. apic_debug("icr_high 0x%x, icr_low 0x%x, "
  418. "short_hand 0x%x, dest 0x%x, trig_mode 0x%x, level 0x%x, "
  419. "dest_mode 0x%x, delivery_mode 0x%x, vector 0x%x\n",
  420. icr_high, icr_low, irq.shorthand, irq.dest_id,
  421. irq.trig_mode, irq.level, irq.dest_mode, irq.delivery_mode,
  422. irq.vector);
  423. mutex_lock(&apic->vcpu->kvm->irq_lock);
  424. kvm_irq_delivery_to_apic(apic->vcpu->kvm, apic, &irq);
  425. mutex_unlock(&apic->vcpu->kvm->irq_lock);
  426. }
  427. static u32 apic_get_tmcct(struct kvm_lapic *apic)
  428. {
  429. ktime_t remaining;
  430. s64 ns;
  431. u32 tmcct;
  432. ASSERT(apic != NULL);
  433. /* if initial count is 0, current count should also be 0 */
  434. if (apic_get_reg(apic, APIC_TMICT) == 0)
  435. return 0;
  436. remaining = hrtimer_get_remaining(&apic->lapic_timer.timer);
  437. if (ktime_to_ns(remaining) < 0)
  438. remaining = ktime_set(0, 0);
  439. ns = mod_64(ktime_to_ns(remaining), apic->lapic_timer.period);
  440. tmcct = div64_u64(ns,
  441. (APIC_BUS_CYCLE_NS * apic->divide_count));
  442. return tmcct;
  443. }
  444. static void __report_tpr_access(struct kvm_lapic *apic, bool write)
  445. {
  446. struct kvm_vcpu *vcpu = apic->vcpu;
  447. struct kvm_run *run = vcpu->run;
  448. set_bit(KVM_REQ_REPORT_TPR_ACCESS, &vcpu->requests);
  449. run->tpr_access.rip = kvm_rip_read(vcpu);
  450. run->tpr_access.is_write = write;
  451. }
  452. static inline void report_tpr_access(struct kvm_lapic *apic, bool write)
  453. {
  454. if (apic->vcpu->arch.tpr_access_reporting)
  455. __report_tpr_access(apic, write);
  456. }
  457. static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset)
  458. {
  459. u32 val = 0;
  460. if (offset >= LAPIC_MMIO_LENGTH)
  461. return 0;
  462. switch (offset) {
  463. case APIC_ID:
  464. if (apic_x2apic_mode(apic))
  465. val = kvm_apic_id(apic);
  466. else
  467. val = kvm_apic_id(apic) << 24;
  468. break;
  469. case APIC_ARBPRI:
  470. printk(KERN_WARNING "Access APIC ARBPRI register "
  471. "which is for P6\n");
  472. break;
  473. case APIC_TMCCT: /* Timer CCR */
  474. val = apic_get_tmcct(apic);
  475. break;
  476. case APIC_TASKPRI:
  477. report_tpr_access(apic, false);
  478. /* fall thru */
  479. default:
  480. apic_update_ppr(apic);
  481. val = apic_get_reg(apic, offset);
  482. break;
  483. }
  484. return val;
  485. }
  486. static inline struct kvm_lapic *to_lapic(struct kvm_io_device *dev)
  487. {
  488. return container_of(dev, struct kvm_lapic, dev);
  489. }
  490. static int apic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
  491. void *data)
  492. {
  493. unsigned char alignment = offset & 0xf;
  494. u32 result;
  495. /* this bitmask has a bit cleared for each reserver register */
  496. static const u64 rmask = 0x43ff01ffffffe70cULL;
  497. if ((alignment + len) > 4) {
  498. apic_debug("KVM_APIC_READ: alignment error %x %d\n",
  499. offset, len);
  500. return 1;
  501. }
  502. if (offset > 0x3f0 || !(rmask & (1ULL << (offset >> 4)))) {
  503. apic_debug("KVM_APIC_READ: read reserved register %x\n",
  504. offset);
  505. return 1;
  506. }
  507. result = __apic_read(apic, offset & ~0xf);
  508. trace_kvm_apic_read(offset, result);
  509. switch (len) {
  510. case 1:
  511. case 2:
  512. case 4:
  513. memcpy(data, (char *)&result + alignment, len);
  514. break;
  515. default:
  516. printk(KERN_ERR "Local APIC read with len = %x, "
  517. "should be 1,2, or 4 instead\n", len);
  518. break;
  519. }
  520. return 0;
  521. }
  522. static int apic_mmio_in_range(struct kvm_lapic *apic, gpa_t addr)
  523. {
  524. return apic_hw_enabled(apic) &&
  525. addr >= apic->base_address &&
  526. addr < apic->base_address + LAPIC_MMIO_LENGTH;
  527. }
  528. static int apic_mmio_read(struct kvm_io_device *this,
  529. gpa_t address, int len, void *data)
  530. {
  531. struct kvm_lapic *apic = to_lapic(this);
  532. u32 offset = address - apic->base_address;
  533. if (!apic_mmio_in_range(apic, address))
  534. return -EOPNOTSUPP;
  535. apic_reg_read(apic, offset, len, data);
  536. return 0;
  537. }
  538. static void update_divide_count(struct kvm_lapic *apic)
  539. {
  540. u32 tmp1, tmp2, tdcr;
  541. tdcr = apic_get_reg(apic, APIC_TDCR);
  542. tmp1 = tdcr & 0xf;
  543. tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
  544. apic->divide_count = 0x1 << (tmp2 & 0x7);
  545. apic_debug("timer divide count is 0x%x\n",
  546. apic->divide_count);
  547. }
  548. static void start_apic_timer(struct kvm_lapic *apic)
  549. {
  550. ktime_t now = apic->lapic_timer.timer.base->get_time();
  551. apic->lapic_timer.period = (u64)apic_get_reg(apic, APIC_TMICT) *
  552. APIC_BUS_CYCLE_NS * apic->divide_count;
  553. atomic_set(&apic->lapic_timer.pending, 0);
  554. if (!apic->lapic_timer.period)
  555. return;
  556. /*
  557. * Do not allow the guest to program periodic timers with small
  558. * interval, since the hrtimers are not throttled by the host
  559. * scheduler.
  560. */
  561. if (apic_lvtt_period(apic)) {
  562. if (apic->lapic_timer.period < NSEC_PER_MSEC/2)
  563. apic->lapic_timer.period = NSEC_PER_MSEC/2;
  564. }
  565. hrtimer_start(&apic->lapic_timer.timer,
  566. ktime_add_ns(now, apic->lapic_timer.period),
  567. HRTIMER_MODE_ABS);
  568. apic_debug("%s: bus cycle is %" PRId64 "ns, now 0x%016"
  569. PRIx64 ", "
  570. "timer initial count 0x%x, period %lldns, "
  571. "expire @ 0x%016" PRIx64 ".\n", __func__,
  572. APIC_BUS_CYCLE_NS, ktime_to_ns(now),
  573. apic_get_reg(apic, APIC_TMICT),
  574. apic->lapic_timer.period,
  575. ktime_to_ns(ktime_add_ns(now,
  576. apic->lapic_timer.period)));
  577. }
  578. static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val)
  579. {
  580. int nmi_wd_enabled = apic_lvt_nmi_mode(apic_get_reg(apic, APIC_LVT0));
  581. if (apic_lvt_nmi_mode(lvt0_val)) {
  582. if (!nmi_wd_enabled) {
  583. apic_debug("Receive NMI setting on APIC_LVT0 "
  584. "for cpu %d\n", apic->vcpu->vcpu_id);
  585. apic->vcpu->kvm->arch.vapics_in_nmi_mode++;
  586. }
  587. } else if (nmi_wd_enabled)
  588. apic->vcpu->kvm->arch.vapics_in_nmi_mode--;
  589. }
  590. static int apic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
  591. {
  592. int ret = 0;
  593. trace_kvm_apic_write(reg, val);
  594. switch (reg) {
  595. case APIC_ID: /* Local APIC ID */
  596. if (!apic_x2apic_mode(apic))
  597. apic_set_reg(apic, APIC_ID, val);
  598. else
  599. ret = 1;
  600. break;
  601. case APIC_TASKPRI:
  602. report_tpr_access(apic, true);
  603. apic_set_tpr(apic, val & 0xff);
  604. break;
  605. case APIC_EOI:
  606. apic_set_eoi(apic);
  607. break;
  608. case APIC_LDR:
  609. if (!apic_x2apic_mode(apic))
  610. apic_set_reg(apic, APIC_LDR, val & APIC_LDR_MASK);
  611. else
  612. ret = 1;
  613. break;
  614. case APIC_DFR:
  615. if (!apic_x2apic_mode(apic))
  616. apic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF);
  617. else
  618. ret = 1;
  619. break;
  620. case APIC_SPIV: {
  621. u32 mask = 0x3ff;
  622. if (apic_get_reg(apic, APIC_LVR) & APIC_LVR_DIRECTED_EOI)
  623. mask |= APIC_SPIV_DIRECTED_EOI;
  624. apic_set_reg(apic, APIC_SPIV, val & mask);
  625. if (!(val & APIC_SPIV_APIC_ENABLED)) {
  626. int i;
  627. u32 lvt_val;
  628. for (i = 0; i < APIC_LVT_NUM; i++) {
  629. lvt_val = apic_get_reg(apic,
  630. APIC_LVTT + 0x10 * i);
  631. apic_set_reg(apic, APIC_LVTT + 0x10 * i,
  632. lvt_val | APIC_LVT_MASKED);
  633. }
  634. atomic_set(&apic->lapic_timer.pending, 0);
  635. }
  636. break;
  637. }
  638. case APIC_ICR:
  639. /* No delay here, so we always clear the pending bit */
  640. apic_set_reg(apic, APIC_ICR, val & ~(1 << 12));
  641. apic_send_ipi(apic);
  642. break;
  643. case APIC_ICR2:
  644. if (!apic_x2apic_mode(apic))
  645. val &= 0xff000000;
  646. apic_set_reg(apic, APIC_ICR2, val);
  647. break;
  648. case APIC_LVT0:
  649. apic_manage_nmi_watchdog(apic, val);
  650. case APIC_LVTT:
  651. case APIC_LVTTHMR:
  652. case APIC_LVTPC:
  653. case APIC_LVT1:
  654. case APIC_LVTERR:
  655. /* TODO: Check vector */
  656. if (!apic_sw_enabled(apic))
  657. val |= APIC_LVT_MASKED;
  658. val &= apic_lvt_mask[(reg - APIC_LVTT) >> 4];
  659. apic_set_reg(apic, reg, val);
  660. break;
  661. case APIC_TMICT:
  662. hrtimer_cancel(&apic->lapic_timer.timer);
  663. apic_set_reg(apic, APIC_TMICT, val);
  664. start_apic_timer(apic);
  665. break;
  666. case APIC_TDCR:
  667. if (val & 4)
  668. printk(KERN_ERR "KVM_WRITE:TDCR %x\n", val);
  669. apic_set_reg(apic, APIC_TDCR, val);
  670. update_divide_count(apic);
  671. break;
  672. case APIC_ESR:
  673. if (apic_x2apic_mode(apic) && val != 0) {
  674. printk(KERN_ERR "KVM_WRITE:ESR not zero %x\n", val);
  675. ret = 1;
  676. }
  677. break;
  678. case APIC_SELF_IPI:
  679. if (apic_x2apic_mode(apic)) {
  680. apic_reg_write(apic, APIC_ICR, 0x40000 | (val & 0xff));
  681. } else
  682. ret = 1;
  683. break;
  684. default:
  685. ret = 1;
  686. break;
  687. }
  688. if (ret)
  689. apic_debug("Local APIC Write to read-only register %x\n", reg);
  690. return ret;
  691. }
  692. static int apic_mmio_write(struct kvm_io_device *this,
  693. gpa_t address, int len, const void *data)
  694. {
  695. struct kvm_lapic *apic = to_lapic(this);
  696. unsigned int offset = address - apic->base_address;
  697. u32 val;
  698. if (!apic_mmio_in_range(apic, address))
  699. return -EOPNOTSUPP;
  700. /*
  701. * APIC register must be aligned on 128-bits boundary.
  702. * 32/64/128 bits registers must be accessed thru 32 bits.
  703. * Refer SDM 8.4.1
  704. */
  705. if (len != 4 || (offset & 0xf)) {
  706. /* Don't shout loud, $infamous_os would cause only noise. */
  707. apic_debug("apic write: bad size=%d %lx\n", len, (long)address);
  708. return 0;
  709. }
  710. val = *(u32*)data;
  711. /* too common printing */
  712. if (offset != APIC_EOI)
  713. apic_debug("%s: offset 0x%x with length 0x%x, and value is "
  714. "0x%x\n", __func__, offset, len, val);
  715. apic_reg_write(apic, offset & 0xff0, val);
  716. return 0;
  717. }
  718. void kvm_free_lapic(struct kvm_vcpu *vcpu)
  719. {
  720. if (!vcpu->arch.apic)
  721. return;
  722. hrtimer_cancel(&vcpu->arch.apic->lapic_timer.timer);
  723. if (vcpu->arch.apic->regs_page)
  724. __free_page(vcpu->arch.apic->regs_page);
  725. kfree(vcpu->arch.apic);
  726. }
  727. /*
  728. *----------------------------------------------------------------------
  729. * LAPIC interface
  730. *----------------------------------------------------------------------
  731. */
  732. void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
  733. {
  734. struct kvm_lapic *apic = vcpu->arch.apic;
  735. if (!apic)
  736. return;
  737. apic_set_tpr(apic, ((cr8 & 0x0f) << 4)
  738. | (apic_get_reg(apic, APIC_TASKPRI) & 4));
  739. }
  740. u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
  741. {
  742. struct kvm_lapic *apic = vcpu->arch.apic;
  743. u64 tpr;
  744. if (!apic)
  745. return 0;
  746. tpr = (u64) apic_get_reg(apic, APIC_TASKPRI);
  747. return (tpr & 0xf0) >> 4;
  748. }
  749. void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
  750. {
  751. struct kvm_lapic *apic = vcpu->arch.apic;
  752. if (!apic) {
  753. value |= MSR_IA32_APICBASE_BSP;
  754. vcpu->arch.apic_base = value;
  755. return;
  756. }
  757. if (!kvm_vcpu_is_bsp(apic->vcpu))
  758. value &= ~MSR_IA32_APICBASE_BSP;
  759. vcpu->arch.apic_base = value;
  760. if (apic_x2apic_mode(apic)) {
  761. u32 id = kvm_apic_id(apic);
  762. u32 ldr = ((id & ~0xf) << 16) | (1 << (id & 0xf));
  763. apic_set_reg(apic, APIC_LDR, ldr);
  764. }
  765. apic->base_address = apic->vcpu->arch.apic_base &
  766. MSR_IA32_APICBASE_BASE;
  767. /* with FSB delivery interrupt, we can restart APIC functionality */
  768. apic_debug("apic base msr is 0x%016" PRIx64 ", and base address is "
  769. "0x%lx.\n", apic->vcpu->arch.apic_base, apic->base_address);
  770. }
  771. void kvm_lapic_reset(struct kvm_vcpu *vcpu)
  772. {
  773. struct kvm_lapic *apic;
  774. int i;
  775. apic_debug("%s\n", __func__);
  776. ASSERT(vcpu);
  777. apic = vcpu->arch.apic;
  778. ASSERT(apic != NULL);
  779. /* Stop the timer in case it's a reset to an active apic */
  780. hrtimer_cancel(&apic->lapic_timer.timer);
  781. apic_set_reg(apic, APIC_ID, vcpu->vcpu_id << 24);
  782. kvm_apic_set_version(apic->vcpu);
  783. for (i = 0; i < APIC_LVT_NUM; i++)
  784. apic_set_reg(apic, APIC_LVTT + 0x10 * i, APIC_LVT_MASKED);
  785. apic_set_reg(apic, APIC_LVT0,
  786. SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
  787. apic_set_reg(apic, APIC_DFR, 0xffffffffU);
  788. apic_set_reg(apic, APIC_SPIV, 0xff);
  789. apic_set_reg(apic, APIC_TASKPRI, 0);
  790. apic_set_reg(apic, APIC_LDR, 0);
  791. apic_set_reg(apic, APIC_ESR, 0);
  792. apic_set_reg(apic, APIC_ICR, 0);
  793. apic_set_reg(apic, APIC_ICR2, 0);
  794. apic_set_reg(apic, APIC_TDCR, 0);
  795. apic_set_reg(apic, APIC_TMICT, 0);
  796. for (i = 0; i < 8; i++) {
  797. apic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
  798. apic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
  799. apic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
  800. }
  801. apic->irr_pending = false;
  802. update_divide_count(apic);
  803. atomic_set(&apic->lapic_timer.pending, 0);
  804. if (kvm_vcpu_is_bsp(vcpu))
  805. vcpu->arch.apic_base |= MSR_IA32_APICBASE_BSP;
  806. apic_update_ppr(apic);
  807. vcpu->arch.apic_arb_prio = 0;
  808. apic_debug(KERN_INFO "%s: vcpu=%p, id=%d, base_msr="
  809. "0x%016" PRIx64 ", base_address=0x%0lx.\n", __func__,
  810. vcpu, kvm_apic_id(apic),
  811. vcpu->arch.apic_base, apic->base_address);
  812. }
  813. bool kvm_apic_present(struct kvm_vcpu *vcpu)
  814. {
  815. return vcpu->arch.apic && apic_hw_enabled(vcpu->arch.apic);
  816. }
  817. int kvm_lapic_enabled(struct kvm_vcpu *vcpu)
  818. {
  819. return kvm_apic_present(vcpu) && apic_sw_enabled(vcpu->arch.apic);
  820. }
  821. /*
  822. *----------------------------------------------------------------------
  823. * timer interface
  824. *----------------------------------------------------------------------
  825. */
  826. static bool lapic_is_periodic(struct kvm_timer *ktimer)
  827. {
  828. struct kvm_lapic *apic = container_of(ktimer, struct kvm_lapic,
  829. lapic_timer);
  830. return apic_lvtt_period(apic);
  831. }
  832. int apic_has_pending_timer(struct kvm_vcpu *vcpu)
  833. {
  834. struct kvm_lapic *lapic = vcpu->arch.apic;
  835. if (lapic && apic_enabled(lapic) && apic_lvt_enabled(lapic, APIC_LVTT))
  836. return atomic_read(&lapic->lapic_timer.pending);
  837. return 0;
  838. }
  839. static int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type)
  840. {
  841. u32 reg = apic_get_reg(apic, lvt_type);
  842. int vector, mode, trig_mode;
  843. if (apic_hw_enabled(apic) && !(reg & APIC_LVT_MASKED)) {
  844. vector = reg & APIC_VECTOR_MASK;
  845. mode = reg & APIC_MODE_MASK;
  846. trig_mode = reg & APIC_LVT_LEVEL_TRIGGER;
  847. return __apic_accept_irq(apic, mode, vector, 1, trig_mode);
  848. }
  849. return 0;
  850. }
  851. void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu)
  852. {
  853. struct kvm_lapic *apic = vcpu->arch.apic;
  854. if (apic)
  855. kvm_apic_local_deliver(apic, APIC_LVT0);
  856. }
  857. static struct kvm_timer_ops lapic_timer_ops = {
  858. .is_periodic = lapic_is_periodic,
  859. };
  860. static const struct kvm_io_device_ops apic_mmio_ops = {
  861. .read = apic_mmio_read,
  862. .write = apic_mmio_write,
  863. };
  864. int kvm_create_lapic(struct kvm_vcpu *vcpu)
  865. {
  866. struct kvm_lapic *apic;
  867. ASSERT(vcpu != NULL);
  868. apic_debug("apic_init %d\n", vcpu->vcpu_id);
  869. apic = kzalloc(sizeof(*apic), GFP_KERNEL);
  870. if (!apic)
  871. goto nomem;
  872. vcpu->arch.apic = apic;
  873. apic->regs_page = alloc_page(GFP_KERNEL);
  874. if (apic->regs_page == NULL) {
  875. printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
  876. vcpu->vcpu_id);
  877. goto nomem_free_apic;
  878. }
  879. apic->regs = page_address(apic->regs_page);
  880. memset(apic->regs, 0, PAGE_SIZE);
  881. apic->vcpu = vcpu;
  882. hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
  883. HRTIMER_MODE_ABS);
  884. apic->lapic_timer.timer.function = kvm_timer_fn;
  885. apic->lapic_timer.t_ops = &lapic_timer_ops;
  886. apic->lapic_timer.kvm = vcpu->kvm;
  887. apic->lapic_timer.vcpu = vcpu;
  888. apic->base_address = APIC_DEFAULT_PHYS_BASE;
  889. vcpu->arch.apic_base = APIC_DEFAULT_PHYS_BASE;
  890. kvm_lapic_reset(vcpu);
  891. kvm_iodevice_init(&apic->dev, &apic_mmio_ops);
  892. return 0;
  893. nomem_free_apic:
  894. kfree(apic);
  895. nomem:
  896. return -ENOMEM;
  897. }
  898. int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
  899. {
  900. struct kvm_lapic *apic = vcpu->arch.apic;
  901. int highest_irr;
  902. if (!apic || !apic_enabled(apic))
  903. return -1;
  904. apic_update_ppr(apic);
  905. highest_irr = apic_find_highest_irr(apic);
  906. if ((highest_irr == -1) ||
  907. ((highest_irr & 0xF0) <= apic_get_reg(apic, APIC_PROCPRI)))
  908. return -1;
  909. return highest_irr;
  910. }
  911. int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
  912. {
  913. u32 lvt0 = apic_get_reg(vcpu->arch.apic, APIC_LVT0);
  914. int r = 0;
  915. if (kvm_vcpu_is_bsp(vcpu)) {
  916. if (!apic_hw_enabled(vcpu->arch.apic))
  917. r = 1;
  918. if ((lvt0 & APIC_LVT_MASKED) == 0 &&
  919. GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
  920. r = 1;
  921. }
  922. return r;
  923. }
  924. void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
  925. {
  926. struct kvm_lapic *apic = vcpu->arch.apic;
  927. if (apic && atomic_read(&apic->lapic_timer.pending) > 0) {
  928. if (kvm_apic_local_deliver(apic, APIC_LVTT))
  929. atomic_dec(&apic->lapic_timer.pending);
  930. }
  931. }
  932. int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
  933. {
  934. int vector = kvm_apic_has_interrupt(vcpu);
  935. struct kvm_lapic *apic = vcpu->arch.apic;
  936. if (vector == -1)
  937. return -1;
  938. apic_set_vector(vector, apic->regs + APIC_ISR);
  939. apic_update_ppr(apic);
  940. apic_clear_irr(vector, apic);
  941. return vector;
  942. }
  943. void kvm_apic_post_state_restore(struct kvm_vcpu *vcpu)
  944. {
  945. struct kvm_lapic *apic = vcpu->arch.apic;
  946. apic->base_address = vcpu->arch.apic_base &
  947. MSR_IA32_APICBASE_BASE;
  948. kvm_apic_set_version(vcpu);
  949. apic_update_ppr(apic);
  950. hrtimer_cancel(&apic->lapic_timer.timer);
  951. update_divide_count(apic);
  952. start_apic_timer(apic);
  953. }
  954. void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
  955. {
  956. struct kvm_lapic *apic = vcpu->arch.apic;
  957. struct hrtimer *timer;
  958. if (!apic)
  959. return;
  960. timer = &apic->lapic_timer.timer;
  961. if (hrtimer_cancel(timer))
  962. hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
  963. }
  964. void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu)
  965. {
  966. u32 data;
  967. void *vapic;
  968. if (!irqchip_in_kernel(vcpu->kvm) || !vcpu->arch.apic->vapic_addr)
  969. return;
  970. vapic = kmap_atomic(vcpu->arch.apic->vapic_page, KM_USER0);
  971. data = *(u32 *)(vapic + offset_in_page(vcpu->arch.apic->vapic_addr));
  972. kunmap_atomic(vapic, KM_USER0);
  973. apic_set_tpr(vcpu->arch.apic, data & 0xff);
  974. }
  975. void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu)
  976. {
  977. u32 data, tpr;
  978. int max_irr, max_isr;
  979. struct kvm_lapic *apic;
  980. void *vapic;
  981. if (!irqchip_in_kernel(vcpu->kvm) || !vcpu->arch.apic->vapic_addr)
  982. return;
  983. apic = vcpu->arch.apic;
  984. tpr = apic_get_reg(apic, APIC_TASKPRI) & 0xff;
  985. max_irr = apic_find_highest_irr(apic);
  986. if (max_irr < 0)
  987. max_irr = 0;
  988. max_isr = apic_find_highest_isr(apic);
  989. if (max_isr < 0)
  990. max_isr = 0;
  991. data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24);
  992. vapic = kmap_atomic(vcpu->arch.apic->vapic_page, KM_USER0);
  993. *(u32 *)(vapic + offset_in_page(vcpu->arch.apic->vapic_addr)) = data;
  994. kunmap_atomic(vapic, KM_USER0);
  995. }
  996. void kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr)
  997. {
  998. if (!irqchip_in_kernel(vcpu->kvm))
  999. return;
  1000. vcpu->arch.apic->vapic_addr = vapic_addr;
  1001. }
  1002. int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  1003. {
  1004. struct kvm_lapic *apic = vcpu->arch.apic;
  1005. u32 reg = (msr - APIC_BASE_MSR) << 4;
  1006. if (!irqchip_in_kernel(vcpu->kvm) || !apic_x2apic_mode(apic))
  1007. return 1;
  1008. /* if this is ICR write vector before command */
  1009. if (msr == 0x830)
  1010. apic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
  1011. return apic_reg_write(apic, reg, (u32)data);
  1012. }
  1013. int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
  1014. {
  1015. struct kvm_lapic *apic = vcpu->arch.apic;
  1016. u32 reg = (msr - APIC_BASE_MSR) << 4, low, high = 0;
  1017. if (!irqchip_in_kernel(vcpu->kvm) || !apic_x2apic_mode(apic))
  1018. return 1;
  1019. if (apic_reg_read(apic, reg, 4, &low))
  1020. return 1;
  1021. if (msr == 0x830)
  1022. apic_reg_read(apic, APIC_ICR2, 4, &high);
  1023. *data = (((u64)high) << 32) | low;
  1024. return 0;
  1025. }