Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
  4 *  Lennox Wu <lennox.wu@sunplusct.com>
  5 *  Chen Liqin <liqin.chen@sunplusct.com>
  6 * Copyright (C) 2012 Regents of the University of California
  7 */
  8
  9
 10#include <linux/mm.h>
 11#include <linux/kernel.h>
 12#include <linux/interrupt.h>
 13#include <linux/perf_event.h>
 14#include <linux/signal.h>
 15#include <linux/uaccess.h>
 
 
 
 16
 17#include <asm/ptrace.h>
 18#include <asm/tlbflush.h>
 19
 20#include "../kernel/head.h"
 21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 22/*
 23 * This routine handles page faults.  It determines the address and the
 24 * problem, and then passes it off to one of the appropriate routines.
 25 */
 26asmlinkage void do_page_fault(struct pt_regs *regs)
 27{
 28	struct task_struct *tsk;
 29	struct vm_area_struct *vma;
 30	struct mm_struct *mm;
 31	unsigned long addr, cause;
 32	unsigned int flags = FAULT_FLAG_DEFAULT;
 33	int code = SEGV_MAPERR;
 34	vm_fault_t fault;
 35
 36	cause = regs->cause;
 37	addr = regs->badaddr;
 38
 39	tsk = current;
 40	mm = tsk->mm;
 41
 
 
 
 42	/*
 43	 * Fault-in kernel-space virtual memory on-demand.
 44	 * The 'reference' page table is init_mm.pgd.
 45	 *
 46	 * NOTE! We MUST NOT take any locks for this case. We may
 47	 * be in an interrupt or a critical region, and should
 48	 * only copy the information from the master page table,
 49	 * nothing more.
 50	 */
 51	if (unlikely((addr >= VMALLOC_START) && (addr <= VMALLOC_END)))
 52		goto vmalloc_fault;
 
 
 
 53
 54	/* Enable interrupts if they were enabled in the parent context. */
 55	if (likely(regs->status & SR_PIE))
 56		local_irq_enable();
 57
 58	/*
 59	 * If we're in an interrupt, have no user context, or are running
 60	 * in an atomic region, then we must not take the fault.
 61	 */
 62	if (unlikely(faulthandler_disabled() || !mm))
 63		goto no_context;
 
 
 
 64
 65	if (user_mode(regs))
 66		flags |= FAULT_FLAG_USER;
 67
 
 
 
 
 
 
 
 68	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
 69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 70retry:
 71	mmap_read_lock(mm);
 72	vma = find_vma(mm, addr);
 73	if (unlikely(!vma))
 74		goto bad_area;
 75	if (likely(vma->vm_start <= addr))
 76		goto good_area;
 77	if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)))
 78		goto bad_area;
 79	if (unlikely(expand_stack(vma, addr)))
 80		goto bad_area;
 81
 82	/*
 83	 * Ok, we have a good vm_area for this memory access, so
 84	 * we can handle it.
 85	 */
 86good_area:
 87	code = SEGV_ACCERR;
 88
 89	switch (cause) {
 90	case EXC_INST_PAGE_FAULT:
 91		if (!(vma->vm_flags & VM_EXEC))
 92			goto bad_area;
 93		break;
 94	case EXC_LOAD_PAGE_FAULT:
 95		if (!(vma->vm_flags & VM_READ))
 96			goto bad_area;
 97		break;
 98	case EXC_STORE_PAGE_FAULT:
 99		if (!(vma->vm_flags & VM_WRITE))
100			goto bad_area;
101		flags |= FAULT_FLAG_WRITE;
102		break;
103	default:
104		panic("%s: unhandled cause %lu", __func__, cause);
105	}
106
107	/*
108	 * If for any reason at all we could not handle the fault,
109	 * make sure we exit gracefully rather than endlessly redo
110	 * the fault.
111	 */
112	fault = handle_mm_fault(vma, addr, flags, regs);
113
114	/*
115	 * If we need to retry but a fatal signal is pending, handle the
116	 * signal first. We do not need to release the mmap_lock because it
117	 * would already be released in __lock_page_or_retry in mm/filemap.c.
118	 */
119	if (fault_signal_pending(fault, regs))
 
 
120		return;
121
122	if (unlikely(fault & VM_FAULT_ERROR)) {
123		if (fault & VM_FAULT_OOM)
124			goto out_of_memory;
125		else if (fault & VM_FAULT_SIGBUS)
126			goto do_sigbus;
127		BUG();
128	}
129
130	if (flags & FAULT_FLAG_ALLOW_RETRY) {
131		if (fault & VM_FAULT_RETRY) {
132			flags |= FAULT_FLAG_TRIED;
133
134			/*
135			 * No need to mmap_read_unlock(mm) as we would
136			 * have already released it in __lock_page_or_retry
137			 * in mm/filemap.c.
138			 */
139			goto retry;
140		}
141	}
142
143	mmap_read_unlock(mm);
144	return;
145
146	/*
147	 * Something tried to access memory that isn't in our memory map.
148	 * Fix it, but check if it's kernel or user first.
149	 */
150bad_area:
151	mmap_read_unlock(mm);
152	/* User mode accesses just cause a SIGSEGV */
153	if (user_mode(regs)) {
154		do_trap(regs, SIGSEGV, code, addr);
155		return;
156	}
157
158no_context:
159	/* Are we prepared to handle this kernel fault? */
160	if (fixup_exception(regs))
161		return;
162
163	/*
164	 * Oops. The kernel tried to access some bad page. We'll have to
165	 * terminate things with extreme prejudice.
166	 */
167	bust_spinlocks(1);
168	pr_alert("Unable to handle kernel %s at virtual address " REG_FMT "\n",
169		(addr < PAGE_SIZE) ? "NULL pointer dereference" :
170		"paging request", addr);
171	die(regs, "Oops");
172	do_exit(SIGKILL);
173
174	/*
175	 * We ran out of memory, call the OOM killer, and return the userspace
176	 * (which will retry the fault, or kill us if we got oom-killed).
177	 */
178out_of_memory:
179	mmap_read_unlock(mm);
180	if (!user_mode(regs))
181		goto no_context;
182	pagefault_out_of_memory();
183	return;
184
185do_sigbus:
186	mmap_read_unlock(mm);
187	/* Kernel mode? Handle exceptions or die */
188	if (!user_mode(regs))
189		goto no_context;
190	do_trap(regs, SIGBUS, BUS_ADRERR, addr);
191	return;
192
193vmalloc_fault:
194	{
195		pgd_t *pgd, *pgd_k;
196		pud_t *pud, *pud_k;
197		p4d_t *p4d, *p4d_k;
198		pmd_t *pmd, *pmd_k;
199		pte_t *pte_k;
200		int index;
201
202		/* User mode accesses just cause a SIGSEGV */
203		if (user_mode(regs))
204			return do_trap(regs, SIGSEGV, code, addr);
205
206		/*
207		 * Synchronize this task's top level page-table
208		 * with the 'reference' page table.
209		 *
210		 * Do _not_ use "tsk->active_mm->pgd" here.
211		 * We might be inside an interrupt in the middle
212		 * of a task switch.
213		 */
214		index = pgd_index(addr);
215		pgd = (pgd_t *)pfn_to_virt(csr_read(CSR_SATP)) + index;
216		pgd_k = init_mm.pgd + index;
217
218		if (!pgd_present(*pgd_k))
219			goto no_context;
220		set_pgd(pgd, *pgd_k);
221
222		p4d = p4d_offset(pgd, addr);
223		p4d_k = p4d_offset(pgd_k, addr);
224		if (!p4d_present(*p4d_k))
225			goto no_context;
226
227		pud = pud_offset(p4d, addr);
228		pud_k = pud_offset(p4d_k, addr);
229		if (!pud_present(*pud_k))
230			goto no_context;
231
232		/*
233		 * Since the vmalloc area is global, it is unnecessary
234		 * to copy individual PTEs
235		 */
236		pmd = pmd_offset(pud, addr);
237		pmd_k = pmd_offset(pud_k, addr);
238		if (!pmd_present(*pmd_k))
239			goto no_context;
240		set_pmd(pmd, *pmd_k);
241
242		/*
243		 * Make sure the actual PTE exists as well to
244		 * catch kernel vmalloc-area accesses to non-mapped
245		 * addresses. If we don't do this, this will just
246		 * silently loop forever.
247		 */
248		pte_k = pte_offset_kernel(pmd_k, addr);
249		if (!pte_present(*pte_k))
250			goto no_context;
251
252		/*
253		 * The kernel assumes that TLBs don't cache invalid
254		 * entries, but in RISC-V, SFENCE.VMA specifies an
255		 * ordering constraint, not a cache flush; it is
256		 * necessary even after writing invalid entries.
257		 */
258		local_flush_tlb_page(addr);
259
 
 
 
 
260		return;
261	}
 
