svm.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721
  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. save->cs.base = 0xffff0000;
  433. save->gdtr.limit = 0xffff;
  434. save->idtr.limit = 0xffff;
  435. init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
  436. init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
  437. save->efer = MSR_EFER_SVME_MASK;
  438. save->dr6 = 0xffff0ff0;
  439. save->dr7 = 0x400;
  440. save->rflags = 2;
  441. save->rip = 0x0000fff0;
  442. /*
  443. * cr0 val on cpu init should be 0x60000010, we enable cpu
  444. * cache by default. the orderly way is to enable cache in bios.
  445. */
  446. save->cr0 = 0x00000010 | CR0_PG_MASK;
  447. save->cr4 = CR4_PAE_MASK;
  448. /* rdx = ?? */
  449. }
  450. static int svm_create_vcpu(struct kvm_vcpu *vcpu)
  451. {
  452. struct page *page;
  453. int r;
  454. r = -ENOMEM;
  455. vcpu->svm = kzalloc(sizeof *vcpu->svm, GFP_KERNEL);
  456. if (!vcpu->svm)
  457. goto out1;
  458. page = alloc_page(GFP_KERNEL);
  459. if (!page)
  460. goto out2;
  461. vcpu->svm->vmcb = page_address(page);
  462. memset(vcpu->svm->vmcb, 0, PAGE_SIZE);
  463. vcpu->svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
  464. vcpu->svm->cr0 = 0x00000010;
  465. vcpu->svm->asid_generation = 0;
  466. memset(vcpu->svm->db_regs, 0, sizeof(vcpu->svm->db_regs));
  467. init_vmcb(vcpu->svm->vmcb);
  468. fx_init(vcpu);
  469. return 0;
  470. out2:
  471. kfree(vcpu->svm);
  472. out1:
  473. return r;
  474. }
  475. static void svm_free_vcpu(struct kvm_vcpu *vcpu)
  476. {
  477. if (!vcpu->svm)
  478. return;
  479. if (vcpu->svm->vmcb)
  480. __free_page(pfn_to_page(vcpu->svm->vmcb_pa >> PAGE_SHIFT));
  481. kfree(vcpu->svm);
  482. }
  483. static struct kvm_vcpu *svm_vcpu_load(struct kvm_vcpu *vcpu)
  484. {
  485. get_cpu();
  486. return vcpu;
  487. }
  488. static void svm_vcpu_put(struct kvm_vcpu *vcpu)
  489. {
  490. put_cpu();
  491. }
  492. static void svm_cache_regs(struct kvm_vcpu *vcpu)
  493. {
  494. vcpu->regs[VCPU_REGS_RAX] = vcpu->svm->vmcb->save.rax;
  495. vcpu->regs[VCPU_REGS_RSP] = vcpu->svm->vmcb->save.rsp;
  496. vcpu->rip = vcpu->svm->vmcb->save.rip;
  497. }
  498. static void svm_decache_regs(struct kvm_vcpu *vcpu)
  499. {
  500. vcpu->svm->vmcb->save.rax = vcpu->regs[VCPU_REGS_RAX];
  501. vcpu->svm->vmcb->save.rsp = vcpu->regs[VCPU_REGS_RSP];
  502. vcpu->svm->vmcb->save.rip = vcpu->rip;
  503. }
  504. static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
  505. {
  506. return vcpu->svm->vmcb->save.rflags;
  507. }
  508. static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  509. {
  510. vcpu->svm->vmcb->save.rflags = rflags;
  511. }
  512. static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
  513. {
  514. struct vmcb_save_area *save = &vcpu->svm->vmcb->save;
  515. switch (seg) {
  516. case VCPU_SREG_CS: return &save->cs;
  517. case VCPU_SREG_DS: return &save->ds;
  518. case VCPU_SREG_ES: return &save->es;
  519. case VCPU_SREG_FS: return &save->fs;
  520. case VCPU_SREG_GS: return &save->gs;
  521. case VCPU_SREG_SS: return &save->ss;
  522. case VCPU_SREG_TR: return &save->tr;
  523. case VCPU_SREG_LDTR: return &save->ldtr;
  524. }
  525. BUG();
  526. return NULL;
  527. }
  528. static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  529. {
  530. struct vmcb_seg *s = svm_seg(vcpu, seg);
  531. return s->base;
  532. }
  533. static void svm_get_segment(struct kvm_vcpu *vcpu,
  534. struct kvm_segment *var, int seg)
  535. {
  536. struct vmcb_seg *s = svm_seg(vcpu, seg);
  537. var->base = s->base;
  538. var->limit = s->limit;
  539. var->selector = s->selector;
  540. var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
  541. var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
  542. var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
  543. var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
  544. var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
  545. var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
  546. var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
  547. var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
  548. var->unusable = !var->present;
  549. }
  550. static void svm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  551. {
  552. struct vmcb_seg *s = svm_seg(vcpu, VCPU_SREG_CS);
  553. *db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
  554. *l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
  555. }
  556. static void svm_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  557. {
  558. dt->limit = vcpu->svm->vmcb->save.idtr.limit;
  559. dt->base = vcpu->svm->vmcb->save.idtr.base;
  560. }
  561. static void svm_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  562. {
  563. vcpu->svm->vmcb->save.idtr.limit = dt->limit;
  564. vcpu->svm->vmcb->save.idtr.base = dt->base ;
  565. }
  566. static void svm_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  567. {
  568. dt->limit = vcpu->svm->vmcb->save.gdtr.limit;
  569. dt->base = vcpu->svm->vmcb->save.gdtr.base;
  570. }
  571. static void svm_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  572. {
  573. vcpu->svm->vmcb->save.gdtr.limit = dt->limit;
  574. vcpu->svm->vmcb->save.gdtr.base = dt->base ;
  575. }
  576. static void svm_decache_cr0_cr4_guest_bits(struct kvm_vcpu *vcpu)
  577. {
  578. }
  579. static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  580. {
  581. #ifdef CONFIG_X86_64
  582. if (vcpu->shadow_efer & KVM_EFER_LME) {
  583. if (!is_paging(vcpu) && (cr0 & CR0_PG_MASK)) {
  584. vcpu->shadow_efer |= KVM_EFER_LMA;
  585. vcpu->svm->vmcb->save.efer |= KVM_EFER_LMA | KVM_EFER_LME;
  586. }
  587. if (is_paging(vcpu) && !(cr0 & CR0_PG_MASK) ) {
  588. vcpu->shadow_efer &= ~KVM_EFER_LMA;
  589. vcpu->svm->vmcb->save.efer &= ~(KVM_EFER_LMA | KVM_EFER_LME);
  590. }
  591. }
  592. #endif
  593. vcpu->svm->cr0 = cr0;
  594. vcpu->svm->vmcb->save.cr0 = cr0 | CR0_PG_MASK | CR0_WP_MASK;
  595. vcpu->cr0 = cr0;
  596. }
  597. static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  598. {
  599. vcpu->cr4 = cr4;
  600. vcpu->svm->vmcb->save.cr4 = cr4 | CR4_PAE_MASK;
  601. }
  602. static void svm_set_segment(struct kvm_vcpu *vcpu,
  603. struct kvm_segment *var, int seg)
  604. {
  605. struct vmcb_seg *s = svm_seg(vcpu, seg);
  606. s->base = var->base;
  607. s->limit = var->limit;
  608. s->selector = var->selector;
  609. if (var->unusable)
  610. s->attrib = 0;
  611. else {
  612. s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
  613. s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
  614. s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
  615. s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
  616. s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
  617. s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
  618. s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
  619. s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
  620. }
  621. if (seg == VCPU_SREG_CS)
  622. vcpu->svm->vmcb->save.cpl
  623. = (vcpu->svm->vmcb->save.cs.attrib
  624. >> SVM_SELECTOR_DPL_SHIFT) & 3;
  625. }
  626. /* FIXME:
  627. vcpu->svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
  628. vcpu->svm->vmcb->control.int_ctl |= (sregs->cr8 & V_TPR_MASK);
  629. */
  630. static int svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
  631. {
  632. return -EOPNOTSUPP;
  633. }
  634. static void load_host_msrs(struct kvm_vcpu *vcpu)
  635. {
  636. int i;
  637. for ( i = 0; i < NR_HOST_SAVE_MSRS; i++)
  638. wrmsrl(host_save_msrs[i], vcpu->svm->host_msrs[i]);
  639. }
  640. static void save_host_msrs(struct kvm_vcpu *vcpu)
  641. {
  642. int i;
  643. for ( i = 0; i < NR_HOST_SAVE_MSRS; i++)
  644. rdmsrl(host_save_msrs[i], vcpu->svm->host_msrs[i]);
  645. }
  646. static void new_asid(struct kvm_vcpu *vcpu, struct svm_cpu_data *svm_data)
  647. {
  648. if (svm_data->next_asid > svm_data->max_asid) {
  649. ++svm_data->asid_generation;
  650. svm_data->next_asid = 1;
  651. vcpu->svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
  652. }
  653. vcpu->cpu = svm_data->cpu;
  654. vcpu->svm->asid_generation = svm_data->asid_generation;
  655. vcpu->svm->vmcb->control.asid = svm_data->next_asid++;
  656. }
  657. static void svm_invlpg(struct kvm_vcpu *vcpu, gva_t address)
  658. {
  659. invlpga(address, vcpu->svm->vmcb->control.asid); // is needed?
  660. }
  661. static unsigned long svm_get_dr(struct kvm_vcpu *vcpu, int dr)
  662. {
  663. return vcpu->svm->db_regs[dr];
  664. }
  665. static void svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value,
  666. int *exception)
  667. {
  668. *exception = 0;
  669. if (vcpu->svm->vmcb->save.dr7 & DR7_GD_MASK) {
  670. vcpu->svm->vmcb->save.dr7 &= ~DR7_GD_MASK;
  671. vcpu->svm->vmcb->save.dr6 |= DR6_BD_MASK;
  672. *exception = DB_VECTOR;
  673. return;
  674. }
  675. switch (dr) {
  676. case 0 ... 3:
  677. vcpu->svm->db_regs[dr] = value;
  678. return;
  679. case 4 ... 5:
  680. if (vcpu->cr4 & CR4_DE_MASK) {
  681. *exception = UD_VECTOR;
  682. return;
  683. }
  684. case 7: {
  685. if (value & ~((1ULL << 32) - 1)) {
  686. *exception = GP_VECTOR;
  687. return;
  688. }
  689. vcpu->svm->vmcb->save.dr7 = value;
  690. return;
  691. }
  692. default:
  693. printk(KERN_DEBUG "%s: unexpected dr %u\n",
  694. __FUNCTION__, dr);
  695. *exception = UD_VECTOR;
  696. return;
  697. }
  698. }
  699. static int pf_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  700. {
  701. u32 exit_int_info = vcpu->svm->vmcb->control.exit_int_info;
  702. u64 fault_address;
  703. u32 error_code;
  704. enum emulation_result er;
  705. int r;
  706. if (is_external_interrupt(exit_int_info))
  707. push_irq(vcpu, exit_int_info & SVM_EVTINJ_VEC_MASK);
  708. spin_lock(&vcpu->kvm->lock);
  709. fault_address = vcpu->svm->vmcb->control.exit_info_2;
  710. error_code = vcpu->svm->vmcb->control.exit_info_1;
  711. r = kvm_mmu_page_fault(vcpu, fault_address, error_code);
  712. if (r < 0) {
  713. spin_unlock(&vcpu->kvm->lock);
  714. return r;
  715. }
  716. if (!r) {
  717. spin_unlock(&vcpu->kvm->lock);
  718. return 1;
  719. }
  720. er = emulate_instruction(vcpu, kvm_run, fault_address, error_code);
  721. spin_unlock(&vcpu->kvm->lock);
  722. switch (er) {
  723. case EMULATE_DONE:
  724. return 1;
  725. case EMULATE_DO_MMIO:
  726. ++kvm_stat.mmio_exits;
  727. kvm_run->exit_reason = KVM_EXIT_MMIO;
  728. return 0;
  729. case EMULATE_FAIL:
  730. vcpu_printf(vcpu, "%s: emulate fail\n", __FUNCTION__);
  731. break;
  732. default:
  733. BUG();
  734. }
  735. kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
  736. return 0;
  737. }
  738. static int shutdown_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  739. {
  740. /*
  741. * VMCB is undefined after a SHUTDOWN intercept
  742. * so reinitialize it.
  743. */
  744. memset(vcpu->svm->vmcb, 0, PAGE_SIZE);
  745. init_vmcb(vcpu->svm->vmcb);
  746. kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
  747. return 0;
  748. }
  749. static int io_get_override(struct kvm_vcpu *vcpu,
  750. struct vmcb_seg **seg,
  751. int *addr_override)
  752. {
  753. u8 inst[MAX_INST_SIZE];
  754. unsigned ins_length;
  755. gva_t rip;
  756. int i;
  757. rip = vcpu->svm->vmcb->save.rip;
  758. ins_length = vcpu->svm->next_rip - rip;
  759. rip += vcpu->svm->vmcb->save.cs.base;
  760. if (ins_length > MAX_INST_SIZE)
  761. printk(KERN_DEBUG
  762. "%s: inst length err, cs base 0x%llx rip 0x%llx "
  763. "next rip 0x%llx ins_length %u\n",
  764. __FUNCTION__,
  765. vcpu->svm->vmcb->save.cs.base,
  766. vcpu->svm->vmcb->save.rip,
  767. vcpu->svm->vmcb->control.exit_info_2,
  768. ins_length);
  769. if (kvm_read_guest(vcpu, rip, ins_length, inst) != ins_length)
  770. /* #PF */
  771. return 0;
  772. *addr_override = 0;
  773. *seg = NULL;
  774. for (i = 0; i < ins_length; i++)
  775. switch (inst[i]) {
  776. case 0xf0:
  777. case 0xf2:
  778. case 0xf3:
  779. case 0x66:
  780. continue;
  781. case 0x67:
  782. *addr_override = 1;
  783. continue;
  784. case 0x2e:
  785. *seg = &vcpu->svm->vmcb->save.cs;
  786. continue;
  787. case 0x36:
  788. *seg = &vcpu->svm->vmcb->save.ss;
  789. continue;
  790. case 0x3e:
  791. *seg = &vcpu->svm->vmcb->save.ds;
  792. continue;
  793. case 0x26:
  794. *seg = &vcpu->svm->vmcb->save.es;
  795. continue;
  796. case 0x64:
  797. *seg = &vcpu->svm->vmcb->save.fs;
  798. continue;
  799. case 0x65:
  800. *seg = &vcpu->svm->vmcb->save.gs;
  801. continue;
  802. default:
  803. return 1;
  804. }
  805. printk(KERN_DEBUG "%s: unexpected\n", __FUNCTION__);
  806. return 0;
  807. }
  808. static unsigned long io_adress(struct kvm_vcpu *vcpu, int ins, u64 *address)
  809. {
  810. unsigned long addr_mask;
  811. unsigned long *reg;
  812. struct vmcb_seg *seg;
  813. int addr_override;
  814. struct vmcb_save_area *save_area = &vcpu->svm->vmcb->save;
  815. u16 cs_attrib = save_area->cs.attrib;
  816. unsigned addr_size = get_addr_size(vcpu);
  817. if (!io_get_override(vcpu, &seg, &addr_override))
  818. return 0;
  819. if (addr_override)
  820. addr_size = (addr_size == 2) ? 4: (addr_size >> 1);
  821. if (ins) {
  822. reg = &vcpu->regs[VCPU_REGS_RDI];
  823. seg = &vcpu->svm->vmcb->save.es;
  824. } else {
  825. reg = &vcpu->regs[VCPU_REGS_RSI];
  826. seg = (seg) ? seg : &vcpu->svm->vmcb->save.ds;
  827. }
  828. addr_mask = ~0ULL >> (64 - (addr_size * 8));
  829. if ((cs_attrib & SVM_SELECTOR_L_MASK) &&
  830. !(vcpu->svm->vmcb->save.rflags & X86_EFLAGS_VM)) {
  831. *address = (*reg & addr_mask);
  832. return addr_mask;
  833. }
  834. if (!(seg->attrib & SVM_SELECTOR_P_SHIFT)) {
  835. svm_inject_gp(vcpu, 0);
  836. return 0;
  837. }
  838. *address = (*reg & addr_mask) + seg->base;
  839. return addr_mask;
  840. }
  841. static int io_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  842. {
  843. u32 io_info = vcpu->svm->vmcb->control.exit_info_1; //address size bug?
  844. int _in = io_info & SVM_IOIO_TYPE_MASK;
  845. ++kvm_stat.io_exits;
  846. vcpu->svm->next_rip = vcpu->svm->vmcb->control.exit_info_2;
  847. kvm_run->exit_reason = KVM_EXIT_IO;
  848. kvm_run->io.port = io_info >> 16;
  849. kvm_run->io.direction = (_in) ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
  850. kvm_run->io.size = ((io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT);
  851. kvm_run->io.string = (io_info & SVM_IOIO_STR_MASK) != 0;
  852. kvm_run->io.rep = (io_info & SVM_IOIO_REP_MASK) != 0;
  853. if (kvm_run->io.string) {
  854. unsigned addr_mask;
  855. addr_mask = io_adress(vcpu, _in, &kvm_run->io.address);
  856. if (!addr_mask) {
  857. printk(KERN_DEBUG "%s: get io address failed\n", __FUNCTION__);
  858. return 1;
  859. }
  860. if (kvm_run->io.rep) {
  861. kvm_run->io.count = vcpu->regs[VCPU_REGS_RCX] & addr_mask;
  862. kvm_run->io.string_down = (vcpu->svm->vmcb->save.rflags
  863. & X86_EFLAGS_DF) != 0;
  864. }
  865. } else {
  866. kvm_run->io.value = vcpu->svm->vmcb->save.rax;
  867. }
  868. return 0;
  869. }
  870. static int nop_on_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  871. {
  872. return 1;
  873. }
  874. static int halt_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  875. {
  876. vcpu->svm->next_rip = vcpu->svm->vmcb->save.rip + 1;
  877. skip_emulated_instruction(vcpu);
  878. if (vcpu->irq_summary)
  879. return 1;
  880. kvm_run->exit_reason = KVM_EXIT_HLT;
  881. ++kvm_stat.halt_exits;
  882. return 0;
  883. }
  884. static int invalid_op_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  885. {
  886. inject_ud(vcpu);
  887. return 1;
  888. }
  889. static int task_switch_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  890. {
  891. printk(KERN_DEBUG "%s: task swiche is unsupported\n", __FUNCTION__);
  892. kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
  893. return 0;
  894. }
  895. static int cpuid_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  896. {
  897. vcpu->svm->next_rip = vcpu->svm->vmcb->save.rip + 2;
  898. kvm_run->exit_reason = KVM_EXIT_CPUID;
  899. return 0;
  900. }
  901. static int emulate_on_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  902. {
  903. if (emulate_instruction(vcpu, NULL, 0, 0) != EMULATE_DONE)
  904. printk(KERN_ERR "%s: failed\n", __FUNCTION__);
  905. return 1;
  906. }
  907. static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
  908. {
  909. switch (ecx) {
  910. case MSR_IA32_TIME_STAMP_COUNTER: {
  911. u64 tsc;
  912. rdtscll(tsc);
  913. *data = vcpu->svm->vmcb->control.tsc_offset + tsc;
  914. break;
  915. }
  916. case MSR_K6_STAR:
  917. *data = vcpu->svm->vmcb->save.star;
  918. break;
  919. #ifdef CONFIG_X86_64
  920. case MSR_LSTAR:
  921. *data = vcpu->svm->vmcb->save.lstar;
  922. break;
  923. case MSR_CSTAR:
  924. *data = vcpu->svm->vmcb->save.cstar;
  925. break;
  926. case MSR_KERNEL_GS_BASE:
  927. *data = vcpu->svm->vmcb->save.kernel_gs_base;
  928. break;
  929. case MSR_SYSCALL_MASK:
  930. *data = vcpu->svm->vmcb->save.sfmask;
  931. break;
  932. #endif
  933. case MSR_IA32_SYSENTER_CS:
  934. *data = vcpu->svm->vmcb->save.sysenter_cs;
  935. break;
  936. case MSR_IA32_SYSENTER_EIP:
  937. *data = vcpu->svm->vmcb->save.sysenter_eip;
  938. break;
  939. case MSR_IA32_SYSENTER_ESP:
  940. *data = vcpu->svm->vmcb->save.sysenter_esp;
  941. break;
  942. default:
  943. return kvm_get_msr_common(vcpu, ecx, data);
  944. }
  945. return 0;
  946. }
  947. static int rdmsr_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  948. {
  949. u32 ecx = vcpu->regs[VCPU_REGS_RCX];
  950. u64 data;
  951. if (svm_get_msr(vcpu, ecx, &data))
  952. svm_inject_gp(vcpu, 0);
  953. else {
  954. vcpu->svm->vmcb->save.rax = data & 0xffffffff;
  955. vcpu->regs[VCPU_REGS_RDX] = data >> 32;
  956. vcpu->svm->next_rip = vcpu->svm->vmcb->save.rip + 2;
  957. skip_emulated_instruction(vcpu);
  958. }
  959. return 1;
  960. }
  961. static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
  962. {
  963. switch (ecx) {
  964. case MSR_IA32_TIME_STAMP_COUNTER: {
  965. u64 tsc;
  966. rdtscll(tsc);
  967. vcpu->svm->vmcb->control.tsc_offset = data - tsc;
  968. break;
  969. }
  970. case MSR_K6_STAR:
  971. vcpu->svm->vmcb->save.star = data;
  972. break;
  973. #ifdef CONFIG_X86_64
  974. case MSR_LSTAR:
  975. vcpu->svm->vmcb->save.lstar = data;
  976. break;
  977. case MSR_CSTAR:
  978. vcpu->svm->vmcb->save.cstar = data;
  979. break;
  980. case MSR_KERNEL_GS_BASE:
  981. vcpu->svm->vmcb->save.kernel_gs_base = data;
  982. break;
  983. case MSR_SYSCALL_MASK:
  984. vcpu->svm->vmcb->save.sfmask = data;
  985. break;
  986. #endif
  987. case MSR_IA32_SYSENTER_CS:
  988. vcpu->svm->vmcb->save.sysenter_cs = data;
  989. break;
  990. case MSR_IA32_SYSENTER_EIP:
  991. vcpu->svm->vmcb->save.sysenter_eip = data;
  992. break;
  993. case MSR_IA32_SYSENTER_ESP:
  994. vcpu->svm->vmcb->save.sysenter_esp = data;
  995. break;
  996. default:
  997. return kvm_set_msr_common(vcpu, ecx, data);
  998. }
  999. return 0;
  1000. }
  1001. static int wrmsr_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  1002. {
  1003. u32 ecx = vcpu->regs[VCPU_REGS_RCX];
  1004. u64 data = (vcpu->svm->vmcb->save.rax & -1u)
  1005. | ((u64)(vcpu->regs[VCPU_REGS_RDX] & -1u) << 32);
  1006. vcpu->svm->next_rip = vcpu->svm->vmcb->save.rip + 2;
  1007. if (svm_set_msr(vcpu, ecx, data))
  1008. svm_inject_gp(vcpu, 0);
  1009. else
  1010. skip_emulated_instruction(vcpu);
  1011. return 1;
  1012. }
  1013. static int msr_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  1014. {
  1015. if (vcpu->svm->vmcb->control.exit_info_1)
  1016. return wrmsr_interception(vcpu, kvm_run);
  1017. else
  1018. return rdmsr_interception(vcpu, kvm_run);
  1019. }
  1020. static int interrupt_window_interception(struct kvm_vcpu *vcpu,
  1021. struct kvm_run *kvm_run)
  1022. {
  1023. /*
  1024. * If the user space waits to inject interrupts, exit as soon as
  1025. * possible
  1026. */
  1027. if (kvm_run->request_interrupt_window &&
  1028. !vcpu->irq_summary) {
  1029. ++kvm_stat.irq_window_exits;
  1030. kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  1031. return 0;
  1032. }
  1033. return 1;
  1034. }
  1035. static int (*svm_exit_handlers[])(struct kvm_vcpu *vcpu,
  1036. struct kvm_run *kvm_run) = {
  1037. [SVM_EXIT_READ_CR0] = emulate_on_interception,
  1038. [SVM_EXIT_READ_CR3] = emulate_on_interception,
  1039. [SVM_EXIT_READ_CR4] = emulate_on_interception,
  1040. /* for now: */
  1041. [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
  1042. [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
  1043. [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
  1044. [SVM_EXIT_READ_DR0] = emulate_on_interception,
  1045. [SVM_EXIT_READ_DR1] = emulate_on_interception,
  1046. [SVM_EXIT_READ_DR2] = emulate_on_interception,
  1047. [SVM_EXIT_READ_DR3] = emulate_on_interception,
  1048. [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
  1049. [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
  1050. [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
  1051. [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
  1052. [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
  1053. [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
  1054. [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
  1055. [SVM_EXIT_INTR] = nop_on_interception,
  1056. [SVM_EXIT_NMI] = nop_on_interception,
  1057. [SVM_EXIT_SMI] = nop_on_interception,
  1058. [SVM_EXIT_INIT] = nop_on_interception,
  1059. [SVM_EXIT_VINTR] = interrupt_window_interception,
  1060. /* [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, */
  1061. [SVM_EXIT_CPUID] = cpuid_interception,
  1062. [SVM_EXIT_HLT] = halt_interception,
  1063. [SVM_EXIT_INVLPG] = emulate_on_interception,
  1064. [SVM_EXIT_INVLPGA] = invalid_op_interception,
  1065. [SVM_EXIT_IOIO] = io_interception,
  1066. [SVM_EXIT_MSR] = msr_interception,
  1067. [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
  1068. [SVM_EXIT_SHUTDOWN] = shutdown_interception,
  1069. [SVM_EXIT_VMRUN] = invalid_op_interception,
  1070. [SVM_EXIT_VMMCALL] = invalid_op_interception,
  1071. [SVM_EXIT_VMLOAD] = invalid_op_interception,
  1072. [SVM_EXIT_VMSAVE] = invalid_op_interception,
  1073. [SVM_EXIT_STGI] = invalid_op_interception,
  1074. [SVM_EXIT_CLGI] = invalid_op_interception,
  1075. [SVM_EXIT_SKINIT] = invalid_op_interception,
  1076. };
  1077. static int handle_exit(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  1078. {
  1079. u32 exit_code = vcpu->svm->vmcb->control.exit_code;
  1080. kvm_run->exit_type = KVM_EXIT_TYPE_VM_EXIT;
  1081. if (is_external_interrupt(vcpu->svm->vmcb->control.exit_int_info) &&
  1082. exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR)
  1083. printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
  1084. "exit_code 0x%x\n",
  1085. __FUNCTION__, vcpu->svm->vmcb->control.exit_int_info,
  1086. exit_code);
  1087. if (exit_code >= sizeof(svm_exit_handlers) / sizeof(*svm_exit_handlers)
  1088. || svm_exit_handlers[exit_code] == 0) {
  1089. kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
  1090. printk(KERN_ERR "%s: 0x%x @ 0x%llx cr0 0x%lx rflags 0x%llx\n",
  1091. __FUNCTION__,
  1092. exit_code,
  1093. vcpu->svm->vmcb->save.rip,
  1094. vcpu->cr0,
  1095. vcpu->svm->vmcb->save.rflags);
  1096. return 0;
  1097. }
  1098. return svm_exit_handlers[exit_code](vcpu, kvm_run);
  1099. }
  1100. static void reload_tss(struct kvm_vcpu *vcpu)
  1101. {
  1102. int cpu = raw_smp_processor_id();
  1103. struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
  1104. svm_data->tss_desc->type = 9; //available 32/64-bit TSS
  1105. load_TR_desc();
  1106. }
  1107. static void pre_svm_run(struct kvm_vcpu *vcpu)
  1108. {
  1109. int cpu = raw_smp_processor_id();
  1110. struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
  1111. vcpu->svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
  1112. if (vcpu->cpu != cpu ||
  1113. vcpu->svm->asid_generation != svm_data->asid_generation)
  1114. new_asid(vcpu, svm_data);
  1115. }
  1116. static inline void kvm_do_inject_irq(struct kvm_vcpu *vcpu)
  1117. {
  1118. struct vmcb_control_area *control;
  1119. control = &vcpu->svm->vmcb->control;
  1120. control->int_vector = pop_irq(vcpu);
  1121. control->int_ctl &= ~V_INTR_PRIO_MASK;
  1122. control->int_ctl |= V_IRQ_MASK |
  1123. ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
  1124. }
  1125. static void kvm_reput_irq(struct kvm_vcpu *vcpu)
  1126. {
  1127. struct vmcb_control_area *control = &vcpu->svm->vmcb->control;
  1128. if (control->int_ctl & V_IRQ_MASK) {
  1129. control->int_ctl &= ~V_IRQ_MASK;
  1130. push_irq(vcpu, control->int_vector);
  1131. }
  1132. vcpu->interrupt_window_open =
  1133. !(control->int_state & SVM_INTERRUPT_SHADOW_MASK);
  1134. }
  1135. static void do_interrupt_requests(struct kvm_vcpu *vcpu,
  1136. struct kvm_run *kvm_run)
  1137. {
  1138. struct vmcb_control_area *control = &vcpu->svm->vmcb->control;
  1139. vcpu->interrupt_window_open =
  1140. (!(control->int_state & SVM_INTERRUPT_SHADOW_MASK) &&
  1141. (vcpu->svm->vmcb->save.rflags & X86_EFLAGS_IF));
  1142. if (vcpu->interrupt_window_open && vcpu->irq_summary)
  1143. /*
  1144. * If interrupts enabled, and not blocked by sti or mov ss. Good.
  1145. */
  1146. kvm_do_inject_irq(vcpu);
  1147. /*
  1148. * Interrupts blocked. Wait for unblock.
  1149. */
  1150. if (!vcpu->interrupt_window_open &&
  1151. (vcpu->irq_summary || kvm_run->request_interrupt_window)) {
  1152. control->intercept |= 1ULL << INTERCEPT_VINTR;
  1153. } else
  1154. control->intercept &= ~(1ULL << INTERCEPT_VINTR);
  1155. }
  1156. static void post_kvm_run_save(struct kvm_vcpu *vcpu,
  1157. struct kvm_run *kvm_run)
  1158. {
  1159. kvm_run->ready_for_interrupt_injection = (vcpu->interrupt_window_open &&
  1160. vcpu->irq_summary == 0);
  1161. kvm_run->if_flag = (vcpu->svm->vmcb->save.rflags & X86_EFLAGS_IF) != 0;
  1162. kvm_run->cr8 = vcpu->cr8;
  1163. kvm_run->apic_base = vcpu->apic_base;
  1164. }
  1165. /*
  1166. * Check if userspace requested an interrupt window, and that the
  1167. * interrupt window is open.
  1168. *
  1169. * No need to exit to userspace if we already have an interrupt queued.
  1170. */
  1171. static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
  1172. struct kvm_run *kvm_run)
  1173. {
  1174. return (!vcpu->irq_summary &&
  1175. kvm_run->request_interrupt_window &&
  1176. vcpu->interrupt_window_open &&
  1177. (vcpu->svm->vmcb->save.rflags & X86_EFLAGS_IF));
  1178. }
  1179. static void save_db_regs(unsigned long *db_regs)
  1180. {
  1181. asm volatile ("mov %%dr0, %0" : "=r"(db_regs[0]));
  1182. asm volatile ("mov %%dr1, %0" : "=r"(db_regs[1]));
  1183. asm volatile ("mov %%dr2, %0" : "=r"(db_regs[2]));
  1184. asm volatile ("mov %%dr3, %0" : "=r"(db_regs[3]));
  1185. }
  1186. static void load_db_regs(unsigned long *db_regs)
  1187. {
  1188. asm volatile ("mov %0, %%dr0" : : "r"(db_regs[0]));
  1189. asm volatile ("mov %0, %%dr1" : : "r"(db_regs[1]));
  1190. asm volatile ("mov %0, %%dr2" : : "r"(db_regs[2]));
  1191. asm volatile ("mov %0, %%dr3" : : "r"(db_regs[3]));
  1192. }
  1193. static int svm_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  1194. {
  1195. u16 fs_selector;
  1196. u16 gs_selector;
  1197. u16 ldt_selector;
  1198. int r;
  1199. again:
  1200. if (!vcpu->mmio_read_completed)
  1201. do_interrupt_requests(vcpu, kvm_run);
  1202. clgi();
  1203. pre_svm_run(vcpu);
  1204. save_host_msrs(vcpu);
  1205. fs_selector = read_fs();
  1206. gs_selector = read_gs();
  1207. ldt_selector = read_ldt();
  1208. vcpu->svm->host_cr2 = kvm_read_cr2();
  1209. vcpu->svm->host_dr6 = read_dr6();
  1210. vcpu->svm->host_dr7 = read_dr7();
  1211. vcpu->svm->vmcb->save.cr2 = vcpu->cr2;
  1212. if (vcpu->svm->vmcb->save.dr7 & 0xff) {
  1213. write_dr7(0);
  1214. save_db_regs(vcpu->svm->host_db_regs);
  1215. load_db_regs(vcpu->svm->db_regs);
  1216. }
  1217. fx_save(vcpu->host_fx_image);
  1218. fx_restore(vcpu->guest_fx_image);
  1219. asm volatile (
  1220. #ifdef CONFIG_X86_64
  1221. "push %%rbx; push %%rcx; push %%rdx;"
  1222. "push %%rsi; push %%rdi; push %%rbp;"
  1223. "push %%r8; push %%r9; push %%r10; push %%r11;"
  1224. "push %%r12; push %%r13; push %%r14; push %%r15;"
  1225. #else
  1226. "push %%ebx; push %%ecx; push %%edx;"
  1227. "push %%esi; push %%edi; push %%ebp;"
  1228. #endif
  1229. #ifdef CONFIG_X86_64
  1230. "mov %c[rbx](%[vcpu]), %%rbx \n\t"
  1231. "mov %c[rcx](%[vcpu]), %%rcx \n\t"
  1232. "mov %c[rdx](%[vcpu]), %%rdx \n\t"
  1233. "mov %c[rsi](%[vcpu]), %%rsi \n\t"
  1234. "mov %c[rdi](%[vcpu]), %%rdi \n\t"
  1235. "mov %c[rbp](%[vcpu]), %%rbp \n\t"
  1236. "mov %c[r8](%[vcpu]), %%r8 \n\t"
  1237. "mov %c[r9](%[vcpu]), %%r9 \n\t"
  1238. "mov %c[r10](%[vcpu]), %%r10 \n\t"
  1239. "mov %c[r11](%[vcpu]), %%r11 \n\t"
  1240. "mov %c[r12](%[vcpu]), %%r12 \n\t"
  1241. "mov %c[r13](%[vcpu]), %%r13 \n\t"
  1242. "mov %c[r14](%[vcpu]), %%r14 \n\t"
  1243. "mov %c[r15](%[vcpu]), %%r15 \n\t"
  1244. #else
  1245. "mov %c[rbx](%[vcpu]), %%ebx \n\t"
  1246. "mov %c[rcx](%[vcpu]), %%ecx \n\t"
  1247. "mov %c[rdx](%[vcpu]), %%edx \n\t"
  1248. "mov %c[rsi](%[vcpu]), %%esi \n\t"
  1249. "mov %c[rdi](%[vcpu]), %%edi \n\t"
  1250. "mov %c[rbp](%[vcpu]), %%ebp \n\t"
  1251. #endif
  1252. #ifdef CONFIG_X86_64
  1253. /* Enter guest mode */
  1254. "push %%rax \n\t"
  1255. "mov %c[svm](%[vcpu]), %%rax \n\t"
  1256. "mov %c[vmcb](%%rax), %%rax \n\t"
  1257. SVM_VMLOAD "\n\t"
  1258. SVM_VMRUN "\n\t"
  1259. SVM_VMSAVE "\n\t"
  1260. "pop %%rax \n\t"
  1261. #else
  1262. /* Enter guest mode */
  1263. "push %%eax \n\t"
  1264. "mov %c[svm](%[vcpu]), %%eax \n\t"
  1265. "mov %c[vmcb](%%eax), %%eax \n\t"
  1266. SVM_VMLOAD "\n\t"
  1267. SVM_VMRUN "\n\t"
  1268. SVM_VMSAVE "\n\t"
  1269. "pop %%eax \n\t"
  1270. #endif
  1271. /* Save guest registers, load host registers */
  1272. #ifdef CONFIG_X86_64
  1273. "mov %%rbx, %c[rbx](%[vcpu]) \n\t"
  1274. "mov %%rcx, %c[rcx](%[vcpu]) \n\t"
  1275. "mov %%rdx, %c[rdx](%[vcpu]) \n\t"
  1276. "mov %%rsi, %c[rsi](%[vcpu]) \n\t"
  1277. "mov %%rdi, %c[rdi](%[vcpu]) \n\t"
  1278. "mov %%rbp, %c[rbp](%[vcpu]) \n\t"
  1279. "mov %%r8, %c[r8](%[vcpu]) \n\t"
  1280. "mov %%r9, %c[r9](%[vcpu]) \n\t"
  1281. "mov %%r10, %c[r10](%[vcpu]) \n\t"
  1282. "mov %%r11, %c[r11](%[vcpu]) \n\t"
  1283. "mov %%r12, %c[r12](%[vcpu]) \n\t"
  1284. "mov %%r13, %c[r13](%[vcpu]) \n\t"
  1285. "mov %%r14, %c[r14](%[vcpu]) \n\t"
  1286. "mov %%r15, %c[r15](%[vcpu]) \n\t"
  1287. "pop %%r15; pop %%r14; pop %%r13; pop %%r12;"
  1288. "pop %%r11; pop %%r10; pop %%r9; pop %%r8;"
  1289. "pop %%rbp; pop %%rdi; pop %%rsi;"
  1290. "pop %%rdx; pop %%rcx; pop %%rbx; \n\t"
  1291. #else
  1292. "mov %%ebx, %c[rbx](%[vcpu]) \n\t"
  1293. "mov %%ecx, %c[rcx](%[vcpu]) \n\t"
  1294. "mov %%edx, %c[rdx](%[vcpu]) \n\t"
  1295. "mov %%esi, %c[rsi](%[vcpu]) \n\t"
  1296. "mov %%edi, %c[rdi](%[vcpu]) \n\t"
  1297. "mov %%ebp, %c[rbp](%[vcpu]) \n\t"
  1298. "pop %%ebp; pop %%edi; pop %%esi;"
  1299. "pop %%edx; pop %%ecx; pop %%ebx; \n\t"
  1300. #endif
  1301. :
  1302. : [vcpu]"a"(vcpu),
  1303. [svm]"i"(offsetof(struct kvm_vcpu, svm)),
  1304. [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
  1305. [rbx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RBX])),
  1306. [rcx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RCX])),
  1307. [rdx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RDX])),
  1308. [rsi]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RSI])),
  1309. [rdi]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RDI])),
  1310. [rbp]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RBP]))
  1311. #ifdef CONFIG_X86_64
  1312. ,[r8 ]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R8 ])),
  1313. [r9 ]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R9 ])),
  1314. [r10]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R10])),
  1315. [r11]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R11])),
  1316. [r12]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R12])),
  1317. [r13]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R13])),
  1318. [r14]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R14])),
  1319. [r15]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R15]))
  1320. #endif
  1321. : "cc", "memory" );
  1322. fx_save(vcpu->guest_fx_image);
  1323. fx_restore(vcpu->host_fx_image);
  1324. if ((vcpu->svm->vmcb->save.dr7 & 0xff))
  1325. load_db_regs(vcpu->svm->host_db_regs);
  1326. vcpu->cr2 = vcpu->svm->vmcb->save.cr2;
  1327. write_dr6(vcpu->svm->host_dr6);
  1328. write_dr7(vcpu->svm->host_dr7);
  1329. kvm_write_cr2(vcpu->svm->host_cr2);
  1330. load_fs(fs_selector);
  1331. load_gs(gs_selector);
  1332. load_ldt(ldt_selector);
  1333. load_host_msrs(vcpu);
  1334. reload_tss(vcpu);
  1335. /*
  1336. * Profile KVM exit RIPs:
  1337. */
  1338. if (unlikely(prof_on == KVM_PROFILING))
  1339. profile_hit(KVM_PROFILING,
  1340. (void *)(unsigned long)vcpu->svm->vmcb->save.rip);
  1341. stgi();
  1342. kvm_reput_irq(vcpu);
  1343. vcpu->svm->next_rip = 0;
  1344. if (vcpu->svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
  1345. kvm_run->exit_type = KVM_EXIT_TYPE_FAIL_ENTRY;
  1346. kvm_run->exit_reason = vcpu->svm->vmcb->control.exit_code;
  1347. post_kvm_run_save(vcpu, kvm_run);
  1348. return 0;
  1349. }
  1350. r = handle_exit(vcpu, kvm_run);
  1351. if (r > 0) {
  1352. if (signal_pending(current)) {
  1353. ++kvm_stat.signal_exits;
  1354. post_kvm_run_save(vcpu, kvm_run);
  1355. return -EINTR;
  1356. }
  1357. if (dm_request_for_irq_injection(vcpu, kvm_run)) {
  1358. ++kvm_stat.request_irq_exits;
  1359. post_kvm_run_save(vcpu, kvm_run);
  1360. return -EINTR;
  1361. }
  1362. kvm_resched(vcpu);
  1363. goto again;
  1364. }
  1365. post_kvm_run_save(vcpu, kvm_run);
  1366. return r;
  1367. }
  1368. static void svm_flush_tlb(struct kvm_vcpu *vcpu)
  1369. {
  1370. force_new_asid(vcpu);
  1371. }
  1372. static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
  1373. {
  1374. vcpu->svm->vmcb->save.cr3 = root;
  1375. force_new_asid(vcpu);
  1376. }
  1377. static void svm_inject_page_fault(struct kvm_vcpu *vcpu,
  1378. unsigned long addr,
  1379. uint32_t err_code)
  1380. {
  1381. uint32_t exit_int_info = vcpu->svm->vmcb->control.exit_int_info;
  1382. ++kvm_stat.pf_guest;
  1383. if (is_page_fault(exit_int_info)) {
  1384. vcpu->svm->vmcb->control.event_inj_err = 0;
  1385. vcpu->svm->vmcb->control.event_inj = SVM_EVTINJ_VALID |
  1386. SVM_EVTINJ_VALID_ERR |
  1387. SVM_EVTINJ_TYPE_EXEPT |
  1388. DF_VECTOR;
  1389. return;
  1390. }
  1391. vcpu->cr2 = addr;
  1392. vcpu->svm->vmcb->save.cr2 = addr;
  1393. vcpu->svm->vmcb->control.event_inj = SVM_EVTINJ_VALID |
  1394. SVM_EVTINJ_VALID_ERR |
  1395. SVM_EVTINJ_TYPE_EXEPT |
  1396. PF_VECTOR;
  1397. vcpu->svm->vmcb->control.event_inj_err = err_code;
  1398. }
  1399. static int is_disabled(void)
  1400. {
  1401. return 0;
  1402. }
  1403. static struct kvm_arch_ops svm_arch_ops = {
  1404. .cpu_has_kvm_support = has_svm,
  1405. .disabled_by_bios = is_disabled,
  1406. .hardware_setup = svm_hardware_setup,
  1407. .hardware_unsetup = svm_hardware_unsetup,
  1408. .hardware_enable = svm_hardware_enable,
  1409. .hardware_disable = svm_hardware_disable,
  1410. .vcpu_create = svm_create_vcpu,
  1411. .vcpu_free = svm_free_vcpu,
  1412. .vcpu_load = svm_vcpu_load,
  1413. .vcpu_put = svm_vcpu_put,
  1414. .set_guest_debug = svm_guest_debug,
  1415. .get_msr = svm_get_msr,
  1416. .set_msr = svm_set_msr,
  1417. .get_segment_base = svm_get_segment_base,
  1418. .get_segment = svm_get_segment,
  1419. .set_segment = svm_set_segment,
  1420. .get_cs_db_l_bits = svm_get_cs_db_l_bits,
  1421. .decache_cr0_cr4_guest_bits = svm_decache_cr0_cr4_guest_bits,
  1422. .set_cr0 = svm_set_cr0,
  1423. .set_cr0_no_modeswitch = svm_set_cr0,
  1424. .set_cr3 = svm_set_cr3,
  1425. .set_cr4 = svm_set_cr4,
  1426. .set_efer = svm_set_efer,
  1427. .get_idt = svm_get_idt,
  1428. .set_idt = svm_set_idt,
  1429. .get_gdt = svm_get_gdt,
  1430. .set_gdt = svm_set_gdt,
  1431. .get_dr = svm_get_dr,
  1432. .set_dr = svm_set_dr,
  1433. .cache_regs = svm_cache_regs,
  1434. .decache_regs = svm_decache_regs,
  1435. .get_rflags = svm_get_rflags,
  1436. .set_rflags = svm_set_rflags,
  1437. .invlpg = svm_invlpg,
  1438. .tlb_flush = svm_flush_tlb,
  1439. .inject_page_fault = svm_inject_page_fault,
  1440. .inject_gp = svm_inject_gp,
  1441. .run = svm_vcpu_run,
  1442. .skip_emulated_instruction = skip_emulated_instruction,
  1443. .vcpu_setup = svm_vcpu_setup,
  1444. };
  1445. static int __init svm_init(void)
  1446. {
  1447. return kvm_init_arch(&svm_arch_ops, THIS_MODULE);
  1448. }
  1449. static void __exit svm_exit(void)
  1450. {
  1451. kvm_exit_arch();
  1452. }
  1453. module_init(svm_init)
  1454. module_exit(svm_exit)