op_helper.c 95.9 KB
Newer Older
B
bellard 已提交
1 2
/*
 *  MIPS emulation helpers for qemu.
3
 *
B
bellard 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16
 *  Copyright (c) 2004-2005 Jocelyn Mayer
 *
 * 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/>.
B
bellard 已提交
18
 */
19
#include <stdlib.h>
B
bellard 已提交
20 21
#include "exec.h"

22 23
#include "host-utils.h"

P
pbrook 已提交
24
#include "helper.h"
25 26 27 28 29

#ifndef CONFIG_USER_ONLY
static inline void cpu_mips_tlb_flush (CPUState *env, int flush_global);
#endif

B
bellard 已提交
30 31 32
/*****************************************************************************/
/* Exceptions processing helpers */

33
void helper_raise_exception_err (uint32_t exception, int error_code)
B
bellard 已提交
34 35
{
#if 1
36 37
    if (exception < 0x100)
        qemu_log("%s: %d %d\n", __func__, exception, error_code);
B
bellard 已提交
38 39 40 41 42 43
#endif
    env->exception_index = exception;
    env->error_code = error_code;
    cpu_loop_exit();
}

44
void helper_raise_exception (uint32_t exception)
B
bellard 已提交
45
{
46
    helper_raise_exception_err(exception, 0);
B
bellard 已提交
47 48
}

49 50
#if !defined(CONFIG_USER_ONLY)
static void do_restore_state (void *pc_ptr)
B
bellard 已提交
51
{
B
bellard 已提交
52 53 54 55 56 57 58
    TranslationBlock *tb;
    unsigned long pc = (unsigned long) pc_ptr;
    
    tb = tb_find_pc (pc);
    if (tb) {
        cpu_restore_state (tb, env, pc, NULL);
    }
B
bellard 已提交
59
}
60
#endif
B
bellard 已提交
61

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
#if defined(CONFIG_USER_ONLY)
#define HELPER_LD(name, insn, type)                                     \
static inline type do_##name(target_ulong addr, int mem_idx)            \
{                                                                       \
    return (type) insn##_raw(addr);                                     \
}
#else
#define HELPER_LD(name, insn, type)                                     \
static inline type do_##name(target_ulong addr, int mem_idx)            \
{                                                                       \
    switch (mem_idx)                                                    \
    {                                                                   \
    case 0: return (type) insn##_kernel(addr); break;                   \
    case 1: return (type) insn##_super(addr); break;                    \
    default:                                                            \
    case 2: return (type) insn##_user(addr); break;                     \
    }                                                                   \
}
#endif
HELPER_LD(lbu, ldub, uint8_t)
HELPER_LD(lw, ldl, int32_t)
#ifdef TARGET_MIPS64
HELPER_LD(ld, ldq, int64_t)
#endif
#undef HELPER_LD

#if defined(CONFIG_USER_ONLY)
#define HELPER_ST(name, insn, type)                                     \
static inline void do_##name(target_ulong addr, type val, int mem_idx)  \
{                                                                       \
    insn##_raw(addr, val);                                              \
}
#else
#define HELPER_ST(name, insn, type)                                     \
static inline void do_##name(target_ulong addr, type val, int mem_idx)  \
{                                                                       \
    switch (mem_idx)                                                    \
    {                                                                   \
    case 0: insn##_kernel(addr, val); break;                            \
    case 1: insn##_super(addr, val); break;                             \
    default:                                                            \
    case 2: insn##_user(addr, val); break;                              \
    }                                                                   \
}
#endif
HELPER_ST(sb, stb, uint8_t)
HELPER_ST(sw, stl, uint32_t)
#ifdef TARGET_MIPS64
HELPER_ST(sd, stq, uint64_t)
#endif
#undef HELPER_ST

114
target_ulong helper_clo (target_ulong arg1)
115
{
116
    return clo32(arg1);
117 118
}

119
target_ulong helper_clz (target_ulong arg1)
120
{
121
    return clz32(arg1);
122 123
}

124
#if defined(TARGET_MIPS64)
125
target_ulong helper_dclo (target_ulong arg1)
126
{
127
    return clo64(arg1);
128 129
}

130
target_ulong helper_dclz (target_ulong arg1)
131
{
132
    return clz64(arg1);
133
}
134
#endif /* TARGET_MIPS64 */
135

B
bellard 已提交
136
/* 64 bits arithmetic for 32 bits hosts */
T
ths 已提交
137
static inline uint64_t get_HILO (void)
B
bellard 已提交
138
{
139
    return ((uint64_t)(env->active_tc.HI[0]) << 32) | (uint32_t)env->active_tc.LO[0];
B
bellard 已提交
140 141
}

T
ths 已提交
142
static inline void set_HILO (uint64_t HILO)
B
bellard 已提交
143
{
144 145
    env->active_tc.LO[0] = (int32_t)HILO;
    env->active_tc.HI[0] = (int32_t)(HILO >> 32);
B
bellard 已提交
146 147
}

148
static inline void set_HIT0_LO (target_ulong arg1, uint64_t HILO)
149
{
150
    env->active_tc.LO[0] = (int32_t)(HILO & 0xFFFFFFFF);
151
    arg1 = env->active_tc.HI[0] = (int32_t)(HILO >> 32);
152 153
}

154
static inline void set_HI_LOT0 (target_ulong arg1, uint64_t HILO)
155
{
156
    arg1 = env->active_tc.LO[0] = (int32_t)(HILO & 0xFFFFFFFF);
157
    env->active_tc.HI[0] = (int32_t)(HILO >> 32);
158 159 160
}

/* Multiplication variants of the vr54xx. */
161
target_ulong helper_muls (target_ulong arg1, target_ulong arg2)
162
{
163
    set_HI_LOT0(arg1, 0 - ((int64_t)(int32_t)arg1 * (int64_t)(int32_t)arg2));
164

165
    return arg1;
166 167
}

