cp1emu.c 63.5 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * cp1emu.c: a MIPS coprocessor 1 (FPU) instruction emulator
L
Linus Torvalds 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * MIPS floating point support
 * Copyright (C) 1994-2000 Algorithmics Ltd.
 *
 * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
 * Copyright (C) 2000  MIPS Technologies, Inc.
 *
 *  This program is free software; you can distribute it and/or modify it
 *  under the terms of the GNU General Public License (Version 2) as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope it will be useful, but WITHOUT
 *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 *  for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
21
 *  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
L
Linus Torvalds 已提交
22 23 24 25 26 27
 *
 * A complete emulator for MIPS coprocessor 1 instructions.  This is
 * required for #float(switch) or #float(trap), where it catches all
 * COP1 instructions via the "CoProcessor Unusable" exception.
 *
 * More surprisingly it is also required for #float(ieee), to help out
28
 * the hardware FPU at the boundaries of the IEEE-754 representation
L
Linus Torvalds 已提交
29 30 31 32
 * (denormalised values, infinities, underflow, etc).  It is made
 * quite nasty because emulation of some non-COP1 instructions is
 * required, e.g. in branch delay slots.
 *
33
 * Note if you know that you won't have an FPU, then you'll get much
L
Linus Torvalds 已提交
34 35 36
 * better performance by compiling with -msoft-float!
 */
#include <linux/sched.h>
37
#include <linux/debugfs.h>
38
#include <linux/percpu-defs.h>
39
#include <linux/perf_event.h>
L
Linus Torvalds 已提交
40

41
#include <asm/branch.h>
L
Linus Torvalds 已提交
42 43 44
#include <asm/inst.h>
#include <asm/ptrace.h>
#include <asm/signal.h>
45
#include <linux/uaccess.h>
46

47
#include <asm/cpu-info.h>
48
#include <asm/processor.h>
L
Linus Torvalds 已提交
49
#include <asm/fpu_emulator.h>
50
#include <asm/fpu.h>
51
#include <asm/mips-r2-to-r6-emul.h>
L
Linus Torvalds 已提交
52 53 54 55 56

#include "ieee754.h"

/* Function which emulates a floating point instruction. */

57
static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *,
L
Linus Torvalds 已提交
58 59 60
	mips_instruction);

static int fpux_emu(struct pt_regs *,
61
	struct mips_fpu_struct *, mips_instruction, void __user **);
L
Linus Torvalds 已提交
62 63 64 65

/* Control registers */

#define FPCREG_RID	0	/* $0  = revision id */
66 67 68
#define FPCREG_FCCR	25	/* $25 = fccr */
#define FPCREG_FEXR	26	/* $26 = fexr */
#define FPCREG_FENR	28	/* $28 = fenr */
L
Linus Torvalds 已提交
69 70 71
#define FPCREG_CSR	31	/* $31 = csr */

/* convert condition code register number to csr bit */
72
const unsigned int fpucondbit[8] = {
73
	FPU_CSR_COND,
L
Linus Torvalds 已提交
74 75 76 77 78 79 80 81 82
	FPU_CSR_COND1,
	FPU_CSR_COND2,
	FPU_CSR_COND3,
	FPU_CSR_COND4,
	FPU_CSR_COND5,
	FPU_CSR_COND6,
	FPU_CSR_COND7
};

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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
/* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */
static const int sd_format[] = {16, 17, 0, 0, 0, 0, 0, 0};
static const int sdps_format[] = {16, 17, 22, 0, 0, 0, 0, 0};
static const int dwl_format[] = {17, 20, 21, 0, 0, 0, 0, 0};
static const int swl_format[] = {16, 20, 21, 0, 0, 0, 0, 0};

/*
 * This functions translates a 32-bit microMIPS instruction
 * into a 32-bit MIPS32 instruction. Returns 0 on success
 * and SIGILL otherwise.
 */
static int microMIPS32_to_MIPS32(union mips_instruction *insn_ptr)
{
	union mips_instruction insn = *insn_ptr;
	union mips_instruction mips32_insn = insn;
	int func, fmt, op;

	switch (insn.mm_i_format.opcode) {
	case mm_ldc132_op:
		mips32_insn.mm_i_format.opcode = ldc1_op;
		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
		break;
	case mm_lwc132_op:
		mips32_insn.mm_i_format.opcode = lwc1_op;
		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
		break;
	case mm_sdc132_op:
		mips32_insn.mm_i_format.opcode = sdc1_op;
		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
		break;
	case mm_swc132_op:
		mips32_insn.mm_i_format.opcode = swc1_op;
		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
		break;
	case mm_pool32i_op:
		/* NOTE: offset is << by 1 if in microMIPS mode. */
		if ((insn.mm_i_format.rt == mm_bc1f_op) ||
		    (insn.mm_i_format.rt == mm_bc1t_op)) {
			mips32_insn.fb_format.opcode = cop1_op;
			mips32_insn.fb_format.bc = bc_op;
			mips32_insn.fb_format.flag =
				(insn.mm_i_format.rt == mm_bc1t_op) ? 1 : 0;
		} else
			return SIGILL;
		break;
	case mm_pool32f_op:
		switch (insn.mm_fp0_format.func) {
		case mm_32f_01_op:
		case mm_32f_11_op:
		case mm_32f_02_op:
		case mm_32f_12_op:
		case mm_32f_41_op:
		case mm_32f_51_op:
		case mm_32f_42_op:
		case mm_32f_52_op:
			op = insn.mm_fp0_format.func;
			if (op == mm_32f_01_op)
				func = madd_s_op;
			else if (op == mm_32f_11_op)
				func = madd_d_op;
			else if (op == mm_32f_02_op)
				func = nmadd_s_op;
			else if (op == mm_32f_12_op)
				func = nmadd_d_op;
			else if (op == mm_32f_41_op)
				func = msub_s_op;
			else if (op == mm_32f_51_op)
				func = msub_d_op;
			else if (op == mm_32f_42_op)
				func = nmsub_s_op;
			else
				func = nmsub_d_op;
			mips32_insn.fp6_format.opcode = cop1x_op;
			mips32_insn.fp6_format.fr = insn.mm_fp6_format.fr;
			mips32_insn.fp6_format.ft = insn.mm_fp6_format.ft;
			mips32_insn.fp6_format.fs = insn.mm_fp6_format.fs;
			mips32_insn.fp6_format.fd = insn.mm_fp6_format.fd;
			mips32_insn.fp6_format.func = func;
			break;
		case mm_32f_10_op:
			func = -1;	/* Invalid */
			op = insn.mm_fp5_format.op & 0x7;
			if (op == mm_ldxc1_op)
				func = ldxc1_op;
			else if (op == mm_sdxc1_op)
				func = sdxc1_op;
			else if (op == mm_lwxc1_op)
				func = lwxc1_op;
			else if (op == mm_swxc1_op)
				func = swxc1_op;

			if (func != -1) {
				mips32_insn.r_format.opcode = cop1x_op;
				mips32_insn.r_format.rs =
					insn.mm_fp5_format.base;
				mips32_insn.r_format.rt =
					insn.mm_fp5_format.index;
				mips32_insn.r_format.rd = 0;
				mips32_insn.r_format.re = insn.mm_fp5_format.fd;
				mips32_insn.r_format.func = func;
			} else
				return SIGILL;
			break;
		case mm_32f_40_op:
			op = -1;	/* Invalid */
			if (insn.mm_fp2_format.op == mm_fmovt_op)
				op = 1;
			else if (insn.mm_fp2_format.op == mm_fmovf_op)
				op = 0;
			if (op != -1) {
				mips32_insn.fp0_format.opcode = cop1_op;
				mips32_insn.fp0_format.fmt =
					sdps_format[insn.mm_fp2_format.fmt];
				mips32_insn.fp0_format.ft =
					(insn.mm_fp2_format.cc<<2) + op;
				mips32_insn.fp0_format.fs =
					insn.mm_fp2_format.fs;
				mips32_insn.fp0_format.fd =
					insn.mm_fp2_format.fd;
				mips32_insn.fp0_format.func = fmovc_op;
			} else
				return SIGILL;
			break;
		case mm_32f_60_op:
			func = -1;	/* Invalid */
			if (insn.mm_fp0_format.op == mm_fadd_op)
				func = fadd_op;
			else if (insn.mm_fp0_format.op == mm_fsub_op)
				func = fsub_op;
			else if (insn.mm_fp0_format.op == mm_fmul_op)
				func = fmul_op;
			else if (insn.mm_fp0_format.op == mm_fdiv_op)
				func = fdiv_op;
			if (func != -1) {
				mips32_insn.fp0_format.opcode = cop1_op;
				mips32_insn.fp0_format.fmt =
					sdps_format[insn.mm_fp0_format.fmt];
				mips32_insn.fp0_format.ft =
					insn.mm_fp0_format.ft;
				mips32_insn.fp0_format.fs =
					insn.mm_fp0_format.fs;
				mips32_insn.fp0_format.fd =
					insn.mm_fp0_format.fd;
				mips32_insn.fp0_format.func = func;
			} else
				return SIGILL;
			break;
		case mm_32f_70_op:
			func = -1;	/* Invalid */
			if (insn.mm_fp0_format.op == mm_fmovn_op)
				func = fmovn_op;
			else if (insn.mm_fp0_format.op == mm_fmovz_op)
				func = fmovz_op;
			if (func != -1) {
				mips32_insn.fp0_format.opcode = cop1_op;
				mips32_insn.fp0_format.fmt =
					sdps_format[insn.mm_fp0_format.fmt];
				mips32_insn.fp0_format.ft =
					insn.mm_fp0_format.ft;
				mips32_insn.fp0_format.fs =
					insn.mm_fp0_format.fs;
				mips32_insn.fp0_format.fd =
					insn.mm_fp0_format.fd;
				mips32_insn.fp0_format.func = func;
			} else
				return SIGILL;
			break;
		case mm_32f_73_op:    /* POOL32FXF */
			switch (insn.mm_fp1_format.op) {
			case mm_movf0_op:
			case mm_movf1_op:
			case mm_movt0_op:
			case mm_movt1_op:
				if ((insn.mm_fp1_format.op & 0x7f) ==
				    mm_movf0_op)
					op = 0;
				else
					op = 1;
				mips32_insn.r_format.opcode = spec_op;
				mips32_insn.r_format.rs = insn.mm_fp4_format.fs;
				mips32_insn.r_format.rt =
					(insn.mm_fp4_format.cc << 2) + op;
				mips32_insn.r_format.rd = insn.mm_fp4_format.rt;
				mips32_insn.r_format.re = 0;
				mips32_insn.r_format.func = movc_op;
				break;
			case mm_fcvtd0_op:
			case mm_fcvtd1_op:
			case mm_fcvts0_op:
			case mm_fcvts1_op:
				if ((insn.mm_fp1_format.op & 0x7f) ==
				    mm_fcvtd0_op) {
					func = fcvtd_op;
					fmt = swl_format[insn.mm_fp3_format.fmt];
				} else {
					func = fcvts_op;
					fmt = dwl_format[insn.mm_fp3_format.fmt];
				}
				mips32_insn.fp0_format.opcode = cop1_op;
				mips32_insn.fp0_format.fmt = fmt;
				mips32_insn.fp0_format.ft = 0;
				mips32_insn.fp0_format.fs =
					insn.mm_fp3_format.fs;
				mips32_insn.fp0_format.fd =
					insn.mm_fp3_format.rt;
				mips32_insn.fp0_format.func = func;
				break;
			case mm_fmov0_op:
			case mm_fmov1_op:
			case mm_fabs0_op:
			case mm_fabs1_op:
			case mm_fneg0_op:
			case mm_fneg1_op:
				if ((insn.mm_fp1_format.op & 0x7f) ==
				    mm_fmov0_op)
					func = fmov_op;
				else if ((insn.mm_fp1_format.op & 0x7f) ==
					 mm_fabs0_op)
					func = fabs_op;
				else
					func = fneg_op;
				mips32_insn.fp0_format.opcode = cop1_op;
				mips32_insn.fp0_format.fmt =
					sdps_format[insn.mm_fp3_format.fmt];
				mips32_insn.fp0_format.ft = 0;
				mips32_insn.fp0_format.fs =
					insn.mm_fp3_format.fs;
				mips32_insn.fp0_format.fd =
					insn.mm_fp3_format.rt;
				mips32_insn.fp0_format.func = func;
				break;
			case mm_ffloorl_op:
			case mm_ffloorw_op:
			case mm_fceill_op:
			case mm_fceilw_op:
			case mm_ftruncl_op:
			case mm_ftruncw_op:
			case mm_froundl_op:
			case mm_froundw_op:
			case mm_fcvtl_op:
			case mm_fcvtw_op:
				if (insn.mm_fp1_format.op == mm_ffloorl_op)
					func = ffloorl_op;
				else if (insn.mm_fp1_format.op == mm_ffloorw_op)
					func = ffloor_op;
				else if (insn.mm_fp1_format.op == mm_fceill_op)
					func = fceill_op;
				else if (insn.mm_fp1_format.op == mm_fceilw_op)
					func = fceil_op;
				else if (insn.mm_fp1_format.op == mm_ftruncl_op)
					func = ftruncl_op;
				else if (insn.mm_fp1_format.op == mm_ftruncw_op)
					func = ftrunc_op;
				else if (insn.mm_fp1_format.op == mm_froundl_op)
					func = froundl_op;
				else if (insn.mm_fp1_format.op == mm_froundw_op)
					func = fround_op;
				else if (insn.mm_fp1_format.op == mm_fcvtl_op)
					func = fcvtl_op;
				else
					func = fcvtw_op;
				mips32_insn.fp0_format.opcode = cop1_op;
				mips32_insn.fp0_format.fmt =
					sd_format[insn.mm_fp1_format.fmt];
				mips32_insn.fp0_format.ft = 0;
				mips32_insn.fp0_format.fs =
					insn.mm_fp1_format.fs;
				mips32_insn.fp0_format.fd =
					insn.mm_fp1_format.rt;
				mips32_insn.fp0_format.func = func;
				break;
			case mm_frsqrt_op:
			case mm_fsqrt_op:
			case mm_frecip_op:
				if (insn.mm_fp1_format.op == mm_frsqrt_op)
					func = frsqrt_op;
				else if (insn.mm_fp1_format.op == mm_fsqrt_op)
					func = fsqrt_op;
				else
					func = frecip_op;
				mips32_insn.fp0_format.opcode = cop1_op;
				mips32_insn.fp0_format.fmt =
					sdps_format[insn.mm_fp1_format.fmt];
				mips32_insn.fp0_format.ft = 0;
				mips32_insn.fp0_format.fs =
					insn.mm_fp1_format.fs;
				mips32_insn.fp0_format.fd =
					insn.mm_fp1_format.rt;
				mips32_insn.fp0_format.func = func;
				break;
			case mm_mfc1_op:
			case mm_mtc1_op:
			case mm_cfc1_op:
			case mm_ctc1_op:
381 382
			case mm_mfhc1_op:
			case mm_mthc1_op:
383 384 385 386 387 388
				if (insn.mm_fp1_format.op == mm_mfc1_op)
					op = mfc_op;
				else if (insn.mm_fp1_format.op == mm_mtc1_op)
					op = mtc_op;
				else if (insn.mm_fp1_format.op == mm_cfc1_op)
					op = cfc_op;
389
				else if (insn.mm_fp1_format.op == mm_ctc1_op)
390
					op = ctc_op;
391 392 393 394
				else if (insn.mm_fp1_format.op == mm_mfhc1_op)
					op = mfhc_op;
				else
					op = mthc_op;
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
				mips32_insn.fp1_format.opcode = cop1_op;
				mips32_insn.fp1_format.op = op;
				mips32_insn.fp1_format.rt =
					insn.mm_fp1_format.rt;
				mips32_insn.fp1_format.fs =
					insn.mm_fp1_format.fs;
				mips32_insn.fp1_format.fd = 0;
				mips32_insn.fp1_format.func = 0;
				break;
			default:
				return SIGILL;
			}
			break;
		case mm_32f_74_op:	/* c.cond.fmt */
			mips32_insn.fp0_format.opcode = cop1_op;
			mips32_insn.fp0_format.fmt =
				sdps_format[insn.mm_fp4_format.fmt];
			mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt;
			mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs;
			mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2;
			mips32_insn.fp0_format.func =
				insn.mm_fp4_format.cond | MM_MIPS32_COND_FC;
			break;
		default:
			return SIGILL;
		}
		break;
	default:
		return SIGILL;
	}

	*insn_ptr = mips32_insn;
	return 0;
}

