cp1emu.c 50.9 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/kconfig.h>
39
#include <linux/percpu-defs.h>
40
#include <linux/perf_event.h>
L
Linus Torvalds 已提交
41

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

#include <asm/processor.h>
L
Linus Torvalds 已提交
49
#include <asm/fpu_emulator.h>
50
#include <asm/fpu.h>
L
Linus Torvalds 已提交
51 52 53 54 55

#include "ieee754.h"

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

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

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

/* Control registers */

#define FPCREG_RID	0	/* $0  = revision id */
#define FPCREG_CSR	31	/* $31 = csr */

67 68 69
/* Determine rounding mode from the RM bits of the FCSR */
#define modeindex(v) ((v) & FPU_CSR_RM)

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

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 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
/* (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:
380 381
			case mm_mfhc1_op:
			case mm_mthc1_op:
382 383 384 385 386 387
				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;
388
				else if (insn.mm_fp1_format.op == mm_ctc1_op)
389
					op = ctc_op;
390 391 392 393
				else if (insn.mm_fp1_format.op == mm_mfhc1_op)
					op = mfhc_op;
				else
					op = mthc_op;
394 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
				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 已提交
429 430 431 432 433 434
/*
 * 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.
 */
435 436
static int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
			 unsigned long *contpc)
L
Linus Torvalds 已提交
437
{
438 439 440 441 442
	union mips_instruction insn = (union mips_instruction)dec_insn.insn;
	unsigned int fcr31;
	unsigned int bit = 0;

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

466 467 468 469
			regs->regs[31] = regs->cp0_epc +
				dec_insn.pc_inc +
				dec_insn.next_pc_inc;
			/* Fall through */
L
Linus Torvalds 已提交
470
		case bltzl_op:
471 472 473
			if (NO_R6EMU)
				break;
		case bltz_op:
474 475 476 477 478 479 480 481 482
			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 已提交
483 484
		case bgezal_op:
		case bgezall_op:
485 486 487 488
			if (NO_R6EMU && (insn.i_format.rs ||
			    insn.i_format.rt == bgezall_op))
				break;

489 490 491 492 493
			regs->regs[31] = regs->cp0_epc +
				dec_insn.pc_inc +
				dec_insn.next_pc_inc;
			/* Fall through */
		case bgezl_op:
494 495 496
			if (NO_R6EMU)
				break;
		case bgez_op:
497 498 499 500 501 502 503 504
			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 已提交
505 506 507 508
			return 1;
		}
		break;
	case jalx_op:
509 510 511 512 513 514 515 516 517 518 519 520 521 522
		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 已提交
523
	case beql_op:
524 525 526
		if (NO_R6EMU)
			break;
	case beq_op:
527 528 529 530 531 532 533 534 535 536
		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 已提交
537
	case bnel_op:
538 539 540
		if (NO_R6EMU)
			break;
	case bne_op:
541 542 543 544 545 546 547 548 549 550
		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 已提交
551
	case blezl_op:
552 553 554
		if (NO_R6EMU)
			break;
	case blez_op:
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578

		/*
		 * 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;
		}
579 580 581 582 583 584 585 586 587
		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 已提交
588
	case bgtzl_op:
589 590 591
		if (NO_R6EMU)
			break;
	case bgtz_op:
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
		/*
		 * 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;
		}

617 618 619 620 621 622 623 624
		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 已提交
625
		return 1;
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
#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;
651 652 653 654 655 656 657 658 659 660 661 662 663
#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;

		return 1;
664
#endif
L
Linus Torvalds 已提交
665 666
	case cop0_op:
	case cop1_op:
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
		/* 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;
			switch (insn.i_format.rs) {
			case bc1eqz_op:
				if (get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1)
				    bit = 1;
				break;
			case bc1nez_op:
				if (!(get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1))
				    bit = 1;
				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 已提交
694 695
	case cop2_op:
	case cop1x_op:
696 697 698
		if (insn.i_format.rs == bc_op) {
			preempt_disable();
			if (is_fpu_owner())
699
			        fcr31 = read_32bit_cp1_register(CP1_STATUS);
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
			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 已提交
732 733 734 735 736 737 738
		break;
	}
	return 0;
}

/*
 * In the Linux kernel, we support selection of FPR format on the
R
Ralf Baechle 已提交
739
 * basis of the Status.FR bit.	If an FPU is not present, the FR bit
740
 * is hardwired to zero, which would imply a 32-bit FPU even for
741
 * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
742 743 744
 * 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
745
 * compatibility enabled and on 32-bit without 64-bit FPU support.
L
Linus Torvalds 已提交
746
 */
