| 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. | 
 | 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 |  | 
| Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 204 | static void do_sigbus(struct pt_regs *regs, unsigned long error_code, | 
 | 205 | 		      unsigned long address) | 
 | 206 | { | 
 | 207 | 	struct task_struct *tsk = current; | 
 | 208 | 	struct mm_struct *mm = tsk->mm; | 
 | 209 |  | 
 | 210 | 	up_read(&mm->mmap_sem); | 
 | 211 | 	/* | 
 | 212 | 	 * Send a sigbus, regardless of whether we were in kernel | 
 | 213 | 	 * or user mode. | 
 | 214 | 	 */ | 
 | 215 | 	tsk->thread.prot_addr = address; | 
 | 216 | 	tsk->thread.trap_no = error_code; | 
 | 217 | 	force_sig(SIGBUS, tsk); | 
 | 218 |  | 
 | 219 | 	/* Kernel mode? Handle exceptions or die */ | 
 | 220 | 	if (!(regs->psw.mask & PSW_MASK_PSTATE)) | 
 | 221 | 		do_no_context(regs, error_code, address); | 
 | 222 | } | 
 | 223 |  | 
| Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 224 | #ifdef CONFIG_S390_EXEC_PROTECT | 
| Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 225 | static int signal_return(struct mm_struct *mm, struct pt_regs *regs, | 
 | 226 | 			 unsigned long address, unsigned long error_code) | 
 | 227 | { | 
| Martin Schwidefsky | be5ec36 | 2007-04-27 16:01:44 +0200 | [diff] [blame] | 228 | 	u16 instruction; | 
| Heiko Carstens | 490f03d | 2007-05-10 15:45:48 +0200 | [diff] [blame] | 229 | 	int rc; | 
 | 230 | #ifdef CONFIG_COMPAT | 
 | 231 | 	int compat; | 
 | 232 | #endif | 
| Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 233 |  | 
| Martin Schwidefsky | be5ec36 | 2007-04-27 16:01:44 +0200 | [diff] [blame] | 234 | 	pagefault_disable(); | 
 | 235 | 	rc = __get_user(instruction, (u16 __user *) regs->psw.addr); | 
 | 236 | 	pagefault_enable(); | 
 | 237 | 	if (rc) | 
 | 238 | 		return -EFAULT; | 
| Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 239 |  | 
| Martin Schwidefsky | be5ec36 | 2007-04-27 16:01:44 +0200 | [diff] [blame] | 240 | 	up_read(&mm->mmap_sem); | 
 | 241 | 	clear_tsk_thread_flag(current, TIF_SINGLE_STEP); | 
 | 242 | #ifdef CONFIG_COMPAT | 
| Heiko Carstens | 7757591 | 2009-06-12 10:26:25 +0200 | [diff] [blame] | 243 | 	compat = is_compat_task(); | 
| Martin Schwidefsky | be5ec36 | 2007-04-27 16:01:44 +0200 | [diff] [blame] | 244 | 	if (compat && instruction == 0x0a77) | 
| Heiko Carstens | c2e8b85 | 2008-04-17 07:46:07 +0200 | [diff] [blame] | 245 | 		sys32_sigreturn(); | 
| Martin Schwidefsky | be5ec36 | 2007-04-27 16:01:44 +0200 | [diff] [blame] | 246 | 	else if (compat && instruction == 0x0aad) | 
| Heiko Carstens | c2e8b85 | 2008-04-17 07:46:07 +0200 | [diff] [blame] | 247 | 		sys32_rt_sigreturn(); | 
| Martin Schwidefsky | be5ec36 | 2007-04-27 16:01:44 +0200 | [diff] [blame] | 248 | 	else | 
 | 249 | #endif | 
 | 250 | 	if (instruction == 0x0a77) | 
| Heiko Carstens | c2e8b85 | 2008-04-17 07:46:07 +0200 | [diff] [blame] | 251 | 		sys_sigreturn(); | 
| Martin Schwidefsky | be5ec36 | 2007-04-27 16:01:44 +0200 | [diff] [blame] | 252 | 	else if (instruction == 0x0aad) | 
| Heiko Carstens | c2e8b85 | 2008-04-17 07:46:07 +0200 | [diff] [blame] | 253 | 		sys_rt_sigreturn(); | 
| Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 254 | 	else { | 
| Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 255 | 		current->thread.prot_addr = address; | 
 | 256 | 		current->thread.trap_no = error_code; | 
 | 257 | 		do_sigsegv(regs, error_code, SEGV_MAPERR, address); | 
 | 258 | 	} | 
 | 259 | 	return 0; | 
| Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 260 | } | 
 | 261 | #endif /* CONFIG_S390_EXEC_PROTECT */ | 
 | 262 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | /* | 
 | 264 |  * This routine handles page faults.  It determines the address, | 
 | 265 |  * and the problem, and then passes it off to one of the appropriate | 
 | 266 |  * routines. | 
 | 267 |  * | 
 | 268 |  * error_code: | 
 | 269 |  *   04       Protection           ->  Write-Protection  (suprression) | 
 | 270 |  *   10       Segment translation  ->  Not present       (nullification) | 
 | 271 |  *   11       Page translation     ->  Not present       (nullification) | 
 | 272 |  *   3b       Region third trans.  ->  Not present       (nullification) | 
 | 273 |  */ | 
