lapic.c 24 KB

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