168
target_ulong helper_mulsu (target_ulong arg1, target_ulong arg2)
169
{
170
    set_HI_LOT0(arg1, 0 - ((uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2));
171

172
    return arg1;
173 174
}

175
target_ulong helper_macc (target_ulong arg1, target_ulong arg2)
176
{
177
    set_HI_LOT0(arg1, ((int64_t)get_HILO()) + ((int64_t)(int32_t)arg1 * (int64_t)(int32_t)arg2));
178

179
    return arg1;
180 181
}

182
target_ulong helper_macchi (target_ulong arg1, target_ulong arg2)
183
{
184
    set_HIT0_LO(arg1, ((int64_t)get_HILO()) + ((int64_t)(int32_t)arg1 * (int64_t)(int32_t)arg2));
185

186
    return arg1;
187 188
}

189
target_ulong helper_maccu (target_ulong arg1, target_ulong arg2)
190
{
191
    set_HI_LOT0(arg1, ((uint64_t)get_HILO()) + ((uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2));
192

193
    return arg1;
194 195
}

196
target_ulong helper_macchiu (target_ulong arg1, target_ulong arg2)
197
{
198
    set_HIT0_LO(arg1, ((uint64_t)get_HILO()) + ((uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2));
199

200
    return arg1;
201 202
}

203
target_ulong helper_msac (target_ulong arg1, target_ulong arg2)
204
{
205
    set_HI_LOT0(arg1, ((int64_t)get_HILO()) - ((int64_t)(int32_t)arg1 * (int64_t)(int32_t)arg2));
206

207
    return arg1;
208 209
}

210
target_ulong helper_msachi (target_ulong arg1, target_ulong arg2)
211
{
212
    set_HIT0_LO(arg1, ((int64_t)get_HILO()) - ((int64_t)(int32_t)arg1 * (int64_t)(int32_t)arg2));
213

214
    return arg1;
215 216
}

217
target_ulong helper_msacu (target_ulong arg1, target_ulong arg2)
218
{
219
    set_HI_LOT0(arg1, ((uint64_t)get_HILO()) - ((uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2));
220

221
    return arg1;
222 223
}

224
target_ulong helper_msachiu (target_ulong arg1, target_ulong arg2)
225
{
226
    set_HIT0_LO(arg1, ((uint64_t)get_HILO()) - ((uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2));
227

228
    return arg1;
229 230
}

231
target_ulong helper_mulhi (target_ulong arg1, target_ulong arg2)
232
{
233
    set_HIT0_LO(arg1, (int64_t)(int32_t)arg1 * (int64_t)(int32_t)arg2);
234

235
    return arg1;
236 237
}

238
target_ulong helper_mulhiu (target_ulong arg1, target_ulong arg2)
239
{
240
    set_HIT0_LO(arg1, (uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2);
241

242
    return arg1;
243 244
}

245
target_ulong helper_mulshi (target_ulong arg1, target_ulong arg2)
246
{
247
    set_HIT0_LO(arg1, 0 - ((int64_t)(int32_t)arg1 * (int64_t)(int32_t)arg2));
248

249
    return arg1;
250 251
}

252
target_ulong helper_mulshiu (target_ulong arg1, target_ulong arg2)
253
{
254
    set_HIT0_LO(arg1, 0 - ((uint64_t)(uint32_t)arg1 * (uint64_t)(uint32_t)arg2));
255

256
    return arg1;
257
}
B
bellard 已提交
258

259
#ifdef TARGET_MIPS64
260
void helper_dmult (target_ulong arg1, target_ulong arg2)
261
{
262
    muls64(&(env->active_tc.LO[0]), &(env->active_tc.HI[0]), arg1, arg2);
263 264
}

265
void helper_dmultu (target_ulong arg1, target_ulong arg2)
266
{
267
    mulu64(&(env->active_tc.LO[0]), &(env->active_tc.HI[0]), arg1, arg2);
268 269 270
}
#endif

271
#ifndef CONFIG_USER_ONLY
272 273 274 275 276 277 278 279 280 281 282 283 284 285

static inline target_phys_addr_t do_translate_address(target_ulong address, int rw)
{
    target_phys_addr_t lladdr;

    lladdr = cpu_mips_translate_address(env, address, rw);

    if (lladdr == -1LL) {
        cpu_loop_exit();
    } else {
        return lladdr;
    }
}

286 287 288
#define HELPER_LD_ATOMIC(name, insn)                                          \
target_ulong helper_##name(target_ulong arg, int mem_idx)                     \
{                                                                             \
289
    env->lladdr = do_translate_address(arg, 0);                               \
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
    env->llval = do_##insn(arg, mem_idx);                                     \
    return env->llval;                                                        \
}
HELPER_LD_ATOMIC(ll, lw)
#ifdef TARGET_MIPS64
HELPER_LD_ATOMIC(lld, ld)
#endif
#undef HELPER_LD_ATOMIC

#define HELPER_ST_ATOMIC(name, ld_insn, st_insn, almask)                      \
target_ulong helper_##name(target_ulong arg1, target_ulong arg2, int mem_idx) \
{                                                                             \
    target_long tmp;                                                          \
                                                                              \
    if (arg2 & almask) {                                                      \
        env->CP0_BadVAddr = arg2;                                             \
        helper_raise_exception(EXCP_AdES);                                    \
    }                                                                         \
308
    if (do_translate_address(arg2, 1) == env->lladdr) {                       \
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
        tmp = do_##ld_insn(arg2, mem_idx);                                    \
        if (tmp == env->llval) {                                              \
            do_##st_insn(arg2, arg1, mem_idx);                                \
            return 1;                                                         \
        }                                                                     \
    }                                                                         \
    return 0;                                                                 \
}
HELPER_ST_ATOMIC(sc, lw, sw, 0x3)
#ifdef TARGET_MIPS64
HELPER_ST_ATOMIC(scd, ld, sd, 0x7)
#endif
#undef HELPER_ST_ATOMIC
#endif

T
ths 已提交
324 325 326 327 328 329 330 331
#ifdef TARGET_WORDS_BIGENDIAN
#define GET_LMASK(v) ((v) & 3)
#define GET_OFFSET(addr, offset) (addr + (offset))
#else
#define GET_LMASK(v) (((v) & 3) ^ 3)
#define GET_OFFSET(addr, offset) (addr - (offset))
#endif

332
target_ulong helper_lwl(target_ulong arg1, target_ulong arg2, int mem_idx)
T
ths 已提交
333 334 335
{
    target_ulong tmp;

336
    tmp = do_lbu(arg2, mem_idx);
337
    arg1 = (arg1 & 0x00FFFFFF) | (tmp << 24);
T
ths 已提交
338

339
    if (GET_LMASK(arg2) <= 2) {
340
        tmp = do_lbu(GET_OFFSET(arg2, 1), mem_idx);
341
        arg1 = (arg1 & 0xFF00FFFF) | (tmp << 16);
T
ths 已提交
342 343
    }

344
    if (GET_LMASK(arg2) <= 1) {
345
        tmp = do_lbu(GET_OFFSET(arg2, 2), mem_idx);
346
        arg1 = (arg1 & 0xFFFF00FF) | (tmp << 8);
T
ths 已提交
347 348
    }

349
    if (GET_LMASK(arg2) == 0) {
350
        tmp = do_lbu(GET_OFFSET(arg2, 3), mem_idx);
351
        arg1 = (arg1 & 0xFFFFFF00) | tmp;
T
ths 已提交
352
    }
353
    return (int32_t)arg1;
T
ths 已提交
354 355
}

356
target_ulong helper_lwr(target_ulong arg1, target_ulong arg2, int mem_idx)
T
ths 已提交
357 358 359
{
    target_ulong tmp;

360
    tmp = do_lbu(arg2, mem_idx);
361
    arg1 = (arg1 & 0xFFFFFF00) | tmp;
T
ths 已提交
362

363
    if (GET_LMASK(arg2) >= 1) {
364
        tmp = do_lbu(GET_OFFSET(arg2, -1), mem_idx);
365
        arg1 = (arg1 & 0xFFFF00FF) | (tmp << 8);
T
ths 已提交
366 367
    }

368
    if (GET_LMASK(arg2) >= 2) {
369
        tmp = do_lbu(GET_OFFSET(arg2, -2), mem_idx);
370
        arg1 = (arg1 & 0xFF00FFFF) | (tmp << 16);
T
ths 已提交
371 372
    }

373
    if (GET_LMASK(arg2) == 3) {
374
        tmp = do_lbu(GET_OFFSET(arg2, -3), mem_idx);
375
        arg1 = (arg1 & 0x00FFFFFF) | (tmp << 24);
T
ths 已提交
376
    }
377
    return (int32_t)arg1;
T
ths 已提交
378 379
}

380
void helper_swl(target_ulong arg1, target_ulong arg2, int mem_idx)
T
ths 已提交
381
{
382
    do_sb(arg2, (uint8_t)(arg1 >> 24), mem_idx);
T
ths 已提交
383

384
    if (GET_LMASK(arg2) <= 2)
385
        do_sb(GET_OFFSET(arg2, 1), (uint8_t)(arg1 >> 16), mem_idx);
T
ths 已提交
386

387
    if (GET_LMASK(arg2) <= 1)
388
        do_sb(GET_OFFSET(arg2, 2), (uint8_t)(arg1 >> 8), mem_idx);
T
ths 已提交
389

390
    if (GET_LMASK(arg2) == 0)
391
        do_sb(GET_OFFSET(arg2, 3), (uint8_t)arg1, mem_idx);
T
ths 已提交
392 393
}

394
void helper_swr(target_ulong arg1, target_ulong arg2, int mem_idx)
T
ths 已提交
395
{
396
    do_sb(arg2, (uint8_t)arg1, mem_idx);
T
ths 已提交
397

398
    if (GET_LMASK(arg2) >= 1)
399
        do_sb(GET_OFFSET(arg2, -1), (uint8_t)(arg1 >> 8), mem_idx);
T
ths 已提交
400

401
    if (GET_LMASK(arg2) >= 2)
402
        do_sb(GET_OFFSET(arg2, -2), (uint8_t)(arg1 >> 16), mem_idx);
T
ths 已提交
403

404
    if (GET_LMASK(arg2) == 3)
405
        do_sb(GET_OFFSET(arg2, -3), (uint8_t)(arg1 >> 24), mem_idx);
T
ths 已提交
406 407 408 409 410 411 412 413 414 415 416 417
}

#if defined(TARGET_MIPS64)
/* "half" load and stores.  We must do the memory access inline,
   or fault handling won't work.  */

#ifdef TARGET_WORDS_BIGENDIAN
#define GET_LMASK64(v) ((v) & 7)
#else
#define GET_LMASK64(v) (((v) & 7) ^ 7)
#endif

418
target_ulong helper_ldl(target_ulong arg1, target_ulong arg2, int mem_idx)
T
ths 已提交
419 420 421
{
    uint64_t tmp;

422
    tmp = do_lbu(arg2, mem_idx);
423
    arg1 = (arg1 & 0x00FFFFFFFFFFFFFFULL) | (tmp << 56);
T
ths 已提交
424

425
    if (GET_LMASK64(arg2) <= 6) {
426
        tmp = do_lbu(GET_OFFSET(arg2, 1), mem_idx);
427
        arg1 = (arg1 & 0xFF00FFFFFFFFFFFFULL) | (tmp << 48);
T
ths 已提交
428 429
    }

430
    if (GET_LMASK64(arg2) <= 5) {
431
        tmp = do_lbu(GET_OFFSET(arg2, 2), mem_idx);
432
        arg1 = (arg1 & 0xFFFF00FFFFFFFFFFULL) | (tmp << 40);
T
ths 已提交
433 434
    }

435
    if (GET_LMASK64(arg2) <= 4) {
436
        tmp = do_lbu(GET_OFFSET(arg2, 3), mem_idx);
437
        arg1 = (arg1 & 0xFFFFFF00FFFFFFFFULL) | (tmp << 32);
T
ths 已提交
438 439
    }

440
    if (GET_LMASK64(arg2) <= 3) {
441
        tmp = do_lbu(GET_OFFSET(arg2, 4), mem_idx);
442
        arg1 = (arg1 & 0xFFFFFFFF00FFFFFFULL) | (tmp << 24);
T
ths 已提交
443 444
    }

445
    if (GET_LMASK64(arg2) <= 2) {
446
        tmp = do_lbu(GET_OFFSET(arg2, 5), mem_idx);
447
        arg1 = (arg1 & 0xFFFFFFFFFF00FFFFULL) | (tmp << 16);
T
ths 已提交
448 449
    }

450
    if (GET_LMASK64(arg2) <= 1) {
451
        tmp = do_lbu(GET_OFFSET(arg2, 6), mem_idx);
452
        arg1 = (arg1 & 0xFFFFFFFFFFFF00FFULL) | (tmp << 8);
T
ths 已提交
453 454
    }

455
    if (GET_LMASK64(arg2) == 0) {
456
        tmp = do_lbu(GET_OFFSET(arg2, 7), mem_idx);
457
        arg1 = (arg1 & 0xFFFFFFFFFFFFFF00ULL) | tmp;
T
ths 已提交
458
    }
459

460
    return arg1;
T
ths 已提交
461 462
}

463
target_ulong helper_ldr(target_ulong arg1, target_ulong arg2, int mem_idx)
T
ths 已提交
464 465 466
{
    uint64_t tmp;

467
    tmp = do_lbu(arg2, mem_idx);
468
    arg1 = (arg1 & 0xFFFFFFFFFFFFFF00ULL) | tmp;
T
ths 已提交
469

470
    if (GET_LMASK64(arg2) >= 1) {
471
        tmp = do_lbu(GET_OFFSET(arg2, -1), mem_idx);
472
        arg1 = (arg1 & 0xFFFFFFFFFFFF00FFULL) | (tmp  << 8);
T
ths 已提交
473 474
    }

475
    if (GET_LMASK64(arg2) >= 2) {
476
        tmp = do_lbu(GET_OFFSET(arg2, -2), mem_idx);
477
        arg1 = (arg1 & 0xFFFFFFFFFF00FFFFULL) | (tmp << 16);
T
ths 已提交
478 479
    }

480
    if (GET_LMASK64(arg2) >= 3) {
481
        tmp = do_lbu(GET_OFFSET(arg2, -3), mem_idx);
482
        arg1 = (arg1 & 0xFFFFFFFF00FFFFFFULL) | (tmp << 24);
T
ths 已提交
483 484
    }

485
    if (GET_LMASK64(arg2) >= 4) {
486
        tmp = do_lbu(GET_OFFSET(arg2, -4), mem_idx);
487
        arg1 = (arg1 & 0xFFFFFF00FFFFFFFFULL) | (tmp << 32);
T
ths 已提交
488 489
    }

490
    if (GET_LMASK64(arg2) >= 5) {
491
        tmp = do_lbu(GET_OFFSET(arg2, -5), mem_idx);
492
        arg1 = (arg1 & 0xFFFF00FFFFFFFFFFULL) | (tmp << 40);
T
ths 已提交
493 494
    }

495
    if (GET_LMASK64(arg2) >= 6) {
496
        tmp = do_lbu(GET_OFFSET(arg2, -6), mem_idx);
497
        arg1 = (arg1 & 0xFF00FFFFFFFFFFFFULL) | (tmp << 48);
T
ths 已提交
498 499
    }

500
    if (GET_LMASK64(arg2) == 7) {
501
        tmp = do_lbu(GET_OFFSET(arg2, -7), mem_idx);
502
        arg1 = (arg1 & 0x00FFFFFFFFFFFFFFULL) | (tmp << 56);
T
ths 已提交
503
    }
504

505
    return arg1;
T
ths 已提交
506 507
}

508
void helper_sdl(target_ulong arg1, target_ulong arg2, int mem_idx)
T
ths 已提交
509
{
510
    do_sb(arg2, (uint8_t)(arg1 >> 56), mem_idx);
T
ths 已提交
511

512
    if (GET_LMASK64(arg2) <= 6)
513
        do_sb(GET_OFFSET(arg2, 1), (uint8_t)(arg1 >> 48), mem_idx);
T
ths 已提交
514

515
    if (GET_LMASK64(arg2) <= 5)
516
        do_sb(GET_OFFSET(arg2, 2), (uint8_t)(arg1 >> 40), mem_idx);
T
ths 已提交
517

518
    if (GET_LMASK64(arg2) <= 4)
519
        do_sb(GET_OFFSET(arg2, 3), (uint8_t)(arg1 >> 32), mem_idx);
T
ths 已提交
520

521
    if (GET_LMASK64(arg2) <= 3)
522
        do_sb(GET_OFFSET(arg2, 4), (uint8_t)(arg1 >> 24), mem_idx);
T
ths 已提交
523

524
    if (GET_LMASK64(arg2) <= 2)
525
        do_sb(GET_OFFSET(arg2, 5), (uint8_t)(arg1 >> 16), mem_idx);
T
ths 已提交
526

527
    if (GET_LMASK64(arg2) <= 1)
528
        do_sb(GET_OFFSET(arg2, 6), (uint8_t)(arg1 >> 8), mem_idx);
T
ths 已提交
529

530
    if (GET_LMASK64(arg2) <= 0)
531
        do_sb(GET_OFFSET(arg2, 7), (uint8_t)arg1, mem_idx);
T
ths 已提交
532 533
}

534
void helper_sdr(target_ulong arg1, target_ulong arg2, int mem_idx)
T
ths 已提交
535
{
536
    do_sb(arg2, (uint8_t)arg1, mem_idx);
T
ths 已提交
537

538
    if (GET_LMASK64(arg2) >= 1)
539
        do_sb(GET_OFFSET(arg2, -1), (uint8_t)(arg1 >> 8), mem_idx);
T
ths 已提交
540

541
    if (GET_LMASK64(arg2) >= 2)
542
        do_sb(GET_OFFSET(arg2, -2), (uint8_t)(arg1 >> 16), mem_idx);
T
ths 已提交
543

544
    if (GET_LMASK64(arg2) >= 3)
545
        do_sb(GET_OFFSET(arg2, -3), (uint8_t)(arg1 >> 24), mem_idx);
T
ths 已提交
546

547
    if (GET_LMASK64(arg2) >= 4)
548
        do_sb(GET_OFFSET(arg2, -4), (uint8_t)(arg1 >> 32), mem_idx);
T
ths 已提交
549

550
    if (GET_LMASK64(arg2) >= 5)
551
        do_sb(GET_OFFSET(arg2, -5), (uint8_t)(arg1 >> 40), mem_idx);
T
ths 已提交
552

553
    if (GET_LMASK64(arg2) >= 6)
554
        do_sb(GET_OFFSET(arg2, -6), (uint8_t)(arg1 >> 48), mem_idx);
T
ths 已提交
555

556
    if (GET_LMASK64(arg2) == 7)
557
        do_sb(GET_OFFSET(arg2, -7), (uint8_t)(arg1 >> 56), mem_idx);
T
ths 已提交
558 559 560
}
#endif /* TARGET_MIPS64 */

561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
static const int multiple_regs[] = { 16, 17, 18, 19, 20, 21, 22, 23, 30 };

void helper_lwm (target_ulong addr, target_ulong reglist, uint32_t mem_idx)
{
    target_ulong base_reglist = reglist & 0xf;
    target_ulong do_r31 = reglist & 0x10;
#ifdef CONFIG_USER_ONLY
#undef ldfun
#define ldfun ldl_raw
#else
    uint32_t (*ldfun)(target_ulong);

    switch (mem_idx)
    {
    case 0: ldfun = ldl_kernel; break;
    case 1: ldfun = ldl_super; break;
    default:
    case 2: ldfun = ldl_user; break;
    }
#endif

    if (base_reglist > 0 && base_reglist <= ARRAY_SIZE (multiple_regs)) {
        target_ulong i;

        for (i = 0; i < base_reglist; i++) {
            env->active_tc.gpr[multiple_regs[i]] = (target_long) ldfun(addr);
            addr += 4;
        }
    }

    if (do_r31) {
        env->active_tc.gpr[31] = (target_long) ldfun(addr);
    }
}

void helper_swm (target_ulong addr, target_ulong reglist, uint32_t mem_idx)
{
    target_ulong base_reglist = reglist & 0xf;
    target_ulong do_r31 = reglist & 0x10;
#ifdef CONFIG_USER_ONLY
#undef stfun
#define stfun stl_raw
#else
    void (*stfun)(target_ulong, uint32_t);

    switch (mem_idx)
    {
    case 0: stfun = stl_kernel; break;
    case 1: stfun = stl_super; break;
     default:
    case 2: stfun = stl_user; break;
    }
#endif

    if (base_reglist > 0 && base_reglist <= ARRAY_SIZE (multiple_regs)) {
        target_ulong i;

        for (i = 0; i < base_reglist; i++) {
            stfun(addr, env->active_tc.gpr[multiple_regs[i]]);
            addr += 4;
        }
    }

    if (do_r31) {
        stfun(addr, env->active_tc.gpr[31]);
    }
}

#if defined(TARGET_MIPS64)
void helper_ldm (target_ulong addr, target_ulong reglist, uint32_t mem_idx)
{
    target_ulong base_reglist = reglist & 0xf;
    target_ulong do_r31 = reglist & 0x10;
#ifdef CONFIG_USER_ONLY
#undef ldfun
#define ldfun ldq_raw
#else
    uint64_t (*ldfun)(target_ulong);

    switch (mem_idx)
    {
    case 0: ldfun = ldq_kernel; break;
    case 1: ldfun = ldq_super; break;
    default:
    case 2: ldfun = ldq_user; break;
    }
#endif

    if (base_reglist > 0 && base_reglist <= ARRAY_SIZE (multiple_regs)) {
        target_ulong i;

        for (i = 0; i < base_reglist; i++) {
            env->active_tc.gpr[multiple_regs[i]] = ldfun(addr);
            addr += 8;
        }
    }

    if (do_r31) {
        env->active_tc.gpr[31] = ldfun(addr);
    }
}

void helper_sdm (target_ulong addr, target_ulong reglist, uint32_t mem_idx)
{
    target_ulong base_reglist = reglist & 0xf;
    target_ulong do_r31 = reglist & 0x10;
#ifdef CONFIG_USER_ONLY
#undef stfun
#define stfun stq_raw
#else
    void (*stfun)(target_ulong, uint64_t);

    switch (mem_idx)
    {
    case 0: stfun = stq_kernel; break;
    case 1: stfun = stq_super; break;
     default:
    case 2: stfun = stq_user; break;
    }
#endif

    if (base_reglist > 0 && base_reglist <= ARRAY_SIZE (multiple_regs)) {
        target_ulong i;

        for (i = 0; i < base_reglist; i++) {
            stfun(addr, env->active_tc.gpr[multiple_regs[i]]);
            addr += 8;
        }
    }

    if (do_r31) {
        stfun(addr, env->active_tc.gpr[31]);
    }
}
#endif

T
ths 已提交
697
#ifndef CONFIG_USER_ONLY
B
bellard 已提交
698
/* CP0 helpers */
699
target_ulong helper_mfc0_mvpcontrol (void)
700
{
701
    return env->mvp->CP0_MVPControl;
702 703
}

704
target_ulong helper_mfc0_mvpconf0 (void)
705
{
706
    return env->mvp->CP0_MVPConf0;
707 708
}

709
target_ulong helper_mfc0_mvpconf1 (void)
710
{
711
    return env->mvp->CP0_MVPConf1;
712 713
}

714
target_ulong helper_mfc0_random (void)
B
bellard 已提交
715
{
716
    return (int32_t)cpu_mips_get_random(env);
717
}
B
bellard 已提交
718

719
target_ulong helper_mfc0_tcstatus (void)
720
{
721
    return env->active_tc.CP0_TCStatus;
722 723
}

724
target_ulong helper_mftc0_tcstatus(void)
725 726 727
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);

728 729 730 731
    if (other_tc == env->current_tc)
        return env->active_tc.CP0_TCStatus;
    else
        return env->tcs[other_tc].CP0_TCStatus;
732 733
}

734
target_ulong helper_mfc0_tcbind (void)
735
{
736
    return env->active_tc.CP0_TCBind;
737 738
}

739
target_ulong helper_mftc0_tcbind(void)
740 741 742
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);

743 744 745 746
    if (other_tc == env->current_tc)
        return env->active_tc.CP0_TCBind;
    else
        return env->tcs[other_tc].CP0_TCBind;
747 748
}

749
target_ulong helper_mfc0_tcrestart (void)
750
{
751
    return env->active_tc.PC;
752 753
}

754
target_ulong helper_mftc0_tcrestart(void)
755 756 757
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);

758 759 760 761
    if (other_tc == env->current_tc)
        return env->active_tc.PC;
    else
        return env->tcs[other_tc].PC;
762 763
}

764
target_ulong helper_mfc0_tchalt (void)
765
{
766
    return env->active_tc.CP0_TCHalt;
767 768
}

769
target_ulong helper_mftc0_tchalt(void)
770 771 772
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);

773 774 775 776
    if (other_tc == env->current_tc)
        return env->active_tc.CP0_TCHalt;
    else
        return env->tcs[other_tc].CP0_TCHalt;
777 778
}

779
target_ulong helper_mfc0_tccontext (void)
780
{
781
    return env->active_tc.CP0_TCContext;
782 783
}

784
target_ulong helper_mftc0_tccontext(void)
785 786 787
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);

788 789 790 791
    if (other_tc == env->current_tc)
        return env->active_tc.CP0_TCContext;
    else
        return env->tcs[other_tc].CP0_TCContext;
792 793
}

794
target_ulong helper_mfc0_tcschedule (void)
795
{
796
    return env->active_tc.CP0_TCSchedule;
797 798
}

799
target_ulong helper_mftc0_tcschedule(void)
800 801 802
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);

803 804 805 806
    if (other_tc == env->current_tc)
        return env->active_tc.CP0_TCSchedule;
    else
        return env->tcs[other_tc].CP0_TCSchedule;
807 808
}

809
target_ulong helper_mfc0_tcschefback (void)
810
{
811
    return env->active_tc.CP0_TCScheFBack;
812 813
}

814
target_ulong helper_mftc0_tcschefback(void)
815 816 817
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);

818 819 820 821
    if (other_tc == env->current_tc)
        return env->active_tc.CP0_TCScheFBack;
    else
        return env->tcs[other_tc].CP0_TCScheFBack;
822 823
}

824
target_ulong helper_mfc0_count (void)
825
{
826
    return (int32_t)cpu_mips_get_count(env);
B
bellard 已提交
827 828
}

829
target_ulong helper_mftc0_entryhi(void)
830 831
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
832
    int32_t tcstatus;
833

834 835 836 837 838 839
    if (other_tc == env->current_tc)
        tcstatus = env->active_tc.CP0_TCStatus;
    else
        tcstatus = env->tcs[other_tc].CP0_TCStatus;

    return (env->CP0_EntryHi & ~0xff) | (tcstatus & 0xff);
840 841
}

842
target_ulong helper_mftc0_status(void)
843 844
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
845
    target_ulong t0;
