2 * PowerPC memory access emulation helpers for QEMU.
4 * Copyright (c) 2003-2007 Jocelyn Mayer
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.
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.
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/>.
19 #include "qemu/osdep.h"
21 #include "exec/exec-all.h"
22 #include "qemu/host-utils.h"
23 #include "exec/helper-proto.h"
24 #include "helper_regs.h"
25 #include "exec/cpu_ldst.h"
31 static inline bool needs_byteswap(const CPUPPCState
*env
)
33 #if defined(TARGET_WORDS_BIGENDIAN)
40 /*****************************************************************************/
41 /* Memory load and stores */
43 static inline target_ulong
addr_add(CPUPPCState
*env
, target_ulong addr
,
46 #if defined(TARGET_PPC64)
47 if (!msr_is_64bit(env
, env
->msr
)) {
48 return (uint32_t)(addr
+ arg
);
56 void helper_lmw(CPUPPCState
*env
, target_ulong addr
, uint32_t reg
)
58 for (; reg
< 32; reg
++) {
59 if (needs_byteswap(env
)) {
60 env
->gpr
[reg
] = bswap32(cpu_ldl_data_ra(env
, addr
, GETPC()));
62 env
->gpr
[reg
] = cpu_ldl_data_ra(env
, addr
, GETPC());
64 addr
= addr_add(env
, addr
, 4);
68 void helper_stmw(CPUPPCState
*env
, target_ulong addr
, uint32_t reg
)
70 for (; reg
< 32; reg
++) {
71 if (needs_byteswap(env
)) {
72 cpu_stl_data_ra(env
, addr
, bswap32((uint32_t)env
->gpr
[reg
]),
75 cpu_stl_data_ra(env
, addr
, (uint32_t)env
->gpr
[reg
], GETPC());
77 addr
= addr_add(env
, addr
, 4);
81 static void do_lsw(CPUPPCState
*env
, target_ulong addr
, uint32_t nb
,
82 uint32_t reg
, uintptr_t raddr
)
86 for (; nb
> 3; nb
-= 4) {
87 env
->gpr
[reg
] = cpu_ldl_data_ra(env
, addr
, raddr
);
89 addr
= addr_add(env
, addr
, 4);
91 if (unlikely(nb
> 0)) {
93 for (sh
= 24; nb
> 0; nb
--, sh
-= 8) {
94 env
->gpr
[reg
] |= cpu_ldub_data_ra(env
, addr
, raddr
) << sh
;
95 addr
= addr_add(env
, addr
, 1);
100 void helper_lsw(CPUPPCState
*env
, target_ulong addr
, uint32_t nb
, uint32_t reg
)
102 do_lsw(env
, addr
, nb
, reg
, GETPC());
105 /* PPC32 specification says we must generate an exception if
106 * rA is in the range of registers to be loaded.
107 * In an other hand, IBM says this is valid, but rA won't be loaded.
108 * For now, I'll follow the spec...
110 void helper_lswx(CPUPPCState
*env
, target_ulong addr
, uint32_t reg
,
111 uint32_t ra
, uint32_t rb
)
113 if (likely(xer_bc
!= 0)) {
114 int num_used_regs
= DIV_ROUND_UP(xer_bc
, 4);
115 if (unlikely((ra
!= 0 && lsw_reg_in_range(reg
, num_used_regs
, ra
)) ||
116 lsw_reg_in_range(reg
, num_used_regs
, rb
))) {
117 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
119 POWERPC_EXCP_INVAL_LSWX
, GETPC());
121 do_lsw(env
, addr
, xer_bc
, reg
, GETPC());
126 void helper_stsw(CPUPPCState
*env
, target_ulong addr
, uint32_t nb
,
131 for (; nb
> 3; nb
-= 4) {
132 cpu_stl_data_ra(env
, addr
, env
->gpr
[reg
], GETPC());
133 reg
= (reg
+ 1) % 32;
134 addr
= addr_add(env
, addr
, 4);
136 if (unlikely(nb
> 0)) {
137 for (sh
= 24; nb
> 0; nb
--, sh
-= 8) {
138 cpu_stb_data_ra(env
, addr
, (env
->gpr
[reg
] >> sh
) & 0xFF, GETPC());
139 addr
= addr_add(env
, addr
, 1);
144 void helper_dcbz(CPUPPCState
*env
, target_ulong addr
, uint32_t opcode
)
146 target_ulong mask
, dcbz_size
= env
->dcache_line_size
;
150 #if defined(TARGET_PPC64)
151 /* Check for dcbz vs dcbzl on 970 */
152 if (env
->excp_model
== POWERPC_EXCP_970
&&
153 !(opcode
& 0x00200000) && ((env
->spr
[SPR_970_HID5
] >> 7) & 0x3) == 1) {
159 mask
= ~(dcbz_size
- 1);
162 /* Check reservation */
163 if ((env
->reserve_addr
& mask
) == (addr
& mask
)) {
164 env
->reserve_addr
= (target_ulong
)-1ULL;
167 /* Try fast path translate */
168 haddr
= tlb_vaddr_to_host(env
, addr
, MMU_DATA_STORE
, env
->dmmu_idx
);
170 memset(haddr
, 0, dcbz_size
);
173 for (i
= 0; i
< dcbz_size
; i
+= 8) {
174 cpu_stq_data_ra(env
, addr
+ i
, 0, GETPC());
179 void helper_icbi(CPUPPCState
*env
, target_ulong addr
)
181 addr
&= ~(env
->dcache_line_size
- 1);
182 /* Invalidate one cache line :
183 * PowerPC specification says this is to be treated like a load
184 * (not a fetch) by the MMU. To be sure it will be so,
185 * do the load "by hand".
187 cpu_ldl_data_ra(env
, addr
, GETPC());
190 /* XXX: to be tested */
191 target_ulong
helper_lscbx(CPUPPCState
*env
, target_ulong addr
, uint32_t reg
,
192 uint32_t ra
, uint32_t rb
)
197 for (i
= 0; i
< xer_bc
; i
++) {
198 c
= cpu_ldub_data_ra(env
, addr
, GETPC());
199 addr
= addr_add(env
, addr
, 1);
200 /* ra (if not 0) and rb are never modified */
201 if (likely(reg
!= rb
&& (ra
== 0 || reg
!= ra
))) {
202 env
->gpr
[reg
] = (env
->gpr
[reg
] & ~(0xFF << d
)) | (c
<< d
);
204 if (unlikely(c
== xer_cmp
)) {
207 if (likely(d
!= 0)) {
218 #if defined(TARGET_PPC64) && defined(CONFIG_ATOMIC128)
219 uint64_t helper_lq_le_parallel(CPUPPCState
*env
, target_ulong addr
,
222 Int128 ret
= helper_atomic_ldo_le_mmu(env
, addr
, opidx
, GETPC());
223 env
->retxh
= int128_gethi(ret
);
224 return int128_getlo(ret
);
227 uint64_t helper_lq_be_parallel(CPUPPCState
*env
, target_ulong addr
,
230 Int128 ret
= helper_atomic_ldo_be_mmu(env
, addr
, opidx
, GETPC());
231 env
->retxh
= int128_gethi(ret
);
232 return int128_getlo(ret
);
235 void helper_stq_le_parallel(CPUPPCState
*env
, target_ulong addr
,
236 uint64_t lo
, uint64_t hi
, uint32_t opidx
)
238 Int128 val
= int128_make128(lo
, hi
);
239 helper_atomic_sto_le_mmu(env
, addr
, val
, opidx
, GETPC());
242 void helper_stq_be_parallel(CPUPPCState
*env
, target_ulong addr
,
243 uint64_t lo
, uint64_t hi
, uint32_t opidx
)
245 Int128 val
= int128_make128(lo
, hi
);
246 helper_atomic_sto_be_mmu(env
, addr
, val
, opidx
, GETPC());
249 uint32_t helper_stqcx_le_parallel(CPUPPCState
*env
, target_ulong addr
,
250 uint64_t new_lo
, uint64_t new_hi
,
253 bool success
= false;
255 if (likely(addr
== env
->reserve_addr
)) {
256 Int128 oldv
, cmpv
, newv
;
258 cmpv
= int128_make128(env
->reserve_val2
, env
->reserve_val
);
259 newv
= int128_make128(new_lo
, new_hi
);
260 oldv
= helper_atomic_cmpxchgo_le_mmu(env
, addr
, cmpv
, newv
,
262 success
= int128_eq(oldv
, cmpv
);
264 env
->reserve_addr
= -1;
265 return env
->so
+ success
* CRF_EQ_BIT
;
268 uint32_t helper_stqcx_be_parallel(CPUPPCState
*env
, target_ulong addr
,
269 uint64_t new_lo
, uint64_t new_hi
,
272 bool success
= false;
274 if (likely(addr
== env
->reserve_addr
)) {
275 Int128 oldv
, cmpv
, newv
;
277 cmpv
= int128_make128(env
->reserve_val2
, env
->reserve_val
);
278 newv
= int128_make128(new_lo
, new_hi
);
279 oldv
= helper_atomic_cmpxchgo_be_mmu(env
, addr
, cmpv
, newv
,
281 success
= int128_eq(oldv
, cmpv
);
283 env
->reserve_addr
= -1;
284 return env
->so
+ success
* CRF_EQ_BIT
;
288 /*****************************************************************************/
289 /* Altivec extension helpers */
290 #if defined(HOST_WORDS_BIGENDIAN)
298 /* We use msr_le to determine index ordering in a vector. However,
299 byteswapping is not simply controlled by msr_le. We also need to take
300 into account endianness of the target. This is done for the little-endian
301 PPC64 user-mode target. */
303 #define LVE(name, access, swap, element) \
304 void helper_##name(CPUPPCState *env, ppc_avr_t *r, \
307 size_t n_elems = ARRAY_SIZE(r->element); \
308 int adjust = HI_IDX*(n_elems - 1); \
309 int sh = sizeof(r->element[0]) >> 1; \
310 int index = (addr & 0xf) >> sh; \
312 index = n_elems - index - 1; \
315 if (needs_byteswap(env)) { \
316 r->element[LO_IDX ? index : (adjust - index)] = \
317 swap(access(env, addr, GETPC())); \
319 r->element[LO_IDX ? index : (adjust - index)] = \
320 access(env, addr, GETPC()); \
324 LVE(lvebx
, cpu_ldub_data_ra
, I
, u8
)
325 LVE(lvehx
, cpu_lduw_data_ra
, bswap16
, u16
)
326 LVE(lvewx
, cpu_ldl_data_ra
, bswap32
, u32
)
330 #define STVE(name, access, swap, element) \
331 void helper_##name(CPUPPCState *env, ppc_avr_t *r, \
334 size_t n_elems = ARRAY_SIZE(r->element); \
335 int adjust = HI_IDX * (n_elems - 1); \
336 int sh = sizeof(r->element[0]) >> 1; \
337 int index = (addr & 0xf) >> sh; \
339 index = n_elems - index - 1; \
342 if (needs_byteswap(env)) { \
343 access(env, addr, swap(r->element[LO_IDX ? index : \
344 (adjust - index)]), \
347 access(env, addr, r->element[LO_IDX ? index : \
348 (adjust - index)], GETPC()); \
352 STVE(stvebx
, cpu_stb_data_ra
, I
, u8
)
353 STVE(stvehx
, cpu_stw_data_ra
, bswap16
, u16
)
354 STVE(stvewx
, cpu_stl_data_ra
, bswap32
, u32
)
359 #define GET_NB(rb) ((rb >> 56) & 0xFF)
361 #define VSX_LXVL(name, lj) \
362 void helper_##name(CPUPPCState *env, target_ulong addr, \
363 target_ulong xt_num, target_ulong rb) \
367 uint64_t nb = GET_NB(rb); \
369 xt.s128 = int128_zero(); \
371 nb = (nb >= 16) ? 16 : nb; \
372 if (msr_le && !lj) { \
373 for (i = 16; i > 16 - nb; i--) { \
374 xt.VsrB(i - 1) = cpu_ldub_data_ra(env, addr, GETPC()); \
375 addr = addr_add(env, addr, 1); \
378 for (i = 0; i < nb; i++) { \
379 xt.VsrB(i) = cpu_ldub_data_ra(env, addr, GETPC()); \
380 addr = addr_add(env, addr, 1); \
384 putVSR(xt_num, &xt, env); \
391 #define VSX_STXVL(name, lj) \
392 void helper_##name(CPUPPCState *env, target_ulong addr, \
393 target_ulong xt_num, target_ulong rb) \
397 target_ulong nb = GET_NB(rb); \
402 getVSR(xt_num, &xt, env); \
403 nb = (nb >= 16) ? 16 : nb; \
404 if (msr_le && !lj) { \
405 for (i = 16; i > 16 - nb; i--) { \
406 cpu_stb_data_ra(env, addr, xt.VsrB(i - 1), GETPC()); \
407 addr = addr_add(env, addr, 1); \
410 for (i = 0; i < nb; i++) { \
411 cpu_stb_data_ra(env, addr, xt.VsrB(i), GETPC()); \
412 addr = addr_add(env, addr, 1); \
421 #endif /* TARGET_PPC64 */
426 void helper_tbegin(CPUPPCState
*env
)
428 /* As a degenerate implementation, always fail tbegin. The reason
429 * given is "Nesting overflow". The "persistent" bit is set,
430 * providing a hint to the error handler to not retry. The TFIAR
431 * captures the address of the failure, which is this tbegin
432 * instruction. Instruction execution will continue with the
433 * next instruction in memory, which is precisely what we want.
436 env
->spr
[SPR_TEXASR
] =
437 (1ULL << TEXASR_FAILURE_PERSISTENT
) |
438 (1ULL << TEXASR_NESTING_OVERFLOW
) |
439 (msr_hv
<< TEXASR_PRIVILEGE_HV
) |
440 (msr_pr
<< TEXASR_PRIVILEGE_PR
) |
441 (1ULL << TEXASR_FAILURE_SUMMARY
) |
442 (1ULL << TEXASR_TFIAR_EXACT
);
443 env
->spr
[SPR_TFIAR
] = env
->nip
| (msr_hv
<< 1) | msr_pr
;
444 env
->spr
[SPR_TFHAR
] = env
->nip
+ 4;
445 env
->crf
[0] = 0xB; /* 0b1010 = transaction failure */