mem_helper.c 9.9 KB
Newer Older
1
/*
2
 *  PowerPC memory access emulation helpers for QEMU.
3
 *
4
 *  Copyright (c) 2003-2007 Jocelyn Mayer
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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/>.
18
 */
P
Peter Maydell 已提交
19
#include "qemu/osdep.h"
B
Blue Swirl 已提交
20
#include "cpu.h"
21
#include "qemu/host-utils.h"
22
#include "exec/helper-proto.h"
23

24
#include "helper_regs.h"
P
Paolo Bonzini 已提交
25
#include "exec/cpu_ldst.h"
B
Blue Swirl 已提交
26

27
//#define DEBUG_OP
28

29 30 31 32 33 34 35 36 37
static inline bool needs_byteswap(const CPUPPCState *env)
{
#if defined(TARGET_WORDS_BIGENDIAN)
  return msr_le;
#else
  return !msr_le;
#endif
}

38 39 40
/*****************************************************************************/
/* Memory load and stores */

41 42
static inline target_ulong addr_add(CPUPPCState *env, target_ulong addr,
                                    target_long arg)
43 44
{
#if defined(TARGET_PPC64)
A
Alexander Graf 已提交
45
    if (!msr_is_64bit(env, env->msr)) {
46 47
        return (uint32_t)(addr + arg);
    } else
48
#endif
49 50 51
    {
        return addr + arg;
    }
52 53
}

54
void helper_lmw(CPUPPCState *env, target_ulong addr, uint32_t reg)
55
{
A
aurel32 已提交
56
    for (; reg < 32; reg++) {
57
        if (needs_byteswap(env)) {
58
            env->gpr[reg] = bswap32(cpu_ldl_data(env, addr));
59
        } else {
60
            env->gpr[reg] = cpu_ldl_data(env, addr);
61
        }
62
        addr = addr_add(env, addr, 4);
63 64 65
    }
}

66
void helper_stmw(CPUPPCState *env, target_ulong addr, uint32_t reg)
67
{
A
aurel32 已提交
68
    for (; reg < 32; reg++) {
69
        if (needs_byteswap(env)) {
70
            cpu_stl_data(env, addr, bswap32((uint32_t)env->gpr[reg]));
71
        } else {
72
            cpu_stl_data(env, addr, (uint32_t)env->gpr[reg]);
73
        }
74
        addr = addr_add(env, addr, 4);
75 76 77
    }
}

78
void helper_lsw(CPUPPCState *env, target_ulong addr, uint32_t nb, uint32_t reg)
79 80
{
    int sh;
81

A
aurel32 已提交
82
    for (; nb > 3; nb -= 4) {
83
        env->gpr[reg] = cpu_ldl_data(env, addr);
84
        reg = (reg + 1) % 32;
85
        addr = addr_add(env, addr, 4);
86 87 88
    }
    if (unlikely(nb > 0)) {
        env->gpr[reg] = 0;
A
aurel32 已提交
89
        for (sh = 24; nb > 0; nb--, sh -= 8) {
90 91
            env->gpr[reg] |= cpu_ldub_data(env, addr) << sh;
            addr = addr_add(env, addr, 1);
92 93 94 95 96 97 98 99
        }
    }
}
/* PPC32 specification says we must generate an exception if
 * rA is in the range of registers to be loaded.
 * In an other hand, IBM says this is valid, but rA won't be loaded.
 * For now, I'll follow the spec...
 */
100 101
void helper_lswx(CPUPPCState *env, target_ulong addr, uint32_t reg,
                 uint32_t ra, uint32_t rb)
102 103
{
    if (likely(xer_bc != 0)) {
A
Alexander Graf 已提交
104
        int num_used_regs = (xer_bc + 3) / 4;
105 106 107
        if (unlikely((ra != 0 && lsw_reg_in_range(reg, num_used_regs, ra)) ||
                     lsw_reg_in_range(reg, num_used_regs, rb))) {
            env->nip += 4;     /* Compensate the "nip - 4" from gen_lswx() */
108
            helper_raise_exception_err(env, POWERPC_EXCP_PROGRAM,
A
aurel32 已提交
109 110
                                       POWERPC_EXCP_INVAL |
                                       POWERPC_EXCP_INVAL_LSWX);
111
        } else {
112
            helper_lsw(env, addr, xer_bc, reg);
113 114 115 116
        }
    }
}