846 847 848 849 850 851
    int32_t tcstatus;

    if (other_tc == env->current_tc)
        tcstatus = env->active_tc.CP0_TCStatus;
    else
        tcstatus = env->tcs[other_tc].CP0_TCStatus;
852

853 854 855 856 857 858
    t0 = env->CP0_Status & ~0xf1000018;
    t0 |= tcstatus & (0xf << CP0TCSt_TCU0);
    t0 |= (tcstatus & (1 << CP0TCSt_TMX)) >> (CP0TCSt_TMX - CP0St_MX);
    t0 |= (tcstatus & (0x3 << CP0TCSt_TKSU)) >> (CP0TCSt_TKSU - CP0St_KSU);

    return t0;
859 860
}

861
target_ulong helper_mfc0_lladdr (void)
862
{
863
    return (int32_t)(env->lladdr >> env->CP0_LLAddr_shift);
864 865
}

866
target_ulong helper_mfc0_watchlo (uint32_t sel)
867
{
868
    return (int32_t)env->CP0_WatchLo[sel];
869 870
}

871
target_ulong helper_mfc0_watchhi (uint32_t sel)
872
{
873
    return env->CP0_WatchHi[sel];
874 875
}

876
target_ulong helper_mfc0_debug (void)
877
{
878
    target_ulong t0 = env->CP0_Debug;
879
    if (env->hflags & MIPS_HFLAG_DM)
880 881 882
        t0 |= 1 << CP0DB_DM;

    return t0;
883 884
}

885
target_ulong helper_mftc0_debug(void)
886 887
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
888 889 890 891 892 893
    int32_t tcstatus;

    if (other_tc == env->current_tc)
        tcstatus = env->active_tc.CP0_Debug_tcstatus;
    else
        tcstatus = env->tcs[other_tc].CP0_Debug_tcstatus;
894 895

    /* XXX: Might be wrong, check with EJTAG spec. */
896
    return (env->CP0_Debug & ~((1 << CP0DB_SSt) | (1 << CP0DB_Halt))) |
897
            (tcstatus & ((1 << CP0DB_SSt) | (1 << CP0DB_Halt)));
898 899 900
}

#if defined(TARGET_MIPS64)
901
target_ulong helper_dmfc0_tcrestart (void)
902
{
903
    return env->active_tc.PC;
904 905
}

906
target_ulong helper_dmfc0_tchalt (void)
907
{
908
    return env->active_tc.CP0_TCHalt;
909 910
}

911
target_ulong helper_dmfc0_tccontext (void)
912
{
913
    return env->active_tc.CP0_TCContext;
914 915
}

916
target_ulong helper_dmfc0_tcschedule (void)
917
{
918
    return env->active_tc.CP0_TCSchedule;
919 920
}

921
target_ulong helper_dmfc0_tcschefback (void)
922
{
923
    return env->active_tc.CP0_TCScheFBack;
924 925
}

926
target_ulong helper_dmfc0_lladdr (void)
927
{
928
    return env->lladdr >> env->CP0_LLAddr_shift;
929 930
}

931
target_ulong helper_dmfc0_watchlo (uint32_t sel)
932
{
933
    return env->CP0_WatchLo[sel];
934 935 936
}
#endif /* TARGET_MIPS64 */

937
void helper_mtc0_index (target_ulong arg1)
938 939 940 941 942 943 944 945
{
    int num = 1;
    unsigned int tmp = env->tlb->nb_tlb;

    do {
        tmp >>= 1;
        num <<= 1;
    } while (tmp);
946
    env->CP0_Index = (env->CP0_Index & 0x80000000) | (arg1 & (num - 1));
947 948
}

949
void helper_mtc0_mvpcontrol (target_ulong arg1)
950 951 952 953 954 955 956 957 958
{
    uint32_t mask = 0;
    uint32_t newval;

    if (env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP))
        mask |= (1 << CP0MVPCo_CPA) | (1 << CP0MVPCo_VPC) |
                (1 << CP0MVPCo_EVP);
    if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
        mask |= (1 << CP0MVPCo_STLB);
959
    newval = (env->mvp->CP0_MVPControl & ~mask) | (arg1 & mask);
960 961 962 963 964 965

    // TODO: Enable/disable shared TLB, enable/disable VPEs.

    env->mvp->CP0_MVPControl = newval;
}

966
void helper_mtc0_vpecontrol (target_ulong arg1)
967 968 969 970 971 972
{
    uint32_t mask;
    uint32_t newval;

    mask = (1 << CP0VPECo_YSI) | (1 << CP0VPECo_GSI) |
           (1 << CP0VPECo_TE) | (0xff << CP0VPECo_TargTC);
973
    newval = (env->CP0_VPEControl & ~mask) | (arg1 & mask);
974 975 976 977 978 979 980 981 982

    /* Yield scheduler intercept not implemented. */
    /* Gating storage scheduler intercept not implemented. */

    // TODO: Enable/disable TCs.

    env->CP0_VPEControl = newval;
}

983
void helper_mtc0_vpeconf0 (target_ulong arg1)
984 985 986 987 988 989 990 991 992
{
    uint32_t mask = 0;
    uint32_t newval;

    if (env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP)) {
        if (env->CP0_VPEConf0 & (1 << CP0VPEC0_VPA))
            mask |= (0xff << CP0VPEC0_XTC);
        mask |= (1 << CP0VPEC0_MVP) | (1 << CP0VPEC0_VPA);
    }
993
    newval = (env->CP0_VPEConf0 & ~mask) | (arg1 & mask);
994 995 996 997 998 999

    // TODO: TC exclusive handling due to ERL/EXL.

    env->CP0_VPEConf0 = newval;
}

1000
void helper_mtc0_vpeconf1 (target_ulong arg1)
1001 1002 1003 1004 1005 1006 1007
{
    uint32_t mask = 0;
    uint32_t newval;

    if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
        mask |= (0xff << CP0VPEC1_NCX) | (0xff << CP0VPEC1_NCP2) |
                (0xff << CP0VPEC1_NCP1);
1008
    newval = (env->CP0_VPEConf1 & ~mask) | (arg1 & mask);
1009 1010 1011 1012 1013 1014 1015 1016 1017

    /* UDI not implemented. */
    /* CP2 not implemented. */

    // TODO: Handle FPU (CP1) binding.

    env->CP0_VPEConf1 = newval;
}

1018
void helper_mtc0_yqmask (target_ulong arg1)
1019 1020 1021 1022 1023
{
    /* Yield qualifier inputs not implemented. */
    env->CP0_YQMask = 0x00000000;
}

1024
void helper_mtc0_vpeopt (target_ulong arg1)
1025
{
1026
    env->CP0_VPEOpt = arg1 & 0x0000ffff;
1027 1028
}

1029
void helper_mtc0_entrylo0 (target_ulong arg1)
1030 1031 1032
{
    /* Large physaddr (PABITS) not implemented */
    /* 1k pages not implemented */
1033
    env->CP0_EntryLo0 = arg1 & 0x3FFFFFFF;
1034 1035
}

1036
void helper_mtc0_tcstatus (target_ulong arg1)
1037 1038 1039 1040
{
    uint32_t mask = env->CP0_TCStatus_rw_bitmask;
    uint32_t newval;

1041
    newval = (env->active_tc.CP0_TCStatus & ~mask) | (arg1 & mask);
1042 1043 1044

    // TODO: Sync with CP0_Status.

1045
    env->active_tc.CP0_TCStatus = newval;
1046 1047
}

1048
void helper_mttc0_tcstatus (target_ulong arg1)
1049 1050 1051 1052 1053
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);

    // TODO: Sync with CP0_Status.

1054
    if (other_tc == env->current_tc)
1055
        env->active_tc.CP0_TCStatus = arg1;
1056
    else
1057
        env->tcs[other_tc].CP0_TCStatus = arg1;
1058 1059
}

1060
void helper_mtc0_tcbind (target_ulong arg1)
1061 1062 1063 1064 1065 1066
{
    uint32_t mask = (1 << CP0TCBd_TBE);
    uint32_t newval;

    if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
        mask |= (1 << CP0TCBd_CurVPE);
1067
    newval = (env->active_tc.CP0_TCBind & ~mask) | (arg1 & mask);
1068
    env->active_tc.CP0_TCBind = newval;
1069 1070
}

1071
void helper_mttc0_tcbind (target_ulong arg1)
1072 1073 1074 1075 1076 1077 1078
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
    uint32_t mask = (1 << CP0TCBd_TBE);
    uint32_t newval;

    if (env->mvp->CP0_MVPControl & (1 << CP0MVPCo_VPC))
        mask |= (1 << CP0TCBd_CurVPE);
1079
    if (other_tc == env->current_tc) {
1080
        newval = (env->active_tc.CP0_TCBind & ~mask) | (arg1 & mask);
1081 1082
        env->active_tc.CP0_TCBind = newval;
    } else {
1083
        newval = (env->tcs[other_tc].CP0_TCBind & ~mask) | (arg1 & mask);
1084 1085
        env->tcs[other_tc].CP0_TCBind = newval;
    }
1086 1087
}

1088
void helper_mtc0_tcrestart (target_ulong arg1)
1089
{
1090
    env->active_tc.PC = arg1;
1091
    env->active_tc.CP0_TCStatus &= ~(1 << CP0TCSt_TDS);
1092
    env->lladdr = 0ULL;
1093 1094 1095
    /* MIPS16 not implemented. */
}

1096
void helper_mttc0_tcrestart (target_ulong arg1)
1097 1098 1099
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);

1100
    if (other_tc == env->current_tc) {
1101
        env->active_tc.PC = arg1;
1102
        env->active_tc.CP0_TCStatus &= ~(1 << CP0TCSt_TDS);
1103
        env->lladdr = 0ULL;
1104 1105
        /* MIPS16 not implemented. */
    } else {
1106
        env->tcs[other_tc].PC = arg1;
1107
        env->tcs[other_tc].CP0_TCStatus &= ~(1 << CP0TCSt_TDS);
1108
        env->lladdr = 0ULL;
1109 1110
        /* MIPS16 not implemented. */
    }
1111 1112
}

1113
void helper_mtc0_tchalt (target_ulong arg1)
1114
{
1115
    env->active_tc.CP0_TCHalt = arg1 & 0x1;
1116 1117 1118 1119

    // TODO: Halt TC / Restart (if allocated+active) TC.
}

1120
void helper_mttc0_tchalt (target_ulong arg1)
1121 1122 1123 1124 1125
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);

    // TODO: Halt TC / Restart (if allocated+active) TC.

1126
    if (other_tc == env->current_tc)
1127
        env->active_tc.CP0_TCHalt = arg1;
1128
    else
1129
        env->tcs[other_tc].CP0_TCHalt = arg1;
1130 1131
}

1132
void helper_mtc0_tccontext (target_ulong arg1)
1133
{
1134
    env->active_tc.CP0_TCContext = arg1;
1135 1136
}

1137
void helper_mttc0_tccontext (target_ulong arg1)
1138 1139 1140
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);

1141
    if (other_tc == env->current_tc)
1142
        env->active_tc.CP0_TCContext = arg1;
1143
    else
1144
        env->tcs[other_tc].CP0_TCContext = arg1;
1145 1146
}

1147
void helper_mtc0_tcschedule (target_ulong arg1)
1148
{
1149
    env->active_tc.CP0_TCSchedule = arg1;
1150 1151
}

1152
void helper_mttc0_tcschedule (target_ulong arg1)
1153 1154 1155
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);

1156
    if (other_tc == env->current_tc)
1157
        env->active_tc.CP0_TCSchedule = arg1;
1158
    else
1159
        env->tcs[other_tc].CP0_TCSchedule = arg1;
1160 1161
}

1162
void helper_mtc0_tcschefback (target_ulong arg1)
1163
{
1164
    env->active_tc.CP0_TCScheFBack = arg1;
1165 1166
}

1167
void helper_mttc0_tcschefback (target_ulong arg1)
1168 1169 1170
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);

1171
    if (other_tc == env->current_tc)
1172
        env->active_tc.CP0_TCScheFBack = arg1;
1173
    else
1174
        env->tcs[other_tc].CP0_TCScheFBack = arg1;
1175 1176
}

1177
void helper_mtc0_entrylo1 (target_ulong arg1)
1178 1179 1180
{
    /* Large physaddr (PABITS) not implemented */
    /* 1k pages not implemented */
1181
    env->CP0_EntryLo1 = arg1 & 0x3FFFFFFF;
1182 1183
}

1184
void helper_mtc0_context (target_ulong arg1)
1185
{
1186
    env->CP0_Context = (env->CP0_Context & 0x007FFFFF) | (arg1 & ~0x007FFFFF);
1187 1188
}

1189
void helper_mtc0_pagemask (target_ulong arg1)
1190 1191
{
    /* 1k pages not implemented */
1192
    env->CP0_PageMask = arg1 & (0x1FFFFFFF & (TARGET_PAGE_MASK << 1));
1193 1194
}

1195
void helper_mtc0_pagegrain (target_ulong arg1)
1196 1197 1198 1199 1200 1201 1202
{
    /* SmartMIPS not implemented */
    /* Large physaddr (PABITS) not implemented */
    /* 1k pages not implemented */
    env->CP0_PageGrain = 0;
}

1203
void helper_mtc0_wired (target_ulong arg1)
1204
{
1205
    env->CP0_Wired = arg1 % env->tlb->nb_tlb;
1206 1207
}

1208
void helper_mtc0_srsconf0 (target_ulong arg1)
1209
{
1210
    env->CP0_SRSConf0 |= arg1 & env->CP0_SRSConf0_rw_bitmask;
1211 1212
}

1213
void helper_mtc0_srsconf1 (target_ulong arg1)
1214
{
1215
    env->CP0_SRSConf1 |= arg1 & env->CP0_SRSConf1_rw_bitmask;
1216 1217
}

1218
void helper_mtc0_srsconf2 (target_ulong arg1)
1219
{
1220
    env->CP0_SRSConf2 |= arg1 & env->CP0_SRSConf2_rw_bitmask;
1221 1222
}

1223
void helper_mtc0_srsconf3 (target_ulong arg1)
1224
{
1225
    env->CP0_SRSConf3 |= arg1 & env->CP0_SRSConf3_rw_bitmask;
1226 1227
}

1228
void helper_mtc0_srsconf4 (target_ulong arg1)
1229
{
1230
    env->CP0_SRSConf4 |= arg1 & env->CP0_SRSConf4_rw_bitmask;
1231 1232
}

1233
void helper_mtc0_hwrena (target_ulong arg1)
1234
{
1235
    env->CP0_HWREna = arg1 & 0x0000000F;
1236 1237
}

1238
void helper_mtc0_count (target_ulong arg1)
1239
{
1240
    cpu_mips_store_count(env, arg1);
1241 1242
}

1243
void helper_mtc0_entryhi (target_ulong arg1)
1244 1245 1246 1247
{
    target_ulong old, val;

    /* 1k pages not implemented */
1248
    val = arg1 & ((TARGET_PAGE_MASK << 1) | 0xFF);
1249 1250 1251 1252 1253 1254
#if defined(TARGET_MIPS64)
    val &= env->SEGMask;
#endif
    old = env->CP0_EntryHi;
    env->CP0_EntryHi = val;
    if (env->CP0_Config3 & (1 << CP0C3_MT)) {
1255 1256
        uint32_t tcst = env->active_tc.CP0_TCStatus & ~0xff;
        env->active_tc.CP0_TCStatus = tcst | (val & 0xff);
1257 1258 1259 1260 1261 1262
    }
    /* If the ASID changes, flush qemu's TLB.  */
    if ((old & 0xFF) != (val & 0xFF))
        cpu_mips_tlb_flush(env, 1);
}

1263
void helper_mttc0_entryhi(target_ulong arg1)
1264 1265
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1266
    int32_t tcstatus;
1267

1268
    env->CP0_EntryHi = (env->CP0_EntryHi & 0xff) | (arg1 & ~0xff);
1269
    if (other_tc == env->current_tc) {
1270
        tcstatus = (env->active_tc.CP0_TCStatus & ~0xff) | (arg1 & 0xff);
1271 1272
        env->active_tc.CP0_TCStatus = tcstatus;
    } else {
1273
        tcstatus = (env->tcs[other_tc].CP0_TCStatus & ~0xff) | (arg1 & 0xff);
1274 1275
        env->tcs[other_tc].CP0_TCStatus = tcstatus;
    }
1276 1277
}

