svm.c 43 KB

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