include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[pandora-kernel.git] / arch / powerpc / kernel / softemu8xx.c
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/user.h>
25 #include <linux/interrupt.h>
26
27 #include <asm/pgtable.h>
28 #include <asm/uaccess.h>
29 #include <asm/system.h>
30 #include <asm/io.h>
31
32 /* Eventually we may need a look-up table, but this works for now.
33 */
34 #define LFS     48
35 #define LFD     50
36 #define LFDU    51
37 #define STFD    54
38 #define STFDU   55
39 #define FMR     63
40
41 void print_8xx_pte(struct mm_struct *mm, unsigned long addr)
42 {
43         pgd_t *pgd;
44         pmd_t *pmd;
45         pte_t *pte;
46
47         printk(" pte @ 0x%8lx: ", addr);
48         pgd = pgd_offset(mm, addr & PAGE_MASK);
49         if (pgd) {
50                 pmd = pmd_offset(pud_offset(pgd, addr & PAGE_MASK),
51                                  addr & PAGE_MASK);
52                 if (pmd && pmd_present(*pmd)) {
53                         pte = pte_offset_kernel(pmd, addr & PAGE_MASK);
54                         if (pte) {
55                                 printk(" (0x%08lx)->(0x%08lx)->0x%08lx\n",
56                                         (long)pgd, (long)pte, (long)pte_val(*pte));
57 #define pp ((long)pte_val(*pte))
58                                 printk(" RPN: %05lx PP: %lx SPS: %lx SH: %lx "
59                                        "CI: %lx v: %lx\n",
60                                        pp>>12,    /* rpn */
61                                        (pp>>10)&3, /* pp */
62                                        (pp>>3)&1, /* small */
63                                        (pp>>2)&1, /* shared */
64                                        (pp>>1)&1, /* cache inhibit */
65                                        pp&1       /* valid */
66                                        );
67 #undef pp
68                         }
69                         else {
70                                 printk("no pte\n");
71                         }
72                 }
73                 else {
74                         printk("no pmd\n");
75                 }
76         }
77         else {
78                 printk("no pgd\n");
79         }
80 }
81
82 int get_8xx_pte(struct mm_struct *mm, unsigned long addr)
83 {
84         pgd_t *pgd;
85         pmd_t *pmd;
86         pte_t *pte;
87         int retval = 0;
88
89         pgd = pgd_offset(mm, addr & PAGE_MASK);
90         if (pgd) {
91                 pmd = pmd_offset(pud_offset(pgd, addr & PAGE_MASK),
92                                  addr & PAGE_MASK);
93                 if (pmd && pmd_present(*pmd)) {
94                         pte = pte_offset_kernel(pmd, addr & PAGE_MASK);
95                         if (pte) {
96                                 retval = (int)pte_val(*pte);
97                         }
98                 }
99         }
100         return retval;
101 }
102
103 /*
104  * We return 0 on success, 1 on unimplemented instruction, and EFAULT
105  * if a load/store faulted.
106  */
107 int Soft_emulate_8xx(struct pt_regs *regs)
108 {
109         u32 inst, instword;
110         u32 flreg, idxreg, disp;
111         int retval;
112         s16 sdisp;
113         u32 *ea, *ip;
114
115         retval = 0;
116
117         instword = *((u32 *)regs->nip);
118         inst = instword >> 26;
119
120         flreg = (instword >> 21) & 0x1f;
121         idxreg = (instword >> 16) & 0x1f;
122         disp = instword & 0xffff;
123
124         ea = (u32 *)(regs->gpr[idxreg] + disp);
125         ip = (u32 *)&current->thread.TS_FPR(flreg);
126
127         switch ( inst )
128         {
129         case LFD:
130                 /* this is a 16 bit quantity that is sign extended
131                  * so use a signed short here -- Cort
132                  */
133                 sdisp = (instword & 0xffff);
134                 ea = (u32 *)(regs->gpr[idxreg] + sdisp);
135                 if (copy_from_user(ip, ea, sizeof(double)))
136                         retval = -EFAULT;
137                 break;
138
139         case LFDU:
140                 if (copy_from_user(ip, ea, sizeof(double)))
141                         retval = -EFAULT;
142                 else
143                         regs->gpr[idxreg] = (u32)ea;
144                 break;
145         case LFS:
146                 sdisp = (instword & 0xffff);
147                 ea = (u32 *)(regs->gpr[idxreg] + sdisp);
148                 if (copy_from_user(ip, ea, sizeof(float)))
149                         retval = -EFAULT;
150                 break;
151         case STFD:
152                 /* this is a 16 bit quantity that is sign extended
153                  * so use a signed short here -- Cort
154                  */
155                 sdisp = (instword & 0xffff);
156                 ea = (u32 *)(regs->gpr[idxreg] + sdisp);
157                 if (copy_to_user(ea, ip, sizeof(double)))
158                         retval = -EFAULT;
159                 break;
160
161         case STFDU:
162                 if (copy_to_user(ea, ip, sizeof(double)))
163                         retval = -EFAULT;
164                 else
165                         regs->gpr[idxreg] = (u32)ea;
166                 break;
167         case FMR:
168                 /* assume this is a fp move -- Cort */
169                 memcpy(ip, &current->thread.TS_FPR((instword>>11)&0x1f),
170                        sizeof(double));
171                 break;
172         default:
173                 retval = 1;
174                 printk("Bad emulation %s/%d\n"
175                        " NIP: %08lx instruction: %08x opcode: %x "
176                        "A: %x B: %x C: %x code: %x rc: %x\n",
177                        current->comm,current->pid,
178                        regs->nip,
179                        instword,inst,
180                        (instword>>16)&0x1f,
181                        (instword>>11)&0x1f,
182                        (instword>>6)&0x1f,
183                        (instword>>1)&0x3ff,
184                        instword&1);
185                 {
186                         int pa;
187                         print_8xx_pte(current->mm,regs->nip);
188                         pa = get_8xx_pte(current->mm,regs->nip) & PAGE_MASK;
189                         pa |= (regs->nip & ~PAGE_MASK);
190                         pa = (unsigned long)__va(pa);
191                         printk("Kernel VA for NIP %x ", pa);
192                         print_8xx_pte(current->mm,pa);
193                 }
194         }
195
196         if (retval == 0)
197                 regs->nip += 4;
198
199         return retval;
200 }