lapic.c 41 KB

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