117 118
void helper_stsw(CPUPPCState *env, target_ulong addr, uint32_t nb,
                 uint32_t reg)
119 120
{
    int sh;
121

A
aurel32 已提交
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);
126 127
    }
    if (unlikely(nb > 0)) {
A
aurel32 已提交
128
        for (sh = 24; nb > 0; nb--, sh -= 8) {
129 130
            cpu_stb_data(env, addr, (env->gpr[reg] >> sh) & 0xFF);
            addr = addr_add(env, addr, 1);
A
aurel32 已提交
131
        }
132 133 134
    }
}

135
static void do_dcbz(CPUPPCState *env, target_ulong addr, int dcache_line_size)
136 137
{
    int i;
138 139 140

    addr &= ~(dcache_line_size - 1);
    for (i = 0; i < dcache_line_size; i += 4) {
141
        cpu_stl_data(env, addr + i, 0);
142
    }
143
    if (env->reserve_addr == addr) {
144
        env->reserve_addr = (target_ulong)-1ULL;
145
    }
146 147
}

A
Alexander Graf 已提交
148
void helper_dcbz(CPUPPCState *env, target_ulong addr, uint32_t is_dcbzl)
149
{
A
Alexander Graf 已提交
150
    int dcbz_size = env->dcache_line_size;
151

152
#if defined(TARGET_PPC64)
A
Alexander Graf 已提交
153 154 155 156
    if (!is_dcbzl &&
        (env->excp_model == POWERPC_EXCP_970) &&
        ((env->spr[SPR_970_HID5] >> 7) & 0x3) == 1) {
        dcbz_size = 32;
157
    }
A
Alexander Graf 已提交
158 159 160 161 162
#endif

    /* XXX add e500mc support */

    do_dcbz(env, addr, dcbz_size);
163 164
}

165
void helper_icbi(CPUPPCState *env, target_ulong addr)
166
{
A
aurel32 已提交
167
    addr &= ~(env->dcache_line_size - 1);
168 169 170 171 172
    /* Invalidate one cache line :
     * PowerPC specification says this is to be treated like a load
     * (not a fetch) by the MMU. To be sure it will be so,
     * do the load "by hand".
     */
173
    cpu_ldl_data(env, addr);
174 175
}

176
/* XXX: to be tested */
177 178
target_ulong helper_lscbx(CPUPPCState *env, target_ulong addr, uint32_t reg,
                          uint32_t ra, uint32_t rb)
179 180
{
    int i, c, d;
181

182 183
    d = 24;
    for (i = 0; i < xer_bc; i++) {
184 185
        c = cpu_ldub_data(env, addr);
        addr = addr_add(env, addr, 1);
186 187 188 189
        /* ra (if not 0) and rb are never modified */
        if (likely(reg != rb && (ra == 0 || reg != ra))) {
            env->gpr[reg] = (env->gpr[reg] & ~(0xFF << d)) | (c << d);
        }
190
        if (unlikely(c == xer_cmp)) {
191
            break;
192
        }
193 194 195 196 197 198 199 200 201 202 203
        if (likely(d != 0)) {
            d -= 8;
        } else {
            d = 24;
            reg++;
            reg = reg & 0x1F;
        }
    }
    return i;
}

A
aurel32 已提交
204 205
/*****************************************************************************/
/* Altivec extension helpers */
206
#if defined(HOST_WORDS_BIGENDIAN)
A
aurel32 已提交
207 208 209 210 211 212 213
#define HI_IDX 0
#define LO_IDX 1
#else
#define HI_IDX 1
#define LO_IDX 0
#endif

214 215 216 217 218
/* We use msr_le to determine index ordering in a vector.  However,
   byteswapping is not simply controlled by msr_le.  We also need to take
   into account endianness of the target.  This is done for the little-endian
   PPC64 user-mode target. */

