lapic.c 68 KB

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