lapic.c 46 KB

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