Linux Audio

Check our new training course

In-person Linux kernel drivers training

Jun 16-20, 2025
Register
Loading...
v3.1
  1/*
  2 * This file is subject to the terms and conditions of the GNU General Public
  3 * License.  See the file "COPYING" in the main directory of this archive
  4 * for more details.
  5 *
  6 * Copyright (C) 1995 - 2000 by Ralf Baechle
  7 */
 
  8#include <linux/signal.h>
  9#include <linux/sched.h>
 10#include <linux/interrupt.h>
 11#include <linux/kernel.h>
 12#include <linux/errno.h>
 13#include <linux/string.h>
 14#include <linux/types.h>
 15#include <linux/ptrace.h>
 
 16#include <linux/mman.h>
 17#include <linux/mm.h>
 18#include <linux/smp.h>
 19#include <linux/module.h>
 20#include <linux/kprobes.h>
 21#include <linux/perf_event.h>
 
 22
 23#include <asm/branch.h>
 24#include <asm/mmu_context.h>
 25#include <asm/system.h>
 26#include <asm/uaccess.h>
 27#include <asm/ptrace.h>
 28#include <asm/highmem.h>		/* For VMALLOC_END */
 29#include <linux/kdebug.h>
 30
 
 
 31/*
 32 * This routine handles page faults.  It determines the address,
 33 * and the problem, and then passes it off to one of the appropriate
 34 * routines.
 35 */
 36asmlinkage void __kprobes do_page_fault(struct pt_regs *regs, unsigned long write,
 37			      unsigned long address)
 38{
 39	struct vm_area_struct * vma = NULL;
 40	struct task_struct *tsk = current;
 41	struct mm_struct *mm = tsk->mm;
 42	const int field = sizeof(unsigned long) * 2;
 43	siginfo_t info;
 44	int fault;
 
 
 
 45
 46#if 0
 47	printk("Cpu%d[%s:%d:%0*lx:%ld:%0*lx]\n", raw_smp_processor_id(),
 48	       current->comm, current->pid, field, address, write,
 49	       field, regs->cp0_epc);
 50#endif
 51
 52#ifdef CONFIG_KPROBES
 53	/*
 54	 * This is to notify the fault handler of the kprobes.  The
 55	 * exception code is redundant as it is also carried in REGS,
 56	 * but we pass it anyhow.
 57	 */
 58	if (notify_die(DIE_PAGE_FAULT, "page fault", regs, -1,
 59		       (regs->cp0_cause >> 2) & 0x1f, SIGSEGV) == NOTIFY_STOP)
 60		return;
 61#endif
 62
 63	info.si_code = SEGV_MAPERR;
 64
 65	/*
 66	 * We fault-in kernel-space virtual memory on-demand. The
 67	 * 'reference' page table is init_mm.pgd.
 68	 *
 69	 * NOTE! We MUST NOT take any locks for this case. We may
 70	 * be in an interrupt or a critical region, and should
 71	 * only copy the information from the master page table,
 72	 * nothing more.
 73	 */
 74#ifdef CONFIG_64BIT
 75# define VMALLOC_FAULT_TARGET no_context
 76#else
 77# define VMALLOC_FAULT_TARGET vmalloc_fault
 78#endif
 79
 80	if (unlikely(address >= VMALLOC_START && address <= VMALLOC_END))
 81		goto VMALLOC_FAULT_TARGET;
 82#ifdef MODULE_START
 83	if (unlikely(address >= MODULE_START && address < MODULE_END))
 84		goto VMALLOC_FAULT_TARGET;
 85#endif
 86
 87	/*
 88	 * If we're in an interrupt or have no user
 89	 * context, we must not take the fault..
 90	 */
 91	if (in_atomic() || !mm)
 92		goto bad_area_nosemaphore;
 93
 
 
 
 94	down_read(&mm->mmap_sem);
 95	vma = find_vma(mm, address);
 96	if (!vma)
 97		goto bad_area;
 98	if (vma->vm_start <= address)
 99		goto good_area;
100	if (!(vma->vm_flags & VM_GROWSDOWN))
101		goto bad_area;
102	if (expand_stack(vma, address))
103		goto bad_area;
104/*
105 * Ok, we have a good vm_area for this memory access, so
106 * we can handle it..
107 */
108good_area:
109	info.si_code = SEGV_ACCERR;
110
111	if (write) {
112		if (!(vma->vm_flags & VM_WRITE))
113			goto bad_area;
 
114	} else {
115		if (kernel_uses_smartmips_rixi) {
116			if (address == regs->cp0_epc && !(vma->vm_flags & VM_EXEC)) {
117#if 0
118				pr_notice("Cpu%d[%s:%d:%0*lx:%ld:%0*lx] XI violation\n",
119					  raw_smp_processor_id(),
120					  current->comm, current->pid,
121					  field, address, write,
122					  field, regs->cp0_epc);
123#endif
124				goto bad_area;
125			}
126			if (!(vma->vm_flags & VM_READ)) {
 
127#if 0
128				pr_notice("Cpu%d[%s:%d:%0*lx:%ld:%0*lx] RI violation\n",
129					  raw_smp_processor_id(),
130					  current->comm, current->pid,
131					  field, address, write,
132					  field, regs->cp0_epc);
133#endif
134				goto bad_area;
135			}
136		} else {
137			if (!(vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)))
138				goto bad_area;
139		}
140	}
141
142	/*
143	 * If for any reason at all we couldn't handle the fault,
144	 * make sure we exit gracefully rather than endlessly redo
145	 * the fault.
146	 */
147	fault = handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0);
 
 
 
 
148	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
149	if (unlikely(fault & VM_FAULT_ERROR)) {
150		if (fault & VM_FAULT_OOM)
151			goto out_of_memory;
 
 
152		else if (fault & VM_FAULT_SIGBUS)
153			goto do_sigbus;
154		BUG();
155	}
156	if (fault & VM_FAULT_MAJOR) {
157		perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address);
158		tsk->maj_flt++;
159	} else {
160		perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address);
161		tsk->min_flt++;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162	}
163
164	up_read(&mm->mmap_sem);
165	return;
166
167/*
168 * Something tried to access memory that isn't in our memory map..
169 * Fix it, but check if it's kernel or user first..
170 */
171bad_area:
172	up_read(&mm->mmap_sem);
173
174bad_area_nosemaphore:
175	/* User mode accesses just cause a SIGSEGV */
176	if (user_mode(regs)) {
177		tsk->thread.cp0_badvaddr = address;
178		tsk->thread.error_code = write;
179#if 0
180		printk("do_page_fault() #2: sending SIGSEGV to %s for "
181		       "invalid %s\n%0*lx (epc == %0*lx, ra == %0*lx)\n",
182		       tsk->comm,
183		       write ? "write access to" : "read access from",
184		       field, address,
185		       field, (unsigned long) regs->cp0_epc,
186		       field, (unsigned long) regs->regs[31]);
187#endif
 
 
 
 
 
 
 
188		info.si_signo = SIGSEGV;
189		info.si_errno = 0;
190		/* info.si_code has been set above */
191		info.si_addr = (void __user *) address;
192		force_sig_info(SIGSEGV, &info, tsk);
193		return;
194	}
195
196no_context:
197	/* Are we prepared to handle this kernel fault?  */
198	if (fixup_exception(regs)) {
199		current->thread.cp0_baduaddr = address;
200		return;
201	}
202
203	/*
204	 * Oops. The kernel tried to access some bad page. We'll have to
205	 * terminate things with extreme prejudice.
206	 */
207	bust_spinlocks(1);
208
209	printk(KERN_ALERT "CPU %d Unable to handle kernel paging request at "
210	       "virtual address %0*lx, epc == %0*lx, ra == %0*lx\n",
211	       raw_smp_processor_id(), field, address, field, regs->cp0_epc,
212	       field,  regs->regs[31]);
213	die("Oops", regs);
214
215out_of_memory:
216	/*
217	 * We ran out of memory, call the OOM killer, and return the userspace
218	 * (which will retry the fault, or kill us if we got oom-killed).
219	 */
220	up_read(&mm->mmap_sem);
 
 
221	pagefault_out_of_memory();
222	return;
223
224do_sigbus:
225	up_read(&mm->mmap_sem);
226
227	/* Kernel mode? Handle exceptions or die */
228	if (!user_mode(regs))
229		goto no_context;
230	else
231	/*
232	 * Send a sigbus, regardless of whether we were in kernel
233	 * or user mode.
234	 */
235#if 0
236		printk("do_page_fault() #3: sending SIGBUS to %s for "
237		       "invalid %s\n%0*lx (epc == %0*lx, ra == %0*lx)\n",
238		       tsk->comm,
239		       write ? "write access to" : "read access from",
240		       field, address,
241		       field, (unsigned long) regs->cp0_epc,
242		       field, (unsigned long) regs->regs[31]);
243#endif
 
