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/>.
20 #include "qemu/host-utils.h"
23 #include "helper_regs.h"
25 #if !defined(CONFIG_USER_ONLY)
26 #include "exec/softmmu_exec.h"
27 #endif /* !defined(CONFIG_USER_ONLY) */
31 /*****************************************************************************/
32 /* Memory load and stores */
34 static inline target_ulong addr_add(CPUPPCState *env, target_ulong addr,
37 #if defined(TARGET_PPC64)
38 if (!msr_is_64bit(env, env->msr)) {
39 return (uint32_t)(addr + arg);
47 void helper_lmw(CPUPPCState *env, target_ulong addr, uint32_t reg)
49 for (; reg < 32; reg++) {
51 env->gpr[reg] = bswap32(cpu_ldl_data(env, addr));
53 env->gpr[reg] = cpu_ldl_data(env, addr);
55 addr = addr_add(env, addr, 4);
59 void helper_stmw(CPUPPCState *env, target_ulong addr, uint32_t reg)
61 for (; reg < 32; reg++) {
63 cpu_stl_data(env, addr, bswap32((uint32_t)env->gpr[reg]));
65 cpu_stl_data(env, addr, (uint32_t)env->gpr[reg]);
67 addr = addr_add(env, addr, 4);
71 void helper_lsw(CPUPPCState *env, target_ulong addr, uint32_t nb, uint32_t reg)
75 for (; nb > 3; nb -= 4) {
76 env->gpr[reg] = cpu_ldl_data(env, addr);
78 addr = addr_add(env, addr, 4);
80 if (unlikely(nb > 0)) {
82 for (sh = 24; nb > 0; nb--, sh -= 8) {
83 env->gpr[reg] |= cpu_ldub_data(env, addr) << sh;
84 addr = addr_add(env, addr, 1);
88 /* PPC32 specification says we must generate an exception if
89 * rA is in the range of registers to be loaded.
90 * In an other hand, IBM says this is valid, but rA won't be loaded.
91 * For now, I'll follow the spec...
93 void helper_lswx(CPUPPCState *env, target_ulong addr, uint32_t reg,
94 uint32_t ra, uint32_t rb)
96 if (likely(xer_bc != 0)) {
97 if (unlikely((ra != 0 && reg < ra && (reg + xer_bc) > ra) ||
98 (reg < rb && (reg + xer_bc) > rb))) {
99 helper_raise_exception_err(env, POWERPC_EXCP_PROGRAM,
101 POWERPC_EXCP_INVAL_LSWX);
103 helper_lsw(env, addr, xer_bc, reg);
108 void helper_stsw(CPUPPCState *env, target_ulong addr, uint32_t nb,
113 for (; nb > 3; nb -= 4) {
114 cpu_stl_data(env, addr, env->gpr[reg]);
115 reg = (reg + 1) % 32;
116 addr = addr_add(env, addr, 4);
118 if (unlikely(nb > 0)) {
119 for (sh = 24; nb > 0; nb--, sh -= 8) {
120 cpu_stb_data(env, addr, (env->gpr[reg] >> sh) & 0xFF);
121 addr = addr_add(env, addr, 1);
126 static void do_dcbz(CPUPPCState *env, target_ulong addr, int dcache_line_size)
130 addr &= ~(dcache_line_size - 1);
131 for (i = 0; i < dcache_line_size; i += 4) {
132 cpu_stl_data(env, addr + i, 0);
134 if (env->reserve_addr == addr) {
135 env->reserve_addr = (target_ulong)-1ULL;
139 void helper_dcbz(CPUPPCState *env, target_ulong addr, uint32_t is_dcbzl)
141 int dcbz_size = env->dcache_line_size;
143 #if defined(TARGET_PPC64)
145 (env->excp_model == POWERPC_EXCP_970) &&
146 ((env->spr[SPR_970_HID5] >> 7) & 0x3) == 1) {
151 /* XXX add e500mc support */
153 do_dcbz(env, addr, dcbz_size);
156 void helper_icbi(CPUPPCState *env, target_ulong addr)
158 addr &= ~(env->dcache_line_size - 1);
159 /* Invalidate one cache line :
160 * PowerPC specification says this is to be treated like a load
161 * (not a fetch) by the MMU. To be sure it will be so,
162 * do the load "by hand".
164 cpu_ldl_data(env, addr);
167 /* XXX: to be tested */
168 target_ulong helper_lscbx(CPUPPCState *env, target_ulong addr, uint32_t reg,
169 uint32_t ra, uint32_t rb)
174 for (i = 0; i < xer_bc; i++) {
175 c = cpu_ldub_data(env, addr);
176 addr = addr_add(env, addr, 1);
177 /* ra (if not 0) and rb are never modified */
178 if (likely(reg != rb && (ra == 0 || reg != ra))) {
179 env->gpr[reg] = (env->gpr[reg] & ~(0xFF << d)) | (c << d);
181 if (unlikely(c == xer_cmp)) {
184 if (likely(d != 0)) {
195 /*****************************************************************************/
196 /* Altivec extension helpers */
197 #if defined(HOST_WORDS_BIGENDIAN)
205 #define LVE(name, access, swap, element) \
206 void helper_##name(CPUPPCState *env, ppc_avr_t *r, \
209 size_t n_elems = ARRAY_SIZE(r->element); \
210 int adjust = HI_IDX*(n_elems - 1); \
211 int sh = sizeof(r->element[0]) >> 1; \
212 int index = (addr & 0xf) >> sh; \
215 index = n_elems - index - 1; \
216 r->element[LO_IDX ? index : (adjust - index)] = \
217 swap(access(env, addr)); \
219 r->element[LO_IDX ? index : (adjust - index)] = \
224 LVE(lvebx, cpu_ldub_data, I, u8)
225 LVE(lvehx, cpu_lduw_data, bswap16, u16)
226 LVE(lvewx, cpu_ldl_data, bswap32, u32)
230 #define STVE(name, access, swap, element) \
231 void helper_##name(CPUPPCState *env, ppc_avr_t *r, \
234 size_t n_elems = ARRAY_SIZE(r->element); \
235 int adjust = HI_IDX * (n_elems - 1); \
236 int sh = sizeof(r->element[0]) >> 1; \
237 int index = (addr & 0xf) >> sh; \
240 index = n_elems - index - 1; \
241 access(env, addr, swap(r->element[LO_IDX ? index : \
242 (adjust - index)])); \
244 access(env, addr, r->element[LO_IDX ? index : \
245 (adjust - index)]); \
249 STVE(stvebx, cpu_stb_data, I, u8)
250 STVE(stvehx, cpu_stw_data, bswap16, u16)
251 STVE(stvewx, cpu_stl_data, bswap32, u32)