L
Linus Torvalds 已提交
430 431 432 433 434 435
/*
 * Redundant with logic already in kernel/branch.c,
 * embedded in compute_return_epc.  At some point,
 * a single subroutine should be used across both
 * modules.
 */
436 437
int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
		  unsigned long *contpc)
L
Linus Torvalds 已提交
438
{
439 440 441
	union mips_instruction insn = (union mips_instruction)dec_insn.insn;
	unsigned int fcr31;
	unsigned int bit = 0;
442 443
	unsigned int bit0;
	union fpureg *fpr;
444 445

	switch (insn.i_format.opcode) {
L
Linus Torvalds 已提交
446
	case spec_op:
447
		switch (insn.r_format.func) {
L
Linus Torvalds 已提交
448
		case jalr_op:
449 450 451 452 453
			if (insn.r_format.rd != 0) {
				regs->regs[insn.r_format.rd] =
					regs->cp0_epc + dec_insn.pc_inc +
					dec_insn.next_pc_inc;
			}
454
			/* Fall through */
L
Linus Torvalds 已提交
455
		case jr_op:
456
			/* For R6, JR already emulated in jalr_op */
457
			if (NO_R6EMU && insn.r_format.func == jr_op)
458
				break;
459
			*contpc = regs->regs[insn.r_format.rs];
L
Linus Torvalds 已提交
460 461 462 463
			return 1;
		}
		break;
	case bcond_op:
464 465 466
		switch (insn.i_format.rt) {
		case bltzal_op:
		case bltzall_op:
467 468 469 470
			if (NO_R6EMU && (insn.i_format.rs ||
			    insn.i_format.rt == bltzall_op))
				break;

471 472 473 474
			regs->regs[31] = regs->cp0_epc +
				dec_insn.pc_inc +
				dec_insn.next_pc_inc;
			/* Fall through */
L
Linus Torvalds 已提交
475
		case bltzl_op:
476 477 478
			if (NO_R6EMU)
				break;
		case bltz_op:
479 480 481 482 483 484 485 486 487
			if ((long)regs->regs[insn.i_format.rs] < 0)
				*contpc = regs->cp0_epc +
					dec_insn.pc_inc +
					(insn.i_format.simmediate << 2);
			else
				*contpc = regs->cp0_epc +
					dec_insn.pc_inc +
					dec_insn.next_pc_inc;
			return 1;
L
Linus Torvalds 已提交
488 489
		case bgezal_op:
		case bgezall_op:
490 491 492 493
			if (NO_R6EMU && (insn.i_format.rs ||
			    insn.i_format.rt == bgezall_op))
				break;

494 495 496 497 498
			regs->regs[31] = regs->cp0_epc +
				dec_insn.pc_inc +
				dec_insn.next_pc_inc;
			/* Fall through */
		case bgezl_op:
499 500 501
			if (NO_R6EMU)
				break;
		case bgez_op:
502 503 504 505 506 507 508 509
			if ((long)regs->regs[insn.i_format.rs] >= 0)
				*contpc = regs->cp0_epc +
					dec_insn.pc_inc +
					(insn.i_format.simmediate << 2);
			else
				*contpc = regs->cp0_epc +
					dec_insn.pc_inc +
					dec_insn.next_pc_inc;
L
Linus Torvalds 已提交
510 511 512 513
			return 1;
		}
		break;
	case jalx_op:
514 515 516 517 518 519 520 521 522 523 524 525 526 527
		set_isa16_mode(bit);
	case jal_op:
		regs->regs[31] = regs->cp0_epc +
			dec_insn.pc_inc +
			dec_insn.next_pc_inc;
		/* Fall through */
	case j_op:
		*contpc = regs->cp0_epc + dec_insn.pc_inc;
		*contpc >>= 28;
		*contpc <<= 28;
		*contpc |= (insn.j_format.target << 2);
		/* Set microMIPS mode bit: XOR for jalx. */
		*contpc ^= bit;
		return 1;
L
Linus Torvalds 已提交
528
	case beql_op:
529 530 531
		if (NO_R6EMU)
			break;
	case beq_op:
532 533 534 535 536 537 538 539 540 541
		if (regs->regs[insn.i_format.rs] ==
		    regs->regs[insn.i_format.rt])
			*contpc = regs->cp0_epc +
				dec_insn.pc_inc +
				(insn.i_format.simmediate << 2);
		else
			*contpc = regs->cp0_epc +
				dec_insn.pc_inc +
				dec_insn.next_pc_inc;
		return 1;
L
Linus Torvalds 已提交
542
	case bnel_op:
543 544 545
		if (NO_R6EMU)
			break;
	case bne_op:
546 547 548 549 550 551 552 553 554 555
		if (regs->regs[insn.i_format.rs] !=
		    regs->regs[insn.i_format.rt])
			*contpc = regs->cp0_epc +
				dec_insn.pc_inc +
				(insn.i_format.simmediate << 2);
		else
			*contpc = regs->cp0_epc +
				dec_insn.pc_inc +
				dec_insn.next_pc_inc;
		return 1;
L
Linus Torvalds 已提交
556
	case blezl_op:
557
		if (!insn.i_format.rt && NO_R6EMU)
558 559
			break;
	case blez_op:
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583

		/*
		 * Compact branches for R6 for the
		 * blez and blezl opcodes.
		 * BLEZ  | rs = 0 | rt != 0  == BLEZALC
		 * BLEZ  | rs = rt != 0      == BGEZALC
		 * BLEZ  | rs != 0 | rt != 0 == BGEUC
		 * BLEZL | rs = 0 | rt != 0  == BLEZC
		 * BLEZL | rs = rt != 0      == BGEZC
		 * BLEZL | rs != 0 | rt != 0 == BGEC
		 *
		 * For real BLEZ{,L}, rt is always 0.
		 */
		if (cpu_has_mips_r6 && insn.i_format.rt) {
			if ((insn.i_format.opcode == blez_op) &&
			    ((!insn.i_format.rs && insn.i_format.rt) ||
			     (insn.i_format.rs == insn.i_format.rt)))
				regs->regs[31] = regs->cp0_epc +
					dec_insn.pc_inc;
			*contpc = regs->cp0_epc + dec_insn.pc_inc +
				dec_insn.next_pc_inc;

			return 1;
		}
584 585 586 587 588 589 590 591 592
		if ((long)regs->regs[insn.i_format.rs] <= 0)
			*contpc = regs->cp0_epc +
				dec_insn.pc_inc +
				(insn.i_format.simmediate << 2);
		else
			*contpc = regs->cp0_epc +
				dec_insn.pc_inc +
				dec_insn.next_pc_inc;
		return 1;
L
Linus Torvalds 已提交
593
	case bgtzl_op:
594
		if (!insn.i_format.rt && NO_R6EMU)
595 596
			break;
	case bgtz_op:
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
		/*
		 * Compact branches for R6 for the
		 * bgtz and bgtzl opcodes.
		 * BGTZ  | rs = 0 | rt != 0  == BGTZALC
		 * BGTZ  | rs = rt != 0      == BLTZALC
		 * BGTZ  | rs != 0 | rt != 0 == BLTUC
		 * BGTZL | rs = 0 | rt != 0  == BGTZC
		 * BGTZL | rs = rt != 0      == BLTZC
		 * BGTZL | rs != 0 | rt != 0 == BLTC
		 *
		 * *ZALC varint for BGTZ &&& rt != 0
		 * For real GTZ{,L}, rt is always 0.
		 */
		if (cpu_has_mips_r6 && insn.i_format.rt) {
			if ((insn.i_format.opcode == blez_op) &&
			    ((!insn.i_format.rs && insn.i_format.rt) ||
			     (insn.i_format.rs == insn.i_format.rt)))
				regs->regs[31] = regs->cp0_epc +
					dec_insn.pc_inc;
			*contpc = regs->cp0_epc + dec_insn.pc_inc +
				dec_insn.next_pc_inc;

			return 1;
		}

622 623 624 625 626 627 628 629
		if ((long)regs->regs[insn.i_format.rs] > 0)
			*contpc = regs->cp0_epc +
				dec_insn.pc_inc +
				(insn.i_format.simmediate << 2);
		else
			*contpc = regs->cp0_epc +
				dec_insn.pc_inc +
				dec_insn.next_pc_inc;
L
Linus Torvalds 已提交
630
		return 1;
631 632
	case pop10_op:
	case pop30_op:
633 634 635 636 637 638 639 640
		if (!cpu_has_mips_r6)
			break;
		if (insn.i_format.rt && !insn.i_format.rs)
			regs->regs[31] = regs->cp0_epc + 4;
		*contpc = regs->cp0_epc + dec_insn.pc_inc +
			dec_insn.next_pc_inc;

		return 1;
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
#ifdef CONFIG_CPU_CAVIUM_OCTEON
	case lwc2_op: /* This is bbit0 on Octeon */
		if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0)
			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
		else
			*contpc = regs->cp0_epc + 8;
		return 1;
	case ldc2_op: /* This is bbit032 on Octeon */
		if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
		else
			*contpc = regs->cp0_epc + 8;
		return 1;
	case swc2_op: /* This is bbit1 on Octeon */
		if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
		else
			*contpc = regs->cp0_epc + 8;
		return 1;
	case sdc2_op: /* This is bbit132 on Octeon */
		if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
		else
			*contpc = regs->cp0_epc + 8;
		return 1;
666 667 668 669 670 671 672 673 674 675 676 677
#else
	case bc6_op:
		/*
		 * Only valid for MIPS R6 but we can still end up
		 * here from a broken userland so just tell emulator
		 * this is not a branch and let it break later on.
		 */
		if  (!cpu_has_mips_r6)
			break;
		*contpc = regs->cp0_epc + dec_insn.pc_inc +
			dec_insn.next_pc_inc;

678 679 680 681 682 683 684 685
		return 1;
	case balc6_op:
		if (!cpu_has_mips_r6)
			break;
		regs->regs[31] = regs->cp0_epc + 4;
		*contpc = regs->cp0_epc + dec_insn.pc_inc +
			dec_insn.next_pc_inc;

686
		return 1;
687
	case pop66_op:
688 689 690 691 692
		if (!cpu_has_mips_r6)
			break;
		*contpc = regs->cp0_epc + dec_insn.pc_inc +
			dec_insn.next_pc_inc;

693
		return 1;
694
	case pop76_op:
695 696 697 698 699 700 701
		if (!cpu_has_mips_r6)
			break;
		if (!insn.i_format.rs)
			regs->regs[31] = regs->cp0_epc + 4;
		*contpc = regs->cp0_epc + dec_insn.pc_inc +
			dec_insn.next_pc_inc;

702
		return 1;
703
#endif
L
Linus Torvalds 已提交
704 705
	case cop0_op:
	case cop1_op:
706 707 708 709 710
		/* Need to check for R6 bc1nez and bc1eqz branches */
		if (cpu_has_mips_r6 &&
		    ((insn.i_format.rs == bc1eqz_op) ||
		     (insn.i_format.rs == bc1nez_op))) {
			bit = 0;
711 712
			fpr = &current->thread.fpu.fpr[insn.i_format.rt];
			bit0 = get_fpr32(fpr, 0) & 0x1;
713 714
			switch (insn.i_format.rs) {
			case bc1eqz_op:
715
				bit = bit0 == 0;
716 717
				break;
			case bc1nez_op:
718
				bit = bit0 != 0;
719 720 721 722 723 724 725 726 727 728 729 730 731 732
				break;
			}
			if (bit)
				*contpc = regs->cp0_epc +
					dec_insn.pc_inc +
					(insn.i_format.simmediate << 2);
			else
				*contpc = regs->cp0_epc +
					dec_insn.pc_inc +
					dec_insn.next_pc_inc;

			return 1;
		}
		/* R2/R6 compatible cop1 instruction. Fall through */
L
Linus Torvalds 已提交
733 734
	case cop2_op:
	case cop1x_op:
735 736 737
		if (insn.i_format.rs == bc_op) {
			preempt_disable();
			if (is_fpu_owner())
738
			        fcr31 = read_32bit_cp1_register(CP1_STATUS);
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
			else
				fcr31 = current->thread.fpu.fcr31;
			preempt_enable();

			bit = (insn.i_format.rt >> 2);
			bit += (bit != 0);
			bit += 23;
			switch (insn.i_format.rt & 3) {
			case 0:	/* bc1f */
			case 2:	/* bc1fl */
				if (~fcr31 & (1 << bit))
					*contpc = regs->cp0_epc +
						dec_insn.pc_inc +
						(insn.i_format.simmediate << 2);
				else
					*contpc = regs->cp0_epc +
						dec_insn.pc_inc +
						dec_insn.next_pc_inc;
				return 1;
			case 1:	/* bc1t */
			case 3:	/* bc1tl */
				if (fcr31 & (1 << bit))
					*contpc = regs->cp0_epc +
						dec_insn.pc_inc +
						(insn.i_format.simmediate << 2);
				else
					*contpc = regs->cp0_epc +
						dec_insn.pc_inc +
						dec_insn.next_pc_inc;
				return 1;
			}
		}
L
Linus Torvalds 已提交
771 772 773 774 775 776 777
		break;
	}
	return 0;
}