747 748
static inline int cop1_64bit(struct pt_regs *xcp)
{
749 750 751 752 753 754
	if (config_enabled(CONFIG_64BIT) && !config_enabled(CONFIG_MIPS32_O32))
		return 1;
	else if (config_enabled(CONFIG_32BIT) &&
		 !config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
		return 0;

755
	return !test_thread_flag(TIF_32BIT_FPREGS);
756 757
}

P
Paul Burton 已提交
758 759 760 761 762
static inline bool hybrid_fprs(void)
{
	return test_thread_flag(TIF_HYBRID_FPREGS);
}

763 764
#define SIFROMREG(si, x)						\
do {									\
P
Paul Burton 已提交
765
	if (cop1_64bit(xcp) && !hybrid_fprs())				\
766
		(si) = (int)get_fpr32(&ctx->fpr[x], 0);			\
P
Paul Burton 已提交
767
	else								\
768
		(si) = (int)get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1);	\
P
Paul Burton 已提交
769
} while (0)
L
Linus Torvalds 已提交
770

771 772
#define SITOREG(si, x)							\
do {									\
P
Paul Burton 已提交
773
	if (cop1_64bit(xcp) && !hybrid_fprs()) {			\
774
		unsigned i;						\
P
Paul Burton 已提交
775
		set_fpr32(&ctx->fpr[x], 0, si);				\
776 777 778
		for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++)	\
			set_fpr32(&ctx->fpr[x], i, 0);			\
	} else {							\
P
Paul Burton 已提交
779
		set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si);		\
780
	}								\
P
Paul Burton 已提交
781
} while (0)
L
Linus Torvalds 已提交
782

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

785 786
#define SITOHREG(si, x)							\
do {									\
787 788 789 790 791
	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)
792

793
#define DIFROMREG(di, x)						\
P
Paul Burton 已提交
794 795
	((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0))

796 797
#define DITOREG(di, x)							\
do {									\
798 799 800 801 802 803
	unsigned fpr, i;						\
	fpr = (x) & ~(cop1_64bit(xcp) == 0);				\
	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 已提交
804

805 806 807 808
#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 已提交
809 810 811 812 813 814

/*
 * Emulate the single floating point instruction pointed at by EPC.
 * Two instructions if the instruction is in a branch delay slot.
 */

815
static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
816
		struct mm_decoded_insn dec_insn, void *__user *fault_addr)
