1*84d9c625SLionel Sambuc /* $NetBSD: userret.h,v 1.26 2013/04/07 07:54:53 kiyohara Exp $ */
26c8f7fc3SBen Gras
36c8f7fc3SBen Gras /*-
46c8f7fc3SBen Gras * Copyright (c) 1998, 2000, 2003, 2006, 2008 The NetBSD Foundation, Inc.
56c8f7fc3SBen Gras * All rights reserved.
66c8f7fc3SBen Gras *
76c8f7fc3SBen Gras * This code is derived from software contributed to The NetBSD Foundation
86c8f7fc3SBen Gras * by Charles M. Hannum, and Andrew Doran.
96c8f7fc3SBen Gras *
106c8f7fc3SBen Gras * Redistribution and use in source and binary forms, with or without
116c8f7fc3SBen Gras * modification, are permitted provided that the following conditions
126c8f7fc3SBen Gras * are met:
136c8f7fc3SBen Gras * 1. Redistributions of source code must retain the above copyright
146c8f7fc3SBen Gras * notice, this list of conditions and the following disclaimer.
156c8f7fc3SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
166c8f7fc3SBen Gras * notice, this list of conditions and the following disclaimer in the
176c8f7fc3SBen Gras * documentation and/or other materials provided with the distribution.
186c8f7fc3SBen Gras *
196c8f7fc3SBen Gras * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
206c8f7fc3SBen Gras * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
216c8f7fc3SBen Gras * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
226c8f7fc3SBen Gras * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
236c8f7fc3SBen Gras * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
246c8f7fc3SBen Gras * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
256c8f7fc3SBen Gras * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
266c8f7fc3SBen Gras * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
276c8f7fc3SBen Gras * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
286c8f7fc3SBen Gras * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
296c8f7fc3SBen Gras * POSSIBILITY OF SUCH DAMAGE.
306c8f7fc3SBen Gras */
316c8f7fc3SBen Gras
326c8f7fc3SBen Gras /*-
336c8f7fc3SBen Gras * Copyright (c) 1990 The Regents of the University of California.
346c8f7fc3SBen Gras * All rights reserved.
356c8f7fc3SBen Gras *
366c8f7fc3SBen Gras * This code is derived from software contributed to Berkeley by
376c8f7fc3SBen Gras * William Jolitz.
386c8f7fc3SBen Gras *
396c8f7fc3SBen Gras * Redistribution and use in source and binary forms, with or without
406c8f7fc3SBen Gras * modification, are permitted provided that the following conditions
416c8f7fc3SBen Gras * are met:
426c8f7fc3SBen Gras * 1. Redistributions of source code must retain the above copyright
436c8f7fc3SBen Gras * notice, this list of conditions and the following disclaimer.
446c8f7fc3SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
456c8f7fc3SBen Gras * notice, this list of conditions and the following disclaimer in the
466c8f7fc3SBen Gras * documentation and/or other materials provided with the distribution.
476c8f7fc3SBen Gras * 3. Neither the name of the University nor the names of its contributors
486c8f7fc3SBen Gras * may be used to endorse or promote products derived from this software
496c8f7fc3SBen Gras * without specific prior written permission.
506c8f7fc3SBen Gras *
516c8f7fc3SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
526c8f7fc3SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
536c8f7fc3SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
546c8f7fc3SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
556c8f7fc3SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
566c8f7fc3SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
576c8f7fc3SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
586c8f7fc3SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
596c8f7fc3SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
606c8f7fc3SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
616c8f7fc3SBen Gras * SUCH DAMAGE.
626c8f7fc3SBen Gras *
636c8f7fc3SBen Gras */
646c8f7fc3SBen Gras
656c8f7fc3SBen Gras #ifndef _SYS_USERRET_H_
666c8f7fc3SBen Gras #define _SYS_USERRET_H_
676c8f7fc3SBen Gras
686c8f7fc3SBen Gras #include <sys/lockdebug.h>
696c8f7fc3SBen Gras #include <sys/intr.h>
706c8f7fc3SBen Gras
716c8f7fc3SBen Gras /*
726c8f7fc3SBen Gras * Define the MI code needed before returning to user mode, for
736c8f7fc3SBen Gras * trap and syscall.
746c8f7fc3SBen Gras */
756c8f7fc3SBen Gras static __inline void
mi_userret(struct lwp * l)766c8f7fc3SBen Gras mi_userret(struct lwp *l)
776c8f7fc3SBen Gras {
786c8f7fc3SBen Gras #ifndef __HAVE_PREEMPTION
796c8f7fc3SBen Gras struct cpu_info *ci;
806c8f7fc3SBen Gras #endif
816c8f7fc3SBen Gras
826c8f7fc3SBen Gras KASSERT(l->l_blcnt == 0);
836c8f7fc3SBen Gras #ifndef __HAVE_PREEMPTION
846c8f7fc3SBen Gras KASSERT(curcpu()->ci_biglock_count == 0);
856c8f7fc3SBen Gras #endif
866c8f7fc3SBen Gras
876c8f7fc3SBen Gras /*
886c8f7fc3SBen Gras * Handle "exceptional" events: pending signals, stop/exit actions,
896c8f7fc3SBen Gras * etc. Note that the event must be flagged BEFORE any AST is
906c8f7fc3SBen Gras * posted as we are reading unlocked.
916c8f7fc3SBen Gras */
926c8f7fc3SBen Gras #ifdef __HAVE_PREEMPTION
936c8f7fc3SBen Gras if (__predict_false(l->l_flag & LW_USERRET)) {
946c8f7fc3SBen Gras lwp_userret(l);
956c8f7fc3SBen Gras }
966c8f7fc3SBen Gras l->l_kpriority = false;
976c8f7fc3SBen Gras /*
986c8f7fc3SBen Gras * cpu_set_curpri(prio) is a MD optimized version of:
996c8f7fc3SBen Gras *
1006c8f7fc3SBen Gras * kpreempt_disable();
1016c8f7fc3SBen Gras * curcpu()->ci_schedstate.spc_curpriority = prio;
1026c8f7fc3SBen Gras * kpreempt_enable();
1036c8f7fc3SBen Gras */
1046c8f7fc3SBen Gras cpu_set_curpri(l->l_priority); /* XXX this needs to die */
1056c8f7fc3SBen Gras #else
1066c8f7fc3SBen Gras ci = l->l_cpu;
1076c8f7fc3SBen Gras if (((l->l_flag & LW_USERRET) | ci->ci_data.cpu_softints) != 0) {
1086c8f7fc3SBen Gras lwp_userret(l);
1096c8f7fc3SBen Gras ci = l->l_cpu;
1106c8f7fc3SBen Gras }
1116c8f7fc3SBen Gras l->l_kpriority = false;
1126c8f7fc3SBen Gras ci->ci_schedstate.spc_curpriority = l->l_priority;
1136c8f7fc3SBen Gras #endif
1146c8f7fc3SBen Gras
1156c8f7fc3SBen Gras LOCKDEBUG_BARRIER(NULL, 0);
1166c8f7fc3SBen Gras KASSERT(l->l_nopreempt == 0);
1176c8f7fc3SBen Gras }
1186c8f7fc3SBen Gras
1196c8f7fc3SBen Gras #endif /* !_SYS_USERRET_H_ */
120