1 /* 2 * (MPSAFE) 3 * 4 * Copyright (c) 2003,2004,2010 The DragonFly Project. All rights reserved. 5 * 6 * This code is derived from software contributed to The DragonFly Project 7 * by Matthew Dillon <dillon@backplane.com> and David Xu <davidxu@freebsd.org> 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 3. Neither the name of The DragonFly Project nor the names of its 20 * contributors may be used to endorse or promote products derived 21 * from this software without specific, prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 /* 38 * This module implements userland mutex helper functions. umtx_sleep() 39 * handling blocking and umtx_wakeup() handles wakeups. The sleep/wakeup 40 * functions operate on user addresses. 41 */ 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/kernel.h> 46 #include <sys/sysproto.h> 47 #include <sys/sysunion.h> 48 #include <sys/sysent.h> 49 #include <sys/syscall.h> 50 #include <sys/module.h> 51 52 #include <cpu/lwbuf.h> 53 54 #include <vm/vm.h> 55 #include <vm/vm_param.h> 56 #include <sys/lock.h> 57 #include <vm/pmap.h> 58 #include <vm/vm_map.h> 59 #include <vm/vm_object.h> 60 #include <vm/vm_page.h> 61 #include <vm/vm_pager.h> 62 #include <vm/vm_pageout.h> 63 #include <vm/vm_extern.h> 64 #include <vm/vm_kern.h> 65 66 #include <vm/vm_page2.h> 67 68 static void umtx_sleep_page_action_cow(vm_page_t m, vm_page_action_t action); 69 70 /* 71 * If the contents of the userland-supplied pointer matches the specified 72 * value enter an interruptable sleep for up to <timeout> microseconds. 73 * If the contents does not match then return immediately. 74 * 75 * Returns 0 if we slept and were woken up, -1 and EWOULDBLOCK if we slept 76 * and timed out, and EBUSY if the contents of the pointer already does 77 * not match the specified value. A timeout of 0 indicates an unlimited sleep. 78 * EINTR is returned if the call was interrupted by a signal (even if 79 * the signal specifies that the system call should restart). 80 * 81 * This function interlocks against call to umtx_wakeup. It does NOT interlock 82 * against changes in *ptr. However, it does not have to. The standard use 83 * of *ptr is to differentiate between an uncontested and a contested mutex 84 * and call umtx_wakeup when releasing a contested mutex. Therefore we can 85 * safely race against changes in *ptr as long as we are properly interlocked 86 * against the umtx_wakeup() call. 87 * 88 * The VM page associated with the mutex is held in an attempt to keep 89 * the mutex's physical address consistent, allowing umtx_sleep() and 90 * umtx_wakeup() to use the physical address as their rendezvous. BUT 91 * situations can arise where the physical address may change, particularly 92 * if a threaded program fork()'s and the mutex's memory becomes 93 * copy-on-write. We register an event on the VM page to catch COWs. 94 * 95 * umtx_sleep { const int *ptr, int value, int timeout } 96 */ 97 int 98 sys_umtx_sleep(struct umtx_sleep_args *uap) 99 { 100 struct lwbuf lwb_cache; 101 struct lwbuf *lwb; 102 struct vm_page_action action; 103 vm_page_t m; 104 void *waddr; 105 int offset; 106 int timeout; 107 int error = EBUSY; 108 109 if (uap->timeout < 0) 110 return (EINVAL); 111 if ((vm_offset_t)uap->ptr & (sizeof(int) - 1)) 112 return (EFAULT); 113 114 /* 115 * When faulting in the page, force any COW pages to be resolved. 116 * Otherwise the physical page we sleep on my not match the page 117 * being woken up. 118 */ 119 m = vm_fault_page_quick((vm_offset_t)uap->ptr, 120 VM_PROT_READ|VM_PROT_WRITE, &error); 121 if (m == NULL) { 122 error = EFAULT; 123 goto done; 124 } 125 lwb = lwbuf_alloc(m, &lwb_cache); 126 offset = (vm_offset_t)uap->ptr & PAGE_MASK; 127 128 /* 129 * The critical section is required to interlock the tsleep against 130 * a wakeup from another cpu. The lfence forces synchronization. 131 */ 132 if (*(int *)(lwbuf_kva(lwb) + offset) == uap->value) { 133 if ((timeout = uap->timeout) != 0) { 134 timeout = (timeout / 1000000) * hz + 135 ((timeout % 1000000) * hz + 999999) / 1000000; 136 } 137 waddr = (void *)((intptr_t)VM_PAGE_TO_PHYS(m) + offset); 138 crit_enter(); 139 tsleep_interlock(waddr, PCATCH | PDOMAIN_UMTX); 140 if (*(int *)(lwbuf_kva(lwb) + offset) == uap->value) { 141 vm_page_init_action(m, &action, umtx_sleep_page_action_cow, waddr); 142 vm_page_register_action(&action, VMEVENT_COW); 143 if (*(int *)(lwbuf_kva(lwb) + offset) == uap->value) { 144 error = tsleep(waddr, PCATCH | PINTERLOCKED | PDOMAIN_UMTX, 145 "umtxsl", timeout); 146 } else { 147 error = EBUSY; 148 } 149 vm_page_unregister_action(&action); 150 } else { 151 error = EBUSY; 152 } 153 crit_exit(); 154 /* Always break out in case of signal, even if restartable */ 155 if (error == ERESTART) 156 error = EINTR; 157 } else { 158 error = EBUSY; 159 } 160 161 lwbuf_free(lwb); 162 /*vm_page_dirty(m); we don't actually dirty the page */ 163 vm_page_unhold(m); 164 done: 165 return(error); 166 } 167 168 /* 169 * If this page is being copied it may no longer represent the page 170 * underlying our virtual address. Wake up any umtx_sleep()'s 171 * that were waiting on its physical address to force them to retry. 172 */ 173 static void 174 umtx_sleep_page_action_cow(vm_page_t m, vm_page_action_t action) 175 { 176 wakeup_domain(action->data, PDOMAIN_UMTX); 177 } 178 179 /* 180 * umtx_wakeup { const int *ptr, int count } 181 * 182 * Wakeup the specified number of processes held in umtx_sleep() on the 183 * specified user address. A count of 0 wakes up all waiting processes. 184 * 185 * XXX assumes that the physical address space does not exceed the virtual 186 * address space. 187 */ 188 int 189 sys_umtx_wakeup(struct umtx_wakeup_args *uap) 190 { 191 vm_page_t m; 192 int offset; 193 int error; 194 void *waddr; 195 196 cpu_mfence(); 197 if ((vm_offset_t)uap->ptr & (sizeof(int) - 1)) 198 return (EFAULT); 199 m = vm_fault_page_quick((vm_offset_t)uap->ptr, VM_PROT_READ, &error); 200 if (m == NULL) { 201 error = EFAULT; 202 goto done; 203 } 204 offset = (vm_offset_t)uap->ptr & PAGE_MASK; 205 waddr = (void *)((intptr_t)VM_PAGE_TO_PHYS(m) + offset); 206 207 if (uap->count == 1) { 208 wakeup_domain_one(waddr, PDOMAIN_UMTX); 209 } else { 210 /* XXX wakes them all up for now */ 211 wakeup_domain(waddr, PDOMAIN_UMTX); 212 } 213 vm_page_unhold(m); 214 error = 0; 215 done: 216 return(error); 217 } 218 219