lapic.c 25 KB

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