L
Linus Torvalds 已提交
817
{
818
	unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
819 820 821 822 823 824 825 826 827
	unsigned int cond, cbit;
	mips_instruction ir;
	int likely, pc_inc;
	u32 __user *wva;
	u64 __user *dva;
	u32 value;
	u32 wval;
	u64 dval;
	int sig;
L
Linus Torvalds 已提交
828

829 830 831 832 833 834 835
	/*
	 * 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 已提交
836
	/* XXX NEC Vr54xx bug workaround */
837
	if (delay_slot(xcp)) {
838 839
		if (dec_insn.micro_mips_mode) {
			if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
840
				clear_delay_slot(xcp);
841 842
		} else {
			if (!isBranchInstr(xcp, dec_insn, &contpc))
843
				clear_delay_slot(xcp);
844 845
		}
	}
L
Linus Torvalds 已提交
846

847
	if (delay_slot(xcp)) {
L
Linus Torvalds 已提交
848 849
		/*
		 * The instruction to be emulated is in a branch delay slot
R
Ralf Baechle 已提交
850
		 * which means that we have to	emulate the branch instruction
L
Linus Torvalds 已提交
851 852 853 854 855 856 857 858 859
		 * 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.
		 */
860 861 862 863 864 865
		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 已提交
866

867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
	/*
	 * 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 已提交
886 887 888
			return SIGILL;
	}

889
emul:
890
	perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
891
	MIPS_FPU_EMU_INC_STATS(emulated);
L
Linus Torvalds 已提交
892
	switch (MIPSInst_OPCODE(ir)) {
893 894 895
	case ldc1_op:
		dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
				     MIPSInst_SIMM(ir));
896
		MIPS_FPU_EMU_INC_STATS(loads);
897

898
		if (!access_ok(VERIFY_READ, dva, sizeof(u64))) {
899
			MIPS_FPU_EMU_INC_STATS(errors);
900
			*fault_addr = dva;
L
Linus Torvalds 已提交
901 902
			return SIGBUS;
		}
903
		if (__get_user(dval, dva)) {
904
			MIPS_FPU_EMU_INC_STATS(errors);
905
			*fault_addr = dva;
906 907
			return SIGSEGV;
		}
908
		DITOREG(dval, MIPSInst_RT(ir));
L
Linus Torvalds 已提交
909 910
		break;

911 912 913
	case sdc1_op:
		dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
				      MIPSInst_SIMM(ir));
914
		MIPS_FPU_EMU_INC_STATS(stores);
915 916
		DIFROMREG(dval, MIPSInst_RT(ir));
		if (!access_ok(VERIFY_WRITE, dva, sizeof(u64))) {
917
			MIPS_FPU_EMU_INC_STATS(errors);
918
			*fault_addr = dva;
L
Linus Torvalds 已提交
919 920
			return SIGBUS;
		}
921
		if (__put_user(dval, dva)) {
922
			MIPS_FPU_EMU_INC_STATS(errors);
923
			*fault_addr = dva;
924 925
			return SIGSEGV;
		}
L
Linus Torvalds 已提交
926 927
		break;

928 929 930
	case lwc1_op:
		wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
				      MIPSInst_SIMM(ir));
931
		MIPS_FPU_EMU_INC_STATS(loads);
932
		if (!access_ok(VERIFY_READ, wva, sizeof(u32))) {
933
			MIPS_FPU_EMU_INC_STATS(errors);
934
			*fault_addr = wva;
L
Linus Torvalds 已提交
935 936
			return SIGBUS;
		}
937
		if (__get_user(wval, wva)) {
938
			MIPS_FPU_EMU_INC_STATS(errors);
939
			*fault_addr = wva;
940 941
			return SIGSEGV;
		}
942
		SITOREG(wval, MIPSInst_RT(ir));
L
Linus Torvalds 已提交
943 944
		break;

945 946 947
	case swc1_op:
		wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
				      MIPSInst_SIMM(ir));
948
		MIPS_FPU_EMU_INC_STATS(stores);
949 950
		SIFROMREG(wval, MIPSInst_RT(ir));
		if (!access_ok(VERIFY_WRITE, wva, sizeof(u32))) {
951
			MIPS_FPU_EMU_INC_STATS(errors);
952
			*fault_addr = wva;
L
Linus Torvalds 已提交
953 954
			return SIGBUS;
		}
955
		if (__put_user(wval, wva)) {
956
			MIPS_FPU_EMU_INC_STATS(errors);
957
			*fault_addr = wva;
958 959
			return SIGSEGV;
		}
L
Linus Torvalds 已提交
960 961 962 963 964
		break;

	case cop1_op:
		switch (MIPSInst_RS(ir)) {
		case dmfc_op:
965 966 967
			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
				return SIGILL;

L
Linus Torvalds 已提交
968 969 970 971 972 973 974 975
			/* copregister fs -> gpr[rt] */
			if (MIPSInst_RT(ir) != 0) {
				DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
					MIPSInst_RD(ir));
			}
			break;

		case dmtc_op:
976 977 978
			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
				return SIGILL;

L
Linus Torvalds 已提交
979 980 981 982
			/* copregister fs <- rt */
			DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
			break;

983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
		case mfhc_op:
			if (!cpu_has_mips_r2)
				goto sigill;

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

		case mthc_op:
			if (!cpu_has_mips_r2)
				goto sigill;

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

L
Linus Torvalds 已提交
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
		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;

1015
		case cfc_op:
L
Linus Torvalds 已提交
1016 1017 1018
			/* cop control register rd -> gpr[rt] */
			if (MIPSInst_RD(ir) == FPCREG_CSR) {
				value = ctx->fcr31;
1019
				value = (value & ~FPU_CSR_RM) | modeindex(value);
1020 1021 1022
				pr_debug("%p gpr[%d]<-csr=%08x\n",
					 (void *) (xcp->cp0_epc),
					 MIPSInst_RT(ir), value);
L
Linus Torvalds 已提交
1023 1024 1025 1026 1027 1028 1029 1030 1031
			}
			else if (MIPSInst_RD(ir) == FPCREG_RID)
				value = 0;
			else
				value = 0;
			if (MIPSInst_RT(ir))
				xcp->regs[MIPSInst_RT(ir)] = value;
			break;

1032
		case ctc_op:
L
Linus Torvalds 已提交
1033 1034 1035 1036 1037 1038 1039 1040 1041
			/* copregister rd <- rt */
			if (MIPSInst_RT(ir) == 0)
				value = 0;
			else
				value = xcp->regs[MIPSInst_RT(ir)];

			/* we only have one writable control reg
			 */
			if (MIPSInst_RD(ir) == FPCREG_CSR) {
1042 1043 1044
				pr_debug("%p gpr[%d]->csr=%08x\n",
					 (void *) (xcp->cp0_epc),
					 MIPSInst_RT(ir), value);
1045 1046 1047 1048 1049

				/*
				 * Don't write reserved bits,
				 * and convert to ieee library modes
				 */
1050 1051
				ctx->fcr31 = (value & ~(FPU_CSR_RSVD | FPU_CSR_RM)) |
					     modeindex(value);
L
Linus Torvalds 已提交
1052 1053 1054 1055 1056 1057
			}
			if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
				return SIGFPE;
			}
			break;

1058
		case bc_op:
1059
			if (delay_slot(xcp))
L
Linus Torvalds 已提交
1060 1061
				return SIGILL;

1062 1063 1064 1065 1066 1067
			if (cpu_has_mips_4_5_r)
				cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
			else
				cbit = FPU_CSR_COND;
			cond = ctx->fcr31 & cbit;

1068
			likely = 0;
L
Linus Torvalds 已提交
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
			switch (MIPSInst_RT(ir) & 3) {
			case bcfl_op:
				likely = 1;
			case bcf_op:
				cond = !cond;
				break;
			case bctl_op:
				likely = 1;
			case bct_op:
				break;
			default:
				/* thats an illegal instruction */
				return SIGILL;
			}

1084
			set_delay_slot(xcp);
