svm.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649
  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/module.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/highmem.h>
  19. #include <asm/desc.h>
  20. #include "kvm_svm.h"
  21. #include "x86_emulate.h"
  22. MODULE_AUTHOR("Qumranet");
  23. MODULE_LICENSE("GPL");
  24. #define IOPM_ALLOC_ORDER 2
  25. #define MSRPM_ALLOC_ORDER 1
  26. #define DB_VECTOR 1
  27. #define UD_VECTOR 6
  28. #define GP_VECTOR 13
  29. #define DR7_GD_MASK (1 << 13)
  30. #define DR6_BD_MASK (1 << 13)
  31. #define CR4_DE_MASK (1UL << 3)
  32. #define SEG_TYPE_LDT 2
  33. #define SEG_TYPE_BUSY_TSS16 3
  34. #define KVM_EFER_LMA (1 << 10)
  35. #define KVM_EFER_LME (1 << 8)
  36. unsigned long iopm_base;
  37. unsigned long msrpm_base;
  38. struct kvm_ldttss_desc {
  39. u16 limit0;
  40. u16 base0;
  41. unsigned base1 : 8, type : 5, dpl : 2, p : 1;
  42. unsigned limit1 : 4, zero0 : 3, g : 1, base2 : 8;
  43. u32 base3;
  44. u32 zero1;
  45. } __attribute__((packed));
  46. struct svm_cpu_data {
  47. int cpu;
  48. uint64_t asid_generation;
  49. uint32_t max_asid;
  50. uint32_t next_asid;
  51. struct kvm_ldttss_desc *tss_desc;
  52. struct page *save_area;
  53. };
  54. static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
  55. struct svm_init_data {
  56. int cpu;
  57. int r;
  58. };
  59. static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
  60. #define NUM_MSR_MAPS (sizeof(msrpm_ranges) / sizeof(*msrpm_ranges))
  61. #define MSRS_RANGE_SIZE 2048
  62. #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
  63. #define MAX_INST_SIZE 15
  64. static unsigned get_addr_size(struct kvm_vcpu *vcpu)
  65. {
  66. struct vmcb_save_area *sa = &vcpu->svm->vmcb->save;
  67. u16 cs_attrib;
  68. if (!(sa->cr0 & CR0_PE_MASK) || (sa->rflags & X86_EFLAGS_VM))
  69. return 2;
  70. cs_attrib = sa->cs.attrib;
  71. return (cs_attrib & SVM_SELECTOR_L_MASK) ? 8 :
  72. (cs_attrib & SVM_SELECTOR_DB_MASK) ? 4 : 2;
  73. }
  74. static inline u8 pop_irq(struct kvm_vcpu *vcpu)
  75. {
  76. int word_index = __ffs(vcpu->irq_summary);
  77. int bit_index = __ffs(vcpu->irq_pending[word_index]);
  78. int irq = word_index * BITS_PER_LONG + bit_index;
  79. clear_bit(bit_index, &vcpu->irq_pending[word_index]);
  80. if (!vcpu->irq_pending[word_index])
  81. clear_bit(word_index, &vcpu->irq_summary);
  82. return irq;
  83. }
  84. static inline void push_irq(struct kvm_vcpu *vcpu, u8 irq)
  85. {
  86. set_bit(irq, vcpu->irq_pending);
  87. set_bit(irq / BITS_PER_LONG, &vcpu->irq_summary);
  88. }
  89. static inline void clgi(void)
  90. {
  91. asm volatile (SVM_CLGI);
  92. }
  93. static inline void stgi(void)
  94. {
  95. asm volatile (SVM_STGI);
  96. }
  97. static inline void invlpga(unsigned long addr, u32 asid)
  98. {
  99. asm volatile (SVM_INVLPGA :: "a"(addr), "c"(asid));
  100. }
  101. static inline unsigned long kvm_read_cr2(void)
  102. {
  103. unsigned long cr2;
  104. asm volatile ("mov %%cr2, %0" : "=r" (cr2));
  105. return cr2;
  106. }
  107. static inline void kvm_write_cr2(unsigned long val)
  108. {
  109. asm volatile ("mov %0, %%cr2" :: "r" (val));
  110. }
  111. static inline unsigned long read_dr6(void)
  112. {
  113. unsigned long dr6;
  114. asm volatile ("mov %%dr6, %0" : "=r" (dr6));
  115. return dr6;
  116. }
  117. static inline void write_dr6(unsigned long val)
  118. {
  119. asm volatile ("mov %0, %%dr6" :: "r" (val));
  120. }
  121. static inline unsigned long read_dr7(void)
  122. {
  123. unsigned long dr7;
  124. asm volatile ("mov %%dr7, %0" : "=r" (dr7));
  125. return dr7;
  126. }
  127. static inline void write_dr7(unsigned long val)
  128. {
  129. asm volatile ("mov %0, %%dr7" :: "r" (val));
  130. }
  131. static inline void force_new_asid(struct kvm_vcpu *vcpu)
  132. {
  133. vcpu->svm->asid_generation--;
  134. }
  135. static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
  136. {
  137. force_new_asid(vcpu);
  138. }
  139. static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  140. {
  141. if (!(efer & KVM_EFER_LMA))
  142. efer &= ~KVM_EFER_LME;
  143. vcpu->svm->vmcb->save.efer = efer | MSR_EFER_SVME_MASK;
  144. vcpu->shadow_efer = efer;
  145. }
  146. static void svm_inject_gp(struct kvm_vcpu *vcpu, unsigned error_code)
  147. {
  148. vcpu->svm->vmcb->control.event_inj = SVM_EVTINJ_VALID |
  149. SVM_EVTINJ_VALID_ERR |
  150. SVM_EVTINJ_TYPE_EXEPT |
  151. GP_VECTOR;
  152. vcpu->svm->vmcb->control.event_inj_err = error_code;
  153. }
  154. static void inject_ud(struct kvm_vcpu *vcpu)
  155. {
  156. vcpu->svm->vmcb->control.event_inj = SVM_EVTINJ_VALID |
  157. SVM_EVTINJ_TYPE_EXEPT |
  158. UD_VECTOR;
  159. }
  160. static void inject_db(struct kvm_vcpu *vcpu)
  161. {
  162. vcpu->svm->vmcb->control.event_inj = SVM_EVTINJ_VALID |
  163. SVM_EVTINJ_TYPE_EXEPT |
  164. DB_VECTOR;
  165. }
  166. static int is_page_fault(uint32_t info)
  167. {
  168. info &= SVM_EVTINJ_VEC_MASK | SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
  169. return info == (PF_VECTOR | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT);
  170. }
  171. static int is_external_interrupt(u32 info)
  172. {
  173. info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
  174. return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
  175. }
  176. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  177. {
  178. if (!vcpu->svm->next_rip) {
  179. printk(KERN_DEBUG "%s: NOP\n", __FUNCTION__);
  180. return;
  181. }
  182. if (vcpu->svm->next_rip - vcpu->svm->vmcb->save.rip > 15) {
  183. printk(KERN_ERR "%s: ip 0x%llx next 0x%llx\n",
  184. __FUNCTION__,
  185. vcpu->svm->vmcb->save.rip,
  186. vcpu->svm->next_rip);
  187. }
  188. vcpu->rip = vcpu->svm->vmcb->save.rip = vcpu->svm->next_rip;
  189. vcpu->svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
  190. }
  191. static int has_svm(void)
  192. {
  193. uint32_t eax, ebx, ecx, edx;
  194. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD) {
  195. printk(KERN_INFO "has_svm: not amd\n");
  196. return 0;
  197. }
  198. cpuid(0x80000000, &eax, &ebx, &ecx, &edx);
  199. if (eax < SVM_CPUID_FUNC) {
  200. printk(KERN_INFO "has_svm: can't execute cpuid_8000000a\n");
  201. return 0;
  202. }
  203. cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
  204. if (!(ecx & (1 << SVM_CPUID_FEATURE_SHIFT))) {
  205. printk(KERN_DEBUG "has_svm: svm not available\n");
  206. return 0;
  207. }
  208. return 1;
  209. }
  210. static void svm_hardware_disable(void *garbage)
  211. {
  212. struct svm_cpu_data *svm_data
  213. = per_cpu(svm_data, raw_smp_processor_id());
  214. if (svm_data) {
  215. uint64_t efer;
  216. wrmsrl(MSR_VM_HSAVE_PA, 0);
  217. rdmsrl(MSR_EFER, efer);
  218. wrmsrl(MSR_EFER, efer & ~MSR_EFER_SVME_MASK);
  219. per_cpu(svm_data, raw_smp_processor_id()) = 0;
  220. __free_page(svm_data->save_area);
  221. kfree(svm_data);
  222. }
  223. }
  224. static void svm_hardware_enable(void *garbage)
  225. {
  226. struct svm_cpu_data *svm_data;
  227. uint64_t efer;
  228. #ifdef CONFIG_X86_64
  229. struct desc_ptr gdt_descr;
  230. #else
  231. struct Xgt_desc_struct gdt_descr;
  232. #endif
  233. struct desc_struct *gdt;
  234. int me = raw_smp_processor_id();
  235. if (!has_svm()) {
  236. printk(KERN_ERR "svm_cpu_init: err EOPNOTSUPP on %d\n", me);
  237. return;
  238. }
  239. svm_data = per_cpu(svm_data, me);
  240. if (!svm_data) {
  241. printk(KERN_ERR "svm_cpu_init: svm_data is NULL on %d\n",
  242. me);
  243. return;
  244. }
  245. svm_data->asid_generation = 1;
  246. svm_data->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
  247. svm_data->next_asid = svm_data->max_asid + 1;
  248. asm volatile ( "sgdt %0" : "=m"(gdt_descr) );
  249. gdt = (struct desc_struct *)gdt_descr.address;
  250. svm_data->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
  251. rdmsrl(MSR_EFER, efer);
  252. wrmsrl(MSR_EFER, efer | MSR_EFER_SVME_MASK);
  253. wrmsrl(MSR_VM_HSAVE_PA,
  254. page_to_pfn(svm_data->save_area) << PAGE_SHIFT);
  255. }
  256. static int svm_cpu_init(int cpu)
  257. {
  258. struct svm_cpu_data *svm_data;
  259. int r;
  260. svm_data = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
  261. if (!svm_data)
  262. return -ENOMEM;
  263. svm_data->cpu = cpu;
  264. svm_data->save_area = alloc_page(GFP_KERNEL);
  265. r = -ENOMEM;
  266. if (!svm_data->save_area)
  267. goto err_1;
  268. per_cpu(svm_data, cpu) = svm_data;
  269. return 0;
  270. err_1:
  271. kfree(svm_data);
  272. return r;
  273. }
  274. static int set_msr_interception(u32 *msrpm, unsigned msr,
  275. int read, int write)
  276. {
  277. int i;
  278. for (i = 0; i < NUM_MSR_MAPS; i++) {
  279. if (msr >= msrpm_ranges[i] &&
  280. msr < msrpm_ranges[i] + MSRS_IN_RANGE) {
  281. u32 msr_offset = (i * MSRS_IN_RANGE + msr -
  282. msrpm_ranges[i]) * 2;
  283. u32 *base = msrpm + (msr_offset / 32);
  284. u32 msr_shift = msr_offset % 32;
  285. u32 mask = ((write) ? 0 : 2) | ((read) ? 0 : 1);
  286. *base = (*base & ~(0x3 << msr_shift)) |
  287. (mask << msr_shift);
  288. return 1;
  289. }
  290. }
  291. printk(KERN_DEBUG "%s: not found 0x%x\n", __FUNCTION__, msr);
  292. return 0;
  293. }
  294. static __init int svm_hardware_setup(void)
  295. {
  296. int cpu;
  297. struct page *iopm_pages;
  298. struct page *msrpm_pages;
  299. void *msrpm_va;
  300. int r;
  301. kvm_emulator_want_group7_invlpg();
  302. iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
  303. if (!iopm_pages)
  304. return -ENOMEM;
  305. memset(page_address(iopm_pages), 0xff,
  306. PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
  307. iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
  308. msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
  309. r = -ENOMEM;
  310. if (!msrpm_pages)
  311. goto err_1;
  312. msrpm_va = page_address(msrpm_pages);
  313. memset(msrpm_va, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
  314. msrpm_base = page_to_pfn(msrpm_pages) << PAGE_SHIFT;
  315. #ifdef CONFIG_X86_64
  316. set_msr_interception(msrpm_va, MSR_GS_BASE, 1, 1);
  317. set_msr_interception(msrpm_va, MSR_FS_BASE, 1, 1);
  318. set_msr_interception(msrpm_va, MSR_KERNEL_GS_BASE, 1, 1);
  319. set_msr_interception(msrpm_va, MSR_LSTAR, 1, 1);
  320. set_msr_interception(msrpm_va, MSR_CSTAR, 1, 1);
  321. set_msr_interception(msrpm_va, MSR_SYSCALL_MASK, 1, 1);
  322. #endif
  323. set_msr_interception(msrpm_va, MSR_K6_STAR, 1, 1);
  324. set_msr_interception(msrpm_va, MSR_IA32_SYSENTER_CS, 1, 1);
  325. set_msr_interception(msrpm_va, MSR_IA32_SYSENTER_ESP, 1, 1);
  326. set_msr_interception(msrpm_va, MSR_IA32_SYSENTER_EIP, 1, 1);
  327. for_each_online_cpu(cpu) {
  328. r = svm_cpu_init(cpu);
  329. if (r)
  330. goto err_2;
  331. }
  332. return 0;
  333. err_2:
  334. __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
  335. msrpm_base = 0;
  336. err_1:
  337. __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
  338. iopm_base = 0;
  339. return r;
  340. }
  341. static __exit void svm_hardware_unsetup(void)
  342. {
  343. __free_pages(pfn_to_page(msrpm_base >> PAGE_SHIFT), MSRPM_ALLOC_ORDER);
  344. __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
  345. iopm_base = msrpm_base = 0;
  346. }
  347. static void init_seg(struct vmcb_seg *seg)
  348. {
  349. seg->selector = 0;
  350. seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
  351. SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
  352. seg->limit = 0xffff;
  353. seg->base = 0;
  354. }
  355. static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
  356. {
  357. seg->selector = 0;
  358. seg->attrib = SVM_SELECTOR_P_MASK | type;
  359. seg->limit = 0xffff;
  360. seg->base = 0;
  361. }
  362. static int svm_vcpu_setup(struct kvm_vcpu *vcpu)
  363. {
  364. return 0;
  365. }
  366. static void init_vmcb(struct vmcb *vmcb)
  367. {
  368. struct vmcb_control_area *control = &vmcb->control;
  369. struct vmcb_save_area *save = &vmcb->save;
  370. u64 tsc;
  371. control->intercept_cr_read = INTERCEPT_CR0_MASK |
  372. INTERCEPT_CR3_MASK |
  373. INTERCEPT_CR4_MASK;
  374. control->intercept_cr_write = INTERCEPT_CR0_MASK |
  375. INTERCEPT_CR3_MASK |
  376. INTERCEPT_CR4_MASK;
  377. control->intercept_dr_read = INTERCEPT_DR0_MASK |
  378. INTERCEPT_DR1_MASK |
  379. INTERCEPT_DR2_MASK |
  380. INTERCEPT_DR3_MASK;
  381. control->intercept_dr_write = INTERCEPT_DR0_MASK |
  382. INTERCEPT_DR1_MASK |
  383. INTERCEPT_DR2_MASK |
  384. INTERCEPT_DR3_MASK |
  385. INTERCEPT_DR5_MASK |
  386. INTERCEPT_DR7_MASK;
  387. control->intercept_exceptions = 1 << PF_VECTOR;
  388. control->intercept = (1ULL << INTERCEPT_INTR) |
  389. (1ULL << INTERCEPT_NMI) |
  390. /*
  391. * selective cr0 intercept bug?
  392. * 0: 0f 22 d8 mov %eax,%cr3
  393. * 3: 0f 20 c0 mov %cr0,%eax
  394. * 6: 0d 00 00 00 80 or $0x80000000,%eax
  395. * b: 0f 22 c0 mov %eax,%cr0
  396. * set cr3 ->interception
  397. * get cr0 ->interception
  398. * set cr0 -> no interception
  399. */
  400. /* (1ULL << INTERCEPT_SELECTIVE_CR0) | */
  401. (1ULL << INTERCEPT_CPUID) |
  402. (1ULL << INTERCEPT_HLT) |
  403. (1ULL << INTERCEPT_INVLPG) |
  404. (1ULL << INTERCEPT_INVLPGA) |
  405. (1ULL << INTERCEPT_IOIO_PROT) |
  406. (1ULL << INTERCEPT_MSR_PROT) |
  407. (1ULL << INTERCEPT_TASK_SWITCH) |
  408. (1ULL << INTERCEPT_VMRUN) |
  409. (1ULL << INTERCEPT_VMMCALL) |
  410. (1ULL << INTERCEPT_VMLOAD) |
  411. (1ULL << INTERCEPT_VMSAVE) |
  412. (1ULL << INTERCEPT_STGI) |
  413. (1ULL << INTERCEPT_CLGI) |
  414. (1ULL << INTERCEPT_SKINIT);
  415. control->iopm_base_pa = iopm_base;
  416. control->msrpm_base_pa = msrpm_base;
  417. rdtscll(tsc);
  418. control->tsc_offset = -tsc;
  419. control->int_ctl = V_INTR_MASKING_MASK;
  420. init_seg(&save->es);
  421. init_seg(&save->ss);
  422. init_seg(&save->ds);
  423. init_seg(&save->fs);
  424. init_seg(&save->gs);
  425. save->cs.selector = 0xf000;
  426. /* Executable/Readable Code Segment */
  427. save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
  428. SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
  429. save->cs.limit = 0xffff;
  430. save->cs.base = 0xffff0000;
  431. save->gdtr.limit = 0xffff;
  432. save->idtr.limit = 0xffff;
  433. init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
  434. init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
  435. save->efer = MSR_EFER_SVME_MASK;
  436. save->dr6 = 0xffff0ff0;
  437. save->dr7 = 0x400;
  438. save->rflags = 2;
  439. save->rip = 0x0000fff0;
  440. /*
  441. * cr0 val on cpu init should be 0x60000010, we enable cpu
  442. * cache by default. the orderly way is to enable cache in bios.
  443. */
  444. save->cr0 = 0x00000010 | CR0_PG_MASK;
  445. save->cr4 = CR4_PAE_MASK;
  446. /* rdx = ?? */
  447. }
  448. static int svm_create_vcpu(struct kvm_vcpu *vcpu)
  449. {
  450. struct page *page;
  451. int r;
  452. r = -ENOMEM;
  453. vcpu->svm = kzalloc(sizeof *vcpu->svm, GFP_KERNEL);
  454. if (!vcpu->svm)
  455. goto out1;
  456. page = alloc_page(GFP_KERNEL);
  457. if (!page)
  458. goto out2;
  459. vcpu->svm->vmcb = page_address(page);
  460. memset(vcpu->svm->vmcb, 0, PAGE_SIZE);
  461. vcpu->svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
  462. vcpu->svm->cr0 = 0x00000010;
  463. vcpu->svm->asid_generation = 0;
  464. memset(vcpu->svm->db_regs, 0, sizeof(vcpu->svm->db_regs));
  465. init_vmcb(vcpu->svm->vmcb);
  466. fx_init(vcpu);
  467. return 0;
  468. out2:
  469. kfree(vcpu->svm);
  470. out1:
  471. return r;
  472. }
  473. static void svm_free_vcpu(struct kvm_vcpu *vcpu)
  474. {
  475. if (!vcpu->svm)
  476. return;
  477. if (vcpu->svm->vmcb)
  478. __free_page(pfn_to_page(vcpu->svm->vmcb_pa >> PAGE_SHIFT));
  479. kfree(vcpu->svm);
  480. }
  481. static struct kvm_vcpu *svm_vcpu_load(struct kvm_vcpu *vcpu)
  482. {
  483. get_cpu();
  484. return vcpu;
  485. }
  486. static void svm_vcpu_put(struct kvm_vcpu *vcpu)
  487. {
  488. put_cpu();
  489. }
  490. static void svm_cache_regs(struct kvm_vcpu *vcpu)
  491. {
  492. vcpu->regs[VCPU_REGS_RAX] = vcpu->svm->vmcb->save.rax;
  493. vcpu->regs[VCPU_REGS_RSP] = vcpu->svm->vmcb->save.rsp;
  494. vcpu->rip = vcpu->svm->vmcb->save.rip;
  495. }
  496. static void svm_decache_regs(struct kvm_vcpu *vcpu)
  497. {
  498. vcpu->svm->vmcb->save.rax = vcpu->regs[VCPU_REGS_RAX];
  499. vcpu->svm->vmcb->save.rsp = vcpu->regs[VCPU_REGS_RSP];
  500. vcpu->svm->vmcb->save.rip = vcpu->rip;
  501. }
  502. static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
  503. {
  504. return vcpu->svm->vmcb->save.rflags;
  505. }
  506. static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  507. {
  508. vcpu->svm->vmcb->save.rflags = rflags;
  509. }
  510. static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
  511. {
  512. struct vmcb_save_area *save = &vcpu->svm->vmcb->save;
  513. switch (seg) {
  514. case VCPU_SREG_CS: return &save->cs;
  515. case VCPU_SREG_DS: return &save->ds;
  516. case VCPU_SREG_ES: return &save->es;
  517. case VCPU_SREG_FS: return &save->fs;
  518. case VCPU_SREG_GS: return &save->gs;
  519. case VCPU_SREG_SS: return &save->ss;
  520. case VCPU_SREG_TR: return &save->tr;
  521. case VCPU_SREG_LDTR: return &save->ldtr;
  522. }
  523. BUG();
  524. return 0;
  525. }
  526. static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  527. {
  528. struct vmcb_seg *s = svm_seg(vcpu, seg);
  529. return s->base;
  530. }
  531. static void svm_get_segment(struct kvm_vcpu *vcpu,
  532. struct kvm_segment *var, int seg)
  533. {
  534. struct vmcb_seg *s = svm_seg(vcpu, seg);
  535. var->base = s->base;
  536. var->limit = s->limit;
  537. var->selector = s->selector;
  538. var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
  539. var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
  540. var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
  541. var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
  542. var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
  543. var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
  544. var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
  545. var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
  546. var->unusable = !var->present;
  547. }
  548. static void svm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  549. {
  550. struct vmcb_seg *s = svm_seg(vcpu, VCPU_SREG_CS);
  551. *db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
  552. *l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
  553. }
  554. static void svm_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  555. {
  556. dt->limit = vcpu->svm->vmcb->save.ldtr.limit;
  557. dt->base = vcpu->svm->vmcb->save.ldtr.base;
  558. }
  559. static void svm_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  560. {
  561. vcpu->svm->vmcb->save.ldtr.limit = dt->limit;
  562. vcpu->svm->vmcb->save.ldtr.base = dt->base ;
  563. }
  564. static void svm_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  565. {
  566. dt->limit = vcpu->svm->vmcb->save.gdtr.limit;
  567. dt->base = vcpu->svm->vmcb->save.gdtr.base;
  568. }
  569. static void svm_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  570. {
  571. vcpu->svm->vmcb->save.gdtr.limit = dt->limit;
  572. vcpu->svm->vmcb->save.gdtr.base = dt->base ;
  573. }
  574. static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  575. {
  576. #ifdef CONFIG_X86_64
  577. if (vcpu->shadow_efer & KVM_EFER_LME) {
  578. if (!is_paging(vcpu) && (cr0 & CR0_PG_MASK)) {
  579. vcpu->shadow_efer |= KVM_EFER_LMA;
  580. vcpu->svm->vmcb->save.efer |= KVM_EFER_LMA | KVM_EFER_LME;
  581. }
  582. if (is_paging(vcpu) && !(cr0 & CR0_PG_MASK) ) {
  583. vcpu->shadow_efer &= ~KVM_EFER_LMA;
  584. vcpu->svm->vmcb->save.efer &= ~(KVM_EFER_LMA | KVM_EFER_LME);
  585. }
  586. }
  587. #endif
  588. vcpu->svm->cr0 = cr0;
  589. vcpu->svm->vmcb->save.cr0 = cr0 | CR0_PG_MASK;
  590. vcpu->cr0 = cr0;
  591. }
  592. static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  593. {
  594. vcpu->cr4 = cr4;
  595. vcpu->svm->vmcb->save.cr4 = cr4 | CR4_PAE_MASK;
  596. }
  597. static void svm_set_segment(struct kvm_vcpu *vcpu,
  598. struct kvm_segment *var, int seg)
  599. {
  600. struct vmcb_seg *s = svm_seg(vcpu, seg);
  601. s->base = var->base;
  602. s->limit = var->limit;
  603. s->selector = var->selector;
  604. if (var->unusable)
  605. s->attrib = 0;
  606. else {
  607. s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
  608. s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
  609. s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
  610. s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
  611. s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
  612. s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
  613. s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
  614. s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
  615. }
  616. if (seg == VCPU_SREG_CS)
  617. vcpu->svm->vmcb->save.cpl
  618. = (vcpu->svm->vmcb->save.cs.attrib
  619. >> SVM_SELECTOR_DPL_SHIFT) & 3;
  620. }
  621. /* FIXME:
  622. vcpu->svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
  623. vcpu->svm->vmcb->control.int_ctl |= (sregs->cr8 & V_TPR_MASK);
  624. */
  625. static int svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
  626. {
  627. return -EOPNOTSUPP;
  628. }
  629. static void load_host_msrs(struct kvm_vcpu *vcpu)
  630. {
  631. int i;
  632. for ( i = 0; i < NR_HOST_SAVE_MSRS; i++)
  633. wrmsrl(host_save_msrs[i], vcpu->svm->host_msrs[i]);
  634. }
  635. static void save_host_msrs(struct kvm_vcpu *vcpu)
  636. {
  637. int i;
  638. for ( i = 0; i < NR_HOST_SAVE_MSRS; i++)
  639. rdmsrl(host_save_msrs[i], vcpu->svm->host_msrs[i]);
  640. }
  641. static void new_asid(struct kvm_vcpu *vcpu, struct svm_cpu_data *svm_data)
  642. {
  643. if (svm_data->next_asid > svm_data->max_asid) {
  644. ++svm_data->asid_generation;
  645. svm_data->next_asid = 1;
  646. vcpu->svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
  647. }
  648. vcpu->cpu = svm_data->cpu;
  649. vcpu->svm->asid_generation = svm_data->asid_generation;
  650. vcpu->svm->vmcb->control.asid = svm_data->next_asid++;
  651. }
  652. static void svm_invlpg(struct kvm_vcpu *vcpu, gva_t address)
  653. {
  654. invlpga(address, vcpu->svm->vmcb->control.asid); // is needed?
  655. }
  656. static unsigned long svm_get_dr(struct kvm_vcpu *vcpu, int dr)
  657. {
  658. return vcpu->svm->db_regs[dr];
  659. }
  660. static void svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value,
  661. int *exception)
  662. {
  663. *exception = 0;
  664. if (vcpu->svm->vmcb->save.dr7 & DR7_GD_MASK) {
  665. vcpu->svm->vmcb->save.dr7 &= ~DR7_GD_MASK;
  666. vcpu->svm->vmcb->save.dr6 |= DR6_BD_MASK;
  667. *exception = DB_VECTOR;
  668. return;
  669. }
  670. switch (dr) {
  671. case 0 ... 3:
  672. vcpu->svm->db_regs[dr] = value;
  673. return;
  674. case 4 ... 5:
  675. if (vcpu->cr4 & CR4_DE_MASK) {
  676. *exception = UD_VECTOR;
  677. return;
  678. }
  679. case 7: {
  680. if (value & ~((1ULL << 32) - 1)) {
  681. *exception = GP_VECTOR;
  682. return;
  683. }
  684. vcpu->svm->vmcb->save.dr7 = value;
  685. return;
  686. }
  687. default:
  688. printk(KERN_DEBUG "%s: unexpected dr %u\n",
  689. __FUNCTION__, dr);
  690. *exception = UD_VECTOR;
  691. return;
  692. }
  693. }
  694. static int pf_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  695. {
  696. u32 exit_int_info = vcpu->svm->vmcb->control.exit_int_info;
  697. u64 fault_address;
  698. u32 error_code;
  699. enum emulation_result er;
  700. if (is_external_interrupt(exit_int_info))
  701. push_irq(vcpu, exit_int_info & SVM_EVTINJ_VEC_MASK);
  702. spin_lock(&vcpu->kvm->lock);
  703. fault_address = vcpu->svm->vmcb->control.exit_info_2;
  704. error_code = vcpu->svm->vmcb->control.exit_info_1;
  705. if (!vcpu->mmu.page_fault(vcpu, fault_address, error_code)) {
  706. spin_unlock(&vcpu->kvm->lock);
  707. return 1;
  708. }
  709. er = emulate_instruction(vcpu, kvm_run, fault_address, error_code);
  710. spin_unlock(&vcpu->kvm->lock);
  711. switch (er) {
  712. case EMULATE_DONE:
  713. return 1;
  714. case EMULATE_DO_MMIO:
  715. ++kvm_stat.mmio_exits;
  716. kvm_run->exit_reason = KVM_EXIT_MMIO;
  717. return 0;
  718. case EMULATE_FAIL:
  719. vcpu_printf(vcpu, "%s: emulate fail\n", __FUNCTION__);
  720. break;
  721. default:
  722. BUG();
  723. }
  724. kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
  725. return 0;
  726. }
  727. static int io_get_override(struct kvm_vcpu *vcpu,
  728. struct vmcb_seg **seg,
  729. int *addr_override)
  730. {
  731. u8 inst[MAX_INST_SIZE];
  732. unsigned ins_length;
  733. gva_t rip;
  734. int i;
  735. rip = vcpu->svm->vmcb->save.rip;
  736. ins_length = vcpu->svm->next_rip - rip;
  737. rip += vcpu->svm->vmcb->save.cs.base;
  738. if (ins_length > MAX_INST_SIZE)
  739. printk(KERN_DEBUG
  740. "%s: inst length err, cs base 0x%llx rip 0x%llx "
  741. "next rip 0x%llx ins_length %u\n",
  742. __FUNCTION__,
  743. vcpu->svm->vmcb->save.cs.base,
  744. vcpu->svm->vmcb->save.rip,
  745. vcpu->svm->vmcb->control.exit_info_2,
  746. ins_length);
  747. if (kvm_read_guest(vcpu, rip, ins_length, inst) != ins_length)
  748. /* #PF */
  749. return 0;
  750. *addr_override = 0;
  751. *seg = 0;
  752. for (i = 0; i < ins_length; i++)
  753. switch (inst[i]) {
  754. case 0xf0:
  755. case 0xf2:
  756. case 0xf3:
  757. case 0x66:
  758. continue;
  759. case 0x67:
  760. *addr_override = 1;
  761. continue;
  762. case 0x2e:
  763. *seg = &vcpu->svm->vmcb->save.cs;
  764. continue;
  765. case 0x36:
  766. *seg = &vcpu->svm->vmcb->save.ss;
  767. continue;
  768. case 0x3e:
  769. *seg = &vcpu->svm->vmcb->save.ds;
  770. continue;
  771. case 0x26:
  772. *seg = &vcpu->svm->vmcb->save.es;
  773. continue;
  774. case 0x64:
  775. *seg = &vcpu->svm->vmcb->save.fs;
  776. continue;
  777. case 0x65:
  778. *seg = &vcpu->svm->vmcb->save.gs;
  779. continue;
  780. default:
  781. return 1;
  782. }
  783. printk(KERN_DEBUG "%s: unexpected\n", __FUNCTION__);
  784. return 0;
  785. }
  786. static unsigned long io_adress(struct kvm_vcpu *vcpu, int ins, u64 *address)
  787. {
  788. unsigned long addr_mask;
  789. unsigned long *reg;
  790. struct vmcb_seg *seg;
  791. int addr_override;
  792. struct vmcb_save_area *save_area = &vcpu->svm->vmcb->save;
  793. u16 cs_attrib = save_area->cs.attrib;
  794. unsigned addr_size = get_addr_size(vcpu);
  795. if (!io_get_override(vcpu, &seg, &addr_override))
  796. return 0;
  797. if (addr_override)
  798. addr_size = (addr_size == 2) ? 4: (addr_size >> 1);
  799. if (ins) {
  800. reg = &vcpu->regs[VCPU_REGS_RDI];
  801. seg = &vcpu->svm->vmcb->save.es;
  802. } else {
  803. reg = &vcpu->regs[VCPU_REGS_RSI];
  804. seg = (seg) ? seg : &vcpu->svm->vmcb->save.ds;
  805. }
  806. addr_mask = ~0ULL >> (64 - (addr_size * 8));
  807. if ((cs_attrib & SVM_SELECTOR_L_MASK) &&
  808. !(vcpu->svm->vmcb->save.rflags & X86_EFLAGS_VM)) {
  809. *address = (*reg & addr_mask);
  810. return addr_mask;
  811. }
  812. if (!(seg->attrib & SVM_SELECTOR_P_SHIFT)) {
  813. svm_inject_gp(vcpu, 0);
  814. return 0;
  815. }
  816. *address = (*reg & addr_mask) + seg->base;
  817. return addr_mask;
  818. }
  819. static int io_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  820. {
  821. u32 io_info = vcpu->svm->vmcb->control.exit_info_1; //address size bug?
  822. int _in = io_info & SVM_IOIO_TYPE_MASK;
  823. ++kvm_stat.io_exits;
  824. vcpu->svm->next_rip = vcpu->svm->vmcb->control.exit_info_2;
  825. kvm_run->exit_reason = KVM_EXIT_IO;
  826. kvm_run->io.port = io_info >> 16;
  827. kvm_run->io.direction = (_in) ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
  828. kvm_run->io.size = ((io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT);
  829. kvm_run->io.string = (io_info & SVM_IOIO_STR_MASK) != 0;
  830. kvm_run->io.rep = (io_info & SVM_IOIO_REP_MASK) != 0;
  831. if (kvm_run->io.string) {
  832. unsigned addr_mask;
  833. addr_mask = io_adress(vcpu, _in, &kvm_run->io.address);
  834. if (!addr_mask) {
  835. printk(KERN_DEBUG "%s: get io address failed\n", __FUNCTION__);
  836. return 1;
  837. }
  838. if (kvm_run->io.rep) {
  839. kvm_run->io.count = vcpu->regs[VCPU_REGS_RCX] & addr_mask;
  840. kvm_run->io.string_down = (vcpu->svm->vmcb->save.rflags
  841. & X86_EFLAGS_DF) != 0;
  842. }
  843. } else {
  844. kvm_run->io.value = vcpu->svm->vmcb->save.rax;
  845. }
  846. return 0;
  847. }
  848. static int nop_on_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  849. {
  850. return 1;
  851. }
  852. static int halt_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  853. {
  854. vcpu->svm->next_rip = vcpu->svm->vmcb->save.rip + 1;
  855. skip_emulated_instruction(vcpu);
  856. if (vcpu->irq_summary && (vcpu->svm->vmcb->save.rflags & X86_EFLAGS_IF))
  857. return 1;
  858. kvm_run->exit_reason = KVM_EXIT_HLT;
  859. return 0;
  860. }
  861. static int invalid_op_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  862. {
  863. inject_ud(vcpu);
  864. return 1;
  865. }
  866. static int task_switch_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  867. {
  868. printk(KERN_DEBUG "%s: task swiche is unsupported\n", __FUNCTION__);
  869. kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
  870. return 0;
  871. }
  872. static int cpuid_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  873. {
  874. vcpu->svm->next_rip = vcpu->svm->vmcb->save.rip + 2;
  875. kvm_run->exit_reason = KVM_EXIT_CPUID;
  876. return 0;
  877. }
  878. static int emulate_on_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  879. {
  880. if (emulate_instruction(vcpu, 0, 0, 0) != EMULATE_DONE)
  881. printk(KERN_ERR "%s: failed\n", __FUNCTION__);
  882. return 1;
  883. }
  884. static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
  885. {
  886. switch (ecx) {
  887. case 0xc0010010: /* SYSCFG */
  888. case 0xc0010015: /* HWCR */
  889. case MSR_IA32_PLATFORM_ID:
  890. case MSR_IA32_P5_MC_ADDR:
  891. case MSR_IA32_P5_MC_TYPE:
  892. case MSR_IA32_MC0_CTL:
  893. case MSR_IA32_MCG_STATUS:
  894. case MSR_IA32_MCG_CAP:
  895. case MSR_IA32_MC0_MISC:
  896. case MSR_IA32_MC0_MISC+4:
  897. case MSR_IA32_MC0_MISC+8:
  898. case MSR_IA32_MC0_MISC+12:
  899. case MSR_IA32_MC0_MISC+16:
  900. case MSR_IA32_UCODE_REV:
  901. /* MTRR registers */
  902. case 0xfe:
  903. case 0x200 ... 0x2ff:
  904. *data = 0;
  905. break;
  906. case MSR_IA32_TIME_STAMP_COUNTER: {
  907. u64 tsc;
  908. rdtscll(tsc);
  909. *data = vcpu->svm->vmcb->control.tsc_offset + tsc;
  910. break;
  911. }
  912. case MSR_EFER:
  913. *data = vcpu->shadow_efer;
  914. break;
  915. case MSR_IA32_APICBASE:
  916. *data = vcpu->apic_base;
  917. break;
  918. case MSR_K6_STAR:
  919. *data = vcpu->svm->vmcb->save.star;
  920. break;
  921. #ifdef CONFIG_X86_64
  922. case MSR_LSTAR:
  923. *data = vcpu->svm->vmcb->save.lstar;
  924. break;
  925. case MSR_CSTAR:
  926. *data = vcpu->svm->vmcb->save.cstar;
  927. break;
  928. case MSR_KERNEL_GS_BASE:
  929. *data = vcpu->svm->vmcb->save.kernel_gs_base;
  930. break;
  931. case MSR_SYSCALL_MASK:
  932. *data = vcpu->svm->vmcb->save.sfmask;
  933. break;
  934. #endif
  935. case MSR_IA32_SYSENTER_CS:
  936. *data = vcpu->svm->vmcb->save.sysenter_cs;
  937. break;
  938. case MSR_IA32_SYSENTER_EIP:
  939. *data = vcpu->svm->vmcb->save.sysenter_eip;
  940. break;
  941. case MSR_IA32_SYSENTER_ESP:
  942. *data = vcpu->svm->vmcb->save.sysenter_esp;
  943. break;
  944. default:
  945. printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", ecx);
  946. return 1;
  947. }
  948. return 0;
  949. }
  950. static int rdmsr_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  951. {
  952. u32 ecx = vcpu->regs[VCPU_REGS_RCX];
  953. u64 data;
  954. if (svm_get_msr(vcpu, ecx, &data))
  955. svm_inject_gp(vcpu, 0);
  956. else {
  957. vcpu->svm->vmcb->save.rax = data & 0xffffffff;
  958. vcpu->regs[VCPU_REGS_RDX] = data >> 32;
  959. vcpu->svm->next_rip = vcpu->svm->vmcb->save.rip + 2;
  960. skip_emulated_instruction(vcpu);
  961. }
  962. return 1;
  963. }
  964. static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
  965. {
  966. switch (ecx) {
  967. #ifdef CONFIG_X86_64
  968. case MSR_EFER:
  969. set_efer(vcpu, data);
  970. break;
  971. #endif
  972. case MSR_IA32_MC0_STATUS:
  973. printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n"
  974. , __FUNCTION__, data);
  975. break;
  976. case MSR_IA32_TIME_STAMP_COUNTER: {
  977. u64 tsc;
  978. rdtscll(tsc);
  979. vcpu->svm->vmcb->control.tsc_offset = data - tsc;
  980. break;
  981. }
  982. case MSR_IA32_UCODE_REV:
  983. case MSR_IA32_UCODE_WRITE:
  984. case 0x200 ... 0x2ff: /* MTRRs */
  985. break;
  986. case MSR_IA32_APICBASE:
  987. vcpu->apic_base = data;
  988. break;
  989. case MSR_K6_STAR:
  990. vcpu->svm->vmcb->save.star = data;
  991. break;
  992. #ifdef CONFIG_X86_64_
  993. case MSR_LSTAR:
  994. vcpu->svm->vmcb->save.lstar = data;
  995. break;
  996. case MSR_CSTAR:
  997. vcpu->svm->vmcb->save.cstar = data;
  998. break;
  999. case MSR_KERNEL_GS_BASE:
  1000. vcpu->svm->vmcb->save.kernel_gs_base = data;
  1001. break;
  1002. case MSR_SYSCALL_MASK:
  1003. vcpu->svm->vmcb->save.sfmask = data;
  1004. break;
  1005. #endif
  1006. case MSR_IA32_SYSENTER_CS:
  1007. vcpu->svm->vmcb->save.sysenter_cs = data;
  1008. break;
  1009. case MSR_IA32_SYSENTER_EIP:
  1010. vcpu->svm->vmcb->save.sysenter_eip = data;
  1011. break;
  1012. case MSR_IA32_SYSENTER_ESP:
  1013. vcpu->svm->vmcb->save.sysenter_esp = data;
  1014. break;
  1015. default:
  1016. printk(KERN_ERR "kvm: unhandled wrmsr: %x\n", ecx);
  1017. return 1;
  1018. }
  1019. return 0;
  1020. }
  1021. static int wrmsr_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  1022. {
  1023. u32 ecx = vcpu->regs[VCPU_REGS_RCX];
  1024. u64 data = (vcpu->svm->vmcb->save.rax & -1u)
  1025. | ((u64)(vcpu->regs[VCPU_REGS_RDX] & -1u) << 32);
  1026. vcpu->svm->next_rip = vcpu->svm->vmcb->save.rip + 2;
  1027. if (svm_set_msr(vcpu, ecx, data))
  1028. svm_inject_gp(vcpu, 0);
  1029. else
  1030. skip_emulated_instruction(vcpu);
  1031. return 1;
  1032. }
  1033. static int msr_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  1034. {
  1035. if (vcpu->svm->vmcb->control.exit_info_1)
  1036. return wrmsr_interception(vcpu, kvm_run);
  1037. else
  1038. return rdmsr_interception(vcpu, kvm_run);
  1039. }
  1040. static int (*svm_exit_handlers[])(struct kvm_vcpu *vcpu,
  1041. struct kvm_run *kvm_run) = {
  1042. [SVM_EXIT_READ_CR0] = emulate_on_interception,
  1043. [SVM_EXIT_READ_CR3] = emulate_on_interception,
  1044. [SVM_EXIT_READ_CR4] = emulate_on_interception,
  1045. /* for now: */
  1046. [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
  1047. [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
  1048. [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
  1049. [SVM_EXIT_READ_DR0] = emulate_on_interception,
  1050. [SVM_EXIT_READ_DR1] = emulate_on_interception,
  1051. [SVM_EXIT_READ_DR2] = emulate_on_interception,
  1052. [SVM_EXIT_READ_DR3] = emulate_on_interception,
  1053. [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
  1054. [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
  1055. [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
  1056. [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
  1057. [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
  1058. [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
  1059. [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
  1060. [SVM_EXIT_INTR] = nop_on_interception,
  1061. [SVM_EXIT_NMI] = nop_on_interception,
  1062. [SVM_EXIT_SMI] = nop_on_interception,
  1063. [SVM_EXIT_INIT] = nop_on_interception,
  1064. /* [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, */
  1065. [SVM_EXIT_CPUID] = cpuid_interception,
  1066. [SVM_EXIT_HLT] = halt_interception,
  1067. [SVM_EXIT_INVLPG] = emulate_on_interception,
  1068. [SVM_EXIT_INVLPGA] = invalid_op_interception,
  1069. [SVM_EXIT_IOIO] = io_interception,
  1070. [SVM_EXIT_MSR] = msr_interception,
  1071. [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
  1072. [SVM_EXIT_VMRUN] = invalid_op_interception,
  1073. [SVM_EXIT_VMMCALL] = invalid_op_interception,
  1074. [SVM_EXIT_VMLOAD] = invalid_op_interception,
  1075. [SVM_EXIT_VMSAVE] = invalid_op_interception,
  1076. [SVM_EXIT_STGI] = invalid_op_interception,
  1077. [SVM_EXIT_CLGI] = invalid_op_interception,
  1078. [SVM_EXIT_SKINIT] = invalid_op_interception,
  1079. };
  1080. static int handle_exit(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  1081. {
  1082. u32 exit_code = vcpu->svm->vmcb->control.exit_code;
  1083. kvm_run->exit_type = KVM_EXIT_TYPE_VM_EXIT;
  1084. if (is_external_interrupt(vcpu->svm->vmcb->control.exit_int_info) &&
  1085. exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR)
  1086. printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
  1087. "exit_code 0x%x\n",
  1088. __FUNCTION__, vcpu->svm->vmcb->control.exit_int_info,
  1089. exit_code);
  1090. if (exit_code >= sizeof(svm_exit_handlers) / sizeof(*svm_exit_handlers)
  1091. || svm_exit_handlers[exit_code] == 0) {
  1092. kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
  1093. printk(KERN_ERR "%s: 0x%x @ 0x%llx cr0 0x%lx rflags 0x%llx\n",
  1094. __FUNCTION__,
  1095. exit_code,
  1096. vcpu->svm->vmcb->save.rip,
  1097. vcpu->cr0,
  1098. vcpu->svm->vmcb->save.rflags);
  1099. return 0;
  1100. }
  1101. return svm_exit_handlers[exit_code](vcpu, kvm_run);
  1102. }
  1103. static void reload_tss(struct kvm_vcpu *vcpu)
  1104. {
  1105. int cpu = raw_smp_processor_id();
  1106. struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
  1107. svm_data->tss_desc->type = 9; //available 32/64-bit TSS
  1108. load_TR_desc();
  1109. }
  1110. static void pre_svm_run(struct kvm_vcpu *vcpu)
  1111. {
  1112. int cpu = raw_smp_processor_id();
  1113. struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
  1114. vcpu->svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
  1115. if (vcpu->cpu != cpu ||
  1116. vcpu->svm->asid_generation != svm_data->asid_generation)
  1117. new_asid(vcpu, svm_data);
  1118. }
  1119. static inline void kvm_try_inject_irq(struct kvm_vcpu *vcpu)
  1120. {
  1121. struct vmcb_control_area *control;
  1122. if (!vcpu->irq_summary)
  1123. return;
  1124. control = &vcpu->svm->vmcb->control;
  1125. control->int_vector = pop_irq(vcpu);
  1126. control->int_ctl &= ~V_INTR_PRIO_MASK;
  1127. control->int_ctl |= V_IRQ_MASK |
  1128. ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
  1129. }
  1130. static void kvm_reput_irq(struct kvm_vcpu *vcpu)
  1131. {
  1132. struct vmcb_control_area *control = &vcpu->svm->vmcb->control;
  1133. if (control->int_ctl & V_IRQ_MASK) {
  1134. control->int_ctl &= ~V_IRQ_MASK;
  1135. push_irq(vcpu, control->int_vector);
  1136. }
  1137. }
  1138. static void save_db_regs(unsigned long *db_regs)
  1139. {
  1140. asm volatile ("mov %%dr0, %0" : "=r"(db_regs[0]));
  1141. asm volatile ("mov %%dr1, %0" : "=r"(db_regs[1]));
  1142. asm volatile ("mov %%dr2, %0" : "=r"(db_regs[2]));
  1143. asm volatile ("mov %%dr3, %0" : "=r"(db_regs[3]));
  1144. }
  1145. static void load_db_regs(unsigned long *db_regs)
  1146. {
  1147. asm volatile ("mov %0, %%dr0" : : "r"(db_regs[0]));
  1148. asm volatile ("mov %0, %%dr1" : : "r"(db_regs[1]));
  1149. asm volatile ("mov %0, %%dr2" : : "r"(db_regs[2]));
  1150. asm volatile ("mov %0, %%dr3" : : "r"(db_regs[3]));
  1151. }
  1152. static int svm_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  1153. {
  1154. u16 fs_selector;
  1155. u16 gs_selector;
  1156. u16 ldt_selector;
  1157. again:
  1158. kvm_try_inject_irq(vcpu);
  1159. clgi();
  1160. pre_svm_run(vcpu);
  1161. save_host_msrs(vcpu);
  1162. fs_selector = read_fs();
  1163. gs_selector = read_gs();
  1164. ldt_selector = read_ldt();
  1165. vcpu->svm->host_cr2 = kvm_read_cr2();
  1166. vcpu->svm->host_dr6 = read_dr6();
  1167. vcpu->svm->host_dr7 = read_dr7();
  1168. vcpu->svm->vmcb->save.cr2 = vcpu->cr2;
  1169. if (vcpu->svm->vmcb->save.dr7 & 0xff) {
  1170. write_dr7(0);
  1171. save_db_regs(vcpu->svm->host_db_regs);
  1172. load_db_regs(vcpu->svm->db_regs);
  1173. }
  1174. fx_save(vcpu->host_fx_image);
  1175. fx_restore(vcpu->guest_fx_image);
  1176. asm volatile (
  1177. #ifdef CONFIG_X86_64
  1178. "push %%rbx; push %%rcx; push %%rdx;"
  1179. "push %%rsi; push %%rdi; push %%rbp;"
  1180. "push %%r8; push %%r9; push %%r10; push %%r11;"
  1181. "push %%r12; push %%r13; push %%r14; push %%r15;"
  1182. #else
  1183. "push %%ebx; push %%ecx; push %%edx;"
  1184. "push %%esi; push %%edi; push %%ebp;"
  1185. #endif
  1186. #ifdef CONFIG_X86_64
  1187. "mov %c[rbx](%[vcpu]), %%rbx \n\t"
  1188. "mov %c[rcx](%[vcpu]), %%rcx \n\t"
  1189. "mov %c[rdx](%[vcpu]), %%rdx \n\t"
  1190. "mov %c[rsi](%[vcpu]), %%rsi \n\t"
  1191. "mov %c[rdi](%[vcpu]), %%rdi \n\t"
  1192. "mov %c[rbp](%[vcpu]), %%rbp \n\t"
  1193. "mov %c[r8](%[vcpu]), %%r8 \n\t"
  1194. "mov %c[r9](%[vcpu]), %%r9 \n\t"
  1195. "mov %c[r10](%[vcpu]), %%r10 \n\t"
  1196. "mov %c[r11](%[vcpu]), %%r11 \n\t"
  1197. "mov %c[r12](%[vcpu]), %%r12 \n\t"
  1198. "mov %c[r13](%[vcpu]), %%r13 \n\t"
  1199. "mov %c[r14](%[vcpu]), %%r14 \n\t"
  1200. "mov %c[r15](%[vcpu]), %%r15 \n\t"
  1201. #else
  1202. "mov %c[rbx](%[vcpu]), %%ebx \n\t"
  1203. "mov %c[rcx](%[vcpu]), %%ecx \n\t"
  1204. "mov %c[rdx](%[vcpu]), %%edx \n\t"
  1205. "mov %c[rsi](%[vcpu]), %%esi \n\t"
  1206. "mov %c[rdi](%[vcpu]), %%edi \n\t"
  1207. "mov %c[rbp](%[vcpu]), %%ebp \n\t"
  1208. #endif
  1209. #ifdef CONFIG_X86_64
  1210. /* Enter guest mode */
  1211. "push %%rax \n\t"
  1212. "mov %c[svm](%[vcpu]), %%rax \n\t"
  1213. "mov %c[vmcb](%%rax), %%rax \n\t"
  1214. SVM_VMLOAD "\n\t"
  1215. SVM_VMRUN "\n\t"
  1216. SVM_VMSAVE "\n\t"
  1217. "pop %%rax \n\t"
  1218. #else
  1219. /* Enter guest mode */
  1220. "push %%eax \n\t"
  1221. "mov %c[svm](%[vcpu]), %%eax \n\t"
  1222. "mov %c[vmcb](%%eax), %%eax \n\t"
  1223. SVM_VMLOAD "\n\t"
  1224. SVM_VMRUN "\n\t"
  1225. SVM_VMSAVE "\n\t"
  1226. "pop %%eax \n\t"
  1227. #endif
  1228. /* Save guest registers, load host registers */
  1229. #ifdef CONFIG_X86_64
  1230. "mov %%rbx, %c[rbx](%[vcpu]) \n\t"
  1231. "mov %%rcx, %c[rcx](%[vcpu]) \n\t"
  1232. "mov %%rdx, %c[rdx](%[vcpu]) \n\t"
  1233. "mov %%rsi, %c[rsi](%[vcpu]) \n\t"
  1234. "mov %%rdi, %c[rdi](%[vcpu]) \n\t"
  1235. "mov %%rbp, %c[rbp](%[vcpu]) \n\t"
  1236. "mov %%r8, %c[r8](%[vcpu]) \n\t"
  1237. "mov %%r9, %c[r9](%[vcpu]) \n\t"
  1238. "mov %%r10, %c[r10](%[vcpu]) \n\t"
  1239. "mov %%r11, %c[r11](%[vcpu]) \n\t"
  1240. "mov %%r12, %c[r12](%[vcpu]) \n\t"
  1241. "mov %%r13, %c[r13](%[vcpu]) \n\t"
  1242. "mov %%r14, %c[r14](%[vcpu]) \n\t"
  1243. "mov %%r15, %c[r15](%[vcpu]) \n\t"
  1244. "pop %%r15; pop %%r14; pop %%r13; pop %%r12;"
  1245. "pop %%r11; pop %%r10; pop %%r9; pop %%r8;"
  1246. "pop %%rbp; pop %%rdi; pop %%rsi;"
  1247. "pop %%rdx; pop %%rcx; pop %%rbx; \n\t"
  1248. #else
  1249. "mov %%ebx, %c[rbx](%[vcpu]) \n\t"
  1250. "mov %%ecx, %c[rcx](%[vcpu]) \n\t"
  1251. "mov %%edx, %c[rdx](%[vcpu]) \n\t"
  1252. "mov %%esi, %c[rsi](%[vcpu]) \n\t"
  1253. "mov %%edi, %c[rdi](%[vcpu]) \n\t"
  1254. "mov %%ebp, %c[rbp](%[vcpu]) \n\t"
  1255. "pop %%ebp; pop %%edi; pop %%esi;"
  1256. "pop %%edx; pop %%ecx; pop %%ebx; \n\t"
  1257. #endif
  1258. :
  1259. : [vcpu]"a"(vcpu),
  1260. [svm]"i"(offsetof(struct kvm_vcpu, svm)),
  1261. [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
  1262. [rbx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RBX])),
  1263. [rcx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RCX])),
  1264. [rdx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RDX])),
  1265. [rsi]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RSI])),
  1266. [rdi]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RDI])),
  1267. [rbp]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RBP]))
  1268. #ifdef CONFIG_X86_64
  1269. ,[r8 ]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R8 ])),
  1270. [r9 ]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R9 ])),
  1271. [r10]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R10])),
  1272. [r11]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R11])),
  1273. [r12]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R12])),
  1274. [r13]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R13])),
  1275. [r14]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R14])),
  1276. [r15]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R15]))
  1277. #endif
  1278. : "cc", "memory" );
  1279. fx_save(vcpu->guest_fx_image);
  1280. fx_restore(vcpu->host_fx_image);
  1281. if ((vcpu->svm->vmcb->save.dr7 & 0xff))
  1282. load_db_regs(vcpu->svm->host_db_regs);
  1283. vcpu->cr2 = vcpu->svm->vmcb->save.cr2;
  1284. write_dr6(vcpu->svm->host_dr6);
  1285. write_dr7(vcpu->svm->host_dr7);
  1286. kvm_write_cr2(vcpu->svm->host_cr2);
  1287. load_fs(fs_selector);
  1288. load_gs(gs_selector);
  1289. load_ldt(ldt_selector);
  1290. load_host_msrs(vcpu);
  1291. reload_tss(vcpu);
  1292. stgi();
  1293. kvm_reput_irq(vcpu);
  1294. vcpu->svm->next_rip = 0;
  1295. if (vcpu->svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
  1296. kvm_run->exit_type = KVM_EXIT_TYPE_FAIL_ENTRY;
  1297. kvm_run->exit_reason = vcpu->svm->vmcb->control.exit_code;
  1298. return 0;
  1299. }
  1300. if (handle_exit(vcpu, kvm_run)) {
  1301. if (signal_pending(current)) {
  1302. ++kvm_stat.signal_exits;
  1303. return -EINTR;
  1304. }
  1305. kvm_resched(vcpu);
  1306. goto again;
  1307. }
  1308. return 0;
  1309. }
  1310. static void svm_flush_tlb(struct kvm_vcpu *vcpu)
  1311. {
  1312. force_new_asid(vcpu);
  1313. }
  1314. static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
  1315. {
  1316. vcpu->svm->vmcb->save.cr3 = root;
  1317. force_new_asid(vcpu);
  1318. }
  1319. static void svm_inject_page_fault(struct kvm_vcpu *vcpu,
  1320. unsigned long addr,
  1321. uint32_t err_code)
  1322. {
  1323. uint32_t exit_int_info = vcpu->svm->vmcb->control.exit_int_info;
  1324. ++kvm_stat.pf_guest;
  1325. if (is_page_fault(exit_int_info)) {
  1326. vcpu->svm->vmcb->control.event_inj_err = 0;
  1327. vcpu->svm->vmcb->control.event_inj = SVM_EVTINJ_VALID |
  1328. SVM_EVTINJ_VALID_ERR |
  1329. SVM_EVTINJ_TYPE_EXEPT |
  1330. DF_VECTOR;
  1331. return;
  1332. }
  1333. vcpu->cr2 = addr;
  1334. vcpu->svm->vmcb->save.cr2 = addr;
  1335. vcpu->svm->vmcb->control.event_inj = SVM_EVTINJ_VALID |
  1336. SVM_EVTINJ_VALID_ERR |
  1337. SVM_EVTINJ_TYPE_EXEPT |
  1338. PF_VECTOR;
  1339. vcpu->svm->vmcb->control.event_inj_err = err_code;
  1340. }
  1341. static int is_disabled(void)
  1342. {
  1343. return 0;
  1344. }
  1345. static struct kvm_arch_ops svm_arch_ops = {
  1346. .cpu_has_kvm_support = has_svm,
  1347. .disabled_by_bios = is_disabled,
  1348. .hardware_setup = svm_hardware_setup,
  1349. .hardware_unsetup = svm_hardware_unsetup,
  1350. .hardware_enable = svm_hardware_enable,
  1351. .hardware_disable = svm_hardware_disable,
  1352. .vcpu_create = svm_create_vcpu,
  1353. .vcpu_free = svm_free_vcpu,
  1354. .vcpu_load = svm_vcpu_load,
  1355. .vcpu_put = svm_vcpu_put,
  1356. .set_guest_debug = svm_guest_debug,
  1357. .get_msr = svm_get_msr,
  1358. .set_msr = svm_set_msr,
  1359. .get_segment_base = svm_get_segment_base,
  1360. .get_segment = svm_get_segment,
  1361. .set_segment = svm_set_segment,
  1362. .get_cs_db_l_bits = svm_get_cs_db_l_bits,
  1363. .set_cr0 = svm_set_cr0,
  1364. .set_cr0_no_modeswitch = svm_set_cr0,
  1365. .set_cr3 = svm_set_cr3,
  1366. .set_cr4 = svm_set_cr4,
  1367. .set_efer = svm_set_efer,
  1368. .get_idt = svm_get_idt,
  1369. .set_idt = svm_set_idt,
  1370. .get_gdt = svm_get_gdt,
  1371. .set_gdt = svm_set_gdt,
  1372. .get_dr = svm_get_dr,
  1373. .set_dr = svm_set_dr,
  1374. .cache_regs = svm_cache_regs,
  1375. .decache_regs = svm_decache_regs,
  1376. .get_rflags = svm_get_rflags,
  1377. .set_rflags = svm_set_rflags,
  1378. .invlpg = svm_invlpg,
  1379. .tlb_flush = svm_flush_tlb,
  1380. .inject_page_fault = svm_inject_page_fault,
  1381. .inject_gp = svm_inject_gp,
  1382. .run = svm_vcpu_run,
  1383. .skip_emulated_instruction = skip_emulated_instruction,
  1384. .vcpu_setup = svm_vcpu_setup,
  1385. };
  1386. static int __init svm_init(void)
  1387. {
  1388. return kvm_init_arch(&svm_arch_ops, THIS_MODULE);
  1389. }
  1390. static void __exit svm_exit(void)
  1391. {
  1392. kvm_exit_arch();
  1393. }
  1394. module_init(svm_init)
  1395. module_exit(svm_exit)