svm.c 46 KB

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