aboutsummaryrefslogtreecommitdiff
blob: 6050dc96cb08ce57c0dd64787b2f4af86fbfda5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* Since sparc's g0 register is hardcoded to 0 in the ISA, the kernel does not
 * bother copying it out when using the regs ptrace.  Instead it shifts things
 * by one and stores [g1..g7] in [0..6] and [o0..o7] in [7..14] (leaving the
 * last u_reg[15] unused).
 *
 * Oddly, the kernel defines are not adjusted to the values as they written in
 * the register structure -- UREG_G0 is 0, UREG_G1 is 1, etc...  So we have to
 * define our own to get correct behavior.  Also, the kernel uses UREG_I# when
 * it's easier for us to think of it in terms of UREG_O# (register windows!).
 *
 * This should work for both 32 and 64 bit Linux userland.
 */
#define U_REG_G1 0
#define U_REG_O0 7

#undef _trace_possible
#define _trace_possible _trace_possible

#ifdef SB_PERSONALITIES

static const struct syscall_entry syscall_table_32[] = {
#ifdef SB_PERSONALITIES_sparc
#define S(s) { SB_SYS_sparc_##s, SB_NR_##s, #s },
#include "trace_syscalls_sparc.h"
#undef S
#endif
	{ SB_NR_UNDEF, SB_NR_UNDEF, NULL },
};
static const struct syscall_entry syscall_table_64[] = {
#ifdef SB_PERSONALITIES_sparc64
#define S(s) { SB_SYS_sparc64_##s, SB_NR_##s, #s },
#include "trace_syscalls_sparc64.h"
#undef S
#endif
	{ SB_NR_UNDEF, SB_NR_UNDEF, NULL },
};

static bool pers_is_32(trace_regs *regs)
{
#ifdef __arch64__
	/* Sparc does not make it easy to detect 32-bit vs 64-bit.
	 * Inspect the syscall trap insn to see which one it is.
	 */
	unsigned long ret = do_ptrace(PTRACE_PEEKTEXT, (void *)regs->tpc, NULL);
	return (ret >> 32) == 0x91d02010;
#else
	return true;
#endif
}

static const struct syscall_entry *trace_check_personality(void *vregs)
{
	trace_regs *regs = vregs;
	if (pers_is_32(regs))
		return syscall_table_32;
	else
		return syscall_table_64;
}

static bool _trace_possible(const void *data)
{
#ifdef __arch64__
	/* sparc64 can trace sparc32. */
	return true;
#else
	/* sparc32 can only trace sparc32 :(. */
	const Elf64_Ehdr *ehdr = data;
	return ehdr->e_ident[EI_CLASS] == ELFCLASS32;
#endif
}

#else

static bool _trace_possible(const void *data)
{
	const Elf64_Ehdr *ehdr = data;
#ifdef __arch64__
	return ehdr->e_ident[EI_CLASS] == ELFCLASS64;
#else
	return ehdr->e_ident[EI_CLASS] == ELFCLASS32;
#endif
}

#endif

/* Sparc systems have swapped the addr/data args. */
#undef trace_get_regs
#undef trace_set_regs
#ifdef __arch64__
# define trace_get_regs(regs) do_ptrace(PTRACE_GETREGS64, regs, NULL)
# define trace_set_regs(regs) do_ptrace(PTRACE_SETREGS64, regs, NULL)
#else
# define trace_get_regs(regs) do_ptrace(PTRACE_GETREGS, regs, NULL)
# define trace_set_regs(regs) do_ptrace(PTRACE_SETREGS, regs, NULL)
#endif

#define trace_reg_sysnum u_regs[U_REG_G1]

static long trace_raw_ret(void *vregs)
{
	trace_regs *regs = vregs;
	return regs->u_regs[U_REG_O0];
}

static void trace_set_ret(void *vregs, int err)
{
	trace_regs *regs = vregs;
#ifndef __arch64__
	/* The carry bit is used to flag errors. */
	regs->psr |= PSR_C;
#else
	regs->tstate |= 0x1100000000;
#endif
	/* Userland negates the value on sparc. */
	regs->u_regs[U_REG_O0] = err;
	trace_set_regs(regs);
}

static unsigned long trace_arg(void *vregs, int num)
{
	trace_regs *regs = vregs;
	if (num < 7)
		return regs->u_regs[U_REG_O0 + num - 1];
	else
		return -1;
}