svm.c 69 KB

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