lapic.c 30 KB

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