blob: 0b5c184dd5b3b80894c3db60ead6432ad20dca57 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright 2003 PathScale, Inc.
Jeff Dikef0c4cad2007-10-16 01:27:18 -07003 * Copyright (C) 2003 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Licensed under the GPL
6 */
7
Jeff Dike95906b22008-02-04 22:31:20 -08008#include <linux/mm.h>
Jeff Dikeba9950c2005-05-20 13:59:07 -07009#include <linux/sched.h>
10#include <linux/errno.h>
Jeff Dike95906b22008-02-04 22:31:20 -080011#define __FRAME_OFFSETS
12#include <asm/ptrace.h>
Jeff Dikeba9950c2005-05-20 13:59:07 -070013#include <asm/uaccess.h>
Richard Weinbergerda028d52015-06-25 22:44:11 +020014#include <asm/ptrace-abi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Jeff Dikef0c4cad2007-10-16 01:27:18 -070016/*
17 * determines which flags the user has access to.
18 * 1 = access 0 = no access
19 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#define FLAG_MASK 0x44dd5UL
21
Al Viro412f90e2011-08-18 20:02:29 +010022static const int reg_offsets[] =
23{
24 [R8 >> 3] = HOST_R8,
25 [R9 >> 3] = HOST_R9,
26 [R10 >> 3] = HOST_R10,
27 [R11 >> 3] = HOST_R11,
28 [R12 >> 3] = HOST_R12,
29 [R13 >> 3] = HOST_R13,
30 [R14 >> 3] = HOST_R14,
31 [R15 >> 3] = HOST_R15,
32 [RIP >> 3] = HOST_IP,
33 [RSP >> 3] = HOST_SP,
Al Viro3579a382011-08-18 20:10:09 +010034 [RAX >> 3] = HOST_AX,
35 [RBX >> 3] = HOST_BX,
36 [RCX >> 3] = HOST_CX,
37 [RDX >> 3] = HOST_DX,
38 [RSI >> 3] = HOST_SI,
39 [RDI >> 3] = HOST_DI,
40 [RBP >> 3] = HOST_BP,
Al Viro412f90e2011-08-18 20:02:29 +010041 [CS >> 3] = HOST_CS,
42 [SS >> 3] = HOST_SS,
43 [FS_BASE >> 3] = HOST_FS_BASE,
44 [GS_BASE >> 3] = HOST_GS_BASE,
45 [DS >> 3] = HOST_DS,
46 [ES >> 3] = HOST_ES,
47 [FS >> 3] = HOST_FS,
48 [GS >> 3] = HOST_GS,
49 [EFLAGS >> 3] = HOST_EFLAGS,
Al Viro966e8032011-08-18 20:12:19 +010050 [ORIG_RAX >> 3] = HOST_ORIG_AX,
Al Viro412f90e2011-08-18 20:02:29 +010051};
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053int putreg(struct task_struct *child, int regno, unsigned long value)
54{
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#ifdef TIF_IA32
Jeff Dike95906b22008-02-04 22:31:20 -080056 /*
57 * Some code in the 64bit emulation may not be 64bit clean.
58 * Don't take any chances.
59 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 if (test_tsk_thread_flag(child, TIF_IA32))
61 value &= 0xffffffff;
62#endif
Jeff Dike95906b22008-02-04 22:31:20 -080063 switch (regno) {
Al Viro412f90e2011-08-18 20:02:29 +010064 case R8:
65 case R9:
66 case R10:
67 case R11:
68 case R12:
69 case R13:
70 case R14:
71 case R15:
72 case RIP:
73 case RSP:
74 case RAX:
75 case RBX:
76 case RCX:
77 case RDX:
78 case RSI:
79 case RDI:
80 case RBP:
Mickaël Salaünce298562016-08-01 23:01:56 +020081 break;
82
Al Viro412f90e2011-08-18 20:02:29 +010083 case ORIG_RAX:
Mickaël Salaünce298562016-08-01 23:01:56 +020084 /* Update the syscall number. */
85 UPT_SYSCALL_NR(&child->thread.regs.regs) = value;
Al Viro412f90e2011-08-18 20:02:29 +010086 break;
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 case FS:
89 case GS:
90 case DS:
91 case ES:
92 case SS:
93 case CS:
94 if (value && (value & 3) != 3)
95 return -EIO;
96 value &= 0xffff;
97 break;
98
99 case FS_BASE:
100 case GS_BASE:
101 if (!((value >> 48) == 0 || (value >> 48) == 0xffff))
102 return -EIO;
103 break;
104
105 case EFLAGS:
106 value &= FLAG_MASK;
Al Viro412f90e2011-08-18 20:02:29 +0100107 child->thread.regs.regs.gp[HOST_EFLAGS] |= value;
108 return 0;
109
110 default:
111 panic("Bad register in putreg(): %d\n", regno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
113
Al Viro412f90e2011-08-18 20:02:29 +0100114 child->thread.regs.regs.gp[reg_offsets[regno >> 3]] = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return 0;
116}
117
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700118int poke_user(struct task_struct *child, long addr, long data)
119{
Jeff Dikef0c4cad2007-10-16 01:27:18 -0700120 if ((addr & 3) || addr < 0)
121 return -EIO;
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700122
Jeff Dikef0c4cad2007-10-16 01:27:18 -0700123 if (addr < MAX_REG_OFFSET)
124 return putreg(child, addr, data);
125 else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
Jeff Dike95906b22008-02-04 22:31:20 -0800126 (addr <= offsetof(struct user, u_debugreg[7]))) {
Jeff Dikef0c4cad2007-10-16 01:27:18 -0700127 addr -= offsetof(struct user, u_debugreg[0]);
128 addr = addr >> 2;
129 if ((addr == 4) || (addr == 5))
130 return -EIO;
131 child->thread.arch.debugregs[addr] = data;
132 return 0;
133 }
134 return -EIO;
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700135}
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137unsigned long getreg(struct task_struct *child, int regno)
138{
Al Viro412f90e2011-08-18 20:02:29 +0100139 unsigned long mask = ~0UL;
140#ifdef TIF_IA32
141 if (test_tsk_thread_flag(child, TIF_IA32))
142 mask = 0xffffffff;
143#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 switch (regno) {
Al Viro412f90e2011-08-18 20:02:29 +0100145 case R8:
146 case R9:
147 case R10:
148 case R11:
149 case R12:
150 case R13:
151 case R14:
152 case R15:
153 case RIP:
154 case RSP:
155 case RAX:
156 case RBX:
157 case RCX:
158 case RDX:
159 case RSI:
160 case RDI:
161 case RBP:
162 case ORIG_RAX:
163 case EFLAGS:
164 case FS_BASE:
165 case GS_BASE:
166 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 case FS:
168 case GS:
169 case DS:
170 case ES:
171 case SS:
172 case CS:
Al Viro412f90e2011-08-18 20:02:29 +0100173 mask = 0xffff;
174 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 default:
Al Viro412f90e2011-08-18 20:02:29 +0100176 panic("Bad register in getreg: %d\n", regno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
Al Viro412f90e2011-08-18 20:02:29 +0100178 return mask & child->thread.regs.regs.gp[reg_offsets[regno >> 3]];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700181int peek_user(struct task_struct *child, long addr, long data)
182{
183 /* read the word at location addr in the USER area. */
Jeff Dikef0c4cad2007-10-16 01:27:18 -0700184 unsigned long tmp;
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700185
Jeff Dikef0c4cad2007-10-16 01:27:18 -0700186 if ((addr & 3) || addr < 0)
187 return -EIO;
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700188
Jeff Dikef0c4cad2007-10-16 01:27:18 -0700189 tmp = 0; /* Default return condition */
Jeff Dike95906b22008-02-04 22:31:20 -0800190 if (addr < MAX_REG_OFFSET)
Jeff Dikef0c4cad2007-10-16 01:27:18 -0700191 tmp = getreg(child, addr);
Jeff Dikef0c4cad2007-10-16 01:27:18 -0700192 else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
Jeff Dike95906b22008-02-04 22:31:20 -0800193 (addr <= offsetof(struct user, u_debugreg[7]))) {
Jeff Dikef0c4cad2007-10-16 01:27:18 -0700194 addr -= offsetof(struct user, u_debugreg[0]);
195 addr = addr >> 2;
196 tmp = child->thread.arch.debugregs[addr];
197 }
198 return put_user(tmp, (unsigned long *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Bodo Stroesser81efcd32006-03-27 01:14:34 -0800201/* XXX Mostly copied from sys-i386 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202int is_syscall(unsigned long addr)
203{
Bodo Stroesser81efcd32006-03-27 01:14:34 -0800204 unsigned short instr;
205 int n;
206
207 n = copy_from_user(&instr, (void __user *) addr, sizeof(instr));
Jeff Dike95906b22008-02-04 22:31:20 -0800208 if (n) {
209 /*
210 * access_process_vm() grants access to vsyscall and stub,
Bodo Stroesser81efcd32006-03-27 01:14:34 -0800211 * while copy_from_user doesn't. Maybe access_process_vm is
212 * slow, but that doesn't matter, since it will be called only
213 * in case of singlestepping, if copy_from_user failed.
214 */
215 n = access_process_vm(current, addr, &instr, sizeof(instr), 0);
Jeff Dikef0c4cad2007-10-16 01:27:18 -0700216 if (n != sizeof(instr)) {
Bodo Stroesser81efcd32006-03-27 01:14:34 -0800217 printk("is_syscall : failed to read instruction from "
218 "0x%lx\n", addr);
Jeff Dikef0c4cad2007-10-16 01:27:18 -0700219 return 1;
Bodo Stroesser81efcd32006-03-27 01:14:34 -0800220 }
221 }
222 /* sysenter */
Jeff Dikef0c4cad2007-10-16 01:27:18 -0700223 return instr == 0x050f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Al Virof2833ae2011-09-14 16:21:37 -0700226static int get_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Jeff Dikee8012b52007-10-16 01:27:16 -0700228 int err, n, cpu = ((struct thread_info *) child->stack)->cpu;
Eli Coopera78ff112016-03-20 00:58:41 +0800229 struct user_i387_struct fpregs;
Jeff Dikee8012b52007-10-16 01:27:16 -0700230
Eli Coopera78ff112016-03-20 00:58:41 +0800231 err = save_i387_registers(userspace_pid[cpu],
232 (unsigned long *) &fpregs);
Jeff Dikee8012b52007-10-16 01:27:16 -0700233 if (err)
234 return err;
235
Eli Coopera78ff112016-03-20 00:58:41 +0800236 n = copy_to_user(buf, &fpregs, sizeof(fpregs));
Jeff Dike95906b22008-02-04 22:31:20 -0800237 if (n > 0)
Jeff Dikee8012b52007-10-16 01:27:16 -0700238 return -EFAULT;
239
240 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
Al Virof2833ae2011-09-14 16:21:37 -0700243static int set_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Jeff Dikee8012b52007-10-16 01:27:16 -0700245 int n, cpu = ((struct thread_info *) child->stack)->cpu;
Eli Coopera78ff112016-03-20 00:58:41 +0800246 struct user_i387_struct fpregs;
Jeff Dikee8012b52007-10-16 01:27:16 -0700247
Eli Coopera78ff112016-03-20 00:58:41 +0800248 n = copy_from_user(&fpregs, buf, sizeof(fpregs));
Jeff Dikee8012b52007-10-16 01:27:16 -0700249 if (n > 0)
250 return -EFAULT;
251
Eli Coopera78ff112016-03-20 00:58:41 +0800252 return restore_i387_registers(userspace_pid[cpu],
253 (unsigned long *) &fpregs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
Namhyung Kim9b05a692010-10-27 15:33:47 -0700256long subarch_ptrace(struct task_struct *child, long request,
257 unsigned long addr, unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Al Virof2833ae2011-09-14 16:21:37 -0700259 int ret = -EIO;
260 void __user *datap = (void __user *) data;
261
262 switch (request) {
263 case PTRACE_GETFPREGS: /* Get the child FPU state. */
264 ret = get_fpregs(datap, child);
265 break;
266 case PTRACE_SETFPREGS: /* Set the child FPU state. */
267 ret = set_fpregs(datap, child);
268 break;
269 case PTRACE_ARCH_PRCTL:
270 /* XXX Calls ptrace on the host - needs some SMP thinking */
271 ret = arch_prctl(child, data, (void __user *) addr);
272 break;
273 }
274
275 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}