lapic.c 25 KB

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