1278
void helper_mtc0_compare (target_ulong arg1)
1279
{
1280
    cpu_mips_store_compare(env, arg1);
1281 1282
}

1283
void helper_mtc0_status (target_ulong arg1)
1284 1285 1286 1287
{
    uint32_t val, old;
    uint32_t mask = env->CP0_Status_rw_bitmask;

1288
    val = arg1 & mask;
1289 1290 1291
    old = env->CP0_Status;
    env->CP0_Status = (env->CP0_Status & ~mask) | val;
    compute_hflags(env);
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
    if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
        qemu_log("Status %08x (%08x) => %08x (%08x) Cause %08x",
                old, old & env->CP0_Cause & CP0Ca_IP_mask,
                val, val & env->CP0_Cause & CP0Ca_IP_mask,
                env->CP0_Cause);
        switch (env->hflags & MIPS_HFLAG_KSU) {
        case MIPS_HFLAG_UM: qemu_log(", UM\n"); break;
        case MIPS_HFLAG_SM: qemu_log(", SM\n"); break;
        case MIPS_HFLAG_KM: qemu_log("\n"); break;
        default: cpu_abort(env, "Invalid MMU mode!\n"); break;
A
Aurelien Jarno 已提交
1302
        }
1303
    }
1304 1305
}

1306
void helper_mttc0_status(target_ulong arg1)
1307 1308
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1309
    int32_t tcstatus = env->tcs[other_tc].CP0_TCStatus;
1310

1311 1312 1313 1314
    env->CP0_Status = arg1 & ~0xf1000018;
    tcstatus = (tcstatus & ~(0xf << CP0TCSt_TCU0)) | (arg1 & (0xf << CP0St_CU0));
    tcstatus = (tcstatus & ~(1 << CP0TCSt_TMX)) | ((arg1 & (1 << CP0St_MX)) << (CP0TCSt_TMX - CP0St_MX));
    tcstatus = (tcstatus & ~(0x3 << CP0TCSt_TKSU)) | ((arg1 & (0x3 << CP0St_KSU)) << (CP0TCSt_TKSU - CP0St_KSU));
1315 1316 1317 1318
    if (other_tc == env->current_tc)
        env->active_tc.CP0_TCStatus = tcstatus;
    else
        env->tcs[other_tc].CP0_TCStatus = tcstatus;
1319 1320
}

1321
void helper_mtc0_intctl (target_ulong arg1)
1322 1323
{
    /* vectored interrupts not implemented, no performance counters. */
1324
    env->CP0_IntCtl = (env->CP0_IntCtl & ~0x000002e0) | (arg1 & 0x000002e0);
1325 1326
}

1327
void helper_mtc0_srsctl (target_ulong arg1)
1328 1329
{
    uint32_t mask = (0xf << CP0SRSCtl_ESS) | (0xf << CP0SRSCtl_PSS);
1330
    env->CP0_SRSCtl = (env->CP0_SRSCtl & ~mask) | (arg1 & mask);
1331 1332
}

1333
void helper_mtc0_cause (target_ulong arg1)
1334 1335 1336
{
    uint32_t mask = 0x00C00300;
    uint32_t old = env->CP0_Cause;
1337
    int i;
1338 1339 1340 1341

    if (env->insn_flags & ISA_MIPS32R2)
        mask |= 1 << CP0Ca_DC;

1342
    env->CP0_Cause = (env->CP0_Cause & ~mask) | (arg1 & mask);
1343 1344 1345 1346 1347 1348 1349

    if ((old ^ env->CP0_Cause) & (1 << CP0Ca_DC)) {
        if (env->CP0_Cause & (1 << CP0Ca_DC))
            cpu_mips_stop_count(env);
        else
            cpu_mips_start_count(env);
    }
1350 1351 1352 1353 1354 1355 1356

    /* Set/reset software interrupts */
    for (i = 0 ; i < 2 ; i++) {
        if ((old ^ env->CP0_Cause) & (1 << (CP0Ca_IP + i))) {
            cpu_mips_soft_irq(env, i, env->CP0_Cause & (1 << (CP0Ca_IP + i)));
        }
    }
1357 1358
}

1359
void helper_mtc0_ebase (target_ulong arg1)
1360 1361
{
    /* vectored interrupts not implemented */
1362
    env->CP0_EBase = (env->CP0_EBase & ~0x3FFFF000) | (arg1 & 0x3FFFF000);
1363 1364
}

1365
void helper_mtc0_config0 (target_ulong arg1)
1366
{
1367
    env->CP0_Config0 = (env->CP0_Config0 & 0x81FFFFF8) | (arg1 & 0x00000007);
1368 1369
}

1370
void helper_mtc0_config2 (target_ulong arg1)
1371 1372 1373 1374 1375
{
    /* tertiary/secondary caches not implemented */
    env->CP0_Config2 = (env->CP0_Config2 & 0x8FFF0FFF);
}

1376 1377 1378 1379 1380 1381 1382
void helper_mtc0_lladdr (target_ulong arg1)
{
    target_long mask = env->CP0_LLAddr_rw_bitmask;
    arg1 = arg1 << env->CP0_LLAddr_shift;
    env->lladdr = (env->lladdr & ~mask) | (arg1 & mask);
}

1383
void helper_mtc0_watchlo (target_ulong arg1, uint32_t sel)
1384 1385 1386
{
    /* Watch exceptions for instructions, data loads, data stores
       not implemented. */
1387
    env->CP0_WatchLo[sel] = (arg1 & ~0x7);
1388 1389
}

1390
void helper_mtc0_watchhi (target_ulong arg1, uint32_t sel)
1391
{
1392 1393
    env->CP0_WatchHi[sel] = (arg1 & 0x40FF0FF8);
    env->CP0_WatchHi[sel] &= ~(env->CP0_WatchHi[sel] & arg1 & 0x7);
1394 1395
}

1396
void helper_mtc0_xcontext (target_ulong arg1)
1397 1398
{
    target_ulong mask = (1ULL << (env->SEGBITS - 7)) - 1;
1399
    env->CP0_XContext = (env->CP0_XContext & mask) | (arg1 & ~mask);
1400 1401
}

1402
void helper_mtc0_framemask (target_ulong arg1)
1403
{
1404
    env->CP0_Framemask = arg1; /* XXX */
1405 1406
}

1407
void helper_mtc0_debug (target_ulong arg1)
1408
{
1409 1410
    env->CP0_Debug = (env->CP0_Debug & 0x8C03FC1F) | (arg1 & 0x13300120);
    if (arg1 & (1 << CP0DB_DM))
1411 1412 1413 1414 1415
        env->hflags |= MIPS_HFLAG_DM;
    else
        env->hflags &= ~MIPS_HFLAG_DM;
}

1416
void helper_mttc0_debug(target_ulong arg1)
1417 1418
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);
1419
    uint32_t val = arg1 & ((1 << CP0DB_SSt) | (1 << CP0DB_Halt));
1420 1421

    /* XXX: Might be wrong, check with EJTAG spec. */
1422 1423 1424 1425
    if (other_tc == env->current_tc)
        env->active_tc.CP0_Debug_tcstatus = val;
    else
        env->tcs[other_tc].CP0_Debug_tcstatus = val;
1426
    env->CP0_Debug = (env->CP0_Debug & ((1 << CP0DB_SSt) | (1 << CP0DB_Halt))) |
1427
                     (arg1 & ~((1 << CP0DB_SSt) | (1 << CP0DB_Halt)));
1428 1429
}

1430
void helper_mtc0_performance0 (target_ulong arg1)
1431
{
1432
    env->CP0_Performance0 = arg1 & 0x000007ff;
1433 1434
}

1435
void helper_mtc0_taglo (target_ulong arg1)
1436
{
1437
    env->CP0_TagLo = arg1 & 0xFFFFFCF6;
1438 1439
}

1440
void helper_mtc0_datalo (target_ulong arg1)
1441
{
1442
    env->CP0_DataLo = arg1; /* XXX */
1443 1444
}

1445
void helper_mtc0_taghi (target_ulong arg1)
1446
{
1447
    env->CP0_TagHi = arg1; /* XXX */
1448 1449
}

1450
void helper_mtc0_datahi (target_ulong arg1)
1451
{
1452
    env->CP0_DataHi = arg1; /* XXX */
1453 1454 1455
}

/* MIPS MT functions */
1456
target_ulong helper_mftgpr(uint32_t sel)
1457 1458 1459
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);

1460 1461 1462 1463
    if (other_tc == env->current_tc)
        return env->active_tc.gpr[sel];
    else
        return env->tcs[other_tc].gpr[sel];
1464 1465
}

1466
target_ulong helper_mftlo(uint32_t sel)
1467 1468 1469
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);

1470 1471 1472 1473
    if (other_tc == env->current_tc)
        return env->active_tc.LO[sel];
    else
        return env->tcs[other_tc].LO[sel];
1474 1475
}

1476
target_ulong helper_mfthi(uint32_t sel)
1477 1478 1479
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);

1480 1481 1482 1483
    if (other_tc == env->current_tc)
        return env->active_tc.HI[sel];
    else
        return env->tcs[other_tc].HI[sel];
1484 1485
}

1486
target_ulong helper_mftacx(uint32_t sel)
1487 1488 1489
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);

1490 1491 1492 1493
    if (other_tc == env->current_tc)
        return env->active_tc.ACX[sel];
    else
        return env->tcs[other_tc].ACX[sel];
1494 1495
}

1496
target_ulong helper_mftdsp(void)
1497 1498 1499
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);

1500 1501 1502 1503
    if (other_tc == env->current_tc)
        return env->active_tc.DSPControl;
    else
        return env->tcs[other_tc].DSPControl;
1504
}
B
bellard 已提交
1505

1506
void helper_mttgpr(target_ulong arg1, uint32_t sel)
1507 1508 1509
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);

1510
    if (other_tc == env->current_tc)
1511
        env->active_tc.gpr[sel] = arg1;
1512
    else
1513
        env->tcs[other_tc].gpr[sel] = arg1;
1514 1515
}

1516
void helper_mttlo(target_ulong arg1, uint32_t sel)
1517 1518 1519
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);

1520
    if (other_tc == env->current_tc)
1521
        env->active_tc.LO[sel] = arg1;
1522
    else
1523
        env->tcs[other_tc].LO[sel] = arg1;
1524 1525
}

1526
void helper_mtthi(target_ulong arg1, uint32_t sel)
1527 1528 1529
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);

1530
    if (other_tc == env->current_tc)
1531
        env->active_tc.HI[sel] = arg1;
1532
    else
1533
        env->tcs[other_tc].HI[sel] = arg1;
1534 1535
}

1536
void helper_mttacx(target_ulong arg1, uint32_t sel)
1537 1538 1539
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);

1540
    if (other_tc == env->current_tc)
1541
        env->active_tc.ACX[sel] = arg1;
1542
    else
1543
        env->tcs[other_tc].ACX[sel] = arg1;
1544 1545
}

1546
void helper_mttdsp(target_ulong arg1)
1547 1548 1549
{
    int other_tc = env->CP0_VPEControl & (0xff << CP0VPECo_TargTC);

1550
    if (other_tc == env->current_tc)
1551
        env->active_tc.DSPControl = arg1;
1552
    else
1553
        env->tcs[other_tc].DSPControl = arg1;
1554 1555 1556
}

/* MIPS MT functions */
1557
target_ulong helper_dmt(target_ulong arg1)
1558 1559
{
    // TODO
1560 1561
    arg1 = 0;
    // rt = arg1
1562

1563
    return arg1;
1564 1565
}

1566
target_ulong helper_emt(target_ulong arg1)
1567 1568
{
    // TODO
1569 1570
    arg1 = 0;
    // rt = arg1
1571

1572
    return arg1;
1573 1574
}

1575
target_ulong helper_dvpe(target_ulong arg1)
1576 1577
{
    // TODO
1578 1579
    arg1 = 0;
    // rt = arg1
1580

1581
    return arg1;
1582 1583
}

1584
target_ulong helper_evpe(target_ulong arg1)
1585 1586
{
    // TODO
1587 1588
    arg1 = 0;
    // rt = arg1
1589

1590
    return arg1;
1591
}
1592
#endif /* !CONFIG_USER_ONLY */
1593

1594
void helper_fork(target_ulong arg1, target_ulong arg2)
1595
{
1596 1597
    // arg1 = rt, arg2 = rs
    arg1 = 0;
1598 1599 1600
    // TODO: store to TC register
}

B
Blue Swirl 已提交
1601
target_ulong helper_yield(target_ulong arg)
1602
{
B
Blue Swirl 已提交
1603 1604
    target_long arg1 = arg;

1605
    if (arg1 < 0) {
1606
        /* No scheduling policy implemented. */
1607
        if (arg1 != -2) {
1608
            if (env->CP0_VPEControl & (1 << CP0VPECo_YSI) &&
1609
                env->active_tc.CP0_TCStatus & (1 << CP0TCSt_DT)) {
1610 1611
                env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
                env->CP0_VPEControl |= 4 << CP0VPECo_EXCPT;
1612
                helper_raise_exception(EXCP_THREAD);
1613 1614
            }
        }
1615
    } else if (arg1 == 0) {
A
aurel32 已提交
1616
        if (0 /* TODO: TC underflow */) {
1617
            env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
1618
            helper_raise_exception(EXCP_THREAD);
1619 1620 1621
        } else {
            // TODO: Deallocate TC
        }
1622
    } else if (arg1 > 0) {
1623 1624 1625
        /* Yield qualifier inputs not implemented. */
        env->CP0_VPEControl &= ~(0x7 << CP0VPECo_EXCPT);
        env->CP0_VPEControl |= 2 << CP0VPECo_EXCPT;
1626
        helper_raise_exception(EXCP_THREAD);
1627
    }
1628
    return env->CP0_YQMask;
1629 1630 1631
}

#ifndef CONFIG_USER_ONLY
B
bellard 已提交
1632
/* TLB management */
1633
static void cpu_mips_tlb_flush (CPUState *env, int flush_global)
1634 1635 1636
{
    /* Flush qemu's TLB and discard all shadowed entries.  */
    tlb_flush (env, flush_global);
1637
    env->tlb->tlb_in_use = env->tlb->nb_tlb;
1638 1639
}

1640
static void r4k_mips_tlb_flush_extra (CPUState *env, int first)
1641 1642
{
    /* Discard entries from env->tlb[first] onwards.  */
1643 1644
    while (env->tlb->tlb_in_use > first) {
        r4k_invalidate_tlb(env, --env->tlb->tlb_in_use, 0);
1645 1646 1647
    }
}

1648
static void r4k_fill_tlb (int idx)
B
bellard 已提交
1649
{
A
Anthony Liguori 已提交
1650
    r4k_tlb_t *tlb;
B
bellard 已提交
1651 1652

    /* XXX: detect conflicting TLBs and raise a MCHECK exception when needed */
1653
    tlb = &env->tlb->mmu.r4k.tlb[idx];
T
ths 已提交
1654
    tlb->VPN = env->CP0_EntryHi & (TARGET_PAGE_MASK << 1);
1655
#if defined(TARGET_MIPS64)
T
ths 已提交
1656
    tlb->VPN &= env->SEGMask;
1657
#endif
1658
    tlb->ASID = env->CP0_EntryHi & 0xFF;
T
ths 已提交
1659
    tlb->PageMask = env->CP0_PageMask;
B
bellard 已提交
1660
    tlb->G = env->CP0_EntryLo0 & env->CP0_EntryLo1 & 1;
1661 1662 1663
    tlb->V0 = (env->CP0_EntryLo0 & 2) != 0;
    tlb->D0 = (env->CP0_EntryLo0 & 4) != 0;
    tlb->C0 = (env->CP0_EntryLo0 >> 3) & 0x7;
B
bellard 已提交
1664
    tlb->PFN[0] = (env->CP0_EntryLo0 >> 6) << 12;
1665 1666 1667
    tlb->V1 = (env->CP0_EntryLo1 & 2) != 0;
    tlb->D1 = (env->CP0_EntryLo1 & 4) != 0;
    tlb->C1 = (env->CP0_EntryLo1 >> 3) & 0x7;
B
bellard 已提交
1668 1669 1670
    tlb->PFN[1] = (env->CP0_EntryLo1 >> 6) << 12;
}

