xref: /aosp_15_r20/external/elfutils/backends/loongarch_initreg.c (revision 7304104da70ce23c86437a01be71edd1a2d7f37e)
1 /* Fetch live process registers from TID.
2    Copyright (C) 2023 OpenAnolis community LoongArch SIG.
3    Copyright (C) 2023 Loongson Technology Corporation Limted.
4    This file is part of elfutils.
5 
6    This file is free software; you can redistribute it and/or modify
7    it under the terms of either
8 
9      * the GNU Lesser General Public License as published by the Free
10        Software Foundation; either version 3 of the License, or (at
11        your option) any later version
12 
13    or
14 
15      * the GNU General Public License as published by the Free
16        Software Foundation; either version 2 of the License, or (at
17        your option) any later version
18 
19    or both in parallel, as here.
20 
21    elfutils is distributed in the hope that it will be useful, but
22    WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24    General Public License for more details.
25 
26    You should have received copies of the GNU General Public License and
27    the GNU Lesser General Public License along with this program.  If
28    not, see <http://www.gnu.org/licenses/>.  */
29 
30 #ifdef HAVE_CONFIG_H
31 # include <config.h>
32 #endif
33 
34 #include "system.h"
35 #include <assert.h>
36 #if defined __loongarch__ && defined __linux__
37 # include <sys/uio.h>
38 # include <sys/procfs.h>
39 # include <sys/ptrace.h>
40 #endif
41 
42 #define BACKEND loongarch_
43 #include "libebl_CPU.h"
44 
45 bool
loongarch_set_initial_registers_tid(pid_t tid,ebl_tid_registers_t * setfunc,void * arg)46 loongarch_set_initial_registers_tid (pid_t tid __attribute__ ((unused)),
47 				 ebl_tid_registers_t *setfunc __attribute__ ((unused)),
48 				 void *arg __attribute__ ((unused)))
49 {
50 #if !defined __loongarch__ || !defined __linux__
51   return false;
52 #else /* __loongarch__ */
53 
54   /* General registers.  */
55   struct user_regs_struct gregs;
56   struct iovec iovec;
57   iovec.iov_base = &gregs;
58   iovec.iov_len = sizeof (gregs);
59   if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec) != 0)
60     return false;
61 
62   /* $r0 is constant 0.  */
63   Dwarf_Word zero = 0;
64   if (! setfunc (0, 1, &zero, arg))
65     return false;
66 
67   /* $r1-$r31.  */
68   if (! setfunc (1, 32, (Dwarf_Word *) &gregs.regs[1], arg))
69     return false;
70 
71   /* PC.  */
72   if (! setfunc (-1, 1, (Dwarf_Word *) &gregs.csr_era, arg))
73     return false;
74 
75   /* Floating-point registers (only 64bits are used).  */
76   struct user_fp_struct fregs;
77   iovec.iov_base = &fregs;
78   iovec.iov_len = sizeof (fregs);
79   if (ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iovec) != 0)
80     return false;
81 
82   /* $f0-$f31 */
83   if (! setfunc (32, 32, (Dwarf_Word *) &fregs.fpr[0], arg))
84     return false;
85 
86   return true;
87 #endif /* __loongarch__ */
88 }
89