blob: bbcab773b23dfe82f8c7caeec100d37ae8ecb518 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright 2003 PathScale, Inc.
3 *
4 * Licensed under the GPL
5 */
6
7#include "linux/linkage.h"
8#include "linux/slab.h"
9#include "linux/shm.h"
Paolo 'Blaisorblade' Giarrussof7fe8782005-05-05 16:15:15 -070010#include "linux/utsname.h"
11#include "linux/personality.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "asm/uaccess.h"
13#define __FRAME_OFFSETS
14#include "asm/ptrace.h"
15#include "asm/unistd.h"
16#include "asm/prctl.h" /* XXX This should get the constants from libc */
Jeff Dikeba9950c2005-05-20 13:59:07 -070017#include "kern.h"
Jeff Dikef3555592007-02-10 01:44:29 -080018#include "os.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Paolo 'Blaisorblade' Giarrusso80f95072005-05-01 08:58:55 -070020asmlinkage long sys_uname64(struct new_utsname __user * name)
21{
22 int err;
23 down_read(&uts_sem);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -070024 err = copy_to_user(name, utsname(), sizeof (*name));
Paolo 'Blaisorblade' Giarrusso80f95072005-05-01 08:58:55 -070025 up_read(&uts_sem);
26 if (personality(current->personality) == PER_LINUX32)
27 err |= copy_to_user(&name->machine, "i686", 5);
28 return err ? -EFAULT : 0;
29}
30
Jeff Dike77bf4402007-10-16 01:26:58 -070031long arch_prctl(struct task_struct *task, int code, unsigned long __user *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
Jeff Dikef3555592007-02-10 01:44:29 -080033 unsigned long *ptr = addr, tmp;
34 long ret;
Jeff Dike6e6d74c2007-02-10 01:44:30 -080035 int pid = task->mm->context.skas.id.u.pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Jeff Dikef3555592007-02-10 01:44:29 -080037 /*
38 * With ARCH_SET_FS (and ARCH_SET_GS is treated similarly to
39 * be safe), we need to call arch_prctl on the host because
40 * setting %fs may result in something else happening (like a
Jeff Dike6e6d74c2007-02-10 01:44:30 -080041 * GDT or thread.fs being set instead). So, we let the host
42 * fiddle the registers and thread struct and restore the
43 * registers afterwards.
Jeff Dikef3555592007-02-10 01:44:29 -080044 *
45 * So, the saved registers are stored to the process (this
46 * needed because a stub may have been the last thing to run),
47 * arch_prctl is run on the host, then the registers are read
48 * back.
49 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 switch(code){
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 case ARCH_SET_FS:
Paolo 'Blaisorblade' Giarrussof767b022005-05-28 15:52:03 -070052 case ARCH_SET_GS:
Jeff Dikef3555592007-02-10 01:44:29 -080053 restore_registers(pid, &current->thread.regs.regs);
54 break;
55 case ARCH_GET_FS:
56 case ARCH_GET_GS:
57 /*
58 * With these two, we read to a local pointer and
59 * put_user it to the userspace pointer that we were
60 * given. If addr isn't valid (because it hasn't been
61 * faulted in or is just bogus), we want put_user to
62 * fault it in (or return -EFAULT) instead of having
63 * the host return -EFAULT.
64 */
65 ptr = &tmp;
66 }
67
68 ret = os_arch_prctl(pid, code, ptr);
69 if(ret)
70 return ret;
71
72 switch(code){
73 case ARCH_SET_FS:
Jeff Dike44f5c4c2007-03-07 20:41:26 -080074 current->thread.arch.fs = (unsigned long) ptr;
75 save_registers(pid, &current->thread.regs.regs);
76 break;
Jeff Dikef3555592007-02-10 01:44:29 -080077 case ARCH_SET_GS:
78 save_registers(pid, &current->thread.regs.regs);
Paolo 'Blaisorblade' Giarrussof767b022005-05-28 15:52:03 -070079 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 case ARCH_GET_FS:
Jeff Dikef3555592007-02-10 01:44:29 -080081 ret = put_user(tmp, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 break;
83 case ARCH_GET_GS:
Jeff Dikef3555592007-02-10 01:44:29 -080084 ret = put_user(tmp, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
87
Jeff Dikef3555592007-02-10 01:44:29 -080088 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91long sys_arch_prctl(int code, unsigned long addr)
92{
Jeff Dike77bf4402007-10-16 01:26:58 -070093 return arch_prctl(current, code, (unsigned long __user *) addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
96long sys_clone(unsigned long clone_flags, unsigned long newsp,
97 void __user *parent_tid, void __user *child_tid)
98{
99 long ret;
100
Jeff Dikee0877f02005-06-25 14:55:21 -0700101 if (!newsp)
102 newsp = UPT_SP(&current->thread.regs.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 current->thread.forking = 1;
Jeff Dikee0877f02005-06-25 14:55:21 -0700104 ret = do_fork(clone_flags, newsp, &current->thread.regs, 0, parent_tid,
105 child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 current->thread.forking = 0;
Jeff Dikef3555592007-02-10 01:44:29 -0800107 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
Jeff Dikef3555592007-02-10 01:44:29 -0800109
Jeff Dike77bf4402007-10-16 01:26:58 -0700110void arch_switch_to(struct task_struct *from, struct task_struct *to)
Jeff Dikef3555592007-02-10 01:44:29 -0800111{
Jeff Dike44f5c4c2007-03-07 20:41:26 -0800112 if((to->thread.arch.fs == 0) || (to->mm == NULL))
Jeff Dikef3555592007-02-10 01:44:29 -0800113 return;
114
Jeff Dike77bf4402007-10-16 01:26:58 -0700115 arch_prctl(to, ARCH_SET_FS, (void __user *) to->thread.arch.fs);
Jeff Dikef3555592007-02-10 01:44:29 -0800116}