/*
 * In the Linux kernel, we support selection of FPR format on the
R
Ralf Baechle 已提交
778
 * basis of the Status.FR bit.	If an FPU is not present, the FR bit
779
 * is hardwired to zero, which would imply a 32-bit FPU even for
780
 * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
781 782 783
 * FPU emu is slow and bulky and optimizing this function offers fairly
 * sizeable benefits so we try to be clever and make this function return
 * a constant whenever possible, that is on 64-bit kernels without O32
784
 * compatibility enabled and on 32-bit without 64-bit FPU support.
L
Linus Torvalds 已提交
785
 */
786 787
static inline int cop1_64bit(struct pt_regs *xcp)
{
788
	if (IS_ENABLED(CONFIG_64BIT) && !IS_ENABLED(CONFIG_MIPS32_O32))
789
		return 1;
790 791
	else if (IS_ENABLED(CONFIG_32BIT) &&
		 !IS_ENABLED(CONFIG_MIPS_O32_FP64_SUPPORT))
792 793
		return 0;

794
	return !test_thread_flag(TIF_32BIT_FPREGS);
795 796
}

P
Paul Burton 已提交
797 798 799 800 801
static inline bool hybrid_fprs(void)
{
	return test_thread_flag(TIF_HYBRID_FPREGS);
}

802 803
#define SIFROMREG(si, x)						\
do {									\
P
Paul Burton 已提交
804
	if (cop1_64bit(xcp) && !hybrid_fprs())				\
805
		(si) = (int)get_fpr32(&ctx->fpr[x], 0);			\
P
Paul Burton 已提交
806
	else								\
807
		(si) = (int)get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1);	\
P
Paul Burton 已提交
808
} while (0)
L
Linus Torvalds 已提交
809

810 811
#define SITOREG(si, x)							\
do {									\
P
Paul Burton 已提交
812
	if (cop1_64bit(xcp) && !hybrid_fprs()) {			\
813
		unsigned i;						\
P
Paul Burton 已提交
814
		set_fpr32(&ctx->fpr[x], 0, si);				\
815 816 817
		for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++)	\
			set_fpr32(&ctx->fpr[x], i, 0);			\
	} else {							\
P
Paul Burton 已提交
818
		set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si);		\
819
	}								\
P
Paul Burton 已提交
820
} while (0)
L
Linus Torvalds 已提交
821

822
#define SIFROMHREG(si, x)	((si) = (int)get_fpr32(&ctx->fpr[x], 1))
823

824 825
#define SITOHREG(si, x)							\
do {									\
826 827 828 829 830
	unsigned i;							\
	set_fpr32(&ctx->fpr[x], 1, si);					\
	for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++)		\
		set_fpr32(&ctx->fpr[x], i, 0);				\
} while (0)
831

832
#define DIFROMREG(di, x)						\
833
	((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) ^ 1)], 0))
P
Paul Burton 已提交
834

835 836
#define DITOREG(di, x)							\
do {									\
837
	unsigned fpr, i;						\
838
	fpr = (x) & ~(cop1_64bit(xcp) ^ 1);				\
839 840 841 842
	set_fpr64(&ctx->fpr[fpr], 0, di);				\
	for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++)		\
		set_fpr64(&ctx->fpr[fpr], i, 0);			\
} while (0)
L
Linus Torvalds 已提交
843

844 845 846 847
#define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
#define SPTOREG(sp, x)	SITOREG((sp).bits, x)
#define DPFROMREG(dp, x)	DIFROMREG((dp).bits, x)
#define DPTOREG(dp, x)	DITOREG((dp).bits, x)
L
Linus Torvalds 已提交
848

849 850 851 852 853 854
/*
 * Emulate a CFC1 instruction.
 */
