1 /*
2  * Copyright (c) 2008-2010 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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
16  *    distribution.
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.
20  *
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
32  * SUCH DAMAGE.
33  */
34 /*
35  * Provides the vkernel with an asynchronous I/O mechanism using pthreads
36  * which operates outside the cpu abstraction.  Cothreads are intended to
37  * operate like DMA engines and may ONLY make libc and cothread_*() calls.
38  * The cothread may NOT call into the vkernel since abstractions like
39  * 'mycpu' do not exist for it.
40  */
41 
42 #include <sys/types.h>
43 #include <sys/interrupt.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/memrange.h>
47 #include <sys/tls.h>
48 #include <sys/bus.h>
49 #include <sys/thread2.h>
50 #include <time.h>
51 
52 #include <vm/vm_extern.h>
53 #include <vm/vm_kern.h>
54 #include <vm/vm_object.h>
55 #include <vm/vm_page.h>
56 
57 #include <machine/cpu.h>
58 #include <machine/cpufunc.h>
59 #include <machine/globaldata.h>
60 #include <machine/md_var.h>
61 #include <machine/pmap.h>
62 #include <machine/smp.h>
63 #include <machine/tls.h>
64 #include <machine/cothread.h>
65 
66 #include <unistd.h>
67 #include <pthread.h>
68 #include <signal.h>
69 #include <stdio.h>
70 
71 static void cothread_thread(void *arg);
72 extern int vmm_enabled;
73 
74 /*
75  * Create a co-processor thread for a virtual kernel.  This thread operates
76  * outside of the virtual kernel cpu abstraction and may only make direct
77  * cothread and libc calls.
78  */
79 cothread_t
80 cothread_create(void (*thr_func)(cothread_t cotd),
81 		void (*thr_intr)(cothread_t cotd),
82 		void *arg, const char *name)
83 {
84 	cothread_t cotd;
85 	void *stack;
86 	pthread_attr_t attr;
87 
88 	cotd = kmalloc(sizeof(*cotd), M_DEVBUF, M_WAITOK|M_ZERO);
89 	cotd->thr_intr = thr_intr;
90 	cotd->thr_func = thr_func;
91 	cotd->arg = arg;
92 	crit_enter();
93 	pthread_mutex_init(&cotd->mutex, NULL);
94 	pthread_cond_init(&cotd->cond, NULL);
95 	crit_exit();
96 
97 	cotd->pintr = pthread_self();
98 
99 	if (thr_intr) {
100 		cotd->intr_id = register_int_virtual(1, (void *)thr_intr,
101 						     cotd, name,
102 						     NULL, INTR_MPSAFE);
103 	}
104 
105 	/*
106 	 * The vkernel's cpu_disable_intr() masks signals.  We don't want
107 	 * our coprocessor thread taking any unix signals :-)
108 	 */
109 	pthread_attr_init(&attr);
110 	if (vmm_enabled) {
111 		stack = mmap(NULL, KERNEL_STACK_SIZE,
112 			     PROT_READ|PROT_WRITE|PROT_EXEC,
113 			     MAP_ANON, -1, 0);
114 		if (stack == MAP_FAILED) {
115 			panic("Unable to allocate stack for cothread\n");
116 		}
117 		pthread_attr_setstack(&attr, stack, KERNEL_STACK_SIZE);
118 	}
119 	crit_enter();
120 	cpu_mask_all_signals();
121 	pthread_create(&cotd->pthr, &attr, (void *)cothread_thread, cotd);
122 	cpu_unmask_all_signals();
123 	crit_exit();
124 	pthread_attr_destroy(&attr);
125 
126 	return(cotd);
127 }
128 
129 /*
130  * Wait for the target thread to terminate and then destroy the cothread
131  * structure.
132  */
133 void
134 cothread_delete(cothread_t *cotdp)
135 {
136 	cothread_t cotd;
137 
138 	if ((cotd = *cotdp) != NULL) {
139 		if (cotd->thr_intr)
140 			unregister_int_virtual(cotd->intr_id);
141 		crit_enter();
142 		pthread_join(cotd->pthr, NULL);
143 		crit_exit();
144 		kfree(cotd, M_DEVBUF);
145 		*cotdp = NULL;
146 	}
147 }
148 
149 static void
150 cothread_thread(void *arg)
151 {
152 	cothread_t cotd = arg;
153 
154 	cpu_mask_all_signals(); /* XXX remove me? should already be masked */
155 	/*
156 	 * %gs (aka mycpu) is illegal in cothreads.   Note that %fs is used
157 	 * by pthreads.
158 	 */
159 	/* JG try another approach? */
160 	tls_set_gs(0, sizeof(struct privatespace));
161 	cotd->thr_func(cotd);
162 }
163 
164 /*
165  * Called by the cothread to generate an interrupt back to the vkernel.
166  */
167 void
168 cothread_intr(cothread_t cotd)
169 {
170 	pthread_kill(cotd->pintr, SIGALRM);
171 }
172 
173 /*
174  * Called by the vkernel to wakeup a cothread.
175  * The cothread must be locked.
176  */
177 void
178 cothread_signal(cothread_t cotd)
179 {
180 	pthread_cond_signal(&cotd->cond);
181 }
182 
183 /*
184  * Called by the cothread to wait for the vkernel to call cothread_signal().
185  * The cothread must be locked.
186  */
187 void
188 cothread_wait(cothread_t cotd)
189 {
190 	pthread_cond_wait(&cotd->cond, &cotd->mutex);
191 }
192 
193 /*
194  * Used for systimer support
195  */
196 void
197 cothread_sleep(cothread_t cotd, struct timespec *ts)
198 {
199 	nanosleep(ts, NULL);
200 }
201 
202 void
203 cothread_wakeup(cothread_t cotd, struct timespec *ts)
204 {
205 	ts->tv_sec = 0;
206 	ts->tv_nsec = 0;
207 	pthread_kill(cotd->pthr, SIGINT);
208 }
209 
210 /*
211  * Typically called by kernel thread or cothread
212  *
213  * These must be a matched pair.  We will acquire a critical
214  * section in cothread_lock() and release it in cothread_unlock().
215  *
216  * We do this to simplify cothread operation to prevent an
217  * interrupt (e.g. vkd_io_intr()) from preempting a vkd_strategy()
218  * call and creating a recursion in the pthread.
219  */
220 void
221 cothread_lock(cothread_t cotd, int is_cotd)
222 {
223 	if (is_cotd) {
224 		pthread_mutex_lock(&cotd->mutex);
225 	} else {
226 		crit_enter_id("cothread");
227 		pthread_mutex_lock(&cotd->mutex);
228 	}
229 }
230 
231 void
232 cothread_unlock(cothread_t cotd, int is_cotd)
233 {
234 	if (is_cotd) {
235 		pthread_mutex_unlock(&cotd->mutex);
236 	} else {
237 		pthread_mutex_unlock(&cotd->mutex);
238 		crit_exit_id("cothread");
239 	}
240 }
241