lapic.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243
  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/export.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 <asm/delay.h>
  35. #include <linux/atomic.h>
  36. #include <linux/jump_label.h>
  37. #include "kvm_cache_regs.h"
  38. #include "irq.h"
  39. #include "trace.h"
  40. #include "x86.h"
  41. #include "cpuid.h"
  42. #include "hyperv.h"
  43. #ifndef CONFIG_X86_64
  44. #define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
  45. #else
  46. #define mod_64(x, y) ((x) % (y))
  47. #endif
  48. #define PRId64 "d"
  49. #define PRIx64 "llx"
  50. #define PRIu64 "u"
  51. #define PRIo64 "o"
  52. #define APIC_BUS_CYCLE_NS 1
  53. /* #define apic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg) */
  54. #define apic_debug(fmt, arg...)
  55. /* 14 is the version for Xeon and Pentium 8.4.8*/
  56. #define APIC_VERSION (0x14UL | ((KVM_APIC_LVT_NUM - 1) << 16))
  57. #define LAPIC_MMIO_LENGTH (1 << 12)
  58. /* followed define is not in apicdef.h */
  59. #define APIC_SHORT_MASK 0xc0000
  60. #define APIC_DEST_NOSHORT 0x0
  61. #define APIC_DEST_MASK 0x800
  62. #define MAX_APIC_VECTOR 256
  63. #define APIC_VECTORS_PER_REG 32
  64. #define APIC_BROADCAST 0xFF
  65. #define X2APIC_BROADCAST 0xFFFFFFFFul
  66. static inline int apic_test_vector(int vec, void *bitmap)
  67. {
  68. return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  69. }
  70. bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
  71. {
  72. struct kvm_lapic *apic = vcpu->arch.apic;
  73. return apic_test_vector(vector, apic->regs + APIC_ISR) ||
  74. apic_test_vector(vector, apic->regs + APIC_IRR);
  75. }
  76. static inline void apic_clear_vector(int vec, void *bitmap)
  77. {
  78. clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  79. }
  80. static inline int __apic_test_and_set_vector(int vec, void *bitmap)
  81. {
  82. return __test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  83. }
  84. static inline int __apic_test_and_clear_vector(int vec, void *bitmap)
  85. {
  86. return __test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  87. }
  88. struct static_key_deferred apic_hw_disabled __read_mostly;
  89. struct static_key_deferred apic_sw_disabled __read_mostly;
  90. static inline int apic_enabled(struct kvm_lapic *apic)
  91. {
  92. return kvm_apic_sw_enabled(apic) && kvm_apic_hw_enabled(apic);
  93. }
  94. #define LVT_MASK \
  95. (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
  96. #define LINT_MASK \
  97. (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
  98. APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
  99. /* The logical map is definitely wrong if we have multiple
  100. * modes at the same time. (Physical map is always right.)
  101. */
  102. static inline bool kvm_apic_logical_map_valid(struct kvm_apic_map *map)
  103. {
  104. return !(map->mode & (map->mode - 1));
  105. }
  106. static inline void
  107. apic_logical_id(struct kvm_apic_map *map, u32 dest_id, u16 *cid, u16 *lid)
  108. {
  109. unsigned lid_bits;
  110. BUILD_BUG_ON(KVM_APIC_MODE_XAPIC_CLUSTER != 4);
  111. BUILD_BUG_ON(KVM_APIC_MODE_XAPIC_FLAT != 8);
  112. BUILD_BUG_ON(KVM_APIC_MODE_X2APIC != 16);
  113. lid_bits = map->mode;
  114. *cid = dest_id >> lid_bits;
  115. *lid = dest_id & ((1 << lid_bits) - 1);
  116. }
  117. static void recalculate_apic_map(struct kvm *kvm)
  118. {
  119. struct kvm_apic_map *new, *old = NULL;
  120. struct kvm_vcpu *vcpu;
  121. int i;
  122. new = kzalloc(sizeof(struct kvm_apic_map), GFP_KERNEL);
  123. mutex_lock(&kvm->arch.apic_map_lock);
  124. if (!new)
  125. goto out;
  126. kvm_for_each_vcpu(i, vcpu, kvm) {
  127. struct kvm_lapic *apic = vcpu->arch.apic;
  128. u16 cid, lid;
  129. u32 ldr, aid;
  130. if (!kvm_apic_present(vcpu))
  131. continue;
  132. aid = kvm_apic_id(apic);
  133. ldr = kvm_lapic_get_reg(apic, APIC_LDR);
  134. if (aid < ARRAY_SIZE(new->phys_map))
  135. new->phys_map[aid] = apic;
  136. if (apic_x2apic_mode(apic)) {
  137. new->mode |= KVM_APIC_MODE_X2APIC;
  138. } else if (ldr) {
  139. ldr = GET_APIC_LOGICAL_ID(ldr);
  140. if (kvm_lapic_get_reg(apic, APIC_DFR) == APIC_DFR_FLAT)
  141. new->mode |= KVM_APIC_MODE_XAPIC_FLAT;
  142. else
  143. new->mode |= KVM_APIC_MODE_XAPIC_CLUSTER;
  144. }
  145. if (!kvm_apic_logical_map_valid(new))
  146. continue;
  147. apic_logical_id(new, ldr, &cid, &lid);
  148. if (lid && cid < ARRAY_SIZE(new->logical_map))
  149. new->logical_map[cid][ffs(lid) - 1] = apic;
  150. }
  151. out:
  152. old = rcu_dereference_protected(kvm->arch.apic_map,
  153. lockdep_is_held(&kvm->arch.apic_map_lock));
  154. rcu_assign_pointer(kvm->arch.apic_map, new);
  155. mutex_unlock(&kvm->arch.apic_map_lock);
  156. if (old)
  157. kfree_rcu(old, rcu);
  158. kvm_make_scan_ioapic_request(kvm);
  159. }
  160. static inline void apic_set_spiv(struct kvm_lapic *apic, u32 val)
  161. {
  162. bool enabled = val & APIC_SPIV_APIC_ENABLED;
  163. kvm_lapic_set_reg(apic, APIC_SPIV, val);
  164. if (enabled != apic->sw_enabled) {
  165. apic->sw_enabled = enabled;
  166. if (enabled) {
  167. static_key_slow_dec_deferred(&apic_sw_disabled);
  168. recalculate_apic_map(apic->vcpu->kvm);
  169. } else
  170. static_key_slow_inc(&apic_sw_disabled.key);
  171. }
  172. }
  173. static inline void kvm_apic_set_id(struct kvm_lapic *apic, u8 id)
  174. {
  175. kvm_lapic_set_reg(apic, APIC_ID, id << 24);
  176. recalculate_apic_map(apic->vcpu->kvm);
  177. }
  178. static inline void kvm_apic_set_ldr(struct kvm_lapic *apic, u32 id)
  179. {
  180. kvm_lapic_set_reg(apic, APIC_LDR, id);
  181. recalculate_apic_map(apic->vcpu->kvm);
  182. }
  183. static inline void kvm_apic_set_x2apic_id(struct kvm_lapic *apic, u8 id)
  184. {
  185. u32 ldr = ((id >> 4) << 16) | (1 << (id & 0xf));
  186. kvm_lapic_set_reg(apic, APIC_ID, id << 24);
  187. kvm_lapic_set_reg(apic, APIC_LDR, ldr);
  188. recalculate_apic_map(apic->vcpu->kvm);
  189. }
  190. static inline int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type)
  191. {
  192. return !(kvm_lapic_get_reg(apic, lvt_type) & APIC_LVT_MASKED);
  193. }
  194. static inline int apic_lvt_vector(struct kvm_lapic *apic, int lvt_type)
  195. {
  196. return kvm_lapic_get_reg(apic, lvt_type) & APIC_VECTOR_MASK;
  197. }
  198. static inline int apic_lvtt_oneshot(struct kvm_lapic *apic)
  199. {
  200. return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_ONESHOT;
  201. }
  202. static inline int apic_lvtt_period(struct kvm_lapic *apic)
  203. {
  204. return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_PERIODIC;
  205. }
  206. static inline int apic_lvtt_tscdeadline(struct kvm_lapic *apic)
  207. {
  208. return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_TSCDEADLINE;
  209. }
  210. static inline int apic_lvt_nmi_mode(u32 lvt_val)
  211. {
  212. return (lvt_val & (APIC_MODE_MASK | APIC_LVT_MASKED)) == APIC_DM_NMI;
  213. }
  214. void kvm_apic_set_version(struct kvm_vcpu *vcpu)
  215. {
  216. struct kvm_lapic *apic = vcpu->arch.apic;
  217. struct kvm_cpuid_entry2 *feat;
  218. u32 v = APIC_VERSION;
  219. if (!lapic_in_kernel(vcpu))
  220. return;
  221. feat = kvm_find_cpuid_entry(apic->vcpu, 0x1, 0);
  222. if (feat && (feat->ecx & (1 << (X86_FEATURE_X2APIC & 31))))
  223. v |= APIC_LVR_DIRECTED_EOI;
  224. kvm_lapic_set_reg(apic, APIC_LVR, v);
  225. }
  226. static const unsigned int apic_lvt_mask[KVM_APIC_LVT_NUM] = {
  227. LVT_MASK , /* part LVTT mask, timer mode mask added at runtime */
  228. LVT_MASK | APIC_MODE_MASK, /* LVTTHMR */
  229. LVT_MASK | APIC_MODE_MASK, /* LVTPC */
  230. LINT_MASK, LINT_MASK, /* LVT0-1 */
  231. LVT_MASK /* LVTERR */
  232. };
  233. static int find_highest_vector(void *bitmap)
  234. {
  235. int vec;
  236. u32 *reg;
  237. for (vec = MAX_APIC_VECTOR - APIC_VECTORS_PER_REG;
  238. vec >= 0; vec -= APIC_VECTORS_PER_REG) {
  239. reg = bitmap + REG_POS(vec);
  240. if (*reg)
  241. return fls(*reg) - 1 + vec;
  242. }
  243. return -1;
  244. }
  245. static u8 count_vectors(void *bitmap)
  246. {
  247. int vec;
  248. u32 *reg;
  249. u8 count = 0;
  250. for (vec = 0; vec < MAX_APIC_VECTOR; vec += APIC_VECTORS_PER_REG) {
  251. reg = bitmap + REG_POS(vec);
  252. count += hweight32(*reg);
  253. }
  254. return count;
  255. }
  256. void __kvm_apic_update_irr(u32 *pir, void *regs)
  257. {
  258. u32 i, pir_val;
  259. for (i = 0; i <= 7; i++) {
  260. pir_val = xchg(&pir[i], 0);
  261. if (pir_val)
  262. *((u32 *)(regs + APIC_IRR + i * 0x10)) |= pir_val;
  263. }
  264. }
  265. EXPORT_SYMBOL_GPL(__kvm_apic_update_irr);
  266. void kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir)
  267. {
  268. struct kvm_lapic *apic = vcpu->arch.apic;
  269. __kvm_apic_update_irr(pir, apic->regs);
  270. kvm_make_request(KVM_REQ_EVENT, vcpu);
  271. }
  272. EXPORT_SYMBOL_GPL(kvm_apic_update_irr);
  273. static inline int apic_search_irr(struct kvm_lapic *apic)
  274. {
  275. return find_highest_vector(apic->regs + APIC_IRR);
  276. }
  277. static inline int apic_find_highest_irr(struct kvm_lapic *apic)
  278. {
  279. int result;
  280. /*
  281. * Note that irr_pending is just a hint. It will be always
  282. * true with virtual interrupt delivery enabled.
  283. */
  284. if (!apic->irr_pending)
  285. return -1;
  286. if (apic->vcpu->arch.apicv_active)
  287. kvm_x86_ops->sync_pir_to_irr(apic->vcpu);
  288. result = apic_search_irr(apic);
  289. ASSERT(result == -1 || result >= 16);
  290. return result;
  291. }
  292. static inline void apic_clear_irr(int vec, struct kvm_lapic *apic)
  293. {
  294. struct kvm_vcpu *vcpu;
  295. vcpu = apic->vcpu;
  296. if (unlikely(vcpu->arch.apicv_active)) {
  297. /* try to update RVI */
  298. apic_clear_vector(vec, apic->regs + APIC_IRR);
  299. kvm_make_request(KVM_REQ_EVENT, vcpu);
  300. } else {
  301. apic->irr_pending = false;
  302. apic_clear_vector(vec, apic->regs + APIC_IRR);
  303. if (apic_search_irr(apic) != -1)
  304. apic->irr_pending = true;
  305. }
  306. }
  307. static inline void apic_set_isr(int vec, struct kvm_lapic *apic)
  308. {
  309. struct kvm_vcpu *vcpu;
  310. if (__apic_test_and_set_vector(vec, apic->regs + APIC_ISR))
  311. return;
  312. vcpu = apic->vcpu;
  313. /*
  314. * With APIC virtualization enabled, all caching is disabled
  315. * because the processor can modify ISR under the hood. Instead
  316. * just set SVI.
  317. */
  318. if (unlikely(vcpu->arch.apicv_active))
  319. kvm_x86_ops->hwapic_isr_update(vcpu, vec);
  320. else {
  321. ++apic->isr_count;
  322. BUG_ON(apic->isr_count > MAX_APIC_VECTOR);
  323. /*
  324. * ISR (in service register) bit is set when injecting an interrupt.
  325. * The highest vector is injected. Thus the latest bit set matches
  326. * the highest bit in ISR.
  327. */
  328. apic->highest_isr_cache = vec;
  329. }
  330. }
  331. static inline int apic_find_highest_isr(struct kvm_lapic *apic)
  332. {
  333. int result;
  334. /*
  335. * Note that isr_count is always 1, and highest_isr_cache
  336. * is always -1, with APIC virtualization enabled.
  337. */
  338. if (!apic->isr_count)
  339. return -1;
  340. if (likely(apic->highest_isr_cache != -1))
  341. return apic->highest_isr_cache;
  342. result = find_highest_vector(apic->regs + APIC_ISR);
  343. ASSERT(result == -1 || result >= 16);
  344. return result;
  345. }
  346. static inline void apic_clear_isr(int vec, struct kvm_lapic *apic)
  347. {
  348. struct kvm_vcpu *vcpu;
  349. if (!__apic_test_and_clear_vector(vec, apic->regs + APIC_ISR))
  350. return;
  351. vcpu = apic->vcpu;
  352. /*
  353. * We do get here for APIC virtualization enabled if the guest
  354. * uses the Hyper-V APIC enlightenment. In this case we may need
  355. * to trigger a new interrupt delivery by writing the SVI field;
  356. * on the other hand isr_count and highest_isr_cache are unused
  357. * and must be left alone.
  358. */
  359. if (unlikely(vcpu->arch.apicv_active))
  360. kvm_x86_ops->hwapic_isr_update(vcpu,
  361. apic_find_highest_isr(apic));
  362. else {
  363. --apic->isr_count;
  364. BUG_ON(apic->isr_count < 0);
  365. apic->highest_isr_cache = -1;
  366. }
  367. }
  368. int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu)
  369. {
  370. /* This may race with setting of irr in __apic_accept_irq() and
  371. * value returned may be wrong, but kvm_vcpu_kick() in __apic_accept_irq
  372. * will cause vmexit immediately and the value will be recalculated
  373. * on the next vmentry.
  374. */
  375. return apic_find_highest_irr(vcpu->arch.apic);
  376. }
  377. static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
  378. int vector, int level, int trig_mode,
  379. struct dest_map *dest_map);
  380. int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq,
  381. struct dest_map *dest_map)
  382. {
  383. struct kvm_lapic *apic = vcpu->arch.apic;
  384. return __apic_accept_irq(apic, irq->delivery_mode, irq->vector,
  385. irq->level, irq->trig_mode, dest_map);
  386. }
  387. static int pv_eoi_put_user(struct kvm_vcpu *vcpu, u8 val)
  388. {
  389. return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, &val,
  390. sizeof(val));
  391. }
  392. static int pv_eoi_get_user(struct kvm_vcpu *vcpu, u8 *val)
  393. {
  394. return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, val,
  395. sizeof(*val));
  396. }
  397. static inline bool pv_eoi_enabled(struct kvm_vcpu *vcpu)
  398. {
  399. return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED;
  400. }
  401. static bool pv_eoi_get_pending(struct kvm_vcpu *vcpu)
  402. {
  403. u8 val;
  404. if (pv_eoi_get_user(vcpu, &val) < 0)
  405. apic_debug("Can't read EOI MSR value: 0x%llx\n",
  406. (unsigned long long)vcpu->arch.pv_eoi.msr_val);
  407. return val & 0x1;
  408. }
  409. static void pv_eoi_set_pending(struct kvm_vcpu *vcpu)
  410. {
  411. if (pv_eoi_put_user(vcpu, KVM_PV_EOI_ENABLED) < 0) {
  412. apic_debug("Can't set EOI MSR value: 0x%llx\n",
  413. (unsigned long long)vcpu->arch.pv_eoi.msr_val);
  414. return;
  415. }
  416. __set_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
  417. }
  418. static void pv_eoi_clr_pending(struct kvm_vcpu *vcpu)
  419. {
  420. if (pv_eoi_put_user(vcpu, KVM_PV_EOI_DISABLED) < 0) {
  421. apic_debug("Can't clear EOI MSR value: 0x%llx\n",
  422. (unsigned long long)vcpu->arch.pv_eoi.msr_val);
  423. return;
  424. }
  425. __clear_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
  426. }
  427. static void apic_update_ppr(struct kvm_lapic *apic)
  428. {
  429. u32 tpr, isrv, ppr, old_ppr;
  430. int isr;
  431. old_ppr = kvm_lapic_get_reg(apic, APIC_PROCPRI);
  432. tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI);
  433. isr = apic_find_highest_isr(apic);
  434. isrv = (isr != -1) ? isr : 0;
  435. if ((tpr & 0xf0) >= (isrv & 0xf0))
  436. ppr = tpr & 0xff;
  437. else
  438. ppr = isrv & 0xf0;
  439. apic_debug("vlapic %p, ppr 0x%x, isr 0x%x, isrv 0x%x",
  440. apic, ppr, isr, isrv);
  441. if (old_ppr != ppr) {
  442. kvm_lapic_set_reg(apic, APIC_PROCPRI, ppr);
  443. if (ppr < old_ppr)
  444. kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
  445. }
  446. }
  447. static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr)
  448. {
  449. kvm_lapic_set_reg(apic, APIC_TASKPRI, tpr);
  450. apic_update_ppr(apic);
  451. }
  452. static bool kvm_apic_broadcast(struct kvm_lapic *apic, u32 mda)
  453. {
  454. if (apic_x2apic_mode(apic))
  455. return mda == X2APIC_BROADCAST;
  456. return GET_APIC_DEST_FIELD(mda) == APIC_BROADCAST;
  457. }
  458. static bool kvm_apic_match_physical_addr(struct kvm_lapic *apic, u32 mda)
  459. {
  460. if (kvm_apic_broadcast(apic, mda))
  461. return true;
  462. if (apic_x2apic_mode(apic))
  463. return mda == kvm_apic_id(apic);
  464. return mda == SET_APIC_DEST_FIELD(kvm_apic_id(apic));
  465. }
  466. static bool kvm_apic_match_logical_addr(struct kvm_lapic *apic, u32 mda)
  467. {
  468. u32 logical_id;
  469. if (kvm_apic_broadcast(apic, mda))
  470. return true;
  471. logical_id = kvm_lapic_get_reg(apic, APIC_LDR);
  472. if (apic_x2apic_mode(apic))
  473. return ((logical_id >> 16) == (mda >> 16))
  474. && (logical_id & mda & 0xffff) != 0;
  475. logical_id = GET_APIC_LOGICAL_ID(logical_id);
  476. mda = GET_APIC_DEST_FIELD(mda);
  477. switch (kvm_lapic_get_reg(apic, APIC_DFR)) {
  478. case APIC_DFR_FLAT:
  479. return (logical_id & mda) != 0;
  480. case APIC_DFR_CLUSTER:
  481. return ((logical_id >> 4) == (mda >> 4))
  482. && (logical_id & mda & 0xf) != 0;
  483. default:
  484. apic_debug("Bad DFR vcpu %d: %08x\n",
  485. apic->vcpu->vcpu_id, kvm_lapic_get_reg(apic, APIC_DFR));
  486. return false;
  487. }
  488. }
  489. /* KVM APIC implementation has two quirks
  490. * - dest always begins at 0 while xAPIC MDA has offset 24,
  491. * - IOxAPIC messages have to be delivered (directly) to x2APIC.
  492. */
  493. static u32 kvm_apic_mda(unsigned int dest_id, struct kvm_lapic *source,
  494. struct kvm_lapic *target)
  495. {
  496. bool ipi = source != NULL;
  497. bool x2apic_mda = apic_x2apic_mode(ipi ? source : target);
  498. if (!ipi && dest_id == APIC_BROADCAST && x2apic_mda)
  499. return X2APIC_BROADCAST;
  500. return x2apic_mda ? dest_id : SET_APIC_DEST_FIELD(dest_id);
  501. }
  502. bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
  503. int short_hand, unsigned int dest, int dest_mode)
  504. {
  505. struct kvm_lapic *target = vcpu->arch.apic;
  506. u32 mda = kvm_apic_mda(dest, source, target);
  507. apic_debug("target %p, source %p, dest 0x%x, "
  508. "dest_mode 0x%x, short_hand 0x%x\n",
  509. target, source, dest, dest_mode, short_hand);
  510. ASSERT(target);
  511. switch (short_hand) {
  512. case APIC_DEST_NOSHORT:
  513. if (dest_mode == APIC_DEST_PHYSICAL)
  514. return kvm_apic_match_physical_addr(target, mda);
  515. else
  516. return kvm_apic_match_logical_addr(target, mda);
  517. case APIC_DEST_SELF:
  518. return target == source;
  519. case APIC_DEST_ALLINC:
  520. return true;
  521. case APIC_DEST_ALLBUT:
  522. return target != source;
  523. default:
  524. apic_debug("kvm: apic: Bad dest shorthand value %x\n",
  525. short_hand);
  526. return false;
  527. }
  528. }
  529. EXPORT_SYMBOL_GPL(kvm_apic_match_dest);
  530. int kvm_vector_to_index(u32 vector, u32 dest_vcpus,
  531. const unsigned long *bitmap, u32 bitmap_size)
  532. {
  533. u32 mod;
  534. int i, idx = -1;
  535. mod = vector % dest_vcpus;
  536. for (i = 0; i <= mod; i++) {
  537. idx = find_next_bit(bitmap, bitmap_size, idx + 1);
  538. BUG_ON(idx == bitmap_size);
  539. }
  540. return idx;
  541. }
  542. static void kvm_apic_disabled_lapic_found(struct kvm *kvm)
  543. {
  544. if (!kvm->arch.disabled_lapic_found) {
  545. kvm->arch.disabled_lapic_found = true;
  546. printk(KERN_INFO
  547. "Disabled LAPIC found during irq injection\n");
  548. }
  549. }
  550. bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
  551. struct kvm_lapic_irq *irq, int *r, struct dest_map *dest_map)
  552. {
  553. struct kvm_apic_map *map;
  554. unsigned long bitmap = 1;
  555. struct kvm_lapic **dst;
  556. int i;
  557. bool ret, x2apic_ipi;
  558. *r = -1;
  559. if (irq->shorthand == APIC_DEST_SELF) {
  560. *r = kvm_apic_set_irq(src->vcpu, irq, dest_map);
  561. return true;
  562. }
  563. if (irq->shorthand)
  564. return false;
  565. x2apic_ipi = src && apic_x2apic_mode(src);
  566. if (irq->dest_id == (x2apic_ipi ? X2APIC_BROADCAST : APIC_BROADCAST))
  567. return false;
  568. ret = true;
  569. rcu_read_lock();
  570. map = rcu_dereference(kvm->arch.apic_map);
  571. if (!map) {
  572. ret = false;
  573. goto out;
  574. }
  575. if (irq->dest_mode == APIC_DEST_PHYSICAL) {
  576. if (irq->dest_id >= ARRAY_SIZE(map->phys_map))
  577. goto out;
  578. dst = &map->phys_map[irq->dest_id];
  579. } else {
  580. u16 cid;
  581. if (!kvm_apic_logical_map_valid(map)) {
  582. ret = false;
  583. goto out;
  584. }
  585. apic_logical_id(map, irq->dest_id, &cid, (u16 *)&bitmap);
  586. if (cid >= ARRAY_SIZE(map->logical_map))
  587. goto out;
  588. dst = map->logical_map[cid];
  589. if (!kvm_lowest_prio_delivery(irq))
  590. goto set_irq;
  591. if (!kvm_vector_hashing_enabled()) {
  592. int l = -1;
  593. for_each_set_bit(i, &bitmap, 16) {
  594. if (!dst[i])
  595. continue;
  596. if (l < 0)
  597. l = i;
  598. else if (kvm_apic_compare_prio(dst[i]->vcpu,
  599. dst[l]->vcpu) < 0)
  600. l = i;
  601. }
  602. bitmap = (l >= 0) ? 1 << l : 0;
  603. } else {
  604. int idx;
  605. unsigned int dest_vcpus;
  606. dest_vcpus = hweight16(bitmap);
  607. if (dest_vcpus == 0)
  608. goto out;
  609. idx = kvm_vector_to_index(irq->vector,
  610. dest_vcpus, &bitmap, 16);
  611. if (!dst[idx]) {
  612. kvm_apic_disabled_lapic_found(kvm);
  613. goto out;
  614. }
  615. bitmap = (idx >= 0) ? 1 << idx : 0;
  616. }
  617. }
  618. set_irq:
  619. for_each_set_bit(i, &bitmap, 16) {
  620. if (!dst[i])
  621. continue;
  622. if (*r < 0)
  623. *r = 0;
  624. *r += kvm_apic_set_irq(dst[i]->vcpu, irq, dest_map);
  625. }
  626. out:
  627. rcu_read_unlock();
  628. return ret;
  629. }
  630. /*
  631. * This routine tries to handler interrupts in posted mode, here is how
  632. * it deals with different cases:
  633. * - For single-destination interrupts, handle it in posted mode
  634. * - Else if vector hashing is enabled and it is a lowest-priority
  635. * interrupt, handle it in posted mode and use the following mechanism
  636. * to find the destinaiton vCPU.
  637. * 1. For lowest-priority interrupts, store all the possible
  638. * destination vCPUs in an array.
  639. * 2. Use "guest vector % max number of destination vCPUs" to find
  640. * the right destination vCPU in the array for the lowest-priority
  641. * interrupt.
  642. * - Otherwise, use remapped mode to inject the interrupt.
  643. */
  644. bool kvm_intr_is_single_vcpu_fast(struct kvm *kvm, struct kvm_lapic_irq *irq,
  645. struct kvm_vcpu **dest_vcpu)
  646. {
  647. struct kvm_apic_map *map;
  648. bool ret = false;
  649. struct kvm_lapic *dst = NULL;
  650. if (irq->shorthand)
  651. return false;
  652. rcu_read_lock();
  653. map = rcu_dereference(kvm->arch.apic_map);
  654. if (!map)
  655. goto out;
  656. if (irq->dest_mode == APIC_DEST_PHYSICAL) {
  657. if (irq->dest_id == 0xFF)
  658. goto out;
  659. if (irq->dest_id >= ARRAY_SIZE(map->phys_map))
  660. goto out;
  661. dst = map->phys_map[irq->dest_id];
  662. if (dst && kvm_apic_present(dst->vcpu))
  663. *dest_vcpu = dst->vcpu;
  664. else
  665. goto out;
  666. } else {
  667. u16 cid;
  668. unsigned long bitmap = 1;
  669. int i, r = 0;
  670. if (!kvm_apic_logical_map_valid(map))
  671. goto out;
  672. apic_logical_id(map, irq->dest_id, &cid, (u16 *)&bitmap);
  673. if (cid >= ARRAY_SIZE(map->logical_map))
  674. goto out;
  675. if (kvm_vector_hashing_enabled() &&
  676. kvm_lowest_prio_delivery(irq)) {
  677. int idx;
  678. unsigned int dest_vcpus;
  679. dest_vcpus = hweight16(bitmap);
  680. if (dest_vcpus == 0)
  681. goto out;
  682. idx = kvm_vector_to_index(irq->vector, dest_vcpus,
  683. &bitmap, 16);
  684. dst = map->logical_map[cid][idx];
  685. if (!dst) {
  686. kvm_apic_disabled_lapic_found(kvm);
  687. goto out;
  688. }
  689. *dest_vcpu = dst->vcpu;
  690. } else {
  691. for_each_set_bit(i, &bitmap, 16) {
  692. dst = map->logical_map[cid][i];
  693. if (++r == 2)
  694. goto out;
  695. }
  696. if (dst && kvm_apic_present(dst->vcpu))
  697. *dest_vcpu = dst->vcpu;
  698. else
  699. goto out;
  700. }
  701. }
  702. ret = true;
  703. out:
  704. rcu_read_unlock();
  705. return ret;
  706. }
  707. /*
  708. * Add a pending IRQ into lapic.
  709. * Return 1 if successfully added and 0 if discarded.
  710. */
  711. static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
  712. int vector, int level, int trig_mode,
  713. struct dest_map *dest_map)
  714. {
  715. int result = 0;
  716. struct kvm_vcpu *vcpu = apic->vcpu;
  717. trace_kvm_apic_accept_irq(vcpu->vcpu_id, delivery_mode,
  718. trig_mode, vector);
  719. switch (delivery_mode) {
  720. case APIC_DM_LOWEST:
  721. vcpu->arch.apic_arb_prio++;
  722. case APIC_DM_FIXED:
  723. if (unlikely(trig_mode && !level))
  724. break;
  725. /* FIXME add logic for vcpu on reset */
  726. if (unlikely(!apic_enabled(apic)))
  727. break;
  728. result = 1;
  729. if (dest_map) {
  730. __set_bit(vcpu->vcpu_id, dest_map->map);
  731. dest_map->vectors[vcpu->vcpu_id] = vector;
  732. }
  733. if (apic_test_vector(vector, apic->regs + APIC_TMR) != !!trig_mode) {
  734. if (trig_mode)
  735. kvm_lapic_set_vector(vector, apic->regs + APIC_TMR);
  736. else
  737. apic_clear_vector(vector, apic->regs + APIC_TMR);
  738. }
  739. if (vcpu->arch.apicv_active)
  740. kvm_x86_ops->deliver_posted_interrupt(vcpu, vector);
  741. else {
  742. kvm_lapic_set_irr(vector, apic);
  743. kvm_make_request(KVM_REQ_EVENT, vcpu);
  744. kvm_vcpu_kick(vcpu);
  745. }
  746. break;
  747. case APIC_DM_REMRD:
  748. result = 1;
  749. vcpu->arch.pv.pv_unhalted = 1;
  750. kvm_make_request(KVM_REQ_EVENT, vcpu);
  751. kvm_vcpu_kick(vcpu);
  752. break;
  753. case APIC_DM_SMI:
  754. result = 1;
  755. kvm_make_request(KVM_REQ_SMI, vcpu);
  756. kvm_vcpu_kick(vcpu);
  757. break;
  758. case APIC_DM_NMI:
  759. result = 1;
  760. kvm_inject_nmi(vcpu);
  761. kvm_vcpu_kick(vcpu);
  762. break;
  763. case APIC_DM_INIT:
  764. if (!trig_mode || level) {
  765. result = 1;
  766. /* assumes that there are only KVM_APIC_INIT/SIPI */
  767. apic->pending_events = (1UL << KVM_APIC_INIT);
  768. /* make sure pending_events is visible before sending
  769. * the request */
  770. smp_wmb();
  771. kvm_make_request(KVM_REQ_EVENT, vcpu);
  772. kvm_vcpu_kick(vcpu);
  773. } else {
  774. apic_debug("Ignoring de-assert INIT to vcpu %d\n",
  775. vcpu->vcpu_id);
  776. }
  777. break;
  778. case APIC_DM_STARTUP:
  779. apic_debug("SIPI to vcpu %d vector 0x%02x\n",
  780. vcpu->vcpu_id, vector);
  781. result = 1;
  782. apic->sipi_vector = vector;
  783. /* make sure sipi_vector is visible for the receiver */
  784. smp_wmb();
  785. set_bit(KVM_APIC_SIPI, &apic->pending_events);
  786. kvm_make_request(KVM_REQ_EVENT, vcpu);
  787. kvm_vcpu_kick(vcpu);
  788. break;
  789. case APIC_DM_EXTINT:
  790. /*
  791. * Should only be called by kvm_apic_local_deliver() with LVT0,
  792. * before NMI watchdog was enabled. Already handled by
  793. * kvm_apic_accept_pic_intr().
  794. */
  795. break;
  796. default:
  797. printk(KERN_ERR "TODO: unsupported delivery mode %x\n",
  798. delivery_mode);
  799. break;
  800. }
  801. return result;
  802. }
  803. int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
  804. {
  805. return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
  806. }
  807. static bool kvm_ioapic_handles_vector(struct kvm_lapic *apic, int vector)
  808. {
  809. return test_bit(vector, apic->vcpu->arch.ioapic_handled_vectors);
  810. }
  811. static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
  812. {
  813. int trigger_mode;
  814. /* Eoi the ioapic only if the ioapic doesn't own the vector. */
  815. if (!kvm_ioapic_handles_vector(apic, vector))
  816. return;
  817. /* Request a KVM exit to inform the userspace IOAPIC. */
  818. if (irqchip_split(apic->vcpu->kvm)) {
  819. apic->vcpu->arch.pending_ioapic_eoi = vector;
  820. kvm_make_request(KVM_REQ_IOAPIC_EOI_EXIT, apic->vcpu);
  821. return;
  822. }
  823. if (apic_test_vector(vector, apic->regs + APIC_TMR))
  824. trigger_mode = IOAPIC_LEVEL_TRIG;
  825. else
  826. trigger_mode = IOAPIC_EDGE_TRIG;
  827. kvm_ioapic_update_eoi(apic->vcpu, vector, trigger_mode);
  828. }
  829. static int apic_set_eoi(struct kvm_lapic *apic)
  830. {
  831. int vector = apic_find_highest_isr(apic);
  832. trace_kvm_eoi(apic, vector);
  833. /*
  834. * Not every write EOI will has corresponding ISR,
  835. * one example is when Kernel check timer on setup_IO_APIC
  836. */
  837. if (vector == -1)
  838. return vector;
  839. apic_clear_isr(vector, apic);
  840. apic_update_ppr(apic);
  841. if (test_bit(vector, vcpu_to_synic(apic->vcpu)->vec_bitmap))
  842. kvm_hv_synic_send_eoi(apic->vcpu, vector);
  843. kvm_ioapic_send_eoi(apic, vector);
  844. kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
  845. return vector;
  846. }
  847. /*
  848. * this interface assumes a trap-like exit, which has already finished
  849. * desired side effect including vISR and vPPR update.
  850. */
  851. void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
  852. {
  853. struct kvm_lapic *apic = vcpu->arch.apic;
  854. trace_kvm_eoi(apic, vector);
  855. kvm_ioapic_send_eoi(apic, vector);
  856. kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
  857. }
  858. EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
  859. static void apic_send_ipi(struct kvm_lapic *apic)
  860. {
  861. u32 icr_low = kvm_lapic_get_reg(apic, APIC_ICR);
  862. u32 icr_high = kvm_lapic_get_reg(apic, APIC_ICR2);
  863. struct kvm_lapic_irq irq;
  864. irq.vector = icr_low & APIC_VECTOR_MASK;
  865. irq.delivery_mode = icr_low & APIC_MODE_MASK;
  866. irq.dest_mode = icr_low & APIC_DEST_MASK;
  867. irq.level = (icr_low & APIC_INT_ASSERT) != 0;
  868. irq.trig_mode = icr_low & APIC_INT_LEVELTRIG;
  869. irq.shorthand = icr_low & APIC_SHORT_MASK;
  870. irq.msi_redir_hint = false;
  871. if (apic_x2apic_mode(apic))
  872. irq.dest_id = icr_high;
  873. else
  874. irq.dest_id = GET_APIC_DEST_FIELD(icr_high);
  875. trace_kvm_apic_ipi(icr_low, irq.dest_id);
  876. apic_debug("icr_high 0x%x, icr_low 0x%x, "
  877. "short_hand 0x%x, dest 0x%x, trig_mode 0x%x, level 0x%x, "
  878. "dest_mode 0x%x, delivery_mode 0x%x, vector 0x%x, "
  879. "msi_redir_hint 0x%x\n",
  880. icr_high, icr_low, irq.shorthand, irq.dest_id,
  881. irq.trig_mode, irq.level, irq.dest_mode, irq.delivery_mode,
  882. irq.vector, irq.msi_redir_hint);
  883. kvm_irq_delivery_to_apic(apic->vcpu->kvm, apic, &irq, NULL);
  884. }
  885. static u32 apic_get_tmcct(struct kvm_lapic *apic)
  886. {
  887. ktime_t remaining;
  888. s64 ns;
  889. u32 tmcct;
  890. ASSERT(apic != NULL);
  891. /* if initial count is 0, current count should also be 0 */
  892. if (kvm_lapic_get_reg(apic, APIC_TMICT) == 0 ||
  893. apic->lapic_timer.period == 0)
  894. return 0;
  895. remaining = hrtimer_get_remaining(&apic->lapic_timer.timer);
  896. if (ktime_to_ns(remaining) < 0)
  897. remaining = ktime_set(0, 0);
  898. ns = mod_64(ktime_to_ns(remaining), apic->lapic_timer.period);
  899. tmcct = div64_u64(ns,
  900. (APIC_BUS_CYCLE_NS * apic->divide_count));
  901. return tmcct;
  902. }
  903. static void __report_tpr_access(struct kvm_lapic *apic, bool write)
  904. {
  905. struct kvm_vcpu *vcpu = apic->vcpu;
  906. struct kvm_run *run = vcpu->run;
  907. kvm_make_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu);
  908. run->tpr_access.rip = kvm_rip_read(vcpu);
  909. run->tpr_access.is_write = write;
  910. }
  911. static inline void report_tpr_access(struct kvm_lapic *apic, bool write)
  912. {
  913. if (apic->vcpu->arch.tpr_access_reporting)
  914. __report_tpr_access(apic, write);
  915. }
  916. static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset)
  917. {
  918. u32 val = 0;
  919. if (offset >= LAPIC_MMIO_LENGTH)
  920. return 0;
  921. switch (offset) {
  922. case APIC_ID:
  923. if (apic_x2apic_mode(apic))
  924. val = kvm_apic_id(apic);
  925. else
  926. val = kvm_apic_id(apic) << 24;
  927. break;
  928. case APIC_ARBPRI:
  929. apic_debug("Access APIC ARBPRI register which is for P6\n");
  930. break;
  931. case APIC_TMCCT: /* Timer CCR */
  932. if (apic_lvtt_tscdeadline(apic))
  933. return 0;
  934. val = apic_get_tmcct(apic);
  935. break;
  936. case APIC_PROCPRI:
  937. apic_update_ppr(apic);
  938. val = kvm_lapic_get_reg(apic, offset);
  939. break;
  940. case APIC_TASKPRI:
  941. report_tpr_access(apic, false);
  942. /* fall thru */
  943. default:
  944. val = kvm_lapic_get_reg(apic, offset);
  945. break;
  946. }
  947. return val;
  948. }
  949. static inline struct kvm_lapic *to_lapic(struct kvm_io_device *dev)
  950. {
  951. return container_of(dev, struct kvm_lapic, dev);
  952. }
  953. int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
  954. void *data)
  955. {
  956. unsigned char alignment = offset & 0xf;
  957. u32 result;
  958. /* this bitmask has a bit cleared for each reserved register */
  959. static const u64 rmask = 0x43ff01ffffffe70cULL;
  960. if ((alignment + len) > 4) {
  961. apic_debug("KVM_APIC_READ: alignment error %x %d\n",
  962. offset, len);
  963. return 1;
  964. }
  965. if (offset > 0x3f0 || !(rmask & (1ULL << (offset >> 4)))) {
  966. apic_debug("KVM_APIC_READ: read reserved register %x\n",
  967. offset);
  968. return 1;
  969. }
  970. result = __apic_read(apic, offset & ~0xf);
  971. trace_kvm_apic_read(offset, result);
  972. switch (len) {
  973. case 1:
  974. case 2:
  975. case 4:
  976. memcpy(data, (char *)&result + alignment, len);
  977. break;
  978. default:
  979. printk(KERN_ERR "Local APIC read with len = %x, "
  980. "should be 1,2, or 4 instead\n", len);
  981. break;
  982. }
  983. return 0;
  984. }
  985. EXPORT_SYMBOL_GPL(kvm_lapic_reg_read);
  986. static int apic_mmio_in_range(struct kvm_lapic *apic, gpa_t addr)
  987. {
  988. return kvm_apic_hw_enabled(apic) &&
  989. addr >= apic->base_address &&
  990. addr < apic->base_address + LAPIC_MMIO_LENGTH;
  991. }
  992. static int apic_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
  993. gpa_t address, int len, void *data)
  994. {
  995. struct kvm_lapic *apic = to_lapic(this);
  996. u32 offset = address - apic->base_address;
  997. if (!apic_mmio_in_range(apic, address))
  998. return -EOPNOTSUPP;
  999. kvm_lapic_reg_read(apic, offset, len, data);
  1000. return 0;
  1001. }
  1002. static void update_divide_count(struct kvm_lapic *apic)
  1003. {
  1004. u32 tmp1, tmp2, tdcr;
  1005. tdcr = kvm_lapic_get_reg(apic, APIC_TDCR);
  1006. tmp1 = tdcr & 0xf;
  1007. tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
  1008. apic->divide_count = 0x1 << (tmp2 & 0x7);
  1009. apic_debug("timer divide count is 0x%x\n",
  1010. apic->divide_count);
  1011. }
  1012. static void apic_update_lvtt(struct kvm_lapic *apic)
  1013. {
  1014. u32 timer_mode = kvm_lapic_get_reg(apic, APIC_LVTT) &
  1015. apic->lapic_timer.timer_mode_mask;
  1016. if (apic->lapic_timer.timer_mode != timer_mode) {
  1017. apic->lapic_timer.timer_mode = timer_mode;
  1018. hrtimer_cancel(&apic->lapic_timer.timer);
  1019. }
  1020. }
  1021. static void apic_timer_expired(struct kvm_lapic *apic)
  1022. {
  1023. struct kvm_vcpu *vcpu = apic->vcpu;
  1024. struct swait_queue_head *q = &vcpu->wq;
  1025. struct kvm_timer *ktimer = &apic->lapic_timer;
  1026. if (atomic_read(&apic->lapic_timer.pending))
  1027. return;
  1028. atomic_inc(&apic->lapic_timer.pending);
  1029. kvm_set_pending_timer(vcpu);
  1030. if (swait_active(q))
  1031. swake_up(q);
  1032. if (apic_lvtt_tscdeadline(apic))
  1033. ktimer->expired_tscdeadline = ktimer->tscdeadline;
  1034. }
  1035. /*
  1036. * On APICv, this test will cause a busy wait
  1037. * during a higher-priority task.
  1038. */
  1039. static bool lapic_timer_int_injected(struct kvm_vcpu *vcpu)
  1040. {
  1041. struct kvm_lapic *apic = vcpu->arch.apic;
  1042. u32 reg = kvm_lapic_get_reg(apic, APIC_LVTT);
  1043. if (kvm_apic_hw_enabled(apic)) {
  1044. int vec = reg & APIC_VECTOR_MASK;
  1045. void *bitmap = apic->regs + APIC_ISR;
  1046. if (vcpu->arch.apicv_active)
  1047. bitmap = apic->regs + APIC_IRR;
  1048. if (apic_test_vector(vec, bitmap))
  1049. return true;
  1050. }
  1051. return false;
  1052. }
  1053. void wait_lapic_expire(struct kvm_vcpu *vcpu)
  1054. {
  1055. struct kvm_lapic *apic = vcpu->arch.apic;
  1056. u64 guest_tsc, tsc_deadline;
  1057. if (!lapic_in_kernel(vcpu))
  1058. return;
  1059. if (apic->lapic_timer.expired_tscdeadline == 0)
  1060. return;
  1061. if (!lapic_timer_int_injected(vcpu))
  1062. return;
  1063. tsc_deadline = apic->lapic_timer.expired_tscdeadline;
  1064. apic->lapic_timer.expired_tscdeadline = 0;
  1065. guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
  1066. trace_kvm_wait_lapic_expire(vcpu->vcpu_id, guest_tsc - tsc_deadline);
  1067. /* __delay is delay_tsc whenever the hardware has TSC, thus always. */
  1068. if (guest_tsc < tsc_deadline)
  1069. __delay(min(tsc_deadline - guest_tsc,
  1070. nsec_to_cycles(vcpu, lapic_timer_advance_ns)));
  1071. }
  1072. static void start_apic_timer(struct kvm_lapic *apic)
  1073. {
  1074. ktime_t now;
  1075. atomic_set(&apic->lapic_timer.pending, 0);
  1076. if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic)) {
  1077. /* lapic timer in oneshot or periodic mode */
  1078. now = apic->lapic_timer.timer.base->get_time();
  1079. apic->lapic_timer.period = (u64)kvm_lapic_get_reg(apic, APIC_TMICT)
  1080. * APIC_BUS_CYCLE_NS * apic->divide_count;
  1081. if (!apic->lapic_timer.period)
  1082. return;
  1083. /*
  1084. * Do not allow the guest to program periodic timers with small
  1085. * interval, since the hrtimers are not throttled by the host
  1086. * scheduler.
  1087. */
  1088. if (apic_lvtt_period(apic)) {
  1089. s64 min_period = min_timer_period_us * 1000LL;
  1090. if (apic->lapic_timer.period < min_period) {
  1091. pr_info_ratelimited(
  1092. "kvm: vcpu %i: requested %lld ns "
  1093. "lapic timer period limited to %lld ns\n",
  1094. apic->vcpu->vcpu_id,
  1095. apic->lapic_timer.period, min_period);
  1096. apic->lapic_timer.period = min_period;
  1097. }
  1098. }
  1099. hrtimer_start(&apic->lapic_timer.timer,
  1100. ktime_add_ns(now, apic->lapic_timer.period),
  1101. HRTIMER_MODE_ABS_PINNED);
  1102. apic_debug("%s: bus cycle is %" PRId64 "ns, now 0x%016"
  1103. PRIx64 ", "
  1104. "timer initial count 0x%x, period %lldns, "
  1105. "expire @ 0x%016" PRIx64 ".\n", __func__,
  1106. APIC_BUS_CYCLE_NS, ktime_to_ns(now),
  1107. kvm_lapic_get_reg(apic, APIC_TMICT),
  1108. apic->lapic_timer.period,
  1109. ktime_to_ns(ktime_add_ns(now,
  1110. apic->lapic_timer.period)));
  1111. } else if (apic_lvtt_tscdeadline(apic)) {
  1112. /* lapic timer in tsc deadline mode */
  1113. u64 guest_tsc, tscdeadline = apic->lapic_timer.tscdeadline;
  1114. u64 ns = 0;
  1115. ktime_t expire;
  1116. struct kvm_vcpu *vcpu = apic->vcpu;
  1117. unsigned long this_tsc_khz = vcpu->arch.virtual_tsc_khz;
  1118. unsigned long flags;
  1119. if (unlikely(!tscdeadline || !this_tsc_khz))
  1120. return;
  1121. local_irq_save(flags);
  1122. now = apic->lapic_timer.timer.base->get_time();
  1123. guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
  1124. if (likely(tscdeadline > guest_tsc)) {
  1125. ns = (tscdeadline - guest_tsc) * 1000000ULL;
  1126. do_div(ns, this_tsc_khz);
  1127. expire = ktime_add_ns(now, ns);
  1128. expire = ktime_sub_ns(expire, lapic_timer_advance_ns);
  1129. hrtimer_start(&apic->lapic_timer.timer,
  1130. expire, HRTIMER_MODE_ABS_PINNED);
  1131. } else
  1132. apic_timer_expired(apic);
  1133. local_irq_restore(flags);
  1134. }
  1135. }
  1136. static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val)
  1137. {
  1138. bool lvt0_in_nmi_mode = apic_lvt_nmi_mode(lvt0_val);
  1139. if (apic->lvt0_in_nmi_mode != lvt0_in_nmi_mode) {
  1140. apic->lvt0_in_nmi_mode = lvt0_in_nmi_mode;
  1141. if (lvt0_in_nmi_mode) {
  1142. apic_debug("Receive NMI setting on APIC_LVT0 "
  1143. "for cpu %d\n", apic->vcpu->vcpu_id);
  1144. atomic_inc(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
  1145. } else
  1146. atomic_dec(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
  1147. }
  1148. }
  1149. int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
  1150. {
  1151. int ret = 0;
  1152. trace_kvm_apic_write(reg, val);
  1153. switch (reg) {
  1154. case APIC_ID: /* Local APIC ID */
  1155. if (!apic_x2apic_mode(apic))
  1156. kvm_apic_set_id(apic, val >> 24);
  1157. else
  1158. ret = 1;
  1159. break;
  1160. case APIC_TASKPRI:
  1161. report_tpr_access(apic, true);
  1162. apic_set_tpr(apic, val & 0xff);
  1163. break;
  1164. case APIC_EOI:
  1165. apic_set_eoi(apic);
  1166. break;
  1167. case APIC_LDR:
  1168. if (!apic_x2apic_mode(apic))
  1169. kvm_apic_set_ldr(apic, val & APIC_LDR_MASK);
  1170. else
  1171. ret = 1;
  1172. break;
  1173. case APIC_DFR:
  1174. if (!apic_x2apic_mode(apic)) {
  1175. kvm_lapic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF);
  1176. recalculate_apic_map(apic->vcpu->kvm);
  1177. } else
  1178. ret = 1;
  1179. break;
  1180. case APIC_SPIV: {
  1181. u32 mask = 0x3ff;
  1182. if (kvm_lapic_get_reg(apic, APIC_LVR) & APIC_LVR_DIRECTED_EOI)
  1183. mask |= APIC_SPIV_DIRECTED_EOI;
  1184. apic_set_spiv(apic, val & mask);
  1185. if (!(val & APIC_SPIV_APIC_ENABLED)) {
  1186. int i;
  1187. u32 lvt_val;
  1188. for (i = 0; i < KVM_APIC_LVT_NUM; i++) {
  1189. lvt_val = kvm_lapic_get_reg(apic,
  1190. APIC_LVTT + 0x10 * i);
  1191. kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i,
  1192. lvt_val | APIC_LVT_MASKED);
  1193. }
  1194. apic_update_lvtt(apic);
  1195. atomic_set(&apic->lapic_timer.pending, 0);
  1196. }
  1197. break;
  1198. }
  1199. case APIC_ICR:
  1200. /* No delay here, so we always clear the pending bit */
  1201. kvm_lapic_set_reg(apic, APIC_ICR, val & ~(1 << 12));
  1202. apic_send_ipi(apic);
  1203. break;
  1204. case APIC_ICR2:
  1205. if (!apic_x2apic_mode(apic))
  1206. val &= 0xff000000;
  1207. kvm_lapic_set_reg(apic, APIC_ICR2, val);
  1208. break;
  1209. case APIC_LVT0:
  1210. apic_manage_nmi_watchdog(apic, val);
  1211. case APIC_LVTTHMR:
  1212. case APIC_LVTPC:
  1213. case APIC_LVT1:
  1214. case APIC_LVTERR:
  1215. /* TODO: Check vector */
  1216. if (!kvm_apic_sw_enabled(apic))
  1217. val |= APIC_LVT_MASKED;
  1218. val &= apic_lvt_mask[(reg - APIC_LVTT) >> 4];
  1219. kvm_lapic_set_reg(apic, reg, val);
  1220. break;
  1221. case APIC_LVTT:
  1222. if (!kvm_apic_sw_enabled(apic))
  1223. val |= APIC_LVT_MASKED;
  1224. val &= (apic_lvt_mask[0] | apic->lapic_timer.timer_mode_mask);
  1225. kvm_lapic_set_reg(apic, APIC_LVTT, val);
  1226. apic_update_lvtt(apic);
  1227. break;
  1228. case APIC_TMICT:
  1229. if (apic_lvtt_tscdeadline(apic))
  1230. break;
  1231. hrtimer_cancel(&apic->lapic_timer.timer);
  1232. kvm_lapic_set_reg(apic, APIC_TMICT, val);
  1233. start_apic_timer(apic);
  1234. break;
  1235. case APIC_TDCR:
  1236. if (val & 4)
  1237. apic_debug("KVM_WRITE:TDCR %x\n", val);
  1238. kvm_lapic_set_reg(apic, APIC_TDCR, val);
  1239. update_divide_count(apic);
  1240. break;
  1241. case APIC_ESR:
  1242. if (apic_x2apic_mode(apic) && val != 0) {
  1243. apic_debug("KVM_WRITE:ESR not zero %x\n", val);
  1244. ret = 1;
  1245. }
  1246. break;
  1247. case APIC_SELF_IPI:
  1248. if (apic_x2apic_mode(apic)) {
  1249. kvm_lapic_reg_write(apic, APIC_ICR, 0x40000 | (val & 0xff));
  1250. } else
  1251. ret = 1;
  1252. break;
  1253. default:
  1254. ret = 1;
  1255. break;
  1256. }
  1257. if (ret)
  1258. apic_debug("Local APIC Write to read-only register %x\n", reg);
  1259. return ret;
  1260. }
  1261. EXPORT_SYMBOL_GPL(kvm_lapic_reg_write);
  1262. static int apic_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
  1263. gpa_t address, int len, const void *data)
  1264. {
  1265. struct kvm_lapic *apic = to_lapic(this);
  1266. unsigned int offset = address - apic->base_address;
  1267. u32 val;
  1268. if (!apic_mmio_in_range(apic, address))
  1269. return -EOPNOTSUPP;
  1270. /*
  1271. * APIC register must be aligned on 128-bits boundary.
  1272. * 32/64/128 bits registers must be accessed thru 32 bits.
  1273. * Refer SDM 8.4.1
  1274. */
  1275. if (len != 4 || (offset & 0xf)) {
  1276. /* Don't shout loud, $infamous_os would cause only noise. */
  1277. apic_debug("apic write: bad size=%d %lx\n", len, (long)address);
  1278. return 0;
  1279. }
  1280. val = *(u32*)data;
  1281. /* too common printing */
  1282. if (offset != APIC_EOI)
  1283. apic_debug("%s: offset 0x%x with length 0x%x, and value is "
  1284. "0x%x\n", __func__, offset, len, val);
  1285. kvm_lapic_reg_write(apic, offset & 0xff0, val);
  1286. return 0;
  1287. }
  1288. void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu)
  1289. {
  1290. kvm_lapic_reg_write(vcpu->arch.apic, APIC_EOI, 0);
  1291. }
  1292. EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi);
  1293. /* emulate APIC access in a trap manner */
  1294. void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset)
  1295. {
  1296. u32 val = 0;
  1297. /* hw has done the conditional check and inst decode */
  1298. offset &= 0xff0;
  1299. kvm_lapic_reg_read(vcpu->arch.apic, offset, 4, &val);
  1300. /* TODO: optimize to just emulate side effect w/o one more write */
  1301. kvm_lapic_reg_write(vcpu->arch.apic, offset, val);
  1302. }
  1303. EXPORT_SYMBOL_GPL(kvm_apic_write_nodecode);
  1304. void kvm_free_lapic(struct kvm_vcpu *vcpu)
  1305. {
  1306. struct kvm_lapic *apic = vcpu->arch.apic;
  1307. if (!vcpu->arch.apic)
  1308. return;
  1309. hrtimer_cancel(&apic->lapic_timer.timer);
  1310. if (!(vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE))
  1311. static_key_slow_dec_deferred(&apic_hw_disabled);
  1312. if (!apic->sw_enabled)
  1313. static_key_slow_dec_deferred(&apic_sw_disabled);
  1314. if (apic->regs)
  1315. free_page((unsigned long)apic->regs);
  1316. kfree(apic);
  1317. }
  1318. /*
  1319. *----------------------------------------------------------------------
  1320. * LAPIC interface
  1321. *----------------------------------------------------------------------
  1322. */
  1323. u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu)
  1324. {
  1325. struct kvm_lapic *apic = vcpu->arch.apic;
  1326. if (!lapic_in_kernel(vcpu) || apic_lvtt_oneshot(apic) ||
  1327. apic_lvtt_period(apic))
  1328. return 0;
  1329. return apic->lapic_timer.tscdeadline;
  1330. }
  1331. void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data)
  1332. {
  1333. struct kvm_lapic *apic = vcpu->arch.apic;
  1334. if (!lapic_in_kernel(vcpu) || apic_lvtt_oneshot(apic) ||
  1335. apic_lvtt_period(apic))
  1336. return;
  1337. hrtimer_cancel(&apic->lapic_timer.timer);
  1338. apic->lapic_timer.tscdeadline = data;
  1339. start_apic_timer(apic);
  1340. }
  1341. void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
  1342. {
  1343. struct kvm_lapic *apic = vcpu->arch.apic;
  1344. apic_set_tpr(apic, ((cr8 & 0x0f) << 4)
  1345. | (kvm_lapic_get_reg(apic, APIC_TASKPRI) & 4));
  1346. }
  1347. u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
  1348. {
  1349. u64 tpr;
  1350. tpr = (u64) kvm_lapic_get_reg(vcpu->arch.apic, APIC_TASKPRI);
  1351. return (tpr & 0xf0) >> 4;
  1352. }
  1353. void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
  1354. {
  1355. u64 old_value = vcpu->arch.apic_base;
  1356. struct kvm_lapic *apic = vcpu->arch.apic;
  1357. if (!apic) {
  1358. value |= MSR_IA32_APICBASE_BSP;
  1359. vcpu->arch.apic_base = value;
  1360. return;
  1361. }
  1362. vcpu->arch.apic_base = value;
  1363. /* update jump label if enable bit changes */
  1364. if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE) {
  1365. if (value & MSR_IA32_APICBASE_ENABLE)
  1366. static_key_slow_dec_deferred(&apic_hw_disabled);
  1367. else
  1368. static_key_slow_inc(&apic_hw_disabled.key);
  1369. recalculate_apic_map(vcpu->kvm);
  1370. }
  1371. if ((old_value ^ value) & X2APIC_ENABLE) {
  1372. if (value & X2APIC_ENABLE) {
  1373. kvm_apic_set_x2apic_id(apic, vcpu->vcpu_id);
  1374. kvm_x86_ops->set_virtual_x2apic_mode(vcpu, true);
  1375. } else
  1376. kvm_x86_ops->set_virtual_x2apic_mode(vcpu, false);
  1377. }
  1378. apic->base_address = apic->vcpu->arch.apic_base &
  1379. MSR_IA32_APICBASE_BASE;
  1380. if ((value & MSR_IA32_APICBASE_ENABLE) &&
  1381. apic->base_address != APIC_DEFAULT_PHYS_BASE)
  1382. pr_warn_once("APIC base relocation is unsupported by KVM");
  1383. /* with FSB delivery interrupt, we can restart APIC functionality */
  1384. apic_debug("apic base msr is 0x%016" PRIx64 ", and base address is "
  1385. "0x%lx.\n", apic->vcpu->arch.apic_base, apic->base_address);
  1386. }
  1387. void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event)
  1388. {
  1389. struct kvm_lapic *apic;
  1390. int i;
  1391. apic_debug("%s\n", __func__);
  1392. ASSERT(vcpu);
  1393. apic = vcpu->arch.apic;
  1394. ASSERT(apic != NULL);
  1395. /* Stop the timer in case it's a reset to an active apic */
  1396. hrtimer_cancel(&apic->lapic_timer.timer);
  1397. if (!init_event)
  1398. kvm_apic_set_id(apic, vcpu->vcpu_id);
  1399. kvm_apic_set_version(apic->vcpu);
  1400. for (i = 0; i < KVM_APIC_LVT_NUM; i++)
  1401. kvm_lapic_set_reg(apic, APIC_LVTT + 0x10 * i, APIC_LVT_MASKED);
  1402. apic_update_lvtt(apic);
  1403. if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_LINT0_REENABLED))
  1404. kvm_lapic_set_reg(apic, APIC_LVT0,
  1405. SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
  1406. apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
  1407. kvm_lapic_set_reg(apic, APIC_DFR, 0xffffffffU);
  1408. apic_set_spiv(apic, 0xff);
  1409. kvm_lapic_set_reg(apic, APIC_TASKPRI, 0);
  1410. if (!apic_x2apic_mode(apic))
  1411. kvm_apic_set_ldr(apic, 0);
  1412. kvm_lapic_set_reg(apic, APIC_ESR, 0);
  1413. kvm_lapic_set_reg(apic, APIC_ICR, 0);
  1414. kvm_lapic_set_reg(apic, APIC_ICR2, 0);
  1415. kvm_lapic_set_reg(apic, APIC_TDCR, 0);
  1416. kvm_lapic_set_reg(apic, APIC_TMICT, 0);
  1417. for (i = 0; i < 8; i++) {
  1418. kvm_lapic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
  1419. kvm_lapic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
  1420. kvm_lapic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
  1421. }
  1422. apic->irr_pending = vcpu->arch.apicv_active;
  1423. apic->isr_count = vcpu->arch.apicv_active ? 1 : 0;
  1424. apic->highest_isr_cache = -1;
  1425. update_divide_count(apic);
  1426. atomic_set(&apic->lapic_timer.pending, 0);
  1427. if (kvm_vcpu_is_bsp(vcpu))
  1428. kvm_lapic_set_base(vcpu,
  1429. vcpu->arch.apic_base | MSR_IA32_APICBASE_BSP);
  1430. vcpu->arch.pv_eoi.msr_val = 0;
  1431. apic_update_ppr(apic);
  1432. vcpu->arch.apic_arb_prio = 0;
  1433. vcpu->arch.apic_attention = 0;
  1434. apic_debug("%s: vcpu=%p, id=%d, base_msr="
  1435. "0x%016" PRIx64 ", base_address=0x%0lx.\n", __func__,
  1436. vcpu, kvm_apic_id(apic),
  1437. vcpu->arch.apic_base, apic->base_address);
  1438. }
  1439. /*
  1440. *----------------------------------------------------------------------
  1441. * timer interface
  1442. *----------------------------------------------------------------------
  1443. */
  1444. static bool lapic_is_periodic(struct kvm_lapic *apic)
  1445. {
  1446. return apic_lvtt_period(apic);
  1447. }
  1448. int apic_has_pending_timer(struct kvm_vcpu *vcpu)
  1449. {
  1450. struct kvm_lapic *apic = vcpu->arch.apic;
  1451. if (apic_enabled(apic) && apic_lvt_enabled(apic, APIC_LVTT))
  1452. return atomic_read(&apic->lapic_timer.pending);
  1453. return 0;
  1454. }
  1455. int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type)
  1456. {
  1457. u32 reg = kvm_lapic_get_reg(apic, lvt_type);
  1458. int vector, mode, trig_mode;
  1459. if (kvm_apic_hw_enabled(apic) && !(reg & APIC_LVT_MASKED)) {
  1460. vector = reg & APIC_VECTOR_MASK;
  1461. mode = reg & APIC_MODE_MASK;
  1462. trig_mode = reg & APIC_LVT_LEVEL_TRIGGER;
  1463. return __apic_accept_irq(apic, mode, vector, 1, trig_mode,
  1464. NULL);
  1465. }
  1466. return 0;
  1467. }
  1468. void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu)
  1469. {
  1470. struct kvm_lapic *apic = vcpu->arch.apic;
  1471. if (apic)
  1472. kvm_apic_local_deliver(apic, APIC_LVT0);
  1473. }
  1474. static const struct kvm_io_device_ops apic_mmio_ops = {
  1475. .read = apic_mmio_read,
  1476. .write = apic_mmio_write,
  1477. };
  1478. static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)
  1479. {
  1480. struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
  1481. struct kvm_lapic *apic = container_of(ktimer, struct kvm_lapic, lapic_timer);
  1482. apic_timer_expired(apic);
  1483. if (lapic_is_periodic(apic)) {
  1484. hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
  1485. return HRTIMER_RESTART;
  1486. } else
  1487. return HRTIMER_NORESTART;
  1488. }
  1489. int kvm_create_lapic(struct kvm_vcpu *vcpu)
  1490. {
  1491. struct kvm_lapic *apic;
  1492. ASSERT(vcpu != NULL);
  1493. apic_debug("apic_init %d\n", vcpu->vcpu_id);
  1494. apic = kzalloc(sizeof(*apic), GFP_KERNEL);
  1495. if (!apic)
  1496. goto nomem;
  1497. vcpu->arch.apic = apic;
  1498. apic->regs = (void *)get_zeroed_page(GFP_KERNEL);
  1499. if (!apic->regs) {
  1500. printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
  1501. vcpu->vcpu_id);
  1502. goto nomem_free_apic;
  1503. }
  1504. apic->vcpu = vcpu;
  1505. hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
  1506. HRTIMER_MODE_ABS_PINNED);
  1507. apic->lapic_timer.timer.function = apic_timer_fn;
  1508. /*
  1509. * APIC is created enabled. This will prevent kvm_lapic_set_base from
  1510. * thinking that APIC satet has changed.
  1511. */
  1512. vcpu->arch.apic_base = MSR_IA32_APICBASE_ENABLE;
  1513. kvm_lapic_set_base(vcpu,
  1514. APIC_DEFAULT_PHYS_BASE | MSR_IA32_APICBASE_ENABLE);
  1515. static_key_slow_inc(&apic_sw_disabled.key); /* sw disabled at reset */
  1516. kvm_lapic_reset(vcpu, false);
  1517. kvm_iodevice_init(&apic->dev, &apic_mmio_ops);
  1518. return 0;
  1519. nomem_free_apic:
  1520. kfree(apic);
  1521. nomem:
  1522. return -ENOMEM;
  1523. }
  1524. int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
  1525. {
  1526. struct kvm_lapic *apic = vcpu->arch.apic;
  1527. int highest_irr;
  1528. if (!apic_enabled(apic))
  1529. return -1;
  1530. apic_update_ppr(apic);
  1531. highest_irr = apic_find_highest_irr(apic);
  1532. if ((highest_irr == -1) ||
  1533. ((highest_irr & 0xF0) <= kvm_lapic_get_reg(apic, APIC_PROCPRI)))
  1534. return -1;
  1535. return highest_irr;
  1536. }
  1537. int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
  1538. {
  1539. u32 lvt0 = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LVT0);
  1540. int r = 0;
  1541. if (!kvm_apic_hw_enabled(vcpu->arch.apic))
  1542. r = 1;
  1543. if ((lvt0 & APIC_LVT_MASKED) == 0 &&
  1544. GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
  1545. r = 1;
  1546. return r;
  1547. }
  1548. void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
  1549. {
  1550. struct kvm_lapic *apic = vcpu->arch.apic;
  1551. if (atomic_read(&apic->lapic_timer.pending) > 0) {
  1552. kvm_apic_local_deliver(apic, APIC_LVTT);
  1553. if (apic_lvtt_tscdeadline(apic))
  1554. apic->lapic_timer.tscdeadline = 0;
  1555. atomic_set(&apic->lapic_timer.pending, 0);
  1556. }
  1557. }
  1558. int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
  1559. {
  1560. int vector = kvm_apic_has_interrupt(vcpu);
  1561. struct kvm_lapic *apic = vcpu->arch.apic;
  1562. if (vector == -1)
  1563. return -1;
  1564. /*
  1565. * We get here even with APIC virtualization enabled, if doing
  1566. * nested virtualization and L1 runs with the "acknowledge interrupt
  1567. * on exit" mode. Then we cannot inject the interrupt via RVI,
  1568. * because the process would deliver it through the IDT.
  1569. */
  1570. apic_set_isr(vector, apic);
  1571. apic_update_ppr(apic);
  1572. apic_clear_irr(vector, apic);
  1573. if (test_bit(vector, vcpu_to_synic(vcpu)->auto_eoi_bitmap)) {
  1574. apic_clear_isr(vector, apic);
  1575. apic_update_ppr(apic);
  1576. }
  1577. return vector;
  1578. }
  1579. void kvm_apic_post_state_restore(struct kvm_vcpu *vcpu,
  1580. struct kvm_lapic_state *s)
  1581. {
  1582. struct kvm_lapic *apic = vcpu->arch.apic;
  1583. kvm_lapic_set_base(vcpu, vcpu->arch.apic_base);
  1584. /* set SPIV separately to get count of SW disabled APICs right */
  1585. apic_set_spiv(apic, *((u32 *)(s->regs + APIC_SPIV)));
  1586. memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
  1587. /* call kvm_apic_set_id() to put apic into apic_map */
  1588. kvm_apic_set_id(apic, kvm_apic_id(apic));
  1589. kvm_apic_set_version(vcpu);
  1590. apic_update_ppr(apic);
  1591. hrtimer_cancel(&apic->lapic_timer.timer);
  1592. apic_update_lvtt(apic);
  1593. apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
  1594. update_divide_count(apic);
  1595. start_apic_timer(apic);
  1596. apic->irr_pending = true;
  1597. apic->isr_count = vcpu->arch.apicv_active ?
  1598. 1 : count_vectors(apic->regs + APIC_ISR);
  1599. apic->highest_isr_cache = -1;
  1600. if (vcpu->arch.apicv_active) {
  1601. if (kvm_x86_ops->apicv_post_state_restore)
  1602. kvm_x86_ops->apicv_post_state_restore(vcpu);
  1603. kvm_x86_ops->hwapic_irr_update(vcpu,
  1604. apic_find_highest_irr(apic));
  1605. kvm_x86_ops->hwapic_isr_update(vcpu,
  1606. apic_find_highest_isr(apic));
  1607. }
  1608. kvm_make_request(KVM_REQ_EVENT, vcpu);
  1609. if (ioapic_in_kernel(vcpu->kvm))
  1610. kvm_rtc_eoi_tracking_restore_one(vcpu);
  1611. vcpu->arch.apic_arb_prio = 0;
  1612. }
  1613. void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
  1614. {
  1615. struct hrtimer *timer;
  1616. if (!lapic_in_kernel(vcpu))
  1617. return;
  1618. timer = &vcpu->arch.apic->lapic_timer.timer;
  1619. if (hrtimer_cancel(timer))
  1620. hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
  1621. }
  1622. /*
  1623. * apic_sync_pv_eoi_from_guest - called on vmexit or cancel interrupt
  1624. *
  1625. * Detect whether guest triggered PV EOI since the
  1626. * last entry. If yes, set EOI on guests's behalf.
  1627. * Clear PV EOI in guest memory in any case.
  1628. */
  1629. static void apic_sync_pv_eoi_from_guest(struct kvm_vcpu *vcpu,
  1630. struct kvm_lapic *apic)
  1631. {
  1632. bool pending;
  1633. int vector;
  1634. /*
  1635. * PV EOI state is derived from KVM_APIC_PV_EOI_PENDING in host
  1636. * and KVM_PV_EOI_ENABLED in guest memory as follows:
  1637. *
  1638. * KVM_APIC_PV_EOI_PENDING is unset:
  1639. * -> host disabled PV EOI.
  1640. * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is set:
  1641. * -> host enabled PV EOI, guest did not execute EOI yet.
  1642. * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is unset:
  1643. * -> host enabled PV EOI, guest executed EOI.
  1644. */
  1645. BUG_ON(!pv_eoi_enabled(vcpu));
  1646. pending = pv_eoi_get_pending(vcpu);
  1647. /*
  1648. * Clear pending bit in any case: it will be set again on vmentry.
  1649. * While this might not be ideal from performance point of view,
  1650. * this makes sure pv eoi is only enabled when we know it's safe.
  1651. */
  1652. pv_eoi_clr_pending(vcpu);
  1653. if (pending)
  1654. return;
  1655. vector = apic_set_eoi(apic);
  1656. trace_kvm_pv_eoi(apic, vector);
  1657. }
  1658. void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu)
  1659. {
  1660. u32 data;
  1661. if (test_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention))
  1662. apic_sync_pv_eoi_from_guest(vcpu, vcpu->arch.apic);
  1663. if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
  1664. return;
  1665. if (kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
  1666. sizeof(u32)))
  1667. return;
  1668. apic_set_tpr(vcpu->arch.apic, data & 0xff);
  1669. }
  1670. /*
  1671. * apic_sync_pv_eoi_to_guest - called before vmentry
  1672. *
  1673. * Detect whether it's safe to enable PV EOI and
  1674. * if yes do so.
  1675. */
  1676. static void apic_sync_pv_eoi_to_guest(struct kvm_vcpu *vcpu,
  1677. struct kvm_lapic *apic)
  1678. {
  1679. if (!pv_eoi_enabled(vcpu) ||
  1680. /* IRR set or many bits in ISR: could be nested. */
  1681. apic->irr_pending ||
  1682. /* Cache not set: could be safe but we don't bother. */
  1683. apic->highest_isr_cache == -1 ||
  1684. /* Need EOI to update ioapic. */
  1685. kvm_ioapic_handles_vector(apic, apic->highest_isr_cache)) {
  1686. /*
  1687. * PV EOI was disabled by apic_sync_pv_eoi_from_guest
  1688. * so we need not do anything here.
  1689. */
  1690. return;
  1691. }
  1692. pv_eoi_set_pending(apic->vcpu);
  1693. }
  1694. void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu)
  1695. {
  1696. u32 data, tpr;
  1697. int max_irr, max_isr;
  1698. struct kvm_lapic *apic = vcpu->arch.apic;
  1699. apic_sync_pv_eoi_to_guest(vcpu, apic);
  1700. if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
  1701. return;
  1702. tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI) & 0xff;
  1703. max_irr = apic_find_highest_irr(apic);
  1704. if (max_irr < 0)
  1705. max_irr = 0;
  1706. max_isr = apic_find_highest_isr(apic);
  1707. if (max_isr < 0)
  1708. max_isr = 0;
  1709. data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24);
  1710. kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
  1711. sizeof(u32));
  1712. }
  1713. int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr)
  1714. {
  1715. if (vapic_addr) {
  1716. if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
  1717. &vcpu->arch.apic->vapic_cache,
  1718. vapic_addr, sizeof(u32)))
  1719. return -EINVAL;
  1720. __set_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
  1721. } else {
  1722. __clear_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
  1723. }
  1724. vcpu->arch.apic->vapic_addr = vapic_addr;
  1725. return 0;
  1726. }
  1727. int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  1728. {
  1729. struct kvm_lapic *apic = vcpu->arch.apic;
  1730. u32 reg = (msr - APIC_BASE_MSR) << 4;
  1731. if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
  1732. return 1;
  1733. if (reg == APIC_ICR2)
  1734. return 1;
  1735. /* if this is ICR write vector before command */
  1736. if (reg == APIC_ICR)
  1737. kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
  1738. return kvm_lapic_reg_write(apic, reg, (u32)data);
  1739. }
  1740. int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
  1741. {
  1742. struct kvm_lapic *apic = vcpu->arch.apic;
  1743. u32 reg = (msr - APIC_BASE_MSR) << 4, low, high = 0;
  1744. if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
  1745. return 1;
  1746. if (reg == APIC_DFR || reg == APIC_ICR2) {
  1747. apic_debug("KVM_APIC_READ: read x2apic reserved register %x\n",
  1748. reg);
  1749. return 1;
  1750. }
  1751. if (kvm_lapic_reg_read(apic, reg, 4, &low))
  1752. return 1;
  1753. if (reg == APIC_ICR)
  1754. kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high);
  1755. *data = (((u64)high) << 32) | low;
  1756. return 0;
  1757. }
  1758. int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 reg, u64 data)
  1759. {
  1760. struct kvm_lapic *apic = vcpu->arch.apic;
  1761. if (!lapic_in_kernel(vcpu))
  1762. return 1;
  1763. /* if this is ICR write vector before command */
  1764. if (reg == APIC_ICR)
  1765. kvm_lapic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
  1766. return kvm_lapic_reg_write(apic, reg, (u32)data);
  1767. }
  1768. int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 reg, u64 *data)
  1769. {
  1770. struct kvm_lapic *apic = vcpu->arch.apic;
  1771. u32 low, high = 0;
  1772. if (!lapic_in_kernel(vcpu))
  1773. return 1;
  1774. if (kvm_lapic_reg_read(apic, reg, 4, &low))
  1775. return 1;
  1776. if (reg == APIC_ICR)
  1777. kvm_lapic_reg_read(apic, APIC_ICR2, 4, &high);
  1778. *data = (((u64)high) << 32) | low;
  1779. return 0;
  1780. }
  1781. int kvm_lapic_enable_pv_eoi(struct kvm_vcpu *vcpu, u64 data)
  1782. {
  1783. u64 addr = data & ~KVM_MSR_ENABLED;
  1784. if (!IS_ALIGNED(addr, 4))
  1785. return 1;
  1786. vcpu->arch.pv_eoi.msr_val = data;
  1787. if (!pv_eoi_enabled(vcpu))
  1788. return 0;
  1789. return kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.pv_eoi.data,
  1790. addr, sizeof(u8));
  1791. }
  1792. void kvm_apic_accept_events(struct kvm_vcpu *vcpu)
  1793. {
  1794. struct kvm_lapic *apic = vcpu->arch.apic;
  1795. u8 sipi_vector;
  1796. unsigned long pe;
  1797. if (!lapic_in_kernel(vcpu) || !apic->pending_events)
  1798. return;
  1799. /*
  1800. * INITs are latched while in SMM. Because an SMM CPU cannot
  1801. * be in KVM_MP_STATE_INIT_RECEIVED state, just eat SIPIs
  1802. * and delay processing of INIT until the next RSM.
  1803. */
  1804. if (is_smm(vcpu)) {
  1805. WARN_ON_ONCE(vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED);
  1806. if (test_bit(KVM_APIC_SIPI, &apic->pending_events))
  1807. clear_bit(KVM_APIC_SIPI, &apic->pending_events);
  1808. return;
  1809. }
  1810. pe = xchg(&apic->pending_events, 0);
  1811. if (test_bit(KVM_APIC_INIT, &pe)) {
  1812. kvm_lapic_reset(vcpu, true);
  1813. kvm_vcpu_reset(vcpu, true);
  1814. if (kvm_vcpu_is_bsp(apic->vcpu))
  1815. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  1816. else
  1817. vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
  1818. }
  1819. if (test_bit(KVM_APIC_SIPI, &pe) &&
  1820. vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
  1821. /* evaluate pending_events before reading the vector */
  1822. smp_rmb();
  1823. sipi_vector = apic->sipi_vector;
  1824. apic_debug("vcpu %d received sipi with vector # %x\n",
  1825. vcpu->vcpu_id, sipi_vector);
  1826. kvm_vcpu_deliver_sipi_vector(vcpu, sipi_vector);
  1827. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  1828. }
  1829. }
  1830. void kvm_lapic_init(void)
  1831. {
  1832. /* do not patch jump label more than once per second */
  1833. jump_label_rate_limit(&apic_hw_disabled, HZ);
  1834. jump_label_rate_limit(&apic_sw_disabled, HZ);
  1835. }