1/* Fetch live process registers from TID. C-SKY version.
2   Copyright (C) 2019 Hangzhou C-SKY Microsystems co.,ltd.
3   This file is part of elfutils.
4
5   This file is free software; you can redistribute it and/or modify
6   it under the terms of either
7
8     * the GNU Lesser General Public License as published by the Free
9       Software Foundation; either version 3 of the License, or (at
10       your option) any later version
11
12   or
13
14     * the GNU General Public License as published by the Free
15       Software Foundation; either version 2 of the License, or (at
16       your option) any later version
17
18   or both in parallel, as here.
19
20   elfutils is distributed in the hope that it will be useful, but
21   WITHOUT ANY WARRANTY; without even the implied warranty of
22   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23   General Public License for more details.
24
25   You should have received copies of the GNU General Public License and
26   the GNU Lesser General Public License along with this program.  If
27   not, see <http://www.gnu.org/licenses/>.  */
28
29#ifdef HAVE_CONFIG_H
30# include <config.h>
31#endif
32
33#include "system.h"
34#include <assert.h>
35#if defined __CSKY__ && defined __linux__
36# include <sys/uio.h>
37# include <sys/procfs.h>
38# include <sys/ptrace.h>
39#endif
40
41#define BACKEND csky_
42#include "libebl_CPU.h"
43
44bool
45csky_set_initial_registers_tid (pid_t tid __attribute__ ((unused)),
46			ebl_tid_registers_t *setfunc __attribute__ ((unused)),
47				void *arg __attribute__ ((unused)))
48{
49#if !defined __CSKY__ || !defined __linux__
50  return false;
51#else /* __CSKY__ */
52  struct pt_regs user_regs;
53  struct iovec iovec;
54  iovec.iov_base = &user_regs;
55  iovec.iov_len = sizeof (user_regs);
56  if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec) != 0)
57    return false;
58
59  Dwarf_Word dwarf_regs[38];
60
61  /* lr.  */
62  dwarf_regs[15] = user_regs.lr;
63  /* sp.  */
64  dwarf_regs[14] = user_regs.usp;
65  /* r0 ~ r13.  */
66  dwarf_regs[0] = user_regs.a0;
67  dwarf_regs[1] = user_regs.a1;
68  dwarf_regs[2] = user_regs.a2;
69  dwarf_regs[3] = user_regs.a3;
70  for (int i = 4; i < 14; i++)
71    dwarf_regs[i] = user_regs.regs[i - 4];
72  /* r ~ r13.  */
73  for (int i = 16; i < 31; i++)
74    dwarf_regs[i] = user_regs.exregs[i - 16];
75  /* tls.  */
76  dwarf_regs[31] = user_regs.tls;
77  /* hi.  */
78  dwarf_regs[36] = user_regs.rhi;
79  /* lo.  */
80  dwarf_regs[37] = user_regs.rlo;
81  /* pc.  */
82  dwarf_regs[32] = user_regs.pc;
83  setfunc (-1, 1, &dwarf_regs[32], arg);
84
85  return setfunc (0, 38, dwarf_regs, arg);
86#endif
87}
88