static inline void cop1_cfc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
			    mips_instruction ir)
{
855 856
	u32 fcr31 = ctx->fcr31;
	u32 value = 0;
857

858 859 860
	switch (MIPSInst_RD(ir)) {
	case FPCREG_CSR:
		value = fcr31;
861
		pr_debug("%p gpr[%d]<-csr=%08x\n",
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
		break;

	case FPCREG_FENR:
		if (!cpu_has_mips_r)
			break;
		value = (fcr31 >> (FPU_CSR_FS_S - MIPS_FENR_FS_S)) &
			MIPS_FENR_FS;
		value |= fcr31 & (FPU_CSR_ALL_E | FPU_CSR_RM);
		pr_debug("%p gpr[%d]<-enr=%08x\n",
			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
		break;

	case FPCREG_FEXR:
		if (!cpu_has_mips_r)
			break;
		value = fcr31 & (FPU_CSR_ALL_X | FPU_CSR_ALL_S);
		pr_debug("%p gpr[%d]<-exr=%08x\n",
			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
		break;

	case FPCREG_FCCR:
		if (!cpu_has_mips_r)
			break;
		value = (fcr31 >> (FPU_CSR_COND_S - MIPS_FCCR_COND0_S)) &
			MIPS_FCCR_COND0;
		value |= (fcr31 >> (FPU_CSR_COND1_S - MIPS_FCCR_COND1_S)) &
			 (MIPS_FCCR_CONDX & ~MIPS_FCCR_COND0);
		pr_debug("%p gpr[%d]<-ccr=%08x\n",
			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
		break;

	case FPCREG_RID:
895
		value = boot_cpu_data.fpu_id;
896 897 898 899 900 901
		break;

	default:
		break;
	}

902 903 904 905 906 907 908 909 910 911
	if (MIPSInst_RT(ir))
		xcp->regs[MIPSInst_RT(ir)] = value;
}

/*
 * Emulate a CTC1 instruction.
 */
static inline void cop1_ctc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
			    mips_instruction ir)
{
912
	u32 fcr31 = ctx->fcr31;
913
	u32 value;
914
	u32 mask;
915 916 917 918 919 920

	if (MIPSInst_RT(ir) == 0)
		value = 0;
	else
		value = xcp->regs[MIPSInst_RT(ir)];

921 922
	switch (MIPSInst_RD(ir)) {
	case FPCREG_CSR:
923
		pr_debug("%p gpr[%d]->csr=%08x\n",
924
			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
925

926
		/* Preserve read-only bits.  */
927
		mask = boot_cpu_data.fpu_msk31;
928
		fcr31 = (value & ~mask) | (fcr31 & mask);
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
		break;

	case FPCREG_FENR:
		if (!cpu_has_mips_r)
			break;
		pr_debug("%p gpr[%d]->enr=%08x\n",
			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
		fcr31 &= ~(FPU_CSR_FS | FPU_CSR_ALL_E | FPU_CSR_RM);
		fcr31 |= (value << (FPU_CSR_FS_S - MIPS_FENR_FS_S)) &
			 FPU_CSR_FS;
		fcr31 |= value & (FPU_CSR_ALL_E | FPU_CSR_RM);
		break;

	case FPCREG_FEXR:
		if (!cpu_has_mips_r)
			break;
		pr_debug("%p gpr[%d]->exr=%08x\n",
			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
		fcr31 &= ~(FPU_CSR_ALL_X | FPU_CSR_ALL_S);
		fcr31 |= value & (FPU_CSR_ALL_X | FPU_CSR_ALL_S);
		break;

	case FPCREG_FCCR:
		if (!cpu_has_mips_r)
			break;
		pr_debug("%p gpr[%d]->ccr=%08x\n",
			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
		fcr31 &= ~(FPU_CSR_CONDX | FPU_CSR_COND);
		fcr31 |= (value << (FPU_CSR_COND_S - MIPS_FCCR_COND0_S)) &
			 FPU_CSR_COND;
		fcr31 |= (value << (FPU_CSR_COND1_S - MIPS_FCCR_COND1_S)) &
			 FPU_CSR_CONDX;
		break;

	default:
		break;
965
	}
966 967

	ctx->fcr31 = fcr31;
968 969
}

L
Linus Torvalds 已提交
970 971 972 973 974
/*
 * Emulate the single floating point instruction pointed at by EPC.
 * Two instructions if the instruction is in a branch delay slot.
 */

975
static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
976
		struct mm_decoded_insn dec_insn, void __user **fault_addr)
L
Linus Torvalds 已提交
977
{
978
	unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
979
	unsigned int cond, cbit, bit0;
980 981
	mips_instruction ir;
	int likely, pc_inc;
982
	union fpureg *fpr;
983 984 985 986 987
	u32 __user *wva;
	u64 __user *dva;
	u32 wval;
	u64 dval;
	int sig;
L
Linus Torvalds 已提交
988

989 990 991 992 993 994 995
	/*
	 * These are giving gcc a gentle hint about what to expect in
	 * dec_inst in order to do better optimization.
	 */
	if (!cpu_has_mmips && dec_insn.micro_mips_mode)
		unreachable();

L
Linus Torvalds 已提交
996
	/* XXX NEC Vr54xx bug workaround */
997
	if (delay_slot(xcp)) {
998 999
		if (dec_insn.micro_mips_mode) {
			if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
1000
				clear_delay_slot(xcp);
1001 1002
		} else {
			if (!isBranchInstr(xcp, dec_insn, &contpc))
1003
				clear_delay_slot(xcp);
1004 1005
		}
	}
L
Linus Torvalds 已提交
1006

1007
	if (delay_slot(xcp)) {
L
Linus Torvalds 已提交
1008 1009
		/*
		 * The instruction to be emulated is in a branch delay slot
R
Ralf Baechle 已提交
1010
		 * which means that we have to	emulate the branch instruction
L
Linus Torvalds 已提交
1011 1012 1013 1014 1015 1016 1017 1018 1019
		 * BEFORE we do the cop1 instruction.
		 *
		 * This branch could be a COP1 branch, but in that case we
		 * would have had a trap for that instruction, and would not
		 * come through this route.
		 *
		 * Linux MIPS branch emulator operates on context, updating the
		 * cp0_epc.
		 */
1020 1021 1022 1023 1024 1025
		ir = dec_insn.next_insn;  /* process delay slot instr */
		pc_inc = dec_insn.next_pc_inc;
	} else {
		ir = dec_insn.insn;       /* process current instr */
		pc_inc = dec_insn.pc_inc;
	}
L
Linus Torvalds 已提交
1026

1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
	/*
	 * Since microMIPS FPU instructios are a subset of MIPS32 FPU
	 * instructions, we want to convert microMIPS FPU instructions
	 * into MIPS32 instructions so that we could reuse all of the
	 * FPU emulation code.
	 *
	 * NOTE: We cannot do this for branch instructions since they
	 *       are not a subset. Example: Cannot emulate a 16-bit
	 *       aligned target address with a MIPS32 instruction.
	 */
	if (dec_insn.micro_mips_mode) {
		/*
		 * If next instruction is a 16-bit instruction, then it
		 * it cannot be a FPU instruction. This could happen
		 * since we can be called for non-FPU instructions.
		 */
		if ((pc_inc == 2) ||
			(microMIPS32_to_MIPS32((union mips_instruction *)&ir)
			 == SIGILL))
L
Linus Torvalds 已提交
1046 1047 1048
			return SIGILL;
	}

1049
emul:
1050
	perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
1051
	MIPS_FPU_EMU_INC_STATS(emulated);
L
Linus Torvalds 已提交
1052
	switch (MIPSInst_OPCODE(ir)) {
1053 1054 1055
	case ldc1_op:
		dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
				     MIPSInst_SIMM(ir));
1056
		MIPS_FPU_EMU_INC_STATS(loads);
1057

1058
		if (!access_ok(VERIFY_READ, dva, sizeof(u64))) {
1059
			MIPS_FPU_EMU_INC_STATS(errors);
1060
			*fault_addr = dva;
L
Linus Torvalds 已提交
1061 1062
			return SIGBUS;
		}
1063
		if (__get_user(dval, dva)) {
1064
			MIPS_FPU_EMU_INC_STATS(errors);
1065
			*fault_addr = dva;
1066 1067
			return SIGSEGV;
		}
1068
		DITOREG(dval, MIPSInst_RT(ir));
L
Linus Torvalds 已提交
1069 1070
		break;

1071 1072 1073
	case sdc1_op:
		dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
				      MIPSInst_SIMM(ir));
1074
		MIPS_FPU_EMU_INC_STATS(stores);
1075 1076
		DIFROMREG(dval, MIPSInst_RT(ir));
		if (!access_ok(VERIFY_WRITE, dva, sizeof(u64))) {
1077
			MIPS_FPU_EMU_INC_STATS(errors);
1078
			*fault_addr = dva;
L
Linus Torvalds 已提交
1079 1080
			return SIGBUS;
		}
1081
		if (__put_user(dval, dva)) {
1082
			MIPS_FPU_EMU_INC_STATS(errors);
1083
			*fault_addr = dva;
1084 1085
			return SIGSEGV;
		}
L
Linus Torvalds 已提交
1086 1087
		break;

1088 1089 1090
	case lwc1_op:
		wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
				      MIPSInst_SIMM(ir));
1091
		MIPS_FPU_EMU_INC_STATS(loads);
1092
		if (!access_ok(VERIFY_READ, wva, sizeof(u32))) {
1093
			MIPS_FPU_EMU_INC_STATS(errors);
1094
			*fault_addr = wva;
L
Linus Torvalds 已提交
1095 1096
			return SIGBUS;
		}
1097
		if (__get_user(wval, wva)) {
1098
			MIPS_FPU_EMU_INC_STATS(errors);
1099
			*fault_addr = wva;
1100 1101
			return SIGSEGV;
		}
1102
		SITOREG(wval, MIPSInst_RT(ir));
L
Linus Torvalds 已提交
1103 1104
		break;

1105 1106 1107
	case swc1_op:
		wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
				      MIPSInst_SIMM(ir));
1108
		MIPS_FPU_EMU_INC_STATS(stores);
1109 1110
		SIFROMREG(wval, MIPSInst_RT(ir));
		if (!access_ok(VERIFY_WRITE, wva, sizeof(u32))) {
1111
			MIPS_FPU_EMU_INC_STATS(errors);
1112
			*fault_addr = wva;
L
Linus Torvalds 已提交
1113 1114
			return SIGBUS;
		}
1115
		if (__put_user(wval, wva)) {
1116
			MIPS_FPU_EMU_INC_STATS(errors);
1117
			*fault_addr = wva;
1118 1119
			return SIGSEGV;
		}
L
Linus Torvalds 已提交
1120 1121 1122 1123 1124
		break;

	case cop1_op:
		switch (MIPSInst_RS(ir)) {
		case dmfc_op:
1125 1126 1127
			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
				return SIGILL;

L
Linus Torvalds 已提交
1128 1129 1130 1131 1132 1133 1134 1135
			/* copregister fs -> gpr[rt] */
			if (MIPSInst_RT(ir) != 0) {
				DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
					MIPSInst_RD(ir));
			}
			break;

		case dmtc_op:
1136 1137 1138
			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
				return SIGILL;

L
Linus Torvalds 已提交
1139 1140 1141 1142
			/* copregister fs <- rt */
			DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
			break;

1143
		case mfhc_op:
1144
			if (!cpu_has_mips_r2_r6)
1145
				return SIGILL;
1146 1147 1148 1149 1150 1151 1152 1153 1154

			/* copregister rd -> gpr[rt] */
			if (MIPSInst_RT(ir) != 0) {
				SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
					MIPSInst_RD(ir));
			}
			break;

		case mthc_op:
1155
			if (!cpu_has_mips_r2_r6)
1156
				return SIGILL;
1157 1158 1159 1160 1161

			/* copregister rd <- gpr[rt] */
			SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
			break;

L
Linus Torvalds 已提交
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
		case mfc_op:
			/* copregister rd -> gpr[rt] */
			if (MIPSInst_RT(ir) != 0) {
				SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
					MIPSInst_RD(ir));
			}
			break;

		case mtc_op:
			/* copregister rd <- rt */
			SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
			break;

1175
		case cfc_op:
L
Linus Torvalds 已提交
1176
			/* cop control register rd -> gpr[rt] */
1177
			cop1_cfc(xcp, ctx, ir);
L
Linus Torvalds 已提交
1178 1179
			break;

1180
		case ctc_op:
L
Linus Torvalds 已提交
1181
			/* copregister rd <- rt */
1182
			cop1_ctc(xcp, ctx, ir);
L
Linus Torvalds 已提交
1183 1184 1185 1186 1187
			if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
				return SIGFPE;
			}
			break;

1188 1189 1190 1191 1192 1193
		case bc1eqz_op:
		case bc1nez_op:
			if (!cpu_has_mips_r6 || delay_slot(xcp))
				return SIGILL;

			cond = likely = 0;
1194 1195
			fpr = &current->thread.fpu.fpr[MIPSInst_RT(ir)];
			bit0 = get_fpr32(fpr, 0) & 0x1;
1196 1197
			switch (MIPSInst_RS(ir)) {
			case bc1eqz_op:
1198
				cond = bit0 == 0;
1199 1200
				break;
			case bc1nez_op:
1201
				cond = bit0 != 0;
1202 1203 1204 1205
				break;
			}
			goto branch_common;

1206
		case bc_op:
1207
			if (delay_slot(xcp))
L
Linus Torvalds 已提交
1208 1209
				return SIGILL;

1210 1211 1212 1213 1214 1215
			if (cpu_has_mips_4_5_r)
				cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
			else
				cbit = FPU_CSR_COND;
			cond = ctx->fcr31 & cbit;

1216
			likely = 0;
L
Linus Torvalds 已提交
1217 1218
			switch (MIPSInst_RT(ir) & 3) {
			case bcfl_op:
1219 1220 1221
				if (cpu_has_mips_2_3_4_5_r)
					likely = 1;
				/* Fall through */
L
Linus Torvalds 已提交
1222 1223 1224 1225
			case bcf_op:
				cond = !cond;
				break;
			case bctl_op:
1226 1227 1228
				if (cpu_has_mips_2_3_4_5_r)
					likely = 1;
				/* Fall through */
L
Linus Torvalds 已提交
1229 1230 1231
			case bct_op:
				break;
			}
1232
branch_common:
1233
			set_delay_slot(xcp);
L
Linus Torvalds 已提交
1234
			if (cond) {
1235 1236
				/*
				 * Branch taken: emulate dslot instruction
L
Linus Torvalds 已提交
1237
				 */
1238 1239 1240 1241 1242 1243 1244 1245
				unsigned long bcpc;

				/*
				 * Remember EPC at the branch to point back
				 * at so that any delay-slot instruction
				 * signal is not silently ignored.
				 */
				bcpc = xcp->cp0_epc;
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
				xcp->cp0_epc += dec_insn.pc_inc;

				contpc = MIPSInst_SIMM(ir);
				ir = dec_insn.next_insn;
				if (dec_insn.micro_mips_mode) {
					contpc = (xcp->cp0_epc + (contpc << 1));

					/* If 16-bit instruction, not FPU. */
					if ((dec_insn.next_pc_inc == 2) ||
						(microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {

						/*
						 * Since this instruction will
						 * be put on the stack with
						 * 32-bit words, get around
						 * this problem by putting a
						 * NOP16 as the second one.
						 */
						if (dec_insn.next_pc_inc == 2)
							ir = (ir & (~0xffff)) | MM_NOP16;

						/*
						 * Single step the non-CP1
						 * instruction in the dslot.
						 */
1271
						sig = mips_dsemul(xcp, ir,
1272
								  bcpc, contpc);
1273 1274
						if (sig < 0)
							break;
1275 1276 1277 1278 1279 1280 1281
						if (sig)
							xcp->cp0_epc = bcpc;
						/*
						 * SIGILL forces out of
						 * the emulation loop.
						 */
						return sig ? sig : SIGILL;
1282 1283 1284
					}
				} else
					contpc = (xcp->cp0_epc + (contpc << 2));
L
Linus Torvalds 已提交
1285 1286 1287 1288

				switch (MIPSInst_OPCODE(ir)) {
				case lwc1_op:
				case swc1_op:
1289
					goto emul;
1290

L
Linus Torvalds 已提交
1291 1292
				case ldc1_op:
				case sdc1_op:
1293
					if (cpu_has_mips_2_3_4_5_r)
1294 1295
						goto emul;

1296
					goto bc_sigill;
1297

L
Linus Torvalds 已提交
1298 1299
				case cop1_op:
					goto emul;
1300

1301
				case cop1x_op:
1302
					if (cpu_has_mips_4_5_64_r2_r6)
1303 1304 1305
						/* its one of ours */
						goto emul;

1306
					goto bc_sigill;
1307

L
Linus Torvalds 已提交
1308
				case spec_op:
1309 1310 1311 1312
					switch (MIPSInst_FUNC(ir)) {
					case movc_op:
						if (cpu_has_mips_4_5_r)
							goto emul;
1313

1314
						goto bc_sigill;
1315
					}
L
Linus Torvalds 已提交
1316
					break;
1317 1318 1319 1320

				bc_sigill:
					xcp->cp0_epc = bcpc;
					return SIGILL;
L
Linus Torvalds 已提交
1321 1322 1323 1324 1325 1326
				}

				/*
				 * Single step the non-cp1
				 * instruction in the dslot
				 */
1327
				sig = mips_dsemul(xcp, ir, bcpc, contpc);
1328 1329
				if (sig < 0)
					break;
1330 1331 1332 1333
				if (sig)
					xcp->cp0_epc = bcpc;
				/* SIGILL forces out of the emulation loop.  */
				return sig ? sig : SIGILL;
1334
			} else if (likely) {	/* branch not taken */
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
				/*
				 * branch likely nullifies
				 * dslot if not taken
				 */
				xcp->cp0_epc += dec_insn.pc_inc;
				contpc += dec_insn.pc_inc;
				/*
				 * else continue & execute
				 * dslot as normal insn
				 */
			}
L
Linus Torvalds 已提交
1346 1347 1348 1349 1350 1351
			break;

		default:
			if (!(MIPSInst_RS(ir) & 0x10))
				return SIGILL;

1352 1353 1354
			/* a real fpu computation instruction */
			if ((sig = fpu_emu(xcp, ctx, ir)))
				return sig;
L
Linus Torvalds 已提交
1355 1356 1357
		}
		break;

1358
	case cop1x_op:
1359
		if (!cpu_has_mips_4_5_64_r2_r6)
1360 1361 1362
			return SIGILL;

		sig = fpux_emu(xcp, ctx, ir, fault_addr);
1363
		if (sig)
L
Linus Torvalds 已提交
1364 1365 1366 1367
			return sig;
		break;

	case spec_op:
1368 1369 1370
		if (!cpu_has_mips_4_5_r)
			return SIGILL;

L
Linus Torvalds 已提交
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
		if (MIPSInst_FUNC(ir) != movc_op)
			return SIGILL;
		cond = fpucondbit[MIPSInst_RT(ir) >> 2];
		if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
			xcp->regs[MIPSInst_RD(ir)] =
				xcp->regs[MIPSInst_RS(ir)];
		break;
	default:
		return SIGILL;
	}

	/* we did it !! */
A
Atsushi Nemoto 已提交
1383
	xcp->cp0_epc = contpc;
1384
	clear_delay_slot(xcp);
1385

L
Linus Torvalds 已提交
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
	return 0;
}