L
Linus Torvalds 已提交
1085
			if (cond) {
1086 1087
				/*
				 * Branch taken: emulate dslot instruction
L
Linus Torvalds 已提交
1088
				 */
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
				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.
						 */
						return mips_dsemul(xcp, ir, contpc);
					}
				} else
					contpc = (xcp->cp0_epc + (contpc << 2));
L
Linus Torvalds 已提交
1118 1119 1120

				switch (MIPSInst_OPCODE(ir)) {
				case lwc1_op:
1121
					goto emul;
1122

L
Linus Torvalds 已提交
1123
				case swc1_op:
1124
					goto emul;
1125

L
Linus Torvalds 已提交
1126 1127
				case ldc1_op:
				case sdc1_op:
1128 1129 1130 1131 1132 1133
					if (cpu_has_mips_2_3_4_5 ||
					    cpu_has_mips64)
						goto emul;

					return SIGILL;
					goto emul;
1134

L
Linus Torvalds 已提交
1135 1136
				case cop1_op:
					goto emul;
1137

1138
				case cop1x_op:
1139
					if (cpu_has_mips_4_5 || cpu_has_mips64 || cpu_has_mips32r2)
1140 1141 1142 1143
						/* its one of ours */
						goto emul;

					return SIGILL;
1144

L
Linus Torvalds 已提交
1145
				case spec_op:
1146 1147 1148
					if (!cpu_has_mips_4_5_r)
						return SIGILL;

L
Linus Torvalds 已提交
1149 1150 1151 1152 1153 1154 1155 1156 1157
					if (MIPSInst_FUNC(ir) == movc_op)
						goto emul;
					break;
				}

				/*
				 * Single step the non-cp1
				 * instruction in the dslot
				 */
A
Atsushi Nemoto 已提交
1158
				return mips_dsemul(xcp, ir, contpc);
1159
			} else if (likely) {	/* branch not taken */
L
Linus Torvalds 已提交
1160 1161 1162 1163
					/*
					 * branch likely nullifies
					 * dslot if not taken
					 */
1164 1165
					xcp->cp0_epc += dec_insn.pc_inc;
					contpc += dec_insn.pc_inc;
L
Linus Torvalds 已提交
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
					/*
					 * else continue & execute
					 * dslot as normal insn
					 */
				}
			break;

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

1177 1178 1179
			/* a real fpu computation instruction */
			if ((sig = fpu_emu(xcp, ctx, ir)))
				return sig;
L
Linus Torvalds 已提交
1180 1181 1182
		}
		break;

1183
	case cop1x_op:
1184
		if (!cpu_has_mips_4_5 && !cpu_has_mips64 && !cpu_has_mips32r2)
1185 1186 1187
			return SIGILL;

		sig = fpux_emu(xcp, ctx, ir, fault_addr);
1188
		if (sig)
L
Linus Torvalds 已提交
1189 1190 1191 1192
			return sig;
		break;

	case spec_op:
1193 1194 1195
		if (!cpu_has_mips_4_5_r)
			return SIGILL;

L
Linus Torvalds 已提交
1196 1197 1198 1199 1200 1201 1202 1203
		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:
1204
sigill:
L
Linus Torvalds 已提交
1205 1206 1207 1208
		return SIGILL;
	}

	/* we did it !! */
A
Atsushi Nemoto 已提交
1209
	xcp->cp0_epc = contpc;
1210
	clear_delay_slot(xcp);
1211

L
Linus Torvalds 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
	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 */
};


/*
 * Additional MIPS4 instructions
 */

1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
#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 已提交
1249 1250
}

1251
static union ieee754dp fpemu_dp_recip(union ieee754dp d)
L
Linus Torvalds 已提交
1252 1253 1254 1255
{
	return ieee754dp_div(ieee754dp_one(0), d);
}

1256
static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
L
Linus Torvalds 已提交
1257 1258 1259 1260
{
	return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
}

1261
static union ieee754sp fpemu_sp_recip(union ieee754sp s)
L
Linus Torvalds 已提交
1262 1263 1264 1265
{
	return ieee754sp_div(ieee754sp_one(0), s);
}

1266
static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
L
Linus Torvalds 已提交
1267 1268 1269 1270
{
	return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
}

1271 1272
DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
L
Linus Torvalds 已提交
1273 1274
DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
1275 1276
DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
L
Linus Torvalds 已提交
1277 1278 1279
DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);

1280
static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1281
	mips_instruction ir, void *__user *fault_addr)
L
Linus Torvalds 已提交
1282 1283 1284
{
	unsigned rcsr = 0;	/* resulting csr */

1285
	MIPS_FPU_EMU_INC_STATS(cp1xops);
L
Linus Torvalds 已提交
1286 1287 1288 1289

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

1290 1291
		union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
		union ieee754sp fd, fr, fs, ft;
1292
		u32 __user *va;
L
Linus Torvalds 已提交
1293 1294 1295 1296
		u32 val;

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

1300
			MIPS_FPU_EMU_INC_STATS(loads);
1301
			if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
1302
				MIPS_FPU_EMU_INC_STATS(errors);
1303
				*fault_addr = va;
L
Linus Torvalds 已提交
1304 1305
				return SIGBUS;
			}
