lapic.c 47 KB

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