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