lapic.c 68 KB

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