xref: /netbsd/sys/sys/lwp.h (revision 14f6a1f0)
1 /*	$NetBSD: lwp.h,v 1.193 2020/01/08 17:38:43 ad Exp $	*/
2 
3 /*
4  * Copyright (c) 2001, 2006, 2007, 2008, 2009, 2010, 2019
5  *    The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Nathan J. Williams 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 #ifndef _SYS_LWP_H_
34 #define _SYS_LWP_H_
35 
36 #if defined(_KERNEL) || defined(_KMEMUSER)
37 
38 #include <sys/param.h>
39 #include <sys/time.h>
40 #include <sys/queue.h>
41 #include <sys/callout.h>
42 #include <sys/kcpuset.h>
43 #include <sys/mutex.h>
44 #include <sys/condvar.h>
45 #include <sys/signalvar.h>
46 #include <sys/sched.h>
47 #include <sys/specificdata.h>
48 #include <sys/syncobj.h>
49 #include <sys/resource.h>
50 
51 #if defined(_KERNEL)
52 struct lwp;
53 /* forward declare this for <machine/cpu.h> so it can get l_cpu. */
54 static __inline struct cpu_info *lwp_getcpu(struct lwp *);
55 #include <machine/cpu.h>		/* curcpu() and cpu_info */
56 #ifdef _KERNEL_OPT
57 #include "opt_kmsan.h"
58 #endif
59 #endif
60 
61 #include <machine/proc.h>		/* Machine-dependent proc substruct. */
62 
63 /*
64  * Lightweight process.  Field markings and the corresponding locks:
65  *
66  * a:	proc_lock
67  * c:	condition variable interlock, passed to cv_wait()
68  * l:	*l_mutex
69  * p:	l_proc->p_lock
70  * s:	spc_mutex, which may or may not be referenced by l_mutex
71  * S:	l_selcluster->sc_lock
72  * (:	unlocked, stable
73  * !:	unlocked, may only be reliably accessed by the LWP itself
74  *
75  * Fields are clustered together by usage (to increase the likelihood
76  * of cache hits) and by size (to reduce dead space in the structure).
77  */
78 
79 #include <sys/pcu.h>
80 
81 struct lockdebug;
82 struct sysent;
83 
84 struct lwp {
85 	/* Scheduling and overall state. */
86 	TAILQ_ENTRY(lwp) l_runq;	/* s: run queue */
87 	union {
88 		void *	info;		/* s: scheduler-specific structure */
89 		u_int	timeslice;	/* l: time-quantum for SCHED_M2 */
90 	} l_sched;
91 	struct cpu_info *volatile l_cpu;/* s: CPU we're on if LSONPROC */
92 	kmutex_t * volatile l_mutex;	/* l: ptr to mutex on sched state */
93 	void		*l_addr;	/* l: PCB address; use lwp_getpcb() */
94 	struct mdlwp	l_md;		/* l: machine-dependent fields. */
95 	int		l_flag;		/* l: misc flag values */
96 	int		l_stat;		/* l: overall LWP status */
97 	struct bintime 	l_rtime;	/* l: real time */
98 	struct bintime	l_stime;	/* l: start time (while ONPROC) */
99 	u_int		l_swtime;	/* l: time swapped in or out */
100 	u_int		l_rticks;	/* l: Saved start time of run */
101 	u_int		l_rticksum;	/* l: Sum of ticks spent running */
102 	u_int		l_slpticks;	/* l: Saved start time of sleep */
103 	u_int		l_slpticksum;	/* l: Sum of ticks spent sleeping */
104 	int		l_biglocks;	/* l: biglock count before sleep */
105 	int		l_class;	/* l: scheduling class */
106 	int		l_kpriority;	/* !: has kernel priority boost */
107 	pri_t		l_kpribase;	/* !: kernel priority base level */
108 	pri_t		l_priority;	/* l: scheduler priority */
109 	pri_t		l_inheritedprio;/* l: inherited priority */
110 	pri_t		l_protectprio;	/* l: for PTHREAD_PRIO_PROTECT */
111 	pri_t		l_auxprio;	/* l: max(inherit,protect) priority */
112 	int		l_protectdepth;	/* l: for PTHREAD_PRIO_PROTECT */
113 	SLIST_HEAD(, turnstile) l_pi_lenders; /* l: ts lending us priority */
114 	volatile uint64_t l_ncsw;	/* l: total context switches */
115 	volatile uint64_t l_nivcsw;	/* l: involuntary context switches */
116 	u_int		l_cpticks;	/* (: Ticks of CPU time */
117 	fixpt_t		l_pctcpu;	/* p: %cpu during l_swtime */
118 	fixpt_t		l_estcpu;	/* l: cpu time for SCHED_4BSD */
119 	psetid_t	l_psid;		/* l: assigned processor-set ID */
120 	struct cpu_info *l_target_cpu;	/* l: target CPU to migrate */
121 	struct lwpctl	*l_lwpctl;	/* p: lwpctl block kernel address */
122 	struct lcpage	*l_lcpage;	/* p: lwpctl containing page */
123 	kcpuset_t	*l_affinity;	/* l: CPU set for affinity */
124 
125 	/* Synchronisation. */
126 	struct turnstile *l_ts;		/* l: current turnstile */
127 	struct syncobj	*l_syncobj;	/* l: sync object operations set */
128 	TAILQ_ENTRY(lwp) l_sleepchain;	/* l: sleep queue */
129 	wchan_t		l_wchan;	/* l: sleep address */
130 	const char	*l_wmesg;	/* l: reason for sleep */
131 	struct sleepq	*l_sleepq;	/* l: current sleep queue */
132 	int		l_sleeperr;	/* !: error before unblock */
133 	u_int		l_slptime;	/* l: time since last blocked */
134 	callout_t	l_timeout_ch;	/* !: callout for tsleep */
135 	u_int		l_emap_gen;	/* !: emap generation number */
136 	kcondvar_t	l_waitcv;	/* a: vfork() wait */
137 	bool		l_vforkwaiting;	/* a: vfork() waiting */
138 
139 #if PCU_UNIT_COUNT > 0
140 	struct cpu_info	* volatile l_pcu_cpu[PCU_UNIT_COUNT];
141 	uint32_t	l_pcu_valid;
142 #endif
143 
144 	/* Process level and global state, misc. */
145 	LIST_ENTRY(lwp)	l_list;		/* a: entry on list of all LWPs */
146 	void		*l_ctxlink;	/* p: uc_link {get,set}context */
147 	struct proc	*l_proc;	/* p: parent process */
148 	LIST_ENTRY(lwp)	l_sibling;	/* p: entry on proc's list of LWPs */
149 	lwpid_t		l_waiter;	/* p: first LWP waiting on us */
150 	lwpid_t 	l_waitingfor;	/* p: specific LWP we are waiting on */
151 	int		l_prflag;	/* p: process level flags */
152 	u_int		l_refcnt;	/* p: reference count on this LWP */
153 	lwpid_t		l_lid;		/* (: LWP identifier; local to proc */
154 	char		*l_name;	/* (: name, optional */
155 
156 	/* State of select() or poll(). */
157 	int		l_selflag;	/* S: polling state flags */
158 	SLIST_HEAD(,selinfo) l_selwait;	/* S: descriptors waited on */
159 	int		l_selret;	/* S: return value of select/poll */
160 	uintptr_t	l_selrec;	/* !: argument for selrecord() */
161 	struct selcluster *l_selcluster;/* !: associated cluster data */
162 	void *		l_selbits;	/* (: select() bit-field */
163 	size_t		l_selni;	/* (: size of a single bit-field */
164 
165 	/* Signals. */
166 	int		l_sigrestore;	/* p: need to restore old sig mask */
167 	sigset_t	l_sigwaitset;	/* p: signals being waited for */
168 	kcondvar_t	l_sigcv;	/* p: for sigsuspend() */
169 	struct ksiginfo	*l_sigwaited;	/* p: delivered signals from set */
170 	sigpend_t	*l_sigpendset;	/* p: XXX issignal()/postsig() baton */
171 	LIST_ENTRY(lwp)	l_sigwaiter;	/* p: chain on list of waiting LWPs */
172 	stack_t		l_sigstk;	/* p: sp & on stack state variable */
173 	sigset_t	l_sigmask;	/* p: signal mask */
174 	sigpend_t	l_sigpend;	/* p: signals to this LWP */
175 	sigset_t	l_sigoldmask;	/* p: mask for sigpause */
176 
177 	/* Private data. */
178 	specificdata_reference
179 		l_specdataref;		/* !: subsystem lwp-specific data */
180 	struct timespec l_ktrcsw;	/* !: for ktrace CSW trace XXX */
181 	void		*l_private;	/* !: svr4-style lwp-private data */
182 	struct lwp	*l_switchto;	/* !: mi_switch: switch to this LWP */
183 	struct kauth_cred *l_cred;	/* !: cached credentials */
184 	struct filedesc	*l_fd;		/* !: cached copy of proc::p_fd */
185 	void		*l_emuldata;	/* !: kernel lwp-private data */
186 	struct fstrans_lwp_info *l_fstrans; /* (: fstrans private data */
187 	u_int		l_cv_signalled;	/* c: restarted by cv_signal() */
188 	u_short		l_shlocks;	/* !: lockdebug: shared locks held */
189 	u_short		l_exlocks;	/* !: lockdebug: excl. locks held */
190 	u_short		l_psrefs;	/* !: count of psref held */
191 	u_short		l_blcnt;	/* !: count of kernel_lock held */
192 	int		l_nopreempt;	/* !: don't preempt me! */
193 	u_int		l_dopreempt;	/* s: kernel preemption pending */
194 	int		l_pflag;	/* !: LWP private flags */
195 	int		l_dupfd;	/* !: side return from cloning devs XXX */
196 	const struct sysent * volatile l_sysent;/* !: currently active syscall */
197 	struct rusage	l_ru;		/* !: accounting information */
198 	uint64_t	l_pfailtime;	/* !: for kernel preemption */
199 	uintptr_t	l_pfailaddr;	/* !: for kernel preemption */
200 	uintptr_t	l_pfaillock;	/* !: for kernel preemption */
201 	_TAILQ_HEAD(,struct lockdebug,volatile) l_ld_locks;/* !: locks held by LWP */
202 	int		l_tcgen;	/* !: for timecounter removal */
203 
204 	/* These are only used by 'options SYSCALL_TIMES'. */
205 	uint32_t	l_syscall_time;	/* !: time epoch for current syscall */
206 	uint64_t	*l_syscall_counter; /* !: counter for current process */
207 
208 	struct kdtrace_thread *l_dtrace; /* (: DTrace-specific data. */
209 
210 #ifdef KMSAN
211 	void		*l_kmsan; /* !: KMSAN private data. */
212 #endif
213 };
214 
215 /*
216  * UAREA_PCB_OFFSET: an offset of PCB structure in the uarea.  MD code may
217  * define it in <machine/proc.h>, to indicate a different uarea layout.
218  */
219 #ifndef UAREA_PCB_OFFSET
220 #define	UAREA_PCB_OFFSET	0
221 #endif
222 
223 LIST_HEAD(lwplist, lwp);		/* A list of LWPs. */
224 
225 #ifdef _KERNEL
226 extern struct lwplist	alllwp;		/* List of all LWPs. */
227 extern lwp_t		lwp0;		/* LWP for proc0. */
228 extern int		maxlwp __read_mostly;	/* max number of lwps */
229 #ifndef MAXLWP
230 #define	MAXLWP		2048
231 #endif
232 #ifndef	__HAVE_CPU_MAXLWP
233 #define	cpu_maxlwp()	MAXLWP
234 #endif
235 #endif
236 
237 #endif /* _KERNEL || _KMEMUSER */
238 
239 /* These flags are kept in l_flag. */
240 #define	LW_IDLE		0x00000001 /* Idle lwp. */
241 #define	LW_LWPCTL	0x00000002 /* Adjust lwpctl in userret */
242 #define	LW_CVLOCKDEBUG	0x00000004 /* Waker does lockdebug */
243 #define	LW_SINTR	0x00000080 /* Sleep is interruptible. */
244 #define	LW_SYSTEM	0x00000200 /* Kernel thread */
245 #define	LW_DBGSUSPEND	0x00010000 /* Suspend by debugger */
246 #define	LW_WSUSPEND	0x00020000 /* Suspend before return to user */
247 #define	LW_BATCH	0x00040000 /* LWP tends to hog CPU */
248 #define	LW_WCORE	0x00080000 /* Stop for core dump on return to user */
249 #define	LW_WEXIT	0x00100000 /* Exit before return to user */
250 #define	LW_PENDSIG	0x01000000 /* Pending signal for us */
251 #define	LW_CANCELLED	0x02000000 /* tsleep should not sleep */
252 #define	LW_WREBOOT	0x08000000 /* System is rebooting, please suspend */
253 #define	LW_UNPARKED	0x10000000 /* Unpark op pending */
254 #define	LW_RUNNING	0x20000000 /* Active on a CPU */
255 #define	LW_RUMP_CLEAR	0x40000000 /* Clear curlwp in RUMP scheduler */
256 #define	LW_RUMP_QEXIT	0x80000000 /* LWP should exit ASAP */
257 
258 /* The second set of flags is kept in l_pflag. */
259 #define	LP_KTRACTIVE	0x00000001 /* Executing ktrace operation */
260 #define	LP_KTRCSW	0x00000002 /* ktrace context switch marker */
261 #define	LP_KTRCSWUSER	0x00000004 /* ktrace context switch marker */
262 #define	LP_PIDLID	0x00000008 /* free LID from PID space on exit */
263 #define	LP_OWEUPC	0x00000010 /* Owe user profiling tick */
264 #define	LP_MPSAFE	0x00000020 /* Starts life without kernel_lock */
265 #define	LP_INTR		0x00000040 /* Soft interrupt handler */
266 #define	LP_SYSCTLWRITE	0x00000080 /* sysctl write lock held */
267 #define	LP_MUSTJOIN	0x00000100 /* Must join kthread on exit */
268 #define	LP_SINGLESTEP	0x00000400 /* Single step thread in ptrace(2) */
269 #define	LP_TIMEINTR	0x00010000 /* Time this soft interrupt */
270 #define	LP_PREEMPTING	0x00020000 /* mi_switch called involuntarily */
271 #define	LP_BOUND	0x80000000 /* Bound to a CPU */
272 
273 /* The third set is kept in l_prflag. */
274 #define	LPR_DETACHED	0x00800000 /* Won't be waited for. */
275 #define	LPR_CRMOD	0x00000100 /* Credentials modified */
276 
277 /*
278  * Mask indicating that there is "exceptional" work to be done on return to
279  * user.
280  */
281 #define	LW_USERRET	\
282     (LW_WEXIT | LW_PENDSIG | LW_WREBOOT | LW_WSUSPEND | LW_WCORE | LW_LWPCTL)
283 
284 /*
285  * Status values.
286  *
287  * A note about LSRUN and LSONPROC: LSRUN indicates that a process is
288  * runnable but *not* yet running, i.e. is on a run queue.  LSONPROC
289  * indicates that the process is actually executing on a CPU, i.e.
290  * it is no longer on a run queue.
291  *
292  * These values are set in stone and must not be reused with future changes.
293  */
294 #define	LSIDL		1	/* Process being created by fork. */
295 #define	LSRUN		2	/* Currently runnable. */
296 #define	LSSLEEP		3	/* Sleeping on an address. */
297 #define	LSSTOP		4	/* Process debugging or suspension. */
298 #define	LSZOMB		5	/* Awaiting collection by parent. */
299 /* define	LSDEAD	6	Process is almost a zombie. (removed in 5.0) */
300 #define	LSONPROC	7	/* Process is currently on a CPU. */
301 #define	LSSUSPENDED	8	/* Not running, not signalable. */
302 
303 #if defined(_KERNEL) || defined(_KMEMUSER)
304 static __inline void *
305 lwp_getpcb(struct lwp *l)
306 {
307 
308 	return l->l_addr;
309 }
310 #endif /* _KERNEL || _KMEMUSER */
311 
312 #ifdef _KERNEL
313 #define	LWP_CACHE_CREDS(l, p)						\
314 do {									\
315 	(void)p;							\
316 	if (__predict_false((l)->l_prflag & LPR_CRMOD))			\
317 		lwp_update_creds(l);					\
318 } while (/* CONSTCOND */ 0)
319 
320 void	lwpinit(void);
321 void	lwp0_init(void);
322 void	lwp_sys_init(void);
323 
324 void	lwp_startup(lwp_t *, lwp_t *);
325 void	startlwp(void *);
326 
327 int	lwp_locked(lwp_t *, kmutex_t *);
328 kmutex_t *lwp_setlock(lwp_t *, kmutex_t *);
329 void	lwp_unlock_to(lwp_t *, kmutex_t *);
330 int	lwp_trylock(lwp_t *);
331 void	lwp_addref(lwp_t *);
332 void	lwp_delref(lwp_t *);
333 void	lwp_delref2(lwp_t *);
334 void	lwp_drainrefs(lwp_t *);
335 bool	lwp_alive(lwp_t *);
336 lwp_t	*lwp_find_first(proc_t *);
337 
338 int	lwp_wait(lwp_t *, lwpid_t, lwpid_t *, bool);
339 void	lwp_continue(lwp_t *);
340 void	lwp_unsleep(lwp_t *, bool);
341 void	lwp_unstop(lwp_t *);
342 void	lwp_exit(lwp_t *);
343 int	lwp_suspend(lwp_t *, lwp_t *);
344 int	lwp_create1(lwp_t *, const void *, size_t, u_long, lwpid_t *);
345 void	lwp_start(lwp_t *, int);
346 void	lwp_update_creds(lwp_t *);
347 void	lwp_migrate(lwp_t *, struct cpu_info *);
348 lwp_t *	lwp_find2(pid_t, lwpid_t);
349 lwp_t *	lwp_find(proc_t *, int);
350 void	lwp_userret(lwp_t *);
351 void	lwp_need_userret(lwp_t *);
352 void	lwp_free(lwp_t *, bool, bool);
353 uint64_t lwp_pctr(void);
354 int	lwp_setprivate(lwp_t *, void *);
355 int	do_lwp_create(lwp_t *, void *, u_long, lwpid_t *, const sigset_t *,
356     const stack_t *);
357 
358 void	lwpinit_specificdata(void);
359 int	lwp_specific_key_create(specificdata_key_t *, specificdata_dtor_t);
360 void	lwp_specific_key_delete(specificdata_key_t);
361 void	lwp_initspecific(lwp_t *);
362 void	lwp_finispecific(lwp_t *);
363 void	*lwp_getspecific(specificdata_key_t);
364 #if defined(_LWP_API_PRIVATE)
365 void	*_lwp_getspecific_by_lwp(lwp_t *, specificdata_key_t);
366 #endif
367 void	lwp_setspecific(specificdata_key_t, void *);
368 void	lwp_setspecific_by_lwp(lwp_t *, specificdata_key_t, void *);
369 
370 /* Syscalls. */
371 int	lwp_park(clockid_t, int, struct timespec *, const void *);
372 int	lwp_unpark(lwpid_t, const void *);
373 
374 /* DDB. */
375 void	lwp_whatis(uintptr_t, void (*)(const char *, ...) __printflike(1, 2));
376 
377 /*
378  * Lock an LWP. XXX _MODULE
379  */
380 static __inline void
381 lwp_lock(lwp_t *l)
382 {
383 	kmutex_t *old = l->l_mutex;
384 
385 	/*
386 	 * Note: mutex_spin_enter() will have posted a read barrier.
387 	 * Re-test l->l_mutex.  If it has changed, we need to try again.
388 	 */
389 	mutex_spin_enter(old);
390 	while (__predict_false(l->l_mutex != old)) {
391 		mutex_spin_exit(old);
392 		old = l->l_mutex;
393 		mutex_spin_enter(old);
394 	}
395 }
396 
397 /*
398  * Unlock an LWP. XXX _MODULE
399  */
400 static __inline void
401 lwp_unlock(lwp_t *l)
402 {
403 	mutex_spin_exit(l->l_mutex);
404 }
405 
406 static __inline void
407 lwp_changepri(lwp_t *l, pri_t pri)
408 {
409 	KASSERT(mutex_owned(l->l_mutex));
410 
411 	if (l->l_priority == pri)
412 		return;
413 
414 	(*l->l_syncobj->sobj_changepri)(l, pri);
415 	KASSERT(l->l_priority == pri);
416 }
417 
418 static __inline void
419 lwp_lendpri(lwp_t *l, pri_t pri)
420 {
421 	KASSERT(mutex_owned(l->l_mutex));
422 
423 	(*l->l_syncobj->sobj_lendpri)(l, pri);
424 	KASSERT(l->l_inheritedprio == pri);
425 }
426 
427 static __inline pri_t
428 lwp_eprio(lwp_t *l)
429 {
430 	pri_t pri;
431 
432 	pri = l->l_priority;
433 	if ((l->l_flag & LW_SYSTEM) == 0 && l->l_kpriority && pri < PRI_KERNEL)
434 		pri = (pri >> 1) + l->l_kpribase;
435 	return MAX(l->l_auxprio, pri);
436 }
437 
438 int lwp_create(lwp_t *, struct proc *, vaddr_t, int, void *, size_t,
439     void (*)(void *), void *, lwp_t **, int, const sigset_t *, const stack_t *);
440 
441 /*
442  * XXX _MODULE
443  * We should provide real stubs for the below that modules can use.
444  */
445 
446 static __inline void
447 spc_lock(struct cpu_info *ci)
448 {
449 	mutex_spin_enter(ci->ci_schedstate.spc_mutex);
450 }
451 
452 static __inline void
453 spc_unlock(struct cpu_info *ci)
454 {
455 	mutex_spin_exit(ci->ci_schedstate.spc_mutex);
456 }
457 
458 static __inline void
459 spc_dlock(struct cpu_info *ci1, struct cpu_info *ci2)
460 {
461 	struct schedstate_percpu *spc1 = &ci1->ci_schedstate;
462 	struct schedstate_percpu *spc2 = &ci2->ci_schedstate;
463 
464 	KASSERT(ci1 != ci2);
465 	if (ci1 < ci2) {
466 		mutex_spin_enter(spc1->spc_mutex);
467 		mutex_spin_enter(spc2->spc_mutex);
468 	} else {
469 		mutex_spin_enter(spc2->spc_mutex);
470 		mutex_spin_enter(spc1->spc_mutex);
471 	}
472 }
473 
474 /*
475  * Allow machine-dependent code to override curlwp in <machine/cpu.h> for
476  * its own convenience.  Otherwise, we declare it as appropriate.
477  */
478 #if !defined(curlwp)
479 #if defined(MULTIPROCESSOR)
480 #define	curlwp		curcpu()->ci_curlwp	/* Current running LWP */
481 #else
482 extern struct lwp	*curlwp;		/* Current running LWP */
483 #endif /* MULTIPROCESSOR */
484 #endif /* ! curlwp */
485 #define	curproc		(curlwp->l_proc)
486 
487 /*
488  * This provide a way for <machine/cpu.h> to get l_cpu for curlwp before
489  * struct lwp is defined.
490  */
491 static __inline struct cpu_info *
492 lwp_getcpu(struct lwp *l)
493 {
494 	return l->l_cpu;
495 }
496 
497 static __inline bool
498 CURCPU_IDLE_P(void)
499 {
500 	struct cpu_info *ci = curcpu();
501 	return ci->ci_onproc == ci->ci_data.cpu_idlelwp;
502 }
503 
504 /*
505  * Disable and re-enable preemption.  Only for low-level kernel
506  * use.  Device drivers and anything that could potentially be
507  * compiled as a module should use kpreempt_disable() and
508  * kpreempt_enable().
509  */
510 static __inline void
511 KPREEMPT_DISABLE(lwp_t *l)
512 {
513 
514 	KASSERT(l == curlwp);
515 	l->l_nopreempt++;
516 	__insn_barrier();
517 }
518 
519 static __inline void
520 KPREEMPT_ENABLE(lwp_t *l)
521 {
522 
523 	KASSERT(l == curlwp);
524 	KASSERT(l->l_nopreempt > 0);
525 	__insn_barrier();
526 	if (--l->l_nopreempt != 0)
527 		return;
528 	__insn_barrier();
529 	if (__predict_false(l->l_dopreempt))
530 		kpreempt(0);
531 	__insn_barrier();
532 }
533 
534 /* For lwp::l_dopreempt */
535 #define	DOPREEMPT_ACTIVE	0x01
536 #define	DOPREEMPT_COUNTED	0x02
537 
538 /*
539  * Prevent curlwp from migrating between CPUs between curlwp_bind and
540  * curlwp_bindx. One use case is psref(9) that has a contract that
541  * forbids migrations.
542  */
543 static __inline int
544 curlwp_bind(void)
545 {
546 	int bound;
547 
548 	bound = curlwp->l_pflag & LP_BOUND;
549 	curlwp->l_pflag |= LP_BOUND;
550 	__insn_barrier();
551 
552 	return bound;
553 }
554 
555 static __inline void
556 curlwp_bindx(int bound)
557 {
558 
559 	KASSERT(curlwp->l_pflag & LP_BOUND);
560 	__insn_barrier();
561 	curlwp->l_pflag ^= bound ^ LP_BOUND;
562 }
563 
564 #endif /* _KERNEL */
565 
566 /* Flags for _lwp_create(), as per Solaris. */
567 #define	LWP_DETACHED	0x00000040
568 #define	LWP_SUSPENDED	0x00000080
569 
570 /* Kernel-internal flags for LWP creation. */
571 #define	LWP_PIDLID	0x40000000
572 #define	LWP_VFORK	0x80000000
573 
574 #endif	/* !_SYS_LWP_H_ */
575