lapic.c 65 KB

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