blob: 86f6b18410ee9e470159c3b63ab8b0e814cff600 [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
Paolo 'Blaisorblade' Giarrusso80f95072005-05-01 08:58:55 -070015asmlinkage long sys_uname64(struct new_utsname __user * name)
16{
17 int err;
Jeff Dikeba180fd2007-10-16 01:27:00 -070018
Paolo 'Blaisorblade' Giarrusso80f95072005-05-01 08:58:55 -070019 down_read(&uts_sem);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -070020 err = copy_to_user(name, utsname(), sizeof (*name));
Paolo 'Blaisorblade' Giarrusso80f95072005-05-01 08:58:55 -070021 up_read(&uts_sem);
Jeff Dikeba180fd2007-10-16 01:27:00 -070022
Paolo 'Blaisorblade' Giarrusso80f95072005-05-01 08:58:55 -070023 if (personality(current->personality) == PER_LINUX32)
24 err |= copy_to_user(&name->machine, "i686", 5);
Jeff Dikeba180fd2007-10-16 01:27:00 -070025
Paolo 'Blaisorblade' Giarrusso80f95072005-05-01 08:58:55 -070026 return err ? -EFAULT : 0;
27}
28
Jeff Dike77bf4402007-10-16 01:26:58 -070029long arch_prctl(struct task_struct *task, int code, unsigned long __user *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Jeff Dikeba180fd2007-10-16 01:27:00 -070031 unsigned long *ptr = addr, tmp;
Jeff Dikef3555592007-02-10 01:44:29 -080032 long ret;
Jeff Dike6c738ff2007-10-16 01:27:06 -070033 int pid = task->mm->context.id.u.pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Jeff Dikef3555592007-02-10 01:44:29 -080035 /*
36 * With ARCH_SET_FS (and ARCH_SET_GS is treated similarly to
37 * be safe), we need to call arch_prctl on the host because
38 * setting %fs may result in something else happening (like a
Jeff Dike6e6d74c2007-02-10 01:44:30 -080039 * GDT or thread.fs being set instead). So, we let the host
40 * fiddle the registers and thread struct and restore the
41 * registers afterwards.
Jeff Dikef3555592007-02-10 01:44:29 -080042 *
43 * So, the saved registers are stored to the process (this
44 * needed because a stub may have been the last thing to run),
45 * arch_prctl is run on the host, then the registers are read
46 * back.
47 */
Jeff Dikeba180fd2007-10-16 01:27:00 -070048 switch (code) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 case ARCH_SET_FS:
Paolo 'Blaisorblade' Giarrussof767b022005-05-28 15:52:03 -070050 case ARCH_SET_GS:
Jeff Dikeba180fd2007-10-16 01:27:00 -070051 restore_registers(pid, &current->thread.regs.regs);
52 break;
53 case ARCH_GET_FS:
54 case ARCH_GET_GS:
55 /*
56 * With these two, we read to a local pointer and
57 * put_user it to the userspace pointer that we were
58 * given. If addr isn't valid (because it hasn't been
59 * faulted in or is just bogus), we want put_user to
60 * fault it in (or return -EFAULT) instead of having
61 * the host return -EFAULT.
62 */
63 ptr = &tmp;
64 }
Jeff Dikef3555592007-02-10 01:44:29 -080065
Jeff Dikeba180fd2007-10-16 01:27:00 -070066 ret = os_arch_prctl(pid, code, ptr);
67 if (ret)
68 return ret;
Jeff Dikef3555592007-02-10 01:44:29 -080069
Jeff Dikeba180fd2007-10-16 01:27:00 -070070 switch (code) {
Jeff Dikef3555592007-02-10 01:44:29 -080071 case ARCH_SET_FS:
Jeff Dike44f5c4c2007-03-07 20:41:26 -080072 current->thread.arch.fs = (unsigned long) ptr;
73 save_registers(pid, &current->thread.regs.regs);
74 break;
Jeff Dikef3555592007-02-10 01:44:29 -080075 case ARCH_SET_GS:
Jeff Dikeba180fd2007-10-16 01:27:00 -070076 save_registers(pid, &current->thread.regs.regs);
Paolo 'Blaisorblade' Giarrussof767b022005-05-28 15:52:03 -070077 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 case ARCH_GET_FS:
Jeff Dikef3555592007-02-10 01:44:29 -080079 ret = put_user(tmp, addr);
Jeff Dikeba180fd2007-10-16 01:27:00 -070080 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 case ARCH_GET_GS:
Jeff Dikef3555592007-02-10 01:44:29 -080082 ret = put_user(tmp, addr);
Jeff Dikeba180fd2007-10-16 01:27:00 -070083 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
85
Jeff Dikef3555592007-02-10 01:44:29 -080086 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89long sys_arch_prctl(int code, unsigned long addr)
90{
Jeff Dike77bf4402007-10-16 01:26:58 -070091 return arch_prctl(current, code, (unsigned long __user *) addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
94long sys_clone(unsigned long clone_flags, unsigned long newsp,
95 void __user *parent_tid, void __user *child_tid)
96{
97 long ret;
98
Jeff Dikee0877f02005-06-25 14:55:21 -070099 if (!newsp)
100 newsp = UPT_SP(&current->thread.regs.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 current->thread.forking = 1;
Jeff Dikee0877f02005-06-25 14:55:21 -0700102 ret = do_fork(clone_flags, newsp, &current->thread.regs, 0, parent_tid,
103 child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 current->thread.forking = 0;
Jeff Dikef3555592007-02-10 01:44:29 -0800105 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
Jeff Dikef3555592007-02-10 01:44:29 -0800107
Jeff Dike77bf4402007-10-16 01:26:58 -0700108void arch_switch_to(struct task_struct *from, struct task_struct *to)
Jeff Dikef3555592007-02-10 01:44:29 -0800109{
Jeff Dikeba180fd2007-10-16 01:27:00 -0700110 if ((to->thread.arch.fs == 0) || (to->mm == NULL))
111 return;
Jeff Dikef3555592007-02-10 01:44:29 -0800112
Jeff Dikeba180fd2007-10-16 01:27:00 -0700113 arch_prctl(to, ARCH_SET_FS, (void __user *) to->thread.arch.fs);
Jeff Dikef3555592007-02-10 01:44:29 -0800114}