File: /usr/src/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     
19     #include <asm/uaccess.h>
20     
21     
22     /* Defines for parisc_acctyp()	*/
23     #define READ		0
24     #define WRITE		1
25     
26     /* Various important other fields */
27     #define bit22set(x)		(x & 0x00000200)
28     #define bits23_25set(x)		(x & 0x000001c0)
29     #define isGraphicsFlushRead(x)	((x & 0xfc003fdf) == 0x04001a80)
30     				/* extended opcode is 0x6a */
31     
32     #define BITSSET		0x1c0	/* for identifying LDCW */
33     
34     /*
35      * parisc_acctyp(unsigned int inst) --
36      *    Given a PA-RISC memory access instruction, determine if the
37      *    the instruction would perform a memory read or memory write
38      *    operation.
39      *
40      *    This function assumes that the given instruction is a memory access
41      *    instruction (i.e. you should really only call it if you know that
42      *    the instruction has generated some sort of a memory access fault).
43      *
44      * Returns:
45      *   VM_READ  if read operation
46      *   VM_WRITE if write operation
47      *   VM_EXEC  if execute operation
48      */
49     static unsigned long
50     parisc_acctyp(unsigned long code, unsigned int inst)
51     {
52     	if (code == 6 || code == 16)
53     	    return VM_EXEC;
54     
55     	switch (inst & 0xf0000000) {
56     	case 0x40000000: /* load */
57     	case 0x50000000: /* new load */
58     		return VM_READ;
59     
60     	case 0x60000000: /* store */
61     	case 0x70000000: /* new store */
62     		return VM_WRITE;
63     
64     	case 0x20000000: /* coproc */
65     	case 0x30000000: /* coproc2 */
66     		if (bit22set(inst))
67     			return VM_WRITE;
68     
69     	case 0x0: /* indexed/memory management */
70     		if (bit22set(inst)) {
71     			/*
72     			 * Check for the 'Graphics Flush Read' instruction.
73     			 * It resembles an FDC instruction, except for bits
74     			 * 20 and 21. Any combination other than zero will
75     			 * utilize the block mover functionality on some
76     			 * older PA-RISC platforms.  The case where a block
77     			 * move is performed from VM to graphics IO space
78     			 * should be treated as a READ.
79     			 *
80     			 * The significance of bits 20,21 in the FDC
81     			 * instruction is:
82     			 *
83     			 *   00  Flush data cache (normal instruction behavior)
84     			 *   01  Graphics flush write  (IO space -> VM)
85     			 *   10  Graphics flush read   (VM -> IO space)
86     			 *   11  Graphics flush read/write (VM <-> IO space)
87     			 */
88     			if (isGraphicsFlushRead(inst))
89     				return VM_READ;
90     			return VM_WRITE;
91     		} else {
92     			/*
93     			 * Check for LDCWX and LDCWS (semaphore instructions).
94     			 * If bits 23 through 25 are all 1's it is one of
95     			 * the above two instructions and is a write.
96     			 *
97     			 * Note: With the limited bits we are looking at,
98     			 * this will also catch PROBEW and PROBEWI. However,
99     			 * these should never get in here because they don't
100     			 * generate exceptions of the type:
101     			 *   Data TLB miss fault/data page fault
102     			 *   Data memory protection trap
103     			 */
104     			if (bits23_25set(inst) == BITSSET)
105     				return VM_WRITE;
106     		}
107     		return VM_READ; /* Default */
108     	}
109     	return VM_READ; /* Default */
110     }
111     
112     #undef bit22set
113     #undef bits23_25set
114     #undef isGraphicsFlushRead
115     #undef BITSSET
116     
117     /* This is similar to expand_stack(), except that it is for stacks
118      * that grow upwards.
119      */
120     
121     static inline int expand_stackup(struct vm_area_struct * vma, unsigned long address)
122     {
123     	unsigned long grow;
124     
125     	address += 4 + PAGE_SIZE - 1;
126     	address &= PAGE_MASK;
127     	grow = (address - vma->vm_end) >> PAGE_SHIFT;
128     	if (address - vma->vm_start > current->rlim[RLIMIT_STACK].rlim_cur ||
129     	    ((vma->vm_mm->total_vm + grow) << PAGE_SHIFT) > current->rlim[RLIMIT_AS].rlim_cur)
130     		return -ENOMEM;
131     	vma->vm_end = address;
132     	vma->vm_mm->total_vm += grow;
133     	if (vma->vm_flags & VM_LOCKED)
134     		vma->vm_mm->locked_vm += grow;
135     	return 0;
136     }
137     
138     
139     /* This is similar to find_vma(), except that it understands that stacks
140      * grow up rather than down.
141      * XXX Optimise by making use of cache and avl tree as per find_vma().
142      */
143     
144     struct vm_area_struct * pa_find_vma(struct mm_struct * mm, unsigned long addr)
145     {
146     	struct vm_area_struct *vma = NULL;
147     
148     	if (mm) {
149     		vma = mm->mmap;
150     		if (!vma || addr < vma->vm_start)
151     			return NULL;
152     		while (vma->vm_next && addr >= vma->vm_next->vm_start)
153     			vma = vma->vm_next;
154     	}
155     	return vma;
156     }
157     
158     
159     /*
160      * This routine handles page faults.  It determines the address,
161      * and the problem, and then passes it off to one of the appropriate
162      * routines.
163      */
164     extern void parisc_terminate(char *, struct pt_regs *, int, unsigned long);
165     
166     void do_page_fault(struct pt_regs *regs, unsigned long code,
167     			      unsigned long address)
168     {
169     	struct vm_area_struct * vma;
170     	struct task_struct *tsk = current;
171     	struct mm_struct *mm = tsk->mm;
172     	const struct exception_table_entry *fix;
173     	unsigned long acc_type;
174     
175     	if (in_interrupt() || !mm)
176     		goto no_context;
177     
178     	down_read(&mm->mmap_sem);
179     	vma = pa_find_vma(mm, address);
180     	if (!vma)
181     		goto bad_area;
182     	if (address < vma->vm_end)
183     		goto good_area;
184     	if (!(vma->vm_flags & VM_GROWSUP) || expand_stackup(vma, address))
185     		goto bad_area;
186     /*
187      * Ok, we have a good vm_area for this memory access. We still need to
188      * check the access permissions.
189      */
190     
191     good_area:
192     
193     	acc_type = parisc_acctyp(code,regs->iir);
194     
195     	if ((vma->vm_flags & acc_type) != acc_type)
196     		goto bad_area;
197     
198     	/*
199     	 * If for any reason at all we couldn't handle the fault, make
200     	 * sure we exit gracefully rather than endlessly redo the
201     	 * fault.
202     	 */
203     
204     	switch (handle_mm_fault(mm, vma, address, (acc_type & VM_WRITE) != 0)) {
205     	      case 1:
206     		++current->min_flt;
207     		break;
208     	      case 2:
209     		++current->maj_flt;
210     		break;
211     	      case 0:
212     		/*
213     		 * We ran out of memory, or some other thing happened
214     		 * to us that made us unable to handle the page fault
215     		 * gracefully.
216     		 */
217     		goto bad_area;
218     	      default:
219     		goto out_of_memory;
220     	}
221     	up_read(&mm->mmap_sem);
222     	return;
223     
224     /*
225      * Something tried to access memory that isn't in our memory map..
226      */
227     bad_area:
228     	up_read(&mm->mmap_sem);
229     
230     	if (user_mode(regs)) {
231     		struct siginfo si;
232     
233     		printk("\ndo_page_fault() pid=%d command='%s'\n",
234     		    tsk->pid, tsk->comm);
235     		show_regs(regs);
236     		/* FIXME: actually we need to get the signo and code correct */
237     		si.si_signo = SIGSEGV;
238     		si.si_errno = 0;
239     		si.si_code = SEGV_MAPERR;
240     		si.si_addr = (void *) address;
241     		force_sig_info(SIGSEGV, &si, current);
242     		return;
243     	}
244     
245     no_context:
246     
247     	if (!user_mode(regs)) {
248     
249     		fix = search_exception_table(regs->iaoq[0]);
250     
251     		if (fix) {
252     
253     			if (fix->skip & 1) 
254     				regs->gr[8] = -EFAULT;
255     			if (fix->skip & 2)
256     				regs->gr[9] = 0;
257     
258     			regs->iaoq[0] += ((fix->skip) & ~3);
259     
260     			/*
261     			 * NOTE: In some cases the faulting instruction
262     			 * may be in the delay slot of a branch. We
263     			 * don't want to take the branch, so we don't
264     			 * increment iaoq[1], instead we set it to be
265     			 * iaoq[0]+4, and clear the B bit in the PSW
266     			 */
267     
268     			regs->iaoq[1] = regs->iaoq[0] + 4;
269     			regs->gr[0] &= ~PSW_B; /* IPSW in gr[0] */
270     
271     			return;
272     		}
273     	}
274     
275     	parisc_terminate("Bad Address (null pointer deref?)",regs,code,address);
276     
277       out_of_memory:
278     	up_read(&mm->mmap_sem);
279     	printk("VM: killing process %s\n", current->comm);
280     	if (user_mode(regs))
281     		do_exit(SIGKILL);
282     	goto no_context;
283     }
284