lapic.c 62 KB

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