svm.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697
  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. /* Turn on to get debugging output*/
  42. /* #define NESTED_DEBUG */
  43. #ifdef NESTED_DEBUG
  44. #define nsvm_printk(fmt, args...) printk(KERN_INFO fmt, ## args)
  45. #else
  46. #define nsvm_printk(fmt, args...) do {} while(0)
  47. #endif
  48. /* enable NPT for AMD64 and X86 with PAE */
  49. #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
  50. static bool npt_enabled = true;
  51. #else
  52. static bool npt_enabled = false;
  53. #endif
  54. static int npt = 1;
  55. module_param(npt, int, S_IRUGO);
  56. static int nested = 0;
  57. module_param(nested, int, S_IRUGO);
  58. static void kvm_reput_irq(struct vcpu_svm *svm);
  59. static void svm_flush_tlb(struct kvm_vcpu *vcpu);
  60. static int nested_svm_exit_handled(struct vcpu_svm *svm, bool kvm_override);
  61. static int nested_svm_vmexit(struct vcpu_svm *svm);
  62. static int nested_svm_vmsave(struct vcpu_svm *svm, void *nested_vmcb,
  63. void *arg2, void *opaque);
  64. static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
  65. bool has_error_code, u32 error_code);
  66. static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
  67. {
  68. return container_of(vcpu, struct vcpu_svm, vcpu);
  69. }
  70. static inline bool is_nested(struct vcpu_svm *svm)
  71. {
  72. return svm->nested_vmcb;
  73. }
  74. static unsigned long iopm_base;
  75. struct kvm_ldttss_desc {
  76. u16 limit0;
  77. u16 base0;
  78. unsigned base1 : 8, type : 5, dpl : 2, p : 1;
  79. unsigned limit1 : 4, zero0 : 3, g : 1, base2 : 8;
  80. u32 base3;
  81. u32 zero1;
  82. } __attribute__((packed));
  83. struct svm_cpu_data {
  84. int cpu;
  85. u64 asid_generation;
  86. u32 max_asid;
  87. u32 next_asid;
  88. struct kvm_ldttss_desc *tss_desc;
  89. struct page *save_area;
  90. };
  91. static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
  92. static uint32_t svm_features;
  93. struct svm_init_data {
  94. int cpu;
  95. int r;
  96. };
  97. static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
  98. #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
  99. #define MSRS_RANGE_SIZE 2048
  100. #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
  101. #define MAX_INST_SIZE 15
  102. static inline u32 svm_has(u32 feat)
  103. {
  104. return svm_features & feat;
  105. }
  106. static inline u8 pop_irq(struct kvm_vcpu *vcpu)
  107. {
  108. int word_index = __ffs(vcpu->arch.irq_summary);
  109. int bit_index = __ffs(vcpu->arch.irq_pending[word_index]);
  110. int irq = word_index * BITS_PER_LONG + bit_index;
  111. clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]);
  112. if (!vcpu->arch.irq_pending[word_index])
  113. clear_bit(word_index, &vcpu->arch.irq_summary);
  114. return irq;
  115. }
  116. static inline void push_irq(struct kvm_vcpu *vcpu, u8 irq)
  117. {
  118. set_bit(irq, vcpu->arch.irq_pending);
  119. set_bit(irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
  120. }
  121. static inline void clgi(void)
  122. {
  123. asm volatile (__ex(SVM_CLGI));
  124. }
  125. static inline void stgi(void)
  126. {
  127. asm volatile (__ex(SVM_STGI));
  128. }
  129. static inline void invlpga(unsigned long addr, u32 asid)
  130. {
  131. asm volatile (__ex(SVM_INVLPGA) :: "a"(addr), "c"(asid));
  132. }
  133. static inline unsigned long kvm_read_cr2(void)
  134. {
  135. unsigned long cr2;
  136. asm volatile ("mov %%cr2, %0" : "=r" (cr2));
  137. return cr2;
  138. }
  139. static inline void kvm_write_cr2(unsigned long val)
  140. {
  141. asm volatile ("mov %0, %%cr2" :: "r" (val));
  142. }
  143. static inline unsigned long read_dr6(void)
  144. {
  145. unsigned long dr6;
  146. asm volatile ("mov %%dr6, %0" : "=r" (dr6));
  147. return dr6;
  148. }
  149. static inline void write_dr6(unsigned long val)
  150. {
  151. asm volatile ("mov %0, %%dr6" :: "r" (val));
  152. }
  153. static inline unsigned long read_dr7(void)
  154. {
  155. unsigned long dr7;
  156. asm volatile ("mov %%dr7, %0" : "=r" (dr7));
  157. return dr7;
  158. }
  159. static inline void write_dr7(unsigned long val)
  160. {
  161. asm volatile ("mov %0, %%dr7" :: "r" (val));
  162. }
  163. static inline void force_new_asid(struct kvm_vcpu *vcpu)
  164. {
  165. to_svm(vcpu)->asid_generation--;
  166. }
  167. static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
  168. {
  169. force_new_asid(vcpu);
  170. }
  171. static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  172. {
  173. if (!npt_enabled && !(efer & EFER_LMA))
  174. efer &= ~EFER_LME;
  175. to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
  176. vcpu->arch.shadow_efer = efer;
  177. }
  178. static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
  179. bool has_error_code, u32 error_code)
  180. {
  181. struct vcpu_svm *svm = to_svm(vcpu);
  182. /* If we are within a nested VM we'd better #VMEXIT and let the
  183. guest handle the exception */
  184. if (nested_svm_check_exception(svm, nr, has_error_code, error_code))
  185. return;
  186. svm->vmcb->control.event_inj = nr
  187. | SVM_EVTINJ_VALID
  188. | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
  189. | SVM_EVTINJ_TYPE_EXEPT;
  190. svm->vmcb->control.event_inj_err = error_code;
  191. }
  192. static bool svm_exception_injected(struct kvm_vcpu *vcpu)
  193. {
  194. struct vcpu_svm *svm = to_svm(vcpu);
  195. return !(svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID);
  196. }
  197. static int is_external_interrupt(u32 info)
  198. {
  199. info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
  200. return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
  201. }
  202. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  203. {
  204. struct vcpu_svm *svm = to_svm(vcpu);
  205. if (!svm->next_rip) {
  206. printk(KERN_DEBUG "%s: NOP\n", __func__);
  207. return;
  208. }
  209. if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
  210. printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
  211. __func__, kvm_rip_read(vcpu), svm->next_rip);
  212. kvm_rip_write(vcpu, svm->next_rip);
  213. svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
  214. vcpu->arch.interrupt_window_open = (svm->vcpu.arch.hflags & HF_GIF_MASK);
  215. }
  216. static int has_svm(void)
  217. {
  218. const char *msg;
  219. if (!cpu_has_svm(&msg)) {
  220. printk(KERN_INFO "has_svn: %s\n", msg);
  221. return 0;
  222. }
  223. return 1;
  224. }
  225. static void svm_hardware_disable(void *garbage)
  226. {
  227. cpu_svm_disable();
  228. }
  229. static void svm_hardware_enable(void *garbage)
  230. {
  231. struct svm_cpu_data *svm_data;
  232. uint64_t efer;
  233. struct desc_ptr gdt_descr;
  234. struct desc_struct *gdt;
  235. int me = raw_smp_processor_id();
  236. if (!has_svm()) {
  237. printk(KERN_ERR "svm_cpu_init: err EOPNOTSUPP on %d\n", me);
  238. return;
  239. }
  240. svm_data = per_cpu(svm_data, me);
  241. if (!svm_data) {
  242. printk(KERN_ERR "svm_cpu_init: svm_data is NULL on %d\n",
  243. me);
  244. return;
  245. }
  246. svm_data->asid_generation = 1;
  247. svm_data->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
  248. svm_data->next_asid = svm_data->max_asid + 1;
  249. asm volatile ("sgdt %0" : "=m"(gdt_descr));
  250. gdt = (struct desc_struct *)gdt_descr.address;
  251. svm_data->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
  252. rdmsrl(MSR_EFER, efer);
  253. wrmsrl(MSR_EFER, efer | EFER_SVME);
  254. wrmsrl(MSR_VM_HSAVE_PA,
  255. page_to_pfn(svm_data->save_area) << PAGE_SHIFT);
  256. }
  257. static void svm_cpu_uninit(int cpu)
  258. {
  259. struct svm_cpu_data *svm_data
  260. = per_cpu(svm_data, raw_smp_processor_id());
  261. if (!svm_data)
  262. return;
  263. per_cpu(svm_data, raw_smp_processor_id()) = NULL;
  264. __free_page(svm_data->save_area);
  265. kfree(svm_data);
  266. }
  267. static int svm_cpu_init(int cpu)
  268. {
  269. struct svm_cpu_data *svm_data;
  270. int r;
  271. svm_data = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
  272. if (!svm_data)
  273. return -ENOMEM;
  274. svm_data->cpu = cpu;
  275. svm_data->save_area = alloc_page(GFP_KERNEL);
  276. r = -ENOMEM;
  277. if (!svm_data->save_area)
  278. goto err_1;
  279. per_cpu(svm_data, cpu) = svm_data;
  280. return 0;
  281. err_1:
  282. kfree(svm_data);
  283. return r;
  284. }
  285. static void set_msr_interception(u32 *msrpm, unsigned msr,
  286. int read, int write)
  287. {
  288. int i;
  289. for (i = 0; i < NUM_MSR_MAPS; i++) {
  290. if (msr >= msrpm_ranges[i] &&
  291. msr < msrpm_ranges[i] + MSRS_IN_RANGE) {
  292. u32 msr_offset = (i * MSRS_IN_RANGE + msr -
  293. msrpm_ranges[i]) * 2;
  294. u32 *base = msrpm + (msr_offset / 32);
  295. u32 msr_shift = msr_offset % 32;
  296. u32 mask = ((write) ? 0 : 2) | ((read) ? 0 : 1);
  297. *base = (*base & ~(0x3 << msr_shift)) |
  298. (mask << msr_shift);
  299. return;
  300. }
  301. }
  302. BUG();
  303. }
  304. static void svm_vcpu_init_msrpm(u32 *msrpm)
  305. {
  306. memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
  307. #ifdef CONFIG_X86_64
  308. set_msr_interception(msrpm, MSR_GS_BASE, 1, 1);
  309. set_msr_interception(msrpm, MSR_FS_BASE, 1, 1);
  310. set_msr_interception(msrpm, MSR_KERNEL_GS_BASE, 1, 1);
  311. set_msr_interception(msrpm, MSR_LSTAR, 1, 1);
  312. set_msr_interception(msrpm, MSR_CSTAR, 1, 1);
  313. set_msr_interception(msrpm, MSR_SYSCALL_MASK, 1, 1);
  314. #endif
  315. set_msr_interception(msrpm, MSR_K6_STAR, 1, 1);
  316. set_msr_interception(msrpm, MSR_IA32_SYSENTER_CS, 1, 1);
  317. set_msr_interception(msrpm, MSR_IA32_SYSENTER_ESP, 1, 1);
  318. set_msr_interception(msrpm, MSR_IA32_SYSENTER_EIP, 1, 1);
  319. }
  320. static void svm_enable_lbrv(struct vcpu_svm *svm)
  321. {
  322. u32 *msrpm = svm->msrpm;
  323. svm->vmcb->control.lbr_ctl = 1;
  324. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
  325. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
  326. set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
  327. set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
  328. }
  329. static void svm_disable_lbrv(struct vcpu_svm *svm)
  330. {
  331. u32 *msrpm = svm->msrpm;
  332. svm->vmcb->control.lbr_ctl = 0;
  333. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
  334. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
  335. set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
  336. set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
  337. }
  338. static __init int svm_hardware_setup(void)
  339. {
  340. int cpu;
  341. struct page *iopm_pages;
  342. void *iopm_va;
  343. int r;
  344. iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
  345. if (!iopm_pages)
  346. return -ENOMEM;
  347. iopm_va = page_address(iopm_pages);
  348. memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
  349. clear_bit(0x80, iopm_va); /* allow direct access to PC debug port */
  350. iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
  351. if (boot_cpu_has(X86_FEATURE_NX))
  352. kvm_enable_efer_bits(EFER_NX);
  353. if (nested) {
  354. printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
  355. kvm_enable_efer_bits(EFER_SVME);
  356. }
  357. for_each_online_cpu(cpu) {
  358. r = svm_cpu_init(cpu);
  359. if (r)
  360. goto err;
  361. }
  362. svm_features = cpuid_edx(SVM_CPUID_FUNC);
  363. if (!svm_has(SVM_FEATURE_NPT))
  364. npt_enabled = false;
  365. if (npt_enabled && !npt) {
  366. printk(KERN_INFO "kvm: Nested Paging disabled\n");
  367. npt_enabled = false;
  368. }
  369. if (npt_enabled) {
  370. printk(KERN_INFO "kvm: Nested Paging enabled\n");
  371. kvm_enable_tdp();
  372. } else
  373. kvm_disable_tdp();
  374. return 0;
  375. err:
  376. __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
  377. iopm_base = 0;
  378. return r;
  379. }
  380. static __exit void svm_hardware_unsetup(void)
  381. {
  382. int cpu;
  383. for_each_online_cpu(cpu)
  384. svm_cpu_uninit(cpu);
  385. __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
  386. iopm_base = 0;
  387. }
  388. static void init_seg(struct vmcb_seg *seg)
  389. {
  390. seg->selector = 0;
  391. seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
  392. SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
  393. seg->limit = 0xffff;
  394. seg->base = 0;
  395. }
  396. static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
  397. {
  398. seg->selector = 0;
  399. seg->attrib = SVM_SELECTOR_P_MASK | type;
  400. seg->limit = 0xffff;
  401. seg->base = 0;
  402. }
  403. static void init_vmcb(struct vcpu_svm *svm)
  404. {
  405. struct vmcb_control_area *control = &svm->vmcb->control;
  406. struct vmcb_save_area *save = &svm->vmcb->save;
  407. control->intercept_cr_read = INTERCEPT_CR0_MASK |
  408. INTERCEPT_CR3_MASK |
  409. INTERCEPT_CR4_MASK;
  410. control->intercept_cr_write = INTERCEPT_CR0_MASK |
  411. INTERCEPT_CR3_MASK |
  412. INTERCEPT_CR4_MASK |
  413. INTERCEPT_CR8_MASK;
  414. control->intercept_dr_read = INTERCEPT_DR0_MASK |
  415. INTERCEPT_DR1_MASK |
  416. INTERCEPT_DR2_MASK |
  417. INTERCEPT_DR3_MASK;
  418. control->intercept_dr_write = INTERCEPT_DR0_MASK |
  419. INTERCEPT_DR1_MASK |
  420. INTERCEPT_DR2_MASK |
  421. INTERCEPT_DR3_MASK |
  422. INTERCEPT_DR5_MASK |
  423. INTERCEPT_DR7_MASK;
  424. control->intercept_exceptions = (1 << PF_VECTOR) |
  425. (1 << UD_VECTOR) |
  426. (1 << MC_VECTOR);
  427. control->intercept = (1ULL << INTERCEPT_INTR) |
  428. (1ULL << INTERCEPT_NMI) |
  429. (1ULL << INTERCEPT_SMI) |
  430. (1ULL << INTERCEPT_CPUID) |
  431. (1ULL << INTERCEPT_INVD) |
  432. (1ULL << INTERCEPT_HLT) |
  433. (1ULL << INTERCEPT_INVLPG) |
  434. (1ULL << INTERCEPT_INVLPGA) |
  435. (1ULL << INTERCEPT_IOIO_PROT) |
  436. (1ULL << INTERCEPT_MSR_PROT) |
  437. (1ULL << INTERCEPT_TASK_SWITCH) |
  438. (1ULL << INTERCEPT_SHUTDOWN) |
  439. (1ULL << INTERCEPT_VMRUN) |
  440. (1ULL << INTERCEPT_VMMCALL) |
  441. (1ULL << INTERCEPT_VMLOAD) |
  442. (1ULL << INTERCEPT_VMSAVE) |
  443. (1ULL << INTERCEPT_STGI) |
  444. (1ULL << INTERCEPT_CLGI) |
  445. (1ULL << INTERCEPT_SKINIT) |
  446. (1ULL << INTERCEPT_WBINVD) |
  447. (1ULL << INTERCEPT_MONITOR) |
  448. (1ULL << INTERCEPT_MWAIT);
  449. control->iopm_base_pa = iopm_base;
  450. control->msrpm_base_pa = __pa(svm->msrpm);
  451. control->tsc_offset = 0;
  452. control->int_ctl = V_INTR_MASKING_MASK;
  453. init_seg(&save->es);
  454. init_seg(&save->ss);
  455. init_seg(&save->ds);
  456. init_seg(&save->fs);
  457. init_seg(&save->gs);
  458. save->cs.selector = 0xf000;
  459. /* Executable/Readable Code Segment */
  460. save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
  461. SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
  462. save->cs.limit = 0xffff;
  463. /*
  464. * cs.base should really be 0xffff0000, but vmx can't handle that, so
  465. * be consistent with it.
  466. *
  467. * Replace when we have real mode working for vmx.
  468. */
  469. save->cs.base = 0xf0000;
  470. save->gdtr.limit = 0xffff;
  471. save->idtr.limit = 0xffff;
  472. init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
  473. init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
  474. save->efer = EFER_SVME;
  475. save->dr6 = 0xffff0ff0;
  476. save->dr7 = 0x400;
  477. save->rflags = 2;
  478. save->rip = 0x0000fff0;
  479. svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
  480. /*
  481. * cr0 val on cpu init should be 0x60000010, we enable cpu
  482. * cache by default. the orderly way is to enable cache in bios.
  483. */
  484. save->cr0 = 0x00000010 | X86_CR0_PG | X86_CR0_WP;
  485. save->cr4 = X86_CR4_PAE;
  486. /* rdx = ?? */
  487. if (npt_enabled) {
  488. /* Setup VMCB for Nested Paging */
  489. control->nested_ctl = 1;
  490. control->intercept &= ~((1ULL << INTERCEPT_TASK_SWITCH) |
  491. (1ULL << INTERCEPT_INVLPG));
  492. control->intercept_exceptions &= ~(1 << PF_VECTOR);
  493. control->intercept_cr_read &= ~(INTERCEPT_CR0_MASK|
  494. INTERCEPT_CR3_MASK);
  495. control->intercept_cr_write &= ~(INTERCEPT_CR0_MASK|
  496. INTERCEPT_CR3_MASK);
  497. save->g_pat = 0x0007040600070406ULL;
  498. /* enable caching because the QEMU Bios doesn't enable it */
  499. save->cr0 = X86_CR0_ET;
  500. save->cr3 = 0;
  501. save->cr4 = 0;
  502. }
  503. force_new_asid(&svm->vcpu);
  504. svm->nested_vmcb = 0;
  505. svm->vcpu.arch.hflags = HF_GIF_MASK;
  506. }
  507. static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
  508. {
  509. struct vcpu_svm *svm = to_svm(vcpu);
  510. init_vmcb(svm);
  511. if (vcpu->vcpu_id != 0) {
  512. kvm_rip_write(vcpu, 0);
  513. svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
  514. svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
  515. }
  516. vcpu->arch.regs_avail = ~0;
  517. vcpu->arch.regs_dirty = ~0;
  518. return 0;
  519. }
  520. static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
  521. {
  522. struct vcpu_svm *svm;
  523. struct page *page;
  524. struct page *msrpm_pages;
  525. struct page *hsave_page;
  526. struct page *nested_msrpm_pages;
  527. int err;
  528. svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  529. if (!svm) {
  530. err = -ENOMEM;
  531. goto out;
  532. }
  533. err = kvm_vcpu_init(&svm->vcpu, kvm, id);
  534. if (err)
  535. goto free_svm;
  536. page = alloc_page(GFP_KERNEL);
  537. if (!page) {
  538. err = -ENOMEM;
  539. goto uninit;
  540. }
  541. err = -ENOMEM;
  542. msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
  543. if (!msrpm_pages)
  544. goto uninit;
  545. nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
  546. if (!nested_msrpm_pages)
  547. goto uninit;
  548. svm->msrpm = page_address(msrpm_pages);
  549. svm_vcpu_init_msrpm(svm->msrpm);
  550. hsave_page = alloc_page(GFP_KERNEL);
  551. if (!hsave_page)
  552. goto uninit;
  553. svm->hsave = page_address(hsave_page);
  554. svm->nested_msrpm = page_address(nested_msrpm_pages);
  555. svm->vmcb = page_address(page);
  556. clear_page(svm->vmcb);
  557. svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
  558. svm->asid_generation = 0;
  559. memset(svm->db_regs, 0, sizeof(svm->db_regs));
  560. init_vmcb(svm);
  561. fx_init(&svm->vcpu);
  562. svm->vcpu.fpu_active = 1;
  563. svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
  564. if (svm->vcpu.vcpu_id == 0)
  565. svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
  566. return &svm->vcpu;
  567. uninit:
  568. kvm_vcpu_uninit(&svm->vcpu);
  569. free_svm:
  570. kmem_cache_free(kvm_vcpu_cache, svm);
  571. out:
  572. return ERR_PTR(err);
  573. }
  574. static void svm_free_vcpu(struct kvm_vcpu *vcpu)
  575. {
  576. struct vcpu_svm *svm = to_svm(vcpu);
  577. __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
  578. __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
  579. __free_page(virt_to_page(svm->hsave));
  580. __free_pages(virt_to_page(svm->nested_msrpm), MSRPM_ALLOC_ORDER);
  581. kvm_vcpu_uninit(vcpu);
  582. kmem_cache_free(kvm_vcpu_cache, svm);
  583. }
  584. static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  585. {
  586. struct vcpu_svm *svm = to_svm(vcpu);
  587. int i;
  588. if (unlikely(cpu != vcpu->cpu)) {
  589. u64 tsc_this, delta;
  590. /*
  591. * Make sure that the guest sees a monotonically
  592. * increasing TSC.
  593. */
  594. rdtscll(tsc_this);
  595. delta = vcpu->arch.host_tsc - tsc_this;
  596. svm->vmcb->control.tsc_offset += delta;
  597. vcpu->cpu = cpu;
  598. kvm_migrate_timers(vcpu);
  599. }
  600. for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
  601. rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
  602. }
  603. static void svm_vcpu_put(struct kvm_vcpu *vcpu)
  604. {
  605. struct vcpu_svm *svm = to_svm(vcpu);
  606. int i;
  607. ++vcpu->stat.host_state_reload;
  608. for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
  609. wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
  610. rdtscll(vcpu->arch.host_tsc);
  611. }
  612. static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
  613. {
  614. return to_svm(vcpu)->vmcb->save.rflags;
  615. }
  616. static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  617. {
  618. to_svm(vcpu)->vmcb->save.rflags = rflags;
  619. }
  620. static void svm_set_vintr(struct vcpu_svm *svm)
  621. {
  622. svm->vmcb->control.intercept |= 1ULL << INTERCEPT_VINTR;
  623. }
  624. static void svm_clear_vintr(struct vcpu_svm *svm)
  625. {
  626. svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
  627. }
  628. static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
  629. {
  630. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  631. switch (seg) {
  632. case VCPU_SREG_CS: return &save->cs;
  633. case VCPU_SREG_DS: return &save->ds;
  634. case VCPU_SREG_ES: return &save->es;
  635. case VCPU_SREG_FS: return &save->fs;
  636. case VCPU_SREG_GS: return &save->gs;
  637. case VCPU_SREG_SS: return &save->ss;
  638. case VCPU_SREG_TR: return &save->tr;
  639. case VCPU_SREG_LDTR: return &save->ldtr;
  640. }
  641. BUG();
  642. return NULL;
  643. }
  644. static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  645. {
  646. struct vmcb_seg *s = svm_seg(vcpu, seg);
  647. return s->base;
  648. }
  649. static void svm_get_segment(struct kvm_vcpu *vcpu,
  650. struct kvm_segment *var, int seg)
  651. {
  652. struct vmcb_seg *s = svm_seg(vcpu, seg);
  653. var->base = s->base;
  654. var->limit = s->limit;
  655. var->selector = s->selector;
  656. var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
  657. var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
  658. var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
  659. var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
  660. var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
  661. var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
  662. var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
  663. var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
  664. /*
  665. * SVM always stores 0 for the 'G' bit in the CS selector in
  666. * the VMCB on a VMEXIT. This hurts cross-vendor migration:
  667. * Intel's VMENTRY has a check on the 'G' bit.
  668. */
  669. if (seg == VCPU_SREG_CS)
  670. var->g = s->limit > 0xfffff;
  671. /*
  672. * Work around a bug where the busy flag in the tr selector
  673. * isn't exposed
  674. */
  675. if (seg == VCPU_SREG_TR)
  676. var->type |= 0x2;
  677. var->unusable = !var->present;
  678. }
  679. static int svm_get_cpl(struct kvm_vcpu *vcpu)
  680. {
  681. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  682. return save->cpl;
  683. }
  684. static void svm_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  685. {
  686. struct vcpu_svm *svm = to_svm(vcpu);
  687. dt->limit = svm->vmcb->save.idtr.limit;
  688. dt->base = svm->vmcb->save.idtr.base;
  689. }
  690. static void svm_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  691. {
  692. struct vcpu_svm *svm = to_svm(vcpu);
  693. svm->vmcb->save.idtr.limit = dt->limit;
  694. svm->vmcb->save.idtr.base = dt->base ;
  695. }
  696. static void svm_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  697. {
  698. struct vcpu_svm *svm = to_svm(vcpu);
  699. dt->limit = svm->vmcb->save.gdtr.limit;
  700. dt->base = svm->vmcb->save.gdtr.base;
  701. }
  702. static void svm_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  703. {
  704. struct vcpu_svm *svm = to_svm(vcpu);
  705. svm->vmcb->save.gdtr.limit = dt->limit;
  706. svm->vmcb->save.gdtr.base = dt->base ;
  707. }
  708. static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  709. {
  710. }
  711. static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  712. {
  713. struct vcpu_svm *svm = to_svm(vcpu);
  714. #ifdef CONFIG_X86_64
  715. if (vcpu->arch.shadow_efer & EFER_LME) {
  716. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
  717. vcpu->arch.shadow_efer |= EFER_LMA;
  718. svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
  719. }
  720. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
  721. vcpu->arch.shadow_efer &= ~EFER_LMA;
  722. svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
  723. }
  724. }
  725. #endif
  726. if (npt_enabled)
  727. goto set;
  728. if ((vcpu->arch.cr0 & X86_CR0_TS) && !(cr0 & X86_CR0_TS)) {
  729. svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
  730. vcpu->fpu_active = 1;
  731. }
  732. vcpu->arch.cr0 = cr0;
  733. cr0 |= X86_CR0_PG | X86_CR0_WP;
  734. if (!vcpu->fpu_active) {
  735. svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
  736. cr0 |= X86_CR0_TS;
  737. }
  738. set:
  739. /*
  740. * re-enable caching here because the QEMU bios
  741. * does not do it - this results in some delay at
  742. * reboot
  743. */
  744. cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
  745. svm->vmcb->save.cr0 = cr0;
  746. }
  747. static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  748. {
  749. unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
  750. unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
  751. if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
  752. force_new_asid(vcpu);
  753. vcpu->arch.cr4 = cr4;
  754. if (!npt_enabled)
  755. cr4 |= X86_CR4_PAE;
  756. cr4 |= host_cr4_mce;
  757. to_svm(vcpu)->vmcb->save.cr4 = cr4;
  758. }
  759. static void svm_set_segment(struct kvm_vcpu *vcpu,
  760. struct kvm_segment *var, int seg)
  761. {
  762. struct vcpu_svm *svm = to_svm(vcpu);
  763. struct vmcb_seg *s = svm_seg(vcpu, seg);
  764. s->base = var->base;
  765. s->limit = var->limit;
  766. s->selector = var->selector;
  767. if (var->unusable)
  768. s->attrib = 0;
  769. else {
  770. s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
  771. s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
  772. s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
  773. s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
  774. s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
  775. s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
  776. s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
  777. s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
  778. }
  779. if (seg == VCPU_SREG_CS)
  780. svm->vmcb->save.cpl
  781. = (svm->vmcb->save.cs.attrib
  782. >> SVM_SELECTOR_DPL_SHIFT) & 3;
  783. }
  784. static int svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
  785. {
  786. int old_debug = vcpu->guest_debug;
  787. struct vcpu_svm *svm = to_svm(vcpu);
  788. vcpu->guest_debug = dbg->control;
  789. svm->vmcb->control.intercept_exceptions &=
  790. ~((1 << DB_VECTOR) | (1 << BP_VECTOR));
  791. if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
  792. if (vcpu->guest_debug &
  793. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  794. svm->vmcb->control.intercept_exceptions |=
  795. 1 << DB_VECTOR;
  796. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  797. svm->vmcb->control.intercept_exceptions |=
  798. 1 << BP_VECTOR;
  799. } else
  800. vcpu->guest_debug = 0;
  801. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  802. svm->vmcb->save.rflags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
  803. else if (old_debug & KVM_GUESTDBG_SINGLESTEP)
  804. svm->vmcb->save.rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
  805. return 0;
  806. }
  807. static int svm_get_irq(struct kvm_vcpu *vcpu)
  808. {
  809. struct vcpu_svm *svm = to_svm(vcpu);
  810. u32 exit_int_info = svm->vmcb->control.exit_int_info;
  811. if (is_external_interrupt(exit_int_info))
  812. return exit_int_info & SVM_EVTINJ_VEC_MASK;
  813. return -1;
  814. }
  815. static void load_host_msrs(struct kvm_vcpu *vcpu)
  816. {
  817. #ifdef CONFIG_X86_64
  818. wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
  819. #endif
  820. }
  821. static void save_host_msrs(struct kvm_vcpu *vcpu)
  822. {
  823. #ifdef CONFIG_X86_64
  824. rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
  825. #endif
  826. }
  827. static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *svm_data)
  828. {
  829. if (svm_data->next_asid > svm_data->max_asid) {
  830. ++svm_data->asid_generation;
  831. svm_data->next_asid = 1;
  832. svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
  833. }
  834. svm->vcpu.cpu = svm_data->cpu;
  835. svm->asid_generation = svm_data->asid_generation;
  836. svm->vmcb->control.asid = svm_data->next_asid++;
  837. }
  838. static unsigned long svm_get_dr(struct kvm_vcpu *vcpu, int dr)
  839. {
  840. unsigned long val = to_svm(vcpu)->db_regs[dr];
  841. KVMTRACE_2D(DR_READ, vcpu, (u32)dr, (u32)val, handler);
  842. return val;
  843. }
  844. static void svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value,
  845. int *exception)
  846. {
  847. struct vcpu_svm *svm = to_svm(vcpu);
  848. *exception = 0;
  849. if (svm->vmcb->save.dr7 & DR7_GD_MASK) {
  850. svm->vmcb->save.dr7 &= ~DR7_GD_MASK;
  851. svm->vmcb->save.dr6 |= DR6_BD_MASK;
  852. *exception = DB_VECTOR;
  853. return;
  854. }
  855. switch (dr) {
  856. case 0 ... 3:
  857. svm->db_regs[dr] = value;
  858. return;
  859. case 4 ... 5:
  860. if (vcpu->arch.cr4 & X86_CR4_DE) {
  861. *exception = UD_VECTOR;
  862. return;
  863. }
  864. case 7: {
  865. if (value & ~((1ULL << 32) - 1)) {
  866. *exception = GP_VECTOR;
  867. return;
  868. }
  869. svm->vmcb->save.dr7 = value;
  870. return;
  871. }
  872. default:
  873. printk(KERN_DEBUG "%s: unexpected dr %u\n",
  874. __func__, dr);
  875. *exception = UD_VECTOR;
  876. return;
  877. }
  878. }
  879. static int pf_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  880. {
  881. u32 exit_int_info = svm->vmcb->control.exit_int_info;
  882. struct kvm *kvm = svm->vcpu.kvm;
  883. u64 fault_address;
  884. u32 error_code;
  885. bool event_injection = false;
  886. if (!irqchip_in_kernel(kvm) &&
  887. is_external_interrupt(exit_int_info)) {
  888. event_injection = true;
  889. push_irq(&svm->vcpu, exit_int_info & SVM_EVTINJ_VEC_MASK);
  890. }
  891. fault_address = svm->vmcb->control.exit_info_2;
  892. error_code = svm->vmcb->control.exit_info_1;
  893. if (!npt_enabled)
  894. KVMTRACE_3D(PAGE_FAULT, &svm->vcpu, error_code,
  895. (u32)fault_address, (u32)(fault_address >> 32),
  896. handler);
  897. else
  898. KVMTRACE_3D(TDP_FAULT, &svm->vcpu, error_code,
  899. (u32)fault_address, (u32)(fault_address >> 32),
  900. handler);
  901. /*
  902. * FIXME: Tis shouldn't be necessary here, but there is a flush
  903. * missing in the MMU code. Until we find this bug, flush the
  904. * complete TLB here on an NPF
  905. */
  906. if (npt_enabled)
  907. svm_flush_tlb(&svm->vcpu);
  908. if (!npt_enabled && event_injection)
  909. kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
  910. return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
  911. }
  912. static int db_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  913. {
  914. if (!(svm->vcpu.guest_debug &
  915. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
  916. kvm_queue_exception(&svm->vcpu, DB_VECTOR);
  917. return 1;
  918. }
  919. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  920. kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
  921. kvm_run->debug.arch.exception = DB_VECTOR;
  922. return 0;
  923. }
  924. static int bp_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  925. {
  926. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  927. kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
  928. kvm_run->debug.arch.exception = BP_VECTOR;
  929. return 0;
  930. }
  931. static int ud_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  932. {
  933. int er;
  934. er = emulate_instruction(&svm->vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
  935. if (er != EMULATE_DONE)
  936. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  937. return 1;
  938. }
  939. static int nm_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  940. {
  941. svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
  942. if (!(svm->vcpu.arch.cr0 & X86_CR0_TS))
  943. svm->vmcb->save.cr0 &= ~X86_CR0_TS;
  944. svm->vcpu.fpu_active = 1;
  945. return 1;
  946. }
  947. static int mc_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  948. {
  949. /*
  950. * On an #MC intercept the MCE handler is not called automatically in
  951. * the host. So do it by hand here.
  952. */
  953. asm volatile (
  954. "int $0x12\n");
  955. /* not sure if we ever come back to this point */
  956. return 1;
  957. }
  958. static int shutdown_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  959. {
  960. /*
  961. * VMCB is undefined after a SHUTDOWN intercept
  962. * so reinitialize it.
  963. */
  964. clear_page(svm->vmcb);
  965. init_vmcb(svm);
  966. kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
  967. return 0;
  968. }
  969. static int io_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  970. {
  971. u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
  972. int size, down, in, string, rep;
  973. unsigned port;
  974. ++svm->vcpu.stat.io_exits;
  975. svm->next_rip = svm->vmcb->control.exit_info_2;
  976. string = (io_info & SVM_IOIO_STR_MASK) != 0;
  977. if (string) {
  978. if (emulate_instruction(&svm->vcpu,
  979. kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
  980. return 0;
  981. return 1;
  982. }
  983. in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
  984. port = io_info >> 16;
  985. size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
  986. rep = (io_info & SVM_IOIO_REP_MASK) != 0;
  987. down = (svm->vmcb->save.rflags & X86_EFLAGS_DF) != 0;
  988. skip_emulated_instruction(&svm->vcpu);
  989. return kvm_emulate_pio(&svm->vcpu, kvm_run, in, size, port);
  990. }
  991. static int nmi_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  992. {
  993. KVMTRACE_0D(NMI, &svm->vcpu, handler);
  994. return 1;
  995. }
  996. static int intr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  997. {
  998. ++svm->vcpu.stat.irq_exits;
  999. KVMTRACE_0D(INTR, &svm->vcpu, handler);
  1000. return 1;
  1001. }
  1002. static int nop_on_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1003. {
  1004. return 1;
  1005. }
  1006. static int halt_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1007. {
  1008. svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
  1009. skip_emulated_instruction(&svm->vcpu);
  1010. return kvm_emulate_halt(&svm->vcpu);
  1011. }
  1012. static int vmmcall_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1013. {
  1014. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1015. skip_emulated_instruction(&svm->vcpu);
  1016. kvm_emulate_hypercall(&svm->vcpu);
  1017. return 1;
  1018. }
  1019. static int nested_svm_check_permissions(struct vcpu_svm *svm)
  1020. {
  1021. if (!(svm->vcpu.arch.shadow_efer & EFER_SVME)
  1022. || !is_paging(&svm->vcpu)) {
  1023. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  1024. return 1;
  1025. }
  1026. if (svm->vmcb->save.cpl) {
  1027. kvm_inject_gp(&svm->vcpu, 0);
  1028. return 1;
  1029. }
  1030. return 0;
  1031. }
  1032. static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
  1033. bool has_error_code, u32 error_code)
  1034. {
  1035. if (is_nested(svm)) {
  1036. svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
  1037. svm->vmcb->control.exit_code_hi = 0;
  1038. svm->vmcb->control.exit_info_1 = error_code;
  1039. svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
  1040. if (nested_svm_exit_handled(svm, false)) {
  1041. nsvm_printk("VMexit -> EXCP 0x%x\n", nr);
  1042. nested_svm_vmexit(svm);
  1043. return 1;
  1044. }
  1045. }
  1046. return 0;
  1047. }
  1048. static inline int nested_svm_intr(struct vcpu_svm *svm)
  1049. {
  1050. if (is_nested(svm)) {
  1051. if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
  1052. return 0;
  1053. if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
  1054. return 0;
  1055. svm->vmcb->control.exit_code = SVM_EXIT_INTR;
  1056. if (nested_svm_exit_handled(svm, false)) {
  1057. nsvm_printk("VMexit -> INTR\n");
  1058. nested_svm_vmexit(svm);
  1059. return 1;
  1060. }
  1061. }
  1062. return 0;
  1063. }
  1064. static struct page *nested_svm_get_page(struct vcpu_svm *svm, u64 gpa)
  1065. {
  1066. struct page *page;
  1067. down_read(&current->mm->mmap_sem);
  1068. page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
  1069. up_read(&current->mm->mmap_sem);
  1070. if (is_error_page(page)) {
  1071. printk(KERN_INFO "%s: could not find page at 0x%llx\n",
  1072. __func__, gpa);
  1073. kvm_release_page_clean(page);
  1074. kvm_inject_gp(&svm->vcpu, 0);
  1075. return NULL;
  1076. }
  1077. return page;
  1078. }
  1079. static int nested_svm_do(struct vcpu_svm *svm,
  1080. u64 arg1_gpa, u64 arg2_gpa, void *opaque,
  1081. int (*handler)(struct vcpu_svm *svm,
  1082. void *arg1,
  1083. void *arg2,
  1084. void *opaque))
  1085. {
  1086. struct page *arg1_page;
  1087. struct page *arg2_page = NULL;
  1088. void *arg1;
  1089. void *arg2 = NULL;
  1090. int retval;
  1091. arg1_page = nested_svm_get_page(svm, arg1_gpa);
  1092. if(arg1_page == NULL)
  1093. return 1;
  1094. if (arg2_gpa) {
  1095. arg2_page = nested_svm_get_page(svm, arg2_gpa);
  1096. if(arg2_page == NULL) {
  1097. kvm_release_page_clean(arg1_page);
  1098. return 1;
  1099. }
  1100. }
  1101. arg1 = kmap_atomic(arg1_page, KM_USER0);
  1102. if (arg2_gpa)
  1103. arg2 = kmap_atomic(arg2_page, KM_USER1);
  1104. retval = handler(svm, arg1, arg2, opaque);
  1105. kunmap_atomic(arg1, KM_USER0);
  1106. if (arg2_gpa)
  1107. kunmap_atomic(arg2, KM_USER1);
  1108. kvm_release_page_dirty(arg1_page);
  1109. if (arg2_gpa)
  1110. kvm_release_page_dirty(arg2_page);
  1111. return retval;
  1112. }
  1113. static int nested_svm_exit_handled_real(struct vcpu_svm *svm,
  1114. void *arg1,
  1115. void *arg2,
  1116. void *opaque)
  1117. {
  1118. struct vmcb *nested_vmcb = (struct vmcb *)arg1;
  1119. bool kvm_overrides = *(bool *)opaque;
  1120. u32 exit_code = svm->vmcb->control.exit_code;
  1121. if (kvm_overrides) {
  1122. switch (exit_code) {
  1123. case SVM_EXIT_INTR:
  1124. case SVM_EXIT_NMI:
  1125. return 0;
  1126. /* For now we are always handling NPFs when using them */
  1127. case SVM_EXIT_NPF:
  1128. if (npt_enabled)
  1129. return 0;
  1130. break;
  1131. /* When we're shadowing, trap PFs */
  1132. case SVM_EXIT_EXCP_BASE + PF_VECTOR:
  1133. if (!npt_enabled)
  1134. return 0;
  1135. break;
  1136. default:
  1137. break;
  1138. }
  1139. }
  1140. switch (exit_code) {
  1141. case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR8: {
  1142. u32 cr_bits = 1 << (exit_code - SVM_EXIT_READ_CR0);
  1143. if (nested_vmcb->control.intercept_cr_read & cr_bits)
  1144. return 1;
  1145. break;
  1146. }
  1147. case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR8: {
  1148. u32 cr_bits = 1 << (exit_code - SVM_EXIT_WRITE_CR0);
  1149. if (nested_vmcb->control.intercept_cr_write & cr_bits)
  1150. return 1;
  1151. break;
  1152. }
  1153. case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR7: {
  1154. u32 dr_bits = 1 << (exit_code - SVM_EXIT_READ_DR0);
  1155. if (nested_vmcb->control.intercept_dr_read & dr_bits)
  1156. return 1;
  1157. break;
  1158. }
  1159. case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR7: {
  1160. u32 dr_bits = 1 << (exit_code - SVM_EXIT_WRITE_DR0);
  1161. if (nested_vmcb->control.intercept_dr_write & dr_bits)
  1162. return 1;
  1163. break;
  1164. }
  1165. case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
  1166. u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
  1167. if (nested_vmcb->control.intercept_exceptions & excp_bits)
  1168. return 1;
  1169. break;
  1170. }
  1171. default: {
  1172. u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
  1173. nsvm_printk("exit code: 0x%x\n", exit_code);
  1174. if (nested_vmcb->control.intercept & exit_bits)
  1175. return 1;
  1176. }
  1177. }
  1178. return 0;
  1179. }
  1180. static int nested_svm_exit_handled_msr(struct vcpu_svm *svm,
  1181. void *arg1, void *arg2,
  1182. void *opaque)
  1183. {
  1184. struct vmcb *nested_vmcb = (struct vmcb *)arg1;
  1185. u8 *msrpm = (u8 *)arg2;
  1186. u32 t0, t1;
  1187. u32 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  1188. u32 param = svm->vmcb->control.exit_info_1 & 1;
  1189. if (!(nested_vmcb->control.intercept & (1ULL << INTERCEPT_MSR_PROT)))
  1190. return 0;
  1191. switch(msr) {
  1192. case 0 ... 0x1fff:
  1193. t0 = (msr * 2) % 8;
  1194. t1 = msr / 8;
  1195. break;
  1196. case 0xc0000000 ... 0xc0001fff:
  1197. t0 = (8192 + msr - 0xc0000000) * 2;
  1198. t1 = (t0 / 8);
  1199. t0 %= 8;
  1200. break;
  1201. case 0xc0010000 ... 0xc0011fff:
  1202. t0 = (16384 + msr - 0xc0010000) * 2;
  1203. t1 = (t0 / 8);
  1204. t0 %= 8;
  1205. break;
  1206. default:
  1207. return 1;
  1208. break;
  1209. }
  1210. if (msrpm[t1] & ((1 << param) << t0))
  1211. return 1;
  1212. return 0;
  1213. }
  1214. static int nested_svm_exit_handled(struct vcpu_svm *svm, bool kvm_override)
  1215. {
  1216. bool k = kvm_override;
  1217. switch (svm->vmcb->control.exit_code) {
  1218. case SVM_EXIT_MSR:
  1219. return nested_svm_do(svm, svm->nested_vmcb,
  1220. svm->nested_vmcb_msrpm, NULL,
  1221. nested_svm_exit_handled_msr);
  1222. default: break;
  1223. }
  1224. return nested_svm_do(svm, svm->nested_vmcb, 0, &k,
  1225. nested_svm_exit_handled_real);
  1226. }
  1227. static int nested_svm_vmexit_real(struct vcpu_svm *svm, void *arg1,
  1228. void *arg2, void *opaque)
  1229. {
  1230. struct vmcb *nested_vmcb = (struct vmcb *)arg1;
  1231. struct vmcb *hsave = svm->hsave;
  1232. u64 nested_save[] = { nested_vmcb->save.cr0,
  1233. nested_vmcb->save.cr3,
  1234. nested_vmcb->save.cr4,
  1235. nested_vmcb->save.efer,
  1236. nested_vmcb->control.intercept_cr_read,
  1237. nested_vmcb->control.intercept_cr_write,
  1238. nested_vmcb->control.intercept_dr_read,
  1239. nested_vmcb->control.intercept_dr_write,
  1240. nested_vmcb->control.intercept_exceptions,
  1241. nested_vmcb->control.intercept,
  1242. nested_vmcb->control.msrpm_base_pa,
  1243. nested_vmcb->control.iopm_base_pa,
  1244. nested_vmcb->control.tsc_offset };
  1245. /* Give the current vmcb to the guest */
  1246. memcpy(nested_vmcb, svm->vmcb, sizeof(struct vmcb));
  1247. nested_vmcb->save.cr0 = nested_save[0];
  1248. if (!npt_enabled)
  1249. nested_vmcb->save.cr3 = nested_save[1];
  1250. nested_vmcb->save.cr4 = nested_save[2];
  1251. nested_vmcb->save.efer = nested_save[3];
  1252. nested_vmcb->control.intercept_cr_read = nested_save[4];
  1253. nested_vmcb->control.intercept_cr_write = nested_save[5];
  1254. nested_vmcb->control.intercept_dr_read = nested_save[6];
  1255. nested_vmcb->control.intercept_dr_write = nested_save[7];
  1256. nested_vmcb->control.intercept_exceptions = nested_save[8];
  1257. nested_vmcb->control.intercept = nested_save[9];
  1258. nested_vmcb->control.msrpm_base_pa = nested_save[10];
  1259. nested_vmcb->control.iopm_base_pa = nested_save[11];
  1260. nested_vmcb->control.tsc_offset = nested_save[12];
  1261. /* We always set V_INTR_MASKING and remember the old value in hflags */
  1262. if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
  1263. nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
  1264. if ((nested_vmcb->control.int_ctl & V_IRQ_MASK) &&
  1265. (nested_vmcb->control.int_vector)) {
  1266. nsvm_printk("WARNING: IRQ 0x%x still enabled on #VMEXIT\n",
  1267. nested_vmcb->control.int_vector);
  1268. }
  1269. /* Restore the original control entries */
  1270. svm->vmcb->control = hsave->control;
  1271. /* Kill any pending exceptions */
  1272. if (svm->vcpu.arch.exception.pending == true)
  1273. nsvm_printk("WARNING: Pending Exception\n");
  1274. svm->vcpu.arch.exception.pending = false;
  1275. /* Restore selected save entries */
  1276. svm->vmcb->save.es = hsave->save.es;
  1277. svm->vmcb->save.cs = hsave->save.cs;
  1278. svm->vmcb->save.ss = hsave->save.ss;
  1279. svm->vmcb->save.ds = hsave->save.ds;
  1280. svm->vmcb->save.gdtr = hsave->save.gdtr;
  1281. svm->vmcb->save.idtr = hsave->save.idtr;
  1282. svm->vmcb->save.rflags = hsave->save.rflags;
  1283. svm_set_efer(&svm->vcpu, hsave->save.efer);
  1284. svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
  1285. svm_set_cr4(&svm->vcpu, hsave->save.cr4);
  1286. if (npt_enabled) {
  1287. svm->vmcb->save.cr3 = hsave->save.cr3;
  1288. svm->vcpu.arch.cr3 = hsave->save.cr3;
  1289. } else {
  1290. kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
  1291. }
  1292. kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
  1293. kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
  1294. kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
  1295. svm->vmcb->save.dr7 = 0;
  1296. svm->vmcb->save.cpl = 0;
  1297. svm->vmcb->control.exit_int_info = 0;
  1298. svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
  1299. /* Exit nested SVM mode */
  1300. svm->nested_vmcb = 0;
  1301. return 0;
  1302. }
  1303. static int nested_svm_vmexit(struct vcpu_svm *svm)
  1304. {
  1305. nsvm_printk("VMexit\n");
  1306. if (nested_svm_do(svm, svm->nested_vmcb, 0,
  1307. NULL, nested_svm_vmexit_real))
  1308. return 1;
  1309. kvm_mmu_reset_context(&svm->vcpu);
  1310. kvm_mmu_load(&svm->vcpu);
  1311. return 0;
  1312. }
  1313. static int nested_svm_vmrun_msrpm(struct vcpu_svm *svm, void *arg1,
  1314. void *arg2, void *opaque)
  1315. {
  1316. int i;
  1317. u32 *nested_msrpm = (u32*)arg1;
  1318. for (i=0; i< PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER) / 4; i++)
  1319. svm->nested_msrpm[i] = svm->msrpm[i] | nested_msrpm[i];
  1320. svm->vmcb->control.msrpm_base_pa = __pa(svm->nested_msrpm);
  1321. return 0;
  1322. }
  1323. static int nested_svm_vmrun(struct vcpu_svm *svm, void *arg1,
  1324. void *arg2, void *opaque)
  1325. {
  1326. struct vmcb *nested_vmcb = (struct vmcb *)arg1;
  1327. struct vmcb *hsave = svm->hsave;
  1328. /* nested_vmcb is our indicator if nested SVM is activated */
  1329. svm->nested_vmcb = svm->vmcb->save.rax;
  1330. /* Clear internal status */
  1331. svm->vcpu.arch.exception.pending = false;
  1332. /* Save the old vmcb, so we don't need to pick what we save, but
  1333. can restore everything when a VMEXIT occurs */
  1334. memcpy(hsave, svm->vmcb, sizeof(struct vmcb));
  1335. /* We need to remember the original CR3 in the SPT case */
  1336. if (!npt_enabled)
  1337. hsave->save.cr3 = svm->vcpu.arch.cr3;
  1338. hsave->save.cr4 = svm->vcpu.arch.cr4;
  1339. hsave->save.rip = svm->next_rip;
  1340. if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
  1341. svm->vcpu.arch.hflags |= HF_HIF_MASK;
  1342. else
  1343. svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
  1344. /* Load the nested guest state */
  1345. svm->vmcb->save.es = nested_vmcb->save.es;
  1346. svm->vmcb->save.cs = nested_vmcb->save.cs;
  1347. svm->vmcb->save.ss = nested_vmcb->save.ss;
  1348. svm->vmcb->save.ds = nested_vmcb->save.ds;
  1349. svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
  1350. svm->vmcb->save.idtr = nested_vmcb->save.idtr;
  1351. svm->vmcb->save.rflags = nested_vmcb->save.rflags;
  1352. svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
  1353. svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
  1354. svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
  1355. if (npt_enabled) {
  1356. svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
  1357. svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
  1358. } else {
  1359. kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
  1360. kvm_mmu_reset_context(&svm->vcpu);
  1361. }
  1362. svm->vmcb->save.cr2 = nested_vmcb->save.cr2;
  1363. kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
  1364. kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
  1365. kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
  1366. /* In case we don't even reach vcpu_run, the fields are not updated */
  1367. svm->vmcb->save.rax = nested_vmcb->save.rax;
  1368. svm->vmcb->save.rsp = nested_vmcb->save.rsp;
  1369. svm->vmcb->save.rip = nested_vmcb->save.rip;
  1370. svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
  1371. svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
  1372. svm->vmcb->save.cpl = nested_vmcb->save.cpl;
  1373. /* We don't want a nested guest to be more powerful than the guest,
  1374. so all intercepts are ORed */
  1375. svm->vmcb->control.intercept_cr_read |=
  1376. nested_vmcb->control.intercept_cr_read;
  1377. svm->vmcb->control.intercept_cr_write |=
  1378. nested_vmcb->control.intercept_cr_write;
  1379. svm->vmcb->control.intercept_dr_read |=
  1380. nested_vmcb->control.intercept_dr_read;
  1381. svm->vmcb->control.intercept_dr_write |=
  1382. nested_vmcb->control.intercept_dr_write;
  1383. svm->vmcb->control.intercept_exceptions |=
  1384. nested_vmcb->control.intercept_exceptions;
  1385. svm->vmcb->control.intercept |= nested_vmcb->control.intercept;
  1386. svm->nested_vmcb_msrpm = nested_vmcb->control.msrpm_base_pa;
  1387. force_new_asid(&svm->vcpu);
  1388. svm->vmcb->control.exit_int_info = nested_vmcb->control.exit_int_info;
  1389. svm->vmcb->control.exit_int_info_err = nested_vmcb->control.exit_int_info_err;
  1390. svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
  1391. if (nested_vmcb->control.int_ctl & V_IRQ_MASK) {
  1392. nsvm_printk("nSVM Injecting Interrupt: 0x%x\n",
  1393. nested_vmcb->control.int_ctl);
  1394. }
  1395. if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
  1396. svm->vcpu.arch.hflags |= HF_VINTR_MASK;
  1397. else
  1398. svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
  1399. nsvm_printk("nSVM exit_int_info: 0x%x | int_state: 0x%x\n",
  1400. nested_vmcb->control.exit_int_info,
  1401. nested_vmcb->control.int_state);
  1402. svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
  1403. svm->vmcb->control.int_state = nested_vmcb->control.int_state;
  1404. svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
  1405. if (nested_vmcb->control.event_inj & SVM_EVTINJ_VALID)
  1406. nsvm_printk("Injecting Event: 0x%x\n",
  1407. nested_vmcb->control.event_inj);
  1408. svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
  1409. svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
  1410. svm->vcpu.arch.hflags |= HF_GIF_MASK;
  1411. return 0;
  1412. }
  1413. static int nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
  1414. {
  1415. to_vmcb->save.fs = from_vmcb->save.fs;
  1416. to_vmcb->save.gs = from_vmcb->save.gs;
  1417. to_vmcb->save.tr = from_vmcb->save.tr;
  1418. to_vmcb->save.ldtr = from_vmcb->save.ldtr;
  1419. to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
  1420. to_vmcb->save.star = from_vmcb->save.star;
  1421. to_vmcb->save.lstar = from_vmcb->save.lstar;
  1422. to_vmcb->save.cstar = from_vmcb->save.cstar;
  1423. to_vmcb->save.sfmask = from_vmcb->save.sfmask;
  1424. to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
  1425. to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
  1426. to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
  1427. return 1;
  1428. }
  1429. static int nested_svm_vmload(struct vcpu_svm *svm, void *nested_vmcb,
  1430. void *arg2, void *opaque)
  1431. {
  1432. return nested_svm_vmloadsave((struct vmcb *)nested_vmcb, svm->vmcb);
  1433. }
  1434. static int nested_svm_vmsave(struct vcpu_svm *svm, void *nested_vmcb,
  1435. void *arg2, void *opaque)
  1436. {
  1437. return nested_svm_vmloadsave(svm->vmcb, (struct vmcb *)nested_vmcb);
  1438. }
  1439. static int vmload_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1440. {
  1441. if (nested_svm_check_permissions(svm))
  1442. return 1;
  1443. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1444. skip_emulated_instruction(&svm->vcpu);
  1445. nested_svm_do(svm, svm->vmcb->save.rax, 0, NULL, nested_svm_vmload);
  1446. return 1;
  1447. }
  1448. static int vmsave_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1449. {
  1450. if (nested_svm_check_permissions(svm))
  1451. return 1;
  1452. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1453. skip_emulated_instruction(&svm->vcpu);
  1454. nested_svm_do(svm, svm->vmcb->save.rax, 0, NULL, nested_svm_vmsave);
  1455. return 1;
  1456. }
  1457. static int vmrun_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1458. {
  1459. nsvm_printk("VMrun\n");
  1460. if (nested_svm_check_permissions(svm))
  1461. return 1;
  1462. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1463. skip_emulated_instruction(&svm->vcpu);
  1464. if (nested_svm_do(svm, svm->vmcb->save.rax, 0,
  1465. NULL, nested_svm_vmrun))
  1466. return 1;
  1467. if (nested_svm_do(svm, svm->nested_vmcb_msrpm, 0,
  1468. NULL, nested_svm_vmrun_msrpm))
  1469. return 1;
  1470. return 1;
  1471. }
  1472. static int stgi_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1473. {
  1474. if (nested_svm_check_permissions(svm))
  1475. return 1;
  1476. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1477. skip_emulated_instruction(&svm->vcpu);
  1478. svm->vcpu.arch.hflags |= HF_GIF_MASK;
  1479. return 1;
  1480. }
  1481. static int clgi_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1482. {
  1483. if (nested_svm_check_permissions(svm))
  1484. return 1;
  1485. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1486. skip_emulated_instruction(&svm->vcpu);
  1487. svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
  1488. /* After a CLGI no interrupts should come */
  1489. svm_clear_vintr(svm);
  1490. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  1491. return 1;
  1492. }
  1493. static int invalid_op_interception(struct vcpu_svm *svm,
  1494. struct kvm_run *kvm_run)
  1495. {
  1496. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  1497. return 1;
  1498. }
  1499. static int task_switch_interception(struct vcpu_svm *svm,
  1500. struct kvm_run *kvm_run)
  1501. {
  1502. u16 tss_selector;
  1503. tss_selector = (u16)svm->vmcb->control.exit_info_1;
  1504. if (svm->vmcb->control.exit_info_2 &
  1505. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
  1506. return kvm_task_switch(&svm->vcpu, tss_selector,
  1507. TASK_SWITCH_IRET);
  1508. if (svm->vmcb->control.exit_info_2 &
  1509. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
  1510. return kvm_task_switch(&svm->vcpu, tss_selector,
  1511. TASK_SWITCH_JMP);
  1512. return kvm_task_switch(&svm->vcpu, tss_selector, TASK_SWITCH_CALL);
  1513. }
  1514. static int cpuid_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1515. {
  1516. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  1517. kvm_emulate_cpuid(&svm->vcpu);
  1518. return 1;
  1519. }
  1520. static int invlpg_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1521. {
  1522. if (emulate_instruction(&svm->vcpu, kvm_run, 0, 0, 0) != EMULATE_DONE)
  1523. pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
  1524. return 1;
  1525. }
  1526. static int emulate_on_interception(struct vcpu_svm *svm,
  1527. struct kvm_run *kvm_run)
  1528. {
  1529. if (emulate_instruction(&svm->vcpu, NULL, 0, 0, 0) != EMULATE_DONE)
  1530. pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
  1531. return 1;
  1532. }
  1533. static int cr8_write_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1534. {
  1535. emulate_instruction(&svm->vcpu, NULL, 0, 0, 0);
  1536. if (irqchip_in_kernel(svm->vcpu.kvm))
  1537. return 1;
  1538. kvm_run->exit_reason = KVM_EXIT_SET_TPR;
  1539. return 0;
  1540. }
  1541. static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
  1542. {
  1543. struct vcpu_svm *svm = to_svm(vcpu);
  1544. switch (ecx) {
  1545. case MSR_IA32_TIME_STAMP_COUNTER: {
  1546. u64 tsc;
  1547. rdtscll(tsc);
  1548. *data = svm->vmcb->control.tsc_offset + tsc;
  1549. break;
  1550. }
  1551. case MSR_K6_STAR:
  1552. *data = svm->vmcb->save.star;
  1553. break;
  1554. #ifdef CONFIG_X86_64
  1555. case MSR_LSTAR:
  1556. *data = svm->vmcb->save.lstar;
  1557. break;
  1558. case MSR_CSTAR:
  1559. *data = svm->vmcb->save.cstar;
  1560. break;
  1561. case MSR_KERNEL_GS_BASE:
  1562. *data = svm->vmcb->save.kernel_gs_base;
  1563. break;
  1564. case MSR_SYSCALL_MASK:
  1565. *data = svm->vmcb->save.sfmask;
  1566. break;
  1567. #endif
  1568. case MSR_IA32_SYSENTER_CS:
  1569. *data = svm->vmcb->save.sysenter_cs;
  1570. break;
  1571. case MSR_IA32_SYSENTER_EIP:
  1572. *data = svm->vmcb->save.sysenter_eip;
  1573. break;
  1574. case MSR_IA32_SYSENTER_ESP:
  1575. *data = svm->vmcb->save.sysenter_esp;
  1576. break;
  1577. /* Nobody will change the following 5 values in the VMCB so
  1578. we can safely return them on rdmsr. They will always be 0
  1579. until LBRV is implemented. */
  1580. case MSR_IA32_DEBUGCTLMSR:
  1581. *data = svm->vmcb->save.dbgctl;
  1582. break;
  1583. case MSR_IA32_LASTBRANCHFROMIP:
  1584. *data = svm->vmcb->save.br_from;
  1585. break;
  1586. case MSR_IA32_LASTBRANCHTOIP:
  1587. *data = svm->vmcb->save.br_to;
  1588. break;
  1589. case MSR_IA32_LASTINTFROMIP:
  1590. *data = svm->vmcb->save.last_excp_from;
  1591. break;
  1592. case MSR_IA32_LASTINTTOIP:
  1593. *data = svm->vmcb->save.last_excp_to;
  1594. break;
  1595. case MSR_VM_HSAVE_PA:
  1596. *data = svm->hsave_msr;
  1597. break;
  1598. case MSR_VM_CR:
  1599. *data = 0;
  1600. break;
  1601. default:
  1602. return kvm_get_msr_common(vcpu, ecx, data);
  1603. }
  1604. return 0;
  1605. }
  1606. static int rdmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1607. {
  1608. u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  1609. u64 data;
  1610. if (svm_get_msr(&svm->vcpu, ecx, &data))
  1611. kvm_inject_gp(&svm->vcpu, 0);
  1612. else {
  1613. KVMTRACE_3D(MSR_READ, &svm->vcpu, ecx, (u32)data,
  1614. (u32)(data >> 32), handler);
  1615. svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
  1616. svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
  1617. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  1618. skip_emulated_instruction(&svm->vcpu);
  1619. }
  1620. return 1;
  1621. }
  1622. static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
  1623. {
  1624. struct vcpu_svm *svm = to_svm(vcpu);
  1625. switch (ecx) {
  1626. case MSR_IA32_TIME_STAMP_COUNTER: {
  1627. u64 tsc;
  1628. rdtscll(tsc);
  1629. svm->vmcb->control.tsc_offset = data - tsc;
  1630. break;
  1631. }
  1632. case MSR_K6_STAR:
  1633. svm->vmcb->save.star = data;
  1634. break;
  1635. #ifdef CONFIG_X86_64
  1636. case MSR_LSTAR:
  1637. svm->vmcb->save.lstar = data;
  1638. break;
  1639. case MSR_CSTAR:
  1640. svm->vmcb->save.cstar = data;
  1641. break;
  1642. case MSR_KERNEL_GS_BASE:
  1643. svm->vmcb->save.kernel_gs_base = data;
  1644. break;
  1645. case MSR_SYSCALL_MASK:
  1646. svm->vmcb->save.sfmask = data;
  1647. break;
  1648. #endif
  1649. case MSR_IA32_SYSENTER_CS:
  1650. svm->vmcb->save.sysenter_cs = data;
  1651. break;
  1652. case MSR_IA32_SYSENTER_EIP:
  1653. svm->vmcb->save.sysenter_eip = data;
  1654. break;
  1655. case MSR_IA32_SYSENTER_ESP:
  1656. svm->vmcb->save.sysenter_esp = data;
  1657. break;
  1658. case MSR_IA32_DEBUGCTLMSR:
  1659. if (!svm_has(SVM_FEATURE_LBRV)) {
  1660. pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
  1661. __func__, data);
  1662. break;
  1663. }
  1664. if (data & DEBUGCTL_RESERVED_BITS)
  1665. return 1;
  1666. svm->vmcb->save.dbgctl = data;
  1667. if (data & (1ULL<<0))
  1668. svm_enable_lbrv(svm);
  1669. else
  1670. svm_disable_lbrv(svm);
  1671. break;
  1672. case MSR_K7_EVNTSEL0:
  1673. case MSR_K7_EVNTSEL1:
  1674. case MSR_K7_EVNTSEL2:
  1675. case MSR_K7_EVNTSEL3:
  1676. case MSR_K7_PERFCTR0:
  1677. case MSR_K7_PERFCTR1:
  1678. case MSR_K7_PERFCTR2:
  1679. case MSR_K7_PERFCTR3:
  1680. /*
  1681. * Just discard all writes to the performance counters; this
  1682. * should keep both older linux and windows 64-bit guests
  1683. * happy
  1684. */
  1685. pr_unimpl(vcpu, "unimplemented perfctr wrmsr: 0x%x data 0x%llx\n", ecx, data);
  1686. break;
  1687. case MSR_VM_HSAVE_PA:
  1688. svm->hsave_msr = data;
  1689. break;
  1690. default:
  1691. return kvm_set_msr_common(vcpu, ecx, data);
  1692. }
  1693. return 0;
  1694. }
  1695. static int wrmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1696. {
  1697. u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  1698. u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
  1699. | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  1700. KVMTRACE_3D(MSR_WRITE, &svm->vcpu, ecx, (u32)data, (u32)(data >> 32),
  1701. handler);
  1702. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  1703. if (svm_set_msr(&svm->vcpu, ecx, data))
  1704. kvm_inject_gp(&svm->vcpu, 0);
  1705. else
  1706. skip_emulated_instruction(&svm->vcpu);
  1707. return 1;
  1708. }
  1709. static int msr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1710. {
  1711. if (svm->vmcb->control.exit_info_1)
  1712. return wrmsr_interception(svm, kvm_run);
  1713. else
  1714. return rdmsr_interception(svm, kvm_run);
  1715. }
  1716. static int interrupt_window_interception(struct vcpu_svm *svm,
  1717. struct kvm_run *kvm_run)
  1718. {
  1719. KVMTRACE_0D(PEND_INTR, &svm->vcpu, handler);
  1720. svm_clear_vintr(svm);
  1721. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  1722. /*
  1723. * If the user space waits to inject interrupts, exit as soon as
  1724. * possible
  1725. */
  1726. if (kvm_run->request_interrupt_window &&
  1727. !svm->vcpu.arch.irq_summary) {
  1728. ++svm->vcpu.stat.irq_window_exits;
  1729. kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  1730. return 0;
  1731. }
  1732. return 1;
  1733. }
  1734. static int (*svm_exit_handlers[])(struct vcpu_svm *svm,
  1735. struct kvm_run *kvm_run) = {
  1736. [SVM_EXIT_READ_CR0] = emulate_on_interception,
  1737. [SVM_EXIT_READ_CR3] = emulate_on_interception,
  1738. [SVM_EXIT_READ_CR4] = emulate_on_interception,
  1739. [SVM_EXIT_READ_CR8] = emulate_on_interception,
  1740. /* for now: */
  1741. [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
  1742. [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
  1743. [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
  1744. [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
  1745. [SVM_EXIT_READ_DR0] = emulate_on_interception,
  1746. [SVM_EXIT_READ_DR1] = emulate_on_interception,
  1747. [SVM_EXIT_READ_DR2] = emulate_on_interception,
  1748. [SVM_EXIT_READ_DR3] = emulate_on_interception,
  1749. [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
  1750. [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
  1751. [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
  1752. [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
  1753. [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
  1754. [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
  1755. [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
  1756. [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
  1757. [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
  1758. [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
  1759. [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
  1760. [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
  1761. [SVM_EXIT_INTR] = intr_interception,
  1762. [SVM_EXIT_NMI] = nmi_interception,
  1763. [SVM_EXIT_SMI] = nop_on_interception,
  1764. [SVM_EXIT_INIT] = nop_on_interception,
  1765. [SVM_EXIT_VINTR] = interrupt_window_interception,
  1766. /* [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, */
  1767. [SVM_EXIT_CPUID] = cpuid_interception,
  1768. [SVM_EXIT_INVD] = emulate_on_interception,
  1769. [SVM_EXIT_HLT] = halt_interception,
  1770. [SVM_EXIT_INVLPG] = invlpg_interception,
  1771. [SVM_EXIT_INVLPGA] = invalid_op_interception,
  1772. [SVM_EXIT_IOIO] = io_interception,
  1773. [SVM_EXIT_MSR] = msr_interception,
  1774. [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
  1775. [SVM_EXIT_SHUTDOWN] = shutdown_interception,
  1776. [SVM_EXIT_VMRUN] = vmrun_interception,
  1777. [SVM_EXIT_VMMCALL] = vmmcall_interception,
  1778. [SVM_EXIT_VMLOAD] = vmload_interception,
  1779. [SVM_EXIT_VMSAVE] = vmsave_interception,
  1780. [SVM_EXIT_STGI] = stgi_interception,
  1781. [SVM_EXIT_CLGI] = clgi_interception,
  1782. [SVM_EXIT_SKINIT] = invalid_op_interception,
  1783. [SVM_EXIT_WBINVD] = emulate_on_interception,
  1784. [SVM_EXIT_MONITOR] = invalid_op_interception,
  1785. [SVM_EXIT_MWAIT] = invalid_op_interception,
  1786. [SVM_EXIT_NPF] = pf_interception,
  1787. };
  1788. static int handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  1789. {
  1790. struct vcpu_svm *svm = to_svm(vcpu);
  1791. u32 exit_code = svm->vmcb->control.exit_code;
  1792. KVMTRACE_3D(VMEXIT, vcpu, exit_code, (u32)svm->vmcb->save.rip,
  1793. (u32)((u64)svm->vmcb->save.rip >> 32), entryexit);
  1794. if (is_nested(svm)) {
  1795. nsvm_printk("nested handle_exit: 0x%x | 0x%lx | 0x%lx | 0x%lx\n",
  1796. exit_code, svm->vmcb->control.exit_info_1,
  1797. svm->vmcb->control.exit_info_2, svm->vmcb->save.rip);
  1798. if (nested_svm_exit_handled(svm, true)) {
  1799. nested_svm_vmexit(svm);
  1800. nsvm_printk("-> #VMEXIT\n");
  1801. return 1;
  1802. }
  1803. }
  1804. if (npt_enabled) {
  1805. int mmu_reload = 0;
  1806. if ((vcpu->arch.cr0 ^ svm->vmcb->save.cr0) & X86_CR0_PG) {
  1807. svm_set_cr0(vcpu, svm->vmcb->save.cr0);
  1808. mmu_reload = 1;
  1809. }
  1810. vcpu->arch.cr0 = svm->vmcb->save.cr0;
  1811. vcpu->arch.cr3 = svm->vmcb->save.cr3;
  1812. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  1813. if (!load_pdptrs(vcpu, vcpu->arch.cr3)) {
  1814. kvm_inject_gp(vcpu, 0);
  1815. return 1;
  1816. }
  1817. }
  1818. if (mmu_reload) {
  1819. kvm_mmu_reset_context(vcpu);
  1820. kvm_mmu_load(vcpu);
  1821. }
  1822. }
  1823. kvm_reput_irq(svm);
  1824. if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
  1825. kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  1826. kvm_run->fail_entry.hardware_entry_failure_reason
  1827. = svm->vmcb->control.exit_code;
  1828. return 0;
  1829. }
  1830. if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
  1831. exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
  1832. exit_code != SVM_EXIT_NPF)
  1833. printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
  1834. "exit_code 0x%x\n",
  1835. __func__, svm->vmcb->control.exit_int_info,
  1836. exit_code);
  1837. if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
  1838. || !svm_exit_handlers[exit_code]) {
  1839. kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
  1840. kvm_run->hw.hardware_exit_reason = exit_code;
  1841. return 0;
  1842. }
  1843. return svm_exit_handlers[exit_code](svm, kvm_run);
  1844. }
  1845. static void reload_tss(struct kvm_vcpu *vcpu)
  1846. {
  1847. int cpu = raw_smp_processor_id();
  1848. struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
  1849. svm_data->tss_desc->type = 9; /* available 32/64-bit TSS */
  1850. load_TR_desc();
  1851. }
  1852. static void pre_svm_run(struct vcpu_svm *svm)
  1853. {
  1854. int cpu = raw_smp_processor_id();
  1855. struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
  1856. svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
  1857. if (svm->vcpu.cpu != cpu ||
  1858. svm->asid_generation != svm_data->asid_generation)
  1859. new_asid(svm, svm_data);
  1860. }
  1861. static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
  1862. {
  1863. struct vmcb_control_area *control;
  1864. KVMTRACE_1D(INJ_VIRQ, &svm->vcpu, (u32)irq, handler);
  1865. ++svm->vcpu.stat.irq_injections;
  1866. control = &svm->vmcb->control;
  1867. control->int_vector = irq;
  1868. control->int_ctl &= ~V_INTR_PRIO_MASK;
  1869. control->int_ctl |= V_IRQ_MASK |
  1870. ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
  1871. }
  1872. static void svm_set_irq(struct kvm_vcpu *vcpu, int irq)
  1873. {
  1874. struct vcpu_svm *svm = to_svm(vcpu);
  1875. nested_svm_intr(svm);
  1876. svm_inject_irq(svm, irq);
  1877. }
  1878. static void update_cr8_intercept(struct kvm_vcpu *vcpu)
  1879. {
  1880. struct vcpu_svm *svm = to_svm(vcpu);
  1881. struct vmcb *vmcb = svm->vmcb;
  1882. int max_irr, tpr;
  1883. if (!irqchip_in_kernel(vcpu->kvm) || vcpu->arch.apic->vapic_addr)
  1884. return;
  1885. vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
  1886. max_irr = kvm_lapic_find_highest_irr(vcpu);
  1887. if (max_irr == -1)
  1888. return;
  1889. tpr = kvm_lapic_get_cr8(vcpu) << 4;
  1890. if (tpr >= (max_irr & 0xf0))
  1891. vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK;
  1892. }
  1893. static void svm_intr_assist(struct kvm_vcpu *vcpu)
  1894. {
  1895. struct vcpu_svm *svm = to_svm(vcpu);
  1896. struct vmcb *vmcb = svm->vmcb;
  1897. int intr_vector = -1;
  1898. if ((vmcb->control.exit_int_info & SVM_EVTINJ_VALID) &&
  1899. ((vmcb->control.exit_int_info & SVM_EVTINJ_TYPE_MASK) == 0)) {
  1900. intr_vector = vmcb->control.exit_int_info &
  1901. SVM_EVTINJ_VEC_MASK;
  1902. vmcb->control.exit_int_info = 0;
  1903. svm_inject_irq(svm, intr_vector);
  1904. goto out;
  1905. }
  1906. if (vmcb->control.int_ctl & V_IRQ_MASK)
  1907. goto out;
  1908. if (!kvm_cpu_has_interrupt(vcpu))
  1909. goto out;
  1910. if (nested_svm_intr(svm))
  1911. goto out;
  1912. if (!(svm->vcpu.arch.hflags & HF_GIF_MASK))
  1913. goto out;
  1914. if (!(vmcb->save.rflags & X86_EFLAGS_IF) ||
  1915. (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) ||
  1916. (vmcb->control.event_inj & SVM_EVTINJ_VALID)) {
  1917. /* unable to deliver irq, set pending irq */
  1918. svm_set_vintr(svm);
  1919. svm_inject_irq(svm, 0x0);
  1920. goto out;
  1921. }
  1922. /* Okay, we can deliver the interrupt: grab it and update PIC state. */
  1923. intr_vector = kvm_cpu_get_interrupt(vcpu);
  1924. svm_inject_irq(svm, intr_vector);
  1925. out:
  1926. update_cr8_intercept(vcpu);
  1927. }
  1928. static void kvm_reput_irq(struct vcpu_svm *svm)
  1929. {
  1930. struct vmcb_control_area *control = &svm->vmcb->control;
  1931. if ((control->int_ctl & V_IRQ_MASK)
  1932. && !irqchip_in_kernel(svm->vcpu.kvm)) {
  1933. control->int_ctl &= ~V_IRQ_MASK;
  1934. push_irq(&svm->vcpu, control->int_vector);
  1935. }
  1936. svm->vcpu.arch.interrupt_window_open =
  1937. !(control->int_state & SVM_INTERRUPT_SHADOW_MASK) &&
  1938. (svm->vcpu.arch.hflags & HF_GIF_MASK);
  1939. }
  1940. static void svm_do_inject_vector(struct vcpu_svm *svm)
  1941. {
  1942. struct kvm_vcpu *vcpu = &svm->vcpu;
  1943. int word_index = __ffs(vcpu->arch.irq_summary);
  1944. int bit_index = __ffs(vcpu->arch.irq_pending[word_index]);
  1945. int irq = word_index * BITS_PER_LONG + bit_index;
  1946. clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]);
  1947. if (!vcpu->arch.irq_pending[word_index])
  1948. clear_bit(word_index, &vcpu->arch.irq_summary);
  1949. svm_inject_irq(svm, irq);
  1950. }
  1951. static void do_interrupt_requests(struct kvm_vcpu *vcpu,
  1952. struct kvm_run *kvm_run)
  1953. {
  1954. struct vcpu_svm *svm = to_svm(vcpu);
  1955. struct vmcb_control_area *control = &svm->vmcb->control;
  1956. if (nested_svm_intr(svm))
  1957. return;
  1958. svm->vcpu.arch.interrupt_window_open =
  1959. (!(control->int_state & SVM_INTERRUPT_SHADOW_MASK) &&
  1960. (svm->vmcb->save.rflags & X86_EFLAGS_IF) &&
  1961. (svm->vcpu.arch.hflags & HF_GIF_MASK));
  1962. if (svm->vcpu.arch.interrupt_window_open && svm->vcpu.arch.irq_summary)
  1963. /*
  1964. * If interrupts enabled, and not blocked by sti or mov ss. Good.
  1965. */
  1966. svm_do_inject_vector(svm);
  1967. /*
  1968. * Interrupts blocked. Wait for unblock.
  1969. */
  1970. if (!svm->vcpu.arch.interrupt_window_open &&
  1971. (svm->vcpu.arch.irq_summary || kvm_run->request_interrupt_window))
  1972. svm_set_vintr(svm);
  1973. else
  1974. svm_clear_vintr(svm);
  1975. }
  1976. static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
  1977. {
  1978. return 0;
  1979. }
  1980. static void save_db_regs(unsigned long *db_regs)
  1981. {
  1982. asm volatile ("mov %%dr0, %0" : "=r"(db_regs[0]));
  1983. asm volatile ("mov %%dr1, %0" : "=r"(db_regs[1]));
  1984. asm volatile ("mov %%dr2, %0" : "=r"(db_regs[2]));
  1985. asm volatile ("mov %%dr3, %0" : "=r"(db_regs[3]));
  1986. }
  1987. static void load_db_regs(unsigned long *db_regs)
  1988. {
  1989. asm volatile ("mov %0, %%dr0" : : "r"(db_regs[0]));
  1990. asm volatile ("mov %0, %%dr1" : : "r"(db_regs[1]));
  1991. asm volatile ("mov %0, %%dr2" : : "r"(db_regs[2]));
  1992. asm volatile ("mov %0, %%dr3" : : "r"(db_regs[3]));
  1993. }
  1994. static void svm_flush_tlb(struct kvm_vcpu *vcpu)
  1995. {
  1996. force_new_asid(vcpu);
  1997. }
  1998. static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
  1999. {
  2000. }
  2001. static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
  2002. {
  2003. struct vcpu_svm *svm = to_svm(vcpu);
  2004. if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) {
  2005. int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
  2006. kvm_lapic_set_tpr(vcpu, cr8);
  2007. }
  2008. }
  2009. static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
  2010. {
  2011. struct vcpu_svm *svm = to_svm(vcpu);
  2012. u64 cr8;
  2013. if (!irqchip_in_kernel(vcpu->kvm))
  2014. return;
  2015. cr8 = kvm_get_cr8(vcpu);
  2016. svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
  2017. svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
  2018. }
  2019. #ifdef CONFIG_X86_64
  2020. #define R "r"
  2021. #else
  2022. #define R "e"
  2023. #endif
  2024. static void svm_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2025. {
  2026. struct vcpu_svm *svm = to_svm(vcpu);
  2027. u16 fs_selector;
  2028. u16 gs_selector;
  2029. u16 ldt_selector;
  2030. svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
  2031. svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
  2032. svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
  2033. pre_svm_run(svm);
  2034. sync_lapic_to_cr8(vcpu);
  2035. save_host_msrs(vcpu);
  2036. fs_selector = kvm_read_fs();
  2037. gs_selector = kvm_read_gs();
  2038. ldt_selector = kvm_read_ldt();
  2039. svm->host_cr2 = kvm_read_cr2();
  2040. svm->host_dr6 = read_dr6();
  2041. svm->host_dr7 = read_dr7();
  2042. if (!is_nested(svm))
  2043. svm->vmcb->save.cr2 = vcpu->arch.cr2;
  2044. /* required for live migration with NPT */
  2045. if (npt_enabled)
  2046. svm->vmcb->save.cr3 = vcpu->arch.cr3;
  2047. if (svm->vmcb->save.dr7 & 0xff) {
  2048. write_dr7(0);
  2049. save_db_regs(svm->host_db_regs);
  2050. load_db_regs(svm->db_regs);
  2051. }
  2052. clgi();
  2053. local_irq_enable();
  2054. asm volatile (
  2055. "push %%"R"bp; \n\t"
  2056. "mov %c[rbx](%[svm]), %%"R"bx \n\t"
  2057. "mov %c[rcx](%[svm]), %%"R"cx \n\t"
  2058. "mov %c[rdx](%[svm]), %%"R"dx \n\t"
  2059. "mov %c[rsi](%[svm]), %%"R"si \n\t"
  2060. "mov %c[rdi](%[svm]), %%"R"di \n\t"
  2061. "mov %c[rbp](%[svm]), %%"R"bp \n\t"
  2062. #ifdef CONFIG_X86_64
  2063. "mov %c[r8](%[svm]), %%r8 \n\t"
  2064. "mov %c[r9](%[svm]), %%r9 \n\t"
  2065. "mov %c[r10](%[svm]), %%r10 \n\t"
  2066. "mov %c[r11](%[svm]), %%r11 \n\t"
  2067. "mov %c[r12](%[svm]), %%r12 \n\t"
  2068. "mov %c[r13](%[svm]), %%r13 \n\t"
  2069. "mov %c[r14](%[svm]), %%r14 \n\t"
  2070. "mov %c[r15](%[svm]), %%r15 \n\t"
  2071. #endif
  2072. /* Enter guest mode */
  2073. "push %%"R"ax \n\t"
  2074. "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
  2075. __ex(SVM_VMLOAD) "\n\t"
  2076. __ex(SVM_VMRUN) "\n\t"
  2077. __ex(SVM_VMSAVE) "\n\t"
  2078. "pop %%"R"ax \n\t"
  2079. /* Save guest registers, load host registers */
  2080. "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
  2081. "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
  2082. "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
  2083. "mov %%"R"si, %c[rsi](%[svm]) \n\t"
  2084. "mov %%"R"di, %c[rdi](%[svm]) \n\t"
  2085. "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
  2086. #ifdef CONFIG_X86_64
  2087. "mov %%r8, %c[r8](%[svm]) \n\t"
  2088. "mov %%r9, %c[r9](%[svm]) \n\t"
  2089. "mov %%r10, %c[r10](%[svm]) \n\t"
  2090. "mov %%r11, %c[r11](%[svm]) \n\t"
  2091. "mov %%r12, %c[r12](%[svm]) \n\t"
  2092. "mov %%r13, %c[r13](%[svm]) \n\t"
  2093. "mov %%r14, %c[r14](%[svm]) \n\t"
  2094. "mov %%r15, %c[r15](%[svm]) \n\t"
  2095. #endif
  2096. "pop %%"R"bp"
  2097. :
  2098. : [svm]"a"(svm),
  2099. [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
  2100. [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
  2101. [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
  2102. [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
  2103. [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
  2104. [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
  2105. [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
  2106. #ifdef CONFIG_X86_64
  2107. , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
  2108. [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
  2109. [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
  2110. [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
  2111. [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
  2112. [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
  2113. [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
  2114. [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
  2115. #endif
  2116. : "cc", "memory"
  2117. , R"bx", R"cx", R"dx", R"si", R"di"
  2118. #ifdef CONFIG_X86_64
  2119. , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
  2120. #endif
  2121. );
  2122. if ((svm->vmcb->save.dr7 & 0xff))
  2123. load_db_regs(svm->host_db_regs);
  2124. vcpu->arch.cr2 = svm->vmcb->save.cr2;
  2125. vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
  2126. vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
  2127. vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
  2128. write_dr6(svm->host_dr6);
  2129. write_dr7(svm->host_dr7);
  2130. kvm_write_cr2(svm->host_cr2);
  2131. kvm_load_fs(fs_selector);
  2132. kvm_load_gs(gs_selector);
  2133. kvm_load_ldt(ldt_selector);
  2134. load_host_msrs(vcpu);
  2135. reload_tss(vcpu);
  2136. local_irq_disable();
  2137. stgi();
  2138. sync_cr8_to_lapic(vcpu);
  2139. svm->next_rip = 0;
  2140. }
  2141. #undef R
  2142. static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
  2143. {
  2144. struct vcpu_svm *svm = to_svm(vcpu);
  2145. if (npt_enabled) {
  2146. svm->vmcb->control.nested_cr3 = root;
  2147. force_new_asid(vcpu);
  2148. return;
  2149. }
  2150. svm->vmcb->save.cr3 = root;
  2151. force_new_asid(vcpu);
  2152. if (vcpu->fpu_active) {
  2153. svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
  2154. svm->vmcb->save.cr0 |= X86_CR0_TS;
  2155. vcpu->fpu_active = 0;
  2156. }
  2157. }
  2158. static int is_disabled(void)
  2159. {
  2160. u64 vm_cr;
  2161. rdmsrl(MSR_VM_CR, vm_cr);
  2162. if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
  2163. return 1;
  2164. return 0;
  2165. }
  2166. static void
  2167. svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  2168. {
  2169. /*
  2170. * Patch in the VMMCALL instruction:
  2171. */
  2172. hypercall[0] = 0x0f;
  2173. hypercall[1] = 0x01;
  2174. hypercall[2] = 0xd9;
  2175. }
  2176. static void svm_check_processor_compat(void *rtn)
  2177. {
  2178. *(int *)rtn = 0;
  2179. }
  2180. static bool svm_cpu_has_accelerated_tpr(void)
  2181. {
  2182. return false;
  2183. }
  2184. static int get_npt_level(void)
  2185. {
  2186. #ifdef CONFIG_X86_64
  2187. return PT64_ROOT_LEVEL;
  2188. #else
  2189. return PT32E_ROOT_LEVEL;
  2190. #endif
  2191. }
  2192. static int svm_get_mt_mask_shift(void)
  2193. {
  2194. return 0;
  2195. }
  2196. static struct kvm_x86_ops svm_x86_ops = {
  2197. .cpu_has_kvm_support = has_svm,
  2198. .disabled_by_bios = is_disabled,
  2199. .hardware_setup = svm_hardware_setup,
  2200. .hardware_unsetup = svm_hardware_unsetup,
  2201. .check_processor_compatibility = svm_check_processor_compat,
  2202. .hardware_enable = svm_hardware_enable,
  2203. .hardware_disable = svm_hardware_disable,
  2204. .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
  2205. .vcpu_create = svm_create_vcpu,
  2206. .vcpu_free = svm_free_vcpu,
  2207. .vcpu_reset = svm_vcpu_reset,
  2208. .prepare_guest_switch = svm_prepare_guest_switch,
  2209. .vcpu_load = svm_vcpu_load,
  2210. .vcpu_put = svm_vcpu_put,
  2211. .set_guest_debug = svm_guest_debug,
  2212. .get_msr = svm_get_msr,
  2213. .set_msr = svm_set_msr,
  2214. .get_segment_base = svm_get_segment_base,
  2215. .get_segment = svm_get_segment,
  2216. .set_segment = svm_set_segment,
  2217. .get_cpl = svm_get_cpl,
  2218. .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
  2219. .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
  2220. .set_cr0 = svm_set_cr0,
  2221. .set_cr3 = svm_set_cr3,
  2222. .set_cr4 = svm_set_cr4,
  2223. .set_efer = svm_set_efer,
  2224. .get_idt = svm_get_idt,
  2225. .set_idt = svm_set_idt,
  2226. .get_gdt = svm_get_gdt,
  2227. .set_gdt = svm_set_gdt,
  2228. .get_dr = svm_get_dr,
  2229. .set_dr = svm_set_dr,
  2230. .get_rflags = svm_get_rflags,
  2231. .set_rflags = svm_set_rflags,
  2232. .tlb_flush = svm_flush_tlb,
  2233. .run = svm_vcpu_run,
  2234. .handle_exit = handle_exit,
  2235. .skip_emulated_instruction = skip_emulated_instruction,
  2236. .patch_hypercall = svm_patch_hypercall,
  2237. .get_irq = svm_get_irq,
  2238. .set_irq = svm_set_irq,
  2239. .queue_exception = svm_queue_exception,
  2240. .exception_injected = svm_exception_injected,
  2241. .inject_pending_irq = svm_intr_assist,
  2242. .inject_pending_vectors = do_interrupt_requests,
  2243. .set_tss_addr = svm_set_tss_addr,
  2244. .get_tdp_level = get_npt_level,
  2245. .get_mt_mask_shift = svm_get_mt_mask_shift,
  2246. };
  2247. static int __init svm_init(void)
  2248. {
  2249. return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
  2250. THIS_MODULE);
  2251. }
  2252. static void __exit svm_exit(void)
  2253. {
  2254. kvm_exit();
  2255. }
  2256. module_init(svm_init)
  2257. module_exit(svm_exit)