lapic.c 59 KB

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