1671
void r4k_helper_tlbwi (void)
B
bellard 已提交
1672
{
A
aurel32 已提交
1673 1674 1675 1676
    int idx;

    idx = (env->CP0_Index & ~0x80000000) % env->tlb->nb_tlb;

1677 1678 1679
    /* Discard cached TLB entries.  We could avoid doing this if the
       tlbwi is just upgrading access permissions on the current entry;
       that might be a further win.  */
1680
    r4k_mips_tlb_flush_extra (env, env->tlb->nb_tlb);
1681

A
aurel32 已提交
1682 1683
    r4k_invalidate_tlb(env, idx, 0);
    r4k_fill_tlb(idx);
B
bellard 已提交
1684 1685
}

1686
void r4k_helper_tlbwr (void)
B
bellard 已提交
1687 1688 1689
{
    int r = cpu_mips_get_random(env);

1690 1691
    r4k_invalidate_tlb(env, r, 1);
    r4k_fill_tlb(r);
B
bellard 已提交
1692 1693
}

1694
void r4k_helper_tlbp (void)
B
bellard 已提交
1695
{
A
Anthony Liguori 已提交
1696
    r4k_tlb_t *tlb;
T
ths 已提交
1697
    target_ulong mask;
B
bellard 已提交
1698
    target_ulong tag;
T
ths 已提交
1699
    target_ulong VPN;
B
bellard 已提交
1700 1701 1702
    uint8_t ASID;
    int i;

B
bellard 已提交
1703
    ASID = env->CP0_EntryHi & 0xFF;
1704 1705
    for (i = 0; i < env->tlb->nb_tlb; i++) {
        tlb = &env->tlb->mmu.r4k.tlb[i];
T
ths 已提交
1706 1707 1708 1709
        /* 1k pages are not supported. */
        mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1);
        tag = env->CP0_EntryHi & ~mask;
        VPN = tlb->VPN & ~mask;
B
bellard 已提交
1710
        /* Check ASID, virtual page number & size */
T
ths 已提交
1711
        if ((tlb->G == 1 || tlb->ASID == ASID) && VPN == tag) {
B
bellard 已提交
1712
            /* TLB match */
T
ths 已提交
1713
            env->CP0_Index = i;
B
bellard 已提交
1714 1715 1716
            break;
        }
    }
1717
    if (i == env->tlb->nb_tlb) {
1718
        /* No match.  Discard any shadow entries, if any of them match.  */
1719
        for (i = env->tlb->nb_tlb; i < env->tlb->tlb_in_use; i++) {
A
aurel32 已提交
1720 1721 1722 1723 1724 1725 1726
            tlb = &env->tlb->mmu.r4k.tlb[i];
            /* 1k pages are not supported. */
            mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1);
            tag = env->CP0_EntryHi & ~mask;
            VPN = tlb->VPN & ~mask;
            /* Check ASID, virtual page number & size */
            if ((tlb->G == 1 || tlb->ASID == ASID) && VPN == tag) {
1727
                r4k_mips_tlb_flush_extra (env, i);
A
aurel32 已提交
1728 1729 1730
                break;
            }
        }
1731

T
ths 已提交
1732
        env->CP0_Index |= 0x80000000;
B
bellard 已提交
1733 1734 1735
    }
}

1736
void r4k_helper_tlbr (void)
B
bellard 已提交
1737
{
A
Anthony Liguori 已提交
1738
    r4k_tlb_t *tlb;
1739
    uint8_t ASID;
A
aurel32 已提交
1740
    int idx;
B
bellard 已提交
1741

1742
    ASID = env->CP0_EntryHi & 0xFF;
A
aurel32 已提交
1743 1744
    idx = (env->CP0_Index & ~0x80000000) % env->tlb->nb_tlb;
    tlb = &env->tlb->mmu.r4k.tlb[idx];
B
bellard 已提交
1745 1746

    /* If this will change the current ASID, flush qemu's TLB.  */
1747 1748 1749
    if (ASID != tlb->ASID)
        cpu_mips_tlb_flush (env, 1);

1750
    r4k_mips_tlb_flush_extra(env, env->tlb->nb_tlb);
B
bellard 已提交
1751

B
bellard 已提交
1752
    env->CP0_EntryHi = tlb->VPN | tlb->ASID;
T
ths 已提交
1753
    env->CP0_PageMask = tlb->PageMask;
T
ths 已提交
1754 1755 1756 1757
    env->CP0_EntryLo0 = tlb->G | (tlb->V0 << 1) | (tlb->D0 << 2) |
                        (tlb->C0 << 3) | (tlb->PFN[0] >> 6);
    env->CP0_EntryLo1 = tlb->G | (tlb->V1 << 1) | (tlb->D1 << 2) |
                        (tlb->C1 << 3) | (tlb->PFN[1] >> 6);
B
bellard 已提交
1758 1759
}

1760
void helper_tlbwi(void)
P
pbrook 已提交
1761
{
1762
    env->tlb->helper_tlbwi();
P
pbrook 已提交
1763 1764
}

1765
void helper_tlbwr(void)
P
pbrook 已提交
1766
{
1767
    env->tlb->helper_tlbwr();
P
pbrook 已提交
1768 1769
}

1770
void helper_tlbp(void)
P
pbrook 已提交
1771
{
1772
    env->tlb->helper_tlbp();
P
pbrook 已提交
1773 1774
}

1775
void helper_tlbr(void)
P
pbrook 已提交
1776
{
1777
    env->tlb->helper_tlbr();
P
pbrook 已提交
1778 1779
}

1780
/* Specials */
1781
target_ulong helper_di (void)
1782
{
1783 1784
    target_ulong t0 = env->CP0_Status;

1785 1786
    env->CP0_Status = t0 & ~(1 << CP0St_IE);
    return t0;
1787 1788
}

1789
target_ulong helper_ei (void)
1790
{
1791 1792
    target_ulong t0 = env->CP0_Status;

1793 1794
    env->CP0_Status = t0 | (1 << CP0St_IE);
    return t0;
1795 1796
}

A
aurel32 已提交
1797
static void debug_pre_eret (void)
B
bellard 已提交
1798
{
1799
    if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
1800 1801 1802 1803 1804 1805 1806 1807
        qemu_log("ERET: PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx,
                env->active_tc.PC, env->CP0_EPC);
        if (env->CP0_Status & (1 << CP0St_ERL))
            qemu_log(" ErrorEPC " TARGET_FMT_lx, env->CP0_ErrorEPC);
        if (env->hflags & MIPS_HFLAG_DM)
            qemu_log(" DEPC " TARGET_FMT_lx, env->CP0_DEPC);
        qemu_log("\n");
    }
1808 1809
}

A
aurel32 已提交
1810
static void debug_post_eret (void)
1811
{
1812
    if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824
        qemu_log("  =>  PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx,
                env->active_tc.PC, env->CP0_EPC);
        if (env->CP0_Status & (1 << CP0St_ERL))
            qemu_log(" ErrorEPC " TARGET_FMT_lx, env->CP0_ErrorEPC);
        if (env->hflags & MIPS_HFLAG_DM)
            qemu_log(" DEPC " TARGET_FMT_lx, env->CP0_DEPC);
        switch (env->hflags & MIPS_HFLAG_KSU) {
        case MIPS_HFLAG_UM: qemu_log(", UM\n"); break;
        case MIPS_HFLAG_SM: qemu_log(", SM\n"); break;
        case MIPS_HFLAG_KM: qemu_log("\n"); break;
        default: cpu_abort(env, "Invalid MMU mode!\n"); break;
        }
T
ths 已提交
1825
    }
B
bellard 已提交
1826 1827
}

1828 1829 1830 1831 1832 1833 1834 1835 1836 1837
static void set_pc (target_ulong error_pc)
{
    env->active_tc.PC = error_pc & ~(target_ulong)1;
    if (error_pc & 1) {
        env->hflags |= MIPS_HFLAG_M16;
    } else {
        env->hflags &= ~(MIPS_HFLAG_M16);
    }
}

1838
void helper_eret (void)
1839
{
1840
    debug_pre_eret();
1841
    if (env->CP0_Status & (1 << CP0St_ERL)) {
1842
        set_pc(env->CP0_ErrorEPC);
1843 1844
        env->CP0_Status &= ~(1 << CP0St_ERL);
    } else {
1845
        set_pc(env->CP0_EPC);
1846 1847 1848
        env->CP0_Status &= ~(1 << CP0St_EXL);
    }
    compute_hflags(env);
1849
    debug_post_eret();
1850
    env->lladdr = 1;
1851 1852
}

1853
void helper_deret (void)
1854
{
1855
    debug_pre_eret();
1856 1857
    set_pc(env->CP0_DEPC);

1858 1859
    env->hflags &= MIPS_HFLAG_DM;
    compute_hflags(env);
1860
    debug_post_eret();
1861
    env->lladdr = 1;
1862
}
T
ths 已提交
1863
#endif /* !CONFIG_USER_ONLY */
1864

1865
target_ulong helper_rdhwr_cpunum(void)
1866 1867 1868
{
    if ((env->hflags & MIPS_HFLAG_CP0) ||
        (env->CP0_HWREna & (1 << 0)))
1869
        return env->CP0_EBase & 0x3ff;
1870
    else
1871
        helper_raise_exception(EXCP_RI);
1872

1873
    return 0;
1874 1875
}

1876
target_ulong helper_rdhwr_synci_step(void)
1877 1878 1879
{
    if ((env->hflags & MIPS_HFLAG_CP0) ||
        (env->CP0_HWREna & (1 << 1)))
1880
        return env->SYNCI_Step;
1881
    else
1882
        helper_raise_exception(EXCP_RI);
1883

1884
    return 0;
1885 1886
}

1887
target_ulong helper_rdhwr_cc(void)
1888 1889 1890
{
    if ((env->hflags & MIPS_HFLAG_CP0) ||
        (env->CP0_HWREna & (1 << 2)))
1891
        return env->CP0_Count;
1892
    else
1893
        helper_raise_exception(EXCP_RI);
1894

1895
    return 0;
1896 1897
}

1898
target_ulong helper_rdhwr_ccres(void)
1899 1900 1901
{
    if ((env->hflags & MIPS_HFLAG_CP0) ||
        (env->CP0_HWREna & (1 << 3)))
1902
        return env->CCRes;
1903
    else
1904
        helper_raise_exception(EXCP_RI);
1905

1906
    return 0;
1907 1908
}

1909
void helper_pmon (int function)
B
bellard 已提交
1910 1911 1912 1913
{
    function /= 2;
    switch (function) {
    case 2: /* TODO: char inbyte(int waitflag); */
1914 1915
        if (env->active_tc.gpr[4] == 0)
            env->active_tc.gpr[2] = -1;
B
bellard 已提交
1916 1917
        /* Fall through */
    case 11: /* TODO: char inbyte (void); */
1918
        env->active_tc.gpr[2] = -1;
B
bellard 已提交
1919 1920 1921
        break;
    case 3:
    case 12:
1922
        printf("%c", (char)(env->active_tc.gpr[4] & 0xFF));
B
bellard 已提交
1923 1924 1925 1926 1927
        break;
    case 17:
        break;
    case 158:
        {
1928
            unsigned char *fmt = (void *)(unsigned long)env->active_tc.gpr[4];
B
bellard 已提交
1929 1930 1931 1932 1933
            printf("%s", fmt);
        }
        break;
    }
}
1934

1935
void helper_wait (void)
T
ths 已提交
1936 1937
{
    env->halted = 1;
1938
    helper_raise_exception(EXCP_HLT);
T
ths 已提交
1939 1940
}

1941
#if !defined(CONFIG_USER_ONLY)
1942

B
bellard 已提交
1943 1944
static void do_unaligned_access (target_ulong addr, int is_write, int is_user, void *retaddr);

1945
#define MMUSUFFIX _mmu
B
bellard 已提交
1946
#define ALIGNED_ONLY
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959

#define SHIFT 0
#include "softmmu_template.h"

#define SHIFT 1
#include "softmmu_template.h"

#define SHIFT 2
#include "softmmu_template.h"

#define SHIFT 3
#include "softmmu_template.h"

B
bellard 已提交
1960 1961 1962 1963
static void do_unaligned_access (target_ulong addr, int is_write, int is_user, void *retaddr)
{
    env->CP0_BadVAddr = addr;
    do_restore_state (retaddr);
1964
    helper_raise_exception ((is_write == 1) ? EXCP_AdES : EXCP_AdEL);
B
bellard 已提交
1965 1966
}

1967
void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977
{
    TranslationBlock *tb;
    CPUState *saved_env;
    unsigned long pc;
    int ret;

    /* XXX: hack to restore env in all cases, even if not called from
       generated code */
    saved_env = env;
    env = cpu_single_env;
1978
    ret = cpu_mips_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989
    if (ret) {
        if (retaddr) {
            /* now we have a real cpu fault */
            pc = (unsigned long)retaddr;
            tb = tb_find_pc(pc);
            if (tb) {
                /* the PC is inside the translated code. It means that we have
                   a virtual CPU fault */
                cpu_restore_state(tb, env, pc, NULL);
            }
        }
1990
        helper_raise_exception_err(env->exception_index, env->error_code);
1991 1992 1993 1994
    }
    env = saved_env;
}

A
Anthony Liguori 已提交
1995
void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
1996
                          int unused, int size)
T
ths 已提交
1997 1998
{
    if (is_exec)
1999
        helper_raise_exception(EXCP_IBE);
T
ths 已提交
2000
    else
2001
        helper_raise_exception(EXCP_DBE);
T
ths 已提交
2002
}
2003
#endif /* !CONFIG_USER_ONLY */
2004 2005 2006

/* Complex FPU operations which may need stack space. */

P
pbrook 已提交
2007 2008 2009 2010
#define FLOAT_ONE32 make_float32(0x3f8 << 20)
#define FLOAT_ONE64 make_float64(0x3ffULL << 52)
#define FLOAT_TWO32 make_float32(1 << 30)
#define FLOAT_TWO64 make_float64(1ULL << 62)
T
ths 已提交
2011 2012 2013 2014
#define FLOAT_QNAN32 0x7fbfffff
#define FLOAT_QNAN64 0x7ff7ffffffffffffULL
#define FLOAT_SNAN32 0x7fffffff
#define FLOAT_SNAN64 0x7fffffffffffffffULL
T
ths 已提交
2015

2016
/* convert MIPS rounding mode in FCR31 to IEEE library */
B
Blue Swirl 已提交
2017
static unsigned int ieee_rm[] = {
2018 2019 2020 2021 2022 2023 2024
    float_round_nearest_even,
    float_round_to_zero,
    float_round_up,
    float_round_down
};

#define RESTORE_ROUNDING_MODE \
2025
    set_float_rounding_mode(ieee_rm[env->active_fpu.fcr31 & 3], &env->active_fpu.fp_status)
2026

2027 2028 2029
#define RESTORE_FLUSH_MODE \
    set_flush_to_zero((env->active_fpu.fcr31 & (1 << 24)) != 0, &env->active_fpu.fp_status);

2030
target_ulong helper_cfc1 (uint32_t reg)
2031
{
2032
    target_ulong arg1;
2033

2034 2035
    switch (reg) {
    case 0:
2036
        arg1 = (int32_t)env->active_fpu.fcr0;
2037 2038
        break;
    case 25:
2039
        arg1 = ((env->active_fpu.fcr31 >> 24) & 0xfe) | ((env->active_fpu.fcr31 >> 23) & 0x1);
2040 2041
        break;
    case 26:
2042
        arg1 = env->active_fpu.fcr31 & 0x0003f07c;
2043 2044
        break;
    case 28:
2045
        arg1 = (env->active_fpu.fcr31 & 0x00000f83) | ((env->active_fpu.fcr31 >> 22) & 0x4);
2046 2047
        break;
    default:
2048
        arg1 = (int32_t)env->active_fpu.fcr31;
2049 2050
        break;
    }
2051

2052
    return arg1;
2053 2054
}

