lapic.c 53 KB

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