lapic.c 46 KB

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