xref: /dragonfly/sys/dev/drm/include/linux/sched.h (revision 08e4ff68)
1 /*
2  * Copyright (c) 2015-2020 François Tigeot <ftigeot@wolfpond.org>
3  * Copyright (c) 2019 Matthew Dillon <dillon@backplane.com>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice unmodified, this list of conditions, and the following
11  *    disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef	_LINUX_SCHED_H_
29 #define	_LINUX_SCHED_H_
30 
31 #include <linux/capability.h>
32 #include <linux/threads.h>
33 #include <linux/kernel.h>
34 #include <linux/types.h>
35 #include <linux/jiffies.h>
36 #include <linux/rbtree.h>
37 #include <linux/thread_info.h>
38 #include <linux/cpumask.h>
39 #include <linux/errno.h>
40 #include <linux/mm_types.h>
41 #include <linux/preempt.h>
42 
43 #include <asm/page.h>
44 
45 #include <linux/smp.h>
46 #include <linux/compiler.h>
47 #include <linux/completion.h>
48 #include <linux/pid.h>
49 #include <linux/rcupdate.h>
50 #include <linux/rculist.h>
51 
52 #include <linux/time.h>
53 #include <linux/timer.h>
54 #include <linux/hrtimer.h>
55 #include <linux/llist.h>
56 #include <linux/gfp.h>
57 
58 #include <asm/processor.h>
59 
60 #include <linux/spinlock.h>
61 
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/proc.h>
65 #include <sys/sched.h>
66 #include <sys/signal2.h>
67 
68 #include <machine/cpu.h>
69 
70 struct seq_file;
71 
72 #define	TASK_RUNNING		0
73 #define	TASK_INTERRUPTIBLE	1
74 #define	TASK_UNINTERRUPTIBLE	2
75 
76 #define TASK_NORMAL		(TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)
77 
78 #define MAX_SCHEDULE_TIMEOUT    LONG_MAX
79 
80 #define TASK_COMM_LEN	MAXCOMLEN
81 
82 struct task_struct {
83 	struct thread *dfly_td;
84 	volatile long state;
85 	struct mm_struct *mm;	/* mirror copy in p->p_linux_mm */
86 	int prio;
87 
88 	/* kthread-specific data */
89 	unsigned long		kt_flags;
90 	int			(*kt_fn)(void *data);
91 	void			*kt_fndata;
92 	int			kt_exitvalue;
93 
94 	/* executable name without path */
95 	char			comm[TASK_COMM_LEN];
96 };
97 
98 #define __set_current_state(state_value)	current->state = (state_value);
99 
100 #define set_current_state(state_value)		\
101 do {						\
102 	__set_current_state(state_value);	\
103 	mb();					\
104 } while (0)
105 
106 /*
107  * schedule_timeout: puts the current thread to sleep until timeout
108  * if its state allows it to.
109  */
110 static inline long
111 schedule_timeout(signed long timeout)
112 {
113 	unsigned long time_before, time_after;
114 	long slept, ret = 0;
115 	int timo;
116 
117 	if (timeout < 0) {
118 		kprintf("schedule_timeout(): timeout cannot be negative\n");
119 		goto done;
120 	}
121 
122 	/*
123 	 * Indefinite wait if timeout is MAX_SCHEDULE_TIMEOUT, but we are
124 	 * also translating to an integer.  The first conditional will
125 	 * cover both but to code defensively test both.
126 	 */
127 	if (timeout >= INT_MAX || timeout == MAX_SCHEDULE_TIMEOUT)
128 		timo = 0;
129 	else
130 		timo = timeout;
131 
132 	switch (current->state) {
133 	case TASK_INTERRUPTIBLE:
134 		time_before = ticks;
135 		tsleep(current, PCATCH, "lstim", timo);
136 		time_after = ticks;
137 		slept = time_after - time_before;
138 		ret = timeout - slept;
139 		if (ret < 0)
140 			ret = 0;
141 		break;
142 	case TASK_UNINTERRUPTIBLE:
143 		tsleep(current, 0, "lstim", timo);
144 		break;
145 	default:
146 		break;
147 	}
148 
149 done:
150 	if (timeout == MAX_SCHEDULE_TIMEOUT)
151 		ret = MAX_SCHEDULE_TIMEOUT;
152 
153 	current->state = TASK_RUNNING;
154 	return ret;
155 }
156 
157 static inline void
158 schedule(void)
159 {
160 	(void)schedule_timeout(MAX_SCHEDULE_TIMEOUT);
161 }
162 
163 static inline signed long
164 schedule_timeout_uninterruptible(signed long timeout)
165 {
166 	__set_current_state(TASK_UNINTERRUPTIBLE);
167 	return schedule_timeout(timeout);
168 }
169 
170 static inline long
171 io_schedule_timeout(signed long timeout)
172 {
173 	return schedule_timeout(timeout);
174 }
175 
176 /*
177  * local_clock: fast time source, monotonic on the same cpu
178  */
179 static inline uint64_t
180 local_clock(void)
181 {
182 	struct timespec ts;
183 
184 	getnanouptime(&ts);
185 	return (ts.tv_sec * NSEC_PER_SEC) + ts.tv_nsec;
186 }
187 
188 static inline void
189 yield(void)
190 {
191 	lwkt_yield();
192 }
193 
194 static inline int
195 wake_up_process(struct task_struct *tsk)
196 {
197 	/* Among other things, this function is supposed to act as
198 	 * a barrier */
199 	smp_wmb();
200 	wakeup(tsk);
201 
202 	return 1;	/* Always indicate the process was woken up */
203 }
204 
205 static inline int
206 signal_pending(struct task_struct *p)
207 {
208 	struct thread *t = p->dfly_td;
209 
210 	/* Some kernel threads do not have lwp, t->td_lwp can be NULL */
211 	if (t->td_lwp == NULL)
212 		return 0;
213 
214 	return CURSIG(t->td_lwp);
215 }
216 
217 static inline int
218 fatal_signal_pending(struct task_struct *p)
219 {
220 	struct thread *t = p->dfly_td;
221 	sigset_t pending_set;
222 
223 	/* Some kernel threads do not have lwp, t->td_lwp can be NULL */
224 	if (t->td_lwp == NULL)
225 		return 0;
226 
227 	pending_set = lwp_sigpend(t->td_lwp);
228 	return SIGISMEMBER(pending_set, SIGKILL);
229 }
230 
231 static inline int
232 signal_pending_state(long state, struct task_struct *p)
233 {
234 	if (state & TASK_INTERRUPTIBLE)
235 		return (signal_pending(p));
236 	else
237 		return (fatal_signal_pending(p));
238 }
239 
240 /* Explicit rescheduling in order to reduce latency */
241 static inline int
242 cond_resched(void)
243 {
244 	lwkt_yield();
245 	return 0;
246 }
247 
248 static inline int
249 send_sig(int sig, struct proc *p, int priv)
250 {
251 	ksignal(p, sig);
252 	return 0;
253 }
254 
255 static inline void
256 set_need_resched(void)
257 {
258 	/* do nothing for now */
259 	/* used on ttm_bo_reserve failures */
260 }
261 
262 static inline bool
263 need_resched(void)
264 {
265 	return any_resched_wanted();
266 }
267 
268 static inline int
269 sched_setscheduler_nocheck(struct task_struct *ts,
270 			   int policy, const struct sched_param *param)
271 {
272 	/* We do not allow different thread scheduling policies */
273 	return 0;
274 }
275 
276 static inline int
277 pagefault_disabled(void)
278 {
279 	return (curthread->td_flags & TDF_NOFAULT);
280 }
281 
282 static inline void
283 mmgrab(struct mm_struct *mm)
284 {
285 	atomic_inc(&mm->mm_count);
286 }
287 
288 #endif	/* _LINUX_SCHED_H_ */
289