svm.c 49 KB

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