lapic.c 63 KB

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