-
Notifications
You must be signed in to change notification settings - Fork 1
/
mappings.c
318 lines (280 loc) · 7.93 KB
/
mappings.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
* Copyright 2012 Iain Bullard <iain.bullard@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sub license,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
#include <stdint.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <string.h>
#include <ctype.h>
#include "mappings.h"
#include "log.h"
int file_descriptors[NUM_MAPPINGS];
int next_free_mapping = 1;
/*
* mappings_trace_fd adds a file descriptor to an array of fd's we are tracing
*/
void mappings_trace_fd(int fd)
{
int i,set;
set = 0;
for (i=0;i<NUM_MAPPINGS;i++) {
if(file_descriptors[i] == 0) {
file_descriptors[i] = fd;
set = 1;
break;
} else if (file_descriptors[i] == fd) {
wrap_log("already tracing file descriptor %d",fd);
exit(1);
}
}
if (set == 0) {
wrap_log("ran out of slots to store file descriptors\n");
exit(1);
}
}
/*
* mappings_is_fd_traced returns 1 if this is a fd we are tracing
*/
int mappings_is_fd_traced(int fd)
{
int i;
for(i=0;i<NUM_MAPPINGS;i++) {
if (file_descriptors[i] == fd) {
return 1;
}
}
return 0;
}
/*
* add_mmap_region add a memory mapping to our array of traced regions
*/
void mappings_trace_mmap(uint32_t address,uint32_t phyaddr,size_t length, int regs)
{
int new_mapping;
new_mapping = next_free_mapping;
if (new_mapping > NUM_MAPPINGS) {
wrap_log("mappings error: ran out of space to store new mappings\n");
exit(1);
}
mappings[new_mapping]->size = length;
mappings[new_mapping]->addr = address;
if (regs == 1) {
// taken from cedarx kernel driver.
mappings[new_mapping]->phyaddr = 0x01C0E000;
} else {
mappings[new_mapping]->phyaddr = (phyaddr & 0x0FFFFFFF);
}
mappings[new_mapping]->saddr = mappings[new_mapping]->addr;
mappings[new_mapping]->eaddr = mappings[new_mapping]->addr + mappings[new_mapping]->size;
mappings[new_mapping]->in_use = 1;
mappings[new_mapping]->regs = regs;
next_free_mapping++;
}
/*
* mappings_is_addr_traced returns true if the address is one we are tracing
*/
uint32_t mappings_is_addr_traced(uint32_t address)
{
int id = mappings_get_id(address);
if (id != 0) {
return 1;
} else {
return 0;
}
}
/*
* mappings_get_id returns the id associated with the address or 0 if no mapping is found
*/
int mappings_get_id(uint32_t address)
{
int i;
unsigned long cmp_addr = (unsigned long) address;
for(i=1;i<NUM_MAPPINGS;i++) {
if (cmp_addr >= mappings[i]->saddr && cmp_addr <= mappings[i]->eaddr) {
return i;
}
}
return 0;
}
/*
* mappings_addr_to_base convrts a address to an offset from the base of a region
*/
uint32_t mappings_addr_to_base(uint32_t address)
{
int id = mappings_get_id(address);
uint32_t ret_addr;
if (id != 0) {
ret_addr = address - mappings[id]->addr;
} else {
ret_addr = 0xFFFFFFFF;
}
return ret_addr;
}
/*
* mappings_addr_to_phys returns a physical address
*/
uint32_t mappings_addr_to_phys(uint32_t address)
{
int id = mappings_get_id(address);
uint32_t ret_addr;
if (id != 0) {
ret_addr = mappings[id]->phyaddr + (address - mappings[id]->addr);
} else {
ret_addr = 0xFFFFFFFF;
}
return ret_addr;
}
/*
* is_regs returns 1 if a mmap address maps to cedarx registers
*/
int mappings_is_regs(uint32_t address)
{
int id = mappings_get_id(address);
if (id != 0) {
return mappings[id]->regs;
} else {
return 0;
}
}
/*
* Protect mmap memory
*/
void mappings_protect_all()
{
int i;
for(i=1;i<NUM_MAPPINGS;i++) {
if (mappings[i]->in_use == 1) {
if (mprotect((void *) mappings[i]->addr, mappings[i]->size, PROT_NONE) < 0) {
wrap_log("mappings_protect_all: mprotect(0x%08X|0x%08X) failed\n",(unsigned int) mappings[i]->addr,mappings[i]->size);
exit(1);
}
}
}
}
/*
* protect specific mmap region
*/
void mappings_protect_mapping(uint32_t address)
{
int i;
for(i=1;i<NUM_MAPPINGS;i++) {
if (mappings[i]->in_use == 1) {
if (address >= mappings[i]->saddr && address <= mappings[i]->eaddr) {
if (mprotect((void *) mappings[i]->addr, mappings[i]->size, PROT_NONE) < 0) {
wrap_log("mappings_protect_mapping: mprotect(0x%08X|0x%08X) failed\n",(unsigned int) mappings[i]->addr,mappings[i]->size);
exit(1);
}
return;
}
}
}
wrap_log("mappings_protect_mapping: couldn't find region\n");
exit(1);
}
/*
* Unprotect mmap memory
*/
void mappings_unprotect_all()
{
int i;
for(i=1;i<NUM_MAPPINGS;i++) {
if (mappings[i]->in_use == 1) {
if (mprotect((void *) mappings[i]->addr, mappings[i]->size, PROT_READ | PROT_WRITE) < 0) {
wrap_log("mappings_unprocted_all mprotect(0x%08X|0x%08X) failed\n",(unsigned int) mappings[i]->addr,mappings[i]->size);
exit(1);
}
}
}
}
/*
* unprotect specific mmap region
*/
void mappings_unprotect_mapping(uint32_t address)
{
int i;
for(i=1;i<NUM_MAPPINGS;i++) {
if (mappings[i]->in_use == 1) {
if (address >= mappings[i]->saddr && address <= mappings[i]->eaddr) {
if (mprotect((void *) mappings[i]->addr, mappings[i]->size,PROT_READ | PROT_WRITE) < 0) {
wrap_log("mappings_unprotect_mapping region mprotect(0x%08X|0x%08X) failed\n",(unsigned int) mappings[i]->addr,mappings[i]->size);
exit(1);
}
return;
}
}
}
wrap_log("mappings_unprotect_mapping couldn't find mapping\n");
exit(1);
}
/*
* init storage for mmap memory regions
*/
void mappings_init()
{
int i;
mappings = (mapping_t **) malloc(sizeof(mapping_t) * NUM_MAPPINGS);
for(i = 0; i < NUM_MAPPINGS; i++)
{
mappings[i] = (mapping_t *) malloc(sizeof(mapping_t));
memset(mappings[i],0,sizeof(mapping_t));
mappings[i]->in_use = 0;
}
}
/*
* hexdump dumps a region of memory for a given size
*/
void hexdump(const void *data, int size)
{
unsigned char *buf = (void *) data;
char alpha[17];
int i;
for (i = 0; i < size; i++) {
if (!(i % 16))
wrap_log("mem\tdump\t0x%08x", (unsigned int) buf + i);
if (((void *) (buf + i)) < ((void *) data)) {
wrap_log(" ");
alpha[i % 16] = '.';
} else {
wrap_log(" %02x", buf[i]);
if (isprint(buf[i]) && (buf[i] < 0xA0))
alpha[i % 16] = buf[i];
else
alpha[i % 16] = '.';
}
if ((i % 16) == 15) {
alpha[16] = 0;
wrap_log("\t|%s|\n", alpha);
}
}
if (i % 16) {
for (i %= 16; i < 16; i++) {
wrap_log(" ");
alpha[i] = '.';
if (i == 15) {
alpha[16] = 0;
wrap_log("\t|%s|\n", alpha);
}
}
}
}