lapic.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Local APIC virtualization
  4. *
  5. * Copyright (C) 2006 Qumranet, Inc.
  6. * Copyright (C) 2007 Novell
  7. * Copyright (C) 2007 Intel
  8. * Copyright 2009 Red Hat, Inc. and/or its affiliates.
  9. *
  10. * Authors:
  11. * Dor Laor <[email protected]>
  12. * Gregory Haskins <[email protected]>
  13. * Yaozu (Eddie) Dong <[email protected]>
  14. *
  15. * Based on Xen 3.1 code, Copyright (c) 2004, Intel Corporation.
  16. */
  17. #include <linux/kvm_host.h>
  18. #include <linux/kvm.h>
  19. #include <linux/mm.h>
  20. #include <linux/highmem.h>
  21. #include <linux/smp.h>
  22. #include <linux/hrtimer.h>
  23. #include <linux/io.h>
  24. #include <linux/export.h>
  25. #include <linux/math64.h>
  26. #include <linux/slab.h>
  27. #include <asm/processor.h>
  28. #include <asm/mce.h>
  29. #include <asm/msr.h>
  30. #include <asm/page.h>
  31. #include <asm/current.h>
  32. #include <asm/apicdef.h>
  33. #include <asm/delay.h>
  34. #include <linux/atomic.h>
  35. #include <linux/jump_label.h>
  36. #include "kvm_cache_regs.h"
  37. #include "irq.h"
  38. #include "ioapic.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. /* 14 is the version for Xeon and Pentium 8.4.8*/
  53. #define APIC_VERSION 0x14UL
  54. #define LAPIC_MMIO_LENGTH (1 << 12)
  55. /* followed define is not in apicdef.h */
  56. #define MAX_APIC_VECTOR 256
  57. #define APIC_VECTORS_PER_REG 32
  58. static bool lapic_timer_advance_dynamic __read_mostly;
  59. #define LAPIC_TIMER_ADVANCE_ADJUST_MIN 100 /* clock cycles */
  60. #define LAPIC_TIMER_ADVANCE_ADJUST_MAX 10000 /* clock cycles */
  61. #define LAPIC_TIMER_ADVANCE_NS_INIT 1000
  62. #define LAPIC_TIMER_ADVANCE_NS_MAX 5000
  63. /* step-by-step approximation to mitigate fluctuation */
  64. #define LAPIC_TIMER_ADVANCE_ADJUST_STEP 8
  65. static int kvm_lapic_msr_read(struct kvm_lapic *apic, u32 reg, u64 *data);
  66. static int kvm_lapic_msr_write(struct kvm_lapic *apic, u32 reg, u64 data);
  67. static inline void __kvm_lapic_set_reg(char *regs, int reg_off, u32 val)
  68. {
  69. *((u32 *) (regs + reg_off)) = val;
  70. }
  71. static inline void kvm_lapic_set_reg(struct kvm_lapic *apic, int reg_off, u32 val)
  72. {
  73. __kvm_lapic_set_reg(apic->regs, reg_off, val);
  74. }
  75. static __always_inline u64 __kvm_lapic_get_reg64(char *regs, int reg)
  76. {
  77. BUILD_BUG_ON(reg != APIC_ICR);
  78. return *((u64 *) (regs + reg));
  79. }
  80. static __always_inline u64 kvm_lapic_get_reg64(struct kvm_lapic *apic, int reg)
  81. {
  82. return __kvm_lapic_get_reg64(apic->regs, reg);
  83. }
  84. static __always_inline void __kvm_lapic_set_reg64(char *regs, int reg, u64 val)
  85. {
  86. BUILD_BUG_ON(reg != APIC_ICR);
  87. *((u64 *) (regs + reg)) = val;
  88. }
  89. static __always_inline void kvm_lapic_set_reg64(struct kvm_lapic *apic,
  90. int reg, u64 val)
  91. {
  92. __kvm_lapic_set_reg64(apic->regs, reg, val);
  93. }
  94. static inline int apic_test_vector(int vec, void *bitmap)
  95. {
  96. return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  97. }
  98. bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
  99. {
  100. struct kvm_lapic *apic = vcpu->arch.apic;
  101. return apic_test_vector(vector, apic->regs + APIC_ISR) ||
  102. apic_test_vector(vector, apic->regs + APIC_IRR);
  103. }
  104. static inline int __apic_test_and_set_vector(int vec, void *bitmap)
  105. {
  106. return __test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  107. }
  108. static inline int __apic_test_and_clear_vector(int vec, void *bitmap)
  109. {
  110. return __test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  111. }
  112. __read_mostly DEFINE_STATIC_KEY_DEFERRED_FALSE(apic_hw_disabled, HZ);
  113. __read_mostly DEFINE_STATIC_KEY_DEFERRED_FALSE(apic_sw_disabled, HZ);
  114. static inline int apic_enabled(struct kvm_lapic *apic)
  115. {
  116. return kvm_apic_sw_enabled(apic) && kvm_apic_hw_enabled(apic);
  117. }
  118. #define LVT_MASK \
  119. (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
  120. #define LINT_MASK \
  121. (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
  122. APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
  123. static inline u32 kvm_x2apic_id(struct kvm_lapic *apic)
  124. {
  125. return apic->vcpu->vcpu_id;
  126. }
  127. static bool kvm_can_post_timer_interrupt(struct kvm_vcpu *vcpu)
  128. {
  129. return pi_inject_timer && kvm_vcpu_apicv_active(vcpu) &&
  130. (kvm_mwait_in_guest(vcpu->kvm) || kvm_hlt_in_guest(vcpu->kvm));
  131. }
  132. bool kvm_can_use_hv_timer(struct kvm_vcpu *vcpu)
  133. {
  134. return kvm_x86_ops.set_hv_timer
  135. && !(kvm_mwait_in_guest(vcpu->kvm) ||
  136. kvm_can_post_timer_interrupt(vcpu));
  137. }
  138. EXPORT_SYMBOL_GPL(kvm_can_use_hv_timer);
  139. static bool kvm_use_posted_timer_interrupt(struct kvm_vcpu *vcpu)
  140. {
  141. return kvm_can_post_timer_interrupt(vcpu) && vcpu->mode == IN_GUEST_MODE;
  142. }
  143. static inline bool kvm_apic_map_get_logical_dest(struct kvm_apic_map *map,
  144. u32 dest_id, struct kvm_lapic ***cluster, u16 *mask) {
  145. switch (map->mode) {
  146. case KVM_APIC_MODE_X2APIC: {
  147. u32 offset = (dest_id >> 16) * 16;
  148. u32 max_apic_id = map->max_apic_id;
  149. if (offset <= max_apic_id) {
  150. u8 cluster_size = min(max_apic_id - offset + 1, 16U);
  151. offset = array_index_nospec(offset, map->max_apic_id + 1);
  152. *cluster = &map->phys_map[offset];
  153. *mask = dest_id & (0xffff >> (16 - cluster_size));
  154. } else {
  155. *mask = 0;
  156. }
  157. return true;
  158. }
  159. case KVM_APIC_MODE_XAPIC_FLAT:
  160. *cluster = map->xapic_flat_map;
  161. *mask = dest_id & 0xff;
  162. return true;
  163. case KVM_APIC_MODE_XAPIC_CLUSTER:
  164. *cluster = map->xapic_cluster_map[(dest_id >> 4) & 0xf];
  165. *mask = dest_id & 0xf;
  166. return true;
  167. default:
  168. /* Not optimized. */
  169. return false;
  170. }
  171. }
  172. static void kvm_apic_map_free(struct rcu_head *rcu)
  173. {
  174. struct kvm_apic_map *map = container_of(rcu, struct kvm_apic_map, rcu);
  175. kvfree(map);
  176. }
  177. /*
  178. * CLEAN -> DIRTY and UPDATE_IN_PROGRESS -> DIRTY changes happen without a lock.
  179. *
  180. * DIRTY -> UPDATE_IN_PROGRESS and UPDATE_IN_PROGRESS -> CLEAN happen with
  181. * apic_map_lock_held.
  182. */
  183. enum {
  184. CLEAN,
  185. UPDATE_IN_PROGRESS,
  186. DIRTY
  187. };
  188. void kvm_recalculate_apic_map(struct kvm *kvm)
  189. {
  190. struct kvm_apic_map *new, *old = NULL;
  191. struct kvm_vcpu *vcpu;
  192. unsigned long i;
  193. u32 max_id = 255; /* enough space for any xAPIC ID */
  194. /* Read kvm->arch.apic_map_dirty before kvm->arch.apic_map. */
  195. if (atomic_read_acquire(&kvm->arch.apic_map_dirty) == CLEAN)
  196. return;
  197. WARN_ONCE(!irqchip_in_kernel(kvm),
  198. "Dirty APIC map without an in-kernel local APIC");
  199. mutex_lock(&kvm->arch.apic_map_lock);
  200. /*
  201. * Read kvm->arch.apic_map_dirty before kvm->arch.apic_map
  202. * (if clean) or the APIC registers (if dirty).
  203. */
  204. if (atomic_cmpxchg_acquire(&kvm->arch.apic_map_dirty,
  205. DIRTY, UPDATE_IN_PROGRESS) == CLEAN) {
  206. /* Someone else has updated the map. */
  207. mutex_unlock(&kvm->arch.apic_map_lock);
  208. return;
  209. }
  210. kvm_for_each_vcpu(i, vcpu, kvm)
  211. if (kvm_apic_present(vcpu))
  212. max_id = max(max_id, kvm_x2apic_id(vcpu->arch.apic));
  213. new = kvzalloc(sizeof(struct kvm_apic_map) +
  214. sizeof(struct kvm_lapic *) * ((u64)max_id + 1),
  215. GFP_KERNEL_ACCOUNT);
  216. if (!new)
  217. goto out;
  218. new->max_apic_id = max_id;
  219. kvm_for_each_vcpu(i, vcpu, kvm) {
  220. struct kvm_lapic *apic = vcpu->arch.apic;
  221. struct kvm_lapic **cluster;
  222. u16 mask;
  223. u32 ldr;
  224. u8 xapic_id;
  225. u32 x2apic_id;
  226. if (!kvm_apic_present(vcpu))
  227. continue;
  228. xapic_id = kvm_xapic_id(apic);
  229. x2apic_id = kvm_x2apic_id(apic);
  230. /* Hotplug hack: see kvm_apic_match_physical_addr(), ... */
  231. if ((apic_x2apic_mode(apic) || x2apic_id > 0xff) &&
  232. x2apic_id <= new->max_apic_id)
  233. new->phys_map[x2apic_id] = apic;
  234. /*
  235. * ... xAPIC ID of VCPUs with APIC ID > 0xff will wrap-around,
  236. * prevent them from masking VCPUs with APIC ID <= 0xff.
  237. */
  238. if (!apic_x2apic_mode(apic) && !new->phys_map[xapic_id])
  239. new->phys_map[xapic_id] = apic;
  240. if (!kvm_apic_sw_enabled(apic))
  241. continue;
  242. ldr = kvm_lapic_get_reg(apic, APIC_LDR);
  243. if (apic_x2apic_mode(apic)) {
  244. new->mode |= KVM_APIC_MODE_X2APIC;
  245. } else if (ldr) {
  246. ldr = GET_APIC_LOGICAL_ID(ldr);
  247. if (kvm_lapic_get_reg(apic, APIC_DFR) == APIC_DFR_FLAT)
  248. new->mode |= KVM_APIC_MODE_XAPIC_FLAT;
  249. else
  250. new->mode |= KVM_APIC_MODE_XAPIC_CLUSTER;
  251. }
  252. if (!kvm_apic_map_get_logical_dest(new, ldr, &cluster, &mask))
  253. continue;
  254. if (mask)
  255. cluster[ffs(mask) - 1] = apic;
  256. }
  257. out:
  258. old = rcu_dereference_protected(kvm->arch.apic_map,
  259. lockdep_is_held(&kvm->arch.apic_map_lock));
  260. rcu_assign_pointer(kvm->arch.apic_map, new);
  261. /*
  262. * Write kvm->arch.apic_map before clearing apic->apic_map_dirty.
  263. * If another update has come in, leave it DIRTY.
  264. */
  265. atomic_cmpxchg_release(&kvm->arch.apic_map_dirty,
  266. UPDATE_IN_PROGRESS, CLEAN);
  267. mutex_unlock(&kvm->arch.apic_map_lock);
  268. if (old)
  269. call_rcu(&old->rcu, kvm_apic_map_free);
  270. kvm_make_scan_ioapic_request(kvm);
  271. }
  272. static inline void apic_set_spiv(struct kvm_lapic *apic, u32 val)
  273. {
  274. bool enabled = val & APIC_SPIV_APIC_ENABLED;
  275. kvm_lapic_set_reg(apic, APIC_SPIV, val);
  276. if (enabled != apic->sw_enabled) {
  277. apic->sw_enabled = enabled;
  278. if (enabled)
  279. static_branch_slow_dec_deferred(&apic_sw_disabled);
  280. else
  281. static_branch_inc(&apic_sw_disabled.key);
  282. atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
  283. }
  284. /* Check if there are APF page ready requests pending */
  285. if (enabled)
  286. kvm_make_request(KVM_REQ_APF_READY, apic->vcpu);
  287. }
  288. static inline void kvm_apic_set_xapic_id(struct kvm_lapic *apic, u8 id)
  289. {
  290. kvm_lapic_set_reg(apic, APIC_ID, id << 24);
  291. atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
  292. }
  293. static inline void kvm_apic_set_ldr(struct kvm_lapic *apic, u32 id)
  294. {
  295. kvm_lapic_set_reg(apic, APIC_LDR, id);
  296. atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
  297. }
  298. static inline void kvm_apic_set_dfr(struct kvm_lapic *apic, u32 val)
  299. {
  300. kvm_lapic_set_reg(apic, APIC_DFR, val);
  301. atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
  302. }
  303. static inline u32 kvm_apic_calc_x2apic_ldr(u32 id)
  304. {
  305. return ((id >> 4) << 16) | (1 << (id & 0xf));
  306. }
  307. static inline void kvm_apic_set_x2apic_id(struct kvm_lapic *apic, u32 id)
  308. {
  309. u32 ldr = kvm_apic_calc_x2apic_ldr(id);
  310. WARN_ON_ONCE(id != apic->vcpu->vcpu_id);
  311. kvm_lapic_set_reg(apic, APIC_ID, id);
  312. kvm_lapic_set_reg(apic, APIC_LDR, ldr);
  313. atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
  314. }
  315. static inline int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type)
  316. {
  317. return !(kvm_lapic_get_reg(apic, lvt_type) & APIC_LVT_MASKED);
  318. }
  319. static inline int apic_lvtt_oneshot(struct kvm_lapic *apic)
  320. {
  321. return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_ONESHOT;
  322. }
  323. static inline int apic_lvtt_period(struct kvm_lapic *apic)
  324. {
  325. return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_PERIODIC;
  326. }
  327. static inline int apic_lvtt_tscdeadline(struct kvm_lapic *apic)
  328. {
  329. return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_TSCDEADLINE;
  330. }
  331. static inline int apic_lvt_nmi_mode(u32 lvt_val)
  332. {
  333. return (lvt_val & (APIC_MODE_MASK | APIC_LVT_MASKED)) == APIC_DM_NMI;
  334. }
  335. static inline bool kvm_lapic_lvt_supported(struct kvm_lapic *apic, int lvt_index)
  336. {
  337. return apic->nr_lvt_entries > lvt_index;
  338. }
  339. static inline int kvm_apic_calc_nr_lvt_entries(struct kvm_vcpu *vcpu)
  340. {
  341. return KVM_APIC_MAX_NR_LVT_ENTRIES - !(vcpu->arch.mcg_cap & MCG_CMCI_P);
  342. }
  343. void kvm_apic_set_version(struct kvm_vcpu *vcpu)
  344. {
  345. struct kvm_lapic *apic = vcpu->arch.apic;
  346. u32 v = 0;
  347. if (!lapic_in_kernel(vcpu))
  348. return;
  349. v = APIC_VERSION | ((apic->nr_lvt_entries - 1) << 16);
  350. /*
  351. * KVM emulates 82093AA datasheet (with in-kernel IOAPIC implementation)
  352. * which doesn't have EOI register; Some buggy OSes (e.g. Windows with
  353. * Hyper-V role) disable EOI broadcast in lapic not checking for IOAPIC
  354. * version first and level-triggered interrupts never get EOIed in
  355. * IOAPIC.
  356. */
  357. if (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) &&
  358. !ioapic_in_kernel(vcpu->kvm))
  359. v |= APIC_LVR_DIRECTED_EOI;
  360. kvm_lapic_set_reg(apic, APIC_LVR, v);
  361. }
  362. void kvm_apic_after_set_mcg_cap(struct kvm_vcpu *vcpu)
  363. {
  364. int nr_lvt_entries = kvm_apic_calc_nr_lvt_entries(vcpu);
  365. struct kvm_lapic *apic = vcpu->arch.apic;
  366. int i;
  367. if (!lapic_in_kernel(vcpu) || nr_lvt_entries == apic->nr_lvt_entries)
  368. return;
  369. /* Initialize/mask any "new" LVT entries. */
  370. for (i = apic->nr_lvt_entries; i < nr_lvt_entries; i++)
  371. kvm_lapic_set_reg(apic, APIC_LVTx(i), APIC_LVT_MASKED);
  372. apic->nr_lvt_entries = nr_lvt_entries;
  373. /* The number of LVT entries is reflected in the version register. */
  374. kvm_apic_set_version(vcpu);
  375. }
  376. static const unsigned int apic_lvt_mask[KVM_APIC_MAX_NR_LVT_ENTRIES] = {
  377. [LVT_TIMER] = LVT_MASK, /* timer mode mask added at runtime */
  378. [LVT_THERMAL_MONITOR] = LVT_MASK | APIC_MODE_MASK,
  379. [LVT_PERFORMANCE_COUNTER] = LVT_MASK | APIC_MODE_MASK,
  380. [LVT_LINT0] = LINT_MASK,
  381. [LVT_LINT1] = LINT_MASK,
  382. [LVT_ERROR] = LVT_MASK,
  383. [LVT_CMCI] = LVT_MASK | APIC_MODE_MASK
  384. };
  385. static int find_highest_vector(void *bitmap)
  386. {
  387. int vec;
  388. u32 *reg;
  389. for (vec = MAX_APIC_VECTOR - APIC_VECTORS_PER_REG;
  390. vec >= 0; vec -= APIC_VECTORS_PER_REG) {
  391. reg = bitmap + REG_POS(vec);
  392. if (*reg)
  393. return __fls(*reg) + vec;
  394. }
  395. return -1;
  396. }
  397. static u8 count_vectors(void *bitmap)
  398. {
  399. int vec;
  400. u32 *reg;
  401. u8 count = 0;
  402. for (vec = 0; vec < MAX_APIC_VECTOR; vec += APIC_VECTORS_PER_REG) {
  403. reg = bitmap + REG_POS(vec);
  404. count += hweight32(*reg);
  405. }
  406. return count;
  407. }
  408. bool __kvm_apic_update_irr(u32 *pir, void *regs, int *max_irr)
  409. {
  410. u32 i, vec;
  411. u32 pir_val, irr_val, prev_irr_val;
  412. int max_updated_irr;
  413. max_updated_irr = -1;
  414. *max_irr = -1;
  415. for (i = vec = 0; i <= 7; i++, vec += 32) {
  416. pir_val = READ_ONCE(pir[i]);
  417. irr_val = *((u32 *)(regs + APIC_IRR + i * 0x10));
  418. if (pir_val) {
  419. prev_irr_val = irr_val;
  420. irr_val |= xchg(&pir[i], 0);
  421. *((u32 *)(regs + APIC_IRR + i * 0x10)) = irr_val;
  422. if (prev_irr_val != irr_val) {
  423. max_updated_irr =
  424. __fls(irr_val ^ prev_irr_val) + vec;
  425. }
  426. }
  427. if (irr_val)
  428. *max_irr = __fls(irr_val) + vec;
  429. }
  430. return ((max_updated_irr != -1) &&
  431. (max_updated_irr == *max_irr));
  432. }
  433. EXPORT_SYMBOL_GPL(__kvm_apic_update_irr);
  434. bool kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir, int *max_irr)
  435. {
  436. struct kvm_lapic *apic = vcpu->arch.apic;
  437. return __kvm_apic_update_irr(pir, apic->regs, max_irr);
  438. }
  439. EXPORT_SYMBOL_GPL(kvm_apic_update_irr);
  440. static inline int apic_search_irr(struct kvm_lapic *apic)
  441. {
  442. return find_highest_vector(apic->regs + APIC_IRR);
  443. }
  444. static inline int apic_find_highest_irr(struct kvm_lapic *apic)
  445. {
  446. int result;
  447. /*
  448. * Note that irr_pending is just a hint. It will be always
  449. * true with virtual interrupt delivery enabled.
  450. */
  451. if (!apic->irr_pending)
  452. return -1;
  453. result = apic_search_irr(apic);
  454. ASSERT(result == -1 || result >= 16);
  455. return result;
  456. }
  457. static inline void apic_clear_irr(int vec, struct kvm_lapic *apic)
  458. {
  459. if (unlikely(apic->apicv_active)) {
  460. /* need to update RVI */
  461. kvm_lapic_clear_vector(vec, apic->regs + APIC_IRR);
  462. static_call_cond(kvm_x86_hwapic_irr_update)(apic->vcpu,
  463. apic_find_highest_irr(apic));
  464. } else {
  465. apic->irr_pending = false;
  466. kvm_lapic_clear_vector(vec, apic->regs + APIC_IRR);
  467. if (apic_search_irr(apic) != -1)
  468. apic->irr_pending = true;
  469. }
  470. }
  471. void kvm_apic_clear_irr(struct kvm_vcpu *vcpu, int vec)
  472. {
  473. apic_clear_irr(vec, vcpu->arch.apic);
  474. }
  475. EXPORT_SYMBOL_GPL(kvm_apic_clear_irr);
  476. static inline void apic_set_isr(int vec, struct kvm_lapic *apic)
  477. {
  478. if (__apic_test_and_set_vector(vec, apic->regs + APIC_ISR))
  479. return;
  480. /*
  481. * With APIC virtualization enabled, all caching is disabled
  482. * because the processor can modify ISR under the hood. Instead
  483. * just set SVI.
  484. */
  485. if (unlikely(apic->apicv_active))
  486. static_call_cond(kvm_x86_hwapic_isr_update)(vec);
  487. else {
  488. ++apic->isr_count;
  489. BUG_ON(apic->isr_count > MAX_APIC_VECTOR);
  490. /*
  491. * ISR (in service register) bit is set when injecting an interrupt.
  492. * The highest vector is injected. Thus the latest bit set matches
  493. * the highest bit in ISR.
  494. */
  495. apic->highest_isr_cache = vec;
  496. }
  497. }
  498. static inline int apic_find_highest_isr(struct kvm_lapic *apic)
  499. {
  500. int result;
  501. /*
  502. * Note that isr_count is always 1, and highest_isr_cache
  503. * is always -1, with APIC virtualization enabled.
  504. */
  505. if (!apic->isr_count)
  506. return -1;
  507. if (likely(apic->highest_isr_cache != -1))
  508. return apic->highest_isr_cache;
  509. result = find_highest_vector(apic->regs + APIC_ISR);
  510. ASSERT(result == -1 || result >= 16);
  511. return result;
  512. }
  513. static inline void apic_clear_isr(int vec, struct kvm_lapic *apic)
  514. {
  515. if (!__apic_test_and_clear_vector(vec, apic->regs + APIC_ISR))
  516. return;
  517. /*
  518. * We do get here for APIC virtualization enabled if the guest
  519. * uses the Hyper-V APIC enlightenment. In this case we may need
  520. * to trigger a new interrupt delivery by writing the SVI field;
  521. * on the other hand isr_count and highest_isr_cache are unused
  522. * and must be left alone.
  523. */
  524. if (unlikely(apic->apicv_active))
  525. static_call_cond(kvm_x86_hwapic_isr_update)(apic_find_highest_isr(apic));
  526. else {
  527. --apic->isr_count;
  528. BUG_ON(apic->isr_count < 0);
  529. apic->highest_isr_cache = -1;
  530. }
  531. }
  532. int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu)
  533. {
  534. /* This may race with setting of irr in __apic_accept_irq() and
  535. * value returned may be wrong, but kvm_vcpu_kick() in __apic_accept_irq
  536. * will cause vmexit immediately and the value will be recalculated
  537. * on the next vmentry.
  538. */
  539. return apic_find_highest_irr(vcpu->arch.apic);
  540. }
  541. EXPORT_SYMBOL_GPL(kvm_lapic_find_highest_irr);
  542. static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
  543. int vector, int level, int trig_mode,
  544. struct dest_map *dest_map);
  545. int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq,
  546. struct dest_map *dest_map)
  547. {
  548. struct kvm_lapic *apic = vcpu->arch.apic;
  549. return __apic_accept_irq(apic, irq->delivery_mode, irq->vector,
  550. irq->level, irq->trig_mode, dest_map);
  551. }
  552. static int __pv_send_ipi(unsigned long *ipi_bitmap, struct kvm_apic_map *map,
  553. struct kvm_lapic_irq *irq, u32 min)
  554. {
  555. int i, count = 0;
  556. struct kvm_vcpu *vcpu;
  557. if (min > map->max_apic_id)
  558. return 0;
  559. for_each_set_bit(i, ipi_bitmap,
  560. min((u32)BITS_PER_LONG, (map->max_apic_id - min + 1))) {
  561. if (map->phys_map[min + i]) {
  562. vcpu = map->phys_map[min + i]->vcpu;
  563. count += kvm_apic_set_irq(vcpu, irq, NULL);
  564. }
  565. }
  566. return count;
  567. }
  568. int kvm_pv_send_ipi(struct kvm *kvm, unsigned long ipi_bitmap_low,
  569. unsigned long ipi_bitmap_high, u32 min,
  570. unsigned long icr, int op_64_bit)
  571. {
  572. struct kvm_apic_map *map;
  573. struct kvm_lapic_irq irq = {0};
  574. int cluster_size = op_64_bit ? 64 : 32;
  575. int count;
  576. if (icr & (APIC_DEST_MASK | APIC_SHORT_MASK))
  577. return -KVM_EINVAL;
  578. irq.vector = icr & APIC_VECTOR_MASK;
  579. irq.delivery_mode = icr & APIC_MODE_MASK;
  580. irq.level = (icr & APIC_INT_ASSERT) != 0;
  581. irq.trig_mode = icr & APIC_INT_LEVELTRIG;
  582. rcu_read_lock();
  583. map = rcu_dereference(kvm->arch.apic_map);
  584. count = -EOPNOTSUPP;
  585. if (likely(map)) {
  586. count = __pv_send_ipi(&ipi_bitmap_low, map, &irq, min);
  587. min += cluster_size;
  588. count += __pv_send_ipi(&ipi_bitmap_high, map, &irq, min);
  589. }
  590. rcu_read_unlock();
  591. return count;
  592. }
  593. static int pv_eoi_put_user(struct kvm_vcpu *vcpu, u8 val)
  594. {
  595. return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, &val,
  596. sizeof(val));
  597. }
  598. static int pv_eoi_get_user(struct kvm_vcpu *vcpu, u8 *val)
  599. {
  600. return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, val,
  601. sizeof(*val));
  602. }
  603. static inline bool pv_eoi_enabled(struct kvm_vcpu *vcpu)
  604. {
  605. return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED;
  606. }
  607. static void pv_eoi_set_pending(struct kvm_vcpu *vcpu)
  608. {
  609. if (pv_eoi_put_user(vcpu, KVM_PV_EOI_ENABLED) < 0)
  610. return;
  611. __set_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
  612. }
  613. static bool pv_eoi_test_and_clr_pending(struct kvm_vcpu *vcpu)
  614. {
  615. u8 val;
  616. if (pv_eoi_get_user(vcpu, &val) < 0)
  617. return false;
  618. val &= KVM_PV_EOI_ENABLED;
  619. if (val && pv_eoi_put_user(vcpu, KVM_PV_EOI_DISABLED) < 0)
  620. return false;
  621. /*
  622. * Clear pending bit in any case: it will be set again on vmentry.
  623. * While this might not be ideal from performance point of view,
  624. * this makes sure pv eoi is only enabled when we know it's safe.
  625. */
  626. __clear_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
  627. return val;
  628. }
  629. static int apic_has_interrupt_for_ppr(struct kvm_lapic *apic, u32 ppr)
  630. {
  631. int highest_irr;
  632. if (kvm_x86_ops.sync_pir_to_irr)
  633. highest_irr = static_call(kvm_x86_sync_pir_to_irr)(apic->vcpu);
  634. else
  635. highest_irr = apic_find_highest_irr(apic);
  636. if (highest_irr == -1 || (highest_irr & 0xF0) <= ppr)
  637. return -1;
  638. return highest_irr;
  639. }
  640. static bool __apic_update_ppr(struct kvm_lapic *apic, u32 *new_ppr)
  641. {
  642. u32 tpr, isrv, ppr, old_ppr;
  643. int isr;
  644. old_ppr = kvm_lapic_get_reg(apic, APIC_PROCPRI);
  645. tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI);
  646. isr = apic_find_highest_isr(apic);
  647. isrv = (isr != -1) ? isr : 0;
  648. if ((tpr & 0xf0) >= (isrv & 0xf0))
  649. ppr = tpr & 0xff;
  650. else
  651. ppr = isrv & 0xf0;
  652. *new_ppr = ppr;
  653. if (old_ppr != ppr)
  654. kvm_lapic_set_reg(apic, APIC_PROCPRI, ppr);
  655. return ppr < old_ppr;
  656. }
  657. static void apic_update_ppr(struct kvm_lapic *apic)
  658. {
  659. u32 ppr;
  660. if (__apic_update_ppr(apic, &ppr) &&
  661. apic_has_interrupt_for_ppr(apic, ppr) != -1)
  662. kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
  663. }
  664. void kvm_apic_update_ppr(struct kvm_vcpu *vcpu)
  665. {
  666. apic_update_ppr(vcpu->arch.apic);
  667. }
  668. EXPORT_SYMBOL_GPL(kvm_apic_update_ppr);
  669. static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr)
  670. {
  671. kvm_lapic_set_reg(apic, APIC_TASKPRI, tpr);
  672. apic_update_ppr(apic);
  673. }
  674. static bool kvm_apic_broadcast(struct kvm_lapic *apic, u32 mda)
  675. {
  676. return mda == (apic_x2apic_mode(apic) ?
  677. X2APIC_BROADCAST : APIC_BROADCAST);
  678. }
  679. static bool kvm_apic_match_physical_addr(struct kvm_lapic *apic, u32 mda)
  680. {
  681. if (kvm_apic_broadcast(apic, mda))
  682. return true;
  683. /*
  684. * Hotplug hack: Accept interrupts for vCPUs in xAPIC mode as if they
  685. * were in x2APIC mode if the target APIC ID can't be encoded as an
  686. * xAPIC ID. This allows unique addressing of hotplugged vCPUs (which
  687. * start in xAPIC mode) with an APIC ID that is unaddressable in xAPIC
  688. * mode. Match the x2APIC ID if and only if the target APIC ID can't
  689. * be encoded in xAPIC to avoid spurious matches against a vCPU that
  690. * changed its (addressable) xAPIC ID (which is writable).
  691. */
  692. if (apic_x2apic_mode(apic) || mda > 0xff)
  693. return mda == kvm_x2apic_id(apic);
  694. return mda == kvm_xapic_id(apic);
  695. }
  696. static bool kvm_apic_match_logical_addr(struct kvm_lapic *apic, u32 mda)
  697. {
  698. u32 logical_id;
  699. if (kvm_apic_broadcast(apic, mda))
  700. return true;
  701. logical_id = kvm_lapic_get_reg(apic, APIC_LDR);
  702. if (apic_x2apic_mode(apic))
  703. return ((logical_id >> 16) == (mda >> 16))
  704. && (logical_id & mda & 0xffff) != 0;
  705. logical_id = GET_APIC_LOGICAL_ID(logical_id);
  706. switch (kvm_lapic_get_reg(apic, APIC_DFR)) {
  707. case APIC_DFR_FLAT:
  708. return (logical_id & mda) != 0;
  709. case APIC_DFR_CLUSTER:
  710. return ((logical_id >> 4) == (mda >> 4))
  711. && (logical_id & mda & 0xf) != 0;
  712. default:
  713. return false;
  714. }
  715. }
  716. /* The KVM local APIC implementation has two quirks:
  717. *
  718. * - Real hardware delivers interrupts destined to x2APIC ID > 0xff to LAPICs
  719. * in xAPIC mode if the "destination & 0xff" matches its xAPIC ID.
  720. * KVM doesn't do that aliasing.
  721. *
  722. * - in-kernel IOAPIC messages have to be delivered directly to
  723. * x2APIC, because the kernel does not support interrupt remapping.
  724. * In order to support broadcast without interrupt remapping, x2APIC
  725. * rewrites the destination of non-IPI messages from APIC_BROADCAST
  726. * to X2APIC_BROADCAST.
  727. *
  728. * The broadcast quirk can be disabled with KVM_CAP_X2APIC_API. This is
  729. * important when userspace wants to use x2APIC-format MSIs, because
  730. * APIC_BROADCAST (0xff) is a legal route for "cluster 0, CPUs 0-7".
  731. */
  732. static u32 kvm_apic_mda(struct kvm_vcpu *vcpu, unsigned int dest_id,
  733. struct kvm_lapic *source, struct kvm_lapic *target)
  734. {
  735. bool ipi = source != NULL;
  736. if (!vcpu->kvm->arch.x2apic_broadcast_quirk_disabled &&
  737. !ipi && dest_id == APIC_BROADCAST && apic_x2apic_mode(target))
  738. return X2APIC_BROADCAST;
  739. return dest_id;
  740. }
  741. bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
  742. int shorthand, unsigned int dest, int dest_mode)
  743. {
  744. struct kvm_lapic *target = vcpu->arch.apic;
  745. u32 mda = kvm_apic_mda(vcpu, dest, source, target);
  746. ASSERT(target);
  747. switch (shorthand) {
  748. case APIC_DEST_NOSHORT:
  749. if (dest_mode == APIC_DEST_PHYSICAL)
  750. return kvm_apic_match_physical_addr(target, mda);
  751. else
  752. return kvm_apic_match_logical_addr(target, mda);
  753. case APIC_DEST_SELF:
  754. return target == source;
  755. case APIC_DEST_ALLINC:
  756. return true;
  757. case APIC_DEST_ALLBUT:
  758. return target != source;
  759. default:
  760. return false;
  761. }
  762. }
  763. EXPORT_SYMBOL_GPL(kvm_apic_match_dest);
  764. int kvm_vector_to_index(u32 vector, u32 dest_vcpus,
  765. const unsigned long *bitmap, u32 bitmap_size)
  766. {
  767. u32 mod;
  768. int i, idx = -1;
  769. mod = vector % dest_vcpus;
  770. for (i = 0; i <= mod; i++) {
  771. idx = find_next_bit(bitmap, bitmap_size, idx + 1);
  772. BUG_ON(idx == bitmap_size);
  773. }
  774. return idx;
  775. }
  776. static void kvm_apic_disabled_lapic_found(struct kvm *kvm)
  777. {
  778. if (!kvm->arch.disabled_lapic_found) {
  779. kvm->arch.disabled_lapic_found = true;
  780. printk(KERN_INFO
  781. "Disabled LAPIC found during irq injection\n");
  782. }
  783. }
  784. static bool kvm_apic_is_broadcast_dest(struct kvm *kvm, struct kvm_lapic **src,
  785. struct kvm_lapic_irq *irq, struct kvm_apic_map *map)
  786. {
  787. if (kvm->arch.x2apic_broadcast_quirk_disabled) {
  788. if ((irq->dest_id == APIC_BROADCAST &&
  789. map->mode != KVM_APIC_MODE_X2APIC))
  790. return true;
  791. if (irq->dest_id == X2APIC_BROADCAST)
  792. return true;
  793. } else {
  794. bool x2apic_ipi = src && *src && apic_x2apic_mode(*src);
  795. if (irq->dest_id == (x2apic_ipi ?
  796. X2APIC_BROADCAST : APIC_BROADCAST))
  797. return true;
  798. }
  799. return false;
  800. }
  801. /* Return true if the interrupt can be handled by using *bitmap as index mask
  802. * for valid destinations in *dst array.
  803. * Return false if kvm_apic_map_get_dest_lapic did nothing useful.
  804. * Note: we may have zero kvm_lapic destinations when we return true, which
  805. * means that the interrupt should be dropped. In this case, *bitmap would be
  806. * zero and *dst undefined.
  807. */
  808. static inline bool kvm_apic_map_get_dest_lapic(struct kvm *kvm,
  809. struct kvm_lapic **src, struct kvm_lapic_irq *irq,
  810. struct kvm_apic_map *map, struct kvm_lapic ***dst,
  811. unsigned long *bitmap)
  812. {
  813. int i, lowest;
  814. if (irq->shorthand == APIC_DEST_SELF && src) {
  815. *dst = src;
  816. *bitmap = 1;
  817. return true;
  818. } else if (irq->shorthand)
  819. return false;
  820. if (!map || kvm_apic_is_broadcast_dest(kvm, src, irq, map))
  821. return false;
  822. if (irq->dest_mode == APIC_DEST_PHYSICAL) {
  823. if (irq->dest_id > map->max_apic_id) {
  824. *bitmap = 0;
  825. } else {
  826. u32 dest_id = array_index_nospec(irq->dest_id, map->max_apic_id + 1);
  827. *dst = &map->phys_map[dest_id];
  828. *bitmap = 1;
  829. }
  830. return true;
  831. }
  832. *bitmap = 0;
  833. if (!kvm_apic_map_get_logical_dest(map, irq->dest_id, dst,
  834. (u16 *)bitmap))
  835. return false;
  836. if (!kvm_lowest_prio_delivery(irq))
  837. return true;
  838. if (!kvm_vector_hashing_enabled()) {
  839. lowest = -1;
  840. for_each_set_bit(i, bitmap, 16) {
  841. if (!(*dst)[i])
  842. continue;
  843. if (lowest < 0)
  844. lowest = i;
  845. else if (kvm_apic_compare_prio((*dst)[i]->vcpu,
  846. (*dst)[lowest]->vcpu) < 0)
  847. lowest = i;
  848. }
  849. } else {
  850. if (!*bitmap)
  851. return true;
  852. lowest = kvm_vector_to_index(irq->vector, hweight16(*bitmap),
  853. bitmap, 16);
  854. if (!(*dst)[lowest]) {
  855. kvm_apic_disabled_lapic_found(kvm);
  856. *bitmap = 0;
  857. return true;
  858. }
  859. }
  860. *bitmap = (lowest >= 0) ? 1 << lowest : 0;
  861. return true;
  862. }
  863. bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
  864. struct kvm_lapic_irq *irq, int *r, struct dest_map *dest_map)
  865. {
  866. struct kvm_apic_map *map;
  867. unsigned long bitmap;
  868. struct kvm_lapic **dst = NULL;
  869. int i;
  870. bool ret;
  871. *r = -1;
  872. if (irq->shorthand == APIC_DEST_SELF) {
  873. if (KVM_BUG_ON(!src, kvm)) {
  874. *r = 0;
  875. return true;
  876. }
  877. *r = kvm_apic_set_irq(src->vcpu, irq, dest_map);
  878. return true;
  879. }
  880. rcu_read_lock();
  881. map = rcu_dereference(kvm->arch.apic_map);
  882. ret = kvm_apic_map_get_dest_lapic(kvm, &src, irq, map, &dst, &bitmap);
  883. if (ret) {
  884. *r = 0;
  885. for_each_set_bit(i, &bitmap, 16) {
  886. if (!dst[i])
  887. continue;
  888. *r += kvm_apic_set_irq(dst[i]->vcpu, irq, dest_map);
  889. }
  890. }
  891. rcu_read_unlock();
  892. return ret;
  893. }
  894. /*
  895. * This routine tries to handle interrupts in posted mode, here is how
  896. * it deals with different cases:
  897. * - For single-destination interrupts, handle it in posted mode
  898. * - Else if vector hashing is enabled and it is a lowest-priority
  899. * interrupt, handle it in posted mode and use the following mechanism
  900. * to find the destination vCPU.
  901. * 1. For lowest-priority interrupts, store all the possible
  902. * destination vCPUs in an array.
  903. * 2. Use "guest vector % max number of destination vCPUs" to find
  904. * the right destination vCPU in the array for the lowest-priority
  905. * interrupt.
  906. * - Otherwise, use remapped mode to inject the interrupt.
  907. */
  908. bool kvm_intr_is_single_vcpu_fast(struct kvm *kvm, struct kvm_lapic_irq *irq,
  909. struct kvm_vcpu **dest_vcpu)
  910. {
  911. struct kvm_apic_map *map;
  912. unsigned long bitmap;
  913. struct kvm_lapic **dst = NULL;
  914. bool ret = false;
  915. if (irq->shorthand)
  916. return false;
  917. rcu_read_lock();
  918. map = rcu_dereference(kvm->arch.apic_map);
  919. if (kvm_apic_map_get_dest_lapic(kvm, NULL, irq, map, &dst, &bitmap) &&
  920. hweight16(bitmap) == 1) {
  921. unsigned long i = find_first_bit(&bitmap, 16);
  922. if (dst[i]) {
  923. *dest_vcpu = dst[i]->vcpu;
  924. ret = true;
  925. }
  926. }
  927. rcu_read_unlock();
  928. return ret;
  929. }
  930. /*
  931. * Add a pending IRQ into lapic.
  932. * Return 1 if successfully added and 0 if discarded.
  933. */
  934. static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
  935. int vector, int level, int trig_mode,
  936. struct dest_map *dest_map)
  937. {
  938. int result = 0;
  939. struct kvm_vcpu *vcpu = apic->vcpu;
  940. trace_kvm_apic_accept_irq(vcpu->vcpu_id, delivery_mode,
  941. trig_mode, vector);
  942. switch (delivery_mode) {
  943. case APIC_DM_LOWEST:
  944. vcpu->arch.apic_arb_prio++;
  945. fallthrough;
  946. case APIC_DM_FIXED:
  947. if (unlikely(trig_mode && !level))
  948. break;
  949. /* FIXME add logic for vcpu on reset */
  950. if (unlikely(!apic_enabled(apic)))
  951. break;
  952. result = 1;
  953. if (dest_map) {
  954. __set_bit(vcpu->vcpu_id, dest_map->map);
  955. dest_map->vectors[vcpu->vcpu_id] = vector;
  956. }
  957. if (apic_test_vector(vector, apic->regs + APIC_TMR) != !!trig_mode) {
  958. if (trig_mode)
  959. kvm_lapic_set_vector(vector,
  960. apic->regs + APIC_TMR);
  961. else
  962. kvm_lapic_clear_vector(vector,
  963. apic->regs + APIC_TMR);
  964. }
  965. static_call(kvm_x86_deliver_interrupt)(apic, delivery_mode,
  966. trig_mode, vector);
  967. break;
  968. case APIC_DM_REMRD:
  969. result = 1;
  970. vcpu->arch.pv.pv_unhalted = 1;
  971. kvm_make_request(KVM_REQ_EVENT, vcpu);
  972. kvm_vcpu_kick(vcpu);
  973. break;
  974. case APIC_DM_SMI:
  975. result = 1;
  976. kvm_make_request(KVM_REQ_SMI, vcpu);
  977. kvm_vcpu_kick(vcpu);
  978. break;
  979. case APIC_DM_NMI:
  980. result = 1;
  981. kvm_inject_nmi(vcpu);
  982. kvm_vcpu_kick(vcpu);
  983. break;
  984. case APIC_DM_INIT:
  985. if (!trig_mode || level) {
  986. result = 1;
  987. /* assumes that there are only KVM_APIC_INIT/SIPI */
  988. apic->pending_events = (1UL << KVM_APIC_INIT);
  989. kvm_make_request(KVM_REQ_EVENT, vcpu);
  990. kvm_vcpu_kick(vcpu);
  991. }
  992. break;
  993. case APIC_DM_STARTUP:
  994. result = 1;
  995. apic->sipi_vector = vector;
  996. /* make sure sipi_vector is visible for the receiver */
  997. smp_wmb();
  998. set_bit(KVM_APIC_SIPI, &apic->pending_events);
  999. kvm_make_request(KVM_REQ_EVENT, vcpu);
  1000. kvm_vcpu_kick(vcpu);
  1001. break;
  1002. case APIC_DM_EXTINT:
  1003. /*
  1004. * Should only be called by kvm_apic_local_deliver() with LVT0,
  1005. * before NMI watchdog was enabled. Already handled by
  1006. * kvm_apic_accept_pic_intr().
  1007. */
  1008. break;
  1009. default:
  1010. printk(KERN_ERR "TODO: unsupported delivery mode %x\n",
  1011. delivery_mode);
  1012. break;
  1013. }
  1014. return result;
  1015. }
  1016. /*
  1017. * This routine identifies the destination vcpus mask meant to receive the
  1018. * IOAPIC interrupts. It either uses kvm_apic_map_get_dest_lapic() to find
  1019. * out the destination vcpus array and set the bitmap or it traverses to
  1020. * each available vcpu to identify the same.
  1021. */
  1022. void kvm_bitmap_or_dest_vcpus(struct kvm *kvm, struct kvm_lapic_irq *irq,
  1023. unsigned long *vcpu_bitmap)
  1024. {
  1025. struct kvm_lapic **dest_vcpu = NULL;
  1026. struct kvm_lapic *src = NULL;
  1027. struct kvm_apic_map *map;
  1028. struct kvm_vcpu *vcpu;
  1029. unsigned long bitmap, i;
  1030. int vcpu_idx;
  1031. bool ret;
  1032. rcu_read_lock();
  1033. map = rcu_dereference(kvm->arch.apic_map);
  1034. ret = kvm_apic_map_get_dest_lapic(kvm, &src, irq, map, &dest_vcpu,
  1035. &bitmap);
  1036. if (ret) {
  1037. for_each_set_bit(i, &bitmap, 16) {
  1038. if (!dest_vcpu[i])
  1039. continue;
  1040. vcpu_idx = dest_vcpu[i]->vcpu->vcpu_idx;
  1041. __set_bit(vcpu_idx, vcpu_bitmap);
  1042. }
  1043. } else {
  1044. kvm_for_each_vcpu(i, vcpu, kvm) {
  1045. if (!kvm_apic_present(vcpu))
  1046. continue;
  1047. if (!kvm_apic_match_dest(vcpu, NULL,
  1048. irq->shorthand,
  1049. irq->dest_id,
  1050. irq->dest_mode))
  1051. continue;
  1052. __set_bit(i, vcpu_bitmap);
  1053. }
  1054. }
  1055. rcu_read_unlock();
  1056. }
  1057. int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
  1058. {
  1059. return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
  1060. }
  1061. static bool kvm_ioapic_handles_vector(struct kvm_lapic *apic, int vector)
  1062. {
  1063. return test_bit(vector, apic->vcpu->arch.ioapic_handled_vectors);
  1064. }
  1065. static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
  1066. {
  1067. int trigger_mode;
  1068. /* Eoi the ioapic only if the ioapic doesn't own the vector. */
  1069. if (!kvm_ioapic_handles_vector(apic, vector))
  1070. return;
  1071. /* Request a KVM exit to inform the userspace IOAPIC. */
  1072. if (irqchip_split(apic->vcpu->kvm)) {
  1073. apic->vcpu->arch.pending_ioapic_eoi = vector;
  1074. kvm_make_request(KVM_REQ_IOAPIC_EOI_EXIT, apic->vcpu);
  1075. return;
  1076. }
  1077. if (apic_test_vector(vector, apic->regs + APIC_TMR))
  1078. trigger_mode = IOAPIC_LEVEL_TRIG;
  1079. else
  1080. trigger_mode = IOAPIC_EDGE_TRIG;
  1081. kvm_ioapic_update_eoi(apic->vcpu, vector, trigger_mode);
  1082. }
  1083. static int apic_set_eoi(struct kvm_lapic *apic)
  1084. {
  1085. int vector = apic_find_highest_isr(apic);
  1086. trace_kvm_eoi(apic, vector);
  1087. /*
  1088. * Not every write EOI will has corresponding ISR,
  1089. * one example is when Kernel check timer on setup_IO_APIC
  1090. */
  1091. if (vector == -1)
  1092. return vector;
  1093. apic_clear_isr(vector, apic);
  1094. apic_update_ppr(apic);
  1095. if (to_hv_vcpu(apic->vcpu) &&
  1096. test_bit(vector, to_hv_synic(apic->vcpu)->vec_bitmap))
  1097. kvm_hv_synic_send_eoi(apic->vcpu, vector);
  1098. kvm_ioapic_send_eoi(apic, vector);
  1099. kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
  1100. return vector;
  1101. }
  1102. /*
  1103. * this interface assumes a trap-like exit, which has already finished
  1104. * desired side effect including vISR and vPPR update.
  1105. */
  1106. void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
  1107. {
  1108. struct kvm_lapic *apic = vcpu->arch.apic;
  1109. trace_kvm_eoi(apic, vector);
  1110. kvm_ioapic_send_eoi(apic, vector);
  1111. kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
  1112. }
  1113. EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
  1114. void kvm_apic_send_ipi(struct kvm_lapic *apic, u32 icr_low, u32 icr_high)
  1115. {
  1116. struct kvm_lapic_irq irq;
  1117. /* KVM has no delay and should always clear the BUSY/PENDING flag. */
  1118. WARN_ON_ONCE(icr_low & APIC_ICR_BUSY);
  1119. irq.vector = icr_low & APIC_VECTOR_MASK;
  1120. irq.delivery_mode = icr_low & APIC_MODE_MASK;
  1121. irq.dest_mode = icr_low & APIC_DEST_MASK;
  1122. irq.level = (icr_low & APIC_INT_ASSERT) != 0;
  1123. irq.trig_mode = icr_low & APIC_INT_LEVELTRIG;
  1124. irq.shorthand = icr_low & APIC_SHORT_MASK;
  1125. irq.msi_redir_hint = false;
  1126. if (apic_x2apic_mode(apic))
  1127. irq.dest_id = icr_high;
  1128. else
  1129. irq.dest_id = GET_XAPIC_DEST_FIELD(icr_high);
  1130. trace_kvm_apic_ipi(icr_low, irq.dest_id);
  1131. kvm_irq_delivery_to_apic(apic->vcpu->kvm, apic, &irq, NULL);
  1132. }
  1133. EXPORT_SYMBOL_GPL(kvm_apic_send_ipi);
  1134. static u32 apic_get_tmcct(struct kvm_lapic *apic)
  1135. {
  1136. ktime_t remaining, now;
  1137. s64 ns;
  1138. u32 tmcct;
  1139. ASSERT(apic != NULL);
  1140. /* if initial count is 0, current count should also be 0 */
  1141. if (kvm_lapic_get_reg(apic, APIC_TMICT) == 0 ||
  1142. apic->lapic_timer.period == 0)
  1143. return 0;
  1144. now = ktime_get();
  1145. remaining = ktime_sub(apic->lapic_timer.target_expiration, now);
  1146. if (ktime_to_ns(remaining) < 0)
  1147. remaining = 0;
  1148. ns = mod_64(ktime_to_ns(remaining), apic->lapic_timer.period);
  1149. tmcct = div64_u64(ns,
  1150. (APIC_BUS_CYCLE_NS * apic->divide_count));
  1151. return tmcct;
  1152. }
  1153. static void __report_tpr_access(struct kvm_lapic *apic, bool write)
  1154. {
  1155. struct kvm_vcpu *vcpu = apic->vcpu;
  1156. struct kvm_run *run = vcpu->run;
  1157. kvm_make_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu);
  1158. run->tpr_access.rip = kvm_rip_read(vcpu);
  1159. run->tpr_access.is_write = write;
  1160. }
  1161. static inline void report_tpr_access(struct kvm_lapic *apic, bool write)
  1162. {
  1163. if (apic->vcpu->arch.tpr_access_reporting)
  1164. __report_tpr_access(apic, write);
  1165. }
  1166. static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset)
  1167. {
  1168. u32 val = 0;
  1169. if (offset >= LAPIC_MMIO_LENGTH)
  1170. return 0;
  1171. switch (offset) {
  1172. case APIC_ARBPRI:
  1173. break;
  1174. case APIC_TMCCT: /* Timer CCR */
  1175. if (apic_lvtt_tscdeadline(apic))
  1176. return 0;
  1177. val = apic_get_tmcct(apic);
  1178. break;
  1179. case APIC_PROCPRI:
  1180. apic_update_ppr(apic);
  1181. val = kvm_lapic_get_reg(apic, offset);
  1182. break;
  1183. case APIC_TASKPRI:
  1184. report_tpr_access(apic, false);
  1185. fallthrough;
  1186. default:
  1187. val = kvm_lapic_get_reg(apic, offset);
  1188. break;
  1189. }
  1190. return val;
  1191. }
  1192. static inline struct kvm_lapic *to_lapic(struct kvm_io_device *dev)
  1193. {
  1194. return container_of(dev, struct kvm_lapic, dev);
  1195. }
  1196. #define APIC_REG_MASK(reg) (1ull << ((reg) >> 4))
  1197. #define APIC_REGS_MASK(first, count) \
  1198. (APIC_REG_MASK(first) * ((1ull << (count)) - 1))
  1199. static int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
  1200. void *data)
  1201. {
  1202. unsigned char alignment = offset & 0xf;
  1203. u32 result;
  1204. /* this bitmask has a bit cleared for each reserved register */
  1205. u64 valid_reg_mask =
  1206. APIC_REG_MASK(APIC_ID) |
  1207. APIC_REG_MASK(APIC_LVR) |
  1208. APIC_REG_MASK(APIC_TASKPRI) |
  1209. APIC_REG_MASK(APIC_PROCPRI) |
  1210. APIC_REG_MASK(APIC_LDR) |
  1211. APIC_REG_MASK(APIC_DFR) |
  1212. APIC_REG_MASK(APIC_SPIV) |
  1213. APIC_REGS_MASK(APIC_ISR, APIC_ISR_NR) |
  1214. APIC_REGS_MASK(APIC_TMR, APIC_ISR_NR) |
  1215. APIC_REGS_MASK(APIC_IRR, APIC_ISR_NR) |
  1216. APIC_REG_MASK(APIC_ESR) |
  1217. APIC_REG_MASK(APIC_ICR) |
  1218. APIC_REG_MASK(APIC_LVTT) |
  1219. APIC_REG_MASK(APIC_LVTTHMR) |
  1220. APIC_REG_MASK(APIC_LVTPC) |
  1221. APIC_REG_MASK(APIC_LVT0) |
  1222. APIC_REG_MASK(APIC_LVT1) |
  1223. APIC_REG_MASK(APIC_LVTERR) |
  1224. APIC_REG_MASK(APIC_TMICT) |
  1225. APIC_REG_MASK(APIC_TMCCT) |
  1226. APIC_REG_MASK(APIC_TDCR);
  1227. if (kvm_lapic_lvt_supported(apic, LVT_CMCI))
  1228. valid_reg_mask |= APIC_REG_MASK(APIC_LVTCMCI);
  1229. /*
  1230. * ARBPRI and ICR2 are not valid in x2APIC mode. WARN if KVM reads ICR
  1231. * in x2APIC mode as it's an 8-byte register in x2APIC and needs to be
  1232. * manually handled by the caller.
  1233. */
  1234. if (!apic_x2apic_mode(apic))
  1235. valid_reg_mask |= APIC_REG_MASK(APIC_ARBPRI) |
  1236. APIC_REG_MASK(APIC_ICR2);
  1237. else
  1238. WARN_ON_ONCE(offset == APIC_ICR);
  1239. if (alignment + len > 4)
  1240. return 1;
  1241. if (offset > 0x3f0 || !(valid_reg_mask & APIC_REG_MASK(offset)))
  1242. return 1;
  1243. result = __apic_read(apic, offset & ~0xf);
  1244. trace_kvm_apic_read(offset, result);
  1245. switch (len) {
  1246. case 1:
  1247. case 2:
  1248. case 4:
  1249. memcpy(data, (char *)&result + alignment, len);
  1250. break;
  1251. default:
  1252. printk(KERN_ERR "Local APIC read with len = %x, "
  1253. "should be 1,2, or 4 instead\n", len);
  1254. break;
  1255. }
  1256. return 0;
  1257. }
  1258. static int apic_mmio_in_range(struct kvm_lapic *apic, gpa_t addr)
  1259. {
  1260. return addr >= apic->base_address &&
  1261. addr < apic->base_address + LAPIC_MMIO_LENGTH;
  1262. }
  1263. static int apic_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
  1264. gpa_t address, int len, void *data)
  1265. {
  1266. struct kvm_lapic *apic = to_lapic(this);
  1267. u32 offset = address - apic->base_address;
  1268. if (!apic_mmio_in_range(apic, address))
  1269. return -EOPNOTSUPP;
  1270. if (!kvm_apic_hw_enabled(apic) || apic_x2apic_mode(apic)) {
  1271. if (!kvm_check_has_quirk(vcpu->kvm,
  1272. KVM_X86_QUIRK_LAPIC_MMIO_HOLE))
  1273. return -EOPNOTSUPP;
  1274. memset(data, 0xff, len);
  1275. return 0;
  1276. }
  1277. kvm_lapic_reg_read(apic, offset, len, data);
  1278. return 0;
  1279. }
  1280. static void update_divide_count(struct kvm_lapic *apic)
  1281. {
  1282. u32 tmp1, tmp2, tdcr;
  1283. tdcr = kvm_lapic_get_reg(apic, APIC_TDCR);
  1284. tmp1 = tdcr & 0xf;
  1285. tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
  1286. apic->divide_count = 0x1 << (tmp2 & 0x7);
  1287. }
  1288. static void limit_periodic_timer_frequency(struct kvm_lapic *apic)
  1289. {
  1290. /*
  1291. * Do not allow the guest to program periodic timers with small
  1292. * interval, since the hrtimers are not throttled by the host
  1293. * scheduler.
  1294. */
  1295. if (apic_lvtt_period(apic) && apic->lapic_timer.period) {
  1296. s64 min_period = min_timer_period_us * 1000LL;
  1297. if (apic->lapic_timer.period < min_period) {
  1298. pr_info_ratelimited(
  1299. "kvm: vcpu %i: requested %lld ns "
  1300. "lapic timer period limited to %lld ns\n",
  1301. apic->vcpu->vcpu_id,
  1302. apic->lapic_timer.period, min_period);
  1303. apic->lapic_timer.period = min_period;
  1304. }
  1305. }
  1306. }
  1307. static void cancel_hv_timer(struct kvm_lapic *apic);
  1308. static void cancel_apic_timer(struct kvm_lapic *apic)
  1309. {
  1310. hrtimer_cancel(&apic->lapic_timer.timer);
  1311. preempt_disable();
  1312. if (apic->lapic_timer.hv_timer_in_use)
  1313. cancel_hv_timer(apic);
  1314. preempt_enable();
  1315. atomic_set(&apic->lapic_timer.pending, 0);
  1316. }
  1317. static void apic_update_lvtt(struct kvm_lapic *apic)
  1318. {
  1319. u32 timer_mode = kvm_lapic_get_reg(apic, APIC_LVTT) &
  1320. apic->lapic_timer.timer_mode_mask;
  1321. if (apic->lapic_timer.timer_mode != timer_mode) {
  1322. if (apic_lvtt_tscdeadline(apic) != (timer_mode ==
  1323. APIC_LVT_TIMER_TSCDEADLINE)) {
  1324. cancel_apic_timer(apic);
  1325. kvm_lapic_set_reg(apic, APIC_TMICT, 0);
  1326. apic->lapic_timer.period = 0;
  1327. apic->lapic_timer.tscdeadline = 0;
  1328. }
  1329. apic->lapic_timer.timer_mode = timer_mode;
  1330. limit_periodic_timer_frequency(apic);
  1331. }
  1332. }
  1333. /*
  1334. * On APICv, this test will cause a busy wait
  1335. * during a higher-priority task.
  1336. */
  1337. static bool lapic_timer_int_injected(struct kvm_vcpu *vcpu)
  1338. {
  1339. struct kvm_lapic *apic = vcpu->arch.apic;
  1340. u32 reg = kvm_lapic_get_reg(apic, APIC_LVTT);
  1341. if (kvm_apic_hw_enabled(apic)) {
  1342. int vec = reg & APIC_VECTOR_MASK;
  1343. void *bitmap = apic->regs + APIC_ISR;
  1344. if (apic->apicv_active)
  1345. bitmap = apic->regs + APIC_IRR;
  1346. if (apic_test_vector(vec, bitmap))
  1347. return true;
  1348. }
  1349. return false;
  1350. }
  1351. static inline void __wait_lapic_expire(struct kvm_vcpu *vcpu, u64 guest_cycles)
  1352. {
  1353. u64 timer_advance_ns = vcpu->arch.apic->lapic_timer.timer_advance_ns;
  1354. /*
  1355. * If the guest TSC is running at a different ratio than the host, then
  1356. * convert the delay to nanoseconds to achieve an accurate delay. Note
  1357. * that __delay() uses delay_tsc whenever the hardware has TSC, thus
  1358. * always for VMX enabled hardware.
  1359. */
  1360. if (vcpu->arch.tsc_scaling_ratio == kvm_caps.default_tsc_scaling_ratio) {
  1361. __delay(min(guest_cycles,
  1362. nsec_to_cycles(vcpu, timer_advance_ns)));
  1363. } else {
  1364. u64 delay_ns = guest_cycles * 1000000ULL;
  1365. do_div(delay_ns, vcpu->arch.virtual_tsc_khz);
  1366. ndelay(min_t(u32, delay_ns, timer_advance_ns));
  1367. }
  1368. }
  1369. static inline void adjust_lapic_timer_advance(struct kvm_vcpu *vcpu,
  1370. s64 advance_expire_delta)
  1371. {
  1372. struct kvm_lapic *apic = vcpu->arch.apic;
  1373. u32 timer_advance_ns = apic->lapic_timer.timer_advance_ns;
  1374. u64 ns;
  1375. /* Do not adjust for tiny fluctuations or large random spikes. */
  1376. if (abs(advance_expire_delta) > LAPIC_TIMER_ADVANCE_ADJUST_MAX ||
  1377. abs(advance_expire_delta) < LAPIC_TIMER_ADVANCE_ADJUST_MIN)
  1378. return;
  1379. /* too early */
  1380. if (advance_expire_delta < 0) {
  1381. ns = -advance_expire_delta * 1000000ULL;
  1382. do_div(ns, vcpu->arch.virtual_tsc_khz);
  1383. timer_advance_ns -= ns/LAPIC_TIMER_ADVANCE_ADJUST_STEP;
  1384. } else {
  1385. /* too late */
  1386. ns = advance_expire_delta * 1000000ULL;
  1387. do_div(ns, vcpu->arch.virtual_tsc_khz);
  1388. timer_advance_ns += ns/LAPIC_TIMER_ADVANCE_ADJUST_STEP;
  1389. }
  1390. if (unlikely(timer_advance_ns > LAPIC_TIMER_ADVANCE_NS_MAX))
  1391. timer_advance_ns = LAPIC_TIMER_ADVANCE_NS_INIT;
  1392. apic->lapic_timer.timer_advance_ns = timer_advance_ns;
  1393. }
  1394. static void __kvm_wait_lapic_expire(struct kvm_vcpu *vcpu)
  1395. {
  1396. struct kvm_lapic *apic = vcpu->arch.apic;
  1397. u64 guest_tsc, tsc_deadline;
  1398. tsc_deadline = apic->lapic_timer.expired_tscdeadline;
  1399. apic->lapic_timer.expired_tscdeadline = 0;
  1400. guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
  1401. trace_kvm_wait_lapic_expire(vcpu->vcpu_id, guest_tsc - tsc_deadline);
  1402. if (lapic_timer_advance_dynamic) {
  1403. adjust_lapic_timer_advance(vcpu, guest_tsc - tsc_deadline);
  1404. /*
  1405. * If the timer fired early, reread the TSC to account for the
  1406. * overhead of the above adjustment to avoid waiting longer
  1407. * than is necessary.
  1408. */
  1409. if (guest_tsc < tsc_deadline)
  1410. guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
  1411. }
  1412. if (guest_tsc < tsc_deadline)
  1413. __wait_lapic_expire(vcpu, tsc_deadline - guest_tsc);
  1414. }
  1415. void kvm_wait_lapic_expire(struct kvm_vcpu *vcpu)
  1416. {
  1417. if (lapic_in_kernel(vcpu) &&
  1418. vcpu->arch.apic->lapic_timer.expired_tscdeadline &&
  1419. vcpu->arch.apic->lapic_timer.timer_advance_ns &&
  1420. lapic_timer_int_injected(vcpu))
  1421. __kvm_wait_lapic_expire(vcpu);
  1422. }
  1423. EXPORT_SYMBOL_GPL(kvm_wait_lapic_expire);
  1424. static void kvm_apic_inject_pending_timer_irqs(struct kvm_lapic *apic)
  1425. {
  1426. struct kvm_timer *ktimer = &apic->lapic_timer;
  1427. kvm_apic_local_deliver(apic, APIC_LVTT);
  1428. if (apic_lvtt_tscdeadline(apic)) {
  1429. ktimer->tscdeadline = 0;
  1430. } else if (apic_lvtt_oneshot(apic)) {
  1431. ktimer->tscdeadline = 0;
  1432. ktimer->target_expiration = 0;
  1433. }
  1434. }
  1435. static void apic_timer_expired(struct kvm_lapic *apic, bool from_timer_fn)
  1436. {
  1437. struct kvm_vcpu *vcpu = apic->vcpu;
  1438. struct kvm_timer *ktimer = &apic->lapic_timer;
  1439. if (atomic_read(&apic->lapic_timer.pending))
  1440. return;
  1441. if (apic_lvtt_tscdeadline(apic) || ktimer->hv_timer_in_use)
  1442. ktimer->expired_tscdeadline = ktimer->tscdeadline;
  1443. if (!from_timer_fn && apic->apicv_active) {
  1444. WARN_ON(kvm_get_running_vcpu() != vcpu);
  1445. kvm_apic_inject_pending_timer_irqs(apic);
  1446. return;
  1447. }
  1448. if (kvm_use_posted_timer_interrupt(apic->vcpu)) {
  1449. /*
  1450. * Ensure the guest's timer has truly expired before posting an
  1451. * interrupt. Open code the relevant checks to avoid querying
  1452. * lapic_timer_int_injected(), which will be false since the
  1453. * interrupt isn't yet injected. Waiting until after injecting
  1454. * is not an option since that won't help a posted interrupt.
  1455. */
  1456. if (vcpu->arch.apic->lapic_timer.expired_tscdeadline &&
  1457. vcpu->arch.apic->lapic_timer.timer_advance_ns)
  1458. __kvm_wait_lapic_expire(vcpu);
  1459. kvm_apic_inject_pending_timer_irqs(apic);
  1460. return;
  1461. }
  1462. atomic_inc(&apic->lapic_timer.pending);
  1463. kvm_make_request(KVM_REQ_UNBLOCK, vcpu);
  1464. if (from_timer_fn)
  1465. kvm_vcpu_kick(vcpu);
  1466. }
  1467. static void start_sw_tscdeadline(struct kvm_lapic *apic)
  1468. {
  1469. struct kvm_timer *ktimer = &apic->lapic_timer;
  1470. u64 guest_tsc, tscdeadline = ktimer->tscdeadline;
  1471. u64 ns = 0;
  1472. ktime_t expire;
  1473. struct kvm_vcpu *vcpu = apic->vcpu;
  1474. unsigned long this_tsc_khz = vcpu->arch.virtual_tsc_khz;
  1475. unsigned long flags;
  1476. ktime_t now;
  1477. if (unlikely(!tscdeadline || !this_tsc_khz))
  1478. return;
  1479. local_irq_save(flags);
  1480. now = ktime_get();
  1481. guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
  1482. ns = (tscdeadline - guest_tsc) * 1000000ULL;
  1483. do_div(ns, this_tsc_khz);
  1484. if (likely(tscdeadline > guest_tsc) &&
  1485. likely(ns > apic->lapic_timer.timer_advance_ns)) {
  1486. expire = ktime_add_ns(now, ns);
  1487. expire = ktime_sub_ns(expire, ktimer->timer_advance_ns);
  1488. hrtimer_start(&ktimer->timer, expire, HRTIMER_MODE_ABS_HARD);
  1489. } else
  1490. apic_timer_expired(apic, false);
  1491. local_irq_restore(flags);
  1492. }
  1493. static inline u64 tmict_to_ns(struct kvm_lapic *apic, u32 tmict)
  1494. {
  1495. return (u64)tmict * APIC_BUS_CYCLE_NS * (u64)apic->divide_count;
  1496. }
  1497. static void update_target_expiration(struct kvm_lapic *apic, uint32_t old_divisor)
  1498. {
  1499. ktime_t now, remaining;
  1500. u64 ns_remaining_old, ns_remaining_new;
  1501. apic->lapic_timer.period =
  1502. tmict_to_ns(apic, kvm_lapic_get_reg(apic, APIC_TMICT));
  1503. limit_periodic_timer_frequency(apic);
  1504. now = ktime_get();
  1505. remaining = ktime_sub(apic->lapic_timer.target_expiration, now);
  1506. if (ktime_to_ns(remaining) < 0)
  1507. remaining = 0;
  1508. ns_remaining_old = ktime_to_ns(remaining);
  1509. ns_remaining_new = mul_u64_u32_div(ns_remaining_old,
  1510. apic->divide_count, old_divisor);
  1511. apic->lapic_timer.tscdeadline +=
  1512. nsec_to_cycles(apic->vcpu, ns_remaining_new) -
  1513. nsec_to_cycles(apic->vcpu, ns_remaining_old);
  1514. apic->lapic_timer.target_expiration = ktime_add_ns(now, ns_remaining_new);
  1515. }
  1516. static bool set_target_expiration(struct kvm_lapic *apic, u32 count_reg)
  1517. {
  1518. ktime_t now;
  1519. u64 tscl = rdtsc();
  1520. s64 deadline;
  1521. now = ktime_get();
  1522. apic->lapic_timer.period =
  1523. tmict_to_ns(apic, kvm_lapic_get_reg(apic, APIC_TMICT));
  1524. if (!apic->lapic_timer.period) {
  1525. apic->lapic_timer.tscdeadline = 0;
  1526. return false;
  1527. }
  1528. limit_periodic_timer_frequency(apic);
  1529. deadline = apic->lapic_timer.period;
  1530. if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic)) {
  1531. if (unlikely(count_reg != APIC_TMICT)) {
  1532. deadline = tmict_to_ns(apic,
  1533. kvm_lapic_get_reg(apic, count_reg));
  1534. if (unlikely(deadline <= 0))
  1535. deadline = apic->lapic_timer.period;
  1536. else if (unlikely(deadline > apic->lapic_timer.period)) {
  1537. pr_info_ratelimited(
  1538. "kvm: vcpu %i: requested lapic timer restore with "
  1539. "starting count register %#x=%u (%lld ns) > initial count (%lld ns). "
  1540. "Using initial count to start timer.\n",
  1541. apic->vcpu->vcpu_id,
  1542. count_reg,
  1543. kvm_lapic_get_reg(apic, count_reg),
  1544. deadline, apic->lapic_timer.period);
  1545. kvm_lapic_set_reg(apic, count_reg, 0);
  1546. deadline = apic->lapic_timer.period;
  1547. }
  1548. }
  1549. }
  1550. apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) +
  1551. nsec_to_cycles(apic->vcpu, deadline);
  1552. apic->lapic_timer.target_expiration = ktime_add_ns(now, deadline);
  1553. return true;
  1554. }
  1555. static void advance_periodic_target_expiration(struct kvm_lapic *apic)
  1556. {
  1557. ktime_t now = ktime_get();
  1558. u64 tscl = rdtsc();
  1559. ktime_t delta;
  1560. /*
  1561. * Synchronize both deadlines to the same time source or
  1562. * differences in the periods (caused by differences in the
  1563. * underlying clocks or numerical approximation errors) will
  1564. * cause the two to drift apart over time as the errors
  1565. * accumulate.
  1566. */
  1567. apic->lapic_timer.target_expiration =
  1568. ktime_add_ns(apic->lapic_timer.target_expiration,
  1569. apic->lapic_timer.period);
  1570. delta = ktime_sub(apic->lapic_timer.target_expiration, now);
  1571. apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) +
  1572. nsec_to_cycles(apic->vcpu, delta);
  1573. }
  1574. static void start_sw_period(struct kvm_lapic *apic)
  1575. {
  1576. if (!apic->lapic_timer.period)
  1577. return;
  1578. if (ktime_after(ktime_get(),
  1579. apic->lapic_timer.target_expiration)) {
  1580. apic_timer_expired(apic, false);
  1581. if (apic_lvtt_oneshot(apic))
  1582. return;
  1583. advance_periodic_target_expiration(apic);
  1584. }
  1585. hrtimer_start(&apic->lapic_timer.timer,
  1586. apic->lapic_timer.target_expiration,
  1587. HRTIMER_MODE_ABS_HARD);
  1588. }
  1589. bool kvm_lapic_hv_timer_in_use(struct kvm_vcpu *vcpu)
  1590. {
  1591. if (!lapic_in_kernel(vcpu))
  1592. return false;
  1593. return vcpu->arch.apic->lapic_timer.hv_timer_in_use;
  1594. }
  1595. EXPORT_SYMBOL_GPL(kvm_lapic_hv_timer_in_use);
  1596. static void cancel_hv_timer(struct kvm_lapic *apic)
  1597. {
  1598. WARN_ON(preemptible());
  1599. WARN_ON(!apic->lapic_timer.hv_timer_in_use);
  1600. static_call(kvm_x86_cancel_hv_timer)(apic->vcpu);
  1601. apic->lapic_timer.hv_timer_in_use = false;
  1602. }
  1603. static bool start_hv_timer(struct kvm_lapic *apic)
  1604. {
  1605. struct kvm_timer *ktimer = &apic->lapic_timer;
  1606. struct kvm_vcpu *vcpu = apic->vcpu;
  1607. bool expired;
  1608. WARN_ON(preemptible());
  1609. if (!kvm_can_use_hv_timer(vcpu))
  1610. return false;
  1611. if (!ktimer->tscdeadline)
  1612. return false;
  1613. if (static_call(kvm_x86_set_hv_timer)(vcpu, ktimer->tscdeadline, &expired))
  1614. return false;
  1615. ktimer->hv_timer_in_use = true;
  1616. hrtimer_cancel(&ktimer->timer);
  1617. /*
  1618. * To simplify handling the periodic timer, leave the hv timer running
  1619. * even if the deadline timer has expired, i.e. rely on the resulting
  1620. * VM-Exit to recompute the periodic timer's target expiration.
  1621. */
  1622. if (!apic_lvtt_period(apic)) {
  1623. /*
  1624. * Cancel the hv timer if the sw timer fired while the hv timer
  1625. * was being programmed, or if the hv timer itself expired.
  1626. */
  1627. if (atomic_read(&ktimer->pending)) {
  1628. cancel_hv_timer(apic);
  1629. } else if (expired) {
  1630. apic_timer_expired(apic, false);
  1631. cancel_hv_timer(apic);
  1632. }
  1633. }
  1634. trace_kvm_hv_timer_state(vcpu->vcpu_id, ktimer->hv_timer_in_use);
  1635. return true;
  1636. }
  1637. static void start_sw_timer(struct kvm_lapic *apic)
  1638. {
  1639. struct kvm_timer *ktimer = &apic->lapic_timer;
  1640. WARN_ON(preemptible());
  1641. if (apic->lapic_timer.hv_timer_in_use)
  1642. cancel_hv_timer(apic);
  1643. if (!apic_lvtt_period(apic) && atomic_read(&ktimer->pending))
  1644. return;
  1645. if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic))
  1646. start_sw_period(apic);
  1647. else if (apic_lvtt_tscdeadline(apic))
  1648. start_sw_tscdeadline(apic);
  1649. trace_kvm_hv_timer_state(apic->vcpu->vcpu_id, false);
  1650. }
  1651. static void restart_apic_timer(struct kvm_lapic *apic)
  1652. {
  1653. preempt_disable();
  1654. if (!apic_lvtt_period(apic) && atomic_read(&apic->lapic_timer.pending))
  1655. goto out;
  1656. if (!start_hv_timer(apic))
  1657. start_sw_timer(apic);
  1658. out:
  1659. preempt_enable();
  1660. }
  1661. void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu)
  1662. {
  1663. struct kvm_lapic *apic = vcpu->arch.apic;
  1664. preempt_disable();
  1665. /* If the preempt notifier has already run, it also called apic_timer_expired */
  1666. if (!apic->lapic_timer.hv_timer_in_use)
  1667. goto out;
  1668. WARN_ON(kvm_vcpu_is_blocking(vcpu));
  1669. apic_timer_expired(apic, false);
  1670. cancel_hv_timer(apic);
  1671. if (apic_lvtt_period(apic) && apic->lapic_timer.period) {
  1672. advance_periodic_target_expiration(apic);
  1673. restart_apic_timer(apic);
  1674. }
  1675. out:
  1676. preempt_enable();
  1677. }
  1678. EXPORT_SYMBOL_GPL(kvm_lapic_expired_hv_timer);
  1679. void kvm_lapic_switch_to_hv_timer(struct kvm_vcpu *vcpu)
  1680. {
  1681. restart_apic_timer(vcpu->arch.apic);
  1682. }
  1683. void kvm_lapic_switch_to_sw_timer(struct kvm_vcpu *vcpu)
  1684. {
  1685. struct kvm_lapic *apic = vcpu->arch.apic;
  1686. preempt_disable();
  1687. /* Possibly the TSC deadline timer is not enabled yet */
  1688. if (apic->lapic_timer.hv_timer_in_use)
  1689. start_sw_timer(apic);
  1690. preempt_enable();
  1691. }
  1692. void kvm_lapic_restart_hv_timer(struct kvm_vcpu *vcpu)
  1693. {
  1694. struct kvm_lapic *apic = vcpu->arch.apic;
  1695. WARN_ON(!apic->lapic_timer.hv_timer_in_use);
  1696. restart_apic_timer(apic);
  1697. }
  1698. static void __start_apic_timer(struct kvm_lapic *apic, u32 count_reg)
  1699. {
  1700. atomic_set(&apic->lapic_timer.pending, 0);
  1701. if ((apic_lvtt_period(apic) || apic_lvtt_oneshot(apic))
  1702. && !set_target_expiration(apic, count_reg))
  1703. return;
  1704. restart_apic_timer(apic);
  1705. }
  1706. static void start_apic_timer(struct kvm_lapic *apic)
  1707. {
  1708. __start_apic_timer(apic, APIC_TMICT);
  1709. }
  1710. static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val)
  1711. {
  1712. bool lvt0_in_nmi_mode = apic_lvt_nmi_mode(lvt0_val);
  1713. if (apic->lvt0_in_nmi_mode != lvt0_in_nmi_mode) {
  1714. apic->lvt0_in_nmi_mode = lvt0_in_nmi_mode;
  1715. if (lvt0_in_nmi_mode) {
  1716. atomic_inc(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
  1717. } else
  1718. atomic_dec(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
  1719. }
  1720. }
  1721. static void kvm_lapic_xapic_id_updated(struct kvm_lapic *apic)
  1722. {
  1723. struct kvm *kvm = apic->vcpu->kvm;
  1724. if (!kvm_apic_hw_enabled(apic))
  1725. return;
  1726. if (KVM_BUG_ON(apic_x2apic_mode(apic), kvm))
  1727. return;
  1728. /*
  1729. * Deliberately truncate the vCPU ID when detecting a modified APIC ID
  1730. * to avoid false positives if the vCPU ID, i.e. x2APIC ID, is a 32-bit
  1731. * value.
  1732. */
  1733. if (kvm_xapic_id(apic) == (u8)apic->vcpu->vcpu_id)
  1734. return;
  1735. kvm_set_apicv_inhibit(apic->vcpu->kvm, APICV_INHIBIT_REASON_APIC_ID_MODIFIED);
  1736. }
  1737. static int get_lvt_index(u32 reg)
  1738. {
  1739. if (reg == APIC_LVTCMCI)
  1740. return LVT_CMCI;
  1741. if (reg < APIC_LVTT || reg > APIC_LVTERR)
  1742. return -1;
  1743. return array_index_nospec(
  1744. (reg - APIC_LVTT) >> 4, KVM_APIC_MAX_NR_LVT_ENTRIES);
  1745. }
  1746. static int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
  1747. {
  1748. int ret = 0;
  1749. trace_kvm_apic_write(reg, val);
  1750. switch (reg) {
  1751. case APIC_ID: /* Local APIC ID */
  1752. if (!apic_x2apic_mode(apic)) {
  1753. kvm_apic_set_xapic_id(apic, val >> 24);
  1754. kvm_lapic_xapic_id_updated(apic);
  1755. } else {
  1756. ret = 1;
  1757. }
  1758. break;
  1759. case APIC_TASKPRI:
  1760. report_tpr_access(apic, true);
  1761. apic_set_tpr(apic, val & 0xff);
  1762. break;
  1763. case APIC_EOI:
  1764. apic_set_eoi(apic);
  1765. break;
  1766. case APIC_LDR:
  1767. if (!apic_x2apic_mode(apic))
  1768. kvm_apic_set_ldr(apic, val & APIC_LDR_MASK);
  1769. else
  1770. ret = 1;
  1771. break;
  1772. case APIC_DFR:
  1773. if (!apic_x2apic_mode(apic))
  1774. kvm_apic_set_dfr(apic, val | 0x0FFFFFFF);
  1775. else
  1776. ret = 1;
  1777. break;
  1778. case APIC_SPIV: {
  1779. u32 mask = 0x3ff;
  1780. if (kvm_lapic_get_reg(apic, APIC_LVR) & APIC_LVR_DIRECTED_EOI)
  1781. mask |= APIC_SPIV_DIRECTED_EOI;
  1782. apic_set_spiv(apic, val & mask);
  1783. if (!(val & APIC_SPIV_APIC_ENABLED)) {
  1784. int i;
  1785. for (i = 0; i < apic->nr_lvt_entries; i++) {
  1786. kvm_lapic_set_reg(apic, APIC_LVTx(i),
  1787. kvm_lapic_get_reg(apic, APIC_LVTx(i)) | APIC_LVT_MASKED);
  1788. }
  1789. apic_update_lvtt(apic);
  1790. atomic_set(&apic->lapic_timer.pending, 0);
  1791. }
  1792. break;
  1793. }
  1794. case APIC_ICR:
  1795. WARN_ON_ONCE(apic_x2apic_mode(apic));
  1796. /* No delay here, so we always clear the pending bit */
  1797. val &= ~APIC_ICR_BUSY;
  1798. kvm_apic_send_ipi(apic, val, kvm_lapic_get_reg(apic, APIC_ICR2));
  1799. kvm_lapic_set_reg(apic, APIC_ICR, val);
  1800. break;
  1801. case APIC_ICR2:
  1802. if (apic_x2apic_mode(apic))
  1803. ret = 1;
  1804. else
  1805. kvm_lapic_set_reg(apic, APIC_ICR2, val & 0xff000000);
  1806. break;
  1807. case APIC_LVT0:
  1808. apic_manage_nmi_watchdog(apic, val);
  1809. fallthrough;
  1810. case APIC_LVTTHMR:
  1811. case APIC_LVTPC:
  1812. case APIC_LVT1:
  1813. case APIC_LVTERR:
  1814. case APIC_LVTCMCI: {
  1815. u32 index = get_lvt_index(reg);
  1816. if (!kvm_lapic_lvt_supported(apic, index)) {
  1817. ret = 1;
  1818. break;
  1819. }
  1820. if (!kvm_apic_sw_enabled(apic))
  1821. val |= APIC_LVT_MASKED;
  1822. val &= apic_lvt_mask[index];
  1823. kvm_lapic_set_reg(apic, reg, val);
  1824. break;
  1825. }
  1826. case APIC_LVTT:
  1827. if (!kvm_apic_sw_enabled(apic))
  1828. val |= APIC_LVT_MASKED;
  1829. val &= (apic_lvt_mask[0] | apic->lapic_timer.timer_mode_mask);
  1830. kvm_lapic_set_reg(apic, APIC_LVTT, val);
  1831. apic_update_lvtt(apic);
  1832. break;
  1833. case APIC_TMICT:
  1834. if (apic_lvtt_tscdeadline(apic))
  1835. break;
  1836. cancel_apic_timer(apic);
  1837. kvm_lapic_set_reg(apic, APIC_TMICT, val);
  1838. start_apic_timer(apic);
  1839. break;
  1840. case APIC_TDCR: {
  1841. uint32_t old_divisor = apic->divide_count;
  1842. kvm_lapic_set_reg(apic, APIC_TDCR, val & 0xb);
  1843. update_divide_count(apic);
  1844. if (apic->divide_count != old_divisor &&
  1845. apic->lapic_timer.period) {
  1846. hrtimer_cancel(&apic->lapic_timer.timer);
  1847. update_target_expiration(apic, old_divisor);
  1848. restart_apic_timer(apic);
  1849. }
  1850. break;
  1851. }
  1852. case APIC_ESR:
  1853. if (apic_x2apic_mode(apic) && val != 0)
  1854. ret = 1;
  1855. break;
  1856. case APIC_SELF_IPI:
  1857. /*
  1858. * Self-IPI exists only when x2APIC is enabled. Bits 7:0 hold
  1859. * the vector, everything else is reserved.
  1860. */
  1861. if (!apic_x2apic_mode(apic) || (val & ~APIC_VECTOR_MASK))
  1862. ret = 1;
  1863. else
  1864. kvm_apic_send_ipi(apic, APIC_DEST_SELF | val, 0);
  1865. break;
  1866. default:
  1867. ret = 1;
  1868. break;
  1869. }
  1870. /*
  1871. * Recalculate APIC maps if necessary, e.g. if the software enable bit
  1872. * was toggled, the APIC ID changed, etc... The maps are marked dirty
  1873. * on relevant changes, i.e. this is a nop for most writes.
  1874. */
  1875. kvm_recalculate_apic_map(apic->vcpu->kvm);
  1876. return ret;
  1877. }
  1878. static int apic_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
  1879. gpa_t address, int len, const void *data)
  1880. {
  1881. struct kvm_lapic *apic = to_lapic(this);
  1882. unsigned int offset = address - apic->base_address;
  1883. u32 val;
  1884. if (!apic_mmio_in_range(apic, address))
  1885. return -EOPNOTSUPP;
  1886. if (!kvm_apic_hw_enabled(apic) || apic_x2apic_mode(apic)) {
  1887. if (!kvm_check_has_quirk(vcpu->kvm,
  1888. KVM_X86_QUIRK_LAPIC_MMIO_HOLE))
  1889. return -EOPNOTSUPP;
  1890. return 0;
  1891. }
  1892. /*
  1893. * APIC register must be aligned on 128-bits boundary.
  1894. * 32/64/128 bits registers must be accessed thru 32 bits.
  1895. * Refer SDM 8.4.1
  1896. */
  1897. if (len != 4 || (offset & 0xf))
  1898. return 0;
  1899. val = *(u32*)data;
  1900. kvm_lapic_reg_write(apic, offset & 0xff0, val);
  1901. return 0;
  1902. }
  1903. void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu)
  1904. {
  1905. kvm_lapic_reg_write(vcpu->arch.apic, APIC_EOI, 0);
  1906. }
  1907. EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi);
  1908. /* emulate APIC access in a trap manner */
  1909. void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset)
  1910. {
  1911. struct kvm_lapic *apic = vcpu->arch.apic;
  1912. /*
  1913. * ICR is a single 64-bit register when x2APIC is enabled, all others
  1914. * registers hold 32-bit values. For legacy xAPIC, ICR writes need to
  1915. * go down the common path to get the upper half from ICR2.
  1916. *
  1917. * Note, using the write helpers may incur an unnecessary write to the
  1918. * virtual APIC state, but KVM needs to conditionally modify the value
  1919. * in certain cases, e.g. to clear the ICR busy bit. The cost of extra
  1920. * conditional branches is likely a wash relative to the cost of the
  1921. * maybe-unecessary write, and both are in the noise anyways.
  1922. */
  1923. if (apic_x2apic_mode(apic) && offset == APIC_ICR)
  1924. kvm_x2apic_icr_write(apic, kvm_lapic_get_reg64(apic, APIC_ICR));
  1925. else
  1926. kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset));
  1927. }
  1928. EXPORT_SYMBOL_GPL(kvm_apic_write_nodecode);
  1929. void kvm_free_lapic(struct kvm_vcpu *vcpu)
  1930. {
  1931. struct kvm_lapic *apic = vcpu->arch.apic;
  1932. if (!vcpu->arch.apic)
  1933. return;
  1934. hrtimer_cancel(&apic->lapic_timer.timer);
  1935. if (!(vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE))
  1936. static_branch_slow_dec_deferred(&apic_hw_disabled);
  1937. if (!apic->sw_enabled)
  1938. static_branch_slow_dec_deferred(&apic_sw_disabled);
  1939. if (apic->regs)
  1940. free_page((unsigned long)apic->regs);
  1941. kfree(apic);
  1942. }
  1943. /*
  1944. *----------------------------------------------------------------------
  1945. * LAPIC interface
  1946. *----------------------------------------------------------------------
  1947. */
  1948. u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu)
  1949. {
  1950. struct kvm_lapic *apic = vcpu->arch.apic;
  1951. if (!kvm_apic_present(vcpu) || !apic_lvtt_tscdeadline(apic))
  1952. return 0;
  1953. return apic->lapic_timer.tscdeadline;
  1954. }
  1955. void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data)
  1956. {
  1957. struct kvm_lapic *apic = vcpu->arch.apic;
  1958. if (!kvm_apic_present(vcpu) || !apic_lvtt_tscdeadline(apic))
  1959. return;
  1960. hrtimer_cancel(&apic->lapic_timer.timer);
  1961. apic->lapic_timer.tscdeadline = data;
  1962. start_apic_timer(apic);
  1963. }
  1964. void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
  1965. {
  1966. apic_set_tpr(vcpu->arch.apic, (cr8 & 0x0f) << 4);
  1967. }
  1968. u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
  1969. {
  1970. u64 tpr;
  1971. tpr = (u64) kvm_lapic_get_reg(vcpu->arch.apic, APIC_TASKPRI);
  1972. return (tpr & 0xf0) >> 4;
  1973. }
  1974. void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
  1975. {
  1976. u64 old_value = vcpu->arch.apic_base;
  1977. struct kvm_lapic *apic = vcpu->arch.apic;
  1978. vcpu->arch.apic_base = value;
  1979. if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE)
  1980. kvm_update_cpuid_runtime(vcpu);
  1981. if (!apic)
  1982. return;
  1983. /* update jump label if enable bit changes */
  1984. if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE) {
  1985. if (value & MSR_IA32_APICBASE_ENABLE) {
  1986. kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
  1987. static_branch_slow_dec_deferred(&apic_hw_disabled);
  1988. /* Check if there are APF page ready requests pending */
  1989. kvm_make_request(KVM_REQ_APF_READY, vcpu);
  1990. } else {
  1991. static_branch_inc(&apic_hw_disabled.key);
  1992. atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
  1993. }
  1994. }
  1995. if (((old_value ^ value) & X2APIC_ENABLE) && (value & X2APIC_ENABLE))
  1996. kvm_apic_set_x2apic_id(apic, vcpu->vcpu_id);
  1997. if ((old_value ^ value) & (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE)) {
  1998. kvm_vcpu_update_apicv(vcpu);
  1999. static_call_cond(kvm_x86_set_virtual_apic_mode)(vcpu);
  2000. }
  2001. apic->base_address = apic->vcpu->arch.apic_base &
  2002. MSR_IA32_APICBASE_BASE;
  2003. if ((value & MSR_IA32_APICBASE_ENABLE) &&
  2004. apic->base_address != APIC_DEFAULT_PHYS_BASE) {
  2005. kvm_set_apicv_inhibit(apic->vcpu->kvm,
  2006. APICV_INHIBIT_REASON_APIC_BASE_MODIFIED);
  2007. }
  2008. }
  2009. void kvm_apic_update_apicv(struct kvm_vcpu *vcpu)
  2010. {
  2011. struct kvm_lapic *apic = vcpu->arch.apic;
  2012. if (apic->apicv_active) {
  2013. /* irr_pending is always true when apicv is activated. */
  2014. apic->irr_pending = true;
  2015. apic->isr_count = 1;
  2016. } else {
  2017. /*
  2018. * Don't clear irr_pending, searching the IRR can race with
  2019. * updates from the CPU as APICv is still active from hardware's
  2020. * perspective. The flag will be cleared as appropriate when
  2021. * KVM injects the interrupt.
  2022. */
  2023. apic->isr_count = count_vectors(apic->regs + APIC_ISR);
  2024. }
  2025. apic->highest_isr_cache = -1;
  2026. }
  2027. EXPORT_SYMBOL_GPL(kvm_apic_update_apicv);
  2028. void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event)
  2029. {
  2030. struct kvm_lapic *apic = vcpu->arch.apic;
  2031. u64 msr_val;
  2032. int i;
  2033. static_call_cond(kvm_x86_apicv_pre_state_restore)(vcpu);
  2034. if (!init_event) {
  2035. msr_val = APIC_DEFAULT_PHYS_BASE | MSR_IA32_APICBASE_ENABLE;
  2036. if (kvm_vcpu_is_reset_bsp(vcpu))
  2037. msr_val |= MSR_IA32_APICBASE_BSP;
  2038. kvm_lapic_set_base(vcpu, msr_val);
  2039. }
  2040. if (!apic)
  2041. return;
  2042. /* Stop the timer in case it's a reset to an active apic */
  2043. hrtimer_cancel(&apic->lapic_timer.timer);
  2044. /* The xAPIC ID is set at RESET even if the APIC was already enabled. */
  2045. if (!init_event)
  2046. kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
  2047. kvm_apic_set_version(apic->vcpu);
  2048. for (i = 0; i < apic->nr_lvt_entries; i++)
  2049. kvm_lapic_set_reg(apic, APIC_LVTx(i), APIC_LVT_MASKED);
  2050. apic_update_lvtt(apic);
  2051. if (kvm_vcpu_is_reset_bsp(vcpu) &&
  2052. kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_LINT0_REENABLED))
  2053. kvm_lapic_set_reg(apic, APIC_LVT0,
  2054. SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
  2055. apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
  2056. kvm_apic_set_dfr(apic, 0xffffffffU);
  2057. apic_set_spiv(apic, 0xff);
  2058. kvm_lapic_set_reg(apic, APIC_TASKPRI, 0);
  2059. if (!apic_x2apic_mode(apic))
  2060. kvm_apic_set_ldr(apic, 0);
  2061. kvm_lapic_set_reg(apic, APIC_ESR, 0);
  2062. if (!apic_x2apic_mode(apic)) {
  2063. kvm_lapic_set_reg(apic, APIC_ICR, 0);
  2064. kvm_lapic_set_reg(apic, APIC_ICR2, 0);
  2065. } else {
  2066. kvm_lapic_set_reg64(apic, APIC_ICR, 0);
  2067. }
  2068. kvm_lapic_set_reg(apic, APIC_TDCR, 0);
  2069. kvm_lapic_set_reg(apic, APIC_TMICT, 0);
  2070. for (i = 0; i < 8; i++) {
  2071. kvm_lapic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
  2072. kvm_lapic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
  2073. kvm_lapic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
  2074. }
  2075. kvm_apic_update_apicv(vcpu);
  2076. update_divide_count(apic);
  2077. atomic_set(&apic->lapic_timer.pending, 0);
  2078. vcpu->arch.pv_eoi.msr_val = 0;
  2079. apic_update_ppr(apic);
  2080. if (apic->apicv_active) {
  2081. static_call_cond(kvm_x86_apicv_post_state_restore)(vcpu);
  2082. static_call_cond(kvm_x86_hwapic_irr_update)(vcpu, -1);
  2083. static_call_cond(kvm_x86_hwapic_isr_update)(-1);
  2084. }
  2085. vcpu->arch.apic_arb_prio = 0;
  2086. vcpu->arch.apic_attention = 0;
  2087. kvm_recalculate_apic_map(vcpu->kvm);
  2088. }
  2089. /*
  2090. *----------------------------------------------------------------------
  2091. * timer interface
  2092. *----------------------------------------------------------------------
  2093. */
  2094. static bool lapic_is_periodic(struct kvm_lapic *apic)
  2095. {
  2096. return apic_lvtt_period(apic);
  2097. }
  2098. int apic_has_pending_timer(struct kvm_vcpu *vcpu)
  2099. {
  2100. struct kvm_lapic *apic = vcpu->arch.apic;
  2101. if (apic_enabled(apic) && apic_lvt_enabled(apic, APIC_LVTT))
  2102. return atomic_read(&apic->lapic_timer.pending);
  2103. return 0;
  2104. }
  2105. int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type)
  2106. {
  2107. u32 reg = kvm_lapic_get_reg(apic, lvt_type);
  2108. int vector, mode, trig_mode;
  2109. int r;
  2110. if (kvm_apic_hw_enabled(apic) && !(reg & APIC_LVT_MASKED)) {
  2111. vector = reg & APIC_VECTOR_MASK;
  2112. mode = reg & APIC_MODE_MASK;
  2113. trig_mode = reg & APIC_LVT_LEVEL_TRIGGER;
  2114. r = __apic_accept_irq(apic, mode, vector, 1, trig_mode, NULL);
  2115. if (r && lvt_type == APIC_LVTPC)
  2116. kvm_lapic_set_reg(apic, APIC_LVTPC, reg | APIC_LVT_MASKED);
  2117. return r;
  2118. }
  2119. return 0;
  2120. }
  2121. void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu)
  2122. {
  2123. struct kvm_lapic *apic = vcpu->arch.apic;
  2124. if (apic)
  2125. kvm_apic_local_deliver(apic, APIC_LVT0);
  2126. }
  2127. static const struct kvm_io_device_ops apic_mmio_ops = {
  2128. .read = apic_mmio_read,
  2129. .write = apic_mmio_write,
  2130. };
  2131. static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)
  2132. {
  2133. struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
  2134. struct kvm_lapic *apic = container_of(ktimer, struct kvm_lapic, lapic_timer);
  2135. apic_timer_expired(apic, true);
  2136. if (lapic_is_periodic(apic)) {
  2137. advance_periodic_target_expiration(apic);
  2138. hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
  2139. return HRTIMER_RESTART;
  2140. } else
  2141. return HRTIMER_NORESTART;
  2142. }
  2143. int kvm_create_lapic(struct kvm_vcpu *vcpu, int timer_advance_ns)
  2144. {
  2145. struct kvm_lapic *apic;
  2146. ASSERT(vcpu != NULL);
  2147. apic = kzalloc(sizeof(*apic), GFP_KERNEL_ACCOUNT);
  2148. if (!apic)
  2149. goto nomem;
  2150. vcpu->arch.apic = apic;
  2151. apic->regs = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
  2152. if (!apic->regs) {
  2153. printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
  2154. vcpu->vcpu_id);
  2155. goto nomem_free_apic;
  2156. }
  2157. apic->vcpu = vcpu;
  2158. apic->nr_lvt_entries = kvm_apic_calc_nr_lvt_entries(vcpu);
  2159. hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
  2160. HRTIMER_MODE_ABS_HARD);
  2161. apic->lapic_timer.timer.function = apic_timer_fn;
  2162. if (timer_advance_ns == -1) {
  2163. apic->lapic_timer.timer_advance_ns = LAPIC_TIMER_ADVANCE_NS_INIT;
  2164. lapic_timer_advance_dynamic = true;
  2165. } else {
  2166. apic->lapic_timer.timer_advance_ns = timer_advance_ns;
  2167. lapic_timer_advance_dynamic = false;
  2168. }
  2169. /*
  2170. * Stuff the APIC ENABLE bit in lieu of temporarily incrementing
  2171. * apic_hw_disabled; the full RESET value is set by kvm_lapic_reset().
  2172. */
  2173. vcpu->arch.apic_base = MSR_IA32_APICBASE_ENABLE;
  2174. static_branch_inc(&apic_sw_disabled.key); /* sw disabled at reset */
  2175. kvm_iodevice_init(&apic->dev, &apic_mmio_ops);
  2176. return 0;
  2177. nomem_free_apic:
  2178. kfree(apic);
  2179. vcpu->arch.apic = NULL;
  2180. nomem:
  2181. return -ENOMEM;
  2182. }
  2183. int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
  2184. {
  2185. struct kvm_lapic *apic = vcpu->arch.apic;
  2186. u32 ppr;
  2187. if (!kvm_apic_present(vcpu))
  2188. return -1;
  2189. __apic_update_ppr(apic, &ppr);
  2190. return apic_has_interrupt_for_ppr(apic, ppr);
  2191. }
  2192. EXPORT_SYMBOL_GPL(kvm_apic_has_interrupt);
  2193. int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
  2194. {
  2195. u32 lvt0 = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LVT0);
  2196. if (!kvm_apic_hw_enabled(vcpu->arch.apic))
  2197. return 1;
  2198. if ((lvt0 & APIC_LVT_MASKED) == 0 &&
  2199. GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
  2200. return 1;
  2201. return 0;
  2202. }
  2203. void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
  2204. {
  2205. struct kvm_lapic *apic = vcpu->arch.apic;
  2206. if (atomic_read(&apic->lapic_timer.pending) > 0) {
  2207. kvm_apic_inject_pending_timer_irqs(apic);
  2208. atomic_set(&apic->lapic_timer.pending, 0);
  2209. }
  2210. }
  2211. int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
  2212. {
  2213. int vector = kvm_apic_has_interrupt(vcpu);
  2214. struct kvm_lapic *apic = vcpu->arch.apic;
  2215. u32 ppr;
  2216. if (vector == -1)
  2217. return -1;
  2218. /*
  2219. * We get here even with APIC virtualization enabled, if doing
  2220. * nested virtualization and L1 runs with the "acknowledge interrupt
  2221. * on exit" mode. Then we cannot inject the interrupt via RVI,
  2222. * because the process would deliver it through the IDT.
  2223. */
  2224. apic_clear_irr(vector, apic);
  2225. if (to_hv_vcpu(vcpu) && test_bit(vector, to_hv_synic(vcpu)->auto_eoi_bitmap)) {
  2226. /*
  2227. * For auto-EOI interrupts, there might be another pending
  2228. * interrupt above PPR, so check whether to raise another
  2229. * KVM_REQ_EVENT.
  2230. */
  2231. apic_update_ppr(apic);
  2232. } else {
  2233. /*
  2234. * For normal interrupts, PPR has been raised and there cannot
  2235. * be a higher-priority pending interrupt---except if there was
  2236. * a concurrent interrupt injection, but that would have
  2237. * triggered KVM_REQ_EVENT already.
  2238. */
  2239. apic_set_isr(vector, apic);
  2240. __apic_update_ppr(apic, &ppr);
  2241. }
  2242. return vector;
  2243. }
  2244. static int kvm_apic_state_fixup(struct kvm_vcpu *vcpu,
  2245. struct kvm_lapic_state *s, bool set)
  2246. {
  2247. if (apic_x2apic_mode(vcpu->arch.apic)) {
  2248. u32 *id = (u32 *)(s->regs + APIC_ID);
  2249. u32 *ldr = (u32 *)(s->regs + APIC_LDR);
  2250. u64 icr;
  2251. if (vcpu->kvm->arch.x2apic_format) {
  2252. if (*id != vcpu->vcpu_id)
  2253. return -EINVAL;
  2254. } else {
  2255. if (set)
  2256. *id >>= 24;
  2257. else
  2258. *id <<= 24;
  2259. }
  2260. /*
  2261. * In x2APIC mode, the LDR is fixed and based on the id. And
  2262. * ICR is internally a single 64-bit register, but needs to be
  2263. * split to ICR+ICR2 in userspace for backwards compatibility.
  2264. */
  2265. if (set) {
  2266. *ldr = kvm_apic_calc_x2apic_ldr(*id);
  2267. icr = __kvm_lapic_get_reg(s->regs, APIC_ICR) |
  2268. (u64)__kvm_lapic_get_reg(s->regs, APIC_ICR2) << 32;
  2269. __kvm_lapic_set_reg64(s->regs, APIC_ICR, icr);
  2270. } else {
  2271. icr = __kvm_lapic_get_reg64(s->regs, APIC_ICR);
  2272. __kvm_lapic_set_reg(s->regs, APIC_ICR2, icr >> 32);
  2273. }
  2274. }
  2275. return 0;
  2276. }
  2277. int kvm_apic_get_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
  2278. {
  2279. memcpy(s->regs, vcpu->arch.apic->regs, sizeof(*s));
  2280. /*
  2281. * Get calculated timer current count for remaining timer period (if
  2282. * any) and store it in the returned register set.
  2283. */
  2284. __kvm_lapic_set_reg(s->regs, APIC_TMCCT,
  2285. __apic_read(vcpu->arch.apic, APIC_TMCCT));
  2286. return kvm_apic_state_fixup(vcpu, s, false);
  2287. }
  2288. int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
  2289. {
  2290. struct kvm_lapic *apic = vcpu->arch.apic;
  2291. int r;
  2292. static_call_cond(kvm_x86_apicv_pre_state_restore)(vcpu);
  2293. kvm_lapic_set_base(vcpu, vcpu->arch.apic_base);
  2294. /* set SPIV separately to get count of SW disabled APICs right */
  2295. apic_set_spiv(apic, *((u32 *)(s->regs + APIC_SPIV)));
  2296. r = kvm_apic_state_fixup(vcpu, s, true);
  2297. if (r) {
  2298. kvm_recalculate_apic_map(vcpu->kvm);
  2299. return r;
  2300. }
  2301. memcpy(vcpu->arch.apic->regs, s->regs, sizeof(*s));
  2302. if (!apic_x2apic_mode(apic))
  2303. kvm_lapic_xapic_id_updated(apic);
  2304. atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
  2305. kvm_recalculate_apic_map(vcpu->kvm);
  2306. kvm_apic_set_version(vcpu);
  2307. apic_update_ppr(apic);
  2308. cancel_apic_timer(apic);
  2309. apic->lapic_timer.expired_tscdeadline = 0;
  2310. apic_update_lvtt(apic);
  2311. apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
  2312. update_divide_count(apic);
  2313. __start_apic_timer(apic, APIC_TMCCT);
  2314. kvm_lapic_set_reg(apic, APIC_TMCCT, 0);
  2315. kvm_apic_update_apicv(vcpu);
  2316. if (apic->apicv_active) {
  2317. static_call_cond(kvm_x86_apicv_post_state_restore)(vcpu);
  2318. static_call_cond(kvm_x86_hwapic_irr_update)(vcpu, apic_find_highest_irr(apic));
  2319. static_call_cond(kvm_x86_hwapic_isr_update)(apic_find_highest_isr(apic));
  2320. }
  2321. kvm_make_request(KVM_REQ_EVENT, vcpu);
  2322. if (ioapic_in_kernel(vcpu->kvm))
  2323. kvm_rtc_eoi_tracking_restore_one(vcpu);
  2324. vcpu->arch.apic_arb_prio = 0;
  2325. return 0;
  2326. }
  2327. void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
  2328. {
  2329. struct hrtimer *timer;
  2330. if (!lapic_in_kernel(vcpu) ||
  2331. kvm_can_post_timer_interrupt(vcpu))
  2332. return;
  2333. timer = &vcpu->arch.apic->lapic_timer.timer;
  2334. if (hrtimer_cancel(timer))
  2335. hrtimer_start_expires(timer, HRTIMER_MODE_ABS_HARD);
  2336. }
  2337. /*
  2338. * apic_sync_pv_eoi_from_guest - called on vmexit or cancel interrupt
  2339. *
  2340. * Detect whether guest triggered PV EOI since the
  2341. * last entry. If yes, set EOI on guests's behalf.
  2342. * Clear PV EOI in guest memory in any case.
  2343. */
  2344. static void apic_sync_pv_eoi_from_guest(struct kvm_vcpu *vcpu,
  2345. struct kvm_lapic *apic)
  2346. {
  2347. int vector;
  2348. /*
  2349. * PV EOI state is derived from KVM_APIC_PV_EOI_PENDING in host
  2350. * and KVM_PV_EOI_ENABLED in guest memory as follows:
  2351. *
  2352. * KVM_APIC_PV_EOI_PENDING is unset:
  2353. * -> host disabled PV EOI.
  2354. * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is set:
  2355. * -> host enabled PV EOI, guest did not execute EOI yet.
  2356. * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is unset:
  2357. * -> host enabled PV EOI, guest executed EOI.
  2358. */
  2359. BUG_ON(!pv_eoi_enabled(vcpu));
  2360. if (pv_eoi_test_and_clr_pending(vcpu))
  2361. return;
  2362. vector = apic_set_eoi(apic);
  2363. trace_kvm_pv_eoi(apic, vector);
  2364. }
  2365. void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu)
  2366. {
  2367. u32 data;
  2368. if (test_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention))
  2369. apic_sync_pv_eoi_from_guest(vcpu, vcpu->arch.apic);
  2370. if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
  2371. return;
  2372. if (kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
  2373. sizeof(u32)))
  2374. return;
  2375. apic_set_tpr(vcpu->arch.apic, data & 0xff);
  2376. }
  2377. /*
  2378. * apic_sync_pv_eoi_to_guest - called before vmentry
  2379. *
  2380. * Detect whether it's safe to enable PV EOI and
  2381. * if yes do so.
  2382. */
  2383. static void apic_sync_pv_eoi_to_guest(struct kvm_vcpu *vcpu,
  2384. struct kvm_lapic *apic)
  2385. {
  2386. if (!pv_eoi_enabled(vcpu) ||
  2387. /* IRR set or many bits in ISR: could be nested. */
  2388. apic->irr_pending ||
  2389. /* Cache not set: could be safe but we don't bother. */
  2390. apic->highest_isr_cache == -1 ||
  2391. /* Need EOI to update ioapic. */
  2392. kvm_ioapic_handles_vector(apic, apic->highest_isr_cache)) {
  2393. /*
  2394. * PV EOI was disabled by apic_sync_pv_eoi_from_guest
  2395. * so we need not do anything here.
  2396. */
  2397. return;
  2398. }
  2399. pv_eoi_set_pending(apic->vcpu);
  2400. }
  2401. void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu)
  2402. {
  2403. u32 data, tpr;
  2404. int max_irr, max_isr;
  2405. struct kvm_lapic *apic = vcpu->arch.apic;
  2406. apic_sync_pv_eoi_to_guest(vcpu, apic);
  2407. if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
  2408. return;
  2409. tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI) & 0xff;
  2410. max_irr = apic_find_highest_irr(apic);
  2411. if (max_irr < 0)
  2412. max_irr = 0;
  2413. max_isr = apic_find_highest_isr(apic);
  2414. if (max_isr < 0)
  2415. max_isr = 0;
  2416. data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24);
  2417. kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
  2418. sizeof(u32));
  2419. }
  2420. int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr)
  2421. {
  2422. if (vapic_addr) {
  2423. if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
  2424. &vcpu->arch.apic->vapic_cache,
  2425. vapic_addr, sizeof(u32)))
  2426. return -EINVAL;
  2427. __set_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
  2428. } else {
  2429. __clear_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
  2430. }
  2431. vcpu->arch.apic->vapic_addr = vapic_addr;
  2432. return 0;
  2433. }
  2434. int kvm_x2apic_icr_write(struct kvm_lapic *apic, u64 data)
  2435. {
  2436. data &= ~APIC_ICR_BUSY;
  2437. kvm_apic_send_ipi(apic, (u32)data, (u32)(data >> 32));
  2438. kvm_lapic_set_reg64(apic, APIC_ICR, data);
  2439. trace_kvm_apic_write(APIC_ICR, data);
  2440. return 0;
  2441. }
  2442. static int kvm_lapic_msr_read(struct kvm_lapic *apic, u32 reg, u64 *data)
  2443. {
  2444. u32 low;
  2445. if (reg == APIC_ICR) {
  2446. *data = kvm_lapic_get_reg64(apic, APIC_ICR);
  2447. return 0;
  2448. }
  2449. if (kvm_lapic_reg_read(apic, reg, 4, &low))
  2450. return 1;
  2451. *data = low;
  2452. return 0;
  2453. }
  2454. static int kvm_lapic_msr_write(struct kvm_lapic *apic, u32 reg, u64 data)
  2455. {
  2456. /*
  2457. * ICR is a 64-bit register in x2APIC mode (and Hyper-V PV vAPIC) and
  2458. * can be written as such, all other registers remain accessible only
  2459. * through 32-bit reads/writes.
  2460. */
  2461. if (reg == APIC_ICR)
  2462. return kvm_x2apic_icr_write(apic, data);
  2463. /* Bits 63:32 are reserved in all other registers. */
  2464. if (data >> 32)
  2465. return 1;
  2466. return kvm_lapic_reg_write(apic, reg, (u32)data);
  2467. }
  2468. int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  2469. {
  2470. struct kvm_lapic *apic = vcpu->arch.apic;
  2471. u32 reg = (msr - APIC_BASE_MSR) << 4;
  2472. if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
  2473. return 1;
  2474. return kvm_lapic_msr_write(apic, reg, data);
  2475. }
  2476. int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
  2477. {
  2478. struct kvm_lapic *apic = vcpu->arch.apic;
  2479. u32 reg = (msr - APIC_BASE_MSR) << 4;
  2480. if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
  2481. return 1;
  2482. if (reg == APIC_DFR)
  2483. return 1;
  2484. return kvm_lapic_msr_read(apic, reg, data);
  2485. }
  2486. int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 reg, u64 data)
  2487. {
  2488. if (!lapic_in_kernel(vcpu))
  2489. return 1;
  2490. return kvm_lapic_msr_write(vcpu->arch.apic, reg, data);
  2491. }
  2492. int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 reg, u64 *data)
  2493. {
  2494. if (!lapic_in_kernel(vcpu))
  2495. return 1;
  2496. return kvm_lapic_msr_read(vcpu->arch.apic, reg, data);
  2497. }
  2498. int kvm_lapic_set_pv_eoi(struct kvm_vcpu *vcpu, u64 data, unsigned long len)
  2499. {
  2500. u64 addr = data & ~KVM_MSR_ENABLED;
  2501. struct gfn_to_hva_cache *ghc = &vcpu->arch.pv_eoi.data;
  2502. unsigned long new_len;
  2503. int ret;
  2504. if (!IS_ALIGNED(addr, 4))
  2505. return 1;
  2506. if (data & KVM_MSR_ENABLED) {
  2507. if (addr == ghc->gpa && len <= ghc->len)
  2508. new_len = ghc->len;
  2509. else
  2510. new_len = len;
  2511. ret = kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, addr, new_len);
  2512. if (ret)
  2513. return ret;
  2514. }
  2515. vcpu->arch.pv_eoi.msr_val = data;
  2516. return 0;
  2517. }
  2518. int kvm_apic_accept_events(struct kvm_vcpu *vcpu)
  2519. {
  2520. struct kvm_lapic *apic = vcpu->arch.apic;
  2521. u8 sipi_vector;
  2522. int r;
  2523. if (!kvm_apic_has_pending_init_or_sipi(vcpu))
  2524. return 0;
  2525. if (is_guest_mode(vcpu)) {
  2526. r = kvm_check_nested_events(vcpu);
  2527. if (r < 0)
  2528. return r == -EBUSY ? 0 : r;
  2529. /*
  2530. * Continue processing INIT/SIPI even if a nested VM-Exit
  2531. * occurred, e.g. pending SIPIs should be dropped if INIT+SIPI
  2532. * are blocked as a result of transitioning to VMX root mode.
  2533. */
  2534. }
  2535. /*
  2536. * INITs are blocked while CPU is in specific states (SMM, VMX root
  2537. * mode, SVM with GIF=0), while SIPIs are dropped if the CPU isn't in
  2538. * wait-for-SIPI (WFS).
  2539. */
  2540. if (!kvm_apic_init_sipi_allowed(vcpu)) {
  2541. WARN_ON_ONCE(vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED);
  2542. clear_bit(KVM_APIC_SIPI, &apic->pending_events);
  2543. return 0;
  2544. }
  2545. if (test_and_clear_bit(KVM_APIC_INIT, &apic->pending_events)) {
  2546. kvm_vcpu_reset(vcpu, true);
  2547. if (kvm_vcpu_is_bsp(apic->vcpu))
  2548. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  2549. else
  2550. vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
  2551. }
  2552. if (test_and_clear_bit(KVM_APIC_SIPI, &apic->pending_events)) {
  2553. if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
  2554. /* evaluate pending_events before reading the vector */
  2555. smp_rmb();
  2556. sipi_vector = apic->sipi_vector;
  2557. static_call(kvm_x86_vcpu_deliver_sipi_vector)(vcpu, sipi_vector);
  2558. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  2559. }
  2560. }
  2561. return 0;
  2562. }
  2563. void kvm_lapic_exit(void)
  2564. {
  2565. static_key_deferred_flush(&apic_hw_disabled);
  2566. WARN_ON(static_branch_unlikely(&apic_hw_disabled.key));
  2567. static_key_deferred_flush(&apic_sw_disabled);
  2568. WARN_ON(static_branch_unlikely(&apic_sw_disabled.key));
  2569. }