svm.c 49 KB

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