15b81b6b3SRodney W. Grimes /*-
2df57947fSPedro F. Giffuni * SPDX-License-Identifier: BSD-4-Clause
3df57947fSPedro F. Giffuni *
43c256f53SDavid Greenman * Copyright (C) 1994, David Greenman
53c256f53SDavid Greenman * Copyright (c) 1990, 1993
63c256f53SDavid Greenman * The Regents of the University of California. All rights reserved.
7c46771a7SKonstantin Belousov * Copyright (c) 2007, 2022 The FreeBSD Foundation
85b81b6b3SRodney W. Grimes *
95b81b6b3SRodney W. Grimes * This code is derived from software contributed to Berkeley by
105b81b6b3SRodney W. Grimes * the University of Utah, and William Jolitz.
115b81b6b3SRodney W. Grimes *
12d07f36b0SJoseph Koshy * Portions of this software were developed by A. Joseph Koshy under
13d07f36b0SJoseph Koshy * sponsorship from the FreeBSD Foundation and Google, Inc.
14d07f36b0SJoseph Koshy *
155b81b6b3SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
165b81b6b3SRodney W. Grimes * modification, are permitted provided that the following conditions
175b81b6b3SRodney W. Grimes * are met:
185b81b6b3SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
195b81b6b3SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
205b81b6b3SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
215b81b6b3SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
225b81b6b3SRodney W. Grimes * documentation and/or other materials provided with the distribution.
235b81b6b3SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software
245b81b6b3SRodney W. Grimes * must display the following acknowledgement:
255b81b6b3SRodney W. Grimes * This product includes software developed by the University of
265b81b6b3SRodney W. Grimes * California, Berkeley and its contributors.
275b81b6b3SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors
285b81b6b3SRodney W. Grimes * may be used to endorse or promote products derived from this software
295b81b6b3SRodney W. Grimes * without specific prior written permission.
305b81b6b3SRodney W. Grimes *
315b81b6b3SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
325b81b6b3SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
335b81b6b3SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
345b81b6b3SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
355b81b6b3SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
365b81b6b3SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
375b81b6b3SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
385b81b6b3SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
395b81b6b3SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
405b81b6b3SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
415b81b6b3SRodney W. Grimes * SUCH DAMAGE.
425b81b6b3SRodney W. Grimes */
435b81b6b3SRodney W. Grimes
44677b542eSDavid E. O'Brien #include <sys/cdefs.h>
45f5f9340bSFabien Thomas #include "opt_hwpmc_hooks.h"
46db6a20e2SGarrett Wollman
4726f9a767SRodney W. Grimes #include <sys/param.h>
4826f9a767SRodney W. Grimes #include <sys/kernel.h>
49c46771a7SKonstantin Belousov #include <sys/limits.h>
507aa7260eSJohn Baldwin #include <sys/lock.h>
51b0f71f1bSMark Johnston #include <sys/msan.h>
5235e0e5b3SJohn Baldwin #include <sys/mutex.h>
537aa7260eSJohn Baldwin #include <sys/proc.h>
54e602ba25SJulian Elischer #include <sys/ktr.h>
5521e52415SBruce Evans #include <sys/resourcevar.h>
56b43179fbSJeff Roberson #include <sys/sched.h>
57afe1a688SKonstantin Belousov #include <sys/syscall.h>
58fc0de8f0SJohn Baldwin #include <sys/syscallsubr.h>
59afe1a688SKonstantin Belousov #include <sys/sysent.h>
607aa7260eSJohn Baldwin #include <sys/systm.h>
61efeaf95aSDavid Greenman #include <sys/vmmeter.h>
62aeaead20SPeter Wemm
6326f9a767SRodney W. Grimes #include <machine/cpu.h>
6426f9a767SRodney W. Grimes
65bf9ce95bSBjoern A. Zeeb #ifdef VIMAGE
66bf9ce95bSBjoern A. Zeeb #include <net/vnet.h>
67bf9ce95bSBjoern A. Zeeb #endif
68bf9ce95bSBjoern A. Zeeb
69f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
70f5f9340bSFabien Thomas #include <sys/pmckern.h>
71f5f9340bSFabien Thomas #endif
72f5f9340bSFabien Thomas
73c46771a7SKonstantin Belousov #ifdef EPOCH_TRACE
74c46771a7SKonstantin Belousov #include <sys/epoch.h>
75c46771a7SKonstantin Belousov #endif
76aed55708SRobert Watson
77*b7b78c1cSRandall Stewart volatile uint32_t __read_frequently hpts_that_need_softclock = 0;
78*b7b78c1cSRandall Stewart
79e3cbc572SGleb Smirnoff void (*tcp_hpts_softclock)(void);
80e3cbc572SGleb Smirnoff
817aa7260eSJohn Baldwin /*
820c14ff0eSRobert Watson * Define the code needed before returning to user mode, for trap and
830c14ff0eSRobert Watson * syscall.
847aa7260eSJohn Baldwin */
85625c76dbSJohn Baldwin void
userret(struct thread * td,struct trapframe * frame)86eb2da9a5SPoul-Henning Kamp userret(struct thread *td, struct trapframe *frame)
873c256f53SDavid Greenman {
88b40ce416SJulian Elischer struct proc *p = td->td_proc;
893c256f53SDavid Greenman
90e602ba25SJulian Elischer CTR3(KTR_SYSC, "userret: thread %p (pid %d, %s)", td, p->p_pid,
91e01eafefSJulian Elischer td->td_name);
9224f3dcfeSKonstantin Belousov KASSERT((p->p_flag & P_WEXIT) == 0,
9324f3dcfeSKonstantin Belousov ("Exiting process returns to usermode"));
9416df17d0SRobert Watson #ifdef DIAGNOSTIC
958f01bee4SKonstantin Belousov /*
968f01bee4SKonstantin Belousov * Check that we called signotify() enough. For
978f01bee4SKonstantin Belousov * multi-threaded processes, where signal distribution might
988f01bee4SKonstantin Belousov * change due to other threads changing sigmask, the check is
998f01bee4SKonstantin Belousov * racy and cannot be performed reliably.
10077d68094SKonstantin Belousov * If current process is vfork child, indicated by P_PPWAIT, then
10177d68094SKonstantin Belousov * issignal() ignores stops, so we block the check to avoid
10277d68094SKonstantin Belousov * classifying pending signals.
1038f01bee4SKonstantin Belousov */
1048f01bee4SKonstantin Belousov if (p->p_numthreads == 1) {
10506c836bbSJohn Baldwin PROC_LOCK(p);
106982d11f8SJeff Roberson thread_lock(td);
107146fc63fSKonstantin Belousov if ((p->p_flag & P_PPWAIT) == 0 &&
108c6d31b83SKonstantin Belousov (td->td_pflags & TDP_SIGFASTBLOCK) == 0 &&
109c6d31b83SKonstantin Belousov SIGPENDING(td) && !td_ast_pending(td, TDA_AST) &&
110c6d31b83SKonstantin Belousov !td_ast_pending(td, TDA_SIG)) {
111146fc63fSKonstantin Belousov thread_unlock(td);
112146fc63fSKonstantin Belousov panic(
113c6d31b83SKonstantin Belousov "failed to set signal flags for ast p %p "
114c6d31b83SKonstantin Belousov "td %p td_ast %#x fl %#x",
115c6d31b83SKonstantin Belousov p, td, td->td_ast, td->td_flags);
11677d68094SKonstantin Belousov }
117982d11f8SJeff Roberson thread_unlock(td);
118688ebe12SJohn Baldwin PROC_UNLOCK(p);
1198f01bee4SKonstantin Belousov }
1206b286ee8SKonstantin Belousov #endif
121a113b17fSKonstantin Belousov
12279065dbaSBruce Evans /*
1236f8132a8SJulian Elischer * Charge system time if profiling.
1246f8132a8SJulian Elischer */
1250e84a878SMateusz Guzik if (__predict_false(p->p_flag & P_PROFIL))
126eb2da9a5SPoul-Henning Kamp addupc_task(td, TRAPF_PC(frame), td->td_pticks * psratio);
1275de96e33SMatt Macy
1285de96e33SMatt Macy #ifdef HWPMC_HOOKS
1295de96e33SMatt Macy if (PMC_THREAD_HAS_SAMPLES(td))
1305de96e33SMatt Macy PMC_CALL_HOOK(td, PMC_FN_THR_USERRET, NULL);
1315de96e33SMatt Macy #endif
132d7955cc0SRandall Stewart /*
133e3cbc572SGleb Smirnoff * Calling tcp_hpts_softclock() here allows us to avoid frequent,
134e3cbc572SGleb Smirnoff * expensive callouts that trash the cache and lead to a much higher
135e3cbc572SGleb Smirnoff * number of interrupts and context switches. Testing on busy web
136e3cbc572SGleb Smirnoff * servers at Netflix has shown that this improves CPU use by 7% over
137e3cbc572SGleb Smirnoff * relying only on callouts to drive HPTS, and also results in idle
138e3cbc572SGleb Smirnoff * power savings on mostly idle servers.
139e3cbc572SGleb Smirnoff * This was inspired by the paper "Soft Timers: Efficient Microsecond
140e3cbc572SGleb Smirnoff * Software Timer Support for Network Processing"
141e3cbc572SGleb Smirnoff * by Mohit Aron and Peter Druschel.
142d7955cc0SRandall Stewart */
143e3cbc572SGleb Smirnoff tcp_hpts_softclock();
1446a987020SJeff Roberson /*
1456a987020SJeff Roberson * Let the scheduler adjust our priority etc.
1466a987020SJeff Roberson */
1476a987020SJeff Roberson sched_userret(td);
14816cbf13bSAttilio Rao
14916cbf13bSAttilio Rao /*
15016cbf13bSAttilio Rao * Check for misbehavior.
1515584e917SAttilio Rao *
1525584e917SAttilio Rao * In case there is a callchain tracing ongoing because of
1535584e917SAttilio Rao * hwpmc(4), skip the scheduler pinning check.
1545584e917SAttilio Rao * hwpmc(4) subsystem, infact, will collect callchain informations
1555584e917SAttilio Rao * at ast() checkpoint, which is past userret().
15616cbf13bSAttilio Rao */
15716cbf13bSAttilio Rao WITNESS_WARN(WARN_PANIC, NULL, "userret: returning");
15816cbf13bSAttilio Rao KASSERT(td->td_critnest == 0,
15916cbf13bSAttilio Rao ("userret: Returning in a critical section"));
16061ef09d1SJeff Roberson KASSERT(td->td_locks == 0,
16116cbf13bSAttilio Rao ("userret: Returning with %d locks held", td->td_locks));
162e7a9eed7SAttilio Rao KASSERT(td->td_rw_rlocks == 0,
163e7a9eed7SAttilio Rao ("userret: Returning with %d rwlocks held in read mode",
164e7a9eed7SAttilio Rao td->td_rw_rlocks));
1652466d12bSMateusz Guzik KASSERT(td->td_sx_slocks == 0,
1662466d12bSMateusz Guzik ("userret: Returning with %d sx locks held in shared mode",
1672466d12bSMateusz Guzik td->td_sx_slocks));
1680db7afd0SAndriy Gapon KASSERT(td->td_lk_slocks == 0,
1690db7afd0SAndriy Gapon ("userret: Returning with %d lockmanager locks held in shared mode",
1700db7afd0SAndriy Gapon td->td_lk_slocks));
17116cbf13bSAttilio Rao KASSERT((td->td_pflags & TDP_NOFAULTING) == 0,
17216cbf13bSAttilio Rao ("userret: Returning with pagefaults disabled"));
1735757b59fSGleb Smirnoff if (__predict_false(!THREAD_CAN_SLEEP())) {
1745757b59fSGleb Smirnoff #ifdef EPOCH_TRACE
1755757b59fSGleb Smirnoff epoch_trace_list(curthread);
1765757b59fSGleb Smirnoff #endif
177022c2f55SGleb Smirnoff KASSERT(0, ("userret: Returning with sleep disabled"));
1785757b59fSGleb Smirnoff }
1795584e917SAttilio Rao KASSERT(td->td_pinned == 0 || (td->td_pflags & TDP_CALLCHAIN) != 0,
180c159f767SGordon Bergling ("userret: Returning with pinned thread"));
181b52d50cfSMateusz Guzik KASSERT(td->td_vp_reserved == NULL,
182b52d50cfSMateusz Guzik ("userret: Returning with preallocated vnode"));
1833a1e5dd8SKonstantin Belousov KASSERT((td->td_flags & (TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)) == 0,
184593efaf9SJohn Baldwin ("userret: Returning with stop signals deferred"));
185ed9e8bc4SKonstantin Belousov KASSERT(td->td_vslock_sz == 0,
186ed9e8bc4SKonstantin Belousov ("userret: Returning with vslock-wired space"));
187bf9ce95bSBjoern A. Zeeb #ifdef VIMAGE
188bf9ce95bSBjoern A. Zeeb /* Unfortunately td_vnet_lpush needs VNET_DEBUG. */
189bf9ce95bSBjoern A. Zeeb VNET_ASSERT(curvnet == NULL,
190bf9ce95bSBjoern A. Zeeb ("%s: Returning on td %p (pid %d, %s) with vnet %p set in %s",
191bf9ce95bSBjoern A. Zeeb __func__, td, p->p_pid, td->td_name, curvnet,
192bf9ce95bSBjoern A. Zeeb (td->td_vnet_lpush != NULL) ? td->td_vnet_lpush : "N/A"));
193bf9ce95bSBjoern A. Zeeb #endif
1943c256f53SDavid Greenman }
1955b81b6b3SRodney W. Grimes
196c6d31b83SKonstantin Belousov static void
ast_prep(struct thread * td,int tda __unused)197c6d31b83SKonstantin Belousov ast_prep(struct thread *td, int tda __unused)
198c6d31b83SKonstantin Belousov {
199c6d31b83SKonstantin Belousov VM_CNT_INC(v_trap);
200c6d31b83SKonstantin Belousov td->td_pticks = 0;
201c6d31b83SKonstantin Belousov if (td->td_cowgen != atomic_load_int(&td->td_proc->p_cowgen))
202c6d31b83SKonstantin Belousov thread_cow_update(td);
203c6d31b83SKonstantin Belousov
204c6d31b83SKonstantin Belousov }
205c6d31b83SKonstantin Belousov
206c6d31b83SKonstantin Belousov struct ast_entry {
207c6d31b83SKonstantin Belousov int ae_flags;
208c6d31b83SKonstantin Belousov int ae_tdp;
209c6d31b83SKonstantin Belousov void (*ae_f)(struct thread *td, int ast);
210c6d31b83SKonstantin Belousov };
211c6d31b83SKonstantin Belousov
212c6d31b83SKonstantin Belousov _Static_assert(TDAI(TDA_MAX) <= UINT_MAX, "Too many ASTs");
213c6d31b83SKonstantin Belousov
214c6d31b83SKonstantin Belousov static struct ast_entry ast_entries[TDA_MAX] __read_mostly = {
215c6d31b83SKonstantin Belousov [TDA_AST] = { .ae_f = ast_prep, .ae_flags = ASTR_UNCOND},
216c6d31b83SKonstantin Belousov };
217c6d31b83SKonstantin Belousov
218c6d31b83SKonstantin Belousov void
ast_register(int ast,int flags,int tdp,void (* f)(struct thread *,int asts))219c6d31b83SKonstantin Belousov ast_register(int ast, int flags, int tdp,
220c6d31b83SKonstantin Belousov void (*f)(struct thread *, int asts))
221c6d31b83SKonstantin Belousov {
222c6d31b83SKonstantin Belousov struct ast_entry *ae;
223c6d31b83SKonstantin Belousov
224c6d31b83SKonstantin Belousov MPASS(ast < TDA_MAX);
225c6d31b83SKonstantin Belousov MPASS((flags & ASTR_TDP) == 0 || ((flags & ASTR_ASTF_REQUIRED) != 0
226c6d31b83SKonstantin Belousov && __bitcount(tdp) == 1));
227c6d31b83SKonstantin Belousov ae = &ast_entries[ast];
228c6d31b83SKonstantin Belousov MPASS(ae->ae_f == NULL);
229c6d31b83SKonstantin Belousov ae->ae_flags = flags;
230c6d31b83SKonstantin Belousov ae->ae_tdp = tdp;
231c6d31b83SKonstantin Belousov atomic_interrupt_fence();
232c6d31b83SKonstantin Belousov ae->ae_f = f;
233c6d31b83SKonstantin Belousov }
234c6d31b83SKonstantin Belousov
235c6d31b83SKonstantin Belousov /*
236c6d31b83SKonstantin Belousov * XXXKIB Note that the deregistration of an AST handler does not
237c6d31b83SKonstantin Belousov * drain threads possibly executing it, which affects unloadable
238c6d31b83SKonstantin Belousov * modules. The issue is either handled by the subsystem using
239c6d31b83SKonstantin Belousov * handlers, or simply ignored. Fixing the problem is considered not
240c6d31b83SKonstantin Belousov * worth the overhead.
241c6d31b83SKonstantin Belousov */
242c6d31b83SKonstantin Belousov void
ast_deregister(int ast)243c6d31b83SKonstantin Belousov ast_deregister(int ast)
244c6d31b83SKonstantin Belousov {
245c6d31b83SKonstantin Belousov struct ast_entry *ae;
246c6d31b83SKonstantin Belousov
247c6d31b83SKonstantin Belousov MPASS(ast < TDA_MAX);
248c6d31b83SKonstantin Belousov ae = &ast_entries[ast];
249c6d31b83SKonstantin Belousov MPASS(ae->ae_f != NULL);
250c6d31b83SKonstantin Belousov ae->ae_f = NULL;
251c6d31b83SKonstantin Belousov atomic_interrupt_fence();
252c6d31b83SKonstantin Belousov ae->ae_flags = 0;
253c6d31b83SKonstantin Belousov ae->ae_tdp = 0;
254c6d31b83SKonstantin Belousov }
255c6d31b83SKonstantin Belousov
256c6d31b83SKonstantin Belousov void
ast_sched_locked(struct thread * td,int tda)257c6d31b83SKonstantin Belousov ast_sched_locked(struct thread *td, int tda)
258c6d31b83SKonstantin Belousov {
259c6d31b83SKonstantin Belousov THREAD_LOCK_ASSERT(td, MA_OWNED);
260c6d31b83SKonstantin Belousov MPASS(tda < TDA_MAX);
261c6d31b83SKonstantin Belousov
262c6d31b83SKonstantin Belousov td->td_ast |= TDAI(tda);
263c6d31b83SKonstantin Belousov }
264c6d31b83SKonstantin Belousov
265c6d31b83SKonstantin Belousov void
ast_unsched_locked(struct thread * td,int tda)266c6d31b83SKonstantin Belousov ast_unsched_locked(struct thread *td, int tda)
267c6d31b83SKonstantin Belousov {
268c6d31b83SKonstantin Belousov THREAD_LOCK_ASSERT(td, MA_OWNED);
269c6d31b83SKonstantin Belousov MPASS(tda < TDA_MAX);
270c6d31b83SKonstantin Belousov
271c6d31b83SKonstantin Belousov td->td_ast &= ~TDAI(tda);
272c6d31b83SKonstantin Belousov }
273c6d31b83SKonstantin Belousov
274c6d31b83SKonstantin Belousov void
ast_sched(struct thread * td,int tda)275c6d31b83SKonstantin Belousov ast_sched(struct thread *td, int tda)
276c6d31b83SKonstantin Belousov {
277c6d31b83SKonstantin Belousov thread_lock(td);
278c6d31b83SKonstantin Belousov ast_sched_locked(td, tda);
279c6d31b83SKonstantin Belousov thread_unlock(td);
280c6d31b83SKonstantin Belousov }
281c6d31b83SKonstantin Belousov
282c6d31b83SKonstantin Belousov void
ast_sched_mask(struct thread * td,int ast)283c6d31b83SKonstantin Belousov ast_sched_mask(struct thread *td, int ast)
284c6d31b83SKonstantin Belousov {
285c6d31b83SKonstantin Belousov thread_lock(td);
286c6d31b83SKonstantin Belousov td->td_ast |= ast;
287c6d31b83SKonstantin Belousov thread_unlock(td);
288c6d31b83SKonstantin Belousov }
289c6d31b83SKonstantin Belousov
290c6d31b83SKonstantin Belousov static bool
ast_handler_calc_tdp_run(struct thread * td,const struct ast_entry * ae)291c6d31b83SKonstantin Belousov ast_handler_calc_tdp_run(struct thread *td, const struct ast_entry *ae)
292c6d31b83SKonstantin Belousov {
293c6d31b83SKonstantin Belousov return ((ae->ae_flags & ASTR_TDP) == 0 ||
294c6d31b83SKonstantin Belousov (td->td_pflags & ae->ae_tdp) != 0);
295c6d31b83SKonstantin Belousov }
296c6d31b83SKonstantin Belousov
2975b81b6b3SRodney W. Grimes /*
2987aa7260eSJohn Baldwin * Process an asynchronous software trap.
2995b81b6b3SRodney W. Grimes */
300c6d31b83SKonstantin Belousov static void
ast_handler(struct thread * td,struct trapframe * framep,bool dtor)301c6d31b83SKonstantin Belousov ast_handler(struct thread *td, struct trapframe *framep, bool dtor)
302c6d31b83SKonstantin Belousov {
303c6d31b83SKonstantin Belousov struct ast_entry *ae;
304c6d31b83SKonstantin Belousov void (*f)(struct thread *td, int asts);
305c6d31b83SKonstantin Belousov int a, td_ast;
306c6d31b83SKonstantin Belousov bool run;
307c6d31b83SKonstantin Belousov
308c6d31b83SKonstantin Belousov if (framep != NULL) {
309c6d31b83SKonstantin Belousov kmsan_mark(framep, sizeof(*framep), KMSAN_STATE_INITED);
310c6d31b83SKonstantin Belousov td->td_frame = framep;
311c6d31b83SKonstantin Belousov }
312c6d31b83SKonstantin Belousov
313c6d31b83SKonstantin Belousov if (__predict_true(!dtor)) {
314c6d31b83SKonstantin Belousov WITNESS_WARN(WARN_PANIC, NULL, "Returning to user mode");
315c6d31b83SKonstantin Belousov mtx_assert(&Giant, MA_NOTOWNED);
316c6d31b83SKonstantin Belousov THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
317c6d31b83SKonstantin Belousov
318c6d31b83SKonstantin Belousov /*
319c6d31b83SKonstantin Belousov * This updates the td_ast for the checks below in one
320c6d31b83SKonstantin Belousov * atomic operation with turning off all scheduled AST's.
321c6d31b83SKonstantin Belousov * If another AST is triggered while we are handling the
322c6d31b83SKonstantin Belousov * AST's saved in td_ast, the td_ast is again non-zero and
323c6d31b83SKonstantin Belousov * ast() will be called again.
324c6d31b83SKonstantin Belousov */
325c6d31b83SKonstantin Belousov thread_lock(td);
326c6d31b83SKonstantin Belousov td_ast = td->td_ast;
327c6d31b83SKonstantin Belousov td->td_ast = 0;
328c6d31b83SKonstantin Belousov thread_unlock(td);
329c6d31b83SKonstantin Belousov } else {
330c6d31b83SKonstantin Belousov /*
331c6d31b83SKonstantin Belousov * The td thread's td_lock is not guaranteed to exist,
332c6d31b83SKonstantin Belousov * the thread might be not initialized enough when it's
333c6d31b83SKonstantin Belousov * destructor is called. It is safe to read and
334c6d31b83SKonstantin Belousov * update td_ast without locking since the thread is
335c6d31b83SKonstantin Belousov * not runnable or visible to other threads.
336c6d31b83SKonstantin Belousov */
337c6d31b83SKonstantin Belousov td_ast = td->td_ast;
338c6d31b83SKonstantin Belousov td->td_ast = 0;
339c6d31b83SKonstantin Belousov }
340c6d31b83SKonstantin Belousov
341c6d31b83SKonstantin Belousov CTR3(KTR_SYSC, "ast: thread %p (pid %d, %s)", td, td->td_proc->p_pid,
342c6d31b83SKonstantin Belousov td->td_proc->p_comm);
343c6d31b83SKonstantin Belousov KASSERT(framep == NULL || TRAPF_USERMODE(framep),
344c6d31b83SKonstantin Belousov ("ast in kernel mode"));
345c6d31b83SKonstantin Belousov
346c6d31b83SKonstantin Belousov for (a = 0; a < nitems(ast_entries); a++) {
347c6d31b83SKonstantin Belousov ae = &ast_entries[a];
348c6d31b83SKonstantin Belousov f = ae->ae_f;
349c6d31b83SKonstantin Belousov if (f == NULL)
350c6d31b83SKonstantin Belousov continue;
351c6d31b83SKonstantin Belousov atomic_interrupt_fence();
352c6d31b83SKonstantin Belousov
353c6d31b83SKonstantin Belousov run = false;
354c6d31b83SKonstantin Belousov if (__predict_false(framep == NULL)) {
355c6d31b83SKonstantin Belousov if ((ae->ae_flags & ASTR_KCLEAR) != 0)
356c6d31b83SKonstantin Belousov run = ast_handler_calc_tdp_run(td, ae);
357c6d31b83SKonstantin Belousov } else {
358c6d31b83SKonstantin Belousov if ((ae->ae_flags & ASTR_UNCOND) != 0)
359c6d31b83SKonstantin Belousov run = true;
360c6d31b83SKonstantin Belousov else if ((ae->ae_flags & ASTR_ASTF_REQUIRED) != 0 &&
361c6d31b83SKonstantin Belousov (td_ast & TDAI(a)) != 0)
362c6d31b83SKonstantin Belousov run = ast_handler_calc_tdp_run(td, ae);
363c6d31b83SKonstantin Belousov }
364c6d31b83SKonstantin Belousov if (run)
365c6d31b83SKonstantin Belousov f(td, td_ast);
366c6d31b83SKonstantin Belousov }
367c6d31b83SKonstantin Belousov }
368c6d31b83SKonstantin Belousov
3690384fff8SJason Evans void
ast(struct trapframe * framep)370e602ba25SJulian Elischer ast(struct trapframe *framep)
3710384fff8SJason Evans {
3726cae6dacSJohn Baldwin struct thread *td;
373b0f71f1bSMark Johnston
3746cae6dacSJohn Baldwin td = curthread;
375c6d31b83SKonstantin Belousov ast_handler(td, framep, false);
376eb2da9a5SPoul-Henning Kamp userret(td, framep);
377a2a1c95cSPeter Wemm }
378afe1a688SKonstantin Belousov
379c6d31b83SKonstantin Belousov void
ast_kclear(struct thread * td)380c6d31b83SKonstantin Belousov ast_kclear(struct thread *td)
381c6d31b83SKonstantin Belousov {
382c6d31b83SKonstantin Belousov ast_handler(td, NULL, td != curthread);
383c6d31b83SKonstantin Belousov }
384c6d31b83SKonstantin Belousov
385b2318c28SKonstantin Belousov const char *
syscallname(struct proc * p,u_int code)386afe1a688SKonstantin Belousov syscallname(struct proc *p, u_int code)
387afe1a688SKonstantin Belousov {
388afe1a688SKonstantin Belousov static const char unknown[] = "unknown";
38934a39b7bSKonstantin Belousov struct sysentvec *sv;
390afe1a688SKonstantin Belousov
39134a39b7bSKonstantin Belousov sv = p->p_sysent;
39234a39b7bSKonstantin Belousov if (sv->sv_syscallnames == NULL || code >= sv->sv_size)
393afe1a688SKonstantin Belousov return (unknown);
39434a39b7bSKonstantin Belousov return (sv->sv_syscallnames[code]);
395afe1a688SKonstantin Belousov }
396