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