lapic.c 27 KB

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