Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 13 | #include <linux/perf_event.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Heiko Carstens | 7757591 | 2009-06-12 10:26:25 +0200 | [diff] [blame] | 23 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/smp.h> |
Christoph Hellwig | 1eeb66a | 2007-05-08 00:27:03 -0700 | [diff] [blame] | 25 | #include <linux/kdebug.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/init.h> |
| 27 | #include <linux/console.h> |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/hardirq.h> |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 30 | #include <linux/kprobes.h> |
Martin Schwidefsky | be5ec36 | 2007-04-27 16:01:44 +0200 | [diff] [blame] | 31 | #include <linux/uaccess.h> |
Gerald Schaefer | 53492b1 | 2008-04-30 13:38:46 +0200 | [diff] [blame] | 32 | #include <linux/hugetlb.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <asm/system.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <asm/pgtable.h> |
Heiko Carstens | 29b08d2 | 2006-12-04 15:40:40 +0100 | [diff] [blame] | 35 | #include <asm/s390_ext.h> |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 36 | #include <asm/mmu_context.h> |
Heiko Carstens | a806170 | 2008-04-17 07:46:26 +0200 | [diff] [blame] | 37 | #include "../kernel/entry.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Martin Schwidefsky | 347a8dc | 2006-01-06 00:19:28 -0800 | [diff] [blame] | 39 | #ifndef CONFIG_64BIT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #define __FAIL_ADDR_MASK 0x7ffff000 |
| 41 | #define __FIXUP_MASK 0x7fffffff |
| 42 | #define __SUBCODE_MASK 0x0200 |
| 43 | #define __PF_RES_FIELD 0ULL |
Martin Schwidefsky | 347a8dc | 2006-01-06 00:19:28 -0800 | [diff] [blame] | 44 | #else /* CONFIG_64BIT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #define __FAIL_ADDR_MASK -4096L |
| 46 | #define __FIXUP_MASK ~0L |
| 47 | #define __SUBCODE_MASK 0x0600 |
| 48 | #define __PF_RES_FIELD 0x8000000000000000ULL |
Martin Schwidefsky | 347a8dc | 2006-01-06 00:19:28 -0800 | [diff] [blame] | 49 | #endif /* CONFIG_64BIT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | #ifdef CONFIG_SYSCTL |
| 52 | extern int sysctl_userprocess_debug; |
| 53 | #endif |
| 54 | |
Martin Schwidefsky | 7ecb344 | 2009-12-07 12:51:44 +0100 | [diff] [blame^] | 55 | static inline int notify_page_fault(struct pt_regs *regs) |
Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 56 | { |
Christoph Hellwig | 33464e3 | 2007-05-04 18:47:46 +0200 | [diff] [blame] | 57 | int ret = 0; |
| 58 | |
Martin Schwidefsky | 7ecb344 | 2009-12-07 12:51:44 +0100 | [diff] [blame^] | 59 | #ifdef CONFIG_KPROBES |
Christoph Hellwig | 33464e3 | 2007-05-04 18:47:46 +0200 | [diff] [blame] | 60 | /* kprobe_running() needs smp_processor_id() */ |
| 61 | if (!user_mode(regs)) { |
| 62 | preempt_disable(); |
| 63 | if (kprobe_running() && kprobe_fault_handler(regs, 14)) |
| 64 | ret = 1; |
| 65 | preempt_enable(); |
| 66 | } |
Martin Schwidefsky | 7ecb344 | 2009-12-07 12:51:44 +0100 | [diff] [blame^] | 67 | #endif |
Christoph Hellwig | 33464e3 | 2007-05-04 18:47:46 +0200 | [diff] [blame] | 68 | return ret; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 69 | } |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 70 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | /* |
| 73 | * Unlock any spinlocks which will prevent us from getting the |
Kirill Korotaev | cefc8be | 2007-02-10 01:46:18 -0800 | [diff] [blame] | 74 | * message out. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | */ |
| 76 | void bust_spinlocks(int yes) |
| 77 | { |
| 78 | if (yes) { |
| 79 | oops_in_progress = 1; |
| 80 | } else { |
| 81 | int loglevel_save = console_loglevel; |
| 82 | console_unblank(); |
| 83 | oops_in_progress = 0; |
| 84 | /* |
| 85 | * OK, the message is on the console. Now we call printk() |
| 86 | * without oops_in_progress set so that printk will give klogd |
| 87 | * a poke. Hold onto your hats... |
| 88 | */ |
| 89 | console_loglevel = 15; |
| 90 | printk(" "); |
| 91 | console_loglevel = loglevel_save; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /* |
Gerald Schaefer | 482b05d | 2007-03-05 23:35:54 +0100 | [diff] [blame] | 96 | * Returns the address space associated with the fault. |
Martin Schwidefsky | 61365e1 | 2009-12-07 12:51:42 +0100 | [diff] [blame] | 97 | * Returns 0 for kernel space and 1 for user space. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | */ |
Martin Schwidefsky | 61365e1 | 2009-12-07 12:51:42 +0100 | [diff] [blame] | 99 | static inline int user_space_fault(unsigned long trans_exc_code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
| 101 | /* |
Martin Schwidefsky | 61365e1 | 2009-12-07 12:51:42 +0100 | [diff] [blame] | 102 | * The lowest two bits of the translation exception |
| 103 | * identification indicate which paging table was used. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | */ |
Martin Schwidefsky | 61365e1 | 2009-12-07 12:51:42 +0100 | [diff] [blame] | 105 | trans_exc_code &= 3; |
| 106 | if (trans_exc_code == 2) |
| 107 | /* Access via secondary space, set_fs setting decides */ |
| 108 | return current->thread.mm_segment.ar4; |
Martin Schwidefsky | b11b533 | 2009-12-07 12:51:43 +0100 | [diff] [blame] | 109 | if (user_mode == HOME_SPACE_MODE) |
Martin Schwidefsky | 61365e1 | 2009-12-07 12:51:42 +0100 | [diff] [blame] | 110 | /* User space if the access has been done via home space. */ |
| 111 | return trans_exc_code == 3; |
| 112 | /* |
| 113 | * If the user space is not the home space the kernel runs in home |
| 114 | * space. Access via secondary space has already been covered, |
| 115 | * access via primary space or access register is from user space |
| 116 | * and access via home space is from the kernel. |
| 117 | */ |
| 118 | return trans_exc_code != 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /* |
| 122 | * Send SIGSEGV to task. This is an external routine |
| 123 | * to keep the stack usage of do_page_fault small. |
| 124 | */ |
| 125 | static void do_sigsegv(struct pt_regs *regs, unsigned long error_code, |
| 126 | int si_code, unsigned long address) |
| 127 | { |
| 128 | struct siginfo si; |
| 129 | |
| 130 | #if defined(CONFIG_SYSCTL) || defined(CONFIG_PROCESS_DEBUG) |
| 131 | #if defined(CONFIG_SYSCTL) |
| 132 | if (sysctl_userprocess_debug) |
| 133 | #endif |
| 134 | { |
| 135 | printk("User process fault: interruption code 0x%lX\n", |
| 136 | error_code); |
| 137 | printk("failing address: %lX\n", address); |
| 138 | show_regs(regs); |
| 139 | } |
| 140 | #endif |
| 141 | si.si_signo = SIGSEGV; |
| 142 | si.si_code = si_code; |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 143 | si.si_addr = (void __user *) address; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | force_sig_info(SIGSEGV, &si, current); |
| 145 | } |
| 146 | |
Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 147 | static void do_no_context(struct pt_regs *regs, unsigned long error_code, |
Martin Schwidefsky | 61365e1 | 2009-12-07 12:51:42 +0100 | [diff] [blame] | 148 | unsigned long trans_exc_code) |
Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 149 | { |
| 150 | const struct exception_table_entry *fixup; |
Martin Schwidefsky | 61365e1 | 2009-12-07 12:51:42 +0100 | [diff] [blame] | 151 | unsigned long address; |
Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 152 | |
| 153 | /* Are we prepared to handle this kernel fault? */ |
| 154 | fixup = search_exception_tables(regs->psw.addr & __FIXUP_MASK); |
| 155 | if (fixup) { |
| 156 | regs->psw.addr = fixup->fixup | PSW_ADDR_AMODE; |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Oops. The kernel tried to access some bad page. We'll have to |
| 162 | * terminate things with extreme prejudice. |
| 163 | */ |
Martin Schwidefsky | 61365e1 | 2009-12-07 12:51:42 +0100 | [diff] [blame] | 164 | address = trans_exc_code & __FAIL_ADDR_MASK; |
Martin Schwidefsky | b11b533 | 2009-12-07 12:51:43 +0100 | [diff] [blame] | 165 | if (!user_space_fault(trans_exc_code)) |
Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 166 | printk(KERN_ALERT "Unable to handle kernel pointer dereference" |
| 167 | " at virtual kernel address %p\n", (void *)address); |
| 168 | else |
| 169 | printk(KERN_ALERT "Unable to handle kernel paging request" |
| 170 | " at virtual user address %p\n", (void *)address); |
| 171 | |
| 172 | die("Oops", regs, error_code); |
| 173 | do_exit(SIGKILL); |
| 174 | } |
| 175 | |
Martin Schwidefsky | 61365e1 | 2009-12-07 12:51:42 +0100 | [diff] [blame] | 176 | static void do_low_address(struct pt_regs *regs, unsigned long error_code, |
| 177 | unsigned long trans_exc_code) |
Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 178 | { |
| 179 | /* Low-address protection hit in kernel mode means |
| 180 | NULL pointer write access in kernel mode. */ |
| 181 | if (regs->psw.mask & PSW_MASK_PSTATE) { |
| 182 | /* Low-address protection hit in user mode 'cannot happen'. */ |
| 183 | die ("Low-address protection", regs, error_code); |
| 184 | do_exit(SIGKILL); |
| 185 | } |
| 186 | |
Martin Schwidefsky | 61365e1 | 2009-12-07 12:51:42 +0100 | [diff] [blame] | 187 | do_no_context(regs, error_code, trans_exc_code); |
Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 188 | } |
| 189 | |
Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 190 | static void do_sigbus(struct pt_regs *regs, unsigned long error_code, |
Martin Schwidefsky | 61365e1 | 2009-12-07 12:51:42 +0100 | [diff] [blame] | 191 | unsigned long trans_exc_code) |
Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 192 | { |
| 193 | struct task_struct *tsk = current; |
| 194 | struct mm_struct *mm = tsk->mm; |
| 195 | |
| 196 | up_read(&mm->mmap_sem); |
| 197 | /* |
| 198 | * Send a sigbus, regardless of whether we were in kernel |
| 199 | * or user mode. |
| 200 | */ |
Martin Schwidefsky | 61365e1 | 2009-12-07 12:51:42 +0100 | [diff] [blame] | 201 | tsk->thread.prot_addr = trans_exc_code & __FAIL_ADDR_MASK; |
Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 202 | tsk->thread.trap_no = error_code; |
| 203 | force_sig(SIGBUS, tsk); |
| 204 | |
| 205 | /* Kernel mode? Handle exceptions or die */ |
| 206 | if (!(regs->psw.mask & PSW_MASK_PSTATE)) |
Martin Schwidefsky | 61365e1 | 2009-12-07 12:51:42 +0100 | [diff] [blame] | 207 | do_no_context(regs, error_code, trans_exc_code); |
Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 210 | #ifdef CONFIG_S390_EXEC_PROTECT |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 211 | static int signal_return(struct mm_struct *mm, struct pt_regs *regs, |
| 212 | unsigned long address, unsigned long error_code) |
| 213 | { |
Martin Schwidefsky | be5ec36 | 2007-04-27 16:01:44 +0200 | [diff] [blame] | 214 | u16 instruction; |
Heiko Carstens | 490f03d | 2007-05-10 15:45:48 +0200 | [diff] [blame] | 215 | int rc; |
| 216 | #ifdef CONFIG_COMPAT |
| 217 | int compat; |
| 218 | #endif |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 219 | |
Martin Schwidefsky | be5ec36 | 2007-04-27 16:01:44 +0200 | [diff] [blame] | 220 | pagefault_disable(); |
| 221 | rc = __get_user(instruction, (u16 __user *) regs->psw.addr); |
| 222 | pagefault_enable(); |
| 223 | if (rc) |
| 224 | return -EFAULT; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 225 | |
Martin Schwidefsky | be5ec36 | 2007-04-27 16:01:44 +0200 | [diff] [blame] | 226 | up_read(&mm->mmap_sem); |
| 227 | clear_tsk_thread_flag(current, TIF_SINGLE_STEP); |
| 228 | #ifdef CONFIG_COMPAT |
Heiko Carstens | 7757591 | 2009-06-12 10:26:25 +0200 | [diff] [blame] | 229 | compat = is_compat_task(); |
Martin Schwidefsky | be5ec36 | 2007-04-27 16:01:44 +0200 | [diff] [blame] | 230 | if (compat && instruction == 0x0a77) |
Heiko Carstens | c2e8b85 | 2008-04-17 07:46:07 +0200 | [diff] [blame] | 231 | sys32_sigreturn(); |
Martin Schwidefsky | be5ec36 | 2007-04-27 16:01:44 +0200 | [diff] [blame] | 232 | else if (compat && instruction == 0x0aad) |
Heiko Carstens | c2e8b85 | 2008-04-17 07:46:07 +0200 | [diff] [blame] | 233 | sys32_rt_sigreturn(); |
Martin Schwidefsky | be5ec36 | 2007-04-27 16:01:44 +0200 | [diff] [blame] | 234 | else |
| 235 | #endif |
| 236 | if (instruction == 0x0a77) |
Heiko Carstens | c2e8b85 | 2008-04-17 07:46:07 +0200 | [diff] [blame] | 237 | sys_sigreturn(); |
Martin Schwidefsky | be5ec36 | 2007-04-27 16:01:44 +0200 | [diff] [blame] | 238 | else if (instruction == 0x0aad) |
Heiko Carstens | c2e8b85 | 2008-04-17 07:46:07 +0200 | [diff] [blame] | 239 | sys_rt_sigreturn(); |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 240 | else { |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 241 | current->thread.prot_addr = address; |
| 242 | current->thread.trap_no = error_code; |
| 243 | do_sigsegv(regs, error_code, SEGV_MAPERR, address); |
| 244 | } |
| 245 | return 0; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 246 | } |
| 247 | #endif /* CONFIG_S390_EXEC_PROTECT */ |
| 248 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | /* |
| 250 | * This routine handles page faults. It determines the address, |
| 251 | * and the problem, and then passes it off to one of the appropriate |
| 252 | * routines. |
| 253 | * |
| 254 | * error_code: |
| 255 | * 04 Protection -> Write-Protection (suprression) |
| 256 | * 10 Segment translation -> Not present (nullification) |
| 257 | * 11 Page translation -> Not present (nullification) |
| 258 | * 3b Region third trans. -> Not present (nullification) |
| 259 | */ |
Gerald Schaefer | 482b05d | 2007-03-05 23:35:54 +0100 | [diff] [blame] | 260 | static inline void |
Martin Schwidefsky | 61365e1 | 2009-12-07 12:51:42 +0100 | [diff] [blame] | 261 | do_exception(struct pt_regs *regs, unsigned long error_code, int write, |
| 262 | unsigned long trans_exc_code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | { |
Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 264 | struct task_struct *tsk; |
| 265 | struct mm_struct *mm; |
| 266 | struct vm_area_struct *vma; |
| 267 | unsigned long address; |
Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 268 | int si_code; |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 269 | int fault; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
Martin Schwidefsky | 7ecb344 | 2009-12-07 12:51:44 +0100 | [diff] [blame^] | 271 | if (notify_page_fault(regs)) |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 272 | return; |
| 273 | |
Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 274 | tsk = current; |
| 275 | mm = tsk->mm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | /* |
| 278 | * Verify that the fault happened in user space, that |
| 279 | * we are not in an interrupt and that there is a |
| 280 | * user context. |
| 281 | */ |
Martin Schwidefsky | 61365e1 | 2009-12-07 12:51:42 +0100 | [diff] [blame] | 282 | if (unlikely(!user_space_fault(trans_exc_code) || in_atomic() || !mm)) |
Gerald Schaefer | 482b05d | 2007-03-05 23:35:54 +0100 | [diff] [blame] | 283 | goto no_context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | |
Martin Schwidefsky | 61365e1 | 2009-12-07 12:51:42 +0100 | [diff] [blame] | 285 | address = trans_exc_code & __FAIL_ADDR_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | /* |
| 287 | * When we get here, the fault happened in the current |
| 288 | * task's user address space, so we can switch on the |
| 289 | * interrupts again and then search the VMAs |
| 290 | */ |
| 291 | local_irq_enable(); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 292 | perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, 0, regs, address); |
Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 293 | down_read(&mm->mmap_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | |
Gerald Schaefer | 482b05d | 2007-03-05 23:35:54 +0100 | [diff] [blame] | 295 | si_code = SEGV_MAPERR; |
| 296 | vma = find_vma(mm, address); |
| 297 | if (!vma) |
| 298 | goto bad_area; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 299 | |
| 300 | #ifdef CONFIG_S390_EXEC_PROTECT |
Martin Schwidefsky | 61365e1 | 2009-12-07 12:51:42 +0100 | [diff] [blame] | 301 | if (unlikely((regs->psw.mask & PSW_MASK_ASC) == PSW_ASC_SECONDARY && |
| 302 | (trans_exc_code & 3) == 0 && !(vma->vm_flags & VM_EXEC))) |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 303 | if (!signal_return(mm, regs, address, error_code)) |
| 304 | /* |
| 305 | * signal_return() has done an up_read(&mm->mmap_sem) |
| 306 | * if it returns 0. |
| 307 | */ |
| 308 | return; |
| 309 | #endif |
| 310 | |
Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 311 | if (vma->vm_start <= address) |
| 312 | goto good_area; |
| 313 | if (!(vma->vm_flags & VM_GROWSDOWN)) |
| 314 | goto bad_area; |
| 315 | if (expand_stack(vma, address)) |
| 316 | goto bad_area; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | /* |
| 318 | * Ok, we have a good vm_area for this memory access, so |
| 319 | * we can handle it.. |
| 320 | */ |
| 321 | good_area: |
| 322 | si_code = SEGV_ACCERR; |
Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 323 | if (!write) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | /* page not present, check vm flags */ |
| 325 | if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))) |
| 326 | goto bad_area; |
| 327 | } else { |
| 328 | if (!(vma->vm_flags & VM_WRITE)) |
| 329 | goto bad_area; |
| 330 | } |
| 331 | |
Gerald Schaefer | 53492b1 | 2008-04-30 13:38:46 +0200 | [diff] [blame] | 332 | if (is_vm_hugetlb_page(vma)) |
| 333 | address &= HPAGE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | /* |
| 335 | * If for any reason at all we couldn't handle the fault, |
| 336 | * make sure we exit gracefully rather than endlessly redo |
| 337 | * the fault. |
| 338 | */ |
Linus Torvalds | d06063c | 2009-04-10 09:01:23 -0700 | [diff] [blame] | 339 | fault = handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0); |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 340 | if (unlikely(fault & VM_FAULT_ERROR)) { |
| 341 | if (fault & VM_FAULT_OOM) { |
Heiko Carstens | 59fa4392 | 2009-03-26 15:23:44 +0100 | [diff] [blame] | 342 | up_read(&mm->mmap_sem); |
| 343 | pagefault_out_of_memory(); |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 344 | return; |
| 345 | } else if (fault & VM_FAULT_SIGBUS) { |
| 346 | do_sigbus(regs, error_code, address); |
| 347 | return; |
| 348 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | BUG(); |
| 350 | } |
Heiko Carstens | bde69af | 2009-09-11 10:29:06 +0200 | [diff] [blame] | 351 | if (fault & VM_FAULT_MAJOR) { |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 352 | tsk->maj_flt++; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 353 | perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 0, |
Heiko Carstens | bde69af | 2009-09-11 10:29:06 +0200 | [diff] [blame] | 354 | regs, address); |
| 355 | } else { |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 356 | tsk->min_flt++; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 357 | perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 0, |
Heiko Carstens | bde69af | 2009-09-11 10:29:06 +0200 | [diff] [blame] | 358 | regs, address); |
| 359 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | up_read(&mm->mmap_sem); |
| 361 | /* |
| 362 | * The instruction that caused the program check will |
| 363 | * be repeated. Don't signal single step via SIGTRAP. |
| 364 | */ |
Gerald Schaefer | 482b05d | 2007-03-05 23:35:54 +0100 | [diff] [blame] | 365 | clear_tsk_thread_flag(tsk, TIF_SINGLE_STEP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | return; |
| 367 | |
| 368 | /* |
| 369 | * Something tried to access memory that isn't in our memory map.. |
| 370 | * Fix it, but check if it's kernel or user first.. |
| 371 | */ |
| 372 | bad_area: |
Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 373 | up_read(&mm->mmap_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 375 | /* User mode accesses just cause a SIGSEGV */ |
| 376 | if (regs->psw.mask & PSW_MASK_PSTATE) { |
| 377 | tsk->thread.prot_addr = address; |
| 378 | tsk->thread.trap_no = error_code; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | do_sigsegv(regs, error_code, si_code, address); |
Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 380 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | no_context: |
Martin Schwidefsky | 61365e1 | 2009-12-07 12:51:42 +0100 | [diff] [blame] | 384 | do_no_context(regs, error_code, trans_exc_code); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Gerald Schaefer | 482b05d | 2007-03-05 23:35:54 +0100 | [diff] [blame] | 387 | void __kprobes do_protection_exception(struct pt_regs *regs, |
Heiko Carstens | a806170 | 2008-04-17 07:46:26 +0200 | [diff] [blame] | 388 | long error_code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | { |
Martin Schwidefsky | 61365e1 | 2009-12-07 12:51:42 +0100 | [diff] [blame] | 390 | unsigned long trans_exc_code = S390_lowcore.trans_exc_code; |
| 391 | |
Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 392 | /* Protection exception is supressing, decrement psw address. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | regs->psw.addr -= (error_code >> 16); |
Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 394 | /* |
| 395 | * Check for low-address protection. This needs to be treated |
| 396 | * as a special case because the translation exception code |
| 397 | * field is not guaranteed to contain valid data in this case. |
| 398 | */ |
Martin Schwidefsky | 61365e1 | 2009-12-07 12:51:42 +0100 | [diff] [blame] | 399 | if (unlikely(!(trans_exc_code & 4))) { |
| 400 | do_low_address(regs, error_code, trans_exc_code); |
Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 401 | return; |
| 402 | } |
Martin Schwidefsky | 61365e1 | 2009-12-07 12:51:42 +0100 | [diff] [blame] | 403 | do_exception(regs, 4, 1, trans_exc_code); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Heiko Carstens | a806170 | 2008-04-17 07:46:26 +0200 | [diff] [blame] | 406 | void __kprobes do_dat_exception(struct pt_regs *regs, long error_code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | { |
Martin Schwidefsky | 61365e1 | 2009-12-07 12:51:42 +0100 | [diff] [blame] | 408 | do_exception(regs, error_code & 0xff, 0, S390_lowcore.trans_exc_code); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 411 | #ifdef CONFIG_64BIT |
| 412 | void __kprobes do_asce_exception(struct pt_regs *regs, unsigned long error_code) |
| 413 | { |
Martin Schwidefsky | 61365e1 | 2009-12-07 12:51:42 +0100 | [diff] [blame] | 414 | unsigned long trans_exc_code = S390_lowcore.trans_exc_code; |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 415 | struct mm_struct *mm; |
| 416 | struct vm_area_struct *vma; |
| 417 | unsigned long address; |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 418 | |
| 419 | mm = current->mm; |
Martin Schwidefsky | 61365e1 | 2009-12-07 12:51:42 +0100 | [diff] [blame] | 420 | address = trans_exc_code & __FAIL_ADDR_MASK; |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 421 | |
Martin Schwidefsky | 61365e1 | 2009-12-07 12:51:42 +0100 | [diff] [blame] | 422 | if (unlikely(!user_space_fault(trans_exc_code) || in_atomic() || !mm)) |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 423 | goto no_context; |
| 424 | |
| 425 | local_irq_enable(); |
| 426 | |
| 427 | down_read(&mm->mmap_sem); |
| 428 | vma = find_vma(mm, address); |
| 429 | up_read(&mm->mmap_sem); |
| 430 | |
| 431 | if (vma) { |
| 432 | update_mm(mm, current); |
| 433 | return; |
| 434 | } |
| 435 | |
| 436 | /* User mode accesses just cause a SIGSEGV */ |
| 437 | if (regs->psw.mask & PSW_MASK_PSTATE) { |
| 438 | current->thread.prot_addr = address; |
| 439 | current->thread.trap_no = error_code; |
| 440 | do_sigsegv(regs, error_code, SEGV_MAPERR, address); |
| 441 | return; |
| 442 | } |
| 443 | |
| 444 | no_context: |
Martin Schwidefsky | 61365e1 | 2009-12-07 12:51:42 +0100 | [diff] [blame] | 445 | do_no_context(regs, error_code, trans_exc_code); |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 446 | } |
| 447 | #endif |
| 448 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | #ifdef CONFIG_PFAULT |
| 450 | /* |
| 451 | * 'pfault' pseudo page faults routines. |
| 452 | */ |
Heiko Carstens | 29b08d2 | 2006-12-04 15:40:40 +0100 | [diff] [blame] | 453 | static ext_int_info_t ext_int_pfault; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | static int pfault_disable = 0; |
| 455 | |
| 456 | static int __init nopfault(char *str) |
| 457 | { |
| 458 | pfault_disable = 1; |
| 459 | return 1; |
| 460 | } |
| 461 | |
| 462 | __setup("nopfault", nopfault); |
| 463 | |
| 464 | typedef struct { |
| 465 | __u16 refdiagc; |
| 466 | __u16 reffcode; |
| 467 | __u16 refdwlen; |
| 468 | __u16 refversn; |
| 469 | __u64 refgaddr; |
| 470 | __u64 refselmk; |
| 471 | __u64 refcmpmk; |
| 472 | __u64 reserved; |
Heiko Carstens | c41fbc6 | 2007-10-12 16:11:51 +0200 | [diff] [blame] | 473 | } __attribute__ ((packed, aligned(8))) pfault_refbk_t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | |
| 475 | int pfault_init(void) |
| 476 | { |
| 477 | pfault_refbk_t refbk = |
| 478 | { 0x258, 0, 5, 2, __LC_CURRENT, 1ULL << 48, 1ULL << 48, |
| 479 | __PF_RES_FIELD }; |
| 480 | int rc; |
| 481 | |
Heiko Carstens | 29b08d2 | 2006-12-04 15:40:40 +0100 | [diff] [blame] | 482 | if (!MACHINE_IS_VM || pfault_disable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | return -1; |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 484 | asm volatile( |
| 485 | " diag %1,%0,0x258\n" |
| 486 | "0: j 2f\n" |
| 487 | "1: la %0,8\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | "2:\n" |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 489 | EX_TABLE(0b,1b) |
| 490 | : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | __ctl_set_bit(0, 9); |
| 492 | return rc; |
| 493 | } |
| 494 | |
| 495 | void pfault_fini(void) |
| 496 | { |
| 497 | pfault_refbk_t refbk = |
| 498 | { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL }; |
| 499 | |
Heiko Carstens | 29b08d2 | 2006-12-04 15:40:40 +0100 | [diff] [blame] | 500 | if (!MACHINE_IS_VM || pfault_disable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | return; |
| 502 | __ctl_clear_bit(0,9); |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 503 | asm volatile( |
| 504 | " diag %0,0,0x258\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | "0:\n" |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 506 | EX_TABLE(0b,0b) |
| 507 | : : "a" (&refbk), "m" (refbk) : "cc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 510 | static void pfault_interrupt(__u16 error_code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | { |
| 512 | struct task_struct *tsk; |
| 513 | __u16 subcode; |
| 514 | |
| 515 | /* |
| 516 | * Get the external interruption subcode & pfault |
| 517 | * initial/completion signal bit. VM stores this |
| 518 | * in the 'cpu address' field associated with the |
| 519 | * external interrupt. |
| 520 | */ |
| 521 | subcode = S390_lowcore.cpu_addr; |
| 522 | if ((subcode & 0xff00) != __SUBCODE_MASK) |
| 523 | return; |
| 524 | |
| 525 | /* |
| 526 | * Get the token (= address of the task structure of the affected task). |
| 527 | */ |
| 528 | tsk = *(struct task_struct **) __LC_PFAULT_INTPARM; |
| 529 | |
| 530 | if (subcode & 0x0080) { |
| 531 | /* signal bit is set -> a page has been swapped in by VM */ |
| 532 | if (xchg(&tsk->thread.pfault_wait, -1) != 0) { |
| 533 | /* Initial interrupt was faster than the completion |
| 534 | * interrupt. pfault_wait is valid. Set pfault_wait |
| 535 | * back to zero and wake up the process. This can |
| 536 | * safely be done because the task is still sleeping |
Martin Schwidefsky | b6d0944 | 2005-09-03 15:58:02 -0700 | [diff] [blame] | 537 | * and can't produce new pfaults. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | tsk->thread.pfault_wait = 0; |
| 539 | wake_up_process(tsk); |
Martin Schwidefsky | b6d0944 | 2005-09-03 15:58:02 -0700 | [diff] [blame] | 540 | put_task_struct(tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | } |
| 542 | } else { |
| 543 | /* signal bit not set -> a real page is missing. */ |
Martin Schwidefsky | b6d0944 | 2005-09-03 15:58:02 -0700 | [diff] [blame] | 544 | get_task_struct(tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | set_task_state(tsk, TASK_UNINTERRUPTIBLE); |
| 546 | if (xchg(&tsk->thread.pfault_wait, 1) != 0) { |
| 547 | /* Completion interrupt was faster than the initial |
| 548 | * interrupt (swapped in a -1 for pfault_wait). Set |
| 549 | * pfault_wait back to zero and exit. This can be |
| 550 | * done safely because tsk is running in kernel |
| 551 | * mode and can't produce new pfaults. */ |
| 552 | tsk->thread.pfault_wait = 0; |
| 553 | set_task_state(tsk, TASK_RUNNING); |
Martin Schwidefsky | b6d0944 | 2005-09-03 15:58:02 -0700 | [diff] [blame] | 554 | put_task_struct(tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | } else |
| 556 | set_tsk_need_resched(tsk); |
| 557 | } |
| 558 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | |
Heiko Carstens | 29b08d2 | 2006-12-04 15:40:40 +0100 | [diff] [blame] | 560 | void __init pfault_irq_init(void) |
| 561 | { |
| 562 | if (!MACHINE_IS_VM) |
| 563 | return; |
| 564 | |
| 565 | /* |
| 566 | * Try to get pfault pseudo page faults going. |
| 567 | */ |
| 568 | if (register_early_external_interrupt(0x2603, pfault_interrupt, |
| 569 | &ext_int_pfault) != 0) |
| 570 | panic("Couldn't request external interrupt 0x2603"); |
| 571 | |
| 572 | if (pfault_init() == 0) |
| 573 | return; |
| 574 | |
| 575 | /* Tough luck, no pfault. */ |
| 576 | pfault_disable = 1; |
| 577 | unregister_early_external_interrupt(0x2603, pfault_interrupt, |
| 578 | &ext_int_pfault); |
| 579 | } |
| 580 | #endif |