]>
Commit | Line | Data |
---|---|---|
9a64fbe4 | 1 | /* |
2f5a189c | 2 | * PowerPC memory access emulation helpers for QEMU. |
5fafdf24 | 3 | * |
76a66253 | 4 | * Copyright (c) 2003-2007 Jocelyn Mayer |
9a64fbe4 FB |
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 | |
8167ee88 | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
9a64fbe4 | 18 | */ |
3e457172 | 19 | #include "cpu.h" |
1de7afc9 | 20 | #include "qemu/host-utils.h" |
2ef6175a | 21 | #include "exec/helper-proto.h" |
9a64fbe4 | 22 | |
0411a972 | 23 | #include "helper_regs.h" |
f08b6170 | 24 | #include "exec/cpu_ldst.h" |
3e457172 | 25 | |
fdabc366 | 26 | //#define DEBUG_OP |
d12d51d5 | 27 | |
e22c357b DK |
28 | static inline bool needs_byteswap(const CPUPPCState *env) |
29 | { | |
30 | #if defined(TARGET_WORDS_BIGENDIAN) | |
31 | return msr_le; | |
32 | #else | |
33 | return !msr_le; | |
34 | #endif | |
35 | } | |
36 | ||
ff4a62cd AJ |
37 | /*****************************************************************************/ |
38 | /* Memory load and stores */ | |
39 | ||
2f5a189c BS |
40 | static inline target_ulong addr_add(CPUPPCState *env, target_ulong addr, |
41 | target_long arg) | |
ff4a62cd AJ |
42 | { |
43 | #if defined(TARGET_PPC64) | |
e42a61f1 | 44 | if (!msr_is_64bit(env, env->msr)) { |
b327c654 BS |
45 | return (uint32_t)(addr + arg); |
46 | } else | |
ff4a62cd | 47 | #endif |
b327c654 BS |
48 | { |
49 | return addr + arg; | |
50 | } | |
ff4a62cd AJ |
51 | } |
52 | ||
2f5a189c | 53 | void helper_lmw(CPUPPCState *env, target_ulong addr, uint32_t reg) |
ff4a62cd | 54 | { |
76db3ba4 | 55 | for (; reg < 32; reg++) { |
e22c357b | 56 | if (needs_byteswap(env)) { |
2f5a189c | 57 | env->gpr[reg] = bswap32(cpu_ldl_data(env, addr)); |
b327c654 | 58 | } else { |
2f5a189c | 59 | env->gpr[reg] = cpu_ldl_data(env, addr); |
b327c654 | 60 | } |
2f5a189c | 61 | addr = addr_add(env, addr, 4); |
ff4a62cd AJ |
62 | } |
63 | } | |
64 | ||
2f5a189c | 65 | void helper_stmw(CPUPPCState *env, target_ulong addr, uint32_t reg) |
ff4a62cd | 66 | { |
76db3ba4 | 67 | for (; reg < 32; reg++) { |
e22c357b | 68 | if (needs_byteswap(env)) { |
2f5a189c | 69 | cpu_stl_data(env, addr, bswap32((uint32_t)env->gpr[reg])); |
b327c654 | 70 | } else { |
2f5a189c | 71 | cpu_stl_data(env, addr, (uint32_t)env->gpr[reg]); |
b327c654 | 72 | } |
2f5a189c | 73 | addr = addr_add(env, addr, 4); |
ff4a62cd AJ |
74 | } |
75 | } | |
76 | ||
2f5a189c | 77 | void helper_lsw(CPUPPCState *env, target_ulong addr, uint32_t nb, uint32_t reg) |
dfbc799d AJ |
78 | { |
79 | int sh; | |
b327c654 | 80 | |
76db3ba4 | 81 | for (; nb > 3; nb -= 4) { |
2f5a189c | 82 | env->gpr[reg] = cpu_ldl_data(env, addr); |
dfbc799d | 83 | reg = (reg + 1) % 32; |
2f5a189c | 84 | addr = addr_add(env, addr, 4); |
dfbc799d AJ |
85 | } |
86 | if (unlikely(nb > 0)) { | |
87 | env->gpr[reg] = 0; | |
76db3ba4 | 88 | for (sh = 24; nb > 0; nb--, sh -= 8) { |
2f5a189c BS |
89 | env->gpr[reg] |= cpu_ldub_data(env, addr) << sh; |
90 | addr = addr_add(env, addr, 1); | |
dfbc799d AJ |
91 | } |
92 | } | |
93 | } | |
94 | /* PPC32 specification says we must generate an exception if | |
95 | * rA is in the range of registers to be loaded. | |
96 | * In an other hand, IBM says this is valid, but rA won't be loaded. | |
97 | * For now, I'll follow the spec... | |
98 | */ | |
2f5a189c BS |
99 | void helper_lswx(CPUPPCState *env, target_ulong addr, uint32_t reg, |
100 | uint32_t ra, uint32_t rb) | |
dfbc799d AJ |
101 | { |
102 | if (likely(xer_bc != 0)) { | |
488661ee AG |
103 | int num_used_regs = (xer_bc + 3) / 4; |
104 | if (unlikely((ra != 0 && reg < ra && (reg + num_used_regs) > ra) || | |
105 | (reg < rb && (reg + num_used_regs) > rb))) { | |
e5f17ac6 | 106 | helper_raise_exception_err(env, POWERPC_EXCP_PROGRAM, |
e06fcd75 AJ |
107 | POWERPC_EXCP_INVAL | |
108 | POWERPC_EXCP_INVAL_LSWX); | |
dfbc799d | 109 | } else { |
2f5a189c | 110 | helper_lsw(env, addr, xer_bc, reg); |
dfbc799d AJ |
111 | } |
112 | } | |
113 | } | |
114 | ||
2f5a189c BS |
115 | void helper_stsw(CPUPPCState *env, target_ulong addr, uint32_t nb, |
116 | uint32_t reg) | |
dfbc799d AJ |
117 | { |
118 | int sh; | |
b327c654 | 119 | |
76db3ba4 | 120 | for (; nb > 3; nb -= 4) { |
2f5a189c | 121 | cpu_stl_data(env, addr, env->gpr[reg]); |
dfbc799d | 122 | reg = (reg + 1) % 32; |
2f5a189c | 123 | addr = addr_add(env, addr, 4); |
dfbc799d AJ |
124 | } |
125 | if (unlikely(nb > 0)) { | |
a16b45e7 | 126 | for (sh = 24; nb > 0; nb--, sh -= 8) { |
2f5a189c BS |
127 | cpu_stb_data(env, addr, (env->gpr[reg] >> sh) & 0xFF); |
128 | addr = addr_add(env, addr, 1); | |
a16b45e7 | 129 | } |
dfbc799d AJ |
130 | } |
131 | } | |
132 | ||
2f5a189c | 133 | static void do_dcbz(CPUPPCState *env, target_ulong addr, int dcache_line_size) |
799a8c8d | 134 | { |
799a8c8d | 135 | int i; |
b327c654 BS |
136 | |
137 | addr &= ~(dcache_line_size - 1); | |
138 | for (i = 0; i < dcache_line_size; i += 4) { | |
2f5a189c | 139 | cpu_stl_data(env, addr + i, 0); |
799a8c8d | 140 | } |
b327c654 | 141 | if (env->reserve_addr == addr) { |
18b21a2f | 142 | env->reserve_addr = (target_ulong)-1ULL; |
b327c654 | 143 | } |
799a8c8d AJ |
144 | } |
145 | ||
8e33944f | 146 | void helper_dcbz(CPUPPCState *env, target_ulong addr, uint32_t is_dcbzl) |
799a8c8d | 147 | { |
8e33944f | 148 | int dcbz_size = env->dcache_line_size; |
799a8c8d | 149 | |
414f5d14 | 150 | #if defined(TARGET_PPC64) |
8e33944f AG |
151 | if (!is_dcbzl && |
152 | (env->excp_model == POWERPC_EXCP_970) && | |
153 | ((env->spr[SPR_970_HID5] >> 7) & 0x3) == 1) { | |
154 | dcbz_size = 32; | |
b327c654 | 155 | } |
8e33944f AG |
156 | #endif |
157 | ||
158 | /* XXX add e500mc support */ | |
159 | ||
160 | do_dcbz(env, addr, dcbz_size); | |
799a8c8d AJ |
161 | } |
162 | ||
2f5a189c | 163 | void helper_icbi(CPUPPCState *env, target_ulong addr) |
37d269df | 164 | { |
76db3ba4 | 165 | addr &= ~(env->dcache_line_size - 1); |
37d269df AJ |
166 | /* Invalidate one cache line : |
167 | * PowerPC specification says this is to be treated like a load | |
168 | * (not a fetch) by the MMU. To be sure it will be so, | |
169 | * do the load "by hand". | |
170 | */ | |
2f5a189c | 171 | cpu_ldl_data(env, addr); |
37d269df AJ |
172 | } |
173 | ||
b327c654 | 174 | /* XXX: to be tested */ |
2f5a189c BS |
175 | target_ulong helper_lscbx(CPUPPCState *env, target_ulong addr, uint32_t reg, |
176 | uint32_t ra, uint32_t rb) | |
bdb4b689 AJ |
177 | { |
178 | int i, c, d; | |
b327c654 | 179 | |
bdb4b689 AJ |
180 | d = 24; |
181 | for (i = 0; i < xer_bc; i++) { | |
2f5a189c BS |
182 | c = cpu_ldub_data(env, addr); |
183 | addr = addr_add(env, addr, 1); | |
bdb4b689 AJ |
184 | /* ra (if not 0) and rb are never modified */ |
185 | if (likely(reg != rb && (ra == 0 || reg != ra))) { | |
186 | env->gpr[reg] = (env->gpr[reg] & ~(0xFF << d)) | (c << d); | |
187 | } | |
b327c654 | 188 | if (unlikely(c == xer_cmp)) { |
bdb4b689 | 189 | break; |
b327c654 | 190 | } |
bdb4b689 AJ |
191 | if (likely(d != 0)) { |
192 | d -= 8; | |
193 | } else { | |
194 | d = 24; | |
195 | reg++; | |
196 | reg = reg & 0x1F; | |
197 | } | |
198 | } | |
199 | return i; | |
200 | } | |
201 | ||
d6a46fe8 AJ |
202 | /*****************************************************************************/ |
203 | /* Altivec extension helpers */ | |
e2542fe2 | 204 | #if defined(HOST_WORDS_BIGENDIAN) |
d6a46fe8 AJ |
205 | #define HI_IDX 0 |
206 | #define LO_IDX 1 | |
207 | #else | |
208 | #define HI_IDX 1 | |
209 | #define LO_IDX 0 | |
210 | #endif | |
211 | ||
e22c357b DK |
212 | /* We use msr_le to determine index ordering in a vector. However, |
213 | byteswapping is not simply controlled by msr_le. We also need to take | |
214 | into account endianness of the target. This is done for the little-endian | |
215 | PPC64 user-mode target. */ | |
216 | ||
cbfb6ae9 | 217 | #define LVE(name, access, swap, element) \ |
2f5a189c BS |
218 | void helper_##name(CPUPPCState *env, ppc_avr_t *r, \ |
219 | target_ulong addr) \ | |
cbfb6ae9 AJ |
220 | { \ |
221 | size_t n_elems = ARRAY_SIZE(r->element); \ | |
b327c654 | 222 | int adjust = HI_IDX*(n_elems - 1); \ |
cbfb6ae9 AJ |
223 | int sh = sizeof(r->element[0]) >> 1; \ |
224 | int index = (addr & 0xf) >> sh; \ | |
b327c654 | 225 | if (msr_le) { \ |
bbfb6f13 | 226 | index = n_elems - index - 1; \ |
e22c357b DK |
227 | } \ |
228 | \ | |
229 | if (needs_byteswap(env)) { \ | |
b327c654 | 230 | r->element[LO_IDX ? index : (adjust - index)] = \ |
2f5a189c | 231 | swap(access(env, addr)); \ |
b327c654 BS |
232 | } else { \ |
233 | r->element[LO_IDX ? index : (adjust - index)] = \ | |
2f5a189c | 234 | access(env, addr); \ |
b327c654 | 235 | } \ |
cbfb6ae9 AJ |
236 | } |
237 | #define I(x) (x) | |
2f5a189c BS |
238 | LVE(lvebx, cpu_ldub_data, I, u8) |
239 | LVE(lvehx, cpu_lduw_data, bswap16, u16) | |
240 | LVE(lvewx, cpu_ldl_data, bswap32, u32) | |
cbfb6ae9 AJ |
241 | #undef I |
242 | #undef LVE | |
243 | ||
b327c654 | 244 | #define STVE(name, access, swap, element) \ |
2f5a189c BS |
245 | void helper_##name(CPUPPCState *env, ppc_avr_t *r, \ |
246 | target_ulong addr) \ | |
b327c654 BS |
247 | { \ |
248 | size_t n_elems = ARRAY_SIZE(r->element); \ | |
249 | int adjust = HI_IDX * (n_elems - 1); \ | |
250 | int sh = sizeof(r->element[0]) >> 1; \ | |
251 | int index = (addr & 0xf) >> sh; \ | |
b327c654 | 252 | if (msr_le) { \ |
bbfb6f13 | 253 | index = n_elems - index - 1; \ |
e22c357b DK |
254 | } \ |
255 | \ | |
256 | if (needs_byteswap(env)) { \ | |
2f5a189c BS |
257 | access(env, addr, swap(r->element[LO_IDX ? index : \ |
258 | (adjust - index)])); \ | |
cbfb6ae9 | 259 | } else { \ |
2f5a189c BS |
260 | access(env, addr, r->element[LO_IDX ? index : \ |
261 | (adjust - index)]); \ | |
cbfb6ae9 AJ |
262 | } \ |
263 | } | |
264 | #define I(x) (x) | |
2f5a189c BS |
265 | STVE(stvebx, cpu_stb_data, I, u8) |
266 | STVE(stvehx, cpu_stw_data, bswap16, u16) | |
267 | STVE(stvewx, cpu_stl_data, bswap32, u32) | |
cbfb6ae9 AJ |
268 | #undef I |
269 | #undef LVE | |
270 | ||
d6a46fe8 AJ |
271 | #undef HI_IDX |
272 | #undef LO_IDX | |
0ff93d11 TM |
273 | |
274 | void helper_tbegin(CPUPPCState *env) | |
275 | { | |
276 | /* As a degenerate implementation, always fail tbegin. The reason | |
277 | * given is "Nesting overflow". The "persistent" bit is set, | |
278 | * providing a hint to the error handler to not retry. The TFIAR | |
279 | * captures the address of the failure, which is this tbegin | |
280 | * instruction. Instruction execution will continue with the | |
281 | * next instruction in memory, which is precisely what we want. | |
282 | */ | |
283 | ||
284 | env->spr[SPR_TEXASR] = | |
285 | (1ULL << TEXASR_FAILURE_PERSISTENT) | | |
286 | (1ULL << TEXASR_NESTING_OVERFLOW) | | |
287 | (msr_hv << TEXASR_PRIVILEGE_HV) | | |
288 | (msr_pr << TEXASR_PRIVILEGE_PR) | | |
289 | (1ULL << TEXASR_FAILURE_SUMMARY) | | |
290 | (1ULL << TEXASR_TFIAR_EXACT); | |
291 | env->spr[SPR_TFIAR] = env->nip | (msr_hv << 1) | msr_pr; | |
292 | env->spr[SPR_TFHAR] = env->nip + 4; | |
293 | env->crf[0] = 0xB; /* 0b1010 = transaction failure */ | |
294 | } |