syscall.c 12.7 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
2
3
4
5
6
7
8
9
/*
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
 * Copyright (C) 1995, 1996, 1997, 2000, 2001, 05 by Ralf Baechle
 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
 * Copyright (C) 2001 MIPS Technologies, Inc.
 */
10
#include <linux/capability.h>
Linus Torvalds's avatar
Linus Torvalds committed
11
12
13
#include <linux/errno.h>
#include <linux/linkage.h>
#include <linux/mm.h>
Alexey Dobriyan's avatar
Alexey Dobriyan committed
14
#include <linux/fs.h>
Linus Torvalds's avatar
Linus Torvalds committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <linux/smp.h>
#include <linux/mman.h>
#include <linux/ptrace.h>
#include <linux/sched.h>
#include <linux/string.h>
#include <linux/syscalls.h>
#include <linux/file.h>
#include <linux/slab.h>
#include <linux/utsname.h>
#include <linux/unistd.h>
#include <linux/sem.h>
#include <linux/msg.h>
#include <linux/shm.h>
#include <linux/compiler.h>
29
#include <linux/module.h>
Adrian Bunk's avatar
Adrian Bunk committed
30
#include <linux/ipc.h>
31
#include <linux/uaccess.h>
Linus Torvalds's avatar
Linus Torvalds committed
32

33
#include <asm/asm.h>
Linus Torvalds's avatar
Linus Torvalds committed
34
35
36
#include <asm/branch.h>
#include <asm/cachectl.h>
#include <asm/cacheflush.h>
37
#include <asm/asm-offsets.h>
Linus Torvalds's avatar
Linus Torvalds committed
38
39
40
41
42
43
#include <asm/signal.h>
#include <asm/sim.h>
#include <asm/shmparam.h>
#include <asm/sysmips.h>
#include <asm/uaccess.h>

44
45
46
47
48
49
50
51
/*
 * For historic reasons the pipe(2) syscall on MIPS has an unusual calling
 * convention.  It returns results in registers $v0 / $v1 which means there
 * is no need for it to do verify the validity of a userspace pointer
 * argument.  Historically that used to be expensive in Linux.  These days
 * the performance advantage is negligible.
 */
asmlinkage int sysm_pipe(nabi_no_regargs volatile struct pt_regs regs)
Linus Torvalds's avatar
Linus Torvalds committed
52
53
54
55
{
	int fd[2];
	int error, res;

Ulrich Drepper's avatar
Ulrich Drepper committed
56
	error = do_pipe_flags(fd, 0);
Linus Torvalds's avatar
Linus Torvalds committed
57
58
59
60
61
62
63
64
65
66
67
68
	if (error) {
		res = error;
		goto out;
	}
	regs.regs[3] = fd[1];
	res = fd[0];
out:
	return res;
}

unsigned long shm_align_mask = PAGE_SIZE - 1;	/* Sane caches */

69
70
EXPORT_SYMBOL(shm_align_mask);

Linus Torvalds's avatar
Linus Torvalds committed
71
72
73
74
75
76
77
78
79
80
81
82
83
#define COLOUR_ALIGN(addr,pgoff)				\
	((((addr) + shm_align_mask) & ~shm_align_mask) +	\
	 (((pgoff) << PAGE_SHIFT) & shm_align_mask))

unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
	unsigned long len, unsigned long pgoff, unsigned long flags)
{
	struct vm_area_struct * vmm;
	int do_color_align;
	unsigned long task_size;

	task_size = STACK_TOP;

84
85
86
	if (len > task_size)
		return -ENOMEM;

Linus Torvalds's avatar
Linus Torvalds committed
87
	if (flags & MAP_FIXED) {
88
89
90
91
		/* Even MAP_FIXED mappings must reside within task_size.  */
		if (task_size - len < addr)
			return -EINVAL;

Linus Torvalds's avatar
Linus Torvalds committed
92
93
94
95
		/*
		 * We do not accept a shared mapping if it would violate
		 * cache aliasing constraints.
		 */
96
97
		if ((flags & MAP_SHARED) &&
		    ((addr - (pgoff << PAGE_SHIFT)) & shm_align_mask))
Linus Torvalds's avatar
Linus Torvalds committed
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
			return -EINVAL;
		return addr;
	}

	do_color_align = 0;
	if (filp || (flags & MAP_SHARED))
		do_color_align = 1;
	if (addr) {
		if (do_color_align)
			addr = COLOUR_ALIGN(addr, pgoff);
		else
			addr = PAGE_ALIGN(addr);
		vmm = find_vma(current->mm, addr);
		if (task_size - len >= addr &&
		    (!vmm || addr + len <= vmm->vm_start))
			return addr;
	}
	addr = TASK_UNMAPPED_BASE;
	if (do_color_align)
		addr = COLOUR_ALIGN(addr, pgoff);
	else
		addr = PAGE_ALIGN(addr);

	for (vmm = find_vma(current->mm, addr); ; vmm = vmm->vm_next) {
		/* At this point:  (!vmm || addr < vmm->vm_end). */
		if (task_size - len < addr)
			return -ENOMEM;
		if (!vmm || addr + len <= vmm->vm_start)
			return addr;
		addr = vmm->vm_end;
		if (do_color_align)
			addr = COLOUR_ALIGN(addr, pgoff);
	}
}

