1 /* $NetBSD: userret.h,v 1.33 2020/03/26 20:19:06 ad Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2000, 2003, 2006, 2008, 2019, 2020
5 * The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Charles M. Hannum, and Andrew Doran.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*-
34 * Copyright (c) 1990 The Regents of the University of California.
35 * All rights reserved.
36 *
37 * This code is derived from software contributed to Berkeley by
38 * William Jolitz.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 */
65
66 #ifndef _SYS_USERRET_H_
67 #define _SYS_USERRET_H_
68
69 #include <sys/lockdebug.h>
70 #include <sys/intr.h>
71 #include <sys/psref.h>
72
73 /*
74 * Define the MI code needed before returning to user mode, for trap and
75 * syscall.
76 *
77 * We handle "exceptional" events: pending signals, stop/exit actions, etc.
78 * Note that the event must be flagged BEFORE any AST is posted as we are
79 * reading unlocked.
80 */
81 static __inline void
mi_userret(struct lwp * l)82 mi_userret(struct lwp *l)
83 {
84 struct cpu_info *ci;
85
86 KPREEMPT_DISABLE(l);
87 ci = l->l_cpu;
88 KASSERTMSG(ci->ci_biglock_count == 0, "kernel_lock leaked");
89 KASSERT(l->l_blcnt == 0);
90 if (__predict_false(ci->ci_want_resched)) {
91 preempt();
92 ci = l->l_cpu;
93 }
94 if (__predict_false(l->l_flag & LW_USERRET)) {
95 KPREEMPT_ENABLE(l);
96 lwp_userret(l);
97 KPREEMPT_DISABLE(l);
98 ci = l->l_cpu;
99 }
100 /*
101 * lwp_eprio() is too involved to use here unlocked. At this point
102 * it only matters for PTHREAD_PRIO_PROTECT; setting a too low value
103 * is OK because the scheduler will find out the true value if we
104 * end up in mi_switch().
105 *
106 * This is being called on every syscall and trap, and remote CPUs
107 * regularly look at ci_schedstate. Keep the cache line in the
108 * SHARED state by only updating spc_curpriority if it has changed.
109 */
110 l->l_kpriority = false;
111 if (ci->ci_schedstate.spc_curpriority != l->l_priority) {
112 ci->ci_schedstate.spc_curpriority = l->l_priority;
113 }
114 KPREEMPT_ENABLE(l);
115
116 LOCKDEBUG_BARRIER(NULL, 0);
117 KASSERT(l->l_nopreempt == 0);
118 PSREF_DEBUG_BARRIER();
119 KASSERT(l->l_psrefs == 0);
120 }
121
122 #endif /* !_SYS_USERRET_H_ */
123