lapic.c 49 KB

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