svm.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670
  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. }
  656. }
  657. static int svm_get_cpl(struct kvm_vcpu *vcpu)
  658. {
  659. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  660. return save->cpl;
  661. }
  662. static void svm_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  663. {
  664. struct vcpu_svm *svm = to_svm(vcpu);
  665. dt->limit = svm->vmcb->save.idtr.limit;
  666. dt->base = svm->vmcb->save.idtr.base;
  667. }
  668. static void svm_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  669. {
  670. struct vcpu_svm *svm = to_svm(vcpu);
  671. svm->vmcb->save.idtr.limit = dt->limit;
  672. svm->vmcb->save.idtr.base = dt->base ;
  673. }
  674. static void svm_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  675. {
  676. struct vcpu_svm *svm = to_svm(vcpu);
  677. dt->limit = svm->vmcb->save.gdtr.limit;
  678. dt->base = svm->vmcb->save.gdtr.base;
  679. }
  680. static void svm_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  681. {
  682. struct vcpu_svm *svm = to_svm(vcpu);
  683. svm->vmcb->save.gdtr.limit = dt->limit;
  684. svm->vmcb->save.gdtr.base = dt->base ;
  685. }
  686. static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  687. {
  688. }
  689. static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  690. {
  691. struct vcpu_svm *svm = to_svm(vcpu);
  692. #ifdef CONFIG_X86_64
  693. if (vcpu->arch.shadow_efer & EFER_LME) {
  694. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
  695. vcpu->arch.shadow_efer |= EFER_LMA;
  696. svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
  697. }
  698. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
  699. vcpu->arch.shadow_efer &= ~EFER_LMA;
  700. svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
  701. }
  702. }
  703. #endif
  704. if (npt_enabled)
  705. goto set;
  706. if ((vcpu->arch.cr0 & X86_CR0_TS) && !(cr0 & X86_CR0_TS)) {
  707. svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
  708. vcpu->fpu_active = 1;
  709. }
  710. vcpu->arch.cr0 = cr0;
  711. cr0 |= X86_CR0_PG | X86_CR0_WP;
  712. if (!vcpu->fpu_active) {
  713. svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
  714. cr0 |= X86_CR0_TS;
  715. }
  716. set:
  717. /*
  718. * re-enable caching here because the QEMU bios
  719. * does not do it - this results in some delay at
  720. * reboot
  721. */
  722. cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
  723. svm->vmcb->save.cr0 = cr0;
  724. }
  725. static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  726. {
  727. unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
  728. unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
  729. if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
  730. force_new_asid(vcpu);
  731. vcpu->arch.cr4 = cr4;
  732. if (!npt_enabled)
  733. cr4 |= X86_CR4_PAE;
  734. cr4 |= host_cr4_mce;
  735. to_svm(vcpu)->vmcb->save.cr4 = cr4;
  736. }
  737. static void svm_set_segment(struct kvm_vcpu *vcpu,
  738. struct kvm_segment *var, int seg)
  739. {
  740. struct vcpu_svm *svm = to_svm(vcpu);
  741. struct vmcb_seg *s = svm_seg(vcpu, seg);
  742. s->base = var->base;
  743. s->limit = var->limit;
  744. s->selector = var->selector;
  745. if (var->unusable)
  746. s->attrib = 0;
  747. else {
  748. s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
  749. s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
  750. s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
  751. s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
  752. s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
  753. s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
  754. s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
  755. s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
  756. }
  757. if (seg == VCPU_SREG_CS)
  758. svm->vmcb->save.cpl
  759. = (svm->vmcb->save.cs.attrib
  760. >> SVM_SELECTOR_DPL_SHIFT) & 3;
  761. }
  762. static int svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
  763. {
  764. int old_debug = vcpu->guest_debug;
  765. struct vcpu_svm *svm = to_svm(vcpu);
  766. vcpu->guest_debug = dbg->control;
  767. svm->vmcb->control.intercept_exceptions &=
  768. ~((1 << DB_VECTOR) | (1 << BP_VECTOR));
  769. if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
  770. if (vcpu->guest_debug &
  771. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  772. svm->vmcb->control.intercept_exceptions |=
  773. 1 << DB_VECTOR;
  774. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  775. svm->vmcb->control.intercept_exceptions |=
  776. 1 << BP_VECTOR;
  777. } else
  778. vcpu->guest_debug = 0;
  779. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
  780. svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
  781. else
  782. svm->vmcb->save.dr7 = vcpu->arch.dr7;
  783. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  784. svm->vmcb->save.rflags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
  785. else if (old_debug & KVM_GUESTDBG_SINGLESTEP)
  786. svm->vmcb->save.rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
  787. return 0;
  788. }
  789. static void load_host_msrs(struct kvm_vcpu *vcpu)
  790. {
  791. #ifdef CONFIG_X86_64
  792. wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
  793. #endif
  794. }
  795. static void save_host_msrs(struct kvm_vcpu *vcpu)
  796. {
  797. #ifdef CONFIG_X86_64
  798. rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
  799. #endif
  800. }
  801. static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *svm_data)
  802. {
  803. if (svm_data->next_asid > svm_data->max_asid) {
  804. ++svm_data->asid_generation;
  805. svm_data->next_asid = 1;
  806. svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
  807. }
  808. svm->vcpu.cpu = svm_data->cpu;
  809. svm->asid_generation = svm_data->asid_generation;
  810. svm->vmcb->control.asid = svm_data->next_asid++;
  811. }
  812. static unsigned long svm_get_dr(struct kvm_vcpu *vcpu, int dr)
  813. {
  814. struct vcpu_svm *svm = to_svm(vcpu);
  815. unsigned long val;
  816. switch (dr) {
  817. case 0 ... 3:
  818. val = vcpu->arch.db[dr];
  819. break;
  820. case 6:
  821. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
  822. val = vcpu->arch.dr6;
  823. else
  824. val = svm->vmcb->save.dr6;
  825. break;
  826. case 7:
  827. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
  828. val = vcpu->arch.dr7;
  829. else
  830. val = svm->vmcb->save.dr7;
  831. break;
  832. default:
  833. val = 0;
  834. }
  835. KVMTRACE_2D(DR_READ, vcpu, (u32)dr, (u32)val, handler);
  836. return val;
  837. }
  838. static void svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value,
  839. int *exception)
  840. {
  841. struct vcpu_svm *svm = to_svm(vcpu);
  842. KVMTRACE_2D(DR_WRITE, vcpu, (u32)dr, (u32)value, handler);
  843. *exception = 0;
  844. switch (dr) {
  845. case 0 ... 3:
  846. vcpu->arch.db[dr] = value;
  847. if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
  848. vcpu->arch.eff_db[dr] = value;
  849. return;
  850. case 4 ... 5:
  851. if (vcpu->arch.cr4 & X86_CR4_DE)
  852. *exception = UD_VECTOR;
  853. return;
  854. case 6:
  855. if (value & 0xffffffff00000000ULL) {
  856. *exception = GP_VECTOR;
  857. return;
  858. }
  859. vcpu->arch.dr6 = (value & DR6_VOLATILE) | DR6_FIXED_1;
  860. return;
  861. case 7:
  862. if (value & 0xffffffff00000000ULL) {
  863. *exception = GP_VECTOR;
  864. return;
  865. }
  866. vcpu->arch.dr7 = (value & DR7_VOLATILE) | DR7_FIXED_1;
  867. if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
  868. svm->vmcb->save.dr7 = vcpu->arch.dr7;
  869. vcpu->arch.switch_db_regs = (value & DR7_BP_EN_MASK);
  870. }
  871. return;
  872. default:
  873. /* FIXME: Possible case? */
  874. printk(KERN_DEBUG "%s: unexpected dr %u\n",
  875. __func__, dr);
  876. *exception = UD_VECTOR;
  877. return;
  878. }
  879. }
  880. static int pf_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  881. {
  882. u64 fault_address;
  883. u32 error_code;
  884. fault_address = svm->vmcb->control.exit_info_2;
  885. error_code = svm->vmcb->control.exit_info_1;
  886. if (!npt_enabled)
  887. KVMTRACE_3D(PAGE_FAULT, &svm->vcpu, error_code,
  888. (u32)fault_address, (u32)(fault_address >> 32),
  889. handler);
  890. else
  891. KVMTRACE_3D(TDP_FAULT, &svm->vcpu, error_code,
  892. (u32)fault_address, (u32)(fault_address >> 32),
  893. handler);
  894. /*
  895. * FIXME: Tis shouldn't be necessary here, but there is a flush
  896. * missing in the MMU code. Until we find this bug, flush the
  897. * complete TLB here on an NPF
  898. */
  899. if (npt_enabled)
  900. svm_flush_tlb(&svm->vcpu);
  901. else {
  902. if (svm->vcpu.arch.interrupt.pending ||
  903. svm->vcpu.arch.exception.pending)
  904. kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
  905. }
  906. return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
  907. }
  908. static int db_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  909. {
  910. if (!(svm->vcpu.guest_debug &
  911. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
  912. kvm_queue_exception(&svm->vcpu, DB_VECTOR);
  913. return 1;
  914. }
  915. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  916. kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
  917. kvm_run->debug.arch.exception = DB_VECTOR;
  918. return 0;
  919. }
  920. static int bp_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  921. {
  922. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  923. kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
  924. kvm_run->debug.arch.exception = BP_VECTOR;
  925. return 0;
  926. }
  927. static int ud_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  928. {
  929. int er;
  930. er = emulate_instruction(&svm->vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
  931. if (er != EMULATE_DONE)
  932. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  933. return 1;
  934. }
  935. static int nm_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  936. {
  937. svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
  938. if (!(svm->vcpu.arch.cr0 & X86_CR0_TS))
  939. svm->vmcb->save.cr0 &= ~X86_CR0_TS;
  940. svm->vcpu.fpu_active = 1;
  941. return 1;
  942. }
  943. static int mc_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  944. {
  945. /*
  946. * On an #MC intercept the MCE handler is not called automatically in
  947. * the host. So do it by hand here.
  948. */
  949. asm volatile (
  950. "int $0x12\n");
  951. /* not sure if we ever come back to this point */
  952. return 1;
  953. }
  954. static int shutdown_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  955. {
  956. /*
  957. * VMCB is undefined after a SHUTDOWN intercept
  958. * so reinitialize it.
  959. */
  960. clear_page(svm->vmcb);
  961. init_vmcb(svm);
  962. kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
  963. return 0;
  964. }
  965. static int io_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  966. {
  967. u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
  968. int size, in, string;
  969. unsigned port;
  970. ++svm->vcpu.stat.io_exits;
  971. svm->next_rip = svm->vmcb->control.exit_info_2;
  972. string = (io_info & SVM_IOIO_STR_MASK) != 0;
  973. if (string) {
  974. if (emulate_instruction(&svm->vcpu,
  975. kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
  976. return 0;
  977. return 1;
  978. }
  979. in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
  980. port = io_info >> 16;
  981. size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
  982. skip_emulated_instruction(&svm->vcpu);
  983. return kvm_emulate_pio(&svm->vcpu, kvm_run, in, size, port);
  984. }
  985. static int nmi_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  986. {
  987. KVMTRACE_0D(NMI, &svm->vcpu, handler);
  988. return 1;
  989. }
  990. static int intr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  991. {
  992. ++svm->vcpu.stat.irq_exits;
  993. KVMTRACE_0D(INTR, &svm->vcpu, handler);
  994. return 1;
  995. }
  996. static int nop_on_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  997. {
  998. return 1;
  999. }
  1000. static int halt_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1001. {
  1002. svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
  1003. skip_emulated_instruction(&svm->vcpu);
  1004. return kvm_emulate_halt(&svm->vcpu);
  1005. }
  1006. static int vmmcall_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1007. {
  1008. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1009. skip_emulated_instruction(&svm->vcpu);
  1010. kvm_emulate_hypercall(&svm->vcpu);
  1011. return 1;
  1012. }
  1013. static int nested_svm_check_permissions(struct vcpu_svm *svm)
  1014. {
  1015. if (!(svm->vcpu.arch.shadow_efer & EFER_SVME)
  1016. || !is_paging(&svm->vcpu)) {
  1017. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  1018. return 1;
  1019. }
  1020. if (svm->vmcb->save.cpl) {
  1021. kvm_inject_gp(&svm->vcpu, 0);
  1022. return 1;
  1023. }
  1024. return 0;
  1025. }
  1026. static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
  1027. bool has_error_code, u32 error_code)
  1028. {
  1029. if (is_nested(svm)) {
  1030. svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
  1031. svm->vmcb->control.exit_code_hi = 0;
  1032. svm->vmcb->control.exit_info_1 = error_code;
  1033. svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
  1034. if (nested_svm_exit_handled(svm, false)) {
  1035. nsvm_printk("VMexit -> EXCP 0x%x\n", nr);
  1036. nested_svm_vmexit(svm);
  1037. return 1;
  1038. }
  1039. }
  1040. return 0;
  1041. }
  1042. static inline int nested_svm_intr(struct vcpu_svm *svm)
  1043. {
  1044. if (is_nested(svm)) {
  1045. if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
  1046. return 0;
  1047. if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
  1048. return 0;
  1049. svm->vmcb->control.exit_code = SVM_EXIT_INTR;
  1050. if (nested_svm_exit_handled(svm, false)) {
  1051. nsvm_printk("VMexit -> INTR\n");
  1052. nested_svm_vmexit(svm);
  1053. return 1;
  1054. }
  1055. }
  1056. return 0;
  1057. }
  1058. static struct page *nested_svm_get_page(struct vcpu_svm *svm, u64 gpa)
  1059. {
  1060. struct page *page;
  1061. down_read(&current->mm->mmap_sem);
  1062. page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
  1063. up_read(&current->mm->mmap_sem);
  1064. if (is_error_page(page)) {
  1065. printk(KERN_INFO "%s: could not find page at 0x%llx\n",
  1066. __func__, gpa);
  1067. kvm_release_page_clean(page);
  1068. kvm_inject_gp(&svm->vcpu, 0);
  1069. return NULL;
  1070. }
  1071. return page;
  1072. }
  1073. static int nested_svm_do(struct vcpu_svm *svm,
  1074. u64 arg1_gpa, u64 arg2_gpa, void *opaque,
  1075. int (*handler)(struct vcpu_svm *svm,
  1076. void *arg1,
  1077. void *arg2,
  1078. void *opaque))
  1079. {
  1080. struct page *arg1_page;
  1081. struct page *arg2_page = NULL;
  1082. void *arg1;
  1083. void *arg2 = NULL;
  1084. int retval;
  1085. arg1_page = nested_svm_get_page(svm, arg1_gpa);
  1086. if(arg1_page == NULL)
  1087. return 1;
  1088. if (arg2_gpa) {
  1089. arg2_page = nested_svm_get_page(svm, arg2_gpa);
  1090. if(arg2_page == NULL) {
  1091. kvm_release_page_clean(arg1_page);
  1092. return 1;
  1093. }
  1094. }
  1095. arg1 = kmap_atomic(arg1_page, KM_USER0);
  1096. if (arg2_gpa)
  1097. arg2 = kmap_atomic(arg2_page, KM_USER1);
  1098. retval = handler(svm, arg1, arg2, opaque);
  1099. kunmap_atomic(arg1, KM_USER0);
  1100. if (arg2_gpa)
  1101. kunmap_atomic(arg2, KM_USER1);
  1102. kvm_release_page_dirty(arg1_page);
  1103. if (arg2_gpa)
  1104. kvm_release_page_dirty(arg2_page);
  1105. return retval;
  1106. }
  1107. static int nested_svm_exit_handled_real(struct vcpu_svm *svm,
  1108. void *arg1,
  1109. void *arg2,
  1110. void *opaque)
  1111. {
  1112. struct vmcb *nested_vmcb = (struct vmcb *)arg1;
  1113. bool kvm_overrides = *(bool *)opaque;
  1114. u32 exit_code = svm->vmcb->control.exit_code;
  1115. if (kvm_overrides) {
  1116. switch (exit_code) {
  1117. case SVM_EXIT_INTR:
  1118. case SVM_EXIT_NMI:
  1119. return 0;
  1120. /* For now we are always handling NPFs when using them */
  1121. case SVM_EXIT_NPF:
  1122. if (npt_enabled)
  1123. return 0;
  1124. break;
  1125. /* When we're shadowing, trap PFs */
  1126. case SVM_EXIT_EXCP_BASE + PF_VECTOR:
  1127. if (!npt_enabled)
  1128. return 0;
  1129. break;
  1130. default:
  1131. break;
  1132. }
  1133. }
  1134. switch (exit_code) {
  1135. case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR8: {
  1136. u32 cr_bits = 1 << (exit_code - SVM_EXIT_READ_CR0);
  1137. if (nested_vmcb->control.intercept_cr_read & cr_bits)
  1138. return 1;
  1139. break;
  1140. }
  1141. case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR8: {
  1142. u32 cr_bits = 1 << (exit_code - SVM_EXIT_WRITE_CR0);
  1143. if (nested_vmcb->control.intercept_cr_write & cr_bits)
  1144. return 1;
  1145. break;
  1146. }
  1147. case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR7: {
  1148. u32 dr_bits = 1 << (exit_code - SVM_EXIT_READ_DR0);
  1149. if (nested_vmcb->control.intercept_dr_read & dr_bits)
  1150. return 1;
  1151. break;
  1152. }
  1153. case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR7: {
  1154. u32 dr_bits = 1 << (exit_code - SVM_EXIT_WRITE_DR0);
  1155. if (nested_vmcb->control.intercept_dr_write & dr_bits)
  1156. return 1;
  1157. break;
  1158. }
  1159. case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
  1160. u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
  1161. if (nested_vmcb->control.intercept_exceptions & excp_bits)
  1162. return 1;
  1163. break;
  1164. }
  1165. default: {
  1166. u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
  1167. nsvm_printk("exit code: 0x%x\n", exit_code);
  1168. if (nested_vmcb->control.intercept & exit_bits)
  1169. return 1;
  1170. }
  1171. }
  1172. return 0;
  1173. }
  1174. static int nested_svm_exit_handled_msr(struct vcpu_svm *svm,
  1175. void *arg1, void *arg2,
  1176. void *opaque)
  1177. {
  1178. struct vmcb *nested_vmcb = (struct vmcb *)arg1;
  1179. u8 *msrpm = (u8 *)arg2;
  1180. u32 t0, t1;
  1181. u32 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  1182. u32 param = svm->vmcb->control.exit_info_1 & 1;
  1183. if (!(nested_vmcb->control.intercept & (1ULL << INTERCEPT_MSR_PROT)))
  1184. return 0;
  1185. switch(msr) {
  1186. case 0 ... 0x1fff:
  1187. t0 = (msr * 2) % 8;
  1188. t1 = msr / 8;
  1189. break;
  1190. case 0xc0000000 ... 0xc0001fff:
  1191. t0 = (8192 + msr - 0xc0000000) * 2;
  1192. t1 = (t0 / 8);
  1193. t0 %= 8;
  1194. break;
  1195. case 0xc0010000 ... 0xc0011fff:
  1196. t0 = (16384 + msr - 0xc0010000) * 2;
  1197. t1 = (t0 / 8);
  1198. t0 %= 8;
  1199. break;
  1200. default:
  1201. return 1;
  1202. break;
  1203. }
  1204. if (msrpm[t1] & ((1 << param) << t0))
  1205. return 1;
  1206. return 0;
  1207. }
  1208. static int nested_svm_exit_handled(struct vcpu_svm *svm, bool kvm_override)
  1209. {
  1210. bool k = kvm_override;
  1211. switch (svm->vmcb->control.exit_code) {
  1212. case SVM_EXIT_MSR:
  1213. return nested_svm_do(svm, svm->nested_vmcb,
  1214. svm->nested_vmcb_msrpm, NULL,
  1215. nested_svm_exit_handled_msr);
  1216. default: break;
  1217. }
  1218. return nested_svm_do(svm, svm->nested_vmcb, 0, &k,
  1219. nested_svm_exit_handled_real);
  1220. }
  1221. static int nested_svm_vmexit_real(struct vcpu_svm *svm, void *arg1,
  1222. void *arg2, void *opaque)
  1223. {
  1224. struct vmcb *nested_vmcb = (struct vmcb *)arg1;
  1225. struct vmcb *hsave = svm->hsave;
  1226. u64 nested_save[] = { nested_vmcb->save.cr0,
  1227. nested_vmcb->save.cr3,
  1228. nested_vmcb->save.cr4,
  1229. nested_vmcb->save.efer,
  1230. nested_vmcb->control.intercept_cr_read,
  1231. nested_vmcb->control.intercept_cr_write,
  1232. nested_vmcb->control.intercept_dr_read,
  1233. nested_vmcb->control.intercept_dr_write,
  1234. nested_vmcb->control.intercept_exceptions,
  1235. nested_vmcb->control.intercept,
  1236. nested_vmcb->control.msrpm_base_pa,
  1237. nested_vmcb->control.iopm_base_pa,
  1238. nested_vmcb->control.tsc_offset };
  1239. /* Give the current vmcb to the guest */
  1240. memcpy(nested_vmcb, svm->vmcb, sizeof(struct vmcb));
  1241. nested_vmcb->save.cr0 = nested_save[0];
  1242. if (!npt_enabled)
  1243. nested_vmcb->save.cr3 = nested_save[1];
  1244. nested_vmcb->save.cr4 = nested_save[2];
  1245. nested_vmcb->save.efer = nested_save[3];
  1246. nested_vmcb->control.intercept_cr_read = nested_save[4];
  1247. nested_vmcb->control.intercept_cr_write = nested_save[5];
  1248. nested_vmcb->control.intercept_dr_read = nested_save[6];
  1249. nested_vmcb->control.intercept_dr_write = nested_save[7];
  1250. nested_vmcb->control.intercept_exceptions = nested_save[8];
  1251. nested_vmcb->control.intercept = nested_save[9];
  1252. nested_vmcb->control.msrpm_base_pa = nested_save[10];
  1253. nested_vmcb->control.iopm_base_pa = nested_save[11];
  1254. nested_vmcb->control.tsc_offset = nested_save[12];
  1255. /* We always set V_INTR_MASKING and remember the old value in hflags */
  1256. if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
  1257. nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
  1258. if ((nested_vmcb->control.int_ctl & V_IRQ_MASK) &&
  1259. (nested_vmcb->control.int_vector)) {
  1260. nsvm_printk("WARNING: IRQ 0x%x still enabled on #VMEXIT\n",
  1261. nested_vmcb->control.int_vector);
  1262. }
  1263. /* Restore the original control entries */
  1264. svm->vmcb->control = hsave->control;
  1265. /* Kill any pending exceptions */
  1266. if (svm->vcpu.arch.exception.pending == true)
  1267. nsvm_printk("WARNING: Pending Exception\n");
  1268. svm->vcpu.arch.exception.pending = false;
  1269. /* Restore selected save entries */
  1270. svm->vmcb->save.es = hsave->save.es;
  1271. svm->vmcb->save.cs = hsave->save.cs;
  1272. svm->vmcb->save.ss = hsave->save.ss;
  1273. svm->vmcb->save.ds = hsave->save.ds;
  1274. svm->vmcb->save.gdtr = hsave->save.gdtr;
  1275. svm->vmcb->save.idtr = hsave->save.idtr;
  1276. svm->vmcb->save.rflags = hsave->save.rflags;
  1277. svm_set_efer(&svm->vcpu, hsave->save.efer);
  1278. svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
  1279. svm_set_cr4(&svm->vcpu, hsave->save.cr4);
  1280. if (npt_enabled) {
  1281. svm->vmcb->save.cr3 = hsave->save.cr3;
  1282. svm->vcpu.arch.cr3 = hsave->save.cr3;
  1283. } else {
  1284. kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
  1285. }
  1286. kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
  1287. kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
  1288. kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
  1289. svm->vmcb->save.dr7 = 0;
  1290. svm->vmcb->save.cpl = 0;
  1291. svm->vmcb->control.exit_int_info = 0;
  1292. svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
  1293. /* Exit nested SVM mode */
  1294. svm->nested_vmcb = 0;
  1295. return 0;
  1296. }
  1297. static int nested_svm_vmexit(struct vcpu_svm *svm)
  1298. {
  1299. nsvm_printk("VMexit\n");
  1300. if (nested_svm_do(svm, svm->nested_vmcb, 0,
  1301. NULL, nested_svm_vmexit_real))
  1302. return 1;
  1303. kvm_mmu_reset_context(&svm->vcpu);
  1304. kvm_mmu_load(&svm->vcpu);
  1305. return 0;
  1306. }
  1307. static int nested_svm_vmrun_msrpm(struct vcpu_svm *svm, void *arg1,
  1308. void *arg2, void *opaque)
  1309. {
  1310. int i;
  1311. u32 *nested_msrpm = (u32*)arg1;
  1312. for (i=0; i< PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER) / 4; i++)
  1313. svm->nested_msrpm[i] = svm->msrpm[i] | nested_msrpm[i];
  1314. svm->vmcb->control.msrpm_base_pa = __pa(svm->nested_msrpm);
  1315. return 0;
  1316. }
  1317. static int nested_svm_vmrun(struct vcpu_svm *svm, void *arg1,
  1318. void *arg2, void *opaque)
  1319. {
  1320. struct vmcb *nested_vmcb = (struct vmcb *)arg1;
  1321. struct vmcb *hsave = svm->hsave;
  1322. /* nested_vmcb is our indicator if nested SVM is activated */
  1323. svm->nested_vmcb = svm->vmcb->save.rax;
  1324. /* Clear internal status */
  1325. svm->vcpu.arch.exception.pending = false;
  1326. /* Save the old vmcb, so we don't need to pick what we save, but
  1327. can restore everything when a VMEXIT occurs */
  1328. memcpy(hsave, svm->vmcb, sizeof(struct vmcb));
  1329. /* We need to remember the original CR3 in the SPT case */
  1330. if (!npt_enabled)
  1331. hsave->save.cr3 = svm->vcpu.arch.cr3;
  1332. hsave->save.cr4 = svm->vcpu.arch.cr4;
  1333. hsave->save.rip = svm->next_rip;
  1334. if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
  1335. svm->vcpu.arch.hflags |= HF_HIF_MASK;
  1336. else
  1337. svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
  1338. /* Load the nested guest state */
  1339. svm->vmcb->save.es = nested_vmcb->save.es;
  1340. svm->vmcb->save.cs = nested_vmcb->save.cs;
  1341. svm->vmcb->save.ss = nested_vmcb->save.ss;
  1342. svm->vmcb->save.ds = nested_vmcb->save.ds;
  1343. svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
  1344. svm->vmcb->save.idtr = nested_vmcb->save.idtr;
  1345. svm->vmcb->save.rflags = nested_vmcb->save.rflags;
  1346. svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
  1347. svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
  1348. svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
  1349. if (npt_enabled) {
  1350. svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
  1351. svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
  1352. } else {
  1353. kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
  1354. kvm_mmu_reset_context(&svm->vcpu);
  1355. }
  1356. svm->vmcb->save.cr2 = nested_vmcb->save.cr2;
  1357. kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
  1358. kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
  1359. kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
  1360. /* In case we don't even reach vcpu_run, the fields are not updated */
  1361. svm->vmcb->save.rax = nested_vmcb->save.rax;
  1362. svm->vmcb->save.rsp = nested_vmcb->save.rsp;
  1363. svm->vmcb->save.rip = nested_vmcb->save.rip;
  1364. svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
  1365. svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
  1366. svm->vmcb->save.cpl = nested_vmcb->save.cpl;
  1367. /* We don't want a nested guest to be more powerful than the guest,
  1368. so all intercepts are ORed */
  1369. svm->vmcb->control.intercept_cr_read |=
  1370. nested_vmcb->control.intercept_cr_read;
  1371. svm->vmcb->control.intercept_cr_write |=
  1372. nested_vmcb->control.intercept_cr_write;
  1373. svm->vmcb->control.intercept_dr_read |=
  1374. nested_vmcb->control.intercept_dr_read;
  1375. svm->vmcb->control.intercept_dr_write |=
  1376. nested_vmcb->control.intercept_dr_write;
  1377. svm->vmcb->control.intercept_exceptions |=
  1378. nested_vmcb->control.intercept_exceptions;
  1379. svm->vmcb->control.intercept |= nested_vmcb->control.intercept;
  1380. svm->nested_vmcb_msrpm = nested_vmcb->control.msrpm_base_pa;
  1381. force_new_asid(&svm->vcpu);
  1382. svm->vmcb->control.exit_int_info = nested_vmcb->control.exit_int_info;
  1383. svm->vmcb->control.exit_int_info_err = nested_vmcb->control.exit_int_info_err;
  1384. svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
  1385. if (nested_vmcb->control.int_ctl & V_IRQ_MASK) {
  1386. nsvm_printk("nSVM Injecting Interrupt: 0x%x\n",
  1387. nested_vmcb->control.int_ctl);
  1388. }
  1389. if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
  1390. svm->vcpu.arch.hflags |= HF_VINTR_MASK;
  1391. else
  1392. svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
  1393. nsvm_printk("nSVM exit_int_info: 0x%x | int_state: 0x%x\n",
  1394. nested_vmcb->control.exit_int_info,
  1395. nested_vmcb->control.int_state);
  1396. svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
  1397. svm->vmcb->control.int_state = nested_vmcb->control.int_state;
  1398. svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
  1399. if (nested_vmcb->control.event_inj & SVM_EVTINJ_VALID)
  1400. nsvm_printk("Injecting Event: 0x%x\n",
  1401. nested_vmcb->control.event_inj);
  1402. svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
  1403. svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
  1404. svm->vcpu.arch.hflags |= HF_GIF_MASK;
  1405. return 0;
  1406. }
  1407. static int nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
  1408. {
  1409. to_vmcb->save.fs = from_vmcb->save.fs;
  1410. to_vmcb->save.gs = from_vmcb->save.gs;
  1411. to_vmcb->save.tr = from_vmcb->save.tr;
  1412. to_vmcb->save.ldtr = from_vmcb->save.ldtr;
  1413. to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
  1414. to_vmcb->save.star = from_vmcb->save.star;
  1415. to_vmcb->save.lstar = from_vmcb->save.lstar;
  1416. to_vmcb->save.cstar = from_vmcb->save.cstar;
  1417. to_vmcb->save.sfmask = from_vmcb->save.sfmask;
  1418. to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
  1419. to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
  1420. to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
  1421. return 1;
  1422. }
  1423. static int nested_svm_vmload(struct vcpu_svm *svm, void *nested_vmcb,
  1424. void *arg2, void *opaque)
  1425. {
  1426. return nested_svm_vmloadsave((struct vmcb *)nested_vmcb, svm->vmcb);
  1427. }
  1428. static int nested_svm_vmsave(struct vcpu_svm *svm, void *nested_vmcb,
  1429. void *arg2, void *opaque)
  1430. {
  1431. return nested_svm_vmloadsave(svm->vmcb, (struct vmcb *)nested_vmcb);
  1432. }
  1433. static int vmload_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1434. {
  1435. if (nested_svm_check_permissions(svm))
  1436. return 1;
  1437. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1438. skip_emulated_instruction(&svm->vcpu);
  1439. nested_svm_do(svm, svm->vmcb->save.rax, 0, NULL, nested_svm_vmload);
  1440. return 1;
  1441. }
  1442. static int vmsave_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_vmsave);
  1449. return 1;
  1450. }
  1451. static int vmrun_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1452. {
  1453. nsvm_printk("VMrun\n");
  1454. if (nested_svm_check_permissions(svm))
  1455. return 1;
  1456. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1457. skip_emulated_instruction(&svm->vcpu);
  1458. if (nested_svm_do(svm, svm->vmcb->save.rax, 0,
  1459. NULL, nested_svm_vmrun))
  1460. return 1;
  1461. if (nested_svm_do(svm, svm->nested_vmcb_msrpm, 0,
  1462. NULL, nested_svm_vmrun_msrpm))
  1463. return 1;
  1464. return 1;
  1465. }
  1466. static int stgi_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1467. {
  1468. if (nested_svm_check_permissions(svm))
  1469. return 1;
  1470. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1471. skip_emulated_instruction(&svm->vcpu);
  1472. svm->vcpu.arch.hflags |= HF_GIF_MASK;
  1473. return 1;
  1474. }
  1475. static int clgi_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. /* After a CLGI no interrupts should come */
  1483. svm_clear_vintr(svm);
  1484. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  1485. return 1;
  1486. }
  1487. static int invalid_op_interception(struct vcpu_svm *svm,
  1488. struct kvm_run *kvm_run)
  1489. {
  1490. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  1491. return 1;
  1492. }
  1493. static int task_switch_interception(struct vcpu_svm *svm,
  1494. struct kvm_run *kvm_run)
  1495. {
  1496. u16 tss_selector;
  1497. int reason;
  1498. int int_type = svm->vmcb->control.exit_int_info &
  1499. SVM_EXITINTINFO_TYPE_MASK;
  1500. int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
  1501. tss_selector = (u16)svm->vmcb->control.exit_info_1;
  1502. if (svm->vmcb->control.exit_info_2 &
  1503. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
  1504. reason = TASK_SWITCH_IRET;
  1505. else if (svm->vmcb->control.exit_info_2 &
  1506. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
  1507. reason = TASK_SWITCH_JMP;
  1508. else if (svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID)
  1509. reason = TASK_SWITCH_GATE;
  1510. else
  1511. reason = TASK_SWITCH_CALL;
  1512. if (reason != TASK_SWITCH_GATE ||
  1513. int_type == SVM_EXITINTINFO_TYPE_SOFT ||
  1514. (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
  1515. (int_vec == OF_VECTOR || int_vec == BP_VECTOR))) {
  1516. if (emulate_instruction(&svm->vcpu, kvm_run, 0, 0,
  1517. EMULTYPE_SKIP) != EMULATE_DONE)
  1518. return 0;
  1519. }
  1520. return kvm_task_switch(&svm->vcpu, tss_selector, reason);
  1521. }
  1522. static int cpuid_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1523. {
  1524. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  1525. kvm_emulate_cpuid(&svm->vcpu);
  1526. return 1;
  1527. }
  1528. static int iret_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1529. {
  1530. ++svm->vcpu.stat.nmi_window_exits;
  1531. svm->vmcb->control.intercept &= ~(1UL << INTERCEPT_IRET);
  1532. svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
  1533. return 1;
  1534. }
  1535. static int invlpg_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1536. {
  1537. if (emulate_instruction(&svm->vcpu, kvm_run, 0, 0, 0) != EMULATE_DONE)
  1538. pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
  1539. return 1;
  1540. }
  1541. static int emulate_on_interception(struct vcpu_svm *svm,
  1542. struct kvm_run *kvm_run)
  1543. {
  1544. if (emulate_instruction(&svm->vcpu, NULL, 0, 0, 0) != EMULATE_DONE)
  1545. pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
  1546. return 1;
  1547. }
  1548. static int cr8_write_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1549. {
  1550. u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
  1551. /* instruction emulation calls kvm_set_cr8() */
  1552. emulate_instruction(&svm->vcpu, NULL, 0, 0, 0);
  1553. if (irqchip_in_kernel(svm->vcpu.kvm)) {
  1554. svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
  1555. return 1;
  1556. }
  1557. if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
  1558. return 1;
  1559. kvm_run->exit_reason = KVM_EXIT_SET_TPR;
  1560. return 0;
  1561. }
  1562. static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
  1563. {
  1564. struct vcpu_svm *svm = to_svm(vcpu);
  1565. switch (ecx) {
  1566. case MSR_IA32_TIME_STAMP_COUNTER: {
  1567. u64 tsc;
  1568. rdtscll(tsc);
  1569. *data = svm->vmcb->control.tsc_offset + tsc;
  1570. break;
  1571. }
  1572. case MSR_K6_STAR:
  1573. *data = svm->vmcb->save.star;
  1574. break;
  1575. #ifdef CONFIG_X86_64
  1576. case MSR_LSTAR:
  1577. *data = svm->vmcb->save.lstar;
  1578. break;
  1579. case MSR_CSTAR:
  1580. *data = svm->vmcb->save.cstar;
  1581. break;
  1582. case MSR_KERNEL_GS_BASE:
  1583. *data = svm->vmcb->save.kernel_gs_base;
  1584. break;
  1585. case MSR_SYSCALL_MASK:
  1586. *data = svm->vmcb->save.sfmask;
  1587. break;
  1588. #endif
  1589. case MSR_IA32_SYSENTER_CS:
  1590. *data = svm->vmcb->save.sysenter_cs;
  1591. break;
  1592. case MSR_IA32_SYSENTER_EIP:
  1593. *data = svm->vmcb->save.sysenter_eip;
  1594. break;
  1595. case MSR_IA32_SYSENTER_ESP:
  1596. *data = svm->vmcb->save.sysenter_esp;
  1597. break;
  1598. /* Nobody will change the following 5 values in the VMCB so
  1599. we can safely return them on rdmsr. They will always be 0
  1600. until LBRV is implemented. */
  1601. case MSR_IA32_DEBUGCTLMSR:
  1602. *data = svm->vmcb->save.dbgctl;
  1603. break;
  1604. case MSR_IA32_LASTBRANCHFROMIP:
  1605. *data = svm->vmcb->save.br_from;
  1606. break;
  1607. case MSR_IA32_LASTBRANCHTOIP:
  1608. *data = svm->vmcb->save.br_to;
  1609. break;
  1610. case MSR_IA32_LASTINTFROMIP:
  1611. *data = svm->vmcb->save.last_excp_from;
  1612. break;
  1613. case MSR_IA32_LASTINTTOIP:
  1614. *data = svm->vmcb->save.last_excp_to;
  1615. break;
  1616. case MSR_VM_HSAVE_PA:
  1617. *data = svm->hsave_msr;
  1618. break;
  1619. case MSR_VM_CR:
  1620. *data = 0;
  1621. break;
  1622. case MSR_IA32_UCODE_REV:
  1623. *data = 0x01000065;
  1624. break;
  1625. default:
  1626. return kvm_get_msr_common(vcpu, ecx, data);
  1627. }
  1628. return 0;
  1629. }
  1630. static int rdmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1631. {
  1632. u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  1633. u64 data;
  1634. if (svm_get_msr(&svm->vcpu, ecx, &data))
  1635. kvm_inject_gp(&svm->vcpu, 0);
  1636. else {
  1637. KVMTRACE_3D(MSR_READ, &svm->vcpu, ecx, (u32)data,
  1638. (u32)(data >> 32), handler);
  1639. svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
  1640. svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
  1641. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  1642. skip_emulated_instruction(&svm->vcpu);
  1643. }
  1644. return 1;
  1645. }
  1646. static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
  1647. {
  1648. struct vcpu_svm *svm = to_svm(vcpu);
  1649. switch (ecx) {
  1650. case MSR_IA32_TIME_STAMP_COUNTER: {
  1651. u64 tsc;
  1652. rdtscll(tsc);
  1653. svm->vmcb->control.tsc_offset = data - tsc;
  1654. break;
  1655. }
  1656. case MSR_K6_STAR:
  1657. svm->vmcb->save.star = data;
  1658. break;
  1659. #ifdef CONFIG_X86_64
  1660. case MSR_LSTAR:
  1661. svm->vmcb->save.lstar = data;
  1662. break;
  1663. case MSR_CSTAR:
  1664. svm->vmcb->save.cstar = data;
  1665. break;
  1666. case MSR_KERNEL_GS_BASE:
  1667. svm->vmcb->save.kernel_gs_base = data;
  1668. break;
  1669. case MSR_SYSCALL_MASK:
  1670. svm->vmcb->save.sfmask = data;
  1671. break;
  1672. #endif
  1673. case MSR_IA32_SYSENTER_CS:
  1674. svm->vmcb->save.sysenter_cs = data;
  1675. break;
  1676. case MSR_IA32_SYSENTER_EIP:
  1677. svm->vmcb->save.sysenter_eip = data;
  1678. break;
  1679. case MSR_IA32_SYSENTER_ESP:
  1680. svm->vmcb->save.sysenter_esp = data;
  1681. break;
  1682. case MSR_IA32_DEBUGCTLMSR:
  1683. if (!svm_has(SVM_FEATURE_LBRV)) {
  1684. pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
  1685. __func__, data);
  1686. break;
  1687. }
  1688. if (data & DEBUGCTL_RESERVED_BITS)
  1689. return 1;
  1690. svm->vmcb->save.dbgctl = data;
  1691. if (data & (1ULL<<0))
  1692. svm_enable_lbrv(svm);
  1693. else
  1694. svm_disable_lbrv(svm);
  1695. break;
  1696. case MSR_K7_EVNTSEL0:
  1697. case MSR_K7_EVNTSEL1:
  1698. case MSR_K7_EVNTSEL2:
  1699. case MSR_K7_EVNTSEL3:
  1700. case MSR_K7_PERFCTR0:
  1701. case MSR_K7_PERFCTR1:
  1702. case MSR_K7_PERFCTR2:
  1703. case MSR_K7_PERFCTR3:
  1704. /*
  1705. * Just discard all writes to the performance counters; this
  1706. * should keep both older linux and windows 64-bit guests
  1707. * happy
  1708. */
  1709. pr_unimpl(vcpu, "unimplemented perfctr wrmsr: 0x%x data 0x%llx\n", ecx, data);
  1710. break;
  1711. case MSR_VM_HSAVE_PA:
  1712. svm->hsave_msr = data;
  1713. break;
  1714. default:
  1715. return kvm_set_msr_common(vcpu, ecx, data);
  1716. }
  1717. return 0;
  1718. }
  1719. static int wrmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1720. {
  1721. u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  1722. u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
  1723. | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  1724. KVMTRACE_3D(MSR_WRITE, &svm->vcpu, ecx, (u32)data, (u32)(data >> 32),
  1725. handler);
  1726. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  1727. if (svm_set_msr(&svm->vcpu, ecx, data))
  1728. kvm_inject_gp(&svm->vcpu, 0);
  1729. else
  1730. skip_emulated_instruction(&svm->vcpu);
  1731. return 1;
  1732. }
  1733. static int msr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1734. {
  1735. if (svm->vmcb->control.exit_info_1)
  1736. return wrmsr_interception(svm, kvm_run);
  1737. else
  1738. return rdmsr_interception(svm, kvm_run);
  1739. }
  1740. static int interrupt_window_interception(struct vcpu_svm *svm,
  1741. struct kvm_run *kvm_run)
  1742. {
  1743. KVMTRACE_0D(PEND_INTR, &svm->vcpu, handler);
  1744. svm_clear_vintr(svm);
  1745. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  1746. /*
  1747. * If the user space waits to inject interrupts, exit as soon as
  1748. * possible
  1749. */
  1750. if (!irqchip_in_kernel(svm->vcpu.kvm) &&
  1751. kvm_run->request_interrupt_window &&
  1752. !kvm_cpu_has_interrupt(&svm->vcpu)) {
  1753. ++svm->vcpu.stat.irq_window_exits;
  1754. kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  1755. return 0;
  1756. }
  1757. return 1;
  1758. }
  1759. static int (*svm_exit_handlers[])(struct vcpu_svm *svm,
  1760. struct kvm_run *kvm_run) = {
  1761. [SVM_EXIT_READ_CR0] = emulate_on_interception,
  1762. [SVM_EXIT_READ_CR3] = emulate_on_interception,
  1763. [SVM_EXIT_READ_CR4] = emulate_on_interception,
  1764. [SVM_EXIT_READ_CR8] = emulate_on_interception,
  1765. /* for now: */
  1766. [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
  1767. [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
  1768. [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
  1769. [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
  1770. [SVM_EXIT_READ_DR0] = emulate_on_interception,
  1771. [SVM_EXIT_READ_DR1] = emulate_on_interception,
  1772. [SVM_EXIT_READ_DR2] = emulate_on_interception,
  1773. [SVM_EXIT_READ_DR3] = emulate_on_interception,
  1774. [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
  1775. [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
  1776. [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
  1777. [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
  1778. [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
  1779. [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
  1780. [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
  1781. [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
  1782. [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
  1783. [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
  1784. [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
  1785. [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
  1786. [SVM_EXIT_INTR] = intr_interception,
  1787. [SVM_EXIT_NMI] = nmi_interception,
  1788. [SVM_EXIT_SMI] = nop_on_interception,
  1789. [SVM_EXIT_INIT] = nop_on_interception,
  1790. [SVM_EXIT_VINTR] = interrupt_window_interception,
  1791. /* [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, */
  1792. [SVM_EXIT_CPUID] = cpuid_interception,
  1793. [SVM_EXIT_IRET] = iret_interception,
  1794. [SVM_EXIT_INVD] = emulate_on_interception,
  1795. [SVM_EXIT_HLT] = halt_interception,
  1796. [SVM_EXIT_INVLPG] = invlpg_interception,
  1797. [SVM_EXIT_INVLPGA] = invalid_op_interception,
  1798. [SVM_EXIT_IOIO] = io_interception,
  1799. [SVM_EXIT_MSR] = msr_interception,
  1800. [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
  1801. [SVM_EXIT_SHUTDOWN] = shutdown_interception,
  1802. [SVM_EXIT_VMRUN] = vmrun_interception,
  1803. [SVM_EXIT_VMMCALL] = vmmcall_interception,
  1804. [SVM_EXIT_VMLOAD] = vmload_interception,
  1805. [SVM_EXIT_VMSAVE] = vmsave_interception,
  1806. [SVM_EXIT_STGI] = stgi_interception,
  1807. [SVM_EXIT_CLGI] = clgi_interception,
  1808. [SVM_EXIT_SKINIT] = invalid_op_interception,
  1809. [SVM_EXIT_WBINVD] = emulate_on_interception,
  1810. [SVM_EXIT_MONITOR] = invalid_op_interception,
  1811. [SVM_EXIT_MWAIT] = invalid_op_interception,
  1812. [SVM_EXIT_NPF] = pf_interception,
  1813. };
  1814. static int handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  1815. {
  1816. struct vcpu_svm *svm = to_svm(vcpu);
  1817. u32 exit_code = svm->vmcb->control.exit_code;
  1818. KVMTRACE_3D(VMEXIT, vcpu, exit_code, (u32)svm->vmcb->save.rip,
  1819. (u32)((u64)svm->vmcb->save.rip >> 32), entryexit);
  1820. if (is_nested(svm)) {
  1821. nsvm_printk("nested handle_exit: 0x%x | 0x%lx | 0x%lx | 0x%lx\n",
  1822. exit_code, svm->vmcb->control.exit_info_1,
  1823. svm->vmcb->control.exit_info_2, svm->vmcb->save.rip);
  1824. if (nested_svm_exit_handled(svm, true)) {
  1825. nested_svm_vmexit(svm);
  1826. nsvm_printk("-> #VMEXIT\n");
  1827. return 1;
  1828. }
  1829. }
  1830. if (npt_enabled) {
  1831. int mmu_reload = 0;
  1832. if ((vcpu->arch.cr0 ^ svm->vmcb->save.cr0) & X86_CR0_PG) {
  1833. svm_set_cr0(vcpu, svm->vmcb->save.cr0);
  1834. mmu_reload = 1;
  1835. }
  1836. vcpu->arch.cr0 = svm->vmcb->save.cr0;
  1837. vcpu->arch.cr3 = svm->vmcb->save.cr3;
  1838. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  1839. if (!load_pdptrs(vcpu, vcpu->arch.cr3)) {
  1840. kvm_inject_gp(vcpu, 0);
  1841. return 1;
  1842. }
  1843. }
  1844. if (mmu_reload) {
  1845. kvm_mmu_reset_context(vcpu);
  1846. kvm_mmu_load(vcpu);
  1847. }
  1848. }
  1849. if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
  1850. kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  1851. kvm_run->fail_entry.hardware_entry_failure_reason
  1852. = svm->vmcb->control.exit_code;
  1853. return 0;
  1854. }
  1855. if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
  1856. exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
  1857. exit_code != SVM_EXIT_NPF)
  1858. printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
  1859. "exit_code 0x%x\n",
  1860. __func__, svm->vmcb->control.exit_int_info,
  1861. exit_code);
  1862. if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
  1863. || !svm_exit_handlers[exit_code]) {
  1864. kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
  1865. kvm_run->hw.hardware_exit_reason = exit_code;
  1866. return 0;
  1867. }
  1868. return svm_exit_handlers[exit_code](svm, kvm_run);
  1869. }
  1870. static void reload_tss(struct kvm_vcpu *vcpu)
  1871. {
  1872. int cpu = raw_smp_processor_id();
  1873. struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
  1874. svm_data->tss_desc->type = 9; /* available 32/64-bit TSS */
  1875. load_TR_desc();
  1876. }
  1877. static void pre_svm_run(struct vcpu_svm *svm)
  1878. {
  1879. int cpu = raw_smp_processor_id();
  1880. struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
  1881. svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
  1882. if (svm->vcpu.cpu != cpu ||
  1883. svm->asid_generation != svm_data->asid_generation)
  1884. new_asid(svm, svm_data);
  1885. }
  1886. static void svm_drop_interrupt_shadow(struct kvm_vcpu *vcpu)
  1887. {
  1888. struct vcpu_svm *svm = to_svm(vcpu);
  1889. svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
  1890. }
  1891. static void svm_inject_nmi(struct kvm_vcpu *vcpu)
  1892. {
  1893. struct vcpu_svm *svm = to_svm(vcpu);
  1894. svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
  1895. vcpu->arch.hflags |= HF_NMI_MASK;
  1896. svm->vmcb->control.intercept |= (1UL << INTERCEPT_IRET);
  1897. ++vcpu->stat.nmi_injections;
  1898. }
  1899. static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
  1900. {
  1901. struct vmcb_control_area *control;
  1902. KVMTRACE_1D(INJ_VIRQ, &svm->vcpu, (u32)irq, handler);
  1903. ++svm->vcpu.stat.irq_injections;
  1904. control = &svm->vmcb->control;
  1905. control->int_vector = irq;
  1906. control->int_ctl &= ~V_INTR_PRIO_MASK;
  1907. control->int_ctl |= V_IRQ_MASK |
  1908. ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
  1909. }
  1910. static void svm_queue_irq(struct kvm_vcpu *vcpu, unsigned nr)
  1911. {
  1912. struct vcpu_svm *svm = to_svm(vcpu);
  1913. svm->vmcb->control.event_inj = nr |
  1914. SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
  1915. }
  1916. static void svm_set_irq(struct kvm_vcpu *vcpu, int irq)
  1917. {
  1918. struct vcpu_svm *svm = to_svm(vcpu);
  1919. nested_svm_intr(svm);
  1920. svm_queue_irq(vcpu, irq);
  1921. }
  1922. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  1923. {
  1924. struct vcpu_svm *svm = to_svm(vcpu);
  1925. if (irr == -1)
  1926. return;
  1927. if (tpr >= irr)
  1928. svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK;
  1929. }
  1930. static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
  1931. {
  1932. struct vcpu_svm *svm = to_svm(vcpu);
  1933. struct vmcb *vmcb = svm->vmcb;
  1934. return !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
  1935. !(svm->vcpu.arch.hflags & HF_NMI_MASK);
  1936. }
  1937. static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
  1938. {
  1939. struct vcpu_svm *svm = to_svm(vcpu);
  1940. struct vmcb *vmcb = svm->vmcb;
  1941. return (vmcb->save.rflags & X86_EFLAGS_IF) &&
  1942. !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
  1943. (svm->vcpu.arch.hflags & HF_GIF_MASK);
  1944. }
  1945. static void enable_irq_window(struct kvm_vcpu *vcpu)
  1946. {
  1947. svm_set_vintr(to_svm(vcpu));
  1948. svm_inject_irq(to_svm(vcpu), 0x0);
  1949. }
  1950. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  1951. {
  1952. struct vcpu_svm *svm = to_svm(vcpu);
  1953. if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
  1954. enable_irq_window(vcpu);
  1955. }
  1956. static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
  1957. {
  1958. return 0;
  1959. }
  1960. static void svm_flush_tlb(struct kvm_vcpu *vcpu)
  1961. {
  1962. force_new_asid(vcpu);
  1963. }
  1964. static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
  1965. {
  1966. }
  1967. static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
  1968. {
  1969. struct vcpu_svm *svm = to_svm(vcpu);
  1970. if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) {
  1971. int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
  1972. kvm_set_cr8(vcpu, cr8);
  1973. }
  1974. }
  1975. static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
  1976. {
  1977. struct vcpu_svm *svm = to_svm(vcpu);
  1978. u64 cr8;
  1979. cr8 = kvm_get_cr8(vcpu);
  1980. svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
  1981. svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
  1982. }
  1983. static void svm_complete_interrupts(struct vcpu_svm *svm)
  1984. {
  1985. u8 vector;
  1986. int type;
  1987. u32 exitintinfo = svm->vmcb->control.exit_int_info;
  1988. svm->vcpu.arch.nmi_injected = false;
  1989. kvm_clear_exception_queue(&svm->vcpu);
  1990. kvm_clear_interrupt_queue(&svm->vcpu);
  1991. if (!(exitintinfo & SVM_EXITINTINFO_VALID))
  1992. return;
  1993. vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
  1994. type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
  1995. switch (type) {
  1996. case SVM_EXITINTINFO_TYPE_NMI:
  1997. svm->vcpu.arch.nmi_injected = true;
  1998. break;
  1999. case SVM_EXITINTINFO_TYPE_EXEPT:
  2000. /* In case of software exception do not reinject an exception
  2001. vector, but re-execute and instruction instead */
  2002. if (vector == BP_VECTOR || vector == OF_VECTOR)
  2003. break;
  2004. if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
  2005. u32 err = svm->vmcb->control.exit_int_info_err;
  2006. kvm_queue_exception_e(&svm->vcpu, vector, err);
  2007. } else
  2008. kvm_queue_exception(&svm->vcpu, vector);
  2009. break;
  2010. case SVM_EXITINTINFO_TYPE_INTR:
  2011. kvm_queue_interrupt(&svm->vcpu, vector);
  2012. break;
  2013. default:
  2014. break;
  2015. }
  2016. }
  2017. #ifdef CONFIG_X86_64
  2018. #define R "r"
  2019. #else
  2020. #define R "e"
  2021. #endif
  2022. static void svm_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2023. {
  2024. struct vcpu_svm *svm = to_svm(vcpu);
  2025. u16 fs_selector;
  2026. u16 gs_selector;
  2027. u16 ldt_selector;
  2028. svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
  2029. svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
  2030. svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
  2031. pre_svm_run(svm);
  2032. sync_lapic_to_cr8(vcpu);
  2033. save_host_msrs(vcpu);
  2034. fs_selector = kvm_read_fs();
  2035. gs_selector = kvm_read_gs();
  2036. ldt_selector = kvm_read_ldt();
  2037. svm->host_cr2 = kvm_read_cr2();
  2038. if (!is_nested(svm))
  2039. svm->vmcb->save.cr2 = vcpu->arch.cr2;
  2040. /* required for live migration with NPT */
  2041. if (npt_enabled)
  2042. svm->vmcb->save.cr3 = vcpu->arch.cr3;
  2043. clgi();
  2044. local_irq_enable();
  2045. asm volatile (
  2046. "push %%"R"bp; \n\t"
  2047. "mov %c[rbx](%[svm]), %%"R"bx \n\t"
  2048. "mov %c[rcx](%[svm]), %%"R"cx \n\t"
  2049. "mov %c[rdx](%[svm]), %%"R"dx \n\t"
  2050. "mov %c[rsi](%[svm]), %%"R"si \n\t"
  2051. "mov %c[rdi](%[svm]), %%"R"di \n\t"
  2052. "mov %c[rbp](%[svm]), %%"R"bp \n\t"
  2053. #ifdef CONFIG_X86_64
  2054. "mov %c[r8](%[svm]), %%r8 \n\t"
  2055. "mov %c[r9](%[svm]), %%r9 \n\t"
  2056. "mov %c[r10](%[svm]), %%r10 \n\t"
  2057. "mov %c[r11](%[svm]), %%r11 \n\t"
  2058. "mov %c[r12](%[svm]), %%r12 \n\t"
  2059. "mov %c[r13](%[svm]), %%r13 \n\t"
  2060. "mov %c[r14](%[svm]), %%r14 \n\t"
  2061. "mov %c[r15](%[svm]), %%r15 \n\t"
  2062. #endif
  2063. /* Enter guest mode */
  2064. "push %%"R"ax \n\t"
  2065. "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
  2066. __ex(SVM_VMLOAD) "\n\t"
  2067. __ex(SVM_VMRUN) "\n\t"
  2068. __ex(SVM_VMSAVE) "\n\t"
  2069. "pop %%"R"ax \n\t"
  2070. /* Save guest registers, load host registers */
  2071. "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
  2072. "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
  2073. "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
  2074. "mov %%"R"si, %c[rsi](%[svm]) \n\t"
  2075. "mov %%"R"di, %c[rdi](%[svm]) \n\t"
  2076. "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
  2077. #ifdef CONFIG_X86_64
  2078. "mov %%r8, %c[r8](%[svm]) \n\t"
  2079. "mov %%r9, %c[r9](%[svm]) \n\t"
  2080. "mov %%r10, %c[r10](%[svm]) \n\t"
  2081. "mov %%r11, %c[r11](%[svm]) \n\t"
  2082. "mov %%r12, %c[r12](%[svm]) \n\t"
  2083. "mov %%r13, %c[r13](%[svm]) \n\t"
  2084. "mov %%r14, %c[r14](%[svm]) \n\t"
  2085. "mov %%r15, %c[r15](%[svm]) \n\t"
  2086. #endif
  2087. "pop %%"R"bp"
  2088. :
  2089. : [svm]"a"(svm),
  2090. [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
  2091. [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
  2092. [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
  2093. [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
  2094. [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
  2095. [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
  2096. [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
  2097. #ifdef CONFIG_X86_64
  2098. , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
  2099. [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
  2100. [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
  2101. [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
  2102. [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
  2103. [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
  2104. [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
  2105. [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
  2106. #endif
  2107. : "cc", "memory"
  2108. , R"bx", R"cx", R"dx", R"si", R"di"
  2109. #ifdef CONFIG_X86_64
  2110. , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
  2111. #endif
  2112. );
  2113. vcpu->arch.cr2 = svm->vmcb->save.cr2;
  2114. vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
  2115. vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
  2116. vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
  2117. kvm_write_cr2(svm->host_cr2);
  2118. kvm_load_fs(fs_selector);
  2119. kvm_load_gs(gs_selector);
  2120. kvm_load_ldt(ldt_selector);
  2121. load_host_msrs(vcpu);
  2122. reload_tss(vcpu);
  2123. local_irq_disable();
  2124. stgi();
  2125. sync_cr8_to_lapic(vcpu);
  2126. svm->next_rip = 0;
  2127. svm_complete_interrupts(svm);
  2128. }
  2129. #undef R
  2130. static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
  2131. {
  2132. struct vcpu_svm *svm = to_svm(vcpu);
  2133. if (npt_enabled) {
  2134. svm->vmcb->control.nested_cr3 = root;
  2135. force_new_asid(vcpu);
  2136. return;
  2137. }
  2138. svm->vmcb->save.cr3 = root;
  2139. force_new_asid(vcpu);
  2140. if (vcpu->fpu_active) {
  2141. svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
  2142. svm->vmcb->save.cr0 |= X86_CR0_TS;
  2143. vcpu->fpu_active = 0;
  2144. }
  2145. }
  2146. static int is_disabled(void)
  2147. {
  2148. u64 vm_cr;
  2149. rdmsrl(MSR_VM_CR, vm_cr);
  2150. if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
  2151. return 1;
  2152. return 0;
  2153. }
  2154. static void
  2155. svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  2156. {
  2157. /*
  2158. * Patch in the VMMCALL instruction:
  2159. */
  2160. hypercall[0] = 0x0f;
  2161. hypercall[1] = 0x01;
  2162. hypercall[2] = 0xd9;
  2163. }
  2164. static void svm_check_processor_compat(void *rtn)
  2165. {
  2166. *(int *)rtn = 0;
  2167. }
  2168. static bool svm_cpu_has_accelerated_tpr(void)
  2169. {
  2170. return false;
  2171. }
  2172. static int get_npt_level(void)
  2173. {
  2174. #ifdef CONFIG_X86_64
  2175. return PT64_ROOT_LEVEL;
  2176. #else
  2177. return PT32E_ROOT_LEVEL;
  2178. #endif
  2179. }
  2180. static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  2181. {
  2182. return 0;
  2183. }
  2184. static struct kvm_x86_ops svm_x86_ops = {
  2185. .cpu_has_kvm_support = has_svm,
  2186. .disabled_by_bios = is_disabled,
  2187. .hardware_setup = svm_hardware_setup,
  2188. .hardware_unsetup = svm_hardware_unsetup,
  2189. .check_processor_compatibility = svm_check_processor_compat,
  2190. .hardware_enable = svm_hardware_enable,
  2191. .hardware_disable = svm_hardware_disable,
  2192. .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
  2193. .vcpu_create = svm_create_vcpu,
  2194. .vcpu_free = svm_free_vcpu,
  2195. .vcpu_reset = svm_vcpu_reset,
  2196. .prepare_guest_switch = svm_prepare_guest_switch,
  2197. .vcpu_load = svm_vcpu_load,
  2198. .vcpu_put = svm_vcpu_put,
  2199. .set_guest_debug = svm_guest_debug,
  2200. .get_msr = svm_get_msr,
  2201. .set_msr = svm_set_msr,
  2202. .get_segment_base = svm_get_segment_base,
  2203. .get_segment = svm_get_segment,
  2204. .set_segment = svm_set_segment,
  2205. .get_cpl = svm_get_cpl,
  2206. .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
  2207. .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
  2208. .set_cr0 = svm_set_cr0,
  2209. .set_cr3 = svm_set_cr3,
  2210. .set_cr4 = svm_set_cr4,
  2211. .set_efer = svm_set_efer,
  2212. .get_idt = svm_get_idt,
  2213. .set_idt = svm_set_idt,
  2214. .get_gdt = svm_get_gdt,
  2215. .set_gdt = svm_set_gdt,
  2216. .get_dr = svm_get_dr,
  2217. .set_dr = svm_set_dr,
  2218. .get_rflags = svm_get_rflags,
  2219. .set_rflags = svm_set_rflags,
  2220. .tlb_flush = svm_flush_tlb,
  2221. .run = svm_vcpu_run,
  2222. .handle_exit = handle_exit,
  2223. .skip_emulated_instruction = skip_emulated_instruction,
  2224. .patch_hypercall = svm_patch_hypercall,
  2225. .set_irq = svm_set_irq,
  2226. .set_nmi = svm_inject_nmi,
  2227. .queue_exception = svm_queue_exception,
  2228. .interrupt_allowed = svm_interrupt_allowed,
  2229. .nmi_allowed = svm_nmi_allowed,
  2230. .enable_nmi_window = enable_nmi_window,
  2231. .enable_irq_window = enable_irq_window,
  2232. .update_cr8_intercept = update_cr8_intercept,
  2233. .drop_interrupt_shadow = svm_drop_interrupt_shadow,
  2234. .set_tss_addr = svm_set_tss_addr,
  2235. .get_tdp_level = get_npt_level,
  2236. .get_mt_mask = svm_get_mt_mask,
  2237. };
  2238. static int __init svm_init(void)
  2239. {
  2240. return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
  2241. THIS_MODULE);
  2242. }
  2243. static void __exit svm_exit(void)
  2244. {
  2245. kvm_exit();
  2246. }
  2247. module_init(svm_init)
  2248. module_exit(svm_exit)