blob: 3687514383669028d9717c35b956b21af5bb1c0e [file] [log] [blame]
Thomas Gleixner50acfb22019-05-29 07:18:00 -07001// SPDX-License-Identifier: GPL-2.0-only
Palmer Dabbelte2c0cdf2017-07-10 18:07:09 -07002/*
3 * Copyright 2010 Tilera Corporation. All Rights Reserved.
4 * Copyright 2015 Regents of the University of California
5 * Copyright 2017 SiFive
6 *
Palmer Dabbelte2c0cdf2017-07-10 18:07:09 -07007 * Copied from arch/tile/kernel/ptrace.c
8 */
9
10#include <asm/ptrace.h>
11#include <asm/syscall.h>
12#include <asm/thread_info.h>
David Abdurachmanov0aea8942018-10-29 11:48:54 +010013#include <linux/audit.h>
Palmer Dabbelte2c0cdf2017-07-10 18:07:09 -070014#include <linux/ptrace.h>
15#include <linux/elf.h>
16#include <linux/regset.h>
17#include <linux/sched.h>
18#include <linux/sched/task_stack.h>
19#include <linux/tracehook.h>
David Abdurachmanov008e9012018-12-10 21:43:55 +010020
21#define CREATE_TRACE_POINTS
Palmer Dabbelte2c0cdf2017-07-10 18:07:09 -070022#include <trace/events/syscalls.h>
23
24enum riscv_regset {
25 REGSET_X,
Jim Wilsonb8c8a952018-10-17 17:59:05 -070026#ifdef CONFIG_FPU
27 REGSET_F,
28#endif
Palmer Dabbelte2c0cdf2017-07-10 18:07:09 -070029};
30
31static int riscv_gpr_get(struct task_struct *target,
32 const struct user_regset *regset,
33 unsigned int pos, unsigned int count,
34 void *kbuf, void __user *ubuf)
35{
36 struct pt_regs *regs;
37
38 regs = task_pt_regs(target);
39 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
40}
41
42static int riscv_gpr_set(struct task_struct *target,
43 const struct user_regset *regset,
44 unsigned int pos, unsigned int count,
45 const void *kbuf, const void __user *ubuf)
46{
47 int ret;
48 struct pt_regs *regs;
49
50 regs = task_pt_regs(target);
Jim Wilson1db9b802018-06-11 14:48:22 -070051 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
Palmer Dabbelte2c0cdf2017-07-10 18:07:09 -070052 return ret;
53}
54
Jim Wilsonb8c8a952018-10-17 17:59:05 -070055#ifdef CONFIG_FPU
56static int riscv_fpr_get(struct task_struct *target,
57 const struct user_regset *regset,
58 unsigned int pos, unsigned int count,
59 void *kbuf, void __user *ubuf)
60{
61 int ret;
62 struct __riscv_d_ext_state *fstate = &target->thread.fstate;
63
64 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, fstate, 0,
65 offsetof(struct __riscv_d_ext_state, fcsr));
66 if (!ret) {
67 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, fstate, 0,
68 offsetof(struct __riscv_d_ext_state, fcsr) +
69 sizeof(fstate->fcsr));
70 }
71
72 return ret;
73}
74
75static int riscv_fpr_set(struct task_struct *target,
76 const struct user_regset *regset,
77 unsigned int pos, unsigned int count,
78 const void *kbuf, const void __user *ubuf)
79{
80 int ret;
81 struct __riscv_d_ext_state *fstate = &target->thread.fstate;
82
83 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0,
84 offsetof(struct __riscv_d_ext_state, fcsr));
85 if (!ret) {
86 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0,
87 offsetof(struct __riscv_d_ext_state, fcsr) +
88 sizeof(fstate->fcsr));
89 }
90
91 return ret;
92}
93#endif
Palmer Dabbelte2c0cdf2017-07-10 18:07:09 -070094
95static const struct user_regset riscv_user_regset[] = {
96 [REGSET_X] = {
97 .core_note_type = NT_PRSTATUS,
98 .n = ELF_NGREG,
99 .size = sizeof(elf_greg_t),
100 .align = sizeof(elf_greg_t),
101 .get = &riscv_gpr_get,
102 .set = &riscv_gpr_set,
103 },
Jim Wilsonb8c8a952018-10-17 17:59:05 -0700104#ifdef CONFIG_FPU
105 [REGSET_F] = {
106 .core_note_type = NT_PRFPREG,
107 .n = ELF_NFPREG,
108 .size = sizeof(elf_fpreg_t),
109 .align = sizeof(elf_fpreg_t),
110 .get = &riscv_fpr_get,
111 .set = &riscv_fpr_set,
112 },
113#endif
Palmer Dabbelte2c0cdf2017-07-10 18:07:09 -0700114};
115
116static const struct user_regset_view riscv_user_native_view = {
117 .name = "riscv",
118 .e_machine = EM_RISCV,
119 .regsets = riscv_user_regset,
120 .n = ARRAY_SIZE(riscv_user_regset),
121};
122
123const struct user_regset_view *task_user_regset_view(struct task_struct *task)
124{
125 return &riscv_user_native_view;
126}
127
128void ptrace_disable(struct task_struct *child)
129{
130 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
131}
132
133long arch_ptrace(struct task_struct *child, long request,
134 unsigned long addr, unsigned long data)
135{
136 long ret = -EIO;
137
138 switch (request) {
139 default:
140 ret = ptrace_request(child, request, addr, data);
141 break;
142 }
143
144 return ret;
145}
146
147/*
148 * Allows PTRACE_SYSCALL to work. These are called from entry.S in
149 * {handle,ret_from}_syscall.
150 */
151void do_syscall_trace_enter(struct pt_regs *regs)
152{
153 if (test_thread_flag(TIF_SYSCALL_TRACE))
154 if (tracehook_report_syscall_entry(regs))
155 syscall_set_nr(current, regs, -1);
156
157#ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
158 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
159 trace_sys_enter(regs, syscall_get_nr(current, regs));
160#endif
David Abdurachmanov0aea8942018-10-29 11:48:54 +0100161
162 audit_syscall_entry(regs->a7, regs->a0, regs->a1, regs->a2, regs->a3);
Palmer Dabbelte2c0cdf2017-07-10 18:07:09 -0700163}
164
165void do_syscall_trace_exit(struct pt_regs *regs)
166{
David Abdurachmanov0aea8942018-10-29 11:48:54 +0100167 audit_syscall_exit(regs);
168
Palmer Dabbelte2c0cdf2017-07-10 18:07:09 -0700169 if (test_thread_flag(TIF_SYSCALL_TRACE))
170 tracehook_report_syscall_exit(regs, 0);
171
172#ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
173 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
David Abdurachmanov775800b2018-12-06 16:26:34 +0100174 trace_sys_exit(regs, regs_return_value(regs));
Palmer Dabbelte2c0cdf2017-07-10 18:07:09 -0700175#endif
176}