| Gerald Schaefer | 482b05d | 2007-03-05 23:35:54 +0100 | [diff] [blame] | 274 | static inline void | 
| Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 275 | do_exception(struct pt_regs *regs, unsigned long error_code, int write) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { | 
| Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 277 | 	struct task_struct *tsk; | 
 | 278 | 	struct mm_struct *mm; | 
 | 279 | 	struct vm_area_struct *vma; | 
 | 280 | 	unsigned long address; | 
| Gerald Schaefer | 482b05d | 2007-03-05 23:35:54 +0100 | [diff] [blame] | 281 | 	int space; | 
| Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 282 | 	int si_code; | 
| Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 283 | 	int fault; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 |  | 
| Christoph Hellwig | 33464e3 | 2007-05-04 18:47:46 +0200 | [diff] [blame] | 285 | 	if (notify_page_fault(regs, error_code)) | 
| Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 286 | 		return; | 
 | 287 |  | 
| Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 288 | 	tsk = current; | 
 | 289 | 	mm = tsk->mm; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 |  | 
| Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 291 | 	/* get the failing address and the affected space */ | 
 | 292 | 	address = S390_lowcore.trans_exc_code & __FAIL_ADDR_MASK; | 
| Gerald Schaefer | 482b05d | 2007-03-05 23:35:54 +0100 | [diff] [blame] | 293 | 	space = check_space(tsk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 |  | 
 | 295 | 	/* | 
 | 296 | 	 * Verify that the fault happened in user space, that | 
 | 297 | 	 * we are not in an interrupt and that there is a  | 
 | 298 | 	 * user context. | 
 | 299 | 	 */ | 
| Gerald Schaefer | 482b05d | 2007-03-05 23:35:54 +0100 | [diff] [blame] | 300 | 	if (unlikely(space == 0 || in_atomic() || !mm)) | 
 | 301 | 		goto no_context; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 |  | 
 | 303 | 	/* | 
 | 304 | 	 * When we get here, the fault happened in the current | 
 | 305 | 	 * task's user address space, so we can switch on the | 
 | 306 | 	 * interrupts again and then search the VMAs | 
 | 307 | 	 */ | 
 | 308 | 	local_irq_enable(); | 
| Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 309 | 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, 0, regs, address); | 
| Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 310 | 	down_read(&mm->mmap_sem); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 |  | 
| Gerald Schaefer | 482b05d | 2007-03-05 23:35:54 +0100 | [diff] [blame] | 312 | 	si_code = SEGV_MAPERR; | 
 | 313 | 	vma = find_vma(mm, address); | 
 | 314 | 	if (!vma) | 
 | 315 | 		goto bad_area; | 
| Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 316 |  | 
 | 317 | #ifdef CONFIG_S390_EXEC_PROTECT | 
| Gerald Schaefer | 482b05d | 2007-03-05 23:35:54 +0100 | [diff] [blame] | 318 | 	if (unlikely((space == 2) && !(vma->vm_flags & VM_EXEC))) | 
| Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 319 | 		if (!signal_return(mm, regs, address, error_code)) | 
 | 320 | 			/* | 
 | 321 | 			 * signal_return() has done an up_read(&mm->mmap_sem) | 
 | 322 | 			 * if it returns 0. | 
 | 323 | 			 */ | 
 | 324 | 			return; | 
 | 325 | #endif | 
 | 326 |  | 
| Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 327 | 	if (vma->vm_start <= address) | 
 | 328 | 		goto good_area; | 
 | 329 | 	if (!(vma->vm_flags & VM_GROWSDOWN)) | 
 | 330 | 		goto bad_area; | 
 | 331 | 	if (expand_stack(vma, address)) | 
 | 332 | 		goto bad_area; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | /* | 
 | 334 |  * Ok, we have a good vm_area for this memory access, so | 
 | 335 |  * we can handle it.. | 
 | 336 |  */ | 
 | 337 | good_area: | 
 | 338 | 	si_code = SEGV_ACCERR; | 
| Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 339 | 	if (!write) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | 		/* page not present, check vm flags */ | 
 | 341 | 		if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))) | 
 | 342 | 			goto bad_area; | 
 | 343 | 	} else { | 
 | 344 | 		if (!(vma->vm_flags & VM_WRITE)) | 
 | 345 | 			goto bad_area; | 
 | 346 | 	} | 
 | 347 |  | 
