blob: f3d82bb6e15a410b5e706617526efe9d56a42243 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jeff Dikeba180fd2007-10-16 01:27:00 -07002 * Copyright (C) 2003 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright 2003 PathScale, Inc.
4 *
5 * Licensed under the GPL
6 */
7
8#include "linux/linkage.h"
Paolo 'Blaisorblade' Giarrussof7fe8782005-05-05 16:15:15 -07009#include "linux/personality.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -070010#include "linux/utsname.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "asm/prctl.h" /* XXX This should get the constants from libc */
Jeff Dikeba180fd2007-10-16 01:27:00 -070012#include "asm/uaccess.h"
Jeff Dikef3555592007-02-10 01:44:29 -080013#include "os.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Jeff Dike77bf4402007-10-16 01:26:58 -070015long arch_prctl(struct task_struct *task, int code, unsigned long __user *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016{
Jeff Dikeba180fd2007-10-16 01:27:00 -070017 unsigned long *ptr = addr, tmp;
Jeff Dikef3555592007-02-10 01:44:29 -080018 long ret;
Jeff Dike6c738ff2007-10-16 01:27:06 -070019 int pid = task->mm->context.id.u.pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Jeff Dikef3555592007-02-10 01:44:29 -080021 /*
22 * With ARCH_SET_FS (and ARCH_SET_GS is treated similarly to
23 * be safe), we need to call arch_prctl on the host because
24 * setting %fs may result in something else happening (like a
Jeff Dike6e6d74c2007-02-10 01:44:30 -080025 * GDT or thread.fs being set instead). So, we let the host
26 * fiddle the registers and thread struct and restore the
27 * registers afterwards.
Jeff Dikef3555592007-02-10 01:44:29 -080028 *
29 * So, the saved registers are stored to the process (this
30 * needed because a stub may have been the last thing to run),
31 * arch_prctl is run on the host, then the registers are read
32 * back.
33 */
Jeff Dikeba180fd2007-10-16 01:27:00 -070034 switch (code) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 case ARCH_SET_FS:
Paolo 'Blaisorblade' Giarrussof767b022005-05-28 15:52:03 -070036 case ARCH_SET_GS:
Jeff Dike3e6f2ac2008-02-04 22:30:58 -080037 ret = restore_registers(pid, &current->thread.regs.regs);
38 if (ret)
39 return ret;
Jeff Dikeba180fd2007-10-16 01:27:00 -070040 break;
41 case ARCH_GET_FS:
42 case ARCH_GET_GS:
43 /*
44 * With these two, we read to a local pointer and
45 * put_user it to the userspace pointer that we were
46 * given. If addr isn't valid (because it hasn't been
47 * faulted in or is just bogus), we want put_user to
48 * fault it in (or return -EFAULT) instead of having
49 * the host return -EFAULT.
50 */
51 ptr = &tmp;
52 }
Jeff Dikef3555592007-02-10 01:44:29 -080053
Jeff Dikeba180fd2007-10-16 01:27:00 -070054 ret = os_arch_prctl(pid, code, ptr);
55 if (ret)
56 return ret;
Jeff Dikef3555592007-02-10 01:44:29 -080057
Jeff Dikeba180fd2007-10-16 01:27:00 -070058 switch (code) {
Jeff Dikef3555592007-02-10 01:44:29 -080059 case ARCH_SET_FS:
Jeff Dike44f5c4c2007-03-07 20:41:26 -080060 current->thread.arch.fs = (unsigned long) ptr;
Jeff Dike3e6f2ac2008-02-04 22:30:58 -080061 ret = save_registers(pid, &current->thread.regs.regs);
Jeff Dike44f5c4c2007-03-07 20:41:26 -080062 break;
Jeff Dikef3555592007-02-10 01:44:29 -080063 case ARCH_SET_GS:
Jeff Dike3e6f2ac2008-02-04 22:30:58 -080064 ret = save_registers(pid, &current->thread.regs.regs);
Paolo 'Blaisorblade' Giarrussof767b022005-05-28 15:52:03 -070065 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 case ARCH_GET_FS:
Jeff Dikef3555592007-02-10 01:44:29 -080067 ret = put_user(tmp, addr);
Jeff Dikeba180fd2007-10-16 01:27:00 -070068 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 case ARCH_GET_GS:
Jeff Dikef3555592007-02-10 01:44:29 -080070 ret = put_user(tmp, addr);
Jeff Dikeba180fd2007-10-16 01:27:00 -070071 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 }
73
Jeff Dikef3555592007-02-10 01:44:29 -080074 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77long sys_arch_prctl(int code, unsigned long addr)
78{
Jeff Dike77bf4402007-10-16 01:26:58 -070079 return arch_prctl(current, code, (unsigned long __user *) addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
82long sys_clone(unsigned long clone_flags, unsigned long newsp,
83 void __user *parent_tid, void __user *child_tid)
84{
85 long ret;
86
Jeff Dikee0877f02005-06-25 14:55:21 -070087 if (!newsp)
88 newsp = UPT_SP(&current->thread.regs.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 current->thread.forking = 1;
Jeff Dikee0877f02005-06-25 14:55:21 -070090 ret = do_fork(clone_flags, newsp, &current->thread.regs, 0, parent_tid,
91 child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 current->thread.forking = 0;
Jeff Dikef3555592007-02-10 01:44:29 -080093 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
Jeff Dikef3555592007-02-10 01:44:29 -080095
Karol Swietlicki291248f2008-02-04 22:30:49 -080096void arch_switch_to(struct task_struct *to)
Jeff Dikef3555592007-02-10 01:44:29 -080097{
Jeff Dikeba180fd2007-10-16 01:27:00 -070098 if ((to->thread.arch.fs == 0) || (to->mm == NULL))
99 return;
Jeff Dikef3555592007-02-10 01:44:29 -0800100
Jeff Dikeba180fd2007-10-16 01:27:00 -0700101 arch_prctl(to, ARCH_SET_FS, (void __user *) to->thread.arch.fs);
Jeff Dikef3555592007-02-10 01:44:29 -0800102}