]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/truss/aarch64-freebsd.c
Checkpoint initial integration work
[FreeBSD/FreeBSD.git] / usr.bin / truss / aarch64-freebsd.c
1 /*
2  * Copyright (c) 2015 The FreeBSD Foundation
3  *
4  * Portions of this software were developed by Konstantin Belousov
5  * under sponsorship from the FreeBSD Foundation.
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  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 /* FreeBSD/arm64-specific system call handling. */
33
34 #include <sys/ptrace.h>
35 #include <sys/syscall.h>
36
37 #include <machine/reg.h>
38 #include <machine/armreg.h>
39 #include <machine/ucontext.h>
40
41 #include <stdio.h>
42 #include <sysdecode.h>
43
44 #include "truss.h"
45
46 static int
47 aarch64_fetch_args(struct trussinfo *trussinfo, u_int narg)
48 {
49         struct reg regs;
50         struct current_syscall *cs;
51         lwpid_t tid;
52         u_int i, reg, syscall_num;
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         syscall_num = regs.x[8];
70         if (syscall_num == SYS_syscall || syscall_num == SYS___syscall) {
71                 reg = 1;
72                 syscall_num = regs.x[0];
73         } else {
74                 reg = 0;
75         }
76
77         for (i = 0; i < narg && reg < 8; i++, reg++)
78                 cs->args[i] = regs.x[reg];
79         return (0);
80 }
81
82 static int
83 aarch64_fetch_retval(struct trussinfo *trussinfo, long *retval, int *errorp)
84 {
85         struct reg regs;
86         lwpid_t tid;
87
88         tid = trussinfo->curthread->tid;
89         if (ptrace(PT_GETREGS, tid, (caddr_t)&regs, 0) < 0) {
90                 fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
91                 return (-1);
92         }
93
94         retval[0] = regs.x[0];
95         retval[1] = regs.x[1];
96         *errorp = !!(regs.spsr & PSR_C);
97         return (0);
98 }
99
100 static struct procabi aarch64_freebsd = {
101         "FreeBSD ELF64",
102         SYSDECODE_ABI_FREEBSD,
103         aarch64_fetch_args,
104         aarch64_fetch_retval
105 };
106
107 PROCABI(aarch64_freebsd);