Linux Audio

Check our new training course

Loading...
v5.4
  1/*
  2 * arch/sh/math-emu/math.c
  3 *
  4 * Copyright (C) 2006 Takashi YOSHII <takasi-y@ops.dti.ne.jp>
  5 *
  6 * This file is subject to the terms and conditions of the GNU General Public
  7 * License.  See the file "COPYING" in the main directory of this archive
  8 * for more details.
  9 */
 10#include <linux/kernel.h>
 11#include <linux/errno.h>
 12#include <linux/types.h>
 13#include <linux/sched/signal.h>
 14#include <linux/signal.h>
 15#include <linux/perf_event.h>
 16
 17#include <linux/uaccess.h>
 
 
 18#include <asm/processor.h>
 19#include <asm/io.h>
 20
 21#include "sfp-util.h"
 22#include <math-emu/soft-fp.h>
 23#include <math-emu/single.h>
 24#include <math-emu/double.h>
 25
 26#define	FPUL		(fregs->fpul)
 27#define FPSCR		(fregs->fpscr)
 28#define FPSCR_RM	(FPSCR&3)
 29#define FPSCR_DN	((FPSCR>>18)&1)
 30#define FPSCR_PR	((FPSCR>>19)&1)
 31#define FPSCR_SZ	((FPSCR>>20)&1)
 32#define FPSCR_FR	((FPSCR>>21)&1)
 33#define FPSCR_MASK	0x003fffffUL
 34
 35#define BANK(n)	(n^(FPSCR_FR?16:0))
 36#define FR	((unsigned long*)(fregs->fp_regs))
 37#define FR0	(FR[BANK(0)])
 38#define FRn	(FR[BANK(n)])
 39#define FRm	(FR[BANK(m)])
 40#define DR	((unsigned long long*)(fregs->fp_regs))
 41#define DRn	(DR[BANK(n)/2])
 42#define DRm	(DR[BANK(m)/2])
 43
 44#define XREG(n)	(n^16)
 45#define XFn	(FR[BANK(XREG(n))])
 46#define XFm	(FR[BANK(XREG(m))])
 47#define XDn	(DR[BANK(XREG(n))/2])
 48#define XDm	(DR[BANK(XREG(m))/2])
 49
 50#define R0	(regs->regs[0])
 51#define Rn	(regs->regs[n])
 52#define Rm	(regs->regs[m])
 53
 54#define WRITE(d,a)	({if(put_user(d, (typeof (d)*)a)) return -EFAULT;})
 55#define READ(d,a)	({if(get_user(d, (typeof (d)*)a)) return -EFAULT;})
 56
 57#define PACK_S(r,f)	FP_PACK_SP(&r,f)
 58#define UNPACK_S(f,r)	FP_UNPACK_SP(f,&r)
 59#define PACK_D(r,f) \
 60	{u32 t[2]; FP_PACK_DP(t,f); ((u32*)&r)[0]=t[1]; ((u32*)&r)[1]=t[0];}
 61#define UNPACK_D(f,r) \
 62	{u32 t[2]; t[0]=((u32*)&r)[1]; t[1]=((u32*)&r)[0]; FP_UNPACK_DP(f,t);}
 63
 64// 2 args instructions.
 65#define BOTH_PRmn(op,x) \
 66	FP_DECL_EX; if(FPSCR_PR) op(D,x,DRm,DRn); else op(S,x,FRm,FRn);
 67
 68#define CMP_X(SZ,R,M,N) do{ \
 69	FP_DECL_##SZ(Fm); FP_DECL_##SZ(Fn); \
 70	UNPACK_##SZ(Fm, M); UNPACK_##SZ(Fn, N); \
 71	FP_CMP_##SZ(R, Fn, Fm, 2); }while(0)
 72#define EQ_X(SZ,R,M,N) do{ \
 73	FP_DECL_##SZ(Fm); FP_DECL_##SZ(Fn); \
 74	UNPACK_##SZ(Fm, M); UNPACK_##SZ(Fn, N); \
 75	FP_CMP_EQ_##SZ(R, Fn, Fm); }while(0)
 76#define CMP(OP) ({ int r; BOTH_PRmn(OP##_X,r); r; })
 77
 78static int
 79fcmp_gt(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
 80{
 81	if (CMP(CMP) > 0)
 82		regs->sr |= 1;
 83	else
 84		regs->sr &= ~1;
 85
 86	return 0;
 87}
 88
 89static int
 90fcmp_eq(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
 91{
 92	if (CMP(CMP /*EQ*/) == 0)
 93		regs->sr |= 1;
 94	else
 95		regs->sr &= ~1;
 96	return 0;
 97}
 98
 99#define ARITH_X(SZ,OP,M,N) do{ \
100	FP_DECL_##SZ(Fm); FP_DECL_##SZ(Fn); FP_DECL_##SZ(Fr); \
101	UNPACK_##SZ(Fm, M); UNPACK_##SZ(Fn, N); \
102	FP_##OP##_##SZ(Fr, Fn, Fm); \
103	PACK_##SZ(N, Fr); }while(0)
104
105static int
106fadd(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
107{
108	BOTH_PRmn(ARITH_X, ADD);
109	return 0;
110}
111
112static int
113fsub(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
114{
115	BOTH_PRmn(ARITH_X, SUB);
116	return 0;
117}
118
119static int
120fmul(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
121{
122	BOTH_PRmn(ARITH_X, MUL);
123	return 0;
124}
125
126static int
127fdiv(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
128{
129	BOTH_PRmn(ARITH_X, DIV);
130	return 0;
131}
132
133static int
134fmac(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
135{
136	FP_DECL_EX;
137	FP_DECL_S(Fr);
138	FP_DECL_S(Ft);
139	FP_DECL_S(F0);
140	FP_DECL_S(Fm);
141	FP_DECL_S(Fn);
142	UNPACK_S(F0, FR0);
143	UNPACK_S(Fm, FRm);
144	UNPACK_S(Fn, FRn);
145	FP_MUL_S(Ft, Fm, F0);
146	FP_ADD_S(Fr, Fn, Ft);
147	PACK_S(FRn, Fr);
148	return 0;
149}
150
151// to process fmov's extension (odd n for DR access XD).
152#define FMOV_EXT(x) if(x&1) x+=16-1
153
154static int
155fmov_idx_reg(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m,
156	     int n)
157{
158	if (FPSCR_SZ) {
159		FMOV_EXT(n);
160		READ(FRn, Rm + R0 + 4);
161		n++;
162		READ(FRn, Rm + R0);
163	} else {
164		READ(FRn, Rm + R0);
165	}
166
167	return 0;
168}
169
170static int
171fmov_mem_reg(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m,
172	     int n)
173{
174	if (FPSCR_SZ) {
175		FMOV_EXT(n);
176		READ(FRn, Rm + 4);
177		n++;
178		READ(FRn, Rm);
179	} else {
180		READ(FRn, Rm);
181	}
182
183	return 0;
184}
185
186static int
187fmov_inc_reg(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m,
188	     int n)
189{
190	if (FPSCR_SZ) {
191		FMOV_EXT(n);
192		READ(FRn, Rm + 4);
193		n++;
194		READ(FRn, Rm);
195		Rm += 8;
196	} else {
197		READ(FRn, Rm);
198		Rm += 4;
199	}
200
201	return 0;
202}
203
204static int
205fmov_reg_idx(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m,
206	     int n)
207{
208	if (FPSCR_SZ) {
209		FMOV_EXT(m);
210		WRITE(FRm, Rn + R0 + 4);
211		m++;
212		WRITE(FRm, Rn + R0);
213	} else {
214		WRITE(FRm, Rn + R0);
215	}
216
217	return 0;
218}
219
220static int
221fmov_reg_mem(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m,
222	     int n)
223{
224	if (FPSCR_SZ) {
225		FMOV_EXT(m);
226		WRITE(FRm, Rn + 4);
227		m++;
228		WRITE(FRm, Rn);
229	} else {
230		WRITE(FRm, Rn);
231	}
232
233	return 0;
234}
235
236static int
237fmov_reg_dec(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m,
238	     int n)
239{
240	if (FPSCR_SZ) {
241		FMOV_EXT(m);
242		Rn -= 8;
243		WRITE(FRm, Rn + 4);
244		m++;
245		WRITE(FRm, Rn);
246	} else {
247		Rn -= 4;
248		WRITE(FRm, Rn);
249	}
250
251	return 0;
252}
253
254static int
255fmov_reg_reg(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m,
256	     int n)
257{
258	if (FPSCR_SZ) {
259		FMOV_EXT(m);
260		FMOV_EXT(n);
261		DRn = DRm;
262	} else {
263		FRn = FRm;
264	}
265
266	return 0;
267}
268
269static int
270fnop_mn(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
271{
272	return -EINVAL;
273}
274
275// 1 arg instructions.
276#define NOTYETn(i) static int i(struct sh_fpu_soft_struct *fregs, int n) \
277	{ printk( #i " not yet done.\n"); return 0; }
278
279NOTYETn(ftrv)
280NOTYETn(fsqrt)
281NOTYETn(fipr)
282NOTYETn(fsca)
283NOTYETn(fsrra)
284
285#define EMU_FLOAT_X(SZ,N) do { \
286	FP_DECL_##SZ(Fn); \
287	FP_FROM_INT_##SZ(Fn, FPUL, 32, int); \
288	PACK_##SZ(N, Fn); }while(0)
289static int ffloat(struct sh_fpu_soft_struct *fregs, int n)
290{
291	FP_DECL_EX;
292
293	if (FPSCR_PR)
294		EMU_FLOAT_X(D, DRn);
295	else
296		EMU_FLOAT_X(S, FRn);
297
298	return 0;
299}
300
301#define EMU_FTRC_X(SZ,N) do { \
302	FP_DECL_##SZ(Fn); \
303	UNPACK_##SZ(Fn, N); \
304	FP_TO_INT_##SZ(FPUL, Fn, 32, 1); }while(0)
305static int ftrc(struct sh_fpu_soft_struct *fregs, int n)
306{
307	FP_DECL_EX;
308
309	if (FPSCR_PR)
310		EMU_FTRC_X(D, DRn);
311	else
312		EMU_FTRC_X(S, FRn);
313
314	return 0;
315}
316
317static int fcnvsd(struct sh_fpu_soft_struct *fregs, int n)
318{
319	FP_DECL_EX;
320	FP_DECL_S(Fn);
321	FP_DECL_D(Fr);
322	UNPACK_S(Fn, FPUL);
323	FP_CONV(D, S, 2, 1, Fr, Fn);
324	PACK_D(DRn, Fr);
325	return 0;
326}
327
328static int fcnvds(struct sh_fpu_soft_struct *fregs, int n)
329{
330	FP_DECL_EX;
331	FP_DECL_D(Fn);
332	FP_DECL_S(Fr);
333	UNPACK_D(Fn, DRn);
334	FP_CONV(S, D, 1, 2, Fr, Fn);
335	PACK_S(FPUL, Fr);
336	return 0;
337}
338
339static int fxchg(struct sh_fpu_soft_struct *fregs, int flag)
340{
341	FPSCR ^= flag;
342	return 0;
343}
344
345static int fsts(struct sh_fpu_soft_struct *fregs, int n)
346{
347	FRn = FPUL;
348	return 0;
349}
350
351static int flds(struct sh_fpu_soft_struct *fregs, int n)
352{
353	FPUL = FRn;
354	return 0;
355}
356
357static int fneg(struct sh_fpu_soft_struct *fregs, int n)
358{
359	FRn ^= (1 << (_FP_W_TYPE_SIZE - 1));
360	return 0;
361}
362
363static int fabs(struct sh_fpu_soft_struct *fregs, int n)
364{
365	FRn &= ~(1 << (_FP_W_TYPE_SIZE - 1));
366	return 0;
367}
368
369static int fld0(struct sh_fpu_soft_struct *fregs, int n)
370{
371	FRn = 0;
372	return 0;
373}
374
375static int fld1(struct sh_fpu_soft_struct *fregs, int n)
376{
377	FRn = (_FP_EXPBIAS_S << (_FP_FRACBITS_S - 1));
378	return 0;
379}
380
381static int fnop_n(struct sh_fpu_soft_struct *fregs, int n)
382{
383	return -EINVAL;
384}
385
386/// Instruction decoders.
387
388static int id_fxfd(struct sh_fpu_soft_struct *, int);
389static int id_fnxd(struct sh_fpu_soft_struct *, struct pt_regs *, int, int);
390
391static int (*fnxd[])(struct sh_fpu_soft_struct *, int) = {
392	fsts, flds, ffloat, ftrc, fneg, fabs, fsqrt, fsrra,
393	fld0, fld1, fcnvsd, fcnvds, fnop_n, fnop_n, fipr, id_fxfd
394};
395
396static int (*fnmx[])(struct sh_fpu_soft_struct *, struct pt_regs *, int, int) = {
397	fadd, fsub, fmul, fdiv, fcmp_eq, fcmp_gt, fmov_idx_reg, fmov_reg_idx,
398	fmov_mem_reg, fmov_inc_reg, fmov_reg_mem, fmov_reg_dec,
399	fmov_reg_reg, id_fnxd, fmac, fnop_mn};
400
401static int id_fxfd(struct sh_fpu_soft_struct *fregs, int x)
402{
403	const int flag[] = { FPSCR_SZ, FPSCR_PR, FPSCR_FR, 0 };
404	switch (x & 3) {
405	case 3:
406		fxchg(fregs, flag[x >> 2]);
407		break;
408	case 1:
409		ftrv(fregs, x - 1);
410		break;
411	default:
412		fsca(fregs, x);
413	}
414	return 0;
415}
416
417static int
418id_fnxd(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int x, int n)
419{
420	return (fnxd[x])(fregs, n);
421}
422
423static int
424id_fnmx(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, u16 code)
425{
426	int n = (code >> 8) & 0xf, m = (code >> 4) & 0xf, x = code & 0xf;
427	return (fnmx[x])(fregs, regs, m, n);
428}
429
430static int
431id_sys(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, u16 code)
432{
433	int n = ((code >> 8) & 0xf);
434	unsigned long *reg = (code & 0x0010) ? &FPUL : &FPSCR;
435
436	switch (code & 0xf0ff) {
437	case 0x005a:
438	case 0x006a:
439		Rn = *reg;
440		break;
441	case 0x405a:
442	case 0x406a:
443		*reg = Rn;
444		break;
445	case 0x4052:
446	case 0x4062:
447		Rn -= 4;
448		WRITE(*reg, Rn);
449		break;
450	case 0x4056:
451	case 0x4066:
452		READ(*reg, Rn);
453		Rn += 4;
454		break;
455	default:
456		return -EINVAL;
457	}
458
459	return 0;
460}
461
462static int fpu_emulate(u16 code, struct sh_fpu_soft_struct *fregs, struct pt_regs *regs)
463{
464	if ((code & 0xf000) == 0xf000)
465		return id_fnmx(fregs, regs, code);
466	else
467		return id_sys(fregs, regs, code);
468}
469
470/**
471 *	denormal_to_double - Given denormalized float number,
472 *	                     store double float
473 *
474 *	@fpu: Pointer to sh_fpu_soft structure
475 *	@n: Index to FP register
476 */
477static void denormal_to_double(struct sh_fpu_soft_struct *fpu, int n)
478{
479	unsigned long du, dl;
480	unsigned long x = fpu->fpul;
481	int exp = 1023 - 126;
482
483	if (x != 0 && (x & 0x7f800000) == 0) {
484		du = (x & 0x80000000);
485		while ((x & 0x00800000) == 0) {
486			x <<= 1;
487			exp--;
488		}
489		x &= 0x007fffff;
490		du |= (exp << 20) | (x >> 3);
491		dl = x << 29;
492
493		fpu->fp_regs[n] = du;
494		fpu->fp_regs[n+1] = dl;
495	}
496}
497
498/**
499 *	ieee_fpe_handler - Handle denormalized number exception
500 *
501 *	@regs: Pointer to register structure
502 *
503 *	Returns 1 when it's handled (should not cause exception).
504 */
505static int ieee_fpe_handler(struct pt_regs *regs)
506{
507	unsigned short insn = *(unsigned short *)regs->pc;
508	unsigned short finsn;
509	unsigned long nextpc;
510	int nib[4] = {
511		(insn >> 12) & 0xf,
512		(insn >> 8) & 0xf,
513		(insn >> 4) & 0xf,
514		insn & 0xf};
515
516	if (nib[0] == 0xb ||
517	    (nib[0] == 0x4 && nib[2] == 0x0 && nib[3] == 0xb)) /* bsr & jsr */
518		regs->pr = regs->pc + 4;
519
520	if (nib[0] == 0xa || nib[0] == 0xb) { /* bra & bsr */
521		nextpc = regs->pc + 4 + ((short) ((insn & 0xfff) << 4) >> 3);
522		finsn = *(unsigned short *) (regs->pc + 2);
523	} else if (nib[0] == 0x8 && nib[1] == 0xd) { /* bt/s */
524		if (regs->sr & 1)
525			nextpc = regs->pc + 4 + ((char) (insn & 0xff) << 1);
526		else
527			nextpc = regs->pc + 4;
528		finsn = *(unsigned short *) (regs->pc + 2);
529	} else if (nib[0] == 0x8 && nib[1] == 0xf) { /* bf/s */
530		if (regs->sr & 1)
531			nextpc = regs->pc + 4;
532		else
533			nextpc = regs->pc + 4 + ((char) (insn & 0xff) << 1);
534		finsn = *(unsigned short *) (regs->pc + 2);
535	} else if (nib[0] == 0x4 && nib[3] == 0xb &&
536		 (nib[2] == 0x0 || nib[2] == 0x2)) { /* jmp & jsr */
537		nextpc = regs->regs[nib[1]];
538		finsn = *(unsigned short *) (regs->pc + 2);
539	} else if (nib[0] == 0x0 && nib[3] == 0x3 &&
540		 (nib[2] == 0x0 || nib[2] == 0x2)) { /* braf & bsrf */
541		nextpc = regs->pc + 4 + regs->regs[nib[1]];
542		finsn = *(unsigned short *) (regs->pc + 2);
543	} else if (insn == 0x000b) { /* rts */
544		nextpc = regs->pr;
545		finsn = *(unsigned short *) (regs->pc + 2);
546	} else {
547		nextpc = regs->pc + 2;
548		finsn = insn;
549	}
550
551	if ((finsn & 0xf1ff) == 0xf0ad) { /* fcnvsd */
552		struct task_struct *tsk = current;
553
554		if ((tsk->thread.xstate->softfpu.fpscr & (1 << 17))) {
555			/* FPU error */
556			denormal_to_double (&tsk->thread.xstate->softfpu,
557					    (finsn >> 8) & 0xf);
558			tsk->thread.xstate->softfpu.fpscr &=
559				~(FPSCR_CAUSE_MASK | FPSCR_FLAG_MASK);
560			task_thread_info(tsk)->status |= TS_USEDFPU;
561		} else {
562			force_sig_fault(SIGFPE, FPE_FLTINV,
563					(void __user *)regs->pc);
564		}
565
566		regs->pc = nextpc;
567		return 1;
568	}
569
570	return 0;
571}
572
573/**
574 * fpu_init - Initialize FPU registers
575 * @fpu: Pointer to software emulated FPU registers.
576 */
577static void fpu_init(struct sh_fpu_soft_struct *fpu)
578{
579	int i;
580
581	fpu->fpscr = FPSCR_INIT;
582	fpu->fpul = 0;
583
584	for (i = 0; i < 16; i++) {
585		fpu->fp_regs[i] = 0;
586		fpu->xfp_regs[i]= 0;
587	}
588}
589
590/**
591 * do_fpu_inst - Handle reserved instructions for FPU emulation
592 * @inst: instruction code.
593 * @regs: registers on stack.
594 */
595int do_fpu_inst(unsigned short inst, struct pt_regs *regs)
596{
597	struct task_struct *tsk = current;
598	struct sh_fpu_soft_struct *fpu = &(tsk->thread.xstate->softfpu);
599
600	perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, 0);
601
602	if (!(task_thread_info(tsk)->status & TS_USEDFPU)) {
603		/* initialize once. */
604		fpu_init(fpu);
605		task_thread_info(tsk)->status |= TS_USEDFPU;
606	}
607
608	return fpu_emulate(inst, fpu, regs);
609}
v6.13.7
  1/*
  2 * arch/sh/math-emu/math.c
  3 *
  4 * Copyright (C) 2006 Takashi YOSHII <takasi-y@ops.dti.ne.jp>
  5 *
  6 * This file is subject to the terms and conditions of the GNU General Public
  7 * License.  See the file "COPYING" in the main directory of this archive
  8 * for more details.
  9 */
 10#include <linux/kernel.h>
 11#include <linux/errno.h>
 12#include <linux/types.h>
 13#include <linux/sched/signal.h>
 14#include <linux/signal.h>
 15#include <linux/perf_event.h>
 16
 17#include <linux/uaccess.h>
 18
 19#include <asm/fpu.h>
 20#include <asm/processor.h>
 21#include <asm/io.h>
 22
 23#include "sfp-util.h"
 24#include <math-emu/soft-fp.h>
 25#include <math-emu/single.h>
 26#include <math-emu/double.h>
 27
 28#define	FPUL		(fregs->fpul)
 29#define FPSCR		(fregs->fpscr)
 30#define FPSCR_RM	(FPSCR&3)
 31#define FPSCR_DN	((FPSCR>>18)&1)
 32#define FPSCR_PR	((FPSCR>>19)&1)
 33#define FPSCR_SZ	((FPSCR>>20)&1)
 34#define FPSCR_FR	((FPSCR>>21)&1)
 35#define FPSCR_MASK	0x003fffffUL
 36
 37#define BANK(n)	(n^(FPSCR_FR?16:0))
 38#define FR	((unsigned long*)(fregs->fp_regs))
 39#define FR0	(FR[BANK(0)])
 40#define FRn	(FR[BANK(n)])
 41#define FRm	(FR[BANK(m)])
 42#define DR	((unsigned long long*)(fregs->fp_regs))
 43#define DRn	(DR[BANK(n)/2])
 44#define DRm	(DR[BANK(m)/2])
 45
 46#define XREG(n)	(n^16)
 47#define XFn	(FR[BANK(XREG(n))])
 48#define XFm	(FR[BANK(XREG(m))])
 49#define XDn	(DR[BANK(XREG(n))/2])
 50#define XDm	(DR[BANK(XREG(m))/2])
 51
 52#define R0	(regs->regs[0])
 53#define Rn	(regs->regs[n])
 54#define Rm	(regs->regs[m])
 55
 56#define MWRITE(d,a)	({if(put_user(d, (typeof (d) __user *)a)) return -EFAULT;})
 57#define MREAD(d,a)	({if(get_user(d, (typeof (d) __user *)a)) return -EFAULT;})
 58
 59#define PACK_S(r,f)	FP_PACK_SP(&r,f)
 60#define UNPACK_S(f,r)	FP_UNPACK_SP(f,&r)
 61#define PACK_D(r,f) \
 62	{u32 t[2]; FP_PACK_DP(t,f); ((u32*)&r)[0]=t[1]; ((u32*)&r)[1]=t[0];}
 63#define UNPACK_D(f,r) \
 64	{u32 t[2]; t[0]=((u32*)&r)[1]; t[1]=((u32*)&r)[0]; FP_UNPACK_DP(f,t);}
 65
 66// 2 args instructions.
 67#define BOTH_PRmn(op,x) \
 68	FP_DECL_EX; if(FPSCR_PR) op(D,x,DRm,DRn); else op(S,x,FRm,FRn);
 69
 70#define CMP_X(SZ,R,M,N) do{ \
 71	FP_DECL_##SZ(Fm); FP_DECL_##SZ(Fn); \
 72	UNPACK_##SZ(Fm, M); UNPACK_##SZ(Fn, N); \
 73	FP_CMP_##SZ(R, Fn, Fm, 2); }while(0)
 74#define EQ_X(SZ,R,M,N) do{ \
 75	FP_DECL_##SZ(Fm); FP_DECL_##SZ(Fn); \
 76	UNPACK_##SZ(Fm, M); UNPACK_##SZ(Fn, N); \
 77	FP_CMP_EQ_##SZ(R, Fn, Fm); }while(0)
 78#define CMP(OP) ({ int r; BOTH_PRmn(OP##_X,r); r; })
 79
 80static int
 81fcmp_gt(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
 82{
 83	if (CMP(CMP) > 0)
 84		regs->sr |= 1;
 85	else
 86		regs->sr &= ~1;
 87
 88	return 0;
 89}
 90
 91static int
 92fcmp_eq(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
 93{
 94	if (CMP(CMP /*EQ*/) == 0)
 95		regs->sr |= 1;
 96	else
 97		regs->sr &= ~1;
 98	return 0;
 99}
100
101#define ARITH_X(SZ,OP,M,N) do{ \
102	FP_DECL_##SZ(Fm); FP_DECL_##SZ(Fn); FP_DECL_##SZ(Fr); \
103	UNPACK_##SZ(Fm, M); UNPACK_##SZ(Fn, N); \
104	FP_##OP##_##SZ(Fr, Fn, Fm); \
105	PACK_##SZ(N, Fr); }while(0)
106
107static int
108fadd(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
109{
110	BOTH_PRmn(ARITH_X, ADD);
111	return 0;
112}
113
114static int
115fsub(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
116{
117	BOTH_PRmn(ARITH_X, SUB);
118	return 0;
119}
120
121static int
122fmul(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
123{
124	BOTH_PRmn(ARITH_X, MUL);
125	return 0;
126}
127
128static int
129fdiv(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
130{
131	BOTH_PRmn(ARITH_X, DIV);
132	return 0;
133}
134
135static int
136fmac(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
137{
138	FP_DECL_EX;
139	FP_DECL_S(Fr);
140	FP_DECL_S(Ft);
141	FP_DECL_S(F0);
142	FP_DECL_S(Fm);
143	FP_DECL_S(Fn);
144	UNPACK_S(F0, FR0);
145	UNPACK_S(Fm, FRm);
146	UNPACK_S(Fn, FRn);
147	FP_MUL_S(Ft, Fm, F0);
148	FP_ADD_S(Fr, Fn, Ft);
149	PACK_S(FRn, Fr);
150	return 0;
151}
152
153// to process fmov's extension (odd n for DR access XD).
154#define FMOV_EXT(x) if(x&1) x+=16-1
155
156static int
157fmov_idx_reg(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m,
158	     int n)
159{
160	if (FPSCR_SZ) {
161		FMOV_EXT(n);
162		MREAD(FRn, Rm + R0 + 4);
163		n++;
164		MREAD(FRn, Rm + R0);
165	} else {
166		MREAD(FRn, Rm + R0);
167	}
168
169	return 0;
170}
171
172static int
173fmov_mem_reg(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m,
174	     int n)
175{
176	if (FPSCR_SZ) {
177		FMOV_EXT(n);
178		MREAD(FRn, Rm + 4);
179		n++;
180		MREAD(FRn, Rm);
181	} else {
182		MREAD(FRn, Rm);
183	}
184
185	return 0;
186}
187
188static int
189fmov_inc_reg(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m,
190	     int n)
191{
192	if (FPSCR_SZ) {
193		FMOV_EXT(n);
194		MREAD(FRn, Rm + 4);
195		n++;
196		MREAD(FRn, Rm);
197		Rm += 8;
198	} else {
199		MREAD(FRn, Rm);
200		Rm += 4;
201	}
202
203	return 0;
204}
205
206static int
207fmov_reg_idx(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m,
208	     int n)
209{
210	if (FPSCR_SZ) {
211		FMOV_EXT(m);
212		MWRITE(FRm, Rn + R0 + 4);
213		m++;
214		MWRITE(FRm, Rn + R0);
215	} else {
216		MWRITE(FRm, Rn + R0);
217	}
218
219	return 0;
220}
221
222static int
223fmov_reg_mem(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m,
224	     int n)
225{
226	if (FPSCR_SZ) {
227		FMOV_EXT(m);
228		MWRITE(FRm, Rn + 4);
229		m++;
230		MWRITE(FRm, Rn);
231	} else {
232		MWRITE(FRm, Rn);
233	}
234
235	return 0;
236}
237
238static int
239fmov_reg_dec(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m,
240	     int n)
241{
242	if (FPSCR_SZ) {
243		FMOV_EXT(m);
244		Rn -= 8;
245		MWRITE(FRm, Rn + 4);
246		m++;
247		MWRITE(FRm, Rn);
248	} else {
249		Rn -= 4;
250		MWRITE(FRm, Rn);
251	}
252
253	return 0;
254}
255
256static int
257fmov_reg_reg(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m,
258	     int n)
259{
260	if (FPSCR_SZ) {
261		FMOV_EXT(m);
262		FMOV_EXT(n);
263		DRn = DRm;
264	} else {
265		FRn = FRm;
266	}
267
268	return 0;
269}
270
271static int
272fnop_mn(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
273{
274	return -EINVAL;
275}
276
277// 1 arg instructions.
278#define NOTYETn(i) static int i(struct sh_fpu_soft_struct *fregs, int n) \
279	{ printk( #i " not yet done.\n"); return 0; }
280
281NOTYETn(ftrv)
282NOTYETn(fsqrt)
283NOTYETn(fipr)
284NOTYETn(fsca)
285NOTYETn(fsrra)
286
287#define EMU_FLOAT_X(SZ,N) do { \
288	FP_DECL_##SZ(Fn); \
289	FP_FROM_INT_##SZ(Fn, FPUL, 32, int); \
290	PACK_##SZ(N, Fn); }while(0)
291static int ffloat(struct sh_fpu_soft_struct *fregs, int n)
292{
293	FP_DECL_EX;
294
295	if (FPSCR_PR)
296		EMU_FLOAT_X(D, DRn);
297	else
298		EMU_FLOAT_X(S, FRn);
299
300	return 0;
301}
302
303#define EMU_FTRC_X(SZ,N) do { \
304	FP_DECL_##SZ(Fn); \
305	UNPACK_##SZ(Fn, N); \
306	FP_TO_INT_##SZ(FPUL, Fn, 32, 1); }while(0)
307static int ftrc(struct sh_fpu_soft_struct *fregs, int n)
308{
309	FP_DECL_EX;
310
311	if (FPSCR_PR)
312		EMU_FTRC_X(D, DRn);
313	else
314		EMU_FTRC_X(S, FRn);
315
316	return 0;
317}
318
319static int fcnvsd(struct sh_fpu_soft_struct *fregs, int n)
320{
321	FP_DECL_EX;
322	FP_DECL_S(Fn);
323	FP_DECL_D(Fr);
324	UNPACK_S(Fn, FPUL);
325	FP_CONV(D, S, 2, 1, Fr, Fn);
326	PACK_D(DRn, Fr);
327	return 0;
328}
329
330static int fcnvds(struct sh_fpu_soft_struct *fregs, int n)
331{
332	FP_DECL_EX;
333	FP_DECL_D(Fn);
334	FP_DECL_S(Fr);
335	UNPACK_D(Fn, DRn);
336	FP_CONV(S, D, 1, 2, Fr, Fn);
337	PACK_S(FPUL, Fr);
338	return 0;
339}
340
341static int fxchg(struct sh_fpu_soft_struct *fregs, int flag)
342{
343	FPSCR ^= flag;
344	return 0;
345}
346
347static int fsts(struct sh_fpu_soft_struct *fregs, int n)
348{
349	FRn = FPUL;
350	return 0;
351}
352
353static int flds(struct sh_fpu_soft_struct *fregs, int n)
354{
355	FPUL = FRn;
356	return 0;
357}
358
359static int fneg(struct sh_fpu_soft_struct *fregs, int n)
360{
361	FRn ^= (1 << (_FP_W_TYPE_SIZE - 1));
362	return 0;
363}
364
365static int fabs(struct sh_fpu_soft_struct *fregs, int n)
366{
367	FRn &= ~(1 << (_FP_W_TYPE_SIZE - 1));
368	return 0;
369}
370
371static int fld0(struct sh_fpu_soft_struct *fregs, int n)
372{
373	FRn = 0;
374	return 0;
375}
376
377static int fld1(struct sh_fpu_soft_struct *fregs, int n)
378{
379	FRn = (_FP_EXPBIAS_S << (_FP_FRACBITS_S - 1));
380	return 0;
381}
382
383static int fnop_n(struct sh_fpu_soft_struct *fregs, int n)
384{
385	return -EINVAL;
386}
387
388/// Instruction decoders.
389
390static int id_fxfd(struct sh_fpu_soft_struct *, int);
391static int id_fnxd(struct sh_fpu_soft_struct *, struct pt_regs *, int, int);
392
393static int (*fnxd[])(struct sh_fpu_soft_struct *, int) = {
394	fsts, flds, ffloat, ftrc, fneg, fabs, fsqrt, fsrra,
395	fld0, fld1, fcnvsd, fcnvds, fnop_n, fnop_n, fipr, id_fxfd
396};
397
398static int (*fnmx[])(struct sh_fpu_soft_struct *, struct pt_regs *, int, int) = {
399	fadd, fsub, fmul, fdiv, fcmp_eq, fcmp_gt, fmov_idx_reg, fmov_reg_idx,
400	fmov_mem_reg, fmov_inc_reg, fmov_reg_mem, fmov_reg_dec,
401	fmov_reg_reg, id_fnxd, fmac, fnop_mn};
402
403static int id_fxfd(struct sh_fpu_soft_struct *fregs, int x)
404{
405	const int flag[] = { FPSCR_SZ, FPSCR_PR, FPSCR_FR, 0 };
406	switch (x & 3) {
407	case 3:
408		fxchg(fregs, flag[x >> 2]);
409		break;
410	case 1:
411		ftrv(fregs, x - 1);
412		break;
413	default:
414		fsca(fregs, x);
415	}
416	return 0;
417}
418
419static int
420id_fnxd(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int x, int n)
421{
422	return (fnxd[x])(fregs, n);
423}
424
425static int
426id_fnmx(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, u16 code)
427{
428	int n = (code >> 8) & 0xf, m = (code >> 4) & 0xf, x = code & 0xf;
429	return (fnmx[x])(fregs, regs, m, n);
430}
431
432static int
433id_sys(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, u16 code)
434{
435	int n = ((code >> 8) & 0xf);
436	unsigned long *reg = (code & 0x0010) ? &FPUL : &FPSCR;
437
438	switch (code & 0xf0ff) {
439	case 0x005a:
440	case 0x006a:
441		Rn = *reg;
442		break;
443	case 0x405a:
444	case 0x406a:
445		*reg = Rn;
446		break;
447	case 0x4052:
448	case 0x4062:
449		Rn -= 4;
450		MWRITE(*reg, Rn);
451		break;
452	case 0x4056:
453	case 0x4066:
454		MREAD(*reg, Rn);
455		Rn += 4;
456		break;
457	default:
458		return -EINVAL;
459	}
460
461	return 0;
462}
463
464static int fpu_emulate(u16 code, struct sh_fpu_soft_struct *fregs, struct pt_regs *regs)
465{
466	if ((code & 0xf000) == 0xf000)
467		return id_fnmx(fregs, regs, code);
468	else
469		return id_sys(fregs, regs, code);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
470}
471
472/**
473 * fpu_init - Initialize FPU registers
474 * @fpu: Pointer to software emulated FPU registers.
475 */
476static void fpu_init(struct sh_fpu_soft_struct *fpu)
477{
478	int i;
479
480	fpu->fpscr = FPSCR_INIT;
481	fpu->fpul = 0;
482
483	for (i = 0; i < 16; i++) {
484		fpu->fp_regs[i] = 0;
485		fpu->xfp_regs[i]= 0;
486	}
487}
488
489/**
490 * do_fpu_inst - Handle reserved instructions for FPU emulation
491 * @inst: instruction code.
492 * @regs: registers on stack.
493 */
494int do_fpu_inst(unsigned short inst, struct pt_regs *regs)
495{
496	struct task_struct *tsk = current;
497	struct sh_fpu_soft_struct *fpu = &(tsk->thread.xstate->softfpu);
498
499	perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, 0);
500
501	if (!(task_thread_info(tsk)->status & TS_USEDFPU)) {
502		/* initialize once. */
503		fpu_init(fpu);
504		task_thread_info(tsk)->status |= TS_USEDFPU;
505	}
506
507	return fpu_emulate(inst, fpu, regs);
508}