244	tsk->thread.cp0_badvaddr = address;
245	info.si_signo = SIGBUS;
246	info.si_errno = 0;
247	info.si_code = BUS_ADRERR;
248	info.si_addr = (void __user *) address;
249	force_sig_info(SIGBUS, &info, tsk);
250
251	return;
252#ifndef CONFIG_64BIT
253vmalloc_fault:
254	{
255		/*
256		 * Synchronize this task's top level page-table
257		 * with the 'reference' page table.
258		 *
259		 * Do _not_ use "tsk" here. We might be inside
260		 * an interrupt in the middle of a task switch..
261		 */
262		int offset = __pgd_offset(address);
263		pgd_t *pgd, *pgd_k;
264		pud_t *pud, *pud_k;
265		pmd_t *pmd, *pmd_k;
266		pte_t *pte_k;
267
268		pgd = (pgd_t *) pgd_current[raw_smp_processor_id()] + offset;
269		pgd_k = init_mm.pgd + offset;
270
271		if (!pgd_present(*pgd_k))
272			goto no_context;
273		set_pgd(pgd, *pgd_k);
274
275		pud = pud_offset(pgd, address);
276		pud_k = pud_offset(pgd_k, address);
277		if (!pud_present(*pud_k))
278			goto no_context;
279
280		pmd = pmd_offset(pud, address);
281		pmd_k = pmd_offset(pud_k, address);
282		if (!pmd_present(*pmd_k))
283			goto no_context;
284		set_pmd(pmd, *pmd_k);
285
286		pte_k = pte_offset_kernel(pmd_k, address);
287		if (!pte_present(*pte_k))
288			goto no_context;
289		return;
290	}
291#endif
 
 
 
 
 
 
 
 
 
 
292}
v4.6
  1/*
  2 * This file is subject to the terms and conditions of the GNU General Public
  3 * License.  See the file "COPYING" in the main directory of this archive
  4 * for more details.
  5 *
  6 * Copyright (C) 1995 - 2000 by Ralf Baechle
  7 */
  8#include <linux/context_tracking.h>
  9#include <linux/signal.h>
 10#include <linux/sched.h>
 11#include <linux/interrupt.h>
 12#include <linux/kernel.h>
 13#include <linux/errno.h>
 14#include <linux/string.h>
 15#include <linux/types.h>
 16#include <linux/ptrace.h>
 17#include <linux/ratelimit.h>
 18#include <linux/mman.h>
 19#include <linux/mm.h>
 20#include <linux/smp.h>
 21#include <linux/module.h>
 22#include <linux/kprobes.h>
 23#include <linux/perf_event.h>
 24#include <linux/uaccess.h>
 25
 26#include <asm/branch.h>
 27#include <asm/mmu_context.h>
 
 
 28#include <asm/ptrace.h>
 29#include <asm/highmem.h>		/* For VMALLOC_END */
 30#include <linux/kdebug.h>
 31
 32int show_unhandled_signals = 1;
 33
 34/*
 35 * This routine handles page faults.  It determines the address,
 36 * and the problem, and then passes it off to one of the appropriate
 37 * routines.
 38 */
 39static void __kprobes __do_page_fault(struct pt_regs *regs, unsigned long write,
 40	unsigned long address)
 41{
 42	struct vm_area_struct * vma = NULL;
 43	struct task_struct *tsk = current;
 44	struct mm_struct *mm = tsk->mm;
 45	const int field = sizeof(unsigned long) * 2;
 46	siginfo_t info;
 47	int fault;
 48	unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
 49
 50	static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
 51
 52#if 0
 53	printk("Cpu%d[%s:%d:%0*lx:%ld:%0*lx]\n", raw_smp_processor_id(),
 54	       current->comm, current->pid, field, address, write,
 55	       field, regs->cp0_epc);
 56#endif
 57
 58#ifdef CONFIG_KPROBES
 59	/*
 60	 * This is to notify the fault handler of the kprobes.
 
 
 61	 */
 62	if (notify_die(DIE_PAGE_FAULT, "page fault", regs, -1,
 63		       current->thread.trap_nr, SIGSEGV) == NOTIFY_STOP)
 64		return;
 65#endif
 66
 67	info.si_code = SEGV_MAPERR;
 68
 69	/*
 70	 * We fault-in kernel-space virtual memory on-demand. The
 71	 * 'reference' page table is init_mm.pgd.
 72	 *
 73	 * NOTE! We MUST NOT take any locks for this case. We may
 74	 * be in an interrupt or a critical region, and should
 75	 * only copy the information from the master page table,
 76	 * nothing more.
 77	 */
 78#ifdef CONFIG_64BIT
 79# define VMALLOC_FAULT_TARGET no_context
 80#else
 81# define VMALLOC_FAULT_TARGET vmalloc_fault
 82#endif
 83
 84	if (unlikely(address >= VMALLOC_START && address <= VMALLOC_END))
 85		goto VMALLOC_FAULT_TARGET;
 86#ifdef MODULE_START
 87	if (unlikely(address >= MODULE_START && address < MODULE_END))
 88		goto VMALLOC_FAULT_TARGET;
 89#endif
 90
 91	/*
 92	 * If we're in an interrupt or have no user
 93	 * context, we must not take the fault..
 94	 */
 95	if (faulthandler_disabled() || !mm)
 96		goto bad_area_nosemaphore;
 97
 98	if (user_mode(regs))
 99		flags |= FAULT_FLAG_USER;
100retry:
101	down_read(&mm->mmap_sem);
102	vma = find_vma(mm, address);
103	if (!vma)
104		goto bad_area;
105	if (vma->vm_start <= address)
106		goto good_area;
107	if (!(vma->vm_flags & VM_GROWSDOWN))
108		goto bad_area;
109	if (expand_stack(vma, address))
110		goto bad_area;
111/*
112 * Ok, we have a good vm_area for this memory access, so
113 * we can handle it..
114 */
115good_area:
116	info.si_code = SEGV_ACCERR;
117
118	if (write) {
119		if (!(vma->vm_flags & VM_WRITE))
120			goto bad_area;
121		flags |= FAULT_FLAG_WRITE;
122	} else {
123		if (cpu_has_rixi) {
124			if (address == regs->cp0_epc && !(vma->vm_flags & VM_EXEC)) {
125#if 0
126				pr_notice("Cpu%d[%s:%d:%0*lx:%ld:%0*lx] XI violation\n",
127					  raw_smp_processor_id(),
128					  current->comm, current->pid,
129					  field, address, write,
130					  field, regs->cp0_epc);
131#endif
132				goto bad_area;
133			}
134			if (!(vma->vm_flags & VM_READ) &&
135			    exception_epc(regs) != address) {
136#if 0
137				pr_notice("Cpu%d[%s:%d:%0*lx:%ld:%0*lx] RI violation\n",
138					  raw_smp_processor_id(),
139					  current->comm, current->pid,
140					  field, address, write,
141					  field, regs->cp0_epc);
142#endif
143				goto bad_area;
144			}
145		} else {
146			if (!(vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)))
147				goto bad_area;
148		}
149	}
150
151	/*
152	 * If for any reason at all we couldn't handle the fault,
153	 * make sure we exit gracefully rather than endlessly redo
154	 * the fault.
155	 */
156	fault = handle_mm_fault(mm, vma, address, flags);
157
158	if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
159		return;
160
161	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
162	if (unlikely(fault & VM_FAULT_ERROR)) {
163		if (fault & VM_FAULT_OOM)
164			goto out_of_memory;
165		else if (fault & VM_FAULT_SIGSEGV)
166			goto bad_area;
167		else if (fault & VM_FAULT_SIGBUS)
168			goto do_sigbus;
169		BUG();
170	}
171	if (flags & FAULT_FLAG_ALLOW_RETRY) {
172		if (fault & VM_FAULT_MAJOR) {
173			perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
174						  regs, address);
175			tsk->maj_flt++;
176		} else {
177			perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
178						  regs, address);
179			tsk->min_flt++;
180		}
181		if (fault & VM_FAULT_RETRY) {
182			flags &= ~FAULT_FLAG_ALLOW_RETRY;
183			flags |= FAULT_FLAG_TRIED;
184
185			/*
186			 * No need to up_read(&mm->mmap_sem) as we would
187			 * have already released it in __lock_page_or_retry
188			 * in mm/filemap.c.
189			 */
190
191			goto retry;
192		}
193	}
194
195	up_read(&mm->mmap_sem);
196	return;
197
198/*
199 * Something tried to access memory that isn't in our memory map..
200 * Fix it, but check if it's kernel or user first..
201 */
202bad_area:
203	up_read(&mm->mmap_sem);
204
205bad_area_nosemaphore:
206	/* User mode accesses just cause a SIGSEGV */
207	if (user_mode(regs)) {
208		tsk->thread.cp0_badvaddr = address;
209		tsk->thread.error_code = write;
210		if (show_unhandled_signals &&
211		    unhandled_signal(tsk, SIGSEGV) &&
212		    __ratelimit(&ratelimit_state)) {
213			pr_info("\ndo_page_fault(): sending SIGSEGV to %s for invalid %s %0*lx",
214				tsk->comm,
215				write ? "write access to" : "read access from",
216				field, address);
217			pr_info("epc = %0*lx in", field,
218				(unsigned long) regs->cp0_epc);
219			print_vma_addr(" ", regs->cp0_epc);
220			pr_info("ra  = %0*lx in", field,
221				(unsigned long) regs->regs[31]);
222			print_vma_addr(" ", regs->regs[31]);
223			pr_info("\n");
224		}
225		current->thread.trap_nr = (regs->cp0_cause >> 2) & 0x1f;
226		info.si_signo = SIGSEGV;
227		info.si_errno = 0;
228		/* info.si_code has been set above */
229		info.si_addr = (void __user *) address;
230		force_sig_info(SIGSEGV, &info, tsk);
231		return;
232	}
233
234no_context:
235	/* Are we prepared to handle this kernel fault?	 */
236	if (fixup_exception(regs)) {
237		current->thread.cp0_baduaddr = address;
238		return;
239	}
240
241	/*
242	 * Oops. The kernel tried to access some bad page. We'll have to
243	 * terminate things with extreme prejudice.
244	 */
245	bust_spinlocks(1);
246
247	printk(KERN_ALERT "CPU %d Unable to handle kernel paging request at "
248	       "virtual address %0*lx, epc == %0*lx, ra == %0*lx\n",
249	       raw_smp_processor_id(), field, address, field, regs->cp0_epc,
250	       field,  regs->regs[31]);
251	die("Oops", regs);
252
253out_of_memory:
254	/*
255	 * We ran out of memory, call the OOM killer, and return the userspace
256	 * (which will retry the fault, or kill us if we got oom-killed).
257	 */
258	up_read(&mm->mmap_sem);
259	if (!user_mode(regs))
260		goto no_context;
261	pagefault_out_of_memory();
262	return;
263
264do_sigbus:
265	up_read(&mm->mmap_sem);
266
267	/* Kernel mode? Handle exceptions or die */
268	if (!user_mode(regs))
269		goto no_context;
270	else
271	/*
272	 * Send a sigbus, regardless of whether we were in kernel
273	 * or user mode.
274	 */
275#if 0
276		printk("do_page_fault() #3: sending SIGBUS to %s for "
277		       "invalid %s\n%0*lx (epc == %0*lx, ra == %0*lx)\n",
278		       tsk->comm,
279		       write ? "write access to" : "read access from",
280		       field, address,
281		       field, (unsigned long) regs->cp0_epc,
282		       field, (unsigned long) regs->regs[31]);
283#endif
284	current->thread.trap_nr = (regs->cp0_cause >> 2) & 0x1f;
285	tsk->thread.cp0_badvaddr = address;
286	info.si_signo = SIGBUS;
287	info.si_errno = 0;
288	info.si_code = BUS_ADRERR;
289	info.si_addr = (void __user *) address;
290	force_sig_info(SIGBUS, &info, tsk);
291
292	return;
293#ifndef CONFIG_64BIT
294vmalloc_fault:
295	{
296		/*
297		 * Synchronize this task's top level page-table
298		 * with the 'reference' page table.
299		 *
300		 * Do _not_ use "tsk" here. We might be inside
301		 * an interrupt in the middle of a task switch..
302		 */
303		int offset = __pgd_offset(address);
304		pgd_t *pgd, *pgd_k;
305		pud_t *pud, *pud_k;
306		pmd_t *pmd, *pmd_k;
307		pte_t *pte_k;
308
309		pgd = (pgd_t *) pgd_current[raw_smp_processor_id()] + offset;
310		pgd_k = init_mm.pgd + offset;
311
312		if (!pgd_present(*pgd_k))
313			goto no_context;
314		set_pgd(pgd, *pgd_k);
315
316		pud = pud_offset(pgd, address);
317		pud_k = pud_offset(pgd_k, address);
318		if (!pud_present(*pud_k))
319			goto no_context;
320
321		pmd = pmd_offset(pud, address);
322		pmd_k = pmd_offset(pud_k, address);
323		if (!pmd_present(*pmd_k))
324			goto no_context;
325		set_pmd(pmd, *pmd_k);
326
327		pte_k = pte_offset_kernel(pmd_k, address);
328		if (!pte_present(*pte_k))
329			goto no_context;
330		return;
331	}
332#endif
333}
334
335asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
336	unsigned long write, unsigned long address)
337{
338	enum ctx_state prev_state;
339
340	prev_state = exception_enter();
341	__do_page_fault(regs, write, address);
342	exception_exit(prev_state);
343}