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/config.h> 14#include <linux/signal.h> 15#include <linux/sched.h> 16#include <linux/kernel.h> 17#include <linux/errno.h> 18#include <linux/string.h> 19#include <linux/types.h> 20#include <linux/ptrace.h> 21#include <linux/mman.h> 22#include <linux/mm.h> 23#include <linux/smp.h> 24#include <linux/smp_lock.h> 25#include <linux/init.h> 26#include <linux/console.h> 27 28#include <asm/system.h> 29#include <asm/uaccess.h> 30#include <asm/pgtable.h> 31#include <asm/hardirq.h> 32 33#ifdef CONFIG_SYSCTL 34extern int sysctl_userprocess_debug; 35#endif 36 37extern void die(const char *,struct pt_regs *,long); 38 39extern spinlock_t timerlist_lock; 40 41/* 42 * Unlock any spinlocks which will prevent us from getting the 43 * message out (timerlist_lock is acquired through the 44 * console unblank code) 45 */ 46void bust_spinlocks(int yes) 47{ 48 spin_lock_init(&timerlist_lock); 49 if (yes) { 50 oops_in_progress = 1; 51 } else { 52 int loglevel_save = console_loglevel; 53 oops_in_progress = 0; 54 console_unblank(); 55 /* 56 * OK, the message is on the console. Now we call printk() 57 * without oops_in_progress set so that printk will give klogd 58 * a poke. Hold onto your hats... 59 */ 60 console_loglevel = 15; 61 printk(" "); 62 console_loglevel = loglevel_save; 63 } 64} 65 66/* 67 * Check which address space is addressed by the access 68 * register in S390_lowcore.exc_access_id. 69 * Returns 1 for user space and 0 for kernel space. 70 */ 71static int __check_access_register(struct pt_regs *regs, int error_code) 72{ 73 int areg = S390_lowcore.exc_access_id; 74 75 if (areg == 0) 76 /* Access via access register 0 -> kernel address */ 77 return 0; 78 if (regs && areg < NUM_ACRS && regs->acrs[areg] <= 1) 79 /* 80 * access register contains 0 -> kernel address, 81 * access register contains 1 -> user space address 82 */ 83 return regs->acrs[areg]; 84 85 /* Something unhealthy was done with the access registers... */ 86 die("page fault via unknown access register", regs, error_code); 87 do_exit(SIGKILL); 88 return 0; 89} 90 91/* 92 * Check which address space the address belongs to. 93 * Returns 1 for user space and 0 for kernel space. 94 */ 95static inline int check_user_space(struct pt_regs *regs, int error_code) 96{ 97 /* 98 * The lowest two bits of S390_lowcore.trans_exc_code indicate 99 * which paging table was used: 100 * 0: Primary Segment Table Descriptor 101 * 1: STD determined via access register 102 * 2: Secondary Segment Table Descriptor 103 * 3: Home Segment Table Descriptor 104 */ 105 int descriptor = S390_lowcore.trans_exc_code & 3; 106 if (descriptor == 1) 107 return __check_access_register(regs, error_code); 108 return descriptor >> 1; 109} 110 111/* 112 * Send SIGSEGV to task. This is an external routine 113 * to keep the stack usage of do_page_fault small. 114 */ 115static void force_sigsegv(struct pt_regs *regs, unsigned long error_code, 116 int si_code, unsigned long address) 117{ 118 struct siginfo si; 119 120#if defined(CONFIG_SYSCTL) || defined(CONFIG_PROCESS_DEBUG) 121#if defined(CONFIG_SYSCTL) 122 if (sysctl_userprocess_debug) 123#endif 124 { 125 printk("User process fault: interruption code 0x%lX\n", 126 error_code); 127 printk("failing address: %lX\n", address); 128 show_regs(regs); 129 } 130#endif 131 si.si_signo = SIGSEGV; 132 si.si_code = si_code; 133 si.si_addr = (void *) address; 134 force_sig_info(SIGSEGV, &si, current); 135} 136 137/* 138 * This routine handles page faults. It determines the address, 139 * and the problem, and then passes it off to one of the appropriate 140 * routines. 141 * 142 * error_code: 143 * 04 Protection -> Write-Protection (suprression) 144 * 10 Segment translation -> Not present (nullification) 145 * 11 Page translation -> Not present (nullification) 146 * 3b Region third trans. -> Not present (nullification) 147 */ 148extern inline void do_exception(struct pt_regs *regs, unsigned long error_code) 149{ 150 struct task_struct *tsk; 151 struct mm_struct *mm; 152 struct vm_area_struct * vma; 153 unsigned long address; 154 int user_address; 155 unsigned long fixup; 156 int si_code = SEGV_MAPERR; 157 158 tsk = current; 159 mm = tsk->mm; 160 161 /* 162 * Check for low-address protection. This needs to be treated 163 * as a special case because the translation exception code 164 * field is not guaranteed to contain valid data in this case. 165 */ 166 if (error_code == 4 && !(S390_lowcore.trans_exc_code & 4)) { 167 168 /* Low-address protection hit in kernel mode means 169 NULL pointer write access in kernel mode. */ 170 if (!(regs->psw.mask & PSW_PROBLEM_STATE)) { 171 address = 0; 172 user_address = 0; 173 goto no_context; 174 } 175 176 /* Low-address protection hit in user mode 'cannot happen'. */ 177 die ("Low-address protection", regs, error_code); 178 do_exit(SIGKILL); 179 } 180 181 /* 182 * get the failing address 183 * more specific the segment and page table portion of 184 * the address 185 */ 186 address = S390_lowcore.trans_exc_code & -4096L; 187 user_address = check_user_space(regs, error_code); 188 189 /* 190 * Verify that the fault happened in user space, that 191 * we are not in an interrupt and that there is a 192 * user context. 193 */ 194 if (user_address == 0 || in_interrupt() || !mm) 195 goto no_context; 196 197 /* 198 * When we get here, the fault happened in the current 199 * task's user address space, so we can switch on the 200 * interrupts again and then search the VMAs 201 */ 202 __sti(); 203 204 down_read(&mm->mmap_sem); 205 206 vma = find_vma(mm, address); 207 if (!vma) 208 goto bad_area; 209 if (vma->vm_start <= address) 210 goto good_area; 211 if (!(vma->vm_flags & VM_GROWSDOWN)) 212 goto bad_area; 213 if (expand_stack(vma, address)) 214 goto bad_area; 215/* 216 * Ok, we have a good vm_area for this memory access, so 217 * we can handle it.. 218 */ 219good_area: 220 si_code = SEGV_ACCERR; 221 if (error_code != 4) { 222 /* page not present, check vm flags */ 223 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))) 224 goto bad_area; 225 } else { 226 if (!(vma->vm_flags & VM_WRITE)) 227 goto bad_area; 228 } 229 230survive: 231 /* 232 * If for any reason at all we couldn't handle the fault, 233 * make sure we exit gracefully rather than endlessly redo 234 * the fault. 235 */ 236 switch (handle_mm_fault(mm, vma, address, error_code == 4)) { 237 case 1: 238 tsk->min_flt++; 239 break; 240 case 2: 241 tsk->maj_flt++; 242 break; 243 case 0: 244 goto do_sigbus; 245 default: 246 goto out_of_memory; 247 } 248 249 up_read(&mm->mmap_sem); 250 return; 251 252/* 253 * Something tried to access memory that isn't in our memory map.. 254 * Fix it, but check if it's kernel or user first.. 255 */ 256bad_area: 257 up_read(&mm->mmap_sem); 258 259 /* User mode accesses just cause a SIGSEGV */ 260 if (regs->psw.mask & PSW_PROBLEM_STATE) { 261 tsk->thread.prot_addr = address; 262 tsk->thread.trap_no = error_code; 263 force_sigsegv(regs, error_code, si_code, address); 264 return; 265 } 266 267no_context: 268 /* Are we prepared to handle this kernel fault? */ 269 if ((fixup = search_exception_table(regs->psw.addr)) != 0) { 270 regs->psw.addr = fixup; 271 return; 272 } 273 274/* 275 * Oops. The kernel tried to access some bad page. We'll have to 276 * terminate things with extreme prejudice. 277 */ 278 if (user_address == 0) 279 printk(KERN_ALERT "Unable to handle kernel pointer dereference" 280 " at virtual kernel address %016lx\n", address); 281 else 282 printk(KERN_ALERT "Unable to handle kernel paging request" 283 " at virtual user address %016lx\n", address); 284 285 die("Oops", regs, error_code); 286 do_exit(SIGKILL); 287 288 289/* 290 * We ran out of memory, or some other thing happened to us that made 291 * us unable to handle the page fault gracefully. 292*/ 293out_of_memory: 294 if (tsk->pid == 1) { 295 yield(); 296 goto survive; 297 } 298 up_read(&mm->mmap_sem); 299 printk("VM: killing process %s\n", tsk->comm); 300 if (regs->psw.mask & PSW_PROBLEM_STATE) 301 do_exit(SIGKILL); 302 goto no_context; 303 304do_sigbus: 305 up_read(&mm->mmap_sem); 306 307 /* 308 * Send a sigbus, regardless of whether we were in kernel 309 * or user mode. 310 */ 311 tsk->thread.prot_addr = address; 312 tsk->thread.trap_no = error_code; 313 force_sig(SIGBUS, tsk); 314 315 /* Kernel mode? Handle exceptions or die */ 316 if (!(regs->psw.mask & PSW_PROBLEM_STATE)) 317 goto no_context; 318} 319 320void do_protection_exception(struct pt_regs *regs, unsigned long error_code) 321{ 322 regs->psw.addr -= (error_code >> 16); 323 do_exception(regs, 4); 324} 325 326void do_segment_exception(struct pt_regs *regs, unsigned long error_code) 327{ 328 do_exception(regs, 0x10); 329} 330 331void do_page_exception(struct pt_regs *regs, unsigned long error_code) 332{ 333 do_exception(regs, 0x11); 334} 335 336void do_region_exception(struct pt_regs *regs, unsigned long error_code) 337{ 338 do_exception(regs, 0x3b); 339} 340 341#ifdef CONFIG_PFAULT 342/* 343 * 'pfault' pseudo page faults routines. 344 */ 345static int pfault_disable = 0; 346 347static int __init nopfault(char *str) 348{ 349 pfault_disable = 1; 350 return 1; 351} 352 353__setup("nopfault", nopfault); 354 355typedef struct { 356 __u16 refdiagc; 357 __u16 reffcode; 358 __u16 refdwlen; 359 __u16 refversn; 360 __u64 refgaddr; 361 __u64 refselmk; 362 __u64 refcmpmk; 363 __u64 reserved; 364} __attribute__ ((packed)) pfault_refbk_t; 365 366typedef struct _pseudo_wait_t { 367 struct _pseudo_wait_t *next; 368 wait_queue_head_t queue; 369 unsigned long address; 370 int resolved; 371} pseudo_wait_t; 372 373int pfault_init(void) 374{ 375 pfault_refbk_t refbk = 376 { 0x258, 0, 5, 2, __LC_KERNEL_STACK, 1ULL << 48, 1ULL << 48, 377 0x8000000000000000ULL }; 378 int rc; 379 380 if (pfault_disable) 381 return -1; 382 __asm__ __volatile__( 383 " diag %1,%0,0x258\n" 384 "0: j 2f\n" 385 "1: la %0,8\n" 386 "2:\n" 387 ".section __ex_table,\"a\"\n" 388 " .align 4\n" 389 " .quad 0b,1b\n" 390 ".previous" 391 : "=d" (rc) : "a" (&refbk) : "cc" ); 392 __ctl_set_bit(0, 9); 393 return rc; 394} 395 396void pfault_fini(void) 397{ 398 pfault_refbk_t refbk = 399 { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL }; 400 401 if (pfault_disable) 402 return; 403 __ctl_clear_bit(0, 9); 404 __asm__ __volatile__( 405 " diag %0,0,0x258\n" 406 "0:\n" 407 ".section __ex_table,\"a\"\n" 408 " .align 4\n" 409 " .quad 0b,0b\n" 410 ".previous" 411 : : "a" (&refbk) : "cc" ); 412} 413 414asmlinkage void 415pfault_interrupt(struct pt_regs *regs, __u16 error_code) 416{ 417 struct task_struct *tsk; 418 wait_queue_head_t queue; 419 wait_queue_head_t *qp; 420 __u16 subcode; 421 422 /* 423 * Get the external interruption subcode & pfault 424 * initial/completion signal bit. VM stores this 425 * in the 'cpu address' field associated with the 426 * external interrupt. 427 */ 428 subcode = S390_lowcore.cpu_addr; 429 if ((subcode & 0xff00) != 0x0600) 430 return; 431 432 /* 433 * Get the token (= address of kernel stack of affected task). 434 */ 435 tsk = (struct task_struct *) 436 (*((unsigned long *) __LC_PFAULT_INTPARM) - THREAD_SIZE); 437 438 /* 439 * We got all needed information from the lowcore and can 440 * now safely switch on interrupts. 441 */ 442 if (regs->psw.mask & PSW_PROBLEM_STATE) 443 __sti(); 444 445 if (subcode & 0x0080) { 446 /* signal bit is set -> a page has been swapped in by VM */ 447 qp = (wait_queue_head_t *) 448 xchg(&tsk->thread.pfault_wait, -1); 449 if (qp != NULL) { 450 /* Initial interrupt was faster than the completion 451 * interrupt. pfault_wait is valid. Set pfault_wait 452 * back to zero and wake up the process. This can 453 * safely be done because the task is still sleeping 454 * and can't procude new pfaults. */ 455 tsk->thread.pfault_wait = 0ULL; 456 wake_up(qp); 457 } 458 } else { 459 /* signal bit not set -> a real page is missing. */ 460 init_waitqueue_head (&queue); 461 qp = (wait_queue_head_t *) 462 xchg(&tsk->thread.pfault_wait, (addr_t) &queue); 463 if (qp != NULL) { 464 /* Completion interrupt was faster than the initial 465 * interrupt (swapped in a -1 for pfault_wait). Set 466 * pfault_wait back to zero and exit. This can be 467 * done safely because tsk is running in kernel 468 * mode and can't produce new pfaults. */ 469 tsk->thread.pfault_wait = 0ULL; 470 } 471 472 /* go to sleep */ 473 wait_event(queue, tsk->thread.pfault_wait == 0ULL); 474 } 475} 476#endif 477 478