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