133
134
135
SYSCALL_DEFINE6(mips_mmap, unsigned long, addr, unsigned long, len,
	unsigned long, prot, unsigned long, flags, unsigned long,
	fd, off_t, offset)
Linus Torvalds's avatar
Linus Torvalds committed
136
137
138
139
140
141
142
{
	unsigned long result;

	result = -EINVAL;
	if (offset & ~PAGE_MASK)
		goto out;

Al Viro's avatar
Al Viro committed
143
	result = sys_mmap_pgoff(addr, len, prot, flags, fd, offset >> PAGE_SHIFT);
Linus Torvalds's avatar
Linus Torvalds committed
144
145
146
147
148

out:
	return result;
}

149
150
151
SYSCALL_DEFINE6(mips_mmap2, unsigned long, addr, unsigned long, len,
	unsigned long, prot, unsigned long, flags, unsigned long, fd,
	unsigned long, pgoff)
Linus Torvalds's avatar
Linus Torvalds committed
152
{
153
154
155
	if (pgoff & (~PAGE_MASK >> 12))
		return -EINVAL;

Al Viro's avatar
Al Viro committed
156
	return sys_mmap_pgoff(addr, len, prot, flags, fd, pgoff >> (PAGE_SHIFT-12));
Linus Torvalds's avatar
Linus Torvalds committed
157
158
159
}

save_static_function(sys_fork);
160
static int __used noinline
Linus Torvalds's avatar
Linus Torvalds committed
161
162
163
164
165
166
_sys_fork(nabi_no_regargs struct pt_regs regs)
{
	return do_fork(SIGCHLD, regs.regs[29], &regs, 0, NULL, NULL);
}

save_static_function(sys_clone);
167
static int __used noinline
Linus Torvalds's avatar
Linus Torvalds committed
168
169
170
171
_sys_clone(nabi_no_regargs struct pt_regs regs)
{
	unsigned long clone_flags;
	unsigned long newsp;
Ralf Baechle's avatar
Ralf Baechle committed
172
	int __user *parent_tidptr, *child_tidptr;
Linus Torvalds's avatar
Linus Torvalds committed
173
174
175
176
177

	clone_flags = regs.regs[4];
	newsp = regs.regs[5];
	if (!newsp)
		newsp = regs.regs[29];
Ralf Baechle's avatar
Ralf Baechle committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
	parent_tidptr = (int __user *) regs.regs[6];
#ifdef CONFIG_32BIT
	/* We need to fetch the fifth argument off the stack.  */
	child_tidptr = NULL;
	if (clone_flags & (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)) {
		int __user *__user *usp = (int __user *__user *) regs.regs[29];
		if (regs.regs[2] == __NR_syscall) {
			if (get_user (child_tidptr, &usp[5]))
				return -EFAULT;
		}
		else if (get_user (child_tidptr, &usp[4]))
			return -EFAULT;
	}
#else
	child_tidptr = (int __user *) regs.regs[8];
#endif
Linus Torvalds's avatar
Linus Torvalds committed
194
195
196
197
198
199
200
201
202
203
204
205
	return do_fork(clone_flags, newsp, &regs, 0,
	               parent_tidptr, child_tidptr);
}

/*
 * sys_execve() executes a new program.
 */
