lapic.c 47 KB

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