Stackdb
Stackdb is a stackable, multi-target and -level source debugger and memory forensics library.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
arch.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 The University of Utah
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #ifndef __ARCH_H__
20 #define __ARCH_H__
21 
22 #include "config.h"
23 #include "common.h"
24 
25 #include <glib.h>
26 
27 /*
28  * Ok. Architecture support should have any instruction-level or
29  * register-level definitions, functions, that we need. For instance,
30  * it should have things like X86_IP, X86_CR1, X86_64_IP, etc.
31  *
32  * Each arch should have a register file that allows
33  * reading/writing/flushing/setting, and doing so for the REGVAL size by
34  * default, but also supporting arbitrary-sized regvals (like 512-bit
35  * avx2 registers, or whatever).
36  *
37  * There should be mappings for debuginfo register numbers. These ABI
38  * register numbers should be the primary register numbers for each
39  * arch.
40  *
41  * But then how does the user access the regs? Ideally, through a CREG
42  * abstraction. Any arch should have an IP/PC; SP; BP; RA; RV; but then
43  * the GP ones always have different names.
44  *
45  * I'm fine to have files like arch_x86.h that have X86_IP 55 macros in
46  * them. We could even allow RAX to mean EAX on x86_64, transparently?
47  * And each arch_ops struct would have translation tables that trnaslate
48  * names to numbers. We can evn support pseudo-registers or MTRRs this
49  * way... the backend just writes numeric registers based on what the
50  * arch_ops type of the target is. So, readreg/writereg might become
51  * arch-specific instead of target-specific???
52  *
53  * Also push breakpoint instruction stuff in.
54  * Also disassembly (?)
55  * Also
56  *
57  * Anyway, backends can cache the current thread's registers when they
58  * load them; then the target API will tell teh backend to write
59  * registers, and it will update its cache; then it will flush later.
60  * The target API/arch ops should keep track of which regs were
61  * modified, and only write those.
62  *
63  * Hm, maybe our register file can actually be generic? Just give it a
64  * max number, and degree of sparseness, and let it do its thing? Then
65  * hint which regs are super-sized... ? Yes!
66  */
67 
68 typedef enum {
71 } endian_t;
72 
73 typedef enum {
74  CREG_IP = 0,
79 } common_reg_t;
80 #define COMMON_REG_COUNT CREG_RET + 1
81 
82 /*
83  * A simple abstraction for machine architectures. Haven't even made
84  * any endian swapping arrangements yet. Right now, this is all about
85  * registers. This structure, coupled to an arch_config, helps the
86  * target lib get the most out of a regcache.
87  *
88  * For now, we just assume that sizeof(REG) is 1 byte; this means that
89  * our data structures stay small enough with flat arrays. Later, we
90  * might need to go to hashtables or sparse arrays for many-register
91  * architectures. When pigs fly... even ia64 was only 128. We could
92  * fix that by changing REG to uint8_t instead of int8_t and doing
93  * better error handling.
94  *
95  * Assume a byte is 8 bits, and that every significant machine
96  * abstraction is at least byte-aligned. We'll never support anything
97  * else!
98  */
99 
100 typedef enum {
102  ARCH_X86 = 1,
104 } arch_type_t;
105 
106 /*
107  * This is a two-dimensional array. It specifies an ordered list of
108  * registers to print at each level of detail. For instance,
109  *
110  * { { 1,3,5,7,-1 },
111  * { 2,4,6,8,-1 },
112  * NULL, }
113  */
114 #define ARCH_SNPRINTF_DETAIL_LEVELS 3 /* 0,1,2 */
115 
116 struct arch {
118  const char *name;
119 
121  unsigned int wordsize;
122  unsigned int ptrsize;
123 
124  /*
125  * Max register number + 1. Not all registers from (0,max) need be
126  * supported; set reg_sizes[i] and reg_names[i] appropriately.
127  */
128  int regcount;
129  /*
130  * This array must be @regcount long. Register numbers that this
131  * architecture does not provide should be set to 0.
132  */
133  uint8_t *reg_sizes;
134  /*
135  * This array must be COMMON_REG_COUNT long. Unbound regs should be
136  * set to -1.
137  */
138  REG *common_to_arch; //[COMMON_REG_COUNT];
139  /*
140  * This array must be @regcount long. Unbound regs should have NULL
141  * names.
142  */
143  char **reg_names;
144 
147 
148  /* One or more opcodes that create a software breakpoint */
150  unsigned int breakpoint_instrs_len;
151  /* How many opcodes are in the above sequence, so we can single-step
152  * past them all.
153  */
155 
156  uint8_t *ret_instrs;
157  unsigned int ret_instrs_len;
158  unsigned int ret_instr_count;
159 
160  uint8_t *full_ret_instrs;
161  unsigned int full_ret_instrs_len;
162  unsigned int full_ret_instr_count;
163 };
164 
165 /* Get the arch struct corresponding to the given arch type. */
166 struct arch *arch_get(arch_type_t at);
167 
168 /* Some simple accessors. */
169 static inline const char *arch_name(struct arch *arch) { return arch->name; }
170 static inline arch_type_t arch_type(struct arch *arch) { return arch->type; }
171 static inline endian_t arch_endian(struct arch *arch) { return arch->endian; }
172 static inline unsigned int arch_wordsize(struct arch *arch) { return arch->wordsize; }
173 static inline unsigned int arch_ptrsize(struct arch *arch) { return arch->ptrsize; }
174 static inline int arch_regcount(struct arch *arch) { return arch->regcount; }
175 
176 int arch_has_reg(struct arch *arch,REG reg);
177 /* Get the size of an arch-specific register number. */
178 unsigned int arch_regsize(struct arch *arch,REG reg);
179 /* Get arch-specific register name. */
180 const char *arch_regname(struct arch *arch,REG reg);
181 /* Get arch-specific reg number for the "common" register. */
182 int arch_regno(struct arch *arch,char *name,REG *reg);
183 /* Get arch-specific reg number for the "common" register. */
184 int arch_cregno(struct arch *arch,common_reg_t creg,REG *reg);
185 
186 #endif /* __ARCH_H__ */
arch_type_t type
Definition: arch.h:117
uint8_t * breakpoint_instrs
Definition: arch.h:149
unsigned int ret_instrs_len
Definition: arch.h:157
int max_snprintf_ordering
Definition: arch.h:146
int arch_regno(struct arch *arch, char *name, REG *reg)
Definition: arch.c:58
unsigned int ptrsize
Definition: arch.h:122
common_reg_t
Definition: arch.h:73
Definition: arch.h:74
unsigned int breakpoint_instr_count
Definition: arch.h:154
unsigned int arch_regsize(struct arch *arch, REG reg)
Definition: arch.c:44
int regcount
Definition: arch.h:128
int * snprintf_ordering[ARCH_SNPRINTF_DETAIL_LEVELS]
Definition: arch.h:145
uint8_t * ret_instrs
Definition: arch.h:156
endian_t
Definition: arch.h:68
int arch_has_reg(struct arch *arch, REG reg)
Definition: arch.c:36
const char * arch_regname(struct arch *arch, REG reg)
Definition: arch.c:51
uint8_t * reg_sizes
Definition: arch.h:133
unsigned int full_ret_instrs_len
Definition: arch.h:161
int arch_cregno(struct arch *arch, common_reg_t creg, REG *reg)
Definition: arch.c:73
arch_type_t
Definition: arch.h:100
REG * common_to_arch
Definition: arch.h:138
unsigned int wordsize
Definition: arch.h:121
struct arch * arch_get(arch_type_t at)
Definition: arch.c:25
Definition: arch.h:102
char ** reg_names
Definition: arch.h:143
int8_t REG
Definition: common.h:93
unsigned int breakpoint_instrs_len
Definition: arch.h:150
Definition: arch.h:116
uint8_t * full_ret_instrs
Definition: arch.h:160
endian_t endian
Definition: arch.h:120
const char * name
Definition: arch.h:118
#define ARCH_SNPRINTF_DETAIL_LEVELS
Definition: arch.h:114
Definition: arch.h:78
Definition: arch.h:75
Definition: arch.h:76
unsigned int full_ret_instr_count
Definition: arch.h:162
unsigned int ret_instr_count
Definition: arch.h:158