svm.c 75 KB

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