lapic.c 46 KB

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