svm.c 73 KB

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