lapic.c 39 KB

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