http://www.usr.com/support/gpl/USR9107_release1.1.tar.gz
[bcm963xx.git] / kernel / linux / arch / parisc / mm / fault.c
1 /* $Id: fault.c,v 1.5 2000/01/26 16:20:29 jsm Exp $
2  *
3  * This file is subject to the terms and conditions of the GNU General Public
4  * License.  See the file "COPYING" in the main directory of this archive
5  * for more details.
6  *
7  *
8  * Copyright (C) 1995, 1996, 1997, 1998 by Ralf Baechle
9  * Copyright 1999 SuSE GmbH (Philipp Rumpf, prumpf@tux.org)
10  * Copyright 1999 Hewlett Packard Co.
11  *
12  */
13
14 #include <linux/mm.h>
15 #include <linux/ptrace.h>
16 #include <linux/sched.h>
17 #include <linux/interrupt.h>
18 #include <linux/module.h>
19
20 #include <asm/uaccess.h>
21 #include <asm/traps.h>
22
23 #define PRINT_USER_FAULTS /* (turn this on if you want user faults to be */
24                          /*  dumped to the console via printk)          */
25
26
27 /* Defines for parisc_acctyp()  */
28 #define READ            0
29 #define WRITE           1
30
31 /* Various important other fields */
32 #define bit22set(x)             (x & 0x00000200)
33 #define bits23_25set(x)         (x & 0x000001c0)
34 #define isGraphicsFlushRead(x)  ((x & 0xfc003fdf) == 0x04001a80)
35                                 /* extended opcode is 0x6a */
36
37 #define BITSSET         0x1c0   /* for identifying LDCW */
38
39 /*
40  * parisc_acctyp(unsigned int inst) --
41  *    Given a PA-RISC memory access instruction, determine if the
42  *    the instruction would perform a memory read or memory write
43  *    operation.
44  *
45  *    This function assumes that the given instruction is a memory access
46  *    instruction (i.e. you should really only call it if you know that
47  *    the instruction has generated some sort of a memory access fault).
48  *
49  * Returns:
50  *   VM_READ  if read operation
51  *   VM_WRITE if write operation
52  *   VM_EXEC  if execute operation
53  */
54 static unsigned long
55 parisc_acctyp(unsigned long code, unsigned int inst)
56 {
57         if (code == 6 || code == 16)
58             return VM_EXEC;
59
60         switch (inst & 0xf0000000) {
61         case 0x40000000: /* load */
62         case 0x50000000: /* new load */
63                 return VM_READ;
64
65         case 0x60000000: /* store */
66         case 0x70000000: /* new store */
67                 return VM_WRITE;
68
69         case 0x20000000: /* coproc */
70         case 0x30000000: /* coproc2 */
71                 if (bit22set(inst))
72                         return VM_WRITE;
73
74         case 0x0: /* indexed/memory management */
75                 if (bit22set(inst)) {
76                         /*
77                          * Check for the 'Graphics Flush Read' instruction.
78                          * It resembles an FDC instruction, except for bits
79                          * 20 and 21. Any combination other than zero will
80                          * utilize the block mover functionality on some
81                          * older PA-RISC platforms.  The case where a block
82                          * move is performed from VM to graphics IO space
83                          * should be treated as a READ.
84                          *
85                          * The significance of bits 20,21 in the FDC
86                          * instruction is:
87                          *
88                          *   00  Flush data cache (normal instruction behavior)
89                          *   01  Graphics flush write  (IO space -> VM)
90                          *   10  Graphics flush read   (VM -> IO space)
91                          *   11  Graphics flush read/write (VM <-> IO space)
92                          */
93                         if (isGraphicsFlushRead(inst))
94                                 return VM_READ;
95                         return VM_WRITE;
96                 } else {
97                         /*
98                          * Check for LDCWX and LDCWS (semaphore instructions).
99                          * If bits 23 through 25 are all 1's it is one of
100                          * the above two instructions and is a write.
101                          *
102                          * Note: With the limited bits we are looking at,
103                          * this will also catch PROBEW and PROBEWI. However,
104                          * these should never get in here because they don't
105                          * generate exceptions of the type:
106                          *   Data TLB miss fault/data page fault
107                          *   Data memory protection trap
108                          */
109                         if (bits23_25set(inst) == BITSSET)
110                                 return VM_WRITE;
111                 }
112                 return VM_READ; /* Default */
113         }
114         return VM_READ; /* Default */
115 }
116
117 #undef bit22set
118 #undef bits23_25set
119 #undef isGraphicsFlushRead
120 #undef BITSSET
121
122
123 #if 0
124 /* This is the treewalk to find a vma which is the highest that has
125  * a start < addr.  We're using find_vma_prev instead right now, but
126  * we might want to use this at some point in the future.  Probably
127  * not, but I want it committed to CVS so I don't lose it :-)
128  */
129                         while (tree != vm_avl_empty) {
130                                 if (tree->vm_start > addr) {
131                                         tree = tree->vm_avl_left;
132                                 } else {
133                                         prev = tree;
134                                         if (prev->vm_next == NULL)
135                                                 break;
136                                         if (prev->vm_next->vm_start > addr)
137                                                 break;
138                                         tree = tree->vm_avl_right;
139                                 }
140                         }
141 #endif
142
143 void do_page_fault(struct pt_regs *regs, unsigned long code,
144                               unsigned long address)
145 {
146         struct vm_area_struct *vma, *prev_vma;
147         struct task_struct *tsk = current;
148         struct mm_struct *mm = tsk->mm;
149         const struct exception_table_entry *fix;
150         unsigned long acc_type;
151
152         if (in_interrupt() || !mm)
153                 goto no_context;
154
155         down_read(&mm->mmap_sem);
156         vma = find_vma_prev(mm, address, &prev_vma);
157         if (!vma || address < vma->vm_start)
158                 goto check_expansion;
159 /*
160  * Ok, we have a good vm_area for this memory access. We still need to
161  * check the access permissions.
162  */
163
164 good_area:
165
166         acc_type = parisc_acctyp(code,regs->iir);
167
168         if ((vma->vm_flags & acc_type) != acc_type)
169                 goto bad_area;
170
171         /*
172          * If for any reason at all we couldn't handle the fault, make
173          * sure we exit gracefully rather than endlessly redo the
174          * fault.
175          */
176
177         switch (handle_mm_fault(mm, vma, address, (acc_type & VM_WRITE) != 0)) {
178               case 1:
179                 ++current->min_flt;
180                 break;
181               case 2:
182                 ++current->maj_flt;
183                 break;
184               case 0:
185                 /*
186                  * We ran out of memory, or some other thing happened
187                  * to us that made us unable to handle the page fault
188                  * gracefully.
189                  */
190                 goto bad_area;
191               default:
192                 goto out_of_memory;
193         }
194         up_read(&mm->mmap_sem);
195         return;
196
197 check_expansion:
198         vma = prev_vma;
199         if (vma && (expand_stack(vma, address) == 0))
200                 goto good_area;
201
202 /*
203  * Something tried to access memory that isn't in our memory map..
204  */
205 bad_area:
206         up_read(&mm->mmap_sem);
207
208         if (user_mode(regs)) {
209                 struct siginfo si;
210
211 #ifdef PRINT_USER_FAULTS
212                 printk(KERN_DEBUG "\n");
213                 printk(KERN_DEBUG "do_page_fault() pid=%d command='%s' type=%lu address=0x%08lx\n",
214                     tsk->pid, tsk->comm, code, address);
215                 if (vma) {
216                         printk(KERN_DEBUG "vm_start = 0x%08lx, vm_end = 0x%08lx\n",
217                                         vma->vm_start, vma->vm_end);
218                 }
219                 show_regs(regs);
220 #endif
221                 /* FIXME: actually we need to get the signo and code correct */
222                 si.si_signo = SIGSEGV;
223                 si.si_errno = 0;
224                 si.si_code = SEGV_MAPERR;
225                 si.si_addr = (void *) address;
226                 force_sig_info(SIGSEGV, &si, current);
227                 return;
228         }
229
230 no_context:
231
232         if (!user_mode(regs)) {
233
234                 fix = search_exception_tables(regs->iaoq[0]);
235
236                 if (fix) {
237
238                         if (fix->skip & 1) 
239                                 regs->gr[8] = -EFAULT;
240                         if (fix->skip & 2)
241                                 regs->gr[9] = 0;
242
243                         regs->iaoq[0] += ((fix->skip) & ~3);
244
245                         /*
246                          * NOTE: In some cases the faulting instruction
247                          * may be in the delay slot of a branch. We
248                          * don't want to take the branch, so we don't
249                          * increment iaoq[1], instead we set it to be
250                          * iaoq[0]+4, and clear the B bit in the PSW
251                          */
252
253                         regs->iaoq[1] = regs->iaoq[0] + 4;
254                         regs->gr[0] &= ~PSW_B; /* IPSW in gr[0] */
255
256                         return;
257                 }
258         }
259
260         parisc_terminate("Bad Address (null pointer deref?)", regs, code, address);
261
262   out_of_memory:
263         up_read(&mm->mmap_sem);
264         printk(KERN_CRIT "VM: killing process %s\n", current->comm);
265         if (user_mode(regs))
266                 do_exit(SIGKILL);
267         goto no_context;
268 }