1 /* 2 * arch/s390/mm/fault.c 3 * 4 * S390 version 5 * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation 6 * Author(s): Hartmut Penner (hp@de.ibm.com) 7 * Ulrich Weigand (uweigand@de.ibm.com) 8 * 9 * Derived from "arch/i386/mm/fault.c" 10 * Copyright (C) 1995 Linus Torvalds 11 */ 12 13 #include <linux/signal.h> 14 #include <linux/sched.h> 15 #include <linux/kernel.h> 16 #include <linux/errno.h> 17 #include <linux/string.h> 18 #include <linux/types.h> 19 #include <linux/ptrace.h> 20 #include <linux/mman.h> 21 #include <linux/mm.h> 22 #include <linux/smp.h> 23 #include <linux/smp_lock.h> 24 #include <linux/init.h> 25 #include <linux/console.h> 26 #include <linux/module.h> 27 #include <linux/hardirq.h> 28 #include <linux/kprobes.h> 29 30 #include <asm/system.h> 31 #include <asm/uaccess.h> 32 #include <asm/pgtable.h> 33 #include <asm/kdebug.h> 34 #include <asm/s390_ext.h> 35 36 #ifndef CONFIG_64BIT 37 #define __FAIL_ADDR_MASK 0x7ffff000 38 #define __FIXUP_MASK 0x7fffffff 39 #define __SUBCODE_MASK 0x0200 40 #define __PF_RES_FIELD 0ULL 41 #else /* CONFIG_64BIT */ 42 #define __FAIL_ADDR_MASK -4096L 43 #define __FIXUP_MASK ~0L 44 #define __SUBCODE_MASK 0x0600 45 #define __PF_RES_FIELD 0x8000000000000000ULL 46 #endif /* CONFIG_64BIT */ 47 48 #ifdef CONFIG_SYSCTL 49 extern int sysctl_userprocess_debug; 50 #endif 51 52 extern void die(const char *,struct pt_regs *,long); 53 54 #ifdef CONFIG_KPROBES 55 static ATOMIC_NOTIFIER_HEAD(notify_page_fault_chain); 56 int register_page_fault_notifier(struct notifier_block *nb) 57 { 58 return atomic_notifier_chain_register(¬ify_page_fault_chain, nb); 59 } 60 61 int unregister_page_fault_notifier(struct notifier_block *nb) 62 { 63 return atomic_notifier_chain_unregister(¬ify_page_fault_chain, nb); 64 } 65 66 static inline int notify_page_fault(enum die_val val, const char *str, 67 struct pt_regs *regs, long err, int trap, int sig) 68 { 69 struct die_args args = { 70 .regs = regs, 71 .str = str, 72 .err = err, 73 .trapnr = trap, 74 .signr = sig 75 }; 76 return atomic_notifier_call_chain(¬ify_page_fault_chain, val, &args); 77 } 78 #else 79 static inline int notify_page_fault(enum die_val val, const char *str, 80 struct pt_regs *regs, long err, int trap, int sig) 81 { 82 return NOTIFY_DONE; 83 } 84 #endif 85 86 87 /* 88 * Unlock any spinlocks which will prevent us from getting the 89 * message out. 90 */ 91 void bust_spinlocks(int yes) 92 { 93 if (yes) { 94 oops_in_progress = 1; 95 } else { 96 int loglevel_save = console_loglevel; 97 console_unblank(); 98 oops_in_progress = 0; 99 /* 100 * OK, the message is on the console. Now we call printk() 101 * without oops_in_progress set so that printk will give klogd 102 * a poke. Hold onto your hats... 103 */ 104 console_loglevel = 15; 105 printk(" "); 106 console_loglevel = loglevel_save; 107 } 108 } 109 110 /* 111 * Returns the address space associated with the fault. 112 * Returns 0 for kernel space, 1 for user space and 113 * 2 for code execution in user space with noexec=on. 114 */ 115 static inline int check_space(struct task_struct *tsk) 116 { 117 /* 118 * The lowest two bits of S390_lowcore.trans_exc_code 119 * indicate which paging table was used. 120 */ 121 int desc = S390_lowcore.trans_exc_code & 3; 122 123 if (desc == 3) /* Home Segment Table Descriptor */ 124 return switch_amode == 0; 125 if (desc == 2) /* Secondary Segment Table Descriptor */ 126 return tsk->thread.mm_segment.ar4; 127 #ifdef CONFIG_S390_SWITCH_AMODE 128 if (unlikely(desc == 1)) { /* STD determined via access register */ 129 /* %a0 always indicates primary space. */ 130 if (S390_lowcore.exc_access_id != 0) { 131 save_access_regs(tsk->thread.acrs); 132 /* 133 * An alet of 0 indicates primary space. 134 * An alet of 1 indicates secondary space. 135 * Any other alet values generate an 136 * alen-translation exception. 137 */ 138 if (tsk->thread.acrs[S390_lowcore.exc_access_id]) 139 return tsk->thread.mm_segment.ar4; 140 } 141 } 142 #endif 143 /* Primary Segment Table Descriptor */ 144 return switch_amode << s390_noexec; 145 } 146 147 /* 148 * Send SIGSEGV to task. This is an external routine 149 * to keep the stack usage of do_page_fault small. 150 */ 151 static void do_sigsegv(struct pt_regs *regs, unsigned long error_code, 152 int si_code, unsigned long address) 153 { 154 struct siginfo si; 155 156 #if defined(CONFIG_SYSCTL) || defined(CONFIG_PROCESS_DEBUG) 157 #if defined(CONFIG_SYSCTL) 158 if (sysctl_userprocess_debug) 159 #endif 160 { 161 printk("User process fault: interruption code 0x%lX\n", 162 error_code); 163 printk("failing address: %lX\n", address); 164 show_regs(regs); 165 } 166 #endif 167 si.si_signo = SIGSEGV; 168 si.si_code = si_code; 169 si.si_addr = (void __user *) address; 170 force_sig_info(SIGSEGV, &si, current); 171 } 172 173 #ifdef CONFIG_S390_EXEC_PROTECT 174 extern long sys_sigreturn(struct pt_regs *regs); 175 extern long sys_rt_sigreturn(struct pt_regs *regs); 176 extern long sys32_sigreturn(struct pt_regs *regs); 177 extern long sys32_rt_sigreturn(struct pt_regs *regs); 178 179 static inline void do_sigreturn(struct mm_struct *mm, struct pt_regs *regs, 180 int rt) 181 { 182 up_read(&mm->mmap_sem); 183 clear_tsk_thread_flag(current, TIF_SINGLE_STEP); 184 #ifdef CONFIG_COMPAT 185 if (test_tsk_thread_flag(current, TIF_31BIT)) { 186 if (rt) 187 sys32_rt_sigreturn(regs); 188 else 189 sys32_sigreturn(regs); 190 return; 191 } 192 #endif /* CONFIG_COMPAT */ 193 if (rt) 194 sys_rt_sigreturn(regs); 195 else 196 sys_sigreturn(regs); 197 return; 198 } 199 200 static int signal_return(struct mm_struct *mm, struct pt_regs *regs, 201 unsigned long address, unsigned long error_code) 202 { 203 pgd_t *pgd; 204 pmd_t *pmd; 205 pte_t *pte; 206 u16 *instruction; 207 unsigned long pfn, uaddr = regs->psw.addr; 208 209 spin_lock(&mm->page_table_lock); 210 pgd = pgd_offset(mm, uaddr); 211 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 212 goto out_fault; 213 pmd = pmd_offset(pgd, uaddr); 214 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd))) 215 goto out_fault; 216 pte = pte_offset_map(pmd_offset(pgd_offset(mm, uaddr), uaddr), uaddr); 217 if (!pte || !pte_present(*pte)) 218 goto out_fault; 219 pfn = pte_pfn(*pte); 220 if (!pfn_valid(pfn)) 221 goto out_fault; 222 spin_unlock(&mm->page_table_lock); 223 224 instruction = (u16 *) ((pfn << PAGE_SHIFT) + (uaddr & (PAGE_SIZE-1))); 225 if (*instruction == 0x0a77) 226 do_sigreturn(mm, regs, 0); 227 else if (*instruction == 0x0aad) 228 do_sigreturn(mm, regs, 1); 229 else { 230 printk("- XXX - do_exception: task = %s, primary, NO EXEC " 231 "-> SIGSEGV\n", current->comm); 232 up_read(&mm->mmap_sem); 233 current->thread.prot_addr = address; 234 current->thread.trap_no = error_code; 235 do_sigsegv(regs, error_code, SEGV_MAPERR, address); 236 } 237 return 0; 238 out_fault: 239 spin_unlock(&mm->page_table_lock); 240 return -EFAULT; 241 } 242 #endif /* CONFIG_S390_EXEC_PROTECT */ 243 244 /* 245 * This routine handles page faults. It determines the address, 246 * and the problem, and then passes it off to one of the appropriate 247 * routines. 248 * 249 * error_code: 250 * 04 Protection -> Write-Protection (suprression) 251 * 10 Segment translation -> Not present (nullification) 252 * 11 Page translation -> Not present (nullification) 253 * 3b Region third trans. -> Not present (nullification) 254 */ 255 static inline void 256 do_exception(struct pt_regs *regs, unsigned long error_code, int is_protection) 257 { 258 struct task_struct *tsk; 259 struct mm_struct *mm; 260 struct vm_area_struct * vma; 261 unsigned long address; 262 const struct exception_table_entry *fixup; 263 int si_code; 264 int space; 265 266 tsk = current; 267 mm = tsk->mm; 268 269 if (notify_page_fault(DIE_PAGE_FAULT, "page fault", regs, error_code, 14, 270 SIGSEGV) == NOTIFY_STOP) 271 return; 272 273 /* 274 * Check for low-address protection. This needs to be treated 275 * as a special case because the translation exception code 276 * field is not guaranteed to contain valid data in this case. 277 */ 278 if (is_protection && !(S390_lowcore.trans_exc_code & 4)) { 279 280 /* Low-address protection hit in kernel mode means 281 NULL pointer write access in kernel mode. */ 282 if (!(regs->psw.mask & PSW_MASK_PSTATE)) { 283 address = 0; 284 space = 0; 285 goto no_context; 286 } 287 288 /* Low-address protection hit in user mode 'cannot happen'. */ 289 die ("Low-address protection", regs, error_code); 290 do_exit(SIGKILL); 291 } 292 293 /* 294 * get the failing address 295 * more specific the segment and page table portion of 296 * the address 297 */ 298 address = S390_lowcore.trans_exc_code & __FAIL_ADDR_MASK; 299 space = check_space(tsk); 300 301 /* 302 * Verify that the fault happened in user space, that 303 * we are not in an interrupt and that there is a 304 * user context. 305 */ 306 if (unlikely(space == 0 || in_atomic() || !mm)) 307 goto no_context; 308 309 /* 310 * When we get here, the fault happened in the current 311 * task's user address space, so we can switch on the 312 * interrupts again and then search the VMAs 313 */ 314 local_irq_enable(); 315 316 down_read(&mm->mmap_sem); 317 318 si_code = SEGV_MAPERR; 319 vma = find_vma(mm, address); 320 if (!vma) 321 goto bad_area; 322 323 #ifdef CONFIG_S390_EXEC_PROTECT 324 if (unlikely((space == 2) && !(vma->vm_flags & VM_EXEC))) 325 if (!signal_return(mm, regs, address, error_code)) 326 /* 327 * signal_return() has done an up_read(&mm->mmap_sem) 328 * if it returns 0. 329 */ 330 return; 331 #endif 332 333 if (vma->vm_start <= address) 334 goto good_area; 335 if (!(vma->vm_flags & VM_GROWSDOWN)) 336 goto bad_area; 337 if (expand_stack(vma, address)) 338 goto bad_area; 339 /* 340 * Ok, we have a good vm_area for this memory access, so 341 * we can handle it.. 342 */ 343 good_area: 344 si_code = SEGV_ACCERR; 345 if (!is_protection) { 346 /* page not present, check vm flags */ 347 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))) 348 goto bad_area; 349 } else { 350 if (!(vma->vm_flags & VM_WRITE)) 351 goto bad_area; 352 } 353 354 survive: 355 /* 356 * If for any reason at all we couldn't handle the fault, 357 * make sure we exit gracefully rather than endlessly redo 358 * the fault. 359 */ 360 switch (handle_mm_fault(mm, vma, address, is_protection)) { 361 case VM_FAULT_MINOR: 362 tsk->min_flt++; 363 break; 364 case VM_FAULT_MAJOR: 365 tsk->maj_flt++; 366 break; 367 case VM_FAULT_SIGBUS: 368 goto do_sigbus; 369 case VM_FAULT_OOM: 370 goto out_of_memory; 371 default: 372 BUG(); 373 } 374 375 up_read(&mm->mmap_sem); 376 /* 377 * The instruction that caused the program check will 378 * be repeated. Don't signal single step via SIGTRAP. 379 */ 380 clear_tsk_thread_flag(tsk, TIF_SINGLE_STEP); 381 return; 382 383 /* 384 * Something tried to access memory that isn't in our memory map.. 385 * Fix it, but check if it's kernel or user first.. 386 */ 387 bad_area: 388 up_read(&mm->mmap_sem); 389 390 /* User mode accesses just cause a SIGSEGV */ 391 if (regs->psw.mask & PSW_MASK_PSTATE) { 392 tsk->thread.prot_addr = address; 393 tsk->thread.trap_no = error_code; 394 do_sigsegv(regs, error_code, si_code, address); 395 return; 396 } 397 398 no_context: 399 /* Are we prepared to handle this kernel fault? */ 400 fixup = search_exception_tables(regs->psw.addr & __FIXUP_MASK); 401 if (fixup) { 402 regs->psw.addr = fixup->fixup | PSW_ADDR_AMODE; 403 return; 404 } 405 406 /* 407 * Oops. The kernel tried to access some bad page. We'll have to 408 * terminate things with extreme prejudice. 409 */ 410 if (space == 0) 411 printk(KERN_ALERT "Unable to handle kernel pointer dereference" 412 " at virtual kernel address %p\n", (void *)address); 413 else 414 printk(KERN_ALERT "Unable to handle kernel paging request" 415 " at virtual user address %p\n", (void *)address); 416 417 die("Oops", regs, error_code); 418 do_exit(SIGKILL); 419 420 421 /* 422 * We ran out of memory, or some other thing happened to us that made 423 * us unable to handle the page fault gracefully. 424 */ 425 out_of_memory: 426 up_read(&mm->mmap_sem); 427 if (is_init(tsk)) { 428 yield(); 429 down_read(&mm->mmap_sem); 430 goto survive; 431 } 432 printk("VM: killing process %s\n", tsk->comm); 433 if (regs->psw.mask & PSW_MASK_PSTATE) 434 do_exit(SIGKILL); 435 goto no_context; 436 437 do_sigbus: 438 up_read(&mm->mmap_sem); 439 440 /* 441 * Send a sigbus, regardless of whether we were in kernel 442 * or user mode. 443 */ 444 tsk->thread.prot_addr = address; 445 tsk->thread.trap_no = error_code; 446 force_sig(SIGBUS, tsk); 447 448 /* Kernel mode? Handle exceptions or die */ 449 if (!(regs->psw.mask & PSW_MASK_PSTATE)) 450 goto no_context; 451 } 452 453 void __kprobes do_protection_exception(struct pt_regs *regs, 454 unsigned long error_code) 455 { 456 regs->psw.addr -= (error_code >> 16); 457 do_exception(regs, 4, 1); 458 } 459 460 void __kprobes do_dat_exception(struct pt_regs *regs, unsigned long error_code) 461 { 462 do_exception(regs, error_code & 0xff, 0); 463 } 464 465 #ifdef CONFIG_PFAULT 466 /* 467 * 'pfault' pseudo page faults routines. 468 */ 469 static ext_int_info_t ext_int_pfault; 470 static int pfault_disable = 0; 471 472 static int __init nopfault(char *str) 473 { 474 pfault_disable = 1; 475 return 1; 476 } 477 478 __setup("nopfault", nopfault); 479 480 typedef struct { 481 __u16 refdiagc; 482 __u16 reffcode; 483 __u16 refdwlen; 484 __u16 refversn; 485 __u64 refgaddr; 486 __u64 refselmk; 487 __u64 refcmpmk; 488 __u64 reserved; 489 } __attribute__ ((packed)) pfault_refbk_t; 490 491 int pfault_init(void) 492 { 493 pfault_refbk_t refbk = 494 { 0x258, 0, 5, 2, __LC_CURRENT, 1ULL << 48, 1ULL << 48, 495 __PF_RES_FIELD }; 496 int rc; 497 498 if (!MACHINE_IS_VM || pfault_disable) 499 return -1; 500 asm volatile( 501 " diag %1,%0,0x258\n" 502 "0: j 2f\n" 503 "1: la %0,8\n" 504 "2:\n" 505 EX_TABLE(0b,1b) 506 : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc"); 507 __ctl_set_bit(0, 9); 508 return rc; 509 } 510 511 void pfault_fini(void) 512 { 513 pfault_refbk_t refbk = 514 { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL }; 515 516 if (!MACHINE_IS_VM || pfault_disable) 517 return; 518 __ctl_clear_bit(0,9); 519 asm volatile( 520 " diag %0,0,0x258\n" 521 "0:\n" 522 EX_TABLE(0b,0b) 523 : : "a" (&refbk), "m" (refbk) : "cc"); 524 } 525 526 static void pfault_interrupt(__u16 error_code) 527 { 528 struct task_struct *tsk; 529 __u16 subcode; 530 531 /* 532 * Get the external interruption subcode & pfault 533 * initial/completion signal bit. VM stores this 534 * in the 'cpu address' field associated with the 535 * external interrupt. 536 */ 537 subcode = S390_lowcore.cpu_addr; 538 if ((subcode & 0xff00) != __SUBCODE_MASK) 539 return; 540 541 /* 542 * Get the token (= address of the task structure of the affected task). 543 */ 544 tsk = *(struct task_struct **) __LC_PFAULT_INTPARM; 545 546 if (subcode & 0x0080) { 547 /* signal bit is set -> a page has been swapped in by VM */ 548 if (xchg(&tsk->thread.pfault_wait, -1) != 0) { 549 /* Initial interrupt was faster than the completion 550 * interrupt. pfault_wait is valid. Set pfault_wait 551 * back to zero and wake up the process. This can 552 * safely be done because the task is still sleeping 553 * and can't produce new pfaults. */ 554 tsk->thread.pfault_wait = 0; 555 wake_up_process(tsk); 556 put_task_struct(tsk); 557 } 558 } else { 559 /* signal bit not set -> a real page is missing. */ 560 get_task_struct(tsk); 561 set_task_state(tsk, TASK_UNINTERRUPTIBLE); 562 if (xchg(&tsk->thread.pfault_wait, 1) != 0) { 563 /* Completion interrupt was faster than the initial 564 * interrupt (swapped in a -1 for pfault_wait). Set 565 * pfault_wait back to zero and exit. This can be 566 * done safely because tsk is running in kernel 567 * mode and can't produce new pfaults. */ 568 tsk->thread.pfault_wait = 0; 569 set_task_state(tsk, TASK_RUNNING); 570 put_task_struct(tsk); 571 } else 572 set_tsk_need_resched(tsk); 573 } 574 } 575 576 void __init pfault_irq_init(void) 577 { 578 if (!MACHINE_IS_VM) 579 return; 580 581 /* 582 * Try to get pfault pseudo page faults going. 583 */ 584 if (register_early_external_interrupt(0x2603, pfault_interrupt, 585 &ext_int_pfault) != 0) 586 panic("Couldn't request external interrupt 0x2603"); 587 588 if (pfault_init() == 0) 589 return; 590 591 /* Tough luck, no pfault. */ 592 pfault_disable = 1; 593 unregister_early_external_interrupt(0x2603, pfault_interrupt, 594 &ext_int_pfault); 595 } 596 #endif 597