lapic.c 40 KB

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