1306 1307 1308 1309 1310
			if (__get_user(val, va)) {
				MIPS_FPU_EMU_INC_STATS(errors);
				*fault_addr = va;
				return SIGSEGV;
			}
L
Linus Torvalds 已提交
1311 1312 1313 1314
			SITOREG(val, MIPSInst_FD(ir));
			break;

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

1318
			MIPS_FPU_EMU_INC_STATS(stores);
L
Linus Torvalds 已提交
1319 1320

			SIFROMREG(val, MIPSInst_FS(ir));
1321
			if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1322
				MIPS_FPU_EMU_INC_STATS(errors);
1323
				*fault_addr = va;
L
Linus Torvalds 已提交
1324 1325
				return SIGBUS;
			}
1326 1327 1328 1329 1330
			if (put_user(val, va)) {
				MIPS_FPU_EMU_INC_STATS(errors);
				*fault_addr = va;
				return SIGSEGV;
			}
L
Linus Torvalds 已提交
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
			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:
1354 1355
			if (ieee754_cxtest(IEEE754_INEXACT)) {
				MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
L
Linus Torvalds 已提交
1356
				rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1357 1358 1359
			}
			if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
				MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
L
Linus Torvalds 已提交
1360
				rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1361 1362 1363
			}
			if (ieee754_cxtest(IEEE754_OVERFLOW)) {
				MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
L
Linus Torvalds 已提交
1364
				rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1365 1366 1367
			}
			if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
				MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
L
Linus Torvalds 已提交
1368
				rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1369
			}
L
Linus Torvalds 已提交
1370 1371 1372

			ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
			if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1373
				/*printk ("SIGFPE: FPU csr = %08x\n",
L
Linus Torvalds 已提交
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
				   ctx->fcr31); */
				return SIGFPE;
			}

			break;

		default:
			return SIGILL;
		}
		break;
	}

	case d_fmt:{		/* 1 */
1387 1388
		union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
		union ieee754dp fd, fr, fs, ft;
1389
		u64 __user *va;
L
Linus Torvalds 已提交
1390 1391 1392 1393
		u64 val;

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

1397
			MIPS_FPU_EMU_INC_STATS(loads);
1398
			if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
1399
				MIPS_FPU_EMU_INC_STATS(errors);
1400
				*fault_addr = va;
L
Linus Torvalds 已提交
1401 1402
				return SIGBUS;
			}
1403 1404 1405 1406 1407
			if (__get_user(val, va)) {
				MIPS_FPU_EMU_INC_STATS(errors);
				*fault_addr = va;
				return SIGSEGV;
			}
L
Linus Torvalds 已提交
1408 1409 1410 1411
			DITOREG(val, MIPSInst_FD(ir));
			break;

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

1415
			MIPS_FPU_EMU_INC_STATS(stores);
L
Linus Torvalds 已提交
1416
			DIFROMREG(val, MIPSInst_FS(ir));
1417
			if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
1418
				MIPS_FPU_EMU_INC_STATS(errors);
1419
				*fault_addr = va;
L
Linus Torvalds 已提交
1420 1421
				return SIGBUS;
			}
1422 1423 1424 1425 1426
			if (__put_user(val, va)) {
				MIPS_FPU_EMU_INC_STATS(errors);
				*fault_addr = va;
				return SIGSEGV;
			}
L
Linus Torvalds 已提交
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455
			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;
	}

1456 1457
	case 0x3:
		if (MIPSInst_FUNC(ir) != pfetch_op)
L
Linus Torvalds 已提交
1458
			return SIGILL;
1459

L
Linus Torvalds 已提交
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
		/* ignore prefx operation */
		break;

	default:
		return SIGILL;
	}

	return 0;
}



/*
 * Emulate a single COP1 arithmetic instruction.
 */