A
aurel32 已提交
219
#define LVE(name, access, swap, element)                        \
220 221
    void helper_##name(CPUPPCState *env, ppc_avr_t *r,          \
                       target_ulong addr)                       \
A
aurel32 已提交
222 223
    {                                                           \
        size_t n_elems = ARRAY_SIZE(r->element);                \
224
        int adjust = HI_IDX*(n_elems - 1);                      \
A
aurel32 已提交
225 226
        int sh = sizeof(r->element[0]) >> 1;                    \
        int index = (addr & 0xf) >> sh;                         \
227
        if (msr_le) {                                           \
228
            index = n_elems - index - 1;                        \
229 230 231
        }                                                       \
                                                                \
        if (needs_byteswap(env)) {                              \
232
            r->element[LO_IDX ? index : (adjust - index)] =     \
233
                swap(access(env, addr));                        \
234 235
        } else {                                                \
            r->element[LO_IDX ? index : (adjust - index)] =     \
236
                access(env, addr);                              \
237
        }                                                       \
A
aurel32 已提交
238 239
    }
#define I(x) (x)
240 241 242
LVE(lvebx, cpu_ldub_data, I, u8)
LVE(lvehx, cpu_lduw_data, bswap16, u16)
LVE(lvewx, cpu_ldl_data, bswap32, u32)
A
aurel32 已提交
243 244 245
#undef I
#undef LVE

246
#define STVE(name, access, swap, element)                               \
247 248
    void helper_##name(CPUPPCState *env, ppc_avr_t *r,                  \
                       target_ulong addr)                               \
249 250 251 252 253 254
    {                                                                   \
        size_t n_elems = ARRAY_SIZE(r->element);                        \
        int adjust = HI_IDX * (n_elems - 1);                            \
        int sh = sizeof(r->element[0]) >> 1;                            \
        int index = (addr & 0xf) >> sh;                                 \
        if (msr_le) {                                                   \
255
            index = n_elems - index - 1;                                \
256 257 258
        }                                                               \
                                                                        \
        if (needs_byteswap(env)) {                                      \
259 260
            access(env, addr, swap(r->element[LO_IDX ? index :          \
                                              (adjust - index)]));      \
A
aurel32 已提交
261
        } else {                                                        \
262 263
            access(env, addr, r->element[LO_IDX ? index :               \
                                         (adjust - index)]);            \
A
aurel32 已提交
264 265 266
        }                                                               \
    }
#define I(x) (x)
267 268 269
STVE(stvebx, cpu_stb_data, I, u8)
STVE(stvehx, cpu_stw_data, bswap16, u16)
STVE(stvewx, cpu_stl_data, bswap32, u32)
A
aurel32 已提交
270 271 272
#undef I
#undef LVE

A
aurel32 已提交
273 274
#undef HI_IDX
#undef LO_IDX
T
Tom Musta 已提交
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296

void helper_tbegin(CPUPPCState *env)
{
    /* As a degenerate implementation, always fail tbegin.  The reason
     * given is "Nesting overflow".  The "persistent" bit is set,
     * providing a hint to the error handler to not retry.  The TFIAR
     * captures the address of the failure, which is this tbegin
     * instruction.  Instruction execution will continue with the
     * next instruction in memory, which is precisely what we want.
     */

    env->spr[SPR_TEXASR] =
        (1ULL << TEXASR_FAILURE_PERSISTENT) |
        (1ULL << TEXASR_NESTING_OVERFLOW) |
        (msr_hv << TEXASR_PRIVILEGE_HV) |
        (msr_pr << TEXASR_PRIVILEGE_PR) |
        (1ULL << TEXASR_FAILURE_SUMMARY) |
        (1ULL << TEXASR_TFIAR_EXACT);
    env->spr[SPR_TFIAR] = env->nip | (msr_hv << 1) | msr_pr;
    env->spr[SPR_TFHAR] = env->nip + 4;
    env->crf[0] = 0xB; /* 0b1010 = transaction failure */
}