win_helper.c 9.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Helpers for CWP and PSTATE handling
 *
 *  Copyright (c) 2003-2005 Fabrice Bellard
 *
 * 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
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

P
Peter Maydell 已提交
20
#include "qemu/osdep.h"
21
#include "cpu.h"
22
#include "exec/exec-all.h"
23
#include "exec/helper-proto.h"
24
#include "trace.h"
25 26 27 28 29 30 31 32 33 34 35 36 37

static inline void memcpy32(target_ulong *dst, const target_ulong *src)
{
    dst[0] = src[0];
    dst[1] = src[1];
    dst[2] = src[2];
    dst[3] = src[3];
    dst[4] = src[4];
    dst[5] = src[5];
    dst[6] = src[6];
    dst[7] = src[7];
}

38
void cpu_set_cwp(CPUSPARCState *env, int new_cwp)
39 40 41 42 43 44 45 46 47 48 49 50 51 52
{
    /* put the modified wrap registers at their proper location */
    if (env->cwp == env->nwindows - 1) {
        memcpy32(env->regbase, env->regbase + env->nwindows * 16);
    }
    env->cwp = new_cwp;

    /* put the wrap registers at their temporary location */
    if (new_cwp == env->nwindows - 1) {
        memcpy32(env->regbase + env->nwindows * 16, env->regbase);
    }
    env->regwptr = env->regbase + (new_cwp * 16);
}

53
target_ulong cpu_get_psr(CPUSPARCState *env)
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
{
    helper_compute_psr(env);

#if !defined(TARGET_SPARC64)
    return env->version | (env->psr & PSR_ICC) |
        (env->psref ? PSR_EF : 0) |
        (env->psrpil << 8) |
        (env->psrs ? PSR_S : 0) |
        (env->psrps ? PSR_PS : 0) |
        (env->psret ? PSR_ET : 0) | env->cwp;
#else
    return env->psr & PSR_ICC;
#endif
}

69
void cpu_put_psr_raw(CPUSPARCState *env, target_ulong val)
70 71 72 73 74 75 76 77 78 79
{
    env->psr = val & PSR_ICC;
#if !defined(TARGET_SPARC64)
    env->psref = (val & PSR_EF) ? 1 : 0;
    env->psrpil = (val & PSR_PIL) >> 8;
    env->psrs = (val & PSR_S) ? 1 : 0;
    env->psrps = (val & PSR_PS) ? 1 : 0;
    env->psret = (val & PSR_ET) ? 1 : 0;
#endif
    env->cc_op = CC_OP_FLAGS;
80 81 82 83 84 85 86 87 88 89 90
#if !defined(TARGET_SPARC64)
    cpu_set_cwp(env, val & PSR_CWP);
#endif
}

void cpu_put_psr(CPUSPARCState *env, target_ulong val)
{
    cpu_put_psr_raw(env, val);
#if ((!defined(TARGET_SPARC64)) && !defined(CONFIG_USER_ONLY))
    cpu_check_irqs(env);
#endif
91 92
}

93
int cpu_cwp_inc(CPUSPARCState *env, int cwp)
94 95 96 97 98 99 100
{
    if (unlikely(cwp >= env->nwindows)) {
        cwp -= env->nwindows;
    }
    return cwp;
}

101
int cpu_cwp_dec(CPUSPARCState *env, int cwp)
102 103 104 105 106 107 108 109
{
    if (unlikely(cwp < 0)) {
        cwp += env->nwindows;
    }
    return cwp;
}

#ifndef TARGET_SPARC64
110
void helper_rett(CPUSPARCState *env)
111 112 113 114
{
    unsigned int cwp;

    if (env->psret == 1) {
115
        cpu_raise_exception_ra(env, TT_ILL_INSN, GETPC());
116 117 118
    }

    env->psret = 1;
119
    cwp = cpu_cwp_inc(env, env->cwp + 1) ;
120
    if (env->wim & (1 << cwp)) {
121
        cpu_raise_exception_ra(env, TT_WIN_UNF, GETPC());
122
    }
123
    cpu_set_cwp(env, cwp);
124 125 126 127 128
    env->psrs = env->psrps;
}

/* XXX: use another pointer for %iN registers to avoid slow wrapping
   handling ? */
129
void helper_save(CPUSPARCState *env)
130 131 132
{
    uint32_t cwp;

133
    cwp = cpu_cwp_dec(env, env->cwp - 1);
134
    if (env->wim & (1 << cwp)) {
135
        cpu_raise_exception_ra(env, TT_WIN_OVF, GETPC());
136
    }
137
    cpu_set_cwp(env, cwp);
138 139
}

