1 /*
2  * Software emulation of some PPC instructions for the 8xx core.
3  *
4  * Copyright (C) 1998 Dan Malek (dmalek@jlc.net)
5  *
6  * Software floating emuation for the MPC8xx processor.  I did this mostly
7  * because it was easier than trying to get the libraries compiled for
8  * software floating point.  The goal is still to get the libraries done,
9  * but I lost patience and needed some hacks to at least get init and
10  * shells running.  The first problem is the setjmp/longjmp that save
11  * and restore the floating point registers.
12  *
13  * For this emulation, our working registers are found on the register
14  * save area.
15  */
16 
17 #include <linux/errno.h>
18 #include <linux/sched.h>
19 #include <linux/kernel.h>
20 #include <linux/mm.h>
21 #include <linux/stddef.h>
22 #include <linux/unistd.h>
23 #include <linux/ptrace.h>
24 #include <linux/slab.h>
25 #include <linux/user.h>
26 #include <linux/a.out.h>
27 #include <linux/interrupt.h>
28 
29 #include <asm/pgtable.h>
30 #include <asm/uaccess.h>
31 #include <asm/system.h>
32 #include <asm/io.h>
33 #include <asm/processor.h>
34 
35 extern void
36 print_8xx_pte(struct mm_struct *mm, unsigned long addr);
37 extern int
38 get_8xx_pte(struct mm_struct *mm, unsigned long addr);
39 
40 /* Eventually we may need a look-up table, but this works for now.
41 */
42 #define LFS	48
43 #define LFD	50
44 #define LFDU	51
45 #define STFD	54
46 #define STFDU	55
47 #define FMR	63
48 
49 /*
50  * We return 0 on success, 1 on unimplemented instruction, and EFAULT
51  * if a load/store faulted.
52  */
53 int
Soft_emulate_8xx(struct pt_regs * regs)54 Soft_emulate_8xx(struct pt_regs *regs)
55 {
56 	uint	inst, instword;
57 	uint	flreg, idxreg, disp;
58 	uint	retval;
59 	signed short sdisp;
60 	uint	*ea, *ip;
61 
62 	retval = 0;
63 
64 	instword = *((uint *)regs->nip);
65 	inst = instword >> 26;
66 
67 	flreg = (instword >> 21) & 0x1f;
68 	idxreg = (instword >> 16) & 0x1f;
69 	disp = instword & 0xffff;
70 
71 	ea = (uint *)(regs->gpr[idxreg] + disp);
72 	ip = (uint *)&current->thread.fpr[flreg];
73 
74 	switch ( inst )
75 	{
76 	case LFD:
77 		/* this is a 16 bit quantity that is sign extended
78 		 * so use a signed short here -- Cort
79 		 */
80 		sdisp = (instword & 0xffff);
81 		ea = (uint *)(regs->gpr[idxreg] + sdisp);
82 		if (copy_from_user(ip, ea, sizeof(double)))
83 			retval = -EFAULT;
84 		break;
85 
86 	case LFDU:
87 		if (copy_from_user(ip, ea, sizeof(double)))
88 			retval = -EFAULT;
89 		else
90 			regs->gpr[idxreg] = (uint)ea;
91 		break;
92 	case LFS:
93 		sdisp = (instword & 0xffff);
94 		ea = (uint *)(regs->gpr[idxreg] + sdisp);
95 		if (copy_from_user(ip, ea, sizeof(float)))
96 			retval = -EFAULT;
97 		break;
98 	case STFD:
99 		/* this is a 16 bit quantity that is sign extended
100 		 * so use a signed short here -- Cort
101 		 */
102 		sdisp = (instword & 0xffff);
103 		ea = (uint *)(regs->gpr[idxreg] + sdisp);
104 		if (copy_to_user(ea, ip, sizeof(double)))
105 			retval = -EFAULT;
106 		break;
107 
108 	case STFDU:
109 		if (copy_to_user(ea, ip, sizeof(double)))
110 			retval = -EFAULT;
111 		else
112 			regs->gpr[idxreg] = (uint)ea;
113 		break;
114 	case FMR:
115 		/* assume this is a fp move -- Cort */
116 		memcpy( ip, &current->thread.fpr[(instword>>11)&0x1f],
117 			sizeof(double) );
118 		break;
119 	default:
120 		retval = 1;
121 		printk("Bad emulation %s/%d\n"
122 		       " NIP: %08lx instruction: %08x opcode: %x "
123 		       "A: %x B: %x C: %x code: %x rc: %x\n",
124 		       current->comm,current->pid,
125 		       regs->nip,
126 		       instword,inst,
127 		       (instword>>16)&0x1f,
128 		       (instword>>11)&0x1f,
129 		       (instword>>6)&0x1f,
130 		       (instword>>1)&0x3ff,
131 		       instword&1);
132 		{
133 			unsigned long pa;
134 			print_8xx_pte(current->mm,regs->nip);
135 			pa = (get_8xx_pte(current->mm,regs->nip) & PAGE_MASK)
136 				| (regs->nip & ~PAGE_MASK);
137 			printk("Kernel VA for NIP 0x%p ", __va(pa));
138 			print_8xx_pte(current->mm, (unsigned long)__va(pa));
139 		}
140 
141 	}
142 
143 	if (retval == 0)
144 		regs->nip += 4;
145 	return(retval);
146 }
147 
148