vserver 1.9.3
[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 #include <linux/hardirq.h>
29
30 #include <asm/system.h>
31 #include <asm/uaccess.h>
32 #include <asm/pgtable.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 do_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
163 do_exception(struct pt_regs *regs, unsigned long error_code, int is_protection)
164 {
165         struct task_struct *tsk;
166         struct mm_struct *mm;
167         struct vm_area_struct * vma;
168         unsigned long address;
169         int user_address;
170         const struct exception_table_entry *fixup;
171         int si_code = SEGV_MAPERR;
172
173         tsk = current;
174         mm = tsk->mm;
175         
176         /* 
177          * Check for low-address protection.  This needs to be treated
178          * as a special case because the translation exception code 
179          * field is not guaranteed to contain valid data in this case.
180          */
181         if (is_protection && !(S390_lowcore.trans_exc_code & 4)) {
182
183                 /* Low-address protection hit in kernel mode means 
184                    NULL pointer write access in kernel mode.  */
185                 if (!(regs->psw.mask & PSW_MASK_PSTATE)) {
186                         address = 0;
187                         user_address = 0;
188                         goto no_context;
189                 }
190
191                 /* Low-address protection hit in user mode 'cannot happen'.  */
192                 die ("Low-address protection", regs, error_code);
193                 do_exit(SIGKILL);
194         }
195
196         /* 
197          * get the failing address 
198          * more specific the segment and page table portion of 
199          * the address 
200          */
201         address = S390_lowcore.trans_exc_code & __FAIL_ADDR_MASK;
202         user_address = check_user_space(regs, error_code);
203
204         /*
205          * Verify that the fault happened in user space, that
206          * we are not in an interrupt and that there is a 
207          * user context.
208          */
209         if (user_address == 0 || in_interrupt() || !mm)
210                 goto no_context;
211
212         /*
213          * When we get here, the fault happened in the current
214          * task's user address space, so we can switch on the
215          * interrupts again and then search the VMAs
216          */
217         local_irq_enable();
218
219         down_read(&mm->mmap_sem);
220
221         vma = find_vma(mm, address);
222         if (!vma)
223                 goto bad_area;
224         if (vma->vm_start <= address) 
225                 goto good_area;
226         if (!(vma->vm_flags & VM_GROWSDOWN))
227                 goto bad_area;
228         if (expand_stack(vma, address))
229                 goto bad_area;
230 /*
231  * Ok, we have a good vm_area for this memory access, so
232  * we can handle it..
233  */
234 good_area:
235         si_code = SEGV_ACCERR;
236         if (!is_protection) {
237                 /* page not present, check vm flags */
238                 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
239                         goto bad_area;
240         } else {
241                 if (!(vma->vm_flags & VM_WRITE))
242                         goto bad_area;
243         }
244
245 survive:
246         /*
247          * If for any reason at all we couldn't handle the fault,
248          * make sure we exit gracefully rather than endlessly redo
249          * the fault.
250          */
251         switch (handle_mm_fault(mm, vma, address, is_protection)) {
252         case VM_FAULT_MINOR:
253                 tsk->min_flt++;
254                 break;
255         case VM_FAULT_MAJOR:
256                 tsk->maj_flt++;
257                 break;
258         case VM_FAULT_SIGBUS:
259                 goto do_sigbus;
260         case VM_FAULT_OOM:
261                 goto out_of_memory;
262         default:
263                 BUG();
264         }
265
266         up_read(&mm->mmap_sem);
267         /*
268          * The instruction that caused the program check will
269          * be repeated. Don't signal single step via SIGTRAP.
270          */
271         clear_tsk_thread_flag(current, TIF_SINGLE_STEP);
272         return;
273
274 /*
275  * Something tried to access memory that isn't in our memory map..
276  * Fix it, but check if it's kernel or user first..
277  */
278 bad_area:
279         up_read(&mm->mmap_sem);
280
281         /* User mode accesses just cause a SIGSEGV */
282         if (regs->psw.mask & PSW_MASK_PSTATE) {
283                 tsk->thread.prot_addr = address;
284                 tsk->thread.trap_no = error_code;
285                 do_sigsegv(regs, error_code, si_code, address);
286                 return;
287         }
288
289 no_context:
290         /* Are we prepared to handle this kernel fault?  */
291         fixup = search_exception_tables(regs->psw.addr & __FIXUP_MASK);
292         if (fixup) {
293                 regs->psw.addr = fixup->fixup | PSW_ADDR_AMODE;
294                 return;
295         }
296
297 /*
298  * Oops. The kernel tried to access some bad page. We'll have to
299  * terminate things with extreme prejudice.
300  */
301         if (user_address == 0)
302                 printk(KERN_ALERT "Unable to handle kernel pointer dereference"
303                        " at virtual kernel address %p\n", (void *)address);
304         else
305                 printk(KERN_ALERT "Unable to handle kernel paging request"
306                        " at virtual user address %p\n", (void *)address);
307
308         die("Oops", regs, error_code);
309         do_exit(SIGKILL);
310
311
312 /*
313  * We ran out of memory, or some other thing happened to us that made
314  * us unable to handle the page fault gracefully.
315 */
316 out_of_memory:
317         up_read(&mm->mmap_sem);
318         if (tsk->pid == 1) {
319                 yield();
320                 goto survive;
321         }
322         printk("VM: killing process %s\n", tsk->comm);
323         if (regs->psw.mask & PSW_MASK_PSTATE)
324                 do_exit(SIGKILL);
325         goto no_context;
326
327 do_sigbus:
328         up_read(&mm->mmap_sem);
329
330         /*
331          * Send a sigbus, regardless of whether we were in kernel
332          * or user mode.
333          */
334         tsk->thread.prot_addr = address;
335         tsk->thread.trap_no = error_code;
336         force_sig(SIGBUS, tsk);
337
338         /* Kernel mode? Handle exceptions or die */
339         if (!(regs->psw.mask & PSW_MASK_PSTATE))
340                 goto no_context;
341 }
342
343 void do_protection_exception(struct pt_regs *regs, unsigned long error_code)
344 {
345         regs->psw.addr -= (error_code >> 16);
346         do_exception(regs, 4, 1);
347 }
348
349 void do_dat_exception(struct pt_regs *regs, unsigned long error_code)
350 {
351         do_exception(regs, error_code & 0xff, 0);
352 }
353
354 #ifndef CONFIG_ARCH_S390X
355
356 typedef struct _pseudo_wait_t {
357        struct _pseudo_wait_t *next;
358        wait_queue_head_t queue;
359        unsigned long address;
360        int resolved;
361 } pseudo_wait_t;
362
363 static pseudo_wait_t *pseudo_lock_queue = NULL;
364 static spinlock_t pseudo_wait_spinlock; /* spinlock to protect lock queue */
365
366 /*
367  * This routine handles 'pagex' pseudo page faults.
368  */
369 asmlinkage void
370 do_pseudo_page_fault(struct pt_regs *regs, unsigned long error_code)
371 {
372         pseudo_wait_t wait_struct;
373         pseudo_wait_t *ptr, *last, *next;
374         unsigned long address;
375
376         /*
377          * get the failing address
378          * more specific the segment and page table portion of
379          * the address
380          */
381         address = S390_lowcore.trans_exc_code & 0xfffff000;
382
383         if (address & 0x80000000) {
384                 /* high bit set -> a page has been swapped in by VM */
385                 address &= 0x7fffffff;
386                 spin_lock(&pseudo_wait_spinlock);
387                 last = NULL;
388                 ptr = pseudo_lock_queue;
389                 while (ptr != NULL) {
390                         next = ptr->next;
391                         if (address == ptr->address) {
392                                  /*
393                                  * This is one of the processes waiting
394                                  * for the page. Unchain from the queue.
395                                  * There can be more than one process
396                                  * waiting for the same page. VM presents
397                                  * an initial and a completion interrupt for
398                                  * every process that tries to access a 
399                                  * page swapped out by VM. 
400                                  */
401                                 if (last == NULL)
402                                         pseudo_lock_queue = next;
403                                 else
404                                         last->next = next;
405                                 /* now wake up the process */
406                                 ptr->resolved = 1;
407                                 wake_up(&ptr->queue);
408                         } else
409                                 last = ptr;
410                         ptr = next;
411                 }
412                 spin_unlock(&pseudo_wait_spinlock);
413         } else {
414                 /* Pseudo page faults in kernel mode is a bad idea */
415                 if (!(regs->psw.mask & PSW_MASK_PSTATE)) {
416                         /*
417                          * VM presents pseudo page faults if the interrupted
418                          * state was not disabled for interrupts. So we can
419                          * get pseudo page fault interrupts while running
420                          * in kernel mode. We simply access the page here
421                          * while we are running disabled. VM will then swap
422                          * in the page synchronously.
423                          */
424                          if (check_user_space(regs, error_code) == 0)
425                                  /* dereference a virtual kernel address */
426                                  __asm__ __volatile__ (
427                                          "  ic 0,0(%0)"
428                                          : : "a" (address) : "0");
429                          else
430                                  /* dereference a virtual user address */
431                                  __asm__ __volatile__ (
432                                          "  la   2,0(%0)\n"
433                                          "  sacf 512\n"
434                                          "  ic   2,0(2)\n"
435                                          "0:sacf 0\n"
436                                          ".section __ex_table,\"a\"\n"
437                                          "  .align 4\n"
438                                          "  .long  0b,0b\n"
439                                          ".previous"
440                                          : : "a" (address) : "2" );
441
442                         return;
443                 }
444                 /* initialize and add element to pseudo_lock_queue */
445                 init_waitqueue_head (&wait_struct.queue);
446                 wait_struct.address = address;
447                 wait_struct.resolved = 0;
448                 spin_lock(&pseudo_wait_spinlock);
449                 wait_struct.next = pseudo_lock_queue;
450                 pseudo_lock_queue = &wait_struct;
451                 spin_unlock(&pseudo_wait_spinlock);
452                 /*
453                  * The instruction that caused the program check will
454                  * be repeated. Don't signal single step via SIGTRAP.
455                  */
456                 clear_tsk_thread_flag(current, TIF_SINGLE_STEP);
457                 /* go to sleep */
458                 wait_event(wait_struct.queue, wait_struct.resolved);
459         }
460 }
461 #endif /* CONFIG_ARCH_S390X */
462
463 #ifdef CONFIG_PFAULT 
464 /*
465  * 'pfault' pseudo page faults routines.
466  */
467 static int pfault_disable = 0;
468
469 static int __init nopfault(char *str)
470 {
471         pfault_disable = 1;
472         return 1;
473 }
474
475 __setup("nopfault", nopfault);
476
477 typedef struct {
478         __u16 refdiagc;
479         __u16 reffcode;
480         __u16 refdwlen;
481         __u16 refversn;
482         __u64 refgaddr;
483         __u64 refselmk;
484         __u64 refcmpmk;
485         __u64 reserved;
486 } __attribute__ ((packed)) pfault_refbk_t;
487
488 int pfault_init(void)
489 {
490         pfault_refbk_t refbk =
491                 { 0x258, 0, 5, 2, __LC_CURRENT, 1ULL << 48, 1ULL << 48,
492                   __PF_RES_FIELD };
493         int rc;
494
495         if (pfault_disable)
496                 return -1;
497         __asm__ __volatile__(
498                 "    diag  %1,%0,0x258\n"
499                 "0:  j     2f\n"
500                 "1:  la    %0,8\n"
501                 "2:\n"
502                 ".section __ex_table,\"a\"\n"
503                 "   .align 4\n"
504 #ifndef CONFIG_ARCH_S390X
505                 "   .long  0b,1b\n"
506 #else /* CONFIG_ARCH_S390X */
507                 "   .quad  0b,1b\n"
508 #endif /* CONFIG_ARCH_S390X */
509                 ".previous"
510                 : "=d" (rc) : "a" (&refbk) : "cc" );
511         __ctl_set_bit(0, 9);
512         return rc;
513 }
514
515 void pfault_fini(void)
516 {
517         pfault_refbk_t refbk =
518         { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL };
519
520         if (pfault_disable)
521                 return;
522         __ctl_clear_bit(0,9);
523         __asm__ __volatile__(
524                 "    diag  %0,0,0x258\n"
525                 "0:\n"
526                 ".section __ex_table,\"a\"\n"
527                 "   .align 4\n"
528 #ifndef CONFIG_ARCH_S390X
529                 "   .long  0b,0b\n"
530 #else /* CONFIG_ARCH_S390X */
531                 "   .quad  0b,0b\n"
532 #endif /* CONFIG_ARCH_S390X */
533                 ".previous"
534                 : : "a" (&refbk) : "cc" );
535 }
536
537 asmlinkage void
538 pfault_interrupt(struct pt_regs *regs, __u16 error_code)
539 {
540         struct task_struct *tsk;
541         __u16 subcode;
542
543         /*
544          * Get the external interruption subcode & pfault
545          * initial/completion signal bit. VM stores this 
546          * in the 'cpu address' field associated with the
547          * external interrupt. 
548          */
549         subcode = S390_lowcore.cpu_addr;
550         if ((subcode & 0xff00) != __SUBCODE_MASK)
551                 return;
552
553         /*
554          * Get the token (= address of the task structure of the affected task).
555          */
556         tsk = *(struct task_struct **) __LC_PFAULT_INTPARM;
557
558         if (subcode & 0x0080) {
559                 /* signal bit is set -> a page has been swapped in by VM */
560                 if (xchg(&tsk->thread.pfault_wait, -1) != 0) {
561                         /* Initial interrupt was faster than the completion
562                          * interrupt. pfault_wait is valid. Set pfault_wait
563                          * back to zero and wake up the process. This can
564                          * safely be done because the task is still sleeping
565                          * and can't procude new pfaults. */
566                         tsk->thread.pfault_wait = 0;
567                         wake_up_process(tsk);
568                 }
569         } else {
570                 /* signal bit not set -> a real page is missing. */
571                 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
572                 if (xchg(&tsk->thread.pfault_wait, 1) != 0) {
573                         /* Completion interrupt was faster than the initial
574                          * interrupt (swapped in a -1 for pfault_wait). Set
575                          * pfault_wait back to zero and exit. This can be
576                          * done safely because tsk is running in kernel 
577                          * mode and can't produce new pfaults. */
578                         tsk->thread.pfault_wait = 0;
579                         set_task_state(tsk, TASK_RUNNING);
580                 } else
581                         set_tsk_need_resched(tsk);
582         }
583 }
584 #endif
585