lapic.c 62 KB

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