lapic.c 60 KB

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