1475
static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
L
Linus Torvalds 已提交
1476 1477 1478 1479
	mips_instruction ir)
{
	int rfmt;		/* resulting format */
	unsigned rcsr = 0;	/* resulting csr */
1480 1481
	unsigned int oldrm;
	unsigned int cbit;
L
Linus Torvalds 已提交
1482 1483
	unsigned cond;
	union {
1484 1485
		union ieee754dp d;
		union ieee754sp s;
L
Linus Torvalds 已提交
1486 1487 1488
		int w;
		s64 l;
	} rv;			/* resulting value */
1489
	u64 bits;
L
Linus Torvalds 已提交
1490

1491
	MIPS_FPU_EMU_INC_STATS(cp1ops);
L
Linus Torvalds 已提交
1492
	switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
1493
	case s_fmt: {		/* 0 */
L
Linus Torvalds 已提交
1494
		union {
1495 1496
			union ieee754sp(*b) (union ieee754sp, union ieee754sp);
			union ieee754sp(*u) (union ieee754sp);
L
Linus Torvalds 已提交
1497
		} handler;
1498
		union ieee754sp fs, ft;
L
Linus Torvalds 已提交
1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516

		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:
1517 1518 1519
			if (!cpu_has_mips_4_5_r)
				return SIGILL;

L
Linus Torvalds 已提交
1520 1521
			handler.u = ieee754sp_sqrt;
			goto scopuop;
1522

1523 1524 1525 1526 1527
		/*
		 * 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 已提交
1528
		case frsqrt_op:
1529 1530 1531
			if (!cpu_has_mips_4_5_r2)
				return SIGILL;

L
Linus Torvalds 已提交
1532 1533
			handler.u = fpemu_sp_rsqrt;
			goto scopuop;
1534

L
Linus Torvalds 已提交
1535
		case frecip_op:
1536 1537 1538
			if (!cpu_has_mips_4_5_r2)
				return SIGILL;

L
Linus Torvalds 已提交
1539 1540
			handler.u = fpemu_sp_recip;
			goto scopuop;
1541

L
Linus Torvalds 已提交
1542
		case fmovc_op:
1543 1544 1545
			if (!cpu_has_mips_4_5_r)
				return SIGILL;

L
Linus Torvalds 已提交
1546 1547 1548 1549 1550 1551
			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;
1552

L
Linus Torvalds 已提交
1553
		case fmovz_op:
1554 1555 1556
			if (!cpu_has_mips_4_5_r)
				return SIGILL;

L
Linus Torvalds 已提交
1557 1558 1559 1560
			if (xcp->regs[MIPSInst_FT(ir)] != 0)
				return 0;
			SPFROMREG(rv.s, MIPSInst_FS(ir));
			break;
1561

L
Linus Torvalds 已提交
1562
		case fmovn_op:
1563 1564 1565
			if (!cpu_has_mips_4_5_r)
				return SIGILL;

L
Linus Torvalds 已提交
1566 1567 1568 1569
			if (xcp->regs[MIPSInst_FT(ir)] == 0)
				return 0;
			SPFROMREG(rv.s, MIPSInst_FS(ir));
			break;
1570

L
Linus Torvalds 已提交
1571 1572 1573
		case fabs_op:
			handler.u = ieee754sp_abs;
			goto scopuop;
1574

L
Linus Torvalds 已提交
1575 1576 1577
		case fneg_op:
			handler.u = ieee754sp_neg;
			goto scopuop;
1578

L
Linus Torvalds 已提交
1579 1580 1581 1582 1583 1584
		case fmov_op:
			/* an easy one */
			SPFROMREG(rv.s, MIPSInst_FS(ir));
			goto copcsr;

			/* binary op on handler */
1585 1586 1587
scopbop:
			SPFROMREG(fs, MIPSInst_FS(ir));
			SPFROMREG(ft, MIPSInst_FT(ir));
L
Linus Torvalds 已提交
1588

1589 1590 1591 1592 1593 1594 1595
			rv.s = (*handler.b) (fs, ft);
			goto copcsr;
scopuop:
			SPFROMREG(fs, MIPSInst_FS(ir));
			rv.s = (*handler.u) (fs);
			goto copcsr;
copcsr:
1596 1597
			if (ieee754_cxtest(IEEE754_INEXACT)) {
				MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
L
Linus Torvalds 已提交
1598
				rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1599 1600 1601
			}
			if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
				MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
L
Linus Torvalds 已提交
1602
				rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1603 1604 1605
			}
			if (ieee754_cxtest(IEEE754_OVERFLOW)) {
				MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
L
Linus Torvalds 已提交
1606
				rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1607 1608 1609
			}
			if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) {
				MIPS_FPU_EMU_INC_STATS(ieee754_zerodiv);
L
Linus Torvalds 已提交
1610
				rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
1611 1612 1613
			}
			if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
				MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
L
Linus Torvalds 已提交
1614
				rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1615
			}
L
Linus Torvalds 已提交
1616 1617 1618 1619 1620 1621
			break;

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

1622
		case fcvtd_op:
L
Linus Torvalds 已提交
1623 1624 1625 1626 1627
			SPFROMREG(fs, MIPSInst_FS(ir));
			rv.d = ieee754dp_fsp(fs);
			rfmt = d_fmt;
			goto copcsr;

1628
		case fcvtw_op:
L
Linus Torvalds 已提交
1629 1630 1631 1632 1633 1634 1635 1636
			SPFROMREG(fs, MIPSInst_FS(ir));
			rv.w = ieee754sp_tint(fs);
			rfmt = w_fmt;
			goto copcsr;

		case fround_op:
		case ftrunc_op:
		case fceil_op:
1637
		case ffloor_op:
1638 1639 1640
			if (!cpu_has_mips_2_3_4_5 && !cpu_has_mips64)
				return SIGILL;

1641
			oldrm = ieee754_csr.rm;
L
Linus Torvalds 已提交
1642
			SPFROMREG(fs, MIPSInst_FS(ir));
1643
			ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
