svm.c 74 KB

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