lapic.c 65 KB

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