ia64/xen-unstable

view linux-2.4.29-xen-sparse/arch/xen/mm/fault.c @ 3516:1a4f61d36171

bitkeeper revision 1.1159.223.31 (41f599bcklevTYwPtWQUZ7QK-azDbg)

Fix recent patch to change the way the version string is generated.
Signed-off-by: ian.pratt@cl.cam.ac.uk
author iap10@freefall.cl.cam.ac.uk
date Tue Jan 25 00:58:36 2005 +0000 (2005-01-25)
parents ed0d4ce83995
children d126cac32f08 0dc3b8b8c298
line source
1 /*
2 * linux/arch/i386/mm/fault.c
3 *
4 * Copyright (C) 1995 Linus Torvalds
5 */
7 #include <linux/signal.h>
8 #include <linux/sched.h>
9 #include <linux/kernel.h>
10 #include <linux/errno.h>
11 #include <linux/string.h>
12 #include <linux/types.h>
13 #include <linux/ptrace.h>
14 #include <linux/mman.h>
15 #include <linux/mm.h>
16 #include <linux/smp.h>
17 #include <linux/smp_lock.h>
18 #include <linux/interrupt.h>
19 #include <linux/init.h>
20 #include <linux/tty.h>
21 #include <linux/vt_kern.h> /* For unblank_screen() */
23 #include <asm/system.h>
24 #include <asm/uaccess.h>
25 #include <asm/pgalloc.h>
26 #include <asm/hardirq.h>
28 extern void die(const char *,struct pt_regs *,long);
30 pgd_t *cur_pgd;
32 extern spinlock_t timerlist_lock;
34 /*
35 * Unlock any spinlocks which will prevent us from getting the
36 * message out (timerlist_lock is acquired through the
37 * console unblank code)
38 */
39 void bust_spinlocks(int yes)
40 {
41 spin_lock_init(&timerlist_lock);
42 if (yes) {
43 oops_in_progress = 1;
44 } else {
45 int loglevel_save = console_loglevel;
46 #ifdef CONFIG_VT
47 unblank_screen();
48 #endif
49 oops_in_progress = 0;
50 /*
51 * OK, the message is on the console. Now we call printk()
52 * without oops_in_progress set so that printk will give klogd
53 * a poke. Hold onto your hats...
54 */
55 console_loglevel = 15; /* NMI oopser may have shut the console up */
56 printk(" ");
57 console_loglevel = loglevel_save;
58 }
59 }
61 /*
62 * This routine handles page faults. It determines the address,
63 * and the problem, and then passes it off to one of the appropriate
64 * routines.
65 *
66 * error_code:
67 * bit 0 == 0 means no page found, 1 means protection fault
68 * bit 1 == 0 means read, 1 means write
69 * bit 2 == 0 means kernel, 1 means user-mode
70 */
71 asmlinkage void do_page_fault(struct pt_regs *regs,
72 unsigned long error_code,
73 unsigned long address)
74 {
75 struct task_struct *tsk = current;
76 struct mm_struct *mm;
77 struct vm_area_struct * vma;
78 unsigned long page;
79 unsigned long fixup;
80 int write;
81 siginfo_t info;
83 /* Set the "privileged fault" bit to something sane. */
84 error_code &= 3;
85 error_code |= (regs->xcs & 2) << 1;
87 if ( flush_page_update_queue() != 0 )
88 return;
90 /*
91 * We fault-in kernel-space virtual memory on-demand. The
92 * 'reference' page table is init_mm.pgd.
93 *
94 * NOTE! We MUST NOT take any locks for this case. We may
95 * be in an interrupt or a critical region, and should
96 * only copy the information from the master page table,
97 * nothing more.
98 *
99 * This verifies that the fault happens in kernel space
100 * (error_code & 4) == 0, and that the fault was not a
101 * protection error (error_code & 1) == 0.
102 */
103 if (address >= TASK_SIZE && !(error_code & 5))
104 goto vmalloc_fault;
106 mm = tsk->mm;
107 info.si_code = SEGV_MAPERR;
109 /*
110 * If we're in an interrupt or have no user
111 * context, we must not take the fault..
112 */
113 if (in_interrupt() || !mm)
114 goto no_context;
116 down_read(&mm->mmap_sem);
118 vma = find_vma(mm, address);
119 if (!vma)
120 goto bad_area;
121 if (vma->vm_start <= address)
122 goto good_area;
123 if (!(vma->vm_flags & VM_GROWSDOWN))
124 goto bad_area;
125 if (error_code & 4) {
126 /*
127 * accessing the stack below %esp is always a bug.
128 * The "+ 32" is there due to some instructions (like
129 * pusha) doing post-decrement on the stack and that
130 * doesn't show up until later..
131 */
132 if (address + 32 < regs->esp)
133 goto bad_area;
134 }
135 if (expand_stack(vma, address))
136 goto bad_area;
137 /*
138 * Ok, we have a good vm_area for this memory access, so
139 * we can handle it..
140 */
141 good_area:
142 info.si_code = SEGV_ACCERR;
143 write = 0;
144 switch (error_code & 3) {
145 default: /* 3: write, present */
146 /* fall through */
147 case 2: /* write, not present */
148 if (!(vma->vm_flags & VM_WRITE))
149 goto bad_area;
150 write++;
151 break;
152 case 1: /* read, present */
153 goto bad_area;
154 case 0: /* read, not present */
155 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
156 goto bad_area;
157 }
159 survive:
160 /*
161 * If for any reason at all we couldn't handle the fault,
162 * make sure we exit gracefully rather than endlessly redo
163 * the fault.
164 */
165 switch (handle_mm_fault(mm, vma, address, write)) {
166 case 1:
167 tsk->min_flt++;
168 break;
169 case 2:
170 tsk->maj_flt++;
171 break;
172 case 0:
173 goto do_sigbus;
174 default:
175 goto out_of_memory;
176 }
178 up_read(&mm->mmap_sem);
179 return;
181 /*
182 * Something tried to access memory that isn't in our memory map..
183 * Fix it, but check if it's kernel or user first..
184 */
185 bad_area:
186 up_read(&mm->mmap_sem);
188 /* User mode accesses just cause a SIGSEGV */
189 if (error_code & 4) {
190 tsk->thread.cr2 = address;
191 /* Kernel addresses are always protection faults */
192 tsk->thread.error_code = error_code | (address >= TASK_SIZE);
193 tsk->thread.trap_no = 14;
194 info.si_signo = SIGSEGV;
195 info.si_errno = 0;
196 /* info.si_code has been set above */
197 info.si_addr = (void *)address;
198 force_sig_info(SIGSEGV, &info, tsk);
199 return;
200 }
202 no_context:
203 /* Are we prepared to handle this kernel fault? */
204 if ((fixup = search_exception_table(regs->eip)) != 0) {
205 regs->eip = fixup;
206 return;
207 }
209 /*
210 * Oops. The kernel tried to access some bad page. We'll have to
211 * terminate things with extreme prejudice.
212 */
214 bust_spinlocks(1);
216 if (address < PAGE_SIZE)
217 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
218 else
219 printk(KERN_ALERT "Unable to handle kernel paging request");
220 printk(" at virtual address %08lx\n",address);
221 printk(" printing eip:\n");
222 printk("%08lx\n", regs->eip);
223 page = ((unsigned long *) cur_pgd)[address >> 22];
224 printk(KERN_ALERT "*pde=%08lx(%08lx)\n", page, machine_to_phys(page));
225 if (page & 1) {
226 page &= PAGE_MASK;
227 address &= 0x003ff000;
228 page = machine_to_phys(page);
229 page = ((unsigned long *) __va(page))[address >> PAGE_SHIFT];
230 printk(KERN_ALERT "*pte=%08lx(%08lx)\n", page,
231 machine_to_phys(page));
232 }
233 die("Oops", regs, error_code);
234 bust_spinlocks(0);
235 do_exit(SIGKILL);
237 /*
238 * We ran out of memory, or some other thing happened to us that made
239 * us unable to handle the page fault gracefully.
240 */
241 out_of_memory:
242 if (tsk->pid == 1) {
243 yield();
244 goto survive;
245 }
246 up_read(&mm->mmap_sem);
247 printk("VM: killing process %s\n", tsk->comm);
248 if (error_code & 4)
249 do_exit(SIGKILL);
250 goto no_context;
252 do_sigbus:
253 up_read(&mm->mmap_sem);
255 /*
256 * Send a sigbus, regardless of whether we were in kernel
257 * or user mode.
258 */
259 tsk->thread.cr2 = address;
260 tsk->thread.error_code = error_code;
261 tsk->thread.trap_no = 14;
262 info.si_signo = SIGBUS;
263 info.si_errno = 0;
264 info.si_code = BUS_ADRERR;
265 info.si_addr = (void *)address;
266 force_sig_info(SIGBUS, &info, tsk);
268 /* Kernel mode? Handle exceptions or die */
269 if (!(error_code & 4))
270 goto no_context;
271 return;
273 vmalloc_fault:
274 {
275 /*
276 * Synchronize this task's top level page-table
277 * with the 'reference' page table.
278 *
279 * Do _not_ use "tsk" here. We might be inside
280 * an interrupt in the middle of a task switch..
281 */
282 int offset = __pgd_offset(address);
283 pgd_t *pgd, *pgd_k;
284 pmd_t *pmd, *pmd_k;
285 pte_t *pte_k;
287 pgd = offset + cur_pgd;
288 pgd_k = init_mm.pgd + offset;
290 if (!pgd_present(*pgd_k))
291 goto no_context;
292 set_pgd(pgd, *pgd_k);
294 pmd = pmd_offset(pgd, address);
295 pmd_k = pmd_offset(pgd_k, address);
296 if (!pmd_present(*pmd_k))
297 goto no_context;
298 set_pmd(pmd, *pmd_k);
299 XEN_flush_page_update_queue(); /* flush PMD update */
301 pte_k = pte_offset(pmd_k, address);
302 if (!pte_present(*pte_k))
303 goto no_context;
304 return;
305 }
306 }