xref: /openbmc/linux/arch/parisc/mm/fault.c (revision c595db6d7c8bcf87ef42204391fa890e5950e566)
1071327ecSAlexander Beregalov /*
21da177e4SLinus Torvalds  * This file is subject to the terms and conditions of the GNU General Public
31da177e4SLinus Torvalds  * License.  See the file "COPYING" in the main directory of this archive
41da177e4SLinus Torvalds  * for more details.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Copyright (C) 1995, 1996, 1997, 1998 by Ralf Baechle
81da177e4SLinus Torvalds  * Copyright 1999 SuSE GmbH (Philipp Rumpf, prumpf@tux.org)
91da177e4SLinus Torvalds  * Copyright 1999 Hewlett Packard Co.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds #include <linux/mm.h>
141da177e4SLinus Torvalds #include <linux/ptrace.h>
151da177e4SLinus Torvalds #include <linux/sched.h>
16b17b0153SIngo Molnar #include <linux/sched/debug.h>
171da177e4SLinus Torvalds #include <linux/interrupt.h>
18a38671d6SPaul Gortmaker #include <linux/extable.h>
1970ffdb93SDavid Hildenbrand #include <linux/uaccess.h>
20606f95e4SHelge Deller #include <linux/hugetlb.h>
21af8a7926SPeter Xu #include <linux/perf_event.h>
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds #include <asm/traps.h>
241da177e4SLinus Torvalds 
2567c35a3bSJohn David Anglin #define DEBUG_NATLB 0
2667c35a3bSJohn David Anglin 
271da177e4SLinus Torvalds /* Various important other fields */
281da177e4SLinus Torvalds #define bit22set(x)		(x & 0x00000200)
291da177e4SLinus Torvalds #define bits23_25set(x)		(x & 0x000001c0)
301da177e4SLinus Torvalds #define isGraphicsFlushRead(x)	((x & 0xfc003fdf) == 0x04001a80)
311da177e4SLinus Torvalds 				/* extended opcode is 0x6a */
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds #define BITSSET		0x1c0	/* for identifying LDCW */
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds 
36fef47e2aSHelge Deller int show_unhandled_signals = 1;
37fef47e2aSHelge Deller 
381da177e4SLinus Torvalds /*
391da177e4SLinus Torvalds  * parisc_acctyp(unsigned int inst) --
401da177e4SLinus Torvalds  *    Given a PA-RISC memory access instruction, determine if the
4116bf37bfSJason Wang  *    instruction would perform a memory read or memory write
421da177e4SLinus Torvalds  *    operation.
431da177e4SLinus Torvalds  *
441da177e4SLinus Torvalds  *    This function assumes that the given instruction is a memory access
451da177e4SLinus Torvalds  *    instruction (i.e. you should really only call it if you know that
461da177e4SLinus Torvalds  *    the instruction has generated some sort of a memory access fault).
471da177e4SLinus Torvalds  *
481da177e4SLinus Torvalds  * Returns:
491da177e4SLinus Torvalds  *   VM_READ  if read operation
501da177e4SLinus Torvalds  *   VM_WRITE if write operation
511da177e4SLinus Torvalds  *   VM_EXEC  if execute operation
521da177e4SLinus Torvalds  */
53a348eab3SHelge Deller unsigned long
parisc_acctyp(unsigned long code,unsigned int inst)541da177e4SLinus Torvalds parisc_acctyp(unsigned long code, unsigned int inst)
551da177e4SLinus Torvalds {
561da177e4SLinus Torvalds 	if (code == 6 || code == 16)
571da177e4SLinus Torvalds 	    return VM_EXEC;
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds 	switch (inst & 0xf0000000) {
601da177e4SLinus Torvalds 	case 0x40000000: /* load */
611da177e4SLinus Torvalds 	case 0x50000000: /* new load */
621da177e4SLinus Torvalds 		return VM_READ;
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds 	case 0x60000000: /* store */
651da177e4SLinus Torvalds 	case 0x70000000: /* new store */
661da177e4SLinus Torvalds 		return VM_WRITE;
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds 	case 0x20000000: /* coproc */
691da177e4SLinus Torvalds 	case 0x30000000: /* coproc2 */
701da177e4SLinus Torvalds 		if (bit22set(inst))
711da177e4SLinus Torvalds 			return VM_WRITE;
72df561f66SGustavo A. R. Silva 		fallthrough;
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds 	case 0x0: /* indexed/memory management */
751da177e4SLinus Torvalds 		if (bit22set(inst)) {
761da177e4SLinus Torvalds 			/*
771da177e4SLinus Torvalds 			 * Check for the 'Graphics Flush Read' instruction.
781da177e4SLinus Torvalds 			 * It resembles an FDC instruction, except for bits
791da177e4SLinus Torvalds 			 * 20 and 21. Any combination other than zero will
801da177e4SLinus Torvalds 			 * utilize the block mover functionality on some
811da177e4SLinus Torvalds 			 * older PA-RISC platforms.  The case where a block
821da177e4SLinus Torvalds 			 * move is performed from VM to graphics IO space
831da177e4SLinus Torvalds 			 * should be treated as a READ.
841da177e4SLinus Torvalds 			 *
851da177e4SLinus Torvalds 			 * The significance of bits 20,21 in the FDC
861da177e4SLinus Torvalds 			 * instruction is:
871da177e4SLinus Torvalds 			 *
881da177e4SLinus Torvalds 			 *   00  Flush data cache (normal instruction behavior)
891da177e4SLinus Torvalds 			 *   01  Graphics flush write  (IO space -> VM)
901da177e4SLinus Torvalds 			 *   10  Graphics flush read   (VM -> IO space)
911da177e4SLinus Torvalds 			 *   11  Graphics flush read/write (VM <-> IO space)
921da177e4SLinus Torvalds 			 */
931da177e4SLinus Torvalds 			if (isGraphicsFlushRead(inst))
941da177e4SLinus Torvalds 				return VM_READ;
951da177e4SLinus Torvalds 			return VM_WRITE;
961da177e4SLinus Torvalds 		} else {
971da177e4SLinus Torvalds 			/*
981da177e4SLinus Torvalds 			 * Check for LDCWX and LDCWS (semaphore instructions).
991da177e4SLinus Torvalds 			 * If bits 23 through 25 are all 1's it is one of
1001da177e4SLinus Torvalds 			 * the above two instructions and is a write.
1011da177e4SLinus Torvalds 			 *
1021da177e4SLinus Torvalds 			 * Note: With the limited bits we are looking at,
1031da177e4SLinus Torvalds 			 * this will also catch PROBEW and PROBEWI. However,
1041da177e4SLinus Torvalds 			 * these should never get in here because they don't
1051da177e4SLinus Torvalds 			 * generate exceptions of the type:
1061da177e4SLinus Torvalds 			 *   Data TLB miss fault/data page fault
1071da177e4SLinus Torvalds 			 *   Data memory protection trap
1081da177e4SLinus Torvalds 			 */
1091da177e4SLinus Torvalds 			if (bits23_25set(inst) == BITSSET)
1101da177e4SLinus Torvalds 				return VM_WRITE;
1111da177e4SLinus Torvalds 		}
1121da177e4SLinus Torvalds 		return VM_READ; /* Default */
1131da177e4SLinus Torvalds 	}
1141da177e4SLinus Torvalds 	return VM_READ; /* Default */
1151da177e4SLinus Torvalds }
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds #undef bit22set
1181da177e4SLinus Torvalds #undef bits23_25set
1191da177e4SLinus Torvalds #undef isGraphicsFlushRead
1201da177e4SLinus Torvalds #undef BITSSET
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds #if 0
1241da177e4SLinus Torvalds /* This is the treewalk to find a vma which is the highest that has
1251da177e4SLinus Torvalds  * a start < addr.  We're using find_vma_prev instead right now, but
1261da177e4SLinus Torvalds  * we might want to use this at some point in the future.  Probably
1271da177e4SLinus Torvalds  * not, but I want it committed to CVS so I don't lose it :-)
1281da177e4SLinus Torvalds  */
1291da177e4SLinus Torvalds 			while (tree != vm_avl_empty) {
1301da177e4SLinus Torvalds 				if (tree->vm_start > addr) {
1311da177e4SLinus Torvalds 					tree = tree->vm_avl_left;
1321da177e4SLinus Torvalds 				} else {
1331da177e4SLinus Torvalds 					prev = tree;
1341da177e4SLinus Torvalds 					if (prev->vm_next == NULL)
1351da177e4SLinus Torvalds 						break;
1361da177e4SLinus Torvalds 					if (prev->vm_next->vm_start > addr)
1371da177e4SLinus Torvalds 						break;
1381da177e4SLinus Torvalds 					tree = tree->vm_avl_right;
1391da177e4SLinus Torvalds 				}
1401da177e4SLinus Torvalds 			}
1411da177e4SLinus Torvalds #endif
1421da177e4SLinus Torvalds 
fixup_exception(struct pt_regs * regs)143c61c25ebSKyle McMartin int fixup_exception(struct pt_regs *regs)
144c61c25ebSKyle McMartin {
145c61c25ebSKyle McMartin 	const struct exception_table_entry *fix;
146c61c25ebSKyle McMartin 
147c61c25ebSKyle McMartin 	fix = search_exception_tables(regs->iaoq[0]);
148c61c25ebSKyle McMartin 	if (fix) {
149d19f5e41SHelge Deller 		/*
150d19f5e41SHelge Deller 		 * Fix up get_user() and put_user().
151d19f5e41SHelge Deller 		 * ASM_EXCEPTIONTABLE_ENTRY_EFAULT() sets the least-significant
152d19f5e41SHelge Deller 		 * bit in the relative address of the fixup routine to indicate
153*fa69a806SHelge Deller 		 * that the register encoded in the "or %r0,%r0,register"
154*fa69a806SHelge Deller 		 * opcode should be loaded with -EFAULT to report a userspace
155*fa69a806SHelge Deller 		 * access error.
156d19f5e41SHelge Deller 		 */
157d19f5e41SHelge Deller 		if (fix->fixup & 1) {
158*fa69a806SHelge Deller 			int fault_error_reg = fix->err_opcode & 0x1f;
159*fa69a806SHelge Deller 			if (!WARN_ON(!fault_error_reg))
160*fa69a806SHelge Deller 				regs->gr[fault_error_reg] = -EFAULT;
161*fa69a806SHelge Deller 			pr_debug("Unalignment fixup of register %d at %pS\n",
162*fa69a806SHelge Deller 				fault_error_reg, (void*)regs->iaoq[0]);
163d19f5e41SHelge Deller 
164d19f5e41SHelge Deller 			/* zero target register for get_user() */
165d19f5e41SHelge Deller 			if (parisc_acctyp(0, regs->iir) == VM_READ) {
166d19f5e41SHelge Deller 				int treg = regs->iir & 0x1f;
167b752c7b2SHelge Deller 				BUG_ON(treg == 0);
168d19f5e41SHelge Deller 				regs->gr[treg] = 0;
169d19f5e41SHelge Deller 			}
170d19f5e41SHelge Deller 		}
171d19f5e41SHelge Deller 
1720de79858SHelge Deller 		regs->iaoq[0] = (unsigned long)&fix->fixup + fix->fixup;
1730de79858SHelge Deller 		regs->iaoq[0] &= ~3;
174c61c25ebSKyle McMartin 		/*
175c61c25ebSKyle McMartin 		 * NOTE: In some cases the faulting instruction
176c61c25ebSKyle McMartin 		 * may be in the delay slot of a branch. We
177c61c25ebSKyle McMartin 		 * don't want to take the branch, so we don't
178c61c25ebSKyle McMartin 		 * increment iaoq[1], instead we set it to be
179c61c25ebSKyle McMartin 		 * iaoq[0]+4, and clear the B bit in the PSW
180c61c25ebSKyle McMartin 		 */
181c61c25ebSKyle McMartin 		regs->iaoq[1] = regs->iaoq[0] + 4;
182c61c25ebSKyle McMartin 		regs->gr[0] &= ~PSW_B; /* IPSW in gr[0] */
183c61c25ebSKyle McMartin 
184c61c25ebSKyle McMartin 		return 1;
185c61c25ebSKyle McMartin 	}
186c61c25ebSKyle McMartin 
187c61c25ebSKyle McMartin 	return 0;
188c61c25ebSKyle McMartin }
189c61c25ebSKyle McMartin 
190fef47e2aSHelge Deller /*
191b391667eSHelge Deller  * parisc hardware trap list
192b391667eSHelge Deller  *
193b391667eSHelge Deller  * Documented in section 3 "Addressing and Access Control" of the
194b391667eSHelge Deller  * "PA-RISC 1.1 Architecture and Instruction Set Reference Manual"
195b391667eSHelge Deller  * https://parisc.wiki.kernel.org/index.php/File:Pa11_acd.pdf
196b391667eSHelge Deller  *
197b391667eSHelge Deller  * For implementation see handle_interruption() in traps.c
198b391667eSHelge Deller  */
199b391667eSHelge Deller static const char * const trap_description[] = {
200aa1bb8b6SHelge Deller 	[1] =	"High-priority machine check (HPMC)",
201aa1bb8b6SHelge Deller 	[2] =	"Power failure interrupt",
202aa1bb8b6SHelge Deller 	[3] =	"Recovery counter trap",
203aa1bb8b6SHelge Deller 	[5] =	"Low-priority machine check",
204aa1bb8b6SHelge Deller 	[6] =	"Instruction TLB miss fault",
205aa1bb8b6SHelge Deller 	[7] =	"Instruction access rights / protection trap",
206aa1bb8b6SHelge Deller 	[8] =	"Illegal instruction trap",
207aa1bb8b6SHelge Deller 	[9] =	"Break instruction trap",
208aa1bb8b6SHelge Deller 	[10] =	"Privileged operation trap",
209aa1bb8b6SHelge Deller 	[11] =	"Privileged register trap",
210aa1bb8b6SHelge Deller 	[12] =	"Overflow trap",
211aa1bb8b6SHelge Deller 	[13] =	"Conditional trap",
212aa1bb8b6SHelge Deller 	[14] =	"FP Assist Exception trap",
213aa1bb8b6SHelge Deller 	[15] =	"Data TLB miss fault",
214aa1bb8b6SHelge Deller 	[16] =	"Non-access ITLB miss fault",
215aa1bb8b6SHelge Deller 	[17] =	"Non-access DTLB miss fault",
216aa1bb8b6SHelge Deller 	[18] =	"Data memory protection/unaligned access trap",
217aa1bb8b6SHelge Deller 	[19] =	"Data memory break trap",
218aa1bb8b6SHelge Deller 	[20] =	"TLB dirty bit trap",
219aa1bb8b6SHelge Deller 	[21] =	"Page reference trap",
220aa1bb8b6SHelge Deller 	[22] =	"Assist emulation trap",
221aa1bb8b6SHelge Deller 	[25] =	"Taken branch trap",
222aa1bb8b6SHelge Deller 	[26] =	"Data memory access rights trap",
223aa1bb8b6SHelge Deller 	[27] =	"Data memory protection ID trap",
224aa1bb8b6SHelge Deller 	[28] =	"Unaligned data reference trap",
225b391667eSHelge Deller };
226b391667eSHelge Deller 
trap_name(unsigned long code)2270a862485SHelge Deller const char *trap_name(unsigned long code)
2280a862485SHelge Deller {
2290a862485SHelge Deller 	const char *t = NULL;
2300a862485SHelge Deller 
2310a862485SHelge Deller 	if (code < ARRAY_SIZE(trap_description))
2320a862485SHelge Deller 		t = trap_description[code];
2330a862485SHelge Deller 
2340a862485SHelge Deller 	return t ? t : "Unknown trap";
2350a862485SHelge Deller }
2360a862485SHelge Deller 
237b391667eSHelge Deller /*
238fef47e2aSHelge Deller  * Print out info about fatal segfaults, if the show_unhandled_signals
239fef47e2aSHelge Deller  * sysctl is set:
240fef47e2aSHelge Deller  */
241fef47e2aSHelge Deller static inline void
show_signal_msg(struct pt_regs * regs,unsigned long code,unsigned long address,struct task_struct * tsk,struct vm_area_struct * vma)242fef47e2aSHelge Deller show_signal_msg(struct pt_regs *regs, unsigned long code,
243fef47e2aSHelge Deller 		unsigned long address, struct task_struct *tsk,
244fef47e2aSHelge Deller 		struct vm_area_struct *vma)
245fef47e2aSHelge Deller {
246fef47e2aSHelge Deller 	if (!unhandled_signal(tsk, SIGSEGV))
247fef47e2aSHelge Deller 		return;
248fef47e2aSHelge Deller 
249fef47e2aSHelge Deller 	if (!printk_ratelimit())
250fef47e2aSHelge Deller 		return;
251fef47e2aSHelge Deller 
252fef47e2aSHelge Deller 	pr_warn("\n");
253fef47e2aSHelge Deller 	pr_warn("do_page_fault() command='%s' type=%lu address=0x%08lx",
254fef47e2aSHelge Deller 	    tsk->comm, code, address);
255fef47e2aSHelge Deller 	print_vma_addr(KERN_CONT " in ", regs->iaoq[0]);
256b391667eSHelge Deller 
257b4a9eb4cSHelge Deller 	pr_cont("\ntrap #%lu: %s%c", code, trap_name(code),
258b391667eSHelge Deller 		vma ? ',':'\n');
259b391667eSHelge Deller 
260fef47e2aSHelge Deller 	if (vma)
2618351badfSDan Carpenter 		pr_cont(" vm_start = 0x%08lx, vm_end = 0x%08lx\n",
262fef47e2aSHelge Deller 			vma->vm_start, vma->vm_end);
263fef47e2aSHelge Deller 
264fef47e2aSHelge Deller 	show_regs(regs);
265fef47e2aSHelge Deller }
266fef47e2aSHelge Deller 
do_page_fault(struct pt_regs * regs,unsigned long code,unsigned long address)2671da177e4SLinus Torvalds void do_page_fault(struct pt_regs *regs, unsigned long code,
2681da177e4SLinus Torvalds 			      unsigned long address)
2691da177e4SLinus Torvalds {
2701da177e4SLinus Torvalds 	struct vm_area_struct *vma, *prev_vma;
2712d8b22deSJohn David Anglin 	struct task_struct *tsk;
2722d8b22deSJohn David Anglin 	struct mm_struct *mm;
2731da177e4SLinus Torvalds 	unsigned long acc_type;
27450a7ca3cSSouptick Joarder 	vm_fault_t fault = 0;
2752d8b22deSJohn David Anglin 	unsigned int flags;
27620dda87bSJohn David Anglin 	char *msg;
2771da177e4SLinus Torvalds 
2782d8b22deSJohn David Anglin 	tsk = current;
2792d8b22deSJohn David Anglin 	mm = tsk->mm;
28020dda87bSJohn David Anglin 	if (!mm) {
28120dda87bSJohn David Anglin 		msg = "Page fault: no context";
2822d8b22deSJohn David Anglin 		goto no_context;
28320dda87bSJohn David Anglin 	}
2842d8b22deSJohn David Anglin 
285dde16072SPeter Xu 	flags = FAULT_FLAG_DEFAULT;
286759496baSJohannes Weiner 	if (user_mode(regs))
287759496baSJohannes Weiner 		flags |= FAULT_FLAG_USER;
2880772dac1SFelipe Pena 
2890772dac1SFelipe Pena 	acc_type = parisc_acctyp(code, regs->iir);
290759496baSJohannes Weiner 	if (acc_type & VM_WRITE)
291759496baSJohannes Weiner 		flags |= FAULT_FLAG_WRITE;
292af8a7926SPeter Xu 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
29338057477SKautuk Consul retry:
294d8ed45c5SMichel Lespinasse 	mmap_read_lock(mm);
2951da177e4SLinus Torvalds 	vma = find_vma_prev(mm, address, &prev_vma);
2968d7071afSLinus Torvalds 	if (!vma || address < vma->vm_start) {
297ea3f8272SLinus Torvalds 		if (!prev_vma || !(prev_vma->vm_flags & VM_GROWSUP))
2988d7071afSLinus Torvalds 			goto bad_area;
2998d7071afSLinus Torvalds 		vma = expand_stack(mm, address);
3008d7071afSLinus Torvalds 		if (!vma)
3018d7071afSLinus Torvalds 			goto bad_area_nosemaphore;
3028d7071afSLinus Torvalds 	}
3038d7071afSLinus Torvalds 
3041da177e4SLinus Torvalds /*
3051da177e4SLinus Torvalds  * Ok, we have a good vm_area for this memory access. We still need to
3061da177e4SLinus Torvalds  * check the access permissions.
3071da177e4SLinus Torvalds  */
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	if ((vma->vm_flags & acc_type) != acc_type)
3101da177e4SLinus Torvalds 		goto bad_area;
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	/*
3131da177e4SLinus Torvalds 	 * If for any reason at all we couldn't handle the fault, make
3141da177e4SLinus Torvalds 	 * sure we exit gracefully rather than endlessly redo the
3151da177e4SLinus Torvalds 	 * fault.
3161da177e4SLinus Torvalds 	 */
3171da177e4SLinus Torvalds 
318af8a7926SPeter Xu 	fault = handle_mm_fault(vma, address, flags, regs);
31938057477SKautuk Consul 
32015261678SAl Viro 	if (fault_signal_pending(fault, regs)) {
32115261678SAl Viro 		if (!user_mode(regs)) {
32215261678SAl Viro 			msg = "Page fault: fault signal on kernel memory";
32315261678SAl Viro 			goto no_context;
32415261678SAl Viro 		}
32538057477SKautuk Consul 		return;
32615261678SAl Viro 	}
32738057477SKautuk Consul 
328d9272525SPeter Xu 	/* The fault is fully completed (including releasing mmap lock) */
329d9272525SPeter Xu 	if (fault & VM_FAULT_COMPLETED)
330d9272525SPeter Xu 		return;
331d9272525SPeter Xu 
33283c54070SNick Piggin 	if (unlikely(fault & VM_FAULT_ERROR)) {
3331da177e4SLinus Torvalds 		/*
33467a5a59dSHelge Deller 		 * We hit a shared mapping outside of the file, or some
3356e346228SLinus Torvalds 		 * other thing happened to us that made us unable to
3366e346228SLinus Torvalds 		 * handle the page fault gracefully.
3371da177e4SLinus Torvalds 		 */
33883c54070SNick Piggin 		if (fault & VM_FAULT_OOM)
3391da177e4SLinus Torvalds 			goto out_of_memory;
34033692f27SLinus Torvalds 		else if (fault & VM_FAULT_SIGSEGV)
34133692f27SLinus Torvalds 			goto bad_area;
342606f95e4SHelge Deller 		else if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
343606f95e4SHelge Deller 				  VM_FAULT_HWPOISON_LARGE))
34483c54070SNick Piggin 			goto bad_area;
34583c54070SNick Piggin 		BUG();
3461da177e4SLinus Torvalds 	}
34738057477SKautuk Consul 	if (fault & VM_FAULT_RETRY) {
34838057477SKautuk Consul 		/*
3493e4e28c5SMichel Lespinasse 		 * No need to mmap_read_unlock(mm) as we would
35038057477SKautuk Consul 		 * have already released it in __lock_page_or_retry
35138057477SKautuk Consul 		 * in mm/filemap.c.
35238057477SKautuk Consul 		 */
3534064b982SPeter Xu 		flags |= FAULT_FLAG_TRIED;
35438057477SKautuk Consul 		goto retry;
35538057477SKautuk Consul 	}
356d8ed45c5SMichel Lespinasse 	mmap_read_unlock(mm);
3571da177e4SLinus Torvalds 	return;
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds /*
3601da177e4SLinus Torvalds  * Something tried to access memory that isn't in our memory map..
3611da177e4SLinus Torvalds  */
3621da177e4SLinus Torvalds bad_area:
363d8ed45c5SMichel Lespinasse 	mmap_read_unlock(mm);
3641da177e4SLinus Torvalds 
3658d7071afSLinus Torvalds bad_area_nosemaphore:
3661da177e4SLinus Torvalds 	if (user_mode(regs)) {
367ccf75290SEric W. Biederman 		int signo, si_code;
368fef47e2aSHelge Deller 
3691f2048fdSHelge Deller 		switch (code) {
3701f2048fdSHelge Deller 		case 15:	/* Data TLB miss fault/Data page fault */
37149d1cb2bSHelge Deller 			/* send SIGSEGV when outside of vma */
37249d1cb2bSHelge Deller 			if (!vma ||
37324746231SHelge Deller 			    address < vma->vm_start || address >= vma->vm_end) {
374ccf75290SEric W. Biederman 				signo = SIGSEGV;
375ccf75290SEric W. Biederman 				si_code = SEGV_MAPERR;
37649d1cb2bSHelge Deller 				break;
37749d1cb2bSHelge Deller 			}
37849d1cb2bSHelge Deller 
37949d1cb2bSHelge Deller 			/* send SIGSEGV for wrong permissions */
38049d1cb2bSHelge Deller 			if ((vma->vm_flags & acc_type) != acc_type) {
381ccf75290SEric W. Biederman 				signo = SIGSEGV;
382ccf75290SEric W. Biederman 				si_code = SEGV_ACCERR;
38349d1cb2bSHelge Deller 				break;
38449d1cb2bSHelge Deller 			}
38549d1cb2bSHelge Deller 
38649d1cb2bSHelge Deller 			/* probably address is outside of mapped file */
387df561f66SGustavo A. R. Silva 			fallthrough;
3881f2048fdSHelge Deller 		case 17:	/* NA data TLB miss / page fault */
3891f2048fdSHelge Deller 		case 18:	/* Unaligned access - PCXS only */
390ccf75290SEric W. Biederman 			signo = SIGBUS;
391ccf75290SEric W. Biederman 			si_code = (code == 18) ? BUS_ADRALN : BUS_ADRERR;
3921f2048fdSHelge Deller 			break;
3931f2048fdSHelge Deller 		case 16:	/* Non-access instruction TLB miss fault */
3941f2048fdSHelge Deller 		case 26:	/* PCXL: Data memory access rights trap */
3951f2048fdSHelge Deller 		default:
396ccf75290SEric W. Biederman 			signo = SIGSEGV;
397ccf75290SEric W. Biederman 			si_code = (code == 26) ? SEGV_ACCERR : SEGV_MAPERR;
39849d1cb2bSHelge Deller 			break;
3991f2048fdSHelge Deller 		}
400606f95e4SHelge Deller #ifdef CONFIG_MEMORY_FAILURE
401606f95e4SHelge Deller 		if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
402c2b0e0d3SEric W. Biederman 			unsigned int lsb = 0;
403606f95e4SHelge Deller 			printk(KERN_ERR
404606f95e4SHelge Deller 	"MCE: Killing %s:%d due to hardware memory corruption fault at %08lx\n",
405606f95e4SHelge Deller 			tsk->comm, tsk->pid, address);
406606f95e4SHelge Deller 			/*
407606f95e4SHelge Deller 			 * Either small page or large page may be poisoned.
408606f95e4SHelge Deller 			 * In other words, VM_FAULT_HWPOISON_LARGE and
409606f95e4SHelge Deller 			 * VM_FAULT_HWPOISON are mutually exclusive.
410606f95e4SHelge Deller 			 */
411606f95e4SHelge Deller 			if (fault & VM_FAULT_HWPOISON_LARGE)
412606f95e4SHelge Deller 				lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
413606f95e4SHelge Deller 			else if (fault & VM_FAULT_HWPOISON)
414606f95e4SHelge Deller 				lsb = PAGE_SHIFT;
415c2b0e0d3SEric W. Biederman 
416c2b0e0d3SEric W. Biederman 			force_sig_mceerr(BUS_MCEERR_AR, (void __user *) address,
417f8eac901SEric W. Biederman 					 lsb);
418c2b0e0d3SEric W. Biederman 			return;
419c2b0e0d3SEric W. Biederman 		}
420c2b0e0d3SEric W. Biederman #endif
421606f95e4SHelge Deller 		show_signal_msg(regs, code, address, tsk, vma);
422606f95e4SHelge Deller 
4232e1661d2SEric W. Biederman 		force_sig_fault(signo, si_code, (void __user *) address);
4241da177e4SLinus Torvalds 		return;
4251da177e4SLinus Torvalds 	}
42620dda87bSJohn David Anglin 	msg = "Page fault: bad address";
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds no_context:
4291da177e4SLinus Torvalds 
430c61c25ebSKyle McMartin 	if (!user_mode(regs) && fixup_exception(regs)) {
4311da177e4SLinus Torvalds 		return;
4321da177e4SLinus Torvalds 	}
4331da177e4SLinus Torvalds 
43420dda87bSJohn David Anglin 	parisc_terminate(msg, regs, code, address);
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds out_of_memory:
437d8ed45c5SMichel Lespinasse 	mmap_read_unlock(mm);
43820dda87bSJohn David Anglin 	if (!user_mode(regs)) {
43920dda87bSJohn David Anglin 		msg = "Page fault: out of memory";
4401da177e4SLinus Torvalds 		goto no_context;
44120dda87bSJohn David Anglin 	}
44253e30d02SNick Piggin 	pagefault_out_of_memory();
4431da177e4SLinus Torvalds }
444e00b0a2aSJohn David Anglin 
445e00b0a2aSJohn David Anglin /* Handle non-access data TLB miss faults.
446e00b0a2aSJohn David Anglin  *
447e00b0a2aSJohn David Anglin  * For probe instructions, accesses to userspace are considered allowed
448e00b0a2aSJohn David Anglin  * if they lie in a valid VMA and the access type matches. We are not
449e00b0a2aSJohn David Anglin  * allowed to handle MM faults here so there may be situations where an
450e00b0a2aSJohn David Anglin  * actual access would fail even though a probe was successful.
451e00b0a2aSJohn David Anglin  */
452e00b0a2aSJohn David Anglin int
handle_nadtlb_fault(struct pt_regs * regs)453e00b0a2aSJohn David Anglin handle_nadtlb_fault(struct pt_regs *regs)
454e00b0a2aSJohn David Anglin {
455e00b0a2aSJohn David Anglin 	unsigned long insn = regs->iir;
456e00b0a2aSJohn David Anglin 	int breg, treg, xreg, val = 0;
4578d7071afSLinus Torvalds 	struct vm_area_struct *vma;
458e00b0a2aSJohn David Anglin 	struct task_struct *tsk;
459e00b0a2aSJohn David Anglin 	struct mm_struct *mm;
460e00b0a2aSJohn David Anglin 	unsigned long address;
461e00b0a2aSJohn David Anglin 	unsigned long acc_type;
462e00b0a2aSJohn David Anglin 
463e00b0a2aSJohn David Anglin 	switch (insn & 0x380) {
464e00b0a2aSJohn David Anglin 	case 0x280:
465e00b0a2aSJohn David Anglin 		/* FDC instruction */
466e00b0a2aSJohn David Anglin 		fallthrough;
467e00b0a2aSJohn David Anglin 	case 0x380:
468e00b0a2aSJohn David Anglin 		/* PDC and FIC instructions */
46967c35a3bSJohn David Anglin 		if (DEBUG_NATLB && printk_ratelimit()) {
47067c35a3bSJohn David Anglin 			pr_warn("WARNING: nullifying cache flush/purge instruction\n");
471e00b0a2aSJohn David Anglin 			show_regs(regs);
472e00b0a2aSJohn David Anglin 		}
473e00b0a2aSJohn David Anglin 		if (insn & 0x20) {
474e00b0a2aSJohn David Anglin 			/* Base modification */
475e00b0a2aSJohn David Anglin 			breg = (insn >> 21) & 0x1f;
476e00b0a2aSJohn David Anglin 			xreg = (insn >> 16) & 0x1f;
477e00b0a2aSJohn David Anglin 			if (breg && xreg)
478e00b0a2aSJohn David Anglin 				regs->gr[breg] += regs->gr[xreg];
479e00b0a2aSJohn David Anglin 		}
480e00b0a2aSJohn David Anglin 		regs->gr[0] |= PSW_N;
481e00b0a2aSJohn David Anglin 		return 1;
482e00b0a2aSJohn David Anglin 
483e00b0a2aSJohn David Anglin 	case 0x180:
484e00b0a2aSJohn David Anglin 		/* PROBE instruction */
485e00b0a2aSJohn David Anglin 		treg = insn & 0x1f;
486e00b0a2aSJohn David Anglin 		if (regs->isr) {
487e00b0a2aSJohn David Anglin 			tsk = current;
488e00b0a2aSJohn David Anglin 			mm = tsk->mm;
489e00b0a2aSJohn David Anglin 			if (mm) {
490e00b0a2aSJohn David Anglin 				/* Search for VMA */
491e00b0a2aSJohn David Anglin 				address = regs->ior;
492e00b0a2aSJohn David Anglin 				mmap_read_lock(mm);
4938d7071afSLinus Torvalds 				vma = vma_lookup(mm, address);
494e00b0a2aSJohn David Anglin 				mmap_read_unlock(mm);
495e00b0a2aSJohn David Anglin 
496e00b0a2aSJohn David Anglin 				/*
497e00b0a2aSJohn David Anglin 				 * Check if access to the VMA is okay.
498e00b0a2aSJohn David Anglin 				 * We don't allow for stack expansion.
499e00b0a2aSJohn David Anglin 				 */
500e00b0a2aSJohn David Anglin 				acc_type = (insn & 0x40) ? VM_WRITE : VM_READ;
501e00b0a2aSJohn David Anglin 				if (vma
502e00b0a2aSJohn David Anglin 				    && (vma->vm_flags & acc_type) == acc_type)
503e00b0a2aSJohn David Anglin 					val = 1;
504e00b0a2aSJohn David Anglin 			}
505e00b0a2aSJohn David Anglin 		}
506e00b0a2aSJohn David Anglin 		if (treg)
507e00b0a2aSJohn David Anglin 			regs->gr[treg] = val;
508e00b0a2aSJohn David Anglin 		regs->gr[0] |= PSW_N;
509e00b0a2aSJohn David Anglin 		return 1;
510e00b0a2aSJohn David Anglin 
511e00b0a2aSJohn David Anglin 	case 0x300:
512e00b0a2aSJohn David Anglin 		/* LPA instruction */
513e00b0a2aSJohn David Anglin 		if (insn & 0x20) {
514e00b0a2aSJohn David Anglin 			/* Base modification */
515e00b0a2aSJohn David Anglin 			breg = (insn >> 21) & 0x1f;
516e00b0a2aSJohn David Anglin 			xreg = (insn >> 16) & 0x1f;
517e00b0a2aSJohn David Anglin 			if (breg && xreg)
518e00b0a2aSJohn David Anglin 				regs->gr[breg] += regs->gr[xreg];
519e00b0a2aSJohn David Anglin 		}
520e00b0a2aSJohn David Anglin 		treg = insn & 0x1f;
521e00b0a2aSJohn David Anglin 		if (treg)
522e00b0a2aSJohn David Anglin 			regs->gr[treg] = 0;
523e00b0a2aSJohn David Anglin 		regs->gr[0] |= PSW_N;
524e00b0a2aSJohn David Anglin 		return 1;
525e00b0a2aSJohn David Anglin 
526e00b0a2aSJohn David Anglin 	default:
527e00b0a2aSJohn David Anglin 		break;
528e00b0a2aSJohn David Anglin 	}
529e00b0a2aSJohn David Anglin 
530e00b0a2aSJohn David Anglin 	return 0;
531e00b0a2aSJohn David Anglin }
532