2055
void helper_ctc1 (target_ulong arg1, uint32_t reg)
2056 2057
{
    switch(reg) {
2058
    case 25:
2059
        if (arg1 & 0xffffff00)
2060
            return;
2061 2062
        env->active_fpu.fcr31 = (env->active_fpu.fcr31 & 0x017fffff) | ((arg1 & 0xfe) << 24) |
                     ((arg1 & 0x1) << 23);
2063 2064
        break;
    case 26:
2065
        if (arg1 & 0x007c0000)
2066
            return;
2067
        env->active_fpu.fcr31 = (env->active_fpu.fcr31 & 0xfffc0f83) | (arg1 & 0x0003f07c);
2068 2069
        break;
    case 28:
2070
        if (arg1 & 0x007c0000)
2071
            return;
2072 2073
        env->active_fpu.fcr31 = (env->active_fpu.fcr31 & 0xfefff07c) | (arg1 & 0x00000f83) |
                     ((arg1 & 0x4) << 22);
2074 2075
        break;
    case 31:
2076
        if (arg1 & 0x007c0000)
2077
            return;
2078
        env->active_fpu.fcr31 = arg1;
2079 2080 2081 2082 2083 2084
        break;
    default:
        return;
    }
    /* set rounding mode */
    RESTORE_ROUNDING_MODE;
2085 2086
    /* set flush-to-zero mode */
    RESTORE_FLUSH_MODE;
2087 2088
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    if ((GET_FP_ENABLE(env->active_fpu.fcr31) | 0x20) & GET_FP_CAUSE(env->active_fpu.fcr31))
2089
        helper_raise_exception(EXCP_FPE);
2090 2091
}

T
ths 已提交
2092
static inline char ieee_ex_to_mips(char xcpt)
2093 2094 2095 2096 2097 2098 2099 2100
{
    return (xcpt & float_flag_inexact) >> 5 |
           (xcpt & float_flag_underflow) >> 3 |
           (xcpt & float_flag_overflow) >> 1 |
           (xcpt & float_flag_divbyzero) << 1 |
           (xcpt & float_flag_invalid) << 4;
}

T
ths 已提交
2101
static inline char mips_ex_to_ieee(char xcpt)
2102 2103 2104 2105 2106 2107 2108 2109
{
    return (xcpt & FP_INEXACT) << 5 |
           (xcpt & FP_UNDERFLOW) << 3 |
           (xcpt & FP_OVERFLOW) << 1 |
           (xcpt & FP_DIV0) >> 1 |
           (xcpt & FP_INVALID) >> 4;
}

T
ths 已提交
2110
static inline void update_fcr31(void)
2111
{
2112
    int tmp = ieee_ex_to_mips(get_float_exception_flags(&env->active_fpu.fp_status));
2113

2114 2115
    SET_FP_CAUSE(env->active_fpu.fcr31, tmp);
    if (GET_FP_ENABLE(env->active_fpu.fcr31) & tmp)
2116
        helper_raise_exception(EXCP_FPE);
2117
    else
2118
        UPDATE_FP_FLAGS(env->active_fpu.fcr31, tmp);
2119 2120
}

2121 2122 2123 2124 2125 2126
/* Float support.
   Single precition routines have a "s" suffix, double precision a
   "d" suffix, 32bit integer "w", 64bit integer "l", paired single "ps",
   paired single lower "pl", paired single upper "pu".  */

/* unary operations, modifying fp status  */
2127
uint64_t helper_float_sqrt_d(uint64_t fdt0)
2128
{
2129
    return float64_sqrt(fdt0, &env->active_fpu.fp_status);
2130 2131
}

2132
uint32_t helper_float_sqrt_s(uint32_t fst0)
2133
{
2134
    return float32_sqrt(fst0, &env->active_fpu.fp_status);
2135
}
2136

2137
uint64_t helper_float_cvtd_s(uint32_t fst0)
2138
{
2139 2140
    uint64_t fdt2;

2141 2142
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    fdt2 = float32_to_float64(fst0, &env->active_fpu.fp_status);
2143
    update_fcr31();
2144
    return fdt2;
2145
}
2146

2147
uint64_t helper_float_cvtd_w(uint32_t wt0)
2148
{
2149 2150
    uint64_t fdt2;

2151 2152
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    fdt2 = int32_to_float64(wt0, &env->active_fpu.fp_status);
2153
    update_fcr31();
2154
    return fdt2;
2155
}
2156

2157
uint64_t helper_float_cvtd_l(uint64_t dt0)
2158
{
2159 2160
    uint64_t fdt2;

2161 2162
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    fdt2 = int64_to_float64(dt0, &env->active_fpu.fp_status);
2163
    update_fcr31();
2164
    return fdt2;
2165
}
2166

2167
uint64_t helper_float_cvtl_d(uint64_t fdt0)
2168
{
2169 2170
    uint64_t dt2;

2171 2172
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
2173
    update_fcr31();
2174
    if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2175 2176
        dt2 = FLOAT_SNAN64;
    return dt2;
2177
}
2178

2179
uint64_t helper_float_cvtl_s(uint32_t fst0)
2180
{
2181 2182
    uint64_t dt2;

2183 2184
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
2185
    update_fcr31();
2186
    if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2187 2188
        dt2 = FLOAT_SNAN64;
    return dt2;
2189 2190
}

2191
uint64_t helper_float_cvtps_pw(uint64_t dt0)
2192
{
2193 2194 2195
    uint32_t fst2;
    uint32_t fsth2;

2196 2197 2198
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    fst2 = int32_to_float32(dt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
    fsth2 = int32_to_float32(dt0 >> 32, &env->active_fpu.fp_status);
2199
    update_fcr31();
2200
    return ((uint64_t)fsth2 << 32) | fst2;
2201
}
2202

2203
uint64_t helper_float_cvtpw_ps(uint64_t fdt0)
2204
{
2205 2206 2207
    uint32_t wt2;
    uint32_t wth2;

2208 2209 2210
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    wt2 = float32_to_int32(fdt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
    wth2 = float32_to_int32(fdt0 >> 32, &env->active_fpu.fp_status);
2211
    update_fcr31();
2212
    if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID)) {
2213 2214 2215 2216
        wt2 = FLOAT_SNAN32;
        wth2 = FLOAT_SNAN32;
    }
    return ((uint64_t)wth2 << 32) | wt2;
2217
}
2218

2219
uint32_t helper_float_cvts_d(uint64_t fdt0)
2220
{
2221 2222
    uint32_t fst2;

2223 2224
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    fst2 = float64_to_float32(fdt0, &env->active_fpu.fp_status);
2225
    update_fcr31();
2226
    return fst2;
2227
}
2228

2229
uint32_t helper_float_cvts_w(uint32_t wt0)
2230
{
2231 2232
    uint32_t fst2;

2233 2234
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    fst2 = int32_to_float32(wt0, &env->active_fpu.fp_status);
2235
    update_fcr31();
2236
    return fst2;
2237
}
2238

2239
uint32_t helper_float_cvts_l(uint64_t dt0)
2240
{
2241 2242
    uint32_t fst2;

2243 2244
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    fst2 = int64_to_float32(dt0, &env->active_fpu.fp_status);
2245
    update_fcr31();
2246
    return fst2;
2247
}
2248

2249
uint32_t helper_float_cvts_pl(uint32_t wt0)
2250
{
2251 2252
    uint32_t wt2;

2253
    set_float_exception_flags(0, &env->active_fpu.fp_status);
2254
    wt2 = wt0;
2255
    update_fcr31();
2256
    return wt2;
2257
}
2258

2259
uint32_t helper_float_cvts_pu(uint32_t wth0)
2260
{
2261 2262
    uint32_t wt2;

2263
    set_float_exception_flags(0, &env->active_fpu.fp_status);
2264
    wt2 = wth0;
2265
    update_fcr31();
2266
    return wt2;
2267
}
2268

2269
uint32_t helper_float_cvtw_s(uint32_t fst0)
2270
{
2271 2272
    uint32_t wt2;

2273 2274
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
2275
    update_fcr31();
2276
    if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2277 2278
        wt2 = FLOAT_SNAN32;
    return wt2;
2279
}
2280

2281
uint32_t helper_float_cvtw_d(uint64_t fdt0)
2282
{
2283 2284
    uint32_t wt2;

2285 2286
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
2287
    update_fcr31();
2288
    if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2289 2290
        wt2 = FLOAT_SNAN32;
    return wt2;
2291 2292
}

2293
uint64_t helper_float_roundl_d(uint64_t fdt0)
2294
{
2295 2296
    uint64_t dt2;

2297 2298
    set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
    dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
2299 2300
    RESTORE_ROUNDING_MODE;
    update_fcr31();
2301
    if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2302 2303
        dt2 = FLOAT_SNAN64;
    return dt2;
2304
}
2305

2306
uint64_t helper_float_roundl_s(uint32_t fst0)
2307
{
2308 2309
    uint64_t dt2;

2310 2311
    set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
    dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
2312 2313
    RESTORE_ROUNDING_MODE;
    update_fcr31();
2314
    if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2315 2316
        dt2 = FLOAT_SNAN64;
    return dt2;
2317
}
2318

2319
uint32_t helper_float_roundw_d(uint64_t fdt0)
2320
{
2321 2322
    uint32_t wt2;

2323 2324
    set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
    wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
2325 2326
    RESTORE_ROUNDING_MODE;
    update_fcr31();
2327
    if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2328 2329
        wt2 = FLOAT_SNAN32;
    return wt2;
2330
}
2331

2332
uint32_t helper_float_roundw_s(uint32_t fst0)
2333
{
2334 2335
    uint32_t wt2;

2336 2337
    set_float_rounding_mode(float_round_nearest_even, &env->active_fpu.fp_status);
    wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
2338 2339
    RESTORE_ROUNDING_MODE;
    update_fcr31();
2340
    if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2341 2342
        wt2 = FLOAT_SNAN32;
    return wt2;
2343 2344
}

2345
uint64_t helper_float_truncl_d(uint64_t fdt0)
2346
{
2347 2348
    uint64_t dt2;

2349
    dt2 = float64_to_int64_round_to_zero(fdt0, &env->active_fpu.fp_status);
2350
    update_fcr31();
2351
    if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2352 2353
        dt2 = FLOAT_SNAN64;
    return dt2;
2354
}
2355

2356
uint64_t helper_float_truncl_s(uint32_t fst0)
2357
{
2358 2359
    uint64_t dt2;

2360
    dt2 = float32_to_int64_round_to_zero(fst0, &env->active_fpu.fp_status);
2361
    update_fcr31();
2362
    if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2363 2364
        dt2 = FLOAT_SNAN64;
    return dt2;
2365
}
2366

2367
uint32_t helper_float_truncw_d(uint64_t fdt0)
2368
{
2369 2370
    uint32_t wt2;

2371
    wt2 = float64_to_int32_round_to_zero(fdt0, &env->active_fpu.fp_status);
2372
    update_fcr31();
2373
    if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2374 2375
        wt2 = FLOAT_SNAN32;
    return wt2;
2376
}
2377

2378
uint32_t helper_float_truncw_s(uint32_t fst0)
2379
{
2380 2381
    uint32_t wt2;

2382
    wt2 = float32_to_int32_round_to_zero(fst0, &env->active_fpu.fp_status);
2383
    update_fcr31();
2384
    if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2385 2386
        wt2 = FLOAT_SNAN32;
    return wt2;
2387 2388
}

2389
uint64_t helper_float_ceill_d(uint64_t fdt0)
2390
{
2391 2392
    uint64_t dt2;

2393 2394
    set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
    dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
2395 2396
    RESTORE_ROUNDING_MODE;
    update_fcr31();
2397
    if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2398 2399
        dt2 = FLOAT_SNAN64;
    return dt2;
2400
}
2401

2402
uint64_t helper_float_ceill_s(uint32_t fst0)
2403
{
2404 2405
    uint64_t dt2;

2406 2407
    set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
    dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
2408 2409
    RESTORE_ROUNDING_MODE;
    update_fcr31();
2410
    if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2411 2412
        dt2 = FLOAT_SNAN64;
    return dt2;
2413
}
2414

2415
uint32_t helper_float_ceilw_d(uint64_t fdt0)
2416
{
2417 2418
    uint32_t wt2;

2419 2420
    set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
    wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
2421 2422
    RESTORE_ROUNDING_MODE;
    update_fcr31();
2423
    if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2424 2425
        wt2 = FLOAT_SNAN32;
    return wt2;
2426
}
2427

2428
uint32_t helper_float_ceilw_s(uint32_t fst0)
2429
{
2430 2431
    uint32_t wt2;

2432 2433
    set_float_rounding_mode(float_round_up, &env->active_fpu.fp_status);
    wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
2434 2435
    RESTORE_ROUNDING_MODE;
    update_fcr31();
2436
    if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2437 2438
        wt2 = FLOAT_SNAN32;
    return wt2;
2439 2440
}

2441
uint64_t helper_float_floorl_d(uint64_t fdt0)
2442
{
2443 2444
    uint64_t dt2;

2445 2446
    set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
    dt2 = float64_to_int64(fdt0, &env->active_fpu.fp_status);
2447 2448
    RESTORE_ROUNDING_MODE;
    update_fcr31();
2449
    if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2450 2451
        dt2 = FLOAT_SNAN64;
    return dt2;
2452
}
2453

2454
uint64_t helper_float_floorl_s(uint32_t fst0)
2455
{
2456 2457
    uint64_t dt2;

2458 2459
    set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
    dt2 = float32_to_int64(fst0, &env->active_fpu.fp_status);
2460 2461
    RESTORE_ROUNDING_MODE;
    update_fcr31();
2462
    if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2463 2464
        dt2 = FLOAT_SNAN64;
    return dt2;
2465
}
2466

2467
uint32_t helper_float_floorw_d(uint64_t fdt0)
2468
{
2469 2470
    uint32_t wt2;

2471 2472
    set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
    wt2 = float64_to_int32(fdt0, &env->active_fpu.fp_status);
2473 2474
    RESTORE_ROUNDING_MODE;
    update_fcr31();
2475
    if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2476 2477
        wt2 = FLOAT_SNAN32;
    return wt2;
2478
}
2479

2480
uint32_t helper_float_floorw_s(uint32_t fst0)
2481
{
2482 2483
    uint32_t wt2;

2484 2485
    set_float_rounding_mode(float_round_down, &env->active_fpu.fp_status);
    wt2 = float32_to_int32(fst0, &env->active_fpu.fp_status);
2486 2487
    RESTORE_ROUNDING_MODE;
    update_fcr31();
2488
    if (GET_FP_CAUSE(env->active_fpu.fcr31) & (FP_OVERFLOW | FP_INVALID))
2489 2490
        wt2 = FLOAT_SNAN32;
    return wt2;
2491 2492
}

2493
/* unary operations, not modifying fp status  */
2494
#define FLOAT_UNOP(name)                                       \
2495
uint64_t helper_float_ ## name ## _d(uint64_t fdt0)                \
2496 2497 2498
{                                                              \
    return float64_ ## name(fdt0);                             \
}                                                              \
2499
uint32_t helper_float_ ## name ## _s(uint32_t fst0)                \
2500 2501 2502
{                                                              \
    return float32_ ## name(fst0);                             \
}                                                              \
2503
uint64_t helper_float_ ## name ## _ps(uint64_t fdt0)               \
2504 2505 2506 2507 2508 2509 2510
{                                                              \
    uint32_t wt0;                                              \
    uint32_t wth0;                                             \
                                                               \
    wt0 = float32_ ## name(fdt0 & 0XFFFFFFFF);                 \
    wth0 = float32_ ## name(fdt0 >> 32);                       \
    return ((uint64_t)wth0 << 32) | wt0;                       \
2511 2512 2513 2514 2515
}
FLOAT_UNOP(abs)
FLOAT_UNOP(chs)
#undef FLOAT_UNOP

T
ths 已提交
2516
/* MIPS specific unary operations */
2517
uint64_t helper_float_recip_d(uint64_t fdt0)
T
ths 已提交
2518
{
2519 2520
    uint64_t fdt2;

2521 2522
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    fdt2 = float64_div(FLOAT_ONE64, fdt0, &env->active_fpu.fp_status);
T
ths 已提交
2523
    update_fcr31();
2524
    return fdt2;
T
ths 已提交
2525
}
2526

2527
uint32_t helper_float_recip_s(uint32_t fst0)
T
ths 已提交
2528
{
2529 2530
    uint32_t fst2;

2531 2532
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    fst2 = float32_div(FLOAT_ONE32, fst0, &env->active_fpu.fp_status);
T
ths 已提交
2533
    update_fcr31();
2534
    return fst2;
T
ths 已提交
2535 2536
}

2537
uint64_t helper_float_rsqrt_d(uint64_t fdt0)
T
ths 已提交
2538
{
2539 2540
    uint64_t fdt2;

2541 2542 2543
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    fdt2 = float64_sqrt(fdt0, &env->active_fpu.fp_status);
    fdt2 = float64_div(FLOAT_ONE64, fdt2, &env->active_fpu.fp_status);
T
ths 已提交
2544
    update_fcr31();
2545
    return fdt2;
T
ths 已提交
2546
}
2547

