ia64/xen-unstable

view linux-2.4.26-xen-sparse/arch/xen/mm/fault.c @ 1774:131c48baa117

bitkeeper revision 1.1071.1.5 (40f41ae00utn5d2f3tlNLcvG_QhiBA)

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