kernel - Introduce lightweight buffers
[dragonfly.git] / sys / kern / kern_umtx.c
CommitLineData
da5fb9ef
MD
1/*
2 * Copyright (c) 2003,2004 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> and David Xu <davidxu@freebsd.org>
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 *
17cde63e 34 * $DragonFly: src/sys/kern/kern_umtx.c,v 1.9 2008/05/09 07:24:45 dillon Exp $
da5fb9ef
MD
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
5c5185ae
SG
52#include <cpu/lwbuf.h>
53
da5fb9ef
MD
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_page.h>
65#include <vm/vm_kern.h>
66
10192bae 67#include <vm/vm_page2.h>
684a93c4 68#include <sys/mplock2.h>
10192bae
MD
69
70static void umtx_sleep_page_action_cow(vm_page_t m, vm_page_action_t action);
71
da5fb9ef
MD
72/*
73 * If the contents of the userland-supplied pointer matches the specified
74 * value enter an interruptable sleep for up to <timeout> microseconds.
75 * If the contents does not match then return immediately.
76 *
10192bae
MD
77 * Returns 0 if we slept and were woken up, -1 and EWOULDBLOCK if we slept
78 * and timed out, and EBUSY if the contents of the pointer already does
79 * not match the specified value. A timeout of 0 indicates an unlimited sleep.
02095b1c
MD
80 * EINTR is returned if the call was interrupted by a signal (even if
81 * the signal specifies that the system call should restart).
da5fb9ef
MD
82 *
83 * This function interlocks against call to umtx_wakeup. It does NOT interlock
84 * against changes in *ptr. However, it does not have to. The standard use
85 * of *ptr is to differentiate between an uncontested and a contested mutex
86 * and call umtx_wakeup when releasing a contested mutex. Therefore we can
87 * safely race against changes in *ptr as long as we are properly interlocked
88 * against the umtx_wakeup() call.
89 *
10192bae
MD
90 * The VM page associated with the mutex is held in an attempt to keep
91 * the mutex's physical address consistent, allowing umtx_sleep() and
92 * umtx_wakeup() to use the physical address as their rendezvous. BUT
93 * situations can arise where the physical address may change, particularly
94 * if a threaded program fork()'s and the mutex's memory becomes
95 * copy-on-write. We register an event on the VM page to catch COWs.
da5fb9ef
MD
96 *
97 * umtx_sleep { const int *ptr, int value, int timeout }
3919ced0
MD
98 *
99 * MPALMOSTSAFE
da5fb9ef
MD
100 */
101int
753fd850 102sys_umtx_sleep(struct umtx_sleep_args *uap)
da5fb9ef
MD
103{
104 int error = EBUSY;
5c5185ae 105 struct lwbuf *lwb;
10192bae 106 struct vm_page_action action;
da5fb9ef
MD
107 vm_page_t m;
108 void *waddr;
06c5a8d6 109 int offset;
da5fb9ef
MD
110 int timeout;
111
10192bae 112 if (uap->timeout < 0)
da5fb9ef 113 return (EINVAL);
06c5a8d6 114 if ((vm_offset_t)uap->ptr & (sizeof(int) - 1))
da5fb9ef 115 return (EFAULT);
10192bae
MD
116
117 /*
118 * When faulting in the page, force any COW pages to be resolved.
119 * Otherwise the physical page we sleep on my not match the page
120 * being woken up.
121 */
3919ced0 122 get_mplock();
10192bae 123 m = vm_fault_page_quick((vm_offset_t)uap->ptr, VM_PROT_READ|VM_PROT_WRITE, &error);
3919ced0
MD
124 if (m == NULL) {
125 error = EFAULT;
126 goto done;
127 }
5c5185ae 128 lwb = lwbuf_alloc(m);
06c5a8d6 129 offset = (vm_offset_t)uap->ptr & PAGE_MASK;
da5fb9ef 130
10192bae
MD
131 /*
132 * The critical section is required to interlock the tsleep against
133 * a wakeup from another cpu. The lfence forces synchronization.
134 */
5c5185ae 135 if (*(int *)(lwbuf_kva(lwb) + offset) == uap->value) {
10192bae
MD
136 if ((timeout = uap->timeout) != 0) {
137 timeout = (timeout / 1000000) * hz +
138 ((timeout % 1000000) * hz + 999999) / 1000000;
139 }
06c5a8d6 140 waddr = (void *)((intptr_t)VM_PAGE_TO_PHYS(m) + offset);
10192bae 141 crit_enter();
ae8e83e6 142 tsleep_interlock(waddr, PCATCH | PDOMAIN_UMTX);
5c5185ae 143 if (*(int *)(lwbuf_kva(lwb) + offset) == uap->value) {
10192bae
MD
144 vm_page_init_action(&action, umtx_sleep_page_action_cow, waddr);
145 vm_page_register_action(m, &action, VMEVENT_COW);
d9345d3a
MD
146 error = tsleep(waddr, PCATCH | PINTERLOCKED | PDOMAIN_UMTX,
147 "umtxsl", timeout);
10192bae
MD
148 vm_page_unregister_action(m, &action);
149 } else {
150 error = EBUSY;
151 }
152 crit_exit();
02095b1c
MD
153 /* Always break out in case of signal, even if restartable */
154 if (error == ERESTART)
e392b468 155 error = EINTR;
da5fb9ef
MD
156 } else {
157 error = EBUSY;
158 }
06c5a8d6 159
5c5185ae 160 lwbuf_free(lwb);
17cde63e 161 /*vm_page_dirty(m); we don't actually dirty the page */
06c5a8d6 162 vm_page_unhold(m);
3919ced0
MD
163done:
164 rel_mplock();
da5fb9ef
MD
165 return(error);
166}
167
168/*
10192bae
MD
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 */
173static void
174umtx_sleep_page_action_cow(vm_page_t m, vm_page_action_t action)
175{
176 wakeup_domain(action->data, PDOMAIN_UMTX);
177}
178
179/*
da5fb9ef
MD
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.
3919ced0
MD
187 *
188 * MPALMOSTSAFE
da5fb9ef
MD
189 */
190int
753fd850 191sys_umtx_wakeup(struct umtx_wakeup_args *uap)
da5fb9ef 192{
06c5a8d6
MD
193 vm_page_t m;
194 int offset;
195 int error;
da5fb9ef
MD
196 void *waddr;
197
35238fa5 198 cpu_mfence();
06c5a8d6 199 if ((vm_offset_t)uap->ptr & (sizeof(int) - 1))
da5fb9ef 200 return (EFAULT);
3919ced0 201 get_mplock();
06c5a8d6 202 m = vm_fault_page_quick((vm_offset_t)uap->ptr, VM_PROT_READ, &error);
3919ced0
MD
203 if (m == NULL) {
204 error = EFAULT;
205 goto done;
206 }
06c5a8d6
MD
207 offset = (vm_offset_t)uap->ptr & PAGE_MASK;
208 waddr = (void *)((intptr_t)VM_PAGE_TO_PHYS(m) + offset);
209
da5fb9ef
MD
210 if (uap->count == 1) {
211 wakeup_domain_one(waddr, PDOMAIN_UMTX);
212 } else {
213 /* XXX wakes them all up for now */
214 wakeup_domain(waddr, PDOMAIN_UMTX);
215 }
06c5a8d6 216 vm_page_unhold(m);
3919ced0
MD
217 error = 0;
218done:
219 rel_mplock();
220 return(error);
da5fb9ef
MD
221}
222