lapic.c 48 KB

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