]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/linux/linux_ptrace.c
Upgrade our copies of clang, llvm, lld, lldb, compiler-rt and libc++ to
[FreeBSD/FreeBSD.git] / sys / i386 / linux / linux_ptrace.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2001 Alexander Kabaev
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer
12  *    in this position and unchanged.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include "opt_cpu.h"
35
36 #include <sys/param.h>
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 #include <sys/proc.h>
40 #include <sys/ptrace.h>
41 #include <sys/syscallsubr.h>
42 #include <sys/systm.h>
43
44 #include <machine/md_var.h>
45 #include <machine/pcb.h>
46 #include <machine/reg.h>
47
48 #include <i386/linux/linux.h>
49 #include <i386/linux/linux_proto.h>
50 #include <compat/linux/linux_signal.h>
51
52 /*
53  *   Linux ptrace requests numbers. Mostly identical to FreeBSD,
54  *   except for MD ones and PT_ATTACH/PT_DETACH.
55  */
56 #define PTRACE_TRACEME          0
57 #define PTRACE_PEEKTEXT         1
58 #define PTRACE_PEEKDATA         2
59 #define PTRACE_PEEKUSR          3
60 #define PTRACE_POKETEXT         4
61 #define PTRACE_POKEDATA         5
62 #define PTRACE_POKEUSR          6
63 #define PTRACE_CONT             7
64 #define PTRACE_KILL             8
65 #define PTRACE_SINGLESTEP       9
66
67 #define PTRACE_ATTACH           16
68 #define PTRACE_DETACH           17
69
70 #define LINUX_PTRACE_SYSCALL    24
71
72 #define PTRACE_GETREGS          12
73 #define PTRACE_SETREGS          13
74 #define PTRACE_GETFPREGS        14
75 #define PTRACE_SETFPREGS        15
76 #define PTRACE_GETFPXREGS       18
77 #define PTRACE_SETFPXREGS       19
78
79 #define PTRACE_SETOPTIONS       21
80
81 /*
82  * Linux keeps debug registers at the following
83  * offset in the user struct
84  */
85 #define LINUX_DBREG_OFFSET      252
86 #define LINUX_DBREG_SIZE        (8*sizeof(l_int))
87
88 static __inline int
89 map_signum(int signum)
90 {
91
92         signum = linux_to_bsd_signal(signum);
93         return ((signum == SIGSTOP)? 0 : signum);
94 }
95
96 struct linux_pt_reg {
97         l_long  ebx;
98         l_long  ecx;
99         l_long  edx;
100         l_long  esi;
101         l_long  edi;
102         l_long  ebp;
103         l_long  eax;
104         l_int   xds;
105         l_int   xes;
106         l_int   xfs;
107         l_int   xgs;
108         l_long  orig_eax;
109         l_long  eip;
110         l_int   xcs;
111         l_long  eflags;
112         l_long  esp;
113         l_int   xss;
114 };
115
116 /*
117  *   Translate i386 ptrace registers between Linux and FreeBSD formats.
118  *   The translation is pretty straighforward, for all registers, but
119  *   orig_eax on Linux side and r_trapno and r_err in FreeBSD
120  */
121 static void
122 map_regs_to_linux(struct reg *bsd_r, struct linux_pt_reg *linux_r)
123 {
124         linux_r->ebx = bsd_r->r_ebx;
125         linux_r->ecx = bsd_r->r_ecx;
126         linux_r->edx = bsd_r->r_edx;
127         linux_r->esi = bsd_r->r_esi;
128         linux_r->edi = bsd_r->r_edi;
129         linux_r->ebp = bsd_r->r_ebp;
130         linux_r->eax = bsd_r->r_eax;
131         linux_r->xds = bsd_r->r_ds;
132         linux_r->xes = bsd_r->r_es;
133         linux_r->xfs = bsd_r->r_fs;
134         linux_r->xgs = bsd_r->r_gs;
135         linux_r->orig_eax = bsd_r->r_eax;
136         linux_r->eip = bsd_r->r_eip;
137         linux_r->xcs = bsd_r->r_cs;
138         linux_r->eflags = bsd_r->r_eflags;
139         linux_r->esp = bsd_r->r_esp;
140         linux_r->xss = bsd_r->r_ss;
141 }
142
143 static void
144 map_regs_from_linux(struct reg *bsd_r, struct linux_pt_reg *linux_r)
145 {
146         bsd_r->r_ebx = linux_r->ebx;
147         bsd_r->r_ecx = linux_r->ecx;
148         bsd_r->r_edx = linux_r->edx;
149         bsd_r->r_esi = linux_r->esi;
150         bsd_r->r_edi = linux_r->edi;
151         bsd_r->r_ebp = linux_r->ebp;
152         bsd_r->r_eax = linux_r->eax;
153         bsd_r->r_ds  = linux_r->xds;
154         bsd_r->r_es  = linux_r->xes;
155         bsd_r->r_fs  = linux_r->xfs;
156         bsd_r->r_gs  = linux_r->xgs;
157         bsd_r->r_eip = linux_r->eip;
158         bsd_r->r_cs  = linux_r->xcs;
159         bsd_r->r_eflags = linux_r->eflags;
160         bsd_r->r_esp = linux_r->esp;
161         bsd_r->r_ss = linux_r->xss;
162 }
163
164 struct linux_pt_fpreg {
165         l_long cwd;
166         l_long swd;
167         l_long twd;
168         l_long fip;
169         l_long fcs;
170         l_long foo;
171         l_long fos;
172         l_long st_space[2*10];
173 };
174
175 static void
176 map_fpregs_to_linux(struct fpreg *bsd_r, struct linux_pt_fpreg *linux_r)
177 {
178         linux_r->cwd = bsd_r->fpr_env[0];
179         linux_r->swd = bsd_r->fpr_env[1];
180         linux_r->twd = bsd_r->fpr_env[2];
181         linux_r->fip = bsd_r->fpr_env[3];
182         linux_r->fcs = bsd_r->fpr_env[4];
183         linux_r->foo = bsd_r->fpr_env[5];
184         linux_r->fos = bsd_r->fpr_env[6];
185         bcopy(bsd_r->fpr_acc, linux_r->st_space, sizeof(linux_r->st_space));
186 }
187
188 static void
189 map_fpregs_from_linux(struct fpreg *bsd_r, struct linux_pt_fpreg *linux_r)
190 {
191         bsd_r->fpr_env[0] = linux_r->cwd;
192         bsd_r->fpr_env[1] = linux_r->swd;
193         bsd_r->fpr_env[2] = linux_r->twd;
194         bsd_r->fpr_env[3] = linux_r->fip;
195         bsd_r->fpr_env[4] = linux_r->fcs;
196         bsd_r->fpr_env[5] = linux_r->foo;
197         bsd_r->fpr_env[6] = linux_r->fos;
198         bcopy(bsd_r->fpr_acc, linux_r->st_space, sizeof(bsd_r->fpr_acc));
199 }
200
201 struct linux_pt_fpxreg {
202         l_ushort        cwd;
203         l_ushort        swd;
204         l_ushort        twd;
205         l_ushort        fop;
206         l_long          fip;
207         l_long          fcs;
208         l_long          foo;
209         l_long          fos;
210         l_long          mxcsr;
211         l_long          reserved;
212         l_long          st_space[32];
213         l_long          xmm_space[32];
214         l_long          padding[56];
215 };
216
217 static int
218 linux_proc_read_fpxregs(struct thread *td, struct linux_pt_fpxreg *fpxregs)
219 {
220
221         PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
222         if (cpu_fxsr == 0 || (td->td_proc->p_flag & P_INMEM) == 0)
223                 return (EIO);
224         bcopy(&get_pcb_user_save_td(td)->sv_xmm, fpxregs, sizeof(*fpxregs));
225         return (0);
226 }
227
228 static int
229 linux_proc_write_fpxregs(struct thread *td, struct linux_pt_fpxreg *fpxregs)
230 {
231
232         PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
233         if (cpu_fxsr == 0 || (td->td_proc->p_flag & P_INMEM) == 0)
234                 return (EIO);
235         bcopy(fpxregs, &get_pcb_user_save_td(td)->sv_xmm, sizeof(*fpxregs));
236         return (0);
237 }
238
239 int
240 linux_ptrace(struct thread *td, struct linux_ptrace_args *uap)
241 {
242         union {
243                 struct linux_pt_reg     reg;
244                 struct linux_pt_fpreg   fpreg;
245                 struct linux_pt_fpxreg  fpxreg;
246         } r;
247         union {
248                 struct reg              bsd_reg;
249                 struct fpreg            bsd_fpreg;
250                 struct dbreg            bsd_dbreg;
251         } u;
252         void *addr;
253         pid_t pid;
254         int error, req;
255
256         error = 0;
257
258         /* by default, just copy data intact */
259         req  = uap->req;
260         pid  = (pid_t)uap->pid;
261         addr = (void *)uap->addr;
262
263         switch (req) {
264         case PTRACE_TRACEME:
265         case PTRACE_POKETEXT:
266         case PTRACE_POKEDATA:
267         case PTRACE_KILL:
268                 error = kern_ptrace(td, req, pid, addr, uap->data);
269                 break;
270         case PTRACE_PEEKTEXT:
271         case PTRACE_PEEKDATA: {
272                 /* need to preserve return value */
273                 int rval = td->td_retval[0];
274                 error = kern_ptrace(td, req, pid, addr, 0);
275                 if (error == 0)
276                         error = copyout(td->td_retval, (void *)uap->data,
277                             sizeof(l_int));
278                 td->td_retval[0] = rval;
279                 break;
280         }
281         case PTRACE_DETACH:
282                 error = kern_ptrace(td, PT_DETACH, pid, (void *)1,
283                      map_signum(uap->data));
284                 break;
285         case PTRACE_SINGLESTEP:
286         case PTRACE_CONT:
287                 error = kern_ptrace(td, req, pid, (void *)1,
288                      map_signum(uap->data));
289                 break;
290         case PTRACE_ATTACH:
291                 error = kern_ptrace(td, PT_ATTACH, pid, addr, uap->data);
292                 break;
293         case PTRACE_GETREGS:
294                 /* Linux is using data where FreeBSD is using addr */
295                 error = kern_ptrace(td, PT_GETREGS, pid, &u.bsd_reg, 0);
296                 if (error == 0) {
297                         map_regs_to_linux(&u.bsd_reg, &r.reg);
298                         error = copyout(&r.reg, (void *)uap->data,
299                             sizeof(r.reg));
300                 }
301                 break;
302         case PTRACE_SETREGS:
303                 /* Linux is using data where FreeBSD is using addr */
304                 error = copyin((void *)uap->data, &r.reg, sizeof(r.reg));
305                 if (error == 0) {
306                         map_regs_from_linux(&u.bsd_reg, &r.reg);
307                         error = kern_ptrace(td, PT_SETREGS, pid, &u.bsd_reg, 0);
308                 }
309                 break;
310         case PTRACE_GETFPREGS:
311                 /* Linux is using data where FreeBSD is using addr */
312                 error = kern_ptrace(td, PT_GETFPREGS, pid, &u.bsd_fpreg, 0);
313                 if (error == 0) {
314                         map_fpregs_to_linux(&u.bsd_fpreg, &r.fpreg);
315                         error = copyout(&r.fpreg, (void *)uap->data,
316                             sizeof(r.fpreg));
317                 }
318                 break;
319         case PTRACE_SETFPREGS:
320                 /* Linux is using data where FreeBSD is using addr */
321                 error = copyin((void *)uap->data, &r.fpreg, sizeof(r.fpreg));
322                 if (error == 0) {
323                         map_fpregs_from_linux(&u.bsd_fpreg, &r.fpreg);
324                         error = kern_ptrace(td, PT_SETFPREGS, pid,
325                             &u.bsd_fpreg, 0);
326                 }
327                 break;
328         case PTRACE_SETFPXREGS:
329                 error = copyin((void *)uap->data, &r.fpxreg, sizeof(r.fpxreg));
330                 if (error)
331                         break;
332                 /* FALL THROUGH */
333         case PTRACE_GETFPXREGS: {
334                 struct proc *p;
335                 struct thread *td2;
336
337                 if (sizeof(struct linux_pt_fpxreg) != sizeof(struct savexmm)) {
338                         static int once = 0;
339                         if (!once) {
340                                 printf("linux: savexmm != linux_pt_fpxreg\n");
341                                 once = 1;
342                         }
343                         error = EIO;
344                         break;
345                 }
346
347                 if ((p = pfind(uap->pid)) == NULL) {
348                         error = ESRCH;
349                         break;
350                 }
351
352                 /* Exiting processes can't be debugged. */
353                 if ((p->p_flag & P_WEXIT) != 0) {
354                         error = ESRCH;
355                         goto fail;
356                 }
357
358                 if ((error = p_candebug(td, p)) != 0)
359                         goto fail;
360
361                 /* System processes can't be debugged. */
362                 if ((p->p_flag & P_SYSTEM) != 0) {
363                         error = EINVAL;
364                         goto fail;
365                 }
366
367                 /* not being traced... */
368                 if ((p->p_flag & P_TRACED) == 0) {
369                         error = EPERM;
370                         goto fail;
371                 }
372
373                 /* not being traced by YOU */
374                 if (p->p_pptr != td->td_proc) {
375                         error = EBUSY;
376                         goto fail;
377                 }
378
379                 /* not currently stopped */
380                 if (!P_SHOULDSTOP(p) || (p->p_flag & P_WAITED) == 0) {
381                         error = EBUSY;
382                         goto fail;
383                 }
384
385                 if (req == PTRACE_GETFPXREGS) {
386                         _PHOLD(p);      /* may block */
387                         td2 = FIRST_THREAD_IN_PROC(p);
388                         error = linux_proc_read_fpxregs(td2, &r.fpxreg);
389                         _PRELE(p);
390                         PROC_UNLOCK(p);
391                         if (error == 0)
392                                 error = copyout(&r.fpxreg, (void *)uap->data,
393                                     sizeof(r.fpxreg));
394                 } else {
395                         /* clear dangerous bits exactly as Linux does*/
396                         r.fpxreg.mxcsr &= 0xffbf;
397                         _PHOLD(p);      /* may block */
398                         td2 = FIRST_THREAD_IN_PROC(p);
399                         error = linux_proc_write_fpxregs(td2, &r.fpxreg);
400                         _PRELE(p);
401                         PROC_UNLOCK(p);
402                 }
403                 break;
404
405         fail:
406                 PROC_UNLOCK(p);
407                 break;
408         }
409         case PTRACE_PEEKUSR:
410         case PTRACE_POKEUSR: {
411                 error = EIO;
412
413                 /* check addr for alignment */
414                 if (uap->addr < 0 || uap->addr & (sizeof(l_int) - 1))
415                         break;
416                 /*
417                  * Allow linux programs to access register values in
418                  * user struct. We simulate this through PT_GET/SETREGS
419                  * as necessary.
420                  */
421                 if (uap->addr < sizeof(struct linux_pt_reg)) {
422                         error = kern_ptrace(td, PT_GETREGS, pid, &u.bsd_reg, 0);
423                         if (error != 0)
424                                 break;
425
426                         map_regs_to_linux(&u.bsd_reg, &r.reg);
427                         if (req == PTRACE_PEEKUSR) {
428                                 error = copyout((char *)&r.reg + uap->addr,
429                                     (void *)uap->data, sizeof(l_int));
430                                 break;
431                         }
432
433                         *(l_int *)((char *)&r.reg + uap->addr) =
434                             (l_int)uap->data;
435
436                         map_regs_from_linux(&u.bsd_reg, &r.reg);
437                         error = kern_ptrace(td, PT_SETREGS, pid, &u.bsd_reg, 0);
438                 }
439
440                 /*
441                  * Simulate debug registers access
442                  */
443                 if (uap->addr >= LINUX_DBREG_OFFSET &&
444                     uap->addr <= LINUX_DBREG_OFFSET + LINUX_DBREG_SIZE) {
445                         error = kern_ptrace(td, PT_GETDBREGS, pid, &u.bsd_dbreg,
446                             0);
447                         if (error != 0)
448                                 break;
449
450                         uap->addr -= LINUX_DBREG_OFFSET;
451                         if (req == PTRACE_PEEKUSR) {
452                                 error = copyout((char *)&u.bsd_dbreg +
453                                     uap->addr, (void *)uap->data,
454                                     sizeof(l_int));
455                                 break;
456                         }
457
458                         *(l_int *)((char *)&u.bsd_dbreg + uap->addr) =
459                              uap->data;
460                         error = kern_ptrace(td, PT_SETDBREGS, pid,
461                             &u.bsd_dbreg, 0);
462                 }
463
464                 break;
465         }
466         case LINUX_PTRACE_SYSCALL:
467                 /* fall through */
468         default:
469                 printf("linux: ptrace(%u, ...) not implemented\n",
470                     (unsigned int)uap->req);
471                 error = EINVAL;
472                 break;
473         }
474
475         return (error);
476 }