/*
 * Conversion table from MIPS compare ops 48-63
 * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
 */
static const unsigned char cmptab[8] = {
	0,			/* cmp_0 (sig) cmp_sf */
	IEEE754_CUN,		/* cmp_un (sig) cmp_ngle */
	IEEE754_CEQ,		/* cmp_eq (sig) cmp_seq */
	IEEE754_CEQ | IEEE754_CUN,	/* cmp_ueq (sig) cmp_ngl  */
	IEEE754_CLT,		/* cmp_olt (sig) cmp_lt */
	IEEE754_CLT | IEEE754_CUN,	/* cmp_ult (sig) cmp_nge */
	IEEE754_CLT | IEEE754_CEQ,	/* cmp_ole (sig) cmp_le */
	IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN,	/* cmp_ule (sig) cmp_ngt */
};

1404 1405 1406 1407 1408 1409 1410 1411
static const unsigned char negative_cmptab[8] = {
	0, /* Reserved */
	IEEE754_CLT | IEEE754_CGT | IEEE754_CEQ,
	IEEE754_CLT | IEEE754_CGT | IEEE754_CUN,
	IEEE754_CLT | IEEE754_CGT,
	/* Reserved */
};

L
Linus Torvalds 已提交
1412 1413 1414 1415 1416

/*
 * Additional MIPS4 instructions
 */

1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
#define DEF3OP(name, p, f1, f2, f3)					\
static union ieee754##p fpemu_##p##_##name(union ieee754##p r,		\
	union ieee754##p s, union ieee754##p t)				\
{									\
	struct _ieee754_csr ieee754_csr_save;				\
	s = f1(s, t);							\
	ieee754_csr_save = ieee754_csr;					\
	s = f2(s, r);							\
	ieee754_csr_save.cx |= ieee754_csr.cx;				\
	ieee754_csr_save.sx |= ieee754_csr.sx;				\
	s = f3(s);							\
	ieee754_csr.cx |= ieee754_csr_save.cx;				\
	ieee754_csr.sx |= ieee754_csr_save.sx;				\
	return s;							\
L
Linus Torvalds 已提交
1431 1432
}

1433
static union ieee754dp fpemu_dp_recip(union ieee754dp d)
L
Linus Torvalds 已提交
1434 1435 1436 1437
{
	return ieee754dp_div(ieee754dp_one(0), d);
}

1438
static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
L
Linus Torvalds 已提交
1439 1440 1441 1442
{
	return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
}

1443
static union ieee754sp fpemu_sp_recip(union ieee754sp s)
L
Linus Torvalds 已提交
1444 1445 1446 1447
{
	return ieee754sp_div(ieee754sp_one(0), s);
}

1448
static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
L
Linus Torvalds 已提交
1449 1450 1451 1452
{
	return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
}

1453 1454
DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
L
Linus Torvalds 已提交
1455 1456
DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
1457 1458
DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
L
Linus Torvalds 已提交
1459 1460 1461
DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);

1462
static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1463
	mips_instruction ir, void __user **fault_addr)
L
Linus Torvalds 已提交
1464 1465 1466
{
	unsigned rcsr = 0;	/* resulting csr */

1467
	MIPS_FPU_EMU_INC_STATS(cp1xops);
L
Linus Torvalds 已提交
1468 1469 1470 1471

	switch (MIPSInst_FMA_FFMT(ir)) {
	case s_fmt:{		/* 0 */

1472 1473
		union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
		union ieee754sp fd, fr, fs, ft;
1474
		u32 __user *va;
L
Linus Torvalds 已提交
1475 1476 1477 1478
		u32 val;

		switch (MIPSInst_FUNC(ir)) {
		case lwxc1_op:
1479
			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
L
Linus Torvalds 已提交
1480 1481
				xcp->regs[MIPSInst_FT(ir)]);

1482
			MIPS_FPU_EMU_INC_STATS(loads);
1483
			if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
1484
				MIPS_FPU_EMU_INC_STATS(errors);
1485
				*fault_addr = va;
L
Linus Torvalds 已提交
1486 1487
				return SIGBUS;
			}
1488 1489 1490 1491 1492
			if (__get_user(val, va)) {
				MIPS_FPU_EMU_INC_STATS(errors);
				*fault_addr = va;
				return SIGSEGV;
			}
L
Linus Torvalds 已提交
1493 1494 1495 1496
			SITOREG(val, MIPSInst_FD(ir));
			break;

		case swxc1_op:
1497
			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
L
Linus Torvalds 已提交
1498 1499
				xcp->regs[MIPSInst_FT(ir)]);

1500
			MIPS_FPU_EMU_INC_STATS(stores);
L
Linus Torvalds 已提交
1501 1502

			SIFROMREG(val, MIPSInst_FS(ir));
1503
			if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1504
				MIPS_FPU_EMU_INC_STATS(errors);
1505
				*fault_addr = va;
L
Linus Torvalds 已提交
1506 1507
				return SIGBUS;
			}
1508 1509 1510 1511 1512
			if (put_user(val, va)) {
				MIPS_FPU_EMU_INC_STATS(errors);
				*fault_addr = va;
				return SIGSEGV;
			}
L
Linus Torvalds 已提交
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
			break;

		case madd_s_op:
			handler = fpemu_sp_madd;
			goto scoptop;
		case msub_s_op:
			handler = fpemu_sp_msub;
			goto scoptop;
		case nmadd_s_op:
			handler = fpemu_sp_nmadd;
			goto scoptop;
		case nmsub_s_op:
			handler = fpemu_sp_nmsub;
			goto scoptop;

		      scoptop:
			SPFROMREG(fr, MIPSInst_FR(ir));
			SPFROMREG(fs, MIPSInst_FS(ir));
			SPFROMREG(ft, MIPSInst_FT(ir));
			fd = (*handler) (fr, fs, ft);
			SPTOREG(fd, MIPSInst_FD(ir));

		      copcsr:
1536 1537
			if (ieee754_cxtest(IEEE754_INEXACT)) {
				MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
L
Linus Torvalds 已提交
1538
				rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1539 1540 1541
			}
			if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
				MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
L
Linus Torvalds 已提交
1542
				rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1543 1544 1545
			}
			if (ieee754_cxtest(IEEE754_OVERFLOW)) {
				MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
L
Linus Torvalds 已提交
1546
				rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1547 1548 1549
			}
			if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
				MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
L
Linus Torvalds 已提交
1550
				rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1551
			}
L
Linus Torvalds 已提交
1552 1553 1554

			ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
			if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1555
				/*printk ("SIGFPE: FPU csr = %08x\n",
L
Linus Torvalds 已提交
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
				   ctx->fcr31); */
				return SIGFPE;
			}

			break;

		default:
			return SIGILL;
		}
		break;
	}

	case d_fmt:{		/* 1 */
1569 1570
		union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
		union ieee754dp fd, fr, fs, ft;
1571
		u64 __user *va;
L
Linus Torvalds 已提交
1572 1573 1574 1575
		u64 val;

		switch (MIPSInst_FUNC(ir)) {
		case ldxc1_op:
1576
			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
L
Linus Torvalds 已提交
1577 1578
				xcp->regs[MIPSInst_FT(ir)]);

1579
			MIPS_FPU_EMU_INC_STATS(loads);
1580
			if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
1581
				MIPS_FPU_EMU_INC_STATS(errors);
1582
				*fault_addr = va;
L
Linus Torvalds 已提交
1583 1584
				return SIGBUS;
			}
1585 1586 1587 1588 1589
			if (__get_user(val, va)) {
				MIPS_FPU_EMU_INC_STATS(errors);
				*fault_addr = va;
				return SIGSEGV;
			}
L
Linus Torvalds 已提交
1590 1591 1592 1593
			DITOREG(val, MIPSInst_FD(ir));
			break;

		case sdxc1_op:
1594
			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
L
Linus Torvalds 已提交
1595 1596
				xcp->regs[MIPSInst_FT(ir)]);

1597
			MIPS_FPU_EMU_INC_STATS(stores);
L
Linus Torvalds 已提交
1598
			DIFROMREG(val, MIPSInst_FS(ir));
1599
			if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
1600
				MIPS_FPU_EMU_INC_STATS(errors);
1601
				*fault_addr = va;
L
Linus Torvalds 已提交
1602 1603
				return SIGBUS;
			}
1604 1605 1606 1607 1608
			if (__put_user(val, va)) {
				MIPS_FPU_EMU_INC_STATS(errors);
				*fault_addr = va;
				return SIGSEGV;
			}
