2 * Copyright (c) 2008-2010 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/platform/vkernel/platform/cothread.c,v 1.3 2008/05/07 17:19:47 dillon Exp $
37 * Provides the vkernel with an asynchronous I/O mechanism using pthreads
38 * which operates outside the cpu abstraction. Cothreads are intended to
39 * operate like DMA engines and may ONLY make libc and cothread_*() calls.
40 * The cothread may NOT call into the vkernel since abstractions like
41 * 'mycpu' do not exist for it.
44 #include <sys/interrupt.h>
45 #include <sys/kernel.h>
46 #include <sys/memrange.h>
48 #include <sys/types.h>
51 #include <vm/vm_extern.h>
52 #include <vm/vm_kern.h>
53 #include <vm/vm_object.h>
54 #include <vm/vm_page.h>
56 #include <machine/cpu.h>
57 #include <machine/cpufunc.h>
58 #include <machine/globaldata.h>
59 #include <machine/md_var.h>
60 #include <machine/pmap.h>
61 #include <machine/smp.h>
62 #include <machine/tls.h>
63 #include <machine/cothread.h>
70 static void cothread_thread(void *arg);
73 * Create a co-processor thread for a virtual kernel. This thread operates
74 * outside of the virtual kernel cpu abstraction and may only make direct
75 * cothread and libc calls.
78 cothread_create(void (*thr_func)(cothread_t cotd),
79 void (*thr_intr)(cothread_t cotd),
80 void *arg, const char *name)
84 cotd = kmalloc(sizeof(*cotd), M_DEVBUF, M_WAITOK|M_ZERO);
85 cotd->thr_intr = thr_intr;
86 cotd->thr_func = thr_func;
89 pthread_mutex_init(&cotd->mutex, NULL);
90 pthread_cond_init(&cotd->cond, NULL);
93 cotd->pintr = pthread_self();
95 cotd->intr_id = register_int(1, (void *)thr_intr, cotd, name, NULL,
99 * The vkernel's cpu_disable_intr() masks signals. We don't want
100 * our coprocessor thread taking any unix signals :-)
103 cpu_mask_all_signals();
104 pthread_create(&cotd->pthr, NULL, (void *)cothread_thread, cotd);
105 cpu_unmask_all_signals();
111 * Wait for the target thread to terminate and then destroy the cothread
115 cothread_delete(cothread_t *cotdp)
119 if ((cotd = *cotdp) != NULL) {
120 unregister_int(cotd->intr_id, 0);
122 pthread_join(cotd->pthr, NULL);
124 kfree(cotd, M_DEVBUF);
130 cothread_thread(void *arg)
132 cothread_t cotd = arg;
134 cpu_mask_all_signals(); /* XXX remove me? should already be masked */
136 * %gs (aka mycpu) is illegal in cothreads. Note that %fs is used
139 /* JG try another approach? */
140 tls_set_gs(0, sizeof(struct privatespace));
141 cotd->thr_func(cotd);
145 * Called by the cothread to generate an interrupt back to the vkernel.
148 cothread_intr(cothread_t cotd)
150 pthread_kill(cotd->pintr, SIGIO);
154 * Called by the vkernel to wakeup a cothread.
155 * The cothread must be locked.
158 cothread_signal(cothread_t cotd)
160 pthread_cond_signal(&cotd->cond);
164 * Called by the cothread to wait for the vkernel to call cothread_signal().
165 * The cothread must be locked.
168 cothread_wait(cothread_t cotd)
170 pthread_cond_wait(&cotd->cond, &cotd->mutex);
174 * Typically called by kernel thread or cothread
176 * These must be a matched pair. We will acquire a critical
177 * section in cothread_lock() and release it in cothread_unlock().
179 * We do this to simplify cothread operation to prevent an
180 * interrupt (e.g. vkd_io_intr()) from preempting a vkd_strategy()
181 * call and creating a recursion in the pthread.
184 cothread_lock(cothread_t cotd, int is_cotd)
187 pthread_mutex_lock(&cotd->mutex);
189 crit_enter_id("cothread");
190 pthread_mutex_lock(&cotd->mutex);
195 cothread_unlock(cothread_t cotd, int is_cotd)
198 pthread_mutex_unlock(&cotd->mutex);
200 pthread_mutex_unlock(&cotd->mutex);
201 crit_exit_id("cothread");