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"
25 #include "helper_regs.h"
26 #include "exec/exec-all.h"
27 #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(env
, addr
));
62 env
->gpr
[reg
] = cpu_ldl_data(env
, addr
);
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(env
, addr
, bswap32((uint32_t)env
->gpr
[reg
]));
74 cpu_stl_data(env
, addr
, (uint32_t)env
->gpr
[reg
]);
76 addr
= addr_add(env
, addr
, 4);
80 void helper_lsw(CPUPPCState
*env
, target_ulong addr
, uint32_t nb
, uint32_t reg
)
84 for (; nb
> 3; nb
-= 4) {
85 env
->gpr
[reg
] = cpu_ldl_data(env
, addr
);
87 addr
= addr_add(env
, addr
, 4);
89 if (unlikely(nb
> 0)) {
91 for (sh
= 24; nb
> 0; nb
--, sh
-= 8) {
92 env
->gpr
[reg
] |= cpu_ldub_data(env
, addr
) << sh
;
93 addr
= addr_add(env
, addr
, 1);
97 /* PPC32 specification says we must generate an exception if
98 * rA is in the range of registers to be loaded.
99 * In an other hand, IBM says this is valid, but rA won't be loaded.
100 * For now, I'll follow the spec...
102 void helper_lswx(CPUPPCState
*env
, target_ulong addr
, uint32_t reg
,
103 uint32_t ra
, uint32_t rb
)
105 if (likely(xer_bc
!= 0)) {
106 int num_used_regs
= (xer_bc
+ 3) / 4;
107 if (unlikely((ra
!= 0 && lsw_reg_in_range(reg
, num_used_regs
, ra
)) ||
108 lsw_reg_in_range(reg
, num_used_regs
, rb
))) {
109 env
->nip
+= 4; /* Compensate the "nip - 4" from gen_lswx() */
110 helper_raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
112 POWERPC_EXCP_INVAL_LSWX
);
114 helper_lsw(env
, addr
, xer_bc
, reg
);
119 void helper_stsw(CPUPPCState
*env
, target_ulong addr
, uint32_t nb
,
124 for (; nb
> 3; nb
-= 4) {
125 cpu_stl_data(env
, addr
, env
->gpr
[reg
]);
126 reg
= (reg
+ 1) % 32;
127 addr
= addr_add(env
, addr
, 4);
129 if (unlikely(nb
> 0)) {
130 for (sh
= 24; nb
> 0; nb
--, sh
-= 8) {
131 cpu_stb_data(env
, addr
, (env
->gpr
[reg
] >> sh
) & 0xFF);
132 addr
= addr_add(env
, addr
, 1);
137 static void do_dcbz(CPUPPCState
*env
, target_ulong addr
, int dcache_line_size
)
141 addr
&= ~(dcache_line_size
- 1);
142 for (i
= 0; i
< dcache_line_size
; i
+= 4) {
143 cpu_stl_data(env
, addr
+ i
, 0);
145 if (env
->reserve_addr
== addr
) {
146 env
->reserve_addr
= (target_ulong
)-1ULL;
150 void helper_dcbz(CPUPPCState
*env
, target_ulong addr
, uint32_t is_dcbzl
)
152 int dcbz_size
= env
->dcache_line_size
;
154 #if defined(TARGET_PPC64)
156 (env
->excp_model
== POWERPC_EXCP_970
) &&
157 ((env
->spr
[SPR_970_HID5
] >> 7) & 0x3) == 1) {
162 /* XXX add e500mc support */
164 do_dcbz(env
, addr
, dcbz_size
);
167 void helper_icbi(CPUPPCState
*env
, target_ulong addr
)
169 addr
&= ~(env
->dcache_line_size
- 1);
170 /* Invalidate one cache line :
171 * PowerPC specification says this is to be treated like a load
172 * (not a fetch) by the MMU. To be sure it will be so,
173 * do the load "by hand".
175 cpu_ldl_data(env
, addr
);
178 /* XXX: to be tested */
179 target_ulong
helper_lscbx(CPUPPCState
*env
, target_ulong addr
, uint32_t reg
,
180 uint32_t ra
, uint32_t rb
)
185 for (i
= 0; i
< xer_bc
; i
++) {
186 c
= cpu_ldub_data(env
, addr
);
187 addr
= addr_add(env
, addr
, 1);
188 /* ra (if not 0) and rb are never modified */
189 if (likely(reg
!= rb
&& (ra
== 0 || reg
!= ra
))) {
190 env
->gpr
[reg
] = (env
->gpr
[reg
] & ~(0xFF << d
)) | (c
<< d
);
192 if (unlikely(c
== xer_cmp
)) {
195 if (likely(d
!= 0)) {
206 /*****************************************************************************/
207 /* Altivec extension helpers */
208 #if defined(HOST_WORDS_BIGENDIAN)
216 /* We use msr_le to determine index ordering in a vector. However,
217 byteswapping is not simply controlled by msr_le. We also need to take
218 into account endianness of the target. This is done for the little-endian
219 PPC64 user-mode target. */
221 #define LVE(name, access, swap, element) \
222 void helper_##name(CPUPPCState *env, ppc_avr_t *r, \
225 size_t n_elems = ARRAY_SIZE(r->element); \
226 int adjust = HI_IDX*(n_elems - 1); \
227 int sh = sizeof(r->element[0]) >> 1; \
228 int index = (addr & 0xf) >> sh; \
230 index = n_elems - index - 1; \
233 if (needs_byteswap(env)) { \
234 r->element[LO_IDX ? index : (adjust - index)] = \
235 swap(access(env, addr, GETPC())); \
237 r->element[LO_IDX ? index : (adjust - index)] = \
238 access(env, addr, GETPC()); \
242 LVE(lvebx
, cpu_ldub_data_ra
, I
, u8
)
243 LVE(lvehx
, cpu_lduw_data_ra
, bswap16
, u16
)
244 LVE(lvewx
, cpu_ldl_data_ra
, bswap32
, u32
)
248 #define STVE(name, access, swap, element) \
249 void helper_##name(CPUPPCState *env, ppc_avr_t *r, \
252 size_t n_elems = ARRAY_SIZE(r->element); \
253 int adjust = HI_IDX * (n_elems - 1); \
254 int sh = sizeof(r->element[0]) >> 1; \
255 int index = (addr & 0xf) >> sh; \
257 index = n_elems - index - 1; \
260 if (needs_byteswap(env)) { \
261 access(env, addr, swap(r->element[LO_IDX ? index : \
262 (adjust - index)]), \
265 access(env, addr, r->element[LO_IDX ? index : \
266 (adjust - index)], GETPC()); \
270 STVE(stvebx
, cpu_stb_data_ra
, I
, u8
)
271 STVE(stvehx
, cpu_stw_data_ra
, bswap16
, u16
)
272 STVE(stvewx
, cpu_stl_data_ra
, bswap32
, u32
)
279 void helper_tbegin(CPUPPCState
*env
)
281 /* As a degenerate implementation, always fail tbegin. The reason
282 * given is "Nesting overflow". The "persistent" bit is set,
283 * providing a hint to the error handler to not retry. The TFIAR
284 * captures the address of the failure, which is this tbegin
285 * instruction. Instruction execution will continue with the
286 * next instruction in memory, which is precisely what we want.
289 env
->spr
[SPR_TEXASR
] =
290 (1ULL << TEXASR_FAILURE_PERSISTENT
) |
291 (1ULL << TEXASR_NESTING_OVERFLOW
) |
292 (msr_hv
<< TEXASR_PRIVILEGE_HV
) |
293 (msr_pr
<< TEXASR_PRIVILEGE_PR
) |
294 (1ULL << TEXASR_FAILURE_SUMMARY
) |
295 (1ULL << TEXASR_TFIAR_EXACT
);
296 env
->spr
[SPR_TFIAR
] = env
->nip
| (msr_hv
<< 1) | msr_pr
;
297 env
->spr
[SPR_TFHAR
] = env
->nip
+ 4;
298 env
->crf
[0] = 0xB; /* 0b1010 = transaction failure */