L
Linus Torvalds 已提交
1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
			break;

		case madd_d_op:
			handler = fpemu_dp_madd;
			goto dcoptop;
		case msub_d_op:
			handler = fpemu_dp_msub;
			goto dcoptop;
		case nmadd_d_op:
			handler = fpemu_dp_nmadd;
			goto dcoptop;
		case nmsub_d_op:
			handler = fpemu_dp_nmsub;
			goto dcoptop;

		      dcoptop:
			DPFROMREG(fr, MIPSInst_FR(ir));
			DPFROMREG(fs, MIPSInst_FS(ir));
			DPFROMREG(ft, MIPSInst_FT(ir));
			fd = (*handler) (fr, fs, ft);
			DPTOREG(fd, MIPSInst_FD(ir));
			goto copcsr;

		default:
			return SIGILL;
		}
		break;
	}

1638 1639
	case 0x3:
		if (MIPSInst_FUNC(ir) != pfetch_op)
L
Linus Torvalds 已提交
1640
			return SIGILL;
1641

L
Linus Torvalds 已提交
1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
		/* ignore prefx operation */
		break;

	default:
		return SIGILL;
	}

	return 0;
}



/*
 * Emulate a single COP1 arithmetic instruction.
 */
1657
static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
L
Linus Torvalds 已提交
1658 1659 1660 1661
	mips_instruction ir)
{
	int rfmt;		/* resulting format */
	unsigned rcsr = 0;	/* resulting csr */
1662 1663
	unsigned int oldrm;
	unsigned int cbit;
L
Linus Torvalds 已提交
1664 1665
	unsigned cond;
	union {
1666 1667
		union ieee754dp d;
		union ieee754sp s;
L
Linus Torvalds 已提交
1668 1669 1670
		int w;
		s64 l;
	} rv;			/* resulting value */
1671
	u64 bits;
L
Linus Torvalds 已提交
1672

1673
	MIPS_FPU_EMU_INC_STATS(cp1ops);
L
Linus Torvalds 已提交
1674
	switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
1675
	case s_fmt: {		/* 0 */
L
Linus Torvalds 已提交
1676
		union {
1677 1678
			union ieee754sp(*b) (union ieee754sp, union ieee754sp);
			union ieee754sp(*u) (union ieee754sp);
L
Linus Torvalds 已提交
1679
		} handler;
1680
		union ieee754sp fd, fs, ft;
L
Linus Torvalds 已提交
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698

		switch (MIPSInst_FUNC(ir)) {
			/* binary ops */
		case fadd_op:
			handler.b = ieee754sp_add;
			goto scopbop;
		case fsub_op:
			handler.b = ieee754sp_sub;
			goto scopbop;
		case fmul_op:
			handler.b = ieee754sp_mul;
			goto scopbop;
		case fdiv_op:
			handler.b = ieee754sp_div;
			goto scopbop;

			/* unary  ops */
		case fsqrt_op:
1699
			if (!cpu_has_mips_2_3_4_5_r)
1700 1701
				return SIGILL;

L
Linus Torvalds 已提交
1702 1703
			handler.u = ieee754sp_sqrt;
			goto scopuop;
1704

1705 1706 1707 1708 1709
		/*
		 * Note that on some MIPS IV implementations such as the
		 * R5000 and R8000 the FSQRT and FRECIP instructions do not
		 * achieve full IEEE-754 accuracy - however this emulator does.
		 */
L
Linus Torvalds 已提交
1710
		case frsqrt_op:
1711
			if (!cpu_has_mips_4_5_64_r2_r6)
1712 1713
				return SIGILL;

L
Linus Torvalds 已提交
1714 1715
			handler.u = fpemu_sp_rsqrt;
			goto scopuop;
1716

L
Linus Torvalds 已提交
1717
		case frecip_op:
1718
			if (!cpu_has_mips_4_5_64_r2_r6)
1719 1720
				return SIGILL;

L
Linus Torvalds 已提交
1721 1722
			handler.u = fpemu_sp_recip;
			goto scopuop;
1723

L
Linus Torvalds 已提交
1724
		case fmovc_op:
1725 1726 1727
			if (!cpu_has_mips_4_5_r)
				return SIGILL;

L
Linus Torvalds 已提交
1728 1729 1730 1731 1732 1733
			cond = fpucondbit[MIPSInst_FT(ir) >> 2];
			if (((ctx->fcr31 & cond) != 0) !=
				((MIPSInst_FT(ir) & 1) != 0))
				return 0;
			SPFROMREG(rv.s, MIPSInst_FS(ir));
			break;
1734

L
Linus Torvalds 已提交
1735
		case fmovz_op:
1736 1737 1738
			if (!cpu_has_mips_4_5_r)
				return SIGILL;

L
Linus Torvalds 已提交
1739 1740 1741 1742
			if (xcp->regs[MIPSInst_FT(ir)] != 0)
				return 0;
			SPFROMREG(rv.s, MIPSInst_FS(ir));
			break;
1743

L
Linus Torvalds 已提交
1744
		case fmovn_op:
1745 1746 1747
			if (!cpu_has_mips_4_5_r)
				return SIGILL;

L
Linus Torvalds 已提交
1748 1749 1750 1751
			if (xcp->regs[MIPSInst_FT(ir)] == 0)
				return 0;
			SPFROMREG(rv.s, MIPSInst_FS(ir));
			break;
1752

1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763
		case fseleqz_op:
			if (!cpu_has_mips_r6)
				return SIGILL;

			SPFROMREG(rv.s, MIPSInst_FT(ir));
			if (rv.w & 0x1)
				rv.w = 0;
			else
				SPFROMREG(rv.s, MIPSInst_FS(ir));
			break;

1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774
		case fselnez_op:
			if (!cpu_has_mips_r6)
				return SIGILL;

			SPFROMREG(rv.s, MIPSInst_FT(ir));
			if (rv.w & 0x1)
				SPFROMREG(rv.s, MIPSInst_FS(ir));
			else
				rv.w = 0;
			break;

1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787
		case fmaddf_op: {
			union ieee754sp ft, fs, fd;

			if (!cpu_has_mips_r6)
				return SIGILL;

			SPFROMREG(ft, MIPSInst_FT(ir));
			SPFROMREG(fs, MIPSInst_FS(ir));
			SPFROMREG(fd, MIPSInst_FD(ir));
			rv.s = ieee754sp_maddf(fd, fs, ft);
			break;
		}

1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800
		case fmsubf_op: {
			union ieee754sp ft, fs, fd;

			if (!cpu_has_mips_r6)
				return SIGILL;

			SPFROMREG(ft, MIPSInst_FT(ir));
			SPFROMREG(fs, MIPSInst_FS(ir));
			SPFROMREG(fd, MIPSInst_FD(ir));
			rv.s = ieee754sp_msubf(fd, fs, ft);
			break;
		}

1801 1802 1803 1804 1805 1806 1807
		case frint_op: {
			union ieee754sp fs;

			if (!cpu_has_mips_r6)
				return SIGILL;

			SPFROMREG(fs, MIPSInst_FS(ir));
1808
			rv.s = ieee754sp_rint(fs);
1809 1810 1811
			goto copcsr;
		}

1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823
		case fclass_op: {
			union ieee754sp fs;

			if (!cpu_has_mips_r6)
				return SIGILL;

			SPFROMREG(fs, MIPSInst_FS(ir));
			rv.w = ieee754sp_2008class(fs);
			rfmt = w_fmt;
			break;
		}

1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847
		case fmin_op: {
			union ieee754sp fs, ft;

			if (!cpu_has_mips_r6)
				return SIGILL;

			SPFROMREG(ft, MIPSInst_FT(ir));
			SPFROMREG(fs, MIPSInst_FS(ir));
			rv.s = ieee754sp_fmin(fs, ft);
			break;
		}

		case fmina_op: {
			union ieee754sp fs, ft;

			if (!cpu_has_mips_r6)
				return SIGILL;

			SPFROMREG(ft, MIPSInst_FT(ir));
			SPFROMREG(fs, MIPSInst_FS(ir));
			rv.s = ieee754sp_fmina(fs, ft);
			break;
		}

1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871
		case fmax_op: {
			union ieee754sp fs, ft;

			if (!cpu_has_mips_r6)
				return SIGILL;

			SPFROMREG(ft, MIPSInst_FT(ir));
			SPFROMREG(fs, MIPSInst_FS(ir));
			rv.s = ieee754sp_fmax(fs, ft);
			break;
		}

		case fmaxa_op: {
			union ieee754sp fs, ft;

			if (!cpu_has_mips_r6)
				return SIGILL;

			SPFROMREG(ft, MIPSInst_FT(ir));
			SPFROMREG(fs, MIPSInst_FS(ir));
			rv.s = ieee754sp_fmaxa(fs, ft);
			break;
		}

L
Linus Torvalds 已提交
1872 1873 1874
		case fabs_op:
			handler.u = ieee754sp_abs;
			goto scopuop;
1875

L
Linus Torvalds 已提交
1876 1877 1878
		case fneg_op:
			handler.u = ieee754sp_neg;
			goto scopuop;
1879

L
Linus Torvalds 已提交
1880 1881 1882 1883 1884 1885
		case fmov_op:
			/* an easy one */
			SPFROMREG(rv.s, MIPSInst_FS(ir));
			goto copcsr;

			/* binary op on handler */
1886 1887 1888
scopbop:
			SPFROMREG(fs, MIPSInst_FS(ir));
			SPFROMREG(ft, MIPSInst_FT(ir));
L
Linus Torvalds 已提交
1889

1890 1891 1892 1893 1894 1895 1896
			rv.s = (*handler.b) (fs, ft);
			goto copcsr;
scopuop:
			SPFROMREG(fs, MIPSInst_FS(ir));
			rv.s = (*handler.u) (fs);
			goto copcsr;
copcsr:
1897 1898
			if (ieee754_cxtest(IEEE754_INEXACT)) {
				MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
L
Linus Torvalds 已提交
1899
				rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1900 1901 1902
			}
			if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
				MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
L
Linus Torvalds 已提交
1903
				rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1904 1905 1906
			}
			if (ieee754_cxtest(IEEE754_OVERFLOW)) {
				MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
L
Linus Torvalds 已提交
1907
				rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1908 1909 1910
			}
			if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) {
				MIPS_FPU_EMU_INC_STATS(ieee754_zerodiv);
L
Linus Torvalds 已提交
1911
				rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
1912 1913 1914
			}
			if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
				MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
L
Linus Torvalds 已提交
1915
				rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1916
			}
L
Linus Torvalds 已提交
1917 1918 1919 1920 1921 1922
			break;

			/* unary conv ops */
		case fcvts_op:
			return SIGILL;	/* not defined */

1923
		case fcvtd_op:
L
Linus Torvalds 已提交
1924 1925 1926 1927 1928
			SPFROMREG(fs, MIPSInst_FS(ir));
			rv.d = ieee754dp_fsp(fs);
			rfmt = d_fmt;
			goto copcsr;

1929
		case fcvtw_op:
L
Linus Torvalds 已提交
1930 1931 1932 1933 1934 1935 1936 1937
			SPFROMREG(fs, MIPSInst_FS(ir));
			rv.w = ieee754sp_tint(fs);
			rfmt = w_fmt;
			goto copcsr;

		case fround_op:
		case ftrunc_op:
		case fceil_op:
1938
		case ffloor_op:
1939
			if (!cpu_has_mips_2_3_4_5_r)
1940 1941
				return SIGILL;

1942
			oldrm = ieee754_csr.rm;
L
Linus Torvalds 已提交
1943
			SPFROMREG(fs, MIPSInst_FS(ir));
1944
			ieee754_csr.rm = MIPSInst_FUNC(ir);
L
Linus Torvalds 已提交
1945 1946 1947 1948 1949
			rv.w = ieee754sp_tint(fs);
			ieee754_csr.rm = oldrm;
			rfmt = w_fmt;
			goto copcsr;

1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
		case fsel_op:
			if (!cpu_has_mips_r6)
				return SIGILL;

			SPFROMREG(fd, MIPSInst_FD(ir));
			if (fd.bits & 0x1)
				SPFROMREG(rv.s, MIPSInst_FT(ir));
			else
				SPFROMREG(rv.s, MIPSInst_FS(ir));
			break;

1961
		case fcvtl_op:
1962
			if (!cpu_has_mips_3_4_5_64_r2_r6)
1963 1964
				return SIGILL;

L
Linus Torvalds 已提交
1965 1966 1967 1968 1969 1970 1971 1972
			SPFROMREG(fs, MIPSInst_FS(ir));
			rv.l = ieee754sp_tlong(fs);
			rfmt = l_fmt;
			goto copcsr;

		case froundl_op:
		case ftruncl_op:
		case fceill_op:
1973
		case ffloorl_op:
