1 // SPDX-License-Identifier: GPL-2.0
3 * linux/arch/alpha/mm/fault.c
5 * Copyright (C) 1995 Linus Torvalds
8 #include <linux/sched/signal.h>
9 #include <linux/kernel.h>
13 #define __EXTERN_INLINE inline
14 #include <asm/mmu_context.h>
15 #include <asm/tlbflush.h>
16 #undef __EXTERN_INLINE
18 #include <linux/signal.h>
19 #include <linux/errno.h>
20 #include <linux/string.h>
21 #include <linux/types.h>
22 #include <linux/ptrace.h>
23 #include <linux/mman.h>
24 #include <linux/smp.h>
25 #include <linux/interrupt.h>
26 #include <linux/extable.h>
27 #include <linux/uaccess.h>
29 extern void die_if_kernel(char *,struct pt_regs
*,long, unsigned long *);
33 * Force a new ASN for a task.
37 unsigned long last_asn
= ASN_FIRST_VERSION
;
41 __load_new_mm_context(struct mm_struct
*next_mm
)
44 struct pcb_struct
*pcb
;
46 mmc
= __get_new_mm_context(next_mm
, smp_processor_id());
47 next_mm
->context
[smp_processor_id()] = mmc
;
49 pcb
= ¤t_thread_info()->pcb
;
50 pcb
->asn
= mmc
& HARDWARE_ASN_MASK
;
51 pcb
->ptbr
= ((unsigned long) next_mm
->pgd
- IDENT_ADDR
) >> PAGE_SHIFT
;
58 * This routine handles page faults. It determines the address,
59 * and the problem, and then passes it off to handle_mm_fault().
62 * 0 = translation not valid
63 * 1 = access violation
65 * 3 = fault-on-execute
69 * -1 = instruction fetch
73 * Registers $9 through $15 are saved in a block just prior to `regs' and
74 * are saved and restored around the call to allow exception code to
78 /* Macro for exception fixup code to access integer registers. */
80 (((unsigned long *)regs)[(r) <= 8 ? (r) : (r) <= 15 ? (r)-16 : \
81 (r) <= 18 ? (r)+10 : (r)-10])
84 do_page_fault(unsigned long address
, unsigned long mmcsr
,
85 long cause
, struct pt_regs
*regs
)
87 struct vm_area_struct
* vma
;
88 struct mm_struct
*mm
= current
->mm
;
89 const struct exception_table_entry
*fixup
;
90 int si_code
= SEGV_MAPERR
;
92 unsigned int flags
= FAULT_FLAG_ALLOW_RETRY
| FAULT_FLAG_KILLABLE
;
94 /* As of EV6, a load into $31/$f31 is a prefetch, and never faults
95 (or is suppressed by the PALcode). Support that for older CPUs
96 by ignoring such an instruction. */
99 __get_user(insn
, (unsigned int __user
*)regs
->pc
);
100 if ((insn
>> 21 & 0x1f) == 0x1f &&
101 /* ldq ldl ldt lds ldg ldf ldwu ldbu */
102 (1ul << (insn
>> 26) & 0x30f00001400ul
)) {
108 /* If we're in an interrupt context, or have no user context,
109 we must not take the fault. */
110 if (!mm
|| faulthandler_disabled())
113 #ifdef CONFIG_ALPHA_LARGE_VMALLOC
114 if (address
>= TASK_SIZE
)
118 flags
|= FAULT_FLAG_USER
;
120 down_read(&mm
->mmap_sem
);
121 vma
= find_vma(mm
, address
);
124 if (vma
->vm_start
<= address
)
126 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
128 if (expand_stack(vma
, address
))
131 /* Ok, we have a good vm_area for this memory access, so
134 si_code
= SEGV_ACCERR
;
136 if (!(vma
->vm_flags
& VM_EXEC
))
139 /* Allow reads even for write-only mappings */
140 if (!(vma
->vm_flags
& (VM_READ
| VM_WRITE
)))
143 if (!(vma
->vm_flags
& VM_WRITE
))
145 flags
|= FAULT_FLAG_WRITE
;
148 /* If for any reason at all we couldn't handle the fault,
149 make sure we exit gracefully rather than endlessly redo
151 fault
= handle_mm_fault(vma
, address
, flags
);
153 if ((fault
& VM_FAULT_RETRY
) && fatal_signal_pending(current
))
156 if (unlikely(fault
& VM_FAULT_ERROR
)) {
157 if (fault
& VM_FAULT_OOM
)
159 else if (fault
& VM_FAULT_SIGSEGV
)
161 else if (fault
& VM_FAULT_SIGBUS
)
166 if (flags
& FAULT_FLAG_ALLOW_RETRY
) {
167 if (fault
& VM_FAULT_MAJOR
)
171 if (fault
& VM_FAULT_RETRY
) {
172 flags
&= ~FAULT_FLAG_ALLOW_RETRY
;
174 /* No need to up_read(&mm->mmap_sem) as we would
175 * have already released it in __lock_page_or_retry
183 up_read(&mm
->mmap_sem
);
187 /* Something tried to access memory that isn't in our memory map.
188 Fix it, but check if it's kernel or user first. */
190 up_read(&mm
->mmap_sem
);
196 /* Are we prepared to handle this fault as an exception? */
197 if ((fixup
= search_exception_tables(regs
->pc
)) != 0) {
199 newpc
= fixup_exception(dpf_reg
, fixup
, regs
->pc
);
204 /* Oops. The kernel tried to access some bad page. We'll have to
205 terminate things with extreme prejudice. */
206 printk(KERN_ALERT
"Unable to handle kernel paging request at "
207 "virtual address %016lx\n", address
);
208 die_if_kernel("Oops", regs
, cause
, (unsigned long*)regs
- 16);
211 /* We ran out of memory, or some other thing happened to us that
212 made us unable to handle the page fault gracefully. */
214 up_read(&mm
->mmap_sem
);
215 if (!user_mode(regs
))
217 pagefault_out_of_memory();
221 up_read(&mm
->mmap_sem
);
222 /* Send a sigbus, regardless of whether we were in kernel
224 force_sig_fault(SIGBUS
, BUS_ADRERR
, (void __user
*) address
, 0);
225 if (!user_mode(regs
))
230 force_sig_fault(SIGSEGV
, si_code
, (void __user
*) address
, 0);
233 #ifdef CONFIG_ALPHA_LARGE_VMALLOC
238 /* Synchronize this task's top level page-table
239 with the "reference" page table from init. */
240 long index
= pgd_index(address
);
243 pgd
= current
->active_mm
->pgd
+ index
;
244 pgd_k
= swapper_pg_dir
+ index
;
245 if (!pgd_present(*pgd
) && pgd_present(*pgd_k
)) {
246 pgd_val(*pgd
) = pgd_val(*pgd_k
);