lapic.c 44 KB

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