svm.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * AMD SVM support
  5. *
  6. * Copyright (C) 2006 Qumranet, Inc.
  7. *
  8. * Authors:
  9. * Yaniv Kamay <yaniv@qumranet.com>
  10. * Avi Kivity <avi@qumranet.com>
  11. *
  12. * This work is licensed under the terms of the GNU GPL, version 2. See
  13. * the COPYING file in the top-level directory.
  14. *
  15. */
  16. #include <linux/kvm_host.h>
  17. #include "kvm_svm.h"
  18. #include "irq.h"
  19. #include "mmu.h"
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/highmem.h>
  24. #include <linux/sched.h>
  25. #include <asm/desc.h>
  26. #define __ex(x) __kvm_handle_fault_on_reboot(x)
  27. MODULE_AUTHOR("Qumranet");
  28. MODULE_LICENSE("GPL");
  29. #define IOPM_ALLOC_ORDER 2
  30. #define MSRPM_ALLOC_ORDER 1
  31. #define DB_VECTOR 1
  32. #define UD_VECTOR 6
  33. #define GP_VECTOR 13
  34. #define DR7_GD_MASK (1 << 13)
  35. #define DR6_BD_MASK (1 << 13)
  36. #define SEG_TYPE_LDT 2
  37. #define SEG_TYPE_BUSY_TSS16 3
  38. #define SVM_FEATURE_NPT (1 << 0)
  39. #define SVM_FEATURE_LBRV (1 << 1)
  40. #define SVM_DEATURE_SVML (1 << 2)
  41. #define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
  42. /* enable NPT for AMD64 and X86 with PAE */
  43. #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
  44. static bool npt_enabled = true;
  45. #else
  46. static bool npt_enabled = false;
  47. #endif
  48. static int npt = 1;
  49. module_param(npt, int, S_IRUGO);
  50. static void kvm_reput_irq(struct vcpu_svm *svm);
  51. static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
  52. {
  53. return container_of(vcpu, struct vcpu_svm, vcpu);
  54. }
  55. static unsigned long iopm_base;
  56. struct kvm_ldttss_desc {
  57. u16 limit0;
  58. u16 base0;
  59. unsigned base1 : 8, type : 5, dpl : 2, p : 1;
  60. unsigned limit1 : 4, zero0 : 3, g : 1, base2 : 8;
  61. u32 base3;
  62. u32 zero1;
  63. } __attribute__((packed));
  64. struct svm_cpu_data {
  65. int cpu;
  66. u64 asid_generation;
  67. u32 max_asid;
  68. u32 next_asid;
  69. struct kvm_ldttss_desc *tss_desc;
  70. struct page *save_area;
  71. };
  72. static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
  73. static uint32_t svm_features;
  74. struct svm_init_data {
  75. int cpu;
  76. int r;
  77. };
  78. static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
  79. #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
  80. #define MSRS_RANGE_SIZE 2048
  81. #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
  82. #define MAX_INST_SIZE 15
  83. static inline u32 svm_has(u32 feat)
  84. {
  85. return svm_features & feat;
  86. }
  87. static inline u8 pop_irq(struct kvm_vcpu *vcpu)
  88. {
  89. int word_index = __ffs(vcpu->arch.irq_summary);
  90. int bit_index = __ffs(vcpu->arch.irq_pending[word_index]);
  91. int irq = word_index * BITS_PER_LONG + bit_index;
  92. clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]);
  93. if (!vcpu->arch.irq_pending[word_index])
  94. clear_bit(word_index, &vcpu->arch.irq_summary);
  95. return irq;
  96. }
  97. static inline void push_irq(struct kvm_vcpu *vcpu, u8 irq)
  98. {
  99. set_bit(irq, vcpu->arch.irq_pending);
  100. set_bit(irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
  101. }
  102. static inline void clgi(void)
  103. {
  104. asm volatile (__ex(SVM_CLGI));
  105. }
  106. static inline void stgi(void)
  107. {
  108. asm volatile (__ex(SVM_STGI));
  109. }
  110. static inline void invlpga(unsigned long addr, u32 asid)
  111. {
  112. asm volatile (__ex(SVM_INVLPGA) :: "a"(addr), "c"(asid));
  113. }
  114. static inline unsigned long kvm_read_cr2(void)
  115. {
  116. unsigned long cr2;
  117. asm volatile ("mov %%cr2, %0" : "=r" (cr2));
  118. return cr2;
  119. }
  120. static inline void kvm_write_cr2(unsigned long val)
  121. {
  122. asm volatile ("mov %0, %%cr2" :: "r" (val));
  123. }
  124. static inline unsigned long read_dr6(void)
  125. {
  126. unsigned long dr6;
  127. asm volatile ("mov %%dr6, %0" : "=r" (dr6));
  128. return dr6;
  129. }
  130. static inline void write_dr6(unsigned long val)
  131. {
  132. asm volatile ("mov %0, %%dr6" :: "r" (val));
  133. }
  134. static inline unsigned long read_dr7(void)
  135. {
  136. unsigned long dr7;
  137. asm volatile ("mov %%dr7, %0" : "=r" (dr7));
  138. return dr7;
  139. }
  140. static inline void write_dr7(unsigned long val)
  141. {
  142. asm volatile ("mov %0, %%dr7" :: "r" (val));
  143. }
  144. static inline void force_new_asid(struct kvm_vcpu *vcpu)
  145. {
  146. to_svm(vcpu)->asid_generation--;
  147. }
  148. static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
  149. {
  150. force_new_asid(vcpu);
  151. }
  152. static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  153. {
  154. if (!npt_enabled && !(efer & EFER_LMA))
  155. efer &= ~EFER_LME;
  156. to_svm(vcpu)->vmcb->save.efer = efer | MSR_EFER_SVME_MASK;
  157. vcpu->arch.shadow_efer = efer;
  158. }
  159. static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
  160. bool has_error_code, u32 error_code)
  161. {
  162. struct vcpu_svm *svm = to_svm(vcpu);
  163. svm->vmcb->control.event_inj = nr
  164. | SVM_EVTINJ_VALID
  165. | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
  166. | SVM_EVTINJ_TYPE_EXEPT;
  167. svm->vmcb->control.event_inj_err = error_code;
  168. }
  169. static bool svm_exception_injected(struct kvm_vcpu *vcpu)
  170. {
  171. struct vcpu_svm *svm = to_svm(vcpu);
  172. return !(svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID);
  173. }
  174. static int is_external_interrupt(u32 info)
  175. {
  176. info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
  177. return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
  178. }
  179. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  180. {
  181. struct vcpu_svm *svm = to_svm(vcpu);
  182. if (!svm->next_rip) {
  183. printk(KERN_DEBUG "%s: NOP\n", __func__);
  184. return;
  185. }
  186. if (svm->next_rip - svm->vmcb->save.rip > MAX_INST_SIZE)
  187. printk(KERN_ERR "%s: ip 0x%llx next 0x%llx\n",
  188. __func__,
  189. svm->vmcb->save.rip,
  190. svm->next_rip);
  191. vcpu->arch.rip = svm->vmcb->save.rip = svm->next_rip;
  192. svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
  193. vcpu->arch.interrupt_window_open = 1;
  194. }
  195. static int has_svm(void)
  196. {
  197. uint32_t eax, ebx, ecx, edx;
  198. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD) {
  199. printk(KERN_INFO "has_svm: not amd\n");
  200. return 0;
  201. }
  202. cpuid(0x80000000, &eax, &ebx, &ecx, &edx);
  203. if (eax < SVM_CPUID_FUNC) {
  204. printk(KERN_INFO "has_svm: can't execute cpuid_8000000a\n");
  205. return 0;
  206. }
  207. cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
  208. if (!(ecx & (1 << SVM_CPUID_FEATURE_SHIFT))) {
  209. printk(KERN_DEBUG "has_svm: svm not available\n");
  210. return 0;
  211. }
  212. return 1;
  213. }
  214. static void svm_hardware_disable(void *garbage)
  215. {
  216. struct svm_cpu_data *svm_data
  217. = per_cpu(svm_data, raw_smp_processor_id());
  218. if (svm_data) {
  219. uint64_t efer;
  220. wrmsrl(MSR_VM_HSAVE_PA, 0);
  221. rdmsrl(MSR_EFER, efer);
  222. wrmsrl(MSR_EFER, efer & ~MSR_EFER_SVME_MASK);
  223. per_cpu(svm_data, raw_smp_processor_id()) = NULL;
  224. __free_page(svm_data->save_area);
  225. kfree(svm_data);
  226. }
  227. }
  228. static void svm_hardware_enable(void *garbage)
  229. {
  230. struct svm_cpu_data *svm_data;
  231. uint64_t efer;
  232. struct desc_ptr gdt_descr;
  233. struct desc_struct *gdt;
  234. int me = raw_smp_processor_id();
  235. if (!has_svm()) {
  236. printk(KERN_ERR "svm_cpu_init: err EOPNOTSUPP on %d\n", me);
  237. return;
  238. }
  239. svm_data = per_cpu(svm_data, me);
  240. if (!svm_data) {
  241. printk(KERN_ERR "svm_cpu_init: svm_data is NULL on %d\n",
  242. me);
  243. return;
  244. }
  245. svm_data->asid_generation = 1;
  246. svm_data->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
  247. svm_data->next_asid = svm_data->max_asid + 1;
  248. asm volatile ("sgdt %0" : "=m"(gdt_descr));
  249. gdt = (struct desc_struct *)gdt_descr.address;
  250. svm_data->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
  251. rdmsrl(MSR_EFER, efer);
  252. wrmsrl(MSR_EFER, efer | MSR_EFER_SVME_MASK);
  253. wrmsrl(MSR_VM_HSAVE_PA,
  254. page_to_pfn(svm_data->save_area) << PAGE_SHIFT);
  255. }
  256. static int svm_cpu_init(int cpu)
  257. {
  258. struct svm_cpu_data *svm_data;
  259. int r;
  260. svm_data = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
  261. if (!svm_data)
  262. return -ENOMEM;
  263. svm_data->cpu = cpu;
  264. svm_data->save_area = alloc_page(GFP_KERNEL);
  265. r = -ENOMEM;
  266. if (!svm_data->save_area)
  267. goto err_1;
  268. per_cpu(svm_data, cpu) = svm_data;
  269. return 0;
  270. err_1:
  271. kfree(svm_data);
  272. return r;
  273. }
  274. static void set_msr_interception(u32 *msrpm, unsigned msr,
  275. int read, int write)
  276. {
  277. int i;
  278. for (i = 0; i < NUM_MSR_MAPS; i++) {
  279. if (msr >= msrpm_ranges[i] &&
  280. msr < msrpm_ranges[i] + MSRS_IN_RANGE) {
  281. u32 msr_offset = (i * MSRS_IN_RANGE + msr -
  282. msrpm_ranges[i]) * 2;
  283. u32 *base = msrpm + (msr_offset / 32);
  284. u32 msr_shift = msr_offset % 32;
  285. u32 mask = ((write) ? 0 : 2) | ((read) ? 0 : 1);
  286. *base = (*base & ~(0x3 << msr_shift)) |
  287. (mask << msr_shift);
  288. return;
  289. }
  290. }
  291. BUG();
  292. }
  293. static void svm_vcpu_init_msrpm(u32 *msrpm)
  294. {
  295. memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
  296. #ifdef CONFIG_X86_64
  297. set_msr_interception(msrpm, MSR_GS_BASE, 1, 1);
  298. set_msr_interception(msrpm, MSR_FS_BASE, 1, 1);
  299. set_msr_interception(msrpm, MSR_KERNEL_GS_BASE, 1, 1);
  300. set_msr_interception(msrpm, MSR_LSTAR, 1, 1);
  301. set_msr_interception(msrpm, MSR_CSTAR, 1, 1);
  302. set_msr_interception(msrpm, MSR_SYSCALL_MASK, 1, 1);
  303. #endif
  304. set_msr_interception(msrpm, MSR_K6_STAR, 1, 1);
  305. set_msr_interception(msrpm, MSR_IA32_SYSENTER_CS, 1, 1);
  306. set_msr_interception(msrpm, MSR_IA32_SYSENTER_ESP, 1, 1);
  307. set_msr_interception(msrpm, MSR_IA32_SYSENTER_EIP, 1, 1);
  308. }
  309. static void svm_enable_lbrv(struct vcpu_svm *svm)
  310. {
  311. u32 *msrpm = svm->msrpm;
  312. svm->vmcb->control.lbr_ctl = 1;
  313. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
  314. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
  315. set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
  316. set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
  317. }
  318. static void svm_disable_lbrv(struct vcpu_svm *svm)
  319. {
  320. u32 *msrpm = svm->msrpm;
  321. svm->vmcb->control.lbr_ctl = 0;
  322. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
  323. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
  324. set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
  325. set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
  326. }
  327. static __init int svm_hardware_setup(void)
  328. {
  329. int cpu;
  330. struct page *iopm_pages;
  331. void *iopm_va;
  332. int r;
  333. iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
  334. if (!iopm_pages)
  335. return -ENOMEM;
  336. iopm_va = page_address(iopm_pages);
  337. memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
  338. clear_bit(0x80, iopm_va); /* allow direct access to PC debug port */
  339. iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
  340. if (boot_cpu_has(X86_FEATURE_NX))
  341. kvm_enable_efer_bits(EFER_NX);
  342. for_each_online_cpu(cpu) {
  343. r = svm_cpu_init(cpu);
  344. if (r)
  345. goto err;
  346. }
  347. svm_features = cpuid_edx(SVM_CPUID_FUNC);
  348. if (!svm_has(SVM_FEATURE_NPT))
  349. npt_enabled = false;
  350. if (npt_enabled && !npt) {
  351. printk(KERN_INFO "kvm: Nested Paging disabled\n");
  352. npt_enabled = false;
  353. }
  354. if (npt_enabled) {
  355. printk(KERN_INFO "kvm: Nested Paging enabled\n");
  356. kvm_enable_tdp();
  357. }
  358. return 0;
  359. err:
  360. __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
  361. iopm_base = 0;
  362. return r;
  363. }
  364. static __exit void svm_hardware_unsetup(void)
  365. {
  366. __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
  367. iopm_base = 0;
  368. }
  369. static void init_seg(struct vmcb_seg *seg)
  370. {
  371. seg->selector = 0;
  372. seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
  373. SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
  374. seg->limit = 0xffff;
  375. seg->base = 0;
  376. }
  377. static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
  378. {
  379. seg->selector = 0;
  380. seg->attrib = SVM_SELECTOR_P_MASK | type;
  381. seg->limit = 0xffff;
  382. seg->base = 0;
  383. }
  384. static void init_vmcb(struct vcpu_svm *svm)
  385. {
  386. struct vmcb_control_area *control = &svm->vmcb->control;
  387. struct vmcb_save_area *save = &svm->vmcb->save;
  388. control->intercept_cr_read = INTERCEPT_CR0_MASK |
  389. INTERCEPT_CR3_MASK |
  390. INTERCEPT_CR4_MASK;
  391. control->intercept_cr_write = INTERCEPT_CR0_MASK |
  392. INTERCEPT_CR3_MASK |
  393. INTERCEPT_CR4_MASK |
  394. INTERCEPT_CR8_MASK;
  395. control->intercept_dr_read = INTERCEPT_DR0_MASK |
  396. INTERCEPT_DR1_MASK |
  397. INTERCEPT_DR2_MASK |
  398. INTERCEPT_DR3_MASK;
  399. control->intercept_dr_write = INTERCEPT_DR0_MASK |
  400. INTERCEPT_DR1_MASK |
  401. INTERCEPT_DR2_MASK |
  402. INTERCEPT_DR3_MASK |
  403. INTERCEPT_DR5_MASK |
  404. INTERCEPT_DR7_MASK;
  405. control->intercept_exceptions = (1 << PF_VECTOR) |
  406. (1 << UD_VECTOR) |
  407. (1 << MC_VECTOR);
  408. control->intercept = (1ULL << INTERCEPT_INTR) |
  409. (1ULL << INTERCEPT_NMI) |
  410. (1ULL << INTERCEPT_SMI) |
  411. (1ULL << INTERCEPT_CPUID) |
  412. (1ULL << INTERCEPT_INVD) |
  413. (1ULL << INTERCEPT_HLT) |
  414. (1ULL << INTERCEPT_INVLPGA) |
  415. (1ULL << INTERCEPT_IOIO_PROT) |
  416. (1ULL << INTERCEPT_MSR_PROT) |
  417. (1ULL << INTERCEPT_TASK_SWITCH) |
  418. (1ULL << INTERCEPT_SHUTDOWN) |
  419. (1ULL << INTERCEPT_VMRUN) |
  420. (1ULL << INTERCEPT_VMMCALL) |
  421. (1ULL << INTERCEPT_VMLOAD) |
  422. (1ULL << INTERCEPT_VMSAVE) |
  423. (1ULL << INTERCEPT_STGI) |
  424. (1ULL << INTERCEPT_CLGI) |
  425. (1ULL << INTERCEPT_SKINIT) |
  426. (1ULL << INTERCEPT_WBINVD) |
  427. (1ULL << INTERCEPT_MONITOR) |
  428. (1ULL << INTERCEPT_MWAIT);
  429. control->iopm_base_pa = iopm_base;
  430. control->msrpm_base_pa = __pa(svm->msrpm);
  431. control->tsc_offset = 0;
  432. control->int_ctl = V_INTR_MASKING_MASK;
  433. init_seg(&save->es);
  434. init_seg(&save->ss);
  435. init_seg(&save->ds);
  436. init_seg(&save->fs);
  437. init_seg(&save->gs);
  438. save->cs.selector = 0xf000;
  439. /* Executable/Readable Code Segment */
  440. save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
  441. SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
  442. save->cs.limit = 0xffff;
  443. /*
  444. * cs.base should really be 0xffff0000, but vmx can't handle that, so
  445. * be consistent with it.
  446. *
  447. * Replace when we have real mode working for vmx.
  448. */
  449. save->cs.base = 0xf0000;
  450. save->gdtr.limit = 0xffff;
  451. save->idtr.limit = 0xffff;
  452. init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
  453. init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
  454. save->efer = MSR_EFER_SVME_MASK;
  455. save->dr6 = 0xffff0ff0;
  456. save->dr7 = 0x400;
  457. save->rflags = 2;
  458. save->rip = 0x0000fff0;
  459. /*
  460. * cr0 val on cpu init should be 0x60000010, we enable cpu
  461. * cache by default. the orderly way is to enable cache in bios.
  462. */
  463. save->cr0 = 0x00000010 | X86_CR0_PG | X86_CR0_WP;
  464. save->cr4 = X86_CR4_PAE;
  465. /* rdx = ?? */
  466. if (npt_enabled) {
  467. /* Setup VMCB for Nested Paging */
  468. control->nested_ctl = 1;
  469. control->intercept &= ~(1ULL << INTERCEPT_TASK_SWITCH);
  470. control->intercept_exceptions &= ~(1 << PF_VECTOR);
  471. control->intercept_cr_read &= ~(INTERCEPT_CR0_MASK|
  472. INTERCEPT_CR3_MASK);
  473. control->intercept_cr_write &= ~(INTERCEPT_CR0_MASK|
  474. INTERCEPT_CR3_MASK);
  475. save->g_pat = 0x0007040600070406ULL;
  476. /* enable caching because the QEMU Bios doesn't enable it */
  477. save->cr0 = X86_CR0_ET;
  478. save->cr3 = 0;
  479. save->cr4 = 0;
  480. }
  481. force_new_asid(&svm->vcpu);
  482. }
  483. static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
  484. {
  485. struct vcpu_svm *svm = to_svm(vcpu);
  486. init_vmcb(svm);
  487. if (vcpu->vcpu_id != 0) {
  488. svm->vmcb->save.rip = 0;
  489. svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
  490. svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
  491. }
  492. return 0;
  493. }
  494. static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
  495. {
  496. struct vcpu_svm *svm;
  497. struct page *page;
  498. struct page *msrpm_pages;
  499. int err;
  500. svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  501. if (!svm) {
  502. err = -ENOMEM;
  503. goto out;
  504. }
  505. err = kvm_vcpu_init(&svm->vcpu, kvm, id);
  506. if (err)
  507. goto free_svm;
  508. page = alloc_page(GFP_KERNEL);
  509. if (!page) {
  510. err = -ENOMEM;
  511. goto uninit;
  512. }
  513. err = -ENOMEM;
  514. msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
  515. if (!msrpm_pages)
  516. goto uninit;
  517. svm->msrpm = page_address(msrpm_pages);
  518. svm_vcpu_init_msrpm(svm->msrpm);
  519. svm->vmcb = page_address(page);
  520. clear_page(svm->vmcb);
  521. svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
  522. svm->asid_generation = 0;
  523. memset(svm->db_regs, 0, sizeof(svm->db_regs));
  524. init_vmcb(svm);
  525. fx_init(&svm->vcpu);
  526. svm->vcpu.fpu_active = 1;
  527. svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
  528. if (svm->vcpu.vcpu_id == 0)
  529. svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
  530. return &svm->vcpu;
  531. uninit:
  532. kvm_vcpu_uninit(&svm->vcpu);
  533. free_svm:
  534. kmem_cache_free(kvm_vcpu_cache, svm);
  535. out:
  536. return ERR_PTR(err);
  537. }
  538. static void svm_free_vcpu(struct kvm_vcpu *vcpu)
  539. {
  540. struct vcpu_svm *svm = to_svm(vcpu);
  541. __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
  542. __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
  543. kvm_vcpu_uninit(vcpu);
  544. kmem_cache_free(kvm_vcpu_cache, svm);
  545. }
  546. static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  547. {
  548. struct vcpu_svm *svm = to_svm(vcpu);
  549. int i;
  550. if (unlikely(cpu != vcpu->cpu)) {
  551. u64 tsc_this, delta;
  552. /*
  553. * Make sure that the guest sees a monotonically
  554. * increasing TSC.
  555. */
  556. rdtscll(tsc_this);
  557. delta = vcpu->arch.host_tsc - tsc_this;
  558. svm->vmcb->control.tsc_offset += delta;
  559. vcpu->cpu = cpu;
  560. kvm_migrate_timers(vcpu);
  561. }
  562. for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
  563. rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
  564. }
  565. static void svm_vcpu_put(struct kvm_vcpu *vcpu)
  566. {
  567. struct vcpu_svm *svm = to_svm(vcpu);
  568. int i;
  569. ++vcpu->stat.host_state_reload;
  570. for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
  571. wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
  572. rdtscll(vcpu->arch.host_tsc);
  573. }
  574. static void svm_vcpu_decache(struct kvm_vcpu *vcpu)
  575. {
  576. }
  577. static void svm_cache_regs(struct kvm_vcpu *vcpu)
  578. {
  579. struct vcpu_svm *svm = to_svm(vcpu);
  580. vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
  581. vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
  582. vcpu->arch.rip = svm->vmcb->save.rip;
  583. }
  584. static void svm_decache_regs(struct kvm_vcpu *vcpu)
  585. {
  586. struct vcpu_svm *svm = to_svm(vcpu);
  587. svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
  588. svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
  589. svm->vmcb->save.rip = vcpu->arch.rip;
  590. }
  591. static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
  592. {
  593. return to_svm(vcpu)->vmcb->save.rflags;
  594. }
  595. static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  596. {
  597. to_svm(vcpu)->vmcb->save.rflags = rflags;
  598. }
  599. static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
  600. {
  601. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  602. switch (seg) {
  603. case VCPU_SREG_CS: return &save->cs;
  604. case VCPU_SREG_DS: return &save->ds;
  605. case VCPU_SREG_ES: return &save->es;
  606. case VCPU_SREG_FS: return &save->fs;
  607. case VCPU_SREG_GS: return &save->gs;
  608. case VCPU_SREG_SS: return &save->ss;
  609. case VCPU_SREG_TR: return &save->tr;
  610. case VCPU_SREG_LDTR: return &save->ldtr;
  611. }
  612. BUG();
  613. return NULL;
  614. }
  615. static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  616. {
  617. struct vmcb_seg *s = svm_seg(vcpu, seg);
  618. return s->base;
  619. }
  620. static void svm_get_segment(struct kvm_vcpu *vcpu,
  621. struct kvm_segment *var, int seg)
  622. {
  623. struct vmcb_seg *s = svm_seg(vcpu, seg);
  624. var->base = s->base;
  625. var->limit = s->limit;
  626. var->selector = s->selector;
  627. var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
  628. var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
  629. var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
  630. var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
  631. var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
  632. var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
  633. var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
  634. var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
  635. var->unusable = !var->present;
  636. }
  637. static int svm_get_cpl(struct kvm_vcpu *vcpu)
  638. {
  639. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  640. return save->cpl;
  641. }
  642. static void svm_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  643. {
  644. struct vcpu_svm *svm = to_svm(vcpu);
  645. dt->limit = svm->vmcb->save.idtr.limit;
  646. dt->base = svm->vmcb->save.idtr.base;
  647. }
  648. static void svm_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  649. {
  650. struct vcpu_svm *svm = to_svm(vcpu);
  651. svm->vmcb->save.idtr.limit = dt->limit;
  652. svm->vmcb->save.idtr.base = dt->base ;
  653. }
  654. static void svm_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  655. {
  656. struct vcpu_svm *svm = to_svm(vcpu);
  657. dt->limit = svm->vmcb->save.gdtr.limit;
  658. dt->base = svm->vmcb->save.gdtr.base;
  659. }
  660. static void svm_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  661. {
  662. struct vcpu_svm *svm = to_svm(vcpu);
  663. svm->vmcb->save.gdtr.limit = dt->limit;
  664. svm->vmcb->save.gdtr.base = dt->base ;
  665. }
  666. static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  667. {
  668. }
  669. static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  670. {
  671. struct vcpu_svm *svm = to_svm(vcpu);
  672. #ifdef CONFIG_X86_64
  673. if (vcpu->arch.shadow_efer & EFER_LME) {
  674. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
  675. vcpu->arch.shadow_efer |= EFER_LMA;
  676. svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
  677. }
  678. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
  679. vcpu->arch.shadow_efer &= ~EFER_LMA;
  680. svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
  681. }
  682. }
  683. #endif
  684. if (npt_enabled)
  685. goto set;
  686. if ((vcpu->arch.cr0 & X86_CR0_TS) && !(cr0 & X86_CR0_TS)) {
  687. svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
  688. vcpu->fpu_active = 1;
  689. }
  690. vcpu->arch.cr0 = cr0;
  691. cr0 |= X86_CR0_PG | X86_CR0_WP;
  692. if (!vcpu->fpu_active) {
  693. svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
  694. cr0 |= X86_CR0_TS;
  695. }
  696. set:
  697. /*
  698. * re-enable caching here because the QEMU bios
  699. * does not do it - this results in some delay at
  700. * reboot
  701. */
  702. cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
  703. svm->vmcb->save.cr0 = cr0;
  704. }
  705. static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  706. {
  707. unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
  708. vcpu->arch.cr4 = cr4;
  709. if (!npt_enabled)
  710. cr4 |= X86_CR4_PAE;
  711. cr4 |= host_cr4_mce;
  712. to_svm(vcpu)->vmcb->save.cr4 = cr4;
  713. }
  714. static void svm_set_segment(struct kvm_vcpu *vcpu,
  715. struct kvm_segment *var, int seg)
  716. {
  717. struct vcpu_svm *svm = to_svm(vcpu);
  718. struct vmcb_seg *s = svm_seg(vcpu, seg);
  719. s->base = var->base;
  720. s->limit = var->limit;
  721. s->selector = var->selector;
  722. if (var->unusable)
  723. s->attrib = 0;
  724. else {
  725. s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
  726. s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
  727. s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
  728. s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
  729. s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
  730. s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
  731. s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
  732. s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
  733. }
  734. if (seg == VCPU_SREG_CS)
  735. svm->vmcb->save.cpl
  736. = (svm->vmcb->save.cs.attrib
  737. >> SVM_SELECTOR_DPL_SHIFT) & 3;
  738. }
  739. static int svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
  740. {
  741. return -EOPNOTSUPP;
  742. }
  743. static int svm_get_irq(struct kvm_vcpu *vcpu)
  744. {
  745. struct vcpu_svm *svm = to_svm(vcpu);
  746. u32 exit_int_info = svm->vmcb->control.exit_int_info;
  747. if (is_external_interrupt(exit_int_info))
  748. return exit_int_info & SVM_EVTINJ_VEC_MASK;
  749. return -1;
  750. }
  751. static void load_host_msrs(struct kvm_vcpu *vcpu)
  752. {
  753. #ifdef CONFIG_X86_64
  754. wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
  755. #endif
  756. }
  757. static void save_host_msrs(struct kvm_vcpu *vcpu)
  758. {
  759. #ifdef CONFIG_X86_64
  760. rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
  761. #endif
  762. }
  763. static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *svm_data)
  764. {
  765. if (svm_data->next_asid > svm_data->max_asid) {
  766. ++svm_data->asid_generation;
  767. svm_data->next_asid = 1;
  768. svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
  769. }
  770. svm->vcpu.cpu = svm_data->cpu;
  771. svm->asid_generation = svm_data->asid_generation;
  772. svm->vmcb->control.asid = svm_data->next_asid++;
  773. }
  774. static unsigned long svm_get_dr(struct kvm_vcpu *vcpu, int dr)
  775. {
  776. unsigned long val = to_svm(vcpu)->db_regs[dr];
  777. KVMTRACE_2D(DR_READ, vcpu, (u32)dr, (u32)val, handler);
  778. return val;
  779. }
  780. static void svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value,
  781. int *exception)
  782. {
  783. struct vcpu_svm *svm = to_svm(vcpu);
  784. *exception = 0;
  785. if (svm->vmcb->save.dr7 & DR7_GD_MASK) {
  786. svm->vmcb->save.dr7 &= ~DR7_GD_MASK;
  787. svm->vmcb->save.dr6 |= DR6_BD_MASK;
  788. *exception = DB_VECTOR;
  789. return;
  790. }
  791. switch (dr) {
  792. case 0 ... 3:
  793. svm->db_regs[dr] = value;
  794. return;
  795. case 4 ... 5:
  796. if (vcpu->arch.cr4 & X86_CR4_DE) {
  797. *exception = UD_VECTOR;
  798. return;
  799. }
  800. case 7: {
  801. if (value & ~((1ULL << 32) - 1)) {
  802. *exception = GP_VECTOR;
  803. return;
  804. }
  805. svm->vmcb->save.dr7 = value;
  806. return;
  807. }
  808. default:
  809. printk(KERN_DEBUG "%s: unexpected dr %u\n",
  810. __func__, dr);
  811. *exception = UD_VECTOR;
  812. return;
  813. }
  814. }
  815. static int pf_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  816. {
  817. u32 exit_int_info = svm->vmcb->control.exit_int_info;
  818. struct kvm *kvm = svm->vcpu.kvm;
  819. u64 fault_address;
  820. u32 error_code;
  821. if (!irqchip_in_kernel(kvm) &&
  822. is_external_interrupt(exit_int_info))
  823. push_irq(&svm->vcpu, exit_int_info & SVM_EVTINJ_VEC_MASK);
  824. fault_address = svm->vmcb->control.exit_info_2;
  825. error_code = svm->vmcb->control.exit_info_1;
  826. if (!npt_enabled)
  827. KVMTRACE_3D(PAGE_FAULT, &svm->vcpu, error_code,
  828. (u32)fault_address, (u32)(fault_address >> 32),
  829. handler);
  830. else
  831. KVMTRACE_3D(TDP_FAULT, &svm->vcpu, error_code,
  832. (u32)fault_address, (u32)(fault_address >> 32),
  833. handler);
  834. return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
  835. }
  836. static int ud_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  837. {
  838. int er;
  839. er = emulate_instruction(&svm->vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
  840. if (er != EMULATE_DONE)
  841. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  842. return 1;
  843. }
  844. static int nm_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  845. {
  846. svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
  847. if (!(svm->vcpu.arch.cr0 & X86_CR0_TS))
  848. svm->vmcb->save.cr0 &= ~X86_CR0_TS;
  849. svm->vcpu.fpu_active = 1;
  850. return 1;
  851. }
  852. static int mc_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  853. {
  854. /*
  855. * On an #MC intercept the MCE handler is not called automatically in
  856. * the host. So do it by hand here.
  857. */
  858. asm volatile (
  859. "int $0x12\n");
  860. /* not sure if we ever come back to this point */
  861. return 1;
  862. }
  863. static int shutdown_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  864. {
  865. /*
  866. * VMCB is undefined after a SHUTDOWN intercept
  867. * so reinitialize it.
  868. */
  869. clear_page(svm->vmcb);
  870. init_vmcb(svm);
  871. kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
  872. return 0;
  873. }
  874. static int io_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  875. {
  876. u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
  877. int size, down, in, string, rep;
  878. unsigned port;
  879. ++svm->vcpu.stat.io_exits;
  880. svm->next_rip = svm->vmcb->control.exit_info_2;
  881. string = (io_info & SVM_IOIO_STR_MASK) != 0;
  882. if (string) {
  883. if (emulate_instruction(&svm->vcpu,
  884. kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
  885. return 0;
  886. return 1;
  887. }
  888. in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
  889. port = io_info >> 16;
  890. size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
  891. rep = (io_info & SVM_IOIO_REP_MASK) != 0;
  892. down = (svm->vmcb->save.rflags & X86_EFLAGS_DF) != 0;
  893. return kvm_emulate_pio(&svm->vcpu, kvm_run, in, size, port);
  894. }
  895. static int nmi_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  896. {
  897. KVMTRACE_0D(NMI, &svm->vcpu, handler);
  898. return 1;
  899. }
  900. static int intr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  901. {
  902. ++svm->vcpu.stat.irq_exits;
  903. KVMTRACE_0D(INTR, &svm->vcpu, handler);
  904. return 1;
  905. }
  906. static int nop_on_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  907. {
  908. return 1;
  909. }
  910. static int halt_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  911. {
  912. svm->next_rip = svm->vmcb->save.rip + 1;
  913. skip_emulated_instruction(&svm->vcpu);
  914. return kvm_emulate_halt(&svm->vcpu);
  915. }
  916. static int vmmcall_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  917. {
  918. svm->next_rip = svm->vmcb->save.rip + 3;
  919. skip_emulated_instruction(&svm->vcpu);
  920. kvm_emulate_hypercall(&svm->vcpu);
  921. return 1;
  922. }
  923. static int invalid_op_interception(struct vcpu_svm *svm,
  924. struct kvm_run *kvm_run)
  925. {
  926. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  927. return 1;
  928. }
  929. static int task_switch_interception(struct vcpu_svm *svm,
  930. struct kvm_run *kvm_run)
  931. {
  932. u16 tss_selector;
  933. tss_selector = (u16)svm->vmcb->control.exit_info_1;
  934. if (svm->vmcb->control.exit_info_2 &
  935. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
  936. return kvm_task_switch(&svm->vcpu, tss_selector,
  937. TASK_SWITCH_IRET);
  938. if (svm->vmcb->control.exit_info_2 &
  939. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
  940. return kvm_task_switch(&svm->vcpu, tss_selector,
  941. TASK_SWITCH_JMP);
  942. return kvm_task_switch(&svm->vcpu, tss_selector, TASK_SWITCH_CALL);
  943. }
  944. static int cpuid_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  945. {
  946. svm->next_rip = svm->vmcb->save.rip + 2;
  947. kvm_emulate_cpuid(&svm->vcpu);
  948. return 1;
  949. }
  950. static int emulate_on_interception(struct vcpu_svm *svm,
  951. struct kvm_run *kvm_run)
  952. {
  953. if (emulate_instruction(&svm->vcpu, NULL, 0, 0, 0) != EMULATE_DONE)
  954. pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
  955. return 1;
  956. }
  957. static int cr8_write_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  958. {
  959. emulate_instruction(&svm->vcpu, NULL, 0, 0, 0);
  960. if (irqchip_in_kernel(svm->vcpu.kvm))
  961. return 1;
  962. kvm_run->exit_reason = KVM_EXIT_SET_TPR;
  963. return 0;
  964. }
  965. static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
  966. {
  967. struct vcpu_svm *svm = to_svm(vcpu);
  968. switch (ecx) {
  969. case MSR_IA32_TIME_STAMP_COUNTER: {
  970. u64 tsc;
  971. rdtscll(tsc);
  972. *data = svm->vmcb->control.tsc_offset + tsc;
  973. break;
  974. }
  975. case MSR_K6_STAR:
  976. *data = svm->vmcb->save.star;
  977. break;
  978. #ifdef CONFIG_X86_64
  979. case MSR_LSTAR:
  980. *data = svm->vmcb->save.lstar;
  981. break;
  982. case MSR_CSTAR:
  983. *data = svm->vmcb->save.cstar;
  984. break;
  985. case MSR_KERNEL_GS_BASE:
  986. *data = svm->vmcb->save.kernel_gs_base;
  987. break;
  988. case MSR_SYSCALL_MASK:
  989. *data = svm->vmcb->save.sfmask;
  990. break;
  991. #endif
  992. case MSR_IA32_SYSENTER_CS:
  993. *data = svm->vmcb->save.sysenter_cs;
  994. break;
  995. case MSR_IA32_SYSENTER_EIP:
  996. *data = svm->vmcb->save.sysenter_eip;
  997. break;
  998. case MSR_IA32_SYSENTER_ESP:
  999. *data = svm->vmcb->save.sysenter_esp;
  1000. break;
  1001. /* Nobody will change the following 5 values in the VMCB so
  1002. we can safely return them on rdmsr. They will always be 0
  1003. until LBRV is implemented. */
  1004. case MSR_IA32_DEBUGCTLMSR:
  1005. *data = svm->vmcb->save.dbgctl;
  1006. break;
  1007. case MSR_IA32_LASTBRANCHFROMIP:
  1008. *data = svm->vmcb->save.br_from;
  1009. break;
  1010. case MSR_IA32_LASTBRANCHTOIP:
  1011. *data = svm->vmcb->save.br_to;
  1012. break;
  1013. case MSR_IA32_LASTINTFROMIP:
  1014. *data = svm->vmcb->save.last_excp_from;
  1015. break;
  1016. case MSR_IA32_LASTINTTOIP:
  1017. *data = svm->vmcb->save.last_excp_to;
  1018. break;
  1019. default:
  1020. return kvm_get_msr_common(vcpu, ecx, data);
  1021. }
  1022. return 0;
  1023. }
  1024. static int rdmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1025. {
  1026. u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  1027. u64 data;
  1028. if (svm_get_msr(&svm->vcpu, ecx, &data))
  1029. kvm_inject_gp(&svm->vcpu, 0);
  1030. else {
  1031. KVMTRACE_3D(MSR_READ, &svm->vcpu, ecx, (u32)data,
  1032. (u32)(data >> 32), handler);
  1033. svm->vmcb->save.rax = data & 0xffffffff;
  1034. svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
  1035. svm->next_rip = svm->vmcb->save.rip + 2;
  1036. skip_emulated_instruction(&svm->vcpu);
  1037. }
  1038. return 1;
  1039. }
  1040. static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
  1041. {
  1042. struct vcpu_svm *svm = to_svm(vcpu);
  1043. switch (ecx) {
  1044. case MSR_IA32_TIME_STAMP_COUNTER: {
  1045. u64 tsc;
  1046. rdtscll(tsc);
  1047. svm->vmcb->control.tsc_offset = data - tsc;
  1048. break;
  1049. }
  1050. case MSR_K6_STAR:
  1051. svm->vmcb->save.star = data;
  1052. break;
  1053. #ifdef CONFIG_X86_64
  1054. case MSR_LSTAR:
  1055. svm->vmcb->save.lstar = data;
  1056. break;
  1057. case MSR_CSTAR:
  1058. svm->vmcb->save.cstar = data;
  1059. break;
  1060. case MSR_KERNEL_GS_BASE:
  1061. svm->vmcb->save.kernel_gs_base = data;
  1062. break;
  1063. case MSR_SYSCALL_MASK:
  1064. svm->vmcb->save.sfmask = data;
  1065. break;
  1066. #endif
  1067. case MSR_IA32_SYSENTER_CS:
  1068. svm->vmcb->save.sysenter_cs = data;
  1069. break;
  1070. case MSR_IA32_SYSENTER_EIP:
  1071. svm->vmcb->save.sysenter_eip = data;
  1072. break;
  1073. case MSR_IA32_SYSENTER_ESP:
  1074. svm->vmcb->save.sysenter_esp = data;
  1075. break;
  1076. case MSR_IA32_DEBUGCTLMSR:
  1077. if (!svm_has(SVM_FEATURE_LBRV)) {
  1078. pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
  1079. __func__, data);
  1080. break;
  1081. }
  1082. if (data & DEBUGCTL_RESERVED_BITS)
  1083. return 1;
  1084. svm->vmcb->save.dbgctl = data;
  1085. if (data & (1ULL<<0))
  1086. svm_enable_lbrv(svm);
  1087. else
  1088. svm_disable_lbrv(svm);
  1089. break;
  1090. case MSR_K7_EVNTSEL0:
  1091. case MSR_K7_EVNTSEL1:
  1092. case MSR_K7_EVNTSEL2:
  1093. case MSR_K7_EVNTSEL3:
  1094. case MSR_K7_PERFCTR0:
  1095. case MSR_K7_PERFCTR1:
  1096. case MSR_K7_PERFCTR2:
  1097. case MSR_K7_PERFCTR3:
  1098. /*
  1099. * Just discard all writes to the performance counters; this
  1100. * should keep both older linux and windows 64-bit guests
  1101. * happy
  1102. */
  1103. pr_unimpl(vcpu, "unimplemented perfctr wrmsr: 0x%x data 0x%llx\n", ecx, data);
  1104. break;
  1105. default:
  1106. return kvm_set_msr_common(vcpu, ecx, data);
  1107. }
  1108. return 0;
  1109. }
  1110. static int wrmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1111. {
  1112. u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  1113. u64 data = (svm->vmcb->save.rax & -1u)
  1114. | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  1115. KVMTRACE_3D(MSR_WRITE, &svm->vcpu, ecx, (u32)data, (u32)(data >> 32),
  1116. handler);
  1117. svm->next_rip = svm->vmcb->save.rip + 2;
  1118. if (svm_set_msr(&svm->vcpu, ecx, data))
  1119. kvm_inject_gp(&svm->vcpu, 0);
  1120. else
  1121. skip_emulated_instruction(&svm->vcpu);
  1122. return 1;
  1123. }
  1124. static int msr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1125. {
  1126. if (svm->vmcb->control.exit_info_1)
  1127. return wrmsr_interception(svm, kvm_run);
  1128. else
  1129. return rdmsr_interception(svm, kvm_run);
  1130. }
  1131. static int interrupt_window_interception(struct vcpu_svm *svm,
  1132. struct kvm_run *kvm_run)
  1133. {
  1134. KVMTRACE_0D(PEND_INTR, &svm->vcpu, handler);
  1135. svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
  1136. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  1137. /*
  1138. * If the user space waits to inject interrupts, exit as soon as
  1139. * possible
  1140. */
  1141. if (kvm_run->request_interrupt_window &&
  1142. !svm->vcpu.arch.irq_summary) {
  1143. ++svm->vcpu.stat.irq_window_exits;
  1144. kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  1145. return 0;
  1146. }
  1147. return 1;
  1148. }
  1149. static int (*svm_exit_handlers[])(struct vcpu_svm *svm,
  1150. struct kvm_run *kvm_run) = {
  1151. [SVM_EXIT_READ_CR0] = emulate_on_interception,
  1152. [SVM_EXIT_READ_CR3] = emulate_on_interception,
  1153. [SVM_EXIT_READ_CR4] = emulate_on_interception,
  1154. [SVM_EXIT_READ_CR8] = emulate_on_interception,
  1155. /* for now: */
  1156. [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
  1157. [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
  1158. [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
  1159. [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
  1160. [SVM_EXIT_READ_DR0] = emulate_on_interception,
  1161. [SVM_EXIT_READ_DR1] = emulate_on_interception,
  1162. [SVM_EXIT_READ_DR2] = emulate_on_interception,
  1163. [SVM_EXIT_READ_DR3] = emulate_on_interception,
  1164. [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
  1165. [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
  1166. [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
  1167. [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
  1168. [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
  1169. [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
  1170. [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
  1171. [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
  1172. [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
  1173. [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
  1174. [SVM_EXIT_INTR] = intr_interception,
  1175. [SVM_EXIT_NMI] = nmi_interception,
  1176. [SVM_EXIT_SMI] = nop_on_interception,
  1177. [SVM_EXIT_INIT] = nop_on_interception,
  1178. [SVM_EXIT_VINTR] = interrupt_window_interception,
  1179. /* [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, */
  1180. [SVM_EXIT_CPUID] = cpuid_interception,
  1181. [SVM_EXIT_INVD] = emulate_on_interception,
  1182. [SVM_EXIT_HLT] = halt_interception,
  1183. [SVM_EXIT_INVLPG] = emulate_on_interception,
  1184. [SVM_EXIT_INVLPGA] = invalid_op_interception,
  1185. [SVM_EXIT_IOIO] = io_interception,
  1186. [SVM_EXIT_MSR] = msr_interception,
  1187. [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
  1188. [SVM_EXIT_SHUTDOWN] = shutdown_interception,
  1189. [SVM_EXIT_VMRUN] = invalid_op_interception,
  1190. [SVM_EXIT_VMMCALL] = vmmcall_interception,
  1191. [SVM_EXIT_VMLOAD] = invalid_op_interception,
  1192. [SVM_EXIT_VMSAVE] = invalid_op_interception,
  1193. [SVM_EXIT_STGI] = invalid_op_interception,
  1194. [SVM_EXIT_CLGI] = invalid_op_interception,
  1195. [SVM_EXIT_SKINIT] = invalid_op_interception,
  1196. [SVM_EXIT_WBINVD] = emulate_on_interception,
  1197. [SVM_EXIT_MONITOR] = invalid_op_interception,
  1198. [SVM_EXIT_MWAIT] = invalid_op_interception,
  1199. [SVM_EXIT_NPF] = pf_interception,
  1200. };
  1201. static int handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  1202. {
  1203. struct vcpu_svm *svm = to_svm(vcpu);
  1204. u32 exit_code = svm->vmcb->control.exit_code;
  1205. KVMTRACE_3D(VMEXIT, vcpu, exit_code, (u32)svm->vmcb->save.rip,
  1206. (u32)((u64)svm->vmcb->save.rip >> 32), entryexit);
  1207. if (npt_enabled) {
  1208. int mmu_reload = 0;
  1209. if ((vcpu->arch.cr0 ^ svm->vmcb->save.cr0) & X86_CR0_PG) {
  1210. svm_set_cr0(vcpu, svm->vmcb->save.cr0);
  1211. mmu_reload = 1;
  1212. }
  1213. vcpu->arch.cr0 = svm->vmcb->save.cr0;
  1214. vcpu->arch.cr3 = svm->vmcb->save.cr3;
  1215. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  1216. if (!load_pdptrs(vcpu, vcpu->arch.cr3)) {
  1217. kvm_inject_gp(vcpu, 0);
  1218. return 1;
  1219. }
  1220. }
  1221. if (mmu_reload) {
  1222. kvm_mmu_reset_context(vcpu);
  1223. kvm_mmu_load(vcpu);
  1224. }
  1225. }
  1226. kvm_reput_irq(svm);
  1227. if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
  1228. kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  1229. kvm_run->fail_entry.hardware_entry_failure_reason
  1230. = svm->vmcb->control.exit_code;
  1231. return 0;
  1232. }
  1233. if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
  1234. exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
  1235. exit_code != SVM_EXIT_NPF)
  1236. printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
  1237. "exit_code 0x%x\n",
  1238. __func__, svm->vmcb->control.exit_int_info,
  1239. exit_code);
  1240. if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
  1241. || !svm_exit_handlers[exit_code]) {
  1242. kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
  1243. kvm_run->hw.hardware_exit_reason = exit_code;
  1244. return 0;
  1245. }
  1246. return svm_exit_handlers[exit_code](svm, kvm_run);
  1247. }
  1248. static void reload_tss(struct kvm_vcpu *vcpu)
  1249. {
  1250. int cpu = raw_smp_processor_id();
  1251. struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
  1252. svm_data->tss_desc->type = 9; /* available 32/64-bit TSS */
  1253. load_TR_desc();
  1254. }
  1255. static void pre_svm_run(struct vcpu_svm *svm)
  1256. {
  1257. int cpu = raw_smp_processor_id();
  1258. struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
  1259. svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
  1260. if (svm->vcpu.cpu != cpu ||
  1261. svm->asid_generation != svm_data->asid_generation)
  1262. new_asid(svm, svm_data);
  1263. }
  1264. static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
  1265. {
  1266. struct vmcb_control_area *control;
  1267. KVMTRACE_1D(INJ_VIRQ, &svm->vcpu, (u32)irq, handler);
  1268. control = &svm->vmcb->control;
  1269. control->int_vector = irq;
  1270. control->int_ctl &= ~V_INTR_PRIO_MASK;
  1271. control->int_ctl |= V_IRQ_MASK |
  1272. ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
  1273. }
  1274. static void svm_set_irq(struct kvm_vcpu *vcpu, int irq)
  1275. {
  1276. struct vcpu_svm *svm = to_svm(vcpu);
  1277. svm_inject_irq(svm, irq);
  1278. }
  1279. static void update_cr8_intercept(struct kvm_vcpu *vcpu)
  1280. {
  1281. struct vcpu_svm *svm = to_svm(vcpu);
  1282. struct vmcb *vmcb = svm->vmcb;
  1283. int max_irr, tpr;
  1284. if (!irqchip_in_kernel(vcpu->kvm) || vcpu->arch.apic->vapic_addr)
  1285. return;
  1286. vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
  1287. max_irr = kvm_lapic_find_highest_irr(vcpu);
  1288. if (max_irr == -1)
  1289. return;
  1290. tpr = kvm_lapic_get_cr8(vcpu) << 4;
  1291. if (tpr >= (max_irr & 0xf0))
  1292. vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK;
  1293. }
  1294. static void svm_intr_assist(struct kvm_vcpu *vcpu)
  1295. {
  1296. struct vcpu_svm *svm = to_svm(vcpu);
  1297. struct vmcb *vmcb = svm->vmcb;
  1298. int intr_vector = -1;
  1299. if ((vmcb->control.exit_int_info & SVM_EVTINJ_VALID) &&
  1300. ((vmcb->control.exit_int_info & SVM_EVTINJ_TYPE_MASK) == 0)) {
  1301. intr_vector = vmcb->control.exit_int_info &
  1302. SVM_EVTINJ_VEC_MASK;
  1303. vmcb->control.exit_int_info = 0;
  1304. svm_inject_irq(svm, intr_vector);
  1305. goto out;
  1306. }
  1307. if (vmcb->control.int_ctl & V_IRQ_MASK)
  1308. goto out;
  1309. if (!kvm_cpu_has_interrupt(vcpu))
  1310. goto out;
  1311. if (!(vmcb->save.rflags & X86_EFLAGS_IF) ||
  1312. (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) ||
  1313. (vmcb->control.event_inj & SVM_EVTINJ_VALID)) {
  1314. /* unable to deliver irq, set pending irq */
  1315. vmcb->control.intercept |= (1ULL << INTERCEPT_VINTR);
  1316. svm_inject_irq(svm, 0x0);
  1317. goto out;
  1318. }
  1319. /* Okay, we can deliver the interrupt: grab it and update PIC state. */
  1320. intr_vector = kvm_cpu_get_interrupt(vcpu);
  1321. svm_inject_irq(svm, intr_vector);
  1322. kvm_timer_intr_post(vcpu, intr_vector);
  1323. out:
  1324. update_cr8_intercept(vcpu);
  1325. }
  1326. static void kvm_reput_irq(struct vcpu_svm *svm)
  1327. {
  1328. struct vmcb_control_area *control = &svm->vmcb->control;
  1329. if ((control->int_ctl & V_IRQ_MASK)
  1330. && !irqchip_in_kernel(svm->vcpu.kvm)) {
  1331. control->int_ctl &= ~V_IRQ_MASK;
  1332. push_irq(&svm->vcpu, control->int_vector);
  1333. }
  1334. svm->vcpu.arch.interrupt_window_open =
  1335. !(control->int_state & SVM_INTERRUPT_SHADOW_MASK);
  1336. }
  1337. static void svm_do_inject_vector(struct vcpu_svm *svm)
  1338. {
  1339. struct kvm_vcpu *vcpu = &svm->vcpu;
  1340. int word_index = __ffs(vcpu->arch.irq_summary);
  1341. int bit_index = __ffs(vcpu->arch.irq_pending[word_index]);
  1342. int irq = word_index * BITS_PER_LONG + bit_index;
  1343. clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]);
  1344. if (!vcpu->arch.irq_pending[word_index])
  1345. clear_bit(word_index, &vcpu->arch.irq_summary);
  1346. svm_inject_irq(svm, irq);
  1347. }
  1348. static void do_interrupt_requests(struct kvm_vcpu *vcpu,
  1349. struct kvm_run *kvm_run)
  1350. {
  1351. struct vcpu_svm *svm = to_svm(vcpu);
  1352. struct vmcb_control_area *control = &svm->vmcb->control;
  1353. svm->vcpu.arch.interrupt_window_open =
  1354. (!(control->int_state & SVM_INTERRUPT_SHADOW_MASK) &&
  1355. (svm->vmcb->save.rflags & X86_EFLAGS_IF));
  1356. if (svm->vcpu.arch.interrupt_window_open && svm->vcpu.arch.irq_summary)
  1357. /*
  1358. * If interrupts enabled, and not blocked by sti or mov ss. Good.
  1359. */
  1360. svm_do_inject_vector(svm);
  1361. /*
  1362. * Interrupts blocked. Wait for unblock.
  1363. */
  1364. if (!svm->vcpu.arch.interrupt_window_open &&
  1365. (svm->vcpu.arch.irq_summary || kvm_run->request_interrupt_window))
  1366. control->intercept |= 1ULL << INTERCEPT_VINTR;
  1367. else
  1368. control->intercept &= ~(1ULL << INTERCEPT_VINTR);
  1369. }
  1370. static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
  1371. {
  1372. return 0;
  1373. }
  1374. static void save_db_regs(unsigned long *db_regs)
  1375. {
  1376. asm volatile ("mov %%dr0, %0" : "=r"(db_regs[0]));
  1377. asm volatile ("mov %%dr1, %0" : "=r"(db_regs[1]));
  1378. asm volatile ("mov %%dr2, %0" : "=r"(db_regs[2]));
  1379. asm volatile ("mov %%dr3, %0" : "=r"(db_regs[3]));
  1380. }
  1381. static void load_db_regs(unsigned long *db_regs)
  1382. {
  1383. asm volatile ("mov %0, %%dr0" : : "r"(db_regs[0]));
  1384. asm volatile ("mov %0, %%dr1" : : "r"(db_regs[1]));
  1385. asm volatile ("mov %0, %%dr2" : : "r"(db_regs[2]));
  1386. asm volatile ("mov %0, %%dr3" : : "r"(db_regs[3]));
  1387. }
  1388. static void svm_flush_tlb(struct kvm_vcpu *vcpu)
  1389. {
  1390. force_new_asid(vcpu);
  1391. }
  1392. static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
  1393. {
  1394. }
  1395. static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
  1396. {
  1397. struct vcpu_svm *svm = to_svm(vcpu);
  1398. if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) {
  1399. int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
  1400. kvm_lapic_set_tpr(vcpu, cr8);
  1401. }
  1402. }
  1403. static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
  1404. {
  1405. struct vcpu_svm *svm = to_svm(vcpu);
  1406. u64 cr8;
  1407. if (!irqchip_in_kernel(vcpu->kvm))
  1408. return;
  1409. cr8 = kvm_get_cr8(vcpu);
  1410. svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
  1411. svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
  1412. }
  1413. static void svm_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  1414. {
  1415. struct vcpu_svm *svm = to_svm(vcpu);
  1416. u16 fs_selector;
  1417. u16 gs_selector;
  1418. u16 ldt_selector;
  1419. pre_svm_run(svm);
  1420. sync_lapic_to_cr8(vcpu);
  1421. save_host_msrs(vcpu);
  1422. fs_selector = read_fs();
  1423. gs_selector = read_gs();
  1424. ldt_selector = read_ldt();
  1425. svm->host_cr2 = kvm_read_cr2();
  1426. svm->host_dr6 = read_dr6();
  1427. svm->host_dr7 = read_dr7();
  1428. svm->vmcb->save.cr2 = vcpu->arch.cr2;
  1429. /* required for live migration with NPT */
  1430. if (npt_enabled)
  1431. svm->vmcb->save.cr3 = vcpu->arch.cr3;
  1432. if (svm->vmcb->save.dr7 & 0xff) {
  1433. write_dr7(0);
  1434. save_db_regs(svm->host_db_regs);
  1435. load_db_regs(svm->db_regs);
  1436. }
  1437. clgi();
  1438. local_irq_enable();
  1439. asm volatile (
  1440. #ifdef CONFIG_X86_64
  1441. "push %%rbp; \n\t"
  1442. #else
  1443. "push %%ebp; \n\t"
  1444. #endif
  1445. #ifdef CONFIG_X86_64
  1446. "mov %c[rbx](%[svm]), %%rbx \n\t"
  1447. "mov %c[rcx](%[svm]), %%rcx \n\t"
  1448. "mov %c[rdx](%[svm]), %%rdx \n\t"
  1449. "mov %c[rsi](%[svm]), %%rsi \n\t"
  1450. "mov %c[rdi](%[svm]), %%rdi \n\t"
  1451. "mov %c[rbp](%[svm]), %%rbp \n\t"
  1452. "mov %c[r8](%[svm]), %%r8 \n\t"
  1453. "mov %c[r9](%[svm]), %%r9 \n\t"
  1454. "mov %c[r10](%[svm]), %%r10 \n\t"
  1455. "mov %c[r11](%[svm]), %%r11 \n\t"
  1456. "mov %c[r12](%[svm]), %%r12 \n\t"
  1457. "mov %c[r13](%[svm]), %%r13 \n\t"
  1458. "mov %c[r14](%[svm]), %%r14 \n\t"
  1459. "mov %c[r15](%[svm]), %%r15 \n\t"
  1460. #else
  1461. "mov %c[rbx](%[svm]), %%ebx \n\t"
  1462. "mov %c[rcx](%[svm]), %%ecx \n\t"
  1463. "mov %c[rdx](%[svm]), %%edx \n\t"
  1464. "mov %c[rsi](%[svm]), %%esi \n\t"
  1465. "mov %c[rdi](%[svm]), %%edi \n\t"
  1466. "mov %c[rbp](%[svm]), %%ebp \n\t"
  1467. #endif
  1468. #ifdef CONFIG_X86_64
  1469. /* Enter guest mode */
  1470. "push %%rax \n\t"
  1471. "mov %c[vmcb](%[svm]), %%rax \n\t"
  1472. __ex(SVM_VMLOAD) "\n\t"
  1473. __ex(SVM_VMRUN) "\n\t"
  1474. __ex(SVM_VMSAVE) "\n\t"
  1475. "pop %%rax \n\t"
  1476. #else
  1477. /* Enter guest mode */
  1478. "push %%eax \n\t"
  1479. "mov %c[vmcb](%[svm]), %%eax \n\t"
  1480. __ex(SVM_VMLOAD) "\n\t"
  1481. __ex(SVM_VMRUN) "\n\t"
  1482. __ex(SVM_VMSAVE) "\n\t"
  1483. "pop %%eax \n\t"
  1484. #endif
  1485. /* Save guest registers, load host registers */
  1486. #ifdef CONFIG_X86_64
  1487. "mov %%rbx, %c[rbx](%[svm]) \n\t"
  1488. "mov %%rcx, %c[rcx](%[svm]) \n\t"
  1489. "mov %%rdx, %c[rdx](%[svm]) \n\t"
  1490. "mov %%rsi, %c[rsi](%[svm]) \n\t"
  1491. "mov %%rdi, %c[rdi](%[svm]) \n\t"
  1492. "mov %%rbp, %c[rbp](%[svm]) \n\t"
  1493. "mov %%r8, %c[r8](%[svm]) \n\t"
  1494. "mov %%r9, %c[r9](%[svm]) \n\t"
  1495. "mov %%r10, %c[r10](%[svm]) \n\t"
  1496. "mov %%r11, %c[r11](%[svm]) \n\t"
  1497. "mov %%r12, %c[r12](%[svm]) \n\t"
  1498. "mov %%r13, %c[r13](%[svm]) \n\t"
  1499. "mov %%r14, %c[r14](%[svm]) \n\t"
  1500. "mov %%r15, %c[r15](%[svm]) \n\t"
  1501. "pop %%rbp; \n\t"
  1502. #else
  1503. "mov %%ebx, %c[rbx](%[svm]) \n\t"
  1504. "mov %%ecx, %c[rcx](%[svm]) \n\t"
  1505. "mov %%edx, %c[rdx](%[svm]) \n\t"
  1506. "mov %%esi, %c[rsi](%[svm]) \n\t"
  1507. "mov %%edi, %c[rdi](%[svm]) \n\t"
  1508. "mov %%ebp, %c[rbp](%[svm]) \n\t"
  1509. "pop %%ebp; \n\t"
  1510. #endif
  1511. :
  1512. : [svm]"a"(svm),
  1513. [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
  1514. [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
  1515. [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
  1516. [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
  1517. [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
  1518. [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
  1519. [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
  1520. #ifdef CONFIG_X86_64
  1521. , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
  1522. [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
  1523. [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
  1524. [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
  1525. [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
  1526. [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
  1527. [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
  1528. [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
  1529. #endif
  1530. : "cc", "memory"
  1531. #ifdef CONFIG_X86_64
  1532. , "rbx", "rcx", "rdx", "rsi", "rdi"
  1533. , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
  1534. #else
  1535. , "ebx", "ecx", "edx" , "esi", "edi"
  1536. #endif
  1537. );
  1538. if ((svm->vmcb->save.dr7 & 0xff))
  1539. load_db_regs(svm->host_db_regs);
  1540. vcpu->arch.cr2 = svm->vmcb->save.cr2;
  1541. write_dr6(svm->host_dr6);
  1542. write_dr7(svm->host_dr7);
  1543. kvm_write_cr2(svm->host_cr2);
  1544. load_fs(fs_selector);
  1545. load_gs(gs_selector);
  1546. load_ldt(ldt_selector);
  1547. load_host_msrs(vcpu);
  1548. reload_tss(vcpu);
  1549. local_irq_disable();
  1550. stgi();
  1551. sync_cr8_to_lapic(vcpu);
  1552. svm->next_rip = 0;
  1553. }
  1554. static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
  1555. {
  1556. struct vcpu_svm *svm = to_svm(vcpu);
  1557. if (npt_enabled) {
  1558. svm->vmcb->control.nested_cr3 = root;
  1559. force_new_asid(vcpu);
  1560. return;
  1561. }
  1562. svm->vmcb->save.cr3 = root;
  1563. force_new_asid(vcpu);
  1564. if (vcpu->fpu_active) {
  1565. svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
  1566. svm->vmcb->save.cr0 |= X86_CR0_TS;
  1567. vcpu->fpu_active = 0;
  1568. }
  1569. }
  1570. static int is_disabled(void)
  1571. {
  1572. u64 vm_cr;
  1573. rdmsrl(MSR_VM_CR, vm_cr);
  1574. if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
  1575. return 1;
  1576. return 0;
  1577. }
  1578. static void
  1579. svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  1580. {
  1581. /*
  1582. * Patch in the VMMCALL instruction:
  1583. */
  1584. hypercall[0] = 0x0f;
  1585. hypercall[1] = 0x01;
  1586. hypercall[2] = 0xd9;
  1587. }
  1588. static void svm_check_processor_compat(void *rtn)
  1589. {
  1590. *(int *)rtn = 0;
  1591. }
  1592. static bool svm_cpu_has_accelerated_tpr(void)
  1593. {
  1594. return false;
  1595. }
  1596. static int get_npt_level(void)
  1597. {
  1598. #ifdef CONFIG_X86_64
  1599. return PT64_ROOT_LEVEL;
  1600. #else
  1601. return PT32E_ROOT_LEVEL;
  1602. #endif
  1603. }
  1604. static struct kvm_x86_ops svm_x86_ops = {
  1605. .cpu_has_kvm_support = has_svm,
  1606. .disabled_by_bios = is_disabled,
  1607. .hardware_setup = svm_hardware_setup,
  1608. .hardware_unsetup = svm_hardware_unsetup,
  1609. .check_processor_compatibility = svm_check_processor_compat,
  1610. .hardware_enable = svm_hardware_enable,
  1611. .hardware_disable = svm_hardware_disable,
  1612. .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
  1613. .vcpu_create = svm_create_vcpu,
  1614. .vcpu_free = svm_free_vcpu,
  1615. .vcpu_reset = svm_vcpu_reset,
  1616. .prepare_guest_switch = svm_prepare_guest_switch,
  1617. .vcpu_load = svm_vcpu_load,
  1618. .vcpu_put = svm_vcpu_put,
  1619. .vcpu_decache = svm_vcpu_decache,
  1620. .set_guest_debug = svm_guest_debug,
  1621. .get_msr = svm_get_msr,
  1622. .set_msr = svm_set_msr,
  1623. .get_segment_base = svm_get_segment_base,
  1624. .get_segment = svm_get_segment,
  1625. .set_segment = svm_set_segment,
  1626. .get_cpl = svm_get_cpl,
  1627. .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
  1628. .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
  1629. .set_cr0 = svm_set_cr0,
  1630. .set_cr3 = svm_set_cr3,
  1631. .set_cr4 = svm_set_cr4,
  1632. .set_efer = svm_set_efer,
  1633. .get_idt = svm_get_idt,
  1634. .set_idt = svm_set_idt,
  1635. .get_gdt = svm_get_gdt,
  1636. .set_gdt = svm_set_gdt,
  1637. .get_dr = svm_get_dr,
  1638. .set_dr = svm_set_dr,
  1639. .cache_regs = svm_cache_regs,
  1640. .decache_regs = svm_decache_regs,
  1641. .get_rflags = svm_get_rflags,
  1642. .set_rflags = svm_set_rflags,
  1643. .tlb_flush = svm_flush_tlb,
  1644. .run = svm_vcpu_run,
  1645. .handle_exit = handle_exit,
  1646. .skip_emulated_instruction = skip_emulated_instruction,
  1647. .patch_hypercall = svm_patch_hypercall,
  1648. .get_irq = svm_get_irq,
  1649. .set_irq = svm_set_irq,
  1650. .queue_exception = svm_queue_exception,
  1651. .exception_injected = svm_exception_injected,
  1652. .inject_pending_irq = svm_intr_assist,
  1653. .inject_pending_vectors = do_interrupt_requests,
  1654. .set_tss_addr = svm_set_tss_addr,
  1655. .get_tdp_level = get_npt_level,
  1656. };
  1657. static int __init svm_init(void)
  1658. {
  1659. return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
  1660. THIS_MODULE);
  1661. }
  1662. static void __exit svm_exit(void)
  1663. {
  1664. kvm_exit();
  1665. }
  1666. module_init(svm_init)
  1667. module_exit(svm_exit)