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