lapic.c 68 KB

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