asmlinkage int sys_execve(nabi_no_regargs struct pt_regs regs)
{
	int error;
	char * filename;

206
	filename = getname((char __user *) (long)regs.regs[4]);
Linus Torvalds's avatar
Linus Torvalds committed
207
208
209
	error = PTR_ERR(filename);
	if (IS_ERR(filename))
		goto out;
210
211
	error = do_execve(filename, (char __user *__user *) (long)regs.regs[5],
	                  (char __user *__user *) (long)regs.regs[6], &regs);
Linus Torvalds's avatar
Linus Torvalds committed
212
213
214
215
216
217
218
219
220
	putname(filename);

out:
	return error;
}

/*
 * Compacrapability ...
 */
221
SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
Linus Torvalds's avatar
Linus Torvalds committed
222
{
223
	if (name && !copy_to_user(name, utsname(), sizeof (*name)))
Linus Torvalds's avatar
Linus Torvalds committed
224
225
226
227
228
229
230
		return 0;
	return -EFAULT;
}

/*
 * Compacrapability ...
 */
231
SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
Linus Torvalds's avatar
Linus Torvalds committed
232
233
234
235
236
{
	int error;

	if (!name)
		return -EFAULT;
237
	if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
Linus Torvalds's avatar
Linus Torvalds committed
238
239
		return -EFAULT;

240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
	error = __copy_to_user(&name->sysname, &utsname()->sysname,
			       __OLD_UTS_LEN);
	error -= __put_user(0, name->sysname + __OLD_UTS_LEN);
	error -= __copy_to_user(&name->nodename, &utsname()->nodename,
				__OLD_UTS_LEN);
	error -= __put_user(0, name->nodename + __OLD_UTS_LEN);
	error -= __copy_to_user(&name->release, &utsname()->release,
				__OLD_UTS_LEN);
	error -= __put_user(0, name->release + __OLD_UTS_LEN);
	error -= __copy_to_user(&name->version, &utsname()->version,
				__OLD_UTS_LEN);
	error -= __put_user(0, name->version + __OLD_UTS_LEN);
	error -= __copy_to_user(&name->machine, &utsname()->machine,
				__OLD_UTS_LEN);
	error = __put_user(0, name->machine + __OLD_UTS_LEN);
Linus Torvalds's avatar
Linus Torvalds committed
255
256
257
258
259
	error = error ? -EFAULT : 0;

	return error;
}

260
SYSCALL_DEFINE1(set_thread_area, unsigned long, addr)
Ralf Baechle's avatar
Ralf Baechle committed
261
{
Al Viro's avatar
Al Viro committed
262
	struct thread_info *ti = task_thread_info(current);
Ralf Baechle's avatar
Ralf Baechle committed
263
264

	ti->tp_value = addr;
265
266
	if (cpu_has_userlocal)
		write_c0_userlocal(addr);
267
268

	return 0;
Ralf Baechle's avatar
Ralf Baechle committed
269
270
}

271
272
static inline int mips_atomic_set(struct pt_regs *regs,
	unsigned long addr, unsigned long new)
