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