lapic.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968
  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/module.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 <linux/atomic.h>
  35. #include <linux/jump_label.h>
  36. #include "kvm_cache_regs.h"
  37. #include "irq.h"
  38. #include "trace.h"
  39. #include "x86.h"
  40. #include "cpuid.h"
  41. #ifndef CONFIG_X86_64
  42. #define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
  43. #else
  44. #define mod_64(x, y) ((x) % (y))
  45. #endif
  46. #define PRId64 "d"
  47. #define PRIx64 "llx"
  48. #define PRIu64 "u"
  49. #define PRIo64 "o"
  50. #define APIC_BUS_CYCLE_NS 1
  51. /* #define apic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg) */
  52. #define apic_debug(fmt, arg...)
  53. #define APIC_LVT_NUM 6
  54. /* 14 is the version for Xeon and Pentium 8.4.8*/
  55. #define APIC_VERSION (0x14UL | ((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. #define VEC_POS(v) ((v) & (32 - 1))
  66. #define REG_POS(v) (((v) >> 5) << 4)
  67. static inline void apic_set_reg(struct kvm_lapic *apic, int reg_off, u32 val)
  68. {
  69. *((u32 *) (apic->regs + reg_off)) = val;
  70. }
  71. static inline int apic_test_vector(int vec, void *bitmap)
  72. {
  73. return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  74. }
  75. bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
  76. {
  77. struct kvm_lapic *apic = vcpu->arch.apic;
  78. return apic_test_vector(vector, apic->regs + APIC_ISR) ||
  79. apic_test_vector(vector, apic->regs + APIC_IRR);
  80. }
  81. static inline void apic_set_vector(int vec, void *bitmap)
  82. {
  83. set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  84. }
  85. static inline void apic_clear_vector(int vec, void *bitmap)
  86. {
  87. clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  88. }
  89. static inline int __apic_test_and_set_vector(int vec, void *bitmap)
  90. {
  91. return __test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  92. }
  93. static inline int __apic_test_and_clear_vector(int vec, void *bitmap)
  94. {
  95. return __test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  96. }
  97. struct static_key_deferred apic_hw_disabled __read_mostly;
  98. struct static_key_deferred apic_sw_disabled __read_mostly;
  99. static inline int apic_enabled(struct kvm_lapic *apic)
  100. {
  101. return kvm_apic_sw_enabled(apic) && kvm_apic_hw_enabled(apic);
  102. }
  103. #define LVT_MASK \
  104. (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
  105. #define LINT_MASK \
  106. (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
  107. APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
  108. static inline int kvm_apic_id(struct kvm_lapic *apic)
  109. {
  110. return (kvm_apic_get_reg(apic, APIC_ID) >> 24) & 0xff;
  111. }
  112. #define KVM_X2APIC_CID_BITS 0
  113. static void recalculate_apic_map(struct kvm *kvm)
  114. {
  115. struct kvm_apic_map *new, *old = NULL;
  116. struct kvm_vcpu *vcpu;
  117. int i;
  118. new = kzalloc(sizeof(struct kvm_apic_map), GFP_KERNEL);
  119. mutex_lock(&kvm->arch.apic_map_lock);
  120. if (!new)
  121. goto out;
  122. new->ldr_bits = 8;
  123. /* flat mode is default */
  124. new->cid_shift = 8;
  125. new->cid_mask = 0;
  126. new->lid_mask = 0xff;
  127. new->broadcast = APIC_BROADCAST;
  128. kvm_for_each_vcpu(i, vcpu, kvm) {
  129. struct kvm_lapic *apic = vcpu->arch.apic;
  130. u16 cid, lid;
  131. u32 ldr;
  132. if (!kvm_apic_present(vcpu))
  133. continue;
  134. /*
  135. * All APICs have to be configured in the same mode by an OS.
  136. * We take advatage of this while building logical id loockup
  137. * table. After reset APICs are in xapic/flat mode, so if we
  138. * find apic with different setting we assume this is the mode
  139. * OS wants all apics to be in; build lookup table accordingly.
  140. */
  141. if (apic_x2apic_mode(apic)) {
  142. new->ldr_bits = 32;
  143. new->cid_shift = 16;
  144. new->cid_mask = (1 << KVM_X2APIC_CID_BITS) - 1;
  145. new->lid_mask = 0xffff;
  146. new->broadcast = X2APIC_BROADCAST;
  147. } else if (kvm_apic_sw_enabled(apic) &&
  148. !new->cid_mask /* flat mode */ &&
  149. kvm_apic_get_reg(apic, APIC_DFR) == APIC_DFR_CLUSTER) {
  150. new->cid_shift = 4;
  151. new->cid_mask = 0xf;
  152. new->lid_mask = 0xf;
  153. }
  154. new->phys_map[kvm_apic_id(apic)] = apic;
  155. ldr = kvm_apic_get_reg(apic, APIC_LDR);
  156. cid = apic_cluster_id(new, ldr);
  157. lid = apic_logical_id(new, ldr);
  158. if (lid)
  159. new->logical_map[cid][ffs(lid) - 1] = apic;
  160. }
  161. out:
  162. old = rcu_dereference_protected(kvm->arch.apic_map,
  163. lockdep_is_held(&kvm->arch.apic_map_lock));
  164. rcu_assign_pointer(kvm->arch.apic_map, new);
  165. mutex_unlock(&kvm->arch.apic_map_lock);
  166. if (old)
  167. kfree_rcu(old, rcu);
  168. kvm_vcpu_request_scan_ioapic(kvm);
  169. }
  170. static inline void apic_set_spiv(struct kvm_lapic *apic, u32 val)
  171. {
  172. bool enabled = val & APIC_SPIV_APIC_ENABLED;
  173. apic_set_reg(apic, APIC_SPIV, val);
  174. if (enabled != apic->sw_enabled) {
  175. apic->sw_enabled = enabled;
  176. if (enabled) {
  177. static_key_slow_dec_deferred(&apic_sw_disabled);
  178. recalculate_apic_map(apic->vcpu->kvm);
  179. } else
  180. static_key_slow_inc(&apic_sw_disabled.key);
  181. }
  182. }
  183. static inline void kvm_apic_set_id(struct kvm_lapic *apic, u8 id)
  184. {
  185. apic_set_reg(apic, APIC_ID, id << 24);
  186. recalculate_apic_map(apic->vcpu->kvm);
  187. }
  188. static inline void kvm_apic_set_ldr(struct kvm_lapic *apic, u32 id)
  189. {
  190. apic_set_reg(apic, APIC_LDR, id);
  191. recalculate_apic_map(apic->vcpu->kvm);
  192. }
  193. static inline int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type)
  194. {
  195. return !(kvm_apic_get_reg(apic, lvt_type) & APIC_LVT_MASKED);
  196. }
  197. static inline int apic_lvt_vector(struct kvm_lapic *apic, int lvt_type)
  198. {
  199. return kvm_apic_get_reg(apic, lvt_type) & APIC_VECTOR_MASK;
  200. }
  201. static inline int apic_lvtt_oneshot(struct kvm_lapic *apic)
  202. {
  203. return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_ONESHOT;
  204. }
  205. static inline int apic_lvtt_period(struct kvm_lapic *apic)
  206. {
  207. return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_PERIODIC;
  208. }
  209. static inline int apic_lvtt_tscdeadline(struct kvm_lapic *apic)
  210. {
  211. return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_TSCDEADLINE;
  212. }
  213. static inline int apic_lvt_nmi_mode(u32 lvt_val)
  214. {
  215. return (lvt_val & (APIC_MODE_MASK | APIC_LVT_MASKED)) == APIC_DM_NMI;
  216. }
  217. void kvm_apic_set_version(struct kvm_vcpu *vcpu)
  218. {
  219. struct kvm_lapic *apic = vcpu->arch.apic;
  220. struct kvm_cpuid_entry2 *feat;
  221. u32 v = APIC_VERSION;
  222. if (!kvm_vcpu_has_lapic(vcpu))
  223. return;
  224. feat = kvm_find_cpuid_entry(apic->vcpu, 0x1, 0);
  225. if (feat && (feat->ecx & (1 << (X86_FEATURE_X2APIC & 31))))
  226. v |= APIC_LVR_DIRECTED_EOI;
  227. apic_set_reg(apic, APIC_LVR, v);
  228. }
  229. static const unsigned int apic_lvt_mask[APIC_LVT_NUM] = {
  230. LVT_MASK , /* part LVTT mask, timer mode mask added at runtime */
  231. LVT_MASK | APIC_MODE_MASK, /* LVTTHMR */
  232. LVT_MASK | APIC_MODE_MASK, /* LVTPC */
  233. LINT_MASK, LINT_MASK, /* LVT0-1 */
  234. LVT_MASK /* LVTERR */
  235. };
  236. static int find_highest_vector(void *bitmap)
  237. {
  238. int vec;
  239. u32 *reg;
  240. for (vec = MAX_APIC_VECTOR - APIC_VECTORS_PER_REG;
  241. vec >= 0; vec -= APIC_VECTORS_PER_REG) {
  242. reg = bitmap + REG_POS(vec);
  243. if (*reg)
  244. return fls(*reg) - 1 + vec;
  245. }
  246. return -1;
  247. }
  248. static u8 count_vectors(void *bitmap)
  249. {
  250. int vec;
  251. u32 *reg;
  252. u8 count = 0;
  253. for (vec = 0; vec < MAX_APIC_VECTOR; vec += APIC_VECTORS_PER_REG) {
  254. reg = bitmap + REG_POS(vec);
  255. count += hweight32(*reg);
  256. }
  257. return count;
  258. }
  259. void kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir)
  260. {
  261. u32 i, pir_val;
  262. struct kvm_lapic *apic = vcpu->arch.apic;
  263. for (i = 0; i <= 7; i++) {
  264. pir_val = xchg(&pir[i], 0);
  265. if (pir_val)
  266. *((u32 *)(apic->regs + APIC_IRR + i * 0x10)) |= pir_val;
  267. }
  268. }
  269. EXPORT_SYMBOL_GPL(kvm_apic_update_irr);
  270. static inline void apic_set_irr(int vec, struct kvm_lapic *apic)
  271. {
  272. apic->irr_pending = true;
  273. apic_set_vector(vec, apic->regs + APIC_IRR);
  274. }
  275. static inline int apic_search_irr(struct kvm_lapic *apic)
  276. {
  277. return find_highest_vector(apic->regs + APIC_IRR);
  278. }
  279. static inline int apic_find_highest_irr(struct kvm_lapic *apic)
  280. {
  281. int result;
  282. /*
  283. * Note that irr_pending is just a hint. It will be always
  284. * true with virtual interrupt delivery enabled.
  285. */
  286. if (!apic->irr_pending)
  287. return -1;
  288. kvm_x86_ops->sync_pir_to_irr(apic->vcpu);
  289. result = apic_search_irr(apic);
  290. ASSERT(result == -1 || result >= 16);
  291. return result;
  292. }
  293. static inline void apic_clear_irr(int vec, struct kvm_lapic *apic)
  294. {
  295. struct kvm_vcpu *vcpu;
  296. vcpu = apic->vcpu;
  297. apic_clear_vector(vec, apic->regs + APIC_IRR);
  298. if (unlikely(kvm_apic_vid_enabled(vcpu->kvm)))
  299. /* try to update RVI */
  300. kvm_make_request(KVM_REQ_EVENT, vcpu);
  301. else {
  302. vec = apic_search_irr(apic);
  303. apic->irr_pending = (vec != -1);
  304. }
  305. }
  306. static inline void apic_set_isr(int vec, struct kvm_lapic *apic)
  307. {
  308. struct kvm_vcpu *vcpu;
  309. if (__apic_test_and_set_vector(vec, apic->regs + APIC_ISR))
  310. return;
  311. vcpu = apic->vcpu;
  312. /*
  313. * With APIC virtualization enabled, all caching is disabled
  314. * because the processor can modify ISR under the hood. Instead
  315. * just set SVI.
  316. */
  317. if (unlikely(kvm_apic_vid_enabled(vcpu->kvm)))
  318. kvm_x86_ops->hwapic_isr_update(vcpu->kvm, vec);
  319. else {
  320. ++apic->isr_count;
  321. BUG_ON(apic->isr_count > MAX_APIC_VECTOR);
  322. /*
  323. * ISR (in service register) bit is set when injecting an interrupt.
  324. * The highest vector is injected. Thus the latest bit set matches
  325. * the highest bit in ISR.
  326. */
  327. apic->highest_isr_cache = vec;
  328. }
  329. }
  330. static inline int apic_find_highest_isr(struct kvm_lapic *apic)
  331. {
  332. int result;
  333. /*
  334. * Note that isr_count is always 1, and highest_isr_cache
  335. * is always -1, with APIC virtualization enabled.
  336. */
  337. if (!apic->isr_count)
  338. return -1;
  339. if (likely(apic->highest_isr_cache != -1))
  340. return apic->highest_isr_cache;
  341. result = find_highest_vector(apic->regs + APIC_ISR);
  342. ASSERT(result == -1 || result >= 16);
  343. return result;
  344. }
  345. static inline void apic_clear_isr(int vec, struct kvm_lapic *apic)
  346. {
  347. struct kvm_vcpu *vcpu;
  348. if (!__apic_test_and_clear_vector(vec, apic->regs + APIC_ISR))
  349. return;
  350. vcpu = apic->vcpu;
  351. /*
  352. * We do get here for APIC virtualization enabled if the guest
  353. * uses the Hyper-V APIC enlightenment. In this case we may need
  354. * to trigger a new interrupt delivery by writing the SVI field;
  355. * on the other hand isr_count and highest_isr_cache are unused
  356. * and must be left alone.
  357. */
  358. if (unlikely(kvm_apic_vid_enabled(vcpu->kvm)))
  359. kvm_x86_ops->hwapic_isr_update(vcpu->kvm,
  360. apic_find_highest_isr(apic));
  361. else {
  362. --apic->isr_count;
  363. BUG_ON(apic->isr_count < 0);
  364. apic->highest_isr_cache = -1;
  365. }
  366. }
  367. int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu)
  368. {
  369. int highest_irr;
  370. /* This may race with setting of irr in __apic_accept_irq() and
  371. * value returned may be wrong, but kvm_vcpu_kick() in __apic_accept_irq
  372. * will cause vmexit immediately and the value will be recalculated
  373. * on the next vmentry.
  374. */
  375. if (!kvm_vcpu_has_lapic(vcpu))
  376. return 0;
  377. highest_irr = apic_find_highest_irr(vcpu->arch.apic);
  378. return highest_irr;
  379. }
  380. static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
  381. int vector, int level, int trig_mode,
  382. unsigned long *dest_map);
  383. int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq,
  384. unsigned long *dest_map)
  385. {
  386. struct kvm_lapic *apic = vcpu->arch.apic;
  387. return __apic_accept_irq(apic, irq->delivery_mode, irq->vector,
  388. irq->level, irq->trig_mode, dest_map);
  389. }
  390. static int pv_eoi_put_user(struct kvm_vcpu *vcpu, u8 val)
  391. {
  392. return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, &val,
  393. sizeof(val));
  394. }
  395. static int pv_eoi_get_user(struct kvm_vcpu *vcpu, u8 *val)
  396. {
  397. return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, val,
  398. sizeof(*val));
  399. }
  400. static inline bool pv_eoi_enabled(struct kvm_vcpu *vcpu)
  401. {
  402. return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED;
  403. }
  404. static bool pv_eoi_get_pending(struct kvm_vcpu *vcpu)
  405. {
  406. u8 val;
  407. if (pv_eoi_get_user(vcpu, &val) < 0)
  408. apic_debug("Can't read EOI MSR value: 0x%llx\n",
  409. (unsigned long long)vcpu->arch.pv_eoi.msr_val);
  410. return val & 0x1;
  411. }
  412. static void pv_eoi_set_pending(struct kvm_vcpu *vcpu)
  413. {
  414. if (pv_eoi_put_user(vcpu, KVM_PV_EOI_ENABLED) < 0) {
  415. apic_debug("Can't set EOI MSR value: 0x%llx\n",
  416. (unsigned long long)vcpu->arch.pv_eoi.msr_val);
  417. return;
  418. }
  419. __set_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
  420. }
  421. static void pv_eoi_clr_pending(struct kvm_vcpu *vcpu)
  422. {
  423. if (pv_eoi_put_user(vcpu, KVM_PV_EOI_DISABLED) < 0) {
  424. apic_debug("Can't clear EOI MSR value: 0x%llx\n",
  425. (unsigned long long)vcpu->arch.pv_eoi.msr_val);
  426. return;
  427. }
  428. __clear_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
  429. }
  430. void kvm_apic_update_tmr(struct kvm_vcpu *vcpu, u32 *tmr)
  431. {
  432. struct kvm_lapic *apic = vcpu->arch.apic;
  433. int i;
  434. for (i = 0; i < 8; i++)
  435. apic_set_reg(apic, APIC_TMR + 0x10 * i, tmr[i]);
  436. }
  437. static void apic_update_ppr(struct kvm_lapic *apic)
  438. {
  439. u32 tpr, isrv, ppr, old_ppr;
  440. int isr;
  441. old_ppr = kvm_apic_get_reg(apic, APIC_PROCPRI);
  442. tpr = kvm_apic_get_reg(apic, APIC_TASKPRI);
  443. isr = apic_find_highest_isr(apic);
  444. isrv = (isr != -1) ? isr : 0;
  445. if ((tpr & 0xf0) >= (isrv & 0xf0))
  446. ppr = tpr & 0xff;
  447. else
  448. ppr = isrv & 0xf0;
  449. apic_debug("vlapic %p, ppr 0x%x, isr 0x%x, isrv 0x%x",
  450. apic, ppr, isr, isrv);
  451. if (old_ppr != ppr) {
  452. apic_set_reg(apic, APIC_PROCPRI, ppr);
  453. if (ppr < old_ppr)
  454. kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
  455. }
  456. }
  457. static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr)
  458. {
  459. apic_set_reg(apic, APIC_TASKPRI, tpr);
  460. apic_update_ppr(apic);
  461. }
  462. static int kvm_apic_broadcast(struct kvm_lapic *apic, u32 dest)
  463. {
  464. return dest == (apic_x2apic_mode(apic) ?
  465. X2APIC_BROADCAST : APIC_BROADCAST);
  466. }
  467. int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u32 dest)
  468. {
  469. return kvm_apic_id(apic) == dest || kvm_apic_broadcast(apic, dest);
  470. }
  471. int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u32 mda)
  472. {
  473. int result = 0;
  474. u32 logical_id;
  475. if (kvm_apic_broadcast(apic, mda))
  476. return 1;
  477. if (apic_x2apic_mode(apic)) {
  478. logical_id = kvm_apic_get_reg(apic, APIC_LDR);
  479. return logical_id & mda;
  480. }
  481. logical_id = GET_APIC_LOGICAL_ID(kvm_apic_get_reg(apic, APIC_LDR));
  482. switch (kvm_apic_get_reg(apic, APIC_DFR)) {
  483. case APIC_DFR_FLAT:
  484. if (logical_id & mda)
  485. result = 1;
  486. break;
  487. case APIC_DFR_CLUSTER:
  488. if (((logical_id >> 4) == (mda >> 0x4))
  489. && (logical_id & mda & 0xf))
  490. result = 1;
  491. break;
  492. default:
  493. apic_debug("Bad DFR vcpu %d: %08x\n",
  494. apic->vcpu->vcpu_id, kvm_apic_get_reg(apic, APIC_DFR));
  495. break;
  496. }
  497. return result;
  498. }
  499. int kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
  500. int short_hand, unsigned int dest, int dest_mode)
  501. {
  502. int result = 0;
  503. struct kvm_lapic *target = vcpu->arch.apic;
  504. apic_debug("target %p, source %p, dest 0x%x, "
  505. "dest_mode 0x%x, short_hand 0x%x\n",
  506. target, source, dest, dest_mode, short_hand);
  507. ASSERT(target);
  508. switch (short_hand) {
  509. case APIC_DEST_NOSHORT:
  510. if (dest_mode == 0)
  511. /* Physical mode. */
  512. result = kvm_apic_match_physical_addr(target, dest);
  513. else
  514. /* Logical mode. */
  515. result = kvm_apic_match_logical_addr(target, dest);
  516. break;
  517. case APIC_DEST_SELF:
  518. result = (target == source);
  519. break;
  520. case APIC_DEST_ALLINC:
  521. result = 1;
  522. break;
  523. case APIC_DEST_ALLBUT:
  524. result = (target != source);
  525. break;
  526. default:
  527. apic_debug("kvm: apic: Bad dest shorthand value %x\n",
  528. short_hand);
  529. break;
  530. }
  531. return result;
  532. }
  533. bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
  534. struct kvm_lapic_irq *irq, int *r, unsigned long *dest_map)
  535. {
  536. struct kvm_apic_map *map;
  537. unsigned long bitmap = 1;
  538. struct kvm_lapic **dst;
  539. int i;
  540. bool ret = false;
  541. *r = -1;
  542. if (irq->shorthand == APIC_DEST_SELF) {
  543. *r = kvm_apic_set_irq(src->vcpu, irq, dest_map);
  544. return true;
  545. }
  546. if (irq->shorthand)
  547. return false;
  548. rcu_read_lock();
  549. map = rcu_dereference(kvm->arch.apic_map);
  550. if (!map)
  551. goto out;
  552. if (irq->dest_id == map->broadcast)
  553. goto out;
  554. if (irq->dest_mode == 0) { /* physical mode */
  555. if (irq->delivery_mode == APIC_DM_LOWEST)
  556. goto out;
  557. dst = &map->phys_map[irq->dest_id & 0xff];
  558. } else {
  559. u32 mda = irq->dest_id << (32 - map->ldr_bits);
  560. dst = map->logical_map[apic_cluster_id(map, mda)];
  561. bitmap = apic_logical_id(map, mda);
  562. if (irq->delivery_mode == APIC_DM_LOWEST) {
  563. int l = -1;
  564. for_each_set_bit(i, &bitmap, 16) {
  565. if (!dst[i])
  566. continue;
  567. if (l < 0)
  568. l = i;
  569. else if (kvm_apic_compare_prio(dst[i]->vcpu, dst[l]->vcpu) < 0)
  570. l = i;
  571. }
  572. bitmap = (l >= 0) ? 1 << l : 0;
  573. }
  574. }
  575. for_each_set_bit(i, &bitmap, 16) {
  576. if (!dst[i])
  577. continue;
  578. if (*r < 0)
  579. *r = 0;
  580. *r += kvm_apic_set_irq(dst[i]->vcpu, irq, dest_map);
  581. }
  582. ret = true;
  583. out:
  584. rcu_read_unlock();
  585. return ret;
  586. }
  587. /*
  588. * Add a pending IRQ into lapic.
  589. * Return 1 if successfully added and 0 if discarded.
  590. */
  591. static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
  592. int vector, int level, int trig_mode,
  593. unsigned long *dest_map)
  594. {
  595. int result = 0;
  596. struct kvm_vcpu *vcpu = apic->vcpu;
  597. trace_kvm_apic_accept_irq(vcpu->vcpu_id, delivery_mode,
  598. trig_mode, vector);
  599. switch (delivery_mode) {
  600. case APIC_DM_LOWEST:
  601. vcpu->arch.apic_arb_prio++;
  602. case APIC_DM_FIXED:
  603. /* FIXME add logic for vcpu on reset */
  604. if (unlikely(!apic_enabled(apic)))
  605. break;
  606. result = 1;
  607. if (dest_map)
  608. __set_bit(vcpu->vcpu_id, dest_map);
  609. if (kvm_x86_ops->deliver_posted_interrupt)
  610. kvm_x86_ops->deliver_posted_interrupt(vcpu, vector);
  611. else {
  612. apic_set_irr(vector, apic);
  613. kvm_make_request(KVM_REQ_EVENT, vcpu);
  614. kvm_vcpu_kick(vcpu);
  615. }
  616. break;
  617. case APIC_DM_REMRD:
  618. result = 1;
  619. vcpu->arch.pv.pv_unhalted = 1;
  620. kvm_make_request(KVM_REQ_EVENT, vcpu);
  621. kvm_vcpu_kick(vcpu);
  622. break;
  623. case APIC_DM_SMI:
  624. apic_debug("Ignoring guest SMI\n");
  625. break;
  626. case APIC_DM_NMI:
  627. result = 1;
  628. kvm_inject_nmi(vcpu);
  629. kvm_vcpu_kick(vcpu);
  630. break;
  631. case APIC_DM_INIT:
  632. if (!trig_mode || level) {
  633. result = 1;
  634. /* assumes that there are only KVM_APIC_INIT/SIPI */
  635. apic->pending_events = (1UL << KVM_APIC_INIT);
  636. /* make sure pending_events is visible before sending
  637. * the request */
  638. smp_wmb();
  639. kvm_make_request(KVM_REQ_EVENT, vcpu);
  640. kvm_vcpu_kick(vcpu);
  641. } else {
  642. apic_debug("Ignoring de-assert INIT to vcpu %d\n",
  643. vcpu->vcpu_id);
  644. }
  645. break;
  646. case APIC_DM_STARTUP:
  647. apic_debug("SIPI to vcpu %d vector 0x%02x\n",
  648. vcpu->vcpu_id, vector);
  649. result = 1;
  650. apic->sipi_vector = vector;
  651. /* make sure sipi_vector is visible for the receiver */
  652. smp_wmb();
  653. set_bit(KVM_APIC_SIPI, &apic->pending_events);
  654. kvm_make_request(KVM_REQ_EVENT, vcpu);
  655. kvm_vcpu_kick(vcpu);
  656. break;
  657. case APIC_DM_EXTINT:
  658. /*
  659. * Should only be called by kvm_apic_local_deliver() with LVT0,
  660. * before NMI watchdog was enabled. Already handled by
  661. * kvm_apic_accept_pic_intr().
  662. */
  663. break;
  664. default:
  665. printk(KERN_ERR "TODO: unsupported delivery mode %x\n",
  666. delivery_mode);
  667. break;
  668. }
  669. return result;
  670. }
  671. int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
  672. {
  673. return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
  674. }
  675. static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
  676. {
  677. if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI) &&
  678. kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) {
  679. int trigger_mode;
  680. if (apic_test_vector(vector, apic->regs + APIC_TMR))
  681. trigger_mode = IOAPIC_LEVEL_TRIG;
  682. else
  683. trigger_mode = IOAPIC_EDGE_TRIG;
  684. kvm_ioapic_update_eoi(apic->vcpu, vector, trigger_mode);
  685. }
  686. }
  687. static int apic_set_eoi(struct kvm_lapic *apic)
  688. {
  689. int vector = apic_find_highest_isr(apic);
  690. trace_kvm_eoi(apic, vector);
  691. /*
  692. * Not every write EOI will has corresponding ISR,
  693. * one example is when Kernel check timer on setup_IO_APIC
  694. */
  695. if (vector == -1)
  696. return vector;
  697. apic_clear_isr(vector, apic);
  698. apic_update_ppr(apic);
  699. kvm_ioapic_send_eoi(apic, vector);
  700. kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
  701. return vector;
  702. }
  703. /*
  704. * this interface assumes a trap-like exit, which has already finished
  705. * desired side effect including vISR and vPPR update.
  706. */
  707. void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
  708. {
  709. struct kvm_lapic *apic = vcpu->arch.apic;
  710. trace_kvm_eoi(apic, vector);
  711. kvm_ioapic_send_eoi(apic, vector);
  712. kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
  713. }
  714. EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
  715. static void apic_send_ipi(struct kvm_lapic *apic)
  716. {
  717. u32 icr_low = kvm_apic_get_reg(apic, APIC_ICR);
  718. u32 icr_high = kvm_apic_get_reg(apic, APIC_ICR2);
  719. struct kvm_lapic_irq irq;
  720. irq.vector = icr_low & APIC_VECTOR_MASK;
  721. irq.delivery_mode = icr_low & APIC_MODE_MASK;
  722. irq.dest_mode = icr_low & APIC_DEST_MASK;
  723. irq.level = icr_low & APIC_INT_ASSERT;
  724. irq.trig_mode = icr_low & APIC_INT_LEVELTRIG;
  725. irq.shorthand = icr_low & APIC_SHORT_MASK;
  726. if (apic_x2apic_mode(apic))
  727. irq.dest_id = icr_high;
  728. else
  729. irq.dest_id = GET_APIC_DEST_FIELD(icr_high);
  730. trace_kvm_apic_ipi(icr_low, irq.dest_id);
  731. apic_debug("icr_high 0x%x, icr_low 0x%x, "
  732. "short_hand 0x%x, dest 0x%x, trig_mode 0x%x, level 0x%x, "
  733. "dest_mode 0x%x, delivery_mode 0x%x, vector 0x%x\n",
  734. icr_high, icr_low, irq.shorthand, irq.dest_id,
  735. irq.trig_mode, irq.level, irq.dest_mode, irq.delivery_mode,
  736. irq.vector);
  737. kvm_irq_delivery_to_apic(apic->vcpu->kvm, apic, &irq, NULL);
  738. }
  739. static u32 apic_get_tmcct(struct kvm_lapic *apic)
  740. {
  741. ktime_t remaining;
  742. s64 ns;
  743. u32 tmcct;
  744. ASSERT(apic != NULL);
  745. /* if initial count is 0, current count should also be 0 */
  746. if (kvm_apic_get_reg(apic, APIC_TMICT) == 0 ||
  747. apic->lapic_timer.period == 0)
  748. return 0;
  749. remaining = hrtimer_get_remaining(&apic->lapic_timer.timer);
  750. if (ktime_to_ns(remaining) < 0)
  751. remaining = ktime_set(0, 0);
  752. ns = mod_64(ktime_to_ns(remaining), apic->lapic_timer.period);
  753. tmcct = div64_u64(ns,
  754. (APIC_BUS_CYCLE_NS * apic->divide_count));
  755. return tmcct;
  756. }
  757. static void __report_tpr_access(struct kvm_lapic *apic, bool write)
  758. {
  759. struct kvm_vcpu *vcpu = apic->vcpu;
  760. struct kvm_run *run = vcpu->run;
  761. kvm_make_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu);
  762. run->tpr_access.rip = kvm_rip_read(vcpu);
  763. run->tpr_access.is_write = write;
  764. }
  765. static inline void report_tpr_access(struct kvm_lapic *apic, bool write)
  766. {
  767. if (apic->vcpu->arch.tpr_access_reporting)
  768. __report_tpr_access(apic, write);
  769. }
  770. static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset)
  771. {
  772. u32 val = 0;
  773. if (offset >= LAPIC_MMIO_LENGTH)
  774. return 0;
  775. switch (offset) {
  776. case APIC_ID:
  777. if (apic_x2apic_mode(apic))
  778. val = kvm_apic_id(apic);
  779. else
  780. val = kvm_apic_id(apic) << 24;
  781. break;
  782. case APIC_ARBPRI:
  783. apic_debug("Access APIC ARBPRI register which is for P6\n");
  784. break;
  785. case APIC_TMCCT: /* Timer CCR */
  786. if (apic_lvtt_tscdeadline(apic))
  787. return 0;
  788. val = apic_get_tmcct(apic);
  789. break;
  790. case APIC_PROCPRI:
  791. apic_update_ppr(apic);
  792. val = kvm_apic_get_reg(apic, offset);
  793. break;
  794. case APIC_TASKPRI:
  795. report_tpr_access(apic, false);
  796. /* fall thru */
  797. default:
  798. val = kvm_apic_get_reg(apic, offset);
  799. break;
  800. }
  801. return val;
  802. }
  803. static inline struct kvm_lapic *to_lapic(struct kvm_io_device *dev)
  804. {
  805. return container_of(dev, struct kvm_lapic, dev);
  806. }
  807. static int apic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
  808. void *data)
  809. {
  810. unsigned char alignment = offset & 0xf;
  811. u32 result;
  812. /* this bitmask has a bit cleared for each reserved register */
  813. static const u64 rmask = 0x43ff01ffffffe70cULL;
  814. if ((alignment + len) > 4) {
  815. apic_debug("KVM_APIC_READ: alignment error %x %d\n",
  816. offset, len);
  817. return 1;
  818. }
  819. if (offset > 0x3f0 || !(rmask & (1ULL << (offset >> 4)))) {
  820. apic_debug("KVM_APIC_READ: read reserved register %x\n",
  821. offset);
  822. return 1;
  823. }
  824. result = __apic_read(apic, offset & ~0xf);
  825. trace_kvm_apic_read(offset, result);
  826. switch (len) {
  827. case 1:
  828. case 2:
  829. case 4:
  830. memcpy(data, (char *)&result + alignment, len);
  831. break;
  832. default:
  833. printk(KERN_ERR "Local APIC read with len = %x, "
  834. "should be 1,2, or 4 instead\n", len);
  835. break;
  836. }
  837. return 0;
  838. }
  839. static int apic_mmio_in_range(struct kvm_lapic *apic, gpa_t addr)
  840. {
  841. return kvm_apic_hw_enabled(apic) &&
  842. addr >= apic->base_address &&
  843. addr < apic->base_address + LAPIC_MMIO_LENGTH;
  844. }
  845. static int apic_mmio_read(struct kvm_io_device *this,
  846. gpa_t address, int len, void *data)
  847. {
  848. struct kvm_lapic *apic = to_lapic(this);
  849. u32 offset = address - apic->base_address;
  850. if (!apic_mmio_in_range(apic, address))
  851. return -EOPNOTSUPP;
  852. apic_reg_read(apic, offset, len, data);
  853. return 0;
  854. }
  855. static void update_divide_count(struct kvm_lapic *apic)
  856. {
  857. u32 tmp1, tmp2, tdcr;
  858. tdcr = kvm_apic_get_reg(apic, APIC_TDCR);
  859. tmp1 = tdcr & 0xf;
  860. tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
  861. apic->divide_count = 0x1 << (tmp2 & 0x7);
  862. apic_debug("timer divide count is 0x%x\n",
  863. apic->divide_count);
  864. }
  865. static void apic_timer_expired(struct kvm_lapic *apic)
  866. {
  867. struct kvm_vcpu *vcpu = apic->vcpu;
  868. wait_queue_head_t *q = &vcpu->wq;
  869. /*
  870. * Note: KVM_REQ_PENDING_TIMER is implicitly checked in
  871. * vcpu_enter_guest.
  872. */
  873. if (atomic_read(&apic->lapic_timer.pending))
  874. return;
  875. atomic_inc(&apic->lapic_timer.pending);
  876. /* FIXME: this code should not know anything about vcpus */
  877. kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
  878. if (waitqueue_active(q))
  879. wake_up_interruptible(q);
  880. }
  881. static void start_apic_timer(struct kvm_lapic *apic)
  882. {
  883. ktime_t now;
  884. atomic_set(&apic->lapic_timer.pending, 0);
  885. if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic)) {
  886. /* lapic timer in oneshot or periodic mode */
  887. now = apic->lapic_timer.timer.base->get_time();
  888. apic->lapic_timer.period = (u64)kvm_apic_get_reg(apic, APIC_TMICT)
  889. * APIC_BUS_CYCLE_NS * apic->divide_count;
  890. if (!apic->lapic_timer.period)
  891. return;
  892. /*
  893. * Do not allow the guest to program periodic timers with small
  894. * interval, since the hrtimers are not throttled by the host
  895. * scheduler.
  896. */
  897. if (apic_lvtt_period(apic)) {
  898. s64 min_period = min_timer_period_us * 1000LL;
  899. if (apic->lapic_timer.period < min_period) {
  900. pr_info_ratelimited(
  901. "kvm: vcpu %i: requested %lld ns "
  902. "lapic timer period limited to %lld ns\n",
  903. apic->vcpu->vcpu_id,
  904. apic->lapic_timer.period, min_period);
  905. apic->lapic_timer.period = min_period;
  906. }
  907. }
  908. hrtimer_start(&apic->lapic_timer.timer,
  909. ktime_add_ns(now, apic->lapic_timer.period),
  910. HRTIMER_MODE_ABS);
  911. apic_debug("%s: bus cycle is %" PRId64 "ns, now 0x%016"
  912. PRIx64 ", "
  913. "timer initial count 0x%x, period %lldns, "
  914. "expire @ 0x%016" PRIx64 ".\n", __func__,
  915. APIC_BUS_CYCLE_NS, ktime_to_ns(now),
  916. kvm_apic_get_reg(apic, APIC_TMICT),
  917. apic->lapic_timer.period,
  918. ktime_to_ns(ktime_add_ns(now,
  919. apic->lapic_timer.period)));
  920. } else if (apic_lvtt_tscdeadline(apic)) {
  921. /* lapic timer in tsc deadline mode */
  922. u64 guest_tsc, tscdeadline = apic->lapic_timer.tscdeadline;
  923. u64 ns = 0;
  924. struct kvm_vcpu *vcpu = apic->vcpu;
  925. unsigned long this_tsc_khz = vcpu->arch.virtual_tsc_khz;
  926. unsigned long flags;
  927. if (unlikely(!tscdeadline || !this_tsc_khz))
  928. return;
  929. local_irq_save(flags);
  930. now = apic->lapic_timer.timer.base->get_time();
  931. guest_tsc = kvm_x86_ops->read_l1_tsc(vcpu, native_read_tsc());
  932. if (likely(tscdeadline > guest_tsc)) {
  933. ns = (tscdeadline - guest_tsc) * 1000000ULL;
  934. do_div(ns, this_tsc_khz);
  935. hrtimer_start(&apic->lapic_timer.timer,
  936. ktime_add_ns(now, ns), HRTIMER_MODE_ABS);
  937. } else
  938. apic_timer_expired(apic);
  939. local_irq_restore(flags);
  940. }
  941. }
  942. static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val)
  943. {
  944. int nmi_wd_enabled = apic_lvt_nmi_mode(kvm_apic_get_reg(apic, APIC_LVT0));
  945. if (apic_lvt_nmi_mode(lvt0_val)) {
  946. if (!nmi_wd_enabled) {
  947. apic_debug("Receive NMI setting on APIC_LVT0 "
  948. "for cpu %d\n", apic->vcpu->vcpu_id);
  949. apic->vcpu->kvm->arch.vapics_in_nmi_mode++;
  950. }
  951. } else if (nmi_wd_enabled)
  952. apic->vcpu->kvm->arch.vapics_in_nmi_mode--;
  953. }
  954. static int apic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
  955. {
  956. int ret = 0;
  957. trace_kvm_apic_write(reg, val);
  958. switch (reg) {
  959. case APIC_ID: /* Local APIC ID */
  960. if (!apic_x2apic_mode(apic))
  961. kvm_apic_set_id(apic, val >> 24);
  962. else
  963. ret = 1;
  964. break;
  965. case APIC_TASKPRI:
  966. report_tpr_access(apic, true);
  967. apic_set_tpr(apic, val & 0xff);
  968. break;
  969. case APIC_EOI:
  970. apic_set_eoi(apic);
  971. break;
  972. case APIC_LDR:
  973. if (!apic_x2apic_mode(apic))
  974. kvm_apic_set_ldr(apic, val & APIC_LDR_MASK);
  975. else
  976. ret = 1;
  977. break;
  978. case APIC_DFR:
  979. if (!apic_x2apic_mode(apic)) {
  980. apic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF);
  981. recalculate_apic_map(apic->vcpu->kvm);
  982. } else
  983. ret = 1;
  984. break;
  985. case APIC_SPIV: {
  986. u32 mask = 0x3ff;
  987. if (kvm_apic_get_reg(apic, APIC_LVR) & APIC_LVR_DIRECTED_EOI)
  988. mask |= APIC_SPIV_DIRECTED_EOI;
  989. apic_set_spiv(apic, val & mask);
  990. if (!(val & APIC_SPIV_APIC_ENABLED)) {
  991. int i;
  992. u32 lvt_val;
  993. for (i = 0; i < APIC_LVT_NUM; i++) {
  994. lvt_val = kvm_apic_get_reg(apic,
  995. APIC_LVTT + 0x10 * i);
  996. apic_set_reg(apic, APIC_LVTT + 0x10 * i,
  997. lvt_val | APIC_LVT_MASKED);
  998. }
  999. atomic_set(&apic->lapic_timer.pending, 0);
  1000. }
  1001. break;
  1002. }
  1003. case APIC_ICR:
  1004. /* No delay here, so we always clear the pending bit */
  1005. apic_set_reg(apic, APIC_ICR, val & ~(1 << 12));
  1006. apic_send_ipi(apic);
  1007. break;
  1008. case APIC_ICR2:
  1009. if (!apic_x2apic_mode(apic))
  1010. val &= 0xff000000;
  1011. apic_set_reg(apic, APIC_ICR2, val);
  1012. break;
  1013. case APIC_LVT0:
  1014. apic_manage_nmi_watchdog(apic, val);
  1015. case APIC_LVTTHMR:
  1016. case APIC_LVTPC:
  1017. case APIC_LVT1:
  1018. case APIC_LVTERR:
  1019. /* TODO: Check vector */
  1020. if (!kvm_apic_sw_enabled(apic))
  1021. val |= APIC_LVT_MASKED;
  1022. val &= apic_lvt_mask[(reg - APIC_LVTT) >> 4];
  1023. apic_set_reg(apic, reg, val);
  1024. break;
  1025. case APIC_LVTT: {
  1026. u32 timer_mode = val & apic->lapic_timer.timer_mode_mask;
  1027. if (apic->lapic_timer.timer_mode != timer_mode) {
  1028. apic->lapic_timer.timer_mode = timer_mode;
  1029. hrtimer_cancel(&apic->lapic_timer.timer);
  1030. }
  1031. if (!kvm_apic_sw_enabled(apic))
  1032. val |= APIC_LVT_MASKED;
  1033. val &= (apic_lvt_mask[0] | apic->lapic_timer.timer_mode_mask);
  1034. apic_set_reg(apic, APIC_LVTT, val);
  1035. break;
  1036. }
  1037. case APIC_TMICT:
  1038. if (apic_lvtt_tscdeadline(apic))
  1039. break;
  1040. hrtimer_cancel(&apic->lapic_timer.timer);
  1041. apic_set_reg(apic, APIC_TMICT, val);
  1042. start_apic_timer(apic);
  1043. break;
  1044. case APIC_TDCR:
  1045. if (val & 4)
  1046. apic_debug("KVM_WRITE:TDCR %x\n", val);
  1047. apic_set_reg(apic, APIC_TDCR, val);
  1048. update_divide_count(apic);
  1049. break;
  1050. case APIC_ESR:
  1051. if (apic_x2apic_mode(apic) && val != 0) {
  1052. apic_debug("KVM_WRITE:ESR not zero %x\n", val);
  1053. ret = 1;
  1054. }
  1055. break;
  1056. case APIC_SELF_IPI:
  1057. if (apic_x2apic_mode(apic)) {
  1058. apic_reg_write(apic, APIC_ICR, 0x40000 | (val & 0xff));
  1059. } else
  1060. ret = 1;
  1061. break;
  1062. default:
  1063. ret = 1;
  1064. break;
  1065. }
  1066. if (ret)
  1067. apic_debug("Local APIC Write to read-only register %x\n", reg);
  1068. return ret;
  1069. }
  1070. static int apic_mmio_write(struct kvm_io_device *this,
  1071. gpa_t address, int len, const void *data)
  1072. {
  1073. struct kvm_lapic *apic = to_lapic(this);
  1074. unsigned int offset = address - apic->base_address;
  1075. u32 val;
  1076. if (!apic_mmio_in_range(apic, address))
  1077. return -EOPNOTSUPP;
  1078. /*
  1079. * APIC register must be aligned on 128-bits boundary.
  1080. * 32/64/128 bits registers must be accessed thru 32 bits.
  1081. * Refer SDM 8.4.1
  1082. */
  1083. if (len != 4 || (offset & 0xf)) {
  1084. /* Don't shout loud, $infamous_os would cause only noise. */
  1085. apic_debug("apic write: bad size=%d %lx\n", len, (long)address);
  1086. return 0;
  1087. }
  1088. val = *(u32*)data;
  1089. /* too common printing */
  1090. if (offset != APIC_EOI)
  1091. apic_debug("%s: offset 0x%x with length 0x%x, and value is "
  1092. "0x%x\n", __func__, offset, len, val);
  1093. apic_reg_write(apic, offset & 0xff0, val);
  1094. return 0;
  1095. }
  1096. void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu)
  1097. {
  1098. if (kvm_vcpu_has_lapic(vcpu))
  1099. apic_reg_write(vcpu->arch.apic, APIC_EOI, 0);
  1100. }
  1101. EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi);
  1102. /* emulate APIC access in a trap manner */
  1103. void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset)
  1104. {
  1105. u32 val = 0;
  1106. /* hw has done the conditional check and inst decode */
  1107. offset &= 0xff0;
  1108. apic_reg_read(vcpu->arch.apic, offset, 4, &val);
  1109. /* TODO: optimize to just emulate side effect w/o one more write */
  1110. apic_reg_write(vcpu->arch.apic, offset, val);
  1111. }
  1112. EXPORT_SYMBOL_GPL(kvm_apic_write_nodecode);
  1113. void kvm_free_lapic(struct kvm_vcpu *vcpu)
  1114. {
  1115. struct kvm_lapic *apic = vcpu->arch.apic;
  1116. if (!vcpu->arch.apic)
  1117. return;
  1118. hrtimer_cancel(&apic->lapic_timer.timer);
  1119. if (!(vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE))
  1120. static_key_slow_dec_deferred(&apic_hw_disabled);
  1121. if (!apic->sw_enabled)
  1122. static_key_slow_dec_deferred(&apic_sw_disabled);
  1123. if (apic->regs)
  1124. free_page((unsigned long)apic->regs);
  1125. kfree(apic);
  1126. }
  1127. /*
  1128. *----------------------------------------------------------------------
  1129. * LAPIC interface
  1130. *----------------------------------------------------------------------
  1131. */
  1132. u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu)
  1133. {
  1134. struct kvm_lapic *apic = vcpu->arch.apic;
  1135. if (!kvm_vcpu_has_lapic(vcpu) || apic_lvtt_oneshot(apic) ||
  1136. apic_lvtt_period(apic))
  1137. return 0;
  1138. return apic->lapic_timer.tscdeadline;
  1139. }
  1140. void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data)
  1141. {
  1142. struct kvm_lapic *apic = vcpu->arch.apic;
  1143. if (!kvm_vcpu_has_lapic(vcpu) || apic_lvtt_oneshot(apic) ||
  1144. apic_lvtt_period(apic))
  1145. return;
  1146. hrtimer_cancel(&apic->lapic_timer.timer);
  1147. apic->lapic_timer.tscdeadline = data;
  1148. start_apic_timer(apic);
  1149. }
  1150. void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
  1151. {
  1152. struct kvm_lapic *apic = vcpu->arch.apic;
  1153. if (!kvm_vcpu_has_lapic(vcpu))
  1154. return;
  1155. apic_set_tpr(apic, ((cr8 & 0x0f) << 4)
  1156. | (kvm_apic_get_reg(apic, APIC_TASKPRI) & 4));
  1157. }
  1158. u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
  1159. {
  1160. u64 tpr;
  1161. if (!kvm_vcpu_has_lapic(vcpu))
  1162. return 0;
  1163. tpr = (u64) kvm_apic_get_reg(vcpu->arch.apic, APIC_TASKPRI);
  1164. return (tpr & 0xf0) >> 4;
  1165. }
  1166. void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
  1167. {
  1168. u64 old_value = vcpu->arch.apic_base;
  1169. struct kvm_lapic *apic = vcpu->arch.apic;
  1170. if (!apic) {
  1171. value |= MSR_IA32_APICBASE_BSP;
  1172. vcpu->arch.apic_base = value;
  1173. return;
  1174. }
  1175. if (!kvm_vcpu_is_bsp(apic->vcpu))
  1176. value &= ~MSR_IA32_APICBASE_BSP;
  1177. vcpu->arch.apic_base = value;
  1178. /* update jump label if enable bit changes */
  1179. if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE) {
  1180. if (value & MSR_IA32_APICBASE_ENABLE)
  1181. static_key_slow_dec_deferred(&apic_hw_disabled);
  1182. else
  1183. static_key_slow_inc(&apic_hw_disabled.key);
  1184. recalculate_apic_map(vcpu->kvm);
  1185. }
  1186. if ((old_value ^ value) & X2APIC_ENABLE) {
  1187. if (value & X2APIC_ENABLE) {
  1188. u32 id = kvm_apic_id(apic);
  1189. u32 ldr = ((id >> 4) << 16) | (1 << (id & 0xf));
  1190. kvm_apic_set_ldr(apic, ldr);
  1191. kvm_x86_ops->set_virtual_x2apic_mode(vcpu, true);
  1192. } else
  1193. kvm_x86_ops->set_virtual_x2apic_mode(vcpu, false);
  1194. }
  1195. apic->base_address = apic->vcpu->arch.apic_base &
  1196. MSR_IA32_APICBASE_BASE;
  1197. /* with FSB delivery interrupt, we can restart APIC functionality */
  1198. apic_debug("apic base msr is 0x%016" PRIx64 ", and base address is "
  1199. "0x%lx.\n", apic->vcpu->arch.apic_base, apic->base_address);
  1200. }
  1201. void kvm_lapic_reset(struct kvm_vcpu *vcpu)
  1202. {
  1203. struct kvm_lapic *apic;
  1204. int i;
  1205. apic_debug("%s\n", __func__);
  1206. ASSERT(vcpu);
  1207. apic = vcpu->arch.apic;
  1208. ASSERT(apic != NULL);
  1209. /* Stop the timer in case it's a reset to an active apic */
  1210. hrtimer_cancel(&apic->lapic_timer.timer);
  1211. kvm_apic_set_id(apic, vcpu->vcpu_id);
  1212. kvm_apic_set_version(apic->vcpu);
  1213. for (i = 0; i < APIC_LVT_NUM; i++)
  1214. apic_set_reg(apic, APIC_LVTT + 0x10 * i, APIC_LVT_MASKED);
  1215. apic->lapic_timer.timer_mode = 0;
  1216. apic_set_reg(apic, APIC_LVT0,
  1217. SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
  1218. apic_set_reg(apic, APIC_DFR, 0xffffffffU);
  1219. apic_set_spiv(apic, 0xff);
  1220. apic_set_reg(apic, APIC_TASKPRI, 0);
  1221. kvm_apic_set_ldr(apic, 0);
  1222. apic_set_reg(apic, APIC_ESR, 0);
  1223. apic_set_reg(apic, APIC_ICR, 0);
  1224. apic_set_reg(apic, APIC_ICR2, 0);
  1225. apic_set_reg(apic, APIC_TDCR, 0);
  1226. apic_set_reg(apic, APIC_TMICT, 0);
  1227. for (i = 0; i < 8; i++) {
  1228. apic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
  1229. apic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
  1230. apic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
  1231. }
  1232. apic->irr_pending = kvm_apic_vid_enabled(vcpu->kvm);
  1233. apic->isr_count = kvm_apic_vid_enabled(vcpu->kvm);
  1234. apic->highest_isr_cache = -1;
  1235. update_divide_count(apic);
  1236. atomic_set(&apic->lapic_timer.pending, 0);
  1237. if (kvm_vcpu_is_bsp(vcpu))
  1238. kvm_lapic_set_base(vcpu,
  1239. vcpu->arch.apic_base | MSR_IA32_APICBASE_BSP);
  1240. vcpu->arch.pv_eoi.msr_val = 0;
  1241. apic_update_ppr(apic);
  1242. vcpu->arch.apic_arb_prio = 0;
  1243. vcpu->arch.apic_attention = 0;
  1244. apic_debug("%s: vcpu=%p, id=%d, base_msr="
  1245. "0x%016" PRIx64 ", base_address=0x%0lx.\n", __func__,
  1246. vcpu, kvm_apic_id(apic),
  1247. vcpu->arch.apic_base, apic->base_address);
  1248. }
  1249. /*
  1250. *----------------------------------------------------------------------
  1251. * timer interface
  1252. *----------------------------------------------------------------------
  1253. */
  1254. static bool lapic_is_periodic(struct kvm_lapic *apic)
  1255. {
  1256. return apic_lvtt_period(apic);
  1257. }
  1258. int apic_has_pending_timer(struct kvm_vcpu *vcpu)
  1259. {
  1260. struct kvm_lapic *apic = vcpu->arch.apic;
  1261. if (kvm_vcpu_has_lapic(vcpu) && apic_enabled(apic) &&
  1262. apic_lvt_enabled(apic, APIC_LVTT))
  1263. return atomic_read(&apic->lapic_timer.pending);
  1264. return 0;
  1265. }
  1266. int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type)
  1267. {
  1268. u32 reg = kvm_apic_get_reg(apic, lvt_type);
  1269. int vector, mode, trig_mode;
  1270. if (kvm_apic_hw_enabled(apic) && !(reg & APIC_LVT_MASKED)) {
  1271. vector = reg & APIC_VECTOR_MASK;
  1272. mode = reg & APIC_MODE_MASK;
  1273. trig_mode = reg & APIC_LVT_LEVEL_TRIGGER;
  1274. return __apic_accept_irq(apic, mode, vector, 1, trig_mode,
  1275. NULL);
  1276. }
  1277. return 0;
  1278. }
  1279. void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu)
  1280. {
  1281. struct kvm_lapic *apic = vcpu->arch.apic;
  1282. if (apic)
  1283. kvm_apic_local_deliver(apic, APIC_LVT0);
  1284. }
  1285. static const struct kvm_io_device_ops apic_mmio_ops = {
  1286. .read = apic_mmio_read,
  1287. .write = apic_mmio_write,
  1288. };
  1289. static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)
  1290. {
  1291. struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
  1292. struct kvm_lapic *apic = container_of(ktimer, struct kvm_lapic, lapic_timer);
  1293. apic_timer_expired(apic);
  1294. if (lapic_is_periodic(apic)) {
  1295. hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
  1296. return HRTIMER_RESTART;
  1297. } else
  1298. return HRTIMER_NORESTART;
  1299. }
  1300. int kvm_create_lapic(struct kvm_vcpu *vcpu)
  1301. {
  1302. struct kvm_lapic *apic;
  1303. ASSERT(vcpu != NULL);
  1304. apic_debug("apic_init %d\n", vcpu->vcpu_id);
  1305. apic = kzalloc(sizeof(*apic), GFP_KERNEL);
  1306. if (!apic)
  1307. goto nomem;
  1308. vcpu->arch.apic = apic;
  1309. apic->regs = (void *)get_zeroed_page(GFP_KERNEL);
  1310. if (!apic->regs) {
  1311. printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
  1312. vcpu->vcpu_id);
  1313. goto nomem_free_apic;
  1314. }
  1315. apic->vcpu = vcpu;
  1316. hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
  1317. HRTIMER_MODE_ABS);
  1318. apic->lapic_timer.timer.function = apic_timer_fn;
  1319. /*
  1320. * APIC is created enabled. This will prevent kvm_lapic_set_base from
  1321. * thinking that APIC satet has changed.
  1322. */
  1323. vcpu->arch.apic_base = MSR_IA32_APICBASE_ENABLE;
  1324. kvm_lapic_set_base(vcpu,
  1325. APIC_DEFAULT_PHYS_BASE | MSR_IA32_APICBASE_ENABLE);
  1326. static_key_slow_inc(&apic_sw_disabled.key); /* sw disabled at reset */
  1327. kvm_lapic_reset(vcpu);
  1328. kvm_iodevice_init(&apic->dev, &apic_mmio_ops);
  1329. return 0;
  1330. nomem_free_apic:
  1331. kfree(apic);
  1332. nomem:
  1333. return -ENOMEM;
  1334. }
  1335. int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
  1336. {
  1337. struct kvm_lapic *apic = vcpu->arch.apic;
  1338. int highest_irr;
  1339. if (!kvm_vcpu_has_lapic(vcpu) || !apic_enabled(apic))
  1340. return -1;
  1341. apic_update_ppr(apic);
  1342. highest_irr = apic_find_highest_irr(apic);
  1343. if ((highest_irr == -1) ||
  1344. ((highest_irr & 0xF0) <= kvm_apic_get_reg(apic, APIC_PROCPRI)))
  1345. return -1;
  1346. return highest_irr;
  1347. }
  1348. int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
  1349. {
  1350. u32 lvt0 = kvm_apic_get_reg(vcpu->arch.apic, APIC_LVT0);
  1351. int r = 0;
  1352. if (!kvm_apic_hw_enabled(vcpu->arch.apic))
  1353. r = 1;
  1354. if ((lvt0 & APIC_LVT_MASKED) == 0 &&
  1355. GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
  1356. r = 1;
  1357. return r;
  1358. }
  1359. void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
  1360. {
  1361. struct kvm_lapic *apic = vcpu->arch.apic;
  1362. if (!kvm_vcpu_has_lapic(vcpu))
  1363. return;
  1364. if (atomic_read(&apic->lapic_timer.pending) > 0) {
  1365. kvm_apic_local_deliver(apic, APIC_LVTT);
  1366. if (apic_lvtt_tscdeadline(apic))
  1367. apic->lapic_timer.tscdeadline = 0;
  1368. atomic_set(&apic->lapic_timer.pending, 0);
  1369. }
  1370. }
  1371. int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
  1372. {
  1373. int vector = kvm_apic_has_interrupt(vcpu);
  1374. struct kvm_lapic *apic = vcpu->arch.apic;
  1375. if (vector == -1)
  1376. return -1;
  1377. /*
  1378. * We get here even with APIC virtualization enabled, if doing
  1379. * nested virtualization and L1 runs with the "acknowledge interrupt
  1380. * on exit" mode. Then we cannot inject the interrupt via RVI,
  1381. * because the process would deliver it through the IDT.
  1382. */
  1383. apic_set_isr(vector, apic);
  1384. apic_update_ppr(apic);
  1385. apic_clear_irr(vector, apic);
  1386. return vector;
  1387. }
  1388. void kvm_apic_post_state_restore(struct kvm_vcpu *vcpu,
  1389. struct kvm_lapic_state *s)
  1390. {
  1391. struct kvm_lapic *apic = vcpu->arch.apic;
  1392. kvm_lapic_set_base(vcpu, vcpu->arch.apic_base);
  1393. /* set SPIV separately to get count of SW disabled APICs right */
  1394. apic_set_spiv(apic, *((u32 *)(s->regs + APIC_SPIV)));
  1395. memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
  1396. /* call kvm_apic_set_id() to put apic into apic_map */
  1397. kvm_apic_set_id(apic, kvm_apic_id(apic));
  1398. kvm_apic_set_version(vcpu);
  1399. apic_update_ppr(apic);
  1400. hrtimer_cancel(&apic->lapic_timer.timer);
  1401. update_divide_count(apic);
  1402. start_apic_timer(apic);
  1403. apic->irr_pending = true;
  1404. apic->isr_count = kvm_apic_vid_enabled(vcpu->kvm) ?
  1405. 1 : count_vectors(apic->regs + APIC_ISR);
  1406. apic->highest_isr_cache = -1;
  1407. if (kvm_x86_ops->hwapic_irr_update)
  1408. kvm_x86_ops->hwapic_irr_update(vcpu,
  1409. apic_find_highest_irr(apic));
  1410. kvm_x86_ops->hwapic_isr_update(vcpu->kvm, apic_find_highest_isr(apic));
  1411. kvm_make_request(KVM_REQ_EVENT, vcpu);
  1412. kvm_rtc_eoi_tracking_restore_one(vcpu);
  1413. }
  1414. void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
  1415. {
  1416. struct hrtimer *timer;
  1417. if (!kvm_vcpu_has_lapic(vcpu))
  1418. return;
  1419. timer = &vcpu->arch.apic->lapic_timer.timer;
  1420. if (hrtimer_cancel(timer))
  1421. hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
  1422. }
  1423. /*
  1424. * apic_sync_pv_eoi_from_guest - called on vmexit or cancel interrupt
  1425. *
  1426. * Detect whether guest triggered PV EOI since the
  1427. * last entry. If yes, set EOI on guests's behalf.
  1428. * Clear PV EOI in guest memory in any case.
  1429. */
  1430. static void apic_sync_pv_eoi_from_guest(struct kvm_vcpu *vcpu,
  1431. struct kvm_lapic *apic)
  1432. {
  1433. bool pending;
  1434. int vector;
  1435. /*
  1436. * PV EOI state is derived from KVM_APIC_PV_EOI_PENDING in host
  1437. * and KVM_PV_EOI_ENABLED in guest memory as follows:
  1438. *
  1439. * KVM_APIC_PV_EOI_PENDING is unset:
  1440. * -> host disabled PV EOI.
  1441. * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is set:
  1442. * -> host enabled PV EOI, guest did not execute EOI yet.
  1443. * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is unset:
  1444. * -> host enabled PV EOI, guest executed EOI.
  1445. */
  1446. BUG_ON(!pv_eoi_enabled(vcpu));
  1447. pending = pv_eoi_get_pending(vcpu);
  1448. /*
  1449. * Clear pending bit in any case: it will be set again on vmentry.
  1450. * While this might not be ideal from performance point of view,
  1451. * this makes sure pv eoi is only enabled when we know it's safe.
  1452. */
  1453. pv_eoi_clr_pending(vcpu);
  1454. if (pending)
  1455. return;
  1456. vector = apic_set_eoi(apic);
  1457. trace_kvm_pv_eoi(apic, vector);
  1458. }
  1459. void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu)
  1460. {
  1461. u32 data;
  1462. if (test_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention))
  1463. apic_sync_pv_eoi_from_guest(vcpu, vcpu->arch.apic);
  1464. if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
  1465. return;
  1466. kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
  1467. sizeof(u32));
  1468. apic_set_tpr(vcpu->arch.apic, data & 0xff);
  1469. }
  1470. /*
  1471. * apic_sync_pv_eoi_to_guest - called before vmentry
  1472. *
  1473. * Detect whether it's safe to enable PV EOI and
  1474. * if yes do so.
  1475. */
  1476. static void apic_sync_pv_eoi_to_guest(struct kvm_vcpu *vcpu,
  1477. struct kvm_lapic *apic)
  1478. {
  1479. if (!pv_eoi_enabled(vcpu) ||
  1480. /* IRR set or many bits in ISR: could be nested. */
  1481. apic->irr_pending ||
  1482. /* Cache not set: could be safe but we don't bother. */
  1483. apic->highest_isr_cache == -1 ||
  1484. /* Need EOI to update ioapic. */
  1485. kvm_ioapic_handles_vector(vcpu->kvm, apic->highest_isr_cache)) {
  1486. /*
  1487. * PV EOI was disabled by apic_sync_pv_eoi_from_guest
  1488. * so we need not do anything here.
  1489. */
  1490. return;
  1491. }
  1492. pv_eoi_set_pending(apic->vcpu);
  1493. }
  1494. void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu)
  1495. {
  1496. u32 data, tpr;
  1497. int max_irr, max_isr;
  1498. struct kvm_lapic *apic = vcpu->arch.apic;
  1499. apic_sync_pv_eoi_to_guest(vcpu, apic);
  1500. if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
  1501. return;
  1502. tpr = kvm_apic_get_reg(apic, APIC_TASKPRI) & 0xff;
  1503. max_irr = apic_find_highest_irr(apic);
  1504. if (max_irr < 0)
  1505. max_irr = 0;
  1506. max_isr = apic_find_highest_isr(apic);
  1507. if (max_isr < 0)
  1508. max_isr = 0;
  1509. data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24);
  1510. kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
  1511. sizeof(u32));
  1512. }
  1513. int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr)
  1514. {
  1515. if (vapic_addr) {
  1516. if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
  1517. &vcpu->arch.apic->vapic_cache,
  1518. vapic_addr, sizeof(u32)))
  1519. return -EINVAL;
  1520. __set_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
  1521. } else {
  1522. __clear_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
  1523. }
  1524. vcpu->arch.apic->vapic_addr = vapic_addr;
  1525. return 0;
  1526. }
  1527. int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  1528. {
  1529. struct kvm_lapic *apic = vcpu->arch.apic;
  1530. u32 reg = (msr - APIC_BASE_MSR) << 4;
  1531. if (!irqchip_in_kernel(vcpu->kvm) || !apic_x2apic_mode(apic))
  1532. return 1;
  1533. /* if this is ICR write vector before command */
  1534. if (msr == 0x830)
  1535. apic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
  1536. return apic_reg_write(apic, reg, (u32)data);
  1537. }
  1538. int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
  1539. {
  1540. struct kvm_lapic *apic = vcpu->arch.apic;
  1541. u32 reg = (msr - APIC_BASE_MSR) << 4, low, high = 0;
  1542. if (!irqchip_in_kernel(vcpu->kvm) || !apic_x2apic_mode(apic))
  1543. return 1;
  1544. if (apic_reg_read(apic, reg, 4, &low))
  1545. return 1;
  1546. if (msr == 0x830)
  1547. apic_reg_read(apic, APIC_ICR2, 4, &high);
  1548. *data = (((u64)high) << 32) | low;
  1549. return 0;
  1550. }
  1551. int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 reg, u64 data)
  1552. {
  1553. struct kvm_lapic *apic = vcpu->arch.apic;
  1554. if (!kvm_vcpu_has_lapic(vcpu))
  1555. return 1;
  1556. /* if this is ICR write vector before command */
  1557. if (reg == APIC_ICR)
  1558. apic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
  1559. return apic_reg_write(apic, reg, (u32)data);
  1560. }
  1561. int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 reg, u64 *data)
  1562. {
  1563. struct kvm_lapic *apic = vcpu->arch.apic;
  1564. u32 low, high = 0;
  1565. if (!kvm_vcpu_has_lapic(vcpu))
  1566. return 1;
  1567. if (apic_reg_read(apic, reg, 4, &low))
  1568. return 1;
  1569. if (reg == APIC_ICR)
  1570. apic_reg_read(apic, APIC_ICR2, 4, &high);
  1571. *data = (((u64)high) << 32) | low;
  1572. return 0;
  1573. }
  1574. int kvm_lapic_enable_pv_eoi(struct kvm_vcpu *vcpu, u64 data)
  1575. {
  1576. u64 addr = data & ~KVM_MSR_ENABLED;
  1577. if (!IS_ALIGNED(addr, 4))
  1578. return 1;
  1579. vcpu->arch.pv_eoi.msr_val = data;
  1580. if (!pv_eoi_enabled(vcpu))
  1581. return 0;
  1582. return kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.pv_eoi.data,
  1583. addr, sizeof(u8));
  1584. }
  1585. void kvm_apic_accept_events(struct kvm_vcpu *vcpu)
  1586. {
  1587. struct kvm_lapic *apic = vcpu->arch.apic;
  1588. unsigned int sipi_vector;
  1589. unsigned long pe;
  1590. if (!kvm_vcpu_has_lapic(vcpu) || !apic->pending_events)
  1591. return;
  1592. pe = xchg(&apic->pending_events, 0);
  1593. if (test_bit(KVM_APIC_INIT, &pe)) {
  1594. kvm_lapic_reset(vcpu);
  1595. kvm_vcpu_reset(vcpu);
  1596. if (kvm_vcpu_is_bsp(apic->vcpu))
  1597. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  1598. else
  1599. vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
  1600. }
  1601. if (test_bit(KVM_APIC_SIPI, &pe) &&
  1602. vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
  1603. /* evaluate pending_events before reading the vector */
  1604. smp_rmb();
  1605. sipi_vector = apic->sipi_vector;
  1606. apic_debug("vcpu %d received sipi with vector # %x\n",
  1607. vcpu->vcpu_id, sipi_vector);
  1608. kvm_vcpu_deliver_sipi_vector(vcpu, sipi_vector);
  1609. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  1610. }
  1611. }
  1612. void kvm_lapic_init(void)
  1613. {
  1614. /* do not patch jump label more than once per second */
  1615. jump_label_rate_limit(&apic_hw_disabled, HZ);
  1616. jump_label_rate_limit(&apic_sw_disabled, HZ);
  1617. }