1974
			if (!cpu_has_mips_3_4_5_64_r2_r6)
1975 1976
				return SIGILL;

1977
			oldrm = ieee754_csr.rm;
L
Linus Torvalds 已提交
1978
			SPFROMREG(fs, MIPSInst_FS(ir));
1979
			ieee754_csr.rm = MIPSInst_FUNC(ir);
L
Linus Torvalds 已提交
1980 1981 1982 1983 1984 1985
			rv.l = ieee754sp_tlong(fs);
			ieee754_csr.rm = oldrm;
			rfmt = l_fmt;
			goto copcsr;

		default:
1986
			if (!NO_R6EMU && MIPSInst_FUNC(ir) >= fcmp_op) {
L
Linus Torvalds 已提交
1987
				unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
1988
				union ieee754sp fs, ft;
L
Linus Torvalds 已提交
1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000

				SPFROMREG(fs, MIPSInst_FS(ir));
				SPFROMREG(ft, MIPSInst_FT(ir));
				rv.w = ieee754sp_cmp(fs, ft,
					cmptab[cmpop & 0x7], cmpop & 0x8);
				rfmt = -1;
				if ((cmpop & 0x8) && ieee754_cxtest
					(IEEE754_INVALID_OPERATION))
					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
				else
					goto copcsr;

2001
			} else
L
Linus Torvalds 已提交
2002 2003 2004 2005 2006 2007
				return SIGILL;
			break;
		}
		break;
	}

2008
	case d_fmt: {
2009
		union ieee754dp fd, fs, ft;
L
Linus Torvalds 已提交
2010
		union {
2011 2012
			union ieee754dp(*b) (union ieee754dp, union ieee754dp);
			union ieee754dp(*u) (union ieee754dp);
L
Linus Torvalds 已提交
2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031
		} handler;

		switch (MIPSInst_FUNC(ir)) {
			/* binary ops */
		case fadd_op:
			handler.b = ieee754dp_add;
			goto dcopbop;
		case fsub_op:
			handler.b = ieee754dp_sub;
			goto dcopbop;
		case fmul_op:
			handler.b = ieee754dp_mul;
			goto dcopbop;
		case fdiv_op:
			handler.b = ieee754dp_div;
			goto dcopbop;

			/* unary  ops */
		case fsqrt_op:
2032 2033 2034
			if (!cpu_has_mips_2_3_4_5_r)
				return SIGILL;

L
Linus Torvalds 已提交
2035 2036
			handler.u = ieee754dp_sqrt;
			goto dcopuop;
2037 2038 2039 2040 2041
		/*
		 * Note that on some MIPS IV implementations such as the
		 * R5000 and R8000 the FSQRT and FRECIP instructions do not
		 * achieve full IEEE-754 accuracy - however this emulator does.
		 */
L
Linus Torvalds 已提交
2042
		case frsqrt_op:
2043
			if (!cpu_has_mips_4_5_64_r2_r6)
2044 2045
				return SIGILL;

L
Linus Torvalds 已提交
2046 2047 2048
			handler.u = fpemu_dp_rsqrt;
			goto dcopuop;
		case frecip_op:
2049
			if (!cpu_has_mips_4_5_64_r2_r6)
2050 2051
				return SIGILL;

L
Linus Torvalds 已提交
2052 2053 2054
			handler.u = fpemu_dp_recip;
			goto dcopuop;
		case fmovc_op:
2055 2056 2057
			if (!cpu_has_mips_4_5_r)
				return SIGILL;

L
Linus Torvalds 已提交
2058 2059 2060 2061 2062 2063 2064
			cond = fpucondbit[MIPSInst_FT(ir) >> 2];
			if (((ctx->fcr31 & cond) != 0) !=
				((MIPSInst_FT(ir) & 1) != 0))
				return 0;
			DPFROMREG(rv.d, MIPSInst_FS(ir));
			break;
		case fmovz_op:
2065 2066 2067
			if (!cpu_has_mips_4_5_r)
				return SIGILL;

L
Linus Torvalds 已提交
2068 2069 2070 2071 2072
			if (xcp->regs[MIPSInst_FT(ir)] != 0)
				return 0;
			DPFROMREG(rv.d, MIPSInst_FS(ir));
			break;
		case fmovn_op:
2073 2074 2075
			if (!cpu_has_mips_4_5_r)
				return SIGILL;

L
Linus Torvalds 已提交
2076 2077 2078 2079
			if (xcp->regs[MIPSInst_FT(ir)] == 0)
				return 0;
			DPFROMREG(rv.d, MIPSInst_FS(ir));
			break;
2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091

		case fseleqz_op:
			if (!cpu_has_mips_r6)
				return SIGILL;

			DPFROMREG(rv.d, MIPSInst_FT(ir));
			if (rv.l & 0x1)
				rv.l = 0;
			else
				DPFROMREG(rv.d, MIPSInst_FS(ir));
			break;

2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102
		case fselnez_op:
			if (!cpu_has_mips_r6)
				return SIGILL;

			DPFROMREG(rv.d, MIPSInst_FT(ir));
			if (rv.l & 0x1)
				DPFROMREG(rv.d, MIPSInst_FS(ir));
			else
				rv.l = 0;
			break;

2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115
		case fmaddf_op: {
			union ieee754dp ft, fs, fd;

			if (!cpu_has_mips_r6)
				return SIGILL;

			DPFROMREG(ft, MIPSInst_FT(ir));
			DPFROMREG(fs, MIPSInst_FS(ir));
			DPFROMREG(fd, MIPSInst_FD(ir));
			rv.d = ieee754dp_maddf(fd, fs, ft);
			break;
		}

2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128
		case fmsubf_op: {
			union ieee754dp ft, fs, fd;

			if (!cpu_has_mips_r6)
				return SIGILL;

			DPFROMREG(ft, MIPSInst_FT(ir));
			DPFROMREG(fs, MIPSInst_FS(ir));
			DPFROMREG(fd, MIPSInst_FD(ir));
			rv.d = ieee754dp_msubf(fd, fs, ft);
			break;
		}

2129 2130 2131 2132 2133 2134 2135
		case frint_op: {
			union ieee754dp fs;

			if (!cpu_has_mips_r6)
				return SIGILL;

			DPFROMREG(fs, MIPSInst_FS(ir));
2136
			rv.d = ieee754dp_rint(fs);
2137 2138 2139
			goto copcsr;
		}

2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151
		case fclass_op: {
			union ieee754dp fs;

			if (!cpu_has_mips_r6)
				return SIGILL;

			DPFROMREG(fs, MIPSInst_FS(ir));
			rv.w = ieee754dp_2008class(fs);
			rfmt = w_fmt;
			break;
		}

2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175
		case fmin_op: {
			union ieee754dp fs, ft;

			if (!cpu_has_mips_r6)
				return SIGILL;

			DPFROMREG(ft, MIPSInst_FT(ir));
			DPFROMREG(fs, MIPSInst_FS(ir));
			rv.d = ieee754dp_fmin(fs, ft);
			break;
		}

		case fmina_op: {
			union ieee754dp fs, ft;

			if (!cpu_has_mips_r6)
				return SIGILL;

			DPFROMREG(ft, MIPSInst_FT(ir));
			DPFROMREG(fs, MIPSInst_FS(ir));
			rv.d = ieee754dp_fmina(fs, ft);
			break;
		}

2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199
		case fmax_op: {
			union ieee754dp fs, ft;

			if (!cpu_has_mips_r6)
				return SIGILL;

			DPFROMREG(ft, MIPSInst_FT(ir));
			DPFROMREG(fs, MIPSInst_FS(ir));
			rv.d = ieee754dp_fmax(fs, ft);
			break;
		}

		case fmaxa_op: {
			union ieee754dp fs, ft;

			if (!cpu_has_mips_r6)
				return SIGILL;

			DPFROMREG(ft, MIPSInst_FT(ir));
			DPFROMREG(fs, MIPSInst_FS(ir));
			rv.d = ieee754dp_fmaxa(fs, ft);
			break;
		}

L
Linus Torvalds 已提交
2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213
		case fabs_op:
			handler.u = ieee754dp_abs;
			goto dcopuop;

		case fneg_op:
			handler.u = ieee754dp_neg;
			goto dcopuop;

		case fmov_op:
			/* an easy one */
			DPFROMREG(rv.d, MIPSInst_FS(ir));
			goto copcsr;

			/* binary op on handler */
2214 2215 2216
dcopbop:
			DPFROMREG(fs, MIPSInst_FS(ir));
			DPFROMREG(ft, MIPSInst_FT(ir));
L
Linus Torvalds 已提交
2217

2218 2219 2220 2221 2222 2223
			rv.d = (*handler.b) (fs, ft);
			goto copcsr;
dcopuop:
			DPFROMREG(fs, MIPSInst_FS(ir));
			rv.d = (*handler.u) (fs);
			goto copcsr;
L
Linus Torvalds 已提交
2224

2225 2226 2227 2228
		/*
		 * unary conv ops
		 */
		case fcvts_op:
L
Linus Torvalds 已提交
2229 2230 2231 2232
			DPFROMREG(fs, MIPSInst_FS(ir));
			rv.s = ieee754sp_fdp(fs);
			rfmt = s_fmt;
			goto copcsr;
2233

L
Linus Torvalds 已提交
2234 2235 2236
		case fcvtd_op:
			return SIGILL;	/* not defined */

2237
		case fcvtw_op:
L
Linus Torvalds 已提交
2238 2239 2240 2241 2242 2243 2244 2245
			DPFROMREG(fs, MIPSInst_FS(ir));
			rv.w = ieee754dp_tint(fs);	/* wrong */
			rfmt = w_fmt;
			goto copcsr;

		case fround_op:
		case ftrunc_op:
		case fceil_op:
2246
		case ffloor_op:
2247 2248 2249
			if (!cpu_has_mips_2_3_4_5_r)
				return SIGILL;

2250
			oldrm = ieee754_csr.rm;
L
Linus Torvalds 已提交
2251
			DPFROMREG(fs, MIPSInst_FS(ir));
2252
			ieee754_csr.rm = MIPSInst_FUNC(ir);
L
Linus Torvalds 已提交
2253 2254 2255 2256 2257
			rv.w = ieee754dp_tint(fs);
			ieee754_csr.rm = oldrm;
			rfmt = w_fmt;
			goto copcsr;

2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268
		case fsel_op:
			if (!cpu_has_mips_r6)
				return SIGILL;

			DPFROMREG(fd, MIPSInst_FD(ir));
			if (fd.bits & 0x1)
				DPFROMREG(rv.d, MIPSInst_FT(ir));
			else
				DPFROMREG(rv.d, MIPSInst_FS(ir));
			break;

2269
		case fcvtl_op:
2270
			if (!cpu_has_mips_3_4_5_64_r2_r6)
2271 2272
				return SIGILL;

L
Linus Torvalds 已提交
2273 2274 2275 2276 2277 2278 2279 2280
			DPFROMREG(fs, MIPSInst_FS(ir));
			rv.l = ieee754dp_tlong(fs);
			rfmt = l_fmt;
			goto copcsr;

		case froundl_op:
		case ftruncl_op:
		case fceill_op:
2281
		case ffloorl_op:
2282
			if (!cpu_has_mips_3_4_5_64_r2_r6)
2283 2284
				return SIGILL;

2285
			oldrm = ieee754_csr.rm;
L
Linus Torvalds 已提交
2286
			DPFROMREG(fs, MIPSInst_FS(ir));
2287
			ieee754_csr.rm = MIPSInst_FUNC(ir);
L
Linus Torvalds 已提交
2288 2289 2290 2291 2292 2293
			rv.l = ieee754dp_tlong(fs);
			ieee754_csr.rm = oldrm;
			rfmt = l_fmt;
			goto copcsr;

		default:
2294
			if (!NO_R6EMU && MIPSInst_FUNC(ir) >= fcmp_op) {
L
Linus Torvalds 已提交
2295
				unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
2296
				union ieee754dp fs, ft;
L
Linus Torvalds 已提交
2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317

				DPFROMREG(fs, MIPSInst_FS(ir));
				DPFROMREG(ft, MIPSInst_FT(ir));
				rv.w = ieee754dp_cmp(fs, ft,
					cmptab[cmpop & 0x7], cmpop & 0x8);
				rfmt = -1;
				if ((cmpop & 0x8)
					&&
					ieee754_cxtest
					(IEEE754_INVALID_OPERATION))
					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
				else
					goto copcsr;

			}
			else {
				return SIGILL;
			}
			break;
		}
		break;
2318 2319 2320 2321
	}

	case w_fmt: {
		union ieee754dp fs;
L
Linus Torvalds 已提交
2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335

		switch (MIPSInst_FUNC(ir)) {
		case fcvts_op:
			/* convert word to single precision real */
			SPFROMREG(fs, MIPSInst_FS(ir));
			rv.s = ieee754sp_fint(fs.bits);
			rfmt = s_fmt;
			goto copcsr;
		case fcvtd_op:
			/* convert word to double precision real */
			SPFROMREG(fs, MIPSInst_FS(ir));
			rv.d = ieee754dp_fint(fs.bits);
			rfmt = d_fmt;
			goto copcsr;
2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393
		default: {
			/* Emulating the new CMP.condn.fmt R6 instruction */
#define CMPOP_MASK	0x7
#define SIGN_BIT	(0x1 << 3)
#define PREDICATE_BIT	(0x1 << 4)

			int cmpop = MIPSInst_FUNC(ir) & CMPOP_MASK;
			int sig = MIPSInst_FUNC(ir) & SIGN_BIT;
			union ieee754sp fs, ft;

			/* This is an R6 only instruction */
			if (!cpu_has_mips_r6 ||
			    (MIPSInst_FUNC(ir) & 0x20))
				return SIGILL;

			/* fmt is w_fmt for single precision so fix it */
			rfmt = s_fmt;
			/* default to false */
			rv.w = 0;

			/* CMP.condn.S */
			SPFROMREG(fs, MIPSInst_FS(ir));
			SPFROMREG(ft, MIPSInst_FT(ir));

			/* positive predicates */
			if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
				if (ieee754sp_cmp(fs, ft, cmptab[cmpop],
						  sig))
				    rv.w = -1; /* true, all 1s */
				if ((sig) &&
				    ieee754_cxtest(IEEE754_INVALID_OPERATION))
					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
				else
					goto copcsr;
			} else {
				/* negative predicates */
				switch (cmpop) {
				case 1:
				case 2:
				case 3:
					if (ieee754sp_cmp(fs, ft,
							  negative_cmptab[cmpop],
							  sig))
						rv.w = -1; /* true, all 1s */
					if (sig &&
					    ieee754_cxtest(IEEE754_INVALID_OPERATION))
						rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
					else
						goto copcsr;
					break;
				default:
					/* Reserved R6 ops */
					pr_err("Reserved MIPS R6 CMP.condn.S operation\n");
					return SIGILL;
				}
			}
			break;
			}
L
Linus Torvalds 已提交
2394
		}
