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