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 * $DragonFly: src/sys/platform/vkernel/platform/cothread.c,v 1.3 2008/05/07 17:19:47 dillon Exp $ 35 */ 36 /* 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. 42 */ 43 44 #include <sys/interrupt.h> 45 #include <sys/kernel.h> 46 #include <sys/memrange.h> 47 #include <sys/tls.h> 48 #include <sys/types.h> 49 #include <sys/bus.h> 50 51 #include <vm/vm_extern.h> 52 #include <vm/vm_kern.h> 53 #include <vm/vm_object.h> 54 #include <vm/vm_page.h> 55 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> 64 65 #include <unistd.h> 66 #include <pthread.h> 67 #include <signal.h> 68 #include <stdio.h> 69 70 static void cothread_thread(void *arg); 71 extern int vmm_enabled; 72 73 /* 74 * Create a co-processor thread for a virtual kernel. This thread operates 75 * outside of the virtual kernel cpu abstraction and may only make direct 76 * cothread and libc calls. 77 */ 78 cothread_t 79 cothread_create(void (*thr_func)(cothread_t cotd), 80 void (*thr_intr)(cothread_t cotd), 81 void *arg, const char *name) 82 { 83 cothread_t cotd; 84 void *stack; 85 pthread_attr_t attr; 86 87 cotd = kmalloc(sizeof(*cotd), M_DEVBUF, M_WAITOK|M_ZERO); 88 cotd->thr_intr = thr_intr; 89 cotd->thr_func = thr_func; 90 cotd->arg = arg; 91 crit_enter(); 92 pthread_mutex_init(&cotd->mutex, NULL); 93 pthread_cond_init(&cotd->cond, NULL); 94 crit_exit(); 95 96 cotd->pintr = pthread_self(); 97 98 cotd->intr_id = register_int_virtual(1, (void *)thr_intr, cotd, name, 99 NULL, INTR_MPSAFE); 100 101 /* 102 * The vkernel's cpu_disable_intr() masks signals. We don't want 103 * our coprocessor thread taking any unix signals :-) 104 */ 105 pthread_attr_init(&attr); 106 if (vmm_enabled) { 107 stack = mmap(NULL, KERNEL_STACK_SIZE, 108 PROT_READ|PROT_WRITE|PROT_EXEC, 109 MAP_ANON, -1, 0); 110 if (stack == MAP_FAILED) { 111 panic("Unable to allocate stack for cothread\n"); 112 } 113 pthread_attr_setstack(&attr, stack, KERNEL_STACK_SIZE); 114 } 115 crit_enter(); 116 cpu_mask_all_signals(); 117 pthread_create(&cotd->pthr, &attr, (void *)cothread_thread, cotd); 118 cpu_unmask_all_signals(); 119 crit_exit(); 120 pthread_attr_destroy(&attr); 121 122 return(cotd); 123 } 124 125 /* 126 * Wait for the target thread to terminate and then destroy the cothread 127 * structure. 128 */ 129 void 130 cothread_delete(cothread_t *cotdp) 131 { 132 cothread_t cotd; 133 134 if ((cotd = *cotdp) != NULL) { 135 unregister_int_virtual(cotd->intr_id); 136 crit_enter(); 137 pthread_join(cotd->pthr, NULL); 138 crit_exit(); 139 kfree(cotd, M_DEVBUF); 140 *cotdp = NULL; 141 } 142 } 143 144 static void 145 cothread_thread(void *arg) 146 { 147 cothread_t cotd = arg; 148 149 cpu_mask_all_signals(); /* XXX remove me? should already be masked */ 150 /* 151 * %gs (aka mycpu) is illegal in cothreads. Note that %fs is used 152 * by pthreads. 153 */ 154 /* JG try another approach? */ 155 tls_set_gs(0, sizeof(struct privatespace)); 156 cotd->thr_func(cotd); 157 } 158 159 /* 160 * Called by the cothread to generate an interrupt back to the vkernel. 161 */ 162 void 163 cothread_intr(cothread_t cotd) 164 { 165 pthread_kill(cotd->pintr, SIGIO); 166 } 167 168 /* 169 * Called by the vkernel to wakeup a cothread. 170 * The cothread must be locked. 171 */ 172 void 173 cothread_signal(cothread_t cotd) 174 { 175 pthread_cond_signal(&cotd->cond); 176 } 177 178 /* 179 * Called by the cothread to wait for the vkernel to call cothread_signal(). 180 * The cothread must be locked. 181 */ 182 void 183 cothread_wait(cothread_t cotd) 184 { 185 pthread_cond_wait(&cotd->cond, &cotd->mutex); 186 } 187 188 /* 189 * Typically called by kernel thread or cothread 190 * 191 * These must be a matched pair. We will acquire a critical 192 * section in cothread_lock() and release it in cothread_unlock(). 193 * 194 * We do this to simplify cothread operation to prevent an 195 * interrupt (e.g. vkd_io_intr()) from preempting a vkd_strategy() 196 * call and creating a recursion in the pthread. 197 */ 198 void 199 cothread_lock(cothread_t cotd, int is_cotd) 200 { 201 if (is_cotd) { 202 pthread_mutex_lock(&cotd->mutex); 203 } else { 204 crit_enter_id("cothread"); 205 pthread_mutex_lock(&cotd->mutex); 206 } 207 } 208 209 void 210 cothread_unlock(cothread_t cotd, int is_cotd) 211 { 212 if (is_cotd) { 213 pthread_mutex_unlock(&cotd->mutex); 214 } else { 215 pthread_mutex_unlock(&cotd->mutex); 216 crit_exit_id("cothread"); 217 } 218 } 219