xref: /linux/arch/alpha/mm/fault.c (revision c537b994505099b7197e7d3125b942ecbcc51eb6)
1 /*
2  *  linux/arch/alpha/mm/fault.c
3  *
4  *  Copyright (C) 1995  Linus Torvalds
5  */
6 
7 #include <linux/sched.h>
8 #include <linux/kernel.h>
9 #include <linux/mm.h>
10 #include <asm/io.h>
11 
12 #define __EXTERN_INLINE inline
13 #include <asm/mmu_context.h>
14 #include <asm/tlbflush.h>
15 #undef  __EXTERN_INLINE
16 
17 #include <linux/signal.h>
18 #include <linux/errno.h>
19 #include <linux/string.h>
20 #include <linux/types.h>
21 #include <linux/ptrace.h>
22 #include <linux/mman.h>
23 #include <linux/smp.h>
24 #include <linux/smp_lock.h>
25 #include <linux/interrupt.h>
26 #include <linux/module.h>
27 
28 #include <asm/system.h>
29 #include <asm/uaccess.h>
30 
31 extern void die_if_kernel(char *,struct pt_regs *,long, unsigned long *);
32 
33 
34 /*
35  * Force a new ASN for a task.
36  */
37 
38 #ifndef CONFIG_SMP
39 unsigned long last_asn = ASN_FIRST_VERSION;
40 #endif
41 
42 void
43 __load_new_mm_context(struct mm_struct *next_mm)
44 {
45 	unsigned long mmc;
46 	struct pcb_struct *pcb;
47 
48 	mmc = __get_new_mm_context(next_mm, smp_processor_id());
49 	next_mm->context[smp_processor_id()] = mmc;
50 
51 	pcb = &current_thread_info()->pcb;
52 	pcb->asn = mmc & HARDWARE_ASN_MASK;
53 	pcb->ptbr = ((unsigned long) next_mm->pgd - IDENT_ADDR) >> PAGE_SHIFT;
54 
55 	__reload_thread(pcb);
56 }
57 
58 
59 /*
60  * This routine handles page faults.  It determines the address,
61  * and the problem, and then passes it off to handle_mm_fault().
62  *
63  * mmcsr:
64  *	0 = translation not valid
65  *	1 = access violation
66  *	2 = fault-on-read
67  *	3 = fault-on-execute
68  *	4 = fault-on-write
69  *
70  * cause:
71  *	-1 = instruction fetch
72  *	0 = load
73  *	1 = store
74  *
75  * Registers $9 through $15 are saved in a block just prior to `regs' and
76  * are saved and restored around the call to allow exception code to
77  * modify them.
78  */
79 
80 /* Macro for exception fixup code to access integer registers.  */
81 #define dpf_reg(r)							\
82 	(((unsigned long *)regs)[(r) <= 8 ? (r) : (r) <= 15 ? (r)-16 :	\
83 				 (r) <= 18 ? (r)+8 : (r)-10])
84 
85 asmlinkage void
86 do_page_fault(unsigned long address, unsigned long mmcsr,
87 	      long cause, struct pt_regs *regs)
88 {
89 	struct vm_area_struct * vma;
90 	struct mm_struct *mm = current->mm;
91 	const struct exception_table_entry *fixup;
92 	int fault, si_code = SEGV_MAPERR;
93 	siginfo_t info;
94 
95 	/* As of EV6, a load into $31/$f31 is a prefetch, and never faults
96 	   (or is suppressed by the PALcode).  Support that for older CPUs
97 	   by ignoring such an instruction.  */
98 	if (cause == 0) {
99 		unsigned int insn;
100 		__get_user(insn, (unsigned int __user *)regs->pc);
101 		if ((insn >> 21 & 0x1f) == 0x1f &&
102 		    /* ldq ldl ldt lds ldg ldf ldwu ldbu */
103 		    (1ul << (insn >> 26) & 0x30f00001400ul)) {
104 			regs->pc += 4;
105 			return;
106 		}
107 	}
108 
109 	/* If we're in an interrupt context, or have no user context,
110 	   we must not take the fault.  */
111 	if (!mm || in_atomic())
112 		goto no_context;
113 
114 #ifdef CONFIG_ALPHA_LARGE_VMALLOC
115 	if (address >= TASK_SIZE)
116 		goto vmalloc_fault;
117 #endif
118 
119 	down_read(&mm->mmap_sem);
120 	vma = find_vma(mm, address);
121 	if (!vma)
122 		goto bad_area;
123 	if (vma->vm_start <= address)
124 		goto good_area;
125 	if (!(vma->vm_flags & VM_GROWSDOWN))
126 		goto bad_area;
127 	if (expand_stack(vma, address))
128 		goto bad_area;
129 
130 	/* Ok, we have a good vm_area for this memory access, so
131 	   we can handle it.  */
132  good_area:
133 	si_code = SEGV_ACCERR;
134 	if (cause < 0) {
135 		if (!(vma->vm_flags & VM_EXEC))
136 			goto bad_area;
137 	} else if (!cause) {
138 		/* Allow reads even for write-only mappings */
139 		if (!(vma->vm_flags & (VM_READ | VM_WRITE)))
140 			goto bad_area;
141 	} else {
142 		if (!(vma->vm_flags & VM_WRITE))
143 			goto bad_area;
144 	}
145 
146  survive:
147 	/* If for any reason at all we couldn't handle the fault,
148 	   make sure we exit gracefully rather than endlessly redo
149 	   the fault.  */
150 	fault = handle_mm_fault(mm, vma, address, cause > 0);
151 	up_read(&mm->mmap_sem);
152 
153 	switch (fault) {
154 	      case VM_FAULT_MINOR:
155 		current->min_flt++;
156 		break;
157 	      case VM_FAULT_MAJOR:
158 		current->maj_flt++;
159 		break;
160 	      case VM_FAULT_SIGBUS:
161 		goto do_sigbus;
162 	      case VM_FAULT_OOM:
163 		goto out_of_memory;
164 	      default:
165 		BUG();
166 	}
167 	return;
168 
169 	/* Something tried to access memory that isn't in our memory map.
170 	   Fix it, but check if it's kernel or user first.  */
171  bad_area:
172 	up_read(&mm->mmap_sem);
173 
174 	if (user_mode(regs))
175 		goto do_sigsegv;
176 
177  no_context:
178 	/* Are we prepared to handle this fault as an exception?  */
179 	if ((fixup = search_exception_tables(regs->pc)) != 0) {
180 		unsigned long newpc;
181 		newpc = fixup_exception(dpf_reg, fixup, regs->pc);
182 		regs->pc = newpc;
183 		return;
184 	}
185 
186 	/* Oops. The kernel tried to access some bad page. We'll have to
187 	   terminate things with extreme prejudice.  */
188 	printk(KERN_ALERT "Unable to handle kernel paging request at "
189 	       "virtual address %016lx\n", address);
190 	die_if_kernel("Oops", regs, cause, (unsigned long*)regs - 16);
191 	do_exit(SIGKILL);
192 
193 	/* We ran out of memory, or some other thing happened to us that
194 	   made us unable to handle the page fault gracefully.  */
195  out_of_memory:
196 	if (is_init(current)) {
197 		yield();
198 		down_read(&mm->mmap_sem);
199 		goto survive;
200 	}
201 	printk(KERN_ALERT "VM: killing process %s(%d)\n",
202 	       current->comm, current->pid);
203 	if (!user_mode(regs))
204 		goto no_context;
205 	do_exit(SIGKILL);
206 
207  do_sigbus:
208 	/* Send a sigbus, regardless of whether we were in kernel
209 	   or user mode.  */
210 	info.si_signo = SIGBUS;
211 	info.si_errno = 0;
212 	info.si_code = BUS_ADRERR;
213 	info.si_addr = (void __user *) address;
214 	force_sig_info(SIGBUS, &info, current);
215 	if (!user_mode(regs))
216 		goto no_context;
217 	return;
218 
219  do_sigsegv:
220 	info.si_signo = SIGSEGV;
221 	info.si_errno = 0;
222 	info.si_code = si_code;
223 	info.si_addr = (void __user *) address;
224 	force_sig_info(SIGSEGV, &info, current);
225 	return;
226 
227 #ifdef CONFIG_ALPHA_LARGE_VMALLOC
228  vmalloc_fault:
229 	if (user_mode(regs))
230 		goto do_sigsegv;
231 	else {
232 		/* Synchronize this task's top level page-table
233 		   with the "reference" page table from init.  */
234 		long index = pgd_index(address);
235 		pgd_t *pgd, *pgd_k;
236 
237 		pgd = current->active_mm->pgd + index;
238 		pgd_k = swapper_pg_dir + index;
239 		if (!pgd_present(*pgd) && pgd_present(*pgd_k)) {
240 			pgd_val(*pgd) = pgd_val(*pgd_k);
241 			return;
242 		}
243 		goto no_context;
244 	}
245 #endif
246 }
247