18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */ 28c2ecf20Sopenharmony_ci/* CPU virtualization extensions handling 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * This should carry the code for handling CPU virtualization extensions 58c2ecf20Sopenharmony_ci * that needs to live in the kernel core. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Author: Eduardo Habkost <ehabkost@redhat.com> 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * Copyright (C) 2008, Red Hat Inc. 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * Contains code from KVM, Copyright (C) 2006 Qumranet, Inc. 128c2ecf20Sopenharmony_ci */ 138c2ecf20Sopenharmony_ci#ifndef _ASM_X86_VIRTEX_H 148c2ecf20Sopenharmony_ci#define _ASM_X86_VIRTEX_H 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#include <asm/processor.h> 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#include <asm/vmx.h> 198c2ecf20Sopenharmony_ci#include <asm/svm.h> 208c2ecf20Sopenharmony_ci#include <asm/tlbflush.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci/* 238c2ecf20Sopenharmony_ci * VMX functions: 248c2ecf20Sopenharmony_ci */ 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_cistatic inline int cpu_has_vmx(void) 278c2ecf20Sopenharmony_ci{ 288c2ecf20Sopenharmony_ci unsigned long ecx = cpuid_ecx(1); 298c2ecf20Sopenharmony_ci return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */ 308c2ecf20Sopenharmony_ci} 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci/** 348c2ecf20Sopenharmony_ci * cpu_vmxoff() - Disable VMX on the current CPU 358c2ecf20Sopenharmony_ci * 368c2ecf20Sopenharmony_ci * Disable VMX and clear CR4.VMXE (even if VMXOFF faults) 378c2ecf20Sopenharmony_ci * 388c2ecf20Sopenharmony_ci * Note, VMXOFF causes a #UD if the CPU is !post-VMXON, but it's impossible to 398c2ecf20Sopenharmony_ci * atomically track post-VMXON state, e.g. this may be called in NMI context. 408c2ecf20Sopenharmony_ci * Eat all faults as all other faults on VMXOFF faults are mode related, i.e. 418c2ecf20Sopenharmony_ci * faults are guaranteed to be due to the !post-VMXON check unless the CPU is 428c2ecf20Sopenharmony_ci * magically in RM, VM86, compat mode, or at CPL>0. 438c2ecf20Sopenharmony_ci */ 448c2ecf20Sopenharmony_cistatic inline void cpu_vmxoff(void) 458c2ecf20Sopenharmony_ci{ 468c2ecf20Sopenharmony_ci asm_volatile_goto("1: vmxoff\n\t" 478c2ecf20Sopenharmony_ci _ASM_EXTABLE(1b, %l[fault]) :::: fault); 488c2ecf20Sopenharmony_cifault: 498c2ecf20Sopenharmony_ci cr4_clear_bits(X86_CR4_VMXE); 508c2ecf20Sopenharmony_ci} 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_cistatic inline int cpu_vmx_enabled(void) 538c2ecf20Sopenharmony_ci{ 548c2ecf20Sopenharmony_ci return __read_cr4() & X86_CR4_VMXE; 558c2ecf20Sopenharmony_ci} 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci/** Disable VMX if it is enabled on the current CPU 588c2ecf20Sopenharmony_ci * 598c2ecf20Sopenharmony_ci * You shouldn't call this if cpu_has_vmx() returns 0. 608c2ecf20Sopenharmony_ci */ 618c2ecf20Sopenharmony_cistatic inline void __cpu_emergency_vmxoff(void) 628c2ecf20Sopenharmony_ci{ 638c2ecf20Sopenharmony_ci if (cpu_vmx_enabled()) 648c2ecf20Sopenharmony_ci cpu_vmxoff(); 658c2ecf20Sopenharmony_ci} 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci/** Disable VMX if it is supported and enabled on the current CPU 688c2ecf20Sopenharmony_ci */ 698c2ecf20Sopenharmony_cistatic inline void cpu_emergency_vmxoff(void) 708c2ecf20Sopenharmony_ci{ 718c2ecf20Sopenharmony_ci if (cpu_has_vmx()) 728c2ecf20Sopenharmony_ci __cpu_emergency_vmxoff(); 738c2ecf20Sopenharmony_ci} 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci/* 798c2ecf20Sopenharmony_ci * SVM functions: 808c2ecf20Sopenharmony_ci */ 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci/** Check if the CPU has SVM support 838c2ecf20Sopenharmony_ci * 848c2ecf20Sopenharmony_ci * You can use the 'msg' arg to get a message describing the problem, 858c2ecf20Sopenharmony_ci * if the function returns zero. Simply pass NULL if you are not interested 868c2ecf20Sopenharmony_ci * on the messages; gcc should take care of not generating code for 878c2ecf20Sopenharmony_ci * the messages on this case. 888c2ecf20Sopenharmony_ci */ 898c2ecf20Sopenharmony_cistatic inline int cpu_has_svm(const char **msg) 908c2ecf20Sopenharmony_ci{ 918c2ecf20Sopenharmony_ci if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD && 928c2ecf20Sopenharmony_ci boot_cpu_data.x86_vendor != X86_VENDOR_HYGON) { 938c2ecf20Sopenharmony_ci if (msg) 948c2ecf20Sopenharmony_ci *msg = "not amd or hygon"; 958c2ecf20Sopenharmony_ci return 0; 968c2ecf20Sopenharmony_ci } 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci if (!boot_cpu_has(X86_FEATURE_SVM)) { 998c2ecf20Sopenharmony_ci if (msg) 1008c2ecf20Sopenharmony_ci *msg = "svm not available"; 1018c2ecf20Sopenharmony_ci return 0; 1028c2ecf20Sopenharmony_ci } 1038c2ecf20Sopenharmony_ci return 1; 1048c2ecf20Sopenharmony_ci} 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci/** Disable SVM on the current CPU 1088c2ecf20Sopenharmony_ci * 1098c2ecf20Sopenharmony_ci * You should call this only if cpu_has_svm() returned true. 1108c2ecf20Sopenharmony_ci */ 1118c2ecf20Sopenharmony_cistatic inline void cpu_svm_disable(void) 1128c2ecf20Sopenharmony_ci{ 1138c2ecf20Sopenharmony_ci uint64_t efer; 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci wrmsrl(MSR_VM_HSAVE_PA, 0); 1168c2ecf20Sopenharmony_ci rdmsrl(MSR_EFER, efer); 1178c2ecf20Sopenharmony_ci if (efer & EFER_SVME) { 1188c2ecf20Sopenharmony_ci /* 1198c2ecf20Sopenharmony_ci * Force GIF=1 prior to disabling SVM to ensure INIT and NMI 1208c2ecf20Sopenharmony_ci * aren't blocked, e.g. if a fatal error occurred between CLGI 1218c2ecf20Sopenharmony_ci * and STGI. Note, STGI may #UD if SVM is disabled from NMI 1228c2ecf20Sopenharmony_ci * context between reading EFER and executing STGI. In that 1238c2ecf20Sopenharmony_ci * case, GIF must already be set, otherwise the NMI would have 1248c2ecf20Sopenharmony_ci * been blocked, so just eat the fault. 1258c2ecf20Sopenharmony_ci */ 1268c2ecf20Sopenharmony_ci asm_volatile_goto("1: stgi\n\t" 1278c2ecf20Sopenharmony_ci _ASM_EXTABLE(1b, %l[fault]) 1288c2ecf20Sopenharmony_ci ::: "memory" : fault); 1298c2ecf20Sopenharmony_cifault: 1308c2ecf20Sopenharmony_ci wrmsrl(MSR_EFER, efer & ~EFER_SVME); 1318c2ecf20Sopenharmony_ci } 1328c2ecf20Sopenharmony_ci} 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci/** Makes sure SVM is disabled, if it is supported on the CPU 1358c2ecf20Sopenharmony_ci */ 1368c2ecf20Sopenharmony_cistatic inline void cpu_emergency_svm_disable(void) 1378c2ecf20Sopenharmony_ci{ 1388c2ecf20Sopenharmony_ci if (cpu_has_svm(NULL)) 1398c2ecf20Sopenharmony_ci cpu_svm_disable(); 1408c2ecf20Sopenharmony_ci} 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci#endif /* _ASM_X86_VIRTEX_H */ 143