]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/truss/powerpc64-freebsd32.c
MFC r360091:
[FreeBSD/FreeBSD.git] / usr.bin / truss / powerpc64-freebsd32.c
1 /*
2  * Copyright 2006 Peter Grehan <grehan@freebsd.org>
3  * Copyright 2005 Orlando Bassotto <orlando@break.net>
4  * Copyright 1998 Sean Eric Fagan
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 /* FreeBSD/powerpc64-freebsd32-specific system call handling. */
32
33 #include <sys/ptrace.h>
34 #include <sys/syscall.h>
35
36 #include <machine/reg.h>
37 #include <machine/frame.h>
38
39 #include <stdbool.h>
40 #include <stdio.h>
41 #include <sysdecode.h>
42
43 #include "truss.h"
44
45 static int
46 powerpc64_freebsd32_fetch_args(struct trussinfo *trussinfo, u_int narg)
47 {
48         struct ptrace_io_desc iorequest;
49         struct reg regs;
50         struct current_syscall *cs;
51         lwpid_t tid;
52         u_int i, reg;
53
54         tid = trussinfo->curthread->tid;
55         cs = &trussinfo->curthread->cs;
56         if (ptrace(PT_GETREGS, tid, (caddr_t)&regs, 0) < 0) {
57                 fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
58                 return (-1);
59         }
60
61         /*
62          * FreeBSD has two special kinds of system call redirections --
63          * SYS_syscall, and SYS___syscall.  The former is the old syscall()
64          * routine, basically; the latter is for quad-aligned arguments.
65          *
66          * The system call argument count and code from ptrace() already
67          * account for these, but we need to skip over the first argument.
68          */
69         reg = 0;
70         switch (regs.fixreg[0]) {
71         case SYS_syscall:
72                 reg += 1;
73                 break;
74         case SYS___syscall:
75                 reg += 2;
76                 break;
77         }
78
79         for (i = 0; i < narg && reg < NARGREG; i++, reg++) {
80                 cs->args[i] = regs.fixreg[FIRSTARG + reg] & 0xffffffff;
81         }
82         if (narg > i) {
83                 uint32_t args32[narg - i];
84                 u_int j;
85
86                 iorequest.piod_op = PIOD_READ_D;
87                 iorequest.piod_offs = (void *)(regs.fixreg[1] + 8);
88                 iorequest.piod_addr = args32;
89                 iorequest.piod_len = sizeof(args32);
90                 ptrace(PT_IO, tid, (caddr_t)&iorequest, 0);
91                 if (iorequest.piod_len == 0)
92                         return (-1);
93                 for (j = 0; j < narg - i; j++)
94                         cs->args[i + j] = args32[j];
95         }
96
97         return (0);
98 }
99
100 static int
101 powerpc64_freebsd32_fetch_retval(struct trussinfo *trussinfo, long *retval, int *errorp)
102 {
103         struct reg regs;
104         lwpid_t tid;
105
106         tid = trussinfo->curthread->tid;
107         if (ptrace(PT_GETREGS, tid, (caddr_t)&regs, 0) < 0) {
108                 fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
109                 return (-1);
110         }
111
112         /* XXX: Does not have fixup for __syscall(). */
113         retval[0] = regs.fixreg[3] & 0xffffffff;
114         retval[1] = regs.fixreg[4] & 0xffffffff;
115         *errorp = !!(regs.cr & 0x10000000);
116         return (0);
117 }
118
119 static struct procabi powerpc64_freebsd32 = {
120         "FreeBSD ELF32",
121         SYSDECODE_ABI_FREEBSD32,
122         powerpc64_freebsd32_fetch_args,
123         powerpc64_freebsd32_fetch_retval,
124         STAILQ_HEAD_INITIALIZER(powerpc64_freebsd32.extra_syscalls),
125         { NULL }
126 };
127
128 PROCABI(powerpc64_freebsd32);