]> git.proxmox.com Git - mirror_qemu.git/blob - target-i386/mem_helper.c
x86: avoid AREG0 for condition code helpers
[mirror_qemu.git] / target-i386 / mem_helper.c
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"
21 #include "dyngen-exec.h"
22 #include "helper.h"
23
24 #if !defined(CONFIG_USER_ONLY)
25 #include "softmmu_exec.h"
26 #endif /* !defined(CONFIG_USER_ONLY) */
27
28 /* broken thread support */
29
30 static spinlock_t global_cpu_lock = SPIN_LOCK_UNLOCKED;
31
32 void helper_lock(void)
33 {
34 spin_lock(&global_cpu_lock);
35 }
36
37 void helper_unlock(void)
38 {
39 spin_unlock(&global_cpu_lock);
40 }
41
42 void helper_cmpxchg8b(target_ulong a0)
43 {
44 uint64_t d;
45 int eflags;
46
47 eflags = cpu_cc_compute_all(env, CC_OP);
48 d = ldq(a0);
49 if (d == (((uint64_t)EDX << 32) | (uint32_t)EAX)) {
50 stq(a0, ((uint64_t)ECX << 32) | (uint32_t)EBX);
51 eflags |= CC_Z;
52 } else {
53 /* always do the store */
54 stq(a0, d);
55 EDX = (uint32_t)(d >> 32);
56 EAX = (uint32_t)d;
57 eflags &= ~CC_Z;
58 }
59 CC_SRC = eflags;
60 }
61
62 #ifdef TARGET_X86_64
63 void helper_cmpxchg16b(target_ulong a0)
64 {
65 uint64_t d0, d1;
66 int eflags;
67
68 if ((a0 & 0xf) != 0) {
69 raise_exception(env, EXCP0D_GPF);
70 }
71 eflags = cpu_cc_compute_all(env, CC_OP);
72 d0 = ldq(a0);
73 d1 = ldq(a0 + 8);
74 if (d0 == EAX && d1 == EDX) {
75 stq(a0, EBX);
76 stq(a0 + 8, ECX);
77 eflags |= CC_Z;
78 } else {
79 /* always do the store */
80 stq(a0, d0);
81 stq(a0 + 8, d1);
82 EDX = d1;
83 EAX = d0;
84 eflags &= ~CC_Z;
85 }
86 CC_SRC = eflags;
87 }
88 #endif
89
90 void helper_boundw(target_ulong a0, int v)
91 {
92 int low, high;
93
94 low = ldsw(a0);
95 high = ldsw(a0 + 2);
96 v = (int16_t)v;
97 if (v < low || v > high) {
98 raise_exception(env, EXCP05_BOUND);
99 }
100 }
101
102 void helper_boundl(target_ulong a0, int v)
103 {
104 int low, high;
105
106 low = ldl(a0);
107 high = ldl(a0 + 4);
108 if (v < low || v > high) {
109 raise_exception(env, EXCP05_BOUND);
110 }
111 }
112
113 #if !defined(CONFIG_USER_ONLY)
114
115 #define MMUSUFFIX _mmu
116
117 #define SHIFT 0
118 #include "softmmu_template.h"
119
120 #define SHIFT 1
121 #include "softmmu_template.h"
122
123 #define SHIFT 2
124 #include "softmmu_template.h"
125
126 #define SHIFT 3
127 #include "softmmu_template.h"
128
129 #endif
130
131 #if !defined(CONFIG_USER_ONLY)
132 /* try to fill the TLB and return an exception if error. If retaddr is
133 NULL, it means that the function was called in C code (i.e. not
134 from generated code or from helper.c) */
135 /* XXX: fix it to restore all registers */
136 void tlb_fill(CPUX86State *env1, target_ulong addr, int is_write, int mmu_idx,
137 uintptr_t retaddr)
138 {
139 TranslationBlock *tb;
140 int ret;
141 CPUX86State *saved_env;
142
143 saved_env = env;
144 env = env1;
145
146 ret = cpu_x86_handle_mmu_fault(env, addr, is_write, mmu_idx);
147 if (ret) {
148 if (retaddr) {
149 /* now we have a real cpu fault */
150 tb = tb_find_pc(retaddr);
151 if (tb) {
152 /* the PC is inside the translated code. It means that we have
153 a virtual CPU fault */
154 cpu_restore_state(tb, env, retaddr);
155 }
156 }
157 raise_exception_err(env, env->exception_index, env->error_code);
158 }
159 env = saved_env;
160 }
161 #endif
162
163 /* temporary wrappers */
164 #if defined(CONFIG_USER_ONLY)
165 #define ldub_data(addr) ldub_raw(addr)
166 #define lduw_data(addr) lduw_raw(addr)
167 #define ldl_data(addr) ldl_raw(addr)
168 #define ldq_data(addr) ldq_raw(addr)
169
170 #define stb_data(addr, data) stb_raw(addr, data)
171 #define stw_data(addr, data) stw_raw(addr, data)
172 #define stl_data(addr, data) stl_raw(addr, data)
173 #define stq_data(addr, data) stq_raw(addr, data)
174 #endif
175
176 #define WRAP_LD(rettype, fn) \
177 rettype cpu_ ## fn(CPUX86State *env1, target_ulong addr) \
178 { \
179 CPUX86State *saved_env; \
180 rettype ret; \
181 \
182 saved_env = env; \
183 env = env1; \
184 ret = fn(addr); \
185 env = saved_env; \
186 return ret; \
187 }
188
189 WRAP_LD(uint32_t, ldub_data)
190 WRAP_LD(uint32_t, lduw_data)
191 WRAP_LD(uint32_t, ldl_data)
192 WRAP_LD(uint64_t, ldq_data)
193 #undef WRAP_LD
194
195 #define WRAP_ST(datatype, fn) \
196 void cpu_ ## fn(CPUX86State *env1, target_ulong addr, datatype val) \
197 { \
198 CPUX86State *saved_env; \
199 \
200 saved_env = env; \
201 env = env1; \
202 fn(addr, val); \
203 env = saved_env; \
204 }
205
206 WRAP_ST(uint32_t, stb_data)
207 WRAP_ST(uint32_t, stw_data)
208 WRAP_ST(uint32_t, stl_data)
209 WRAP_ST(uint64_t, stq_data)
210 #undef WRAP_ST