blob: c9b176534d65bd15d97a86769cf978c510b7c475 [file] [log] [blame]
Jeff Dikeba180fd2007-10-16 01:27:00 -07001/*
2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Bodo Stroesser81efcd32006-03-27 01:14:34 -08006#include "linux/mm.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -07007#include "linux/sched.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include "asm/uaccess.h"
Jeff Dikee8012b52007-10-16 01:27:16 -07009#include "skas.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
Karol Swietlicki291248f2008-02-04 22:30:49 -080011extern int arch_switch_tls(struct task_struct *to);
Jeff Dike77bf4402007-10-16 01:26:58 -070012
Karol Swietlicki291248f2008-02-04 22:30:49 -080013void arch_switch_to(struct task_struct *to)
Paolo 'Blaisorblade' Giarrusso972410b2006-03-31 02:30:21 -080014{
Karol Swietlicki291248f2008-02-04 22:30:49 -080015 int err = arch_switch_tls(to);
Paolo 'Blaisorblade' Giarrusso54d8d3b2006-03-31 02:30:24 -080016 if (!err)
17 return;
18
19 if (err != -EINVAL)
Jeff Dikeba180fd2007-10-16 01:27:00 -070020 printk(KERN_WARNING "arch_switch_tls failed, errno %d, "
21 "not EINVAL\n", -err);
Paolo 'Blaisorblade' Giarrusso54d8d3b2006-03-31 02:30:24 -080022 else
Jeff Dike77bf4402007-10-16 01:26:58 -070023 printk(KERN_WARNING "arch_switch_tls failed, errno = EINVAL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070024}
25
26int is_syscall(unsigned long addr)
27{
28 unsigned short instr;
29 int n;
30
31 n = copy_from_user(&instr, (void __user *) addr, sizeof(instr));
Jeff Dikeba180fd2007-10-16 01:27:00 -070032 if (n) {
Bodo Stroesser81efcd32006-03-27 01:14:34 -080033 /* access_process_vm() grants access to vsyscall and stub,
34 * while copy_from_user doesn't. Maybe access_process_vm is
35 * slow, but that doesn't matter, since it will be called only
36 * in case of singlestepping, if copy_from_user failed.
37 */
38 n = access_process_vm(current, addr, &instr, sizeof(instr), 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -070039 if (n != sizeof(instr)) {
40 printk(KERN_ERR "is_syscall : failed to read "
41 "instruction from 0x%lx\n", addr);
42 return 1;
Bodo Stroesser81efcd32006-03-27 01:14:34 -080043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 }
45 /* int 0x80 or sysenter */
Jeff Dikeba180fd2007-10-16 01:27:00 -070046 return (instr == 0x80cd) || (instr == 0x340f);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
49/* determines which flags the user has access to. */
50/* 1 = access 0 = no access */
51#define FLAG_MASK 0x00044dd5
52
53int putreg(struct task_struct *child, int regno, unsigned long value)
54{
55 regno >>= 2;
56 switch (regno) {
57 case FS:
58 if (value && (value & 3) != 3)
59 return -EIO;
60 PT_REGS_FS(&child->thread.regs) = value;
61 return 0;
62 case GS:
63 if (value && (value & 3) != 3)
64 return -EIO;
65 PT_REGS_GS(&child->thread.regs) = value;
66 return 0;
67 case DS:
68 case ES:
69 if (value && (value & 3) != 3)
70 return -EIO;
71 value &= 0xffff;
72 break;
73 case SS:
74 case CS:
75 if ((value & 3) != 3)
76 return -EIO;
77 value &= 0xffff;
78 break;
79 case EFL:
80 value &= FLAG_MASK;
81 value |= PT_REGS_EFLAGS(&child->thread.regs);
82 break;
83 }
84 PT_REGS_SET(&child->thread.regs, regno, value);
85 return 0;
86}
87
Bodo Stroesser82c1c112005-05-06 21:30:46 -070088int poke_user(struct task_struct *child, long addr, long data)
89{
Jeff Dikeba180fd2007-10-16 01:27:00 -070090 if ((addr & 3) || addr < 0)
91 return -EIO;
Bodo Stroesser82c1c112005-05-06 21:30:46 -070092
Jeff Dikeba180fd2007-10-16 01:27:00 -070093 if (addr < MAX_REG_OFFSET)
94 return putreg(child, addr, data);
95 else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
96 (addr <= offsetof(struct user, u_debugreg[7]))) {
97 addr -= offsetof(struct user, u_debugreg[0]);
98 addr = addr >> 2;
99 if ((addr == 4) || (addr == 5))
100 return -EIO;
101 child->thread.arch.debugregs[addr] = data;
102 return 0;
103 }
104 return -EIO;
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700105}
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107unsigned long getreg(struct task_struct *child, int regno)
108{
109 unsigned long retval = ~0UL;
110
111 regno >>= 2;
112 switch (regno) {
113 case FS:
114 case GS:
115 case DS:
116 case ES:
117 case SS:
118 case CS:
119 retval = 0xffff;
120 /* fall through */
121 default:
122 retval &= PT_REG(&child->thread.regs, regno);
123 }
124 return retval;
125}
126
Jeff Dikeba180fd2007-10-16 01:27:00 -0700127/* read the word at location addr in the USER area. */
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700128int peek_user(struct task_struct *child, long addr, long data)
129{
Al Viro4d338e12006-03-31 02:30:15 -0800130 unsigned long tmp;
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700131
Al Viro4d338e12006-03-31 02:30:15 -0800132 if ((addr & 3) || addr < 0)
133 return -EIO;
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700134
Al Viro4d338e12006-03-31 02:30:15 -0800135 tmp = 0; /* Default return condition */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700136 if (addr < MAX_REG_OFFSET) {
Al Viro4d338e12006-03-31 02:30:15 -0800137 tmp = getreg(child, addr);
138 }
Jeff Dikeba180fd2007-10-16 01:27:00 -0700139 else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
140 (addr <= offsetof(struct user, u_debugreg[7]))) {
Al Viro4d338e12006-03-31 02:30:15 -0800141 addr -= offsetof(struct user, u_debugreg[0]);
142 addr = addr >> 2;
143 tmp = child->thread.arch.debugregs[addr];
144 }
145 return put_user(tmp, (unsigned long __user *) data);
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700146}
147
Jeff Dikee8012b52007-10-16 01:27:16 -0700148int get_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Jeff Dikee8012b52007-10-16 01:27:16 -0700150 int err, n, cpu = ((struct thread_info *) child->stack)->cpu;
Jeff Dike47906dd2008-05-12 14:01:50 -0700151 struct user_i387_struct fpregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Jeff Dike47906dd2008-05-12 14:01:50 -0700153 err = save_fp_registers(userspace_pid[cpu], (unsigned long *) &fpregs);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700154 if (err)
Jeff Dikee8012b52007-10-16 01:27:16 -0700155 return err;
156
Jeff Dike47906dd2008-05-12 14:01:50 -0700157 n = copy_to_user(buf, &fpregs, sizeof(fpregs));
Jeff Dikee8012b52007-10-16 01:27:16 -0700158 if(n > 0)
Jeff Dikeba180fd2007-10-16 01:27:00 -0700159 return -EFAULT;
Jeff Dikee8012b52007-10-16 01:27:16 -0700160
161 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Jeff Dikee8012b52007-10-16 01:27:16 -0700164int set_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Jeff Dikee8012b52007-10-16 01:27:16 -0700166 int n, cpu = ((struct thread_info *) child->stack)->cpu;
Jeff Dike47906dd2008-05-12 14:01:50 -0700167 struct user_i387_struct fpregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Jeff Dike47906dd2008-05-12 14:01:50 -0700169 n = copy_from_user(&fpregs, buf, sizeof(fpregs));
Jeff Dikee8012b52007-10-16 01:27:16 -0700170 if (n > 0)
171 return -EFAULT;
172
Jeff Dike47906dd2008-05-12 14:01:50 -0700173 return restore_fp_registers(userspace_pid[cpu],
174 (unsigned long *) &fpregs);
Jeff Dikee8012b52007-10-16 01:27:16 -0700175}
176
177int get_fpxregs(struct user_fxsr_struct __user *buf, struct task_struct *child)
178{
179 int err, n, cpu = ((struct thread_info *) child->stack)->cpu;
Jeff Dike47906dd2008-05-12 14:01:50 -0700180 struct user_fxsr_struct fpregs;
Jeff Dikee8012b52007-10-16 01:27:16 -0700181
Jeff Dike47906dd2008-05-12 14:01:50 -0700182 err = save_fpx_registers(userspace_pid[cpu], (unsigned long *) &fpregs);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700183 if (err)
Jeff Dikee8012b52007-10-16 01:27:16 -0700184 return err;
185
Jeff Dike47906dd2008-05-12 14:01:50 -0700186 n = copy_to_user(buf, &fpregs, sizeof(fpregs));
Jeff Dikee8012b52007-10-16 01:27:16 -0700187 if(n > 0)
Jeff Dikeba180fd2007-10-16 01:27:00 -0700188 return -EFAULT;
Jeff Dikee8012b52007-10-16 01:27:16 -0700189
190 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Jeff Dikee8012b52007-10-16 01:27:16 -0700193int set_fpxregs(struct user_fxsr_struct __user *buf, struct task_struct *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Jeff Dikee8012b52007-10-16 01:27:16 -0700195 int n, cpu = ((struct thread_info *) child->stack)->cpu;
Jeff Dike47906dd2008-05-12 14:01:50 -0700196 struct user_fxsr_struct fpregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Jeff Dike47906dd2008-05-12 14:01:50 -0700198 n = copy_from_user(&fpregs, buf, sizeof(fpregs));
Jeff Dikee8012b52007-10-16 01:27:16 -0700199 if (n > 0)
200 return -EFAULT;
201
Jeff Dike47906dd2008-05-12 14:01:50 -0700202 return restore_fpx_registers(userspace_pid[cpu],
203 (unsigned long *) &fpregs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Jeff Dikee8012b52007-10-16 01:27:16 -0700206long subarch_ptrace(struct task_struct *child, long request, long addr,
207 long data)
208{
209 return -EIO;
210}