Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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