2548
uint32_t helper_float_rsqrt_s(uint32_t fst0)
T
ths 已提交
2549
{
2550 2551
    uint32_t fst2;

2552 2553 2554
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    fst2 = float32_sqrt(fst0, &env->active_fpu.fp_status);
    fst2 = float32_div(FLOAT_ONE32, fst2, &env->active_fpu.fp_status);
T
ths 已提交
2555
    update_fcr31();
2556
    return fst2;
T
ths 已提交
2557 2558
}

2559
uint64_t helper_float_recip1_d(uint64_t fdt0)
T
ths 已提交
2560
{
2561 2562
    uint64_t fdt2;

2563 2564
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    fdt2 = float64_div(FLOAT_ONE64, fdt0, &env->active_fpu.fp_status);
T
ths 已提交
2565
    update_fcr31();
2566
    return fdt2;
T
ths 已提交
2567
}
2568

2569
uint32_t helper_float_recip1_s(uint32_t fst0)
T
ths 已提交
2570
{
2571 2572
    uint32_t fst2;

2573 2574
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    fst2 = float32_div(FLOAT_ONE32, fst0, &env->active_fpu.fp_status);
T
ths 已提交
2575
    update_fcr31();
2576
    return fst2;
T
ths 已提交
2577
}
2578

2579
uint64_t helper_float_recip1_ps(uint64_t fdt0)
T
ths 已提交
2580
{
2581 2582 2583
    uint32_t fst2;
    uint32_t fsth2;

2584 2585 2586
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    fst2 = float32_div(FLOAT_ONE32, fdt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
    fsth2 = float32_div(FLOAT_ONE32, fdt0 >> 32, &env->active_fpu.fp_status);
T
ths 已提交
2587
    update_fcr31();
2588
    return ((uint64_t)fsth2 << 32) | fst2;
T
ths 已提交
2589 2590
}

2591
uint64_t helper_float_rsqrt1_d(uint64_t fdt0)
T
ths 已提交
2592
{
2593 2594
    uint64_t fdt2;

2595 2596 2597
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    fdt2 = float64_sqrt(fdt0, &env->active_fpu.fp_status);
    fdt2 = float64_div(FLOAT_ONE64, fdt2, &env->active_fpu.fp_status);
T
ths 已提交
2598
    update_fcr31();
2599
    return fdt2;
T
ths 已提交
2600
}
2601

2602
uint32_t helper_float_rsqrt1_s(uint32_t fst0)
T
ths 已提交
2603
{
2604 2605
    uint32_t fst2;

2606 2607 2608
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    fst2 = float32_sqrt(fst0, &env->active_fpu.fp_status);
    fst2 = float32_div(FLOAT_ONE32, fst2, &env->active_fpu.fp_status);
T
ths 已提交
2609
    update_fcr31();
2610
    return fst2;
T
ths 已提交
2611
}
2612

2613
uint64_t helper_float_rsqrt1_ps(uint64_t fdt0)
T
ths 已提交
2614
{
2615 2616 2617
    uint32_t fst2;
    uint32_t fsth2;

2618 2619 2620 2621 2622
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    fst2 = float32_sqrt(fdt0 & 0XFFFFFFFF, &env->active_fpu.fp_status);
    fsth2 = float32_sqrt(fdt0 >> 32, &env->active_fpu.fp_status);
    fst2 = float32_div(FLOAT_ONE32, fst2, &env->active_fpu.fp_status);
    fsth2 = float32_div(FLOAT_ONE32, fsth2, &env->active_fpu.fp_status);
T
ths 已提交
2623
    update_fcr31();
2624
    return ((uint64_t)fsth2 << 32) | fst2;
T
ths 已提交
2625 2626
}

2627
#define FLOAT_OP(name, p) void helper_float_##name##_##p(void)
2628

2629
/* binary operations */
2630
#define FLOAT_BINOP(name)                                          \
2631
uint64_t helper_float_ ## name ## _d(uint64_t fdt0, uint64_t fdt1)     \
2632 2633 2634
{                                                                  \
    uint64_t dt2;                                                  \
                                                                   \
2635 2636
    set_float_exception_flags(0, &env->active_fpu.fp_status);            \
    dt2 = float64_ ## name (fdt0, fdt1, &env->active_fpu.fp_status);     \
2637
    update_fcr31();                                                \
2638
    if (GET_FP_CAUSE(env->active_fpu.fcr31) & FP_INVALID)                \
2639 2640 2641 2642
        dt2 = FLOAT_QNAN64;                                        \
    return dt2;                                                    \
}                                                                  \
                                                                   \
2643
uint32_t helper_float_ ## name ## _s(uint32_t fst0, uint32_t fst1)     \
2644 2645 2646
{                                                                  \
    uint32_t wt2;                                                  \
                                                                   \
2647 2648
    set_float_exception_flags(0, &env->active_fpu.fp_status);            \
    wt2 = float32_ ## name (fst0, fst1, &env->active_fpu.fp_status);     \
2649
    update_fcr31();                                                \
2650
    if (GET_FP_CAUSE(env->active_fpu.fcr31) & FP_INVALID)                \
2651 2652 2653 2654
        wt2 = FLOAT_QNAN32;                                        \
    return wt2;                                                    \
}                                                                  \
                                                                   \
2655
uint64_t helper_float_ ## name ## _ps(uint64_t fdt0, uint64_t fdt1)    \
2656 2657 2658 2659 2660 2661 2662 2663
{                                                                  \
    uint32_t fst0 = fdt0 & 0XFFFFFFFF;                             \
    uint32_t fsth0 = fdt0 >> 32;                                   \
    uint32_t fst1 = fdt1 & 0XFFFFFFFF;                             \
    uint32_t fsth1 = fdt1 >> 32;                                   \
    uint32_t wt2;                                                  \
    uint32_t wth2;                                                 \
                                                                   \
2664 2665 2666
    set_float_exception_flags(0, &env->active_fpu.fp_status);            \
    wt2 = float32_ ## name (fst0, fst1, &env->active_fpu.fp_status);     \
    wth2 = float32_ ## name (fsth0, fsth1, &env->active_fpu.fp_status);  \
2667
    update_fcr31();                                                \
2668
    if (GET_FP_CAUSE(env->active_fpu.fcr31) & FP_INVALID) {              \
2669 2670 2671 2672
        wt2 = FLOAT_QNAN32;                                        \
        wth2 = FLOAT_QNAN32;                                       \
    }                                                              \
    return ((uint64_t)wth2 << 32) | wt2;                           \
2673
}
2674

2675 2676 2677 2678 2679 2680
FLOAT_BINOP(add)
FLOAT_BINOP(sub)
FLOAT_BINOP(mul)
FLOAT_BINOP(div)
#undef FLOAT_BINOP

2681
/* ternary operations */
2682
#define FLOAT_TERNOP(name1, name2)                                        \
2683
uint64_t helper_float_ ## name1 ## name2 ## _d(uint64_t fdt0, uint64_t fdt1,  \
2684 2685
                                           uint64_t fdt2)                 \
{                                                                         \
2686 2687
    fdt0 = float64_ ## name1 (fdt0, fdt1, &env->active_fpu.fp_status);          \
    return float64_ ## name2 (fdt0, fdt2, &env->active_fpu.fp_status);          \
2688 2689
}                                                                         \
                                                                          \
2690
uint32_t helper_float_ ## name1 ## name2 ## _s(uint32_t fst0, uint32_t fst1,  \
2691 2692
                                           uint32_t fst2)                 \
{                                                                         \
2693 2694
    fst0 = float32_ ## name1 (fst0, fst1, &env->active_fpu.fp_status);          \
    return float32_ ## name2 (fst0, fst2, &env->active_fpu.fp_status);          \
2695 2696
}                                                                         \
                                                                          \
2697
uint64_t helper_float_ ## name1 ## name2 ## _ps(uint64_t fdt0, uint64_t fdt1, \
2698 2699 2700 2701 2702 2703 2704 2705 2706
                                            uint64_t fdt2)                \
{                                                                         \
    uint32_t fst0 = fdt0 & 0XFFFFFFFF;                                    \
    uint32_t fsth0 = fdt0 >> 32;                                          \
    uint32_t fst1 = fdt1 & 0XFFFFFFFF;                                    \
    uint32_t fsth1 = fdt1 >> 32;                                          \
    uint32_t fst2 = fdt2 & 0XFFFFFFFF;                                    \
    uint32_t fsth2 = fdt2 >> 32;                                          \
                                                                          \
2707 2708 2709 2710
    fst0 = float32_ ## name1 (fst0, fst1, &env->active_fpu.fp_status);          \
    fsth0 = float32_ ## name1 (fsth0, fsth1, &env->active_fpu.fp_status);       \
    fst2 = float32_ ## name2 (fst0, fst2, &env->active_fpu.fp_status);          \
    fsth2 = float32_ ## name2 (fsth0, fsth2, &env->active_fpu.fp_status);       \
2711
    return ((uint64_t)fsth2 << 32) | fst2;                                \
2712
}
2713

2714 2715 2716 2717 2718
FLOAT_TERNOP(mul, add)
FLOAT_TERNOP(mul, sub)
#undef FLOAT_TERNOP

/* negated ternary operations */
2719
#define FLOAT_NTERNOP(name1, name2)                                       \
2720
uint64_t helper_float_n ## name1 ## name2 ## _d(uint64_t fdt0, uint64_t fdt1, \
2721 2722
                                           uint64_t fdt2)                 \
{                                                                         \
2723 2724
    fdt0 = float64_ ## name1 (fdt0, fdt1, &env->active_fpu.fp_status);          \
    fdt2 = float64_ ## name2 (fdt0, fdt2, &env->active_fpu.fp_status);          \
2725 2726 2727
    return float64_chs(fdt2);                                             \
}                                                                         \
                                                                          \
2728
uint32_t helper_float_n ## name1 ## name2 ## _s(uint32_t fst0, uint32_t fst1, \
2729 2730
                                           uint32_t fst2)                 \
{                                                                         \
2731 2732
    fst0 = float32_ ## name1 (fst0, fst1, &env->active_fpu.fp_status);          \
    fst2 = float32_ ## name2 (fst0, fst2, &env->active_fpu.fp_status);          \
2733 2734 2735
    return float32_chs(fst2);                                             \
}                                                                         \
                                                                          \
2736
uint64_t helper_float_n ## name1 ## name2 ## _ps(uint64_t fdt0, uint64_t fdt1,\
2737 2738 2739 2740 2741 2742 2743 2744 2745
                                           uint64_t fdt2)                 \
{                                                                         \
    uint32_t fst0 = fdt0 & 0XFFFFFFFF;                                    \
    uint32_t fsth0 = fdt0 >> 32;                                          \
    uint32_t fst1 = fdt1 & 0XFFFFFFFF;                                    \
    uint32_t fsth1 = fdt1 >> 32;                                          \
    uint32_t fst2 = fdt2 & 0XFFFFFFFF;                                    \
    uint32_t fsth2 = fdt2 >> 32;                                          \
                                                                          \
2746 2747 2748 2749
    fst0 = float32_ ## name1 (fst0, fst1, &env->active_fpu.fp_status);          \
    fsth0 = float32_ ## name1 (fsth0, fsth1, &env->active_fpu.fp_status);       \
    fst2 = float32_ ## name2 (fst0, fst2, &env->active_fpu.fp_status);          \
    fsth2 = float32_ ## name2 (fsth0, fsth2, &env->active_fpu.fp_status);       \
2750 2751 2752
    fst2 = float32_chs(fst2);                                             \
    fsth2 = float32_chs(fsth2);                                           \
    return ((uint64_t)fsth2 << 32) | fst2;                                \
2753
}
2754

2755 2756 2757 2758
FLOAT_NTERNOP(mul, add)
FLOAT_NTERNOP(mul, sub)
#undef FLOAT_NTERNOP

T
ths 已提交
2759
/* MIPS specific binary operations */
2760
uint64_t helper_float_recip2_d(uint64_t fdt0, uint64_t fdt2)
T
ths 已提交
2761
{
2762 2763 2764
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    fdt2 = float64_mul(fdt0, fdt2, &env->active_fpu.fp_status);
    fdt2 = float64_chs(float64_sub(fdt2, FLOAT_ONE64, &env->active_fpu.fp_status));
T
ths 已提交
2765
    update_fcr31();
2766
    return fdt2;
T
ths 已提交
2767
}
2768

2769
uint32_t helper_float_recip2_s(uint32_t fst0, uint32_t fst2)
T
ths 已提交
2770
{
2771 2772 2773
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
    fst2 = float32_chs(float32_sub(fst2, FLOAT_ONE32, &env->active_fpu.fp_status));
T
ths 已提交
2774
    update_fcr31();
2775
    return fst2;
T
ths 已提交
2776
}
2777

2778
uint64_t helper_float_recip2_ps(uint64_t fdt0, uint64_t fdt2)
T
ths 已提交
2779
{
2780 2781 2782 2783 2784
    uint32_t fst0 = fdt0 & 0XFFFFFFFF;
    uint32_t fsth0 = fdt0 >> 32;
    uint32_t fst2 = fdt2 & 0XFFFFFFFF;
    uint32_t fsth2 = fdt2 >> 32;

2785 2786 2787 2788 2789
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
    fsth2 = float32_mul(fsth0, fsth2, &env->active_fpu.fp_status);
    fst2 = float32_chs(float32_sub(fst2, FLOAT_ONE32, &env->active_fpu.fp_status));
    fsth2 = float32_chs(float32_sub(fsth2, FLOAT_ONE32, &env->active_fpu.fp_status));
T
ths 已提交
2790
    update_fcr31();
2791
    return ((uint64_t)fsth2 << 32) | fst2;
T
ths 已提交
2792 2793
}

2794
uint64_t helper_float_rsqrt2_d(uint64_t fdt0, uint64_t fdt2)
T
ths 已提交
2795
{
2796 2797 2798 2799
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    fdt2 = float64_mul(fdt0, fdt2, &env->active_fpu.fp_status);
    fdt2 = float64_sub(fdt2, FLOAT_ONE64, &env->active_fpu.fp_status);
    fdt2 = float64_chs(float64_div(fdt2, FLOAT_TWO64, &env->active_fpu.fp_status));
T
ths 已提交
2800
    update_fcr31();
2801
    return fdt2;
T
ths 已提交
2802
}
2803

2804
uint32_t helper_float_rsqrt2_s(uint32_t fst0, uint32_t fst2)
T
ths 已提交
2805
{
2806 2807 2808 2809
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
    fst2 = float32_sub(fst2, FLOAT_ONE32, &env->active_fpu.fp_status);
    fst2 = float32_chs(float32_div(fst2, FLOAT_TWO32, &env->active_fpu.fp_status));
T
ths 已提交
2810
    update_fcr31();
2811
    return fst2;
T
ths 已提交
2812
}
2813

2814
uint64_t helper_float_rsqrt2_ps(uint64_t fdt0, uint64_t fdt2)
T
ths 已提交
2815
{
2816 2817 2818 2819 2820
    uint32_t fst0 = fdt0 & 0XFFFFFFFF;
    uint32_t fsth0 = fdt0 >> 32;
    uint32_t fst2 = fdt2 & 0XFFFFFFFF;
    uint32_t fsth2 = fdt2 >> 32;

2821 2822 2823 2824 2825 2826 2827
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    fst2 = float32_mul(fst0, fst2, &env->active_fpu.fp_status);
    fsth2 = float32_mul(fsth0, fsth2, &env->active_fpu.fp_status);
    fst2 = float32_sub(fst2, FLOAT_ONE32, &env->active_fpu.fp_status);
    fsth2 = float32_sub(fsth2, FLOAT_ONE32, &env->active_fpu.fp_status);
    fst2 = float32_chs(float32_div(fst2, FLOAT_TWO32, &env->active_fpu.fp_status));
    fsth2 = float32_chs(float32_div(fsth2, FLOAT_TWO32, &env->active_fpu.fp_status));
T
ths 已提交
2828
    update_fcr31();
2829
    return ((uint64_t)fsth2 << 32) | fst2;
T
ths 已提交
2830 2831
}

