blob: 2497bac560663150f49355f163c6fdadf44861a4 [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
Al Viro37185b32012-10-08 03:27:32 +01006#include <linux/mm.h>
7#include <linux/sched.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -08008#include <linux/uaccess.h>
Richard Weinbergerda028d52015-06-25 22:44:11 +02009#include <asm/ptrace-abi.h>
Al Viro37185b32012-10-08 03:27:32 +010010#include <skas.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Karol Swietlicki291248f2008-02-04 22:30:49 -080012extern int arch_switch_tls(struct task_struct *to);
Jeff Dike77bf4402007-10-16 01:26:58 -070013
Karol Swietlicki291248f2008-02-04 22:30:49 -080014void arch_switch_to(struct task_struct *to)
Paolo 'Blaisorblade' Giarrusso972410b2006-03-31 02:30:21 -080015{
Karol Swietlicki291248f2008-02-04 22:30:49 -080016 int err = arch_switch_tls(to);
Paolo 'Blaisorblade' Giarrusso54d8d3b2006-03-31 02:30:24 -080017 if (!err)
18 return;
19
20 if (err != -EINVAL)
Jeff Dikeba180fd2007-10-16 01:27:00 -070021 printk(KERN_WARNING "arch_switch_tls failed, errno %d, "
22 "not EINVAL\n", -err);
Paolo 'Blaisorblade' Giarrusso54d8d3b2006-03-31 02:30:24 -080023 else
Jeff Dike77bf4402007-10-16 01:26:58 -070024 printk(KERN_WARNING "arch_switch_tls failed, errno = EINVAL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070025}
26
27int is_syscall(unsigned long addr)
28{
29 unsigned short instr;
30 int n;
31
32 n = copy_from_user(&instr, (void __user *) addr, sizeof(instr));
Jeff Dikeba180fd2007-10-16 01:27:00 -070033 if (n) {
Bodo Stroesser81efcd32006-03-27 01:14:34 -080034 /* access_process_vm() grants access to vsyscall and stub,
35 * while copy_from_user doesn't. Maybe access_process_vm is
36 * slow, but that doesn't matter, since it will be called only
37 * in case of singlestepping, if copy_from_user failed.
38 */
Lorenzo Stoakesf307ab62016-10-13 01:20:20 +010039 n = access_process_vm(current, addr, &instr, sizeof(instr),
40 FOLL_FORCE);
Jeff Dikeba180fd2007-10-16 01:27:00 -070041 if (n != sizeof(instr)) {
42 printk(KERN_ERR "is_syscall : failed to read "
43 "instruction from 0x%lx\n", addr);
44 return 1;
Bodo Stroesser81efcd32006-03-27 01:14:34 -080045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 }
47 /* int 0x80 or sysenter */
Jeff Dikeba180fd2007-10-16 01:27:00 -070048 return (instr == 0x80cd) || (instr == 0x340f);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
51/* determines which flags the user has access to. */
52/* 1 = access 0 = no access */
53#define FLAG_MASK 0x00044dd5
54
Al Viro412f90e2011-08-18 20:02:29 +010055static const int reg_offsets[] = {
Al Viro3579a382011-08-18 20:10:09 +010056 [EBX] = HOST_BX,
57 [ECX] = HOST_CX,
58 [EDX] = HOST_DX,
59 [ESI] = HOST_SI,
60 [EDI] = HOST_DI,
61 [EBP] = HOST_BP,
62 [EAX] = HOST_AX,
Al Viro412f90e2011-08-18 20:02:29 +010063 [DS] = HOST_DS,
64 [ES] = HOST_ES,
65 [FS] = HOST_FS,
66 [GS] = HOST_GS,
67 [EIP] = HOST_IP,
68 [CS] = HOST_CS,
69 [EFL] = HOST_EFLAGS,
70 [UESP] = HOST_SP,
71 [SS] = HOST_SS,
Mickaël Salaüne04c9892015-12-29 21:35:44 +010072 [ORIG_EAX] = HOST_ORIG_AX,
Al Viro412f90e2011-08-18 20:02:29 +010073};
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075int putreg(struct task_struct *child, int regno, unsigned long value)
76{
77 regno >>= 2;
78 switch (regno) {
Al Viro412f90e2011-08-18 20:02:29 +010079 case EBX:
80 case ECX:
81 case EDX:
82 case ESI:
83 case EDI:
84 case EBP:
85 case EAX:
86 case EIP:
87 case UESP:
Mickaël Salaünce298562016-08-01 23:01:56 +020088 break;
Mickaël Salaüne04c9892015-12-29 21:35:44 +010089 case ORIG_EAX:
Mickaël Salaünce298562016-08-01 23:01:56 +020090 /* Update the syscall number. */
91 UPT_SYSCALL_NR(&child->thread.regs.regs) = value;
Al Viro412f90e2011-08-18 20:02:29 +010092 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 case FS:
94 if (value && (value & 3) != 3)
95 return -EIO;
Al Viro412f90e2011-08-18 20:02:29 +010096 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 case GS:
98 if (value && (value & 3) != 3)
99 return -EIO;
Al Viro412f90e2011-08-18 20:02:29 +0100100 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 case DS:
102 case ES:
103 if (value && (value & 3) != 3)
104 return -EIO;
105 value &= 0xffff;
106 break;
107 case SS:
108 case CS:
109 if ((value & 3) != 3)
110 return -EIO;
111 value &= 0xffff;
112 break;
113 case EFL:
114 value &= FLAG_MASK;
Al Viro412f90e2011-08-18 20:02:29 +0100115 child->thread.regs.regs.gp[HOST_EFLAGS] |= value;
116 return 0;
Al Viro412f90e2011-08-18 20:02:29 +0100117 default :
118 panic("Bad register in putreg() : %d\n", regno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
Al Viro412f90e2011-08-18 20:02:29 +0100120 child->thread.regs.regs.gp[reg_offsets[regno]] = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return 0;
122}
123
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700124int poke_user(struct task_struct *child, long addr, long data)
125{
Jeff Dikeba180fd2007-10-16 01:27:00 -0700126 if ((addr & 3) || addr < 0)
127 return -EIO;
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700128
Jeff Dikeba180fd2007-10-16 01:27:00 -0700129 if (addr < MAX_REG_OFFSET)
130 return putreg(child, addr, data);
131 else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
132 (addr <= offsetof(struct user, u_debugreg[7]))) {
133 addr -= offsetof(struct user, u_debugreg[0]);
134 addr = addr >> 2;
135 if ((addr == 4) || (addr == 5))
136 return -EIO;
137 child->thread.arch.debugregs[addr] = data;
138 return 0;
139 }
140 return -EIO;
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700141}
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143unsigned long getreg(struct task_struct *child, int regno)
144{
Al Viro412f90e2011-08-18 20:02:29 +0100145 unsigned long mask = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 regno >>= 2;
148 switch (regno) {
149 case FS:
150 case GS:
151 case DS:
152 case ES:
153 case SS:
154 case CS:
Al Viro412f90e2011-08-18 20:02:29 +0100155 mask = 0xffff;
156 break;
157 case EIP:
158 case UESP:
159 case EAX:
160 case EBX:
161 case ECX:
162 case EDX:
163 case ESI:
164 case EDI:
165 case EBP:
166 case EFL:
Mickaël Salaüne04c9892015-12-29 21:35:44 +0100167 case ORIG_EAX:
Al Viro412f90e2011-08-18 20:02:29 +0100168 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 default:
Al Viro412f90e2011-08-18 20:02:29 +0100170 panic("Bad register in getreg() : %d\n", regno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
Al Viro412f90e2011-08-18 20:02:29 +0100172 return mask & child->thread.regs.regs.gp[reg_offsets[regno]];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Jeff Dikeba180fd2007-10-16 01:27:00 -0700175/* read the word at location addr in the USER area. */
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700176int peek_user(struct task_struct *child, long addr, long data)
177{
Al Viro4d338e12006-03-31 02:30:15 -0800178 unsigned long tmp;
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700179
Al Viro4d338e12006-03-31 02:30:15 -0800180 if ((addr & 3) || addr < 0)
181 return -EIO;
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700182
Al Viro4d338e12006-03-31 02:30:15 -0800183 tmp = 0; /* Default return condition */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700184 if (addr < MAX_REG_OFFSET) {
Al Viro4d338e12006-03-31 02:30:15 -0800185 tmp = getreg(child, addr);
186 }
Jeff Dikeba180fd2007-10-16 01:27:00 -0700187 else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
188 (addr <= offsetof(struct user, u_debugreg[7]))) {
Al Viro4d338e12006-03-31 02:30:15 -0800189 addr -= offsetof(struct user, u_debugreg[0]);
190 addr = addr >> 2;
191 tmp = child->thread.arch.debugregs[addr];
192 }
193 return put_user(tmp, (unsigned long __user *) data);
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700194}
195
Al Virof2833ae2011-09-14 16:21:37 -0700196static int get_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Linus Torvaldsd896fa22016-09-13 14:29:23 -0700198 int err, n, cpu = task_cpu(child);
Jeff Dike47906dd2008-05-12 14:01:50 -0700199 struct user_i387_struct fpregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Eli Coopera78ff112016-03-20 00:58:41 +0800201 err = save_i387_registers(userspace_pid[cpu],
202 (unsigned long *) &fpregs);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700203 if (err)
Jeff Dikee8012b52007-10-16 01:27:16 -0700204 return err;
205
Jeff Dike47906dd2008-05-12 14:01:50 -0700206 n = copy_to_user(buf, &fpregs, sizeof(fpregs));
Jeff Dikee8012b52007-10-16 01:27:16 -0700207 if(n > 0)
Jeff Dikeba180fd2007-10-16 01:27:00 -0700208 return -EFAULT;
Jeff Dikee8012b52007-10-16 01:27:16 -0700209
210 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
Al Virof2833ae2011-09-14 16:21:37 -0700213static int set_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Linus Torvaldsd896fa22016-09-13 14:29:23 -0700215 int n, cpu = task_cpu(child);
Jeff Dike47906dd2008-05-12 14:01:50 -0700216 struct user_i387_struct fpregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Jeff Dike47906dd2008-05-12 14:01:50 -0700218 n = copy_from_user(&fpregs, buf, sizeof(fpregs));
Jeff Dikee8012b52007-10-16 01:27:16 -0700219 if (n > 0)
220 return -EFAULT;
221
Eli Coopera78ff112016-03-20 00:58:41 +0800222 return restore_i387_registers(userspace_pid[cpu],
Jeff Dike47906dd2008-05-12 14:01:50 -0700223 (unsigned long *) &fpregs);
Jeff Dikee8012b52007-10-16 01:27:16 -0700224}
225
Al Virof2833ae2011-09-14 16:21:37 -0700226static int get_fpxregs(struct user_fxsr_struct __user *buf, struct task_struct *child)
Jeff Dikee8012b52007-10-16 01:27:16 -0700227{
Linus Torvaldsd896fa22016-09-13 14:29:23 -0700228 int err, n, cpu = task_cpu(child);
Jeff Dike47906dd2008-05-12 14:01:50 -0700229 struct user_fxsr_struct fpregs;
Jeff Dikee8012b52007-10-16 01:27:16 -0700230
Jeff Dike47906dd2008-05-12 14:01:50 -0700231 err = save_fpx_registers(userspace_pid[cpu], (unsigned long *) &fpregs);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700232 if (err)
Jeff Dikee8012b52007-10-16 01:27:16 -0700233 return err;
234
Jeff Dike47906dd2008-05-12 14:01:50 -0700235 n = copy_to_user(buf, &fpregs, sizeof(fpregs));
Jeff Dikee8012b52007-10-16 01:27:16 -0700236 if(n > 0)
Jeff Dikeba180fd2007-10-16 01:27:00 -0700237 return -EFAULT;
Jeff Dikee8012b52007-10-16 01:27:16 -0700238
239 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Al Virof2833ae2011-09-14 16:21:37 -0700242static int set_fpxregs(struct user_fxsr_struct __user *buf, struct task_struct *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Linus Torvaldsd896fa22016-09-13 14:29:23 -0700244 int n, cpu = task_cpu(child);
Jeff Dike47906dd2008-05-12 14:01:50 -0700245 struct user_fxsr_struct fpregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Jeff Dike47906dd2008-05-12 14:01:50 -0700247 n = copy_from_user(&fpregs, buf, sizeof(fpregs));
Jeff Dikee8012b52007-10-16 01:27:16 -0700248 if (n > 0)
249 return -EFAULT;
250
Jeff Dike47906dd2008-05-12 14:01:50 -0700251 return restore_fpx_registers(userspace_pid[cpu],
252 (unsigned long *) &fpregs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
Namhyung Kim9b05a692010-10-27 15:33:47 -0700255long subarch_ptrace(struct task_struct *child, long request,
256 unsigned long addr, unsigned long data)
Jeff Dikee8012b52007-10-16 01:27:16 -0700257{
Al Viro01599cd2011-09-14 16:21:34 -0700258 int ret = -EIO;
259 void __user *datap = (void __user *) data;
Al Viro01599cd2011-09-14 16:21:34 -0700260 switch (request) {
Al Virof2833ae2011-09-14 16:21:37 -0700261 case PTRACE_GETFPREGS: /* Get the child FPU state. */
262 ret = get_fpregs(datap, child);
263 break;
264 case PTRACE_SETFPREGS: /* Set the child FPU state. */
265 ret = set_fpregs(datap, child);
266 break;
Al Viro01599cd2011-09-14 16:21:34 -0700267 case PTRACE_GETFPXREGS: /* Get the child FPU state. */
268 ret = get_fpxregs(datap, child);
269 break;
270 case PTRACE_SETFPXREGS: /* Set the child FPU state. */
271 ret = set_fpxregs(datap, child);
272 break;
Al Virof2833ae2011-09-14 16:21:37 -0700273 default:
274 ret = -EIO;
Al Viro01599cd2011-09-14 16:21:34 -0700275 }
Al Viro01599cd2011-09-14 16:21:34 -0700276 return ret;
Jeff Dikee8012b52007-10-16 01:27:16 -0700277}