lapic.c 27 KB

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