1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) 4 */ 5 6 #include <linux/mm.h> 7 #include <linux/sched/signal.h> 8 #include <linux/hardirq.h> 9 #include <linux/module.h> 10 #include <linux/uaccess.h> 11 #include <linux/sched/debug.h> 12 #include <asm/current.h> 13 #include <asm/tlbflush.h> 14 #include <arch.h> 15 #include <as-layout.h> 16 #include <kern_util.h> 17 #include <os.h> 18 #include <skas.h> 19 20 static void show_segv_info(struct uml_pt_regs *regs) 21 { 22 struct task_struct *tsk = current; 23 struct faultinfo *fi = UPT_FAULTINFO(regs); 24 25 if (!unhandled_signal(tsk, SIGSEGV)) 26 return; 27 28 if (!printk_ratelimit()) 29 return; 30 31 printk("%s%s[%d]: segfault at %lx ip %px sp %px error %x", 32 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG, 33 tsk->comm, task_pid_nr(tsk), FAULT_ADDRESS(*fi), 34 (void *)UPT_IP(regs), (void *)UPT_SP(regs), 35 fi->error_code); 36 37 print_vma_addr(KERN_CONT " in ", UPT_IP(regs)); 38 printk(KERN_CONT "\n"); 39 } 40 41 static void bad_segv(struct faultinfo fi, unsigned long ip) 42 { 43 current->thread.arch.faultinfo = fi; 44 force_sig_fault(SIGSEGV, SEGV_ACCERR, (void __user *) FAULT_ADDRESS(fi)); 45 } 46 47 void fatal_sigsegv(void) 48 { 49 force_fatal_sig(SIGSEGV); 50 do_signal(¤t->thread.regs); 51 /* 52 * This is to tell gcc that we're not returning - do_signal 53 * can, in general, return, but in this case, it's not, since 54 * we just got a fatal SIGSEGV queued. 55 */ 56 os_dump_core(); 57 } 58 59 /** 60 * segv_handler() - the SIGSEGV handler 61 * @sig: the signal number 62 * @unused_si: the signal info struct; unused in this handler 63 * @regs: the ptrace register information 64 * @mc: the mcontext of the signal 65 * 66 * The handler first extracts the faultinfo from the UML ptrace regs struct. 67 * If the userfault did not happen in an UML userspace process, bad_segv is called. 68 * Otherwise the signal did happen in a cloned userspace process, handle it. 69 */ 70 void segv_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs, 71 void *mc) 72 { 73 struct faultinfo * fi = UPT_FAULTINFO(regs); 74 75 if (UPT_IS_USER(regs) && !SEGV_IS_FIXABLE(fi)) { 76 show_segv_info(regs); 77 bad_segv(*fi, UPT_IP(regs)); 78 return; 79 } 80 segv(*fi, UPT_IP(regs), UPT_IS_USER(regs), regs, mc); 81 } 82 83 /* 84 * We give a *copy* of the faultinfo in the regs to segv. 85 * This must be done, since nesting SEGVs could overwrite 86 * the info in the regs. A pointer to the info then would 87 * give us bad data! 88 */ 89 unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user, 90 struct uml_pt_regs *regs, void *mc) 91 { 92 int si_code; 93 int err; 94 int is_write = FAULT_WRITE(fi); 95 unsigned long address = FAULT_ADDRESS(fi); 96 97 if (!is_user && regs) 98 current->thread.segv_regs = container_of(regs, struct pt_regs, regs); 99 100 if (!is_user && address >= start_vm && address < end_vm) { 101 /* 102 * Kernel has pending updates from set_ptes that were not 103 * flushed yet. Syncing them should fix the pagefault (if not 104 * we'll get here again and panic). 105 */ 106 err = um_tlb_sync(&init_mm); 107 if (err == -ENOMEM) 108 report_enomem(); 109 if (err) 110 panic("Failed to sync kernel TLBs: %d", err); 111 goto out; 112 } 113 else if (current->pagefault_disabled) { 114 if (!mc) { 115 show_regs(container_of(regs, struct pt_regs, regs)); 116 panic("Segfault with pagefaults disabled but no mcontext"); 117 } 118 if (!current->thread.segv_continue) { 119 show_regs(container_of(regs, struct pt_regs, regs)); 120 panic("Segfault without recovery target"); 121 } 122 mc_set_rip(mc, current->thread.segv_continue); 123 current->thread.segv_continue = NULL; 124 goto out; 125 } 126 else if (current->mm == NULL) { 127 show_regs(container_of(regs, struct pt_regs, regs)); 128 panic("Segfault with no mm"); 129 } 130 else if (!is_user && address > PAGE_SIZE && address < TASK_SIZE) { 131 show_regs(container_of(regs, struct pt_regs, regs)); 132 panic("Kernel tried to access user memory at addr 0x%lx, ip 0x%lx", 133 address, ip); 134 } 135 136 if (SEGV_IS_FIXABLE(&fi)) 137 err = handle_page_fault(address, ip, is_write, is_user, 138 &si_code); 139 else { 140 err = -EFAULT; 141 /* 142 * A thread accessed NULL, we get a fault, but CR2 is invalid. 143 * This code is used in __do_copy_from_user() of TT mode. 144 * XXX tt mode is gone, so maybe this isn't needed any more 145 */ 146 address = 0; 147 } 148 149 if (!err) 150 goto out; 151 else if (!is_user && arch_fixup(ip, regs)) 152 goto out; 153 154 if (!is_user) { 155 show_regs(container_of(regs, struct pt_regs, regs)); 156 panic("Kernel mode fault at addr 0x%lx, ip 0x%lx", 157 address, ip); 158 } 159 160 show_segv_info(regs); 161 162 if (err == -EACCES) { 163 current->thread.arch.faultinfo = fi; 164 force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address); 165 } else { 166 BUG_ON(err != -EFAULT); 167 current->thread.arch.faultinfo = fi; 168 force_sig_fault(SIGSEGV, si_code, (void __user *) address); 169 } 170 171 out: 172 if (regs) 173 current->thread.segv_regs = NULL; 174 175 return 0; 176 } 177 178 void relay_signal(int sig, struct siginfo *si, struct uml_pt_regs *regs, 179 void *mc) 180 { 181 int code, err; 182 if (!UPT_IS_USER(regs)) { 183 if (sig == SIGBUS) 184 printk(KERN_ERR "Bus error - the host /dev/shm or /tmp " 185 "mount likely just ran out of space\n"); 186 panic("Kernel mode signal %d", sig); 187 } 188 189 arch_examine_signal(sig, regs); 190 191 /* Is the signal layout for the signal known? 192 * Signal data must be scrubbed to prevent information leaks. 193 */ 194 code = si->si_code; 195 err = si->si_errno; 196 if ((err == 0) && (siginfo_layout(sig, code) == SIL_FAULT)) { 197 struct faultinfo *fi = UPT_FAULTINFO(regs); 198 current->thread.arch.faultinfo = *fi; 199 force_sig_fault(sig, code, (void __user *)FAULT_ADDRESS(*fi)); 200 } else { 201 printk(KERN_ERR "Attempted to relay unknown signal %d (si_code = %d) with errno %d\n", 202 sig, code, err); 203 force_sig(sig); 204 } 205 } 206 207 void winch(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs, 208 void *mc) 209 { 210 do_IRQ(WINCH_IRQ, regs); 211 } 212