xref: /OK3568_Linux_fs/kernel/arch/x86/um/syscalls_64.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (C) 2003 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3*4882a593Smuzhiyun  * Copyright 2003 PathScale, Inc.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Licensed under the GPL
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/sched.h>
9*4882a593Smuzhiyun #include <linux/sched/mm.h>
10*4882a593Smuzhiyun #include <linux/syscalls.h>
11*4882a593Smuzhiyun #include <linux/uaccess.h>
12*4882a593Smuzhiyun #include <asm/prctl.h> /* XXX This should get the constants from libc */
13*4882a593Smuzhiyun #include <os.h>
14*4882a593Smuzhiyun #include <registers.h>
15*4882a593Smuzhiyun 
arch_prctl(struct task_struct * task,int option,unsigned long __user * arg2)16*4882a593Smuzhiyun long arch_prctl(struct task_struct *task, int option,
17*4882a593Smuzhiyun 		unsigned long __user *arg2)
18*4882a593Smuzhiyun {
19*4882a593Smuzhiyun 	unsigned long *ptr = arg2, tmp;
20*4882a593Smuzhiyun 	long ret;
21*4882a593Smuzhiyun 	int pid = task->mm->context.id.u.pid;
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun 	/*
24*4882a593Smuzhiyun 	 * With ARCH_SET_FS (and ARCH_SET_GS is treated similarly to
25*4882a593Smuzhiyun 	 * be safe), we need to call arch_prctl on the host because
26*4882a593Smuzhiyun 	 * setting %fs may result in something else happening (like a
27*4882a593Smuzhiyun 	 * GDT or thread.fs being set instead).  So, we let the host
28*4882a593Smuzhiyun 	 * fiddle the registers and thread struct and restore the
29*4882a593Smuzhiyun 	 * registers afterwards.
30*4882a593Smuzhiyun 	 *
31*4882a593Smuzhiyun 	 * So, the saved registers are stored to the process (this
32*4882a593Smuzhiyun 	 * needed because a stub may have been the last thing to run),
33*4882a593Smuzhiyun 	 * arch_prctl is run on the host, then the registers are read
34*4882a593Smuzhiyun 	 * back.
35*4882a593Smuzhiyun 	 */
36*4882a593Smuzhiyun 	switch (option) {
37*4882a593Smuzhiyun 	case ARCH_SET_FS:
38*4882a593Smuzhiyun 	case ARCH_SET_GS:
39*4882a593Smuzhiyun 		ret = restore_pid_registers(pid, &current->thread.regs.regs);
40*4882a593Smuzhiyun 		if (ret)
41*4882a593Smuzhiyun 			return ret;
42*4882a593Smuzhiyun 		break;
43*4882a593Smuzhiyun 	case ARCH_GET_FS:
44*4882a593Smuzhiyun 	case ARCH_GET_GS:
45*4882a593Smuzhiyun 		/*
46*4882a593Smuzhiyun 		 * With these two, we read to a local pointer and
47*4882a593Smuzhiyun 		 * put_user it to the userspace pointer that we were
48*4882a593Smuzhiyun 		 * given.  If addr isn't valid (because it hasn't been
49*4882a593Smuzhiyun 		 * faulted in or is just bogus), we want put_user to
50*4882a593Smuzhiyun 		 * fault it in (or return -EFAULT) instead of having
51*4882a593Smuzhiyun 		 * the host return -EFAULT.
52*4882a593Smuzhiyun 		 */
53*4882a593Smuzhiyun 		ptr = &tmp;
54*4882a593Smuzhiyun 	}
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	ret = os_arch_prctl(pid, option, ptr);
57*4882a593Smuzhiyun 	if (ret)
58*4882a593Smuzhiyun 		return ret;
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	switch (option) {
61*4882a593Smuzhiyun 	case ARCH_SET_FS:
62*4882a593Smuzhiyun 		current->thread.arch.fs = (unsigned long) ptr;
63*4882a593Smuzhiyun 		ret = save_registers(pid, &current->thread.regs.regs);
64*4882a593Smuzhiyun 		break;
65*4882a593Smuzhiyun 	case ARCH_SET_GS:
66*4882a593Smuzhiyun 		ret = save_registers(pid, &current->thread.regs.regs);
67*4882a593Smuzhiyun 		break;
68*4882a593Smuzhiyun 	case ARCH_GET_FS:
69*4882a593Smuzhiyun 		ret = put_user(tmp, arg2);
70*4882a593Smuzhiyun 		break;
71*4882a593Smuzhiyun 	case ARCH_GET_GS:
72*4882a593Smuzhiyun 		ret = put_user(tmp, arg2);
73*4882a593Smuzhiyun 		break;
74*4882a593Smuzhiyun 	}
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	return ret;
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun 
SYSCALL_DEFINE2(arch_prctl,int,option,unsigned long,arg2)79*4882a593Smuzhiyun SYSCALL_DEFINE2(arch_prctl, int, option, unsigned long, arg2)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun 	return arch_prctl(current, option, (unsigned long __user *) arg2);
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun 
arch_switch_to(struct task_struct * to)84*4882a593Smuzhiyun void arch_switch_to(struct task_struct *to)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun 	if ((to->thread.arch.fs == 0) || (to->mm == NULL))
87*4882a593Smuzhiyun 		return;
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	arch_prctl(to, ARCH_SET_FS, (void __user *) to->thread.arch.fs);
90*4882a593Smuzhiyun }
91