262}
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
  4 *  Lennox Wu <lennox.wu@sunplusct.com>
  5 *  Chen Liqin <liqin.chen@sunplusct.com>
  6 * Copyright (C) 2012 Regents of the University of California
  7 */
  8
  9
 10#include <linux/mm.h>
 11#include <linux/kernel.h>
 12#include <linux/interrupt.h>
 13#include <linux/perf_event.h>
 14#include <linux/signal.h>
 15#include <linux/uaccess.h>
 16#include <linux/kprobes.h>
 17#include <linux/kfence.h>
 18#include <linux/entry-common.h>
 19
 20#include <asm/ptrace.h>
 21#include <asm/tlbflush.h>
 22
 23#include "../kernel/head.h"
 24
 25static void die_kernel_fault(const char *msg, unsigned long addr,
 26		struct pt_regs *regs)
 27{
 28	bust_spinlocks(1);
 29
 30	pr_alert("Unable to handle kernel %s at virtual address " REG_FMT "\n", msg,
 31		addr);
 32
 33	bust_spinlocks(0);
 34	die(regs, "Oops");
 35	make_task_dead(SIGKILL);
 36}
 37
 38static inline void no_context(struct pt_regs *regs, unsigned long addr)
 39{
 40	const char *msg;
 41
 42	/* Are we prepared to handle this kernel fault? */
 43	if (fixup_exception(regs))
 44		return;
 45
 46	/*
 47	 * Oops. The kernel tried to access some bad page. We'll have to
 48	 * terminate things with extreme prejudice.
 49	 */
 50	if (addr < PAGE_SIZE)
 51		msg = "NULL pointer dereference";
 52	else {
 53		if (kfence_handle_page_fault(addr, regs->cause == EXC_STORE_PAGE_FAULT, regs))
 54			return;
 55
 56		msg = "paging request";
 57	}
 58
 59	die_kernel_fault(msg, addr, regs);
 60}
 61
 62static inline void mm_fault_error(struct pt_regs *regs, unsigned long addr, vm_fault_t fault)
 63{
 64	if (!user_mode(regs)) {
 65		no_context(regs, addr);
 66		return;
 67	}
 68
 69	if (fault & VM_FAULT_OOM) {
 70		/*
 71		 * We ran out of memory, call the OOM killer, and return the userspace
 72		 * (which will retry the fault, or kill us if we got oom-killed).
 73		 */
 74		pagefault_out_of_memory();
 75		return;
 76	} else if (fault & (VM_FAULT_SIGBUS | VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE)) {
 77		/* Kernel mode? Handle exceptions or die */
 78		do_trap(regs, SIGBUS, BUS_ADRERR, addr);
 79		return;
 80	} else if (fault & VM_FAULT_SIGSEGV) {
 81		do_trap(regs, SIGSEGV, SEGV_MAPERR, addr);
 82		return;
 83	}
 84
 85	BUG();
 86}
 87
 88static inline void
 89bad_area_nosemaphore(struct pt_regs *regs, int code, unsigned long addr)
 90{
 91	/*
 92	 * Something tried to access memory that isn't in our memory map.
 93	 * Fix it, but check if it's kernel or user first.
 94	 */
 95	/* User mode accesses just cause a SIGSEGV */
 96	if (user_mode(regs)) {
 97		do_trap(regs, SIGSEGV, code, addr);
 98		return;
 99	}
100
101	no_context(regs, addr);
102}
103
104static inline void
105bad_area(struct pt_regs *regs, struct mm_struct *mm, int code,
106	 unsigned long addr)
107{
108	mmap_read_unlock(mm);
109
110	bad_area_nosemaphore(regs, code, addr);
111}
112
113static inline void vmalloc_fault(struct pt_regs *regs, int code, unsigned long addr)
114{
115	pgd_t *pgd, *pgd_k;
116	pud_t *pud_k;
117	p4d_t *p4d_k;
118	pmd_t *pmd_k;
119	pte_t *pte_k;
120	int index;
121	unsigned long pfn;
122
123	/* User mode accesses just cause a SIGSEGV */
124	if (user_mode(regs))
125		return do_trap(regs, SIGSEGV, code, addr);
126
127	/*
128	 * Synchronize this task's top level page-table
129	 * with the 'reference' page table.
130	 *
131	 * Do _not_ use "tsk->active_mm->pgd" here.
132	 * We might be inside an interrupt in the middle
133	 * of a task switch.
134	 */
135	index = pgd_index(addr);
136	pfn = csr_read(CSR_SATP) & SATP_PPN;
137	pgd = (pgd_t *)pfn_to_virt(pfn) + index;
138	pgd_k = init_mm.pgd + index;
139
140	if (!pgd_present(pgdp_get(pgd_k))) {
141		no_context(regs, addr);
142		return;
143	}
144	set_pgd(pgd, pgdp_get(pgd_k));
145
146	p4d_k = p4d_offset(pgd_k, addr);
147	if (!p4d_present(p4dp_get(p4d_k))) {
148		no_context(regs, addr);
149		return;
150	}
151
152	pud_k = pud_offset(p4d_k, addr);
153	if (!pud_present(pudp_get(pud_k))) {
154		no_context(regs, addr);
155		return;
156	}
157	if (pud_leaf(pudp_get(pud_k)))
158		goto flush_tlb;
159
160	/*
161	 * Since the vmalloc area is global, it is unnecessary
162	 * to copy individual PTEs
163	 */
164	pmd_k = pmd_offset(pud_k, addr);
165	if (!pmd_present(pmdp_get(pmd_k))) {
166		no_context(regs, addr);
167		return;
168	}
169	if (pmd_leaf(pmdp_get(pmd_k)))
170		goto flush_tlb;
171
172	/*
173	 * Make sure the actual PTE exists as well to
174	 * catch kernel vmalloc-area accesses to non-mapped
175	 * addresses. If we don't do this, this will just
176	 * silently loop forever.
177	 */
178	pte_k = pte_offset_kernel(pmd_k, addr);
179	if (!pte_present(ptep_get(pte_k))) {
180		no_context(regs, addr);
181		return;
182	}
183
184	/*
185	 * The kernel assumes that TLBs don't cache invalid
186	 * entries, but in RISC-V, SFENCE.VMA specifies an
187	 * ordering constraint, not a cache flush; it is
188	 * necessary even after writing invalid entries.
189	 */
190flush_tlb:
191	local_flush_tlb_page(addr);
192}
193
194static inline bool access_error(unsigned long cause, struct vm_area_struct *vma)
195{
196	switch (cause) {
197	case EXC_INST_PAGE_FAULT:
198		if (!(vma->vm_flags & VM_EXEC)) {
199			return true;
200		}
201		break;
202	case EXC_LOAD_PAGE_FAULT:
203		/* Write implies read */
204		if (!(vma->vm_flags & (VM_READ | VM_WRITE))) {
205			return true;
206		}
207		break;
208	case EXC_STORE_PAGE_FAULT:
209		if (!(vma->vm_flags & VM_WRITE)) {
210			return true;
211		}
212		break;
213	default:
214		panic("%s: unhandled cause %lu", __func__, cause);
215	}
216	return false;
217}
218
219/*
220 * This routine handles page faults.  It determines the address and the
221 * problem, and then passes it off to one of the appropriate routines.
222 */
223void handle_page_fault(struct pt_regs *regs)
224{
225	struct task_struct *tsk;
226	struct vm_area_struct *vma;
227	struct mm_struct *mm;
228	unsigned long addr, cause;
229	unsigned int flags = FAULT_FLAG_DEFAULT;
230	int code = SEGV_MAPERR;
231	vm_fault_t fault;
232
233	cause = regs->cause;
234	addr = regs->badaddr;
235
236	tsk = current;
237	mm = tsk->mm;
238
239	if (kprobe_page_fault(regs, cause))
240		return;
241
242	/*
243	 * Fault-in kernel-space virtual memory on-demand.
244	 * The 'reference' page table is init_mm.pgd.
245	 *
246	 * NOTE! We MUST NOT take any locks for this case. We may
247	 * be in an interrupt or a critical region, and should
248	 * only copy the information from the master page table,
249	 * nothing more.
250	 */
251	if ((!IS_ENABLED(CONFIG_MMU) || !IS_ENABLED(CONFIG_64BIT)) &&
252	    unlikely(addr >= VMALLOC_START && addr < VMALLOC_END)) {
253		vmalloc_fault(regs, code, addr);
254		return;
255	}
256
257	/* Enable interrupts if they were enabled in the parent context. */
258	if (!regs_irqs_disabled(regs))
259		local_irq_enable();
260
261	/*
262	 * If we're in an interrupt, have no user context, or are running
263	 * in an atomic region, then we must not take the fault.
264	 */
265	if (unlikely(faulthandler_disabled() || !mm)) {
266		tsk->thread.bad_cause = cause;
267		no_context(regs, addr);
268		return;
269	}
270
271	if (user_mode(regs))
272		flags |= FAULT_FLAG_USER;
273
274	if (!user_mode(regs) && addr < TASK_SIZE && unlikely(!(regs->status & SR_SUM))) {
275		if (fixup_exception(regs))
276			return;
277
278		die_kernel_fault("access to user memory without uaccess routines", addr, regs);
279	}
280
281	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
282
283	if (cause == EXC_STORE_PAGE_FAULT)
284		flags |= FAULT_FLAG_WRITE;
285	else if (cause == EXC_INST_PAGE_FAULT)
286		flags |= FAULT_FLAG_INSTRUCTION;
287	if (!(flags & FAULT_FLAG_USER))
288		goto lock_mmap;
289
290	vma = lock_vma_under_rcu(mm, addr);
291	if (!vma)
292		goto lock_mmap;
293
294	if (unlikely(access_error(cause, vma))) {
295		vma_end_read(vma);
296		count_vm_vma_lock_event(VMA_LOCK_SUCCESS);
297		tsk->thread.bad_cause = cause;
298		bad_area_nosemaphore(regs, SEGV_ACCERR, addr);
299		return;
300	}
301
302	fault = handle_mm_fault(vma, addr, flags | FAULT_FLAG_VMA_LOCK, regs);
303	if (!(fault & (VM_FAULT_RETRY | VM_FAULT_COMPLETED)))
304		vma_end_read(vma);
305
306	if (!(fault & VM_FAULT_RETRY)) {
307		count_vm_vma_lock_event(VMA_LOCK_SUCCESS);
308		goto done;
309	}
310	count_vm_vma_lock_event(VMA_LOCK_RETRY);
311	if (fault & VM_FAULT_MAJOR)
312		flags |= FAULT_FLAG_TRIED;
313
314	if (fault_signal_pending(fault, regs)) {
315		if (!user_mode(regs))
316			no_context(regs, addr);
317		return;
318	}
319lock_mmap:
320
321retry:
322	vma = lock_mm_and_find_vma(mm, addr, regs);
323	if (unlikely(!vma)) {
324		tsk->thread.bad_cause = cause;
325		bad_area_nosemaphore(regs, code, addr);
326		return;
327	}
 
 
 
 
328
329	/*
330	 * Ok, we have a good vm_area for this memory access, so
331	 * we can handle it.
332	 */
 
333	code = SEGV_ACCERR;
334
335	if (unlikely(access_error(cause, vma))) {
336		tsk->thread.bad_cause = cause;
337		bad_area(regs, mm, code, addr);
338		return;
 
 
 
 
 
 
 
 
 
 
 
 
339	}
340
341	/*
342	 * If for any reason at all we could not handle the fault,
343	 * make sure we exit gracefully rather than endlessly redo
344	 * the fault.
345	 */
346	fault = handle_mm_fault(vma, addr, flags, regs);
347
348	/*
349	 * If we need to retry but a fatal signal is pending, handle the
350	 * signal first. We do not need to release the mmap_lock because it
351	 * would already be released in __lock_page_or_retry in mm/filemap.c.
352	 */
353	if (fault_signal_pending(fault, regs)) {
354		if (!user_mode(regs))
355			no_context(regs, addr);
356		return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
357	}
358
359	/* The fault is fully completed (including releasing mmap lock) */
360	if (fault & VM_FAULT_COMPLETED)
 
 
 
 
 
 
 
 
 
 
361		return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
362
363	if (unlikely(fault & VM_FAULT_RETRY)) {
364		flags |= FAULT_FLAG_TRIED;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
365
366		/*
367		 * No need to mmap_read_unlock(mm) as we would
368		 * have already released it in __lock_page_or_retry
369		 * in mm/filemap.c.
 
 
 
370		 */
371		goto retry;
372	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
373
374	mmap_read_unlock(mm);
 
 
 
 
 
 
375
376done:
377	if (unlikely(fault & VM_FAULT_ERROR)) {
378		tsk->thread.bad_cause = cause;
379		mm_fault_error(regs, addr, fault);
380		return;
381	}
382	return;
383}