ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.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/config.h>
14 #include <linux/signal.h>
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/string.h>
19 #include <linux/types.h>
20 #include <linux/ptrace.h>
21 #include <linux/mman.h>
22 #include <linux/mm.h>
23 #include <linux/smp.h>
24 #include <linux/smp_lock.h>
25 #include <linux/init.h>
26 #include <linux/console.h>
27 #include <linux/module.h>
28
29 #include <asm/system.h>
30 #include <asm/uaccess.h>
31 #include <asm/pgtable.h>
32 #include <asm/hardirq.h>
33
34 #ifndef CONFIG_ARCH_S390X
35 #define __FAIL_ADDR_MASK 0x7ffff000
36 #define __FIXUP_MASK 0x7fffffff
37 #define __SUBCODE_MASK 0x0200
38 #define __PF_RES_FIELD 0ULL
39 #else /* CONFIG_ARCH_S390X */
40 #define __FAIL_ADDR_MASK -4096L
41 #define __FIXUP_MASK ~0L
42 #define __SUBCODE_MASK 0x0600
43 #define __PF_RES_FIELD 0x8000000000000000ULL
44 #endif /* CONFIG_ARCH_S390X */
45
46 #ifdef CONFIG_SYSCTL
47 extern int sysctl_userprocess_debug;
48 #endif
49
50 extern void die(const char *,struct pt_regs *,long);
51
52 extern spinlock_t timerlist_lock;
53
54 /*
55  * Unlock any spinlocks which will prevent us from getting the
56  * message out (timerlist_lock is acquired through the
57  * console unblank code)
58  */
59 void bust_spinlocks(int yes)
60 {
61         if (yes) {
62                 oops_in_progress = 1;
63         } else {
64                 int loglevel_save = console_loglevel;
65                 oops_in_progress = 0;
66                 console_unblank();
67                 /*
68                  * OK, the message is on the console.  Now we call printk()
69                  * without oops_in_progress set so that printk will give klogd
70                  * a poke.  Hold onto your hats...
71                  */
72                 console_loglevel = 15;
73                 printk(" ");
74                 console_loglevel = loglevel_save;
75         }
76 }
77
78 /*
79  * Check which address space is addressed by the access
80  * register in S390_lowcore.exc_access_id.
81  * Returns 1 for user space and 0 for kernel space.
82  */
83 static int __check_access_register(struct pt_regs *regs, int error_code)
84 {
85         int areg = S390_lowcore.exc_access_id;
86
87         if (areg == 0)
88                 /* Access via access register 0 -> kernel address */
89                 return 0;
90         if (regs && areg < NUM_ACRS && current->thread.acrs[areg] <= 1)
91                 /*
92                  * access register contains 0 -> kernel address,
93                  * access register contains 1 -> user space address
94                  */
95                 return current->thread.acrs[areg];
96
97         /* Something unhealthy was done with the access registers... */
98         die("page fault via unknown access register", regs, error_code);
99         do_exit(SIGKILL);
100         return 0;
101 }
102
103 /*
104  * Check which address space the address belongs to.
105  * Returns 1 for user space and 0 for kernel space.
106  */
107 static inline int check_user_space(struct pt_regs *regs, int error_code)
108 {
109         /*
110          * The lowest two bits of S390_lowcore.trans_exc_code indicate
111          * which paging table was used:
112          *   0: Primary Segment Table Descriptor
113          *   1: STD determined via access register
114          *   2: Secondary Segment Table Descriptor
115          *   3: Home Segment Table Descriptor
116          */
117         int descriptor = S390_lowcore.trans_exc_code & 3;
118         if (descriptor == 1) {
119                 save_access_regs(current->thread.acrs);
120                 return __check_access_register(regs, error_code);
121         }
122         return descriptor >> 1;
123 }
124
125 /*
126  * Send SIGSEGV to task.  This is an external routine
127  * to keep the stack usage of do_page_fault small.
128  */
129 static void force_sigsegv(struct pt_regs *regs, unsigned long error_code,
130                           int si_code, unsigned long address)
131 {
132         struct siginfo si;
133
134 #if defined(CONFIG_SYSCTL) || defined(CONFIG_PROCESS_DEBUG)
135 #if defined(CONFIG_SYSCTL)
136         if (sysctl_userprocess_debug)
137 #endif
138         {
139                 printk("User process fault: interruption code 0x%lX\n",
140                        error_code);
141                 printk("failing address: %lX\n", address);
142                 show_regs(regs);
143         }
144 #endif
145         si.si_signo = SIGSEGV;
146         si.si_code = si_code;
147         si.si_addr = (void *) address;
148         force_sig_info(SIGSEGV, &si, current);
149 }
150
151 /*
152  * This routine handles page faults.  It determines the address,
153  * and the problem, and then passes it off to one of the appropriate
154  * routines.
155  *
156  * error_code:
157  *   04       Protection           ->  Write-Protection  (suprression)
158  *   10       Segment translation  ->  Not present       (nullification)
159  *   11       Page translation     ->  Not present       (nullification)
160  *   3b       Region third trans.  ->  Not present       (nullification)
161  */
162 extern inline void do_exception(struct pt_regs *regs, unsigned long error_code)
163 {
164         struct task_struct *tsk;
165         struct mm_struct *mm;
166         struct vm_area_struct * vma;
167         unsigned long address;
168         int user_address;
169         const struct exception_table_entry *fixup;
170         int si_code = SEGV_MAPERR;
171
172         tsk = current;
173         mm = tsk->mm;
174         
175         /* 
176          * Check for low-address protection.  This needs to be treated
177          * as a special case because the translation exception code 
178          * field is not guaranteed to contain valid data in this case.
179          */
180         if (error_code == 4 && !(S390_lowcore.trans_exc_code & 4)) {
181
182                 /* Low-address protection hit in kernel mode means 
183                    NULL pointer write access in kernel mode.  */
184                 if (!(regs->psw.mask & PSW_MASK_PSTATE)) {
185                         address = 0;
186                         user_address = 0;
187                         goto no_context;
188                 }
189
190                 /* Low-address protection hit in user mode 'cannot happen'.  */
191                 die ("Low-address protection", regs, error_code);
192                 do_exit(SIGKILL);
193         }
194
195         /* 
196          * get the failing address 
197          * more specific the segment and page table portion of 
198          * the address 
199          */
200         address = S390_lowcore.trans_exc_code & __FAIL_ADDR_MASK;
201         user_address = check_user_space(regs, error_code);
202
203         /*
204          * Verify that the fault happened in user space, that
205          * we are not in an interrupt and that there is a 
206          * user context.
207          */
208         if (user_address == 0 || in_interrupt() || !mm)
209                 goto no_context;
210
211         /*
212          * When we get here, the fault happened in the current
213          * task's user address space, so we can switch on the
214          * interrupts again and then search the VMAs
215          */
216         local_irq_enable();
217
218         down_read(&mm->mmap_sem);
219
220         vma = find_vma(mm, address);
221         if (!vma)
222                 goto bad_area;
223         if (vma->vm_start <= address) 
224                 goto good_area;
225         if (!(vma->vm_flags & VM_GROWSDOWN))
226                 goto bad_area;
227         if (expand_stack(vma, address))
228                 goto bad_area;
229 /*
230  * Ok, we have a good vm_area for this memory access, so
231  * we can handle it..
232  */
233 good_area:
234         si_code = SEGV_ACCERR;
235         if (error_code != 4) {
236                 /* page not present, check vm flags */
237                 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
238                         goto bad_area;
239         } else {
240                 if (!(vma->vm_flags & VM_WRITE))
241                         goto bad_area;
242         }
243
244 survive:
245         /*
246          * If for any reason at all we couldn't handle the fault,
247          * make sure we exit gracefully rather than endlessly redo
248          * the fault.
249          */
250         switch (handle_mm_fault(mm, vma, address, error_code == 4)) {
251         case VM_FAULT_MINOR:
252                 tsk->min_flt++;
253                 break;
254         case VM_FAULT_MAJOR:
255                 tsk->maj_flt++;
256                 break;
257         case VM_FAULT_SIGBUS:
258                 goto do_sigbus;
259         case VM_FAULT_OOM:
260                 goto out_of_memory;
261         default:
262                 BUG();
263         }
264
265         up_read(&mm->mmap_sem);
266         return;
267
268 /*
269  * Something tried to access memory that isn't in our memory map..
270  * Fix it, but check if it's kernel or user first..
271  */
272 bad_area:
273         up_read(&mm->mmap_sem);
274
275         /* User mode accesses just cause a SIGSEGV */
276         if (regs->psw.mask & PSW_MASK_PSTATE) {
277                 tsk->thread.prot_addr = address;
278                 tsk->thread.trap_no = error_code;
279                 force_sigsegv(regs, error_code, si_code, address);
280                 return;
281         }
282
283 no_context:
284         /* Are we prepared to handle this kernel fault?  */
285         fixup = search_exception_tables(regs->psw.addr & __FIXUP_MASK);
286         if (fixup) {
287                 regs->psw.addr = fixup->fixup | PSW_ADDR_AMODE;
288                 return;
289         }
290
291 /*
292  * Oops. The kernel tried to access some bad page. We'll have to
293  * terminate things with extreme prejudice.
294  */
295         if (user_address == 0)
296                 printk(KERN_ALERT "Unable to handle kernel pointer dereference"
297                        " at virtual kernel address %p\n", (void *)address);
298         else
299                 printk(KERN_ALERT "Unable to handle kernel paging request"
300                        " at virtual user address %p\n", (void *)address);
301
302         die("Oops", regs, error_code);
303         do_exit(SIGKILL);
304
305
306 /*
307  * We ran out of memory, or some other thing happened to us that made
308  * us unable to handle the page fault gracefully.
309 */
310 out_of_memory:
311         up_read(&mm->mmap_sem);
312         if (tsk->pid == 1) {
313                 yield();
314                 goto survive;
315         }
316         printk("VM: killing process %s\n", tsk->comm);
317         if (regs->psw.mask & PSW_MASK_PSTATE)
318                 do_exit(SIGKILL);
319         goto no_context;
320
321 do_sigbus:
322         up_read(&mm->mmap_sem);
323
324         /*
325          * Send a sigbus, regardless of whether we were in kernel
326          * or user mode.
327          */
328         tsk->thread.prot_addr = address;
329         tsk->thread.trap_no = error_code;
330         force_sig(SIGBUS, tsk);
331
332         /* Kernel mode? Handle exceptions or die */
333         if (!(regs->psw.mask & PSW_MASK_PSTATE))
334                 goto no_context;
335 }
336
337 void do_protection_exception(struct pt_regs *regs, unsigned long error_code)
338 {
339         regs->psw.addr -= (error_code >> 16);
340         do_exception(regs, 4);
341 }
342
343 void do_segment_exception(struct pt_regs *regs, unsigned long error_code)
344 {
345         do_exception(regs, 0x10);
346 }
347
348 void do_page_exception(struct pt_regs *regs, unsigned long error_code)
349 {
350         do_exception(regs, 0x11);
351 }
352
353 #ifdef CONFIG_ARCH_S390X
354
355 void
356 do_region_exception(struct pt_regs *regs, unsigned long error_code)
357 {
358         do_exception(regs, 0x3b);
359 }
360
361 #else /* CONFIG_ARCH_S390X */
362
363 typedef struct _pseudo_wait_t {
364        struct _pseudo_wait_t *next;
365        wait_queue_head_t queue;
366        unsigned long address;
367        int resolved;
368 } pseudo_wait_t;
369
370 static pseudo_wait_t *pseudo_lock_queue = NULL;
371 static spinlock_t pseudo_wait_spinlock; /* spinlock to protect lock queue */
372
373 /*
374  * This routine handles 'pagex' pseudo page faults.
375  */
376 asmlinkage void
377 do_pseudo_page_fault(struct pt_regs *regs, unsigned long error_code)
378 {
379         pseudo_wait_t wait_struct;
380         pseudo_wait_t *ptr, *last, *next;
381         unsigned long address;
382
383         /*
384          * get the failing address
385          * more specific the segment and page table portion of
386          * the address
387          */
388         address = S390_lowcore.trans_exc_code & 0xfffff000;
389
390         if (address & 0x80000000) {
391                 /* high bit set -> a page has been swapped in by VM */
392                 address &= 0x7fffffff;
393                 spin_lock(&pseudo_wait_spinlock);
394                 last = NULL;
395                 ptr = pseudo_lock_queue;
396                 while (ptr != NULL) {
397                         next = ptr->next;
398                         if (address == ptr->address) {
399                                  /*
400                                  * This is one of the processes waiting
401                                  * for the page. Unchain from the queue.
402                                  * There can be more than one process
403                                  * waiting for the same page. VM presents
404                                  * an initial and a completion interrupt for
405                                  * every process that tries to access a 
406                                  * page swapped out by VM. 
407                                  */
408                                 if (last == NULL)
409                                         pseudo_lock_queue = next;
410                                 else
411                                         last->next = next;
412                                 /* now wake up the process */
413                                 ptr->resolved = 1;
414                                 wake_up(&ptr->queue);
415                         } else
416                                 last = ptr;
417                         ptr = next;
418                 }
419                 spin_unlock(&pseudo_wait_spinlock);
420         } else {
421                 /* Pseudo page faults in kernel mode is a bad idea */
422                 if (!(regs->psw.mask & PSW_MASK_PSTATE)) {
423                         /*
424                          * VM presents pseudo page faults if the interrupted
425                          * state was not disabled for interrupts. So we can
426                          * get pseudo page fault interrupts while running
427                          * in kernel mode. We simply access the page here
428                          * while we are running disabled. VM will then swap
429                          * in the page synchronously.
430                          */
431                          if (check_user_space(regs, error_code) == 0)
432                                  /* dereference a virtual kernel address */
433                                  __asm__ __volatile__ (
434                                          "  ic 0,0(%0)"
435                                          : : "a" (address) : "0");
436                          else
437                                  /* dereference a virtual user address */
438                                  __asm__ __volatile__ (
439                                          "  la   2,0(%0)\n"
440                                          "  sacf 512\n"
441                                          "  ic   2,0(2)\n"
442                                          "0:sacf 0\n"
443                                          ".section __ex_table,\"a\"\n"
444                                          "  .align 4\n"
445                                          "  .long  0b,0b\n"
446                                          ".previous"
447                                          : : "a" (address) : "2" );
448
449                         return;
450                 }
451                 /* initialize and add element to pseudo_lock_queue */
452                 init_waitqueue_head (&wait_struct.queue);
453                 wait_struct.address = address;
454                 wait_struct.resolved = 0;
455                 spin_lock(&pseudo_wait_spinlock);
456                 wait_struct.next = pseudo_lock_queue;
457                 pseudo_lock_queue = &wait_struct;
458                 spin_unlock(&pseudo_wait_spinlock);
459                 /* go to sleep */
460                 wait_event(wait_struct.queue, wait_struct.resolved);
461         }
462 }
463 #endif /* CONFIG_ARCH_S390X */
464
465 #ifdef CONFIG_PFAULT 
466 /*
467  * 'pfault' pseudo page faults routines.
468  */
469 static int pfault_disable = 0;
470
471 static int __init nopfault(char *str)
472 {
473         pfault_disable = 1;
474         return 1;
475 }
476
477 __setup("nopfault", nopfault);
478
479 typedef struct {
480         __u16 refdiagc;
481         __u16 reffcode;
482         __u16 refdwlen;
483         __u16 refversn;
484         __u64 refgaddr;
485         __u64 refselmk;
486         __u64 refcmpmk;
487         __u64 reserved;
488 } __attribute__ ((packed)) pfault_refbk_t;
489
490 int pfault_init(void)
491 {
492         pfault_refbk_t refbk =
493                 { 0x258, 0, 5, 2, __LC_CURRENT, 1ULL << 48, 1ULL << 48,
494                   __PF_RES_FIELD };
495         int rc;
496
497         if (pfault_disable)
498                 return -1;
499         __asm__ __volatile__(
500                 "    diag  %1,%0,0x258\n"
501                 "0:  j     2f\n"
502                 "1:  la    %0,8\n"
503                 "2:\n"
504                 ".section __ex_table,\"a\"\n"
505                 "   .align 4\n"
506 #ifndef CONFIG_ARCH_S390X
507                 "   .long  0b,1b\n"
508 #else /* CONFIG_ARCH_S390X */
509                 "   .quad  0b,1b\n"
510 #endif /* CONFIG_ARCH_S390X */
511                 ".previous"
512                 : "=d" (rc) : "a" (&refbk) : "cc" );
513         __ctl_set_bit(0, 9);
514         return rc;
515 }
516
517 void pfault_fini(void)
518 {
519         pfault_refbk_t refbk =
520         { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL };
521
522         if (pfault_disable)
523                 return;
524         __ctl_clear_bit(0,9);
525         __asm__ __volatile__(
526                 "    diag  %0,0,0x258\n"
527                 "0:\n"
528                 ".section __ex_table,\"a\"\n"
529                 "   .align 4\n"
530 #ifndef CONFIG_ARCH_S390X
531                 "   .long  0b,0b\n"
532 #else /* CONFIG_ARCH_S390X */
533                 "   .quad  0b,0b\n"
534 #endif /* CONFIG_ARCH_S390X */
535                 ".previous"
536                 : : "a" (&refbk) : "cc" );
537 }
538
539 asmlinkage void
540 pfault_interrupt(struct pt_regs *regs, __u16 error_code)
541 {
542         struct task_struct *tsk;
543         __u16 subcode;
544
545         /*
546          * Get the external interruption subcode & pfault
547          * initial/completion signal bit. VM stores this 
548          * in the 'cpu address' field associated with the
549          * external interrupt. 
550          */
551         subcode = S390_lowcore.cpu_addr;
552         if ((subcode & 0xff00) != __SUBCODE_MASK)
553                 return;
554
555         /*
556          * Get the token (= address of the task structure of the affected task).
557          */
558         tsk = *(struct task_struct **) __LC_PFAULT_INTPARM;
559
560         if (subcode & 0x0080) {
561                 /* signal bit is set -> a page has been swapped in by VM */
562                 if (xchg(&tsk->thread.pfault_wait, -1) != 0) {
563                         /* Initial interrupt was faster than the completion
564                          * interrupt. pfault_wait is valid. Set pfault_wait
565                          * back to zero and wake up the process. This can
566                          * safely be done because the task is still sleeping
567                          * and can't procude new pfaults. */
568                         tsk->thread.pfault_wait = 0;
569                         wake_up_process(tsk);
570                 }
571         } else {
572                 /* signal bit not set -> a real page is missing. */
573                 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
574                 if (xchg(&tsk->thread.pfault_wait, 1) != 0) {
575                         /* Completion interrupt was faster than the initial
576                          * interrupt (swapped in a -1 for pfault_wait). Set
577                          * pfault_wait back to zero and exit. This can be
578                          * done safely because tsk is running in kernel 
579                          * mode and can't produce new pfaults. */
580                         tsk->thread.pfault_wait = 0;
581                         set_task_state(tsk, TASK_RUNNING);
582                 } else
583                         set_tsk_need_resched(tsk);
584         }
585 }
586 #endif
587