svm.c 48 KB

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