1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Memory fault handling for Hexagon
4  *
5  * Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.
6  */
7 
8 /*
9  * Page fault handling for the Hexagon Virtual Machine.
10  * Can also be called by a native port emulating the HVM
11  * execptions.
12  */
13 
14 #include <asm/traps.h>
15 #include <linux/uaccess.h>
16 #include <linux/mm.h>
17 #include <linux/sched/signal.h>
18 #include <linux/signal.h>
19 #include <linux/extable.h>
20 #include <linux/hardirq.h>
21 #include <linux/perf_event.h>
22 
23 /*
24  * Decode of hardware exception sends us to one of several
25  * entry points.  At each, we generate canonical arguments
26  * for handling by the abstract memory management code.
27  */
28 #define FLT_IFETCH     -1
29 #define FLT_LOAD        0
30 #define FLT_STORE       1
31 
32 
33 /*
34  * Canonical page fault handler
35  */
do_page_fault(unsigned long address,long cause,struct pt_regs * regs)36 void do_page_fault(unsigned long address, long cause, struct pt_regs *regs)
37 {
38 	struct vm_area_struct *vma;
39 	struct mm_struct *mm = current->mm;
40 	int si_signo;
41 	int si_code = SEGV_MAPERR;
42 	vm_fault_t fault;
43 	const struct exception_table_entry *fixup;
44 	unsigned int flags = FAULT_FLAG_DEFAULT;
45 
46 	/*
47 	 * If we're in an interrupt or have no user context,
48 	 * then must not take the fault.
49 	 */
50 	if (unlikely(in_interrupt() || !mm))
51 		goto no_context;
52 
53 	local_irq_enable();
54 
55 	if (user_mode(regs))
56 		flags |= FAULT_FLAG_USER;
57 
58 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
59 retry:
60 	mmap_read_lock(mm);
61 	vma = find_vma(mm, address);
62 	if (!vma)
63 		goto bad_area;
64 
65 	if (vma->vm_start <= address)
66 		goto good_area;
67 
68 	if (!(vma->vm_flags & VM_GROWSDOWN))
69 		goto bad_area;
70 
71 	if (expand_stack(vma, address))
72 		goto bad_area;
73 
74 good_area:
75 	/* Address space is OK.  Now check access rights. */
76 	si_code = SEGV_ACCERR;
77 
78 	switch (cause) {
79 	case FLT_IFETCH:
80 		if (!(vma->vm_flags & VM_EXEC))
81 			goto bad_area;
82 		break;
83 	case FLT_LOAD:
84 		if (!(vma->vm_flags & VM_READ))
85 			goto bad_area;
86 		break;
87 	case FLT_STORE:
88 		if (!(vma->vm_flags & VM_WRITE))
89 			goto bad_area;
90 		flags |= FAULT_FLAG_WRITE;
91 		break;
92 	}
93 
94 	fault = handle_mm_fault(vma, address, flags, regs);
95 
96 	if (fault_signal_pending(fault, regs))
97 		return;
98 
99 	/* The most common case -- we are done. */
100 	if (likely(!(fault & VM_FAULT_ERROR))) {
101 		if (fault & VM_FAULT_RETRY) {
102 			flags |= FAULT_FLAG_TRIED;
103 			goto retry;
104 		}
105 
106 		mmap_read_unlock(mm);
107 		return;
108 	}
109 
110 	mmap_read_unlock(mm);
111 
112 	/* Handle copyin/out exception cases */
113 	if (!user_mode(regs))
114 		goto no_context;
115 
116 	if (fault & VM_FAULT_OOM) {
117 		pagefault_out_of_memory();
118 		return;
119 	}
120 
121 	/* User-mode address is in the memory map, but we are
122 	 * unable to fix up the page fault.
123 	 */
124 	if (fault & VM_FAULT_SIGBUS) {
125 		si_signo = SIGBUS;
126 		si_code = BUS_ADRERR;
127 	}
128 	/* Address is not in the memory map */
129 	else {
130 		si_signo = SIGSEGV;
131 		si_code  = SEGV_ACCERR;
132 	}
133 	force_sig_fault(si_signo, si_code, (void __user *)address);
134 	return;
135 
136 bad_area:
137 	mmap_read_unlock(mm);
138 
139 	if (user_mode(regs)) {
140 		force_sig_fault(SIGSEGV, si_code, (void __user *)address);
141 		return;
142 	}
143 	/* Kernel-mode fault falls through */
144 
145 no_context:
146 	fixup = search_exception_tables(pt_elr(regs));
147 	if (fixup) {
148 		pt_set_elr(regs, fixup->fixup);
149 		return;
150 	}
151 
152 	/* Things are looking very, very bad now */
153 	bust_spinlocks(1);
154 	printk(KERN_EMERG "Unable to handle kernel paging request at "
155 		"virtual address 0x%08lx, regs %p\n", address, regs);
156 	die("Bad Kernel VA", regs, SIGKILL);
157 }
158 
159 
read_protection_fault(struct pt_regs * regs)160 void read_protection_fault(struct pt_regs *regs)
161 {
162 	unsigned long badvadr = pt_badva(regs);
163 
164 	do_page_fault(badvadr, FLT_LOAD, regs);
165 }
166 
write_protection_fault(struct pt_regs * regs)167 void write_protection_fault(struct pt_regs *regs)
168 {
169 	unsigned long badvadr = pt_badva(regs);
170 
171 	do_page_fault(badvadr, FLT_STORE, regs);
172 }
173 
execute_protection_fault(struct pt_regs * regs)174 void execute_protection_fault(struct pt_regs *regs)
175 {
176 	unsigned long badvadr = pt_badva(regs);
177 
178 	do_page_fault(badvadr, FLT_IFETCH, regs);
179 }
180