lapic.c 46 KB

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