| Gerald Schaefer | 53492b1 | 2008-04-30 13:38:46 +0200 | [diff] [blame] | 348 | 	if (is_vm_hugetlb_page(vma)) | 
 | 349 | 		address &= HPAGE_MASK; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | 	/* | 
 | 351 | 	 * If for any reason at all we couldn't handle the fault, | 
 | 352 | 	 * make sure we exit gracefully rather than endlessly redo | 
 | 353 | 	 * the fault. | 
 | 354 | 	 */ | 
| Linus Torvalds | d06063c | 2009-04-10 09:01:23 -0700 | [diff] [blame] | 355 | 	fault = handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0); | 
| Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 356 | 	if (unlikely(fault & VM_FAULT_ERROR)) { | 
 | 357 | 		if (fault & VM_FAULT_OOM) { | 
| Heiko Carstens | 59fa439 | 2009-03-26 15:23:44 +0100 | [diff] [blame] | 358 | 			up_read(&mm->mmap_sem); | 
 | 359 | 			pagefault_out_of_memory(); | 
| Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 360 | 			return; | 
 | 361 | 		} else if (fault & VM_FAULT_SIGBUS) { | 
 | 362 | 			do_sigbus(regs, error_code, address); | 
 | 363 | 			return; | 
 | 364 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | 		BUG(); | 
 | 366 | 	} | 
| Heiko Carstens | bde69af | 2009-09-11 10:29:06 +0200 | [diff] [blame] | 367 | 	if (fault & VM_FAULT_MAJOR) { | 
| Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 368 | 		tsk->maj_flt++; | 
| Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 369 | 		perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 0, | 
| Heiko Carstens | bde69af | 2009-09-11 10:29:06 +0200 | [diff] [blame] | 370 | 				     regs, address); | 
 | 371 | 	} else { | 
| Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 372 | 		tsk->min_flt++; | 
| Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 373 | 		perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 0, | 
| Heiko Carstens | bde69af | 2009-09-11 10:29:06 +0200 | [diff] [blame] | 374 | 				     regs, address); | 
 | 375 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 |         up_read(&mm->mmap_sem); | 
 | 377 | 	/* | 
 | 378 | 	 * The instruction that caused the program check will | 
 | 379 | 	 * be repeated. Don't signal single step via SIGTRAP. | 
 | 380 | 	 */ | 
