lapic.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  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. printk(KERN_DEBUG "Ignoring guest NMI\n");
  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. if (offset >= LAPIC_MMIO_LENGTH)
  479. return 0;
  480. switch (offset) {
  481. case APIC_ARBPRI:
  482. printk(KERN_WARNING "Access APIC ARBPRI register "
  483. "which is for P6\n");
  484. break;
  485. case APIC_TMCCT: /* Timer CCR */
  486. val = apic_get_tmcct(apic);
  487. break;
  488. case APIC_TASKPRI:
  489. report_tpr_access(apic, false);
  490. /* fall thru */
  491. default:
  492. apic_update_ppr(apic);
  493. val = apic_get_reg(apic, offset);
  494. break;
  495. }
  496. return val;
  497. }
  498. static void apic_mmio_read(struct kvm_io_device *this,
  499. gpa_t address, int len, void *data)
  500. {
  501. struct kvm_lapic *apic = (struct kvm_lapic *)this->private;
  502. unsigned int offset = address - apic->base_address;
  503. unsigned char alignment = offset & 0xf;
  504. u32 result;
  505. if ((alignment + len) > 4) {
  506. printk(KERN_ERR "KVM_APIC_READ: alignment error %lx %d",
  507. (unsigned long)address, len);
  508. return;
  509. }
  510. result = __apic_read(apic, offset & ~0xf);
  511. switch (len) {
  512. case 1:
  513. case 2:
  514. case 4:
  515. memcpy(data, (char *)&result + alignment, len);
  516. break;
  517. default:
  518. printk(KERN_ERR "Local APIC read with len = %x, "
  519. "should be 1,2, or 4 instead\n", len);
  520. break;
  521. }
  522. }
  523. static void update_divide_count(struct kvm_lapic *apic)
  524. {
  525. u32 tmp1, tmp2, tdcr;
  526. tdcr = apic_get_reg(apic, APIC_TDCR);
  527. tmp1 = tdcr & 0xf;
  528. tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
  529. apic->timer.divide_count = 0x1 << (tmp2 & 0x7);
  530. apic_debug("timer divide count is 0x%x\n",
  531. apic->timer.divide_count);
  532. }
  533. static void start_apic_timer(struct kvm_lapic *apic)
  534. {
  535. ktime_t now = apic->timer.dev.base->get_time();
  536. apic->timer.last_update = now;
  537. apic->timer.period = apic_get_reg(apic, APIC_TMICT) *
  538. APIC_BUS_CYCLE_NS * apic->timer.divide_count;
  539. atomic_set(&apic->timer.pending, 0);
  540. if (!apic->timer.period)
  541. return;
  542. hrtimer_start(&apic->timer.dev,
  543. ktime_add_ns(now, apic->timer.period),
  544. HRTIMER_MODE_ABS);
  545. apic_debug("%s: bus cycle is %" PRId64 "ns, now 0x%016"
  546. PRIx64 ", "
  547. "timer initial count 0x%x, period %lldns, "
  548. "expire @ 0x%016" PRIx64 ".\n", __func__,
  549. APIC_BUS_CYCLE_NS, ktime_to_ns(now),
  550. apic_get_reg(apic, APIC_TMICT),
  551. apic->timer.period,
  552. ktime_to_ns(ktime_add_ns(now,
  553. apic->timer.period)));
  554. }
  555. static void apic_mmio_write(struct kvm_io_device *this,
  556. gpa_t address, int len, const void *data)
  557. {
  558. struct kvm_lapic *apic = (struct kvm_lapic *)this->private;
  559. unsigned int offset = address - apic->base_address;
  560. unsigned char alignment = offset & 0xf;
  561. u32 val;
  562. /*
  563. * APIC register must be aligned on 128-bits boundary.
  564. * 32/64/128 bits registers must be accessed thru 32 bits.
  565. * Refer SDM 8.4.1
  566. */
  567. if (len != 4 || alignment) {
  568. if (printk_ratelimit())
  569. printk(KERN_ERR "apic write: bad size=%d %lx\n",
  570. len, (long)address);
  571. return;
  572. }
  573. val = *(u32 *) data;
  574. /* too common printing */
  575. if (offset != APIC_EOI)
  576. apic_debug("%s: offset 0x%x with length 0x%x, and value is "
  577. "0x%x\n", __func__, offset, len, val);
  578. offset &= 0xff0;
  579. switch (offset) {
  580. case APIC_ID: /* Local APIC ID */
  581. apic_set_reg(apic, APIC_ID, val);
  582. break;
  583. case APIC_TASKPRI:
  584. report_tpr_access(apic, true);
  585. apic_set_tpr(apic, val & 0xff);
  586. break;
  587. case APIC_EOI:
  588. apic_set_eoi(apic);
  589. break;
  590. case APIC_LDR:
  591. apic_set_reg(apic, APIC_LDR, val & APIC_LDR_MASK);
  592. break;
  593. case APIC_DFR:
  594. apic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF);
  595. break;
  596. case APIC_SPIV:
  597. apic_set_reg(apic, APIC_SPIV, val & 0x3ff);
  598. if (!(val & APIC_SPIV_APIC_ENABLED)) {
  599. int i;
  600. u32 lvt_val;
  601. for (i = 0; i < APIC_LVT_NUM; i++) {
  602. lvt_val = apic_get_reg(apic,
  603. APIC_LVTT + 0x10 * i);
  604. apic_set_reg(apic, APIC_LVTT + 0x10 * i,
  605. lvt_val | APIC_LVT_MASKED);
  606. }
  607. atomic_set(&apic->timer.pending, 0);
  608. }
  609. break;
  610. case APIC_ICR:
  611. /* No delay here, so we always clear the pending bit */
  612. apic_set_reg(apic, APIC_ICR, val & ~(1 << 12));
  613. apic_send_ipi(apic);
  614. break;
  615. case APIC_ICR2:
  616. apic_set_reg(apic, APIC_ICR2, val & 0xff000000);
  617. break;
  618. case APIC_LVTT:
  619. case APIC_LVTTHMR:
  620. case APIC_LVTPC:
  621. case APIC_LVT0:
  622. case APIC_LVT1:
  623. case APIC_LVTERR:
  624. /* TODO: Check vector */
  625. if (!apic_sw_enabled(apic))
  626. val |= APIC_LVT_MASKED;
  627. val &= apic_lvt_mask[(offset - APIC_LVTT) >> 4];
  628. apic_set_reg(apic, offset, val);
  629. break;
  630. case APIC_TMICT:
  631. hrtimer_cancel(&apic->timer.dev);
  632. apic_set_reg(apic, APIC_TMICT, val);
  633. start_apic_timer(apic);
  634. return;
  635. case APIC_TDCR:
  636. if (val & 4)
  637. printk(KERN_ERR "KVM_WRITE:TDCR %x\n", val);
  638. apic_set_reg(apic, APIC_TDCR, val);
  639. update_divide_count(apic);
  640. break;
  641. default:
  642. apic_debug("Local APIC Write to read-only register %x\n",
  643. offset);
  644. break;
  645. }
  646. }
  647. static int apic_mmio_range(struct kvm_io_device *this, gpa_t addr)
  648. {
  649. struct kvm_lapic *apic = (struct kvm_lapic *)this->private;
  650. int ret = 0;
  651. if (apic_hw_enabled(apic) &&
  652. (addr >= apic->base_address) &&
  653. (addr < (apic->base_address + LAPIC_MMIO_LENGTH)))
  654. ret = 1;
  655. return ret;
  656. }
  657. void kvm_free_lapic(struct kvm_vcpu *vcpu)
  658. {
  659. if (!vcpu->arch.apic)
  660. return;
  661. hrtimer_cancel(&vcpu->arch.apic->timer.dev);
  662. if (vcpu->arch.apic->regs_page)
  663. __free_page(vcpu->arch.apic->regs_page);
  664. kfree(vcpu->arch.apic);
  665. }
  666. /*
  667. *----------------------------------------------------------------------
  668. * LAPIC interface
  669. *----------------------------------------------------------------------
  670. */
  671. void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
  672. {
  673. struct kvm_lapic *apic = vcpu->arch.apic;
  674. if (!apic)
  675. return;
  676. apic_set_tpr(apic, ((cr8 & 0x0f) << 4)
  677. | (apic_get_reg(apic, APIC_TASKPRI) & 4));
  678. }
  679. EXPORT_SYMBOL_GPL(kvm_lapic_set_tpr);
  680. u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
  681. {
  682. struct kvm_lapic *apic = vcpu->arch.apic;
  683. u64 tpr;
  684. if (!apic)
  685. return 0;
  686. tpr = (u64) apic_get_reg(apic, APIC_TASKPRI);
  687. return (tpr & 0xf0) >> 4;
  688. }
  689. EXPORT_SYMBOL_GPL(kvm_lapic_get_cr8);
  690. void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
  691. {
  692. struct kvm_lapic *apic = vcpu->arch.apic;
  693. if (!apic) {
  694. value |= MSR_IA32_APICBASE_BSP;
  695. vcpu->arch.apic_base = value;
  696. return;
  697. }
  698. if (apic->vcpu->vcpu_id)
  699. value &= ~MSR_IA32_APICBASE_BSP;
  700. vcpu->arch.apic_base = value;
  701. apic->base_address = apic->vcpu->arch.apic_base &
  702. MSR_IA32_APICBASE_BASE;
  703. /* with FSB delivery interrupt, we can restart APIC functionality */
  704. apic_debug("apic base msr is 0x%016" PRIx64 ", and base address is "
  705. "0x%lx.\n", apic->vcpu->arch.apic_base, apic->base_address);
  706. }
  707. u64 kvm_lapic_get_base(struct kvm_vcpu *vcpu)
  708. {
  709. return vcpu->arch.apic_base;
  710. }
  711. EXPORT_SYMBOL_GPL(kvm_lapic_get_base);
  712. void kvm_lapic_reset(struct kvm_vcpu *vcpu)
  713. {
  714. struct kvm_lapic *apic;
  715. int i;
  716. apic_debug("%s\n", __func__);
  717. ASSERT(vcpu);
  718. apic = vcpu->arch.apic;
  719. ASSERT(apic != NULL);
  720. /* Stop the timer in case it's a reset to an active apic */
  721. hrtimer_cancel(&apic->timer.dev);
  722. apic_set_reg(apic, APIC_ID, vcpu->vcpu_id << 24);
  723. apic_set_reg(apic, APIC_LVR, APIC_VERSION);
  724. for (i = 0; i < APIC_LVT_NUM; i++)
  725. apic_set_reg(apic, APIC_LVTT + 0x10 * i, APIC_LVT_MASKED);
  726. apic_set_reg(apic, APIC_LVT0,
  727. SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
  728. apic_set_reg(apic, APIC_DFR, 0xffffffffU);
  729. apic_set_reg(apic, APIC_SPIV, 0xff);
  730. apic_set_reg(apic, APIC_TASKPRI, 0);
  731. apic_set_reg(apic, APIC_LDR, 0);
  732. apic_set_reg(apic, APIC_ESR, 0);
  733. apic_set_reg(apic, APIC_ICR, 0);
  734. apic_set_reg(apic, APIC_ICR2, 0);
  735. apic_set_reg(apic, APIC_TDCR, 0);
  736. apic_set_reg(apic, APIC_TMICT, 0);
  737. for (i = 0; i < 8; i++) {
  738. apic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
  739. apic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
  740. apic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
  741. }
  742. update_divide_count(apic);
  743. atomic_set(&apic->timer.pending, 0);
  744. if (vcpu->vcpu_id == 0)
  745. vcpu->arch.apic_base |= MSR_IA32_APICBASE_BSP;
  746. apic_update_ppr(apic);
  747. apic_debug(KERN_INFO "%s: vcpu=%p, id=%d, base_msr="
  748. "0x%016" PRIx64 ", base_address=0x%0lx.\n", __func__,
  749. vcpu, kvm_apic_id(apic),
  750. vcpu->arch.apic_base, apic->base_address);
  751. }
  752. EXPORT_SYMBOL_GPL(kvm_lapic_reset);
  753. int kvm_lapic_enabled(struct kvm_vcpu *vcpu)
  754. {
  755. struct kvm_lapic *apic = vcpu->arch.apic;
  756. int ret = 0;
  757. if (!apic)
  758. return 0;
  759. ret = apic_enabled(apic);
  760. return ret;
  761. }
  762. EXPORT_SYMBOL_GPL(kvm_lapic_enabled);
  763. /*
  764. *----------------------------------------------------------------------
  765. * timer interface
  766. *----------------------------------------------------------------------
  767. */
  768. /* TODO: make sure __apic_timer_fn runs in current pCPU */
  769. static int __apic_timer_fn(struct kvm_lapic *apic)
  770. {
  771. int result = 0;
  772. wait_queue_head_t *q = &apic->vcpu->wq;
  773. atomic_inc(&apic->timer.pending);
  774. set_bit(KVM_REQ_PENDING_TIMER, &apic->vcpu->requests);
  775. if (waitqueue_active(q)) {
  776. apic->vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  777. wake_up_interruptible(q);
  778. }
  779. if (apic_lvtt_period(apic)) {
  780. result = 1;
  781. apic->timer.dev.expires = ktime_add_ns(
  782. apic->timer.dev.expires,
  783. apic->timer.period);
  784. }
  785. return result;
  786. }
  787. int apic_has_pending_timer(struct kvm_vcpu *vcpu)
  788. {
  789. struct kvm_lapic *lapic = vcpu->arch.apic;
  790. if (lapic && apic_enabled(lapic) && apic_lvt_enabled(lapic, APIC_LVTT))
  791. return atomic_read(&lapic->timer.pending);
  792. return 0;
  793. }
  794. static int __inject_apic_timer_irq(struct kvm_lapic *apic)
  795. {
  796. int vector;
  797. vector = apic_lvt_vector(apic, APIC_LVTT);
  798. return __apic_accept_irq(apic, APIC_DM_FIXED, vector, 1, 0);
  799. }
  800. static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)
  801. {
  802. struct kvm_lapic *apic;
  803. int restart_timer = 0;
  804. apic = container_of(data, struct kvm_lapic, timer.dev);
  805. restart_timer = __apic_timer_fn(apic);
  806. if (restart_timer)
  807. return HRTIMER_RESTART;
  808. else
  809. return HRTIMER_NORESTART;
  810. }
  811. int kvm_create_lapic(struct kvm_vcpu *vcpu)
  812. {
  813. struct kvm_lapic *apic;
  814. ASSERT(vcpu != NULL);
  815. apic_debug("apic_init %d\n", vcpu->vcpu_id);
  816. apic = kzalloc(sizeof(*apic), GFP_KERNEL);
  817. if (!apic)
  818. goto nomem;
  819. vcpu->arch.apic = apic;
  820. apic->regs_page = alloc_page(GFP_KERNEL);
  821. if (apic->regs_page == NULL) {
  822. printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
  823. vcpu->vcpu_id);
  824. goto nomem_free_apic;
  825. }
  826. apic->regs = page_address(apic->regs_page);
  827. memset(apic->regs, 0, PAGE_SIZE);
  828. apic->vcpu = vcpu;
  829. hrtimer_init(&apic->timer.dev, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  830. apic->timer.dev.function = apic_timer_fn;
  831. apic->base_address = APIC_DEFAULT_PHYS_BASE;
  832. vcpu->arch.apic_base = APIC_DEFAULT_PHYS_BASE;
  833. kvm_lapic_reset(vcpu);
  834. apic->dev.read = apic_mmio_read;
  835. apic->dev.write = apic_mmio_write;
  836. apic->dev.in_range = apic_mmio_range;
  837. apic->dev.private = apic;
  838. return 0;
  839. nomem_free_apic:
  840. kfree(apic);
  841. nomem:
  842. return -ENOMEM;
  843. }
  844. EXPORT_SYMBOL_GPL(kvm_create_lapic);
  845. int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
  846. {
  847. struct kvm_lapic *apic = vcpu->arch.apic;
  848. int highest_irr;
  849. if (!apic || !apic_enabled(apic))
  850. return -1;
  851. apic_update_ppr(apic);
  852. highest_irr = apic_find_highest_irr(apic);
  853. if ((highest_irr == -1) ||
  854. ((highest_irr & 0xF0) <= apic_get_reg(apic, APIC_PROCPRI)))
  855. return -1;
  856. return highest_irr;
  857. }
  858. int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
  859. {
  860. u32 lvt0 = apic_get_reg(vcpu->arch.apic, APIC_LVT0);
  861. int r = 0;
  862. if (vcpu->vcpu_id == 0) {
  863. if (!apic_hw_enabled(vcpu->arch.apic))
  864. r = 1;
  865. if ((lvt0 & APIC_LVT_MASKED) == 0 &&
  866. GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
  867. r = 1;
  868. }
  869. return r;
  870. }
  871. void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
  872. {
  873. struct kvm_lapic *apic = vcpu->arch.apic;
  874. if (apic && apic_lvt_enabled(apic, APIC_LVTT) &&
  875. atomic_read(&apic->timer.pending) > 0) {
  876. if (__inject_apic_timer_irq(apic))
  877. atomic_dec(&apic->timer.pending);
  878. }
  879. }
  880. void kvm_apic_timer_intr_post(struct kvm_vcpu *vcpu, int vec)
  881. {
  882. struct kvm_lapic *apic = vcpu->arch.apic;
  883. if (apic && apic_lvt_vector(apic, APIC_LVTT) == vec)
  884. apic->timer.last_update = ktime_add_ns(
  885. apic->timer.last_update,
  886. apic->timer.period);
  887. }
  888. int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
  889. {
  890. int vector = kvm_apic_has_interrupt(vcpu);
  891. struct kvm_lapic *apic = vcpu->arch.apic;
  892. if (vector == -1)
  893. return -1;
  894. apic_set_vector(vector, apic->regs + APIC_ISR);
  895. apic_update_ppr(apic);
  896. apic_clear_irr(vector, apic);
  897. return vector;
  898. }
  899. void kvm_apic_post_state_restore(struct kvm_vcpu *vcpu)
  900. {
  901. struct kvm_lapic *apic = vcpu->arch.apic;
  902. apic->base_address = vcpu->arch.apic_base &
  903. MSR_IA32_APICBASE_BASE;
  904. apic_set_reg(apic, APIC_LVR, APIC_VERSION);
  905. apic_update_ppr(apic);
  906. hrtimer_cancel(&apic->timer.dev);
  907. update_divide_count(apic);
  908. start_apic_timer(apic);
  909. }
  910. void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
  911. {
  912. struct kvm_lapic *apic = vcpu->arch.apic;
  913. struct hrtimer *timer;
  914. if (!apic)
  915. return;
  916. timer = &apic->timer.dev;
  917. if (hrtimer_cancel(timer))
  918. hrtimer_start(timer, timer->expires, HRTIMER_MODE_ABS);
  919. }
  920. void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu)
  921. {
  922. u32 data;
  923. void *vapic;
  924. if (!irqchip_in_kernel(vcpu->kvm) || !vcpu->arch.apic->vapic_addr)
  925. return;
  926. vapic = kmap_atomic(vcpu->arch.apic->vapic_page, KM_USER0);
  927. data = *(u32 *)(vapic + offset_in_page(vcpu->arch.apic->vapic_addr));
  928. kunmap_atomic(vapic, KM_USER0);
  929. apic_set_tpr(vcpu->arch.apic, data & 0xff);
  930. }
  931. void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu)
  932. {
  933. u32 data, tpr;
  934. int max_irr, max_isr;
  935. struct kvm_lapic *apic;
  936. void *vapic;
  937. if (!irqchip_in_kernel(vcpu->kvm) || !vcpu->arch.apic->vapic_addr)
  938. return;
  939. apic = vcpu->arch.apic;
  940. tpr = apic_get_reg(apic, APIC_TASKPRI) & 0xff;
  941. max_irr = apic_find_highest_irr(apic);
  942. if (max_irr < 0)
  943. max_irr = 0;
  944. max_isr = apic_find_highest_isr(apic);
  945. if (max_isr < 0)
  946. max_isr = 0;
  947. data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24);
  948. vapic = kmap_atomic(vcpu->arch.apic->vapic_page, KM_USER0);
  949. *(u32 *)(vapic + offset_in_page(vcpu->arch.apic->vapic_addr)) = data;
  950. kunmap_atomic(vapic, KM_USER0);
  951. }
  952. void kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr)
  953. {
  954. if (!irqchip_in_kernel(vcpu->kvm))
  955. return;
  956. vcpu->arch.apic->vapic_addr = vapic_addr;
  957. }