Linus Torvalds's avatar
Linus Torvalds committed
273
{
274
275
276
277
278
279
280
281
282
283
284
	unsigned long old, tmp;
	unsigned int err;

	if (unlikely(addr & 3))
		return -EINVAL;

	if (unlikely(!access_ok(VERIFY_WRITE, addr, 4)))
		return -EINVAL;

	if (cpu_has_llsc && R10000_LLSC_WAR) {
		__asm__ __volatile__ (
Ralf Baechle's avatar
Ralf Baechle committed
285
		"	.set	mips3					\n"
286
287
288
289
290
291
292
293
294
295
296
297
298
299
		"	li	%[err], 0				\n"
		"1:	ll	%[old], (%[addr])			\n"
		"	move	%[tmp], %[new]				\n"
		"2:	sc	%[tmp], (%[addr])			\n"
		"	beqzl	%[tmp], 1b				\n"
		"3:							\n"
		"	.section .fixup,\"ax\"				\n"
		"4:	li	%[err], %[efault]			\n"
		"	j	3b					\n"
		"	.previous					\n"
		"	.section __ex_table,\"a\"			\n"
		"	"STR(PTR)"	1b, 4b				\n"
		"	"STR(PTR)"	2b, 4b				\n"
		"	.previous					\n"
Ralf Baechle's avatar
Ralf Baechle committed
300
		"	.set	mips0					\n"
301
302
303
304
305
306
307
308
309
		: [old] "=&r" (old),
		  [err] "=&r" (err),
		  [tmp] "=&r" (tmp)
		: [addr] "r" (addr),
		  [new] "r" (new),
		  [efault] "i" (-EFAULT)
		: "memory");
	} else if (cpu_has_llsc) {
		__asm__ __volatile__ (
Ralf Baechle's avatar
Ralf Baechle committed
310
		"	.set	mips3					\n"
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
		"	li	%[err], 0				\n"
		"1:	ll	%[old], (%[addr])			\n"
		"	move	%[tmp], %[new]				\n"
		"2:	sc	%[tmp], (%[addr])			\n"
		"	bnez	%[tmp], 4f				\n"
		"3:							\n"
		"	.subsection 2					\n"
		"4:	b	1b					\n"
		"	.previous					\n"
		"							\n"
		"	.section .fixup,\"ax\"				\n"
		"5:	li	%[err], %[efault]			\n"
		"	j	3b					\n"
		"	.previous					\n"
		"	.section __ex_table,\"a\"			\n"
		"	"STR(PTR)"	1b, 5b				\n"
		"	"STR(PTR)"	2b, 5b				\n"
		"	.previous					\n"
Ralf Baechle's avatar
Ralf Baechle committed
329
		"	.set	mips0					\n"
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
		: [old] "=&r" (old),
		  [err] "=&r" (err),
		  [tmp] "=&r" (tmp)
		: [addr] "r" (addr),
		  [new] "r" (new),
		  [efault] "i" (-EFAULT)
		: "memory");
	} else {
		do {
			preempt_disable();
			ll_bit = 1;
			ll_task = current;
			preempt_enable();

			err = __get_user(old, (unsigned int *) addr);
			err |= __put_user(new, (unsigned int *) addr);
			if (err)
				break;
			rmb();
		} while (!ll_bit);
	}

	if (unlikely(err))
		return err;

	regs->regs[2] = old;
	regs->regs[7] = 0;	/* No error */

	/*
	 * Don't let your children do this ...
	 */
	__asm__ __volatile__(
	"	move	$29, %0						\n"
	"	j	syscall_exit					\n"
	: /* no outputs */
	: "r" (regs));

	/* unreached.  Honestly.  */
	while (1);
}

save_static_function(sys_sysmips);
static int __used noinline
_sys_sysmips(nabi_no_regargs struct pt_regs regs)
{
	long cmd, arg1, arg2, arg3;

	cmd = regs.regs[4];
	arg1 = regs.regs[5];
	arg2 = regs.regs[6];
	arg3 = regs.regs[7];

382
	switch (cmd) {
Linus Torvalds's avatar
Linus Torvalds committed
383
	case MIPS_ATOMIC_SET:
384
		return mips_atomic_set(&regs, arg1, arg2);
Linus Torvalds's avatar
Linus Torvalds committed
385
386

	case MIPS_FIXADE:
387
388
389
390
391
392
393
394
395
396
397
398
		if (arg1 & ~3)
			return -EINVAL;

		if (arg1 & 1)
			set_thread_flag(TIF_FIXADE);
		else
			clear_thread_flag(TIF_FIXADE);
		if (arg1 & 2)
			set_thread_flag(TIF_LOGADE);
		else
			clear_thread_flag(TIF_FIXADE);

Linus Torvalds's avatar
Linus Torvalds committed
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
		return 0;

	case FLUSH_CACHE:
		__flush_cache_all();
		return 0;
	}

	return -EINVAL;
}

/*
 * sys_ipc() is the de-multiplexer for the SysV IPC calls..
 *
 * This is really horribly ugly.
 */
414
415
SYSCALL_DEFINE6(ipc, unsigned int, call, int, first, int, second,
	unsigned long, third, void __user *, ptr, long, fifth)
