debuggers.hg

view xenolinux-2.4.21-sparse/arch/xeno/mm/fault.c @ 666:6d07235a19e8

bitkeeper revision 1.339.1.12 (3f132a3btAOPZiDtrKz16GwA9Wr_ow)

memory.c, fault.c, i386_ksyms.c:
Simplified Xenolinux mm code by removing some 386-only functionality.
author kaf24@scramble.cl.cam.ac.uk
date Mon Jul 14 22:10:03 2003 +0000 (2003-07-14)
parents b6f13a6cdded
children 4fd1861ec41a
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 PT_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 < pt_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 tsk->thread.error_code = error_code;
212 tsk->thread.trap_no = 14;
213 info.si_signo = SIGSEGV;
214 info.si_errno = 0;
215 /* info.si_code has been set above */
216 info.si_addr = (void *)address;
217 force_sig_info(SIGSEGV, &info, tsk);
218 return;
219 }
221 no_context:
222 /* Are we prepared to handle this kernel fault? */
223 if ((fixup = search_exception_table(regs->eip)) != 0) {
224 regs->eip = fixup;
225 return;
226 }
228 /*
229 * Oops. The kernel tried to access some bad page. We'll have to
230 * terminate things with extreme prejudice.
231 */
233 bust_spinlocks(1);
235 if (address < PAGE_SIZE)
236 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
237 else
238 printk(KERN_ALERT "Unable to handle kernel paging request");
239 printk(" at virtual address %08lx\n",address);
240 printk(" printing eip:\n");
241 printk("%08lx\n", regs->eip);
242 page = ((unsigned long *) cur_pgd)[address >> 22];
243 printk(KERN_ALERT "*pde=%08lx(%08lx)\n", page, machine_to_phys(page));
244 if (page & 1) {
245 page &= PAGE_MASK;
246 address &= 0x003ff000;
247 page = machine_to_phys(page);
248 page = ((unsigned long *) __va(page))[address >> PAGE_SHIFT];
249 printk(KERN_ALERT "*pte=%08lx(%08lx)\n", page,
250 machine_to_phys(page));
251 }
252 die("Oops", regs, error_code);
253 bust_spinlocks(0);
254 do_exit(SIGKILL);
256 /*
257 * We ran out of memory, or some other thing happened to us that made
258 * us unable to handle the page fault gracefully.
259 */
260 out_of_memory:
261 if (tsk->pid == 1) {
262 yield();
263 goto survive;
264 }
265 up_read(&mm->mmap_sem);
266 printk("VM: killing process %s\n", tsk->comm);
267 if (error_code & 4)
268 do_exit(SIGKILL);
269 goto no_context;
271 do_sigbus:
272 up_read(&mm->mmap_sem);
274 /*
275 * Send a sigbus, regardless of whether we were in kernel
276 * or user mode.
277 */
278 tsk->thread.cr2 = address;
279 tsk->thread.error_code = error_code;
280 tsk->thread.trap_no = 14;
281 info.si_signo = SIGBUS;
282 info.si_errno = 0;
283 info.si_code = BUS_ADRERR;
284 info.si_addr = (void *)address;
285 force_sig_info(SIGBUS, &info, tsk);
287 /* Kernel mode? Handle exceptions or die */
288 if (!(error_code & 4))
289 goto no_context;
290 return;
292 vmalloc_fault:
293 {
294 /*
295 * Synchronize this task's top level page-table
296 * with the 'reference' page table.
297 *
298 * Do _not_ use "tsk" here. We might be inside
299 * an interrupt in the middle of a task switch..
300 */
301 int offset = __pgd_offset(address);
302 pgd_t *pgd, *pgd_k;
303 pmd_t *pmd, *pmd_k;
304 pte_t *pte_k;
306 pgd = offset + cur_pgd;
307 pgd_k = init_mm.pgd + offset;
309 if (!pgd_present(*pgd_k))
310 goto no_context;
311 set_pgd(pgd, *pgd_k);
313 pmd = pmd_offset(pgd, address);
314 pmd_k = pmd_offset(pgd_k, address);
315 if (!pmd_present(*pmd_k))
316 goto no_context;
317 set_pmd(pmd, *pmd_k);
318 XENO_flush_page_update_queue(); /* flush PMD update */
320 pte_k = pte_offset(pmd_k, address);
321 if (!pte_present(*pte_k))
322 goto no_context;
323 return;
324 }
325 }