aboutsummaryrefslogtreecommitdiff
path: root/sys/compat/linux/linux_futex.c
blob: 889d29d6d48556009938d1416cf574cd745c151a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*	$NetBSD: linux_futex.c,v 1.5 2005/11/23 16:14:57 manu Exp $ */

/*-
 * Copyright (c) 2005 Emmanuel Dreyfus, all rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Emmanuel Dreyfus
 * 4. The name of the author may not be used to endorse or promote 
 *    products derived from this software without specific prior written 
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS'' 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS 
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#if 0
 __KERNEL_RCSID(1, "$NetBSD: linux_futex.c,v 1.5 2005/11/23 16:14:57 manu Exp $");
#endif

#include "opt_compat.h"

#include <sys/param.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/queue.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/malloc.h>

#ifdef COMPAT_LINUX32
#include <machine/../linux32/linux.h>
#include <machine/../linux32/linux32_proto.h>
#else
#include <machine/../linux/linux.h>
#include <machine/../linux/linux_proto.h>
#endif
#include <compat/linux/linux_futex.h>

struct futex;

struct waiting_proc {
	struct thread *wp_t;
	struct futex *wp_new_futex;
	TAILQ_ENTRY(waiting_proc) wp_list;
};
struct futex {
	void *f_uaddr;
	int f_refcount;
	LIST_ENTRY(futex) f_list;
	TAILQ_HEAD(lf_waiting_proc, waiting_proc) f_waiting_proc;
};

LIST_HEAD(futex_list, futex) futex_list;
struct mtx futex_mtx;		/* this protects the LIST of futexes */

#define FUTEX_LOCK mtx_lock(&futex_mtx)
#define FUTEX_UNLOCK mtx_unlock(&futex_mtx)

#define FUTEX_LOCKED	1
#define FUTEX_UNLOCKED	0

#define FUTEX_SYSTEM_LOCK mtx_lock(&Giant)
#define FUTEX_SYSTEM_UNLOCK mtx_unlock(&Giant)

static struct futex *futex_get(void *, int);
static void futex_put(struct futex *);
static int futex_sleep(struct futex *, struct thread *, unsigned long);
static int futex_wake(struct futex *, int, struct futex *);
#ifdef __i386__
static int futex_atomic_op(struct thread *td, int encoded_op, caddr_t uaddr);
#endif 

/* support.s */
int futex_xchgl(int oparg, caddr_t uaddr, int *oldval);
int futex_addl(int oparg, caddr_t uaddr, int *oldval);
int futex_orl(int oparg, caddr_t uaddr, int *oldval);
int futex_andnl(int oparg, caddr_t uaddr, int *oldval);
int futex_xorl(int oparg, caddr_t uaddr, int *oldval);

