blob: 927fcd955651367aff76562abf6f3a47d1d52886 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
6#include <linux/config.h>
7#include <linux/compiler.h>
8#include "linux/sched.h"
Bodo Stroesser81efcd32006-03-27 01:14:34 -08009#include "linux/mm.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "asm/elf.h"
11#include "asm/ptrace.h"
12#include "asm/uaccess.h"
13#include "asm/unistd.h"
14#include "sysdep/ptrace.h"
15#include "sysdep/sigcontext.h"
16#include "sysdep/sc.h"
17
Paolo 'Blaisorblade' Giarrusso972410b2006-03-31 02:30:21 -080018void arch_switch_to_tt(struct task_struct *from, struct task_struct *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -070019{
Paolo 'Blaisorblade' Giarrusso972410b2006-03-31 02:30:21 -080020 update_debugregs(to->thread.arch.debugregs_seq);
21}
22
23void arch_switch_to_skas(struct task_struct *from, struct task_struct *to)
24{
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));
33 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);
40 if(n != sizeof(instr)) {
41 printk("is_syscall : failed to read instruction from "
42 "0x%lx\n", addr);
43 return(1);
44 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 }
46 /* int 0x80 or sysenter */
47 return((instr == 0x80cd) || (instr == 0x340f));
48}
49
50/* determines which flags the user has access to. */
51/* 1 = access 0 = no access */
52#define FLAG_MASK 0x00044dd5
53
54int putreg(struct task_struct *child, int regno, unsigned long value)
55{
56 regno >>= 2;
57 switch (regno) {
58 case FS:
59 if (value && (value & 3) != 3)
60 return -EIO;
61 PT_REGS_FS(&child->thread.regs) = value;
62 return 0;
63 case GS:
64 if (value && (value & 3) != 3)
65 return -EIO;
66 PT_REGS_GS(&child->thread.regs) = value;
67 return 0;
68 case DS:
69 case ES:
70 if (value && (value & 3) != 3)
71 return -EIO;
72 value &= 0xffff;
73 break;
74 case SS:
75 case CS:
76 if ((value & 3) != 3)
77 return -EIO;
78 value &= 0xffff;
79 break;
80 case EFL:
81 value &= FLAG_MASK;
82 value |= PT_REGS_EFLAGS(&child->thread.regs);
83 break;
84 }
85 PT_REGS_SET(&child->thread.regs, regno, value);
86 return 0;
87}
88
Bodo Stroesser82c1c112005-05-06 21:30:46 -070089int poke_user(struct task_struct *child, long addr, long data)
90{
91 if ((addr & 3) || addr < 0)
92 return -EIO;
93
94 if (addr < MAX_REG_OFFSET)
95 return putreg(child, addr, data);
96
97 else if((addr >= offsetof(struct user, u_debugreg[0])) &&
98 (addr <= offsetof(struct user, u_debugreg[7]))){
99 addr -= offsetof(struct user, u_debugreg[0]);
100 addr = addr >> 2;
101 if((addr == 4) || (addr == 5)) return -EIO;
102 child->thread.arch.debugregs[addr] = data;
103 return 0;
104 }
105 return -EIO;
106}
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108unsigned long getreg(struct task_struct *child, int regno)
109{
110 unsigned long retval = ~0UL;
111
112 regno >>= 2;
113 switch (regno) {
114 case FS:
115 case GS:
116 case DS:
117 case ES:
118 case SS:
119 case CS:
120 retval = 0xffff;
121 /* fall through */
122 default:
123 retval &= PT_REG(&child->thread.regs, regno);
124 }
125 return retval;
126}
127
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700128int peek_user(struct task_struct *child, long addr, long data)
129{
130/* read the word at location addr in the USER area. */
Al Viro4d338e12006-03-31 02:30:15 -0800131 unsigned long tmp;
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700132
Al Viro4d338e12006-03-31 02:30:15 -0800133 if ((addr & 3) || addr < 0)
134 return -EIO;
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700135
Al Viro4d338e12006-03-31 02:30:15 -0800136 tmp = 0; /* Default return condition */
137 if(addr < MAX_REG_OFFSET){
138 tmp = getreg(child, addr);
139 }
140 else if((addr >= offsetof(struct user, u_debugreg[0])) &&
141 (addr <= offsetof(struct user, u_debugreg[7]))){
142 addr -= offsetof(struct user, u_debugreg[0]);
143 addr = addr >> 2;
144 tmp = child->thread.arch.debugregs[addr];
145 }
146 return put_user(tmp, (unsigned long __user *) data);
Bodo Stroesser82c1c112005-05-06 21:30:46 -0700147}
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149struct i387_fxsave_struct {
150 unsigned short cwd;
151 unsigned short swd;
152 unsigned short twd;
153 unsigned short fop;
154 long fip;
155 long fcs;
156 long foo;
157 long fos;
158 long mxcsr;
159 long reserved;
160 long st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
161 long xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
162 long padding[56];
163};
164
165/*
166 * FPU tag word conversions.
167 */
168
169static inline unsigned short twd_i387_to_fxsr( unsigned short twd )
170{
171 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
172
173 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
174 tmp = ~twd;
175 tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
176 /* and move the valid bits to the lower byte. */
177 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
178 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
179 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
180 return tmp;
181}
182
183static inline unsigned long twd_fxsr_to_i387( struct i387_fxsave_struct *fxsave )
184{
185 struct _fpxreg *st = NULL;
186 unsigned long twd = (unsigned long) fxsave->twd;
187 unsigned long tag;
188 unsigned long ret = 0xffff0000;
189 int i;
190
191#define FPREG_ADDR(f, n) ((char *)&(f)->st_space + (n) * 16);
192
193 for ( i = 0 ; i < 8 ; i++ ) {
194 if ( twd & 0x1 ) {
195 st = (struct _fpxreg *) FPREG_ADDR( fxsave, i );
196
197 switch ( st->exponent & 0x7fff ) {
198 case 0x7fff:
199 tag = 2; /* Special */
200 break;
201 case 0x0000:
202 if ( !st->significand[0] &&
203 !st->significand[1] &&
204 !st->significand[2] &&
205 !st->significand[3] ) {
206 tag = 1; /* Zero */
207 } else {
208 tag = 2; /* Special */
209 }
210 break;
211 default:
212 if ( st->significand[3] & 0x8000 ) {
213 tag = 0; /* Valid */
214 } else {
215 tag = 2; /* Special */
216 }
217 break;
218 }
219 } else {
220 tag = 3; /* Empty */
221 }
222 ret |= (tag << (2 * i));
223 twd = twd >> 1;
224 }
225 return ret;
226}
227
228/*
229 * FXSR floating point environment conversions.
230 */
231
232#ifdef CONFIG_MODE_TT
233static inline int convert_fxsr_to_user_tt(struct _fpstate __user *buf,
234 struct pt_regs *regs)
235{
236 struct i387_fxsave_struct *fxsave = SC_FXSR_ENV(PT_REGS_SC(regs));
237 unsigned long env[7];
238 struct _fpreg __user *to;
239 struct _fpxreg *from;
240 int i;
241
242 env[0] = (unsigned long)fxsave->cwd | 0xffff0000;
243 env[1] = (unsigned long)fxsave->swd | 0xffff0000;
244 env[2] = twd_fxsr_to_i387(fxsave);
245 env[3] = fxsave->fip;
246 env[4] = fxsave->fcs | ((unsigned long)fxsave->fop << 16);
247 env[5] = fxsave->foo;
248 env[6] = fxsave->fos;
249
250 if ( __copy_to_user( buf, env, 7 * sizeof(unsigned long) ) )
251 return 1;
252
253 to = &buf->_st[0];
254 from = (struct _fpxreg *) &fxsave->st_space[0];
255 for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
256 if ( __copy_to_user( to, from, sizeof(*to) ) )
257 return 1;
258 }
259 return 0;
260}
261#endif
262
263static inline int convert_fxsr_to_user(struct _fpstate __user *buf,
264 struct pt_regs *regs)
265{
266 return(CHOOSE_MODE(convert_fxsr_to_user_tt(buf, regs), 0));
267}
268
269#ifdef CONFIG_MODE_TT
270static inline int convert_fxsr_from_user_tt(struct pt_regs *regs,
271 struct _fpstate __user *buf)
272{
273 struct i387_fxsave_struct *fxsave = SC_FXSR_ENV(PT_REGS_SC(regs));
274 unsigned long env[7];
275 struct _fpxreg *to;
276 struct _fpreg __user *from;
277 int i;
278
279 if ( __copy_from_user( env, buf, 7 * sizeof(long) ) )
280 return 1;
281
282 fxsave->cwd = (unsigned short)(env[0] & 0xffff);
283 fxsave->swd = (unsigned short)(env[1] & 0xffff);
284 fxsave->twd = twd_i387_to_fxsr((unsigned short)(env[2] & 0xffff));
285 fxsave->fip = env[3];
286 fxsave->fop = (unsigned short)((env[4] & 0xffff0000) >> 16);
287 fxsave->fcs = (env[4] & 0xffff);
288 fxsave->foo = env[5];
289 fxsave->fos = env[6];
290
291 to = (struct _fpxreg *) &fxsave->st_space[0];
292 from = &buf->_st[0];
293 for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
294 if ( __copy_from_user( to, from, sizeof(*from) ) )
295 return 1;
296 }
297 return 0;
298}
299#endif
300
301static inline int convert_fxsr_from_user(struct pt_regs *regs,
302 struct _fpstate __user *buf)
303{
304 return(CHOOSE_MODE(convert_fxsr_from_user_tt(regs, buf), 0));
305}
306
307int get_fpregs(unsigned long buf, struct task_struct *child)
308{
309 int err;
310
311 err = convert_fxsr_to_user((struct _fpstate __user *) buf,
312 &child->thread.regs);
313 if(err) return(-EFAULT);
314 else return(0);
315}
316
317int set_fpregs(unsigned long buf, struct task_struct *child)
318{
319 int err;
320
321 err = convert_fxsr_from_user(&child->thread.regs,
322 (struct _fpstate __user *) buf);
323 if(err) return(-EFAULT);
324 else return(0);
325}
326
327#ifdef CONFIG_MODE_TT
328int get_fpxregs_tt(unsigned long buf, struct task_struct *tsk)
329{
330 struct pt_regs *regs = &tsk->thread.regs;
331 struct i387_fxsave_struct *fxsave = SC_FXSR_ENV(PT_REGS_SC(regs));
332 int err;
333
334 err = __copy_to_user((void __user *) buf, fxsave,
335 sizeof(struct user_fxsr_struct));
336 if(err) return -EFAULT;
337 else return 0;
338}
339#endif
340
341int get_fpxregs(unsigned long buf, struct task_struct *tsk)
342{
343 return(CHOOSE_MODE(get_fpxregs_tt(buf, tsk), 0));
344}
345
346#ifdef CONFIG_MODE_TT
347int set_fpxregs_tt(unsigned long buf, struct task_struct *tsk)
348{
349 struct pt_regs *regs = &tsk->thread.regs;
350 struct i387_fxsave_struct *fxsave = SC_FXSR_ENV(PT_REGS_SC(regs));
351 int err;
352
353 err = __copy_from_user(fxsave, (void __user *) buf,
354 sizeof(struct user_fxsr_struct) );
355 if(err) return -EFAULT;
356 else return 0;
357}
358#endif
359
360int set_fpxregs(unsigned long buf, struct task_struct *tsk)
361{
362 return(CHOOSE_MODE(set_fpxregs_tt(buf, tsk), 0));
363}
364
365#ifdef notdef
366int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
367{
368 fpu->cwd = (((SC_FP_CW(PT_REGS_SC(regs)) & 0xffff) << 16) |
369 (SC_FP_SW(PT_REGS_SC(regs)) & 0xffff));
370 fpu->swd = SC_FP_CSSEL(PT_REGS_SC(regs)) & 0xffff;
371 fpu->twd = SC_FP_IPOFF(PT_REGS_SC(regs));
372 fpu->fip = SC_FP_CSSEL(PT_REGS_SC(regs)) & 0xffff;
373 fpu->fcs = SC_FP_DATAOFF(PT_REGS_SC(regs));
374 fpu->foo = SC_FP_DATASEL(PT_REGS_SC(regs));
375 fpu->fos = 0;
376 memcpy(fpu->st_space, (void *) SC_FP_ST(PT_REGS_SC(regs)),
377 sizeof(fpu->st_space));
378 return(1);
379}
380#endif
381
382#ifdef CONFIG_MODE_TT
383static inline void copy_fpu_fxsave_tt(struct pt_regs *regs,
384 struct user_i387_struct *buf)
385{
386 struct i387_fxsave_struct *fpu = SC_FXSR_ENV(PT_REGS_SC(regs));
387 unsigned short *to;
388 unsigned short *from;
389 int i;
390
391 memcpy( buf, fpu, 7 * sizeof(long) );
392
393 to = (unsigned short *) &buf->st_space[0];
394 from = (unsigned short *) &fpu->st_space[0];
395 for ( i = 0 ; i < 8 ; i++, to += 5, from += 8 ) {
396 memcpy( to, from, 5 * sizeof(unsigned short) );
397 }
398}
399#endif
400
401static inline void copy_fpu_fxsave(struct pt_regs *regs,
402 struct user_i387_struct *buf)
403{
404 (void) CHOOSE_MODE(copy_fpu_fxsave_tt(regs, buf), 0);
405}
406
407int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu )
408{
409 copy_fpu_fxsave(regs, (struct user_i387_struct *) fpu);
410 return(1);
411}
412
413/*
414 * Overrides for Emacs so that we follow Linus's tabbing style.
415 * Emacs will notice this stuff at the end of the file and automatically
416 * adjust the settings for this buffer only. This must remain at the end
417 * of the file.
418 * ---------------------------------------------------------------------------
419 * Local variables:
420 * c-file-style: "linux"
421 * End:
422 */