svm.c 68 KB

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