1 /* Page Fault Handling for ARC (TLB Miss / ProtV)
3 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 #include <linux/signal.h>
11 #include <linux/interrupt.h>
12 #include <linux/sched.h>
13 #include <linux/errno.h>
14 #include <linux/ptrace.h>
15 #include <linux/uaccess.h>
16 #include <linux/kdebug.h>
17 #include <linux/perf_event.h>
18 #include <asm/pgalloc.h>
21 static int handle_vmalloc_fault(unsigned long address
)
24 * Synchronize this task's top level page-table
25 * with the 'reference' page table.
31 pgd
= pgd_offset_fast(current
->active_mm
, address
);
32 pgd_k
= pgd_offset_k(address
);
34 if (!pgd_present(*pgd_k
))
37 pud
= pud_offset(pgd
, address
);
38 pud_k
= pud_offset(pgd_k
, address
);
39 if (!pud_present(*pud_k
))
42 pmd
= pmd_offset(pud
, address
);
43 pmd_k
= pmd_offset(pud_k
, address
);
44 if (!pmd_present(*pmd_k
))
49 /* XXX: create the TLB entry here */
56 void do_page_fault(unsigned long address
, struct pt_regs
*regs
)
58 struct vm_area_struct
*vma
= NULL
;
59 struct task_struct
*tsk
= current
;
60 struct mm_struct
*mm
= tsk
->mm
;
63 int write
= regs
->ecr_cause
& ECR_C_PROTV_STORE
; /* ST/EX */
64 unsigned int flags
= FAULT_FLAG_ALLOW_RETRY
| FAULT_FLAG_KILLABLE
;
67 * We fault-in kernel-space virtual memory on-demand. The
68 * 'reference' page table is init_mm.pgd.
70 * NOTE! We MUST NOT take any locks for this case. We may
71 * be in an interrupt or a critical region, and should
72 * only copy the information from the master page table,
75 if (address
>= VMALLOC_START
&& address
<= VMALLOC_END
) {
76 ret
= handle_vmalloc_fault(address
);
78 goto bad_area_nosemaphore
;
83 info
.si_code
= SEGV_MAPERR
;
86 * If we're in an interrupt or have no user
87 * context, we must not take the fault..
89 if (faulthandler_disabled() || !mm
)
93 flags
|= FAULT_FLAG_USER
;
95 down_read(&mm
->mmap_sem
);
96 vma
= find_vma(mm
, address
);
99 if (vma
->vm_start
<= address
)
101 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
103 if (expand_stack(vma
, address
))
107 * Ok, we have a good vm_area for this memory access, so
111 info
.si_code
= SEGV_ACCERR
;
113 /* Handle protection violation, execute on heap or stack */
115 if ((regs
->ecr_vec
== ECR_V_PROTV
) &&
116 (regs
->ecr_cause
== ECR_C_PROTV_INST_FETCH
))
120 if (!(vma
->vm_flags
& VM_WRITE
))
122 flags
|= FAULT_FLAG_WRITE
;
124 if (!(vma
->vm_flags
& (VM_READ
| VM_EXEC
)))
129 * If for any reason at all we couldn't handle the fault,
130 * make sure we exit gracefully rather than endlessly redo
133 fault
= handle_mm_fault(mm
, vma
, address
, flags
);
135 /* If Pagefault was interrupted by SIGKILL, exit page fault "early" */
136 if (unlikely(fatal_signal_pending(current
))) {
137 if ((fault
& VM_FAULT_ERROR
) && !(fault
& VM_FAULT_RETRY
))
138 up_read(&mm
->mmap_sem
);
143 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS
, 1, regs
, address
);
145 if (likely(!(fault
& VM_FAULT_ERROR
))) {
146 if (flags
& FAULT_FLAG_ALLOW_RETRY
) {
147 /* To avoid updating stats twice for retry case */
148 if (fault
& VM_FAULT_MAJOR
) {
150 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ
, 1,
154 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN
, 1,
158 if (fault
& VM_FAULT_RETRY
) {
159 flags
&= ~FAULT_FLAG_ALLOW_RETRY
;
160 flags
|= FAULT_FLAG_TRIED
;
165 /* Fault Handled Gracefully */
166 up_read(&mm
->mmap_sem
);
170 if (fault
& VM_FAULT_OOM
)
172 else if (fault
& VM_FAULT_SIGSEGV
)
174 else if (fault
& VM_FAULT_SIGBUS
)
181 * Something tried to access memory that isn't in our memory map..
182 * Fix it, but check if it's kernel or user first..
185 up_read(&mm
->mmap_sem
);
187 bad_area_nosemaphore
:
188 /* User mode accesses just cause a SIGSEGV */
189 if (user_mode(regs
)) {
190 tsk
->thread
.fault_address
= address
;
191 info
.si_signo
= SIGSEGV
;
193 /* info.si_code has been set above */
194 info
.si_addr
= (void __user
*)address
;
195 force_sig_info(SIGSEGV
, &info
, tsk
);
200 /* Are we prepared to handle this kernel fault?
202 * (The kernel has valid exception-points in the source
203 * when it acesses user-memory. When it fails in one
204 * of those points, we find it in a table and do a jump
205 * to some fixup code that loads an appropriate error
208 if (fixup_exception(regs
))
211 die("Oops", regs
, address
);
214 up_read(&mm
->mmap_sem
);
216 if (user_mode(regs
)) {
217 pagefault_out_of_memory();
224 up_read(&mm
->mmap_sem
);
226 if (!user_mode(regs
))
229 tsk
->thread
.fault_address
= address
;
230 info
.si_signo
= SIGBUS
;
232 info
.si_code
= BUS_ADRERR
;
233 info
.si_addr
= (void __user
*)address
;
234 force_sig_info(SIGBUS
, &info
, tsk
);