int
linux_sys_futex(struct thread *td, struct linux_sys_futex_args *args)
{
	int val;
	int ret;
	struct l_timespec timeout = { 0, 0 };
	int error = 0;
	struct futex *f;
	struct futex *newf;
	int timeout_hz;
	struct timeval tv = {0, 0};
#ifdef __i386__
	struct futex *f2;
	int op_ret;
#endif

#ifdef	DEBUG
	if (ldebug(sys_futex))
	   	printf(ARGS(futex,"%p, %i, %i"), args->uaddr, args->op, args->val);
#endif

	switch (args->op) {
	case LINUX_FUTEX_WAIT:
	   	FUTEX_SYSTEM_LOCK;

		if ((error = copyin(args->uaddr, 
		    &val, sizeof(val))) != 0) {
		   	FUTEX_SYSTEM_UNLOCK;
			return error;
		}

		if (val != args->val) {
		   	FUTEX_SYSTEM_UNLOCK;
			return EWOULDBLOCK;
		}

		if (args->timeout != NULL) {
			if ((error = copyin(args->timeout, 
			    &timeout, sizeof(timeout))) != 0) {
			  	FUTEX_SYSTEM_UNLOCK;
				return error;
			}
		}

#ifdef DEBUG
		if (ldebug(sys_futex))
   			printf("FUTEX_WAIT %d: val = %d, uaddr = %p, "
		    		"*uaddr = %d, timeout = %d.%09ld\n", 
		    		td->td_proc->p_pid, args->val, 
		    		args->uaddr, val, timeout.tv_sec, timeout.tv_nsec); 
#endif
		tv.tv_usec = timeout.tv_sec * 1000000 + timeout.tv_nsec / 1000;
		timeout_hz = tvtohz(&tv);

		if (timeout.tv_sec == 0 && timeout.tv_nsec == 0)
		   	timeout_hz = 0;
      		/*
                 * If the user process requests a non null timeout,
                 * make sure we do not turn it into an infinite
                 * timeout because timeout_hz gets null.
                 *
                 * We use a minimal timeout of 1/hz. Maybe it would
                 * make sense to just return ETIMEDOUT without sleeping.
                 */
                if (((timeout.tv_sec != 0) || (timeout.tv_nsec != 0)) &&
                    (timeout_hz == 0))
                        timeout_hz = 1;


		f = futex_get(args->uaddr, FUTEX_UNLOCKED);
		ret = futex_sleep(f, td, timeout_hz);
		futex_put(f);

#ifdef DEBUG
		if (ldebug(sys_futex))
   			printf("FUTEX_WAIT %d: uaddr = %p, "
		    		"ret = %d\n", td->td_proc->p_pid, args->uaddr, ret); 
#endif

		FUTEX_SYSTEM_UNLOCK;
		switch (ret) {
		case EWOULDBLOCK:	/* timeout */
			return ETIMEDOUT;
			break;
		case EINTR:		/* signal */
			return EINTR;
			break;
		case 0:			/* FUTEX_WAKE received */
#ifdef DEBUG
			if (ldebug(sys_futex))
				printf("FUTEX_WAIT %d: uaddr = %p, got FUTEX_WAKE\n",
			    		td->td_proc->p_pid, args->uaddr); 
#endif
			return 0;
			break;
		default:
#ifdef DEBUG
			if (ldebug(sys_futex))
   				printf("FUTEX_WAIT: unexpected ret = %d\n", ret);
#endif
			break;
		}

		/* NOTREACHED */
		break;
		
	case LINUX_FUTEX_WAKE:
		FUTEX_SYSTEM_LOCK;

		/* 
		 * XXX: Linux is able cope with different addresses 
		 * corresponding to the same mapped memory in the sleeping 
		 * and the waker process.
		 */
#ifdef DEBUG
		if (ldebug(sys_futex))
   			printf("FUTEX_WAKE %d: uaddr = %p, val = %d\n", 
		   		 td->td_proc->p_pid, args->uaddr, args->val); 
#endif
		f = futex_get(args->uaddr, FUTEX_UNLOCKED);
		td->td_retval[0] = futex_wake(f, args->val, NULL);
		futex_put(f);

		FUTEX_SYSTEM_UNLOCK;
		break;

	case LINUX_FUTEX_CMP_REQUEUE:
		FUTEX_SYSTEM_LOCK;

		if ((error = copyin(args->uaddr, 
		    &val, sizeof(val))) != 0) {
		   	FUTEX_SYSTEM_UNLOCK;
			return error;
		}

		if (val != args->val3) {
		   	FUTEX_SYSTEM_UNLOCK;
			return EAGAIN;
		}

		f = futex_get(args->uaddr, FUTEX_UNLOCKED);
		newf = futex_get(args->uaddr2, FUTEX_UNLOCKED);
		td->td_retval[0] = futex_wake(f, args->val, newf);
		futex_put(f);
		futex_put(newf);

		FUTEX_SYSTEM_UNLOCK;
		break;

	case LINUX_FUTEX_REQUEUE:
		FUTEX_SYSTEM_LOCK;
		
		f = futex_get(args->uaddr, FUTEX_UNLOCKED);
		newf = futex_get(args->uaddr2, FUTEX_UNLOCKED);
		td->td_retval[0] = futex_wake(f, args->val, newf);
		futex_put(f);
		futex_put(newf);
		
		FUTEX_SYSTEM_UNLOCK;
		break;

	case LINUX_FUTEX_FD:
		printf("linux_sys_futex: unimplemented op %d\n", 
		    args->op);
		break;

	case LINUX_FUTEX_WAKE_OP:
#ifdef	__i386__
		FUTEX_SYSTEM_LOCK;
#ifdef DEBUG
		if (ldebug(sys_futex))
   		   	printf("FUTEX_WAKE_OP: %d: uaddr = %p, op = %d, val = %d, uaddr2 = %p, val3 = %d\n",
		      		td->td_proc->p_pid, args->uaddr, args->op, args->val, args->uaddr2, args->val3);
#endif
		f = futex_get(args->uaddr, FUTEX_UNLOCKED);
		f2 = futex_get(args->uaddr2, FUTEX_UNLOCKED);

		/* This function returns positive number as results
		 * and negative as errors
		 */
		op_ret = futex_atomic_op(td, args->val3, args->uaddr2);
		if (op_ret < 0) {

		   	/* XXX: we dont handle the EFAULT yet */
		   	if (op_ret != -EFAULT) {
			   	futex_put(f);
			   	futex_put(f2);
				FUTEX_SYSTEM_UNLOCK;
			   	return (-op_ret);
			}

			futex_put(f);
			futex_put(f2);

		   	FUTEX_SYSTEM_UNLOCK;
			return (EFAULT);

		}

		ret = futex_wake(f, args->val, NULL);
		futex_put(f);
		if (op_ret > 0) {
		   	printf("second wakeup\n");
		   	op_ret = 0;
		   	/* Linux always puts there 0 retries */
   		   	op_ret += futex_wake(f2, 0, NULL);
			ret += op_ret;
		}
		futex_put(f2);
		td->td_retval[0] = ret;

		FUTEX_SYSTEM_UNLOCK;
#else
		printf("linux_sys_futex: wake_op not implemented");
#endif
	   	break;

	default:
		printf("linux_sys_futex: unknown op %d\n", 
		    args->op);
		break;
	}
	return 0;
}

