lapic.c 30 KB

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