lapic.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445
  1. /*
  2. * Local APIC virtualization
  3. *
  4. * Copyright (C) 2006 Qumranet, Inc.
  5. * Copyright (C) 2007 Novell
  6. * Copyright (C) 2007 Intel
  7. * Copyright 2009 Red Hat, Inc. and/or its affiliates.
  8. *
  9. * Authors:
  10. * Dor Laor <dor.laor@qumranet.com>
  11. * Gregory Haskins <ghaskins@novell.com>
  12. * Yaozu (Eddie) Dong <eddie.dong@intel.com>
  13. *
  14. * Based on Xen 3.1 code, Copyright (c) 2004, Intel Corporation.
  15. *
  16. * This work is licensed under the terms of the GNU GPL, version 2. See
  17. * the COPYING file in the top-level directory.
  18. */
  19. #include <linux/kvm_host.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 <linux/math64.h>
  28. #include <linux/slab.h>
  29. #include <asm/processor.h>
  30. #include <asm/msr.h>
  31. #include <asm/page.h>
  32. #include <asm/current.h>
  33. #include <asm/apicdef.h>
  34. #include <linux/atomic.h>
  35. #include "kvm_cache_regs.h"
  36. #include "irq.h"
  37. #include "trace.h"
  38. #include "x86.h"
  39. #include "cpuid.h"
  40. #ifndef CONFIG_X86_64
  41. #define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
  42. #else
  43. #define mod_64(x, y) ((x) % (y))
  44. #endif
  45. #define PRId64 "d"
  46. #define PRIx64 "llx"
  47. #define PRIu64 "u"
  48. #define PRIo64 "o"
  49. #define APIC_BUS_CYCLE_NS 1
  50. /* #define apic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg) */
  51. #define apic_debug(fmt, arg...)
  52. #define APIC_LVT_NUM 6
  53. /* 14 is the version for Xeon and Pentium 8.4.8*/
  54. #define APIC_VERSION (0x14UL | ((APIC_LVT_NUM - 1) << 16))
  55. #define LAPIC_MMIO_LENGTH (1 << 12)
  56. /* followed define is not in apicdef.h */
  57. #define APIC_SHORT_MASK 0xc0000
  58. #define APIC_DEST_NOSHORT 0x0
  59. #define APIC_DEST_MASK 0x800
  60. #define MAX_APIC_VECTOR 256
  61. #define VEC_POS(v) ((v) & (32 - 1))
  62. #define REG_POS(v) (((v) >> 5) << 4)
  63. static unsigned int min_timer_period_us = 500;
  64. module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR);
  65. static inline u32 apic_get_reg(struct kvm_lapic *apic, int reg_off)
  66. {
  67. return *((u32 *) (apic->regs + reg_off));
  68. }
  69. static inline void apic_set_reg(struct kvm_lapic *apic, int reg_off, u32 val)
  70. {
  71. *((u32 *) (apic->regs + reg_off)) = val;
  72. }
  73. static inline int apic_test_and_set_vector(int vec, void *bitmap)
  74. {
  75. return test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  76. }
  77. static inline int apic_test_and_clear_vector(int vec, void *bitmap)
  78. {
  79. return test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  80. }
  81. static inline int apic_test_vector(int vec, void *bitmap)
  82. {
  83. return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  84. }
  85. static inline void apic_set_vector(int vec, void *bitmap)
  86. {
  87. set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  88. }
  89. static inline void apic_clear_vector(int vec, void *bitmap)
  90. {
  91. clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  92. }
  93. static inline int __apic_test_and_set_vector(int vec, void *bitmap)
  94. {
  95. return __test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  96. }
  97. static inline int __apic_test_and_clear_vector(int vec, void *bitmap)
  98. {
  99. return __test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  100. }
  101. static inline int apic_hw_enabled(struct kvm_lapic *apic)
  102. {
  103. return (apic)->vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE;
  104. }
  105. static inline int apic_sw_enabled(struct kvm_lapic *apic)
  106. {
  107. return apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_APIC_ENABLED;
  108. }
  109. static inline int apic_enabled(struct kvm_lapic *apic)
  110. {
  111. return apic_sw_enabled(apic) && apic_hw_enabled(apic);
  112. }
  113. #define LVT_MASK \
  114. (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
  115. #define LINT_MASK \
  116. (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
  117. APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
  118. static inline int kvm_apic_id(struct kvm_lapic *apic)
  119. {
  120. return (apic_get_reg(apic, APIC_ID) >> 24) & 0xff;
  121. }
  122. static inline int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type)
  123. {
  124. return !(apic_get_reg(apic, lvt_type) & APIC_LVT_MASKED);
  125. }
  126. static inline int apic_lvt_vector(struct kvm_lapic *apic, int lvt_type)
  127. {
  128. return apic_get_reg(apic, lvt_type) & APIC_VECTOR_MASK;
  129. }
  130. static inline int apic_lvtt_oneshot(struct kvm_lapic *apic)
  131. {
  132. return ((apic_get_reg(apic, APIC_LVTT) &
  133. apic->lapic_timer.timer_mode_mask) == APIC_LVT_TIMER_ONESHOT);
  134. }
  135. static inline int apic_lvtt_period(struct kvm_lapic *apic)
  136. {
  137. return ((apic_get_reg(apic, APIC_LVTT) &
  138. apic->lapic_timer.timer_mode_mask) == APIC_LVT_TIMER_PERIODIC);
  139. }
  140. static inline int apic_lvtt_tscdeadline(struct kvm_lapic *apic)
  141. {
  142. return ((apic_get_reg(apic, APIC_LVTT) &
  143. apic->lapic_timer.timer_mode_mask) ==
  144. APIC_LVT_TIMER_TSCDEADLINE);
  145. }
  146. static inline int apic_lvt_nmi_mode(u32 lvt_val)
  147. {
  148. return (lvt_val & (APIC_MODE_MASK | APIC_LVT_MASKED)) == APIC_DM_NMI;
  149. }
  150. void kvm_apic_set_version(struct kvm_vcpu *vcpu)
  151. {
  152. struct kvm_lapic *apic = vcpu->arch.apic;
  153. struct kvm_cpuid_entry2 *feat;
  154. u32 v = APIC_VERSION;
  155. if (!irqchip_in_kernel(vcpu->kvm))
  156. return;
  157. feat = kvm_find_cpuid_entry(apic->vcpu, 0x1, 0);
  158. if (feat && (feat->ecx & (1 << (X86_FEATURE_X2APIC & 31))))
  159. v |= APIC_LVR_DIRECTED_EOI;
  160. apic_set_reg(apic, APIC_LVR, v);
  161. }
  162. static inline int apic_x2apic_mode(struct kvm_lapic *apic)
  163. {
  164. return apic->vcpu->arch.apic_base & X2APIC_ENABLE;
  165. }
  166. static unsigned int apic_lvt_mask[APIC_LVT_NUM] = {
  167. LVT_MASK , /* part LVTT mask, timer mode mask added at runtime */
  168. LVT_MASK | APIC_MODE_MASK, /* LVTTHMR */
  169. LVT_MASK | APIC_MODE_MASK, /* LVTPC */
  170. LINT_MASK, LINT_MASK, /* LVT0-1 */
  171. LVT_MASK /* LVTERR */
  172. };
  173. static int find_highest_vector(void *bitmap)
  174. {
  175. u32 *word = bitmap;
  176. int word_offset = MAX_APIC_VECTOR >> 5;
  177. while ((word_offset != 0) && (word[(--word_offset) << 2] == 0))
  178. continue;
  179. if (likely(!word_offset && !word[0]))
  180. return -1;
  181. else
  182. return fls(word[word_offset << 2]) - 1 + (word_offset << 5);
  183. }
  184. static u8 count_vectors(void *bitmap)
  185. {
  186. u32 *word = bitmap;
  187. int word_offset;
  188. u8 count = 0;
  189. for (word_offset = 0; word_offset < MAX_APIC_VECTOR >> 5; ++word_offset)
  190. count += hweight32(word[word_offset << 2]);
  191. return count;
  192. }
  193. static inline int apic_test_and_set_irr(int vec, struct kvm_lapic *apic)
  194. {
  195. apic->irr_pending = true;
  196. return apic_test_and_set_vector(vec, apic->regs + APIC_IRR);
  197. }
  198. static inline int apic_search_irr(struct kvm_lapic *apic)
  199. {
  200. return find_highest_vector(apic->regs + APIC_IRR);
  201. }
  202. static inline int apic_find_highest_irr(struct kvm_lapic *apic)
  203. {
  204. int result;
  205. if (!apic->irr_pending)
  206. return -1;
  207. result = apic_search_irr(apic);
  208. ASSERT(result == -1 || result >= 16);
  209. return result;
  210. }
  211. static inline void apic_clear_irr(int vec, struct kvm_lapic *apic)
  212. {
  213. apic->irr_pending = false;
  214. apic_clear_vector(vec, apic->regs + APIC_IRR);
  215. if (apic_search_irr(apic) != -1)
  216. apic->irr_pending = true;
  217. }
  218. static inline void apic_set_isr(int vec, struct kvm_lapic *apic)
  219. {
  220. if (!__apic_test_and_set_vector(vec, apic->regs + APIC_ISR))
  221. ++apic->isr_count;
  222. BUG_ON(apic->isr_count > MAX_APIC_VECTOR);
  223. /*
  224. * ISR (in service register) bit is set when injecting an interrupt.
  225. * The highest vector is injected. Thus the latest bit set matches
  226. * the highest bit in ISR.
  227. */
  228. apic->highest_isr_cache = vec;
  229. }
  230. static inline void apic_clear_isr(int vec, struct kvm_lapic *apic)
  231. {
  232. if (__apic_test_and_clear_vector(vec, apic->regs + APIC_ISR))
  233. --apic->isr_count;
  234. BUG_ON(apic->isr_count < 0);
  235. apic->highest_isr_cache = -1;
  236. }
  237. int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu)
  238. {
  239. struct kvm_lapic *apic = vcpu->arch.apic;
  240. int highest_irr;
  241. /* This may race with setting of irr in __apic_accept_irq() and
  242. * value returned may be wrong, but kvm_vcpu_kick() in __apic_accept_irq
  243. * will cause vmexit immediately and the value will be recalculated
  244. * on the next vmentry.
  245. */
  246. if (!apic)
  247. return 0;
  248. highest_irr = apic_find_highest_irr(apic);
  249. return highest_irr;
  250. }
  251. static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
  252. int vector, int level, int trig_mode);
  253. int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq)
  254. {
  255. struct kvm_lapic *apic = vcpu->arch.apic;
  256. return __apic_accept_irq(apic, irq->delivery_mode, irq->vector,
  257. irq->level, irq->trig_mode);
  258. }
  259. static inline int apic_find_highest_isr(struct kvm_lapic *apic)
  260. {
  261. int result;
  262. if (!apic->isr_count)
  263. return -1;
  264. if (likely(apic->highest_isr_cache != -1))
  265. return apic->highest_isr_cache;
  266. result = find_highest_vector(apic->regs + APIC_ISR);
  267. ASSERT(result == -1 || result >= 16);
  268. return result;
  269. }
  270. static void apic_update_ppr(struct kvm_lapic *apic)
  271. {
  272. u32 tpr, isrv, ppr, old_ppr;
  273. int isr;
  274. old_ppr = apic_get_reg(apic, APIC_PROCPRI);
  275. tpr = apic_get_reg(apic, APIC_TASKPRI);
  276. isr = apic_find_highest_isr(apic);
  277. isrv = (isr != -1) ? isr : 0;
  278. if ((tpr & 0xf0) >= (isrv & 0xf0))
  279. ppr = tpr & 0xff;
  280. else
  281. ppr = isrv & 0xf0;
  282. apic_debug("vlapic %p, ppr 0x%x, isr 0x%x, isrv 0x%x",
  283. apic, ppr, isr, isrv);
  284. if (old_ppr != ppr) {
  285. apic_set_reg(apic, APIC_PROCPRI, ppr);
  286. if (ppr < old_ppr)
  287. kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
  288. }
  289. }
  290. static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr)
  291. {
  292. apic_set_reg(apic, APIC_TASKPRI, tpr);
  293. apic_update_ppr(apic);
  294. }
  295. int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest)
  296. {
  297. return dest == 0xff || kvm_apic_id(apic) == dest;
  298. }
  299. int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda)
  300. {
  301. int result = 0;
  302. u32 logical_id;
  303. if (apic_x2apic_mode(apic)) {
  304. logical_id = apic_get_reg(apic, APIC_LDR);
  305. return logical_id & mda;
  306. }
  307. logical_id = GET_APIC_LOGICAL_ID(apic_get_reg(apic, APIC_LDR));
  308. switch (apic_get_reg(apic, APIC_DFR)) {
  309. case APIC_DFR_FLAT:
  310. if (logical_id & mda)
  311. result = 1;
  312. break;
  313. case APIC_DFR_CLUSTER:
  314. if (((logical_id >> 4) == (mda >> 0x4))
  315. && (logical_id & mda & 0xf))
  316. result = 1;
  317. break;
  318. default:
  319. apic_debug("Bad DFR vcpu %d: %08x\n",
  320. apic->vcpu->vcpu_id, apic_get_reg(apic, APIC_DFR));
  321. break;
  322. }
  323. return result;
  324. }
  325. int kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
  326. int short_hand, int dest, int dest_mode)
  327. {
  328. int result = 0;
  329. struct kvm_lapic *target = vcpu->arch.apic;
  330. apic_debug("target %p, source %p, dest 0x%x, "
  331. "dest_mode 0x%x, short_hand 0x%x\n",
  332. target, source, dest, dest_mode, short_hand);
  333. ASSERT(target);
  334. switch (short_hand) {
  335. case APIC_DEST_NOSHORT:
  336. if (dest_mode == 0)
  337. /* Physical mode. */
  338. result = kvm_apic_match_physical_addr(target, dest);
  339. else
  340. /* Logical mode. */
  341. result = kvm_apic_match_logical_addr(target, dest);
  342. break;
  343. case APIC_DEST_SELF:
  344. result = (target == source);
  345. break;
  346. case APIC_DEST_ALLINC:
  347. result = 1;
  348. break;
  349. case APIC_DEST_ALLBUT:
  350. result = (target != source);
  351. break;
  352. default:
  353. apic_debug("kvm: apic: Bad dest shorthand value %x\n",
  354. short_hand);
  355. break;
  356. }
  357. return result;
  358. }
  359. /*
  360. * Add a pending IRQ into lapic.
  361. * Return 1 if successfully added and 0 if discarded.
  362. */
  363. static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
  364. int vector, int level, int trig_mode)
  365. {
  366. int result = 0;
  367. struct kvm_vcpu *vcpu = apic->vcpu;
  368. switch (delivery_mode) {
  369. case APIC_DM_LOWEST:
  370. vcpu->arch.apic_arb_prio++;
  371. case APIC_DM_FIXED:
  372. /* FIXME add logic for vcpu on reset */
  373. if (unlikely(!apic_enabled(apic)))
  374. break;
  375. if (trig_mode) {
  376. apic_debug("level trig mode for vector %d", vector);
  377. apic_set_vector(vector, apic->regs + APIC_TMR);
  378. } else
  379. apic_clear_vector(vector, apic->regs + APIC_TMR);
  380. result = !apic_test_and_set_irr(vector, apic);
  381. trace_kvm_apic_accept_irq(vcpu->vcpu_id, delivery_mode,
  382. trig_mode, vector, !result);
  383. if (!result) {
  384. if (trig_mode)
  385. apic_debug("level trig mode repeatedly for "
  386. "vector %d", vector);
  387. break;
  388. }
  389. kvm_make_request(KVM_REQ_EVENT, vcpu);
  390. kvm_vcpu_kick(vcpu);
  391. break;
  392. case APIC_DM_REMRD:
  393. apic_debug("Ignoring delivery mode 3\n");
  394. break;
  395. case APIC_DM_SMI:
  396. apic_debug("Ignoring guest SMI\n");
  397. break;
  398. case APIC_DM_NMI:
  399. result = 1;
  400. kvm_inject_nmi(vcpu);
  401. kvm_vcpu_kick(vcpu);
  402. break;
  403. case APIC_DM_INIT:
  404. if (!trig_mode || level) {
  405. result = 1;
  406. vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
  407. kvm_make_request(KVM_REQ_EVENT, vcpu);
  408. kvm_vcpu_kick(vcpu);
  409. } else {
  410. apic_debug("Ignoring de-assert INIT to vcpu %d\n",
  411. vcpu->vcpu_id);
  412. }
  413. break;
  414. case APIC_DM_STARTUP:
  415. apic_debug("SIPI to vcpu %d vector 0x%02x\n",
  416. vcpu->vcpu_id, vector);
  417. if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
  418. result = 1;
  419. vcpu->arch.sipi_vector = vector;
  420. vcpu->arch.mp_state = KVM_MP_STATE_SIPI_RECEIVED;
  421. kvm_make_request(KVM_REQ_EVENT, vcpu);
  422. kvm_vcpu_kick(vcpu);
  423. }
  424. break;
  425. case APIC_DM_EXTINT:
  426. /*
  427. * Should only be called by kvm_apic_local_deliver() with LVT0,
  428. * before NMI watchdog was enabled. Already handled by
  429. * kvm_apic_accept_pic_intr().
  430. */
  431. break;
  432. default:
  433. printk(KERN_ERR "TODO: unsupported delivery mode %x\n",
  434. delivery_mode);
  435. break;
  436. }
  437. return result;
  438. }
  439. int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
  440. {
  441. return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
  442. }
  443. static void apic_set_eoi(struct kvm_lapic *apic)
  444. {
  445. int vector = apic_find_highest_isr(apic);
  446. /*
  447. * Not every write EOI will has corresponding ISR,
  448. * one example is when Kernel check timer on setup_IO_APIC
  449. */
  450. if (vector == -1)
  451. return;
  452. apic_clear_isr(vector, apic);
  453. apic_update_ppr(apic);
  454. if (!(apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI) &&
  455. kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) {
  456. int trigger_mode;
  457. if (apic_test_vector(vector, apic->regs + APIC_TMR))
  458. trigger_mode = IOAPIC_LEVEL_TRIG;
  459. else
  460. trigger_mode = IOAPIC_EDGE_TRIG;
  461. kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
  462. }
  463. kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
  464. }
  465. static void apic_send_ipi(struct kvm_lapic *apic)
  466. {
  467. u32 icr_low = apic_get_reg(apic, APIC_ICR);
  468. u32 icr_high = apic_get_reg(apic, APIC_ICR2);
  469. struct kvm_lapic_irq irq;
  470. irq.vector = icr_low & APIC_VECTOR_MASK;
  471. irq.delivery_mode = icr_low & APIC_MODE_MASK;
  472. irq.dest_mode = icr_low & APIC_DEST_MASK;
  473. irq.level = icr_low & APIC_INT_ASSERT;
  474. irq.trig_mode = icr_low & APIC_INT_LEVELTRIG;
  475. irq.shorthand = icr_low & APIC_SHORT_MASK;
  476. if (apic_x2apic_mode(apic))
  477. irq.dest_id = icr_high;
  478. else
  479. irq.dest_id = GET_APIC_DEST_FIELD(icr_high);
  480. trace_kvm_apic_ipi(icr_low, irq.dest_id);
  481. apic_debug("icr_high 0x%x, icr_low 0x%x, "
  482. "short_hand 0x%x, dest 0x%x, trig_mode 0x%x, level 0x%x, "
  483. "dest_mode 0x%x, delivery_mode 0x%x, vector 0x%x\n",
  484. icr_high, icr_low, irq.shorthand, irq.dest_id,
  485. irq.trig_mode, irq.level, irq.dest_mode, irq.delivery_mode,
  486. irq.vector);
  487. kvm_irq_delivery_to_apic(apic->vcpu->kvm, apic, &irq);
  488. }
  489. static u32 apic_get_tmcct(struct kvm_lapic *apic)
  490. {
  491. ktime_t remaining;
  492. s64 ns;
  493. u32 tmcct;
  494. ASSERT(apic != NULL);
  495. /* if initial count is 0, current count should also be 0 */
  496. if (apic_get_reg(apic, APIC_TMICT) == 0)
  497. return 0;
  498. remaining = hrtimer_get_remaining(&apic->lapic_timer.timer);
  499. if (ktime_to_ns(remaining) < 0)
  500. remaining = ktime_set(0, 0);
  501. ns = mod_64(ktime_to_ns(remaining), apic->lapic_timer.period);
  502. tmcct = div64_u64(ns,
  503. (APIC_BUS_CYCLE_NS * apic->divide_count));
  504. return tmcct;
  505. }
  506. static void __report_tpr_access(struct kvm_lapic *apic, bool write)
  507. {
  508. struct kvm_vcpu *vcpu = apic->vcpu;
  509. struct kvm_run *run = vcpu->run;
  510. kvm_make_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu);
  511. run->tpr_access.rip = kvm_rip_read(vcpu);
  512. run->tpr_access.is_write = write;
  513. }
  514. static inline void report_tpr_access(struct kvm_lapic *apic, bool write)
  515. {
  516. if (apic->vcpu->arch.tpr_access_reporting)
  517. __report_tpr_access(apic, write);
  518. }
  519. static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset)
  520. {
  521. u32 val = 0;
  522. if (offset >= LAPIC_MMIO_LENGTH)
  523. return 0;
  524. switch (offset) {
  525. case APIC_ID:
  526. if (apic_x2apic_mode(apic))
  527. val = kvm_apic_id(apic);
  528. else
  529. val = kvm_apic_id(apic) << 24;
  530. break;
  531. case APIC_ARBPRI:
  532. apic_debug("Access APIC ARBPRI register which is for P6\n");
  533. break;
  534. case APIC_TMCCT: /* Timer CCR */
  535. if (apic_lvtt_tscdeadline(apic))
  536. return 0;
  537. val = apic_get_tmcct(apic);
  538. break;
  539. case APIC_TASKPRI:
  540. report_tpr_access(apic, false);
  541. /* fall thru */
  542. default:
  543. apic_update_ppr(apic);
  544. val = apic_get_reg(apic, offset);
  545. break;
  546. }
  547. return val;
  548. }
  549. static inline struct kvm_lapic *to_lapic(struct kvm_io_device *dev)
  550. {
  551. return container_of(dev, struct kvm_lapic, dev);
  552. }
  553. static int apic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
  554. void *data)
  555. {
  556. unsigned char alignment = offset & 0xf;
  557. u32 result;
  558. /* this bitmask has a bit cleared for each reserver register */
  559. static const u64 rmask = 0x43ff01ffffffe70cULL;
  560. if ((alignment + len) > 4) {
  561. apic_debug("KVM_APIC_READ: alignment error %x %d\n",
  562. offset, len);
  563. return 1;
  564. }
  565. if (offset > 0x3f0 || !(rmask & (1ULL << (offset >> 4)))) {
  566. apic_debug("KVM_APIC_READ: read reserved register %x\n",
  567. offset);
  568. return 1;
  569. }
  570. result = __apic_read(apic, offset & ~0xf);
  571. trace_kvm_apic_read(offset, result);
  572. switch (len) {
  573. case 1:
  574. case 2:
  575. case 4:
  576. memcpy(data, (char *)&result + alignment, len);
  577. break;
  578. default:
  579. printk(KERN_ERR "Local APIC read with len = %x, "
  580. "should be 1,2, or 4 instead\n", len);
  581. break;
  582. }
  583. return 0;
  584. }
  585. static int apic_mmio_in_range(struct kvm_lapic *apic, gpa_t addr)
  586. {
  587. return apic_hw_enabled(apic) &&
  588. addr >= apic->base_address &&
  589. addr < apic->base_address + LAPIC_MMIO_LENGTH;
  590. }
  591. static int apic_mmio_read(struct kvm_io_device *this,
  592. gpa_t address, int len, void *data)
  593. {
  594. struct kvm_lapic *apic = to_lapic(this);
  595. u32 offset = address - apic->base_address;
  596. if (!apic_mmio_in_range(apic, address))
  597. return -EOPNOTSUPP;
  598. apic_reg_read(apic, offset, len, data);
  599. return 0;
  600. }
  601. static void update_divide_count(struct kvm_lapic *apic)
  602. {
  603. u32 tmp1, tmp2, tdcr;
  604. tdcr = apic_get_reg(apic, APIC_TDCR);
  605. tmp1 = tdcr & 0xf;
  606. tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
  607. apic->divide_count = 0x1 << (tmp2 & 0x7);
  608. apic_debug("timer divide count is 0x%x\n",
  609. apic->divide_count);
  610. }
  611. static void start_apic_timer(struct kvm_lapic *apic)
  612. {
  613. ktime_t now;
  614. atomic_set(&apic->lapic_timer.pending, 0);
  615. if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic)) {
  616. /* lapic timer in oneshot or peroidic mode */
  617. now = apic->lapic_timer.timer.base->get_time();
  618. apic->lapic_timer.period = (u64)apic_get_reg(apic, APIC_TMICT)
  619. * APIC_BUS_CYCLE_NS * apic->divide_count;
  620. if (!apic->lapic_timer.period)
  621. return;
  622. /*
  623. * Do not allow the guest to program periodic timers with small
  624. * interval, since the hrtimers are not throttled by the host
  625. * scheduler.
  626. */
  627. if (apic_lvtt_period(apic)) {
  628. s64 min_period = min_timer_period_us * 1000LL;
  629. if (apic->lapic_timer.period < min_period) {
  630. pr_info_ratelimited(
  631. "kvm: vcpu %i: requested %lld ns "
  632. "lapic timer period limited to %lld ns\n",
  633. apic->vcpu->vcpu_id,
  634. apic->lapic_timer.period, min_period);
  635. apic->lapic_timer.period = min_period;
  636. }
  637. }
  638. hrtimer_start(&apic->lapic_timer.timer,
  639. ktime_add_ns(now, apic->lapic_timer.period),
  640. HRTIMER_MODE_ABS);
  641. apic_debug("%s: bus cycle is %" PRId64 "ns, now 0x%016"
  642. PRIx64 ", "
  643. "timer initial count 0x%x, period %lldns, "
  644. "expire @ 0x%016" PRIx64 ".\n", __func__,
  645. APIC_BUS_CYCLE_NS, ktime_to_ns(now),
  646. apic_get_reg(apic, APIC_TMICT),
  647. apic->lapic_timer.period,
  648. ktime_to_ns(ktime_add_ns(now,
  649. apic->lapic_timer.period)));
  650. } else if (apic_lvtt_tscdeadline(apic)) {
  651. /* lapic timer in tsc deadline mode */
  652. u64 guest_tsc, tscdeadline = apic->lapic_timer.tscdeadline;
  653. u64 ns = 0;
  654. struct kvm_vcpu *vcpu = apic->vcpu;
  655. unsigned long this_tsc_khz = vcpu->arch.virtual_tsc_khz;
  656. unsigned long flags;
  657. if (unlikely(!tscdeadline || !this_tsc_khz))
  658. return;
  659. local_irq_save(flags);
  660. now = apic->lapic_timer.timer.base->get_time();
  661. guest_tsc = kvm_x86_ops->read_l1_tsc(vcpu);
  662. if (likely(tscdeadline > guest_tsc)) {
  663. ns = (tscdeadline - guest_tsc) * 1000000ULL;
  664. do_div(ns, this_tsc_khz);
  665. }
  666. hrtimer_start(&apic->lapic_timer.timer,
  667. ktime_add_ns(now, ns), HRTIMER_MODE_ABS);
  668. local_irq_restore(flags);
  669. }
  670. }
  671. static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val)
  672. {
  673. int nmi_wd_enabled = apic_lvt_nmi_mode(apic_get_reg(apic, APIC_LVT0));
  674. if (apic_lvt_nmi_mode(lvt0_val)) {
  675. if (!nmi_wd_enabled) {
  676. apic_debug("Receive NMI setting on APIC_LVT0 "
  677. "for cpu %d\n", apic->vcpu->vcpu_id);
  678. apic->vcpu->kvm->arch.vapics_in_nmi_mode++;
  679. }
  680. } else if (nmi_wd_enabled)
  681. apic->vcpu->kvm->arch.vapics_in_nmi_mode--;
  682. }
  683. static int apic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
  684. {
  685. int ret = 0;
  686. trace_kvm_apic_write(reg, val);
  687. switch (reg) {
  688. case APIC_ID: /* Local APIC ID */
  689. if (!apic_x2apic_mode(apic))
  690. apic_set_reg(apic, APIC_ID, val);
  691. else
  692. ret = 1;
  693. break;
  694. case APIC_TASKPRI:
  695. report_tpr_access(apic, true);
  696. apic_set_tpr(apic, val & 0xff);
  697. break;
  698. case APIC_EOI:
  699. apic_set_eoi(apic);
  700. break;
  701. case APIC_LDR:
  702. if (!apic_x2apic_mode(apic))
  703. apic_set_reg(apic, APIC_LDR, val & APIC_LDR_MASK);
  704. else
  705. ret = 1;
  706. break;
  707. case APIC_DFR:
  708. if (!apic_x2apic_mode(apic))
  709. apic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF);
  710. else
  711. ret = 1;
  712. break;
  713. case APIC_SPIV: {
  714. u32 mask = 0x3ff;
  715. if (apic_get_reg(apic, APIC_LVR) & APIC_LVR_DIRECTED_EOI)
  716. mask |= APIC_SPIV_DIRECTED_EOI;
  717. apic_set_reg(apic, APIC_SPIV, val & mask);
  718. if (!(val & APIC_SPIV_APIC_ENABLED)) {
  719. int i;
  720. u32 lvt_val;
  721. for (i = 0; i < APIC_LVT_NUM; i++) {
  722. lvt_val = apic_get_reg(apic,
  723. APIC_LVTT + 0x10 * i);
  724. apic_set_reg(apic, APIC_LVTT + 0x10 * i,
  725. lvt_val | APIC_LVT_MASKED);
  726. }
  727. atomic_set(&apic->lapic_timer.pending, 0);
  728. }
  729. break;
  730. }
  731. case APIC_ICR:
  732. /* No delay here, so we always clear the pending bit */
  733. apic_set_reg(apic, APIC_ICR, val & ~(1 << 12));
  734. apic_send_ipi(apic);
  735. break;
  736. case APIC_ICR2:
  737. if (!apic_x2apic_mode(apic))
  738. val &= 0xff000000;
  739. apic_set_reg(apic, APIC_ICR2, val);
  740. break;
  741. case APIC_LVT0:
  742. apic_manage_nmi_watchdog(apic, val);
  743. case APIC_LVTTHMR:
  744. case APIC_LVTPC:
  745. case APIC_LVT1:
  746. case APIC_LVTERR:
  747. /* TODO: Check vector */
  748. if (!apic_sw_enabled(apic))
  749. val |= APIC_LVT_MASKED;
  750. val &= apic_lvt_mask[(reg - APIC_LVTT) >> 4];
  751. apic_set_reg(apic, reg, val);
  752. break;
  753. case APIC_LVTT:
  754. if ((apic_get_reg(apic, APIC_LVTT) &
  755. apic->lapic_timer.timer_mode_mask) !=
  756. (val & apic->lapic_timer.timer_mode_mask))
  757. hrtimer_cancel(&apic->lapic_timer.timer);
  758. if (!apic_sw_enabled(apic))
  759. val |= APIC_LVT_MASKED;
  760. val &= (apic_lvt_mask[0] | apic->lapic_timer.timer_mode_mask);
  761. apic_set_reg(apic, APIC_LVTT, val);
  762. break;
  763. case APIC_TMICT:
  764. if (apic_lvtt_tscdeadline(apic))
  765. break;
  766. hrtimer_cancel(&apic->lapic_timer.timer);
  767. apic_set_reg(apic, APIC_TMICT, val);
  768. start_apic_timer(apic);
  769. break;
  770. case APIC_TDCR:
  771. if (val & 4)
  772. apic_debug("KVM_WRITE:TDCR %x\n", val);
  773. apic_set_reg(apic, APIC_TDCR, val);
  774. update_divide_count(apic);
  775. break;
  776. case APIC_ESR:
  777. if (apic_x2apic_mode(apic) && val != 0) {
  778. apic_debug("KVM_WRITE:ESR not zero %x\n", val);
  779. ret = 1;
  780. }
  781. break;
  782. case APIC_SELF_IPI:
  783. if (apic_x2apic_mode(apic)) {
  784. apic_reg_write(apic, APIC_ICR, 0x40000 | (val & 0xff));
  785. } else
  786. ret = 1;
  787. break;
  788. default:
  789. ret = 1;
  790. break;
  791. }
  792. if (ret)
  793. apic_debug("Local APIC Write to read-only register %x\n", reg);
  794. return ret;
  795. }
  796. static int apic_mmio_write(struct kvm_io_device *this,
  797. gpa_t address, int len, const void *data)
  798. {
  799. struct kvm_lapic *apic = to_lapic(this);
  800. unsigned int offset = address - apic->base_address;
  801. u32 val;
  802. if (!apic_mmio_in_range(apic, address))
  803. return -EOPNOTSUPP;
  804. /*
  805. * APIC register must be aligned on 128-bits boundary.
  806. * 32/64/128 bits registers must be accessed thru 32 bits.
  807. * Refer SDM 8.4.1
  808. */
  809. if (len != 4 || (offset & 0xf)) {
  810. /* Don't shout loud, $infamous_os would cause only noise. */
  811. apic_debug("apic write: bad size=%d %lx\n", len, (long)address);
  812. return 0;
  813. }
  814. val = *(u32*)data;
  815. /* too common printing */
  816. if (offset != APIC_EOI)
  817. apic_debug("%s: offset 0x%x with length 0x%x, and value is "
  818. "0x%x\n", __func__, offset, len, val);
  819. apic_reg_write(apic, offset & 0xff0, val);
  820. return 0;
  821. }
  822. void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu)
  823. {
  824. struct kvm_lapic *apic = vcpu->arch.apic;
  825. if (apic)
  826. apic_reg_write(vcpu->arch.apic, APIC_EOI, 0);
  827. }
  828. EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi);
  829. void kvm_free_lapic(struct kvm_vcpu *vcpu)
  830. {
  831. if (!vcpu->arch.apic)
  832. return;
  833. hrtimer_cancel(&vcpu->arch.apic->lapic_timer.timer);
  834. if (vcpu->arch.apic->regs)
  835. free_page((unsigned long)vcpu->arch.apic->regs);
  836. kfree(vcpu->arch.apic);
  837. }
  838. /*
  839. *----------------------------------------------------------------------
  840. * LAPIC interface
  841. *----------------------------------------------------------------------
  842. */
  843. u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu)
  844. {
  845. struct kvm_lapic *apic = vcpu->arch.apic;
  846. if (!apic)
  847. return 0;
  848. if (apic_lvtt_oneshot(apic) || apic_lvtt_period(apic))
  849. return 0;
  850. return apic->lapic_timer.tscdeadline;
  851. }
  852. void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data)
  853. {
  854. struct kvm_lapic *apic = vcpu->arch.apic;
  855. if (!apic)
  856. return;
  857. if (apic_lvtt_oneshot(apic) || apic_lvtt_period(apic))
  858. return;
  859. hrtimer_cancel(&apic->lapic_timer.timer);
  860. apic->lapic_timer.tscdeadline = data;
  861. start_apic_timer(apic);
  862. }
  863. void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
  864. {
  865. struct kvm_lapic *apic = vcpu->arch.apic;
  866. if (!apic)
  867. return;
  868. apic_set_tpr(apic, ((cr8 & 0x0f) << 4)
  869. | (apic_get_reg(apic, APIC_TASKPRI) & 4));
  870. }
  871. u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
  872. {
  873. struct kvm_lapic *apic = vcpu->arch.apic;
  874. u64 tpr;
  875. if (!apic)
  876. return 0;
  877. tpr = (u64) apic_get_reg(apic, APIC_TASKPRI);
  878. return (tpr & 0xf0) >> 4;
  879. }
  880. void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
  881. {
  882. struct kvm_lapic *apic = vcpu->arch.apic;
  883. if (!apic) {
  884. value |= MSR_IA32_APICBASE_BSP;
  885. vcpu->arch.apic_base = value;
  886. return;
  887. }
  888. if (!kvm_vcpu_is_bsp(apic->vcpu))
  889. value &= ~MSR_IA32_APICBASE_BSP;
  890. vcpu->arch.apic_base = value;
  891. if (apic_x2apic_mode(apic)) {
  892. u32 id = kvm_apic_id(apic);
  893. u32 ldr = ((id & ~0xf) << 16) | (1 << (id & 0xf));
  894. apic_set_reg(apic, APIC_LDR, ldr);
  895. }
  896. apic->base_address = apic->vcpu->arch.apic_base &
  897. MSR_IA32_APICBASE_BASE;
  898. /* with FSB delivery interrupt, we can restart APIC functionality */
  899. apic_debug("apic base msr is 0x%016" PRIx64 ", and base address is "
  900. "0x%lx.\n", apic->vcpu->arch.apic_base, apic->base_address);
  901. }
  902. void kvm_lapic_reset(struct kvm_vcpu *vcpu)
  903. {
  904. struct kvm_lapic *apic;
  905. int i;
  906. apic_debug("%s\n", __func__);
  907. ASSERT(vcpu);
  908. apic = vcpu->arch.apic;
  909. ASSERT(apic != NULL);
  910. /* Stop the timer in case it's a reset to an active apic */
  911. hrtimer_cancel(&apic->lapic_timer.timer);
  912. apic_set_reg(apic, APIC_ID, vcpu->vcpu_id << 24);
  913. kvm_apic_set_version(apic->vcpu);
  914. for (i = 0; i < APIC_LVT_NUM; i++)
  915. apic_set_reg(apic, APIC_LVTT + 0x10 * i, APIC_LVT_MASKED);
  916. apic_set_reg(apic, APIC_LVT0,
  917. SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
  918. apic_set_reg(apic, APIC_DFR, 0xffffffffU);
  919. apic_set_reg(apic, APIC_SPIV, 0xff);
  920. apic_set_reg(apic, APIC_TASKPRI, 0);
  921. apic_set_reg(apic, APIC_LDR, 0);
  922. apic_set_reg(apic, APIC_ESR, 0);
  923. apic_set_reg(apic, APIC_ICR, 0);
  924. apic_set_reg(apic, APIC_ICR2, 0);
  925. apic_set_reg(apic, APIC_TDCR, 0);
  926. apic_set_reg(apic, APIC_TMICT, 0);
  927. for (i = 0; i < 8; i++) {
  928. apic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
  929. apic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
  930. apic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
  931. }
  932. apic->irr_pending = false;
  933. apic->isr_count = 0;
  934. apic->highest_isr_cache = -1;
  935. update_divide_count(apic);
  936. atomic_set(&apic->lapic_timer.pending, 0);
  937. if (kvm_vcpu_is_bsp(vcpu))
  938. vcpu->arch.apic_base |= MSR_IA32_APICBASE_BSP;
  939. apic_update_ppr(apic);
  940. vcpu->arch.apic_arb_prio = 0;
  941. vcpu->arch.apic_attention = 0;
  942. apic_debug(KERN_INFO "%s: vcpu=%p, id=%d, base_msr="
  943. "0x%016" PRIx64 ", base_address=0x%0lx.\n", __func__,
  944. vcpu, kvm_apic_id(apic),
  945. vcpu->arch.apic_base, apic->base_address);
  946. }
  947. bool kvm_apic_present(struct kvm_vcpu *vcpu)
  948. {
  949. return vcpu->arch.apic && apic_hw_enabled(vcpu->arch.apic);
  950. }
  951. int kvm_lapic_enabled(struct kvm_vcpu *vcpu)
  952. {
  953. return kvm_apic_present(vcpu) && apic_sw_enabled(vcpu->arch.apic);
  954. }
  955. /*
  956. *----------------------------------------------------------------------
  957. * timer interface
  958. *----------------------------------------------------------------------
  959. */
  960. static bool lapic_is_periodic(struct kvm_timer *ktimer)
  961. {
  962. struct kvm_lapic *apic = container_of(ktimer, struct kvm_lapic,
  963. lapic_timer);
  964. return apic_lvtt_period(apic);
  965. }
  966. int apic_has_pending_timer(struct kvm_vcpu *vcpu)
  967. {
  968. struct kvm_lapic *lapic = vcpu->arch.apic;
  969. if (lapic && apic_enabled(lapic) && apic_lvt_enabled(lapic, APIC_LVTT))
  970. return atomic_read(&lapic->lapic_timer.pending);
  971. return 0;
  972. }
  973. int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type)
  974. {
  975. u32 reg = apic_get_reg(apic, lvt_type);
  976. int vector, mode, trig_mode;
  977. if (apic_hw_enabled(apic) && !(reg & APIC_LVT_MASKED)) {
  978. vector = reg & APIC_VECTOR_MASK;
  979. mode = reg & APIC_MODE_MASK;
  980. trig_mode = reg & APIC_LVT_LEVEL_TRIGGER;
  981. return __apic_accept_irq(apic, mode, vector, 1, trig_mode);
  982. }
  983. return 0;
  984. }
  985. void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu)
  986. {
  987. struct kvm_lapic *apic = vcpu->arch.apic;
  988. if (apic)
  989. kvm_apic_local_deliver(apic, APIC_LVT0);
  990. }
  991. static struct kvm_timer_ops lapic_timer_ops = {
  992. .is_periodic = lapic_is_periodic,
  993. };
  994. static const struct kvm_io_device_ops apic_mmio_ops = {
  995. .read = apic_mmio_read,
  996. .write = apic_mmio_write,
  997. };
  998. int kvm_create_lapic(struct kvm_vcpu *vcpu)
  999. {
  1000. struct kvm_lapic *apic;
  1001. ASSERT(vcpu != NULL);
  1002. apic_debug("apic_init %d\n", vcpu->vcpu_id);
  1003. apic = kzalloc(sizeof(*apic), GFP_KERNEL);
  1004. if (!apic)
  1005. goto nomem;
  1006. vcpu->arch.apic = apic;
  1007. apic->regs = (void *)get_zeroed_page(GFP_KERNEL);
  1008. if (!apic->regs) {
  1009. printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
  1010. vcpu->vcpu_id);
  1011. goto nomem_free_apic;
  1012. }
  1013. apic->vcpu = vcpu;
  1014. hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
  1015. HRTIMER_MODE_ABS);
  1016. apic->lapic_timer.timer.function = kvm_timer_fn;
  1017. apic->lapic_timer.t_ops = &lapic_timer_ops;
  1018. apic->lapic_timer.kvm = vcpu->kvm;
  1019. apic->lapic_timer.vcpu = vcpu;
  1020. apic->base_address = APIC_DEFAULT_PHYS_BASE;
  1021. vcpu->arch.apic_base = APIC_DEFAULT_PHYS_BASE;
  1022. kvm_lapic_reset(vcpu);
  1023. kvm_iodevice_init(&apic->dev, &apic_mmio_ops);
  1024. return 0;
  1025. nomem_free_apic:
  1026. kfree(apic);
  1027. nomem:
  1028. return -ENOMEM;
  1029. }
  1030. int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
  1031. {
  1032. struct kvm_lapic *apic = vcpu->arch.apic;
  1033. int highest_irr;
  1034. if (!apic || !apic_enabled(apic))
  1035. return -1;
  1036. apic_update_ppr(apic);
  1037. highest_irr = apic_find_highest_irr(apic);
  1038. if ((highest_irr == -1) ||
  1039. ((highest_irr & 0xF0) <= apic_get_reg(apic, APIC_PROCPRI)))
  1040. return -1;
  1041. return highest_irr;
  1042. }
  1043. int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
  1044. {
  1045. u32 lvt0 = apic_get_reg(vcpu->arch.apic, APIC_LVT0);
  1046. int r = 0;
  1047. if (!apic_hw_enabled(vcpu->arch.apic))
  1048. r = 1;
  1049. if ((lvt0 & APIC_LVT_MASKED) == 0 &&
  1050. GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
  1051. r = 1;
  1052. return r;
  1053. }
  1054. void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
  1055. {
  1056. struct kvm_lapic *apic = vcpu->arch.apic;
  1057. if (apic && atomic_read(&apic->lapic_timer.pending) > 0) {
  1058. if (kvm_apic_local_deliver(apic, APIC_LVTT))
  1059. atomic_dec(&apic->lapic_timer.pending);
  1060. }
  1061. }
  1062. int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
  1063. {
  1064. int vector = kvm_apic_has_interrupt(vcpu);
  1065. struct kvm_lapic *apic = vcpu->arch.apic;
  1066. if (vector == -1)
  1067. return -1;
  1068. apic_set_isr(vector, apic);
  1069. apic_update_ppr(apic);
  1070. apic_clear_irr(vector, apic);
  1071. return vector;
  1072. }
  1073. void kvm_apic_post_state_restore(struct kvm_vcpu *vcpu)
  1074. {
  1075. struct kvm_lapic *apic = vcpu->arch.apic;
  1076. apic->base_address = vcpu->arch.apic_base &
  1077. MSR_IA32_APICBASE_BASE;
  1078. kvm_apic_set_version(vcpu);
  1079. apic_update_ppr(apic);
  1080. hrtimer_cancel(&apic->lapic_timer.timer);
  1081. update_divide_count(apic);
  1082. start_apic_timer(apic);
  1083. apic->irr_pending = true;
  1084. apic->isr_count = count_vectors(apic->regs + APIC_ISR);
  1085. apic->highest_isr_cache = -1;
  1086. kvm_make_request(KVM_REQ_EVENT, vcpu);
  1087. }
  1088. void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
  1089. {
  1090. struct kvm_lapic *apic = vcpu->arch.apic;
  1091. struct hrtimer *timer;
  1092. if (!apic)
  1093. return;
  1094. timer = &apic->lapic_timer.timer;
  1095. if (hrtimer_cancel(timer))
  1096. hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
  1097. }
  1098. void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu)
  1099. {
  1100. u32 data;
  1101. void *vapic;
  1102. if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
  1103. return;
  1104. vapic = kmap_atomic(vcpu->arch.apic->vapic_page);
  1105. data = *(u32 *)(vapic + offset_in_page(vcpu->arch.apic->vapic_addr));
  1106. kunmap_atomic(vapic);
  1107. apic_set_tpr(vcpu->arch.apic, data & 0xff);
  1108. }
  1109. void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu)
  1110. {
  1111. u32 data, tpr;
  1112. int max_irr, max_isr;
  1113. struct kvm_lapic *apic;
  1114. void *vapic;
  1115. if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
  1116. return;
  1117. apic = vcpu->arch.apic;
  1118. tpr = apic_get_reg(apic, APIC_TASKPRI) & 0xff;
  1119. max_irr = apic_find_highest_irr(apic);
  1120. if (max_irr < 0)
  1121. max_irr = 0;
  1122. max_isr = apic_find_highest_isr(apic);
  1123. if (max_isr < 0)
  1124. max_isr = 0;
  1125. data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24);
  1126. vapic = kmap_atomic(vcpu->arch.apic->vapic_page);
  1127. *(u32 *)(vapic + offset_in_page(vcpu->arch.apic->vapic_addr)) = data;
  1128. kunmap_atomic(vapic);
  1129. }
  1130. void kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr)
  1131. {
  1132. vcpu->arch.apic->vapic_addr = vapic_addr;
  1133. if (vapic_addr)
  1134. __set_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
  1135. else
  1136. __clear_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
  1137. }
  1138. int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  1139. {
  1140. struct kvm_lapic *apic = vcpu->arch.apic;
  1141. u32 reg = (msr - APIC_BASE_MSR) << 4;
  1142. if (!irqchip_in_kernel(vcpu->kvm) || !apic_x2apic_mode(apic))
  1143. return 1;
  1144. /* if this is ICR write vector before command */
  1145. if (msr == 0x830)
  1146. apic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
  1147. return apic_reg_write(apic, reg, (u32)data);
  1148. }
  1149. int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
  1150. {
  1151. struct kvm_lapic *apic = vcpu->arch.apic;
  1152. u32 reg = (msr - APIC_BASE_MSR) << 4, low, high = 0;
  1153. if (!irqchip_in_kernel(vcpu->kvm) || !apic_x2apic_mode(apic))
  1154. return 1;
  1155. if (apic_reg_read(apic, reg, 4, &low))
  1156. return 1;
  1157. if (msr == 0x830)
  1158. apic_reg_read(apic, APIC_ICR2, 4, &high);
  1159. *data = (((u64)high) << 32) | low;
  1160. return 0;
  1161. }
  1162. int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 reg, u64 data)
  1163. {
  1164. struct kvm_lapic *apic = vcpu->arch.apic;
  1165. if (!irqchip_in_kernel(vcpu->kvm))
  1166. return 1;
  1167. /* if this is ICR write vector before command */
  1168. if (reg == APIC_ICR)
  1169. apic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
  1170. return apic_reg_write(apic, reg, (u32)data);
  1171. }
  1172. int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 reg, u64 *data)
  1173. {
  1174. struct kvm_lapic *apic = vcpu->arch.apic;
  1175. u32 low, high = 0;
  1176. if (!irqchip_in_kernel(vcpu->kvm))
  1177. return 1;
  1178. if (apic_reg_read(apic, reg, 4, &low))
  1179. return 1;
  1180. if (reg == APIC_ICR)
  1181. apic_reg_read(apic, APIC_ICR2, 4, &high);
  1182. *data = (((u64)high) << 32) | low;
  1183. return 0;
  1184. }