File: /usr/src/linux/arch/ia64/mm/fault.c

1     /*
2      * MMU fault handling support.
3      *
4      * Copyright (C) 1998-2000 Hewlett-Packard Co
5      * Copyright (C) 1998-2000 David Mosberger-Tang <davidm@hpl.hp.com>
6      */
7     #include <linux/sched.h>
8     #include <linux/kernel.h>
9     #include <linux/mm.h>
10     #include <linux/smp_lock.h>
11     #include <linux/interrupt.h>
12     
13     #include <asm/pgtable.h>
14     #include <asm/processor.h>
15     #include <asm/system.h>
16     #include <asm/uaccess.h>
17     #include <asm/hardirq.h>
18     
19     extern void die_if_kernel (char *, struct pt_regs *, long);
20     
21     /*
22      * This routine is analogous to expand_stack() but instead grows the
23      * register backing store (which grows towards higher addresses).
24      * Since the register backing store is access sequentially, we
25      * disallow growing the RBS by more than a page at a time.  Note that
26      * the VM_GROWSUP flag can be set on any VM area but that's fine
27      * because the total process size is still limited by RLIMIT_STACK and
28      * RLIMIT_AS.
29      */
30     static inline long
31     expand_backing_store (struct vm_area_struct *vma, unsigned long address)
32     {
33     	unsigned long grow;
34     
35     	grow = PAGE_SIZE >> PAGE_SHIFT;
36     	if (address - vma->vm_start > current->rlim[RLIMIT_STACK].rlim_cur
37     	    || (((vma->vm_mm->total_vm + grow) << PAGE_SHIFT) > current->rlim[RLIMIT_AS].rlim_cur))
38     		return -ENOMEM;
39     	vma->vm_end += PAGE_SIZE;
40     	vma->vm_mm->total_vm += grow;
41     	if (vma->vm_flags & VM_LOCKED)
42     		vma->vm_mm->locked_vm += grow;
43     	return 0;
44     }
45     
46     void
47     ia64_do_page_fault (unsigned long address, unsigned long isr, struct pt_regs *regs)
48     {
49     	struct mm_struct *mm = current->mm;
50     	struct exception_fixup fix;
51     	struct vm_area_struct *vma, *prev_vma;
52     	struct siginfo si;
53     	int signal = SIGSEGV;
54     	unsigned long mask;
55     
56     	/*
57     	 * If we're in an interrupt or have no user
58     	 * context, we must not take the fault..
59     	 */
60     	if (in_interrupt() || !mm)
61     		goto no_context;
62     
63     	down_read(&mm->mmap_sem);
64     
65     	vma = find_vma_prev(mm, address, &prev_vma);
66     	if (!vma)
67     		goto bad_area;
68     
69     	/* find_vma_prev() returns vma such that address < vma->vm_end or NULL */
70     	if (address < vma->vm_start)
71     		goto check_expansion;
72     
73       good_area:
74     	/* OK, we've got a good vm_area for this memory area.  Check the access permissions: */
75     
76     #	define VM_READ_BIT	0
77     #	define VM_WRITE_BIT	1
78     #	define VM_EXEC_BIT	2
79     
80     #	if (((1 << VM_READ_BIT) != VM_READ || (1 << VM_WRITE_BIT) != VM_WRITE) \
81     	    || (1 << VM_EXEC_BIT) != VM_EXEC)
82     #		error File is out of sync with <linux/mm.h>.  Pleaes update.
83     #	endif
84     
85     	mask = (  (((isr >> IA64_ISR_X_BIT) & 1UL) << VM_EXEC_BIT)
86     		| (((isr >> IA64_ISR_W_BIT) & 1UL) << VM_WRITE_BIT)
87     		| (((isr >> IA64_ISR_R_BIT) & 1UL) << VM_READ_BIT));
88     
89     	if ((vma->vm_flags & mask) != mask)
90     		goto bad_area;
91     
92     	/*
93     	 * If for any reason at all we couldn't handle the fault, make
94     	 * sure we exit gracefully rather than endlessly redo the
95     	 * fault.
96     	 */
97     	switch (handle_mm_fault(mm, vma, address, mask) != 0) {
98     	      case 1:
99     		++current->min_flt;
100     		break;
101     	      case 2:
102     		++current->maj_flt;
103     		break;
104     	      case 0:
105     		/*
106     		 * We ran out of memory, or some other thing happened
107     		 * to us that made us unable to handle the page fault
108     		 * gracefully.
109     		 */
110     		signal = SIGBUS;
111     		goto bad_area;
112     	      default:
113     		goto out_of_memory;
114     	}
115     	up_read(&mm->mmap_sem);
116     	return;
117     
118       check_expansion:
119     	if (!(prev_vma && (prev_vma->vm_flags & VM_GROWSUP) && (address == prev_vma->vm_end))) {
120     		if (!(vma->vm_flags & VM_GROWSDOWN))
121     			goto bad_area;
122     		if (rgn_index(address) != rgn_index(vma->vm_start)
123     		    || rgn_offset(address) >= RGN_MAP_LIMIT)
124     			goto bad_area;
125     		if (expand_stack(vma, address))
126     			goto bad_area;
127     	} else {
128     		vma = prev_vma;
129     		if (rgn_index(address) != rgn_index(vma->vm_start)
130     		    || rgn_offset(address) >= RGN_MAP_LIMIT)
131     			goto bad_area;
132     		if (expand_backing_store(vma, address))
133     			goto bad_area;
134     	}
135     	goto good_area;
136     
137       bad_area:
138     	up_read(&mm->mmap_sem);
139     	if (isr & IA64_ISR_SP) {
140     		/*
141     		 * This fault was due to a speculative load set the "ed" bit in the psr to
142     		 * ensure forward progress (target register will get a NaT).
143     		 */
144     		ia64_psr(regs)->ed = 1;
145     		return;
146     	}
147     	if (user_mode(regs)) {
148     		si.si_signo = signal;
149     		si.si_errno = 0;
150     		si.si_code = SI_KERNEL;
151     		si.si_addr = (void *) address;
152     		force_sig_info(signal, &si, current);
153     		return;
154     	}
155     
156       no_context:
157     	if (isr & IA64_ISR_SP) {
158     		/*
159     		 * This fault was due to a speculative load set the "ed" bit in the psr to
160     		 * ensure forward progress (target register will get a NaT).
161     		 */
162     		ia64_psr(regs)->ed = 1;
163     		return;
164     	}
165     
166     #ifdef GAS_HAS_LOCAL_TAGS
167     	fix = search_exception_table(regs->cr_iip + ia64_psr(regs)->ri);
168     #else
169     	fix = search_exception_table(regs->cr_iip);
170     #endif
171     	if (fix.cont) {
172     		handle_exception(regs, fix);
173     		return;
174     	}
175     
176     	/*
177     	 * Oops. The kernel tried to access some bad page. We'll have
178     	 * to terminate things with extreme prejudice.
179     	 */
180     	printk(KERN_ALERT "Unable to handle kernel paging request at "
181     	       "virtual address %016lx\n", address);
182     	die_if_kernel("Oops", regs, isr);
183     	do_exit(SIGKILL);
184     	return;
185     
186       out_of_memory:
187     	up_read(&mm->mmap_sem);
188     	printk("VM: killing process %s\n", current->comm);
189     	if (user_mode(regs))
190     		do_exit(SIGKILL);
191     	goto no_context;
192     }
193