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 "qemu/host-utils.h"
22 #include "exec/helper-proto.h"
24 #include "helper_regs.h"
25 #include "exec/cpu_ldst.h"
29 static inline bool needs_byteswap(const CPUPPCState
*env
)
31 #if defined(TARGET_WORDS_BIGENDIAN)
38 /*****************************************************************************/
39 /* Memory load and stores */
41 static inline target_ulong
addr_add(CPUPPCState
*env
, target_ulong addr
,
44 #if defined(TARGET_PPC64)
45 if (!msr_is_64bit(env
, env
->msr
)) {
46 return (uint32_t)(addr
+ arg
);
54 void helper_lmw(CPUPPCState
*env
, target_ulong addr
, uint32_t reg
)
56 for (; reg
< 32; reg
++) {
57 if (needs_byteswap(env
)) {
58 env
->gpr
[reg
] = bswap32(cpu_ldl_data(env
, addr
));
60 env
->gpr
[reg
] = cpu_ldl_data(env
, addr
);
62 addr
= addr_add(env
, addr
, 4);
66 void helper_stmw(CPUPPCState
*env
, target_ulong addr
, uint32_t reg
)
68 for (; reg
< 32; reg
++) {
69 if (needs_byteswap(env
)) {
70 cpu_stl_data(env
, addr
, bswap32((uint32_t)env
->gpr
[reg
]));
72 cpu_stl_data(env
, addr
, (uint32_t)env
->gpr
[reg
]);
74 addr
= addr_add(env
, addr
, 4);
78 void helper_lsw(CPUPPCState
*env
, target_ulong addr
, uint32_t nb
, uint32_t reg
)
82 for (; nb
> 3; nb
-= 4) {
83 env
->gpr
[reg
] = cpu_ldl_data(env
, addr
);
85 addr
= addr_add(env
, addr
, 4);
87 if (unlikely(nb
> 0)) {
89 for (sh
= 24; nb
> 0; nb
--, sh
-= 8) {
90 env
->gpr
[reg
] |= cpu_ldub_data(env
, addr
) << sh
;
91 addr
= addr_add(env
, addr
, 1);
95 /* PPC32 specification says we must generate an exception if
96 * rA is in the range of registers to be loaded.
97 * In an other hand, IBM says this is valid, but rA won't be loaded.
98 * For now, I'll follow the spec...
100 void helper_lswx(CPUPPCState
*env
, target_ulong addr
, uint32_t reg
,
101 uint32_t ra
, uint32_t rb
)
103 if (likely(xer_bc
!= 0)) {
104 int num_used_regs
= (xer_bc
+ 3) / 4;
105 if (unlikely((ra
!= 0 && lsw_reg_in_range(reg
, num_used_regs
, ra
)) ||
106 lsw_reg_in_range(reg
, num_used_regs
, rb
))) {
107 env
->nip
+= 4; /* Compensate the "nip - 4" from gen_lswx() */
108 helper_raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
110 POWERPC_EXCP_INVAL_LSWX
);
112 helper_lsw(env
, addr
, xer_bc
, reg
);
117 void helper_stsw(CPUPPCState
*env
, target_ulong addr
, uint32_t nb
,
122 for (; nb
> 3; nb
-= 4) {
123 cpu_stl_data(env
, addr
, env
->gpr
[reg
]);
124 reg
= (reg
+ 1) % 32;
125 addr
= addr_add(env
, addr
, 4);
127 if (unlikely(nb
> 0)) {
128 for (sh
= 24; nb
> 0; nb
--, sh
-= 8) {
129 cpu_stb_data(env
, addr
, (env
->gpr
[reg
] >> sh
) & 0xFF);
130 addr
= addr_add(env
, addr
, 1);
135 static void do_dcbz(CPUPPCState
*env
, target_ulong addr
, int dcache_line_size
)
139 addr
&= ~(dcache_line_size
- 1);
140 for (i
= 0; i
< dcache_line_size
; i
+= 4) {
141 cpu_stl_data(env
, addr
+ i
, 0);
143 if (env
->reserve_addr
== addr
) {
144 env
->reserve_addr
= (target_ulong
)-1ULL;
148 void helper_dcbz(CPUPPCState
*env
, target_ulong addr
, uint32_t is_dcbzl
)
150 int dcbz_size
= env
->dcache_line_size
;
152 #if defined(TARGET_PPC64)
154 (env
->excp_model
== POWERPC_EXCP_970
) &&
155 ((env
->spr
[SPR_970_HID5
] >> 7) & 0x3) == 1) {
160 /* XXX add e500mc support */
162 do_dcbz(env
, addr
, dcbz_size
);
165 void helper_icbi(CPUPPCState
*env
, target_ulong addr
)
167 addr
&= ~(env
->dcache_line_size
- 1);
168 /* Invalidate one cache line :
169 * PowerPC specification says this is to be treated like a load
170 * (not a fetch) by the MMU. To be sure it will be so,
171 * do the load "by hand".
173 cpu_ldl_data(env
, addr
);
176 /* XXX: to be tested */
177 target_ulong
helper_lscbx(CPUPPCState
*env
, target_ulong addr
, uint32_t reg
,
178 uint32_t ra
, uint32_t rb
)
183 for (i
= 0; i
< xer_bc
; i
++) {
184 c
= cpu_ldub_data(env
, addr
);
185 addr
= addr_add(env
, addr
, 1);
186 /* ra (if not 0) and rb are never modified */
187 if (likely(reg
!= rb
&& (ra
== 0 || reg
!= ra
))) {
188 env
->gpr
[reg
] = (env
->gpr
[reg
] & ~(0xFF << d
)) | (c
<< d
);
190 if (unlikely(c
== xer_cmp
)) {
193 if (likely(d
!= 0)) {
204 /*****************************************************************************/
205 /* Altivec extension helpers */
206 #if defined(HOST_WORDS_BIGENDIAN)
214 /* We use msr_le to determine index ordering in a vector. However,
215 byteswapping is not simply controlled by msr_le. We also need to take
216 into account endianness of the target. This is done for the little-endian
217 PPC64 user-mode target. */
219 #define LVE(name, access, swap, element) \
220 void helper_##name(CPUPPCState *env, ppc_avr_t *r, \
223 size_t n_elems = ARRAY_SIZE(r->element); \
224 int adjust = HI_IDX*(n_elems - 1); \
225 int sh = sizeof(r->element[0]) >> 1; \
226 int index = (addr & 0xf) >> sh; \
228 index = n_elems - index - 1; \
231 if (needs_byteswap(env)) { \
232 r->element[LO_IDX ? index : (adjust - index)] = \
233 swap(access(env, addr)); \
235 r->element[LO_IDX ? index : (adjust - index)] = \
240 LVE(lvebx
, cpu_ldub_data
, I
, u8
)
241 LVE(lvehx
, cpu_lduw_data
, bswap16
, u16
)
242 LVE(lvewx
, cpu_ldl_data
, bswap32
, u32
)
246 #define STVE(name, access, swap, element) \
247 void helper_##name(CPUPPCState *env, ppc_avr_t *r, \
250 size_t n_elems = ARRAY_SIZE(r->element); \
251 int adjust = HI_IDX * (n_elems - 1); \
252 int sh = sizeof(r->element[0]) >> 1; \
253 int index = (addr & 0xf) >> sh; \
255 index = n_elems - index - 1; \
258 if (needs_byteswap(env)) { \
259 access(env, addr, swap(r->element[LO_IDX ? index : \
260 (adjust - index)])); \
262 access(env, addr, r->element[LO_IDX ? index : \
263 (adjust - index)]); \
267 STVE(stvebx
, cpu_stb_data
, I
, u8
)
268 STVE(stvehx
, cpu_stw_data
, bswap16
, u16
)
269 STVE(stvewx
, cpu_stl_data
, bswap32
, u32
)
276 void helper_tbegin(CPUPPCState
*env
)
278 /* As a degenerate implementation, always fail tbegin. The reason
279 * given is "Nesting overflow". The "persistent" bit is set,
280 * providing a hint to the error handler to not retry. The TFIAR
281 * captures the address of the failure, which is this tbegin
282 * instruction. Instruction execution will continue with the
283 * next instruction in memory, which is precisely what we want.
286 env
->spr
[SPR_TEXASR
] =
287 (1ULL << TEXASR_FAILURE_PERSISTENT
) |
288 (1ULL << TEXASR_NESTING_OVERFLOW
) |
289 (msr_hv
<< TEXASR_PRIVILEGE_HV
) |
290 (msr_pr
<< TEXASR_PRIVILEGE_PR
) |
291 (1ULL << TEXASR_FAILURE_SUMMARY
) |
292 (1ULL << TEXASR_TFIAR_EXACT
);
293 env
->spr
[SPR_TFIAR
] = env
->nip
| (msr_hv
<< 1) | msr_pr
;
294 env
->spr
[SPR_TFHAR
] = env
->nip
+ 4;
295 env
->crf
[0] = 0xB; /* 0b1010 = transaction failure */