Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 1 | /* |
| 2 | * x86 memory access 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 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 "cpu.h" |
Richard Henderson | 2ef6175 | 2014-04-07 22:31:41 -0700 | [diff] [blame] | 21 | #include "exec/helper-proto.h" |
Paolo Bonzini | f08b617 | 2014-03-28 19:42:10 +0100 | [diff] [blame] | 22 | #include "exec/cpu_ldst.h" |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 23 | |
| 24 | /* broken thread support */ |
| 25 | |
| 26 | static spinlock_t global_cpu_lock = SPIN_LOCK_UNLOCKED; |
| 27 | |
| 28 | void helper_lock(void) |
| 29 | { |
| 30 | spin_lock(&global_cpu_lock); |
| 31 | } |
| 32 | |
| 33 | void helper_unlock(void) |
| 34 | { |
| 35 | spin_unlock(&global_cpu_lock); |
| 36 | } |
| 37 | |
Blue Swirl | 92fc4b5 | 2012-04-29 20:35:48 +0000 | [diff] [blame] | 38 | void helper_cmpxchg8b(CPUX86State *env, target_ulong a0) |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 39 | { |
| 40 | uint64_t d; |
| 41 | int eflags; |
| 42 | |
Blue Swirl | f0967a1 | 2012-04-29 12:45:34 +0000 | [diff] [blame] | 43 | eflags = cpu_cc_compute_all(env, CC_OP); |
Blue Swirl | 92fc4b5 | 2012-04-29 20:35:48 +0000 | [diff] [blame] | 44 | d = cpu_ldq_data(env, a0); |
liguang | 00f5e6f | 2013-05-28 16:21:02 +0800 | [diff] [blame] | 45 | if (d == (((uint64_t)env->regs[R_EDX] << 32) | (uint32_t)env->regs[R_EAX])) { |
liguang | a416561 | 2013-05-28 16:21:01 +0800 | [diff] [blame] | 46 | cpu_stq_data(env, a0, ((uint64_t)env->regs[R_ECX] << 32) | (uint32_t)env->regs[R_EBX]); |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 47 | eflags |= CC_Z; |
| 48 | } else { |
| 49 | /* always do the store */ |
Blue Swirl | 92fc4b5 | 2012-04-29 20:35:48 +0000 | [diff] [blame] | 50 | cpu_stq_data(env, a0, d); |
liguang | 00f5e6f | 2013-05-28 16:21:02 +0800 | [diff] [blame] | 51 | env->regs[R_EDX] = (uint32_t)(d >> 32); |
liguang | 4b34e3a | 2013-05-28 16:20:59 +0800 | [diff] [blame] | 52 | env->regs[R_EAX] = (uint32_t)d; |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 53 | eflags &= ~CC_Z; |
| 54 | } |
| 55 | CC_SRC = eflags; |
| 56 | } |
| 57 | |
| 58 | #ifdef TARGET_X86_64 |
Blue Swirl | 92fc4b5 | 2012-04-29 20:35:48 +0000 | [diff] [blame] | 59 | void helper_cmpxchg16b(CPUX86State *env, target_ulong a0) |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 60 | { |
| 61 | uint64_t d0, d1; |
| 62 | int eflags; |
| 63 | |
| 64 | if ((a0 & 0xf) != 0) { |
| 65 | raise_exception(env, EXCP0D_GPF); |
| 66 | } |
Blue Swirl | f0967a1 | 2012-04-29 12:45:34 +0000 | [diff] [blame] | 67 | eflags = cpu_cc_compute_all(env, CC_OP); |
Blue Swirl | 92fc4b5 | 2012-04-29 20:35:48 +0000 | [diff] [blame] | 68 | d0 = cpu_ldq_data(env, a0); |
| 69 | d1 = cpu_ldq_data(env, a0 + 8); |
liguang | 00f5e6f | 2013-05-28 16:21:02 +0800 | [diff] [blame] | 70 | if (d0 == env->regs[R_EAX] && d1 == env->regs[R_EDX]) { |
liguang | 70b5136 | 2013-05-28 16:21:00 +0800 | [diff] [blame] | 71 | cpu_stq_data(env, a0, env->regs[R_EBX]); |
liguang | a416561 | 2013-05-28 16:21:01 +0800 | [diff] [blame] | 72 | cpu_stq_data(env, a0 + 8, env->regs[R_ECX]); |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 73 | eflags |= CC_Z; |
| 74 | } else { |
| 75 | /* always do the store */ |
Blue Swirl | 92fc4b5 | 2012-04-29 20:35:48 +0000 | [diff] [blame] | 76 | cpu_stq_data(env, a0, d0); |
| 77 | cpu_stq_data(env, a0 + 8, d1); |
liguang | 00f5e6f | 2013-05-28 16:21:02 +0800 | [diff] [blame] | 78 | env->regs[R_EDX] = d1; |
liguang | 4b34e3a | 2013-05-28 16:20:59 +0800 | [diff] [blame] | 79 | env->regs[R_EAX] = d0; |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 80 | eflags &= ~CC_Z; |
| 81 | } |
| 82 | CC_SRC = eflags; |
| 83 | } |
| 84 | #endif |
| 85 | |
Blue Swirl | 92fc4b5 | 2012-04-29 20:35:48 +0000 | [diff] [blame] | 86 | void helper_boundw(CPUX86State *env, target_ulong a0, int v) |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 87 | { |
| 88 | int low, high; |
| 89 | |
Blue Swirl | 92fc4b5 | 2012-04-29 20:35:48 +0000 | [diff] [blame] | 90 | low = cpu_ldsw_data(env, a0); |
| 91 | high = cpu_ldsw_data(env, a0 + 2); |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 92 | v = (int16_t)v; |
| 93 | if (v < low || v > high) { |
| 94 | raise_exception(env, EXCP05_BOUND); |
| 95 | } |
| 96 | } |
| 97 | |
Blue Swirl | 92fc4b5 | 2012-04-29 20:35:48 +0000 | [diff] [blame] | 98 | void helper_boundl(CPUX86State *env, target_ulong a0, int v) |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 99 | { |
| 100 | int low, high; |
| 101 | |
Blue Swirl | 92fc4b5 | 2012-04-29 20:35:48 +0000 | [diff] [blame] | 102 | low = cpu_ldl_data(env, a0); |
| 103 | high = cpu_ldl_data(env, a0 + 4); |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 104 | if (v < low || v > high) { |
| 105 | raise_exception(env, EXCP05_BOUND); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | #if !defined(CONFIG_USER_ONLY) |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 110 | /* try to fill the TLB and return an exception if error. If retaddr is |
Andreas Färber | d5a11fe | 2013-08-27 00:28:06 +0200 | [diff] [blame] | 111 | * NULL, it means that the function was called in C code (i.e. not |
| 112 | * from generated code or from helper.c) |
| 113 | */ |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 114 | /* XXX: fix it to restore all registers */ |
Andreas Färber | d5a11fe | 2013-08-27 00:28:06 +0200 | [diff] [blame] | 115 | void tlb_fill(CPUState *cs, target_ulong addr, int is_write, int mmu_idx, |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 116 | uintptr_t retaddr) |
| 117 | { |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 118 | int ret; |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 119 | |
Andreas Färber | 2710342 | 2013-08-26 08:31:06 +0200 | [diff] [blame] | 120 | ret = x86_cpu_handle_mmu_fault(cs, addr, is_write, mmu_idx); |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 121 | if (ret) { |
Andreas Färber | d5a11fe | 2013-08-27 00:28:06 +0200 | [diff] [blame] | 122 | X86CPU *cpu = X86_CPU(cs); |
| 123 | CPUX86State *env = &cpu->env; |
| 124 | |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 125 | if (retaddr) { |
| 126 | /* now we have a real cpu fault */ |
Andreas Färber | 3f38f30 | 2013-09-01 16:51:34 +0200 | [diff] [blame] | 127 | cpu_restore_state(cs, retaddr); |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 128 | } |
Andreas Färber | 2710342 | 2013-08-26 08:31:06 +0200 | [diff] [blame] | 129 | raise_exception_err(env, cs->exception_index, env->error_code); |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 130 | } |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 131 | } |
| 132 | #endif |