svm.c 43 KB

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