lapic.c 47 KB

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