^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/hardirq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/sched/debug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <asm/current.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <asm/tlbflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <arch.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <as-layout.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <kern_util.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <os.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <skas.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * Note this is constrained to return 0, -EFAULT, -EACCES, -ENOMEM by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * segv().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) int handle_page_fault(unsigned long address, unsigned long ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) int is_write, int is_user, int *code_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct mm_struct *mm = current->mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct vm_area_struct *vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) pmd_t *pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) pte_t *pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) int err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) unsigned int flags = FAULT_FLAG_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) *code_out = SEGV_MAPERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * If the fault was with pagefaults disabled, don't take the fault, just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (faulthandler_disabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) goto out_nosemaphore;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (is_user)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) flags |= FAULT_FLAG_USER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) mmap_read_lock(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) vma = find_vma(mm, address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (!vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) else if (vma->vm_start <= address)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) goto good_area;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) else if (!(vma->vm_flags & VM_GROWSDOWN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) else if (is_user && !ARCH_IS_STACKGROW(address))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) else if (expand_stack(vma, address))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) good_area:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) *code_out = SEGV_ACCERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (is_write) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (!(vma->vm_flags & VM_WRITE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) flags |= FAULT_FLAG_WRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) /* Don't require VM_READ|VM_EXEC for write faults! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) vm_fault_t fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) fault = handle_mm_fault(vma, address, flags, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) goto out_nosemaphore;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (unlikely(fault & VM_FAULT_ERROR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (fault & VM_FAULT_OOM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) goto out_of_memory;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) } else if (fault & VM_FAULT_SIGSEGV) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) } else if (fault & VM_FAULT_SIGBUS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) err = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (flags & FAULT_FLAG_ALLOW_RETRY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (fault & VM_FAULT_RETRY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) flags |= FAULT_FLAG_TRIED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) pmd = pmd_off(mm, address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) pte = pte_offset_kernel(pmd, address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) } while (!pte_present(*pte));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * The below warning was added in place of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * pte_mkyoung(); if (is_write) pte_mkdirty();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * If it's triggered, we'd see normally a hang here (a clean pte is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * marked read-only to emulate the dirty bit).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * However, the generic code can mark a PTE writable but clean on a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * concurrent read fault, triggering this harmlessly. So comment it out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) #if 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) WARN_ON(!pte_young(*pte) || (is_write && !pte_dirty(*pte)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) flush_tlb_page(vma, address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) mmap_read_unlock(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) out_nosemaphore:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) out_of_memory:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * We ran out of memory, call the OOM killer, and return the userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * (which will retry the fault, or kill us if we got oom-killed).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) mmap_read_unlock(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (!is_user)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) goto out_nosemaphore;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) pagefault_out_of_memory();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) EXPORT_SYMBOL(handle_page_fault);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static void show_segv_info(struct uml_pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct task_struct *tsk = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct faultinfo *fi = UPT_FAULTINFO(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (!unhandled_signal(tsk, SIGSEGV))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (!printk_ratelimit())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) printk("%s%s[%d]: segfault at %lx ip %px sp %px error %x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) tsk->comm, task_pid_nr(tsk), FAULT_ADDRESS(*fi),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) (void *)UPT_IP(regs), (void *)UPT_SP(regs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) fi->error_code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) print_vma_addr(KERN_CONT " in ", UPT_IP(regs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) printk(KERN_CONT "\n");
^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) static void bad_segv(struct faultinfo fi, unsigned long ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) current->thread.arch.faultinfo = fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) force_sig_fault(SIGSEGV, SEGV_ACCERR, (void __user *) FAULT_ADDRESS(fi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) void fatal_sigsegv(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) force_sigsegv(SIGSEGV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) do_signal(¤t->thread.regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * This is to tell gcc that we're not returning - do_signal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * can, in general, return, but in this case, it's not, since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * we just got a fatal SIGSEGV queued.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) os_dump_core();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * segv_handler() - the SIGSEGV handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * @sig: the signal number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * @unused_si: the signal info struct; unused in this handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * @regs: the ptrace register information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) * The handler first extracts the faultinfo from the UML ptrace regs struct.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * If the userfault did not happen in an UML userspace process, bad_segv is called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * Otherwise the signal did happen in a cloned userspace process, handle it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) void segv_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) struct faultinfo * fi = UPT_FAULTINFO(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (UPT_IS_USER(regs) && !SEGV_IS_FIXABLE(fi)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) show_segv_info(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) bad_segv(*fi, UPT_IP(regs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) segv(*fi, UPT_IP(regs), UPT_IS_USER(regs), regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * We give a *copy* of the faultinfo in the regs to segv.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * This must be done, since nesting SEGVs could overwrite
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * the info in the regs. A pointer to the info then would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * give us bad data!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) struct uml_pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) jmp_buf *catcher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) int si_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) int is_write = FAULT_WRITE(fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) unsigned long address = FAULT_ADDRESS(fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (!is_user && regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) current->thread.segv_regs = container_of(regs, struct pt_regs, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (!is_user && (address >= start_vm) && (address < end_vm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) flush_tlb_kernel_vm();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) else if (current->mm == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) show_regs(container_of(regs, struct pt_regs, regs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) panic("Segfault with no mm");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) else if (!is_user && address > PAGE_SIZE && address < TASK_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) show_regs(container_of(regs, struct pt_regs, regs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) panic("Kernel tried to access user memory at addr 0x%lx, ip 0x%lx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) address, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (SEGV_IS_FIXABLE(&fi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) err = handle_page_fault(address, ip, is_write, is_user,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) &si_code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) * A thread accessed NULL, we get a fault, but CR2 is invalid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * This code is used in __do_copy_from_user() of TT mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) * XXX tt mode is gone, so maybe this isn't needed any more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) address = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) catcher = current->thread.fault_catcher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) else if (catcher != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) current->thread.fault_addr = (void *) address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) UML_LONGJMP(catcher, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) else if (current->thread.fault_addr != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) panic("fault_addr set but no fault catcher");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) else if (!is_user && arch_fixup(ip, regs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (!is_user) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) show_regs(container_of(regs, struct pt_regs, regs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) address, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) show_segv_info(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) if (err == -EACCES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) current->thread.arch.faultinfo = fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) BUG_ON(err != -EFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) current->thread.arch.faultinfo = fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) force_sig_fault(SIGSEGV, si_code, (void __user *) address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) current->thread.segv_regs = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) void relay_signal(int sig, struct siginfo *si, struct uml_pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) int code, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (!UPT_IS_USER(regs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (sig == SIGBUS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) printk(KERN_ERR "Bus error - the host /dev/shm or /tmp "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) "mount likely just ran out of space\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) panic("Kernel mode signal %d", sig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) arch_examine_signal(sig, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) /* Is the signal layout for the signal known?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * Signal data must be scrubbed to prevent information leaks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) code = si->si_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) err = si->si_errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if ((err == 0) && (siginfo_layout(sig, code) == SIL_FAULT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) struct faultinfo *fi = UPT_FAULTINFO(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) current->thread.arch.faultinfo = *fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) force_sig_fault(sig, code, (void __user *)FAULT_ADDRESS(*fi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) printk(KERN_ERR "Attempted to relay unknown signal %d (si_code = %d) with errno %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) sig, code, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) force_sig(sig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) void bus_handler(int sig, struct siginfo *si, struct uml_pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (current->thread.fault_catcher != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) UML_LONGJMP(current->thread.fault_catcher, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) relay_signal(sig, si, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) void winch(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) do_IRQ(WINCH_IRQ, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) void trap_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }