lapic.c 65 KB

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