Linus Torvalds's avatar
Linus Torvalds committed
416
417
418
419
420
421
422
423
{
	int version, ret;

	version = call >> 16; /* hack for backward compatibility */
	call &= 0xffff;

	switch (call) {
	case SEMOP:
424
425
		return sys_semtimedop(first, (struct sembuf __user *)ptr,
		                      second, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
426
	case SEMTIMEDOP:
427
428
429
		return sys_semtimedop(first, (struct sembuf __user *)ptr,
				      second,
				      (const struct timespec __user *)fifth);
Linus Torvalds's avatar
Linus Torvalds committed
430
	case SEMGET:
431
		return sys_semget(first, second, third);
Linus Torvalds's avatar
Linus Torvalds committed
432
433
434
435
	case SEMCTL: {
		union semun fourth;
		if (!ptr)
			return -EINVAL;
436
		if (get_user(fourth.__pad, (void __user *__user *) ptr))
Linus Torvalds's avatar
Linus Torvalds committed
437
			return -EFAULT;
438
		return sys_semctl(first, second, third, fourth);
Linus Torvalds's avatar
Linus Torvalds committed
439
440
441
	}

	case MSGSND:
442
443
		return sys_msgsnd(first, (struct msgbuf __user *) ptr,
				  second, third);
Linus Torvalds's avatar
Linus Torvalds committed
444
445
446
447
448
449
450
451
	case MSGRCV:
		switch (version) {
		case 0: {
			struct ipc_kludge tmp;
			if (!ptr)
				return -EINVAL;

			if (copy_from_user(&tmp,
452
					   (struct ipc_kludge __user *) ptr,
453
					   sizeof(tmp)))
Linus Torvalds's avatar
Linus Torvalds committed
454
				return -EFAULT;
455
456
			return sys_msgrcv(first, tmp.msgp, second,
					  tmp.msgtyp, third);
Linus Torvalds's avatar
Linus Torvalds committed
457
458
		}
		default:
459
460
461
			return sys_msgrcv(first,
					  (struct msgbuf __user *) ptr,
					  second, fifth, third);
Linus Torvalds's avatar
Linus Torvalds committed
462
463
		}
	case MSGGET:
464
		return sys_msgget((key_t) first, second);
Linus Torvalds's avatar
Linus Torvalds committed
465
	case MSGCTL:
466
467
		return sys_msgctl(first, second,
				  (struct msqid_ds __user *) ptr);
Linus Torvalds's avatar
Linus Torvalds committed
468
469
470
471

	case SHMAT:
		switch (version) {
		default: {
472
			unsigned long raddr;
473
474
			ret = do_shmat(first, (char __user *) ptr, second,
				       &raddr);
Linus Torvalds's avatar
Linus Torvalds committed
475
476
			if (ret)
				return ret;
477
			return put_user(raddr, (unsigned long __user *) third);
Linus Torvalds's avatar
Linus Torvalds committed
478
479
480
481
		}
		case 1:	/* iBCS2 emulator entry point */
			if (!segment_eq(get_fs(), get_ds()))
				return -EINVAL;
482
483
			return do_shmat(first, (char __user *) ptr, second,
				        (unsigned long *) third);
Linus Torvalds's avatar
Linus Torvalds committed
484
485
		}
	case SHMDT:
486
		return sys_shmdt((char __user *)ptr);
Linus Torvalds's avatar
Linus Torvalds committed
487
	case SHMGET:
488
		return sys_shmget(first, second, third);
Linus Torvalds's avatar
Linus Torvalds committed
489
	case SHMCTL:
490
491
		return sys_shmctl(first, second,
				  (struct shmid_ds __user *) ptr);
Linus Torvalds's avatar
Linus Torvalds committed
492
493
494
495
496
497
498
499
	default:
		return -ENOSYS;
	}
}

/*
 * No implemented yet ...
 */
500
SYSCALL_DEFINE3(cachectl, char *, addr, int, nbytes, int, op)
Linus Torvalds's avatar
Linus Torvalds committed
501
502
503
504
505
506
507
508
509
510
511
512
{
	return -ENOSYS;
}

/*
 * If we ever come here the user sp is bad.  Zap the process right away.
 * Due to the bad stack signaling wouldn't work.
 */
asmlinkage void bad_stack(void)
{
	do_exit(SIGSEGV);
}
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541

/*
 * Do a system call from kernel instead of calling sys_execve so we
 * end up with proper pt_regs.
 */
int kernel_execve(const char *filename, char *const argv[], char *const envp[])
{
	register unsigned long __a0 asm("$4") = (unsigned long) filename;
	register unsigned long __a1 asm("$5") = (unsigned long) argv;
	register unsigned long __a2 asm("$6") = (unsigned long) envp;
	register unsigned long __a3 asm("$7");
	unsigned long __v0;

	__asm__ volatile ("					\n"
	"	.set	noreorder				\n"
	"	li	$2, %5		# __NR_execve		\n"
	"	syscall						\n"
	"	move	%0, $2					\n"
	"	.set	reorder					\n"
	: "=&r" (__v0), "=r" (__a3)
	: "r" (__a0), "r" (__a1), "r" (__a2), "i" (__NR_execve)
	: "$2", "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15", "$24",
	  "memory");

	if (__a3 == 0)
		return __v0;

	return -__v0;
}