ia64/xen-unstable

view linux-2.4-xen-sparse/arch/xen/mm/fault.c @ 5853:9b713b8d1100

Fix the path to qemu-dm
author kaf24@firebug.cl.cam.ac.uk
date Mon Jul 25 21:02:24 2005 +0000 (2005-07-25)
parents 56a63f9f378f
children 8799d14bef77 8799d14bef77 9312a3e8a6f8 dfaf788ab18c
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 /*
88 * We fault-in kernel-space virtual memory on-demand. The
89 * 'reference' page table is init_mm.pgd.
90 *
91 * NOTE! We MUST NOT take any locks for this case. We may
92 * be in an interrupt or a critical region, and should
93 * only copy the information from the master page table,
94 * nothing more.
95 *
96 * This verifies that the fault happens in kernel space
97 * (error_code & 4) == 0, and that the fault was not a
98 * protection error (error_code & 1) == 0.
99 */
100 if (address >= TASK_SIZE && !(error_code & 5))
101 goto vmalloc_fault;
103 mm = tsk->mm;
104 info.si_code = SEGV_MAPERR;
106 /*
107 * If we're in an interrupt or have no user
108 * context, we must not take the fault..
109 */
110 if (in_interrupt() || !mm)
111 goto no_context;
113 down_read(&mm->mmap_sem);
115 vma = find_vma(mm, address);
116 if (!vma)
117 goto bad_area;
118 if (vma->vm_start <= address)
119 goto good_area;
120 if (!(vma->vm_flags & VM_GROWSDOWN))
121 goto bad_area;
122 if (error_code & 4) {
123 /*
124 * accessing the stack below %esp is always a bug.
125 * The "+ 32" is there due to some instructions (like
126 * pusha) doing post-decrement on the stack and that
127 * doesn't show up until later..
128 */
129 if (address + 32 < regs->esp)
130 goto bad_area;
131 }
132 if (expand_stack(vma, address))
133 goto bad_area;
134 /*
135 * Ok, we have a good vm_area for this memory access, so
136 * we can handle it..
137 */
138 good_area:
139 info.si_code = SEGV_ACCERR;
140 write = 0;
141 switch (error_code & 3) {
142 default: /* 3: write, present */
143 /* fall through */
144 case 2: /* write, not present */
145 if (!(vma->vm_flags & VM_WRITE))
146 goto bad_area;
147 write++;
148 break;
149 case 1: /* read, present */
150 goto bad_area;
151 case 0: /* read, not present */
152 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
153 goto bad_area;
154 }
156 survive:
157 /*
158 * If for any reason at all we couldn't handle the fault,
159 * make sure we exit gracefully rather than endlessly redo
160 * the fault.
161 */
162 switch (handle_mm_fault(mm, vma, address, write)) {
163 case 1:
164 tsk->min_flt++;
165 break;
166 case 2:
167 tsk->maj_flt++;
168 break;
169 case 0:
170 goto do_sigbus;
171 default:
172 goto out_of_memory;
173 }
175 up_read(&mm->mmap_sem);
176 return;
178 /*
179 * Something tried to access memory that isn't in our memory map..
180 * Fix it, but check if it's kernel or user first..
181 */
182 bad_area:
183 up_read(&mm->mmap_sem);
185 /* User mode accesses just cause a SIGSEGV */
186 if (error_code & 4) {
187 tsk->thread.cr2 = address;
188 /* Kernel addresses are always protection faults */
189 tsk->thread.error_code = error_code | (address >= TASK_SIZE);
190 tsk->thread.trap_no = 14;
191 info.si_signo = SIGSEGV;
192 info.si_errno = 0;
193 /* info.si_code has been set above */
194 info.si_addr = (void *)address;
195 force_sig_info(SIGSEGV, &info, tsk);
196 return;
197 }
199 no_context:
200 /* Are we prepared to handle this kernel fault? */
201 if ((fixup = search_exception_table(regs->eip)) != 0) {
202 regs->eip = fixup;
203 return;
204 }
206 /*
207 * Oops. The kernel tried to access some bad page. We'll have to
208 * terminate things with extreme prejudice.
209 */
211 bust_spinlocks(1);
213 if (address < PAGE_SIZE)
214 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
215 else
216 printk(KERN_ALERT "Unable to handle kernel paging request");
217 printk(" at virtual address %08lx\n",address);
218 printk(" printing eip:\n");
219 printk("%08lx\n", regs->eip);
220 page = ((unsigned long *) cur_pgd)[address >> 22];
221 printk(KERN_ALERT "*pde=%08lx(%08lx)\n", page, machine_to_phys(page));
222 if (page & 1) {
223 page &= PAGE_MASK;
224 address &= 0x003ff000;
225 page = machine_to_phys(page);
226 page = ((unsigned long *) __va(page))[address >> PAGE_SHIFT];
227 printk(KERN_ALERT "*pte=%08lx(%08lx)\n", page,
228 machine_to_phys(page));
229 }
230 die("Oops", regs, error_code);
231 bust_spinlocks(0);
232 do_exit(SIGKILL);
234 /*
235 * We ran out of memory, or some other thing happened to us that made
236 * us unable to handle the page fault gracefully.
237 */
238 out_of_memory:
239 if (tsk->pid == 1) {
240 yield();
241 goto survive;
242 }
243 up_read(&mm->mmap_sem);
244 printk("VM: killing process %s\n", tsk->comm);
245 if (error_code & 4)
246 do_exit(SIGKILL);
247 goto no_context;
249 do_sigbus:
250 up_read(&mm->mmap_sem);
252 /*
253 * Send a sigbus, regardless of whether we were in kernel
254 * or user mode.
255 */
256 tsk->thread.cr2 = address;
257 tsk->thread.error_code = error_code;
258 tsk->thread.trap_no = 14;
259 info.si_signo = SIGBUS;
260 info.si_errno = 0;
261 info.si_code = BUS_ADRERR;
262 info.si_addr = (void *)address;
263 force_sig_info(SIGBUS, &info, tsk);
265 /* Kernel mode? Handle exceptions or die */
266 if (!(error_code & 4))
267 goto no_context;
268 return;
270 vmalloc_fault:
271 {
272 /*
273 * Synchronize this task's top level page-table
274 * with the 'reference' page table.
275 *
276 * Do _not_ use "tsk" here. We might be inside
277 * an interrupt in the middle of a task switch..
278 */
279 int offset = __pgd_offset(address);
280 pgd_t *pgd, *pgd_k;
281 pmd_t *pmd, *pmd_k;
282 pte_t *pte_k;
284 pgd = offset + cur_pgd;
285 pgd_k = init_mm.pgd + offset;
287 if (!pgd_present(*pgd_k))
288 goto no_context;
289 set_pgd(pgd, *pgd_k);
291 pmd = pmd_offset(pgd, address);
292 pmd_k = pmd_offset(pgd_k, address);
293 if (!pmd_present(*pmd_k))
294 goto no_context;
295 set_pmd(pmd, *pmd_k);
297 pte_k = pte_offset(pmd_k, address);
298 if (!pte_present(*pte_k))
299 goto no_context;
300 return;
301 }
302 }