]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/truss/riscv-freebsd.c
Add two missing eventhandler.h headers
[FreeBSD/FreeBSD.git] / usr.bin / truss / riscv-freebsd.c
1 /*-
2  * Copyright 2017 Li-Wen Hsu <lwhsu@FreeBSD.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 /* FreeBSD/riscv-specific system call handling. */
30
31 #include <sys/ptrace.h>
32 #include <sys/syscall.h>
33
34 #include <machine/frame.h>
35 #include <machine/reg.h>
36
37 #include <stdbool.h>
38 #include <stdio.h>
39 #include <sysdecode.h>
40
41 #include "truss.h"
42
43 static int
44 riscv_fetch_args(struct trussinfo *trussinfo, u_int narg)
45 {
46         struct reg regs;
47         struct current_syscall *cs;
48         lwpid_t tid;
49         u_int i, reg, syscall_num;
50
51         tid = trussinfo->curthread->tid;
52         cs = &trussinfo->curthread->cs;
53         if (ptrace(PT_GETREGS, tid, (caddr_t)&regs, 0) < 0) {
54                 fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
55                 return (-1);
56         }
57
58         /*
59          * FreeBSD has two special kinds of system call redirections --
60          * SYS_syscall, and SYS___syscall.  The former is the old syscall()
61          * routine, basically; the latter is for quad-aligned arguments.
62          *
63          * The system call argument count and code from ptrace() already
64          * account for these, but we need to skip over the first argument.
65          */
66         syscall_num = regs.t[0];
67         if (syscall_num == SYS_syscall || syscall_num == SYS___syscall) {
68                 reg = 1;
69                 syscall_num = regs.a[0];
70         } else {
71                 reg = 0;
72         }
73
74         for (i = 0; i < narg && reg < NARGREG; i++, reg++)
75                 cs->args[i] = regs.a[reg];
76         return (0);
77 }
78
79 static int
80 riscv_fetch_retval(struct trussinfo *trussinfo, long *retval, int *errorp)
81 {
82         struct reg regs;
83         lwpid_t tid;
84
85         tid = trussinfo->curthread->tid;
86         if (ptrace(PT_GETREGS, tid, (caddr_t)&regs, 0) < 0) {
87                 fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
88                 return (-1);
89         }
90
91         retval[0] = regs.a[0];
92         retval[1] = regs.a[1];
93         *errorp = !!(regs.t[0]);
94         return (0);
95 }
96
97 static struct procabi riscv_freebsd = {
98         "FreeBSD ELF64",
99         SYSDECODE_ABI_FREEBSD,
100         riscv_fetch_args,
101         riscv_fetch_retval,
102         STAILQ_HEAD_INITIALIZER(riscv_freebsd.extra_syscalls),
103         { NULL }
104 };
105
106 PROCABI(riscv_freebsd);