1/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License.  See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (C) 1995 - 2000 by Ralf Baechle
7 */
8#include <linux/config.h>
9#include <linux/signal.h>
10#include <linux/sched.h>
11#include <linux/interrupt.h>
12#include <linux/kernel.h>
13#include <linux/errno.h>
14#include <linux/string.h>
15#include <linux/types.h>
16#include <linux/ptrace.h>
17#include <linux/mman.h>
18#include <linux/mm.h>
19#include <linux/smp.h>
20#include <linux/smp_lock.h>
21#include <linux/version.h>
22
23#include <asm/branch.h>
24#include <asm/hardirq.h>
25#include <asm/pgalloc.h>
26#include <asm/mmu_context.h>
27#include <asm/softirq.h>
28#include <asm/system.h>
29#include <asm/uaccess.h>
30
31#define development_version (LINUX_VERSION_CODE & 0x100)
32
33/*
34 * Macro for exception fixup code to access integer registers.
35 */
36#define dpf_reg(r) (regs->regs[r])
37
38extern spinlock_t timerlist_lock;
39
40/*
41 * Unlock any spinlocks which will prevent us from getting the
42 * message out (timerlist_lock is acquired through the
43 * console unblank code)
44 */
45void bust_spinlocks(int yes)
46{
47	spin_lock_init(&timerlist_lock);
48	if (yes) {
49		oops_in_progress = 1;
50#ifdef CONFIG_SMP
51		/* Many serial drivers do __global_cli() */
52		global_irq_lock = SPIN_LOCK_UNLOCKED;
53#endif
54	} else {
55		int loglevel_save = console_loglevel;
56#ifdef CONFIG_VT
57		unblank_screen();
58#endif
59		oops_in_progress = 0;
60		/*
61		 * OK, the message is on the console.  Now we call printk()
62		 * without oops_in_progress set so that printk will give klogd
63		 * a poke.  Hold onto your hats...
64		 */
65		console_loglevel = 15;		/* NMI oopser may have shut the console up */
66		printk(" ");
67		console_loglevel = loglevel_save;
68	}
69}
70
71/*
72 * This routine handles page faults.  It determines the address,
73 * and the problem, and then passes it off to one of the appropriate
74 * routines.
75 */
76asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write,
77			      unsigned long address)
78{
79	struct vm_area_struct * vma;
80	struct task_struct *tsk = current;
81	struct mm_struct *mm = tsk->mm;
82	unsigned long fixup;
83	siginfo_t info;
84
85	/*
86	 * We fault-in kernel-space virtual memory on-demand. The
87	 * 'reference' page table is init_mm.pgd.
88	 *
89	 * NOTE! We MUST NOT take any locks for this case. We may
90	 * be in an interrupt or a critical region, and should
91	 * only copy the information from the master page table,
92	 * nothing more.
93	 */
94	if (address >= VMALLOC_START)
95		goto vmalloc_fault;
96
97	info.si_code = SEGV_MAPERR;
98	/*
99	 * If we're in an interrupt or have no user
100	 * context, we must not take the fault..
101	 */
102	if (in_interrupt() || !mm)
103		goto no_context;
104	down_read(&mm->mmap_sem);
105	vma = find_vma(mm, address);
106	if (!vma)
107		goto bad_area;
108	if (vma->vm_start <= address)
109		goto good_area;
110	if (!(vma->vm_flags & VM_GROWSDOWN))
111		goto bad_area;
112	if (expand_stack(vma, address))
113		goto bad_area;
114/*
115 * Ok, we have a good vm_area for this memory access, so
116 * we can handle it..
117 */
118good_area:
119	info.si_code = SEGV_ACCERR;
120
121	if (write) {
122		if (!(vma->vm_flags & VM_WRITE))
123			goto bad_area;
124	} else {
125		if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
126			goto bad_area;
127	}
128
129survive:
130	/*
131	 * If for any reason at all we couldn't handle the fault,
132	 * make sure we exit gracefully rather than endlessly redo
133	 * the fault.
134	 */
135	switch (handle_mm_fault(mm, vma, address, write)) {
136	case 1:
137		tsk->min_flt++;
138		break;
139	case 2:
140		tsk->maj_flt++;
141		break;
142	case 0:
143		goto do_sigbus;
144	default:
145		goto out_of_memory;
146	}
147
148	up_read(&mm->mmap_sem);
149	return;
150
151/*
152 * Something tried to access memory that isn't in our memory map..
153 * Fix it, but check if it's kernel or user first..
154 */
155bad_area:
156	up_read(&mm->mmap_sem);
157
158	/* User mode accesses just cause a SIGSEGV */
159	if (user_mode(regs)) {
160		tsk->thread.cp0_badvaddr = address;
161		tsk->thread.error_code = write;
162		info.si_signo = SIGSEGV;
163		info.si_errno = 0;
164		/* info.si_code has been set above */
165		info.si_addr = (void *) address;
166		force_sig_info(SIGSEGV, &info, tsk);
167		return;
168	}
169
170no_context:
171	/* Are we prepared to handle this kernel fault?  */
172	fixup = search_exception_table(exception_epc(regs));
173	if (fixup) {
174		long new_epc;
175
176		tsk->thread.cp0_baduaddr = address;
177		new_epc = fixup_exception(dpf_reg, fixup, regs->cp0_epc);
178		if (development_version)
179			printk(KERN_DEBUG "%s: Exception at [<%lx>] (%lx)\n",
180			       tsk->comm, regs->cp0_epc, new_epc);
181		regs->cp0_epc = new_epc;
182		return;
183	}
184
185	/*
186	 * Oops. The kernel tried to access some bad page. We'll have to
187	 * terminate things with extreme prejudice.
188	 */
189	printk(KERN_ALERT "Unable to handle kernel paging request at virtual "
190	       "address %08lx, epc == %08lx, ra == %08lx\n",
191	       address, regs->cp0_epc, regs->regs[31]);
192	die("Oops", regs);
193	/* Game over.  */
194
195/*
196 * We ran out of memory, or some other thing happened to us that made
197 * us unable to handle the page fault gracefully.
198 */
199out_of_memory:
200	if (tsk->pid == 1) {
201		yield();
202		goto survive;
203	}
204	up_read(&mm->mmap_sem);
205	printk(KERN_NOTICE "VM: killing process %s\n", tsk->comm);
206	if (user_mode(regs))
207		do_exit(SIGKILL);
208	goto no_context;
209
210do_sigbus:
211	up_read(&mm->mmap_sem);
212
213	/*
214	 * Send a sigbus, regardless of whether we were in kernel
215	 * or user mode.
216	 */
217	tsk->thread.cp0_badvaddr = address;
218	info.si_signo = SIGBUS;
219	info.si_errno = 0;
220	info.si_code = BUS_ADRERR;
221	info.si_addr = (void *) address;
222	force_sig_info(SIGBUS, &info, tsk);
223
224	/* Kernel mode? Handle exceptions or die */
225	if (!user_mode(regs))
226		goto no_context;
227
228	return;
229
230vmalloc_fault:
231	{
232		/*
233		 * Synchronize this task's top level page-table
234		 * with the 'reference' page table.
235		 *
236		 * Do _not_ use "tsk" here. We might be inside
237		 * an interrupt in the middle of a task switch..
238		 */
239		int offset = __pgd_offset(address);
240		pgd_t *pgd, *pgd_k;
241		pmd_t *pmd, *pmd_k;
242		pte_t *pte_k;
243
244		pgd = (pgd_t *) pgd_current[smp_processor_id()] + offset;
245		pgd_k = init_mm.pgd + offset;
246
247		if (!pgd_present(*pgd_k))
248			goto no_context;
249		set_pgd(pgd, *pgd_k);
250
251		pmd = pmd_offset(pgd, address);
252		pmd_k = pmd_offset(pgd_k, address);
253		if (!pmd_present(*pmd_k))
254			goto no_context;
255		set_pmd(pmd, *pmd_k);
256
257		pte_k = pte_offset(pmd_k, address);
258		if (!pte_present(*pte_k))
259			goto no_context;
260		return;
261	}
262}
263