2395
		break;
L
Linus Torvalds 已提交
2396 2397
	}

2398
	case l_fmt:
2399

2400
		if (!cpu_has_mips_3_4_5_64_r2_r6)
2401 2402
			return SIGILL;

P
Paul Burton 已提交
2403 2404
		DIFROMREG(bits, MIPSInst_FS(ir));

L
Linus Torvalds 已提交
2405 2406 2407
		switch (MIPSInst_FUNC(ir)) {
		case fcvts_op:
			/* convert long to single precision real */
P
Paul Burton 已提交
2408
			rv.s = ieee754sp_flong(bits);
L
Linus Torvalds 已提交
2409 2410 2411 2412
			rfmt = s_fmt;
			goto copcsr;
		case fcvtd_op:
			/* convert long to double precision real */
P
Paul Burton 已提交
2413
			rv.d = ieee754dp_flong(bits);
L
Linus Torvalds 已提交
2414 2415
			rfmt = d_fmt;
			goto copcsr;
2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433
		default: {
			/* Emulating the new CMP.condn.fmt R6 instruction */
			int cmpop = MIPSInst_FUNC(ir) & CMPOP_MASK;
			int sig = MIPSInst_FUNC(ir) & SIGN_BIT;
			union ieee754dp fs, ft;

			if (!cpu_has_mips_r6 ||
			    (MIPSInst_FUNC(ir) & 0x20))
				return SIGILL;

			/* fmt is l_fmt for double precision so fix it */
			rfmt = d_fmt;
			/* default to false */
			rv.l = 0;

			/* CMP.condn.D */
			DPFROMREG(fs, MIPSInst_FS(ir));
			DPFROMREG(ft, MIPSInst_FT(ir));
L
Linus Torvalds 已提交
2434

2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469
			/* positive predicates */
			if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
				if (ieee754dp_cmp(fs, ft,
						  cmptab[cmpop], sig))
				    rv.l = -1LL; /* true, all 1s */
				if (sig &&
				    ieee754_cxtest(IEEE754_INVALID_OPERATION))
					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
				else
					goto copcsr;
			} else {
				/* negative predicates */
				switch (cmpop) {
				case 1:
				case 2:
				case 3:
					if (ieee754dp_cmp(fs, ft,
							  negative_cmptab[cmpop],
							  sig))
						rv.l = -1LL; /* true, all 1s */
					if (sig &&
					    ieee754_cxtest(IEEE754_INVALID_OPERATION))
						rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
					else
						goto copcsr;
					break;
				default:
					/* Reserved R6 ops */
					pr_err("Reserved MIPS R6 CMP.condn.D operation\n");
					return SIGILL;
				}
			}
			break;
			}
		}
2470 2471
		break;

L
Linus Torvalds 已提交
2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484
	default:
		return SIGILL;
	}

	/*
	 * Update the fpu CSR register for this operation.
	 * If an exception is required, generate a tidy SIGFPE exception,
	 * without updating the result register.
	 * Note: cause exception bits do not accumulate, they are rewritten
	 * for each op; only the flag/sticky bits accumulate.
	 */
	ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
	if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
2485
		/*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
L
Linus Torvalds 已提交
2486 2487 2488 2489 2490 2491 2492
		return SIGFPE;
	}

	/*
	 * Now we can safely write the result back to the register file.
	 */
	switch (rfmt) {
2493 2494 2495
	case -1:

		if (cpu_has_mips_4_5_r)
2496
			cbit = fpucondbit[MIPSInst_FD(ir) >> 2];
2497 2498
		else
			cbit = FPU_CSR_COND;
L
Linus Torvalds 已提交
2499
		if (rv.w)
2500
			ctx->fcr31 |= cbit;
L
Linus Torvalds 已提交
2501
		else
2502
			ctx->fcr31 &= ~cbit;
L
Linus Torvalds 已提交
2503
		break;
2504

L
Linus Torvalds 已提交
2505 2506 2507 2508 2509 2510 2511 2512 2513 2514
	case d_fmt:
		DPTOREG(rv.d, MIPSInst_FD(ir));
		break;
	case s_fmt:
		SPTOREG(rv.s, MIPSInst_FD(ir));
		break;
	case w_fmt:
		SITOREG(rv.w, MIPSInst_FD(ir));
		break;
	case l_fmt:
2515
		if (!cpu_has_mips_3_4_5_64_r2_r6)
2516 2517
			return SIGILL;

L
Linus Torvalds 已提交
2518 2519 2520 2521 2522 2523 2524 2525 2526
		DITOREG(rv.l, MIPSInst_FD(ir));
		break;
	default:
		return SIGILL;
	}

	return 0;
}

2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555
/*
 * Emulate FPU instructions.
 *
 * If we use FPU hardware, then we have been typically called to handle
 * an unimplemented operation, such as where an operand is a NaN or
 * denormalized.  In that case exit the emulation loop after a single
 * iteration so as to let hardware execute any subsequent instructions.
 *
 * If we have no FPU hardware or it has been disabled, then continue
 * emulating floating-point instructions until one of these conditions
 * has occurred:
 *
 * - a non-FPU instruction has been encountered,
 *
 * - an attempt to emulate has ended with a signal,
 *
 * - the ISA mode has been switched.
 *
 * We need to terminate the emulation loop if we got switched to the
 * MIPS16 mode, whether supported or not, so that we do not attempt
 * to emulate a MIPS16 instruction as a regular MIPS FPU instruction.
 * Similarly if we got switched to the microMIPS mode and only the
 * regular MIPS mode is supported, so that we do not attempt to emulate
 * a microMIPS instruction as a regular MIPS FPU instruction.  Or if
 * we got switched to the regular MIPS mode and only the microMIPS mode
 * is supported, so that we do not attempt to emulate a regular MIPS
 * instruction that should cause an Address Error exception instead.
 * For simplicity we always terminate upon an ISA mode switch.
 */
2556
int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
2557
	int has_fpu, void __user **fault_addr)
L
Linus Torvalds 已提交
2558
{
2559
	unsigned long oldepc, prevepc;
2560 2561 2562
	struct mm_decoded_insn dec_insn;
	u16 instr[4];
	u16 *instr_ptr;
L
Linus Torvalds 已提交
2563 2564 2565 2566 2567 2568
	int sig = 0;

	oldepc = xcp->cp0_epc;
	do {
		prevepc = xcp->cp0_epc;

2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622
		if (get_isa16_mode(prevepc) && cpu_has_mmips) {
			/*
			 * Get next 2 microMIPS instructions and convert them
			 * into 32-bit instructions.
			 */
			if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
			    (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
			    (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
			    (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
				MIPS_FPU_EMU_INC_STATS(errors);
				return SIGBUS;
			}
			instr_ptr = instr;

			/* Get first instruction. */
			if (mm_insn_16bit(*instr_ptr)) {
				/* Duplicate the half-word. */
				dec_insn.insn = (*instr_ptr << 16) |
					(*instr_ptr);
				/* 16-bit instruction. */
				dec_insn.pc_inc = 2;
				instr_ptr += 1;
			} else {
				dec_insn.insn = (*instr_ptr << 16) |
					*(instr_ptr+1);
				/* 32-bit instruction. */
				dec_insn.pc_inc = 4;
				instr_ptr += 2;
			}
			/* Get second instruction. */
			if (mm_insn_16bit(*instr_ptr)) {
				/* Duplicate the half-word. */
				dec_insn.next_insn = (*instr_ptr << 16) |
					(*instr_ptr);
				/* 16-bit instruction. */
				dec_insn.next_pc_inc = 2;
			} else {
				dec_insn.next_insn = (*instr_ptr << 16) |
					*(instr_ptr+1);
				/* 32-bit instruction. */
				dec_insn.next_pc_inc = 4;
			}
			dec_insn.micro_mips_mode = 1;
		} else {
			if ((get_user(dec_insn.insn,
			    (mips_instruction __user *) xcp->cp0_epc)) ||
			    (get_user(dec_insn.next_insn,
			    (mips_instruction __user *)(xcp->cp0_epc+4)))) {
				MIPS_FPU_EMU_INC_STATS(errors);
				return SIGBUS;
			}
			dec_insn.pc_inc = 4;
			dec_insn.next_pc_inc = 4;
			dec_insn.micro_mips_mode = 0;
2623
		}
2624 2625 2626 2627 2628

		if ((dec_insn.insn == 0) ||
		   ((dec_insn.pc_inc == 2) &&
		   ((dec_insn.insn & 0xffff) == MM_NOP16)))
			xcp->cp0_epc += dec_insn.pc_inc;	/* Skip NOPs */
L
Linus Torvalds 已提交
2629
		else {
2630
			/*
2631 2632
			 * The 'ieee754_csr' is an alias of ctx->fcr31.
			 * No need to copy ctx->fcr31 to ieee754_csr.
2633
			 */
2634
			sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
L
Linus Torvalds 已提交
2635 2636
		}

2637
		if (has_fpu)
L
Linus Torvalds 已提交
2638 2639 2640
			break;
		if (sig)
			break;
2641 2642 2643 2644 2645 2646 2647 2648 2649
		/*
		 * We have to check for the ISA bit explicitly here,
		 * because `get_isa16_mode' may return 0 if support
		 * for code compression has been globally disabled,
		 * or otherwise we may produce the wrong signal or
		 * even proceed successfully where we must not.
		 */
		if ((xcp->cp0_epc ^ prevepc) & 0x1)
			break;
L
Linus Torvalds 已提交
2650 2651 2652 2653 2654 2655

		cond_resched();
	} while (xcp->cp0_epc > prevepc);

	/* SIGILL indicates a non-fpu instruction */
	if (sig == SIGILL && xcp->cp0_epc != oldepc)
2656
		/* but if EPC has advanced, then ignore it */
L
Linus Torvalds 已提交
2657 2658 2659 2660
		sig = 0;

	return sig;
}