lapic.c 30 KB

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