| Gerald Schaefer | 482b05d | 2007-03-05 23:35:54 +0100 | [diff] [blame] | 381 | 	clear_tsk_thread_flag(tsk, TIF_SINGLE_STEP); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 |         return; | 
 | 383 |  | 
 | 384 | /* | 
 | 385 |  * Something tried to access memory that isn't in our memory map.. | 
 | 386 |  * Fix it, but check if it's kernel or user first.. | 
 | 387 |  */ | 
 | 388 | bad_area: | 
| Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 389 | 	up_read(&mm->mmap_sem); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 |  | 
| Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 391 | 	/* User mode accesses just cause a SIGSEGV */ | 
 | 392 | 	if (regs->psw.mask & PSW_MASK_PSTATE) { | 
 | 393 | 		tsk->thread.prot_addr = address; | 
 | 394 | 		tsk->thread.trap_no = error_code; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | 		do_sigsegv(regs, error_code, si_code, address); | 
| Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 396 | 		return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | 	} | 
 | 398 |  | 
 | 399 | no_context: | 
| Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 400 | 	do_no_context(regs, error_code, address); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | } | 
 | 402 |  | 
| Gerald Schaefer | 482b05d | 2007-03-05 23:35:54 +0100 | [diff] [blame] | 403 | void __kprobes do_protection_exception(struct pt_regs *regs, | 
| Heiko Carstens | a806170 | 2008-04-17 07:46:26 +0200 | [diff] [blame] | 404 | 				       long error_code) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | { | 
| Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 406 | 	/* Protection exception is supressing, decrement psw address. */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | 	regs->psw.addr -= (error_code >> 16); | 
| Martin Schwidefsky | 10c1031 | 2007-04-27 16:01:43 +0200 | [diff] [blame] | 408 | 	/* | 
 | 409 | 	 * Check for low-address protection.  This needs to be treated | 
 | 410 | 	 * as a special case because the translation exception code | 
 | 411 | 	 * field is not guaranteed to contain valid data in this case. | 
 | 412 | 	 */ | 
 | 413 | 	if (unlikely(!(S390_lowcore.trans_exc_code & 4))) { | 
 | 414 | 		do_low_address(regs, error_code); | 
 | 415 | 		return; | 
 | 416 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | 	do_exception(regs, 4, 1); | 
 | 418 | } | 
 | 419 |  | 
| Heiko Carstens | a806170 | 2008-04-17 07:46:26 +0200 | [diff] [blame] | 420 | void __kprobes do_dat_exception(struct pt_regs *regs, long error_code) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | { | 
 | 422 | 	do_exception(regs, error_code & 0xff, 0); | 
 | 423 | } | 
 | 424 |  | 
| Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 425 | #ifdef CONFIG_64BIT | 
 | 426 | void __kprobes do_asce_exception(struct pt_regs *regs, unsigned long error_code) | 
 | 427 | { | 
 | 428 | 	struct mm_struct *mm; | 
 | 429 | 	struct vm_area_struct *vma; | 
 | 430 | 	unsigned long address; | 
 | 431 | 	int space; | 
 | 432 |  | 
 | 433 | 	mm = current->mm; | 
 | 434 | 	address = S390_lowcore.trans_exc_code & __FAIL_ADDR_MASK; | 
 | 435 | 	space = check_space(current); | 
 | 436 |  | 
 | 437 | 	if (unlikely(space == 0 || in_atomic() || !mm)) | 
 | 438 | 		goto no_context; | 
 | 439 |  | 
 | 440 | 	local_irq_enable(); | 
 | 441 |  | 
 | 442 | 	down_read(&mm->mmap_sem); | 
 | 443 | 	vma = find_vma(mm, address); | 
 | 444 | 	up_read(&mm->mmap_sem); | 
 | 445 |  | 
 | 446 | 	if (vma) { | 
 | 447 | 		update_mm(mm, current); | 
 | 448 | 		return; | 
 | 449 | 	} | 
 | 450 |  | 
 | 451 | 	/* User mode accesses just cause a SIGSEGV */ | 
 | 452 | 	if (regs->psw.mask & PSW_MASK_PSTATE) { | 
 | 453 | 		current->thread.prot_addr = address; | 
 | 454 | 		current->thread.trap_no = error_code; | 
 | 455 | 		do_sigsegv(regs, error_code, SEGV_MAPERR, address); | 
 | 456 | 		return; | 
 | 457 | 	} | 
 | 458 |  | 
 | 459 | no_context: | 
 | 460 | 	do_no_context(regs, error_code, address); | 
 | 461 | } | 
 | 462 | #endif | 
 | 463 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | #ifdef CONFIG_PFAULT  | 
 | 465 | /* | 
 | 466 |  * 'pfault' pseudo page faults routines. | 
 | 467 |  */ | 
| Heiko Carstens | 29b08d2 | 2006-12-04 15:40:40 +0100 | [diff] [blame] | 468 | static ext_int_info_t ext_int_pfault; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | static int pfault_disable = 0; | 
 | 470 |  | 
 | 471 | static int __init nopfault(char *str) | 
 | 472 | { | 
 | 473 | 	pfault_disable = 1; | 
 | 474 | 	return 1; | 
 | 475 | } | 
 | 476 |  | 
 | 477 | __setup("nopfault", nopfault); | 
 | 478 |  | 
 | 479 | typedef struct { | 
 | 480 | 	__u16 refdiagc; | 
 | 481 | 	__u16 reffcode; | 
 | 482 | 	__u16 refdwlen; | 
 | 483 | 	__u16 refversn; | 
 | 484 | 	__u64 refgaddr; | 
 | 485 | 	__u64 refselmk; | 
 | 486 | 	__u64 refcmpmk; | 
 | 487 | 	__u64 reserved; | 
| Heiko Carstens | c41fbc6 | 2007-10-12 16:11:51 +0200 | [diff] [blame] | 488 | } __attribute__ ((packed, aligned(8))) pfault_refbk_t; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 |  | 
 | 490 | int pfault_init(void) | 
 | 491 | { | 
 | 492 | 	pfault_refbk_t refbk = | 
 | 493 | 		{ 0x258, 0, 5, 2, __LC_CURRENT, 1ULL << 48, 1ULL << 48, | 
 | 494 | 		  __PF_RES_FIELD }; | 
 | 495 |         int rc; | 
 | 496 |  | 
| Heiko Carstens | 29b08d2 | 2006-12-04 15:40:40 +0100 | [diff] [blame] | 497 | 	if (!MACHINE_IS_VM || pfault_disable) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | 		return -1; | 
| Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 499 | 	asm volatile( | 
 | 500 | 		"	diag	%1,%0,0x258\n" | 
 | 501 | 		"0:	j	2f\n" | 
 | 502 | 		"1:	la	%0,8\n" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | 		"2:\n" | 
| Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 504 | 		EX_TABLE(0b,1b) | 
 | 505 | 		: "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 |         __ctl_set_bit(0, 9); | 
 | 507 |         return rc; | 
 | 508 | } | 
 | 509 |  | 
 | 510 | void pfault_fini(void) | 
 | 511 | { | 
 | 512 | 	pfault_refbk_t refbk = | 
 | 513 | 	{ 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL }; | 
 | 514 |  | 
| Heiko Carstens | 29b08d2 | 2006-12-04 15:40:40 +0100 | [diff] [blame] | 515 | 	if (!MACHINE_IS_VM || pfault_disable) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | 		return; | 
 | 517 | 	__ctl_clear_bit(0,9); | 
| Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 518 | 	asm volatile( | 
 | 519 | 		"	diag	%0,0,0x258\n" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | 		"0:\n" | 
| Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 521 | 		EX_TABLE(0b,0b) | 
 | 522 | 		: : "a" (&refbk), "m" (refbk) : "cc"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | } | 
 | 524 |  | 
| Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 525 | static void pfault_interrupt(__u16 error_code) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | { | 
 | 527 | 	struct task_struct *tsk; | 
 | 528 | 	__u16 subcode; | 
 | 529 |  | 
 | 530 | 	/* | 
 | 531 | 	 * Get the external interruption subcode & pfault | 
 | 532 | 	 * initial/completion signal bit. VM stores this  | 
 | 533 | 	 * in the 'cpu address' field associated with the | 
 | 534 |          * external interrupt.  | 
 | 535 | 	 */ | 
 | 536 | 	subcode = S390_lowcore.cpu_addr; | 
 | 537 | 	if ((subcode & 0xff00) != __SUBCODE_MASK) | 
 | 538 | 		return; | 
 | 539 |  | 
 | 540 | 	/* | 
 | 541 | 	 * Get the token (= address of the task structure of the affected task). | 
 | 542 | 	 */ | 
 | 543 | 	tsk = *(struct task_struct **) __LC_PFAULT_INTPARM; | 
 | 544 |  | 
 | 545 | 	if (subcode & 0x0080) { | 
 | 546 | 		/* signal bit is set -> a page has been swapped in by VM */ | 
 | 547 | 		if (xchg(&tsk->thread.pfault_wait, -1) != 0) { | 
 | 548 | 			/* Initial interrupt was faster than the completion | 
 | 549 | 			 * interrupt. pfault_wait is valid. Set pfault_wait | 
 | 550 | 			 * back to zero and wake up the process. This can | 
 | 551 | 			 * safely be done because the task is still sleeping | 
| Martin Schwidefsky | b6d0944 | 2005-09-03 15:58:02 -0700 | [diff] [blame] | 552 | 			 * and can't produce new pfaults. */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | 			tsk->thread.pfault_wait = 0; | 
 | 554 | 			wake_up_process(tsk); | 
| Martin Schwidefsky | b6d0944 | 2005-09-03 15:58:02 -0700 | [diff] [blame] | 555 | 			put_task_struct(tsk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | 		} | 
 | 557 | 	} else { | 
 | 558 | 		/* signal bit not set -> a real page is missing. */ | 
| Martin Schwidefsky | b6d0944 | 2005-09-03 15:58:02 -0700 | [diff] [blame] | 559 | 		get_task_struct(tsk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | 		set_task_state(tsk, TASK_UNINTERRUPTIBLE); | 
 | 561 | 		if (xchg(&tsk->thread.pfault_wait, 1) != 0) { | 
 | 562 | 			/* Completion interrupt was faster than the initial | 
 | 563 | 			 * interrupt (swapped in a -1 for pfault_wait). Set | 
 | 564 | 			 * pfault_wait back to zero and exit. This can be | 
 | 565 | 			 * done safely because tsk is running in kernel  | 
 | 566 | 			 * mode and can't produce new pfaults. */ | 
 | 567 | 			tsk->thread.pfault_wait = 0; | 
 | 568 | 			set_task_state(tsk, TASK_RUNNING); | 
| Martin Schwidefsky | b6d0944 | 2005-09-03 15:58:02 -0700 | [diff] [blame] | 569 | 			put_task_struct(tsk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | 		} else | 
 | 571 | 			set_tsk_need_resched(tsk); | 
 | 572 | 	} | 
 | 573 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 |  | 
| Heiko Carstens | 29b08d2 | 2006-12-04 15:40:40 +0100 | [diff] [blame] | 575 | void __init pfault_irq_init(void) | 
 | 576 | { | 
 | 577 | 	if (!MACHINE_IS_VM) | 
 | 578 | 		return; | 
 | 579 |  | 
 | 580 | 	/* | 
 | 581 | 	 * Try to get pfault pseudo page faults going. | 
 | 582 | 	 */ | 
 | 583 | 	if (register_early_external_interrupt(0x2603, pfault_interrupt, | 
 | 584 | 					      &ext_int_pfault) != 0) | 
 | 585 | 		panic("Couldn't request external interrupt 0x2603"); | 
 | 586 |  | 
 | 587 | 	if (pfault_init() == 0) | 
 | 588 | 		return; | 
 | 589 |  | 
 | 590 | 	/* Tough luck, no pfault. */ | 
 | 591 | 	pfault_disable = 1; | 
 | 592 | 	unregister_early_external_interrupt(0x2603, pfault_interrupt, | 
 | 593 | 					    &ext_int_pfault); | 
 | 594 | } | 
 | 595 | #endif |