lapic.c 54 KB

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