1/* libunwind - a platform-independent unwind library
2   Copyright (C) 2012 Tommi Rantala <tt.rantala@gmail.com>
3
4This file is part of libunwind.
5
6Permission is hereby granted, free of charge, to any person obtaining
7a copy of this software and associated documentation files (the
8"Software"), to deal in the Software without restriction, including
9without limitation the rights to use, copy, modify, merge, publish,
10distribute, sublicense, and/or sell copies of the Software, and to
11permit persons to whom the Software is furnished to do so, subject to
12the following conditions:
13
14The above copyright notice and this permission notice shall be
15included in all copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
24
25#include "_UCD_lib.h"
26
27#include "_UCD_internal.h"
28
29int
30_UCD_access_reg (unw_addr_space_t as,
31                                unw_regnum_t regnum, unw_word_t *valp,
32                                int write, void *arg)
33{
34  struct UCD_info *ui = arg;
35
36  if (write)
37    {
38      Debug(0, "write is not supported\n");
39      return -UNW_EINVAL;
40    }
41
42#if defined(UNW_TARGET_AARCH64)
43  if (regnum < 0 || regnum >= UNW_AARCH64_FPCR)
44    goto badreg;
45#elif defined(UNW_TARGET_ARM)
46  if (regnum < 0 || regnum >= 16)
47    goto badreg;
48#elif defined(UNW_TARGET_SH)
49  if (regnum < 0 || regnum > UNW_SH_PR)
50    goto badreg;
51#else
52#if defined(UNW_TARGET_MIPS)
53  static const uint8_t remap_regs[] =
54    {
55      [UNW_MIPS_R0]  = EF_REG0,
56      [UNW_MIPS_R1]  = EF_REG1,
57      [UNW_MIPS_R2]  = EF_REG2,
58      [UNW_MIPS_R3]  = EF_REG3,
59      [UNW_MIPS_R4]  = EF_REG4,
60      [UNW_MIPS_R5]  = EF_REG5,
61      [UNW_MIPS_R6]  = EF_REG6,
62      [UNW_MIPS_R7]  = EF_REG7,
63      [UNW_MIPS_R8]  = EF_REG8,
64      [UNW_MIPS_R9]  = EF_REG9,
65      [UNW_MIPS_R10] = EF_REG10,
66      [UNW_MIPS_R11] = EF_REG11,
67      [UNW_MIPS_R12] = EF_REG12,
68      [UNW_MIPS_R13] = EF_REG13,
69      [UNW_MIPS_R14] = EF_REG14,
70      [UNW_MIPS_R15] = EF_REG15,
71      [UNW_MIPS_R16] = EF_REG16,
72      [UNW_MIPS_R17] = EF_REG17,
73      [UNW_MIPS_R18] = EF_REG18,
74      [UNW_MIPS_R19] = EF_REG19,
75      [UNW_MIPS_R20] = EF_REG20,
76      [UNW_MIPS_R21] = EF_REG21,
77      [UNW_MIPS_R22] = EF_REG22,
78      [UNW_MIPS_R23] = EF_REG23,
79      [UNW_MIPS_R24] = EF_REG24,
80      [UNW_MIPS_R25] = EF_REG25,
81      [UNW_MIPS_R28] = EF_REG28,
82      [UNW_MIPS_R29] = EF_REG29,
83      [UNW_MIPS_R30] = EF_REG30,
84      [UNW_MIPS_R31] = EF_REG31,
85      [UNW_MIPS_PC]  = EF_CP0_EPC,
86    };
87#elif defined(UNW_TARGET_X86)
88  static const uint8_t remap_regs[] =
89    {
90      /* names from libunwind-x86.h */
91      [UNW_X86_EAX]    = offsetof(struct user_regs_struct, eax) / sizeof(long),
92      [UNW_X86_EDX]    = offsetof(struct user_regs_struct, edx) / sizeof(long),
93      [UNW_X86_ECX]    = offsetof(struct user_regs_struct, ecx) / sizeof(long),
94      [UNW_X86_EBX]    = offsetof(struct user_regs_struct, ebx) / sizeof(long),
95      [UNW_X86_ESI]    = offsetof(struct user_regs_struct, esi) / sizeof(long),
96      [UNW_X86_EDI]    = offsetof(struct user_regs_struct, edi) / sizeof(long),
97      [UNW_X86_EBP]    = offsetof(struct user_regs_struct, ebp) / sizeof(long),
98      [UNW_X86_ESP]    = offsetof(struct user_regs_struct, esp) / sizeof(long),
99      [UNW_X86_EIP]    = offsetof(struct user_regs_struct, eip) / sizeof(long),
100      [UNW_X86_EFLAGS] = offsetof(struct user_regs_struct, eflags) / sizeof(long),
101      [UNW_X86_TRAPNO] = offsetof(struct user_regs_struct, orig_eax) / sizeof(long),
102    };
103#elif defined(UNW_TARGET_X86_64)
104  static const int8_t remap_regs[] =
105    {
106      [UNW_X86_64_RAX]    = offsetof(struct user_regs_struct, rax) / sizeof(long),
107      [UNW_X86_64_RDX]    = offsetof(struct user_regs_struct, rdx) / sizeof(long),
108      [UNW_X86_64_RCX]    = offsetof(struct user_regs_struct, rcx) / sizeof(long),
109      [UNW_X86_64_RBX]    = offsetof(struct user_regs_struct, rbx) / sizeof(long),
110      [UNW_X86_64_RSI]    = offsetof(struct user_regs_struct, rsi) / sizeof(long),
111      [UNW_X86_64_RDI]    = offsetof(struct user_regs_struct, rdi) / sizeof(long),
112      [UNW_X86_64_RBP]    = offsetof(struct user_regs_struct, rbp) / sizeof(long),
113      [UNW_X86_64_RSP]    = offsetof(struct user_regs_struct, rsp) / sizeof(long),
114      [UNW_X86_64_RIP]    = offsetof(struct user_regs_struct, rip) / sizeof(long),
115    };
116#else
117#error Port me
118#endif
119
120  if (regnum < 0 || regnum >= (unw_regnum_t)ARRAY_SIZE(remap_regs))
121    goto badreg;
122
123  regnum = remap_regs[regnum];
124#endif
125
126  /* pr_reg is a long[] array, but it contains struct user_regs_struct's
127   * image.
128   */
129  Debug(1, "pr_reg[%d]:%ld (0x%lx)\n", regnum,
130		(long)ui->prstatus->pr_reg[regnum],
131		(long)ui->prstatus->pr_reg[regnum]
132  );
133  *valp = ui->prstatus->pr_reg[regnum];
134
135  return 0;
136
137badreg:
138  Debug(0, "bad regnum:%d\n", regnum);
139  return -UNW_EINVAL;
140}
141