lapic.c 66 KB

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