2832
uint64_t helper_float_addr_ps(uint64_t fdt0, uint64_t fdt1)
2833
{
2834 2835 2836 2837 2838 2839 2840
    uint32_t fst0 = fdt0 & 0XFFFFFFFF;
    uint32_t fsth0 = fdt0 >> 32;
    uint32_t fst1 = fdt1 & 0XFFFFFFFF;
    uint32_t fsth1 = fdt1 >> 32;
    uint32_t fst2;
    uint32_t fsth2;

2841 2842 2843
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    fst2 = float32_add (fst0, fsth0, &env->active_fpu.fp_status);
    fsth2 = float32_add (fst1, fsth1, &env->active_fpu.fp_status);
2844
    update_fcr31();
2845
    return ((uint64_t)fsth2 << 32) | fst2;
2846 2847
}

2848
uint64_t helper_float_mulr_ps(uint64_t fdt0, uint64_t fdt1)
T
ths 已提交
2849
{
2850 2851 2852 2853 2854 2855 2856
    uint32_t fst0 = fdt0 & 0XFFFFFFFF;
    uint32_t fsth0 = fdt0 >> 32;
    uint32_t fst1 = fdt1 & 0XFFFFFFFF;
    uint32_t fsth1 = fdt1 >> 32;
    uint32_t fst2;
    uint32_t fsth2;

2857 2858 2859
    set_float_exception_flags(0, &env->active_fpu.fp_status);
    fst2 = float32_mul (fst0, fsth0, &env->active_fpu.fp_status);
    fsth2 = float32_mul (fst1, fsth1, &env->active_fpu.fp_status);
T
ths 已提交
2860
    update_fcr31();
2861
    return ((uint64_t)fsth2 << 32) | fst2;
T
ths 已提交
2862 2863
}

T
ths 已提交
2864
/* compare operations */
2865
#define FOP_COND_D(op, cond)                                   \
2866
void helper_cmp_d_ ## op (uint64_t fdt0, uint64_t fdt1, int cc)    \
2867 2868 2869 2870
{                                                              \
    int c = cond;                                              \
    update_fcr31();                                            \
    if (c)                                                     \
2871
        SET_FP_COND(cc, env->active_fpu);                      \
2872
    else                                                       \
2873
        CLEAR_FP_COND(cc, env->active_fpu);                    \
2874
}                                                              \
2875
void helper_cmpabs_d_ ## op (uint64_t fdt0, uint64_t fdt1, int cc) \
2876 2877 2878 2879 2880 2881 2882
{                                                              \
    int c;                                                     \
    fdt0 = float64_abs(fdt0);                                  \
    fdt1 = float64_abs(fdt1);                                  \
    c = cond;                                                  \
    update_fcr31();                                            \
    if (c)                                                     \
2883
        SET_FP_COND(cc, env->active_fpu);                      \
2884
    else                                                       \
2885
        CLEAR_FP_COND(cc, env->active_fpu);                    \
2886 2887
}

A
aurel32 已提交
2888
static int float64_is_unordered(int sig, float64 a, float64 b STATUS_PARAM)
2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903
{
    if (float64_is_signaling_nan(a) ||
        float64_is_signaling_nan(b) ||
        (sig && (float64_is_nan(a) || float64_is_nan(b)))) {
        float_raise(float_flag_invalid, status);
        return 1;
    } else if (float64_is_nan(a) || float64_is_nan(b)) {
        return 1;
    } else {
        return 0;
    }
}

/* NOTE: the comma operator will make "cond" to eval to false,
 * but float*_is_unordered() is still called. */
2904 2905 2906 2907 2908 2909 2910 2911
FOP_COND_D(f,   (float64_is_unordered(0, fdt1, fdt0, &env->active_fpu.fp_status), 0))
FOP_COND_D(un,  float64_is_unordered(0, fdt1, fdt0, &env->active_fpu.fp_status))
FOP_COND_D(eq,  !float64_is_unordered(0, fdt1, fdt0, &env->active_fpu.fp_status) && float64_eq(fdt0, fdt1, &env->active_fpu.fp_status))
FOP_COND_D(ueq, float64_is_unordered(0, fdt1, fdt0, &env->active_fpu.fp_status)  || float64_eq(fdt0, fdt1, &env->active_fpu.fp_status))
FOP_COND_D(olt, !float64_is_unordered(0, fdt1, fdt0, &env->active_fpu.fp_status) && float64_lt(fdt0, fdt1, &env->active_fpu.fp_status))
FOP_COND_D(ult, float64_is_unordered(0, fdt1, fdt0, &env->active_fpu.fp_status)  || float64_lt(fdt0, fdt1, &env->active_fpu.fp_status))
FOP_COND_D(ole, !float64_is_unordered(0, fdt1, fdt0, &env->active_fpu.fp_status) && float64_le(fdt0, fdt1, &env->active_fpu.fp_status))
FOP_COND_D(ule, float64_is_unordered(0, fdt1, fdt0, &env->active_fpu.fp_status)  || float64_le(fdt0, fdt1, &env->active_fpu.fp_status))
2912 2913
/* NOTE: the comma operator will make "cond" to eval to false,
 * but float*_is_unordered() is still called. */
2914 2915 2916 2917 2918 2919 2920 2921
FOP_COND_D(sf,  (float64_is_unordered(1, fdt1, fdt0, &env->active_fpu.fp_status), 0))
FOP_COND_D(ngle,float64_is_unordered(1, fdt1, fdt0, &env->active_fpu.fp_status))
FOP_COND_D(seq, !float64_is_unordered(1, fdt1, fdt0, &env->active_fpu.fp_status) && float64_eq(fdt0, fdt1, &env->active_fpu.fp_status))
FOP_COND_D(ngl, float64_is_unordered(1, fdt1, fdt0, &env->active_fpu.fp_status)  || float64_eq(fdt0, fdt1, &env->active_fpu.fp_status))
FOP_COND_D(lt,  !float64_is_unordered(1, fdt1, fdt0, &env->active_fpu.fp_status) && float64_lt(fdt0, fdt1, &env->active_fpu.fp_status))
FOP_COND_D(nge, float64_is_unordered(1, fdt1, fdt0, &env->active_fpu.fp_status)  || float64_lt(fdt0, fdt1, &env->active_fpu.fp_status))
FOP_COND_D(le,  !float64_is_unordered(1, fdt1, fdt0, &env->active_fpu.fp_status) && float64_le(fdt0, fdt1, &env->active_fpu.fp_status))
FOP_COND_D(ngt, float64_is_unordered(1, fdt1, fdt0, &env->active_fpu.fp_status)  || float64_le(fdt0, fdt1, &env->active_fpu.fp_status))
2922 2923

#define FOP_COND_S(op, cond)                                   \
2924
void helper_cmp_s_ ## op (uint32_t fst0, uint32_t fst1, int cc)    \
2925 2926 2927 2928
{                                                              \
    int c = cond;                                              \
    update_fcr31();                                            \
    if (c)                                                     \
2929
        SET_FP_COND(cc, env->active_fpu);                      \
2930
    else                                                       \
2931
        CLEAR_FP_COND(cc, env->active_fpu);                    \
2932
}                                                              \
2933
void helper_cmpabs_s_ ## op (uint32_t fst0, uint32_t fst1, int cc) \
2934 2935 2936 2937 2938 2939 2940
{                                                              \
    int c;                                                     \
    fst0 = float32_abs(fst0);                                  \
    fst1 = float32_abs(fst1);                                  \
    c = cond;                                                  \
    update_fcr31();                                            \
    if (c)                                                     \
2941
        SET_FP_COND(cc, env->active_fpu);                      \
2942
    else                                                       \
2943
        CLEAR_FP_COND(cc, env->active_fpu);                    \
2944 2945
}

A
aurel32 已提交
2946
static flag float32_is_unordered(int sig, float32 a, float32 b STATUS_PARAM)
2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961
{
    if (float32_is_signaling_nan(a) ||
        float32_is_signaling_nan(b) ||
        (sig && (float32_is_nan(a) || float32_is_nan(b)))) {
        float_raise(float_flag_invalid, status);
        return 1;
    } else if (float32_is_nan(a) || float32_is_nan(b)) {
        return 1;
    } else {
        return 0;
    }
}

/* NOTE: the comma operator will make "cond" to eval to false,
 * but float*_is_unordered() is still called. */
2962 2963 2964 2965 2966 2967 2968 2969
FOP_COND_S(f,   (float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status), 0))
FOP_COND_S(un,  float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status))
FOP_COND_S(eq,  !float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status) && float32_eq(fst0, fst1, &env->active_fpu.fp_status))
FOP_COND_S(ueq, float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status)  || float32_eq(fst0, fst1, &env->active_fpu.fp_status))
FOP_COND_S(olt, !float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status) && float32_lt(fst0, fst1, &env->active_fpu.fp_status))
FOP_COND_S(ult, float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status)  || float32_lt(fst0, fst1, &env->active_fpu.fp_status))
FOP_COND_S(ole, !float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status) && float32_le(fst0, fst1, &env->active_fpu.fp_status))
FOP_COND_S(ule, float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status)  || float32_le(fst0, fst1, &env->active_fpu.fp_status))
2970 2971
/* NOTE: the comma operator will make "cond" to eval to false,
 * but float*_is_unordered() is still called. */
2972 2973 2974 2975 2976 2977 2978 2979
FOP_COND_S(sf,  (float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status), 0))
FOP_COND_S(ngle,float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status))
FOP_COND_S(seq, !float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status) && float32_eq(fst0, fst1, &env->active_fpu.fp_status))
FOP_COND_S(ngl, float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status)  || float32_eq(fst0, fst1, &env->active_fpu.fp_status))
FOP_COND_S(lt,  !float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status) && float32_lt(fst0, fst1, &env->active_fpu.fp_status))
FOP_COND_S(nge, float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status)  || float32_lt(fst0, fst1, &env->active_fpu.fp_status))
FOP_COND_S(le,  !float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status) && float32_le(fst0, fst1, &env->active_fpu.fp_status))
FOP_COND_S(ngt, float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status)  || float32_le(fst0, fst1, &env->active_fpu.fp_status))
2980 2981

#define FOP_COND_PS(op, condl, condh)                           \
2982
void helper_cmp_ps_ ## op (uint64_t fdt0, uint64_t fdt1, int cc)    \
2983 2984 2985 2986 2987 2988 2989 2990 2991 2992
{                                                               \
    uint32_t fst0 = float32_abs(fdt0 & 0XFFFFFFFF);             \
    uint32_t fsth0 = float32_abs(fdt0 >> 32);                   \
    uint32_t fst1 = float32_abs(fdt1 & 0XFFFFFFFF);             \
    uint32_t fsth1 = float32_abs(fdt1 >> 32);                   \
    int cl = condl;                                             \
    int ch = condh;                                             \
                                                                \
    update_fcr31();                                             \
    if (cl)                                                     \
2993
        SET_FP_COND(cc, env->active_fpu);                       \
2994
    else                                                        \
2995
        CLEAR_FP_COND(cc, env->active_fpu);                     \
2996
    if (ch)                                                     \
2997
        SET_FP_COND(cc + 1, env->active_fpu);                   \
2998
    else                                                        \
2999
        CLEAR_FP_COND(cc + 1, env->active_fpu);                 \
3000
}                                                               \
3001
void helper_cmpabs_ps_ ## op (uint64_t fdt0, uint64_t fdt1, int cc) \
3002 3003 3004 3005 3006 3007 3008 3009 3010 3011
{                                                               \
    uint32_t fst0 = float32_abs(fdt0 & 0XFFFFFFFF);             \
    uint32_t fsth0 = float32_abs(fdt0 >> 32);                   \
    uint32_t fst1 = float32_abs(fdt1 & 0XFFFFFFFF);             \
    uint32_t fsth1 = float32_abs(fdt1 >> 32);                   \
    int cl = condl;                                             \
    int ch = condh;                                             \
                                                                \
    update_fcr31();                                             \
    if (cl)                                                     \
3012
        SET_FP_COND(cc, env->active_fpu);                       \
3013
    else                                                        \
3014
        CLEAR_FP_COND(cc, env->active_fpu);                     \
3015
    if (ch)                                                     \
3016
        SET_FP_COND(cc + 1, env->active_fpu);                   \
3017
    else                                                        \
3018
        CLEAR_FP_COND(cc + 1, env->active_fpu);                 \
3019 3020 3021 3022
}

/* NOTE: the comma operator will make "cond" to eval to false,
 * but float*_is_unordered() is still called. */
3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038
FOP_COND_PS(f,   (float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status), 0),
                 (float32_is_unordered(0, fsth1, fsth0, &env->active_fpu.fp_status), 0))
FOP_COND_PS(un,  float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status),
                 float32_is_unordered(0, fsth1, fsth0, &env->active_fpu.fp_status))
FOP_COND_PS(eq,  !float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status)   && float32_eq(fst0, fst1, &env->active_fpu.fp_status),
                 !float32_is_unordered(0, fsth1, fsth0, &env->active_fpu.fp_status) && float32_eq(fsth0, fsth1, &env->active_fpu.fp_status))
FOP_COND_PS(ueq, float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status)    || float32_eq(fst0, fst1, &env->active_fpu.fp_status),
                 float32_is_unordered(0, fsth1, fsth0, &env->active_fpu.fp_status)  || float32_eq(fsth0, fsth1, &env->active_fpu.fp_status))
FOP_COND_PS(olt, !float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status)   && float32_lt(fst0, fst1, &env->active_fpu.fp_status),
                 !float32_is_unordered(0, fsth1, fsth0, &env->active_fpu.fp_status) && float32_lt(fsth0, fsth1, &env->active_fpu.fp_status))
FOP_COND_PS(ult, float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status)    || float32_lt(fst0, fst1, &env->active_fpu.fp_status),
                 float32_is_unordered(0, fsth1, fsth0, &env->active_fpu.fp_status)  || float32_lt(fsth0, fsth1, &env->active_fpu.fp_status))
FOP_COND_PS(ole, !float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status)   && float32_le(fst0, fst1, &env->active_fpu.fp_status),
                 !float32_is_unordered(0, fsth1, fsth0, &env->active_fpu.fp_status) && float32_le(fsth0, fsth1, &env->active_fpu.fp_status))
FOP_COND_PS(ule, float32_is_unordered(0, fst1, fst0, &env->active_fpu.fp_status)    || float32_le(fst0, fst1, &env->active_fpu.fp_status),
                 float32_is_unordered(0, fsth1, fsth0, &env->active_fpu.fp_status)  || float32_le(fsth0, fsth1, &env->active_fpu.fp_status))
3039 3040
/* NOTE: the comma operator will make "cond" to eval to false,
 * but float*_is_unordered() is still called. */
3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056
FOP_COND_PS(sf,  (float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status), 0),
                 (float32_is_unordered(1, fsth1, fsth0, &env->active_fpu.fp_status), 0))
FOP_COND_PS(ngle,float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status),
                 float32_is_unordered(1, fsth1, fsth0, &env->active_fpu.fp_status))
FOP_COND_PS(seq, !float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status)   && float32_eq(fst0, fst1, &env->active_fpu.fp_status),
                 !float32_is_unordered(1, fsth1, fsth0, &env->active_fpu.fp_status) && float32_eq(fsth0, fsth1, &env->active_fpu.fp_status))
FOP_COND_PS(ngl, float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status)    || float32_eq(fst0, fst1, &env->active_fpu.fp_status),
                 float32_is_unordered(1, fsth1, fsth0, &env->active_fpu.fp_status)  || float32_eq(fsth0, fsth1, &env->active_fpu.fp_status))
FOP_COND_PS(lt,  !float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status)   && float32_lt(fst0, fst1, &env->active_fpu.fp_status),
                 !float32_is_unordered(1, fsth1, fsth0, &env->active_fpu.fp_status) && float32_lt(fsth0, fsth1, &env->active_fpu.fp_status))
FOP_COND_PS(nge, float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status)    || float32_lt(fst0, fst1, &env->active_fpu.fp_status),
                 float32_is_unordered(1, fsth1, fsth0, &env->active_fpu.fp_status)  || float32_lt(fsth0, fsth1, &env->active_fpu.fp_status))
FOP_COND_PS(le,  !float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status)   && float32_le(fst0, fst1, &env->active_fpu.fp_status),
                 !float32_is_unordered(1, fsth1, fsth0, &env->active_fpu.fp_status) && float32_le(fsth0, fsth1, &env->active_fpu.fp_status))
FOP_COND_PS(ngt, float32_is_unordered(1, fst1, fst0, &env->active_fpu.fp_status)    || float32_le(fst0, fst1, &env->active_fpu.fp_status),
                 float32_is_unordered(1, fsth1, fsth0, &env->active_fpu.fp_status)  || float32_le(fsth0, fsth1, &env->active_fpu.fp_status))