svm.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890
  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. INTERCEPT_CR8_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. 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_exceptions &= ~(1 << PF_VECTOR);
  480. control->intercept_cr_read &= ~(INTERCEPT_CR0_MASK|
  481. INTERCEPT_CR3_MASK);
  482. control->intercept_cr_write &= ~(INTERCEPT_CR0_MASK|
  483. INTERCEPT_CR3_MASK);
  484. save->g_pat = 0x0007040600070406ULL;
  485. /* enable caching because the QEMU Bios doesn't enable it */
  486. save->cr0 = X86_CR0_ET;
  487. save->cr3 = 0;
  488. save->cr4 = 0;
  489. }
  490. }
  491. static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
  492. {
  493. struct vcpu_svm *svm = to_svm(vcpu);
  494. init_vmcb(svm);
  495. if (vcpu->vcpu_id != 0) {
  496. svm->vmcb->save.rip = 0;
  497. svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
  498. svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
  499. }
  500. return 0;
  501. }
  502. static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
  503. {
  504. struct vcpu_svm *svm;
  505. struct page *page;
  506. struct page *msrpm_pages;
  507. int err;
  508. svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  509. if (!svm) {
  510. err = -ENOMEM;
  511. goto out;
  512. }
  513. err = kvm_vcpu_init(&svm->vcpu, kvm, id);
  514. if (err)
  515. goto free_svm;
  516. page = alloc_page(GFP_KERNEL);
  517. if (!page) {
  518. err = -ENOMEM;
  519. goto uninit;
  520. }
  521. err = -ENOMEM;
  522. msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
  523. if (!msrpm_pages)
  524. goto uninit;
  525. svm->msrpm = page_address(msrpm_pages);
  526. svm_vcpu_init_msrpm(svm->msrpm);
  527. svm->vmcb = page_address(page);
  528. clear_page(svm->vmcb);
  529. svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
  530. svm->asid_generation = 0;
  531. memset(svm->db_regs, 0, sizeof(svm->db_regs));
  532. init_vmcb(svm);
  533. fx_init(&svm->vcpu);
  534. svm->vcpu.fpu_active = 1;
  535. svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
  536. if (svm->vcpu.vcpu_id == 0)
  537. svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
  538. return &svm->vcpu;
  539. uninit:
  540. kvm_vcpu_uninit(&svm->vcpu);
  541. free_svm:
  542. kmem_cache_free(kvm_vcpu_cache, svm);
  543. out:
  544. return ERR_PTR(err);
  545. }
  546. static void svm_free_vcpu(struct kvm_vcpu *vcpu)
  547. {
  548. struct vcpu_svm *svm = to_svm(vcpu);
  549. __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
  550. __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
  551. kvm_vcpu_uninit(vcpu);
  552. kmem_cache_free(kvm_vcpu_cache, svm);
  553. }
  554. static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  555. {
  556. struct vcpu_svm *svm = to_svm(vcpu);
  557. int i;
  558. if (unlikely(cpu != vcpu->cpu)) {
  559. u64 tsc_this, delta;
  560. /*
  561. * Make sure that the guest sees a monotonically
  562. * increasing TSC.
  563. */
  564. rdtscll(tsc_this);
  565. delta = vcpu->arch.host_tsc - tsc_this;
  566. svm->vmcb->control.tsc_offset += delta;
  567. vcpu->cpu = cpu;
  568. kvm_migrate_apic_timer(vcpu);
  569. }
  570. for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
  571. rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
  572. }
  573. static void svm_vcpu_put(struct kvm_vcpu *vcpu)
  574. {
  575. struct vcpu_svm *svm = to_svm(vcpu);
  576. int i;
  577. ++vcpu->stat.host_state_reload;
  578. for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
  579. wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
  580. rdtscll(vcpu->arch.host_tsc);
  581. }
  582. static void svm_vcpu_decache(struct kvm_vcpu *vcpu)
  583. {
  584. }
  585. static void svm_cache_regs(struct kvm_vcpu *vcpu)
  586. {
  587. struct vcpu_svm *svm = to_svm(vcpu);
  588. vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
  589. vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
  590. vcpu->arch.rip = svm->vmcb->save.rip;
  591. }
  592. static void svm_decache_regs(struct kvm_vcpu *vcpu)
  593. {
  594. struct vcpu_svm *svm = to_svm(vcpu);
  595. svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
  596. svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
  597. svm->vmcb->save.rip = vcpu->arch.rip;
  598. }
  599. static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
  600. {
  601. return to_svm(vcpu)->vmcb->save.rflags;
  602. }
  603. static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  604. {
  605. to_svm(vcpu)->vmcb->save.rflags = rflags;
  606. }
  607. static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
  608. {
  609. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  610. switch (seg) {
  611. case VCPU_SREG_CS: return &save->cs;
  612. case VCPU_SREG_DS: return &save->ds;
  613. case VCPU_SREG_ES: return &save->es;
  614. case VCPU_SREG_FS: return &save->fs;
  615. case VCPU_SREG_GS: return &save->gs;
  616. case VCPU_SREG_SS: return &save->ss;
  617. case VCPU_SREG_TR: return &save->tr;
  618. case VCPU_SREG_LDTR: return &save->ldtr;
  619. }
  620. BUG();
  621. return NULL;
  622. }
  623. static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  624. {
  625. struct vmcb_seg *s = svm_seg(vcpu, seg);
  626. return s->base;
  627. }
  628. static void svm_get_segment(struct kvm_vcpu *vcpu,
  629. struct kvm_segment *var, int seg)
  630. {
  631. struct vmcb_seg *s = svm_seg(vcpu, seg);
  632. var->base = s->base;
  633. var->limit = s->limit;
  634. var->selector = s->selector;
  635. var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
  636. var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
  637. var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
  638. var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
  639. var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
  640. var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
  641. var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
  642. var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
  643. var->unusable = !var->present;
  644. }
  645. static int svm_get_cpl(struct kvm_vcpu *vcpu)
  646. {
  647. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  648. return save->cpl;
  649. }
  650. static void svm_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  651. {
  652. struct vcpu_svm *svm = to_svm(vcpu);
  653. dt->limit = svm->vmcb->save.idtr.limit;
  654. dt->base = svm->vmcb->save.idtr.base;
  655. }
  656. static void svm_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  657. {
  658. struct vcpu_svm *svm = to_svm(vcpu);
  659. svm->vmcb->save.idtr.limit = dt->limit;
  660. svm->vmcb->save.idtr.base = dt->base ;
  661. }
  662. static void svm_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  663. {
  664. struct vcpu_svm *svm = to_svm(vcpu);
  665. dt->limit = svm->vmcb->save.gdtr.limit;
  666. dt->base = svm->vmcb->save.gdtr.base;
  667. }
  668. static void svm_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  669. {
  670. struct vcpu_svm *svm = to_svm(vcpu);
  671. svm->vmcb->save.gdtr.limit = dt->limit;
  672. svm->vmcb->save.gdtr.base = dt->base ;
  673. }
  674. static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  675. {
  676. }
  677. static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  678. {
  679. struct vcpu_svm *svm = to_svm(vcpu);
  680. #ifdef CONFIG_X86_64
  681. if (vcpu->arch.shadow_efer & EFER_LME) {
  682. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
  683. vcpu->arch.shadow_efer |= EFER_LMA;
  684. svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
  685. }
  686. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
  687. vcpu->arch.shadow_efer &= ~EFER_LMA;
  688. svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
  689. }
  690. }
  691. #endif
  692. if (npt_enabled)
  693. goto set;
  694. if ((vcpu->arch.cr0 & X86_CR0_TS) && !(cr0 & X86_CR0_TS)) {
  695. svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
  696. vcpu->fpu_active = 1;
  697. }
  698. vcpu->arch.cr0 = cr0;
  699. cr0 |= X86_CR0_PG | X86_CR0_WP;
  700. if (!vcpu->fpu_active) {
  701. svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
  702. cr0 |= X86_CR0_TS;
  703. }
  704. set:
  705. /*
  706. * re-enable caching here because the QEMU bios
  707. * does not do it - this results in some delay at
  708. * reboot
  709. */
  710. cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
  711. svm->vmcb->save.cr0 = cr0;
  712. }
  713. static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  714. {
  715. unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
  716. vcpu->arch.cr4 = cr4;
  717. if (!npt_enabled)
  718. cr4 |= X86_CR4_PAE;
  719. cr4 |= host_cr4_mce;
  720. to_svm(vcpu)->vmcb->save.cr4 = cr4;
  721. }
  722. static void svm_set_segment(struct kvm_vcpu *vcpu,
  723. struct kvm_segment *var, int seg)
  724. {
  725. struct vcpu_svm *svm = to_svm(vcpu);
  726. struct vmcb_seg *s = svm_seg(vcpu, seg);
  727. s->base = var->base;
  728. s->limit = var->limit;
  729. s->selector = var->selector;
  730. if (var->unusable)
  731. s->attrib = 0;
  732. else {
  733. s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
  734. s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
  735. s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
  736. s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
  737. s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
  738. s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
  739. s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
  740. s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
  741. }
  742. if (seg == VCPU_SREG_CS)
  743. svm->vmcb->save.cpl
  744. = (svm->vmcb->save.cs.attrib
  745. >> SVM_SELECTOR_DPL_SHIFT) & 3;
  746. }
  747. /* FIXME:
  748. svm(vcpu)->vmcb->control.int_ctl &= ~V_TPR_MASK;
  749. svm(vcpu)->vmcb->control.int_ctl |= (sregs->cr8 & V_TPR_MASK);
  750. */
  751. static int svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
  752. {
  753. return -EOPNOTSUPP;
  754. }
  755. static int svm_get_irq(struct kvm_vcpu *vcpu)
  756. {
  757. struct vcpu_svm *svm = to_svm(vcpu);
  758. u32 exit_int_info = svm->vmcb->control.exit_int_info;
  759. if (is_external_interrupt(exit_int_info))
  760. return exit_int_info & SVM_EVTINJ_VEC_MASK;
  761. return -1;
  762. }
  763. static void load_host_msrs(struct kvm_vcpu *vcpu)
  764. {
  765. #ifdef CONFIG_X86_64
  766. wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
  767. #endif
  768. }
  769. static void save_host_msrs(struct kvm_vcpu *vcpu)
  770. {
  771. #ifdef CONFIG_X86_64
  772. rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
  773. #endif
  774. }
  775. static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *svm_data)
  776. {
  777. if (svm_data->next_asid > svm_data->max_asid) {
  778. ++svm_data->asid_generation;
  779. svm_data->next_asid = 1;
  780. svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
  781. }
  782. svm->vcpu.cpu = svm_data->cpu;
  783. svm->asid_generation = svm_data->asid_generation;
  784. svm->vmcb->control.asid = svm_data->next_asid++;
  785. }
  786. static unsigned long svm_get_dr(struct kvm_vcpu *vcpu, int dr)
  787. {
  788. return to_svm(vcpu)->db_regs[dr];
  789. }
  790. static void svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value,
  791. int *exception)
  792. {
  793. struct vcpu_svm *svm = to_svm(vcpu);
  794. *exception = 0;
  795. if (svm->vmcb->save.dr7 & DR7_GD_MASK) {
  796. svm->vmcb->save.dr7 &= ~DR7_GD_MASK;
  797. svm->vmcb->save.dr6 |= DR6_BD_MASK;
  798. *exception = DB_VECTOR;
  799. return;
  800. }
  801. switch (dr) {
  802. case 0 ... 3:
  803. svm->db_regs[dr] = value;
  804. return;
  805. case 4 ... 5:
  806. if (vcpu->arch.cr4 & X86_CR4_DE) {
  807. *exception = UD_VECTOR;
  808. return;
  809. }
  810. case 7: {
  811. if (value & ~((1ULL << 32) - 1)) {
  812. *exception = GP_VECTOR;
  813. return;
  814. }
  815. svm->vmcb->save.dr7 = value;
  816. return;
  817. }
  818. default:
  819. printk(KERN_DEBUG "%s: unexpected dr %u\n",
  820. __func__, dr);
  821. *exception = UD_VECTOR;
  822. return;
  823. }
  824. }
  825. static int pf_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  826. {
  827. u32 exit_int_info = svm->vmcb->control.exit_int_info;
  828. struct kvm *kvm = svm->vcpu.kvm;
  829. u64 fault_address;
  830. u32 error_code;
  831. if (!irqchip_in_kernel(kvm) &&
  832. is_external_interrupt(exit_int_info))
  833. push_irq(&svm->vcpu, exit_int_info & SVM_EVTINJ_VEC_MASK);
  834. fault_address = svm->vmcb->control.exit_info_2;
  835. error_code = svm->vmcb->control.exit_info_1;
  836. return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
  837. }
  838. static int ud_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  839. {
  840. int er;
  841. er = emulate_instruction(&svm->vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
  842. if (er != EMULATE_DONE)
  843. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  844. return 1;
  845. }
  846. static int nm_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  847. {
  848. svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
  849. if (!(svm->vcpu.arch.cr0 & X86_CR0_TS))
  850. svm->vmcb->save.cr0 &= ~X86_CR0_TS;
  851. svm->vcpu.fpu_active = 1;
  852. return 1;
  853. }
  854. static int shutdown_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  855. {
  856. /*
  857. * VMCB is undefined after a SHUTDOWN intercept
  858. * so reinitialize it.
  859. */
  860. clear_page(svm->vmcb);
  861. init_vmcb(svm);
  862. kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
  863. return 0;
  864. }
  865. static int io_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  866. {
  867. u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
  868. int size, down, in, string, rep;
  869. unsigned port;
  870. ++svm->vcpu.stat.io_exits;
  871. svm->next_rip = svm->vmcb->control.exit_info_2;
  872. string = (io_info & SVM_IOIO_STR_MASK) != 0;
  873. if (string) {
  874. if (emulate_instruction(&svm->vcpu,
  875. kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
  876. return 0;
  877. return 1;
  878. }
  879. in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
  880. port = io_info >> 16;
  881. size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
  882. rep = (io_info & SVM_IOIO_REP_MASK) != 0;
  883. down = (svm->vmcb->save.rflags & X86_EFLAGS_DF) != 0;
  884. return kvm_emulate_pio(&svm->vcpu, kvm_run, in, size, port);
  885. }
  886. static int nop_on_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  887. {
  888. return 1;
  889. }
  890. static int halt_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  891. {
  892. svm->next_rip = svm->vmcb->save.rip + 1;
  893. skip_emulated_instruction(&svm->vcpu);
  894. return kvm_emulate_halt(&svm->vcpu);
  895. }
  896. static int vmmcall_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  897. {
  898. svm->next_rip = svm->vmcb->save.rip + 3;
  899. skip_emulated_instruction(&svm->vcpu);
  900. kvm_emulate_hypercall(&svm->vcpu);
  901. return 1;
  902. }
  903. static int invalid_op_interception(struct vcpu_svm *svm,
  904. struct kvm_run *kvm_run)
  905. {
  906. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  907. return 1;
  908. }
  909. static int task_switch_interception(struct vcpu_svm *svm,
  910. struct kvm_run *kvm_run)
  911. {
  912. u16 tss_selector;
  913. tss_selector = (u16)svm->vmcb->control.exit_info_1;
  914. if (svm->vmcb->control.exit_info_2 &
  915. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
  916. return kvm_task_switch(&svm->vcpu, tss_selector,
  917. TASK_SWITCH_IRET);
  918. if (svm->vmcb->control.exit_info_2 &
  919. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
  920. return kvm_task_switch(&svm->vcpu, tss_selector,
  921. TASK_SWITCH_JMP);
  922. return kvm_task_switch(&svm->vcpu, tss_selector, TASK_SWITCH_CALL);
  923. }
  924. static int cpuid_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  925. {
  926. svm->next_rip = svm->vmcb->save.rip + 2;
  927. kvm_emulate_cpuid(&svm->vcpu);
  928. return 1;
  929. }
  930. static int emulate_on_interception(struct vcpu_svm *svm,
  931. struct kvm_run *kvm_run)
  932. {
  933. if (emulate_instruction(&svm->vcpu, NULL, 0, 0, 0) != EMULATE_DONE)
  934. pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
  935. return 1;
  936. }
  937. static int cr8_write_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  938. {
  939. emulate_instruction(&svm->vcpu, NULL, 0, 0, 0);
  940. if (irqchip_in_kernel(svm->vcpu.kvm))
  941. return 1;
  942. kvm_run->exit_reason = KVM_EXIT_SET_TPR;
  943. return 0;
  944. }
  945. static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
  946. {
  947. struct vcpu_svm *svm = to_svm(vcpu);
  948. switch (ecx) {
  949. case MSR_IA32_TIME_STAMP_COUNTER: {
  950. u64 tsc;
  951. rdtscll(tsc);
  952. *data = svm->vmcb->control.tsc_offset + tsc;
  953. break;
  954. }
  955. case MSR_K6_STAR:
  956. *data = svm->vmcb->save.star;
  957. break;
  958. #ifdef CONFIG_X86_64
  959. case MSR_LSTAR:
  960. *data = svm->vmcb->save.lstar;
  961. break;
  962. case MSR_CSTAR:
  963. *data = svm->vmcb->save.cstar;
  964. break;
  965. case MSR_KERNEL_GS_BASE:
  966. *data = svm->vmcb->save.kernel_gs_base;
  967. break;
  968. case MSR_SYSCALL_MASK:
  969. *data = svm->vmcb->save.sfmask;
  970. break;
  971. #endif
  972. case MSR_IA32_SYSENTER_CS:
  973. *data = svm->vmcb->save.sysenter_cs;
  974. break;
  975. case MSR_IA32_SYSENTER_EIP:
  976. *data = svm->vmcb->save.sysenter_eip;
  977. break;
  978. case MSR_IA32_SYSENTER_ESP:
  979. *data = svm->vmcb->save.sysenter_esp;
  980. break;
  981. /* Nobody will change the following 5 values in the VMCB so
  982. we can safely return them on rdmsr. They will always be 0
  983. until LBRV is implemented. */
  984. case MSR_IA32_DEBUGCTLMSR:
  985. *data = svm->vmcb->save.dbgctl;
  986. break;
  987. case MSR_IA32_LASTBRANCHFROMIP:
  988. *data = svm->vmcb->save.br_from;
  989. break;
  990. case MSR_IA32_LASTBRANCHTOIP:
  991. *data = svm->vmcb->save.br_to;
  992. break;
  993. case MSR_IA32_LASTINTFROMIP:
  994. *data = svm->vmcb->save.last_excp_from;
  995. break;
  996. case MSR_IA32_LASTINTTOIP:
  997. *data = svm->vmcb->save.last_excp_to;
  998. break;
  999. default:
  1000. return kvm_get_msr_common(vcpu, ecx, data);
  1001. }
  1002. return 0;
  1003. }
  1004. static int rdmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1005. {
  1006. u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  1007. u64 data;
  1008. if (svm_get_msr(&svm->vcpu, ecx, &data))
  1009. kvm_inject_gp(&svm->vcpu, 0);
  1010. else {
  1011. svm->vmcb->save.rax = data & 0xffffffff;
  1012. svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
  1013. svm->next_rip = svm->vmcb->save.rip + 2;
  1014. skip_emulated_instruction(&svm->vcpu);
  1015. }
  1016. return 1;
  1017. }
  1018. static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
  1019. {
  1020. struct vcpu_svm *svm = to_svm(vcpu);
  1021. switch (ecx) {
  1022. case MSR_IA32_TIME_STAMP_COUNTER: {
  1023. u64 tsc;
  1024. rdtscll(tsc);
  1025. svm->vmcb->control.tsc_offset = data - tsc;
  1026. break;
  1027. }
  1028. case MSR_K6_STAR:
  1029. svm->vmcb->save.star = data;
  1030. break;
  1031. #ifdef CONFIG_X86_64
  1032. case MSR_LSTAR:
  1033. svm->vmcb->save.lstar = data;
  1034. break;
  1035. case MSR_CSTAR:
  1036. svm->vmcb->save.cstar = data;
  1037. break;
  1038. case MSR_KERNEL_GS_BASE:
  1039. svm->vmcb->save.kernel_gs_base = data;
  1040. break;
  1041. case MSR_SYSCALL_MASK:
  1042. svm->vmcb->save.sfmask = data;
  1043. break;
  1044. #endif
  1045. case MSR_IA32_SYSENTER_CS:
  1046. svm->vmcb->save.sysenter_cs = data;
  1047. break;
  1048. case MSR_IA32_SYSENTER_EIP:
  1049. svm->vmcb->save.sysenter_eip = data;
  1050. break;
  1051. case MSR_IA32_SYSENTER_ESP:
  1052. svm->vmcb->save.sysenter_esp = data;
  1053. break;
  1054. case MSR_IA32_DEBUGCTLMSR:
  1055. if (!svm_has(SVM_FEATURE_LBRV)) {
  1056. pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
  1057. __func__, data);
  1058. break;
  1059. }
  1060. if (data & DEBUGCTL_RESERVED_BITS)
  1061. return 1;
  1062. svm->vmcb->save.dbgctl = data;
  1063. if (data & (1ULL<<0))
  1064. svm_enable_lbrv(svm);
  1065. else
  1066. svm_disable_lbrv(svm);
  1067. break;
  1068. case MSR_K7_EVNTSEL0:
  1069. case MSR_K7_EVNTSEL1:
  1070. case MSR_K7_EVNTSEL2:
  1071. case MSR_K7_EVNTSEL3:
  1072. /*
  1073. * only support writing 0 to the performance counters for now
  1074. * to make Windows happy. Should be replaced by a real
  1075. * performance counter emulation later.
  1076. */
  1077. if (data != 0)
  1078. goto unhandled;
  1079. break;
  1080. default:
  1081. unhandled:
  1082. return kvm_set_msr_common(vcpu, ecx, data);
  1083. }
  1084. return 0;
  1085. }
  1086. static int wrmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1087. {
  1088. u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  1089. u64 data = (svm->vmcb->save.rax & -1u)
  1090. | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  1091. svm->next_rip = svm->vmcb->save.rip + 2;
  1092. if (svm_set_msr(&svm->vcpu, ecx, data))
  1093. kvm_inject_gp(&svm->vcpu, 0);
  1094. else
  1095. skip_emulated_instruction(&svm->vcpu);
  1096. return 1;
  1097. }
  1098. static int msr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1099. {
  1100. if (svm->vmcb->control.exit_info_1)
  1101. return wrmsr_interception(svm, kvm_run);
  1102. else
  1103. return rdmsr_interception(svm, kvm_run);
  1104. }
  1105. static int interrupt_window_interception(struct vcpu_svm *svm,
  1106. struct kvm_run *kvm_run)
  1107. {
  1108. svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
  1109. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  1110. /*
  1111. * If the user space waits to inject interrupts, exit as soon as
  1112. * possible
  1113. */
  1114. if (kvm_run->request_interrupt_window &&
  1115. !svm->vcpu.arch.irq_summary) {
  1116. ++svm->vcpu.stat.irq_window_exits;
  1117. kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  1118. return 0;
  1119. }
  1120. return 1;
  1121. }
  1122. static int (*svm_exit_handlers[])(struct vcpu_svm *svm,
  1123. struct kvm_run *kvm_run) = {
  1124. [SVM_EXIT_READ_CR0] = emulate_on_interception,
  1125. [SVM_EXIT_READ_CR3] = emulate_on_interception,
  1126. [SVM_EXIT_READ_CR4] = emulate_on_interception,
  1127. [SVM_EXIT_READ_CR8] = emulate_on_interception,
  1128. /* for now: */
  1129. [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
  1130. [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
  1131. [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
  1132. [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
  1133. [SVM_EXIT_READ_DR0] = emulate_on_interception,
  1134. [SVM_EXIT_READ_DR1] = emulate_on_interception,
  1135. [SVM_EXIT_READ_DR2] = emulate_on_interception,
  1136. [SVM_EXIT_READ_DR3] = emulate_on_interception,
  1137. [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
  1138. [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
  1139. [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
  1140. [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
  1141. [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
  1142. [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
  1143. [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
  1144. [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
  1145. [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
  1146. [SVM_EXIT_INTR] = nop_on_interception,
  1147. [SVM_EXIT_NMI] = nop_on_interception,
  1148. [SVM_EXIT_SMI] = nop_on_interception,
  1149. [SVM_EXIT_INIT] = nop_on_interception,
  1150. [SVM_EXIT_VINTR] = interrupt_window_interception,
  1151. /* [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, */
  1152. [SVM_EXIT_CPUID] = cpuid_interception,
  1153. [SVM_EXIT_INVD] = emulate_on_interception,
  1154. [SVM_EXIT_HLT] = halt_interception,
  1155. [SVM_EXIT_INVLPG] = emulate_on_interception,
  1156. [SVM_EXIT_INVLPGA] = invalid_op_interception,
  1157. [SVM_EXIT_IOIO] = io_interception,
  1158. [SVM_EXIT_MSR] = msr_interception,
  1159. [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
  1160. [SVM_EXIT_SHUTDOWN] = shutdown_interception,
  1161. [SVM_EXIT_VMRUN] = invalid_op_interception,
  1162. [SVM_EXIT_VMMCALL] = vmmcall_interception,
  1163. [SVM_EXIT_VMLOAD] = invalid_op_interception,
  1164. [SVM_EXIT_VMSAVE] = invalid_op_interception,
  1165. [SVM_EXIT_STGI] = invalid_op_interception,
  1166. [SVM_EXIT_CLGI] = invalid_op_interception,
  1167. [SVM_EXIT_SKINIT] = invalid_op_interception,
  1168. [SVM_EXIT_WBINVD] = emulate_on_interception,
  1169. [SVM_EXIT_MONITOR] = invalid_op_interception,
  1170. [SVM_EXIT_MWAIT] = invalid_op_interception,
  1171. [SVM_EXIT_NPF] = pf_interception,
  1172. };
  1173. static int handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  1174. {
  1175. struct vcpu_svm *svm = to_svm(vcpu);
  1176. u32 exit_code = svm->vmcb->control.exit_code;
  1177. if (npt_enabled) {
  1178. int mmu_reload = 0;
  1179. if ((vcpu->arch.cr0 ^ svm->vmcb->save.cr0) & X86_CR0_PG) {
  1180. svm_set_cr0(vcpu, svm->vmcb->save.cr0);
  1181. mmu_reload = 1;
  1182. }
  1183. vcpu->arch.cr0 = svm->vmcb->save.cr0;
  1184. vcpu->arch.cr3 = svm->vmcb->save.cr3;
  1185. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  1186. if (!load_pdptrs(vcpu, vcpu->arch.cr3)) {
  1187. kvm_inject_gp(vcpu, 0);
  1188. return 1;
  1189. }
  1190. }
  1191. if (mmu_reload) {
  1192. kvm_mmu_reset_context(vcpu);
  1193. kvm_mmu_load(vcpu);
  1194. }
  1195. }
  1196. kvm_reput_irq(svm);
  1197. if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
  1198. kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  1199. kvm_run->fail_entry.hardware_entry_failure_reason
  1200. = svm->vmcb->control.exit_code;
  1201. return 0;
  1202. }
  1203. if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
  1204. exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
  1205. exit_code != SVM_EXIT_NPF)
  1206. printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
  1207. "exit_code 0x%x\n",
  1208. __func__, svm->vmcb->control.exit_int_info,
  1209. exit_code);
  1210. if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
  1211. || !svm_exit_handlers[exit_code]) {
  1212. kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
  1213. kvm_run->hw.hardware_exit_reason = exit_code;
  1214. return 0;
  1215. }
  1216. return svm_exit_handlers[exit_code](svm, kvm_run);
  1217. }
  1218. static void reload_tss(struct kvm_vcpu *vcpu)
  1219. {
  1220. int cpu = raw_smp_processor_id();
  1221. struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
  1222. svm_data->tss_desc->type = 9; /* available 32/64-bit TSS */
  1223. load_TR_desc();
  1224. }
  1225. static void pre_svm_run(struct vcpu_svm *svm)
  1226. {
  1227. int cpu = raw_smp_processor_id();
  1228. struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
  1229. svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
  1230. if (svm->vcpu.cpu != cpu ||
  1231. svm->asid_generation != svm_data->asid_generation)
  1232. new_asid(svm, svm_data);
  1233. }
  1234. static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
  1235. {
  1236. struct vmcb_control_area *control;
  1237. control = &svm->vmcb->control;
  1238. control->int_vector = irq;
  1239. control->int_ctl &= ~V_INTR_PRIO_MASK;
  1240. control->int_ctl |= V_IRQ_MASK |
  1241. ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
  1242. }
  1243. static void svm_set_irq(struct kvm_vcpu *vcpu, int irq)
  1244. {
  1245. struct vcpu_svm *svm = to_svm(vcpu);
  1246. svm_inject_irq(svm, irq);
  1247. }
  1248. static void svm_intr_assist(struct kvm_vcpu *vcpu)
  1249. {
  1250. struct vcpu_svm *svm = to_svm(vcpu);
  1251. struct vmcb *vmcb = svm->vmcb;
  1252. int intr_vector = -1;
  1253. if ((vmcb->control.exit_int_info & SVM_EVTINJ_VALID) &&
  1254. ((vmcb->control.exit_int_info & SVM_EVTINJ_TYPE_MASK) == 0)) {
  1255. intr_vector = vmcb->control.exit_int_info &
  1256. SVM_EVTINJ_VEC_MASK;
  1257. vmcb->control.exit_int_info = 0;
  1258. svm_inject_irq(svm, intr_vector);
  1259. return;
  1260. }
  1261. if (vmcb->control.int_ctl & V_IRQ_MASK)
  1262. return;
  1263. if (!kvm_cpu_has_interrupt(vcpu))
  1264. return;
  1265. if (!(vmcb->save.rflags & X86_EFLAGS_IF) ||
  1266. (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) ||
  1267. (vmcb->control.event_inj & SVM_EVTINJ_VALID)) {
  1268. /* unable to deliver irq, set pending irq */
  1269. vmcb->control.intercept |= (1ULL << INTERCEPT_VINTR);
  1270. svm_inject_irq(svm, 0x0);
  1271. return;
  1272. }
  1273. /* Okay, we can deliver the interrupt: grab it and update PIC state. */
  1274. intr_vector = kvm_cpu_get_interrupt(vcpu);
  1275. svm_inject_irq(svm, intr_vector);
  1276. kvm_timer_intr_post(vcpu, intr_vector);
  1277. }
  1278. static void kvm_reput_irq(struct vcpu_svm *svm)
  1279. {
  1280. struct vmcb_control_area *control = &svm->vmcb->control;
  1281. if ((control->int_ctl & V_IRQ_MASK)
  1282. && !irqchip_in_kernel(svm->vcpu.kvm)) {
  1283. control->int_ctl &= ~V_IRQ_MASK;
  1284. push_irq(&svm->vcpu, control->int_vector);
  1285. }
  1286. svm->vcpu.arch.interrupt_window_open =
  1287. !(control->int_state & SVM_INTERRUPT_SHADOW_MASK);
  1288. }
  1289. static void svm_do_inject_vector(struct vcpu_svm *svm)
  1290. {
  1291. struct kvm_vcpu *vcpu = &svm->vcpu;
  1292. int word_index = __ffs(vcpu->arch.irq_summary);
  1293. int bit_index = __ffs(vcpu->arch.irq_pending[word_index]);
  1294. int irq = word_index * BITS_PER_LONG + bit_index;
  1295. clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]);
  1296. if (!vcpu->arch.irq_pending[word_index])
  1297. clear_bit(word_index, &vcpu->arch.irq_summary);
  1298. svm_inject_irq(svm, irq);
  1299. }
  1300. static void do_interrupt_requests(struct kvm_vcpu *vcpu,
  1301. struct kvm_run *kvm_run)
  1302. {
  1303. struct vcpu_svm *svm = to_svm(vcpu);
  1304. struct vmcb_control_area *control = &svm->vmcb->control;
  1305. svm->vcpu.arch.interrupt_window_open =
  1306. (!(control->int_state & SVM_INTERRUPT_SHADOW_MASK) &&
  1307. (svm->vmcb->save.rflags & X86_EFLAGS_IF));
  1308. if (svm->vcpu.arch.interrupt_window_open && svm->vcpu.arch.irq_summary)
  1309. /*
  1310. * If interrupts enabled, and not blocked by sti or mov ss. Good.
  1311. */
  1312. svm_do_inject_vector(svm);
  1313. /*
  1314. * Interrupts blocked. Wait for unblock.
  1315. */
  1316. if (!svm->vcpu.arch.interrupt_window_open &&
  1317. (svm->vcpu.arch.irq_summary || kvm_run->request_interrupt_window))
  1318. control->intercept |= 1ULL << INTERCEPT_VINTR;
  1319. else
  1320. control->intercept &= ~(1ULL << INTERCEPT_VINTR);
  1321. }
  1322. static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
  1323. {
  1324. return 0;
  1325. }
  1326. static void save_db_regs(unsigned long *db_regs)
  1327. {
  1328. asm volatile ("mov %%dr0, %0" : "=r"(db_regs[0]));
  1329. asm volatile ("mov %%dr1, %0" : "=r"(db_regs[1]));
  1330. asm volatile ("mov %%dr2, %0" : "=r"(db_regs[2]));
  1331. asm volatile ("mov %%dr3, %0" : "=r"(db_regs[3]));
  1332. }
  1333. static void load_db_regs(unsigned long *db_regs)
  1334. {
  1335. asm volatile ("mov %0, %%dr0" : : "r"(db_regs[0]));
  1336. asm volatile ("mov %0, %%dr1" : : "r"(db_regs[1]));
  1337. asm volatile ("mov %0, %%dr2" : : "r"(db_regs[2]));
  1338. asm volatile ("mov %0, %%dr3" : : "r"(db_regs[3]));
  1339. }
  1340. static void svm_flush_tlb(struct kvm_vcpu *vcpu)
  1341. {
  1342. force_new_asid(vcpu);
  1343. }
  1344. static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
  1345. {
  1346. }
  1347. static void svm_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  1348. {
  1349. struct vcpu_svm *svm = to_svm(vcpu);
  1350. u16 fs_selector;
  1351. u16 gs_selector;
  1352. u16 ldt_selector;
  1353. pre_svm_run(svm);
  1354. save_host_msrs(vcpu);
  1355. fs_selector = read_fs();
  1356. gs_selector = read_gs();
  1357. ldt_selector = read_ldt();
  1358. svm->host_cr2 = kvm_read_cr2();
  1359. svm->host_dr6 = read_dr6();
  1360. svm->host_dr7 = read_dr7();
  1361. svm->vmcb->save.cr2 = vcpu->arch.cr2;
  1362. /* required for live migration with NPT */
  1363. if (npt_enabled)
  1364. svm->vmcb->save.cr3 = vcpu->arch.cr3;
  1365. if (svm->vmcb->save.dr7 & 0xff) {
  1366. write_dr7(0);
  1367. save_db_regs(svm->host_db_regs);
  1368. load_db_regs(svm->db_regs);
  1369. }
  1370. clgi();
  1371. local_irq_enable();
  1372. asm volatile (
  1373. #ifdef CONFIG_X86_64
  1374. "push %%rbp; \n\t"
  1375. #else
  1376. "push %%ebp; \n\t"
  1377. #endif
  1378. #ifdef CONFIG_X86_64
  1379. "mov %c[rbx](%[svm]), %%rbx \n\t"
  1380. "mov %c[rcx](%[svm]), %%rcx \n\t"
  1381. "mov %c[rdx](%[svm]), %%rdx \n\t"
  1382. "mov %c[rsi](%[svm]), %%rsi \n\t"
  1383. "mov %c[rdi](%[svm]), %%rdi \n\t"
  1384. "mov %c[rbp](%[svm]), %%rbp \n\t"
  1385. "mov %c[r8](%[svm]), %%r8 \n\t"
  1386. "mov %c[r9](%[svm]), %%r9 \n\t"
  1387. "mov %c[r10](%[svm]), %%r10 \n\t"
  1388. "mov %c[r11](%[svm]), %%r11 \n\t"
  1389. "mov %c[r12](%[svm]), %%r12 \n\t"
  1390. "mov %c[r13](%[svm]), %%r13 \n\t"
  1391. "mov %c[r14](%[svm]), %%r14 \n\t"
  1392. "mov %c[r15](%[svm]), %%r15 \n\t"
  1393. #else
  1394. "mov %c[rbx](%[svm]), %%ebx \n\t"
  1395. "mov %c[rcx](%[svm]), %%ecx \n\t"
  1396. "mov %c[rdx](%[svm]), %%edx \n\t"
  1397. "mov %c[rsi](%[svm]), %%esi \n\t"
  1398. "mov %c[rdi](%[svm]), %%edi \n\t"
  1399. "mov %c[rbp](%[svm]), %%ebp \n\t"
  1400. #endif
  1401. #ifdef CONFIG_X86_64
  1402. /* Enter guest mode */
  1403. "push %%rax \n\t"
  1404. "mov %c[vmcb](%[svm]), %%rax \n\t"
  1405. SVM_VMLOAD "\n\t"
  1406. SVM_VMRUN "\n\t"
  1407. SVM_VMSAVE "\n\t"
  1408. "pop %%rax \n\t"
  1409. #else
  1410. /* Enter guest mode */
  1411. "push %%eax \n\t"
  1412. "mov %c[vmcb](%[svm]), %%eax \n\t"
  1413. SVM_VMLOAD "\n\t"
  1414. SVM_VMRUN "\n\t"
  1415. SVM_VMSAVE "\n\t"
  1416. "pop %%eax \n\t"
  1417. #endif
  1418. /* Save guest registers, load host registers */
  1419. #ifdef CONFIG_X86_64
  1420. "mov %%rbx, %c[rbx](%[svm]) \n\t"
  1421. "mov %%rcx, %c[rcx](%[svm]) \n\t"
  1422. "mov %%rdx, %c[rdx](%[svm]) \n\t"
  1423. "mov %%rsi, %c[rsi](%[svm]) \n\t"
  1424. "mov %%rdi, %c[rdi](%[svm]) \n\t"
  1425. "mov %%rbp, %c[rbp](%[svm]) \n\t"
  1426. "mov %%r8, %c[r8](%[svm]) \n\t"
  1427. "mov %%r9, %c[r9](%[svm]) \n\t"
  1428. "mov %%r10, %c[r10](%[svm]) \n\t"
  1429. "mov %%r11, %c[r11](%[svm]) \n\t"
  1430. "mov %%r12, %c[r12](%[svm]) \n\t"
  1431. "mov %%r13, %c[r13](%[svm]) \n\t"
  1432. "mov %%r14, %c[r14](%[svm]) \n\t"
  1433. "mov %%r15, %c[r15](%[svm]) \n\t"
  1434. "pop %%rbp; \n\t"
  1435. #else
  1436. "mov %%ebx, %c[rbx](%[svm]) \n\t"
  1437. "mov %%ecx, %c[rcx](%[svm]) \n\t"
  1438. "mov %%edx, %c[rdx](%[svm]) \n\t"
  1439. "mov %%esi, %c[rsi](%[svm]) \n\t"
  1440. "mov %%edi, %c[rdi](%[svm]) \n\t"
  1441. "mov %%ebp, %c[rbp](%[svm]) \n\t"
  1442. "pop %%ebp; \n\t"
  1443. #endif
  1444. :
  1445. : [svm]"a"(svm),
  1446. [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
  1447. [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
  1448. [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
  1449. [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
  1450. [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
  1451. [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
  1452. [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
  1453. #ifdef CONFIG_X86_64
  1454. , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
  1455. [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
  1456. [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
  1457. [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
  1458. [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
  1459. [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
  1460. [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
  1461. [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
  1462. #endif
  1463. : "cc", "memory"
  1464. #ifdef CONFIG_X86_64
  1465. , "rbx", "rcx", "rdx", "rsi", "rdi"
  1466. , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
  1467. #else
  1468. , "ebx", "ecx", "edx" , "esi", "edi"
  1469. #endif
  1470. );
  1471. if ((svm->vmcb->save.dr7 & 0xff))
  1472. load_db_regs(svm->host_db_regs);
  1473. vcpu->arch.cr2 = svm->vmcb->save.cr2;
  1474. write_dr6(svm->host_dr6);
  1475. write_dr7(svm->host_dr7);
  1476. kvm_write_cr2(svm->host_cr2);
  1477. load_fs(fs_selector);
  1478. load_gs(gs_selector);
  1479. load_ldt(ldt_selector);
  1480. load_host_msrs(vcpu);
  1481. reload_tss(vcpu);
  1482. local_irq_disable();
  1483. stgi();
  1484. svm->next_rip = 0;
  1485. }
  1486. static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
  1487. {
  1488. struct vcpu_svm *svm = to_svm(vcpu);
  1489. if (npt_enabled) {
  1490. svm->vmcb->control.nested_cr3 = root;
  1491. force_new_asid(vcpu);
  1492. return;
  1493. }
  1494. svm->vmcb->save.cr3 = root;
  1495. force_new_asid(vcpu);
  1496. if (vcpu->fpu_active) {
  1497. svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
  1498. svm->vmcb->save.cr0 |= X86_CR0_TS;
  1499. vcpu->fpu_active = 0;
  1500. }
  1501. }
  1502. static int is_disabled(void)
  1503. {
  1504. u64 vm_cr;
  1505. rdmsrl(MSR_VM_CR, vm_cr);
  1506. if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
  1507. return 1;
  1508. return 0;
  1509. }
  1510. static void
  1511. svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  1512. {
  1513. /*
  1514. * Patch in the VMMCALL instruction:
  1515. */
  1516. hypercall[0] = 0x0f;
  1517. hypercall[1] = 0x01;
  1518. hypercall[2] = 0xd9;
  1519. }
  1520. static void svm_check_processor_compat(void *rtn)
  1521. {
  1522. *(int *)rtn = 0;
  1523. }
  1524. static bool svm_cpu_has_accelerated_tpr(void)
  1525. {
  1526. return false;
  1527. }
  1528. static struct kvm_x86_ops svm_x86_ops = {
  1529. .cpu_has_kvm_support = has_svm,
  1530. .disabled_by_bios = is_disabled,
  1531. .hardware_setup = svm_hardware_setup,
  1532. .hardware_unsetup = svm_hardware_unsetup,
  1533. .check_processor_compatibility = svm_check_processor_compat,
  1534. .hardware_enable = svm_hardware_enable,
  1535. .hardware_disable = svm_hardware_disable,
  1536. .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
  1537. .vcpu_create = svm_create_vcpu,
  1538. .vcpu_free = svm_free_vcpu,
  1539. .vcpu_reset = svm_vcpu_reset,
  1540. .prepare_guest_switch = svm_prepare_guest_switch,
  1541. .vcpu_load = svm_vcpu_load,
  1542. .vcpu_put = svm_vcpu_put,
  1543. .vcpu_decache = svm_vcpu_decache,
  1544. .set_guest_debug = svm_guest_debug,
  1545. .get_msr = svm_get_msr,
  1546. .set_msr = svm_set_msr,
  1547. .get_segment_base = svm_get_segment_base,
  1548. .get_segment = svm_get_segment,
  1549. .set_segment = svm_set_segment,
  1550. .get_cpl = svm_get_cpl,
  1551. .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
  1552. .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
  1553. .set_cr0 = svm_set_cr0,
  1554. .set_cr3 = svm_set_cr3,
  1555. .set_cr4 = svm_set_cr4,
  1556. .set_efer = svm_set_efer,
  1557. .get_idt = svm_get_idt,
  1558. .set_idt = svm_set_idt,
  1559. .get_gdt = svm_get_gdt,
  1560. .set_gdt = svm_set_gdt,
  1561. .get_dr = svm_get_dr,
  1562. .set_dr = svm_set_dr,
  1563. .cache_regs = svm_cache_regs,
  1564. .decache_regs = svm_decache_regs,
  1565. .get_rflags = svm_get_rflags,
  1566. .set_rflags = svm_set_rflags,
  1567. .tlb_flush = svm_flush_tlb,
  1568. .run = svm_vcpu_run,
  1569. .handle_exit = handle_exit,
  1570. .skip_emulated_instruction = skip_emulated_instruction,
  1571. .patch_hypercall = svm_patch_hypercall,
  1572. .get_irq = svm_get_irq,
  1573. .set_irq = svm_set_irq,
  1574. .queue_exception = svm_queue_exception,
  1575. .exception_injected = svm_exception_injected,
  1576. .inject_pending_irq = svm_intr_assist,
  1577. .inject_pending_vectors = do_interrupt_requests,
  1578. .set_tss_addr = svm_set_tss_addr,
  1579. };
  1580. static int __init svm_init(void)
  1581. {
  1582. return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
  1583. THIS_MODULE);
  1584. }
  1585. static void __exit svm_exit(void)
  1586. {
  1587. kvm_exit();
  1588. }
  1589. module_init(svm_init)
  1590. module_exit(svm_exit)