xref: /netbsd-src/sys/arch/arm/arm/ast.c (revision 496e2b659f2819a14d8716cb7e5992ffb8b0455f)
1 /*	$NetBSD: ast.c,v 1.32 2021/02/01 19:31:34 skrll Exp $	*/
2 
3 /*
4  * Copyright (c) 1994,1995 Mark Brinicombe
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the RiscBSD team.
18  * 4. The name of the company nor the name of the author may be used to
19  *    endorse or promote products derived from this software without specific
20  *    prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTERS BE LIABLE FOR ANY DIRECT,
26  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * RiscBSD kernel project
35  *
36  * ast.c
37  *
38  * Code to handle ast's and returns to user mode
39  *
40  * Created      : 11/10/94
41  */
42 
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: ast.c,v 1.32 2021/02/01 19:31:34 skrll Exp $");
45 
46 #include "opt_ddb.h"
47 
48 #include <sys/param.h>
49 #include <sys/cpu.h>
50 #include <sys/proc.h>
51 #include <sys/acct.h>
52 #include <sys/systm.h>
53 #include <sys/atomic.h>
54 #include <sys/kernel.h>
55 #include <sys/signal.h>
56 #include <sys/userret.h>
57 #include <sys/vmmeter.h>
58 
59 #include <arm/locore.h>
60 
61 #include <uvm/uvm_extern.h>
62 
63 /*
64  * Prototypes
65  */
66 void ast(struct trapframe *);
67 
68 void
userret(struct lwp * l)69 userret(struct lwp *l)
70 {
71 #if defined(ARM_MMU_EXTENDED)
72 	/*
73 	 * If our ASID got released, access via TTBR0 will have been disabled.
74 	 * So if it is disabled, activate the lwp again to get a new ASID.
75 	 */
76 #ifdef __HAVE_PREEMPTION
77 	kpreempt_disable();
78 #endif
79 	KASSERTMSG(curcpu()->ci_pmap_cur == l->l_proc->p_vmspace->vm_map.pmap,
80 	    "%p vs %p", curcpu()->ci_pmap_cur,
81 	    l->l_proc->p_vmspace->vm_map.pmap);
82 	if (__predict_false(armreg_ttbcr_read() & TTBCR_S_PD0)) {
83 		pmap_activate(l);
84 	}
85 	KASSERT(!(armreg_ttbcr_read() & TTBCR_S_PD0));
86 #ifdef __HAVE_PREEMPTION
87 	kpreempt_enable();
88 #endif
89 #endif
90 
91 	/* Invoke MI userret code */
92 	mi_userret(l);
93 
94 	KASSERT(VALID_PSR(lwp_trapframe(l)->tf_spsr));
95 }
96 
97 
98 /*
99  * Handle asynchronous system traps.
100  * This is called from the irq handler to deliver signals
101  * and switch processes if required.
102  */
103 
104 void
ast(struct trapframe * tf)105 ast(struct trapframe *tf)
106 {
107 	struct lwp * const l = curlwp;
108 
109 	/* Interrupts were restored by exception_exit. */
110 
111 	KASSERT(VALID_PSR(tf->tf_spsr));
112 
113 #ifdef __HAVE_PREEMPTION
114 	kpreempt_disable();
115 #endif
116 	struct cpu_info * const ci = curcpu();
117 
118 	ci->ci_data.cpu_ntrap++;
119 
120 	KDASSERT(ci->ci_cpl == IPL_NONE);
121 #ifdef __HAVE_PREEMPTION
122 	kpreempt_enable();
123 #endif
124 
125 	if (l->l_pflag & LP_OWEUPC) {
126 		l->l_pflag &= ~LP_OWEUPC;
127 		ADDUPROF(l);
128 	}
129 
130 	userret(l);
131 }
132