lapic.c 51 KB

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