xref: /dragonfly/sys/dev/drm/include/linux/sched.h (revision f1e2e235)
1 /*
2  * Copyright (c) 2015-2019 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/cpumask.h>
38 #include <linux/errno.h>
39 #include <linux/mm_types.h>
40 #include <linux/preempt.h>
41 
42 #include <asm/page.h>
43 
44 #include <linux/smp.h>
45 #include <linux/compiler.h>
46 #include <linux/completion.h>
47 #include <linux/pid.h>
48 #include <linux/rcupdate.h>
49 #include <linux/rculist.h>
50 
51 #include <linux/time.h>
52 #include <linux/timer.h>
53 #include <linux/hrtimer.h>
54 #include <linux/gfp.h>
55 
56 #include <asm/processor.h>
57 
58 #include <linux/spinlock.h>
59 
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/proc.h>
63 #include <sys/sched.h>
64 #include <sys/signal2.h>
65 
66 struct seq_file;
67 
68 #define	TASK_RUNNING		0
69 #define	TASK_INTERRUPTIBLE	1
70 #define	TASK_UNINTERRUPTIBLE	2
71 
72 #define TASK_NORMAL		(TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)
73 
74 #define MAX_SCHEDULE_TIMEOUT    LONG_MAX
75 
76 struct task_struct {
77 	struct thread *dfly_td;
78 	volatile long state;
79 	struct mm_struct *mm;	/* mirror copy in p->p_linux_mm */
80 	int prio;
81 };
82 
83 /*
84  * schedule_timeout: puts the current thread to sleep until timeout
85  * if its state allows it to.
86  */
87 static inline long
88 schedule_timeout(signed long timeout)
89 {
90 	unsigned long time_before, time_after;
91 	long slept, ret = 0;
92 	int timo;
93 
94 	if (timeout < 0) {
95 		kprintf("schedule_timeout(): timeout cannot be negative\n");
96 		goto done;
97 	}
98 
99 	/*
100 	 * Indefinite wait if timeout is MAX_SCHEDULE_TIMEOUT, but we are
101 	 * also translating to an integer.  The first conditional will
102 	 * cover both but to code defensively test both.
103 	 */
104 	if (timeout >= INT_MAX || timeout == MAX_SCHEDULE_TIMEOUT)
105 		timo = 0;
106 	else
107 		timo = timeout;
108 
109 	switch (current->state) {
110 	case TASK_INTERRUPTIBLE:
111 		time_before = ticks;
112 		tsleep(current, PCATCH, "lstim", timo);
113 		time_after = ticks;
114 		slept = time_after - time_before;
115 		ret = timeout - slept;
116 		if (ret < 0)
117 			ret = 0;
118 		break;
119 	case TASK_UNINTERRUPTIBLE:
120 		tsleep(current, 0, "lstim", timo);
121 		break;
122 	default:
123 		/* We are supposed to return immediately here */
124 		tsleep(current, 0, "lstim", 1);
125 		break;
126 	}
127 
128 done:
129 	if (timeout == MAX_SCHEDULE_TIMEOUT)
130 		ret = MAX_SCHEDULE_TIMEOUT;
131 
132 	current->state = TASK_RUNNING;
133 	return ret;
134 }
135 
136 static inline long
137 io_schedule_timeout(signed long timeout)
138 {
139 	return schedule_timeout(timeout);
140 }
141 
142 #define TASK_COMM_LEN	MAXCOMLEN
143 
144 /*
145  * local_clock: fast time source, monotonic on the same cpu
146  */
147 static inline uint64_t
148 local_clock(void)
149 {
150 	struct timespec ts;
151 
152 	getnanouptime(&ts);
153 	return (ts.tv_sec * NSEC_PER_SEC) + ts.tv_nsec;
154 }
155 
156 static inline void
157 yield(void)
158 {
159 	lwkt_yield();
160 }
161 
162 #define __set_current_state(state_value)	current->state = (state_value);
163 
164 #define set_current_state(state_value)		\
165 do {						\
166 	__set_current_state(state_value);	\
167 	mb();					\
168 } while (0)
169 
170 static inline int
171 wake_up_process(struct task_struct *tsk)
172 {
173 	/* Among other things, this function is supposed to act as
174 	 * a barrier */
175 	smp_wmb();
176 	wakeup(tsk);
177 
178 	return 1;	/* Always indicate the process was woken up */
179 }
180 
181 static inline int
182 signal_pending(struct task_struct *p)
183 {
184 	struct thread *t = p->dfly_td;
185 
186 	/* Some kernel threads do not have lwp, t->td_lwp can be NULL */
187 	if (t->td_lwp == NULL)
188 		return 0;
189 
190 	return CURSIG(t->td_lwp);
191 }
192 
193 static inline int
194 fatal_signal_pending(struct task_struct *p)
195 {
196 	struct thread *t = p->dfly_td;
197 	sigset_t pending_set;
198 
199 	/* Some kernel threads do not have lwp, t->td_lwp can be NULL */
200 	if (t->td_lwp == NULL)
201 		return 0;
202 
203 	pending_set = lwp_sigpend(t->td_lwp);
204 	return SIGISMEMBER(pending_set, SIGKILL);
205 }
206 
207 static inline int
208 signal_pending_state(long state, struct task_struct *p)
209 {
210 	if (state & TASK_INTERRUPTIBLE)
211 		return (signal_pending(p));
212 	else
213 		return (fatal_signal_pending(p));
214 }
215 
216 /* Explicit rescheduling in order to reduce latency */
217 static inline int
218 cond_resched(void)
219 {
220 	lwkt_yield();
221 	return 0;
222 }
223 
224 static inline int
225 send_sig(int sig, struct proc *p, int priv)
226 {
227 	ksignal(p, sig);
228 	return 0;
229 }
230 
231 static inline void
232 set_need_resched(void)
233 {
234 	/* do nothing for now */
235 	/* used on ttm_bo_reserve failures */
236 }
237 
238 #endif	/* _LINUX_SCHED_H_ */
239