L
Linus Torvalds 已提交
1644 1645 1646 1647 1648
			rv.w = ieee754sp_tint(fs);
			ieee754_csr.rm = oldrm;
			rfmt = w_fmt;
			goto copcsr;

1649
		case fcvtl_op:
1650 1651 1652
			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
				return SIGILL;

L
Linus Torvalds 已提交
1653 1654 1655 1656 1657 1658 1659 1660
			SPFROMREG(fs, MIPSInst_FS(ir));
			rv.l = ieee754sp_tlong(fs);
			rfmt = l_fmt;
			goto copcsr;

		case froundl_op:
		case ftruncl_op:
		case fceill_op:
1661
		case ffloorl_op:
1662 1663 1664
			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
				return SIGILL;

1665
			oldrm = ieee754_csr.rm;
L
Linus Torvalds 已提交
1666
			SPFROMREG(fs, MIPSInst_FS(ir));
1667
			ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
L
Linus Torvalds 已提交
1668 1669 1670 1671 1672 1673 1674 1675
			rv.l = ieee754sp_tlong(fs);
			ieee754_csr.rm = oldrm;
			rfmt = l_fmt;
			goto copcsr;

		default:
			if (MIPSInst_FUNC(ir) >= fcmp_op) {
				unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
1676
				union ieee754sp fs, ft;
L
Linus Torvalds 已提交
1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688

				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;

1689
			} else
L
Linus Torvalds 已提交
1690 1691 1692 1693 1694 1695
				return SIGILL;
			break;
		}
		break;
	}

1696 1697
	case d_fmt: {
		union ieee754dp fs, ft;
L
Linus Torvalds 已提交
1698
		union {
1699 1700
			union ieee754dp(*b) (union ieee754dp, union ieee754dp);
			union ieee754dp(*u) (union ieee754dp);
L
Linus Torvalds 已提交
1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719
		} 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:
1720 1721 1722
			if (!cpu_has_mips_2_3_4_5_r)
				return SIGILL;

L
Linus Torvalds 已提交
1723 1724
			handler.u = ieee754dp_sqrt;
			goto dcopuop;
1725 1726 1727 1728 1729
		/*
		 * 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 已提交
1730
		case frsqrt_op:
1731 1732 1733
			if (!cpu_has_mips_4_5_r2)
				return SIGILL;

L
Linus Torvalds 已提交
1734 1735 1736
			handler.u = fpemu_dp_rsqrt;
			goto dcopuop;
		case frecip_op:
1737 1738 1739
			if (!cpu_has_mips_4_5_r2)
				return SIGILL;

L
Linus Torvalds 已提交
1740 1741 1742
			handler.u = fpemu_dp_recip;
			goto dcopuop;
		case fmovc_op:
1743 1744 1745
			if (!cpu_has_mips_4_5_r)
				return SIGILL;

L
Linus Torvalds 已提交
1746 1747 1748 1749 1750 1751 1752
			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:
1753 1754 1755
			if (!cpu_has_mips_4_5_r)
				return SIGILL;

L
Linus Torvalds 已提交
1756 1757 1758 1759 1760
			if (xcp->regs[MIPSInst_FT(ir)] != 0)
				return 0;
			DPFROMREG(rv.d, MIPSInst_FS(ir));
			break;
		case fmovn_op:
1761 1762 1763
			if (!cpu_has_mips_4_5_r)
				return SIGILL;

L
Linus Torvalds 已提交
1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
			if (xcp->regs[MIPSInst_FT(ir)] == 0)
				return 0;
			DPFROMREG(rv.d, MIPSInst_FS(ir));
			break;
		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 */
1782 1783 1784
dcopbop:
			DPFROMREG(fs, MIPSInst_FS(ir));
			DPFROMREG(ft, MIPSInst_FT(ir));
L
Linus Torvalds 已提交
1785

1786 1787 1788 1789 1790 1791
			rv.d = (*handler.b) (fs, ft);
			goto copcsr;
dcopuop:
			DPFROMREG(fs, MIPSInst_FS(ir));
			rv.d = (*handler.u) (fs);
			goto copcsr;
L
Linus Torvalds 已提交
1792

1793 1794 1795 1796
		/*
		 * unary conv ops
		 */
		case fcvts_op:
L
Linus Torvalds 已提交
1797 1798 1799 1800
			DPFROMREG(fs, MIPSInst_FS(ir));
			rv.s = ieee754sp_fdp(fs);
			rfmt = s_fmt;
			goto copcsr;
1801

L
Linus Torvalds 已提交
1802 1803 1804
		case fcvtd_op:
			return SIGILL;	/* not defined */

1805
		case fcvtw_op:
L
Linus Torvalds 已提交
1806 1807 1808 1809 1810 1811 1812 1813
			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:
1814
		case ffloor_op:
1815 1816 1817
			if (!cpu_has_mips_2_3_4_5_r)
				return SIGILL;

1818
			oldrm = ieee754_csr.rm;
L
Linus Torvalds 已提交
1819
			DPFROMREG(fs, MIPSInst_FS(ir));
1820
			ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
L
Linus Torvalds 已提交
1821 1822 1823 1824 1825
			rv.w = ieee754dp_tint(fs);
			ieee754_csr.rm = oldrm;
			rfmt = w_fmt;
			goto copcsr;

1826
		case fcvtl_op:
1827 1828 1829
			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
				return SIGILL;

L
Linus Torvalds 已提交
1830 1831 1832 1833 1834 1835 1836 1837
			DPFROMREG(fs, MIPSInst_FS(ir));
			rv.l = ieee754dp_tlong(fs);
			rfmt = l_fmt;
			goto copcsr;

		case froundl_op:
		case ftruncl_op:
		case fceill_op:
1838
		case ffloorl_op:
1839 1840 1841
			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
				return SIGILL;

1842
			oldrm = ieee754_csr.rm;
L
Linus Torvalds 已提交
1843
			DPFROMREG(fs, MIPSInst_FS(ir));
1844
			ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
L
Linus Torvalds 已提交
1845 1846 1847 1848 1849 1850 1851 1852
			rv.l = ieee754dp_tlong(fs);
			ieee754_csr.rm = oldrm;
			rfmt = l_fmt;
			goto copcsr;

		default:
			if (MIPSInst_FUNC(ir) >= fcmp_op) {
				unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
1853
				union ieee754dp fs, ft;
L
Linus Torvalds 已提交
1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875

				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;

1876
	case w_fmt:
L
Linus Torvalds 已提交
1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895
		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;
		default:
			return SIGILL;
		}
		break;
	}

