2 * Copyright (c) 2017 Mark Johnston <markj@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conds
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conds, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conds and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/signalvar.h>
34 #include <sys/sleepqueue.h>
36 #include <linux/delay.h>
37 #include <linux/errno.h>
38 #include <linux/kernel.h>
39 #include <linux/list.h>
40 #include <linux/sched.h>
41 #include <linux/spinlock.h>
42 #include <linux/wait.h>
45 linux_add_to_sleepqueue(void *wchan, struct task_struct *task,
46 const char *wmesg, int timeout, int state)
50 MPASS((state & ~(TASK_PARKED | TASK_NORMAL)) == 0);
52 flags = SLEEPQ_SLEEP | ((state & TASK_INTERRUPTIBLE) != 0 ?
53 SLEEPQ_INTERRUPTIBLE : 0);
55 sleepq_add(wchan, NULL, wmesg, flags, 0);
57 sleepq_set_timeout(wchan, timeout);
60 if ((state & TASK_INTERRUPTIBLE) != 0) {
62 ret = -sleepq_wait_sig(wchan, 0);
64 ret = -sleepq_timedwait_sig(wchan, 0);
67 sleepq_wait(wchan, 0);
70 ret = -sleepq_timedwait(wchan, 0);
74 /* filter return value */
75 if (ret != 0 && ret != -EWOULDBLOCK) {
76 linux_schedule_save_interrupt_value(task, ret);
83 linux_msleep_interruptible(unsigned int ms)
87 /* guard against invalid values */
90 ret = -pause_sbt("lnxsleep", mstosbt(ms), 0, C_HARDCLOCK | C_CATCH);
96 linux_schedule_save_interrupt_value(current, ret);
102 wake_up_task(struct task_struct *task, unsigned int state)
104 int ret, wakeup_swapper;
106 ret = wakeup_swapper = 0;
108 if ((atomic_read(&task->state) & state) != 0) {
109 set_task_state(task, TASK_WAKING);
110 wakeup_swapper = sleepq_signal(task, SLEEPQ_SLEEP, 0, 0);
113 sleepq_release(task);
120 linux_signal_pending(struct task_struct *task)
125 td = task->task_thread;
126 PROC_LOCK(td->td_proc);
127 pending = td->td_siglist;
128 SIGSETOR(pending, td->td_proc->p_siglist);
129 SIGSETNAND(pending, td->td_sigmask);
130 PROC_UNLOCK(td->td_proc);
131 return (!SIGISEMPTY(pending));
135 linux_fatal_signal_pending(struct task_struct *task)
140 td = task->task_thread;
141 PROC_LOCK(td->td_proc);
142 ret = SIGISMEMBER(td->td_siglist, SIGKILL) ||
143 SIGISMEMBER(td->td_proc->p_siglist, SIGKILL);
144 PROC_UNLOCK(td->td_proc);
149 linux_signal_pending_state(long state, struct task_struct *task)
152 MPASS((state & ~TASK_NORMAL) == 0);
154 if ((state & TASK_INTERRUPTIBLE) == 0)
156 return (linux_signal_pending(task));
160 linux_send_sig(int signo, struct task_struct *task)
164 td = task->task_thread;
165 PROC_LOCK(td->td_proc);
167 PROC_UNLOCK(td->td_proc);
171 autoremove_wake_function(wait_queue_t *wq, unsigned int state, int flags,
174 struct task_struct *task;
178 if ((ret = wake_up_task(task, state)) != 0)
179 list_del_init(&wq->task_list);
184 default_wake_function(wait_queue_t *wq, unsigned int state, int flags,
187 return (wake_up_task(wq->private, state));
191 linux_init_wait_entry(wait_queue_t *wq, int flags)
194 memset(wq, 0, sizeof(*wq));
196 wq->private = current;
197 wq->func = autoremove_wake_function;
198 INIT_LIST_HEAD(&wq->task_list);
202 linux_wake_up(wait_queue_head_t *wqh, unsigned int state, int nr, bool locked)
204 wait_queue_t *pos, *next;
207 spin_lock(&wqh->lock);
208 list_for_each_entry_safe(pos, next, &wqh->task_list, task_list) {
209 if (pos->func == NULL) {
210 if (wake_up_task(pos->private, state) != 0 && --nr == 0)
213 if (pos->func(pos, state, 0, NULL) != 0 && --nr == 0)
218 spin_unlock(&wqh->lock);
222 linux_prepare_to_wait(wait_queue_head_t *wqh, wait_queue_t *wq, int state)
225 spin_lock(&wqh->lock);
226 if (list_empty(&wq->task_list))
227 __add_wait_queue(wqh, wq);
228 set_task_state(current, state);
229 spin_unlock(&wqh->lock);
233 linux_finish_wait(wait_queue_head_t *wqh, wait_queue_t *wq)
236 spin_lock(&wqh->lock);
237 set_task_state(current, TASK_RUNNING);
238 if (!list_empty(&wq->task_list)) {
239 __remove_wait_queue(wqh, wq);
240 INIT_LIST_HEAD(&wq->task_list);
242 spin_unlock(&wqh->lock);
246 linux_waitqueue_active(wait_queue_head_t *wqh)
250 spin_lock(&wqh->lock);
251 ret = !list_empty(&wqh->task_list);
252 spin_unlock(&wqh->lock);
257 linux_wait_event_common(wait_queue_head_t *wqh, wait_queue_t *wq, int timeout,
258 unsigned int state, spinlock_t *lock)
260 struct task_struct *task;
264 spin_unlock_irq(lock);
266 /* range check timeout */
269 else if (timeout == MAX_SCHEDULE_TIMEOUT)
275 * Our wait queue entry is on the stack - make sure it doesn't
276 * get swapped out while we sleep.
278 PHOLD(task->task_thread->td_proc);
280 if (atomic_read(&task->state) != TASK_WAKING) {
281 ret = linux_add_to_sleepqueue(task, task, "wevent", timeout,
284 sleepq_release(task);
287 PRELE(task->task_thread->td_proc);
295 linux_schedule_timeout(int timeout)
297 struct task_struct *task;
304 /* range check timeout */
307 else if (timeout == MAX_SCHEDULE_TIMEOUT)
310 remainder = ticks + timeout;
313 state = atomic_read(&task->state);
314 if (state != TASK_WAKING) {
315 ret = linux_add_to_sleepqueue(task, task, "sched", timeout,
318 sleepq_release(task);
321 set_task_state(task, TASK_RUNNING);
324 return (MAX_SCHEDULE_TIMEOUT);
326 /* range check return value */
329 /* range check return value */
330 if (ret == -ERESTARTSYS && remainder < 1)
332 else if (remainder < 0)
334 else if (remainder > timeout)
340 wake_up_sleepers(void *wchan)
345 wakeup_swapper = sleepq_signal(wchan, SLEEPQ_SLEEP, 0, 0);
346 sleepq_release(wchan);
351 #define bit_to_wchan(word, bit) ((void *)(((uintptr_t)(word) << 6) | (bit)))
354 linux_wake_up_bit(void *word, int bit)
357 wake_up_sleepers(bit_to_wchan(word, bit));
361 linux_wait_on_bit_timeout(unsigned long *word, int bit, unsigned int state,
364 struct task_struct *task;
368 /* range check timeout */
371 else if (timeout == MAX_SCHEDULE_TIMEOUT)
375 wchan = bit_to_wchan(word, bit);
378 if ((*word & (1 << bit)) == 0) {
379 sleepq_release(wchan);
383 set_task_state(task, state);
384 ret = linux_add_to_sleepqueue(wchan, task, "wbit", timeout,
389 set_task_state(task, TASK_RUNNING);
395 linux_wake_up_atomic_t(atomic_t *a)
402 linux_wait_on_atomic_t(atomic_t *a, unsigned int state)
404 struct task_struct *task;
412 if (atomic_read(a) == 0) {
413 sleepq_release(wchan);
417 set_task_state(task, state);
418 ret = linux_add_to_sleepqueue(wchan, task, "watomic", 0, state);
422 set_task_state(task, TASK_RUNNING);
428 linux_wake_up_state(struct task_struct *task, unsigned int state)
431 return (wake_up_task(task, state) != 0);