lapic.c 26 KB

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