lapic.c 28 KB

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