lapic.c 89 KB

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