1219820Sjeff/*-
2219820Sjeff * Copyright (c) 2010 Isilon Systems, Inc.
3219820Sjeff * Copyright (c) 2010 iX Systems, Inc.
4219820Sjeff * Copyright (c) 2010 Panasas, Inc.
5329976Shselasky * Copyright (c) 2013-2018 Mellanox Technologies, Ltd.
6219820Sjeff * All rights reserved.
7219820Sjeff *
8219820Sjeff * Redistribution and use in source and binary forms, with or without
9219820Sjeff * modification, are permitted provided that the following conditions
10219820Sjeff * are met:
11219820Sjeff * 1. Redistributions of source code must retain the above copyright
12219820Sjeff *    notice unmodified, this list of conditions, and the following
13219820Sjeff *    disclaimer.
14219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright
15219820Sjeff *    notice, this list of conditions and the following disclaimer in the
16219820Sjeff *    documentation and/or other materials provided with the distribution.
17219820Sjeff *
18219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19219820Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20219820Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21219820Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22219820Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23219820Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24219820Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25219820Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26219820Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27219820Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28289644Shselasky *
29289644Shselasky * $FreeBSD: stable/11/sys/compat/linuxkpi/common/include/linux/sched.h 369549 2021-04-06 10:30:29Z hselasky $
30219820Sjeff */
31219820Sjeff#ifndef	_LINUX_SCHED_H_
32219820Sjeff#define	_LINUX_SCHED_H_
33219820Sjeff
34219820Sjeff#include <sys/param.h>
35219820Sjeff#include <sys/systm.h>
36219820Sjeff#include <sys/proc.h>
37219820Sjeff#include <sys/sched.h>
38219820Sjeff#include <sys/sleepqueue.h>
39328653Shselasky#include <sys/time.h>
40219820Sjeff
41328653Shselasky#include <linux/bitmap.h>
42328653Shselasky#include <linux/compat.h>
43328653Shselasky#include <linux/completion.h>
44328653Shselasky#include <linux/mm_types.h>
45328653Shselasky#include <linux/pid.h>
46328653Shselasky#include <linux/slab.h>
47328653Shselasky#include <linux/string.h>
48328653Shselasky#include <linux/time.h>
49219820Sjeff
50328653Shselasky#include <asm/atomic.h>
51219820Sjeff
52328653Shselasky#define	MAX_SCHEDULE_TIMEOUT	INT_MAX
53219820Sjeff
54328653Shselasky#define	TASK_RUNNING		0x0000
55328653Shselasky#define	TASK_INTERRUPTIBLE	0x0001
56328653Shselasky#define	TASK_UNINTERRUPTIBLE	0x0002
57328653Shselasky#define	TASK_NORMAL		(TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)
58328653Shselasky#define	TASK_WAKING		0x0100
59328653Shselasky#define	TASK_PARKED		0x0200
60328653Shselasky
61328653Shselasky#define	TASK_COMM_LEN		(MAXCOMLEN + 1)
62328653Shselasky
63337897Shselaskystruct work_struct;
64219820Sjeffstruct task_struct {
65328653Shselasky	struct thread *task_thread;
66328653Shselasky	struct mm_struct *mm;
67328653Shselasky	linux_task_fn_t *task_fn;
68328653Shselasky	void   *task_data;
69219820Sjeff	int	task_ret;
70328653Shselasky	atomic_t usage;
71328653Shselasky	atomic_t state;
72328653Shselasky	atomic_t kthread_flags;
73328653Shselasky	pid_t	pid;	/* BSD thread ID */
74299526Shselasky	const char    *comm;
75328653Shselasky	void   *bsd_ioctl_data;
76328653Shselasky	unsigned bsd_ioctl_len;
77328653Shselasky	struct completion parked;
78328653Shselasky	struct completion exited;
79364391Shselasky#define	TS_RCU_TYPE_MAX 2
80364391Shselasky	TAILQ_ENTRY(task_struct) rcu_entry[TS_RCU_TYPE_MAX];
81364391Shselasky	int rcu_recurse[TS_RCU_TYPE_MAX];
82329976Shselasky	int bsd_interrupt_value;
83337897Shselasky	struct work_struct *work;	/* current work struct, if set */
84369549Shselasky  	unsigned rcu_section[TS_RCU_TYPE_MAX];
85219820Sjeff};
86219820Sjeff
87328653Shselasky#define	current	({ \
88328653Shselasky	struct thread *__td = curthread; \
89328653Shselasky	linux_set_current(__td); \
90328653Shselasky	((struct task_struct *)__td->td_lkpi_task); \
91328653Shselasky})
92219820Sjeff
93328653Shselasky#define	task_pid_group_leader(task) (task)->task_thread->td_proc->p_pid
94328653Shselasky#define	task_pid(task)		((task)->pid)
95328653Shselasky#define	task_pid_nr(task)	((task)->pid)
96335417Shselasky#define	task_pid_vnr(task)	((task)->pid)
97328653Shselasky#define	get_pid(x)		(x)
98328653Shselasky#define	put_pid(x)		do { } while (0)
99328653Shselasky#define	current_euid()	(curthread->td_ucred->cr_uid)
100289993Shselasky
101328653Shselasky#define	set_task_state(task, x)		atomic_set(&(task)->state, (x))
102328653Shselasky#define	__set_task_state(task, x)	((task)->state.counter = (x))
103328653Shselasky#define	set_current_state(x)		set_task_state(current, x)
104328653Shselasky#define	__set_current_state(x)		__set_task_state(current, x)
105219820Sjeff
106328653Shselaskystatic inline void
107328653Shselaskyget_task_struct(struct task_struct *task)
108328653Shselasky{
109328653Shselasky	atomic_inc(&task->usage);
110328653Shselasky}
111219820Sjeff
112328653Shselaskystatic inline void
113328653Shselaskyput_task_struct(struct task_struct *task)
114328653Shselasky{
115328653Shselasky	if (atomic_dec_and_test(&task->usage))
116328653Shselasky		linux_free_current(task);
117328653Shselasky}
118219820Sjeff
119328655Shselasky#define	cond_resched()	do { if (!cold) sched_relinquish(curthread); } while (0)
120219820Sjeff
121328653Shselasky#define	yield()		kern_yield(PRI_UNCHANGED)
122219820Sjeff#define	sched_yield()	sched_relinquish(curthread)
123219820Sjeff
124328653Shselasky#define	need_resched() (curthread->td_flags & TDF_NEEDRESCHED)
125328653Shselasky
126328653Shselaskybool linux_signal_pending(struct task_struct *task);
127328653Shselaskybool linux_fatal_signal_pending(struct task_struct *task);
128328653Shselaskybool linux_signal_pending_state(long state, struct task_struct *task);
129328653Shselaskyvoid linux_send_sig(int signo, struct task_struct *task);
130328653Shselasky
131328653Shselasky#define	signal_pending(task)		linux_signal_pending(task)
132328653Shselasky#define	fatal_signal_pending(task)	linux_fatal_signal_pending(task)
133328653Shselasky#define	signal_pending_state(state, task)		\
134328653Shselasky	linux_signal_pending_state(state, task)
135328653Shselasky#define	send_sig(signo, task, priv) do {		\
136329976Shselasky	CTASSERT((priv) == 0);				\
137328653Shselasky	linux_send_sig(signo, task);			\
138328653Shselasky} while (0)
139328653Shselasky
140328653Shselaskyint linux_schedule_timeout(int timeout);
141328653Shselasky
142329976Shselaskystatic inline void
143329976Shselaskylinux_schedule_save_interrupt_value(struct task_struct *task, int value)
144329976Shselasky{
145329976Shselasky	task->bsd_interrupt_value = value;
146329976Shselasky}
147329976Shselasky
148345924Shselaskybool linux_task_exiting(struct task_struct *task);
149345924Shselasky
150345924Shselasky#define	current_exiting() \
151345924Shselasky	linux_task_exiting(current)
152345924Shselasky
153329976Shselaskystatic inline int
154329976Shselaskylinux_schedule_get_interrupt_value(struct task_struct *task)
155329976Shselasky{
156329976Shselasky	int value = task->bsd_interrupt_value;
157329976Shselasky	task->bsd_interrupt_value = 0;
158329976Shselasky	return (value);
159329976Shselasky}
160329976Shselasky
161328653Shselasky#define	schedule()					\
162328653Shselasky	(void)linux_schedule_timeout(MAX_SCHEDULE_TIMEOUT)
163328653Shselasky#define	schedule_timeout(timeout)			\
164328653Shselasky	linux_schedule_timeout(timeout)
165328653Shselasky#define	schedule_timeout_killable(timeout)		\
166334761Shselasky	schedule_timeout_interruptible(timeout)
167328653Shselasky#define	schedule_timeout_interruptible(timeout) ({	\
168328653Shselasky	set_current_state(TASK_INTERRUPTIBLE);		\
169328653Shselasky	schedule_timeout(timeout);			\
170328653Shselasky})
171328653Shselasky#define	schedule_timeout_uninterruptible(timeout) ({	\
172328653Shselasky	set_current_state(TASK_UNINTERRUPTIBLE);	\
173328653Shselasky	schedule_timeout(timeout);			\
174328653Shselasky})
175328653Shselasky
176328653Shselasky#define	io_schedule()			schedule()
177328653Shselasky#define	io_schedule_timeout(timeout)	schedule_timeout(timeout)
178328653Shselasky
179328653Shselaskystatic inline uint64_t
180328653Shselaskylocal_clock(void)
181289566Shselasky{
182328653Shselasky	struct timespec ts;
183289566Shselasky
184328653Shselasky	nanotime(&ts);
185328653Shselasky	return ((uint64_t)ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec);
186289566Shselasky}
187289566Shselasky
188345926Shselaskystatic inline const char *
189345926Shselaskyget_task_comm(char *buf, struct task_struct *task)
190345926Shselasky{
191345926Shselasky
192345926Shselasky	buf[0] = 0; /* buffer is too small */
193345926Shselasky	return (task->comm);
194345926Shselasky}
195345926Shselasky
196219820Sjeff#endif	/* _LINUX_SCHED_H_ */
197