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"
28 #include "qemu/atomic128.h"
30 /* #define DEBUG_OP */
32 static inline bool needs_byteswap(const CPUPPCState
*env
)
34 #if defined(TARGET_WORDS_BIGENDIAN)
41 /*****************************************************************************/
42 /* Memory load and stores */
44 static inline target_ulong
addr_add(CPUPPCState
*env
, target_ulong addr
,
47 #if defined(TARGET_PPC64)
48 if (!msr_is_64bit(env
, env
->msr
)) {
49 return (uint32_t)(addr
+ arg
);
57 void helper_lmw(CPUPPCState
*env
, target_ulong addr
, uint32_t reg
)
59 for (; reg
< 32; reg
++) {
60 if (needs_byteswap(env
)) {
61 env
->gpr
[reg
] = bswap32(cpu_ldl_data_ra(env
, addr
, GETPC()));
63 env
->gpr
[reg
] = cpu_ldl_data_ra(env
, addr
, GETPC());
65 addr
= addr_add(env
, addr
, 4);
69 void helper_stmw(CPUPPCState
*env
, target_ulong addr
, uint32_t reg
)
71 for (; reg
< 32; reg
++) {
72 if (needs_byteswap(env
)) {
73 cpu_stl_data_ra(env
, addr
, bswap32((uint32_t)env
->gpr
[reg
]),
76 cpu_stl_data_ra(env
, addr
, (uint32_t)env
->gpr
[reg
], GETPC());
78 addr
= addr_add(env
, addr
, 4);
82 static void do_lsw(CPUPPCState
*env
, target_ulong addr
, uint32_t nb
,
83 uint32_t reg
, uintptr_t raddr
)
87 for (; nb
> 3; nb
-= 4) {
88 env
->gpr
[reg
] = cpu_ldl_data_ra(env
, addr
, raddr
);
90 addr
= addr_add(env
, addr
, 4);
92 if (unlikely(nb
> 0)) {
94 for (sh
= 24; nb
> 0; nb
--, sh
-= 8) {
95 env
->gpr
[reg
] |= cpu_ldub_data_ra(env
, addr
, raddr
) << sh
;
96 addr
= addr_add(env
, addr
, 1);
101 void helper_lsw(CPUPPCState
*env
, target_ulong addr
, uint32_t nb
, uint32_t reg
)
103 do_lsw(env
, addr
, nb
, reg
, GETPC());
107 * PPC32 specification says we must generate an exception if rA is in
108 * the range of registers to be loaded. In an other hand, IBM says
109 * this is valid, but rA won't be loaded. For now, I'll follow the
112 void helper_lswx(CPUPPCState
*env
, target_ulong addr
, uint32_t reg
,
113 uint32_t ra
, uint32_t rb
)
115 if (likely(xer_bc
!= 0)) {
116 int num_used_regs
= DIV_ROUND_UP(xer_bc
, 4);
117 if (unlikely((ra
!= 0 && lsw_reg_in_range(reg
, num_used_regs
, ra
)) ||
118 lsw_reg_in_range(reg
, num_used_regs
, rb
))) {
119 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
121 POWERPC_EXCP_INVAL_LSWX
, GETPC());
123 do_lsw(env
, addr
, xer_bc
, reg
, GETPC());
128 void helper_stsw(CPUPPCState
*env
, target_ulong addr
, uint32_t nb
,
133 for (; nb
> 3; nb
-= 4) {
134 cpu_stl_data_ra(env
, addr
, env
->gpr
[reg
], GETPC());
135 reg
= (reg
+ 1) % 32;
136 addr
= addr_add(env
, addr
, 4);
138 if (unlikely(nb
> 0)) {
139 for (sh
= 24; nb
> 0; nb
--, sh
-= 8) {
140 cpu_stb_data_ra(env
, addr
, (env
->gpr
[reg
] >> sh
) & 0xFF, GETPC());
141 addr
= addr_add(env
, addr
, 1);
146 static void dcbz_common(CPUPPCState
*env
, target_ulong addr
,
147 uint32_t opcode
, bool epid
, uintptr_t retaddr
)
149 target_ulong mask
, dcbz_size
= env
->dcache_line_size
;
152 int mmu_idx
= epid
? PPC_TLB_EPID_STORE
: env
->dmmu_idx
;
154 #if defined(TARGET_PPC64)
155 /* Check for dcbz vs dcbzl on 970 */
156 if (env
->excp_model
== POWERPC_EXCP_970
&&
157 !(opcode
& 0x00200000) && ((env
->spr
[SPR_970_HID5
] >> 7) & 0x3) == 1) {
163 mask
= ~(dcbz_size
- 1);
166 /* Check reservation */
167 if ((env
->reserve_addr
& mask
) == (addr
& mask
)) {
168 env
->reserve_addr
= (target_ulong
)-1ULL;
171 /* Try fast path translate */
172 haddr
= tlb_vaddr_to_host(env
, addr
, MMU_DATA_STORE
, mmu_idx
);
174 memset(haddr
, 0, dcbz_size
);
177 for (i
= 0; i
< dcbz_size
; i
+= 8) {
179 #if !defined(CONFIG_USER_ONLY)
180 /* Does not make sense on USER_ONLY config */
181 cpu_stq_eps_ra(env
, addr
+ i
, 0, retaddr
);
184 cpu_stq_data_ra(env
, addr
+ i
, 0, retaddr
);
190 void helper_dcbz(CPUPPCState
*env
, target_ulong addr
, uint32_t opcode
)
192 dcbz_common(env
, addr
, opcode
, false, GETPC());
195 void helper_dcbzep(CPUPPCState
*env
, target_ulong addr
, uint32_t opcode
)
197 dcbz_common(env
, addr
, opcode
, true, GETPC());
200 void helper_icbi(CPUPPCState
*env
, target_ulong addr
)
202 addr
&= ~(env
->dcache_line_size
- 1);
204 * Invalidate one cache line :
205 * PowerPC specification says this is to be treated like a load
206 * (not a fetch) by the MMU. To be sure it will be so,
207 * do the load "by hand".
209 cpu_ldl_data_ra(env
, addr
, GETPC());
212 void helper_icbiep(CPUPPCState
*env
, target_ulong addr
)
214 #if !defined(CONFIG_USER_ONLY)
215 /* See comments above */
216 addr
&= ~(env
->dcache_line_size
- 1);
217 cpu_ldl_epl_ra(env
, addr
, GETPC());
221 /* XXX: to be tested */
222 target_ulong
helper_lscbx(CPUPPCState
*env
, target_ulong addr
, uint32_t reg
,
223 uint32_t ra
, uint32_t rb
)
228 for (i
= 0; i
< xer_bc
; i
++) {
229 c
= cpu_ldub_data_ra(env
, addr
, GETPC());
230 addr
= addr_add(env
, addr
, 1);
231 /* ra (if not 0) and rb are never modified */
232 if (likely(reg
!= rb
&& (ra
== 0 || reg
!= ra
))) {
233 env
->gpr
[reg
] = (env
->gpr
[reg
] & ~(0xFF << d
)) | (c
<< d
);
235 if (unlikely(c
== xer_cmp
)) {
238 if (likely(d
!= 0)) {
250 uint64_t helper_lq_le_parallel(CPUPPCState
*env
, target_ulong addr
,
255 /* We will have raised EXCP_ATOMIC from the translator. */
256 assert(HAVE_ATOMIC128
);
257 ret
= helper_atomic_ldo_le_mmu(env
, addr
, opidx
, GETPC());
258 env
->retxh
= int128_gethi(ret
);
259 return int128_getlo(ret
);
262 uint64_t helper_lq_be_parallel(CPUPPCState
*env
, target_ulong addr
,
267 /* We will have raised EXCP_ATOMIC from the translator. */
268 assert(HAVE_ATOMIC128
);
269 ret
= helper_atomic_ldo_be_mmu(env
, addr
, opidx
, GETPC());
270 env
->retxh
= int128_gethi(ret
);
271 return int128_getlo(ret
);
274 void helper_stq_le_parallel(CPUPPCState
*env
, target_ulong addr
,
275 uint64_t lo
, uint64_t hi
, uint32_t opidx
)
279 /* We will have raised EXCP_ATOMIC from the translator. */
280 assert(HAVE_ATOMIC128
);
281 val
= int128_make128(lo
, hi
);
282 helper_atomic_sto_le_mmu(env
, addr
, val
, opidx
, GETPC());
285 void helper_stq_be_parallel(CPUPPCState
*env
, target_ulong addr
,
286 uint64_t lo
, uint64_t hi
, uint32_t opidx
)
290 /* We will have raised EXCP_ATOMIC from the translator. */
291 assert(HAVE_ATOMIC128
);
292 val
= int128_make128(lo
, hi
);
293 helper_atomic_sto_be_mmu(env
, addr
, val
, opidx
, GETPC());
296 uint32_t helper_stqcx_le_parallel(CPUPPCState
*env
, target_ulong addr
,
297 uint64_t new_lo
, uint64_t new_hi
,
300 bool success
= false;
302 /* We will have raised EXCP_ATOMIC from the translator. */
303 assert(HAVE_CMPXCHG128
);
305 if (likely(addr
== env
->reserve_addr
)) {
306 Int128 oldv
, cmpv
, newv
;
308 cmpv
= int128_make128(env
->reserve_val2
, env
->reserve_val
);
309 newv
= int128_make128(new_lo
, new_hi
);
310 oldv
= helper_atomic_cmpxchgo_le_mmu(env
, addr
, cmpv
, newv
,
312 success
= int128_eq(oldv
, cmpv
);
314 env
->reserve_addr
= -1;
315 return env
->so
+ success
* CRF_EQ_BIT
;
318 uint32_t helper_stqcx_be_parallel(CPUPPCState
*env
, target_ulong addr
,
319 uint64_t new_lo
, uint64_t new_hi
,
322 bool success
= false;
324 /* We will have raised EXCP_ATOMIC from the translator. */
325 assert(HAVE_CMPXCHG128
);
327 if (likely(addr
== env
->reserve_addr
)) {
328 Int128 oldv
, cmpv
, newv
;
330 cmpv
= int128_make128(env
->reserve_val2
, env
->reserve_val
);
331 newv
= int128_make128(new_lo
, new_hi
);
332 oldv
= helper_atomic_cmpxchgo_be_mmu(env
, addr
, cmpv
, newv
,
334 success
= int128_eq(oldv
, cmpv
);
336 env
->reserve_addr
= -1;
337 return env
->so
+ success
* CRF_EQ_BIT
;
341 /*****************************************************************************/
342 /* Altivec extension helpers */
343 #if defined(HOST_WORDS_BIGENDIAN)
352 * We use msr_le to determine index ordering in a vector. However,
353 * byteswapping is not simply controlled by msr_le. We also need to
354 * take into account endianness of the target. This is done for the
355 * little-endian PPC64 user-mode target.
358 #define LVE(name, access, swap, element) \
359 void helper_##name(CPUPPCState *env, ppc_avr_t *r, \
362 size_t n_elems = ARRAY_SIZE(r->element); \
363 int adjust = HI_IDX * (n_elems - 1); \
364 int sh = sizeof(r->element[0]) >> 1; \
365 int index = (addr & 0xf) >> sh; \
367 index = n_elems - index - 1; \
370 if (needs_byteswap(env)) { \
371 r->element[LO_IDX ? index : (adjust - index)] = \
372 swap(access(env, addr, GETPC())); \
374 r->element[LO_IDX ? index : (adjust - index)] = \
375 access(env, addr, GETPC()); \
379 LVE(lvebx
, cpu_ldub_data_ra
, I
, u8
)
380 LVE(lvehx
, cpu_lduw_data_ra
, bswap16
, u16
)
381 LVE(lvewx
, cpu_ldl_data_ra
, bswap32
, u32
)
385 #define STVE(name, access, swap, element) \
386 void helper_##name(CPUPPCState *env, ppc_avr_t *r, \
389 size_t n_elems = ARRAY_SIZE(r->element); \
390 int adjust = HI_IDX * (n_elems - 1); \
391 int sh = sizeof(r->element[0]) >> 1; \
392 int index = (addr & 0xf) >> sh; \
394 index = n_elems - index - 1; \
397 if (needs_byteswap(env)) { \
398 access(env, addr, swap(r->element[LO_IDX ? index : \
399 (adjust - index)]), \
402 access(env, addr, r->element[LO_IDX ? index : \
403 (adjust - index)], GETPC()); \
407 STVE(stvebx
, cpu_stb_data_ra
, I
, u8
)
408 STVE(stvehx
, cpu_stw_data_ra
, bswap16
, u16
)
409 STVE(stvewx
, cpu_stl_data_ra
, bswap32
, u32
)
414 #define GET_NB(rb) ((rb >> 56) & 0xFF)
416 #define VSX_LXVL(name, lj) \
417 void helper_##name(CPUPPCState *env, target_ulong addr, \
418 target_ulong xt_num, target_ulong rb) \
422 uint64_t nb = GET_NB(rb); \
424 xt.s128 = int128_zero(); \
426 nb = (nb >= 16) ? 16 : nb; \
427 if (msr_le && !lj) { \
428 for (i = 16; i > 16 - nb; i--) { \
429 xt.VsrB(i - 1) = cpu_ldub_data_ra(env, addr, GETPC()); \
430 addr = addr_add(env, addr, 1); \
433 for (i = 0; i < nb; i++) { \
434 xt.VsrB(i) = cpu_ldub_data_ra(env, addr, GETPC()); \
435 addr = addr_add(env, addr, 1); \
439 putVSR(xt_num, &xt, env); \
446 #define VSX_STXVL(name, lj) \
447 void helper_##name(CPUPPCState *env, target_ulong addr, \
448 target_ulong xt_num, target_ulong rb) \
452 target_ulong nb = GET_NB(rb); \
457 getVSR(xt_num, &xt, env); \
458 nb = (nb >= 16) ? 16 : nb; \
459 if (msr_le && !lj) { \
460 for (i = 16; i > 16 - nb; i--) { \
461 cpu_stb_data_ra(env, addr, xt.VsrB(i - 1), GETPC()); \
462 addr = addr_add(env, addr, 1); \
465 for (i = 0; i < nb; i++) { \
466 cpu_stb_data_ra(env, addr, xt.VsrB(i), GETPC()); \
467 addr = addr_add(env, addr, 1); \
476 #endif /* TARGET_PPC64 */
481 void helper_tbegin(CPUPPCState
*env
)
484 * As a degenerate implementation, always fail tbegin. The reason
485 * given is "Nesting overflow". The "persistent" bit is set,
486 * providing a hint to the error handler to not retry. The TFIAR
487 * captures the address of the failure, which is this tbegin
488 * instruction. Instruction execution will continue with the next
489 * instruction in memory, which is precisely what we want.
492 env
->spr
[SPR_TEXASR
] =
493 (1ULL << TEXASR_FAILURE_PERSISTENT
) |
494 (1ULL << TEXASR_NESTING_OVERFLOW
) |
495 (msr_hv
<< TEXASR_PRIVILEGE_HV
) |
496 (msr_pr
<< TEXASR_PRIVILEGE_PR
) |
497 (1ULL << TEXASR_FAILURE_SUMMARY
) |
498 (1ULL << TEXASR_TFIAR_EXACT
);
499 env
->spr
[SPR_TFIAR
] = env
->nip
| (msr_hv
<< 1) | msr_pr
;
500 env
->spr
[SPR_TFHAR
] = env
->nip
+ 4;
501 env
->crf
[0] = 0xB; /* 0b1010 = transaction failure */