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" |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 21 | #include "helper.h" |
| 22 | |
| 23 | #if !defined(CONFIG_USER_ONLY) |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 24 | #include "exec/softmmu_exec.h" |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 25 | #endif /* !defined(CONFIG_USER_ONLY) */ |
| 26 | |
| 27 | /* broken thread support */ |
| 28 | |
| 29 | static spinlock_t global_cpu_lock = SPIN_LOCK_UNLOCKED; |
| 30 | |
| 31 | void helper_lock(void) |
| 32 | { |
| 33 | spin_lock(&global_cpu_lock); |
| 34 | } |
| 35 | |
| 36 | void helper_unlock(void) |
| 37 | { |
| 38 | spin_unlock(&global_cpu_lock); |
| 39 | } |
| 40 | |
Blue Swirl | 92fc4b5 | 2012-04-29 20:35:48 +0000 | [diff] [blame] | 41 | void helper_cmpxchg8b(CPUX86State *env, target_ulong a0) |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 42 | { |
| 43 | uint64_t d; |
| 44 | int eflags; |
| 45 | |
Blue Swirl | f0967a1 | 2012-04-29 12:45:34 +0000 | [diff] [blame] | 46 | eflags = cpu_cc_compute_all(env, CC_OP); |
Blue Swirl | 92fc4b5 | 2012-04-29 20:35:48 +0000 | [diff] [blame] | 47 | d = cpu_ldq_data(env, a0); |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 48 | if (d == (((uint64_t)EDX << 32) | (uint32_t)EAX)) { |
Blue Swirl | 92fc4b5 | 2012-04-29 20:35:48 +0000 | [diff] [blame] | 49 | cpu_stq_data(env, a0, ((uint64_t)ECX << 32) | (uint32_t)EBX); |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 50 | eflags |= CC_Z; |
| 51 | } else { |
| 52 | /* always do the store */ |
Blue Swirl | 92fc4b5 | 2012-04-29 20:35:48 +0000 | [diff] [blame] | 53 | cpu_stq_data(env, a0, d); |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 54 | EDX = (uint32_t)(d >> 32); |
| 55 | EAX = (uint32_t)d; |
| 56 | eflags &= ~CC_Z; |
| 57 | } |
| 58 | CC_SRC = eflags; |
| 59 | } |
| 60 | |
| 61 | #ifdef TARGET_X86_64 |
Blue Swirl | 92fc4b5 | 2012-04-29 20:35:48 +0000 | [diff] [blame] | 62 | void helper_cmpxchg16b(CPUX86State *env, target_ulong a0) |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 63 | { |
| 64 | uint64_t d0, d1; |
| 65 | int eflags; |
| 66 | |
| 67 | if ((a0 & 0xf) != 0) { |
| 68 | raise_exception(env, EXCP0D_GPF); |
| 69 | } |
Blue Swirl | f0967a1 | 2012-04-29 12:45:34 +0000 | [diff] [blame] | 70 | eflags = cpu_cc_compute_all(env, CC_OP); |
Blue Swirl | 92fc4b5 | 2012-04-29 20:35:48 +0000 | [diff] [blame] | 71 | d0 = cpu_ldq_data(env, a0); |
| 72 | d1 = cpu_ldq_data(env, a0 + 8); |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 73 | if (d0 == EAX && d1 == EDX) { |
Blue Swirl | 92fc4b5 | 2012-04-29 20:35:48 +0000 | [diff] [blame] | 74 | cpu_stq_data(env, a0, EBX); |
| 75 | cpu_stq_data(env, a0 + 8, ECX); |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 76 | eflags |= CC_Z; |
| 77 | } else { |
| 78 | /* always do the store */ |
Blue Swirl | 92fc4b5 | 2012-04-29 20:35:48 +0000 | [diff] [blame] | 79 | cpu_stq_data(env, a0, d0); |
| 80 | cpu_stq_data(env, a0 + 8, d1); |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 81 | EDX = d1; |
| 82 | EAX = d0; |
| 83 | eflags &= ~CC_Z; |
| 84 | } |
| 85 | CC_SRC = eflags; |
| 86 | } |
| 87 | #endif |
| 88 | |
Blue Swirl | 92fc4b5 | 2012-04-29 20:35:48 +0000 | [diff] [blame] | 89 | void helper_boundw(CPUX86State *env, target_ulong a0, int v) |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 90 | { |
| 91 | int low, high; |
| 92 | |
Blue Swirl | 92fc4b5 | 2012-04-29 20:35:48 +0000 | [diff] [blame] | 93 | low = cpu_ldsw_data(env, a0); |
| 94 | high = cpu_ldsw_data(env, a0 + 2); |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 95 | v = (int16_t)v; |
| 96 | if (v < low || v > high) { |
| 97 | raise_exception(env, EXCP05_BOUND); |
| 98 | } |
| 99 | } |
| 100 | |
Blue Swirl | 92fc4b5 | 2012-04-29 20:35:48 +0000 | [diff] [blame] | 101 | void helper_boundl(CPUX86State *env, target_ulong a0, int v) |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 102 | { |
| 103 | int low, high; |
| 104 | |
Blue Swirl | 92fc4b5 | 2012-04-29 20:35:48 +0000 | [diff] [blame] | 105 | low = cpu_ldl_data(env, a0); |
| 106 | high = cpu_ldl_data(env, a0 + 4); |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 107 | if (v < low || v > high) { |
| 108 | raise_exception(env, EXCP05_BOUND); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | #if !defined(CONFIG_USER_ONLY) |
| 113 | |
| 114 | #define MMUSUFFIX _mmu |
| 115 | |
| 116 | #define SHIFT 0 |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 117 | #include "exec/softmmu_template.h" |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 118 | |
| 119 | #define SHIFT 1 |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 120 | #include "exec/softmmu_template.h" |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 121 | |
| 122 | #define SHIFT 2 |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 123 | #include "exec/softmmu_template.h" |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 124 | |
| 125 | #define SHIFT 3 |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 126 | #include "exec/softmmu_template.h" |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 127 | |
| 128 | #endif |
| 129 | |
| 130 | #if !defined(CONFIG_USER_ONLY) |
| 131 | /* try to fill the TLB and return an exception if error. If retaddr is |
| 132 | NULL, it means that the function was called in C code (i.e. not |
| 133 | from generated code or from helper.c) */ |
| 134 | /* XXX: fix it to restore all registers */ |
Blue Swirl | 92fc4b5 | 2012-04-29 20:35:48 +0000 | [diff] [blame] | 135 | void tlb_fill(CPUX86State *env, target_ulong addr, int is_write, int mmu_idx, |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 136 | uintptr_t retaddr) |
| 137 | { |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 138 | int ret; |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 139 | |
| 140 | ret = cpu_x86_handle_mmu_fault(env, addr, is_write, mmu_idx); |
| 141 | if (ret) { |
| 142 | if (retaddr) { |
| 143 | /* now we have a real cpu fault */ |
Blue Swirl | a8a826a | 2012-12-04 20:16:07 +0000 | [diff] [blame] | 144 | cpu_restore_state(env, retaddr); |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 145 | } |
| 146 | raise_exception_err(env, env->exception_index, env->error_code); |
| 147 | } |
Blue Swirl | 1077499 | 2012-04-29 16:39:13 +0000 | [diff] [blame] | 148 | } |
| 149 | #endif |