| 1 | /* |
| 2 | * x86 misc helpers |
| 3 | * |
| 4 | * Copyright (c) 2003 Fabrice Bellard |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "qemu/osdep.h" |
| 21 | #include "qemu/log.h" |
| 22 | #include "accel/tcg/cpu-loop.h" |
| 23 | #include "cpu.h" |
| 24 | #include "exec/helper-proto.h" |
| 25 | #include "exec/cputlb.h" |
| 26 | #include "helper-tcg.h" |
| 27 | |
| 28 | /* |
| 29 | * NOTE: the translator must set DisasContext.cc_op to CC_OP_EFLAGS |
| 30 | * after generating a call to a helper that uses this. |
| 31 | */ |
| 32 | void cpu_load_eflags(CPUX86State *env, int eflags, int update_mask) |
| 33 | { |
| 34 | CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C); |
| 35 | CC_OP = CC_OP_EFLAGS; |
| 36 | env->df = 1 - (2 * ((eflags >> 10) & 1)); |
| 37 | env->eflags = (env->eflags & ~update_mask) | |
| 38 | (eflags & update_mask) | 0x2; |
| 39 | } |
| 40 | |
| 41 | void helper_into(CPUX86State *env, int next_eip_addend) |
| 42 | { |
| 43 | int eflags; |
| 44 | |
| 45 | eflags = cpu_cc_compute_all(env); |
| 46 | if (eflags & CC_O) { |
| 47 | raise_interrupt(env, EXCP04_INTO, next_eip_addend); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | void helper_cpuid(CPUX86State *env) |
| 52 | { |
| 53 | uint32_t eax, ebx, ecx, edx; |
| 54 | |
| 55 | cpu_svm_check_intercept_param(env, SVM_EXIT_CPUID, 0, GETPC()); |
| 56 | |
| 57 | cpu_x86_cpuid(env, (uint32_t)env->regs[R_EAX], (uint32_t)env->regs[R_ECX], |
| 58 | &eax, &ebx, &ecx, &edx); |
| 59 | env->regs[R_EAX] = eax; |
| 60 | env->regs[R_EBX] = ebx; |
| 61 | env->regs[R_ECX] = ecx; |
| 62 | env->regs[R_EDX] = edx; |
| 63 | } |
| 64 | |
| 65 | void helper_rdtsc(CPUX86State *env) |
| 66 | { |
| 67 | uint64_t val; |
| 68 | |
| 69 | if ((env->cr[4] & CR4_TSD_MASK) && ((env->hflags & HF_CPL_MASK) != 0)) { |
| 70 | raise_exception_ra(env, EXCP0D_GPF, GETPC()); |
| 71 | } |
| 72 | cpu_svm_check_intercept_param(env, SVM_EXIT_RDTSC, 0, GETPC()); |
| 73 | |
| 74 | val = cpu_get_tsc(env) + env->tsc_offset; |
| 75 | env->regs[R_EAX] = (uint32_t)(val); |
| 76 | env->regs[R_EDX] = (uint32_t)(val >> 32); |
| 77 | } |
| 78 | |
| 79 | G_NORETURN void helper_rdpmc(CPUX86State *env) |
| 80 | { |
| 81 | if (((env->cr[4] & CR4_PCE_MASK) == 0 ) && |
| 82 | ((env->hflags & HF_CPL_MASK) != 0)) { |
| 83 | raise_exception_ra(env, EXCP0D_GPF, GETPC()); |
| 84 | } |
| 85 | cpu_svm_check_intercept_param(env, SVM_EXIT_RDPMC, 0, GETPC()); |
| 86 | |
| 87 | /* currently unimplemented */ |
| 88 | qemu_log_mask(LOG_UNIMP, "x86: unimplemented rdpmc\n"); |
| 89 | raise_exception_err(env, EXCP06_ILLOP, 0); |
| 90 | } |
| 91 | |
| 92 | G_NORETURN void helper_pause(CPUX86State *env) |
| 93 | { |
| 94 | CPUState *cs = env_cpu(env); |
| 95 | |
| 96 | /* Do gen_eob() tasks before going back to the main loop. */ |
| 97 | do_end_instruction(env); |
| 98 | helper_rechecking_single_step(env); |
| 99 | |
| 100 | /* Just let another CPU run. */ |
| 101 | cs->exception_index = EXCP_INTERRUPT; |
| 102 | cpu_loop_exit(cs); |
| 103 | } |
| 104 | |
| 105 | uint64_t helper_rdpkru(CPUX86State *env, uint32_t ecx) |
| 106 | { |
| 107 | if ((env->cr[4] & CR4_PKE_MASK) == 0) { |
| 108 | raise_exception_err_ra(env, EXCP06_ILLOP, 0, GETPC()); |
| 109 | } |
| 110 | if (ecx != 0) { |
| 111 | raise_exception_err_ra(env, EXCP0D_GPF, 0, GETPC()); |
| 112 | } |
| 113 | |
| 114 | return env->pkru; |
| 115 | } |
| 116 | |
| 117 | void helper_wrpkru(CPUX86State *env, uint32_t ecx, uint64_t val) |
| 118 | { |
| 119 | CPUState *cs = env_cpu(env); |
| 120 | |
| 121 | if ((env->cr[4] & CR4_PKE_MASK) == 0) { |
| 122 | raise_exception_err_ra(env, EXCP06_ILLOP, 0, GETPC()); |
| 123 | } |
| 124 | if (ecx != 0 || (val & 0xFFFFFFFF00000000ull)) { |
| 125 | raise_exception_err_ra(env, EXCP0D_GPF, 0, GETPC()); |
| 126 | } |
| 127 | |
| 128 | env->pkru = val; |
| 129 | tlb_flush(cs); |
| 130 | } |
| 131 | |
| 132 | target_ulong HELPER(rdpid)(CPUX86State *env) |
| 133 | { |
| 134 | #if !defined CONFIG_USER_ONLY |
| 135 | return env->tsc_aux; |
| 136 | #elif defined CONFIG_LINUX && defined CONFIG_GETCPU |
| 137 | unsigned cpu, node; |
| 138 | getcpu(&cpu, &node); |
| 139 | return (node << 12) | (cpu & 0xfff); |
| 140 | #elif defined CONFIG_SCHED_GETCPU |
| 141 | return sched_getcpu(); |
| 142 | #else |
| 143 | return 0; |
| 144 | #endif |
| 145 | } |