lapic.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910
  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/module.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 <linux/atomic.h>
  35. #include <linux/jump_label.h>
  36. #include "kvm_cache_regs.h"
  37. #include "irq.h"
  38. #include "trace.h"
  39. #include "x86.h"
  40. #include "cpuid.h"
  41. #ifndef CONFIG_X86_64
  42. #define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
  43. #else
  44. #define mod_64(x, y) ((x) % (y))
  45. #endif
  46. #define PRId64 "d"
  47. #define PRIx64 "llx"
  48. #define PRIu64 "u"
  49. #define PRIo64 "o"
  50. #define APIC_BUS_CYCLE_NS 1
  51. /* #define apic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg) */
  52. #define apic_debug(fmt, arg...)
  53. #define APIC_LVT_NUM 6
  54. /* 14 is the version for Xeon and Pentium 8.4.8*/
  55. #define APIC_VERSION (0x14UL | ((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 VEC_POS(v) ((v) & (32 - 1))
  64. #define REG_POS(v) (((v) >> 5) << 4)
  65. static inline void apic_set_reg(struct kvm_lapic *apic, int reg_off, u32 val)
  66. {
  67. *((u32 *) (apic->regs + reg_off)) = val;
  68. }
  69. static inline int apic_test_vector(int vec, void *bitmap)
  70. {
  71. return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  72. }
  73. bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
  74. {
  75. struct kvm_lapic *apic = vcpu->arch.apic;
  76. return apic_test_vector(vector, apic->regs + APIC_ISR) ||
  77. apic_test_vector(vector, apic->regs + APIC_IRR);
  78. }
  79. static inline void apic_set_vector(int vec, void *bitmap)
  80. {
  81. set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  82. }
  83. static inline void apic_clear_vector(int vec, void *bitmap)
  84. {
  85. clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  86. }
  87. static inline int __apic_test_and_set_vector(int vec, void *bitmap)
  88. {
  89. return __test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  90. }
  91. static inline int __apic_test_and_clear_vector(int vec, void *bitmap)
  92. {
  93. return __test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  94. }
  95. struct static_key_deferred apic_hw_disabled __read_mostly;
  96. struct static_key_deferred apic_sw_disabled __read_mostly;
  97. static inline void apic_set_spiv(struct kvm_lapic *apic, u32 val)
  98. {
  99. if ((kvm_apic_get_reg(apic, APIC_SPIV) ^ val) & APIC_SPIV_APIC_ENABLED) {
  100. if (val & APIC_SPIV_APIC_ENABLED)
  101. static_key_slow_dec_deferred(&apic_sw_disabled);
  102. else
  103. static_key_slow_inc(&apic_sw_disabled.key);
  104. }
  105. apic_set_reg(apic, APIC_SPIV, val);
  106. }
  107. static inline int apic_enabled(struct kvm_lapic *apic)
  108. {
  109. return kvm_apic_sw_enabled(apic) && kvm_apic_hw_enabled(apic);
  110. }
  111. #define LVT_MASK \
  112. (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
  113. #define LINT_MASK \
  114. (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
  115. APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
  116. static inline int kvm_apic_id(struct kvm_lapic *apic)
  117. {
  118. return (kvm_apic_get_reg(apic, APIC_ID) >> 24) & 0xff;
  119. }
  120. #define KVM_X2APIC_CID_BITS 0
  121. static void recalculate_apic_map(struct kvm *kvm)
  122. {
  123. struct kvm_apic_map *new, *old = NULL;
  124. struct kvm_vcpu *vcpu;
  125. int i;
  126. new = kzalloc(sizeof(struct kvm_apic_map), GFP_KERNEL);
  127. mutex_lock(&kvm->arch.apic_map_lock);
  128. if (!new)
  129. goto out;
  130. new->ldr_bits = 8;
  131. /* flat mode is default */
  132. new->cid_shift = 8;
  133. new->cid_mask = 0;
  134. new->lid_mask = 0xff;
  135. kvm_for_each_vcpu(i, vcpu, kvm) {
  136. struct kvm_lapic *apic = vcpu->arch.apic;
  137. u16 cid, lid;
  138. u32 ldr;
  139. if (!kvm_apic_present(vcpu))
  140. continue;
  141. /*
  142. * All APICs have to be configured in the same mode by an OS.
  143. * We take advatage of this while building logical id loockup
  144. * table. After reset APICs are in xapic/flat mode, so if we
  145. * find apic with different setting we assume this is the mode
  146. * OS wants all apics to be in; build lookup table accordingly.
  147. */
  148. if (apic_x2apic_mode(apic)) {
  149. new->ldr_bits = 32;
  150. new->cid_shift = 16;
  151. new->cid_mask = (1 << KVM_X2APIC_CID_BITS) - 1;
  152. new->lid_mask = 0xffff;
  153. } else if (kvm_apic_sw_enabled(apic) &&
  154. !new->cid_mask /* flat mode */ &&
  155. kvm_apic_get_reg(apic, APIC_DFR) == APIC_DFR_CLUSTER) {
  156. new->cid_shift = 4;
  157. new->cid_mask = 0xf;
  158. new->lid_mask = 0xf;
  159. }
  160. new->phys_map[kvm_apic_id(apic)] = apic;
  161. ldr = kvm_apic_get_reg(apic, APIC_LDR);
  162. cid = apic_cluster_id(new, ldr);
  163. lid = apic_logical_id(new, ldr);
  164. if (lid)
  165. new->logical_map[cid][ffs(lid) - 1] = apic;
  166. }
  167. out:
  168. old = rcu_dereference_protected(kvm->arch.apic_map,
  169. lockdep_is_held(&kvm->arch.apic_map_lock));
  170. rcu_assign_pointer(kvm->arch.apic_map, new);
  171. mutex_unlock(&kvm->arch.apic_map_lock);
  172. if (old)
  173. kfree_rcu(old, rcu);
  174. kvm_vcpu_request_scan_ioapic(kvm);
  175. }
  176. static inline void kvm_apic_set_id(struct kvm_lapic *apic, u8 id)
  177. {
  178. apic_set_reg(apic, APIC_ID, id << 24);
  179. recalculate_apic_map(apic->vcpu->kvm);
  180. }
  181. static inline void kvm_apic_set_ldr(struct kvm_lapic *apic, u32 id)
  182. {
  183. apic_set_reg(apic, APIC_LDR, id);
  184. recalculate_apic_map(apic->vcpu->kvm);
  185. }
  186. static inline int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type)
  187. {
  188. return !(kvm_apic_get_reg(apic, lvt_type) & APIC_LVT_MASKED);
  189. }
  190. static inline int apic_lvt_vector(struct kvm_lapic *apic, int lvt_type)
  191. {
  192. return kvm_apic_get_reg(apic, lvt_type) & APIC_VECTOR_MASK;
  193. }
  194. static inline int apic_lvtt_oneshot(struct kvm_lapic *apic)
  195. {
  196. return ((kvm_apic_get_reg(apic, APIC_LVTT) &
  197. apic->lapic_timer.timer_mode_mask) == APIC_LVT_TIMER_ONESHOT);
  198. }
  199. static inline int apic_lvtt_period(struct kvm_lapic *apic)
  200. {
  201. return ((kvm_apic_get_reg(apic, APIC_LVTT) &
  202. apic->lapic_timer.timer_mode_mask) == APIC_LVT_TIMER_PERIODIC);
  203. }
  204. static inline int apic_lvtt_tscdeadline(struct kvm_lapic *apic)
  205. {
  206. return ((kvm_apic_get_reg(apic, APIC_LVTT) &
  207. apic->lapic_timer.timer_mode_mask) ==
  208. APIC_LVT_TIMER_TSCDEADLINE);
  209. }
  210. static inline int apic_lvt_nmi_mode(u32 lvt_val)
  211. {
  212. return (lvt_val & (APIC_MODE_MASK | APIC_LVT_MASKED)) == APIC_DM_NMI;
  213. }
  214. void kvm_apic_set_version(struct kvm_vcpu *vcpu)
  215. {
  216. struct kvm_lapic *apic = vcpu->arch.apic;
  217. struct kvm_cpuid_entry2 *feat;
  218. u32 v = APIC_VERSION;
  219. if (!kvm_vcpu_has_lapic(vcpu))
  220. return;
  221. feat = kvm_find_cpuid_entry(apic->vcpu, 0x1, 0);
  222. if (feat && (feat->ecx & (1 << (X86_FEATURE_X2APIC & 31))))
  223. v |= APIC_LVR_DIRECTED_EOI;
  224. apic_set_reg(apic, APIC_LVR, v);
  225. }
  226. static const unsigned int apic_lvt_mask[APIC_LVT_NUM] = {
  227. LVT_MASK , /* part LVTT mask, timer mode mask added at runtime */
  228. LVT_MASK | APIC_MODE_MASK, /* LVTTHMR */
  229. LVT_MASK | APIC_MODE_MASK, /* LVTPC */
  230. LINT_MASK, LINT_MASK, /* LVT0-1 */
  231. LVT_MASK /* LVTERR */
  232. };
  233. static int find_highest_vector(void *bitmap)
  234. {
  235. int vec;
  236. u32 *reg;
  237. for (vec = MAX_APIC_VECTOR - APIC_VECTORS_PER_REG;
  238. vec >= 0; vec -= APIC_VECTORS_PER_REG) {
  239. reg = bitmap + REG_POS(vec);
  240. if (*reg)
  241. return fls(*reg) - 1 + vec;
  242. }
  243. return -1;
  244. }
  245. static u8 count_vectors(void *bitmap)
  246. {
  247. int vec;
  248. u32 *reg;
  249. u8 count = 0;
  250. for (vec = 0; vec < MAX_APIC_VECTOR; vec += APIC_VECTORS_PER_REG) {
  251. reg = bitmap + REG_POS(vec);
  252. count += hweight32(*reg);
  253. }
  254. return count;
  255. }
  256. void kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir)
  257. {
  258. u32 i, pir_val;
  259. struct kvm_lapic *apic = vcpu->arch.apic;
  260. for (i = 0; i <= 7; i++) {
  261. pir_val = xchg(&pir[i], 0);
  262. if (pir_val)
  263. *((u32 *)(apic->regs + APIC_IRR + i * 0x10)) |= pir_val;
  264. }
  265. }
  266. EXPORT_SYMBOL_GPL(kvm_apic_update_irr);
  267. static inline void apic_set_irr(int vec, struct kvm_lapic *apic)
  268. {
  269. apic->irr_pending = true;
  270. apic_set_vector(vec, apic->regs + APIC_IRR);
  271. }
  272. static inline int apic_search_irr(struct kvm_lapic *apic)
  273. {
  274. return find_highest_vector(apic->regs + APIC_IRR);
  275. }
  276. static inline int apic_find_highest_irr(struct kvm_lapic *apic)
  277. {
  278. int result;
  279. /*
  280. * Note that irr_pending is just a hint. It will be always
  281. * true with virtual interrupt delivery enabled.
  282. */
  283. if (!apic->irr_pending)
  284. return -1;
  285. kvm_x86_ops->sync_pir_to_irr(apic->vcpu);
  286. result = apic_search_irr(apic);
  287. ASSERT(result == -1 || result >= 16);
  288. return result;
  289. }
  290. static inline void apic_clear_irr(int vec, struct kvm_lapic *apic)
  291. {
  292. apic->irr_pending = false;
  293. apic_clear_vector(vec, apic->regs + APIC_IRR);
  294. if (apic_search_irr(apic) != -1)
  295. apic->irr_pending = true;
  296. }
  297. static inline void apic_set_isr(int vec, struct kvm_lapic *apic)
  298. {
  299. /* Note that we never get here with APIC virtualization enabled. */
  300. if (!__apic_test_and_set_vector(vec, apic->regs + APIC_ISR))
  301. ++apic->isr_count;
  302. BUG_ON(apic->isr_count > MAX_APIC_VECTOR);
  303. /*
  304. * ISR (in service register) bit is set when injecting an interrupt.
  305. * The highest vector is injected. Thus the latest bit set matches
  306. * the highest bit in ISR.
  307. */
  308. apic->highest_isr_cache = vec;
  309. }
  310. static inline int apic_find_highest_isr(struct kvm_lapic *apic)
  311. {
  312. int result;
  313. /*
  314. * Note that isr_count is always 1, and highest_isr_cache
  315. * is always -1, with APIC virtualization enabled.
  316. */
  317. if (!apic->isr_count)
  318. return -1;
  319. if (likely(apic->highest_isr_cache != -1))
  320. return apic->highest_isr_cache;
  321. result = find_highest_vector(apic->regs + APIC_ISR);
  322. ASSERT(result == -1 || result >= 16);
  323. return result;
  324. }
  325. static inline void apic_clear_isr(int vec, struct kvm_lapic *apic)
  326. {
  327. struct kvm_vcpu *vcpu;
  328. if (!__apic_test_and_clear_vector(vec, apic->regs + APIC_ISR))
  329. return;
  330. vcpu = apic->vcpu;
  331. /*
  332. * We do get here for APIC virtualization enabled if the guest
  333. * uses the Hyper-V APIC enlightenment. In this case we may need
  334. * to trigger a new interrupt delivery by writing the SVI field;
  335. * on the other hand isr_count and highest_isr_cache are unused
  336. * and must be left alone.
  337. */
  338. if (unlikely(kvm_apic_vid_enabled(vcpu->kvm)))
  339. kvm_x86_ops->hwapic_isr_update(vcpu->kvm,
  340. apic_find_highest_isr(apic));
  341. else {
  342. --apic->isr_count;
  343. BUG_ON(apic->isr_count < 0);
  344. apic->highest_isr_cache = -1;
  345. }
  346. }
  347. int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu)
  348. {
  349. int highest_irr;
  350. /* This may race with setting of irr in __apic_accept_irq() and
  351. * value returned may be wrong, but kvm_vcpu_kick() in __apic_accept_irq
  352. * will cause vmexit immediately and the value will be recalculated
  353. * on the next vmentry.
  354. */
  355. if (!kvm_vcpu_has_lapic(vcpu))
  356. return 0;
  357. highest_irr = apic_find_highest_irr(vcpu->arch.apic);
  358. return highest_irr;
  359. }
  360. static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
  361. int vector, int level, int trig_mode,
  362. unsigned long *dest_map);
  363. int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq,
  364. unsigned long *dest_map)
  365. {
  366. struct kvm_lapic *apic = vcpu->arch.apic;
  367. return __apic_accept_irq(apic, irq->delivery_mode, irq->vector,
  368. irq->level, irq->trig_mode, dest_map);
  369. }
  370. static int pv_eoi_put_user(struct kvm_vcpu *vcpu, u8 val)
  371. {
  372. return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, &val,
  373. sizeof(val));
  374. }
  375. static int pv_eoi_get_user(struct kvm_vcpu *vcpu, u8 *val)
  376. {
  377. return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, val,
  378. sizeof(*val));
  379. }
  380. static inline bool pv_eoi_enabled(struct kvm_vcpu *vcpu)
  381. {
  382. return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED;
  383. }
  384. static bool pv_eoi_get_pending(struct kvm_vcpu *vcpu)
  385. {
  386. u8 val;
  387. if (pv_eoi_get_user(vcpu, &val) < 0)
  388. apic_debug("Can't read EOI MSR value: 0x%llx\n",
  389. (unsigned long long)vcpu->arch.pv_eoi.msr_val);
  390. return val & 0x1;
  391. }
  392. static void pv_eoi_set_pending(struct kvm_vcpu *vcpu)
  393. {
  394. if (pv_eoi_put_user(vcpu, KVM_PV_EOI_ENABLED) < 0) {
  395. apic_debug("Can't set EOI MSR value: 0x%llx\n",
  396. (unsigned long long)vcpu->arch.pv_eoi.msr_val);
  397. return;
  398. }
  399. __set_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
  400. }
  401. static void pv_eoi_clr_pending(struct kvm_vcpu *vcpu)
  402. {
  403. if (pv_eoi_put_user(vcpu, KVM_PV_EOI_DISABLED) < 0) {
  404. apic_debug("Can't clear EOI MSR value: 0x%llx\n",
  405. (unsigned long long)vcpu->arch.pv_eoi.msr_val);
  406. return;
  407. }
  408. __clear_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
  409. }
  410. void kvm_apic_update_tmr(struct kvm_vcpu *vcpu, u32 *tmr)
  411. {
  412. struct kvm_lapic *apic = vcpu->arch.apic;
  413. int i;
  414. for (i = 0; i < 8; i++)
  415. apic_set_reg(apic, APIC_TMR + 0x10 * i, tmr[i]);
  416. }
  417. static void apic_update_ppr(struct kvm_lapic *apic)
  418. {
  419. u32 tpr, isrv, ppr, old_ppr;
  420. int isr;
  421. old_ppr = kvm_apic_get_reg(apic, APIC_PROCPRI);
  422. tpr = kvm_apic_get_reg(apic, APIC_TASKPRI);
  423. isr = apic_find_highest_isr(apic);
  424. isrv = (isr != -1) ? isr : 0;
  425. if ((tpr & 0xf0) >= (isrv & 0xf0))
  426. ppr = tpr & 0xff;
  427. else
  428. ppr = isrv & 0xf0;
  429. apic_debug("vlapic %p, ppr 0x%x, isr 0x%x, isrv 0x%x",
  430. apic, ppr, isr, isrv);
  431. if (old_ppr != ppr) {
  432. apic_set_reg(apic, APIC_PROCPRI, ppr);
  433. if (ppr < old_ppr)
  434. kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
  435. }
  436. }
  437. static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr)
  438. {
  439. apic_set_reg(apic, APIC_TASKPRI, tpr);
  440. apic_update_ppr(apic);
  441. }
  442. int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest)
  443. {
  444. return dest == 0xff || kvm_apic_id(apic) == dest;
  445. }
  446. int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda)
  447. {
  448. int result = 0;
  449. u32 logical_id;
  450. if (apic_x2apic_mode(apic)) {
  451. logical_id = kvm_apic_get_reg(apic, APIC_LDR);
  452. return logical_id & mda;
  453. }
  454. logical_id = GET_APIC_LOGICAL_ID(kvm_apic_get_reg(apic, APIC_LDR));
  455. switch (kvm_apic_get_reg(apic, APIC_DFR)) {
  456. case APIC_DFR_FLAT:
  457. if (logical_id & mda)
  458. result = 1;
  459. break;
  460. case APIC_DFR_CLUSTER:
  461. if (((logical_id >> 4) == (mda >> 0x4))
  462. && (logical_id & mda & 0xf))
  463. result = 1;
  464. break;
  465. default:
  466. apic_debug("Bad DFR vcpu %d: %08x\n",
  467. apic->vcpu->vcpu_id, kvm_apic_get_reg(apic, APIC_DFR));
  468. break;
  469. }
  470. return result;
  471. }
  472. int kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
  473. int short_hand, int dest, int dest_mode)
  474. {
  475. int result = 0;
  476. struct kvm_lapic *target = vcpu->arch.apic;
  477. apic_debug("target %p, source %p, dest 0x%x, "
  478. "dest_mode 0x%x, short_hand 0x%x\n",
  479. target, source, dest, dest_mode, short_hand);
  480. ASSERT(target);
  481. switch (short_hand) {
  482. case APIC_DEST_NOSHORT:
  483. if (dest_mode == 0)
  484. /* Physical mode. */
  485. result = kvm_apic_match_physical_addr(target, dest);
  486. else
  487. /* Logical mode. */
  488. result = kvm_apic_match_logical_addr(target, dest);
  489. break;
  490. case APIC_DEST_SELF:
  491. result = (target == source);
  492. break;
  493. case APIC_DEST_ALLINC:
  494. result = 1;
  495. break;
  496. case APIC_DEST_ALLBUT:
  497. result = (target != source);
  498. break;
  499. default:
  500. apic_debug("kvm: apic: Bad dest shorthand value %x\n",
  501. short_hand);
  502. break;
  503. }
  504. return result;
  505. }
  506. bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
  507. struct kvm_lapic_irq *irq, int *r, unsigned long *dest_map)
  508. {
  509. struct kvm_apic_map *map;
  510. unsigned long bitmap = 1;
  511. struct kvm_lapic **dst;
  512. int i;
  513. bool ret = false;
  514. *r = -1;
  515. if (irq->shorthand == APIC_DEST_SELF) {
  516. *r = kvm_apic_set_irq(src->vcpu, irq, dest_map);
  517. return true;
  518. }
  519. if (irq->shorthand)
  520. return false;
  521. rcu_read_lock();
  522. map = rcu_dereference(kvm->arch.apic_map);
  523. if (!map)
  524. goto out;
  525. if (irq->dest_mode == 0) { /* physical mode */
  526. if (irq->delivery_mode == APIC_DM_LOWEST ||
  527. irq->dest_id == 0xff)
  528. goto out;
  529. dst = &map->phys_map[irq->dest_id & 0xff];
  530. } else {
  531. u32 mda = irq->dest_id << (32 - map->ldr_bits);
  532. dst = map->logical_map[apic_cluster_id(map, mda)];
  533. bitmap = apic_logical_id(map, mda);
  534. if (irq->delivery_mode == APIC_DM_LOWEST) {
  535. int l = -1;
  536. for_each_set_bit(i, &bitmap, 16) {
  537. if (!dst[i])
  538. continue;
  539. if (l < 0)
  540. l = i;
  541. else if (kvm_apic_compare_prio(dst[i]->vcpu, dst[l]->vcpu) < 0)
  542. l = i;
  543. }
  544. bitmap = (l >= 0) ? 1 << l : 0;
  545. }
  546. }
  547. for_each_set_bit(i, &bitmap, 16) {
  548. if (!dst[i])
  549. continue;
  550. if (*r < 0)
  551. *r = 0;
  552. *r += kvm_apic_set_irq(dst[i]->vcpu, irq, dest_map);
  553. }
  554. ret = true;
  555. out:
  556. rcu_read_unlock();
  557. return ret;
  558. }
  559. /*
  560. * Add a pending IRQ into lapic.
  561. * Return 1 if successfully added and 0 if discarded.
  562. */
  563. static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
  564. int vector, int level, int trig_mode,
  565. unsigned long *dest_map)
  566. {
  567. int result = 0;
  568. struct kvm_vcpu *vcpu = apic->vcpu;
  569. switch (delivery_mode) {
  570. case APIC_DM_LOWEST:
  571. vcpu->arch.apic_arb_prio++;
  572. case APIC_DM_FIXED:
  573. /* FIXME add logic for vcpu on reset */
  574. if (unlikely(!apic_enabled(apic)))
  575. break;
  576. result = 1;
  577. if (dest_map)
  578. __set_bit(vcpu->vcpu_id, dest_map);
  579. if (kvm_x86_ops->deliver_posted_interrupt)
  580. kvm_x86_ops->deliver_posted_interrupt(vcpu, vector);
  581. else {
  582. apic_set_irr(vector, apic);
  583. kvm_make_request(KVM_REQ_EVENT, vcpu);
  584. kvm_vcpu_kick(vcpu);
  585. }
  586. trace_kvm_apic_accept_irq(vcpu->vcpu_id, delivery_mode,
  587. trig_mode, vector, false);
  588. break;
  589. case APIC_DM_REMRD:
  590. result = 1;
  591. vcpu->arch.pv.pv_unhalted = 1;
  592. kvm_make_request(KVM_REQ_EVENT, vcpu);
  593. kvm_vcpu_kick(vcpu);
  594. break;
  595. case APIC_DM_SMI:
  596. apic_debug("Ignoring guest SMI\n");
  597. break;
  598. case APIC_DM_NMI:
  599. result = 1;
  600. kvm_inject_nmi(vcpu);
  601. kvm_vcpu_kick(vcpu);
  602. break;
  603. case APIC_DM_INIT:
  604. if (!trig_mode || level) {
  605. result = 1;
  606. /* assumes that there are only KVM_APIC_INIT/SIPI */
  607. apic->pending_events = (1UL << KVM_APIC_INIT);
  608. /* make sure pending_events is visible before sending
  609. * the request */
  610. smp_wmb();
  611. kvm_make_request(KVM_REQ_EVENT, vcpu);
  612. kvm_vcpu_kick(vcpu);
  613. } else {
  614. apic_debug("Ignoring de-assert INIT to vcpu %d\n",
  615. vcpu->vcpu_id);
  616. }
  617. break;
  618. case APIC_DM_STARTUP:
  619. apic_debug("SIPI to vcpu %d vector 0x%02x\n",
  620. vcpu->vcpu_id, vector);
  621. result = 1;
  622. apic->sipi_vector = vector;
  623. /* make sure sipi_vector is visible for the receiver */
  624. smp_wmb();
  625. set_bit(KVM_APIC_SIPI, &apic->pending_events);
  626. kvm_make_request(KVM_REQ_EVENT, vcpu);
  627. kvm_vcpu_kick(vcpu);
  628. break;
  629. case APIC_DM_EXTINT:
  630. /*
  631. * Should only be called by kvm_apic_local_deliver() with LVT0,
  632. * before NMI watchdog was enabled. Already handled by
  633. * kvm_apic_accept_pic_intr().
  634. */
  635. break;
  636. default:
  637. printk(KERN_ERR "TODO: unsupported delivery mode %x\n",
  638. delivery_mode);
  639. break;
  640. }
  641. return result;
  642. }
  643. int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
  644. {
  645. return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
  646. }
  647. static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
  648. {
  649. if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI) &&
  650. kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) {
  651. int trigger_mode;
  652. if (apic_test_vector(vector, apic->regs + APIC_TMR))
  653. trigger_mode = IOAPIC_LEVEL_TRIG;
  654. else
  655. trigger_mode = IOAPIC_EDGE_TRIG;
  656. kvm_ioapic_update_eoi(apic->vcpu, vector, trigger_mode);
  657. }
  658. }
  659. static int apic_set_eoi(struct kvm_lapic *apic)
  660. {
  661. int vector = apic_find_highest_isr(apic);
  662. trace_kvm_eoi(apic, vector);
  663. /*
  664. * Not every write EOI will has corresponding ISR,
  665. * one example is when Kernel check timer on setup_IO_APIC
  666. */
  667. if (vector == -1)
  668. return vector;
  669. apic_clear_isr(vector, apic);
  670. apic_update_ppr(apic);
  671. kvm_ioapic_send_eoi(apic, vector);
  672. kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
  673. return vector;
  674. }
  675. /*
  676. * this interface assumes a trap-like exit, which has already finished
  677. * desired side effect including vISR and vPPR update.
  678. */
  679. void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
  680. {
  681. struct kvm_lapic *apic = vcpu->arch.apic;
  682. trace_kvm_eoi(apic, vector);
  683. kvm_ioapic_send_eoi(apic, vector);
  684. kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
  685. }
  686. EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
  687. static void apic_send_ipi(struct kvm_lapic *apic)
  688. {
  689. u32 icr_low = kvm_apic_get_reg(apic, APIC_ICR);
  690. u32 icr_high = kvm_apic_get_reg(apic, APIC_ICR2);
  691. struct kvm_lapic_irq irq;
  692. irq.vector = icr_low & APIC_VECTOR_MASK;
  693. irq.delivery_mode = icr_low & APIC_MODE_MASK;
  694. irq.dest_mode = icr_low & APIC_DEST_MASK;
  695. irq.level = icr_low & APIC_INT_ASSERT;
  696. irq.trig_mode = icr_low & APIC_INT_LEVELTRIG;
  697. irq.shorthand = icr_low & APIC_SHORT_MASK;
  698. if (apic_x2apic_mode(apic))
  699. irq.dest_id = icr_high;
  700. else
  701. irq.dest_id = GET_APIC_DEST_FIELD(icr_high);
  702. trace_kvm_apic_ipi(icr_low, irq.dest_id);
  703. apic_debug("icr_high 0x%x, icr_low 0x%x, "
  704. "short_hand 0x%x, dest 0x%x, trig_mode 0x%x, level 0x%x, "
  705. "dest_mode 0x%x, delivery_mode 0x%x, vector 0x%x\n",
  706. icr_high, icr_low, irq.shorthand, irq.dest_id,
  707. irq.trig_mode, irq.level, irq.dest_mode, irq.delivery_mode,
  708. irq.vector);
  709. kvm_irq_delivery_to_apic(apic->vcpu->kvm, apic, &irq, NULL);
  710. }
  711. static u32 apic_get_tmcct(struct kvm_lapic *apic)
  712. {
  713. ktime_t remaining;
  714. s64 ns;
  715. u32 tmcct;
  716. ASSERT(apic != NULL);
  717. /* if initial count is 0, current count should also be 0 */
  718. if (kvm_apic_get_reg(apic, APIC_TMICT) == 0 ||
  719. apic->lapic_timer.period == 0)
  720. return 0;
  721. remaining = hrtimer_get_remaining(&apic->lapic_timer.timer);
  722. if (ktime_to_ns(remaining) < 0)
  723. remaining = ktime_set(0, 0);
  724. ns = mod_64(ktime_to_ns(remaining), apic->lapic_timer.period);
  725. tmcct = div64_u64(ns,
  726. (APIC_BUS_CYCLE_NS * apic->divide_count));
  727. return tmcct;
  728. }
  729. static void __report_tpr_access(struct kvm_lapic *apic, bool write)
  730. {
  731. struct kvm_vcpu *vcpu = apic->vcpu;
  732. struct kvm_run *run = vcpu->run;
  733. kvm_make_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu);
  734. run->tpr_access.rip = kvm_rip_read(vcpu);
  735. run->tpr_access.is_write = write;
  736. }
  737. static inline void report_tpr_access(struct kvm_lapic *apic, bool write)
  738. {
  739. if (apic->vcpu->arch.tpr_access_reporting)
  740. __report_tpr_access(apic, write);
  741. }
  742. static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset)
  743. {
  744. u32 val = 0;
  745. if (offset >= LAPIC_MMIO_LENGTH)
  746. return 0;
  747. switch (offset) {
  748. case APIC_ID:
  749. if (apic_x2apic_mode(apic))
  750. val = kvm_apic_id(apic);
  751. else
  752. val = kvm_apic_id(apic) << 24;
  753. break;
  754. case APIC_ARBPRI:
  755. apic_debug("Access APIC ARBPRI register which is for P6\n");
  756. break;
  757. case APIC_TMCCT: /* Timer CCR */
  758. if (apic_lvtt_tscdeadline(apic))
  759. return 0;
  760. val = apic_get_tmcct(apic);
  761. break;
  762. case APIC_PROCPRI:
  763. apic_update_ppr(apic);
  764. val = kvm_apic_get_reg(apic, offset);
  765. break;
  766. case APIC_TASKPRI:
  767. report_tpr_access(apic, false);
  768. /* fall thru */
  769. default:
  770. val = kvm_apic_get_reg(apic, offset);
  771. break;
  772. }
  773. return val;
  774. }
  775. static inline struct kvm_lapic *to_lapic(struct kvm_io_device *dev)
  776. {
  777. return container_of(dev, struct kvm_lapic, dev);
  778. }
  779. static int apic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
  780. void *data)
  781. {
  782. unsigned char alignment = offset & 0xf;
  783. u32 result;
  784. /* this bitmask has a bit cleared for each reserved register */
  785. static const u64 rmask = 0x43ff01ffffffe70cULL;
  786. if ((alignment + len) > 4) {
  787. apic_debug("KVM_APIC_READ: alignment error %x %d\n",
  788. offset, len);
  789. return 1;
  790. }
  791. if (offset > 0x3f0 || !(rmask & (1ULL << (offset >> 4)))) {
  792. apic_debug("KVM_APIC_READ: read reserved register %x\n",
  793. offset);
  794. return 1;
  795. }
  796. result = __apic_read(apic, offset & ~0xf);
  797. trace_kvm_apic_read(offset, result);
  798. switch (len) {
  799. case 1:
  800. case 2:
  801. case 4:
  802. memcpy(data, (char *)&result + alignment, len);
  803. break;
  804. default:
  805. printk(KERN_ERR "Local APIC read with len = %x, "
  806. "should be 1,2, or 4 instead\n", len);
  807. break;
  808. }
  809. return 0;
  810. }
  811. static int apic_mmio_in_range(struct kvm_lapic *apic, gpa_t addr)
  812. {
  813. return kvm_apic_hw_enabled(apic) &&
  814. addr >= apic->base_address &&
  815. addr < apic->base_address + LAPIC_MMIO_LENGTH;
  816. }
  817. static int apic_mmio_read(struct kvm_io_device *this,
  818. gpa_t address, int len, void *data)
  819. {
  820. struct kvm_lapic *apic = to_lapic(this);
  821. u32 offset = address - apic->base_address;
  822. if (!apic_mmio_in_range(apic, address))
  823. return -EOPNOTSUPP;
  824. apic_reg_read(apic, offset, len, data);
  825. return 0;
  826. }
  827. static void update_divide_count(struct kvm_lapic *apic)
  828. {
  829. u32 tmp1, tmp2, tdcr;
  830. tdcr = kvm_apic_get_reg(apic, APIC_TDCR);
  831. tmp1 = tdcr & 0xf;
  832. tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
  833. apic->divide_count = 0x1 << (tmp2 & 0x7);
  834. apic_debug("timer divide count is 0x%x\n",
  835. apic->divide_count);
  836. }
  837. static void start_apic_timer(struct kvm_lapic *apic)
  838. {
  839. ktime_t now;
  840. atomic_set(&apic->lapic_timer.pending, 0);
  841. if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic)) {
  842. /* lapic timer in oneshot or periodic mode */
  843. now = apic->lapic_timer.timer.base->get_time();
  844. apic->lapic_timer.period = (u64)kvm_apic_get_reg(apic, APIC_TMICT)
  845. * APIC_BUS_CYCLE_NS * apic->divide_count;
  846. if (!apic->lapic_timer.period)
  847. return;
  848. /*
  849. * Do not allow the guest to program periodic timers with small
  850. * interval, since the hrtimers are not throttled by the host
  851. * scheduler.
  852. */
  853. if (apic_lvtt_period(apic)) {
  854. s64 min_period = min_timer_period_us * 1000LL;
  855. if (apic->lapic_timer.period < min_period) {
  856. pr_info_ratelimited(
  857. "kvm: vcpu %i: requested %lld ns "
  858. "lapic timer period limited to %lld ns\n",
  859. apic->vcpu->vcpu_id,
  860. apic->lapic_timer.period, min_period);
  861. apic->lapic_timer.period = min_period;
  862. }
  863. }
  864. hrtimer_start(&apic->lapic_timer.timer,
  865. ktime_add_ns(now, apic->lapic_timer.period),
  866. HRTIMER_MODE_ABS);
  867. apic_debug("%s: bus cycle is %" PRId64 "ns, now 0x%016"
  868. PRIx64 ", "
  869. "timer initial count 0x%x, period %lldns, "
  870. "expire @ 0x%016" PRIx64 ".\n", __func__,
  871. APIC_BUS_CYCLE_NS, ktime_to_ns(now),
  872. kvm_apic_get_reg(apic, APIC_TMICT),
  873. apic->lapic_timer.period,
  874. ktime_to_ns(ktime_add_ns(now,
  875. apic->lapic_timer.period)));
  876. } else if (apic_lvtt_tscdeadline(apic)) {
  877. /* lapic timer in tsc deadline mode */
  878. u64 guest_tsc, tscdeadline = apic->lapic_timer.tscdeadline;
  879. u64 ns = 0;
  880. struct kvm_vcpu *vcpu = apic->vcpu;
  881. unsigned long this_tsc_khz = vcpu->arch.virtual_tsc_khz;
  882. unsigned long flags;
  883. if (unlikely(!tscdeadline || !this_tsc_khz))
  884. return;
  885. local_irq_save(flags);
  886. now = apic->lapic_timer.timer.base->get_time();
  887. guest_tsc = kvm_x86_ops->read_l1_tsc(vcpu, native_read_tsc());
  888. if (likely(tscdeadline > guest_tsc)) {
  889. ns = (tscdeadline - guest_tsc) * 1000000ULL;
  890. do_div(ns, this_tsc_khz);
  891. }
  892. hrtimer_start(&apic->lapic_timer.timer,
  893. ktime_add_ns(now, ns), HRTIMER_MODE_ABS);
  894. local_irq_restore(flags);
  895. }
  896. }
  897. static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val)
  898. {
  899. int nmi_wd_enabled = apic_lvt_nmi_mode(kvm_apic_get_reg(apic, APIC_LVT0));
  900. if (apic_lvt_nmi_mode(lvt0_val)) {
  901. if (!nmi_wd_enabled) {
  902. apic_debug("Receive NMI setting on APIC_LVT0 "
  903. "for cpu %d\n", apic->vcpu->vcpu_id);
  904. apic->vcpu->kvm->arch.vapics_in_nmi_mode++;
  905. }
  906. } else if (nmi_wd_enabled)
  907. apic->vcpu->kvm->arch.vapics_in_nmi_mode--;
  908. }
  909. static int apic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
  910. {
  911. int ret = 0;
  912. trace_kvm_apic_write(reg, val);
  913. switch (reg) {
  914. case APIC_ID: /* Local APIC ID */
  915. if (!apic_x2apic_mode(apic))
  916. kvm_apic_set_id(apic, val >> 24);
  917. else
  918. ret = 1;
  919. break;
  920. case APIC_TASKPRI:
  921. report_tpr_access(apic, true);
  922. apic_set_tpr(apic, val & 0xff);
  923. break;
  924. case APIC_EOI:
  925. apic_set_eoi(apic);
  926. break;
  927. case APIC_LDR:
  928. if (!apic_x2apic_mode(apic))
  929. kvm_apic_set_ldr(apic, val & APIC_LDR_MASK);
  930. else
  931. ret = 1;
  932. break;
  933. case APIC_DFR:
  934. if (!apic_x2apic_mode(apic)) {
  935. apic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF);
  936. recalculate_apic_map(apic->vcpu->kvm);
  937. } else
  938. ret = 1;
  939. break;
  940. case APIC_SPIV: {
  941. u32 mask = 0x3ff;
  942. if (kvm_apic_get_reg(apic, APIC_LVR) & APIC_LVR_DIRECTED_EOI)
  943. mask |= APIC_SPIV_DIRECTED_EOI;
  944. apic_set_spiv(apic, val & mask);
  945. if (!(val & APIC_SPIV_APIC_ENABLED)) {
  946. int i;
  947. u32 lvt_val;
  948. for (i = 0; i < APIC_LVT_NUM; i++) {
  949. lvt_val = kvm_apic_get_reg(apic,
  950. APIC_LVTT + 0x10 * i);
  951. apic_set_reg(apic, APIC_LVTT + 0x10 * i,
  952. lvt_val | APIC_LVT_MASKED);
  953. }
  954. atomic_set(&apic->lapic_timer.pending, 0);
  955. }
  956. break;
  957. }
  958. case APIC_ICR:
  959. /* No delay here, so we always clear the pending bit */
  960. apic_set_reg(apic, APIC_ICR, val & ~(1 << 12));
  961. apic_send_ipi(apic);
  962. break;
  963. case APIC_ICR2:
  964. if (!apic_x2apic_mode(apic))
  965. val &= 0xff000000;
  966. apic_set_reg(apic, APIC_ICR2, val);
  967. break;
  968. case APIC_LVT0:
  969. apic_manage_nmi_watchdog(apic, val);
  970. case APIC_LVTTHMR:
  971. case APIC_LVTPC:
  972. case APIC_LVT1:
  973. case APIC_LVTERR:
  974. /* TODO: Check vector */
  975. if (!kvm_apic_sw_enabled(apic))
  976. val |= APIC_LVT_MASKED;
  977. val &= apic_lvt_mask[(reg - APIC_LVTT) >> 4];
  978. apic_set_reg(apic, reg, val);
  979. break;
  980. case APIC_LVTT:
  981. if ((kvm_apic_get_reg(apic, APIC_LVTT) &
  982. apic->lapic_timer.timer_mode_mask) !=
  983. (val & apic->lapic_timer.timer_mode_mask))
  984. hrtimer_cancel(&apic->lapic_timer.timer);
  985. if (!kvm_apic_sw_enabled(apic))
  986. val |= APIC_LVT_MASKED;
  987. val &= (apic_lvt_mask[0] | apic->lapic_timer.timer_mode_mask);
  988. apic_set_reg(apic, APIC_LVTT, val);
  989. break;
  990. case APIC_TMICT:
  991. if (apic_lvtt_tscdeadline(apic))
  992. break;
  993. hrtimer_cancel(&apic->lapic_timer.timer);
  994. apic_set_reg(apic, APIC_TMICT, val);
  995. start_apic_timer(apic);
  996. break;
  997. case APIC_TDCR:
  998. if (val & 4)
  999. apic_debug("KVM_WRITE:TDCR %x\n", val);
  1000. apic_set_reg(apic, APIC_TDCR, val);
  1001. update_divide_count(apic);
  1002. break;
  1003. case APIC_ESR:
  1004. if (apic_x2apic_mode(apic) && val != 0) {
  1005. apic_debug("KVM_WRITE:ESR not zero %x\n", val);
  1006. ret = 1;
  1007. }
  1008. break;
  1009. case APIC_SELF_IPI:
  1010. if (apic_x2apic_mode(apic)) {
  1011. apic_reg_write(apic, APIC_ICR, 0x40000 | (val & 0xff));
  1012. } else
  1013. ret = 1;
  1014. break;
  1015. default:
  1016. ret = 1;
  1017. break;
  1018. }
  1019. if (ret)
  1020. apic_debug("Local APIC Write to read-only register %x\n", reg);
  1021. return ret;
  1022. }
  1023. static int apic_mmio_write(struct kvm_io_device *this,
  1024. gpa_t address, int len, const void *data)
  1025. {
  1026. struct kvm_lapic *apic = to_lapic(this);
  1027. unsigned int offset = address - apic->base_address;
  1028. u32 val;
  1029. if (!apic_mmio_in_range(apic, address))
  1030. return -EOPNOTSUPP;
  1031. /*
  1032. * APIC register must be aligned on 128-bits boundary.
  1033. * 32/64/128 bits registers must be accessed thru 32 bits.
  1034. * Refer SDM 8.4.1
  1035. */
  1036. if (len != 4 || (offset & 0xf)) {
  1037. /* Don't shout loud, $infamous_os would cause only noise. */
  1038. apic_debug("apic write: bad size=%d %lx\n", len, (long)address);
  1039. return 0;
  1040. }
  1041. val = *(u32*)data;
  1042. /* too common printing */
  1043. if (offset != APIC_EOI)
  1044. apic_debug("%s: offset 0x%x with length 0x%x, and value is "
  1045. "0x%x\n", __func__, offset, len, val);
  1046. apic_reg_write(apic, offset & 0xff0, val);
  1047. return 0;
  1048. }
  1049. void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu)
  1050. {
  1051. if (kvm_vcpu_has_lapic(vcpu))
  1052. apic_reg_write(vcpu->arch.apic, APIC_EOI, 0);
  1053. }
  1054. EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi);
  1055. /* emulate APIC access in a trap manner */
  1056. void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset)
  1057. {
  1058. u32 val = 0;
  1059. /* hw has done the conditional check and inst decode */
  1060. offset &= 0xff0;
  1061. apic_reg_read(vcpu->arch.apic, offset, 4, &val);
  1062. /* TODO: optimize to just emulate side effect w/o one more write */
  1063. apic_reg_write(vcpu->arch.apic, offset, val);
  1064. }
  1065. EXPORT_SYMBOL_GPL(kvm_apic_write_nodecode);
  1066. void kvm_free_lapic(struct kvm_vcpu *vcpu)
  1067. {
  1068. struct kvm_lapic *apic = vcpu->arch.apic;
  1069. if (!vcpu->arch.apic)
  1070. return;
  1071. hrtimer_cancel(&apic->lapic_timer.timer);
  1072. if (!(vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE))
  1073. static_key_slow_dec_deferred(&apic_hw_disabled);
  1074. if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_APIC_ENABLED))
  1075. static_key_slow_dec_deferred(&apic_sw_disabled);
  1076. if (apic->regs)
  1077. free_page((unsigned long)apic->regs);
  1078. kfree(apic);
  1079. }
  1080. /*
  1081. *----------------------------------------------------------------------
  1082. * LAPIC interface
  1083. *----------------------------------------------------------------------
  1084. */
  1085. u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu)
  1086. {
  1087. struct kvm_lapic *apic = vcpu->arch.apic;
  1088. if (!kvm_vcpu_has_lapic(vcpu) || apic_lvtt_oneshot(apic) ||
  1089. apic_lvtt_period(apic))
  1090. return 0;
  1091. return apic->lapic_timer.tscdeadline;
  1092. }
  1093. void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data)
  1094. {
  1095. struct kvm_lapic *apic = vcpu->arch.apic;
  1096. if (!kvm_vcpu_has_lapic(vcpu) || apic_lvtt_oneshot(apic) ||
  1097. apic_lvtt_period(apic))
  1098. return;
  1099. hrtimer_cancel(&apic->lapic_timer.timer);
  1100. apic->lapic_timer.tscdeadline = data;
  1101. start_apic_timer(apic);
  1102. }
  1103. void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
  1104. {
  1105. struct kvm_lapic *apic = vcpu->arch.apic;
  1106. if (!kvm_vcpu_has_lapic(vcpu))
  1107. return;
  1108. apic_set_tpr(apic, ((cr8 & 0x0f) << 4)
  1109. | (kvm_apic_get_reg(apic, APIC_TASKPRI) & 4));
  1110. }
  1111. u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
  1112. {
  1113. u64 tpr;
  1114. if (!kvm_vcpu_has_lapic(vcpu))
  1115. return 0;
  1116. tpr = (u64) kvm_apic_get_reg(vcpu->arch.apic, APIC_TASKPRI);
  1117. return (tpr & 0xf0) >> 4;
  1118. }
  1119. void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
  1120. {
  1121. u64 old_value = vcpu->arch.apic_base;
  1122. struct kvm_lapic *apic = vcpu->arch.apic;
  1123. if (!apic) {
  1124. value |= MSR_IA32_APICBASE_BSP;
  1125. vcpu->arch.apic_base = value;
  1126. return;
  1127. }
  1128. if (!kvm_vcpu_is_bsp(apic->vcpu))
  1129. value &= ~MSR_IA32_APICBASE_BSP;
  1130. vcpu->arch.apic_base = value;
  1131. /* update jump label if enable bit changes */
  1132. if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE) {
  1133. if (value & MSR_IA32_APICBASE_ENABLE)
  1134. static_key_slow_dec_deferred(&apic_hw_disabled);
  1135. else
  1136. static_key_slow_inc(&apic_hw_disabled.key);
  1137. recalculate_apic_map(vcpu->kvm);
  1138. }
  1139. if ((old_value ^ value) & X2APIC_ENABLE) {
  1140. if (value & X2APIC_ENABLE) {
  1141. u32 id = kvm_apic_id(apic);
  1142. u32 ldr = ((id >> 4) << 16) | (1 << (id & 0xf));
  1143. kvm_apic_set_ldr(apic, ldr);
  1144. kvm_x86_ops->set_virtual_x2apic_mode(vcpu, true);
  1145. } else
  1146. kvm_x86_ops->set_virtual_x2apic_mode(vcpu, false);
  1147. }
  1148. apic->base_address = apic->vcpu->arch.apic_base &
  1149. MSR_IA32_APICBASE_BASE;
  1150. /* with FSB delivery interrupt, we can restart APIC functionality */
  1151. apic_debug("apic base msr is 0x%016" PRIx64 ", and base address is "
  1152. "0x%lx.\n", apic->vcpu->arch.apic_base, apic->base_address);
  1153. }
  1154. void kvm_lapic_reset(struct kvm_vcpu *vcpu)
  1155. {
  1156. struct kvm_lapic *apic;
  1157. int i;
  1158. apic_debug("%s\n", __func__);
  1159. ASSERT(vcpu);
  1160. apic = vcpu->arch.apic;
  1161. ASSERT(apic != NULL);
  1162. /* Stop the timer in case it's a reset to an active apic */
  1163. hrtimer_cancel(&apic->lapic_timer.timer);
  1164. kvm_apic_set_id(apic, vcpu->vcpu_id);
  1165. kvm_apic_set_version(apic->vcpu);
  1166. for (i = 0; i < APIC_LVT_NUM; i++)
  1167. apic_set_reg(apic, APIC_LVTT + 0x10 * i, APIC_LVT_MASKED);
  1168. apic_set_reg(apic, APIC_LVT0,
  1169. SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
  1170. apic_set_reg(apic, APIC_DFR, 0xffffffffU);
  1171. apic_set_spiv(apic, 0xff);
  1172. apic_set_reg(apic, APIC_TASKPRI, 0);
  1173. kvm_apic_set_ldr(apic, 0);
  1174. apic_set_reg(apic, APIC_ESR, 0);
  1175. apic_set_reg(apic, APIC_ICR, 0);
  1176. apic_set_reg(apic, APIC_ICR2, 0);
  1177. apic_set_reg(apic, APIC_TDCR, 0);
  1178. apic_set_reg(apic, APIC_TMICT, 0);
  1179. for (i = 0; i < 8; i++) {
  1180. apic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
  1181. apic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
  1182. apic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
  1183. }
  1184. apic->irr_pending = kvm_apic_vid_enabled(vcpu->kvm);
  1185. apic->isr_count = kvm_apic_vid_enabled(vcpu->kvm);
  1186. apic->highest_isr_cache = -1;
  1187. update_divide_count(apic);
  1188. atomic_set(&apic->lapic_timer.pending, 0);
  1189. if (kvm_vcpu_is_bsp(vcpu))
  1190. kvm_lapic_set_base(vcpu,
  1191. vcpu->arch.apic_base | MSR_IA32_APICBASE_BSP);
  1192. vcpu->arch.pv_eoi.msr_val = 0;
  1193. apic_update_ppr(apic);
  1194. vcpu->arch.apic_arb_prio = 0;
  1195. vcpu->arch.apic_attention = 0;
  1196. apic_debug(KERN_INFO "%s: vcpu=%p, id=%d, base_msr="
  1197. "0x%016" PRIx64 ", base_address=0x%0lx.\n", __func__,
  1198. vcpu, kvm_apic_id(apic),
  1199. vcpu->arch.apic_base, apic->base_address);
  1200. }
  1201. /*
  1202. *----------------------------------------------------------------------
  1203. * timer interface
  1204. *----------------------------------------------------------------------
  1205. */
  1206. static bool lapic_is_periodic(struct kvm_lapic *apic)
  1207. {
  1208. return apic_lvtt_period(apic);
  1209. }
  1210. int apic_has_pending_timer(struct kvm_vcpu *vcpu)
  1211. {
  1212. struct kvm_lapic *apic = vcpu->arch.apic;
  1213. if (kvm_vcpu_has_lapic(vcpu) && apic_enabled(apic) &&
  1214. apic_lvt_enabled(apic, APIC_LVTT))
  1215. return atomic_read(&apic->lapic_timer.pending);
  1216. return 0;
  1217. }
  1218. int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type)
  1219. {
  1220. u32 reg = kvm_apic_get_reg(apic, lvt_type);
  1221. int vector, mode, trig_mode;
  1222. if (kvm_apic_hw_enabled(apic) && !(reg & APIC_LVT_MASKED)) {
  1223. vector = reg & APIC_VECTOR_MASK;
  1224. mode = reg & APIC_MODE_MASK;
  1225. trig_mode = reg & APIC_LVT_LEVEL_TRIGGER;
  1226. return __apic_accept_irq(apic, mode, vector, 1, trig_mode,
  1227. NULL);
  1228. }
  1229. return 0;
  1230. }
  1231. void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu)
  1232. {
  1233. struct kvm_lapic *apic = vcpu->arch.apic;
  1234. if (apic)
  1235. kvm_apic_local_deliver(apic, APIC_LVT0);
  1236. }
  1237. static const struct kvm_io_device_ops apic_mmio_ops = {
  1238. .read = apic_mmio_read,
  1239. .write = apic_mmio_write,
  1240. };
  1241. static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)
  1242. {
  1243. struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
  1244. struct kvm_lapic *apic = container_of(ktimer, struct kvm_lapic, lapic_timer);
  1245. struct kvm_vcpu *vcpu = apic->vcpu;
  1246. wait_queue_head_t *q = &vcpu->wq;
  1247. /*
  1248. * There is a race window between reading and incrementing, but we do
  1249. * not care about potentially losing timer events in the !reinject
  1250. * case anyway. Note: KVM_REQ_PENDING_TIMER is implicitly checked
  1251. * in vcpu_enter_guest.
  1252. */
  1253. if (!atomic_read(&ktimer->pending)) {
  1254. atomic_inc(&ktimer->pending);
  1255. /* FIXME: this code should not know anything about vcpus */
  1256. kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
  1257. }
  1258. if (waitqueue_active(q))
  1259. wake_up_interruptible(q);
  1260. if (lapic_is_periodic(apic)) {
  1261. hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
  1262. return HRTIMER_RESTART;
  1263. } else
  1264. return HRTIMER_NORESTART;
  1265. }
  1266. int kvm_create_lapic(struct kvm_vcpu *vcpu)
  1267. {
  1268. struct kvm_lapic *apic;
  1269. ASSERT(vcpu != NULL);
  1270. apic_debug("apic_init %d\n", vcpu->vcpu_id);
  1271. apic = kzalloc(sizeof(*apic), GFP_KERNEL);
  1272. if (!apic)
  1273. goto nomem;
  1274. vcpu->arch.apic = apic;
  1275. apic->regs = (void *)get_zeroed_page(GFP_KERNEL);
  1276. if (!apic->regs) {
  1277. printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
  1278. vcpu->vcpu_id);
  1279. goto nomem_free_apic;
  1280. }
  1281. apic->vcpu = vcpu;
  1282. hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
  1283. HRTIMER_MODE_ABS);
  1284. apic->lapic_timer.timer.function = apic_timer_fn;
  1285. /*
  1286. * APIC is created enabled. This will prevent kvm_lapic_set_base from
  1287. * thinking that APIC satet has changed.
  1288. */
  1289. vcpu->arch.apic_base = MSR_IA32_APICBASE_ENABLE;
  1290. kvm_lapic_set_base(vcpu,
  1291. APIC_DEFAULT_PHYS_BASE | MSR_IA32_APICBASE_ENABLE);
  1292. static_key_slow_inc(&apic_sw_disabled.key); /* sw disabled at reset */
  1293. kvm_lapic_reset(vcpu);
  1294. kvm_iodevice_init(&apic->dev, &apic_mmio_ops);
  1295. return 0;
  1296. nomem_free_apic:
  1297. kfree(apic);
  1298. nomem:
  1299. return -ENOMEM;
  1300. }
  1301. int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
  1302. {
  1303. struct kvm_lapic *apic = vcpu->arch.apic;
  1304. int highest_irr;
  1305. if (!kvm_vcpu_has_lapic(vcpu) || !apic_enabled(apic))
  1306. return -1;
  1307. apic_update_ppr(apic);
  1308. highest_irr = apic_find_highest_irr(apic);
  1309. if ((highest_irr == -1) ||
  1310. ((highest_irr & 0xF0) <= kvm_apic_get_reg(apic, APIC_PROCPRI)))
  1311. return -1;
  1312. return highest_irr;
  1313. }
  1314. int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
  1315. {
  1316. u32 lvt0 = kvm_apic_get_reg(vcpu->arch.apic, APIC_LVT0);
  1317. int r = 0;
  1318. if (!kvm_apic_hw_enabled(vcpu->arch.apic))
  1319. r = 1;
  1320. if ((lvt0 & APIC_LVT_MASKED) == 0 &&
  1321. GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
  1322. r = 1;
  1323. return r;
  1324. }
  1325. void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
  1326. {
  1327. struct kvm_lapic *apic = vcpu->arch.apic;
  1328. if (!kvm_vcpu_has_lapic(vcpu))
  1329. return;
  1330. if (atomic_read(&apic->lapic_timer.pending) > 0) {
  1331. kvm_apic_local_deliver(apic, APIC_LVTT);
  1332. atomic_set(&apic->lapic_timer.pending, 0);
  1333. }
  1334. }
  1335. int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
  1336. {
  1337. int vector = kvm_apic_has_interrupt(vcpu);
  1338. struct kvm_lapic *apic = vcpu->arch.apic;
  1339. /* Note that we never get here with APIC virtualization enabled. */
  1340. if (vector == -1)
  1341. return -1;
  1342. apic_set_isr(vector, apic);
  1343. apic_update_ppr(apic);
  1344. apic_clear_irr(vector, apic);
  1345. return vector;
  1346. }
  1347. void kvm_apic_post_state_restore(struct kvm_vcpu *vcpu,
  1348. struct kvm_lapic_state *s)
  1349. {
  1350. struct kvm_lapic *apic = vcpu->arch.apic;
  1351. kvm_lapic_set_base(vcpu, vcpu->arch.apic_base);
  1352. /* set SPIV separately to get count of SW disabled APICs right */
  1353. apic_set_spiv(apic, *((u32 *)(s->regs + APIC_SPIV)));
  1354. memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
  1355. /* call kvm_apic_set_id() to put apic into apic_map */
  1356. kvm_apic_set_id(apic, kvm_apic_id(apic));
  1357. kvm_apic_set_version(vcpu);
  1358. apic_update_ppr(apic);
  1359. hrtimer_cancel(&apic->lapic_timer.timer);
  1360. update_divide_count(apic);
  1361. start_apic_timer(apic);
  1362. apic->irr_pending = true;
  1363. apic->isr_count = kvm_apic_vid_enabled(vcpu->kvm) ?
  1364. 1 : count_vectors(apic->regs + APIC_ISR);
  1365. apic->highest_isr_cache = -1;
  1366. kvm_x86_ops->hwapic_isr_update(vcpu->kvm, apic_find_highest_isr(apic));
  1367. kvm_make_request(KVM_REQ_EVENT, vcpu);
  1368. kvm_rtc_eoi_tracking_restore_one(vcpu);
  1369. }
  1370. void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
  1371. {
  1372. struct hrtimer *timer;
  1373. if (!kvm_vcpu_has_lapic(vcpu))
  1374. return;
  1375. timer = &vcpu->arch.apic->lapic_timer.timer;
  1376. if (hrtimer_cancel(timer))
  1377. hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
  1378. }
  1379. /*
  1380. * apic_sync_pv_eoi_from_guest - called on vmexit or cancel interrupt
  1381. *
  1382. * Detect whether guest triggered PV EOI since the
  1383. * last entry. If yes, set EOI on guests's behalf.
  1384. * Clear PV EOI in guest memory in any case.
  1385. */
  1386. static void apic_sync_pv_eoi_from_guest(struct kvm_vcpu *vcpu,
  1387. struct kvm_lapic *apic)
  1388. {
  1389. bool pending;
  1390. int vector;
  1391. /*
  1392. * PV EOI state is derived from KVM_APIC_PV_EOI_PENDING in host
  1393. * and KVM_PV_EOI_ENABLED in guest memory as follows:
  1394. *
  1395. * KVM_APIC_PV_EOI_PENDING is unset:
  1396. * -> host disabled PV EOI.
  1397. * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is set:
  1398. * -> host enabled PV EOI, guest did not execute EOI yet.
  1399. * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is unset:
  1400. * -> host enabled PV EOI, guest executed EOI.
  1401. */
  1402. BUG_ON(!pv_eoi_enabled(vcpu));
  1403. pending = pv_eoi_get_pending(vcpu);
  1404. /*
  1405. * Clear pending bit in any case: it will be set again on vmentry.
  1406. * While this might not be ideal from performance point of view,
  1407. * this makes sure pv eoi is only enabled when we know it's safe.
  1408. */
  1409. pv_eoi_clr_pending(vcpu);
  1410. if (pending)
  1411. return;
  1412. vector = apic_set_eoi(apic);
  1413. trace_kvm_pv_eoi(apic, vector);
  1414. }
  1415. void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu)
  1416. {
  1417. u32 data;
  1418. if (test_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention))
  1419. apic_sync_pv_eoi_from_guest(vcpu, vcpu->arch.apic);
  1420. if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
  1421. return;
  1422. kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
  1423. sizeof(u32));
  1424. apic_set_tpr(vcpu->arch.apic, data & 0xff);
  1425. }
  1426. /*
  1427. * apic_sync_pv_eoi_to_guest - called before vmentry
  1428. *
  1429. * Detect whether it's safe to enable PV EOI and
  1430. * if yes do so.
  1431. */
  1432. static void apic_sync_pv_eoi_to_guest(struct kvm_vcpu *vcpu,
  1433. struct kvm_lapic *apic)
  1434. {
  1435. if (!pv_eoi_enabled(vcpu) ||
  1436. /* IRR set or many bits in ISR: could be nested. */
  1437. apic->irr_pending ||
  1438. /* Cache not set: could be safe but we don't bother. */
  1439. apic->highest_isr_cache == -1 ||
  1440. /* Need EOI to update ioapic. */
  1441. kvm_ioapic_handles_vector(vcpu->kvm, apic->highest_isr_cache)) {
  1442. /*
  1443. * PV EOI was disabled by apic_sync_pv_eoi_from_guest
  1444. * so we need not do anything here.
  1445. */
  1446. return;
  1447. }
  1448. pv_eoi_set_pending(apic->vcpu);
  1449. }
  1450. void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu)
  1451. {
  1452. u32 data, tpr;
  1453. int max_irr, max_isr;
  1454. struct kvm_lapic *apic = vcpu->arch.apic;
  1455. apic_sync_pv_eoi_to_guest(vcpu, apic);
  1456. if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
  1457. return;
  1458. tpr = kvm_apic_get_reg(apic, APIC_TASKPRI) & 0xff;
  1459. max_irr = apic_find_highest_irr(apic);
  1460. if (max_irr < 0)
  1461. max_irr = 0;
  1462. max_isr = apic_find_highest_isr(apic);
  1463. if (max_isr < 0)
  1464. max_isr = 0;
  1465. data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24);
  1466. kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
  1467. sizeof(u32));
  1468. }
  1469. int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr)
  1470. {
  1471. if (vapic_addr) {
  1472. if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
  1473. &vcpu->arch.apic->vapic_cache,
  1474. vapic_addr, sizeof(u32)))
  1475. return -EINVAL;
  1476. __set_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
  1477. } else {
  1478. __clear_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
  1479. }
  1480. vcpu->arch.apic->vapic_addr = vapic_addr;
  1481. return 0;
  1482. }
  1483. int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  1484. {
  1485. struct kvm_lapic *apic = vcpu->arch.apic;
  1486. u32 reg = (msr - APIC_BASE_MSR) << 4;
  1487. if (!irqchip_in_kernel(vcpu->kvm) || !apic_x2apic_mode(apic))
  1488. return 1;
  1489. /* if this is ICR write vector before command */
  1490. if (msr == 0x830)
  1491. apic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
  1492. return apic_reg_write(apic, reg, (u32)data);
  1493. }
  1494. int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
  1495. {
  1496. struct kvm_lapic *apic = vcpu->arch.apic;
  1497. u32 reg = (msr - APIC_BASE_MSR) << 4, low, high = 0;
  1498. if (!irqchip_in_kernel(vcpu->kvm) || !apic_x2apic_mode(apic))
  1499. return 1;
  1500. if (apic_reg_read(apic, reg, 4, &low))
  1501. return 1;
  1502. if (msr == 0x830)
  1503. apic_reg_read(apic, APIC_ICR2, 4, &high);
  1504. *data = (((u64)high) << 32) | low;
  1505. return 0;
  1506. }
  1507. int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 reg, u64 data)
  1508. {
  1509. struct kvm_lapic *apic = vcpu->arch.apic;
  1510. if (!kvm_vcpu_has_lapic(vcpu))
  1511. return 1;
  1512. /* if this is ICR write vector before command */
  1513. if (reg == APIC_ICR)
  1514. apic_reg_write(apic, APIC_ICR2, (u32)(data >> 32));
  1515. return apic_reg_write(apic, reg, (u32)data);
  1516. }
  1517. int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 reg, u64 *data)
  1518. {
  1519. struct kvm_lapic *apic = vcpu->arch.apic;
  1520. u32 low, high = 0;
  1521. if (!kvm_vcpu_has_lapic(vcpu))
  1522. return 1;
  1523. if (apic_reg_read(apic, reg, 4, &low))
  1524. return 1;
  1525. if (reg == APIC_ICR)
  1526. apic_reg_read(apic, APIC_ICR2, 4, &high);
  1527. *data = (((u64)high) << 32) | low;
  1528. return 0;
  1529. }
  1530. int kvm_lapic_enable_pv_eoi(struct kvm_vcpu *vcpu, u64 data)
  1531. {
  1532. u64 addr = data & ~KVM_MSR_ENABLED;
  1533. if (!IS_ALIGNED(addr, 4))
  1534. return 1;
  1535. vcpu->arch.pv_eoi.msr_val = data;
  1536. if (!pv_eoi_enabled(vcpu))
  1537. return 0;
  1538. return kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.pv_eoi.data,
  1539. addr, sizeof(u8));
  1540. }
  1541. void kvm_apic_accept_events(struct kvm_vcpu *vcpu)
  1542. {
  1543. struct kvm_lapic *apic = vcpu->arch.apic;
  1544. unsigned int sipi_vector;
  1545. unsigned long pe;
  1546. if (!kvm_vcpu_has_lapic(vcpu) || !apic->pending_events)
  1547. return;
  1548. pe = xchg(&apic->pending_events, 0);
  1549. if (test_bit(KVM_APIC_INIT, &pe)) {
  1550. kvm_lapic_reset(vcpu);
  1551. kvm_vcpu_reset(vcpu);
  1552. if (kvm_vcpu_is_bsp(apic->vcpu))
  1553. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  1554. else
  1555. vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
  1556. }
  1557. if (test_bit(KVM_APIC_SIPI, &pe) &&
  1558. vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
  1559. /* evaluate pending_events before reading the vector */
  1560. smp_rmb();
  1561. sipi_vector = apic->sipi_vector;
  1562. pr_debug("vcpu %d received sipi with vector # %x\n",
  1563. vcpu->vcpu_id, sipi_vector);
  1564. kvm_vcpu_deliver_sipi_vector(vcpu, sipi_vector);
  1565. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  1566. }
  1567. }
  1568. void kvm_lapic_init(void)
  1569. {
  1570. /* do not patch jump label more than once per second */
  1571. jump_label_rate_limit(&apic_hw_disabled, HZ);
  1572. jump_label_rate_limit(&apic_sw_disabled, HZ);
  1573. }