15c48b108SAl Viro /*
25c48b108SAl Viro * Copyright (C) 2003 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
35c48b108SAl Viro * Copyright 2003 PathScale, Inc.
45c48b108SAl Viro *
55c48b108SAl Viro * Licensed under the GPL
65c48b108SAl Viro */
75c48b108SAl Viro
8d2ce4e92SAl Viro #include <linux/sched.h>
9589ee628SIngo Molnar #include <linux/sched/mm.h>
10ff3f097eSKyle Huey #include <linux/syscalls.h>
117017f42fSRichard Weinberger #include <linux/uaccess.h>
12d2ce4e92SAl Viro #include <asm/prctl.h> /* XXX This should get the constants from libc */
13*dbba7f70SAl Viro #include <registers.h>
14d2ce4e92SAl Viro #include <os.h>
155c48b108SAl Viro
arch_prctl(struct task_struct * task,int option,unsigned long __user * arg2)16d582799fSKyle Huey long arch_prctl(struct task_struct *task, int option,
1717a6e1b8SKyle Huey unsigned long __user *arg2)
185c48b108SAl Viro {
1917a6e1b8SKyle Huey unsigned long *ptr = arg2, tmp;
205c48b108SAl Viro long ret;
215c48b108SAl Viro int pid = task->mm->context.id.u.pid;
225c48b108SAl Viro
235c48b108SAl Viro /*
245c48b108SAl Viro * With ARCH_SET_FS (and ARCH_SET_GS is treated similarly to
255c48b108SAl Viro * be safe), we need to call arch_prctl on the host because
265c48b108SAl Viro * setting %fs may result in something else happening (like a
275c48b108SAl Viro * GDT or thread.fs being set instead). So, we let the host
285c48b108SAl Viro * fiddle the registers and thread struct and restore the
295c48b108SAl Viro * registers afterwards.
305c48b108SAl Viro *
315c48b108SAl Viro * So, the saved registers are stored to the process (this
325c48b108SAl Viro * needed because a stub may have been the last thing to run),
335c48b108SAl Viro * arch_prctl is run on the host, then the registers are read
345c48b108SAl Viro * back.
355c48b108SAl Viro */
36dd93938aSKyle Huey switch (option) {
375c48b108SAl Viro case ARCH_SET_FS:
385c48b108SAl Viro case ARCH_SET_GS:
39077b7320SRandy Dunlap ret = restore_pid_registers(pid, ¤t->thread.regs.regs);
405c48b108SAl Viro if (ret)
415c48b108SAl Viro return ret;
425c48b108SAl Viro break;
435c48b108SAl Viro case ARCH_GET_FS:
445c48b108SAl Viro case ARCH_GET_GS:
455c48b108SAl Viro /*
465c48b108SAl Viro * With these two, we read to a local pointer and
475c48b108SAl Viro * put_user it to the userspace pointer that we were
485c48b108SAl Viro * given. If addr isn't valid (because it hasn't been
495c48b108SAl Viro * faulted in or is just bogus), we want put_user to
505c48b108SAl Viro * fault it in (or return -EFAULT) instead of having
515c48b108SAl Viro * the host return -EFAULT.
525c48b108SAl Viro */
535c48b108SAl Viro ptr = &tmp;
545c48b108SAl Viro }
555c48b108SAl Viro
56dd93938aSKyle Huey ret = os_arch_prctl(pid, option, ptr);
575c48b108SAl Viro if (ret)
585c48b108SAl Viro return ret;
595c48b108SAl Viro
60dd93938aSKyle Huey switch (option) {
615c48b108SAl Viro case ARCH_SET_FS:
625c48b108SAl Viro current->thread.arch.fs = (unsigned long) ptr;
635c48b108SAl Viro ret = save_registers(pid, ¤t->thread.regs.regs);
645c48b108SAl Viro break;
655c48b108SAl Viro case ARCH_SET_GS:
665c48b108SAl Viro ret = save_registers(pid, ¤t->thread.regs.regs);
675c48b108SAl Viro break;
685c48b108SAl Viro case ARCH_GET_FS:
6917a6e1b8SKyle Huey ret = put_user(tmp, arg2);
705c48b108SAl Viro break;
715c48b108SAl Viro case ARCH_GET_GS:
7217a6e1b8SKyle Huey ret = put_user(tmp, arg2);
735c48b108SAl Viro break;
745c48b108SAl Viro }
755c48b108SAl Viro
765c48b108SAl Viro return ret;
775c48b108SAl Viro }
785c48b108SAl Viro
SYSCALL_DEFINE2(arch_prctl,int,option,unsigned long,arg2)7917a6e1b8SKyle Huey SYSCALL_DEFINE2(arch_prctl, int, option, unsigned long, arg2)
805c48b108SAl Viro {
8117a6e1b8SKyle Huey return arch_prctl(current, option, (unsigned long __user *) arg2);
825c48b108SAl Viro }
835c48b108SAl Viro
arch_switch_to(struct task_struct * to)845c48b108SAl Viro void arch_switch_to(struct task_struct *to)
855c48b108SAl Viro {
865c48b108SAl Viro if ((to->thread.arch.fs == 0) || (to->mm == NULL))
875c48b108SAl Viro return;
885c48b108SAl Viro
895c48b108SAl Viro arch_prctl(to, ARCH_SET_FS, (void __user *) to->thread.arch.fs);
905c48b108SAl Viro }
91577ade59SAl Viro
SYSCALL_DEFINE6(mmap,unsigned long,addr,unsigned long,len,unsigned long,prot,unsigned long,flags,unsigned long,fd,unsigned long,off)92577ade59SAl Viro SYSCALL_DEFINE6(mmap, unsigned long, addr, unsigned long, len,
93577ade59SAl Viro unsigned long, prot, unsigned long, flags,
94577ade59SAl Viro unsigned long, fd, unsigned long, off)
95577ade59SAl Viro {
96577ade59SAl Viro if (off & ~PAGE_MASK)
97577ade59SAl Viro return -EINVAL;
98577ade59SAl Viro
99577ade59SAl Viro return ksys_mmap_pgoff(addr, len, prot, flags, fd, off >> PAGE_SHIFT);
100577ade59SAl Viro }
101