blob: 47c78d5e5c3202a04f5c8a6a65432459142678f5 [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>
8#include <asm/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 */
39 n = access_process_vm(current, addr, &instr, sizeof(instr), 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -070040 if (n != sizeof(instr)) {
41 printk(KERN_ERR "is_syscall : failed to read "
42 "instruction from 0x%lx\n", addr);
43 return 1;
Bodo Stroesser81efcd32006-03-27 01:14:34 -080044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 }
46 /* int 0x80 or sysenter */
Jeff Dikeba180fd2007-10-16 01:27:00 -070047 return (instr == 0x80cd) || (instr == 0x340f);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
50/* determines which flags the user has access to. */
51/* 1 = access 0 = no access */
52#define FLAG_MASK 0x00044dd5
53
Al Viro412f90e2011-08-18 20:02:29 +010054static const int reg_offsets[] = {
Al Viro3579a382011-08-18 20:10:09 +010055 [EBX] = HOST_BX,
56 [ECX] = HOST_CX,
57 [EDX] = HOST_DX,
58 [ESI] = HOST_SI,
59 [EDI] = HOST_DI,
60 [EBP] = HOST_BP,
61 [EAX] = HOST_AX,
Al Viro412f90e2011-08-18 20:02:29 +010062 [DS] = HOST_DS,
63 [ES] = HOST_ES,
64 [FS] = HOST_FS,
65 [GS] = HOST_GS,
66 [EIP] = HOST_IP,
67 [CS] = HOST_CS,
68 [EFL] = HOST_EFLAGS,
69 [UESP] = HOST_SP,
70 [SS] = HOST_SS,
Mickaël Salaüne04c9892015-12-29 21:35:44 +010071 [ORIG_EAX] = HOST_ORIG_AX,
Al Viro412f90e2011-08-18 20:02:29 +010072};
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074int putreg(struct task_struct *child, int regno, unsigned long value)
75{
76 regno >>= 2;
77 switch (regno) {
Al Viro412f90e2011-08-18 20:02:29 +010078 case EBX:
79 case ECX:
80 case EDX:
81 case ESI:
82 case EDI:
83 case EBP:
84 case EAX:
85 case EIP:
86 case UESP:
Mickaël Salaüne04c9892015-12-29 21:35:44 +010087 case ORIG_EAX:
Al Viro412f90e2011-08-18 20:02:29 +010088 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 case FS:
90 if (value && (value & 3) != 3)
91 return -EIO;
Al Viro412f90e2011-08-18 20:02:29 +010092 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 case GS:
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 DS:
98 case ES:
99 if (value && (value & 3) != 3)
100 return -EIO;
101 value &= 0xffff;
102 break;
103 case SS:
104 case CS:
105 if ((value & 3) != 3)
106 return -EIO;
107 value &= 0xffff;
108 break;
109 case EFL:
110 value &= FLAG_MASK;
Al Viro412f90e2011-08-18 20:02:29 +0100111 child->thread.regs.regs.gp[HOST_EFLAGS] |= value;
112 return 0;
Al Viro412f90e2011-08-18 20:02:29 +0100113 default :
114 panic("Bad register in putreg() : %d\n", regno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
Al Viro412f90e2011-08-18 20:02:29 +0100116 child->thread.regs.regs.gp[reg_offsets[regno]] = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return 0;
118}
119
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700120int poke_user(struct task_struct *child, long addr, long data)
121{
Jeff Dikeba180fd2007-10-16 01:27:00 -0700122 if ((addr & 3) || addr < 0)
123 return -EIO;
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700124
Jeff Dikeba180fd2007-10-16 01:27:00 -0700125 if (addr < MAX_REG_OFFSET)
126 return putreg(child, addr, data);
127 else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
128 (addr <= offsetof(struct user, u_debugreg[7]))) {
129 addr -= offsetof(struct user, u_debugreg[0]);
130 addr = addr >> 2;
131 if ((addr == 4) || (addr == 5))
132 return -EIO;
133 child->thread.arch.debugregs[addr] = data;
134 return 0;
135 }
136 return -EIO;
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700137}
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139unsigned long getreg(struct task_struct *child, int regno)
140{
Al Viro412f90e2011-08-18 20:02:29 +0100141 unsigned long mask = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 regno >>= 2;
144 switch (regno) {
145 case FS:
146 case GS:
147 case DS:
148 case ES:
149 case SS:
150 case CS:
Al Viro412f90e2011-08-18 20:02:29 +0100151 mask = 0xffff;
152 break;
153 case EIP:
154 case UESP:
155 case EAX:
156 case EBX:
157 case ECX:
158 case EDX:
159 case ESI:
160 case EDI:
161 case EBP:
162 case EFL:
Mickaël Salaüne04c9892015-12-29 21:35:44 +0100163 case ORIG_EAX:
Al Viro412f90e2011-08-18 20:02:29 +0100164 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 default:
Al Viro412f90e2011-08-18 20:02:29 +0100166 panic("Bad register in getreg() : %d\n", regno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
Al Viro412f90e2011-08-18 20:02:29 +0100168 return mask & child->thread.regs.regs.gp[reg_offsets[regno]];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Jeff Dikeba180fd2007-10-16 01:27:00 -0700171/* read the word at location addr in the USER area. */
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700172int peek_user(struct task_struct *child, long addr, long data)
173{
Al Viro4d338e12006-03-31 02:30:15 -0800174 unsigned long tmp;
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700175
Al Viro4d338e12006-03-31 02:30:15 -0800176 if ((addr & 3) || addr < 0)
177 return -EIO;
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700178
Al Viro4d338e12006-03-31 02:30:15 -0800179 tmp = 0; /* Default return condition */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700180 if (addr < MAX_REG_OFFSET) {
Al Viro4d338e12006-03-31 02:30:15 -0800181 tmp = getreg(child, addr);
182 }
Jeff Dikeba180fd2007-10-16 01:27:00 -0700183 else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
184 (addr <= offsetof(struct user, u_debugreg[7]))) {
Al Viro4d338e12006-03-31 02:30:15 -0800185 addr -= offsetof(struct user, u_debugreg[0]);
186 addr = addr >> 2;
187 tmp = child->thread.arch.debugregs[addr];
188 }
189 return put_user(tmp, (unsigned long __user *) data);
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700190}
191
Al Virof2833ae2011-09-14 16:21:37 -0700192static int get_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Jeff Dikee8012b52007-10-16 01:27:16 -0700194 int err, n, cpu = ((struct thread_info *) child->stack)->cpu;
Jeff Dike47906dd2008-05-12 14:01:50 -0700195 struct user_i387_struct fpregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Jeff Dike47906dd2008-05-12 14:01:50 -0700197 err = save_fp_registers(userspace_pid[cpu], (unsigned long *) &fpregs);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700198 if (err)
Jeff Dikee8012b52007-10-16 01:27:16 -0700199 return err;
200
Jeff Dike47906dd2008-05-12 14:01:50 -0700201 n = copy_to_user(buf, &fpregs, sizeof(fpregs));
Jeff Dikee8012b52007-10-16 01:27:16 -0700202 if(n > 0)
Jeff Dikeba180fd2007-10-16 01:27:00 -0700203 return -EFAULT;
Jeff Dikee8012b52007-10-16 01:27:16 -0700204
205 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Al Virof2833ae2011-09-14 16:21:37 -0700208static int set_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Jeff Dikee8012b52007-10-16 01:27:16 -0700210 int n, cpu = ((struct thread_info *) child->stack)->cpu;
Jeff Dike47906dd2008-05-12 14:01:50 -0700211 struct user_i387_struct fpregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Jeff Dike47906dd2008-05-12 14:01:50 -0700213 n = copy_from_user(&fpregs, buf, sizeof(fpregs));
Jeff Dikee8012b52007-10-16 01:27:16 -0700214 if (n > 0)
215 return -EFAULT;
216
Jeff Dike47906dd2008-05-12 14:01:50 -0700217 return restore_fp_registers(userspace_pid[cpu],
218 (unsigned long *) &fpregs);
Jeff Dikee8012b52007-10-16 01:27:16 -0700219}
220
Al Virof2833ae2011-09-14 16:21:37 -0700221static int get_fpxregs(struct user_fxsr_struct __user *buf, struct task_struct *child)
Jeff Dikee8012b52007-10-16 01:27:16 -0700222{
223 int err, n, cpu = ((struct thread_info *) child->stack)->cpu;
Jeff Dike47906dd2008-05-12 14:01:50 -0700224 struct user_fxsr_struct fpregs;
Jeff Dikee8012b52007-10-16 01:27:16 -0700225
Jeff Dike47906dd2008-05-12 14:01:50 -0700226 err = save_fpx_registers(userspace_pid[cpu], (unsigned long *) &fpregs);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700227 if (err)
Jeff Dikee8012b52007-10-16 01:27:16 -0700228 return err;
229
Jeff Dike47906dd2008-05-12 14:01:50 -0700230 n = copy_to_user(buf, &fpregs, sizeof(fpregs));
Jeff Dikee8012b52007-10-16 01:27:16 -0700231 if(n > 0)
Jeff Dikeba180fd2007-10-16 01:27:00 -0700232 return -EFAULT;
Jeff Dikee8012b52007-10-16 01:27:16 -0700233
234 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Al Virof2833ae2011-09-14 16:21:37 -0700237static int set_fpxregs(struct user_fxsr_struct __user *buf, struct task_struct *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Jeff Dikee8012b52007-10-16 01:27:16 -0700239 int n, cpu = ((struct thread_info *) child->stack)->cpu;
Jeff Dike47906dd2008-05-12 14:01:50 -0700240 struct user_fxsr_struct fpregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Jeff Dike47906dd2008-05-12 14:01:50 -0700242 n = copy_from_user(&fpregs, buf, sizeof(fpregs));
Jeff Dikee8012b52007-10-16 01:27:16 -0700243 if (n > 0)
244 return -EFAULT;
245
Jeff Dike47906dd2008-05-12 14:01:50 -0700246 return restore_fpx_registers(userspace_pid[cpu],
247 (unsigned long *) &fpregs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
Namhyung Kim9b05a692010-10-27 15:33:47 -0700250long subarch_ptrace(struct task_struct *child, long request,
251 unsigned long addr, unsigned long data)
Jeff Dikee8012b52007-10-16 01:27:16 -0700252{
Al Viro01599cd2011-09-14 16:21:34 -0700253 int ret = -EIO;
254 void __user *datap = (void __user *) data;
Al Viro01599cd2011-09-14 16:21:34 -0700255 switch (request) {
Al Virof2833ae2011-09-14 16:21:37 -0700256 case PTRACE_GETFPREGS: /* Get the child FPU state. */
257 ret = get_fpregs(datap, child);
258 break;
259 case PTRACE_SETFPREGS: /* Set the child FPU state. */
260 ret = set_fpregs(datap, child);
261 break;
Al Viro01599cd2011-09-14 16:21:34 -0700262 case PTRACE_GETFPXREGS: /* Get the child FPU state. */
263 ret = get_fpxregs(datap, child);
264 break;
265 case PTRACE_SETFPXREGS: /* Set the child FPU state. */
266 ret = set_fpxregs(datap, child);
267 break;
Al Virof2833ae2011-09-14 16:21:37 -0700268 default:
269 ret = -EIO;
Al Viro01599cd2011-09-14 16:21:34 -0700270 }
Al Viro01599cd2011-09-14 16:21:34 -0700271 return ret;
Jeff Dikee8012b52007-10-16 01:27:16 -0700272}