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