140
void helper_restore(CPUSPARCState *env)
141 142 143
{
    uint32_t cwp;

144
    cwp = cpu_cwp_inc(env, env->cwp + 1);
145
    if (env->wim & (1 << cwp)) {
146
        cpu_raise_exception_ra(env, TT_WIN_UNF, GETPC());
147
    }
148
    cpu_set_cwp(env, cwp);
149 150
}

151
void helper_wrpsr(CPUSPARCState *env, target_ulong new_psr)
152 153
{
    if ((new_psr & PSR_CWP) >= env->nwindows) {
154
        cpu_raise_exception_ra(env, TT_ILL_INSN, GETPC());
155 156 157 158 159
    } else {
        cpu_put_psr(env, new_psr);
    }
}

160
target_ulong helper_rdpsr(CPUSPARCState *env)
161
{
162
    return cpu_get_psr(env);
163 164 165 166 167
}

#else
/* XXX: use another pointer for %iN registers to avoid slow wrapping
   handling ? */
168
void helper_save(CPUSPARCState *env)
169 170 171
{
    uint32_t cwp;

172
    cwp = cpu_cwp_dec(env, env->cwp - 1);
173
    if (env->cansave == 0) {
174 175 176 177
        int tt = TT_SPILL | (env->otherwin != 0
                             ? (TT_WOTHER | ((env->wstate & 0x38) >> 1))
                             : ((env->wstate & 0x7) << 2));
        cpu_raise_exception_ra(env, tt, GETPC());
178 179 180
    } else {
        if (env->cleanwin - env->canrestore == 0) {
            /* XXX Clean windows without trap */
181
            cpu_raise_exception_ra(env, TT_CLRWIN, GETPC());
182 183 184
        } else {
            env->cansave--;
            env->canrestore++;
185
            cpu_set_cwp(env, cwp);
186 187 188 189
        }
    }
}

190
void helper_restore(CPUSPARCState *env)
191 192 193
{
    uint32_t cwp;

194
    cwp = cpu_cwp_inc(env, env->cwp + 1);
195
    if (env->canrestore == 0) {
196 197 198 199
        int tt = TT_FILL | (env->otherwin != 0
                            ? (TT_WOTHER | ((env->wstate & 0x38) >> 1))
                            : ((env->wstate & 0x7) << 2));
        cpu_raise_exception_ra(env, tt, GETPC());
200 201 202
    } else {
        env->cansave++;
        env->canrestore--;
203
        cpu_set_cwp(env, cwp);
204 205 206
    }
}

207
void helper_flushw(CPUSPARCState *env)
208 209
{
    if (env->cansave != env->nwindows - 2) {
210 211 212 213
        int tt = TT_SPILL | (env->otherwin != 0
                             ? (TT_WOTHER | ((env->wstate & 0x38) >> 1))
                             : ((env->wstate & 0x7) << 2));
        cpu_raise_exception_ra(env, tt, GETPC());
214 215 216
    }
}

217
void helper_saved(CPUSPARCState *env)
218 219 220 221 222 223 224 225 226
{
    env->cansave++;
    if (env->otherwin == 0) {
        env->canrestore--;
    } else {
        env->otherwin--;
    }
}

227
void helper_restored(CPUSPARCState *env)
228 229 230 231 232 233 234 235 236 237 238 239
{
    env->canrestore++;
    if (env->cleanwin < env->nwindows - 1) {
        env->cleanwin++;
    }
    if (env->otherwin == 0) {
        env->cansave--;
    } else {
        env->otherwin--;
    }
}

240
target_ulong cpu_get_ccr(CPUSPARCState *env)
241 242 243
{
    target_ulong psr;

244
    psr = cpu_get_psr(env);
245 246 247 248

    return ((env->xcc >> 20) << 4) | ((psr & PSR_ICC) >> 20);
}

249
void cpu_put_ccr(CPUSPARCState *env, target_ulong val)
250 251 252 253 254 255
{
    env->xcc = (val >> 4) << 20;
    env->psr = (val & 0xf) << 20;
    CC_OP = CC_OP_FLAGS;
}

256
target_ulong cpu_get_cwp64(CPUSPARCState *env)
257 258 259 260
{
    return env->nwindows - 1 - env->cwp;
}

261
void cpu_put_cwp64(CPUSPARCState *env, int cwp)
262 263 264 265
{
    if (unlikely(cwp >= env->nwindows || cwp < 0)) {
        cwp %= env->nwindows;
    }
266
    cpu_set_cwp(env, env->nwindows - 1 - cwp);
267 268
}

269
target_ulong helper_rdccr(CPUSPARCState *env)
270
{
271
    return cpu_get_ccr(env);
272 273
}

