1f7b2429fSBlue Swirl /* 2f7b2429fSBlue Swirl * x86 misc helpers 3f7b2429fSBlue Swirl * 4f7b2429fSBlue Swirl * Copyright (c) 2003 Fabrice Bellard 5f7b2429fSBlue Swirl * 6f7b2429fSBlue Swirl * This library is free software; you can redistribute it and/or 7f7b2429fSBlue Swirl * modify it under the terms of the GNU Lesser General Public 8f7b2429fSBlue Swirl * License as published by the Free Software Foundation; either 9d9ff33adSChetan Pant * version 2.1 of the License, or (at your option) any later version. 10f7b2429fSBlue Swirl * 11f7b2429fSBlue Swirl * This library is distributed in the hope that it will be useful, 12f7b2429fSBlue Swirl * but WITHOUT ANY WARRANTY; without even the implied warranty of 13f7b2429fSBlue Swirl * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14f7b2429fSBlue Swirl * Lesser General Public License for more details. 15f7b2429fSBlue Swirl * 16f7b2429fSBlue Swirl * You should have received a copy of the GNU Lesser General Public 17f7b2429fSBlue Swirl * License along with this library; if not, see <http://www.gnu.org/licenses/>. 18f7b2429fSBlue Swirl */ 19f7b2429fSBlue Swirl 20b6a0aa05SPeter Maydell #include "qemu/osdep.h" 21f7b2429fSBlue Swirl #include "cpu.h" 222ef6175aSRichard Henderson #include "exec/helper-proto.h" 2363c91552SPaolo Bonzini #include "exec/exec-all.h" 24ed69e831SClaudio Fontana #include "helper-tcg.h" 2592fc4b58SBlue Swirl 2669483f31SClaudio Fontana /* 2769483f31SClaudio Fontana * NOTE: the translator must set DisasContext.cc_op to CC_OP_EFLAGS 2869483f31SClaudio Fontana * after generating a call to a helper that uses this. 2969483f31SClaudio Fontana */ 3069483f31SClaudio Fontana void cpu_load_eflags(CPUX86State *env, int eflags, int update_mask) 3169483f31SClaudio Fontana { 3269483f31SClaudio Fontana CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C); 3369483f31SClaudio Fontana CC_OP = CC_OP_EFLAGS; 3469483f31SClaudio Fontana env->df = 1 - (2 * ((eflags >> 10) & 1)); 3569483f31SClaudio Fontana env->eflags = (env->eflags & ~update_mask) | 3669483f31SClaudio Fontana (eflags & update_mask) | 0x2; 3769483f31SClaudio Fontana } 3869483f31SClaudio Fontana 394a7443beSBlue Swirl void helper_into(CPUX86State *env, int next_eip_addend) 40f7b2429fSBlue Swirl { 41f7b2429fSBlue Swirl int eflags; 42f7b2429fSBlue Swirl 43f0967a1aSBlue Swirl eflags = cpu_cc_compute_all(env, CC_OP); 44f7b2429fSBlue Swirl if (eflags & CC_O) { 45f7b2429fSBlue Swirl raise_interrupt(env, EXCP04_INTO, 1, 0, next_eip_addend); 46f7b2429fSBlue Swirl } 47f7b2429fSBlue Swirl } 48f7b2429fSBlue Swirl 494a7443beSBlue Swirl void helper_cpuid(CPUX86State *env) 50f7b2429fSBlue Swirl { 51f7b2429fSBlue Swirl uint32_t eax, ebx, ecx, edx; 52f7b2429fSBlue Swirl 5365c9d60aSPaolo Bonzini cpu_svm_check_intercept_param(env, SVM_EXIT_CPUID, 0, GETPC()); 54f7b2429fSBlue Swirl 5590a2541bSliguang cpu_x86_cpuid(env, (uint32_t)env->regs[R_EAX], (uint32_t)env->regs[R_ECX], 5690a2541bSliguang &eax, &ebx, &ecx, &edx); 574b34e3adSliguang env->regs[R_EAX] = eax; 5870b51365Sliguang env->regs[R_EBX] = ebx; 59a4165610Sliguang env->regs[R_ECX] = ecx; 6000f5e6f2Sliguang env->regs[R_EDX] = edx; 61f7b2429fSBlue Swirl } 62f7b2429fSBlue Swirl 634a7443beSBlue Swirl void helper_rdtsc(CPUX86State *env) 64f7b2429fSBlue Swirl { 65f7b2429fSBlue Swirl uint64_t val; 66f7b2429fSBlue Swirl 67f7b2429fSBlue Swirl if ((env->cr[4] & CR4_TSD_MASK) && ((env->hflags & HF_CPL_MASK) != 0)) { 684054cdecSPavel Dovgalyuk raise_exception_ra(env, EXCP0D_GPF, GETPC()); 69f7b2429fSBlue Swirl } 7065c9d60aSPaolo Bonzini cpu_svm_check_intercept_param(env, SVM_EXIT_RDTSC, 0, GETPC()); 71f7b2429fSBlue Swirl 72f7b2429fSBlue Swirl val = cpu_get_tsc(env) + env->tsc_offset; 734b34e3adSliguang env->regs[R_EAX] = (uint32_t)(val); 7400f5e6f2Sliguang env->regs[R_EDX] = (uint32_t)(val >> 32); 75f7b2429fSBlue Swirl } 76f7b2429fSBlue Swirl 774a7443beSBlue Swirl void helper_rdtscp(CPUX86State *env) 78f7b2429fSBlue Swirl { 794a7443beSBlue Swirl helper_rdtsc(env); 80a4165610Sliguang env->regs[R_ECX] = (uint32_t)(env->tsc_aux); 81f7b2429fSBlue Swirl } 82f7b2429fSBlue Swirl 83b82055aeSRichard Henderson void QEMU_NORETURN helper_rdpmc(CPUX86State *env) 84f7b2429fSBlue Swirl { 85c45b426aSZheng Zhan Liang if (((env->cr[4] & CR4_PCE_MASK) == 0 ) && 86c45b426aSZheng Zhan Liang ((env->hflags & HF_CPL_MASK) != 0)) { 874054cdecSPavel Dovgalyuk raise_exception_ra(env, EXCP0D_GPF, GETPC()); 88f7b2429fSBlue Swirl } 8965c9d60aSPaolo Bonzini cpu_svm_check_intercept_param(env, SVM_EXIT_RDPMC, 0, GETPC()); 90f7b2429fSBlue Swirl 91f7b2429fSBlue Swirl /* currently unimplemented */ 92f7b2429fSBlue Swirl qemu_log_mask(LOG_UNIMP, "x86: unimplemented rdpmc\n"); 93f7b2429fSBlue Swirl raise_exception_err(env, EXCP06_ILLOP, 0); 94f7b2429fSBlue Swirl } 95f7b2429fSBlue Swirl 96*4ea2449bSRichard Henderson void QEMU_NORETURN do_pause(CPUX86State *env) 9781f3053bSPaolo Bonzini { 98eb26784fSRichard Henderson CPUState *cs = env_cpu(env); 9981f3053bSPaolo Bonzini 10081f3053bSPaolo Bonzini /* Just let another CPU run. */ 10127103424SAndreas Färber cs->exception_index = EXCP_INTERRUPT; 1025638d180SAndreas Färber cpu_loop_exit(cs); 10381f3053bSPaolo Bonzini } 10481f3053bSPaolo Bonzini 105b82055aeSRichard Henderson void QEMU_NORETURN helper_pause(CPUX86State *env, int next_eip_addend) 10681f3053bSPaolo Bonzini { 10765c9d60aSPaolo Bonzini cpu_svm_check_intercept_param(env, SVM_EXIT_PAUSE, 0, GETPC()); 10881f3053bSPaolo Bonzini env->eip += next_eip_addend; 10981f3053bSPaolo Bonzini 110eb26784fSRichard Henderson do_pause(env); 11181f3053bSPaolo Bonzini } 11281f3053bSPaolo Bonzini 113b82055aeSRichard Henderson void QEMU_NORETURN helper_debug(CPUX86State *env) 114f7b2429fSBlue Swirl { 1156aa9e42fSRichard Henderson CPUState *cs = env_cpu(env); 11627103424SAndreas Färber 11727103424SAndreas Färber cs->exception_index = EXCP_DEBUG; 1185638d180SAndreas Färber cpu_loop_exit(cs); 119f7b2429fSBlue Swirl } 1200f70ed47SPaolo Bonzini 1210f70ed47SPaolo Bonzini uint64_t helper_rdpkru(CPUX86State *env, uint32_t ecx) 1220f70ed47SPaolo Bonzini { 1230f70ed47SPaolo Bonzini if ((env->cr[4] & CR4_PKE_MASK) == 0) { 1240f70ed47SPaolo Bonzini raise_exception_err_ra(env, EXCP06_ILLOP, 0, GETPC()); 1250f70ed47SPaolo Bonzini } 1260f70ed47SPaolo Bonzini if (ecx != 0) { 1270f70ed47SPaolo Bonzini raise_exception_err_ra(env, EXCP0D_GPF, 0, GETPC()); 1280f70ed47SPaolo Bonzini } 1290f70ed47SPaolo Bonzini 1300f70ed47SPaolo Bonzini return env->pkru; 1310f70ed47SPaolo Bonzini } 1320f70ed47SPaolo Bonzini 1330f70ed47SPaolo Bonzini void helper_wrpkru(CPUX86State *env, uint32_t ecx, uint64_t val) 1340f70ed47SPaolo Bonzini { 1356aa9e42fSRichard Henderson CPUState *cs = env_cpu(env); 1360f70ed47SPaolo Bonzini 1370f70ed47SPaolo Bonzini if ((env->cr[4] & CR4_PKE_MASK) == 0) { 1380f70ed47SPaolo Bonzini raise_exception_err_ra(env, EXCP06_ILLOP, 0, GETPC()); 1390f70ed47SPaolo Bonzini } 1400f70ed47SPaolo Bonzini if (ecx != 0 || (val & 0xFFFFFFFF00000000ull)) { 1410f70ed47SPaolo Bonzini raise_exception_err_ra(env, EXCP0D_GPF, 0, GETPC()); 1420f70ed47SPaolo Bonzini } 1430f70ed47SPaolo Bonzini 1440f70ed47SPaolo Bonzini env->pkru = val; 145d10eb08fSAlex Bennée tlb_flush(cs); 1460f70ed47SPaolo Bonzini } 147