]> Git Repo - qemu.git/blame - target-ppc/op_helper.c
target-ppc: convert dcbz instruction to TCG
[qemu.git] / target-ppc / op_helper.c
CommitLineData
9a64fbe4 1/*
3fc6c082 2 * PowerPC emulation helpers for qemu.
5fafdf24 3 *
76a66253 4 * Copyright (c) 2003-2007 Jocelyn Mayer
9a64fbe4
FB
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
9a64fbe4 20#include "exec.h"
603fccce 21#include "host-utils.h"
a7812ae4 22#include "helper.h"
9a64fbe4 23
0411a972 24#include "helper_regs.h"
0487d6a8
JM
25#include "op_helper.h"
26
9a64fbe4 27#define MEMSUFFIX _raw
0487d6a8 28#include "op_helper.h"
9a64fbe4 29#include "op_helper_mem.h"
a541f297 30#if !defined(CONFIG_USER_ONLY)
9a64fbe4 31#define MEMSUFFIX _user
0487d6a8 32#include "op_helper.h"
9a64fbe4
FB
33#include "op_helper_mem.h"
34#define MEMSUFFIX _kernel
0487d6a8 35#include "op_helper.h"
9a64fbe4 36#include "op_helper_mem.h"
1e42b8f0
JM
37#define MEMSUFFIX _hypv
38#include "op_helper.h"
39#include "op_helper_mem.h"
40#endif
9a64fbe4 41
fdabc366
FB
42//#define DEBUG_OP
43//#define DEBUG_EXCEPTIONS
76a66253 44//#define DEBUG_SOFTWARE_TLB
fdabc366 45
9a64fbe4
FB
46/*****************************************************************************/
47/* Exceptions processing helpers */
9a64fbe4 48
64adab3f 49void helper_raise_exception_err (uint32_t exception, uint32_t error_code)
9a64fbe4 50{
64adab3f 51 raise_exception_err(env, exception, error_code);
76a66253 52}
9fddaa0c 53
64adab3f 54void helper_raise_debug (void)
9fddaa0c 55{
64adab3f 56 raise_exception(env, EXCP_DEBUG);
9a64fbe4
FB
57}
58
76a66253
JM
59/*****************************************************************************/
60/* Registers load and stores */
a7812ae4 61target_ulong helper_load_cr (void)
76a66253 62{
e1571908
AJ
63 return (env->crf[0] << 28) |
64 (env->crf[1] << 24) |
65 (env->crf[2] << 20) |
66 (env->crf[3] << 16) |
67 (env->crf[4] << 12) |
68 (env->crf[5] << 8) |
69 (env->crf[6] << 4) |
70 (env->crf[7] << 0);
76a66253
JM
71}
72
e1571908 73void helper_store_cr (target_ulong val, uint32_t mask)
76a66253
JM
74{
75 int i, sh;
76
36081602 77 for (i = 0, sh = 7; i < 8; i++, sh--) {
76a66253 78 if (mask & (1 << sh))
e1571908 79 env->crf[i] = (val >> (sh * 4)) & 0xFUL;
76a66253
JM
80 }
81}
82
c80f84e3
JM
83#if defined(TARGET_PPC64)
84void do_store_pri (int prio)
85{
86 env->spr[SPR_PPR] &= ~0x001C000000000000ULL;
87 env->spr[SPR_PPR] |= ((uint64_t)prio & 0x7) << 50;
88}
89#endif
90
a496775f
JM
91target_ulong ppc_load_dump_spr (int sprn)
92{
6b80055d 93 if (loglevel != 0) {
a496775f
JM
94 fprintf(logfile, "Read SPR %d %03x => " ADDRX "\n",
95 sprn, sprn, env->spr[sprn]);
96 }
97
98 return env->spr[sprn];
99}
100
101void ppc_store_dump_spr (int sprn, target_ulong val)
102{
6b80055d 103 if (loglevel != 0) {
a496775f
JM
104 fprintf(logfile, "Write SPR %d %03x => " ADDRX " <= " ADDRX "\n",
105 sprn, sprn, env->spr[sprn], val);
106 }
107 env->spr[sprn] = val;
108}
109
ff4a62cd
AJ
110/*****************************************************************************/
111/* Memory load and stores */
112
113static always_inline target_ulong get_addr(target_ulong addr)
114{
115#if defined(TARGET_PPC64)
116 if (msr_sf)
117 return addr;
118 else
119#endif
120 return (uint32_t)addr;
121}
122
123void helper_lmw (target_ulong addr, uint32_t reg)
124{
125#ifdef CONFIG_USER_ONLY
126#define ldfun ldl_raw
127#else
128 int (*ldfun)(target_ulong);
129
130 switch (env->mmu_idx) {
131 default:
132 case 0: ldfun = ldl_user;
133 break;
134 case 1: ldfun = ldl_kernel;
135 break;
136 case 2: ldfun = ldl_hypv;
137 break;
138 }
139#endif
140 for (; reg < 32; reg++, addr += 4) {
141 if (msr_le)
142 env->gpr[reg] = bswap32(ldfun(get_addr(addr)));
143 else
144 env->gpr[reg] = ldfun(get_addr(addr));
145 }
146}
147
148void helper_stmw (target_ulong addr, uint32_t reg)
149{
150#ifdef CONFIG_USER_ONLY
151#define stfun stl_raw
152#else
153 void (*stfun)(target_ulong, int);
154
155 switch (env->mmu_idx) {
156 default:
157 case 0: stfun = stl_user;
158 break;
159 case 1: stfun = stl_kernel;
160 break;
161 case 2: stfun = stl_hypv;
162 break;
163 }
164#endif
165 for (; reg < 32; reg++, addr += 4) {
166 if (msr_le)
167 stfun(get_addr(addr), bswap32((uint32_t)env->gpr[reg]));
168 else
169 stfun(get_addr(addr), (uint32_t)env->gpr[reg]);
170 }
171}
172
799a8c8d
AJ
173static void do_dcbz(target_ulong addr, int dcache_line_size)
174{
175 target_long mask = get_addr(~(dcache_line_size - 1));
176 int i;
177#ifdef CONFIG_USER_ONLY
178#define stfun stl_raw
179#else
180 void (*stfun)(target_ulong, int);
181
182 switch (env->mmu_idx) {
183 default:
184 case 0: stfun = stl_user;
185 break;
186 case 1: stfun = stl_kernel;
187 break;
188 case 2: stfun = stl_hypv;
189 break;
190 }
191#endif
192 addr &= mask;
193 for (i = 0 ; i < dcache_line_size ; i += 4) {
194 stfun(addr + i , 0);
195 }
196 if ((env->reserve & mask) == addr)
197 env->reserve = (target_ulong)-1ULL;
198}
199
200void helper_dcbz(target_ulong addr)
201{
202 do_dcbz(addr, env->dcache_line_size);
203}
204
205void helper_dcbz_970(target_ulong addr)
206{
207 if (((env->spr[SPR_970_HID5] >> 7) & 0x3) == 1)
208 do_dcbz(addr, 32);
209 else
210 do_dcbz(addr, env->dcache_line_size);
211}
212
9a64fbe4 213/*****************************************************************************/
fdabc366 214/* Fixed point operations helpers */
d9bce9d9 215#if defined(TARGET_PPC64)
d9bce9d9 216
74637406
AJ
217/* multiply high word */
218uint64_t helper_mulhd (uint64_t arg1, uint64_t arg2)
fdabc366 219{
74637406 220 uint64_t tl, th;
fdabc366 221
74637406
AJ
222 muls64(&tl, &th, arg1, arg2);
223 return th;
d9bce9d9 224}
d9bce9d9 225
74637406
AJ
226/* multiply high word unsigned */
227uint64_t helper_mulhdu (uint64_t arg1, uint64_t arg2)
fdabc366 228{
74637406 229 uint64_t tl, th;
fdabc366 230
74637406
AJ
231 mulu64(&tl, &th, arg1, arg2);
232 return th;
fdabc366
FB
233}
234
74637406 235uint64_t helper_mulldo (uint64_t arg1, uint64_t arg2)
fdabc366 236{
d9bce9d9
JM
237 int64_t th;
238 uint64_t tl;
239
74637406 240 muls64(&tl, (uint64_t *)&th, arg1, arg2);
88ad920b 241 /* If th != 0 && th != -1, then we had an overflow */
6f2d8978 242 if (likely((uint64_t)(th + 1) <= 1)) {
3d7b417e 243 env->xer &= ~(1 << XER_OV);
fdabc366 244 } else {
3d7b417e 245 env->xer |= (1 << XER_OV) | (1 << XER_SO);
fdabc366 246 }
74637406 247 return (int64_t)tl;
d9bce9d9
JM
248}
249#endif
250
26d67362 251target_ulong helper_cntlzw (target_ulong t)
603fccce 252{
26d67362 253 return clz32(t);
603fccce
JM
254}
255
256#if defined(TARGET_PPC64)
26d67362 257target_ulong helper_cntlzd (target_ulong t)
603fccce 258{
26d67362 259 return clz64(t);
603fccce
JM
260}
261#endif
262
9a64fbe4 263/* shift right arithmetic helper */
26d67362 264target_ulong helper_sraw (target_ulong value, target_ulong shift)
9a64fbe4
FB
265{
266 int32_t ret;
267
26d67362
AJ
268 if (likely(!(shift & 0x20))) {
269 if (likely((uint32_t)shift != 0)) {
270 shift &= 0x1f;
271 ret = (int32_t)value >> shift;
272 if (likely(ret >= 0 || (value & ((1 << shift) - 1)) == 0)) {
3d7b417e 273 env->xer &= ~(1 << XER_CA);
fdabc366 274 } else {
3d7b417e 275 env->xer |= (1 << XER_CA);
fdabc366
FB
276 }
277 } else {
26d67362 278 ret = (int32_t)value;
3d7b417e 279 env->xer &= ~(1 << XER_CA);
fdabc366
FB
280 }
281 } else {
26d67362
AJ
282 ret = (int32_t)value >> 31;
283 if (ret) {
3d7b417e 284 env->xer |= (1 << XER_CA);
26d67362
AJ
285 } else {
286 env->xer &= ~(1 << XER_CA);
76a66253 287 }
fdabc366 288 }
26d67362 289 return (target_long)ret;
9a64fbe4
FB
290}
291
d9bce9d9 292#if defined(TARGET_PPC64)
26d67362 293target_ulong helper_srad (target_ulong value, target_ulong shift)
d9bce9d9
JM
294{
295 int64_t ret;
296
26d67362
AJ
297 if (likely(!(shift & 0x40))) {
298 if (likely((uint64_t)shift != 0)) {
299 shift &= 0x3f;
300 ret = (int64_t)value >> shift;
301 if (likely(ret >= 0 || (value & ((1 << shift) - 1)) == 0)) {
3d7b417e 302 env->xer &= ~(1 << XER_CA);
d9bce9d9 303 } else {
3d7b417e 304 env->xer |= (1 << XER_CA);
d9bce9d9
JM
305 }
306 } else {
26d67362 307 ret = (int64_t)value;
3d7b417e 308 env->xer &= ~(1 << XER_CA);
d9bce9d9
JM
309 }
310 } else {
26d67362
AJ
311 ret = (int64_t)value >> 63;
312 if (ret) {
3d7b417e 313 env->xer |= (1 << XER_CA);
26d67362
AJ
314 } else {
315 env->xer &= ~(1 << XER_CA);
d9bce9d9
JM
316 }
317 }
26d67362 318 return ret;
d9bce9d9
JM
319}
320#endif
321
26d67362 322target_ulong helper_popcntb (target_ulong val)
d9bce9d9 323{
6176a26d
AJ
324 val = (val & 0x55555555) + ((val >> 1) & 0x55555555);
325 val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
326 val = (val & 0x0f0f0f0f) + ((val >> 4) & 0x0f0f0f0f);
327 return val;
d9bce9d9
JM
328}
329
330#if defined(TARGET_PPC64)
26d67362 331target_ulong helper_popcntb_64 (target_ulong val)
d9bce9d9 332{
6176a26d
AJ
333 val = (val & 0x5555555555555555ULL) + ((val >> 1) & 0x5555555555555555ULL);
334 val = (val & 0x3333333333333333ULL) + ((val >> 2) & 0x3333333333333333ULL);
335 val = (val & 0x0f0f0f0f0f0f0f0fULL) + ((val >> 4) & 0x0f0f0f0f0f0f0f0fULL);
336 return val;
d9bce9d9
JM
337}
338#endif
339
fdabc366 340/*****************************************************************************/
9a64fbe4 341/* Floating point operations helpers */
a0d7d5a7
AJ
342uint64_t helper_float32_to_float64(uint32_t arg)
343{
344 CPU_FloatU f;
345 CPU_DoubleU d;
346 f.l = arg;
347 d.d = float32_to_float64(f.f, &env->fp_status);
348 return d.ll;
349}
350
351uint32_t helper_float64_to_float32(uint64_t arg)
352{
353 CPU_FloatU f;
354 CPU_DoubleU d;
355 d.ll = arg;
356 f.f = float64_to_float32(d.d, &env->fp_status);
357 return f.l;
358}
359
0ca9d380 360static always_inline int fpisneg (float64 d)
7c58044c 361{
0ca9d380 362 CPU_DoubleU u;
7c58044c 363
0ca9d380 364 u.d = d;
7c58044c 365
0ca9d380 366 return u.ll >> 63 != 0;
7c58044c
JM
367}
368
0ca9d380 369static always_inline int isden (float64 d)
7c58044c 370{
0ca9d380 371 CPU_DoubleU u;
7c58044c 372
0ca9d380 373 u.d = d;
7c58044c 374
0ca9d380 375 return ((u.ll >> 52) & 0x7FF) == 0;
7c58044c
JM
376}
377
0ca9d380 378static always_inline int iszero (float64 d)
7c58044c 379{
0ca9d380 380 CPU_DoubleU u;
7c58044c 381
0ca9d380 382 u.d = d;
7c58044c 383
0ca9d380 384 return (u.ll & ~0x8000000000000000ULL) == 0;
7c58044c
JM
385}
386
0ca9d380 387static always_inline int isinfinity (float64 d)
7c58044c 388{
0ca9d380 389 CPU_DoubleU u;
7c58044c 390
0ca9d380 391 u.d = d;
7c58044c 392
0ca9d380
AJ
393 return ((u.ll >> 52) & 0x7FF) == 0x7FF &&
394 (u.ll & 0x000FFFFFFFFFFFFFULL) == 0;
7c58044c
JM
395}
396
80621676
AJ
397#ifdef CONFIG_SOFTFLOAT
398static always_inline int isfinite (float64 d)
399{
400 CPU_DoubleU u;
401
402 u.d = d;
403
404 return (((u.ll >> 52) & 0x7FF) != 0x7FF);
405}
406
407static always_inline int isnormal (float64 d)
408{
409 CPU_DoubleU u;
410
411 u.d = d;
412
413 uint32_t exp = (u.ll >> 52) & 0x7FF;
414 return ((0 < exp) && (exp < 0x7FF));
415}
416#endif
417
af12906f 418uint32_t helper_compute_fprf (uint64_t arg, uint32_t set_fprf)
7c58044c 419{
af12906f 420 CPU_DoubleU farg;
7c58044c 421 int isneg;
af12906f
AJ
422 int ret;
423 farg.ll = arg;
424 isneg = fpisneg(farg.d);
425 if (unlikely(float64_is_nan(farg.d))) {
426 if (float64_is_signaling_nan(farg.d)) {
7c58044c 427 /* Signaling NaN: flags are undefined */
af12906f 428 ret = 0x00;
7c58044c
JM
429 } else {
430 /* Quiet NaN */
af12906f 431 ret = 0x11;
7c58044c 432 }
af12906f 433 } else if (unlikely(isinfinity(farg.d))) {
7c58044c
JM
434 /* +/- infinity */
435 if (isneg)
af12906f 436 ret = 0x09;
7c58044c 437 else
af12906f 438 ret = 0x05;
7c58044c 439 } else {
af12906f 440 if (iszero(farg.d)) {
7c58044c
JM
441 /* +/- zero */
442 if (isneg)
af12906f 443 ret = 0x12;
7c58044c 444 else
af12906f 445 ret = 0x02;
7c58044c 446 } else {
af12906f 447 if (isden(farg.d)) {
7c58044c 448 /* Denormalized numbers */
af12906f 449 ret = 0x10;
7c58044c
JM
450 } else {
451 /* Normalized numbers */
af12906f 452 ret = 0x00;
7c58044c
JM
453 }
454 if (isneg) {
af12906f 455 ret |= 0x08;
7c58044c 456 } else {
af12906f 457 ret |= 0x04;
7c58044c
JM
458 }
459 }
460 }
461 if (set_fprf) {
462 /* We update FPSCR_FPRF */
463 env->fpscr &= ~(0x1F << FPSCR_FPRF);
af12906f 464 env->fpscr |= ret << FPSCR_FPRF;
7c58044c
JM
465 }
466 /* We just need fpcc to update Rc1 */
af12906f 467 return ret & 0xF;
7c58044c
JM
468}
469
470/* Floating-point invalid operations exception */
af12906f 471static always_inline uint64_t fload_invalid_op_excp (int op)
7c58044c 472{
af12906f 473 uint64_t ret = 0;
7c58044c
JM
474 int ve;
475
476 ve = fpscr_ve;
477 if (op & POWERPC_EXCP_FP_VXSNAN) {
478 /* Operation on signaling NaN */
479 env->fpscr |= 1 << FPSCR_VXSNAN;
480 }
481 if (op & POWERPC_EXCP_FP_VXSOFT) {
482 /* Software-defined condition */
483 env->fpscr |= 1 << FPSCR_VXSOFT;
484 }
485 switch (op & ~(POWERPC_EXCP_FP_VXSOFT | POWERPC_EXCP_FP_VXSNAN)) {
486 case POWERPC_EXCP_FP_VXISI:
487 /* Magnitude subtraction of infinities */
488 env->fpscr |= 1 << FPSCR_VXISI;
489 goto update_arith;
490 case POWERPC_EXCP_FP_VXIDI:
491 /* Division of infinity by infinity */
492 env->fpscr |= 1 << FPSCR_VXIDI;
493 goto update_arith;
494 case POWERPC_EXCP_FP_VXZDZ:
495 /* Division of zero by zero */
496 env->fpscr |= 1 << FPSCR_VXZDZ;
497 goto update_arith;
498 case POWERPC_EXCP_FP_VXIMZ:
499 /* Multiplication of zero by infinity */
500 env->fpscr |= 1 << FPSCR_VXIMZ;
501 goto update_arith;
502 case POWERPC_EXCP_FP_VXVC:
503 /* Ordered comparison of NaN */
504 env->fpscr |= 1 << FPSCR_VXVC;
505 env->fpscr &= ~(0xF << FPSCR_FPCC);
506 env->fpscr |= 0x11 << FPSCR_FPCC;
507 /* We must update the target FPR before raising the exception */
508 if (ve != 0) {
509 env->exception_index = POWERPC_EXCP_PROGRAM;
510 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_VXVC;
511 /* Update the floating-point enabled exception summary */
512 env->fpscr |= 1 << FPSCR_FEX;
513 /* Exception is differed */
514 ve = 0;
515 }
516 break;
517 case POWERPC_EXCP_FP_VXSQRT:
518 /* Square root of a negative number */
519 env->fpscr |= 1 << FPSCR_VXSQRT;
520 update_arith:
521 env->fpscr &= ~((1 << FPSCR_FR) | (1 << FPSCR_FI));
522 if (ve == 0) {
523 /* Set the result to quiet NaN */
af12906f 524 ret = UINT64_MAX;
7c58044c
JM
525 env->fpscr &= ~(0xF << FPSCR_FPCC);
526 env->fpscr |= 0x11 << FPSCR_FPCC;
527 }
528 break;
529 case POWERPC_EXCP_FP_VXCVI:
530 /* Invalid conversion */
531 env->fpscr |= 1 << FPSCR_VXCVI;
532 env->fpscr &= ~((1 << FPSCR_FR) | (1 << FPSCR_FI));
533 if (ve == 0) {
534 /* Set the result to quiet NaN */
af12906f 535 ret = UINT64_MAX;
7c58044c
JM
536 env->fpscr &= ~(0xF << FPSCR_FPCC);
537 env->fpscr |= 0x11 << FPSCR_FPCC;
538 }
539 break;
540 }
541 /* Update the floating-point invalid operation summary */
542 env->fpscr |= 1 << FPSCR_VX;
543 /* Update the floating-point exception summary */
544 env->fpscr |= 1 << FPSCR_FX;
545 if (ve != 0) {
546 /* Update the floating-point enabled exception summary */
547 env->fpscr |= 1 << FPSCR_FEX;
548 if (msr_fe0 != 0 || msr_fe1 != 0)
64adab3f 549 raise_exception_err(env, POWERPC_EXCP_PROGRAM, POWERPC_EXCP_FP | op);
7c58044c 550 }
af12906f 551 return ret;
7c58044c
JM
552}
553
af12906f 554static always_inline uint64_t float_zero_divide_excp (uint64_t arg1, uint64_t arg2)
7c58044c 555{
7c58044c
JM
556 env->fpscr |= 1 << FPSCR_ZX;
557 env->fpscr &= ~((1 << FPSCR_FR) | (1 << FPSCR_FI));
558 /* Update the floating-point exception summary */
559 env->fpscr |= 1 << FPSCR_FX;
560 if (fpscr_ze != 0) {
561 /* Update the floating-point enabled exception summary */
562 env->fpscr |= 1 << FPSCR_FEX;
563 if (msr_fe0 != 0 || msr_fe1 != 0) {
64adab3f
AJ
564 raise_exception_err(env, POWERPC_EXCP_PROGRAM,
565 POWERPC_EXCP_FP | POWERPC_EXCP_FP_ZX);
7c58044c
JM
566 }
567 } else {
568 /* Set the result to infinity */
af12906f
AJ
569 arg1 = ((arg1 ^ arg2) & 0x8000000000000000ULL);
570 arg1 |= 0x7FFULL << 52;
7c58044c 571 }
af12906f 572 return arg1;
7c58044c
JM
573}
574
575static always_inline void float_overflow_excp (void)
576{
577 env->fpscr |= 1 << FPSCR_OX;
578 /* Update the floating-point exception summary */
579 env->fpscr |= 1 << FPSCR_FX;
580 if (fpscr_oe != 0) {
581 /* XXX: should adjust the result */
582 /* Update the floating-point enabled exception summary */
583 env->fpscr |= 1 << FPSCR_FEX;
584 /* We must update the target FPR before raising the exception */
585 env->exception_index = POWERPC_EXCP_PROGRAM;
586 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_OX;
587 } else {
588 env->fpscr |= 1 << FPSCR_XX;
589 env->fpscr |= 1 << FPSCR_FI;
590 }
591}
592
593static always_inline void float_underflow_excp (void)
594{
595 env->fpscr |= 1 << FPSCR_UX;
596 /* Update the floating-point exception summary */
597 env->fpscr |= 1 << FPSCR_FX;
598 if (fpscr_ue != 0) {
599 /* XXX: should adjust the result */
600 /* Update the floating-point enabled exception summary */
601 env->fpscr |= 1 << FPSCR_FEX;
602 /* We must update the target FPR before raising the exception */
603 env->exception_index = POWERPC_EXCP_PROGRAM;
604 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_UX;
605 }
606}
607
608static always_inline void float_inexact_excp (void)
609{
610 env->fpscr |= 1 << FPSCR_XX;
611 /* Update the floating-point exception summary */
612 env->fpscr |= 1 << FPSCR_FX;
613 if (fpscr_xe != 0) {
614 /* Update the floating-point enabled exception summary */
615 env->fpscr |= 1 << FPSCR_FEX;
616 /* We must update the target FPR before raising the exception */
617 env->exception_index = POWERPC_EXCP_PROGRAM;
618 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_XX;
619 }
620}
621
622static always_inline void fpscr_set_rounding_mode (void)
623{
624 int rnd_type;
625
626 /* Set rounding mode */
627 switch (fpscr_rn) {
628 case 0:
629 /* Best approximation (round to nearest) */
630 rnd_type = float_round_nearest_even;
631 break;
632 case 1:
633 /* Smaller magnitude (round toward zero) */
634 rnd_type = float_round_to_zero;
635 break;
636 case 2:
637 /* Round toward +infinite */
638 rnd_type = float_round_up;
639 break;
640 default:
641 case 3:
642 /* Round toward -infinite */
643 rnd_type = float_round_down;
644 break;
645 }
646 set_float_rounding_mode(rnd_type, &env->fp_status);
647}
648
af12906f 649void helper_fpscr_setbit (uint32_t bit)
7c58044c
JM
650{
651 int prev;
652
653 prev = (env->fpscr >> bit) & 1;
654 env->fpscr |= 1 << bit;
655 if (prev == 0) {
656 switch (bit) {
657 case FPSCR_VX:
658 env->fpscr |= 1 << FPSCR_FX;
659 if (fpscr_ve)
660 goto raise_ve;
661 case FPSCR_OX:
662 env->fpscr |= 1 << FPSCR_FX;
663 if (fpscr_oe)
664 goto raise_oe;
665 break;
666 case FPSCR_UX:
667 env->fpscr |= 1 << FPSCR_FX;
668 if (fpscr_ue)
669 goto raise_ue;
670 break;
671 case FPSCR_ZX:
672 env->fpscr |= 1 << FPSCR_FX;
673 if (fpscr_ze)
674 goto raise_ze;
675 break;
676 case FPSCR_XX:
677 env->fpscr |= 1 << FPSCR_FX;
678 if (fpscr_xe)
679 goto raise_xe;
680 break;
681 case FPSCR_VXSNAN:
682 case FPSCR_VXISI:
683 case FPSCR_VXIDI:
684 case FPSCR_VXZDZ:
685 case FPSCR_VXIMZ:
686 case FPSCR_VXVC:
687 case FPSCR_VXSOFT:
688 case FPSCR_VXSQRT:
689 case FPSCR_VXCVI:
690 env->fpscr |= 1 << FPSCR_VX;
691 env->fpscr |= 1 << FPSCR_FX;
692 if (fpscr_ve != 0)
693 goto raise_ve;
694 break;
695 case FPSCR_VE:
696 if (fpscr_vx != 0) {
697 raise_ve:
698 env->error_code = POWERPC_EXCP_FP;
699 if (fpscr_vxsnan)
700 env->error_code |= POWERPC_EXCP_FP_VXSNAN;
701 if (fpscr_vxisi)
702 env->error_code |= POWERPC_EXCP_FP_VXISI;
703 if (fpscr_vxidi)
704 env->error_code |= POWERPC_EXCP_FP_VXIDI;
705 if (fpscr_vxzdz)
706 env->error_code |= POWERPC_EXCP_FP_VXZDZ;
707 if (fpscr_vximz)
708 env->error_code |= POWERPC_EXCP_FP_VXIMZ;
709 if (fpscr_vxvc)
710 env->error_code |= POWERPC_EXCP_FP_VXVC;
711 if (fpscr_vxsoft)
712 env->error_code |= POWERPC_EXCP_FP_VXSOFT;
713 if (fpscr_vxsqrt)
714 env->error_code |= POWERPC_EXCP_FP_VXSQRT;
715 if (fpscr_vxcvi)
716 env->error_code |= POWERPC_EXCP_FP_VXCVI;
717 goto raise_excp;
718 }
719 break;
720 case FPSCR_OE:
721 if (fpscr_ox != 0) {
722 raise_oe:
723 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_OX;
724 goto raise_excp;
725 }
726 break;
727 case FPSCR_UE:
728 if (fpscr_ux != 0) {
729 raise_ue:
730 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_UX;
731 goto raise_excp;
732 }
733 break;
734 case FPSCR_ZE:
735 if (fpscr_zx != 0) {
736 raise_ze:
737 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_ZX;
738 goto raise_excp;
739 }
740 break;
741 case FPSCR_XE:
742 if (fpscr_xx != 0) {
743 raise_xe:
744 env->error_code = POWERPC_EXCP_FP | POWERPC_EXCP_FP_XX;
745 goto raise_excp;
746 }
747 break;
748 case FPSCR_RN1:
749 case FPSCR_RN:
750 fpscr_set_rounding_mode();
751 break;
752 default:
753 break;
754 raise_excp:
755 /* Update the floating-point enabled exception summary */
756 env->fpscr |= 1 << FPSCR_FEX;
757 /* We have to update Rc1 before raising the exception */
758 env->exception_index = POWERPC_EXCP_PROGRAM;
759 break;
760 }
761 }
762}
763
af12906f 764void helper_store_fpscr (uint64_t arg, uint32_t mask)
7c58044c
JM
765{
766 /*
767 * We use only the 32 LSB of the incoming fpr
768 */
7c58044c
JM
769 uint32_t prev, new;
770 int i;
771
7c58044c 772 prev = env->fpscr;
af12906f 773 new = (uint32_t)arg;
7c58044c
JM
774 new &= ~0x90000000;
775 new |= prev & 0x90000000;
776 for (i = 0; i < 7; i++) {
777 if (mask & (1 << i)) {
778 env->fpscr &= ~(0xF << (4 * i));
779 env->fpscr |= new & (0xF << (4 * i));
780 }
781 }
782 /* Update VX and FEX */
783 if (fpscr_ix != 0)
784 env->fpscr |= 1 << FPSCR_VX;
5567025f
AJ
785 else
786 env->fpscr &= ~(1 << FPSCR_VX);
7c58044c
JM
787 if ((fpscr_ex & fpscr_eex) != 0) {
788 env->fpscr |= 1 << FPSCR_FEX;
789 env->exception_index = POWERPC_EXCP_PROGRAM;
790 /* XXX: we should compute it properly */
791 env->error_code = POWERPC_EXCP_FP;
792 }
5567025f
AJ
793 else
794 env->fpscr &= ~(1 << FPSCR_FEX);
7c58044c
JM
795 fpscr_set_rounding_mode();
796}
7c58044c 797
af12906f 798void helper_float_check_status (void)
7c58044c 799{
af12906f 800#ifdef CONFIG_SOFTFLOAT
7c58044c
JM
801 if (env->exception_index == POWERPC_EXCP_PROGRAM &&
802 (env->error_code & POWERPC_EXCP_FP)) {
803 /* Differred floating-point exception after target FPR update */
804 if (msr_fe0 != 0 || msr_fe1 != 0)
64adab3f 805 raise_exception_err(env, env->exception_index, env->error_code);
7c58044c
JM
806 } else if (env->fp_status.float_exception_flags & float_flag_overflow) {
807 float_overflow_excp();
808 } else if (env->fp_status.float_exception_flags & float_flag_underflow) {
809 float_underflow_excp();
810 } else if (env->fp_status.float_exception_flags & float_flag_inexact) {
811 float_inexact_excp();
812 }
af12906f
AJ
813#else
814 if (env->exception_index == POWERPC_EXCP_PROGRAM &&
815 (env->error_code & POWERPC_EXCP_FP)) {
816 /* Differred floating-point exception after target FPR update */
817 if (msr_fe0 != 0 || msr_fe1 != 0)
64adab3f 818 raise_exception_err(env, env->exception_index, env->error_code);
af12906f
AJ
819 }
820 RETURN();
821#endif
822}
823
824#ifdef CONFIG_SOFTFLOAT
825void helper_reset_fpstatus (void)
826{
827 env->fp_status.float_exception_flags = 0;
7c58044c
JM
828}
829#endif
830
af12906f
AJ
831/* fadd - fadd. */
832uint64_t helper_fadd (uint64_t arg1, uint64_t arg2)
7c58044c 833{
af12906f
AJ
834 CPU_DoubleU farg1, farg2;
835
836 farg1.ll = arg1;
837 farg2.ll = arg2;
838#if USE_PRECISE_EMULATION
839 if (unlikely(float64_is_signaling_nan(farg1.d) ||
840 float64_is_signaling_nan(farg2.d))) {
7c58044c 841 /* sNaN addition */
af12906f
AJ
842 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
843 } else if (likely(isfinite(farg1.d) || isfinite(farg2.d) ||
844 fpisneg(farg1.d) == fpisneg(farg2.d))) {
845 farg1.d = float64_add(farg1.d, farg2.d, &env->fp_status);
7c58044c
JM
846 } else {
847 /* Magnitude subtraction of infinities */
af12906f 848 farg1.ll == fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI);
7c58044c 849 }
af12906f
AJ
850#else
851 farg1.d = float64_add(farg1.d, farg2.d, &env->fp_status);
852#endif
853 return farg1.ll;
7c58044c
JM
854}
855
af12906f
AJ
856/* fsub - fsub. */
857uint64_t helper_fsub (uint64_t arg1, uint64_t arg2)
858{
859 CPU_DoubleU farg1, farg2;
860
861 farg1.ll = arg1;
862 farg2.ll = arg2;
863#if USE_PRECISE_EMULATION
7c58044c 864{
af12906f
AJ
865 if (unlikely(float64_is_signaling_nan(farg1.d) ||
866 float64_is_signaling_nan(farg2.d))) {
7c58044c 867 /* sNaN subtraction */
af12906f
AJ
868 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
869 } else if (likely(isfinite(farg1.d) || isfinite(farg2.d) ||
870 fpisneg(farg1.d) != fpisneg(farg2.d))) {
871 farg1.d = float64_sub(farg1.d, farg2.d, &env->fp_status);
7c58044c
JM
872 } else {
873 /* Magnitude subtraction of infinities */
af12906f 874 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI);
7c58044c
JM
875 }
876}
af12906f
AJ
877#else
878 farg1.d = float64_sub(farg1.d, farg2.d, &env->fp_status);
879#endif
880 return farg1.ll;
881}
7c58044c 882
af12906f
AJ
883/* fmul - fmul. */
884uint64_t helper_fmul (uint64_t arg1, uint64_t arg2)
7c58044c 885{
af12906f
AJ
886 CPU_DoubleU farg1, farg2;
887
888 farg1.ll = arg1;
889 farg2.ll = arg2;
890#if USE_PRECISE_EMULATION
891 if (unlikely(float64_is_signaling_nan(farg1.d) ||
892 float64_is_signaling_nan(farg2.d))) {
7c58044c 893 /* sNaN multiplication */
af12906f
AJ
894 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
895 } else if (unlikely((isinfinity(farg1.d) && iszero(farg2.d)) ||
896 (iszero(farg1.d) && isinfinity(farg2.d)))) {
7c58044c 897 /* Multiplication of zero by infinity */
af12906f 898 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ);
7c58044c 899 } else {
af12906f 900 farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
7c58044c
JM
901 }
902}
af12906f
AJ
903#else
904 farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
905#endif
906 return farg1.ll;
907}
7c58044c 908
af12906f
AJ
909/* fdiv - fdiv. */
910uint64_t helper_fdiv (uint64_t arg1, uint64_t arg2)
7c58044c 911{
af12906f
AJ
912 CPU_DoubleU farg1, farg2;
913
914 farg1.ll = arg1;
915 farg2.ll = arg2;
916#if USE_PRECISE_EMULATION
917 if (unlikely(float64_is_signaling_nan(farg1.d) ||
918 float64_is_signaling_nan(farg2.d))) {
7c58044c 919 /* sNaN division */
af12906f
AJ
920 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
921 } else if (unlikely(isinfinity(farg1.d) && isinfinity(farg2.d))) {
7c58044c 922 /* Division of infinity by infinity */
af12906f
AJ
923 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXIDI);
924 } else if (unlikely(iszero(farg2.d))) {
925 if (iszero(farg1.d)) {
7c58044c 926 /* Division of zero by zero */
af12906f 927 farg1.ll fload_invalid_op_excp(POWERPC_EXCP_FP_VXZDZ);
7c58044c
JM
928 } else {
929 /* Division by zero */
af12906f 930 farg1.ll = float_zero_divide_excp(farg1.d, farg2.d);
7c58044c
JM
931 }
932 } else {
af12906f 933 farg1.d = float64_div(farg1.d, farg2.d, &env->fp_status);
7c58044c 934 }
af12906f
AJ
935#else
936 farg1.d = float64_div(farg1.d, farg2.d, &env->fp_status);
937#endif
938 return farg1.ll;
7c58044c 939}
7c58044c 940
af12906f
AJ
941/* fabs */
942uint64_t helper_fabs (uint64_t arg)
9a64fbe4 943{
af12906f 944 CPU_DoubleU farg;
9a64fbe4 945
af12906f
AJ
946 farg.ll = arg;
947 farg.d = float64_abs(farg.d);
948 return farg.ll;
949}
950
951/* fnabs */
952uint64_t helper_fnabs (uint64_t arg)
953{
954 CPU_DoubleU farg;
955
956 farg.ll = arg;
957 farg.d = float64_abs(farg.d);
958 farg.d = float64_chs(farg.d);
959 return farg.ll;
960}
961
962/* fneg */
963uint64_t helper_fneg (uint64_t arg)
964{
965 CPU_DoubleU farg;
966
967 farg.ll = arg;
968 farg.d = float64_chs(farg.d);
969 return farg.ll;
970}
971
972/* fctiw - fctiw. */
973uint64_t helper_fctiw (uint64_t arg)
974{
975 CPU_DoubleU farg;
976 farg.ll = arg;
977
978 if (unlikely(float64_is_signaling_nan(farg.d))) {
7c58044c 979 /* sNaN conversion */
af12906f
AJ
980 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
981 } else if (unlikely(float64_is_nan(farg.d) || isinfinity(farg.d))) {
7c58044c 982 /* qNan / infinity conversion */
af12906f 983 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
7c58044c 984 } else {
af12906f 985 farg.ll = float64_to_int32(farg.d, &env->fp_status);
1cdb9c3d 986#if USE_PRECISE_EMULATION
7c58044c
JM
987 /* XXX: higher bits are not supposed to be significant.
988 * to make tests easier, return the same as a real PowerPC 750
989 */
af12906f 990 farg.ll |= 0xFFF80000ULL << 32;
e864cabd 991#endif
7c58044c 992 }
af12906f 993 return farg.ll;
9a64fbe4
FB
994}
995
af12906f
AJ
996/* fctiwz - fctiwz. */
997uint64_t helper_fctiwz (uint64_t arg)
9a64fbe4 998{
af12906f
AJ
999 CPU_DoubleU farg;
1000 farg.ll = arg;
4ecc3190 1001
af12906f 1002 if (unlikely(float64_is_signaling_nan(farg.d))) {
7c58044c 1003 /* sNaN conversion */
af12906f
AJ
1004 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
1005 } else if (unlikely(float64_is_nan(farg.d) || isinfinity(farg.d))) {
7c58044c 1006 /* qNan / infinity conversion */
af12906f 1007 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
7c58044c 1008 } else {
af12906f 1009 farg.ll = float64_to_int32_round_to_zero(farg.d, &env->fp_status);
1cdb9c3d 1010#if USE_PRECISE_EMULATION
7c58044c
JM
1011 /* XXX: higher bits are not supposed to be significant.
1012 * to make tests easier, return the same as a real PowerPC 750
1013 */
af12906f 1014 farg.ll |= 0xFFF80000ULL << 32;
e864cabd 1015#endif
7c58044c 1016 }
af12906f 1017 return farg.ll;
9a64fbe4
FB
1018}
1019
426613db 1020#if defined(TARGET_PPC64)
af12906f
AJ
1021/* fcfid - fcfid. */
1022uint64_t helper_fcfid (uint64_t arg)
426613db 1023{
af12906f
AJ
1024 CPU_DoubleU farg;
1025 farg.d = int64_to_float64(arg, &env->fp_status);
1026 return farg.ll;
426613db
JM
1027}
1028
af12906f
AJ
1029/* fctid - fctid. */
1030uint64_t helper_fctid (uint64_t arg)
426613db 1031{
af12906f
AJ
1032 CPU_DoubleU farg;
1033 farg.ll = arg;
426613db 1034
af12906f 1035 if (unlikely(float64_is_signaling_nan(farg.d))) {
7c58044c 1036 /* sNaN conversion */
af12906f
AJ
1037 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
1038 } else if (unlikely(float64_is_nan(farg.d) || isinfinity(farg.d))) {
7c58044c 1039 /* qNan / infinity conversion */
af12906f 1040 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
7c58044c 1041 } else {
af12906f 1042 farg.ll = float64_to_int64(farg.d, &env->fp_status);
7c58044c 1043 }
af12906f 1044 return farg.ll;
426613db
JM
1045}
1046
af12906f
AJ
1047/* fctidz - fctidz. */
1048uint64_t helper_fctidz (uint64_t arg)
426613db 1049{
af12906f
AJ
1050 CPU_DoubleU farg;
1051 farg.ll = arg;
426613db 1052
af12906f 1053 if (unlikely(float64_is_signaling_nan(farg.d))) {
7c58044c 1054 /* sNaN conversion */
af12906f
AJ
1055 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
1056 } else if (unlikely(float64_is_nan(farg.d) || isinfinity(farg.d))) {
7c58044c 1057 /* qNan / infinity conversion */
af12906f 1058 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
7c58044c 1059 } else {
af12906f 1060 farg.ll = float64_to_int64_round_to_zero(farg.d, &env->fp_status);
7c58044c 1061 }
af12906f 1062 return farg.ll;
426613db
JM
1063}
1064
1065#endif
1066
af12906f 1067static always_inline uint64_t do_fri (uint64_t arg, int rounding_mode)
d7e4b87e 1068{
af12906f
AJ
1069 CPU_DoubleU farg;
1070 farg.ll = arg;
1071
1072 if (unlikely(float64_is_signaling_nan(farg.d))) {
7c58044c 1073 /* sNaN round */
af12906f
AJ
1074 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
1075 } else if (unlikely(float64_is_nan(farg.d) || isinfinity(farg.d))) {
7c58044c 1076 /* qNan / infinity round */
af12906f 1077 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
7c58044c
JM
1078 } else {
1079 set_float_rounding_mode(rounding_mode, &env->fp_status);
af12906f 1080 farg.ll = float64_round_to_int(farg.d, &env->fp_status);
7c58044c
JM
1081 /* Restore rounding mode from FPSCR */
1082 fpscr_set_rounding_mode();
1083 }
af12906f 1084 return farg.ll;
d7e4b87e
JM
1085}
1086
af12906f 1087uint64_t helper_frin (uint64_t arg)
d7e4b87e 1088{
af12906f 1089 return do_fri(arg, float_round_nearest_even);
d7e4b87e
JM
1090}
1091
af12906f 1092uint64_t helper_friz (uint64_t arg)
d7e4b87e 1093{
af12906f 1094 return do_fri(arg, float_round_to_zero);
d7e4b87e
JM
1095}
1096
af12906f 1097uint64_t helper_frip (uint64_t arg)
d7e4b87e 1098{
af12906f 1099 return do_fri(arg, float_round_up);
d7e4b87e
JM
1100}
1101
af12906f 1102uint64_t helper_frim (uint64_t arg)
d7e4b87e 1103{
af12906f 1104 return do_fri(arg, float_round_down);
d7e4b87e
JM
1105}
1106
af12906f
AJ
1107/* fmadd - fmadd. */
1108uint64_t helper_fmadd (uint64_t arg1, uint64_t arg2, uint64_t arg3)
e864cabd 1109{
af12906f
AJ
1110 CPU_DoubleU farg1, farg2, farg3;
1111
1112 farg1.ll = arg1;
1113 farg2.ll = arg2;
1114 farg3.ll = arg3;
1115#if USE_PRECISE_EMULATION
1116 if (unlikely(float64_is_signaling_nan(farg1.d) ||
1117 float64_is_signaling_nan(farg2.d) ||
1118 float64_is_signaling_nan(farg3.d))) {
7c58044c 1119 /* sNaN operation */
af12906f 1120 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
7c58044c 1121 } else {
e864cabd 1122#ifdef FLOAT128
7c58044c
JM
1123 /* This is the way the PowerPC specification defines it */
1124 float128 ft0_128, ft1_128;
1125
af12906f
AJ
1126 ft0_128 = float64_to_float128(farg1.d, &env->fp_status);
1127 ft1_128 = float64_to_float128(farg2.d, &env->fp_status);
7c58044c 1128 ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
af12906f 1129 ft1_128 = float64_to_float128(farg3.d, &env->fp_status);
7c58044c 1130 ft0_128 = float128_add(ft0_128, ft1_128, &env->fp_status);
af12906f 1131 farg1.d = float128_to_float64(ft0_128, &env->fp_status);
e864cabd 1132#else
7c58044c 1133 /* This is OK on x86 hosts */
af12906f 1134 farg1.d = (farg1.d * farg2.d) + farg3.d;
e864cabd 1135#endif
7c58044c 1136 }
af12906f
AJ
1137#else
1138 farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
1139 farg1.d = float64_add(farg1.d, farg3.d, &env->fp_status);
1140#endif
1141 return farg1.ll;
e864cabd
JM
1142}
1143
af12906f
AJ
1144/* fmsub - fmsub. */
1145uint64_t helper_fmsub (uint64_t arg1, uint64_t arg2, uint64_t arg3)
e864cabd 1146{
af12906f
AJ
1147 CPU_DoubleU farg1, farg2, farg3;
1148
1149 farg1.ll = arg1;
1150 farg2.ll = arg2;
1151 farg3.ll = arg3;
1152#if USE_PRECISE_EMULATION
1153 if (unlikely(float64_is_signaling_nan(farg1.d) ||
1154 float64_is_signaling_nan(farg2.d) ||
1155 float64_is_signaling_nan(farg3.d))) {
7c58044c 1156 /* sNaN operation */
af12906f 1157 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
7c58044c 1158 } else {
e864cabd 1159#ifdef FLOAT128
7c58044c
JM
1160 /* This is the way the PowerPC specification defines it */
1161 float128 ft0_128, ft1_128;
1162
af12906f
AJ
1163 ft0_128 = float64_to_float128(farg1.d, &env->fp_status);
1164 ft1_128 = float64_to_float128(farg2.d, &env->fp_status);
7c58044c 1165 ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
af12906f 1166 ft1_128 = float64_to_float128(farg3.d, &env->fp_status);
7c58044c 1167 ft0_128 = float128_sub(ft0_128, ft1_128, &env->fp_status);
af12906f 1168 farg1.d = float128_to_float64(ft0_128, &env->fp_status);
e864cabd 1169#else
7c58044c 1170 /* This is OK on x86 hosts */
af12906f 1171 farg1.d = (farg1.d * farg2.d) - farg3.d;
e864cabd 1172#endif
7c58044c 1173 }
af12906f
AJ
1174#else
1175 farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
1176 farg1.d = float64_sub(farg1.d, farg3.d, &env->fp_status);
1177#endif
1178 return farg1.ll;
e864cabd 1179}
e864cabd 1180
af12906f
AJ
1181/* fnmadd - fnmadd. */
1182uint64_t helper_fnmadd (uint64_t arg1, uint64_t arg2, uint64_t arg3)
4b3686fa 1183{
af12906f
AJ
1184 CPU_DoubleU farg1, farg2, farg3;
1185
1186 farg1.ll = arg1;
1187 farg2.ll = arg2;
1188 farg3.ll = arg3;
1189
1190 if (unlikely(float64_is_signaling_nan(farg1.d) ||
1191 float64_is_signaling_nan(farg2.d) ||
1192 float64_is_signaling_nan(farg3.d))) {
7c58044c 1193 /* sNaN operation */
af12906f 1194 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
7c58044c 1195 } else {
1cdb9c3d 1196#if USE_PRECISE_EMULATION
e864cabd 1197#ifdef FLOAT128
7c58044c
JM
1198 /* This is the way the PowerPC specification defines it */
1199 float128 ft0_128, ft1_128;
1200
af12906f
AJ
1201 ft0_128 = float64_to_float128(farg1.d, &env->fp_status);
1202 ft1_128 = float64_to_float128(farg2.d, &env->fp_status);
7c58044c 1203 ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
af12906f 1204 ft1_128 = float64_to_float128(farg3.d, &env->fp_status);
7c58044c 1205 ft0_128 = float128_add(ft0_128, ft1_128, &env->fp_status);
af12906f 1206 farg1.d= float128_to_float64(ft0_128, &env->fp_status);
e864cabd 1207#else
7c58044c 1208 /* This is OK on x86 hosts */
af12906f 1209 farg1.d = (farg1.d * farg2.d) + farg3.d;
e864cabd
JM
1210#endif
1211#else
af12906f
AJ
1212 farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
1213 farg1.d = float64_add(farg1.d, farg3.d, &env->fp_status);
e864cabd 1214#endif
af12906f
AJ
1215 if (likely(!isnan(farg1.d)))
1216 farg1.d = float64_chs(farg1.d);
7c58044c 1217 }
af12906f 1218 return farg1.ll;
4b3686fa
FB
1219}
1220
af12906f
AJ
1221/* fnmsub - fnmsub. */
1222uint64_t helper_fnmsub (uint64_t arg1, uint64_t arg2, uint64_t arg3)
4b3686fa 1223{
af12906f
AJ
1224 CPU_DoubleU farg1, farg2, farg3;
1225
1226 farg1.ll = arg1;
1227 farg2.ll = arg2;
1228 farg3.ll = arg3;
1229
1230 if (unlikely(float64_is_signaling_nan(farg1.d) ||
1231 float64_is_signaling_nan(farg2.d) ||
1232 float64_is_signaling_nan(farg3.d))) {
7c58044c 1233 /* sNaN operation */
af12906f 1234 farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
7c58044c 1235 } else {
1cdb9c3d 1236#if USE_PRECISE_EMULATION
e864cabd 1237#ifdef FLOAT128
7c58044c
JM
1238 /* This is the way the PowerPC specification defines it */
1239 float128 ft0_128, ft1_128;
1240
af12906f
AJ
1241 ft0_128 = float64_to_float128(farg1.d, &env->fp_status);
1242 ft1_128 = float64_to_float128(farg2.d, &env->fp_status);
7c58044c 1243 ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
af12906f 1244 ft1_128 = float64_to_float128(farg3.d, &env->fp_status);
7c58044c 1245 ft0_128 = float128_sub(ft0_128, ft1_128, &env->fp_status);
af12906f 1246 farg1.d = float128_to_float64(ft0_128, &env->fp_status);
e864cabd 1247#else
7c58044c 1248 /* This is OK on x86 hosts */
af12906f 1249 farg1.d = (farg1.d * farg2.d) - farg3.d;
e864cabd
JM
1250#endif
1251#else
af12906f
AJ
1252 farg1.d = float64_mul(farg1.d, farg2.d, &env->fp_status);
1253 farg1.d = float64_sub(farg1.d, farg3.d, &env->fp_status);
e864cabd 1254#endif
af12906f
AJ
1255 if (likely(!isnan(farg1.d)))
1256 farg1.d = float64_chs(farg1.d);
7c58044c 1257 }
af12906f 1258 return farg1.ll;
1ef59d0a
FB
1259}
1260
af12906f
AJ
1261/* frsp - frsp. */
1262uint64_t helper_frsp (uint64_t arg)
7c58044c 1263{
af12906f
AJ
1264 CPU_DoubleU farg;
1265 farg.ll = arg;
1266
1267#if USE_PRECISE_EMULATION
1268 if (unlikely(float64_is_signaling_nan(farg.d))) {
7c58044c 1269 /* sNaN square root */
af12906f 1270 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
7c58044c 1271 } else {
af12906f 1272 fard.d = float64_to_float32(farg.d, &env->fp_status);
7c58044c 1273 }
af12906f
AJ
1274#else
1275 farg.d = float64_to_float32(farg.d, &env->fp_status);
1276#endif
1277 return farg.ll;
7c58044c 1278}
7c58044c 1279
af12906f
AJ
1280/* fsqrt - fsqrt. */
1281uint64_t helper_fsqrt (uint64_t arg)
9a64fbe4 1282{
af12906f
AJ
1283 CPU_DoubleU farg;
1284 farg.ll = arg;
1285
1286 if (unlikely(float64_is_signaling_nan(farg.d))) {
7c58044c 1287 /* sNaN square root */
af12906f
AJ
1288 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1289 } else if (unlikely(fpisneg(farg.d) && !iszero(farg.d))) {
7c58044c 1290 /* Square root of a negative nonzero number */
af12906f 1291 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT);
7c58044c 1292 } else {
af12906f 1293 farg.d = float64_sqrt(farg.d, &env->fp_status);
7c58044c 1294 }
af12906f 1295 return farg.ll;
9a64fbe4
FB
1296}
1297
af12906f
AJ
1298/* fre - fre. */
1299uint64_t helper_fre (uint64_t arg)
d7e4b87e 1300{
af12906f
AJ
1301 CPU_DoubleU farg;
1302 farg.ll = arg;
d7e4b87e 1303
af12906f 1304 if (unlikely(float64_is_signaling_nan(farg.d))) {
7c58044c 1305 /* sNaN reciprocal */
af12906f
AJ
1306 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1307 } else if (unlikely(iszero(farg.d))) {
7c58044c 1308 /* Zero reciprocal */
af12906f
AJ
1309 farg.ll = float_zero_divide_excp(1.0, farg.d);
1310 } else if (likely(isnormal(farg.d))) {
1311 farg.d = float64_div(1.0, farg.d, &env->fp_status);
d7e4b87e 1312 } else {
af12906f
AJ
1313 if (farg.ll == 0x8000000000000000ULL) {
1314 farg.ll = 0xFFF0000000000000ULL;
1315 } else if (farg.ll == 0x0000000000000000ULL) {
1316 farg.ll = 0x7FF0000000000000ULL;
1317 } else if (isnan(farg.d)) {
1318 farg.ll = 0x7FF8000000000000ULL;
1319 } else if (fpisneg(farg.d)) {
1320 farg.ll = 0x8000000000000000ULL;
d7e4b87e 1321 } else {
af12906f 1322 farg.ll = 0x0000000000000000ULL;
d7e4b87e 1323 }
d7e4b87e 1324 }
af12906f 1325 return farg.d;
d7e4b87e
JM
1326}
1327
af12906f
AJ
1328/* fres - fres. */
1329uint64_t helper_fres (uint64_t arg)
9a64fbe4 1330{
af12906f
AJ
1331 CPU_DoubleU farg;
1332 farg.ll = arg;
4ecc3190 1333
af12906f 1334 if (unlikely(float64_is_signaling_nan(farg.d))) {
7c58044c 1335 /* sNaN reciprocal */
af12906f
AJ
1336 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1337 } else if (unlikely(iszero(farg.d))) {
7c58044c 1338 /* Zero reciprocal */
af12906f
AJ
1339 farg.ll = float_zero_divide_excp(1.0, farg.d);
1340 } else if (likely(isnormal(farg.d))) {
1cdb9c3d 1341#if USE_PRECISE_EMULATION
af12906f
AJ
1342 farg.d = float64_div(1.0, farg.d, &env->fp_status);
1343 farg.d = float64_to_float32(farg.d, &env->fp_status);
e864cabd 1344#else
af12906f 1345 farg.d = float32_div(1.0, farg.d, &env->fp_status);
e864cabd 1346#endif
4ecc3190 1347 } else {
af12906f
AJ
1348 if (farg.ll == 0x8000000000000000ULL) {
1349 farg.ll = 0xFFF0000000000000ULL;
1350 } else if (farg.ll == 0x0000000000000000ULL) {
1351 farg.ll = 0x7FF0000000000000ULL;
1352 } else if (isnan(farg.d)) {
1353 farg.ll = 0x7FF8000000000000ULL;
1354 } else if (fpisneg(farg.d)) {
1355 farg.ll = 0x8000000000000000ULL;
4ecc3190 1356 } else {
af12906f 1357 farg.ll = 0x0000000000000000ULL;
4ecc3190 1358 }
4ecc3190 1359 }
af12906f 1360 return farg.ll;
9a64fbe4
FB
1361}
1362
af12906f
AJ
1363/* frsqrte - frsqrte. */
1364uint64_t helper_frsqrte (uint64_t arg)
9a64fbe4 1365{
af12906f
AJ
1366 CPU_DoubleU farg;
1367 farg.ll = arg;
4ecc3190 1368
af12906f 1369 if (unlikely(float64_is_signaling_nan(farg.d))) {
7c58044c 1370 /* sNaN reciprocal square root */
af12906f
AJ
1371 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1372 } else if (unlikely(fpisneg(farg.d) && !iszero(farg.d))) {
7c58044c 1373 /* Reciprocal square root of a negative nonzero number */
af12906f
AJ
1374 farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT);
1375 } else if (likely(isnormal(farg.d))) {
1376 farg.d = float64_sqrt(farg.d, &env->fp_status);
1377 farg.d = float32_div(1.0, farg.d, &env->fp_status);
4ecc3190 1378 } else {
af12906f
AJ
1379 if (farg.ll == 0x8000000000000000ULL) {
1380 farg.ll = 0xFFF0000000000000ULL;
1381 } else if (farg.ll == 0x0000000000000000ULL) {
1382 farg.ll = 0x7FF0000000000000ULL;
1383 } else if (isnan(farg.d)) {
1384 farg.ll |= 0x000FFFFFFFFFFFFFULL;
1385 } else if (fpisneg(farg.d)) {
1386 farg.ll = 0x7FF8000000000000ULL;
4ecc3190 1387 } else {
af12906f 1388 farg.ll = 0x0000000000000000ULL;
4ecc3190 1389 }
4ecc3190 1390 }
af12906f 1391 return farg.ll;
9a64fbe4
FB
1392}
1393
af12906f
AJ
1394/* fsel - fsel. */
1395uint64_t helper_fsel (uint64_t arg1, uint64_t arg2, uint64_t arg3)
9a64fbe4 1396{
af12906f
AJ
1397 CPU_DoubleU farg1, farg2, farg3;
1398
1399 farg1.ll = arg1;
1400 farg2.ll = arg2;
1401 farg3.ll = arg3;
1402
1403 if (!fpisneg(farg1.d) || iszero(farg1.d))
1404 return farg2.ll;
4ecc3190 1405 else
af12906f 1406 return farg2.ll;
9a64fbe4
FB
1407}
1408
af12906f 1409uint32_t helper_fcmpu (uint64_t arg1, uint64_t arg2)
9a64fbe4 1410{
af12906f 1411 CPU_DoubleU farg1, farg2;
e1571908 1412 uint32_t ret = 0;
af12906f
AJ
1413 farg1.ll = arg1;
1414 farg2.ll = arg2;
e1571908 1415
af12906f
AJ
1416 if (unlikely(float64_is_signaling_nan(farg1.d) ||
1417 float64_is_signaling_nan(farg2.d))) {
7c58044c
JM
1418 /* sNaN comparison */
1419 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
1420 } else {
af12906f 1421 if (float64_lt(farg1.d, farg2.d, &env->fp_status)) {
e1571908 1422 ret = 0x08UL;
af12906f 1423 } else if (!float64_le(farg1.d, farg2.d, &env->fp_status)) {
e1571908 1424 ret = 0x04UL;
fdabc366 1425 } else {
e1571908 1426 ret = 0x02UL;
fdabc366 1427 }
9a64fbe4 1428 }
7c58044c 1429 env->fpscr &= ~(0x0F << FPSCR_FPRF);
e1571908
AJ
1430 env->fpscr |= ret << FPSCR_FPRF;
1431 return ret;
9a64fbe4
FB
1432}
1433
af12906f 1434uint32_t helper_fcmpo (uint64_t arg1, uint64_t arg2)
9a64fbe4 1435{
af12906f 1436 CPU_DoubleU farg1, farg2;
e1571908 1437 uint32_t ret = 0;
af12906f
AJ
1438 farg1.ll = arg1;
1439 farg2.ll = arg2;
e1571908 1440
af12906f
AJ
1441 if (unlikely(float64_is_nan(farg1.d) ||
1442 float64_is_nan(farg2.d))) {
1443 if (float64_is_signaling_nan(farg1.d) ||
1444 float64_is_signaling_nan(farg2.d)) {
7c58044c
JM
1445 /* sNaN comparison */
1446 fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN |
1447 POWERPC_EXCP_FP_VXVC);
1448 } else {
1449 /* qNaN comparison */
1450 fload_invalid_op_excp(POWERPC_EXCP_FP_VXVC);
1451 }
1452 } else {
af12906f 1453 if (float64_lt(farg1.d, farg2.d, &env->fp_status)) {
e1571908 1454 ret = 0x08UL;
af12906f 1455 } else if (!float64_le(farg1.d, farg2.d, &env->fp_status)) {
e1571908 1456 ret = 0x04UL;
fdabc366 1457 } else {
e1571908 1458 ret = 0x02UL;
fdabc366 1459 }
9a64fbe4 1460 }
7c58044c 1461 env->fpscr &= ~(0x0F << FPSCR_FPRF);
e1571908
AJ
1462 env->fpscr |= ret << FPSCR_FPRF;
1463 return ret;
9a64fbe4
FB
1464}
1465
76a66253 1466#if !defined (CONFIG_USER_ONLY)
6b80055d 1467void cpu_dump_rfi (target_ulong RA, target_ulong msr);
0411a972 1468
6676f424 1469void do_store_msr (void)
0411a972 1470{
6676f424
AJ
1471 T0 = hreg_store_msr(env, T0, 0);
1472 if (T0 != 0) {
0411a972 1473 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
64adab3f 1474 raise_exception(env, T0);
0411a972
JM
1475 }
1476}
1477
1478static always_inline void __do_rfi (target_ulong nip, target_ulong msr,
1479 target_ulong msrm, int keep_msrh)
9a64fbe4 1480{
426613db 1481#if defined(TARGET_PPC64)
0411a972
JM
1482 if (msr & (1ULL << MSR_SF)) {
1483 nip = (uint64_t)nip;
1484 msr &= (uint64_t)msrm;
a42bd6cc 1485 } else {
0411a972
JM
1486 nip = (uint32_t)nip;
1487 msr = (uint32_t)(msr & msrm);
1488 if (keep_msrh)
1489 msr |= env->msr & ~((uint64_t)0xFFFFFFFF);
a42bd6cc 1490 }
426613db 1491#else
0411a972
JM
1492 nip = (uint32_t)nip;
1493 msr &= (uint32_t)msrm;
426613db 1494#endif
0411a972
JM
1495 /* XXX: beware: this is false if VLE is supported */
1496 env->nip = nip & ~((target_ulong)0x00000003);
a4f30719 1497 hreg_store_msr(env, msr, 1);
fdabc366 1498#if defined (DEBUG_OP)
0411a972 1499 cpu_dump_rfi(env->nip, env->msr);
fdabc366 1500#endif
0411a972
JM
1501 /* No need to raise an exception here,
1502 * as rfi is always the last insn of a TB
1503 */
fdabc366 1504 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
9a64fbe4 1505}
d9bce9d9 1506
0411a972
JM
1507void do_rfi (void)
1508{
1509 __do_rfi(env->spr[SPR_SRR0], env->spr[SPR_SRR1],
1510 ~((target_ulong)0xFFFF0000), 1);
1511}
1512
d9bce9d9 1513#if defined(TARGET_PPC64)
426613db
JM
1514void do_rfid (void)
1515{
0411a972
JM
1516 __do_rfi(env->spr[SPR_SRR0], env->spr[SPR_SRR1],
1517 ~((target_ulong)0xFFFF0000), 0);
d9bce9d9 1518}
7863667f 1519
be147d08
JM
1520void do_hrfid (void)
1521{
0411a972
JM
1522 __do_rfi(env->spr[SPR_HSRR0], env->spr[SPR_HSRR1],
1523 ~((target_ulong)0xFFFF0000), 0);
be147d08
JM
1524}
1525#endif
76a66253 1526#endif
9a64fbe4 1527
cab3bee2 1528void helper_tw (target_ulong arg1, target_ulong arg2, uint32_t flags)
9a64fbe4 1529{
cab3bee2
AJ
1530 if (!likely(!(((int32_t)arg1 < (int32_t)arg2 && (flags & 0x10)) ||
1531 ((int32_t)arg1 > (int32_t)arg2 && (flags & 0x08)) ||
1532 ((int32_t)arg1 == (int32_t)arg2 && (flags & 0x04)) ||
1533 ((uint32_t)arg1 < (uint32_t)arg2 && (flags & 0x02)) ||
1534 ((uint32_t)arg1 > (uint32_t)arg2 && (flags & 0x01))))) {
64adab3f 1535 raise_exception_err(env, POWERPC_EXCP_PROGRAM, POWERPC_EXCP_TRAP);
a42bd6cc 1536 }
9a64fbe4
FB
1537}
1538
d9bce9d9 1539#if defined(TARGET_PPC64)
cab3bee2 1540void helper_td (target_ulong arg1, target_ulong arg2, uint32_t flags)
d9bce9d9 1541{
cab3bee2
AJ
1542 if (!likely(!(((int64_t)arg1 < (int64_t)arg2 && (flags & 0x10)) ||
1543 ((int64_t)arg1 > (int64_t)arg2 && (flags & 0x08)) ||
1544 ((int64_t)arg1 == (int64_t)arg2 && (flags & 0x04)) ||
1545 ((uint64_t)arg1 < (uint64_t)arg2 && (flags & 0x02)) ||
1546 ((uint64_t)arg1 > (uint64_t)arg2 && (flags & 0x01)))))
64adab3f 1547 raise_exception_err(env, POWERPC_EXCP_PROGRAM, POWERPC_EXCP_TRAP);
d9bce9d9
JM
1548}
1549#endif
1550
fdabc366 1551/*****************************************************************************/
76a66253
JM
1552/* PowerPC 601 specific instructions (POWER bridge) */
1553void do_POWER_abso (void)
9a64fbe4 1554{
9c7e37e7 1555 if ((int32_t)T0 == INT32_MIN) {
76a66253 1556 T0 = INT32_MAX;
3d7b417e 1557 env->xer |= (1 << XER_OV) | (1 << XER_SO);
9c7e37e7 1558 } else if ((int32_t)T0 < 0) {
76a66253 1559 T0 = -T0;
3d7b417e 1560 env->xer &= ~(1 << XER_OV);
9c7e37e7 1561 } else {
3d7b417e 1562 env->xer &= ~(1 << XER_OV);
76a66253 1563 }
9a64fbe4
FB
1564}
1565
76a66253 1566void do_POWER_clcs (void)
9a64fbe4 1567{
76a66253
JM
1568 switch (T0) {
1569 case 0x0CUL:
1570 /* Instruction cache line size */
d63001d1 1571 T0 = env->icache_line_size;
76a66253
JM
1572 break;
1573 case 0x0DUL:
1574 /* Data cache line size */
d63001d1 1575 T0 = env->dcache_line_size;
76a66253
JM
1576 break;
1577 case 0x0EUL:
1578 /* Minimum cache line size */
d63001d1
JM
1579 T0 = env->icache_line_size < env->dcache_line_size ?
1580 env->icache_line_size : env->dcache_line_size;
76a66253
JM
1581 break;
1582 case 0x0FUL:
1583 /* Maximum cache line size */
d63001d1
JM
1584 T0 = env->icache_line_size > env->dcache_line_size ?
1585 env->icache_line_size : env->dcache_line_size;
76a66253
JM
1586 break;
1587 default:
1588 /* Undefined */
1589 break;
1590 }
1591}
1592
1593void do_POWER_div (void)
1594{
1595 uint64_t tmp;
1596
6f2d8978
JM
1597 if (((int32_t)T0 == INT32_MIN && (int32_t)T1 == (int32_t)-1) ||
1598 (int32_t)T1 == 0) {
1599 T0 = UINT32_MAX * ((uint32_t)T0 >> 31);
76a66253
JM
1600 env->spr[SPR_MQ] = 0;
1601 } else {
1602 tmp = ((uint64_t)T0 << 32) | env->spr[SPR_MQ];
1603 env->spr[SPR_MQ] = tmp % T1;
d9bce9d9 1604 T0 = tmp / (int32_t)T1;
76a66253
JM
1605 }
1606}
1607
1608void do_POWER_divo (void)
1609{
1610 int64_t tmp;
1611
6f2d8978
JM
1612 if (((int32_t)T0 == INT32_MIN && (int32_t)T1 == (int32_t)-1) ||
1613 (int32_t)T1 == 0) {
1614 T0 = UINT32_MAX * ((uint32_t)T0 >> 31);
76a66253 1615 env->spr[SPR_MQ] = 0;
3d7b417e 1616 env->xer |= (1 << XER_OV) | (1 << XER_SO);
76a66253
JM
1617 } else {
1618 tmp = ((uint64_t)T0 << 32) | env->spr[SPR_MQ];
1619 env->spr[SPR_MQ] = tmp % T1;
d9bce9d9 1620 tmp /= (int32_t)T1;
76a66253 1621 if (tmp > (int64_t)INT32_MAX || tmp < (int64_t)INT32_MIN) {
3d7b417e 1622 env->xer |= (1 << XER_OV) | (1 << XER_SO);
76a66253 1623 } else {
3d7b417e 1624 env->xer &= ~(1 << XER_OV);
76a66253
JM
1625 }
1626 T0 = tmp;
1627 }
1628}
1629
1630void do_POWER_divs (void)
1631{
6f2d8978
JM
1632 if (((int32_t)T0 == INT32_MIN && (int32_t)T1 == (int32_t)-1) ||
1633 (int32_t)T1 == 0) {
1634 T0 = UINT32_MAX * ((uint32_t)T0 >> 31);
76a66253
JM
1635 env->spr[SPR_MQ] = 0;
1636 } else {
1637 env->spr[SPR_MQ] = T0 % T1;
d9bce9d9 1638 T0 = (int32_t)T0 / (int32_t)T1;
76a66253
JM
1639 }
1640}
1641
1642void do_POWER_divso (void)
1643{
6f2d8978
JM
1644 if (((int32_t)T0 == INT32_MIN && (int32_t)T1 == (int32_t)-1) ||
1645 (int32_t)T1 == 0) {
1646 T0 = UINT32_MAX * ((uint32_t)T0 >> 31);
76a66253 1647 env->spr[SPR_MQ] = 0;
3d7b417e 1648 env->xer |= (1 << XER_OV) | (1 << XER_SO);
76a66253 1649 } else {
d9bce9d9
JM
1650 T0 = (int32_t)T0 / (int32_t)T1;
1651 env->spr[SPR_MQ] = (int32_t)T0 % (int32_t)T1;
3d7b417e 1652 env->xer &= ~(1 << XER_OV);
76a66253
JM
1653 }
1654}
1655
1656void do_POWER_dozo (void)
1657{
d9bce9d9 1658 if ((int32_t)T1 > (int32_t)T0) {
76a66253
JM
1659 T2 = T0;
1660 T0 = T1 - T0;
d9bce9d9
JM
1661 if (((uint32_t)(~T2) ^ (uint32_t)T1 ^ UINT32_MAX) &
1662 ((uint32_t)(~T2) ^ (uint32_t)T0) & (1UL << 31)) {
3d7b417e 1663 env->xer |= (1 << XER_OV) | (1 << XER_SO);
76a66253 1664 } else {
3d7b417e 1665 env->xer &= ~(1 << XER_OV);
76a66253
JM
1666 }
1667 } else {
1668 T0 = 0;
3d7b417e 1669 env->xer &= ~(1 << XER_OV);
76a66253
JM
1670 }
1671}
1672
1673void do_POWER_maskg (void)
1674{
1675 uint32_t ret;
1676
d9bce9d9 1677 if ((uint32_t)T0 == (uint32_t)(T1 + 1)) {
6f2d8978 1678 ret = UINT32_MAX;
76a66253 1679 } else {
6f2d8978
JM
1680 ret = (UINT32_MAX >> ((uint32_t)T0)) ^
1681 ((UINT32_MAX >> ((uint32_t)T1)) >> 1);
d9bce9d9 1682 if ((uint32_t)T0 > (uint32_t)T1)
76a66253
JM
1683 ret = ~ret;
1684 }
1685 T0 = ret;
1686}
1687
1688void do_POWER_mulo (void)
1689{
1690 uint64_t tmp;
1691
1692 tmp = (uint64_t)T0 * (uint64_t)T1;
1693 env->spr[SPR_MQ] = tmp >> 32;
1694 T0 = tmp;
1695 if (tmp >> 32 != ((uint64_t)T0 >> 16) * ((uint64_t)T1 >> 16)) {
3d7b417e 1696 env->xer |= (1 << XER_OV) | (1 << XER_SO);
76a66253 1697 } else {
3d7b417e 1698 env->xer &= ~(1 << XER_OV);
76a66253
JM
1699 }
1700}
1701
1702#if !defined (CONFIG_USER_ONLY)
1703void do_POWER_rac (void)
1704{
76a66253 1705 mmu_ctx_t ctx;
faadf50e 1706 int nb_BATs;
76a66253
JM
1707
1708 /* We don't have to generate many instances of this instruction,
1709 * as rac is supervisor only.
1710 */
faadf50e
JM
1711 /* XXX: FIX THIS: Pretend we have no BAT */
1712 nb_BATs = env->nb_BATs;
1713 env->nb_BATs = 0;
1714 if (get_physical_address(env, &ctx, T0, 0, ACCESS_INT) == 0)
76a66253 1715 T0 = ctx.raddr;
faadf50e 1716 env->nb_BATs = nb_BATs;
76a66253
JM
1717}
1718
1719void do_POWER_rfsvc (void)
1720{
0411a972 1721 __do_rfi(env->lr, env->ctr, 0x0000FFFF, 0);
76a66253
JM
1722}
1723
056401ea
JM
1724void do_store_hid0_601 (void)
1725{
1726 uint32_t hid0;
1727
1728 hid0 = env->spr[SPR_HID0];
1729 if ((T0 ^ hid0) & 0x00000008) {
1730 /* Change current endianness */
1731 env->hflags &= ~(1 << MSR_LE);
1732 env->hflags_nmsr &= ~(1 << MSR_LE);
1733 env->hflags_nmsr |= (1 << MSR_LE) & (((T0 >> 3) & 1) << MSR_LE);
1734 env->hflags |= env->hflags_nmsr;
1735 if (loglevel != 0) {
1736 fprintf(logfile, "%s: set endianness to %c => " ADDRX "\n",
1737 __func__, T0 & 0x8 ? 'l' : 'b', env->hflags);
1738 }
1739 }
1740 env->spr[SPR_HID0] = T0;
76a66253
JM
1741}
1742#endif
1743
1744/*****************************************************************************/
1745/* 602 specific instructions */
1746/* mfrom is the most crazy instruction ever seen, imho ! */
1747/* Real implementation uses a ROM table. Do the same */
1748#define USE_MFROM_ROM_TABLE
cf02a65c 1749target_ulong helper_602_mfrom (target_ulong arg)
76a66253 1750{
cf02a65c 1751 if (likely(arg < 602)) {
d9bce9d9 1752#if defined(USE_MFROM_ROM_TABLE)
76a66253 1753#include "mfrom_table.c"
cf02a65c 1754 return mfrom_ROM_table[T0];
fdabc366 1755#else
76a66253
JM
1756 double d;
1757 /* Extremly decomposed:
cf02a65c
AJ
1758 * -arg / 256
1759 * return 256 * log10(10 + 1.0) + 0.5
76a66253 1760 */
cf02a65c 1761 d = arg;
76a66253
JM
1762 d = float64_div(d, 256, &env->fp_status);
1763 d = float64_chs(d);
1764 d = exp10(d); // XXX: use float emulation function
1765 d = float64_add(d, 1.0, &env->fp_status);
1766 d = log10(d); // XXX: use float emulation function
1767 d = float64_mul(d, 256, &env->fp_status);
1768 d = float64_add(d, 0.5, &env->fp_status);
cf02a65c 1769 return float64_round_to_int(d, &env->fp_status);
fdabc366 1770#endif
76a66253 1771 } else {
cf02a65c 1772 return 0;
76a66253
JM
1773 }
1774}
1775
1776/*****************************************************************************/
1777/* Embedded PowerPC specific helpers */
76a66253 1778
a750fc0b
JM
1779/* XXX: to be improved to check access rights when in user-mode */
1780void do_load_dcr (void)
1781{
1782 target_ulong val;
1783
1784 if (unlikely(env->dcr_env == NULL)) {
1785 if (loglevel != 0) {
1786 fprintf(logfile, "No DCR environment\n");
1787 }
64adab3f
AJ
1788 raise_exception_err(env, POWERPC_EXCP_PROGRAM,
1789 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL);
a750fc0b
JM
1790 } else if (unlikely(ppc_dcr_read(env->dcr_env, T0, &val) != 0)) {
1791 if (loglevel != 0) {
1792 fprintf(logfile, "DCR read error %d %03x\n", (int)T0, (int)T0);
1793 }
64adab3f
AJ
1794 raise_exception_err(env, POWERPC_EXCP_PROGRAM,
1795 POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_REG);
a750fc0b
JM
1796 } else {
1797 T0 = val;
1798 }
1799}
1800
1801void do_store_dcr (void)
1802{
1803 if (unlikely(env->dcr_env == NULL)) {
1804 if (loglevel != 0) {
1805 fprintf(logfile, "No DCR environment\n");
1806 }
64adab3f
AJ
1807 raise_exception_err(env, POWERPC_EXCP_PROGRAM,
1808 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL);
a750fc0b
JM
1809 } else if (unlikely(ppc_dcr_write(env->dcr_env, T0, T1) != 0)) {
1810 if (loglevel != 0) {
1811 fprintf(logfile, "DCR write error %d %03x\n", (int)T0, (int)T0);
1812 }
64adab3f
AJ
1813 raise_exception_err(env, POWERPC_EXCP_PROGRAM,
1814 POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_REG);
a750fc0b
JM
1815 }
1816}
1817
76a66253 1818#if !defined(CONFIG_USER_ONLY)
a42bd6cc 1819void do_40x_rfci (void)
76a66253 1820{
0411a972
JM
1821 __do_rfi(env->spr[SPR_40x_SRR2], env->spr[SPR_40x_SRR3],
1822 ~((target_ulong)0xFFFF0000), 0);
a42bd6cc
JM
1823}
1824
1825void do_rfci (void)
1826{
0411a972
JM
1827 __do_rfi(env->spr[SPR_BOOKE_CSRR0], SPR_BOOKE_CSRR1,
1828 ~((target_ulong)0x3FFF0000), 0);
a42bd6cc
JM
1829}
1830
1831void do_rfdi (void)
1832{
0411a972
JM
1833 __do_rfi(env->spr[SPR_BOOKE_DSRR0], SPR_BOOKE_DSRR1,
1834 ~((target_ulong)0x3FFF0000), 0);
a42bd6cc
JM
1835}
1836
1837void do_rfmci (void)
1838{
0411a972
JM
1839 __do_rfi(env->spr[SPR_BOOKE_MCSRR0], SPR_BOOKE_MCSRR1,
1840 ~((target_ulong)0x3FFF0000), 0);
76a66253
JM
1841}
1842
76a66253
JM
1843void do_load_403_pb (int num)
1844{
1845 T0 = env->pb[num];
1846}
1847
1848void do_store_403_pb (int num)
1849{
1850 if (likely(env->pb[num] != T0)) {
1851 env->pb[num] = T0;
1852 /* Should be optimized */
1853 tlb_flush(env, 1);
1854 }
1855}
1856#endif
1857
1858/* 440 specific */
1859void do_440_dlmzb (void)
1860{
1861 target_ulong mask;
1862 int i;
1863
1864 i = 1;
1865 for (mask = 0xFF000000; mask != 0; mask = mask >> 8) {
1866 if ((T0 & mask) == 0)
1867 goto done;
1868 i++;
1869 }
1870 for (mask = 0xFF000000; mask != 0; mask = mask >> 8) {
1871 if ((T1 & mask) == 0)
1872 break;
1873 i++;
1874 }
1875 done:
1876 T0 = i;
fdabc366
FB
1877}
1878
1c97856d 1879/*****************************************************************************/
0487d6a8
JM
1880/* SPE extension helpers */
1881/* Use a table to make this quicker */
1882static uint8_t hbrev[16] = {
1883 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
1884 0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF,
1885};
1886
b068d6a7 1887static always_inline uint8_t byte_reverse (uint8_t val)
0487d6a8
JM
1888{
1889 return hbrev[val >> 4] | (hbrev[val & 0xF] << 4);
1890}
1891
b068d6a7 1892static always_inline uint32_t word_reverse (uint32_t val)
0487d6a8
JM
1893{
1894 return byte_reverse(val >> 24) | (byte_reverse(val >> 16) << 8) |
1895 (byte_reverse(val >> 8) << 16) | (byte_reverse(val) << 24);
1896}
1897
3cd7d1dd 1898#define MASKBITS 16 // Random value - to be fixed (implementation dependant)
57951c27 1899target_ulong helper_brinc (target_ulong arg1, target_ulong arg2)
0487d6a8
JM
1900{
1901 uint32_t a, b, d, mask;
1902
3cd7d1dd 1903 mask = UINT32_MAX >> (32 - MASKBITS);
57951c27
AJ
1904 a = arg1 & mask;
1905 b = arg2 & mask;
3cd7d1dd 1906 d = word_reverse(1 + word_reverse(a | ~b));
57951c27 1907 return (arg1 & ~mask) | (d & b);
0487d6a8
JM
1908}
1909
57951c27 1910uint32_t helper_cntlsw32 (uint32_t val)
0487d6a8
JM
1911{
1912 if (val & 0x80000000)
603fccce 1913 return clz32(~val);
0487d6a8 1914 else
603fccce 1915 return clz32(val);
0487d6a8
JM
1916}
1917
57951c27 1918uint32_t helper_cntlzw32 (uint32_t val)
0487d6a8 1919{
603fccce 1920 return clz32(val);
0487d6a8
JM
1921}
1922
1c97856d
AJ
1923/* Single-precision floating-point conversions */
1924static always_inline uint32_t efscfsi (uint32_t val)
0487d6a8 1925{
0ca9d380 1926 CPU_FloatU u;
0487d6a8
JM
1927
1928 u.f = int32_to_float32(val, &env->spe_status);
1929
0ca9d380 1930 return u.l;
0487d6a8
JM
1931}
1932
1c97856d 1933static always_inline uint32_t efscfui (uint32_t val)
0487d6a8 1934{
0ca9d380 1935 CPU_FloatU u;
0487d6a8
JM
1936
1937 u.f = uint32_to_float32(val, &env->spe_status);
1938
0ca9d380 1939 return u.l;
0487d6a8
JM
1940}
1941
1c97856d 1942static always_inline int32_t efsctsi (uint32_t val)
0487d6a8 1943{
0ca9d380 1944 CPU_FloatU u;
0487d6a8 1945
0ca9d380 1946 u.l = val;
0487d6a8
JM
1947 /* NaN are not treated the same way IEEE 754 does */
1948 if (unlikely(isnan(u.f)))
1949 return 0;
1950
1951 return float32_to_int32(u.f, &env->spe_status);
1952}
1953
1c97856d 1954static always_inline uint32_t efsctui (uint32_t val)
0487d6a8 1955{
0ca9d380 1956 CPU_FloatU u;
0487d6a8 1957
0ca9d380 1958 u.l = val;
0487d6a8
JM
1959 /* NaN are not treated the same way IEEE 754 does */
1960 if (unlikely(isnan(u.f)))
1961 return 0;
1962
1963 return float32_to_uint32(u.f, &env->spe_status);
1964}
1965
1c97856d 1966static always_inline uint32_t efsctsiz (uint32_t val)
0487d6a8 1967{
0ca9d380 1968 CPU_FloatU u;
0487d6a8 1969
0ca9d380 1970 u.l = val;
0487d6a8
JM
1971 /* NaN are not treated the same way IEEE 754 does */
1972 if (unlikely(isnan(u.f)))
1973 return 0;
1974
1975 return float32_to_int32_round_to_zero(u.f, &env->spe_status);
1976}
1977
1c97856d 1978static always_inline uint32_t efsctuiz (uint32_t val)
0487d6a8 1979{
0ca9d380 1980 CPU_FloatU u;
0487d6a8 1981
0ca9d380 1982 u.l = val;
0487d6a8
JM
1983 /* NaN are not treated the same way IEEE 754 does */
1984 if (unlikely(isnan(u.f)))
1985 return 0;
1986
1987 return float32_to_uint32_round_to_zero(u.f, &env->spe_status);
1988}
1989
1c97856d 1990static always_inline uint32_t efscfsf (uint32_t val)
0487d6a8 1991{
0ca9d380 1992 CPU_FloatU u;
0487d6a8
JM
1993 float32 tmp;
1994
1995 u.f = int32_to_float32(val, &env->spe_status);
1996 tmp = int64_to_float32(1ULL << 32, &env->spe_status);
1997 u.f = float32_div(u.f, tmp, &env->spe_status);
1998
0ca9d380 1999 return u.l;
0487d6a8
JM
2000}
2001
1c97856d 2002static always_inline uint32_t efscfuf (uint32_t val)
0487d6a8 2003{
0ca9d380 2004 CPU_FloatU u;
0487d6a8
JM
2005 float32 tmp;
2006
2007 u.f = uint32_to_float32(val, &env->spe_status);
2008 tmp = uint64_to_float32(1ULL << 32, &env->spe_status);
2009 u.f = float32_div(u.f, tmp, &env->spe_status);
2010
0ca9d380 2011 return u.l;
0487d6a8
JM
2012}
2013
1c97856d 2014static always_inline uint32_t efsctsf (uint32_t val)
0487d6a8 2015{
0ca9d380 2016 CPU_FloatU u;
0487d6a8
JM
2017 float32 tmp;
2018
0ca9d380 2019 u.l = val;
0487d6a8
JM
2020 /* NaN are not treated the same way IEEE 754 does */
2021 if (unlikely(isnan(u.f)))
2022 return 0;
2023 tmp = uint64_to_float32(1ULL << 32, &env->spe_status);
2024 u.f = float32_mul(u.f, tmp, &env->spe_status);
2025
2026 return float32_to_int32(u.f, &env->spe_status);
2027}
2028
1c97856d 2029static always_inline uint32_t efsctuf (uint32_t val)
0487d6a8 2030{
0ca9d380 2031 CPU_FloatU u;
0487d6a8
JM
2032 float32 tmp;
2033
0ca9d380 2034 u.l = val;
0487d6a8
JM
2035 /* NaN are not treated the same way IEEE 754 does */
2036 if (unlikely(isnan(u.f)))
2037 return 0;
2038 tmp = uint64_to_float32(1ULL << 32, &env->spe_status);
2039 u.f = float32_mul(u.f, tmp, &env->spe_status);
2040
2041 return float32_to_uint32(u.f, &env->spe_status);
2042}
2043
1c97856d
AJ
2044#define HELPER_SPE_SINGLE_CONV(name) \
2045uint32_t helper_e##name (uint32_t val) \
2046{ \
2047 return e##name(val); \
2048}
2049/* efscfsi */
2050HELPER_SPE_SINGLE_CONV(fscfsi);
2051/* efscfui */
2052HELPER_SPE_SINGLE_CONV(fscfui);
2053/* efscfuf */
2054HELPER_SPE_SINGLE_CONV(fscfuf);
2055/* efscfsf */
2056HELPER_SPE_SINGLE_CONV(fscfsf);
2057/* efsctsi */
2058HELPER_SPE_SINGLE_CONV(fsctsi);
2059/* efsctui */
2060HELPER_SPE_SINGLE_CONV(fsctui);
2061/* efsctsiz */
2062HELPER_SPE_SINGLE_CONV(fsctsiz);
2063/* efsctuiz */
2064HELPER_SPE_SINGLE_CONV(fsctuiz);
2065/* efsctsf */
2066HELPER_SPE_SINGLE_CONV(fsctsf);
2067/* efsctuf */
2068HELPER_SPE_SINGLE_CONV(fsctuf);
2069
2070#define HELPER_SPE_VECTOR_CONV(name) \
2071uint64_t helper_ev##name (uint64_t val) \
2072{ \
2073 return ((uint64_t)e##name(val >> 32) << 32) | \
2074 (uint64_t)e##name(val); \
0487d6a8 2075}
1c97856d
AJ
2076/* evfscfsi */
2077HELPER_SPE_VECTOR_CONV(fscfsi);
2078/* evfscfui */
2079HELPER_SPE_VECTOR_CONV(fscfui);
2080/* evfscfuf */
2081HELPER_SPE_VECTOR_CONV(fscfuf);
2082/* evfscfsf */
2083HELPER_SPE_VECTOR_CONV(fscfsf);
2084/* evfsctsi */
2085HELPER_SPE_VECTOR_CONV(fsctsi);
2086/* evfsctui */
2087HELPER_SPE_VECTOR_CONV(fsctui);
2088/* evfsctsiz */
2089HELPER_SPE_VECTOR_CONV(fsctsiz);
2090/* evfsctuiz */
2091HELPER_SPE_VECTOR_CONV(fsctuiz);
2092/* evfsctsf */
2093HELPER_SPE_VECTOR_CONV(fsctsf);
2094/* evfsctuf */
2095HELPER_SPE_VECTOR_CONV(fsctuf);
0487d6a8 2096
1c97856d
AJ
2097/* Single-precision floating-point arithmetic */
2098static always_inline uint32_t efsadd (uint32_t op1, uint32_t op2)
0487d6a8 2099{
1c97856d
AJ
2100 CPU_FloatU u1, u2;
2101 u1.l = op1;
2102 u2.l = op2;
2103 u1.f = float32_add(u1.f, u2.f, &env->spe_status);
2104 return u1.l;
0487d6a8
JM
2105}
2106
1c97856d 2107static always_inline uint32_t efssub (uint32_t op1, uint32_t op2)
0487d6a8 2108{
1c97856d
AJ
2109 CPU_FloatU u1, u2;
2110 u1.l = op1;
2111 u2.l = op2;
2112 u1.f = float32_sub(u1.f, u2.f, &env->spe_status);
2113 return u1.l;
0487d6a8
JM
2114}
2115
1c97856d 2116static always_inline uint32_t efsmul (uint32_t op1, uint32_t op2)
0487d6a8 2117{
1c97856d
AJ
2118 CPU_FloatU u1, u2;
2119 u1.l = op1;
2120 u2.l = op2;
2121 u1.f = float32_mul(u1.f, u2.f, &env->spe_status);
2122 return u1.l;
0487d6a8
JM
2123}
2124
1c97856d 2125static always_inline uint32_t efsdiv (uint32_t op1, uint32_t op2)
0487d6a8 2126{
1c97856d
AJ
2127 CPU_FloatU u1, u2;
2128 u1.l = op1;
2129 u2.l = op2;
2130 u1.f = float32_div(u1.f, u2.f, &env->spe_status);
2131 return u1.l;
0487d6a8
JM
2132}
2133
1c97856d
AJ
2134#define HELPER_SPE_SINGLE_ARITH(name) \
2135uint32_t helper_e##name (uint32_t op1, uint32_t op2) \
2136{ \
2137 return e##name(op1, op2); \
2138}
2139/* efsadd */
2140HELPER_SPE_SINGLE_ARITH(fsadd);
2141/* efssub */
2142HELPER_SPE_SINGLE_ARITH(fssub);
2143/* efsmul */
2144HELPER_SPE_SINGLE_ARITH(fsmul);
2145/* efsdiv */
2146HELPER_SPE_SINGLE_ARITH(fsdiv);
2147
2148#define HELPER_SPE_VECTOR_ARITH(name) \
2149uint64_t helper_ev##name (uint64_t op1, uint64_t op2) \
2150{ \
2151 return ((uint64_t)e##name(op1 >> 32, op2 >> 32) << 32) | \
2152 (uint64_t)e##name(op1, op2); \
2153}
2154/* evfsadd */
2155HELPER_SPE_VECTOR_ARITH(fsadd);
2156/* evfssub */
2157HELPER_SPE_VECTOR_ARITH(fssub);
2158/* evfsmul */
2159HELPER_SPE_VECTOR_ARITH(fsmul);
2160/* evfsdiv */
2161HELPER_SPE_VECTOR_ARITH(fsdiv);
2162
2163/* Single-precision floating-point comparisons */
2164static always_inline uint32_t efststlt (uint32_t op1, uint32_t op2)
0487d6a8 2165{
1c97856d
AJ
2166 CPU_FloatU u1, u2;
2167 u1.l = op1;
2168 u2.l = op2;
2169 return float32_lt(u1.f, u2.f, &env->spe_status) ? 4 : 0;
0487d6a8
JM
2170}
2171
1c97856d 2172static always_inline uint32_t efststgt (uint32_t op1, uint32_t op2)
0487d6a8 2173{
1c97856d
AJ
2174 CPU_FloatU u1, u2;
2175 u1.l = op1;
2176 u2.l = op2;
2177 return float32_le(u1.f, u2.f, &env->spe_status) ? 0 : 4;
0487d6a8
JM
2178}
2179
1c97856d 2180static always_inline uint32_t efststeq (uint32_t op1, uint32_t op2)
0487d6a8 2181{
1c97856d
AJ
2182 CPU_FloatU u1, u2;
2183 u1.l = op1;
2184 u2.l = op2;
2185 return float32_eq(u1.f, u2.f, &env->spe_status) ? 4 : 0;
0487d6a8
JM
2186}
2187
1c97856d 2188static always_inline uint32_t efscmplt (uint32_t op1, uint32_t op2)
0487d6a8
JM
2189{
2190 /* XXX: TODO: test special values (NaN, infinites, ...) */
1c97856d 2191 return efststlt(op1, op2);
0487d6a8
JM
2192}
2193
1c97856d 2194static always_inline uint32_t efscmpgt (uint32_t op1, uint32_t op2)
0487d6a8
JM
2195{
2196 /* XXX: TODO: test special values (NaN, infinites, ...) */
1c97856d 2197 return efststgt(op1, op2);
0487d6a8
JM
2198}
2199
1c97856d 2200static always_inline uint32_t efscmpeq (uint32_t op1, uint32_t op2)
0487d6a8
JM
2201{
2202 /* XXX: TODO: test special values (NaN, infinites, ...) */
1c97856d 2203 return efststeq(op1, op2);
0487d6a8
JM
2204}
2205
1c97856d
AJ
2206#define HELPER_SINGLE_SPE_CMP(name) \
2207uint32_t helper_e##name (uint32_t op1, uint32_t op2) \
2208{ \
2209 return e##name(op1, op2) << 2; \
2210}
2211/* efststlt */
2212HELPER_SINGLE_SPE_CMP(fststlt);
2213/* efststgt */
2214HELPER_SINGLE_SPE_CMP(fststgt);
2215/* efststeq */
2216HELPER_SINGLE_SPE_CMP(fststeq);
2217/* efscmplt */
2218HELPER_SINGLE_SPE_CMP(fscmplt);
2219/* efscmpgt */
2220HELPER_SINGLE_SPE_CMP(fscmpgt);
2221/* efscmpeq */
2222HELPER_SINGLE_SPE_CMP(fscmpeq);
2223
2224static always_inline uint32_t evcmp_merge (int t0, int t1)
0487d6a8 2225{
1c97856d 2226 return (t0 << 3) | (t1 << 2) | ((t0 | t1) << 1) | (t0 & t1);
0487d6a8
JM
2227}
2228
1c97856d
AJ
2229#define HELPER_VECTOR_SPE_CMP(name) \
2230uint32_t helper_ev##name (uint64_t op1, uint64_t op2) \
2231{ \
2232 return evcmp_merge(e##name(op1 >> 32, op2 >> 32), e##name(op1, op2)); \
0487d6a8 2233}
1c97856d
AJ
2234/* evfststlt */
2235HELPER_VECTOR_SPE_CMP(fststlt);
2236/* evfststgt */
2237HELPER_VECTOR_SPE_CMP(fststgt);
2238/* evfststeq */
2239HELPER_VECTOR_SPE_CMP(fststeq);
2240/* evfscmplt */
2241HELPER_VECTOR_SPE_CMP(fscmplt);
2242/* evfscmpgt */
2243HELPER_VECTOR_SPE_CMP(fscmpgt);
2244/* evfscmpeq */
2245HELPER_VECTOR_SPE_CMP(fscmpeq);
0487d6a8 2246
1c97856d
AJ
2247/* Double-precision floating-point conversion */
2248uint64_t helper_efdcfsi (uint32_t val)
0487d6a8 2249{
1c97856d
AJ
2250 CPU_DoubleU u;
2251
2252 u.d = int32_to_float64(val, &env->spe_status);
2253
2254 return u.ll;
0487d6a8
JM
2255}
2256
1c97856d 2257uint64_t helper_efdcfsid (uint64_t val)
0487d6a8 2258{
0ca9d380 2259 CPU_DoubleU u;
0487d6a8 2260
0ca9d380 2261 u.d = int64_to_float64(val, &env->spe_status);
0487d6a8 2262
0ca9d380 2263 return u.ll;
0487d6a8
JM
2264}
2265
1c97856d
AJ
2266uint64_t helper_efdcfui (uint32_t val)
2267{
2268 CPU_DoubleU u;
2269
2270 u.d = uint32_to_float64(val, &env->spe_status);
2271
2272 return u.ll;
2273}
2274
2275uint64_t helper_efdcfuid (uint64_t val)
0487d6a8 2276{
0ca9d380 2277 CPU_DoubleU u;
0487d6a8 2278
0ca9d380 2279 u.d = uint64_to_float64(val, &env->spe_status);
0487d6a8 2280
0ca9d380 2281 return u.ll;
0487d6a8
JM
2282}
2283
1c97856d 2284uint32_t helper_efdctsi (uint64_t val)
0487d6a8 2285{
0ca9d380 2286 CPU_DoubleU u;
0487d6a8 2287
0ca9d380 2288 u.ll = val;
0487d6a8 2289 /* NaN are not treated the same way IEEE 754 does */
0ca9d380 2290 if (unlikely(isnan(u.d)))
0487d6a8
JM
2291 return 0;
2292
1c97856d 2293 return float64_to_int32(u.d, &env->spe_status);
0487d6a8
JM
2294}
2295
1c97856d 2296uint32_t helper_efdctui (uint64_t val)
0487d6a8 2297{
0ca9d380 2298 CPU_DoubleU u;
0487d6a8 2299
0ca9d380 2300 u.ll = val;
0487d6a8 2301 /* NaN are not treated the same way IEEE 754 does */
0ca9d380 2302 if (unlikely(isnan(u.d)))
0487d6a8
JM
2303 return 0;
2304
1c97856d 2305 return float64_to_uint32(u.d, &env->spe_status);
0487d6a8
JM
2306}
2307
1c97856d 2308uint32_t helper_efdctsiz (uint64_t val)
0487d6a8 2309{
0ca9d380 2310 CPU_DoubleU u;
0487d6a8 2311
0ca9d380 2312 u.ll = val;
0487d6a8 2313 /* NaN are not treated the same way IEEE 754 does */
0ca9d380 2314 if (unlikely(isnan(u.d)))
0487d6a8
JM
2315 return 0;
2316
1c97856d 2317 return float64_to_int32_round_to_zero(u.d, &env->spe_status);
0487d6a8
JM
2318}
2319
1c97856d 2320uint64_t helper_efdctsidz (uint64_t val)
0487d6a8 2321{
0ca9d380 2322 CPU_DoubleU u;
0487d6a8 2323
0ca9d380 2324 u.ll = val;
0487d6a8 2325 /* NaN are not treated the same way IEEE 754 does */
0ca9d380 2326 if (unlikely(isnan(u.d)))
0487d6a8
JM
2327 return 0;
2328
1c97856d 2329 return float64_to_int64_round_to_zero(u.d, &env->spe_status);
0487d6a8
JM
2330}
2331
1c97856d 2332uint32_t helper_efdctuiz (uint64_t val)
0487d6a8 2333{
1c97856d 2334 CPU_DoubleU u;
0487d6a8 2335
1c97856d
AJ
2336 u.ll = val;
2337 /* NaN are not treated the same way IEEE 754 does */
2338 if (unlikely(isnan(u.d)))
2339 return 0;
0487d6a8 2340
1c97856d 2341 return float64_to_uint32_round_to_zero(u.d, &env->spe_status);
0487d6a8
JM
2342}
2343
1c97856d 2344uint64_t helper_efdctuidz (uint64_t val)
0487d6a8 2345{
1c97856d 2346 CPU_DoubleU u;
0487d6a8 2347
1c97856d
AJ
2348 u.ll = val;
2349 /* NaN are not treated the same way IEEE 754 does */
2350 if (unlikely(isnan(u.d)))
2351 return 0;
0487d6a8 2352
1c97856d 2353 return float64_to_uint64_round_to_zero(u.d, &env->spe_status);
0487d6a8
JM
2354}
2355
1c97856d 2356uint64_t helper_efdcfsf (uint32_t val)
0487d6a8 2357{
0ca9d380 2358 CPU_DoubleU u;
0487d6a8
JM
2359 float64 tmp;
2360
0ca9d380 2361 u.d = int32_to_float64(val, &env->spe_status);
0487d6a8 2362 tmp = int64_to_float64(1ULL << 32, &env->spe_status);
0ca9d380 2363 u.d = float64_div(u.d, tmp, &env->spe_status);
0487d6a8 2364
0ca9d380 2365 return u.ll;
0487d6a8
JM
2366}
2367
1c97856d 2368uint64_t helper_efdcfuf (uint32_t val)
0487d6a8 2369{
0ca9d380 2370 CPU_DoubleU u;
0487d6a8
JM
2371 float64 tmp;
2372
0ca9d380 2373 u.d = uint32_to_float64(val, &env->spe_status);
0487d6a8 2374 tmp = int64_to_float64(1ULL << 32, &env->spe_status);
0ca9d380 2375 u.d = float64_div(u.d, tmp, &env->spe_status);
0487d6a8 2376
0ca9d380 2377 return u.ll;
0487d6a8
JM
2378}
2379
1c97856d 2380uint32_t helper_efdctsf (uint64_t val)
0487d6a8 2381{
0ca9d380 2382 CPU_DoubleU u;
0487d6a8
JM
2383 float64 tmp;
2384
0ca9d380 2385 u.ll = val;
0487d6a8 2386 /* NaN are not treated the same way IEEE 754 does */
0ca9d380 2387 if (unlikely(isnan(u.d)))
0487d6a8
JM
2388 return 0;
2389 tmp = uint64_to_float64(1ULL << 32, &env->spe_status);
0ca9d380 2390 u.d = float64_mul(u.d, tmp, &env->spe_status);
0487d6a8 2391
0ca9d380 2392 return float64_to_int32(u.d, &env->spe_status);
0487d6a8
JM
2393}
2394
1c97856d 2395uint32_t helper_efdctuf (uint64_t val)
0487d6a8 2396{
0ca9d380 2397 CPU_DoubleU u;
0487d6a8
JM
2398 float64 tmp;
2399
0ca9d380 2400 u.ll = val;
0487d6a8 2401 /* NaN are not treated the same way IEEE 754 does */
0ca9d380 2402 if (unlikely(isnan(u.d)))
0487d6a8
JM
2403 return 0;
2404 tmp = uint64_to_float64(1ULL << 32, &env->spe_status);
0ca9d380 2405 u.d = float64_mul(u.d, tmp, &env->spe_status);
0487d6a8 2406
0ca9d380 2407 return float64_to_uint32(u.d, &env->spe_status);
0487d6a8
JM
2408}
2409
1c97856d 2410uint32_t helper_efscfd (uint64_t val)
0487d6a8 2411{
0ca9d380
AJ
2412 CPU_DoubleU u1;
2413 CPU_FloatU u2;
0487d6a8 2414
0ca9d380
AJ
2415 u1.ll = val;
2416 u2.f = float64_to_float32(u1.d, &env->spe_status);
0487d6a8 2417
0ca9d380 2418 return u2.l;
0487d6a8
JM
2419}
2420
1c97856d 2421uint64_t helper_efdcfs (uint32_t val)
0487d6a8 2422{
0ca9d380
AJ
2423 CPU_DoubleU u2;
2424 CPU_FloatU u1;
0487d6a8 2425
0ca9d380
AJ
2426 u1.l = val;
2427 u2.d = float32_to_float64(u1.f, &env->spe_status);
0487d6a8 2428
0ca9d380 2429 return u2.ll;
0487d6a8
JM
2430}
2431
1c97856d
AJ
2432/* Double precision fixed-point arithmetic */
2433uint64_t helper_efdadd (uint64_t op1, uint64_t op2)
0487d6a8 2434{
1c97856d
AJ
2435 CPU_DoubleU u1, u2;
2436 u1.ll = op1;
2437 u2.ll = op2;
2438 u1.d = float64_add(u1.d, u2.d, &env->spe_status);
2439 return u1.ll;
0487d6a8
JM
2440}
2441
1c97856d 2442uint64_t helper_efdsub (uint64_t op1, uint64_t op2)
0487d6a8 2443{
1c97856d
AJ
2444 CPU_DoubleU u1, u2;
2445 u1.ll = op1;
2446 u2.ll = op2;
2447 u1.d = float64_sub(u1.d, u2.d, &env->spe_status);
2448 return u1.ll;
0487d6a8
JM
2449}
2450
1c97856d 2451uint64_t helper_efdmul (uint64_t op1, uint64_t op2)
0487d6a8 2452{
1c97856d
AJ
2453 CPU_DoubleU u1, u2;
2454 u1.ll = op1;
2455 u2.ll = op2;
2456 u1.d = float64_mul(u1.d, u2.d, &env->spe_status);
2457 return u1.ll;
0487d6a8
JM
2458}
2459
1c97856d 2460uint64_t helper_efddiv (uint64_t op1, uint64_t op2)
0487d6a8 2461{
1c97856d
AJ
2462 CPU_DoubleU u1, u2;
2463 u1.ll = op1;
2464 u2.ll = op2;
2465 u1.d = float64_div(u1.d, u2.d, &env->spe_status);
2466 return u1.ll;
0487d6a8
JM
2467}
2468
1c97856d
AJ
2469/* Double precision floating point helpers */
2470uint32_t helper_efdtstlt (uint64_t op1, uint64_t op2)
0487d6a8 2471{
1c97856d
AJ
2472 CPU_DoubleU u1, u2;
2473 u1.ll = op1;
2474 u2.ll = op2;
2475 return float64_lt(u1.d, u2.d, &env->spe_status) ? 4 : 0;
0487d6a8
JM
2476}
2477
1c97856d 2478uint32_t helper_efdtstgt (uint64_t op1, uint64_t op2)
0487d6a8 2479{
1c97856d
AJ
2480 CPU_DoubleU u1, u2;
2481 u1.ll = op1;
2482 u2.ll = op2;
2483 return float64_le(u1.d, u2.d, &env->spe_status) ? 0 : 4;
0487d6a8
JM
2484}
2485
1c97856d 2486uint32_t helper_efdtsteq (uint64_t op1, uint64_t op2)
0487d6a8 2487{
1c97856d
AJ
2488 CPU_DoubleU u1, u2;
2489 u1.ll = op1;
2490 u2.ll = op2;
2491 return float64_eq(u1.d, u2.d, &env->spe_status) ? 4 : 0;
0487d6a8
JM
2492}
2493
1c97856d 2494uint32_t helper_efdcmplt (uint64_t op1, uint64_t op2)
0487d6a8 2495{
1c97856d
AJ
2496 /* XXX: TODO: test special values (NaN, infinites, ...) */
2497 return helper_efdtstlt(op1, op2);
0487d6a8
JM
2498}
2499
1c97856d
AJ
2500uint32_t helper_efdcmpgt (uint64_t op1, uint64_t op2)
2501{
2502 /* XXX: TODO: test special values (NaN, infinites, ...) */
2503 return helper_efdtstgt(op1, op2);
2504}
0487d6a8 2505
1c97856d
AJ
2506uint32_t helper_efdcmpeq (uint64_t op1, uint64_t op2)
2507{
2508 /* XXX: TODO: test special values (NaN, infinites, ...) */
2509 return helper_efdtsteq(op1, op2);
2510}
0487d6a8 2511
fdabc366
FB
2512/*****************************************************************************/
2513/* Softmmu support */
2514#if !defined (CONFIG_USER_ONLY)
2515
2516#define MMUSUFFIX _mmu
fdabc366
FB
2517
2518#define SHIFT 0
2519#include "softmmu_template.h"
2520
2521#define SHIFT 1
2522#include "softmmu_template.h"
2523
2524#define SHIFT 2
2525#include "softmmu_template.h"
2526
2527#define SHIFT 3
2528#include "softmmu_template.h"
2529
2530/* try to fill the TLB and return an exception if error. If retaddr is
2531 NULL, it means that the function was called in C code (i.e. not
2532 from generated code or from helper.c) */
2533/* XXX: fix it to restore all registers */
6ebbf390 2534void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
fdabc366
FB
2535{
2536 TranslationBlock *tb;
2537 CPUState *saved_env;
44f8625d 2538 unsigned long pc;
fdabc366
FB
2539 int ret;
2540
2541 /* XXX: hack to restore env in all cases, even if not called from
2542 generated code */
2543 saved_env = env;
2544 env = cpu_single_env;
6ebbf390 2545 ret = cpu_ppc_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
76a66253 2546 if (unlikely(ret != 0)) {
fdabc366
FB
2547 if (likely(retaddr)) {
2548 /* now we have a real cpu fault */
44f8625d 2549 pc = (unsigned long)retaddr;
fdabc366
FB
2550 tb = tb_find_pc(pc);
2551 if (likely(tb)) {
2552 /* the PC is inside the translated code. It means that we have
2553 a virtual CPU fault */
2554 cpu_restore_state(tb, env, pc, NULL);
76a66253 2555 }
fdabc366 2556 }
64adab3f 2557 raise_exception_err(env, env->exception_index, env->error_code);
fdabc366
FB
2558 }
2559 env = saved_env;
9a64fbe4
FB
2560}
2561
76a66253
JM
2562/* Software driven TLBs management */
2563/* PowerPC 602/603 software TLB load instructions helpers */
0f3955e2 2564static void helper_load_6xx_tlb (target_ulong new_EPN, int is_code)
76a66253
JM
2565{
2566 target_ulong RPN, CMP, EPN;
2567 int way;
d9bce9d9 2568
76a66253
JM
2569 RPN = env->spr[SPR_RPA];
2570 if (is_code) {
2571 CMP = env->spr[SPR_ICMP];
2572 EPN = env->spr[SPR_IMISS];
2573 } else {
2574 CMP = env->spr[SPR_DCMP];
2575 EPN = env->spr[SPR_DMISS];
2576 }
2577 way = (env->spr[SPR_SRR1] >> 17) & 1;
2578#if defined (DEBUG_SOFTWARE_TLB)
2579 if (loglevel != 0) {
6b542af7
JM
2580 fprintf(logfile, "%s: EPN " TDX " " ADDRX " PTE0 " ADDRX
2581 " PTE1 " ADDRX " way %d\n",
2582 __func__, T0, EPN, CMP, RPN, way);
76a66253
JM
2583 }
2584#endif
2585 /* Store this TLB */
0f3955e2 2586 ppc6xx_tlb_store(env, (uint32_t)(new_EPN & TARGET_PAGE_MASK),
d9bce9d9 2587 way, is_code, CMP, RPN);
76a66253
JM
2588}
2589
0f3955e2
AJ
2590void helper_load_6xx_tlbd (target_ulong EPN)
2591{
2592 helper_load_6xx_tlb(EPN, 0);
2593}
2594
2595void helper_load_6xx_tlbi (target_ulong EPN)
2596{
2597 helper_load_6xx_tlb(EPN, 1);
2598}
2599
2600/* PowerPC 74xx software TLB load instructions helpers */
2601static void helper_load_74xx_tlb (target_ulong new_EPN, int is_code)
7dbe11ac
JM
2602{
2603 target_ulong RPN, CMP, EPN;
2604 int way;
2605
2606 RPN = env->spr[SPR_PTELO];
2607 CMP = env->spr[SPR_PTEHI];
2608 EPN = env->spr[SPR_TLBMISS] & ~0x3;
2609 way = env->spr[SPR_TLBMISS] & 0x3;
2610#if defined (DEBUG_SOFTWARE_TLB)
2611 if (loglevel != 0) {
6b542af7
JM
2612 fprintf(logfile, "%s: EPN " TDX " " ADDRX " PTE0 " ADDRX
2613 " PTE1 " ADDRX " way %d\n",
2614 __func__, T0, EPN, CMP, RPN, way);
7dbe11ac
JM
2615 }
2616#endif
2617 /* Store this TLB */
0f3955e2 2618 ppc6xx_tlb_store(env, (uint32_t)(new_EPN & TARGET_PAGE_MASK),
7dbe11ac
JM
2619 way, is_code, CMP, RPN);
2620}
2621
0f3955e2
AJ
2622void helper_load_74xx_tlbd (target_ulong EPN)
2623{
2624 helper_load_74xx_tlb(EPN, 0);
2625}
2626
2627void helper_load_74xx_tlbi (target_ulong EPN)
2628{
2629 helper_load_74xx_tlb(EPN, 1);
2630}
2631
a11b8151 2632static always_inline target_ulong booke_tlb_to_page_size (int size)
a8dea12f
JM
2633{
2634 return 1024 << (2 * size);
2635}
2636
a11b8151 2637static always_inline int booke_page_size_to_tlb (target_ulong page_size)
a8dea12f
JM
2638{
2639 int size;
2640
2641 switch (page_size) {
2642 case 0x00000400UL:
2643 size = 0x0;
2644 break;
2645 case 0x00001000UL:
2646 size = 0x1;
2647 break;
2648 case 0x00004000UL:
2649 size = 0x2;
2650 break;
2651 case 0x00010000UL:
2652 size = 0x3;
2653 break;
2654 case 0x00040000UL:
2655 size = 0x4;
2656 break;
2657 case 0x00100000UL:
2658 size = 0x5;
2659 break;
2660 case 0x00400000UL:
2661 size = 0x6;
2662 break;
2663 case 0x01000000UL:
2664 size = 0x7;
2665 break;
2666 case 0x04000000UL:
2667 size = 0x8;
2668 break;
2669 case 0x10000000UL:
2670 size = 0x9;
2671 break;
2672 case 0x40000000UL:
2673 size = 0xA;
2674 break;
2675#if defined (TARGET_PPC64)
2676 case 0x000100000000ULL:
2677 size = 0xB;
2678 break;
2679 case 0x000400000000ULL:
2680 size = 0xC;
2681 break;
2682 case 0x001000000000ULL:
2683 size = 0xD;
2684 break;
2685 case 0x004000000000ULL:
2686 size = 0xE;
2687 break;
2688 case 0x010000000000ULL:
2689 size = 0xF;
2690 break;
2691#endif
2692 default:
2693 size = -1;
2694 break;
2695 }
2696
2697 return size;
2698}
2699
76a66253 2700/* Helpers for 4xx TLB management */
76a66253
JM
2701void do_4xx_tlbre_lo (void)
2702{
a8dea12f
JM
2703 ppcemb_tlb_t *tlb;
2704 int size;
76a66253
JM
2705
2706 T0 &= 0x3F;
a8dea12f
JM
2707 tlb = &env->tlb[T0].tlbe;
2708 T0 = tlb->EPN;
2709 if (tlb->prot & PAGE_VALID)
2710 T0 |= 0x400;
2711 size = booke_page_size_to_tlb(tlb->size);
2712 if (size < 0 || size > 0x7)
2713 size = 1;
2714 T0 |= size << 7;
2715 env->spr[SPR_40x_PID] = tlb->PID;
76a66253
JM
2716}
2717
2718void do_4xx_tlbre_hi (void)
2719{
a8dea12f 2720 ppcemb_tlb_t *tlb;
76a66253
JM
2721
2722 T0 &= 0x3F;
a8dea12f
JM
2723 tlb = &env->tlb[T0].tlbe;
2724 T0 = tlb->RPN;
2725 if (tlb->prot & PAGE_EXEC)
2726 T0 |= 0x200;
2727 if (tlb->prot & PAGE_WRITE)
2728 T0 |= 0x100;
76a66253
JM
2729}
2730
c55e9aef 2731void do_4xx_tlbwe_hi (void)
76a66253 2732{
a8dea12f 2733 ppcemb_tlb_t *tlb;
76a66253
JM
2734 target_ulong page, end;
2735
c55e9aef 2736#if defined (DEBUG_SOFTWARE_TLB)
6b80055d 2737 if (loglevel != 0) {
6b542af7 2738 fprintf(logfile, "%s T0 " TDX " T1 " TDX "\n", __func__, T0, T1);
c55e9aef
JM
2739 }
2740#endif
76a66253 2741 T0 &= 0x3F;
a8dea12f 2742 tlb = &env->tlb[T0].tlbe;
76a66253
JM
2743 /* Invalidate previous TLB (if it's valid) */
2744 if (tlb->prot & PAGE_VALID) {
2745 end = tlb->EPN + tlb->size;
c55e9aef 2746#if defined (DEBUG_SOFTWARE_TLB)
6b80055d 2747 if (loglevel != 0) {
c55e9aef
JM
2748 fprintf(logfile, "%s: invalidate old TLB %d start " ADDRX
2749 " end " ADDRX "\n", __func__, (int)T0, tlb->EPN, end);
2750 }
2751#endif
76a66253
JM
2752 for (page = tlb->EPN; page < end; page += TARGET_PAGE_SIZE)
2753 tlb_flush_page(env, page);
2754 }
a8dea12f 2755 tlb->size = booke_tlb_to_page_size((T1 >> 7) & 0x7);
c294fc58
JM
2756 /* We cannot handle TLB size < TARGET_PAGE_SIZE.
2757 * If this ever occurs, one should use the ppcemb target instead
2758 * of the ppc or ppc64 one
2759 */
2760 if ((T1 & 0x40) && tlb->size < TARGET_PAGE_SIZE) {
71c8b8fd
JM
2761 cpu_abort(env, "TLB size " TARGET_FMT_lu " < %u "
2762 "are not supported (%d)\n",
c294fc58
JM
2763 tlb->size, TARGET_PAGE_SIZE, (int)((T1 >> 7) & 0x7));
2764 }
a750fc0b 2765 tlb->EPN = T1 & ~(tlb->size - 1);
c55e9aef 2766 if (T1 & 0x40)
76a66253
JM
2767 tlb->prot |= PAGE_VALID;
2768 else
2769 tlb->prot &= ~PAGE_VALID;
c294fc58
JM
2770 if (T1 & 0x20) {
2771 /* XXX: TO BE FIXED */
2772 cpu_abort(env, "Little-endian TLB entries are not supported by now\n");
2773 }
c55e9aef 2774 tlb->PID = env->spr[SPR_40x_PID]; /* PID */
a8dea12f 2775 tlb->attr = T1 & 0xFF;
c55e9aef 2776#if defined (DEBUG_SOFTWARE_TLB)
c294fc58
JM
2777 if (loglevel != 0) {
2778 fprintf(logfile, "%s: set up TLB %d RPN " PADDRX " EPN " ADDRX
c55e9aef 2779 " size " ADDRX " prot %c%c%c%c PID %d\n", __func__,
5fafdf24 2780 (int)T0, tlb->RPN, tlb->EPN, tlb->size,
c55e9aef
JM
2781 tlb->prot & PAGE_READ ? 'r' : '-',
2782 tlb->prot & PAGE_WRITE ? 'w' : '-',
2783 tlb->prot & PAGE_EXEC ? 'x' : '-',
2784 tlb->prot & PAGE_VALID ? 'v' : '-', (int)tlb->PID);
2785 }
2786#endif
76a66253
JM
2787 /* Invalidate new TLB (if valid) */
2788 if (tlb->prot & PAGE_VALID) {
2789 end = tlb->EPN + tlb->size;
c55e9aef 2790#if defined (DEBUG_SOFTWARE_TLB)
6b80055d 2791 if (loglevel != 0) {
c55e9aef
JM
2792 fprintf(logfile, "%s: invalidate TLB %d start " ADDRX
2793 " end " ADDRX "\n", __func__, (int)T0, tlb->EPN, end);
2794 }
2795#endif
76a66253
JM
2796 for (page = tlb->EPN; page < end; page += TARGET_PAGE_SIZE)
2797 tlb_flush_page(env, page);
2798 }
76a66253
JM
2799}
2800
c55e9aef 2801void do_4xx_tlbwe_lo (void)
76a66253 2802{
a8dea12f 2803 ppcemb_tlb_t *tlb;
76a66253 2804
c55e9aef 2805#if defined (DEBUG_SOFTWARE_TLB)
6b80055d 2806 if (loglevel != 0) {
6b542af7 2807 fprintf(logfile, "%s T0 " TDX " T1 " TDX "\n", __func__, T0, T1);
c55e9aef
JM
2808 }
2809#endif
76a66253 2810 T0 &= 0x3F;
a8dea12f 2811 tlb = &env->tlb[T0].tlbe;
76a66253
JM
2812 tlb->RPN = T1 & 0xFFFFFC00;
2813 tlb->prot = PAGE_READ;
2814 if (T1 & 0x200)
2815 tlb->prot |= PAGE_EXEC;
2816 if (T1 & 0x100)
2817 tlb->prot |= PAGE_WRITE;
c55e9aef 2818#if defined (DEBUG_SOFTWARE_TLB)
6b80055d
JM
2819 if (loglevel != 0) {
2820 fprintf(logfile, "%s: set up TLB %d RPN " PADDRX " EPN " ADDRX
c55e9aef 2821 " size " ADDRX " prot %c%c%c%c PID %d\n", __func__,
5fafdf24 2822 (int)T0, tlb->RPN, tlb->EPN, tlb->size,
c55e9aef
JM
2823 tlb->prot & PAGE_READ ? 'r' : '-',
2824 tlb->prot & PAGE_WRITE ? 'w' : '-',
2825 tlb->prot & PAGE_EXEC ? 'x' : '-',
2826 tlb->prot & PAGE_VALID ? 'v' : '-', (int)tlb->PID);
2827 }
2828#endif
76a66253 2829}
5eb7995e 2830
a4bb6c3e
JM
2831/* PowerPC 440 TLB management */
2832void do_440_tlbwe (int word)
5eb7995e
JM
2833{
2834 ppcemb_tlb_t *tlb;
a4bb6c3e 2835 target_ulong EPN, RPN, size;
5eb7995e
JM
2836 int do_flush_tlbs;
2837
2838#if defined (DEBUG_SOFTWARE_TLB)
2839 if (loglevel != 0) {
6b542af7 2840 fprintf(logfile, "%s word %d T0 " TDX " T1 " TDX "\n",
69facb78 2841 __func__, word, T0, T1);
5eb7995e
JM
2842 }
2843#endif
2844 do_flush_tlbs = 0;
2845 T0 &= 0x3F;
2846 tlb = &env->tlb[T0].tlbe;
a4bb6c3e
JM
2847 switch (word) {
2848 default:
2849 /* Just here to please gcc */
2850 case 0:
2851 EPN = T1 & 0xFFFFFC00;
2852 if ((tlb->prot & PAGE_VALID) && EPN != tlb->EPN)
5eb7995e 2853 do_flush_tlbs = 1;
a4bb6c3e
JM
2854 tlb->EPN = EPN;
2855 size = booke_tlb_to_page_size((T1 >> 4) & 0xF);
2856 if ((tlb->prot & PAGE_VALID) && tlb->size < size)
2857 do_flush_tlbs = 1;
2858 tlb->size = size;
2859 tlb->attr &= ~0x1;
2860 tlb->attr |= (T1 >> 8) & 1;
2861 if (T1 & 0x200) {
2862 tlb->prot |= PAGE_VALID;
2863 } else {
2864 if (tlb->prot & PAGE_VALID) {
2865 tlb->prot &= ~PAGE_VALID;
2866 do_flush_tlbs = 1;
2867 }
5eb7995e 2868 }
a4bb6c3e
JM
2869 tlb->PID = env->spr[SPR_440_MMUCR] & 0x000000FF;
2870 if (do_flush_tlbs)
2871 tlb_flush(env, 1);
2872 break;
2873 case 1:
2874 RPN = T1 & 0xFFFFFC0F;
2875 if ((tlb->prot & PAGE_VALID) && tlb->RPN != RPN)
2876 tlb_flush(env, 1);
2877 tlb->RPN = RPN;
2878 break;
2879 case 2:
2880 tlb->attr = (tlb->attr & 0x1) | (T1 & 0x0000FF00);
2881 tlb->prot = tlb->prot & PAGE_VALID;
2882 if (T1 & 0x1)
2883 tlb->prot |= PAGE_READ << 4;
2884 if (T1 & 0x2)
2885 tlb->prot |= PAGE_WRITE << 4;
2886 if (T1 & 0x4)
2887 tlb->prot |= PAGE_EXEC << 4;
2888 if (T1 & 0x8)
2889 tlb->prot |= PAGE_READ;
2890 if (T1 & 0x10)
2891 tlb->prot |= PAGE_WRITE;
2892 if (T1 & 0x20)
2893 tlb->prot |= PAGE_EXEC;
2894 break;
5eb7995e 2895 }
5eb7995e
JM
2896}
2897
a4bb6c3e 2898void do_440_tlbre (int word)
5eb7995e
JM
2899{
2900 ppcemb_tlb_t *tlb;
2901 int size;
2902
2903 T0 &= 0x3F;
2904 tlb = &env->tlb[T0].tlbe;
a4bb6c3e
JM
2905 switch (word) {
2906 default:
2907 /* Just here to please gcc */
2908 case 0:
2909 T0 = tlb->EPN;
2910 size = booke_page_size_to_tlb(tlb->size);
2911 if (size < 0 || size > 0xF)
2912 size = 1;
2913 T0 |= size << 4;
2914 if (tlb->attr & 0x1)
2915 T0 |= 0x100;
2916 if (tlb->prot & PAGE_VALID)
2917 T0 |= 0x200;
2918 env->spr[SPR_440_MMUCR] &= ~0x000000FF;
2919 env->spr[SPR_440_MMUCR] |= tlb->PID;
2920 break;
2921 case 1:
2922 T0 = tlb->RPN;
2923 break;
2924 case 2:
2925 T0 = tlb->attr & ~0x1;
2926 if (tlb->prot & (PAGE_READ << 4))
2927 T0 |= 0x1;
2928 if (tlb->prot & (PAGE_WRITE << 4))
2929 T0 |= 0x2;
2930 if (tlb->prot & (PAGE_EXEC << 4))
2931 T0 |= 0x4;
2932 if (tlb->prot & PAGE_READ)
2933 T0 |= 0x8;
2934 if (tlb->prot & PAGE_WRITE)
2935 T0 |= 0x10;
2936 if (tlb->prot & PAGE_EXEC)
2937 T0 |= 0x20;
2938 break;
2939 }
5eb7995e 2940}
76a66253 2941#endif /* !CONFIG_USER_ONLY */
This page took 0.645161 seconds and 4 git commands to generate.