lapic.c 29 KB

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