lapic.c 60 KB

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