nested.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Kernel-based Virtual Machine driver for Linux
  4. *
  5. * AMD SVM support
  6. *
  7. * Copyright (C) 2006 Qumranet, Inc.
  8. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  9. *
  10. * Authors:
  11. * Yaniv Kamay <[email protected]>
  12. * Avi Kivity <[email protected]>
  13. */
  14. #define pr_fmt(fmt) "SVM: " fmt
  15. #include <linux/kvm_types.h>
  16. #include <linux/kvm_host.h>
  17. #include <linux/kernel.h>
  18. #include <asm/msr-index.h>
  19. #include <asm/debugreg.h>
  20. #include "kvm_emulate.h"
  21. #include "trace.h"
  22. #include "mmu.h"
  23. #include "x86.h"
  24. #include "cpuid.h"
  25. #include "lapic.h"
  26. #include "svm.h"
  27. #include "hyperv.h"
  28. #define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK
  29. static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
  30. struct x86_exception *fault)
  31. {
  32. struct vcpu_svm *svm = to_svm(vcpu);
  33. struct vmcb *vmcb = svm->vmcb;
  34. if (vmcb->control.exit_code != SVM_EXIT_NPF) {
  35. /*
  36. * TODO: track the cause of the nested page fault, and
  37. * correctly fill in the high bits of exit_info_1.
  38. */
  39. vmcb->control.exit_code = SVM_EXIT_NPF;
  40. vmcb->control.exit_code_hi = 0;
  41. vmcb->control.exit_info_1 = (1ULL << 32);
  42. vmcb->control.exit_info_2 = fault->address;
  43. }
  44. vmcb->control.exit_info_1 &= ~0xffffffffULL;
  45. vmcb->control.exit_info_1 |= fault->error_code;
  46. nested_svm_vmexit(svm);
  47. }
  48. static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
  49. {
  50. struct vcpu_svm *svm = to_svm(vcpu);
  51. u64 cr3 = svm->nested.ctl.nested_cr3;
  52. u64 pdpte;
  53. int ret;
  54. ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(cr3), &pdpte,
  55. offset_in_page(cr3) + index * 8, 8);
  56. if (ret)
  57. return 0;
  58. return pdpte;
  59. }
  60. static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
  61. {
  62. struct vcpu_svm *svm = to_svm(vcpu);
  63. return svm->nested.ctl.nested_cr3;
  64. }
  65. static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
  66. {
  67. struct vcpu_svm *svm = to_svm(vcpu);
  68. WARN_ON(mmu_is_nested(vcpu));
  69. vcpu->arch.mmu = &vcpu->arch.guest_mmu;
  70. /*
  71. * The NPT format depends on L1's CR4 and EFER, which is in vmcb01. Note,
  72. * when called via KVM_SET_NESTED_STATE, that state may _not_ match current
  73. * vCPU state. CR0.WP is explicitly ignored, while CR0.PG is required.
  74. */
  75. kvm_init_shadow_npt_mmu(vcpu, X86_CR0_PG, svm->vmcb01.ptr->save.cr4,
  76. svm->vmcb01.ptr->save.efer,
  77. svm->nested.ctl.nested_cr3);
  78. vcpu->arch.mmu->get_guest_pgd = nested_svm_get_tdp_cr3;
  79. vcpu->arch.mmu->get_pdptr = nested_svm_get_tdp_pdptr;
  80. vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit;
  81. vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
  82. }
  83. static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
  84. {
  85. vcpu->arch.mmu = &vcpu->arch.root_mmu;
  86. vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
  87. }
  88. static bool nested_vmcb_needs_vls_intercept(struct vcpu_svm *svm)
  89. {
  90. if (!svm->v_vmload_vmsave_enabled)
  91. return true;
  92. if (!nested_npt_enabled(svm))
  93. return true;
  94. if (!(svm->nested.ctl.virt_ext & VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK))
  95. return true;
  96. return false;
  97. }
  98. void recalc_intercepts(struct vcpu_svm *svm)
  99. {
  100. struct vmcb_control_area *c, *h;
  101. struct vmcb_ctrl_area_cached *g;
  102. unsigned int i;
  103. vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
  104. if (!is_guest_mode(&svm->vcpu))
  105. return;
  106. c = &svm->vmcb->control;
  107. h = &svm->vmcb01.ptr->control;
  108. g = &svm->nested.ctl;
  109. for (i = 0; i < MAX_INTERCEPT; i++)
  110. c->intercepts[i] = h->intercepts[i];
  111. if (g->int_ctl & V_INTR_MASKING_MASK) {
  112. /* We only want the cr8 intercept bits of L1 */
  113. vmcb_clr_intercept(c, INTERCEPT_CR8_READ);
  114. vmcb_clr_intercept(c, INTERCEPT_CR8_WRITE);
  115. /*
  116. * Once running L2 with HF_VINTR_MASK, EFLAGS.IF does not
  117. * affect any interrupt we may want to inject; therefore,
  118. * interrupt window vmexits are irrelevant to L0.
  119. */
  120. vmcb_clr_intercept(c, INTERCEPT_VINTR);
  121. }
  122. /* We don't want to see VMMCALLs from a nested guest */
  123. vmcb_clr_intercept(c, INTERCEPT_VMMCALL);
  124. for (i = 0; i < MAX_INTERCEPT; i++)
  125. c->intercepts[i] |= g->intercepts[i];
  126. /* If SMI is not intercepted, ignore guest SMI intercept as well */
  127. if (!intercept_smi)
  128. vmcb_clr_intercept(c, INTERCEPT_SMI);
  129. if (nested_vmcb_needs_vls_intercept(svm)) {
  130. /*
  131. * If the virtual VMLOAD/VMSAVE is not enabled for the L2,
  132. * we must intercept these instructions to correctly
  133. * emulate them in case L1 doesn't intercept them.
  134. */
  135. vmcb_set_intercept(c, INTERCEPT_VMLOAD);
  136. vmcb_set_intercept(c, INTERCEPT_VMSAVE);
  137. } else {
  138. WARN_ON(!(c->virt_ext & VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK));
  139. }
  140. }
  141. /*
  142. * Merge L0's (KVM) and L1's (Nested VMCB) MSR permission bitmaps. The function
  143. * is optimized in that it only merges the parts where KVM MSR permission bitmap
  144. * may contain zero bits.
  145. */
  146. static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
  147. {
  148. struct hv_vmcb_enlightenments *hve = &svm->nested.ctl.hv_enlightenments;
  149. int i;
  150. /*
  151. * MSR bitmap update can be skipped when:
  152. * - MSR bitmap for L1 hasn't changed.
  153. * - Nested hypervisor (L1) is attempting to launch the same L2 as
  154. * before.
  155. * - Nested hypervisor (L1) is using Hyper-V emulation interface and
  156. * tells KVM (L0) there were no changes in MSR bitmap for L2.
  157. */
  158. if (!svm->nested.force_msr_bitmap_recalc &&
  159. kvm_hv_hypercall_enabled(&svm->vcpu) &&
  160. hve->hv_enlightenments_control.msr_bitmap &&
  161. (svm->nested.ctl.clean & BIT(HV_VMCB_NESTED_ENLIGHTENMENTS)))
  162. goto set_msrpm_base_pa;
  163. if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
  164. return true;
  165. for (i = 0; i < MSRPM_OFFSETS; i++) {
  166. u32 value, p;
  167. u64 offset;
  168. if (msrpm_offsets[i] == 0xffffffff)
  169. break;
  170. p = msrpm_offsets[i];
  171. /* x2apic msrs are intercepted always for the nested guest */
  172. if (is_x2apic_msrpm_offset(p))
  173. continue;
  174. offset = svm->nested.ctl.msrpm_base_pa + (p * 4);
  175. if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
  176. return false;
  177. svm->nested.msrpm[p] = svm->msrpm[p] | value;
  178. }
  179. svm->nested.force_msr_bitmap_recalc = false;
  180. set_msrpm_base_pa:
  181. svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
  182. return true;
  183. }
  184. /*
  185. * Bits 11:0 of bitmap address are ignored by hardware
  186. */
  187. static bool nested_svm_check_bitmap_pa(struct kvm_vcpu *vcpu, u64 pa, u32 size)
  188. {
  189. u64 addr = PAGE_ALIGN(pa);
  190. return kvm_vcpu_is_legal_gpa(vcpu, addr) &&
  191. kvm_vcpu_is_legal_gpa(vcpu, addr + size - 1);
  192. }
  193. static bool nested_svm_check_tlb_ctl(struct kvm_vcpu *vcpu, u8 tlb_ctl)
  194. {
  195. /* Nested FLUSHBYASID is not supported yet. */
  196. switch(tlb_ctl) {
  197. case TLB_CONTROL_DO_NOTHING:
  198. case TLB_CONTROL_FLUSH_ALL_ASID:
  199. return true;
  200. default:
  201. return false;
  202. }
  203. }
  204. static bool __nested_vmcb_check_controls(struct kvm_vcpu *vcpu,
  205. struct vmcb_ctrl_area_cached *control)
  206. {
  207. if (CC(!vmcb12_is_intercept(control, INTERCEPT_VMRUN)))
  208. return false;
  209. if (CC(control->asid == 0))
  210. return false;
  211. if (CC((control->nested_ctl & SVM_NESTED_CTL_NP_ENABLE) && !npt_enabled))
  212. return false;
  213. if (CC(!nested_svm_check_bitmap_pa(vcpu, control->msrpm_base_pa,
  214. MSRPM_SIZE)))
  215. return false;
  216. if (CC(!nested_svm_check_bitmap_pa(vcpu, control->iopm_base_pa,
  217. IOPM_SIZE)))
  218. return false;
  219. if (CC(!nested_svm_check_tlb_ctl(vcpu, control->tlb_ctl)))
  220. return false;
  221. return true;
  222. }
  223. /* Common checks that apply to both L1 and L2 state. */
  224. static bool __nested_vmcb_check_save(struct kvm_vcpu *vcpu,
  225. struct vmcb_save_area_cached *save)
  226. {
  227. if (CC(!(save->efer & EFER_SVME)))
  228. return false;
  229. if (CC((save->cr0 & X86_CR0_CD) == 0 && (save->cr0 & X86_CR0_NW)) ||
  230. CC(save->cr0 & ~0xffffffffULL))
  231. return false;
  232. if (CC(!kvm_dr6_valid(save->dr6)) || CC(!kvm_dr7_valid(save->dr7)))
  233. return false;
  234. /*
  235. * These checks are also performed by KVM_SET_SREGS,
  236. * except that EFER.LMA is not checked by SVM against
  237. * CR0.PG && EFER.LME.
  238. */
  239. if ((save->efer & EFER_LME) && (save->cr0 & X86_CR0_PG)) {
  240. if (CC(!(save->cr4 & X86_CR4_PAE)) ||
  241. CC(!(save->cr0 & X86_CR0_PE)) ||
  242. CC(kvm_vcpu_is_illegal_gpa(vcpu, save->cr3)))
  243. return false;
  244. }
  245. /* Note, SVM doesn't have any additional restrictions on CR4. */
  246. if (CC(!__kvm_is_valid_cr4(vcpu, save->cr4)))
  247. return false;
  248. if (CC(!kvm_valid_efer(vcpu, save->efer)))
  249. return false;
  250. return true;
  251. }
  252. static bool nested_vmcb_check_save(struct kvm_vcpu *vcpu)
  253. {
  254. struct vcpu_svm *svm = to_svm(vcpu);
  255. struct vmcb_save_area_cached *save = &svm->nested.save;
  256. return __nested_vmcb_check_save(vcpu, save);
  257. }
  258. static bool nested_vmcb_check_controls(struct kvm_vcpu *vcpu)
  259. {
  260. struct vcpu_svm *svm = to_svm(vcpu);
  261. struct vmcb_ctrl_area_cached *ctl = &svm->nested.ctl;
  262. return __nested_vmcb_check_controls(vcpu, ctl);
  263. }
  264. static
  265. void __nested_copy_vmcb_control_to_cache(struct kvm_vcpu *vcpu,
  266. struct vmcb_ctrl_area_cached *to,
  267. struct vmcb_control_area *from)
  268. {
  269. unsigned int i;
  270. for (i = 0; i < MAX_INTERCEPT; i++)
  271. to->intercepts[i] = from->intercepts[i];
  272. to->iopm_base_pa = from->iopm_base_pa;
  273. to->msrpm_base_pa = from->msrpm_base_pa;
  274. to->tsc_offset = from->tsc_offset;
  275. to->tlb_ctl = from->tlb_ctl;
  276. to->int_ctl = from->int_ctl;
  277. to->int_vector = from->int_vector;
  278. to->int_state = from->int_state;
  279. to->exit_code = from->exit_code;
  280. to->exit_code_hi = from->exit_code_hi;
  281. to->exit_info_1 = from->exit_info_1;
  282. to->exit_info_2 = from->exit_info_2;
  283. to->exit_int_info = from->exit_int_info;
  284. to->exit_int_info_err = from->exit_int_info_err;
  285. to->nested_ctl = from->nested_ctl;
  286. to->event_inj = from->event_inj;
  287. to->event_inj_err = from->event_inj_err;
  288. to->next_rip = from->next_rip;
  289. to->nested_cr3 = from->nested_cr3;
  290. to->virt_ext = from->virt_ext;
  291. to->pause_filter_count = from->pause_filter_count;
  292. to->pause_filter_thresh = from->pause_filter_thresh;
  293. /* Copy asid here because nested_vmcb_check_controls will check it. */
  294. to->asid = from->asid;
  295. to->msrpm_base_pa &= ~0x0fffULL;
  296. to->iopm_base_pa &= ~0x0fffULL;
  297. /* Hyper-V extensions (Enlightened VMCB) */
  298. if (kvm_hv_hypercall_enabled(vcpu)) {
  299. to->clean = from->clean;
  300. memcpy(&to->hv_enlightenments, &from->hv_enlightenments,
  301. sizeof(to->hv_enlightenments));
  302. }
  303. }
  304. void nested_copy_vmcb_control_to_cache(struct vcpu_svm *svm,
  305. struct vmcb_control_area *control)
  306. {
  307. __nested_copy_vmcb_control_to_cache(&svm->vcpu, &svm->nested.ctl, control);
  308. }
  309. static void __nested_copy_vmcb_save_to_cache(struct vmcb_save_area_cached *to,
  310. struct vmcb_save_area *from)
  311. {
  312. /*
  313. * Copy only fields that are validated, as we need them
  314. * to avoid TOC/TOU races.
  315. */
  316. to->efer = from->efer;
  317. to->cr0 = from->cr0;
  318. to->cr3 = from->cr3;
  319. to->cr4 = from->cr4;
  320. to->dr6 = from->dr6;
  321. to->dr7 = from->dr7;
  322. }
  323. void nested_copy_vmcb_save_to_cache(struct vcpu_svm *svm,
  324. struct vmcb_save_area *save)
  325. {
  326. __nested_copy_vmcb_save_to_cache(&svm->nested.save, save);
  327. }
  328. /*
  329. * Synchronize fields that are written by the processor, so that
  330. * they can be copied back into the vmcb12.
  331. */
  332. void nested_sync_control_from_vmcb02(struct vcpu_svm *svm)
  333. {
  334. u32 mask;
  335. svm->nested.ctl.event_inj = svm->vmcb->control.event_inj;
  336. svm->nested.ctl.event_inj_err = svm->vmcb->control.event_inj_err;
  337. /* Only a few fields of int_ctl are written by the processor. */
  338. mask = V_IRQ_MASK | V_TPR_MASK;
  339. if (!(svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) &&
  340. svm_is_intercept(svm, INTERCEPT_VINTR)) {
  341. /*
  342. * In order to request an interrupt window, L0 is usurping
  343. * svm->vmcb->control.int_ctl and possibly setting V_IRQ
  344. * even if it was clear in L1's VMCB. Restoring it would be
  345. * wrong. However, in this case V_IRQ will remain true until
  346. * interrupt_window_interception calls svm_clear_vintr and
  347. * restores int_ctl. We can just leave it aside.
  348. */
  349. mask &= ~V_IRQ_MASK;
  350. }
  351. if (nested_vgif_enabled(svm))
  352. mask |= V_GIF_MASK;
  353. svm->nested.ctl.int_ctl &= ~mask;
  354. svm->nested.ctl.int_ctl |= svm->vmcb->control.int_ctl & mask;
  355. }
  356. /*
  357. * Transfer any event that L0 or L1 wanted to inject into L2 to
  358. * EXIT_INT_INFO.
  359. */
  360. static void nested_save_pending_event_to_vmcb12(struct vcpu_svm *svm,
  361. struct vmcb *vmcb12)
  362. {
  363. struct kvm_vcpu *vcpu = &svm->vcpu;
  364. u32 exit_int_info = 0;
  365. unsigned int nr;
  366. if (vcpu->arch.exception.injected) {
  367. nr = vcpu->arch.exception.vector;
  368. exit_int_info = nr | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT;
  369. if (vcpu->arch.exception.has_error_code) {
  370. exit_int_info |= SVM_EVTINJ_VALID_ERR;
  371. vmcb12->control.exit_int_info_err =
  372. vcpu->arch.exception.error_code;
  373. }
  374. } else if (vcpu->arch.nmi_injected) {
  375. exit_int_info = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
  376. } else if (vcpu->arch.interrupt.injected) {
  377. nr = vcpu->arch.interrupt.nr;
  378. exit_int_info = nr | SVM_EVTINJ_VALID;
  379. if (vcpu->arch.interrupt.soft)
  380. exit_int_info |= SVM_EVTINJ_TYPE_SOFT;
  381. else
  382. exit_int_info |= SVM_EVTINJ_TYPE_INTR;
  383. }
  384. vmcb12->control.exit_int_info = exit_int_info;
  385. }
  386. static void nested_svm_transition_tlb_flush(struct kvm_vcpu *vcpu)
  387. {
  388. /*
  389. * TODO: optimize unconditional TLB flush/MMU sync. A partial list of
  390. * things to fix before this can be conditional:
  391. *
  392. * - Flush TLBs for both L1 and L2 remote TLB flush
  393. * - Honor L1's request to flush an ASID on nested VMRUN
  394. * - Sync nested NPT MMU on VMRUN that flushes L2's ASID[*]
  395. * - Don't crush a pending TLB flush in vmcb02 on nested VMRUN
  396. * - Flush L1's ASID on KVM_REQ_TLB_FLUSH_GUEST
  397. *
  398. * [*] Unlike nested EPT, SVM's ASID management can invalidate nested
  399. * NPT guest-physical mappings on VMRUN.
  400. */
  401. kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
  402. kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
  403. }
  404. /*
  405. * Load guest's/host's cr3 on nested vmentry or vmexit. @nested_npt is true
  406. * if we are emulating VM-Entry into a guest with NPT enabled.
  407. */
  408. static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
  409. bool nested_npt, bool reload_pdptrs)
  410. {
  411. if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3)))
  412. return -EINVAL;
  413. if (reload_pdptrs && !nested_npt && is_pae_paging(vcpu) &&
  414. CC(!load_pdptrs(vcpu, cr3)))
  415. return -EINVAL;
  416. vcpu->arch.cr3 = cr3;
  417. /* Re-initialize the MMU, e.g. to pick up CR4 MMU role changes. */
  418. kvm_init_mmu(vcpu);
  419. if (!nested_npt)
  420. kvm_mmu_new_pgd(vcpu, cr3);
  421. return 0;
  422. }
  423. void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm)
  424. {
  425. if (!svm->nested.vmcb02.ptr)
  426. return;
  427. /* FIXME: merge g_pat from vmcb01 and vmcb12. */
  428. svm->nested.vmcb02.ptr->save.g_pat = svm->vmcb01.ptr->save.g_pat;
  429. }
  430. static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12)
  431. {
  432. bool new_vmcb12 = false;
  433. struct vmcb *vmcb01 = svm->vmcb01.ptr;
  434. struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;
  435. nested_vmcb02_compute_g_pat(svm);
  436. /* Load the nested guest state */
  437. if (svm->nested.vmcb12_gpa != svm->nested.last_vmcb12_gpa) {
  438. new_vmcb12 = true;
  439. svm->nested.last_vmcb12_gpa = svm->nested.vmcb12_gpa;
  440. svm->nested.force_msr_bitmap_recalc = true;
  441. }
  442. if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_SEG))) {
  443. vmcb02->save.es = vmcb12->save.es;
  444. vmcb02->save.cs = vmcb12->save.cs;
  445. vmcb02->save.ss = vmcb12->save.ss;
  446. vmcb02->save.ds = vmcb12->save.ds;
  447. vmcb02->save.cpl = vmcb12->save.cpl;
  448. vmcb_mark_dirty(vmcb02, VMCB_SEG);
  449. }
  450. if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DT))) {
  451. vmcb02->save.gdtr = vmcb12->save.gdtr;
  452. vmcb02->save.idtr = vmcb12->save.idtr;
  453. vmcb_mark_dirty(vmcb02, VMCB_DT);
  454. }
  455. kvm_set_rflags(&svm->vcpu, vmcb12->save.rflags | X86_EFLAGS_FIXED);
  456. svm_set_efer(&svm->vcpu, svm->nested.save.efer);
  457. svm_set_cr0(&svm->vcpu, svm->nested.save.cr0);
  458. svm_set_cr4(&svm->vcpu, svm->nested.save.cr4);
  459. svm->vcpu.arch.cr2 = vmcb12->save.cr2;
  460. kvm_rax_write(&svm->vcpu, vmcb12->save.rax);
  461. kvm_rsp_write(&svm->vcpu, vmcb12->save.rsp);
  462. kvm_rip_write(&svm->vcpu, vmcb12->save.rip);
  463. /* In case we don't even reach vcpu_run, the fields are not updated */
  464. vmcb02->save.rax = vmcb12->save.rax;
  465. vmcb02->save.rsp = vmcb12->save.rsp;
  466. vmcb02->save.rip = vmcb12->save.rip;
  467. /* These bits will be set properly on the first execution when new_vmc12 is true */
  468. if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_DR))) {
  469. vmcb02->save.dr7 = svm->nested.save.dr7 | DR7_FIXED_1;
  470. svm->vcpu.arch.dr6 = svm->nested.save.dr6 | DR6_ACTIVE_LOW;
  471. vmcb_mark_dirty(vmcb02, VMCB_DR);
  472. }
  473. if (unlikely(svm->lbrv_enabled && (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))) {
  474. /*
  475. * Reserved bits of DEBUGCTL are ignored. Be consistent with
  476. * svm_set_msr's definition of reserved bits.
  477. */
  478. svm_copy_lbrs(vmcb02, vmcb12);
  479. vmcb02->save.dbgctl &= ~DEBUGCTL_RESERVED_BITS;
  480. svm_update_lbrv(&svm->vcpu);
  481. } else if (unlikely(vmcb01->control.virt_ext & LBR_CTL_ENABLE_MASK)) {
  482. svm_copy_lbrs(vmcb02, vmcb01);
  483. }
  484. }
  485. static inline bool is_evtinj_soft(u32 evtinj)
  486. {
  487. u32 type = evtinj & SVM_EVTINJ_TYPE_MASK;
  488. u8 vector = evtinj & SVM_EVTINJ_VEC_MASK;
  489. if (!(evtinj & SVM_EVTINJ_VALID))
  490. return false;
  491. if (type == SVM_EVTINJ_TYPE_SOFT)
  492. return true;
  493. return type == SVM_EVTINJ_TYPE_EXEPT && kvm_exception_is_soft(vector);
  494. }
  495. static bool is_evtinj_nmi(u32 evtinj)
  496. {
  497. u32 type = evtinj & SVM_EVTINJ_TYPE_MASK;
  498. if (!(evtinj & SVM_EVTINJ_VALID))
  499. return false;
  500. return type == SVM_EVTINJ_TYPE_NMI;
  501. }
  502. static void nested_vmcb02_prepare_control(struct vcpu_svm *svm,
  503. unsigned long vmcb12_rip,
  504. unsigned long vmcb12_csbase)
  505. {
  506. u32 int_ctl_vmcb01_bits = V_INTR_MASKING_MASK;
  507. u32 int_ctl_vmcb12_bits = V_TPR_MASK | V_IRQ_INJECTION_BITS_MASK;
  508. struct kvm_vcpu *vcpu = &svm->vcpu;
  509. struct vmcb *vmcb01 = svm->vmcb01.ptr;
  510. struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;
  511. u32 pause_count12;
  512. u32 pause_thresh12;
  513. /*
  514. * Filled at exit: exit_code, exit_code_hi, exit_info_1, exit_info_2,
  515. * exit_int_info, exit_int_info_err, next_rip, insn_len, insn_bytes.
  516. */
  517. if (svm->vgif_enabled && (svm->nested.ctl.int_ctl & V_GIF_ENABLE_MASK))
  518. int_ctl_vmcb12_bits |= (V_GIF_MASK | V_GIF_ENABLE_MASK);
  519. else
  520. int_ctl_vmcb01_bits |= (V_GIF_MASK | V_GIF_ENABLE_MASK);
  521. /* Copied from vmcb01. msrpm_base can be overwritten later. */
  522. vmcb02->control.nested_ctl = vmcb01->control.nested_ctl;
  523. vmcb02->control.iopm_base_pa = vmcb01->control.iopm_base_pa;
  524. vmcb02->control.msrpm_base_pa = vmcb01->control.msrpm_base_pa;
  525. /* Done at vmrun: asid. */
  526. /* Also overwritten later if necessary. */
  527. vmcb02->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
  528. /* nested_cr3. */
  529. if (nested_npt_enabled(svm))
  530. nested_svm_init_mmu_context(vcpu);
  531. vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset(
  532. vcpu->arch.l1_tsc_offset,
  533. svm->nested.ctl.tsc_offset,
  534. svm->tsc_ratio_msr);
  535. vmcb02->control.tsc_offset = vcpu->arch.tsc_offset;
  536. if (svm->tsc_scaling_enabled &&
  537. svm->tsc_ratio_msr != kvm_caps.default_tsc_scaling_ratio)
  538. nested_svm_update_tsc_ratio_msr(vcpu);
  539. vmcb02->control.int_ctl =
  540. (svm->nested.ctl.int_ctl & int_ctl_vmcb12_bits) |
  541. (vmcb01->control.int_ctl & int_ctl_vmcb01_bits);
  542. vmcb02->control.int_vector = svm->nested.ctl.int_vector;
  543. vmcb02->control.int_state = svm->nested.ctl.int_state;
  544. vmcb02->control.event_inj = svm->nested.ctl.event_inj;
  545. vmcb02->control.event_inj_err = svm->nested.ctl.event_inj_err;
  546. /*
  547. * next_rip is consumed on VMRUN as the return address pushed on the
  548. * stack for injected soft exceptions/interrupts. If nrips is exposed
  549. * to L1, take it verbatim from vmcb12. If nrips is supported in
  550. * hardware but not exposed to L1, stuff the actual L2 RIP to emulate
  551. * what a nrips=0 CPU would do (L1 is responsible for advancing RIP
  552. * prior to injecting the event).
  553. */
  554. if (svm->nrips_enabled)
  555. vmcb02->control.next_rip = svm->nested.ctl.next_rip;
  556. else if (boot_cpu_has(X86_FEATURE_NRIPS))
  557. vmcb02->control.next_rip = vmcb12_rip;
  558. svm->nmi_l1_to_l2 = is_evtinj_nmi(vmcb02->control.event_inj);
  559. if (is_evtinj_soft(vmcb02->control.event_inj)) {
  560. svm->soft_int_injected = true;
  561. svm->soft_int_csbase = vmcb12_csbase;
  562. svm->soft_int_old_rip = vmcb12_rip;
  563. if (svm->nrips_enabled)
  564. svm->soft_int_next_rip = svm->nested.ctl.next_rip;
  565. else
  566. svm->soft_int_next_rip = vmcb12_rip;
  567. }
  568. vmcb02->control.virt_ext = vmcb01->control.virt_ext &
  569. LBR_CTL_ENABLE_MASK;
  570. if (svm->lbrv_enabled)
  571. vmcb02->control.virt_ext |=
  572. (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK);
  573. if (!nested_vmcb_needs_vls_intercept(svm))
  574. vmcb02->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
  575. pause_count12 = svm->pause_filter_enabled ? svm->nested.ctl.pause_filter_count : 0;
  576. pause_thresh12 = svm->pause_threshold_enabled ? svm->nested.ctl.pause_filter_thresh : 0;
  577. if (kvm_pause_in_guest(svm->vcpu.kvm)) {
  578. /* use guest values since host doesn't intercept PAUSE */
  579. vmcb02->control.pause_filter_count = pause_count12;
  580. vmcb02->control.pause_filter_thresh = pause_thresh12;
  581. } else {
  582. /* start from host values otherwise */
  583. vmcb02->control.pause_filter_count = vmcb01->control.pause_filter_count;
  584. vmcb02->control.pause_filter_thresh = vmcb01->control.pause_filter_thresh;
  585. /* ... but ensure filtering is disabled if so requested. */
  586. if (vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_PAUSE)) {
  587. if (!pause_count12)
  588. vmcb02->control.pause_filter_count = 0;
  589. if (!pause_thresh12)
  590. vmcb02->control.pause_filter_thresh = 0;
  591. }
  592. }
  593. nested_svm_transition_tlb_flush(vcpu);
  594. /* Enter Guest-Mode */
  595. enter_guest_mode(vcpu);
  596. /*
  597. * Merge guest and host intercepts - must be called with vcpu in
  598. * guest-mode to take effect.
  599. */
  600. recalc_intercepts(svm);
  601. }
  602. static void nested_svm_copy_common_state(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
  603. {
  604. /*
  605. * Some VMCB state is shared between L1 and L2 and thus has to be
  606. * moved at the time of nested vmrun and vmexit.
  607. *
  608. * VMLOAD/VMSAVE state would also belong in this category, but KVM
  609. * always performs VMLOAD and VMSAVE from the VMCB01.
  610. */
  611. to_vmcb->save.spec_ctrl = from_vmcb->save.spec_ctrl;
  612. }
  613. int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa,
  614. struct vmcb *vmcb12, bool from_vmrun)
  615. {
  616. struct vcpu_svm *svm = to_svm(vcpu);
  617. int ret;
  618. trace_kvm_nested_vmenter(svm->vmcb->save.rip,
  619. vmcb12_gpa,
  620. vmcb12->save.rip,
  621. vmcb12->control.int_ctl,
  622. vmcb12->control.event_inj,
  623. vmcb12->control.nested_ctl,
  624. vmcb12->control.nested_cr3,
  625. vmcb12->save.cr3,
  626. KVM_ISA_SVM);
  627. trace_kvm_nested_intercepts(vmcb12->control.intercepts[INTERCEPT_CR] & 0xffff,
  628. vmcb12->control.intercepts[INTERCEPT_CR] >> 16,
  629. vmcb12->control.intercepts[INTERCEPT_EXCEPTION],
  630. vmcb12->control.intercepts[INTERCEPT_WORD3],
  631. vmcb12->control.intercepts[INTERCEPT_WORD4],
  632. vmcb12->control.intercepts[INTERCEPT_WORD5]);
  633. svm->nested.vmcb12_gpa = vmcb12_gpa;
  634. WARN_ON(svm->vmcb == svm->nested.vmcb02.ptr);
  635. nested_svm_copy_common_state(svm->vmcb01.ptr, svm->nested.vmcb02.ptr);
  636. svm_switch_vmcb(svm, &svm->nested.vmcb02);
  637. nested_vmcb02_prepare_control(svm, vmcb12->save.rip, vmcb12->save.cs.base);
  638. nested_vmcb02_prepare_save(svm, vmcb12);
  639. ret = nested_svm_load_cr3(&svm->vcpu, svm->nested.save.cr3,
  640. nested_npt_enabled(svm), from_vmrun);
  641. if (ret)
  642. return ret;
  643. if (!from_vmrun)
  644. kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
  645. svm_set_gif(svm, true);
  646. if (kvm_vcpu_apicv_active(vcpu))
  647. kvm_make_request(KVM_REQ_APICV_UPDATE, vcpu);
  648. return 0;
  649. }
  650. int nested_svm_vmrun(struct kvm_vcpu *vcpu)
  651. {
  652. struct vcpu_svm *svm = to_svm(vcpu);
  653. int ret;
  654. struct vmcb *vmcb12;
  655. struct kvm_host_map map;
  656. u64 vmcb12_gpa;
  657. struct vmcb *vmcb01 = svm->vmcb01.ptr;
  658. if (!svm->nested.hsave_msr) {
  659. kvm_inject_gp(vcpu, 0);
  660. return 1;
  661. }
  662. if (is_smm(vcpu)) {
  663. kvm_queue_exception(vcpu, UD_VECTOR);
  664. return 1;
  665. }
  666. vmcb12_gpa = svm->vmcb->save.rax;
  667. ret = kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map);
  668. if (ret == -EINVAL) {
  669. kvm_inject_gp(vcpu, 0);
  670. return 1;
  671. } else if (ret) {
  672. return kvm_skip_emulated_instruction(vcpu);
  673. }
  674. ret = kvm_skip_emulated_instruction(vcpu);
  675. vmcb12 = map.hva;
  676. if (WARN_ON_ONCE(!svm->nested.initialized))
  677. return -EINVAL;
  678. nested_copy_vmcb_control_to_cache(svm, &vmcb12->control);
  679. nested_copy_vmcb_save_to_cache(svm, &vmcb12->save);
  680. if (!nested_vmcb_check_save(vcpu) ||
  681. !nested_vmcb_check_controls(vcpu)) {
  682. vmcb12->control.exit_code = SVM_EXIT_ERR;
  683. vmcb12->control.exit_code_hi = 0;
  684. vmcb12->control.exit_info_1 = 0;
  685. vmcb12->control.exit_info_2 = 0;
  686. goto out;
  687. }
  688. /*
  689. * Since vmcb01 is not in use, we can use it to store some of the L1
  690. * state.
  691. */
  692. vmcb01->save.efer = vcpu->arch.efer;
  693. vmcb01->save.cr0 = kvm_read_cr0(vcpu);
  694. vmcb01->save.cr4 = vcpu->arch.cr4;
  695. vmcb01->save.rflags = kvm_get_rflags(vcpu);
  696. vmcb01->save.rip = kvm_rip_read(vcpu);
  697. if (!npt_enabled)
  698. vmcb01->save.cr3 = kvm_read_cr3(vcpu);
  699. svm->nested.nested_run_pending = 1;
  700. if (enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12, true))
  701. goto out_exit_err;
  702. if (nested_svm_vmrun_msrpm(svm))
  703. goto out;
  704. out_exit_err:
  705. svm->nested.nested_run_pending = 0;
  706. svm->nmi_l1_to_l2 = false;
  707. svm->soft_int_injected = false;
  708. svm->vmcb->control.exit_code = SVM_EXIT_ERR;
  709. svm->vmcb->control.exit_code_hi = 0;
  710. svm->vmcb->control.exit_info_1 = 0;
  711. svm->vmcb->control.exit_info_2 = 0;
  712. nested_svm_vmexit(svm);
  713. out:
  714. kvm_vcpu_unmap(vcpu, &map, true);
  715. return ret;
  716. }
  717. /* Copy state save area fields which are handled by VMRUN */
  718. void svm_copy_vmrun_state(struct vmcb_save_area *to_save,
  719. struct vmcb_save_area *from_save)
  720. {
  721. to_save->es = from_save->es;
  722. to_save->cs = from_save->cs;
  723. to_save->ss = from_save->ss;
  724. to_save->ds = from_save->ds;
  725. to_save->gdtr = from_save->gdtr;
  726. to_save->idtr = from_save->idtr;
  727. to_save->rflags = from_save->rflags | X86_EFLAGS_FIXED;
  728. to_save->efer = from_save->efer;
  729. to_save->cr0 = from_save->cr0;
  730. to_save->cr3 = from_save->cr3;
  731. to_save->cr4 = from_save->cr4;
  732. to_save->rax = from_save->rax;
  733. to_save->rsp = from_save->rsp;
  734. to_save->rip = from_save->rip;
  735. to_save->cpl = 0;
  736. }
  737. void svm_copy_vmloadsave_state(struct vmcb *to_vmcb, struct vmcb *from_vmcb)
  738. {
  739. to_vmcb->save.fs = from_vmcb->save.fs;
  740. to_vmcb->save.gs = from_vmcb->save.gs;
  741. to_vmcb->save.tr = from_vmcb->save.tr;
  742. to_vmcb->save.ldtr = from_vmcb->save.ldtr;
  743. to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
  744. to_vmcb->save.star = from_vmcb->save.star;
  745. to_vmcb->save.lstar = from_vmcb->save.lstar;
  746. to_vmcb->save.cstar = from_vmcb->save.cstar;
  747. to_vmcb->save.sfmask = from_vmcb->save.sfmask;
  748. to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
  749. to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
  750. to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
  751. }
  752. int nested_svm_vmexit(struct vcpu_svm *svm)
  753. {
  754. struct kvm_vcpu *vcpu = &svm->vcpu;
  755. struct vmcb *vmcb01 = svm->vmcb01.ptr;
  756. struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;
  757. struct vmcb *vmcb12;
  758. struct kvm_host_map map;
  759. int rc;
  760. rc = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.vmcb12_gpa), &map);
  761. if (rc) {
  762. if (rc == -EINVAL)
  763. kvm_inject_gp(vcpu, 0);
  764. return 1;
  765. }
  766. vmcb12 = map.hva;
  767. /* Exit Guest-Mode */
  768. leave_guest_mode(vcpu);
  769. svm->nested.vmcb12_gpa = 0;
  770. WARN_ON_ONCE(svm->nested.nested_run_pending);
  771. kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
  772. /* in case we halted in L2 */
  773. svm->vcpu.arch.mp_state = KVM_MP_STATE_RUNNABLE;
  774. /* Give the current vmcb to the guest */
  775. vmcb12->save.es = vmcb02->save.es;
  776. vmcb12->save.cs = vmcb02->save.cs;
  777. vmcb12->save.ss = vmcb02->save.ss;
  778. vmcb12->save.ds = vmcb02->save.ds;
  779. vmcb12->save.gdtr = vmcb02->save.gdtr;
  780. vmcb12->save.idtr = vmcb02->save.idtr;
  781. vmcb12->save.efer = svm->vcpu.arch.efer;
  782. vmcb12->save.cr0 = kvm_read_cr0(vcpu);
  783. vmcb12->save.cr3 = kvm_read_cr3(vcpu);
  784. vmcb12->save.cr2 = vmcb02->save.cr2;
  785. vmcb12->save.cr4 = svm->vcpu.arch.cr4;
  786. vmcb12->save.rflags = kvm_get_rflags(vcpu);
  787. vmcb12->save.rip = kvm_rip_read(vcpu);
  788. vmcb12->save.rsp = kvm_rsp_read(vcpu);
  789. vmcb12->save.rax = kvm_rax_read(vcpu);
  790. vmcb12->save.dr7 = vmcb02->save.dr7;
  791. vmcb12->save.dr6 = svm->vcpu.arch.dr6;
  792. vmcb12->save.cpl = vmcb02->save.cpl;
  793. vmcb12->control.int_state = vmcb02->control.int_state;
  794. vmcb12->control.exit_code = vmcb02->control.exit_code;
  795. vmcb12->control.exit_code_hi = vmcb02->control.exit_code_hi;
  796. vmcb12->control.exit_info_1 = vmcb02->control.exit_info_1;
  797. vmcb12->control.exit_info_2 = vmcb02->control.exit_info_2;
  798. if (vmcb12->control.exit_code != SVM_EXIT_ERR)
  799. nested_save_pending_event_to_vmcb12(svm, vmcb12);
  800. if (svm->nrips_enabled)
  801. vmcb12->control.next_rip = vmcb02->control.next_rip;
  802. vmcb12->control.int_ctl = svm->nested.ctl.int_ctl;
  803. vmcb12->control.tlb_ctl = svm->nested.ctl.tlb_ctl;
  804. vmcb12->control.event_inj = svm->nested.ctl.event_inj;
  805. vmcb12->control.event_inj_err = svm->nested.ctl.event_inj_err;
  806. if (!kvm_pause_in_guest(vcpu->kvm)) {
  807. vmcb01->control.pause_filter_count = vmcb02->control.pause_filter_count;
  808. vmcb_mark_dirty(vmcb01, VMCB_INTERCEPTS);
  809. }
  810. nested_svm_copy_common_state(svm->nested.vmcb02.ptr, svm->vmcb01.ptr);
  811. svm_switch_vmcb(svm, &svm->vmcb01);
  812. if (unlikely(svm->lbrv_enabled && (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))) {
  813. svm_copy_lbrs(vmcb12, vmcb02);
  814. svm_update_lbrv(vcpu);
  815. } else if (unlikely(vmcb01->control.virt_ext & LBR_CTL_ENABLE_MASK)) {
  816. svm_copy_lbrs(vmcb01, vmcb02);
  817. svm_update_lbrv(vcpu);
  818. }
  819. /*
  820. * On vmexit the GIF is set to false and
  821. * no event can be injected in L1.
  822. */
  823. svm_set_gif(svm, false);
  824. vmcb01->control.exit_int_info = 0;
  825. svm->vcpu.arch.tsc_offset = svm->vcpu.arch.l1_tsc_offset;
  826. if (vmcb01->control.tsc_offset != svm->vcpu.arch.tsc_offset) {
  827. vmcb01->control.tsc_offset = svm->vcpu.arch.tsc_offset;
  828. vmcb_mark_dirty(vmcb01, VMCB_INTERCEPTS);
  829. }
  830. if (kvm_caps.has_tsc_control &&
  831. vcpu->arch.tsc_scaling_ratio != vcpu->arch.l1_tsc_scaling_ratio) {
  832. vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio;
  833. __svm_write_tsc_multiplier(vcpu->arch.tsc_scaling_ratio);
  834. }
  835. svm->nested.ctl.nested_cr3 = 0;
  836. /*
  837. * Restore processor state that had been saved in vmcb01
  838. */
  839. kvm_set_rflags(vcpu, vmcb01->save.rflags);
  840. svm_set_efer(vcpu, vmcb01->save.efer);
  841. svm_set_cr0(vcpu, vmcb01->save.cr0 | X86_CR0_PE);
  842. svm_set_cr4(vcpu, vmcb01->save.cr4);
  843. kvm_rax_write(vcpu, vmcb01->save.rax);
  844. kvm_rsp_write(vcpu, vmcb01->save.rsp);
  845. kvm_rip_write(vcpu, vmcb01->save.rip);
  846. svm->vcpu.arch.dr7 = DR7_FIXED_1;
  847. kvm_update_dr7(&svm->vcpu);
  848. trace_kvm_nested_vmexit_inject(vmcb12->control.exit_code,
  849. vmcb12->control.exit_info_1,
  850. vmcb12->control.exit_info_2,
  851. vmcb12->control.exit_int_info,
  852. vmcb12->control.exit_int_info_err,
  853. KVM_ISA_SVM);
  854. kvm_vcpu_unmap(vcpu, &map, true);
  855. nested_svm_transition_tlb_flush(vcpu);
  856. nested_svm_uninit_mmu_context(vcpu);
  857. rc = nested_svm_load_cr3(vcpu, vmcb01->save.cr3, false, true);
  858. if (rc)
  859. return 1;
  860. /*
  861. * Drop what we picked up for L2 via svm_complete_interrupts() so it
  862. * doesn't end up in L1.
  863. */
  864. svm->vcpu.arch.nmi_injected = false;
  865. kvm_clear_exception_queue(vcpu);
  866. kvm_clear_interrupt_queue(vcpu);
  867. /*
  868. * If we are here following the completion of a VMRUN that
  869. * is being single-stepped, queue the pending #DB intercept
  870. * right now so that it an be accounted for before we execute
  871. * L1's next instruction.
  872. */
  873. if (unlikely(vmcb01->save.rflags & X86_EFLAGS_TF))
  874. kvm_queue_exception(&(svm->vcpu), DB_VECTOR);
  875. /*
  876. * Un-inhibit the AVIC right away, so that other vCPUs can start
  877. * to benefit from it right away.
  878. */
  879. if (kvm_apicv_activated(vcpu->kvm))
  880. kvm_vcpu_update_apicv(vcpu);
  881. return 0;
  882. }
  883. static void nested_svm_triple_fault(struct kvm_vcpu *vcpu)
  884. {
  885. struct vcpu_svm *svm = to_svm(vcpu);
  886. if (!vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_SHUTDOWN))
  887. return;
  888. kvm_clear_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  889. nested_svm_simple_vmexit(to_svm(vcpu), SVM_EXIT_SHUTDOWN);
  890. }
  891. int svm_allocate_nested(struct vcpu_svm *svm)
  892. {
  893. struct page *vmcb02_page;
  894. if (svm->nested.initialized)
  895. return 0;
  896. vmcb02_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
  897. if (!vmcb02_page)
  898. return -ENOMEM;
  899. svm->nested.vmcb02.ptr = page_address(vmcb02_page);
  900. svm->nested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) << PAGE_SHIFT);
  901. svm->nested.msrpm = svm_vcpu_alloc_msrpm();
  902. if (!svm->nested.msrpm)
  903. goto err_free_vmcb02;
  904. svm_vcpu_init_msrpm(&svm->vcpu, svm->nested.msrpm);
  905. svm->nested.initialized = true;
  906. return 0;
  907. err_free_vmcb02:
  908. __free_page(vmcb02_page);
  909. return -ENOMEM;
  910. }
  911. void svm_free_nested(struct vcpu_svm *svm)
  912. {
  913. if (!svm->nested.initialized)
  914. return;
  915. if (WARN_ON_ONCE(svm->vmcb != svm->vmcb01.ptr))
  916. svm_switch_vmcb(svm, &svm->vmcb01);
  917. svm_vcpu_free_msrpm(svm->nested.msrpm);
  918. svm->nested.msrpm = NULL;
  919. __free_page(virt_to_page(svm->nested.vmcb02.ptr));
  920. svm->nested.vmcb02.ptr = NULL;
  921. /*
  922. * When last_vmcb12_gpa matches the current vmcb12 gpa,
  923. * some vmcb12 fields are not loaded if they are marked clean
  924. * in the vmcb12, since in this case they are up to date already.
  925. *
  926. * When the vmcb02 is freed, this optimization becomes invalid.
  927. */
  928. svm->nested.last_vmcb12_gpa = INVALID_GPA;
  929. svm->nested.initialized = false;
  930. }
  931. void svm_leave_nested(struct kvm_vcpu *vcpu)
  932. {
  933. struct vcpu_svm *svm = to_svm(vcpu);
  934. if (is_guest_mode(vcpu)) {
  935. svm->nested.nested_run_pending = 0;
  936. svm->nested.vmcb12_gpa = INVALID_GPA;
  937. leave_guest_mode(vcpu);
  938. svm_switch_vmcb(svm, &svm->vmcb01);
  939. nested_svm_uninit_mmu_context(vcpu);
  940. vmcb_mark_all_dirty(svm->vmcb);
  941. if (kvm_apicv_activated(vcpu->kvm))
  942. kvm_make_request(KVM_REQ_APICV_UPDATE, vcpu);
  943. }
  944. kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
  945. }
  946. static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
  947. {
  948. u32 offset, msr, value;
  949. int write, mask;
  950. if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_MSR_PROT)))
  951. return NESTED_EXIT_HOST;
  952. msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  953. offset = svm_msrpm_offset(msr);
  954. write = svm->vmcb->control.exit_info_1 & 1;
  955. mask = 1 << ((2 * (msr & 0xf)) + write);
  956. if (offset == MSR_INVALID)
  957. return NESTED_EXIT_DONE;
  958. /* Offset is in 32 bit units but need in 8 bit units */
  959. offset *= 4;
  960. if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.ctl.msrpm_base_pa + offset, &value, 4))
  961. return NESTED_EXIT_DONE;
  962. return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
  963. }
  964. static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
  965. {
  966. unsigned port, size, iopm_len;
  967. u16 val, mask;
  968. u8 start_bit;
  969. u64 gpa;
  970. if (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_IOIO_PROT)))
  971. return NESTED_EXIT_HOST;
  972. port = svm->vmcb->control.exit_info_1 >> 16;
  973. size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
  974. SVM_IOIO_SIZE_SHIFT;
  975. gpa = svm->nested.ctl.iopm_base_pa + (port / 8);
  976. start_bit = port % 8;
  977. iopm_len = (start_bit + size > 8) ? 2 : 1;
  978. mask = (0xf >> (4 - size)) << start_bit;
  979. val = 0;
  980. if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
  981. return NESTED_EXIT_DONE;
  982. return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
  983. }
  984. static int nested_svm_intercept(struct vcpu_svm *svm)
  985. {
  986. u32 exit_code = svm->vmcb->control.exit_code;
  987. int vmexit = NESTED_EXIT_HOST;
  988. switch (exit_code) {
  989. case SVM_EXIT_MSR:
  990. vmexit = nested_svm_exit_handled_msr(svm);
  991. break;
  992. case SVM_EXIT_IOIO:
  993. vmexit = nested_svm_intercept_ioio(svm);
  994. break;
  995. case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
  996. if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
  997. vmexit = NESTED_EXIT_DONE;
  998. break;
  999. }
  1000. case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
  1001. if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
  1002. vmexit = NESTED_EXIT_DONE;
  1003. break;
  1004. }
  1005. case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
  1006. /*
  1007. * Host-intercepted exceptions have been checked already in
  1008. * nested_svm_exit_special. There is nothing to do here,
  1009. * the vmexit is injected by svm_check_nested_events.
  1010. */
  1011. vmexit = NESTED_EXIT_DONE;
  1012. break;
  1013. }
  1014. case SVM_EXIT_ERR: {
  1015. vmexit = NESTED_EXIT_DONE;
  1016. break;
  1017. }
  1018. default: {
  1019. if (vmcb12_is_intercept(&svm->nested.ctl, exit_code))
  1020. vmexit = NESTED_EXIT_DONE;
  1021. }
  1022. }
  1023. return vmexit;
  1024. }
  1025. int nested_svm_exit_handled(struct vcpu_svm *svm)
  1026. {
  1027. int vmexit;
  1028. vmexit = nested_svm_intercept(svm);
  1029. if (vmexit == NESTED_EXIT_DONE)
  1030. nested_svm_vmexit(svm);
  1031. return vmexit;
  1032. }
  1033. int nested_svm_check_permissions(struct kvm_vcpu *vcpu)
  1034. {
  1035. if (!(vcpu->arch.efer & EFER_SVME) || !is_paging(vcpu)) {
  1036. kvm_queue_exception(vcpu, UD_VECTOR);
  1037. return 1;
  1038. }
  1039. if (to_svm(vcpu)->vmcb->save.cpl) {
  1040. kvm_inject_gp(vcpu, 0);
  1041. return 1;
  1042. }
  1043. return 0;
  1044. }
  1045. static bool nested_svm_is_exception_vmexit(struct kvm_vcpu *vcpu, u8 vector,
  1046. u32 error_code)
  1047. {
  1048. struct vcpu_svm *svm = to_svm(vcpu);
  1049. return (svm->nested.ctl.intercepts[INTERCEPT_EXCEPTION] & BIT(vector));
  1050. }
  1051. static void nested_svm_inject_exception_vmexit(struct kvm_vcpu *vcpu)
  1052. {
  1053. struct kvm_queued_exception *ex = &vcpu->arch.exception_vmexit;
  1054. struct vcpu_svm *svm = to_svm(vcpu);
  1055. struct vmcb *vmcb = svm->vmcb;
  1056. vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + ex->vector;
  1057. vmcb->control.exit_code_hi = 0;
  1058. if (ex->has_error_code)
  1059. vmcb->control.exit_info_1 = ex->error_code;
  1060. /*
  1061. * EXITINFO2 is undefined for all exception intercepts other
  1062. * than #PF.
  1063. */
  1064. if (ex->vector == PF_VECTOR) {
  1065. if (ex->has_payload)
  1066. vmcb->control.exit_info_2 = ex->payload;
  1067. else
  1068. vmcb->control.exit_info_2 = vcpu->arch.cr2;
  1069. } else if (ex->vector == DB_VECTOR) {
  1070. /* See kvm_check_and_inject_events(). */
  1071. kvm_deliver_exception_payload(vcpu, ex);
  1072. if (vcpu->arch.dr7 & DR7_GD) {
  1073. vcpu->arch.dr7 &= ~DR7_GD;
  1074. kvm_update_dr7(vcpu);
  1075. }
  1076. } else {
  1077. WARN_ON(ex->has_payload);
  1078. }
  1079. nested_svm_vmexit(svm);
  1080. }
  1081. static inline bool nested_exit_on_init(struct vcpu_svm *svm)
  1082. {
  1083. return vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_INIT);
  1084. }
  1085. static int svm_check_nested_events(struct kvm_vcpu *vcpu)
  1086. {
  1087. struct kvm_lapic *apic = vcpu->arch.apic;
  1088. struct vcpu_svm *svm = to_svm(vcpu);
  1089. /*
  1090. * Only a pending nested run blocks a pending exception. If there is a
  1091. * previously injected event, the pending exception occurred while said
  1092. * event was being delivered and thus needs to be handled.
  1093. */
  1094. bool block_nested_exceptions = svm->nested.nested_run_pending;
  1095. /*
  1096. * New events (not exceptions) are only recognized at instruction
  1097. * boundaries. If an event needs reinjection, then KVM is handling a
  1098. * VM-Exit that occurred _during_ instruction execution; new events are
  1099. * blocked until the instruction completes.
  1100. */
  1101. bool block_nested_events = block_nested_exceptions ||
  1102. kvm_event_needs_reinjection(vcpu);
  1103. if (lapic_in_kernel(vcpu) &&
  1104. test_bit(KVM_APIC_INIT, &apic->pending_events)) {
  1105. if (block_nested_events)
  1106. return -EBUSY;
  1107. if (!nested_exit_on_init(svm))
  1108. return 0;
  1109. nested_svm_simple_vmexit(svm, SVM_EXIT_INIT);
  1110. return 0;
  1111. }
  1112. if (vcpu->arch.exception_vmexit.pending) {
  1113. if (block_nested_exceptions)
  1114. return -EBUSY;
  1115. nested_svm_inject_exception_vmexit(vcpu);
  1116. return 0;
  1117. }
  1118. if (vcpu->arch.exception.pending) {
  1119. if (block_nested_exceptions)
  1120. return -EBUSY;
  1121. return 0;
  1122. }
  1123. if (vcpu->arch.smi_pending && !svm_smi_blocked(vcpu)) {
  1124. if (block_nested_events)
  1125. return -EBUSY;
  1126. if (!nested_exit_on_smi(svm))
  1127. return 0;
  1128. nested_svm_simple_vmexit(svm, SVM_EXIT_SMI);
  1129. return 0;
  1130. }
  1131. if (vcpu->arch.nmi_pending && !svm_nmi_blocked(vcpu)) {
  1132. if (block_nested_events)
  1133. return -EBUSY;
  1134. if (!nested_exit_on_nmi(svm))
  1135. return 0;
  1136. nested_svm_simple_vmexit(svm, SVM_EXIT_NMI);
  1137. return 0;
  1138. }
  1139. if (kvm_cpu_has_interrupt(vcpu) && !svm_interrupt_blocked(vcpu)) {
  1140. if (block_nested_events)
  1141. return -EBUSY;
  1142. if (!nested_exit_on_intr(svm))
  1143. return 0;
  1144. trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
  1145. nested_svm_simple_vmexit(svm, SVM_EXIT_INTR);
  1146. return 0;
  1147. }
  1148. return 0;
  1149. }
  1150. int nested_svm_exit_special(struct vcpu_svm *svm)
  1151. {
  1152. u32 exit_code = svm->vmcb->control.exit_code;
  1153. switch (exit_code) {
  1154. case SVM_EXIT_INTR:
  1155. case SVM_EXIT_NMI:
  1156. case SVM_EXIT_NPF:
  1157. return NESTED_EXIT_HOST;
  1158. case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
  1159. u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
  1160. if (svm->vmcb01.ptr->control.intercepts[INTERCEPT_EXCEPTION] &
  1161. excp_bits)
  1162. return NESTED_EXIT_HOST;
  1163. else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR &&
  1164. svm->vcpu.arch.apf.host_apf_flags)
  1165. /* Trap async PF even if not shadowing */
  1166. return NESTED_EXIT_HOST;
  1167. break;
  1168. }
  1169. default:
  1170. break;
  1171. }
  1172. return NESTED_EXIT_CONTINUE;
  1173. }
  1174. void nested_svm_update_tsc_ratio_msr(struct kvm_vcpu *vcpu)
  1175. {
  1176. struct vcpu_svm *svm = to_svm(vcpu);
  1177. vcpu->arch.tsc_scaling_ratio =
  1178. kvm_calc_nested_tsc_multiplier(vcpu->arch.l1_tsc_scaling_ratio,
  1179. svm->tsc_ratio_msr);
  1180. __svm_write_tsc_multiplier(vcpu->arch.tsc_scaling_ratio);
  1181. }
  1182. /* Inverse operation of nested_copy_vmcb_control_to_cache(). asid is copied too. */
  1183. static void nested_copy_vmcb_cache_to_control(struct vmcb_control_area *dst,
  1184. struct vmcb_ctrl_area_cached *from)
  1185. {
  1186. unsigned int i;
  1187. memset(dst, 0, sizeof(struct vmcb_control_area));
  1188. for (i = 0; i < MAX_INTERCEPT; i++)
  1189. dst->intercepts[i] = from->intercepts[i];
  1190. dst->iopm_base_pa = from->iopm_base_pa;
  1191. dst->msrpm_base_pa = from->msrpm_base_pa;
  1192. dst->tsc_offset = from->tsc_offset;
  1193. dst->asid = from->asid;
  1194. dst->tlb_ctl = from->tlb_ctl;
  1195. dst->int_ctl = from->int_ctl;
  1196. dst->int_vector = from->int_vector;
  1197. dst->int_state = from->int_state;
  1198. dst->exit_code = from->exit_code;
  1199. dst->exit_code_hi = from->exit_code_hi;
  1200. dst->exit_info_1 = from->exit_info_1;
  1201. dst->exit_info_2 = from->exit_info_2;
  1202. dst->exit_int_info = from->exit_int_info;
  1203. dst->exit_int_info_err = from->exit_int_info_err;
  1204. dst->nested_ctl = from->nested_ctl;
  1205. dst->event_inj = from->event_inj;
  1206. dst->event_inj_err = from->event_inj_err;
  1207. dst->next_rip = from->next_rip;
  1208. dst->nested_cr3 = from->nested_cr3;
  1209. dst->virt_ext = from->virt_ext;
  1210. dst->pause_filter_count = from->pause_filter_count;
  1211. dst->pause_filter_thresh = from->pause_filter_thresh;
  1212. /* 'clean' and 'hv_enlightenments' are not changed by KVM */
  1213. }
  1214. static int svm_get_nested_state(struct kvm_vcpu *vcpu,
  1215. struct kvm_nested_state __user *user_kvm_nested_state,
  1216. u32 user_data_size)
  1217. {
  1218. struct vcpu_svm *svm;
  1219. struct vmcb_control_area *ctl;
  1220. unsigned long r;
  1221. struct kvm_nested_state kvm_state = {
  1222. .flags = 0,
  1223. .format = KVM_STATE_NESTED_FORMAT_SVM,
  1224. .size = sizeof(kvm_state),
  1225. };
  1226. struct vmcb __user *user_vmcb = (struct vmcb __user *)
  1227. &user_kvm_nested_state->data.svm[0];
  1228. if (!vcpu)
  1229. return kvm_state.size + KVM_STATE_NESTED_SVM_VMCB_SIZE;
  1230. svm = to_svm(vcpu);
  1231. if (user_data_size < kvm_state.size)
  1232. goto out;
  1233. /* First fill in the header and copy it out. */
  1234. if (is_guest_mode(vcpu)) {
  1235. kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb12_gpa;
  1236. kvm_state.size += KVM_STATE_NESTED_SVM_VMCB_SIZE;
  1237. kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
  1238. if (svm->nested.nested_run_pending)
  1239. kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
  1240. }
  1241. if (gif_set(svm))
  1242. kvm_state.flags |= KVM_STATE_NESTED_GIF_SET;
  1243. if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
  1244. return -EFAULT;
  1245. if (!is_guest_mode(vcpu))
  1246. goto out;
  1247. /*
  1248. * Copy over the full size of the VMCB rather than just the size
  1249. * of the structs.
  1250. */
  1251. if (clear_user(user_vmcb, KVM_STATE_NESTED_SVM_VMCB_SIZE))
  1252. return -EFAULT;
  1253. ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
  1254. if (!ctl)
  1255. return -ENOMEM;
  1256. nested_copy_vmcb_cache_to_control(ctl, &svm->nested.ctl);
  1257. r = copy_to_user(&user_vmcb->control, ctl,
  1258. sizeof(user_vmcb->control));
  1259. kfree(ctl);
  1260. if (r)
  1261. return -EFAULT;
  1262. if (copy_to_user(&user_vmcb->save, &svm->vmcb01.ptr->save,
  1263. sizeof(user_vmcb->save)))
  1264. return -EFAULT;
  1265. out:
  1266. return kvm_state.size;
  1267. }
  1268. static int svm_set_nested_state(struct kvm_vcpu *vcpu,
  1269. struct kvm_nested_state __user *user_kvm_nested_state,
  1270. struct kvm_nested_state *kvm_state)
  1271. {
  1272. struct vcpu_svm *svm = to_svm(vcpu);
  1273. struct vmcb __user *user_vmcb = (struct vmcb __user *)
  1274. &user_kvm_nested_state->data.svm[0];
  1275. struct vmcb_control_area *ctl;
  1276. struct vmcb_save_area *save;
  1277. struct vmcb_save_area_cached save_cached;
  1278. struct vmcb_ctrl_area_cached ctl_cached;
  1279. unsigned long cr0;
  1280. int ret;
  1281. BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) >
  1282. KVM_STATE_NESTED_SVM_VMCB_SIZE);
  1283. if (kvm_state->format != KVM_STATE_NESTED_FORMAT_SVM)
  1284. return -EINVAL;
  1285. if (kvm_state->flags & ~(KVM_STATE_NESTED_GUEST_MODE |
  1286. KVM_STATE_NESTED_RUN_PENDING |
  1287. KVM_STATE_NESTED_GIF_SET))
  1288. return -EINVAL;
  1289. /*
  1290. * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's
  1291. * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed.
  1292. */
  1293. if (!(vcpu->arch.efer & EFER_SVME)) {
  1294. /* GIF=1 and no guest mode are required if SVME=0. */
  1295. if (kvm_state->flags != KVM_STATE_NESTED_GIF_SET)
  1296. return -EINVAL;
  1297. }
  1298. /* SMM temporarily disables SVM, so we cannot be in guest mode. */
  1299. if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
  1300. return -EINVAL;
  1301. if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) {
  1302. svm_leave_nested(vcpu);
  1303. svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
  1304. return 0;
  1305. }
  1306. if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa))
  1307. return -EINVAL;
  1308. if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE)
  1309. return -EINVAL;
  1310. ret = -ENOMEM;
  1311. ctl = kzalloc(sizeof(*ctl), GFP_KERNEL_ACCOUNT);
  1312. save = kzalloc(sizeof(*save), GFP_KERNEL_ACCOUNT);
  1313. if (!ctl || !save)
  1314. goto out_free;
  1315. ret = -EFAULT;
  1316. if (copy_from_user(ctl, &user_vmcb->control, sizeof(*ctl)))
  1317. goto out_free;
  1318. if (copy_from_user(save, &user_vmcb->save, sizeof(*save)))
  1319. goto out_free;
  1320. ret = -EINVAL;
  1321. __nested_copy_vmcb_control_to_cache(vcpu, &ctl_cached, ctl);
  1322. if (!__nested_vmcb_check_controls(vcpu, &ctl_cached))
  1323. goto out_free;
  1324. /*
  1325. * Processor state contains L2 state. Check that it is
  1326. * valid for guest mode (see nested_vmcb_check_save).
  1327. */
  1328. cr0 = kvm_read_cr0(vcpu);
  1329. if (((cr0 & X86_CR0_CD) == 0) && (cr0 & X86_CR0_NW))
  1330. goto out_free;
  1331. /*
  1332. * Validate host state saved from before VMRUN (see
  1333. * nested_svm_check_permissions).
  1334. */
  1335. __nested_copy_vmcb_save_to_cache(&save_cached, save);
  1336. if (!(save->cr0 & X86_CR0_PG) ||
  1337. !(save->cr0 & X86_CR0_PE) ||
  1338. (save->rflags & X86_EFLAGS_VM) ||
  1339. !__nested_vmcb_check_save(vcpu, &save_cached))
  1340. goto out_free;
  1341. /*
  1342. * All checks done, we can enter guest mode. Userspace provides
  1343. * vmcb12.control, which will be combined with L1 and stored into
  1344. * vmcb02, and the L1 save state which we store in vmcb01.
  1345. * L2 registers if needed are moved from the current VMCB to VMCB02.
  1346. */
  1347. if (is_guest_mode(vcpu))
  1348. svm_leave_nested(vcpu);
  1349. else
  1350. svm->nested.vmcb02.ptr->save = svm->vmcb01.ptr->save;
  1351. svm_set_gif(svm, !!(kvm_state->flags & KVM_STATE_NESTED_GIF_SET));
  1352. svm->nested.nested_run_pending =
  1353. !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
  1354. svm->nested.vmcb12_gpa = kvm_state->hdr.svm.vmcb_pa;
  1355. svm_copy_vmrun_state(&svm->vmcb01.ptr->save, save);
  1356. nested_copy_vmcb_control_to_cache(svm, ctl);
  1357. svm_switch_vmcb(svm, &svm->nested.vmcb02);
  1358. nested_vmcb02_prepare_control(svm, svm->vmcb->save.rip, svm->vmcb->save.cs.base);
  1359. /*
  1360. * While the nested guest CR3 is already checked and set by
  1361. * KVM_SET_SREGS, it was set when nested state was yet loaded,
  1362. * thus MMU might not be initialized correctly.
  1363. * Set it again to fix this.
  1364. */
  1365. ret = nested_svm_load_cr3(&svm->vcpu, vcpu->arch.cr3,
  1366. nested_npt_enabled(svm), false);
  1367. if (WARN_ON_ONCE(ret))
  1368. goto out_free;
  1369. svm->nested.force_msr_bitmap_recalc = true;
  1370. kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
  1371. ret = 0;
  1372. out_free:
  1373. kfree(save);
  1374. kfree(ctl);
  1375. return ret;
  1376. }
  1377. static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)
  1378. {
  1379. struct vcpu_svm *svm = to_svm(vcpu);
  1380. if (WARN_ON(!is_guest_mode(vcpu)))
  1381. return true;
  1382. if (!vcpu->arch.pdptrs_from_userspace &&
  1383. !nested_npt_enabled(svm) && is_pae_paging(vcpu))
  1384. /*
  1385. * Reload the guest's PDPTRs since after a migration
  1386. * the guest CR3 might be restored prior to setting the nested
  1387. * state which can lead to a load of wrong PDPTRs.
  1388. */
  1389. if (CC(!load_pdptrs(vcpu, vcpu->arch.cr3)))
  1390. return false;
  1391. if (!nested_svm_vmrun_msrpm(svm)) {
  1392. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  1393. vcpu->run->internal.suberror =
  1394. KVM_INTERNAL_ERROR_EMULATION;
  1395. vcpu->run->internal.ndata = 0;
  1396. return false;
  1397. }
  1398. return true;
  1399. }
  1400. struct kvm_x86_nested_ops svm_nested_ops = {
  1401. .leave_nested = svm_leave_nested,
  1402. .is_exception_vmexit = nested_svm_is_exception_vmexit,
  1403. .check_events = svm_check_nested_events,
  1404. .triple_fault = nested_svm_triple_fault,
  1405. .get_nested_state_pages = svm_get_nested_state_pages,
  1406. .get_state = svm_get_nested_state,
  1407. .set_state = svm_set_nested_state,
  1408. };