lapic.c 33 KB

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