blob: 2ae5e0284f5699458ba16a9a8380f14b8cba1cac [file] [log] [blame]
Palmer Dabbelte2c0cdf2017-07-10 18:07:09 -07001/*
2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
3 * Copyright 2015 Regents of the University of California
4 * Copyright 2017 SiFive
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation, version 2.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * Copied from arch/tile/kernel/ptrace.c
16 */
17
18#include <asm/ptrace.h>
19#include <asm/syscall.h>
20#include <asm/thread_info.h>
David Abdurachmanov0aea8942018-10-29 11:48:54 +010021#include <linux/audit.h>
Palmer Dabbelte2c0cdf2017-07-10 18:07:09 -070022#include <linux/ptrace.h>
23#include <linux/elf.h>
24#include <linux/regset.h>
25#include <linux/sched.h>
26#include <linux/sched/task_stack.h>
27#include <linux/tracehook.h>
David Abdurachmanov008e9012018-12-10 21:43:55 +010028
29#define CREATE_TRACE_POINTS
Palmer Dabbelte2c0cdf2017-07-10 18:07:09 -070030#include <trace/events/syscalls.h>
31
32enum riscv_regset {
33 REGSET_X,
Jim Wilsonb8c8a952018-10-17 17:59:05 -070034#ifdef CONFIG_FPU
35 REGSET_F,
36#endif
Palmer Dabbelte2c0cdf2017-07-10 18:07:09 -070037};
38
39static int riscv_gpr_get(struct task_struct *target,
40 const struct user_regset *regset,
41 unsigned int pos, unsigned int count,
42 void *kbuf, void __user *ubuf)
43{
44 struct pt_regs *regs;
45
46 regs = task_pt_regs(target);
47 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
48}
49
50static int riscv_gpr_set(struct task_struct *target,
51 const struct user_regset *regset,
52 unsigned int pos, unsigned int count,
53 const void *kbuf, const void __user *ubuf)
54{
55 int ret;
56 struct pt_regs *regs;
57
58 regs = task_pt_regs(target);
Jim Wilson1db9b802018-06-11 14:48:22 -070059 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
Palmer Dabbelte2c0cdf2017-07-10 18:07:09 -070060 return ret;
61}
62
Jim Wilsonb8c8a952018-10-17 17:59:05 -070063#ifdef CONFIG_FPU
64static int riscv_fpr_get(struct task_struct *target,
65 const struct user_regset *regset,
66 unsigned int pos, unsigned int count,
67 void *kbuf, void __user *ubuf)
68{
69 int ret;
70 struct __riscv_d_ext_state *fstate = &target->thread.fstate;
71
72 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, fstate, 0,
73 offsetof(struct __riscv_d_ext_state, fcsr));
74 if (!ret) {
75 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, fstate, 0,
76 offsetof(struct __riscv_d_ext_state, fcsr) +
77 sizeof(fstate->fcsr));
78 }
79
80 return ret;
81}
82
83static int riscv_fpr_set(struct task_struct *target,
84 const struct user_regset *regset,
85 unsigned int pos, unsigned int count,
86 const void *kbuf, const void __user *ubuf)
87{
88 int ret;
89 struct __riscv_d_ext_state *fstate = &target->thread.fstate;
90
91 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0,
92 offsetof(struct __riscv_d_ext_state, fcsr));
93 if (!ret) {
94 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0,
95 offsetof(struct __riscv_d_ext_state, fcsr) +
96 sizeof(fstate->fcsr));
97 }
98
99 return ret;
100}
101#endif
Palmer Dabbelte2c0cdf2017-07-10 18:07:09 -0700102
103static const struct user_regset riscv_user_regset[] = {
104 [REGSET_X] = {
105 .core_note_type = NT_PRSTATUS,
106 .n = ELF_NGREG,
107 .size = sizeof(elf_greg_t),
108 .align = sizeof(elf_greg_t),
109 .get = &riscv_gpr_get,
110 .set = &riscv_gpr_set,
111 },
Jim Wilsonb8c8a952018-10-17 17:59:05 -0700112#ifdef CONFIG_FPU
113 [REGSET_F] = {
114 .core_note_type = NT_PRFPREG,
115 .n = ELF_NFPREG,
116 .size = sizeof(elf_fpreg_t),
117 .align = sizeof(elf_fpreg_t),
118 .get = &riscv_fpr_get,
119 .set = &riscv_fpr_set,
120 },
121#endif
Palmer Dabbelte2c0cdf2017-07-10 18:07:09 -0700122};
123
124static const struct user_regset_view riscv_user_native_view = {
125 .name = "riscv",
126 .e_machine = EM_RISCV,
127 .regsets = riscv_user_regset,
128 .n = ARRAY_SIZE(riscv_user_regset),
129};
130
131const struct user_regset_view *task_user_regset_view(struct task_struct *task)
132{
133 return &riscv_user_native_view;
134}
135
136void ptrace_disable(struct task_struct *child)
137{
138 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
139}
140
141long arch_ptrace(struct task_struct *child, long request,
142 unsigned long addr, unsigned long data)
143{
144 long ret = -EIO;
145
146 switch (request) {
147 default:
148 ret = ptrace_request(child, request, addr, data);
149 break;
150 }
151
152 return ret;
153}
154
155/*
156 * Allows PTRACE_SYSCALL to work. These are called from entry.S in
157 * {handle,ret_from}_syscall.
158 */
159void do_syscall_trace_enter(struct pt_regs *regs)
160{
161 if (test_thread_flag(TIF_SYSCALL_TRACE))
162 if (tracehook_report_syscall_entry(regs))
163 syscall_set_nr(current, regs, -1);
164
165#ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
166 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
167 trace_sys_enter(regs, syscall_get_nr(current, regs));
168#endif
David Abdurachmanov0aea8942018-10-29 11:48:54 +0100169
170 audit_syscall_entry(regs->a7, regs->a0, regs->a1, regs->a2, regs->a3);
Palmer Dabbelte2c0cdf2017-07-10 18:07:09 -0700171}
172
173void do_syscall_trace_exit(struct pt_regs *regs)
174{
David Abdurachmanov0aea8942018-10-29 11:48:54 +0100175 audit_syscall_exit(regs);
176
Palmer Dabbelte2c0cdf2017-07-10 18:07:09 -0700177 if (test_thread_flag(TIF_SYSCALL_TRACE))
178 tracehook_report_syscall_exit(regs, 0);
179
180#ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
181 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
David Abdurachmanov775800b2018-12-06 16:26:34 +0100182 trace_sys_exit(regs, regs_return_value(regs));
Palmer Dabbelte2c0cdf2017-07-10 18:07:09 -0700183#endif
184}