]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/s390/mm/fault.c
[S390] Minor fault path optimization.
[karo-tx-linux.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 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 inline void do_sigreturn(struct mm_struct *mm, struct pt_regs *regs,
267                                 int rt)
268 {
269         up_read(&mm->mmap_sem);
270         clear_tsk_thread_flag(current, TIF_SINGLE_STEP);
271 #ifdef CONFIG_COMPAT
272         if (test_tsk_thread_flag(current, TIF_31BIT)) {
273                 if (rt)
274                         sys32_rt_sigreturn(regs);
275                 else
276                         sys32_sigreturn(regs);
277                 return;
278         }
279 #endif /* CONFIG_COMPAT */
280         if (rt)
281                 sys_rt_sigreturn(regs);
282         else
283                 sys_sigreturn(regs);
284         return;
285 }
286
287 static int signal_return(struct mm_struct *mm, struct pt_regs *regs,
288                          unsigned long address, unsigned long error_code)
289 {
290         pgd_t *pgd;
291         pmd_t *pmd;
292         pte_t *pte;
293         u16 *instruction;
294         unsigned long pfn, uaddr = regs->psw.addr;
295
296         spin_lock(&mm->page_table_lock);
297         pgd = pgd_offset(mm, uaddr);
298         if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
299                 goto out_fault;
300         pmd = pmd_offset(pgd, uaddr);
301         if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
302                 goto out_fault;
303         pte = pte_offset_map(pmd_offset(pgd_offset(mm, uaddr), uaddr), uaddr);
304         if (!pte || !pte_present(*pte))
305                 goto out_fault;
306         pfn = pte_pfn(*pte);
307         if (!pfn_valid(pfn))
308                 goto out_fault;
309         spin_unlock(&mm->page_table_lock);
310
311         instruction = (u16 *) ((pfn << PAGE_SHIFT) + (uaddr & (PAGE_SIZE-1)));
312         if (*instruction == 0x0a77)
313                 do_sigreturn(mm, regs, 0);
314         else if (*instruction == 0x0aad)
315                 do_sigreturn(mm, regs, 1);
316         else {
317                 printk("- XXX - do_exception: task = %s, primary, NO EXEC "
318                        "-> SIGSEGV\n", current->comm);
319                 up_read(&mm->mmap_sem);
320                 current->thread.prot_addr = address;
321                 current->thread.trap_no = error_code;
322                 do_sigsegv(regs, error_code, SEGV_MAPERR, address);
323         }
324         return 0;
325 out_fault:
326         spin_unlock(&mm->page_table_lock);
327         return -EFAULT;
328 }
329 #endif /* CONFIG_S390_EXEC_PROTECT */
330
331 /*
332  * This routine handles page faults.  It determines the address,
333  * and the problem, and then passes it off to one of the appropriate
334  * routines.
335  *
336  * error_code:
337  *   04       Protection           ->  Write-Protection  (suprression)
338  *   10       Segment translation  ->  Not present       (nullification)
339  *   11       Page translation     ->  Not present       (nullification)
340  *   3b       Region third trans.  ->  Not present       (nullification)
341  */
342 static inline void
343 do_exception(struct pt_regs *regs, unsigned long error_code, int write)
344 {
345         struct task_struct *tsk;
346         struct mm_struct *mm;
347         struct vm_area_struct *vma;
348         unsigned long address;
349         int space;
350         int si_code;
351
352         if (notify_page_fault(regs, error_code) == NOTIFY_STOP)
353                 return;
354
355         tsk = current;
356         mm = tsk->mm;
357
358         /* get the failing address and the affected space */
359         address = S390_lowcore.trans_exc_code & __FAIL_ADDR_MASK;
360         space = check_space(tsk);
361
362         /*
363          * Verify that the fault happened in user space, that
364          * we are not in an interrupt and that there is a 
365          * user context.
366          */
367         if (unlikely(space == 0 || in_atomic() || !mm))
368                 goto no_context;
369
370         /*
371          * When we get here, the fault happened in the current
372          * task's user address space, so we can switch on the
373          * interrupts again and then search the VMAs
374          */
375         local_irq_enable();
376
377         down_read(&mm->mmap_sem);
378
379         si_code = SEGV_MAPERR;
380         vma = find_vma(mm, address);
381         if (!vma)
382                 goto bad_area;
383
384 #ifdef CONFIG_S390_EXEC_PROTECT
385         if (unlikely((space == 2) && !(vma->vm_flags & VM_EXEC)))
386                 if (!signal_return(mm, regs, address, error_code))
387                         /*
388                          * signal_return() has done an up_read(&mm->mmap_sem)
389                          * if it returns 0.
390                          */
391                         return;
392 #endif
393
394         if (vma->vm_start <= address)
395                 goto good_area;
396         if (!(vma->vm_flags & VM_GROWSDOWN))
397                 goto bad_area;
398         if (expand_stack(vma, address))
399                 goto bad_area;
400 /*
401  * Ok, we have a good vm_area for this memory access, so
402  * we can handle it..
403  */
404 good_area:
405         si_code = SEGV_ACCERR;
406         if (!write) {
407                 /* page not present, check vm flags */
408                 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
409                         goto bad_area;
410         } else {
411                 if (!(vma->vm_flags & VM_WRITE))
412                         goto bad_area;
413         }
414
415 survive:
416         /*
417          * If for any reason at all we couldn't handle the fault,
418          * make sure we exit gracefully rather than endlessly redo
419          * the fault.
420          */
421         switch (handle_mm_fault(mm, vma, address, write)) {
422         case VM_FAULT_MINOR:
423                 tsk->min_flt++;
424                 break;
425         case VM_FAULT_MAJOR:
426                 tsk->maj_flt++;
427                 break;
428         case VM_FAULT_SIGBUS:
429                 do_sigbus(regs, error_code, address);
430                 return;
431         case VM_FAULT_OOM:
432                 if (do_out_of_memory(regs, error_code, address))
433                         goto survive;
434                 return;
435         default:
436                 BUG();
437         }
438
439         up_read(&mm->mmap_sem);
440         /*
441          * The instruction that caused the program check will
442          * be repeated. Don't signal single step via SIGTRAP.
443          */
444         clear_tsk_thread_flag(tsk, TIF_SINGLE_STEP);
445         return;
446
447 /*
448  * Something tried to access memory that isn't in our memory map..
449  * Fix it, but check if it's kernel or user first..
450  */
451 bad_area:
452         up_read(&mm->mmap_sem);
453
454         /* User mode accesses just cause a SIGSEGV */
455         if (regs->psw.mask & PSW_MASK_PSTATE) {
456                 tsk->thread.prot_addr = address;
457                 tsk->thread.trap_no = error_code;
458                 do_sigsegv(regs, error_code, si_code, address);
459                 return;
460         }
461
462 no_context:
463         do_no_context(regs, error_code, address);
464 }
465
466 void __kprobes do_protection_exception(struct pt_regs *regs,
467                                        unsigned long error_code)
468 {
469         /* Protection exception is supressing, decrement psw address. */
470         regs->psw.addr -= (error_code >> 16);
471         /*
472          * Check for low-address protection.  This needs to be treated
473          * as a special case because the translation exception code
474          * field is not guaranteed to contain valid data in this case.
475          */
476         if (unlikely(!(S390_lowcore.trans_exc_code & 4))) {
477                 do_low_address(regs, error_code);
478                 return;
479         }
480         do_exception(regs, 4, 1);
481 }
482
483 void __kprobes do_dat_exception(struct pt_regs *regs, unsigned long error_code)
484 {
485         do_exception(regs, error_code & 0xff, 0);
486 }
487
488 #ifdef CONFIG_PFAULT 
489 /*
490  * 'pfault' pseudo page faults routines.
491  */
492 static ext_int_info_t ext_int_pfault;
493 static int pfault_disable = 0;
494
495 static int __init nopfault(char *str)
496 {
497         pfault_disable = 1;
498         return 1;
499 }
500
501 __setup("nopfault", nopfault);
502
503 typedef struct {
504         __u16 refdiagc;
505         __u16 reffcode;
506         __u16 refdwlen;
507         __u16 refversn;
508         __u64 refgaddr;
509         __u64 refselmk;
510         __u64 refcmpmk;
511         __u64 reserved;
512 } __attribute__ ((packed)) pfault_refbk_t;
513
514 int pfault_init(void)
515 {
516         pfault_refbk_t refbk =
517                 { 0x258, 0, 5, 2, __LC_CURRENT, 1ULL << 48, 1ULL << 48,
518                   __PF_RES_FIELD };
519         int rc;
520
521         if (!MACHINE_IS_VM || pfault_disable)
522                 return -1;
523         asm volatile(
524                 "       diag    %1,%0,0x258\n"
525                 "0:     j       2f\n"
526                 "1:     la      %0,8\n"
527                 "2:\n"
528                 EX_TABLE(0b,1b)
529                 : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc");
530         __ctl_set_bit(0, 9);
531         return rc;
532 }
533
534 void pfault_fini(void)
535 {
536         pfault_refbk_t refbk =
537         { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL };
538
539         if (!MACHINE_IS_VM || pfault_disable)
540                 return;
541         __ctl_clear_bit(0,9);
542         asm volatile(
543                 "       diag    %0,0,0x258\n"
544                 "0:\n"
545                 EX_TABLE(0b,0b)
546                 : : "a" (&refbk), "m" (refbk) : "cc");
547 }
548
549 static void pfault_interrupt(__u16 error_code)
550 {
551         struct task_struct *tsk;
552         __u16 subcode;
553
554         /*
555          * Get the external interruption subcode & pfault
556          * initial/completion signal bit. VM stores this 
557          * in the 'cpu address' field associated with the
558          * external interrupt. 
559          */
560         subcode = S390_lowcore.cpu_addr;
561         if ((subcode & 0xff00) != __SUBCODE_MASK)
562                 return;
563
564         /*
565          * Get the token (= address of the task structure of the affected task).
566          */
567         tsk = *(struct task_struct **) __LC_PFAULT_INTPARM;
568
569         if (subcode & 0x0080) {
570                 /* signal bit is set -> a page has been swapped in by VM */
571                 if (xchg(&tsk->thread.pfault_wait, -1) != 0) {
572                         /* Initial interrupt was faster than the completion
573                          * interrupt. pfault_wait is valid. Set pfault_wait
574                          * back to zero and wake up the process. This can
575                          * safely be done because the task is still sleeping
576                          * and can't produce new pfaults. */
577                         tsk->thread.pfault_wait = 0;
578                         wake_up_process(tsk);
579                         put_task_struct(tsk);
580                 }
581         } else {
582                 /* signal bit not set -> a real page is missing. */
583                 get_task_struct(tsk);
584                 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
585                 if (xchg(&tsk->thread.pfault_wait, 1) != 0) {
586                         /* Completion interrupt was faster than the initial
587                          * interrupt (swapped in a -1 for pfault_wait). Set
588                          * pfault_wait back to zero and exit. This can be
589                          * done safely because tsk is running in kernel 
590                          * mode and can't produce new pfaults. */
591                         tsk->thread.pfault_wait = 0;
592                         set_task_state(tsk, TASK_RUNNING);
593                         put_task_struct(tsk);
594                 } else
595                         set_tsk_need_resched(tsk);
596         }
597 }
598
599 void __init pfault_irq_init(void)
600 {
601         if (!MACHINE_IS_VM)
602                 return;
603
604         /*
605          * Try to get pfault pseudo page faults going.
606          */
607         if (register_early_external_interrupt(0x2603, pfault_interrupt,
608                                               &ext_int_pfault) != 0)
609                 panic("Couldn't request external interrupt 0x2603");
610
611         if (pfault_init() == 0)
612                 return;
613
614         /* Tough luck, no pfault. */
615         pfault_disable = 1;
616         unregister_early_external_interrupt(0x2603, pfault_interrupt,
617                                             &ext_int_pfault);
618 }
619 #endif