svm.c 43 KB

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