xref: /openbmc/linux/arch/arc/mm/fault.c (revision 85c5e337)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Page Fault Handling for ARC (TLB Miss / ProtV)
3  *
4  * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
5  */
6 
7 #include <linux/signal.h>
8 #include <linux/interrupt.h>
9 #include <linux/sched/signal.h>
10 #include <linux/errno.h>
11 #include <linux/ptrace.h>
12 #include <linux/uaccess.h>
13 #include <linux/kdebug.h>
14 #include <linux/perf_event.h>
15 #include <linux/mm_types.h>
16 #include <asm/pgalloc.h>
17 #include <asm/mmu.h>
18 
19 /*
20  * kernel virtual address is required to implement vmalloc/pkmap/fixmap
21  * Refer to asm/processor.h for System Memory Map
22  *
23  * It simply copies the PMD entry (pointer to 2nd level page table or hugepage)
24  * from swapper pgdir to task pgdir. The 2nd level table/page is thus shared
25  */
26 noinline static int handle_kernel_vaddr_fault(unsigned long address)
27 {
28 	/*
29 	 * Synchronize this task's top level page-table
30 	 * with the 'reference' page table.
31 	 */
32 	pgd_t *pgd, *pgd_k;
33 	pud_t *pud, *pud_k;
34 	pmd_t *pmd, *pmd_k;
35 
36 	pgd = pgd_offset_fast(current->active_mm, address);
37 	pgd_k = pgd_offset_k(address);
38 
39 	if (!pgd_present(*pgd_k))
40 		goto bad_area;
41 
42 	pud = pud_offset(pgd, address);
43 	pud_k = pud_offset(pgd_k, address);
44 	if (!pud_present(*pud_k))
45 		goto bad_area;
46 
47 	pmd = pmd_offset(pud, address);
48 	pmd_k = pmd_offset(pud_k, address);
49 	if (!pmd_present(*pmd_k))
50 		goto bad_area;
51 
52 	set_pmd(pmd, *pmd_k);
53 
54 	/* XXX: create the TLB entry here */
55 	return 0;
56 
57 bad_area:
58 	return 1;
59 }
60 
61 void do_page_fault(unsigned long address, struct pt_regs *regs)
62 {
63 	struct vm_area_struct *vma = NULL;
64 	struct task_struct *tsk = current;
65 	struct mm_struct *mm = tsk->mm;
66 	int si_code = SEGV_MAPERR;
67 	unsigned int write = 0, exec = 0, mask;
68 	vm_fault_t fault;
69 	unsigned int flags;
70 
71 	/*
72 	 * NOTE! We MUST NOT take any locks for this case. We may
73 	 * be in an interrupt or a critical region, and should
74 	 * only copy the information from the master page table,
75 	 * nothing more.
76 	 */
77 	if (address >= VMALLOC_START && !user_mode(regs)) {
78 		if (unlikely(handle_kernel_vaddr_fault(address)))
79 			goto no_context;
80 		else
81 			return;
82 	}
83 
84 	/*
85 	 * If we're in an interrupt or have no user
86 	 * context, we must not take the fault..
87 	 */
88 	if (faulthandler_disabled() || !mm)
89 		goto no_context;
90 
91 	if (regs->ecr_cause & ECR_C_PROTV_STORE)	/* ST/EX */
92 		write = 1;
93 	else if ((regs->ecr_vec == ECR_V_PROTV) &&
94 	         (regs->ecr_cause == ECR_C_PROTV_INST_FETCH))
95 		exec = 1;
96 
97 	flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
98 	if (user_mode(regs))
99 		flags |= FAULT_FLAG_USER;
100 	if (write)
101 		flags |= FAULT_FLAG_WRITE;
102 
103 retry:
104 	down_read(&mm->mmap_sem);
105 
106 	vma = find_vma(mm, address);
107 	if (!vma)
108 		goto bad_area;
109 	if (unlikely(address < vma->vm_start)) {
110 		if (!(vma->vm_flags & VM_GROWSDOWN) || expand_stack(vma, address))
111 			goto bad_area;
112 	}
113 
114 	/*
115 	 * vm_area is good, now check permissions for this memory access
116 	 */
117 	mask = VM_READ;
118 	if (write)
119 		mask = VM_WRITE;
120 	if (exec)
121 		mask = VM_EXEC;
122 
123 	if (!(vma->vm_flags & mask)) {
124 		si_code = SEGV_ACCERR;
125 		goto bad_area;
126 	}
127 
128 	/*
129 	 * If for any reason at all we couldn't handle the fault,
130 	 * make sure we exit gracefully rather than endlessly redo
131 	 * the fault.
132 	 */
133 	fault = handle_mm_fault(vma, address, flags);
134 
135 	if (fatal_signal_pending(current)) {
136 
137 		/*
138 		 * if fault retry, mmap_sem already relinquished by core mm
139 		 * so OK to return to user mode (with signal handled first)
140 		 */
141 		if (fault & VM_FAULT_RETRY) {
142 			if (!user_mode(regs))
143 				goto no_context;
144 			return;
145 		}
146 	}
147 
148 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
149 
150 	if (likely(!(fault & VM_FAULT_ERROR))) {
151 		if (flags & FAULT_FLAG_ALLOW_RETRY) {
152 			/* To avoid updating stats twice for retry case */
153 			if (fault & VM_FAULT_MAJOR) {
154 				tsk->maj_flt++;
155 				perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
156 					      regs, address);
157 			} else {
158 				tsk->min_flt++;
159 				perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
160 					      regs, address);
161 			}
162 
163 			if (fault & VM_FAULT_RETRY) {
164 				flags &= ~FAULT_FLAG_ALLOW_RETRY;
165 				flags |= FAULT_FLAG_TRIED;
166 				goto retry;
167 			}
168 		}
169 
170 		/* Fault Handled Gracefully */
171 		up_read(&mm->mmap_sem);
172 		return;
173 	}
174 
175 	if (fault & VM_FAULT_OOM)
176 		goto out_of_memory;
177 	else if (fault & VM_FAULT_SIGSEGV)
178 		goto bad_area;
179 	else if (fault & VM_FAULT_SIGBUS)
180 		goto do_sigbus;
181 
182 	/* no man's land */
183 	BUG();
184 
185 	/*
186 	 * Something tried to access memory that isn't in our memory map..
187 	 * Fix it, but check if it's kernel or user first..
188 	 */
189 bad_area:
190 	up_read(&mm->mmap_sem);
191 
192 	/* User mode accesses just cause a SIGSEGV */
193 	if (user_mode(regs)) {
194 		tsk->thread.fault_address = address;
195 		force_sig_fault(SIGSEGV, si_code, (void __user *)address, tsk);
196 		return;
197 	}
198 
199 no_context:
200 	/* Are we prepared to handle this kernel fault?
201 	 *
202 	 * (The kernel has valid exception-points in the source
203 	 *  when it accesses user-memory. When it fails in one
204 	 *  of those points, we find it in a table and do a jump
205 	 *  to some fixup code that loads an appropriate error
206 	 *  code)
207 	 */
208 	if (fixup_exception(regs))
209 		return;
210 
211 	die("Oops", regs, address);
212 
213 out_of_memory:
214 	up_read(&mm->mmap_sem);
215 
216 	if (user_mode(regs)) {
217 		pagefault_out_of_memory();
218 		return;
219 	}
220 
221 	goto no_context;
222 
223 do_sigbus:
224 	up_read(&mm->mmap_sem);
225 
226 	if (!user_mode(regs))
227 		goto no_context;
228 
229 	tsk->thread.fault_address = address;
230 	force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address, tsk);
231 }
232