]> CyberLeo.Net >> Repos - FreeBSD/releng/10.3.git/blob - usr.bin/truss/powerpc-fbsd.c
- Copy stable/10@296371 to releng/10.3 in preparation for 10.3-RC1
[FreeBSD/releng/10.3.git] / usr.bin / truss / powerpc-fbsd.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/powerpc-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 <stdio.h>
40
41 #include "truss.h"
42
43 #ifdef __powerpc64__    /* 32-bit compatibility */
44 #include "freebsd32_syscalls.h"
45 #define syscallnames    freebsd32_syscallnames
46 #else                   /* native 32-bit */
47 #include "syscalls.h"
48 #endif
49
50 static int
51 powerpc_fetch_args(struct trussinfo *trussinfo, u_int narg)
52 {
53         struct ptrace_io_desc iorequest;
54         struct reg regs;
55         struct current_syscall *cs;
56         lwpid_t tid;
57         u_int i, reg;
58
59         tid = trussinfo->curthread->tid;
60         cs = &trussinfo->curthread->cs;
61         if (ptrace(PT_GETREGS, tid, (caddr_t)&regs, 0) < 0) {
62                 fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
63                 return (-1);
64         }
65
66         /*
67          * FreeBSD has two special kinds of system call redirections --
68          * SYS_syscall, and SYS___syscall.  The former is the old syscall()
69          * routine, basically; the latter is for quad-aligned arguments.
70          *
71          * The system call argument count and code from ptrace() already
72          * account for these, but we need to skip over the first argument.
73          */
74         reg = 0;
75         switch (regs.fixreg[0]) {
76         case SYS_syscall:
77                 reg += 1;
78                 break;
79         case SYS___syscall:
80                 reg += 2;
81                 break;
82         }
83
84         for (i = 0; i < narg && reg < NARGREG; i++, reg++) {
85 #ifdef __powerpc64__
86                 cs->args[i] = regs.fixreg[FIRSTARG + reg] & 0xffffffff;
87 #else
88                 cs->args[i] = regs.fixreg[FIRSTARG + reg];
89 #endif
90         }
91         if (narg > i) {
92 #ifdef __powerpc64__
93                 uint32_t args32[narg - i];
94                 u_int j;
95
96 #endif
97                 iorequest.piod_op = PIOD_READ_D;
98                 iorequest.piod_offs = (void *)(regs.fixreg[1] + 8);
99 #ifdef __powerpc64__
100                 iorequest.piod_addr = args32;
101                 iorequest.piod_len = sizeof(args32);
102 #else
103                 iorequest.piod_addr = &cs->args[i];
104                 iorequest.piod_len = (narg - i) * sizeof(cs->args[0]);
105 #endif
106                 ptrace(PT_IO, tid, (caddr_t)&iorequest, 0);
107                 if (iorequest.piod_len == 0)
108                         return (-1);
109 #ifdef __powerpc64__
110                 for (j = 0; j < narg - i; j++)
111                         cs->args[i + j] = args32[j];
112 #endif
113         }
114
115         return (0);
116 }
117
118 static int
119 powerpc_fetch_retval(struct trussinfo *trussinfo, long *retval, int *errorp)
120 {
121         struct reg regs;
122         lwpid_t tid;
123
124         tid = trussinfo->curthread->tid;
125         if (ptrace(PT_GETREGS, tid, (caddr_t)&regs, 0) < 0) {
126                 fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
127                 return (-1);
128         }
129
130         /* XXX: Does not have fixup for __syscall(). */
131 #ifdef __powerpc64__
132         retval[0] = regs.fixreg[3] & 0xffffffff;
133         retval[1] = regs.fixreg[4] & 0xffffffff;
134 #else
135         retval[0] = regs.fixreg[3];
136         retval[1] = regs.fixreg[4];
137 #endif
138         *errorp = !!(regs.cr & 0x10000000);
139         return (0);
140 }
141
142 static struct procabi powerpc_fbsd = {
143         "FreeBSD ELF32",
144         syscallnames,
145         nitems(syscallnames),
146         powerpc_fetch_args,
147         powerpc_fetch_retval
148 };
149
150 PROCABI(powerpc_fbsd);