Merge remote-tracking branches 'asoc/topic/rockchip', 'asoc/topic/rt5514', 'asoc...
[sfrench/cifs-2.6.git] / arch / alpha / mm / fault.c
1 /*
2  *  linux/arch/alpha/mm/fault.c
3  *
4  *  Copyright (C) 1995  Linus Torvalds
5  */
6
7 #include <linux/sched/signal.h>
8 #include <linux/kernel.h>
9 #include <linux/mm.h>
10 #include <asm/io.h>
11
12 #define __EXTERN_INLINE inline
13 #include <asm/mmu_context.h>
14 #include <asm/tlbflush.h>
15 #undef  __EXTERN_INLINE
16
17 #include <linux/signal.h>
18 #include <linux/errno.h>
19 #include <linux/string.h>
20 #include <linux/types.h>
21 #include <linux/ptrace.h>
22 #include <linux/mman.h>
23 #include <linux/smp.h>
24 #include <linux/interrupt.h>
25 #include <linux/extable.h>
26 #include <linux/uaccess.h>
27
28 extern void die_if_kernel(char *,struct pt_regs *,long, unsigned long *);
29
30
31 /*
32  * Force a new ASN for a task.
33  */
34
35 #ifndef CONFIG_SMP
36 unsigned long last_asn = ASN_FIRST_VERSION;
37 #endif
38
39 void
40 __load_new_mm_context(struct mm_struct *next_mm)
41 {
42         unsigned long mmc;
43         struct pcb_struct *pcb;
44
45         mmc = __get_new_mm_context(next_mm, smp_processor_id());
46         next_mm->context[smp_processor_id()] = mmc;
47
48         pcb = &current_thread_info()->pcb;
49         pcb->asn = mmc & HARDWARE_ASN_MASK;
50         pcb->ptbr = ((unsigned long) next_mm->pgd - IDENT_ADDR) >> PAGE_SHIFT;
51
52         __reload_thread(pcb);
53 }
54
55
56 /*
57  * This routine handles page faults.  It determines the address,
58  * and the problem, and then passes it off to handle_mm_fault().
59  *
60  * mmcsr:
61  *      0 = translation not valid
62  *      1 = access violation
63  *      2 = fault-on-read
64  *      3 = fault-on-execute
65  *      4 = fault-on-write
66  *
67  * cause:
68  *      -1 = instruction fetch
69  *      0 = load
70  *      1 = store
71  *
72  * Registers $9 through $15 are saved in a block just prior to `regs' and
73  * are saved and restored around the call to allow exception code to
74  * modify them.
75  */
76
77 /* Macro for exception fixup code to access integer registers.  */
78 #define dpf_reg(r)                                                      \
79         (((unsigned long *)regs)[(r) <= 8 ? (r) : (r) <= 15 ? (r)-16 :  \
80                                  (r) <= 18 ? (r)+8 : (r)-10])
81
82 asmlinkage void
83 do_page_fault(unsigned long address, unsigned long mmcsr,
84               long cause, struct pt_regs *regs)
85 {
86         struct vm_area_struct * vma;
87         struct mm_struct *mm = current->mm;
88         const struct exception_table_entry *fixup;
89         int fault, si_code = SEGV_MAPERR;
90         siginfo_t info;
91         unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
92
93         /* As of EV6, a load into $31/$f31 is a prefetch, and never faults
94            (or is suppressed by the PALcode).  Support that for older CPUs
95            by ignoring such an instruction.  */
96         if (cause == 0) {
97                 unsigned int insn;
98                 __get_user(insn, (unsigned int __user *)regs->pc);
99                 if ((insn >> 21 & 0x1f) == 0x1f &&
100                     /* ldq ldl ldt lds ldg ldf ldwu ldbu */
101                     (1ul << (insn >> 26) & 0x30f00001400ul)) {
102                         regs->pc += 4;
103                         return;
104                 }
105         }
106
107         /* If we're in an interrupt context, or have no user context,
108            we must not take the fault.  */
109         if (!mm || faulthandler_disabled())
110                 goto no_context;
111
112 #ifdef CONFIG_ALPHA_LARGE_VMALLOC
113         if (address >= TASK_SIZE)
114                 goto vmalloc_fault;
115 #endif
116         if (user_mode(regs))
117                 flags |= FAULT_FLAG_USER;
118 retry:
119         down_read(&mm->mmap_sem);
120         vma = find_vma(mm, address);
121         if (!vma)
122                 goto bad_area;
123         if (vma->vm_start <= address)
124                 goto good_area;
125         if (!(vma->vm_flags & VM_GROWSDOWN))
126                 goto bad_area;
127         if (expand_stack(vma, address))
128                 goto bad_area;
129
130         /* Ok, we have a good vm_area for this memory access, so
131            we can handle it.  */
132  good_area:
133         si_code = SEGV_ACCERR;
134         if (cause < 0) {
135                 if (!(vma->vm_flags & VM_EXEC))
136                         goto bad_area;
137         } else if (!cause) {
138                 /* Allow reads even for write-only mappings */
139                 if (!(vma->vm_flags & (VM_READ | VM_WRITE)))
140                         goto bad_area;
141         } else {
142                 if (!(vma->vm_flags & VM_WRITE))
143                         goto bad_area;
144                 flags |= FAULT_FLAG_WRITE;
145         }
146
147         /* If for any reason at all we couldn't handle the fault,
148            make sure we exit gracefully rather than endlessly redo
149            the fault.  */
150         fault = handle_mm_fault(vma, address, flags);
151
152         if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
153                 return;
154
155         if (unlikely(fault & VM_FAULT_ERROR)) {
156                 if (fault & VM_FAULT_OOM)
157                         goto out_of_memory;
158                 else if (fault & VM_FAULT_SIGSEGV)
159                         goto bad_area;
160                 else if (fault & VM_FAULT_SIGBUS)
161                         goto do_sigbus;
162                 BUG();
163         }
164
165         if (flags & FAULT_FLAG_ALLOW_RETRY) {
166                 if (fault & VM_FAULT_MAJOR)
167                         current->maj_flt++;
168                 else
169                         current->min_flt++;
170                 if (fault & VM_FAULT_RETRY) {
171                         flags &= ~FAULT_FLAG_ALLOW_RETRY;
172
173                          /* No need to up_read(&mm->mmap_sem) as we would
174                          * have already released it in __lock_page_or_retry
175                          * in mm/filemap.c.
176                          */
177
178                         goto retry;
179                 }
180         }
181
182         up_read(&mm->mmap_sem);
183
184         return;
185
186         /* Something tried to access memory that isn't in our memory map.
187            Fix it, but check if it's kernel or user first.  */
188  bad_area:
189         up_read(&mm->mmap_sem);
190
191         if (user_mode(regs))
192                 goto do_sigsegv;
193
194  no_context:
195         /* Are we prepared to handle this fault as an exception?  */
196         if ((fixup = search_exception_tables(regs->pc)) != 0) {
197                 unsigned long newpc;
198                 newpc = fixup_exception(dpf_reg, fixup, regs->pc);
199                 regs->pc = newpc;
200                 return;
201         }
202
203         /* Oops. The kernel tried to access some bad page. We'll have to
204            terminate things with extreme prejudice.  */
205         printk(KERN_ALERT "Unable to handle kernel paging request at "
206                "virtual address %016lx\n", address);
207         die_if_kernel("Oops", regs, cause, (unsigned long*)regs - 16);
208         do_exit(SIGKILL);
209
210         /* We ran out of memory, or some other thing happened to us that
211            made us unable to handle the page fault gracefully.  */
212  out_of_memory:
213         up_read(&mm->mmap_sem);
214         if (!user_mode(regs))
215                 goto no_context;
216         pagefault_out_of_memory();
217         return;
218
219  do_sigbus:
220         up_read(&mm->mmap_sem);
221         /* Send a sigbus, regardless of whether we were in kernel
222            or user mode.  */
223         info.si_signo = SIGBUS;
224         info.si_errno = 0;
225         info.si_code = BUS_ADRERR;
226         info.si_addr = (void __user *) address;
227         force_sig_info(SIGBUS, &info, current);
228         if (!user_mode(regs))
229                 goto no_context;
230         return;
231
232  do_sigsegv:
233         info.si_signo = SIGSEGV;
234         info.si_errno = 0;
235         info.si_code = si_code;
236         info.si_addr = (void __user *) address;
237         force_sig_info(SIGSEGV, &info, current);
238         return;
239
240 #ifdef CONFIG_ALPHA_LARGE_VMALLOC
241  vmalloc_fault:
242         if (user_mode(regs))
243                 goto do_sigsegv;
244         else {
245                 /* Synchronize this task's top level page-table
246                    with the "reference" page table from init.  */
247                 long index = pgd_index(address);
248                 pgd_t *pgd, *pgd_k;
249
250                 pgd = current->active_mm->pgd + index;
251                 pgd_k = swapper_pg_dir + index;
252                 if (!pgd_present(*pgd) && pgd_present(*pgd_k)) {
253                         pgd_val(*pgd) = pgd_val(*pgd_k);
254                         return;
255                 }
256                 goto no_context;
257         }
258 #endif
259 }