lapic.c 50 KB

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