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