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/errno.h>
37 #include <linux/kernel.h>
38 #include <linux/list.h>
39 #include <linux/sched.h>
40 #include <linux/spinlock.h>
41 #include <linux/wait.h>
44 linux_add_to_sleepqueue(void *wchan, const char *wmesg, int timeout, int state)
48 MPASS((state & ~TASK_NORMAL) == 0);
50 flags = SLEEPQ_SLEEP | ((state & TASK_INTERRUPTIBLE) != 0 ?
51 SLEEPQ_INTERRUPTIBLE : 0);
53 sleepq_add(wchan, NULL, wmesg, flags, 0);
55 sleepq_set_timeout(wchan, timeout);
56 if ((state & TASK_INTERRUPTIBLE) != 0) {
58 ret = -sleepq_wait_sig(wchan, 0);
60 ret = -sleepq_timedwait_sig(wchan, 0);
63 sleepq_wait(wchan, 0);
66 ret = -sleepq_timedwait(wchan, 0);
68 /* filter return value */
69 if (ret != 0 && ret != -EWOULDBLOCK)
75 wake_up_task(struct task_struct *task, unsigned int state)
77 int ret, wakeup_swapper;
79 ret = wakeup_swapper = 0;
81 if ((atomic_read(&task->state) & state) != 0) {
82 set_task_state(task, TASK_WAKING);
83 wakeup_swapper = sleepq_signal(task, SLEEPQ_SLEEP, 0, 0);
93 linux_signal_pending(struct task_struct *task)
98 td = task->task_thread;
99 PROC_LOCK(td->td_proc);
100 pending = td->td_siglist;
101 SIGSETOR(pending, td->td_proc->p_siglist);
102 SIGSETNAND(pending, td->td_sigmask);
103 PROC_UNLOCK(td->td_proc);
104 return (!SIGISEMPTY(pending));
108 linux_fatal_signal_pending(struct task_struct *task)
113 td = task->task_thread;
114 PROC_LOCK(td->td_proc);
115 ret = SIGISMEMBER(td->td_siglist, SIGKILL) ||
116 SIGISMEMBER(td->td_proc->p_siglist, SIGKILL);
117 PROC_UNLOCK(td->td_proc);
122 linux_signal_pending_state(long state, struct task_struct *task)
125 MPASS((state & ~TASK_NORMAL) == 0);
127 if ((state & TASK_INTERRUPTIBLE) == 0)
129 return (linux_signal_pending(task));
133 linux_send_sig(int signo, struct task_struct *task)
137 td = task->task_thread;
138 PROC_LOCK(td->td_proc);
140 PROC_UNLOCK(td->td_proc);
144 autoremove_wake_function(wait_queue_t *wq, unsigned int state, int flags,
147 struct task_struct *task;
151 if ((ret = wake_up_task(task, state)) != 0)
152 list_del_init(&wq->task_list);
157 linux_wake_up(wait_queue_head_t *wqh, unsigned int state, int nr, bool locked)
159 wait_queue_t *pos, *next;
162 spin_lock(&wqh->lock);
163 list_for_each_entry_safe(pos, next, &wqh->task_list, task_list) {
164 if (pos->func == NULL) {
165 if (wake_up_task(pos->private, state) != 0 && --nr == 0)
168 if (pos->func(pos, state, 0, NULL) != 0 && --nr == 0)
173 spin_unlock(&wqh->lock);
177 linux_prepare_to_wait(wait_queue_head_t *wqh, wait_queue_t *wq, int state)
180 spin_lock(&wqh->lock);
181 if (list_empty(&wq->task_list))
182 __add_wait_queue(wqh, wq);
183 set_task_state(current, state);
184 spin_unlock(&wqh->lock);
188 linux_finish_wait(wait_queue_head_t *wqh, wait_queue_t *wq)
191 spin_lock(&wqh->lock);
192 set_task_state(current, TASK_RUNNING);
193 if (!list_empty(&wq->task_list)) {
194 __remove_wait_queue(wqh, wq);
195 INIT_LIST_HEAD(&wq->task_list);
197 spin_unlock(&wqh->lock);
201 linux_waitqueue_active(wait_queue_head_t *wqh)
205 spin_lock(&wqh->lock);
206 ret = !list_empty(&wqh->task_list);
207 spin_unlock(&wqh->lock);
212 linux_wait_event_common(wait_queue_head_t *wqh, wait_queue_t *wq, int timeout,
213 unsigned int state, spinlock_t *lock)
215 struct task_struct *task;
219 spin_unlock_irq(lock);
223 /* range check timeout */
226 else if (timeout == MAX_SCHEDULE_TIMEOUT)
232 * Our wait queue entry is on the stack - make sure it doesn't
233 * get swapped out while we sleep.
235 PHOLD(task->task_thread->td_proc);
237 if (atomic_read(&task->state) != TASK_WAKING) {
238 ret = linux_add_to_sleepqueue(task, "wevent", timeout, state);
240 sleepq_release(task);
241 ret = linux_signal_pending_state(state, task) ? -ERESTARTSYS : 0;
243 PRELE(task->task_thread->td_proc);
253 linux_schedule_timeout(int timeout)
255 struct task_struct *task;
261 /* range check timeout */
264 else if (timeout == MAX_SCHEDULE_TIMEOUT)
267 remainder = ticks + timeout;
272 state = atomic_read(&task->state);
273 if (state != TASK_WAKING)
274 (void)linux_add_to_sleepqueue(task, "sched", timeout, state);
276 sleepq_release(task);
277 set_task_state(task, TASK_RUNNING);
282 return (MAX_SCHEDULE_TIMEOUT);
284 /* range check return value */
288 else if (remainder > timeout)
294 wake_up_sleepers(void *wchan)
299 wakeup_swapper = sleepq_signal(wchan, SLEEPQ_SLEEP, 0, 0);
300 sleepq_release(wchan);
305 #define bit_to_wchan(word, bit) ((void *)(((uintptr_t)(word) << 6) | (bit)))
308 linux_wake_up_bit(void *word, int bit)
311 wake_up_sleepers(bit_to_wchan(word, bit));
315 linux_wait_on_bit_timeout(unsigned long *word, int bit, unsigned int state,
318 struct task_struct *task;
324 /* range check timeout */
327 else if (timeout == MAX_SCHEDULE_TIMEOUT)
331 wchan = bit_to_wchan(word, bit);
334 if ((*word & (1 << bit)) == 0) {
335 sleepq_release(wchan);
339 set_task_state(task, state);
340 ret = linux_add_to_sleepqueue(wchan, "wbit", timeout, state);
344 set_task_state(task, TASK_RUNNING);
352 linux_wake_up_atomic_t(atomic_t *a)
359 linux_wait_on_atomic_t(atomic_t *a, unsigned int state)
361 struct task_struct *task;
371 if (atomic_read(a) == 0) {
372 sleepq_release(wchan);
376 set_task_state(task, state);
377 ret = linux_add_to_sleepqueue(wchan, "watomic", 0, state);
381 set_task_state(task, TASK_RUNNING);
389 linux_wake_up_state(struct task_struct *task, unsigned int state)
392 return (wake_up_task(task, state) != 0);