lapic.c 68 KB

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