1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * linux/arch/alpha/mm/fault.c 4 * 5 * Copyright (C) 1995 Linus Torvalds 6 */ 7 8 #include <linux/sched/signal.h> 9 #include <linux/kernel.h> 10 #include <linux/mm.h> 11 #include <linux/hugetlb.h> 12 #include <asm/io.h> 13 14 #define __EXTERN_INLINE inline 15 #include <asm/mmu_context.h> 16 #include <asm/tlbflush.h> 17 #undef __EXTERN_INLINE 18 19 #include <linux/signal.h> 20 #include <linux/errno.h> 21 #include <linux/string.h> 22 #include <linux/types.h> 23 #include <linux/ptrace.h> 24 #include <linux/mman.h> 25 #include <linux/smp.h> 26 #include <linux/interrupt.h> 27 #include <linux/extable.h> 28 #include <linux/uaccess.h> 29 #include <linux/perf_event.h> 30 31 #include <asm/pal.h> 32 33 extern void die_if_kernel(char *,struct pt_regs *,long, unsigned long *); 34 35 int show_unhandled_signals = 1; 36 37 static void 38 show_signal_msg(struct pt_regs *regs, unsigned long address, int signo, 39 const char *desc) 40 { 41 const char *level; 42 43 if (!show_unhandled_signals || !unhandled_signal(current, signo)) 44 return; 45 if (!printk_ratelimit()) 46 return; 47 48 /* Losing init is fatal, so shout about it. */ 49 level = task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG; 50 51 printk("%s%s[%d]: %s at %016lx pc %016lx sp %016lx\n", 52 level, current->comm, task_pid_nr(current), desc, 53 address, regs->pc, rdusp()); 54 print_vma_addr(KERN_CONT " in ", regs->pc); 55 printk(KERN_CONT "\n"); 56 } 57 58 /* 59 * Force a new ASN for a task. 60 */ 61 62 #ifndef CONFIG_SMP 63 unsigned long last_asn = ASN_FIRST_VERSION; 64 #endif 65 66 void 67 __load_new_mm_context(struct mm_struct *next_mm) 68 { 69 unsigned long mmc; 70 struct pcb_struct *pcb; 71 72 mmc = __get_new_mm_context(next_mm, smp_processor_id()); 73 WRITE_ONCE(next_mm->context[smp_processor_id()], mmc); 74 /* Publish the context before this CPU can access the mm. */ 75 smp_mb(); 76 77 pcb = ¤t_thread_info()->pcb; 78 pcb->asn = mmc & HARDWARE_ASN_MASK; 79 pcb->ptbr = ((unsigned long) next_mm->pgd - IDENT_ADDR) >> PAGE_SHIFT; 80 81 __reload_thread(pcb); 82 } 83 84 85 /* 86 * This routine handles page faults. It determines the address, 87 * and the problem, and then passes it off to handle_mm_fault(). 88 * 89 * mmcsr: 90 * 0 = translation not valid 91 * 1 = access violation 92 * 2 = fault-on-read 93 * 3 = fault-on-execute 94 * 4 = fault-on-write 95 * 96 * cause: 97 * -1 = instruction fetch 98 * 0 = load 99 * 1 = store 100 * 101 * Registers $9 through $15 are saved in a block just prior to `regs' and 102 * are saved and restored around the call to allow exception code to 103 * modify them. 104 */ 105 106 /* Macro for exception fixup code to access integer registers. */ 107 #define dpf_reg(r) \ 108 (((unsigned long *)regs)[(r) <= 8 ? (r) : (r) <= 15 ? (r)-17 : \ 109 (r) <= 18 ? (r)+11 : (r)-10]) 110 111 /* 112 * As of EV6, a load into $31/$f31 is a prefetch, and never faults 113 * (or is suppressed by the PALcode). Support that for older CPUs 114 * by ignoring such an instruction. 115 */ 116 static bool is_prefetch(struct pt_regs *regs) 117 { 118 unsigned int insn; 119 int err; 120 121 if (user_mode(regs)) 122 err = get_user(insn, (unsigned int __user *)regs->pc); 123 else 124 err = get_kernel_nofault(insn, (unsigned int *)regs->pc); 125 if (err) 126 return false; 127 128 /* ldq ldl ldt lds ldg ldf ldwu ldbu */ 129 return (insn >> 21 & 0x1f) == 0x1f && 130 (1ul << (insn >> 26) & 0x30f00001400ul); 131 } 132 133 asmlinkage void 134 do_page_fault(unsigned long address, unsigned long mmcsr, 135 long cause, struct pt_regs *regs) 136 { 137 struct vm_area_struct * vma; 138 struct mm_struct *mm = current->mm; 139 const struct exception_table_entry *fixup; 140 int si_code = SEGV_MAPERR; 141 unsigned int lsb; 142 vm_fault_t fault; 143 unsigned int flags = FAULT_FLAG_DEFAULT; 144 145 if (cause == 0 && is_prefetch(regs)) { 146 regs->pc += 4; 147 return; 148 } 149 150 /* If we're in an interrupt context, or have no user context, 151 we must not take the fault. */ 152 if (!mm || faulthandler_disabled()) 153 goto no_context; 154 155 #ifdef CONFIG_ALPHA_LARGE_VMALLOC 156 if (address >= TASK_SIZE) 157 goto vmalloc_fault; 158 #endif 159 if (user_mode(regs)) 160 flags |= FAULT_FLAG_USER; 161 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); 162 retry: 163 vma = lock_mm_and_find_vma(mm, address, regs); 164 if (!vma) 165 goto bad_area_nosemaphore; 166 167 /* Ok, we have a good vm_area for this memory access, so 168 we can handle it. */ 169 si_code = SEGV_ACCERR; 170 if (cause < 0) { 171 if (!(vma->vm_flags & VM_EXEC)) 172 goto bad_area; 173 } else if (!cause) { 174 /* Allow reads even for write-only mappings */ 175 if (!(vma->vm_flags & (VM_READ | VM_WRITE))) 176 goto bad_area; 177 } else { 178 if (!(vma->vm_flags & VM_WRITE)) 179 goto bad_area; 180 flags |= FAULT_FLAG_WRITE; 181 } 182 183 /* If for any reason at all we couldn't handle the fault, 184 make sure we exit gracefully rather than endlessly redo 185 the fault. */ 186 fault = handle_mm_fault(vma, address, flags, regs); 187 188 if (fault_signal_pending(fault, regs)) { 189 if (!user_mode(regs)) 190 goto no_context; 191 return; 192 } 193 194 /* The fault is fully completed (including releasing mmap lock) */ 195 if (fault & VM_FAULT_COMPLETED) 196 return; 197 198 if (unlikely(fault & VM_FAULT_ERROR)) { 199 if (fault & VM_FAULT_OOM) 200 goto out_of_memory; 201 else if (fault & VM_FAULT_SIGSEGV) 202 goto bad_area; 203 else if (fault & VM_FAULT_SIGBUS) 204 goto do_sigbus; 205 else if (fault & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE)) 206 goto do_sigbus_mceerr; 207 BUG(); 208 } 209 210 if (fault & VM_FAULT_RETRY) { 211 flags |= FAULT_FLAG_TRIED; 212 213 /* No need to mmap_read_unlock(mm) as we would 214 * have already released it in __lock_page_or_retry 215 * in mm/filemap.c. 216 */ 217 218 goto retry; 219 } 220 221 mmap_read_unlock(mm); 222 223 return; 224 225 /* Something tried to access memory that isn't in our memory map. 226 Fix it, but check if it's kernel or user first. */ 227 bad_area: 228 mmap_read_unlock(mm); 229 230 bad_area_nosemaphore: 231 if (user_mode(regs)) 232 goto do_sigsegv; 233 234 no_context: 235 /* Are we prepared to handle this fault as an exception? */ 236 if ((fixup = search_exception_tables(regs->pc)) != 0) { 237 unsigned long newpc; 238 newpc = fixup_exception(dpf_reg, fixup, regs->pc); 239 regs->pc = newpc; 240 return; 241 } 242 243 /* Oops. The kernel tried to access some bad page. We'll have to 244 terminate things with extreme prejudice. */ 245 printk(KERN_ALERT "Unable to handle kernel paging request at " 246 "virtual address %016lx\n", address); 247 die_if_kernel("Oops", regs, cause, (unsigned long*)regs - 16); 248 make_task_dead(SIGKILL); 249 250 /* We ran out of memory, or some other thing happened to us that 251 made us unable to handle the page fault gracefully. */ 252 out_of_memory: 253 mmap_read_unlock(mm); 254 if (!user_mode(regs)) 255 goto no_context; 256 pagefault_out_of_memory(); 257 return; 258 259 do_sigbus: 260 mmap_read_unlock(mm); 261 /* Send a sigbus, regardless of whether we were in kernel 262 or user mode. */ 263 if (user_mode(regs)) 264 show_signal_msg(regs, address, SIGBUS, "bus error"); 265 force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *) address); 266 if (!user_mode(regs)) 267 goto no_context; 268 return; 269 270 do_sigbus_mceerr: 271 mmap_read_unlock(mm); 272 if (!user_mode(regs)) 273 goto no_context; 274 /* 275 * Report the size of the poisoned area, which for a hugetlb fault 276 * is the size of the huge page that could not be mapped. 277 */ 278 lsb = PAGE_SHIFT; 279 if (fault & VM_FAULT_HWPOISON_LARGE) 280 lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault)); 281 show_signal_msg(regs, address, SIGBUS, "hardware memory error"); 282 force_sig_mceerr(BUS_MCEERR_AR, (void __user *) address, lsb); 283 return; 284 285 do_sigsegv: 286 show_signal_msg(regs, address, SIGSEGV, 287 si_code == SEGV_MAPERR ? "unmapped access" 288 : "access violation"); 289 force_sig_fault(SIGSEGV, si_code, (void __user *) address); 290 return; 291 292 #ifdef CONFIG_ALPHA_LARGE_VMALLOC 293 vmalloc_fault: 294 if (user_mode(regs)) 295 goto do_sigsegv; 296 else { 297 /* Synchronize this task's top level page-table 298 with the "reference" page table from init. */ 299 long index = pgd_index(address); 300 pgd_t *pgd, *pgd_k; 301 302 pgd = current->active_mm->pgd + index; 303 pgd_k = swapper_pg_dir + index; 304 if (!pgd_present(*pgd) && pgd_present(*pgd_k)) { 305 pgd_val(*pgd) = pgd_val(*pgd_k); 306 return; 307 } 308 goto no_context; 309 } 310 #endif 311 } 312