svm.c 79 KB

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