svm.c 45 KB

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