static struct futex *
futex_get(void *uaddr, int locked)
{
	struct futex *f;

	if (locked == FUTEX_UNLOCKED)
   	   	FUTEX_LOCK;
	LIST_FOREACH(f, &futex_list, f_list) {
		if (f->f_uaddr == uaddr) {
			f->f_refcount++;
			if (locked == FUTEX_UNLOCKED)
   			   	FUTEX_UNLOCK;
			return f;
		}
	}
	if (locked == FUTEX_UNLOCKED)
   	   	FUTEX_UNLOCK;

	/* Not found, create it */
	f = malloc(sizeof(*f), M_LINUX, M_WAITOK);
	f->f_uaddr = uaddr;
	f->f_refcount = 1;
	TAILQ_INIT(&f->f_waiting_proc);
	if (locked == FUTEX_UNLOCKED)
   	   	FUTEX_LOCK;
	LIST_INSERT_HEAD(&futex_list, f, f_list);
	if (locked == FUTEX_UNLOCKED)
   	   	FUTEX_UNLOCK;

	return f;
}

static void 
futex_put(f)
	struct futex *f;
{
   	FUTEX_LOCK;
   	f->f_refcount--;
	if (f->f_refcount == 0) {
		LIST_REMOVE(f, f_list);
		free(f, M_LINUX);
	}
	FUTEX_UNLOCK;

	return;
}

static int 
futex_sleep(struct futex *f, struct thread *td, unsigned long timeout)
{
	struct waiting_proc *wp;
	int ret;

	wp = malloc(sizeof(*wp), M_LINUX, M_WAITOK);
	wp->wp_t = td;
	wp->wp_new_futex = NULL;
	FUTEX_LOCK;
	TAILQ_INSERT_TAIL(&f->f_waiting_proc, wp, wp_list);
	FUTEX_UNLOCK;

#ifdef DEBUG
	if (ldebug(sys_futex))
   		printf("FUTEX --> %d tlseep timeout = %ld\n", td->td_proc->p_pid,
	  	      timeout);
#endif
	ret = tsleep(wp, PCATCH|PZERO, "linuxfutex", timeout);

	FUTEX_LOCK;
	TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
	FUTEX_UNLOCK;

	if ((ret == 0) && (wp->wp_new_futex != NULL)) {
		ret = futex_sleep(wp->wp_new_futex, td, timeout);
		futex_put(wp->wp_new_futex); /* futex_get called in wakeup */
	}

	free(wp, M_LINUX);

	return ret;
}

static int
futex_wake(struct futex *f, int n, struct futex *newf)
{
	struct waiting_proc *wp;
	int count = 0; 

	FUTEX_LOCK;
	TAILQ_FOREACH(wp, &f->f_waiting_proc, wp_list) {
		if (count <= n) {
			wakeup(wp);
			count++;
		} else {
			if (newf != NULL) {
				/* futex_put called after tsleep */
				wp->wp_new_futex = futex_get(newf->f_uaddr, FUTEX_LOCKED);
				wakeup(wp);
			}
		}
	}
	FUTEX_UNLOCK;

	return count;
}

#ifdef __i386__
static int
futex_atomic_op(struct thread *td, int encoded_op, caddr_t uaddr)
{
   	int op = (encoded_op >> 28) & 7;
        int cmp = (encoded_op >> 24) & 15;
        int oparg = (encoded_op << 8) >> 20;
        int cmparg = (encoded_op << 20) >> 20;
        int oldval = 0, ret;

	if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28))
	   	oparg = 1 << oparg;

#ifdef DEBUG	
	printf("futex_atomic_op: op = %d, cmp = %d, oparg = %d, cmparg = %d, uaddr = %p\n",
	      op, cmp, oparg, cmparg, uaddr);
#endif
	/* XXX: linux verifies access here and returns EFAULT */

	critical_enter();

	switch (op) {
	   	case FUTEX_OP_SET:
		   	ret = futex_xchgl(oparg, uaddr, &oldval);
			break;
		case FUTEX_OP_ADD:
			ret = futex_addl(oparg, uaddr, &oldval);
			break;
		case FUTEX_OP_OR:
			ret = futex_orl(oparg, uaddr, &oldval);
			break;
		case FUTEX_OP_ANDN:
			ret = futex_andnl(oparg, uaddr, &oldval);
			break;
		case FUTEX_OP_XOR:
			ret = futex_xorl(oparg, uaddr, &oldval);
			break;
		default:
			ret = -ENOSYS;
	}

	critical_exit();

	if (!ret)
	   	switch (cmp) {
		      case FUTEX_OP_CMP_EQ: 
			 	ret = (oldval == cmparg); 
				break;
		      case FUTEX_OP_CMP_NE: 
				ret = (oldval != cmparg); 
				break;
		      case FUTEX_OP_CMP_LT: 
				ret = (oldval < cmparg); 
				break;
		      case FUTEX_OP_CMP_GE: 
				ret = (oldval >= cmparg); 
				break;
		      case FUTEX_OP_CMP_LE: 
				ret = (oldval <= cmparg); 
				break;
		      case FUTEX_OP_CMP_GT: 
				ret = (oldval > cmparg); 
				break;
		      default: ret = -ENOSYS;
		}

	return (ret);
}
#endif