lapic.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  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. if (!result) {
  312. if (trig_mode)
  313. apic_debug("level trig mode repeatedly for "
  314. "vector %d", vector);
  315. break;
  316. }
  317. if (trig_mode) {
  318. apic_debug("level trig mode for vector %d", vector);
  319. apic_set_vector(vector, apic->regs + APIC_TMR);
  320. } else
  321. apic_clear_vector(vector, apic->regs + APIC_TMR);
  322. kvm_vcpu_kick(vcpu);
  323. break;
  324. case APIC_DM_REMRD:
  325. printk(KERN_DEBUG "Ignoring delivery mode 3\n");
  326. break;
  327. case APIC_DM_SMI:
  328. printk(KERN_DEBUG "Ignoring guest SMI\n");
  329. break;
  330. case APIC_DM_NMI:
  331. result = 1;
  332. kvm_inject_nmi(vcpu);
  333. kvm_vcpu_kick(vcpu);
  334. break;
  335. case APIC_DM_INIT:
  336. if (level) {
  337. result = 1;
  338. if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE)
  339. printk(KERN_DEBUG
  340. "INIT on a runnable vcpu %d\n",
  341. vcpu->vcpu_id);
  342. vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
  343. kvm_vcpu_kick(vcpu);
  344. } else {
  345. apic_debug("Ignoring de-assert INIT to vcpu %d\n",
  346. vcpu->vcpu_id);
  347. }
  348. break;
  349. case APIC_DM_STARTUP:
  350. apic_debug("SIPI to vcpu %d vector 0x%02x\n",
  351. vcpu->vcpu_id, vector);
  352. if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
  353. result = 1;
  354. vcpu->arch.sipi_vector = vector;
  355. vcpu->arch.mp_state = KVM_MP_STATE_SIPI_RECEIVED;
  356. kvm_vcpu_kick(vcpu);
  357. }
  358. break;
  359. case APIC_DM_EXTINT:
  360. /*
  361. * Should only be called by kvm_apic_local_deliver() with LVT0,
  362. * before NMI watchdog was enabled. Already handled by
  363. * kvm_apic_accept_pic_intr().
  364. */
  365. break;
  366. default:
  367. printk(KERN_ERR "TODO: unsupported delivery mode %x\n",
  368. delivery_mode);
  369. break;
  370. }
  371. return result;
  372. }
  373. int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
  374. {
  375. return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
  376. }
  377. static void apic_set_eoi(struct kvm_lapic *apic)
  378. {
  379. int vector = apic_find_highest_isr(apic);
  380. int trigger_mode;
  381. /*
  382. * Not every write EOI will has corresponding ISR,
  383. * one example is when Kernel check timer on setup_IO_APIC
  384. */
  385. if (vector == -1)
  386. return;
  387. apic_clear_vector(vector, apic->regs + APIC_ISR);
  388. apic_update_ppr(apic);
  389. if (apic_test_and_clear_vector(vector, apic->regs + APIC_TMR))
  390. trigger_mode = IOAPIC_LEVEL_TRIG;
  391. else
  392. trigger_mode = IOAPIC_EDGE_TRIG;
  393. if (!(apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI)) {
  394. mutex_lock(&apic->vcpu->kvm->irq_lock);
  395. kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
  396. mutex_unlock(&apic->vcpu->kvm->irq_lock);
  397. }
  398. }
  399. static void apic_send_ipi(struct kvm_lapic *apic)
  400. {
  401. u32 icr_low = apic_get_reg(apic, APIC_ICR);
  402. u32 icr_high = apic_get_reg(apic, APIC_ICR2);
  403. struct kvm_lapic_irq irq;
  404. irq.vector = icr_low & APIC_VECTOR_MASK;
  405. irq.delivery_mode = icr_low & APIC_MODE_MASK;
  406. irq.dest_mode = icr_low & APIC_DEST_MASK;
  407. irq.level = icr_low & APIC_INT_ASSERT;
  408. irq.trig_mode = icr_low & APIC_INT_LEVELTRIG;
  409. irq.shorthand = icr_low & APIC_SHORT_MASK;
  410. if (apic_x2apic_mode(apic))
  411. irq.dest_id = icr_high;
  412. else
  413. irq.dest_id = GET_APIC_DEST_FIELD(icr_high);
  414. apic_debug("icr_high 0x%x, icr_low 0x%x, "
  415. "short_hand 0x%x, dest 0x%x, trig_mode 0x%x, level 0x%x, "
  416. "dest_mode 0x%x, delivery_mode 0x%x, vector 0x%x\n",
  417. icr_high, icr_low, irq.shorthand, irq.dest_id,
  418. irq.trig_mode, irq.level, irq.dest_mode, irq.delivery_mode,
  419. irq.vector);
  420. mutex_lock(&apic->vcpu->kvm->irq_lock);
  421. kvm_irq_delivery_to_apic(apic->vcpu->kvm, apic, &irq);
  422. mutex_unlock(&apic->vcpu->kvm->irq_lock);
  423. }
  424. static u32 apic_get_tmcct(struct kvm_lapic *apic)
  425. {
  426. ktime_t remaining;
  427. s64 ns;
  428. u32 tmcct;
  429. ASSERT(apic != NULL);
  430. /* if initial count is 0, current count should also be 0 */
  431. if (apic_get_reg(apic, APIC_TMICT) == 0)
  432. return 0;
  433. remaining = hrtimer_expires_remaining(&apic->lapic_timer.timer);
  434. if (ktime_to_ns(remaining) < 0)
  435. remaining = ktime_set(0, 0);
  436. ns = mod_64(ktime_to_ns(remaining), apic->lapic_timer.period);
  437. tmcct = div64_u64(ns,
  438. (APIC_BUS_CYCLE_NS * apic->divide_count));
  439. return tmcct;
  440. }
  441. static void __report_tpr_access(struct kvm_lapic *apic, bool write)
  442. {
  443. struct kvm_vcpu *vcpu = apic->vcpu;
  444. struct kvm_run *run = vcpu->run;
  445. set_bit(KVM_REQ_REPORT_TPR_ACCESS, &vcpu->requests);
  446. run->tpr_access.rip = kvm_rip_read(vcpu);
  447. run->tpr_access.is_write = write;
  448. }
  449. static inline void report_tpr_access(struct kvm_lapic *apic, bool write)
  450. {
  451. if (apic->vcpu->arch.tpr_access_reporting)
  452. __report_tpr_access(apic, write);
  453. }
  454. static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset)
  455. {
  456. u32 val = 0;
  457. if (offset >= LAPIC_MMIO_LENGTH)
  458. return 0;
  459. switch (offset) {
  460. case APIC_ID:
  461. if (apic_x2apic_mode(apic))
  462. val = kvm_apic_id(apic);
  463. else
  464. val = kvm_apic_id(apic) << 24;
  465. break;
  466. case APIC_ARBPRI:
  467. printk(KERN_WARNING "Access APIC ARBPRI register "
  468. "which is for P6\n");
  469. break;
  470. case APIC_TMCCT: /* Timer CCR */
  471. val = apic_get_tmcct(apic);
  472. break;
  473. case APIC_TASKPRI:
  474. report_tpr_access(apic, false);
  475. /* fall thru */
  476. default:
  477. apic_update_ppr(apic);
  478. val = apic_get_reg(apic, offset);
  479. break;
  480. }
  481. return val;
  482. }
  483. static inline struct kvm_lapic *to_lapic(struct kvm_io_device *dev)
  484. {
  485. return container_of(dev, struct kvm_lapic, dev);
  486. }
  487. static int apic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
  488. void *data)
  489. {
  490. unsigned char alignment = offset & 0xf;
  491. u32 result;
  492. /* this bitmask has a bit cleared for each reserver register */
  493. static const u64 rmask = 0x43ff01ffffffe70cULL;
  494. if ((alignment + len) > 4) {
  495. printk(KERN_ERR "KVM_APIC_READ: alignment error %x %d\n",
  496. offset, len);
  497. return 1;
  498. }
  499. if (offset > 0x3f0 || !(rmask & (1ULL << (offset >> 4)))) {
  500. printk(KERN_ERR "KVM_APIC_READ: read reserved register %x\n",
  501. offset);
  502. return 1;
  503. }
  504. result = __apic_read(apic, offset & ~0xf);
  505. trace_kvm_apic_read(offset, result);
  506. switch (len) {
  507. case 1:
  508. case 2:
  509. case 4:
  510. memcpy(data, (char *)&result + alignment, len);
  511. break;
  512. default:
  513. printk(KERN_ERR "Local APIC read with len = %x, "
  514. "should be 1,2, or 4 instead\n", len);
  515. break;
  516. }
  517. return 0;
  518. }
  519. static int apic_mmio_in_range(struct kvm_lapic *apic, gpa_t addr)
  520. {
  521. return apic_hw_enabled(apic) &&
  522. addr >= apic->base_address &&
  523. addr < apic->base_address + LAPIC_MMIO_LENGTH;
  524. }
  525. static int apic_mmio_read(struct kvm_io_device *this,
  526. gpa_t address, int len, void *data)
  527. {
  528. struct kvm_lapic *apic = to_lapic(this);
  529. u32 offset = address - apic->base_address;
  530. if (!apic_mmio_in_range(apic, address))
  531. return -EOPNOTSUPP;
  532. apic_reg_read(apic, offset, len, data);
  533. return 0;
  534. }
  535. static void update_divide_count(struct kvm_lapic *apic)
  536. {
  537. u32 tmp1, tmp2, tdcr;
  538. tdcr = apic_get_reg(apic, APIC_TDCR);
  539. tmp1 = tdcr & 0xf;
  540. tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
  541. apic->divide_count = 0x1 << (tmp2 & 0x7);
  542. apic_debug("timer divide count is 0x%x\n",
  543. apic->divide_count);
  544. }
  545. static void start_apic_timer(struct kvm_lapic *apic)
  546. {
  547. ktime_t now = apic->lapic_timer.timer.base->get_time();
  548. apic->lapic_timer.period = apic_get_reg(apic, APIC_TMICT) *
  549. APIC_BUS_CYCLE_NS * apic->divide_count;
  550. atomic_set(&apic->lapic_timer.pending, 0);
  551. if (!apic->lapic_timer.period)
  552. return;
  553. hrtimer_start(&apic->lapic_timer.timer,
  554. ktime_add_ns(now, apic->lapic_timer.period),
  555. HRTIMER_MODE_ABS);
  556. apic_debug("%s: bus cycle is %" PRId64 "ns, now 0x%016"
  557. PRIx64 ", "
  558. "timer initial count 0x%x, period %lldns, "
  559. "expire @ 0x%016" PRIx64 ".\n", __func__,
  560. APIC_BUS_CYCLE_NS, ktime_to_ns(now),
  561. apic_get_reg(apic, APIC_TMICT),
  562. apic->lapic_timer.period,
  563. ktime_to_ns(ktime_add_ns(now,
  564. apic->lapic_timer.period)));
  565. }
  566. static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val)
  567. {
  568. int nmi_wd_enabled = apic_lvt_nmi_mode(apic_get_reg(apic, APIC_LVT0));
  569. if (apic_lvt_nmi_mode(lvt0_val)) {
  570. if (!nmi_wd_enabled) {
  571. apic_debug("Receive NMI setting on APIC_LVT0 "
  572. "for cpu %d\n", apic->vcpu->vcpu_id);
  573. apic->vcpu->kvm->arch.vapics_in_nmi_mode++;
  574. }
  575. } else if (nmi_wd_enabled)
  576. apic->vcpu->kvm->arch.vapics_in_nmi_mode--;
  577. }
  578. static int apic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
  579. {
  580. int ret = 0;
  581. trace_kvm_apic_write(reg, val);
  582. switch (reg) {
  583. case APIC_ID: /* Local APIC ID */
  584. if (!apic_x2apic_mode(apic))
  585. apic_set_reg(apic, APIC_ID, val);
  586. else
  587. ret = 1;
  588. break;
  589. case APIC_TASKPRI:
  590. report_tpr_access(apic, true);
  591. apic_set_tpr(apic, val & 0xff);
  592. break;
  593. case APIC_EOI:
  594. apic_set_eoi(apic);
  595. break;
  596. case APIC_LDR:
  597. if (!apic_x2apic_mode(apic))
  598. apic_set_reg(apic, APIC_LDR, val & APIC_LDR_MASK);
  599. else
  600. ret = 1;
  601. break;
  602. case APIC_DFR:
  603. if (!apic_x2apic_mode(apic))
  604. apic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF);
  605. else
  606. ret = 1;
  607. break;
  608. case APIC_SPIV: {
  609. u32 mask = 0x3ff;
  610. if (apic_get_reg(apic, APIC_LVR) & APIC_LVR_DIRECTED_EOI)
  611. mask |= APIC_SPIV_DIRECTED_EOI;
  612. apic_set_reg(apic, APIC_SPIV, val & mask);
  613. if (!(val & APIC_SPIV_APIC_ENABLED)) {
  614. int i;
  615. u32 lvt_val;
  616. for (i = 0; i < APIC_LVT_NUM; i++) {
  617. lvt_val = apic_get_reg(apic,
  618. APIC_LVTT + 0x10 * i);
  619. apic_set_reg(apic, APIC_LVTT + 0x10 * i,
  620. lvt_val | APIC_LVT_MASKED);
  621. }
  622. atomic_set(&apic->lapic_timer.pending, 0);
  623. }
  624. break;
  625. }
  626. case APIC_ICR:
  627. /* No delay here, so we always clear the pending bit */
  628. apic_set_reg(apic, APIC_ICR, val & ~(1 << 12));
  629. apic_send_ipi(apic);
  630. break;
  631. case APIC_ICR2:
  632. if (!apic_x2apic_mode(apic))
  633. val &= 0xff000000;
  634. apic_set_reg(apic, APIC_ICR2, val);
  635. break;
  636. case APIC_LVT0:
  637. apic_manage_nmi_watchdog(apic, val);
  638. case APIC_LVTT:
  639. case APIC_LVTTHMR:
  640. case APIC_LVTPC:
  641. case APIC_LVT1:
  642. case APIC_LVTERR:
  643. /* TODO: Check vector */
  644. if (!apic_sw_enabled(apic))
  645. val |= APIC_LVT_MASKED;
  646. val &= apic_lvt_mask[(reg - APIC_LVTT) >> 4];
  647. apic_set_reg(apic, reg, val);
  648. break;
  649. case APIC_TMICT:
  650. hrtimer_cancel(&apic->lapic_timer.timer);
  651. apic_set_reg(apic, APIC_TMICT, val);
  652. start_apic_timer(apic);
  653. break;
  654. case APIC_TDCR:
  655. if (val & 4)
  656. printk(KERN_ERR "KVM_WRITE:TDCR %x\n", val);
  657. apic_set_reg(apic, APIC_TDCR, val);
  658. update_divide_count(apic);
  659. break;
  660. case APIC_ESR:
  661. if (apic_x2apic_mode(apic) && val != 0) {
  662. printk(KERN_ERR "KVM_WRITE:ESR not zero %x\n", val);
  663. ret = 1;
  664. }
  665. break;
  666. case APIC_SELF_IPI:
  667. if (apic_x2apic_mode(apic)) {
  668. apic_reg_write(apic, APIC_ICR, 0x40000 | (val & 0xff));
  669. } else
  670. ret = 1;
  671. break;
  672. default:
  673. ret = 1;
  674. break;
  675. }
  676. if (ret)
  677. apic_debug("Local APIC Write to read-only register %x\n", reg);
  678. return ret;
  679. }
  680. static int apic_mmio_write(struct kvm_io_device *this,
  681. gpa_t address, int len, const void *data)
  682. {
  683. struct kvm_lapic *apic = to_lapic(this);
  684. unsigned int offset = address - apic->base_address;
  685. u32 val;
  686. if (!apic_mmio_in_range(apic, address))
  687. return -EOPNOTSUPP;
  688. /*
  689. * APIC register must be aligned on 128-bits boundary.
  690. * 32/64/128 bits registers must be accessed thru 32 bits.
  691. * Refer SDM 8.4.1
  692. */
  693. if (len != 4 || (offset & 0xf)) {
  694. /* Don't shout loud, $infamous_os would cause only noise. */
  695. apic_debug("apic write: bad size=%d %lx\n", len, (long)address);
  696. return 0;
  697. }
  698. val = *(u32*)data;
  699. /* too common printing */
  700. if (offset != APIC_EOI)
  701. apic_debug("%s: offset 0x%x with length 0x%x, and value is "
  702. "0x%x\n", __func__, offset, len, val);
  703. apic_reg_write(apic, offset & 0xff0, val);
  704. return 0;
  705. }
  706. void kvm_free_lapic(struct kvm_vcpu *vcpu)
  707. {
  708. if (!vcpu->arch.apic)
  709. return;
  710. hrtimer_cancel(&vcpu->arch.apic->lapic_timer.timer);
  711. if (vcpu->arch.apic->regs_page)
  712. __free_page(vcpu->arch.apic->regs_page);
  713. kfree(vcpu->arch.apic);
  714. }
  715. /*
  716. *----------------------------------------------------------------------
  717. * LAPIC interface
  718. *----------------------------------------------------------------------
  719. */
  720. void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
  721. {
  722. struct kvm_lapic *apic = vcpu->arch.apic;
  723. if (!apic)
  724. return;
  725. apic_set_tpr(apic, ((cr8 & 0x0f) << 4)
  726. | (apic_get_reg(apic, APIC_TASKPRI) & 4));
  727. }
  728. u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
  729. {
  730. struct kvm_lapic *apic = vcpu->arch.apic;
  731. u64 tpr;
  732. if (!apic)
  733. return 0;
  734. tpr = (u64) apic_get_reg(apic, APIC_TASKPRI);
  735. return (tpr & 0xf0) >> 4;
  736. }
  737. void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
  738. {
  739. struct kvm_lapic *apic = vcpu->arch.apic;
  740. if (!apic) {
  741. value |= MSR_IA32_APICBASE_BSP;
  742. vcpu->arch.apic_base = value;
  743. return;
  744. }
  745. if (!kvm_vcpu_is_bsp(apic->vcpu))
  746. value &= ~MSR_IA32_APICBASE_BSP;
  747. vcpu->arch.apic_base = value;
  748. if (apic_x2apic_mode(apic)) {
  749. u32 id = kvm_apic_id(apic);
  750. u32 ldr = ((id & ~0xf) << 16) | (1 << (id & 0xf));
  751. apic_set_reg(apic, APIC_LDR, ldr);
  752. }
  753. apic->base_address = apic->vcpu->arch.apic_base &
  754. MSR_IA32_APICBASE_BASE;
  755. /* with FSB delivery interrupt, we can restart APIC functionality */
  756. apic_debug("apic base msr is 0x%016" PRIx64 ", and base address is "
  757. "0x%lx.\n", apic->vcpu->arch.apic_base, apic->base_address);
  758. }
  759. void kvm_lapic_reset(struct kvm_vcpu *vcpu)
  760. {
  761. struct kvm_lapic *apic;
  762. int i;
  763. apic_debug("%s\n", __func__);
  764. ASSERT(vcpu);
  765. apic = vcpu->arch.apic;
  766. ASSERT(apic != NULL);
  767. /* Stop the timer in case it's a reset to an active apic */
  768. hrtimer_cancel(&apic->lapic_timer.timer);
  769. apic_set_reg(apic, APIC_ID, vcpu->vcpu_id << 24);
  770. kvm_apic_set_version(apic->vcpu);
  771. for (i = 0; i < APIC_LVT_NUM; i++)
  772. apic_set_reg(apic, APIC_LVTT + 0x10 * i, APIC_LVT_MASKED);
  773. apic_set_reg(apic, APIC_LVT0,
  774. SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
  775. apic_set_reg(apic, APIC_DFR, 0xffffffffU);
  776. apic_set_reg(apic, APIC_SPIV, 0xff);
  777. apic_set_reg(apic, APIC_TASKPRI, 0);
  778. apic_set_reg(apic, APIC_LDR, 0);
  779. apic_set_reg(apic, APIC_ESR, 0);
  780. apic_set_reg(apic, APIC_ICR, 0);
  781. apic_set_reg(apic, APIC_ICR2, 0);
  782. apic_set_reg(apic, APIC_TDCR, 0);
  783. apic_set_reg(apic, APIC_TMICT, 0);
  784. for (i = 0; i < 8; i++) {
  785. apic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
  786. apic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
  787. apic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
  788. }
  789. apic->irr_pending = false;
  790. update_divide_count(apic);
  791. atomic_set(&apic->lapic_timer.pending, 0);
  792. if (kvm_vcpu_is_bsp(vcpu))
  793. vcpu->arch.apic_base |= MSR_IA32_APICBASE_BSP;
  794. apic_update_ppr(apic);
  795. vcpu->arch.apic_arb_prio = 0;
  796. apic_debug(KERN_INFO "%s: vcpu=%p, id=%d, base_msr="
  797. "0x%016" PRIx64 ", base_address=0x%0lx.\n", __func__,
  798. vcpu, kvm_apic_id(apic),
  799. vcpu->arch.apic_base, apic->base_address);
  800. }
  801. bool kvm_apic_present(struct kvm_vcpu *vcpu)
  802. {
  803. return vcpu->arch.apic && apic_hw_enabled(vcpu->arch.apic);
  804. }
  805. int kvm_lapic_enabled(struct kvm_vcpu *vcpu)
  806. {
  807. return kvm_apic_present(vcpu) && apic_sw_enabled(vcpu->arch.apic);
  808. }
  809. /*
  810. *----------------------------------------------------------------------
  811. * timer interface
  812. *----------------------------------------------------------------------
  813. */
  814. static bool lapic_is_periodic(struct kvm_timer *ktimer)
  815. {
  816. struct kvm_lapic *apic = container_of(ktimer, struct kvm_lapic,
  817. lapic_timer);
  818. return apic_lvtt_period(apic);
  819. }
  820. int apic_has_pending_timer(struct kvm_vcpu *vcpu)
  821. {
  822. struct kvm_lapic *lapic = vcpu->arch.apic;
  823. if (lapic && apic_enabled(lapic) && apic_lvt_enabled(lapic, APIC_LVTT))
  824. return atomic_read(&lapic->lapic_timer.pending);
  825. return 0;
  826. }
  827. static int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type)
  828. {
  829. u32 reg = apic_get_reg(apic, lvt_type);
  830. int vector, mode, trig_mode;
  831. if (apic_hw_enabled(apic) && !(reg & APIC_LVT_MASKED)) {
  832. vector = reg & APIC_VECTOR_MASK;
  833. mode = reg & APIC_MODE_MASK;
  834. trig_mode = reg & APIC_LVT_LEVEL_TRIGGER;
  835. return __apic_accept_irq(apic, mode, vector, 1, trig_mode);
  836. }
  837. return 0;
  838. }
  839. void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu)
  840. {
  841. struct kvm_lapic *apic = vcpu->arch.apic;
  842. if (apic)
  843. kvm_apic_local_deliver(apic, APIC_LVT0);
  844. }
  845. static struct kvm_timer_ops lapic_timer_ops = {
  846. .is_periodic = lapic_is_periodic,
  847. };
  848. static const struct kvm_io_device_ops apic_mmio_ops = {
  849. .read = apic_mmio_read,
  850. .write = apic_mmio_write,
  851. };
  852. int kvm_create_lapic(struct kvm_vcpu *vcpu)
  853. {
  854. struct kvm_lapic *apic;
  855. ASSERT(vcpu != NULL);
  856. apic_debug("apic_init %d\n", vcpu->vcpu_id);
  857. apic = kzalloc(sizeof(*apic), GFP_KERNEL);
  858. if (!apic)
  859. goto nomem;
  860. vcpu->arch.apic = apic;
  861. apic->regs_page = alloc_page(GFP_KERNEL);
  862. if (apic->regs_page == NULL) {
  863. printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
  864. vcpu->vcpu_id);
  865. goto nomem_free_apic;
  866. }
  867. apic->regs = page_address(apic->regs_page);
  868. memset(apic->regs, 0, PAGE_SIZE);
  869. apic->vcpu = vcpu;
  870. hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
  871. HRTIMER_MODE_ABS);
  872. apic->lapic_timer.timer.function = kvm_timer_fn;
  873. apic->lapic_timer.t_ops = &lapic_timer_ops;
  874. apic->lapic_timer.kvm = vcpu->kvm;
  875. apic->lapic_timer.vcpu = vcpu;
  876. apic->base_address = APIC_DEFAULT_PHYS_BASE;
  877. vcpu->arch.apic_base = APIC_DEFAULT_PHYS_BASE;
  878. kvm_lapic_reset(vcpu);
  879. kvm_iodevice_init(&apic->dev, &apic_mmio_ops);
  880. return 0;
  881. nomem_free_apic:
  882. kfree(apic);
  883. nomem:
  884. return -ENOMEM;
  885. }
  886. int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
  887. {
  888. struct kvm_lapic *apic = vcpu->arch.apic;
  889. int highest_irr;
  890. if (!apic || !apic_enabled(apic))
  891. return -1;
  892. apic_update_ppr(apic);
  893. highest_irr = apic_find_highest_irr(apic);
  894. if ((highest_irr == -1) ||
  895. ((highest_irr & 0xF0) <= apic_get_reg(apic, APIC_PROCPRI)))
  896. return -1;
  897. return highest_irr;
  898. }
  899. int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
  900. {
  901. u32 lvt0 = apic_get_reg(vcpu->arch.apic, APIC_LVT0);
  902. int r = 0;
  903. if (kvm_vcpu_is_bsp(vcpu)) {
  904. if (!apic_hw_enabled(vcpu->arch.apic))
  905. r = 1;
  906. if ((lvt0 & APIC_LVT_MASKED) == 0 &&
  907. GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
  908. r = 1;
  909. }
  910. return r;
  911. }
  912. void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
  913. {
  914. struct kvm_lapic *apic = vcpu->arch.apic;
  915. if (apic && atomic_read(&apic->lapic_timer.pending) > 0) {
  916. if (kvm_apic_local_deliver(apic, APIC_LVTT))
  917. atomic_dec(&apic->lapic_timer.pending);
  918. }
  919. }
  920. int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
  921. {
  922. int vector = kvm_apic_has_interrupt(vcpu);
  923. struct kvm_lapic *apic = vcpu->arch.apic;
  924. if (vector == -1)
  925. return -1;
  926. apic_set_vector(vector, apic->regs + APIC_ISR);
  927. apic_update_ppr(apic);
  928. apic_clear_irr(vector, apic);
  929. return vector;
  930. }
  931. void kvm_apic_post_state_restore(struct kvm_vcpu *vcpu)
  932. {
  933. struct kvm_lapic *apic = vcpu->arch.apic;
  934. apic->base_address = vcpu->arch.apic_base &
  935. MSR_IA32_APICBASE_BASE;
  936. kvm_apic_set_version(vcpu);
  937. apic_update_ppr(apic);
  938. hrtimer_cancel(&apic->lapic_timer.timer);
  939. update_divide_count(apic);
  940. start_apic_timer(apic);
  941. }
  942. void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
  943. {
  944. struct kvm_lapic *apic = vcpu->arch.apic;
  945. struct hrtimer *timer;
  946. if (!apic)
  947. return;
  948. timer = &apic->lapic_timer.timer;
  949. if (hrtimer_cancel(timer))
  950. hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
  951. }
  952. void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu)
  953. {
  954. u32 data;
  955. void *vapic;
  956. if (!irqchip_in_kernel(vcpu->kvm) || !vcpu->arch.apic->vapic_addr)
  957. return;
  958. vapic = kmap_atomic(vcpu->arch.apic->vapic_page, KM_USER0);
  959. data = *(u32 *)(vapic + offset_in_page(vcpu->arch.apic->vapic_addr));
  960. kunmap_atomic(vapic, KM_USER0);
  961. apic_set_tpr(vcpu->arch.apic, data & 0xff);
  962. }
  963. void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu)
  964. {
  965. u32 data, tpr;
  966. int max_irr, max_isr;
  967. struct kvm_lapic *apic;
  968. void *vapic;
  969. if (!irqchip_in_kernel(vcpu->kvm) || !vcpu->arch.apic->vapic_addr)
  970. return;
  971. apic = vcpu->arch.apic;
  972. tpr = apic_get_reg(apic, APIC_TASKPRI) & 0xff;
  973. max_irr = apic_find_highest_irr(apic);
  974. if (max_irr < 0)
  975. max_irr = 0;
  976. max_isr = apic_find_highest_isr(apic);
  977. if (max_isr < 0)
  978. max_isr = 0;
  979. data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24);
  980. vapic = kmap_atomic(vcpu->arch.apic->vapic_page, KM_USER0);
  981. *(u32 *)(vapic + offset_in_page(vcpu->arch.apic->vapic_addr)) = data;
  982. kunmap_atomic(vapic, KM_USER0);
  983. }
  984. void kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr)
  985. {
  986. if (!irqchip_in_kernel(vcpu->kvm))
  987. return;
  988. vcpu->arch.apic->vapic_addr = vapic_addr;
  989. }
  990. int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  991. {
  992. struct kvm_lapic *apic = vcpu->arch.apic;
  993. u32 reg = (msr - APIC_BASE_MSR) << 4;
  994. if (!irqchip_in_kernel(vcpu->kvm) || !apic_x2apic_mode(apic))
  995. return 1;
  996. /* if this is ICR write vector before command */
  997. if (msr == 0x830)
  998. apic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
  999. return apic_reg_write(apic, reg, (u32)data);
  1000. }
  1001. int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
  1002. {
  1003. struct kvm_lapic *apic = vcpu->arch.apic;
  1004. u32 reg = (msr - APIC_BASE_MSR) << 4, low, high = 0;
  1005. if (!irqchip_in_kernel(vcpu->kvm) || !apic_x2apic_mode(apic))
  1006. return 1;
  1007. if (apic_reg_read(apic, reg, 4, &low))
  1008. return 1;
  1009. if (msr == 0x830)
  1010. apic_reg_read(apic, APIC_ICR2, 4, &high);
  1011. *data = (((u64)high) << 32) | low;
  1012. return 0;
  1013. }