274
void helper_wrccr(CPUSPARCState *env, target_ulong new_ccr)
275
{
276
    cpu_put_ccr(env, new_ccr);
277 278 279 280
}

/* CWP handling is reversed in V9, but we still use the V8 register
   order. */
281
target_ulong helper_rdcwp(CPUSPARCState *env)
282
{
283
    return cpu_get_cwp64(env);
284 285
}

286
void helper_wrcwp(CPUSPARCState *env, target_ulong new_cwp)
287
{
288
    cpu_put_cwp64(env, new_cwp);
289 290
}

291
static inline uint64_t *get_gregset(CPUSPARCState *env, uint32_t pstate)
292 293 294
{
    switch (pstate) {
    default:
295
        trace_win_helper_gregset_error(pstate);
296 297 298 299 300 301 302 303 304 305 306 307
        /* pass through to normal set of global registers */
    case 0:
        return env->bgregs;
    case PS_AG:
        return env->agregs;
    case PS_MG:
        return env->mgregs;
    case PS_IG:
        return env->igregs;
    }
}

308
void cpu_change_pstate(CPUSPARCState *env, uint32_t new_pstate)
309 310 311 312 313 314 315 316 317 318 319 320 321
{
    uint32_t pstate_regs, new_pstate_regs;
    uint64_t *src, *dst;

    if (env->def->features & CPU_FEATURE_GL) {
        /* PS_AG is not implemented in this case */
        new_pstate &= ~PS_AG;
    }

    pstate_regs = env->pstate & 0xc01;
    new_pstate_regs = new_pstate & 0xc01;

    if (new_pstate_regs != pstate_regs) {
322 323
        trace_win_helper_switch_pstate(pstate_regs, new_pstate_regs);

324
        /* Switch global register bank */
325 326
        src = get_gregset(env, new_pstate_regs);
        dst = get_gregset(env, pstate_regs);
327 328 329
        memcpy32(dst, env->gregs);
        memcpy32(env->gregs, src);
    } else {
330
        trace_win_helper_no_switch_pstate(new_pstate_regs);
331 332 333 334
    }
    env->pstate = new_pstate;
}

335
void helper_wrpstate(CPUSPARCState *env, target_ulong new_state)
336
{
337
    cpu_change_pstate(env, new_state & 0xf3f);
338 339 340 341 342 343 344 345

#if !defined(CONFIG_USER_ONLY)
    if (cpu_interrupts_enabled(env)) {
        cpu_check_irqs(env);
    }
#endif
}

346
void helper_wrpil(CPUSPARCState *env, target_ulong new_pil)
347 348
{
#if !defined(CONFIG_USER_ONLY)
349
    trace_win_helper_wrpil(env->psrpil, (uint32_t)new_pil);
350 351 352 353 354 355 356 357 358

    env->psrpil = new_pil;

    if (cpu_interrupts_enabled(env)) {
        cpu_check_irqs(env);
    }
#endif
}

359
void helper_done(CPUSPARCState *env)
360 361 362 363 364
{
    trap_state *tsptr = cpu_tsptr(env);

    env->pc = tsptr->tnpc;
    env->npc = tsptr->tnpc + 4;
365
    cpu_put_ccr(env, tsptr->tstate >> 32);
366
    env->asi = (tsptr->tstate >> 24) & 0xff;
367 368
    cpu_change_pstate(env, (tsptr->tstate >> 8) & 0xf3f);
    cpu_put_cwp64(env, tsptr->tstate & 0xff);
369 370
    env->tl--;

371
    trace_win_helper_done(env->tl);
372 373 374 375 376 377 378 379

#if !defined(CONFIG_USER_ONLY)
    if (cpu_interrupts_enabled(env)) {
        cpu_check_irqs(env);
    }
#endif
}

380
void helper_retry(CPUSPARCState *env)
381 382 383 384 385
{
    trap_state *tsptr = cpu_tsptr(env);

    env->pc = tsptr->tpc;
    env->npc = tsptr->tnpc;
386
    cpu_put_ccr(env, tsptr->tstate >> 32);
387
    env->asi = (tsptr->tstate >> 24) & 0xff;
388 389
    cpu_change_pstate(env, (tsptr->tstate >> 8) & 0xf3f);
    cpu_put_cwp64(env, tsptr->tstate & 0xff);
390 391
    env->tl--;

392
    trace_win_helper_retry(env->tl);
393 394 395 396 397 398 399 400

#if !defined(CONFIG_USER_ONLY)
    if (cpu_interrupts_enabled(env)) {
        cpu_check_irqs(env);
    }
#endif
}
#endif