MPSAFE: MPSAFE kern/kern_umtx.c
[dragonfly.git] / sys / kern / kern_umtx.c
CommitLineData
da5fb9ef 1/*
6a25f455
VS
2 * (MPSAFE)
3 *
4 * Copyright (c) 2003,2004,2010 The DragonFly Project. All rights reserved.
da5fb9ef
MD
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 *
17cde63e 36 * $DragonFly: src/sys/kern/kern_umtx.c,v 1.9 2008/05/09 07:24:45 dillon Exp $
da5fb9ef
MD
37 */
38
39/*
40 * This module implements userland mutex helper functions. umtx_sleep()
41 * handling blocking and umtx_wakeup() handles wakeups. The sleep/wakeup
42 * functions operate on user addresses.
43 */
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/kernel.h>
48#include <sys/sysproto.h>
49#include <sys/sysunion.h>
50#include <sys/sysent.h>
51#include <sys/syscall.h>
52#include <sys/module.h>
53
5c5185ae
SG
54#include <cpu/lwbuf.h>
55
da5fb9ef
MD
56#include <vm/vm.h>
57#include <vm/vm_param.h>
58#include <sys/lock.h>
59#include <vm/pmap.h>
60#include <vm/vm_map.h>
61#include <vm/vm_object.h>
62#include <vm/vm_page.h>
63#include <vm/vm_pager.h>
64#include <vm/vm_pageout.h>
65#include <vm/vm_extern.h>
66#include <vm/vm_page.h>
67#include <vm/vm_kern.h>
68
10192bae
MD
69#include <vm/vm_page2.h>
70
71static void umtx_sleep_page_action_cow(vm_page_t m, vm_page_action_t action);
72
da5fb9ef
MD
73/*
74 * If the contents of the userland-supplied pointer matches the specified
75 * value enter an interruptable sleep for up to <timeout> microseconds.
76 * If the contents does not match then return immediately.
77 *
10192bae
MD
78 * Returns 0 if we slept and were woken up, -1 and EWOULDBLOCK if we slept
79 * and timed out, and EBUSY if the contents of the pointer already does
80 * not match the specified value. A timeout of 0 indicates an unlimited sleep.
02095b1c
MD
81 * EINTR is returned if the call was interrupted by a signal (even if
82 * the signal specifies that the system call should restart).
da5fb9ef
MD
83 *
84 * This function interlocks against call to umtx_wakeup. It does NOT interlock
85 * against changes in *ptr. However, it does not have to. The standard use
86 * of *ptr is to differentiate between an uncontested and a contested mutex
87 * and call umtx_wakeup when releasing a contested mutex. Therefore we can
88 * safely race against changes in *ptr as long as we are properly interlocked
89 * against the umtx_wakeup() call.
90 *
10192bae
MD
91 * The VM page associated with the mutex is held in an attempt to keep
92 * the mutex's physical address consistent, allowing umtx_sleep() and
93 * umtx_wakeup() to use the physical address as their rendezvous. BUT
94 * situations can arise where the physical address may change, particularly
95 * if a threaded program fork()'s and the mutex's memory becomes
96 * copy-on-write. We register an event on the VM page to catch COWs.
da5fb9ef
MD
97 *
98 * umtx_sleep { const int *ptr, int value, int timeout }
99 */
100int
753fd850 101sys_umtx_sleep(struct umtx_sleep_args *uap)
da5fb9ef
MD
102{
103 int error = EBUSY;
5c5185ae 104 struct lwbuf *lwb;
10192bae 105 struct vm_page_action action;
da5fb9ef
MD
106 vm_page_t m;
107 void *waddr;
06c5a8d6 108 int offset;
da5fb9ef
MD
109 int timeout;
110
10192bae 111 if (uap->timeout < 0)
da5fb9ef 112 return (EINVAL);
06c5a8d6 113 if ((vm_offset_t)uap->ptr & (sizeof(int) - 1))
da5fb9ef 114 return (EFAULT);
10192bae
MD
115
116 /*
117 * When faulting in the page, force any COW pages to be resolved.
118 * Otherwise the physical page we sleep on my not match the page
119 * being woken up.
120 */
6a25f455 121 lwkt_gettoken(&vm_token);
10192bae 122 m = vm_fault_page_quick((vm_offset_t)uap->ptr, VM_PROT_READ|VM_PROT_WRITE, &error);
3919ced0
MD
123 if (m == NULL) {
124 error = EFAULT;
125 goto done;
126 }
5c5185ae 127 lwb = lwbuf_alloc(m);
06c5a8d6 128 offset = (vm_offset_t)uap->ptr & PAGE_MASK;
da5fb9ef 129
10192bae
MD
130 /*
131 * The critical section is required to interlock the tsleep against
132 * a wakeup from another cpu. The lfence forces synchronization.
133 */
5c5185ae 134 if (*(int *)(lwbuf_kva(lwb) + offset) == uap->value) {
10192bae
MD
135 if ((timeout = uap->timeout) != 0) {
136 timeout = (timeout / 1000000) * hz +
137 ((timeout % 1000000) * hz + 999999) / 1000000;
138 }
06c5a8d6 139 waddr = (void *)((intptr_t)VM_PAGE_TO_PHYS(m) + offset);
10192bae 140 crit_enter();
ae8e83e6 141 tsleep_interlock(waddr, PCATCH | PDOMAIN_UMTX);
5c5185ae 142 if (*(int *)(lwbuf_kva(lwb) + offset) == uap->value) {
10192bae
MD
143 vm_page_init_action(&action, umtx_sleep_page_action_cow, waddr);
144 vm_page_register_action(m, &action, VMEVENT_COW);
d9345d3a
MD
145 error = tsleep(waddr, PCATCH | PINTERLOCKED | PDOMAIN_UMTX,
146 "umtxsl", timeout);
10192bae
MD
147 vm_page_unregister_action(m, &action);
148 } else {
149 error = EBUSY;
150 }
151 crit_exit();
02095b1c
MD
152 /* Always break out in case of signal, even if restartable */
153 if (error == ERESTART)
e392b468 154 error = EINTR;
da5fb9ef
MD
155 } else {
156 error = EBUSY;
157 }
06c5a8d6 158
5c5185ae 159 lwbuf_free(lwb);
17cde63e 160 /*vm_page_dirty(m); we don't actually dirty the page */
06c5a8d6 161 vm_page_unhold(m);
3919ced0 162done:
6a25f455 163 lwkt_reltoken(&vm_token);
da5fb9ef
MD
164 return(error);
165}
166
167/*
10192bae
MD
168 * If this page is being copied it may no longer represent the page
169 * underlying our virtual address. Wake up any umtx_sleep()'s
170 * that were waiting on its physical address to force them to retry.
171 */
172static void
173umtx_sleep_page_action_cow(vm_page_t m, vm_page_action_t action)
174{
175 wakeup_domain(action->data, PDOMAIN_UMTX);
176}
177
178/*
da5fb9ef
MD
179 * umtx_wakeup { const int *ptr, int count }
180 *
181 * Wakeup the specified number of processes held in umtx_sleep() on the
182 * specified user address. A count of 0 wakes up all waiting processes.
183 *
184 * XXX assumes that the physical address space does not exceed the virtual
185 * address space.
186 */
187int
753fd850 188sys_umtx_wakeup(struct umtx_wakeup_args *uap)
da5fb9ef 189{
06c5a8d6
MD
190 vm_page_t m;
191 int offset;
192 int error;
da5fb9ef
MD
193 void *waddr;
194
35238fa5 195 cpu_mfence();
06c5a8d6 196 if ((vm_offset_t)uap->ptr & (sizeof(int) - 1))
da5fb9ef 197 return (EFAULT);
6a25f455 198 lwkt_gettoken(&vm_token);
06c5a8d6 199 m = vm_fault_page_quick((vm_offset_t)uap->ptr, VM_PROT_READ, &error);
3919ced0
MD
200 if (m == NULL) {
201 error = EFAULT;
202 goto done;
203 }
06c5a8d6
MD
204 offset = (vm_offset_t)uap->ptr & PAGE_MASK;
205 waddr = (void *)((intptr_t)VM_PAGE_TO_PHYS(m) + offset);
206
da5fb9ef
MD
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 }
06c5a8d6 213 vm_page_unhold(m);
3919ced0
MD
214 error = 0;
215done:
6a25f455 216 lwkt_reltoken(&vm_token);
3919ced0 217 return(error);
da5fb9ef
MD
218}
219