1896
	case l_fmt:
1897 1898 1899 1900

		if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
			return SIGILL;

P
Paul Burton 已提交
1901 1902
		DIFROMREG(bits, MIPSInst_FS(ir));

L
Linus Torvalds 已提交
1903 1904 1905
		switch (MIPSInst_FUNC(ir)) {
		case fcvts_op:
			/* convert long to single precision real */
P
Paul Burton 已提交
1906
			rv.s = ieee754sp_flong(bits);
L
Linus Torvalds 已提交
1907 1908 1909 1910
			rfmt = s_fmt;
			goto copcsr;
		case fcvtd_op:
			/* convert long to double precision real */
P
Paul Burton 已提交
1911
			rv.d = ieee754dp_flong(bits);
L
Linus Torvalds 已提交
1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931
			rfmt = d_fmt;
			goto copcsr;
		default:
			return SIGILL;
		}
		break;

	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) {
1932
		/*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
L
Linus Torvalds 已提交
1933 1934 1935 1936 1937 1938 1939
		return SIGFPE;
	}

	/*
	 * Now we can safely write the result back to the register file.
	 */
	switch (rfmt) {
1940 1941 1942
	case -1:

		if (cpu_has_mips_4_5_r)
1943
			cbit = fpucondbit[MIPSInst_FD(ir) >> 2];
1944 1945
		else
			cbit = FPU_CSR_COND;
L
Linus Torvalds 已提交
1946
		if (rv.w)
1947
			ctx->fcr31 |= cbit;
L
Linus Torvalds 已提交
1948
		else
1949
			ctx->fcr31 &= ~cbit;
L
Linus Torvalds 已提交
1950
		break;
1951

L
Linus Torvalds 已提交
1952 1953 1954 1955 1956 1957 1958 1959 1960 1961
	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:
1962 1963 1964
		if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
			return SIGILL;

L
Linus Torvalds 已提交
1965 1966 1967 1968 1969 1970 1971 1972 1973
		DITOREG(rv.l, MIPSInst_FD(ir));
		break;
	default:
		return SIGILL;
	}

	return 0;
}

1974
int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1975
	int has_fpu, void *__user *fault_addr)
L
Linus Torvalds 已提交
1976
{
1977
	unsigned long oldepc, prevepc;
1978 1979 1980
	struct mm_decoded_insn dec_insn;
	u16 instr[4];
	u16 *instr_ptr;
L
Linus Torvalds 已提交
1981 1982 1983 1984 1985 1986
	int sig = 0;

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

1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
		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;
2041
		}
2042 2043 2044 2045 2046

		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 已提交
2047
		else {
2048 2049
			/*
			 * The 'ieee754_csr' is an alias of
R
Ralf Baechle 已提交
2050 2051
			 * ctx->fcr31.	No need to copy ctx->fcr31 to
			 * ieee754_csr.	 But ieee754_csr.rm is ieee
2052 2053
			 * library modes. (not mips rounding mode)
			 */
2054
			sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
L
Linus Torvalds 已提交
2055 2056
		}

2057
		if (has_fpu)
L
Linus Torvalds 已提交
2058 2059 2060 2061 2062 2063 2064 2065 2066
			break;
		if (sig)
			break;

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

	/* SIGILL indicates a non-fpu instruction */
	if (sig == SIGILL && xcp->cp0_epc != oldepc)
2067
		/* but if EPC has advanced, then ignore it */
L
Linus Torvalds 已提交
2068 2069 2070 2071
		sig = 0;

	return sig;
}