-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathemu_memory.c
More file actions
576 lines (457 loc) · 12.2 KB
/
emu_memory.c
File metadata and controls
576 lines (457 loc) · 12.2 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/********************************************************************************
* libemu
*
* - x86 shellcode emulation -
*
*
* Copyright (C) 2007 Paul Baecher & Markus Koetter
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*
* contact nepenthesdev@users.sourceforge.net
*
*******************************************************************************/
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stdbool.h>
#include "emu/emu.h"
#include "emu/emu_log.h"
#include "emu/emu_memory.h"
#include "emu/emu_string.h"
#include "emu/emu_breakpoint.h"
#define EMU_PAGE_BITS 12 /* size of one page, 2^12 = 4096 */
#define EMU_PAGESET_BITS 10 /* number of pages in one pageset, 2^10 = 1024 */
#ifndef EMU_PAGE_SIZE
#define EMU_PAGE_SIZE (1 << EMU_PAGE_BITS)
#endif
#define EMU_PAGESET_SIZE (1 << EMU_PAGESET_BITS)
#define PAGESET(x) ((x) >> (EMU_PAGESET_BITS + EMU_PAGE_BITS))
#define PAGE(x) (((x) >> EMU_PAGE_BITS) & ((1 << EMU_PAGESET_BITS) - 1))
#define OFFSET(x) (((1 << EMU_PAGE_BITS) - 1) & (x))
#define FS_SEGMENT_DEFAULT_OFFSET 0x7ffdf000
struct emu_memory
{
struct emu *emu;
void ***pagetable;
uint32_t segment_offset;
enum emu_segment segment_current;
uint32_t segment_table[6];
bool read_only_access;
struct emu_breakpoint *breakpoint;
};
#if 1
/*static void emu_memory_debug_pagetable(struct emu_memory *m)
{
int pagesets = 1 << (32 - EMU_PAGESET_BITS - EMU_PAGE_BITS);
int pagesets_used = 0;
printf("*** memory debug\n");
int i, j;
for( i = 0; i < pagesets; i++ )
{
if( m->pagetable[i] != NULL )
{
printf(" pageset %d allocated at 0x%08x\n", i, (int)m->pagetable[i]);
int pages = 1 << (EMU_PAGESET_BITS);
int pages_used = 0;
for( j = 0; j < pages; j++ )
{
if( m->pagetable[i][j] != NULL )
{
printf(" page %d allocated at 0x%08x\n", j, (int)m->pagetable[i][j]);
pages_used++;
}
}
printf(" %d/%d pages used\n", pages_used, pages);
pagesets_used++;
}
}
printf(" %d/%d pagesets used\n", pagesets_used, pagesets);
printf("*** end of memory debug\n");
}
static void emu_memory_debug_addr(uint32_t addr)
{
printf("addr 0x%08x, pageset 0x%08x, page 0x%08x, offset 0x%08x\n",
addr, EMU_PAGESET(addr), EMU_PAGE(addr), OFFSET(addr));
}*/
#endif
uint32_t emu_memory_get_usage(struct emu_memory *m)
{
uint32_t usage = (1 << (32 - EMU_PAGE_BITS - EMU_PAGESET_BITS)) * sizeof(void *); /* pageset table */
int pagesets = 1 << (32 - EMU_PAGESET_BITS - EMU_PAGE_BITS);
int i, j;
for( i = 0; i < pagesets; i++ )
{
if( m->pagetable[i] != NULL )
{
usage += EMU_PAGESET_SIZE * sizeof(void *);
int pages = 1 << (EMU_PAGESET_BITS);
for( j = 0; j < pages; j++ )
if( m->pagetable[i][j] != NULL )
usage += EMU_PAGE_SIZE;
}
}
return usage;
}
struct emu_memory *emu_memory_new(struct emu *e)
{
struct emu_memory *em = (struct emu_memory *)malloc(sizeof(struct emu_memory));
if( em == NULL )
{
return NULL;
}
memset(em, 0, sizeof(struct emu_memory));
em->emu = e;
em->pagetable = malloc((1 << (32 - EMU_PAGE_BITS - EMU_PAGESET_BITS)) * sizeof(void *));
if( em->pagetable == NULL )
{
return NULL;
}
memset(em->pagetable, 0, (1 << (32 - EMU_PAGE_BITS - EMU_PAGESET_BITS)) * sizeof(void *));
em->segment_table[s_fs] = FS_SEGMENT_DEFAULT_OFFSET;
em->read_only_access = false;
em->breakpoint = emu_breakpoint_alloc(em);
if ( em->breakpoint == NULL )
{
return NULL;
}
return em;
}
void emu_memory_free(struct emu_memory *m)
{
int i, j;
emu_breakpoint_free(m->breakpoint);
for( i = 0; i < (1 << (32 - EMU_PAGESET_BITS - EMU_PAGE_BITS)); i++ )
{
if( m->pagetable[i] != NULL )
{
for( j = 0; j < EMU_PAGESET_SIZE; j++ )
{
if( m->pagetable[i][j] != NULL ) {
free(m->pagetable[i][j]);
m->pagetable[i][j] = NULL;
}
}
free(m->pagetable[i]);
m->pagetable[i] = NULL;
}
}
free(m->pagetable);
free(m);
}
void emu_memory_clear(struct emu_memory *m)
{
int i, j;
for( i = 0; i < (1 << (32 - EMU_PAGESET_BITS - EMU_PAGE_BITS)); i++ )
{
if( m->pagetable[i] != NULL )
{
for( j = 0; j < EMU_PAGESET_SIZE; j++ )
if( m->pagetable[i][j] != NULL )
free(m->pagetable[i][j]);
free(m->pagetable[i]);
}
}
memset(m->pagetable, 0, (1 << (32 - EMU_PAGE_BITS - EMU_PAGESET_BITS)) * sizeof(void *));
m->segment_table[s_fs] = FS_SEGMENT_DEFAULT_OFFSET;
m->read_only_access = false;
}
static inline int page_is_alloc(struct emu_memory *em, uint32_t addr)
{
if( em->pagetable[EMU_PAGESET(addr)] != NULL )
{
if( em->pagetable[EMU_PAGESET(addr)][EMU_PAGE(addr)] != NULL )
{
return -1;
}
}
return 0;
}
static inline int page_alloc(struct emu_memory *em, uint32_t addr)
{
if( em->pagetable[EMU_PAGESET(addr)] == NULL )
{
em->pagetable[EMU_PAGESET(addr)] = malloc(EMU_PAGESET_SIZE * sizeof(void *));
if( em->pagetable[EMU_PAGESET(addr)] == NULL )
{
emu_errno_set(em->emu, ENOMEM);
emu_strerror_set(em->emu, "out of memory\n", addr);
return -1;
}
memset(em->pagetable[EMU_PAGESET(addr)], 0, EMU_PAGESET_SIZE * sizeof(void *));
}
if( em->pagetable[EMU_PAGESET(addr)][EMU_PAGE(addr)] == NULL )
{
em->pagetable[EMU_PAGESET(addr)][EMU_PAGE(addr)] = malloc(EMU_PAGE_SIZE);
if( em->pagetable[EMU_PAGESET(addr)][EMU_PAGE(addr)] == NULL )
{
emu_errno_set(em->emu, ENOMEM);
emu_strerror_set(em->emu, "out of memory\n", addr);
return -1;
}
memset(em->pagetable[EMU_PAGESET(addr)][EMU_PAGE(addr)], 0, EMU_PAGE_SIZE);
}
return 0;
}
static inline void *translate_addr(struct emu_memory *em, uint32_t addr)
{
if( em->pagetable[EMU_PAGESET(addr)] != NULL )
{
if( em->pagetable[EMU_PAGESET(addr)][EMU_PAGE(addr)] != NULL )
{
return em->pagetable[EMU_PAGESET(addr)][EMU_PAGE(addr)] + OFFSET(addr);
}
}
return NULL;
}
int32_t emu_memory_read_byte(struct emu_memory *m, uint32_t addr, uint8_t *byte)
{
addr += m->segment_offset;
void *address = translate_addr(m, addr);
if( address == NULL )
{
emu_errno_set(m->emu, EFAULT);
emu_strerror_set(m->emu, "error accessing 0x%08x not mapped\n", addr);
return -1;
}
*byte = *((uint8_t *)address);
return 0;
}
int32_t emu_memory_read_word(struct emu_memory *m, uint32_t addr, uint16_t *word)
{
#if BYTE_ORDER == BIG_ENDIAN
uint16_t val;
int32_t retval = emu_memory_read_block(m, addr, &val, 2);
val = ((val & 0xff00) >> 8) |
((val & 0x00ff) << 8);
bcopy(&val,word,2);
return retval;
#else
return emu_memory_read_block(m, addr, word, 2);
#endif
}
int32_t emu_memory_read_dword(struct emu_memory *m, uint32_t addr, uint32_t *dword)
{
#if BYTE_ORDER == BIG_ENDIAN
uint32_t val;
int32_t retval = emu_memory_read_block(m, addr, &val, 4);
val = ((val & (0xff000000)) >> 24) |
((val & (0x00ff0000)) >> 8) |
((val & (0x0000ff00)) << 8) |
((val & (0x000000ff)) << 24);
memcpy(dword, &val, 4);
return retval;
#else
return emu_memory_read_block(m, addr, dword, 4);
#endif
}
int32_t emu_memory_read_block(struct emu_memory *m, uint32_t addr, void *dest, size_t len)
{
emu_breakpoint_check(m, addr, EMU_ACCESS_READ);
uint32_t oaddr = addr; /* save original addr for recursive call */
addr += m->segment_offset;
void *address = translate_addr(m, addr);
if( address == NULL )
{
emu_errno_set(m->emu, EFAULT);
emu_strerror_set(m->emu, "error accessing 0x%08x not mapped\n", addr);
return -1;
}
if (OFFSET(addr) + len <= EMU_PAGE_SIZE)
{
bcopy(address, dest, len);
return 0;
}
else
{
uint32_t cb = EMU_PAGE_SIZE - OFFSET(addr);
bcopy(address, dest, cb);
return emu_memory_read_block(m, oaddr + cb, dest + cb, len - cb);
}
}
int32_t emu_memory_read_string(struct emu_memory *m, uint32_t addr, struct emu_string *s, uint32_t maxsize)
{
uint32_t i = 0;
void *address;
while( 1 )
{
if (i > maxsize - 1)
return -1;
address = translate_addr(m, addr + i);
if( address == NULL )
return -1;
if( *(uint8_t *)address == '\0' )
break;
i++;
}
s->data = malloc(i + 1);
memset(s->data, 0, i + 1);
s->size = i;
return emu_memory_read_block(m, addr, s->data, i);
}
int32_t emu_memory_write_byte(struct emu_memory *m, uint32_t addr, uint8_t byte)
{
if ( m->read_only_access == true )
return 0;
addr += m->segment_offset;
void *address = translate_addr(m, addr);
if( address == NULL )
{
if( page_alloc(m, addr) == -1 )
return -1;
address = translate_addr(m, addr);
}
*((uint8_t *)address) = byte;
return 0;
}
int32_t emu_memory_write_word(struct emu_memory *m, uint32_t addr, uint16_t word)
{
if (m->read_only_access == true)
return 0;
#if BYTE_ORDER == BIG_ENDIAN
uint16_t val;
bcopy(&word, &val, 2);
val = ((val & 0xff00) >> 8) |
((val & 0x00ff) << 8);
return emu_memory_write_block(m, addr, &val, 2);
#else
return emu_memory_write_block(m, addr, &word, 2);
#endif
}
int32_t emu_memory_write_dword(struct emu_memory *m, uint32_t addr, uint32_t dword)
{
if (m->read_only_access == true)
return 0;
#if BYTE_ORDER == BIG_ENDIAN
uint32_t val;
bcopy(&dword, &val, 4);
val = ((val & (0xff000000)) >> 24) |
((val & (0x00ff0000)) >> 8) |
((val & (0x0000ff00)) << 8) |
((val & (0x000000ff)) << 24);
return emu_memory_write_block(m, addr, &val, 4);
#else
return emu_memory_write_block(m, addr, &dword, 4);
#endif
}
int32_t emu_memory_write_block(struct emu_memory *m, uint32_t addr, const void *src, size_t len)
{
emu_breakpoint_check(m, addr, EMU_ACCESS_WRITE);
if (m->read_only_access == true)
return 0;
uint32_t oaddr = addr; /* save original addr for recursive call */
addr += m->segment_offset;
void *address = translate_addr(m, addr);
if( address == NULL )
{
if( page_alloc(m, addr) == -1 )
return -1;
address = translate_addr(m, addr);
}
if (OFFSET(addr) + len <= EMU_PAGE_SIZE)
{
bcopy(src, address, len);
return 0;
}
else
{
uint32_t cb = EMU_PAGE_SIZE - OFFSET(addr);
bcopy(src, address, cb);
return emu_memory_write_block(m, oaddr + cb, src + cb, len - cb);
}
return 0;
}
void emu_memory_segment_select(struct emu_memory *m, enum emu_segment s)
{
m->segment_current = s;
m->segment_offset = m->segment_table[m->segment_current];
}
enum emu_segment emu_memory_segment_get(struct emu_memory *m)
{
return m->segment_current;
}
/* make sure a memory block of size *len* is allocated at *addr* */
/*int32_t emu_memory_alloc_at(struct emu_memory *m, uint32_t addr, size_t len)
{
len += addr % EMU_PAGE_SIZE;
addr = (addr >> EMU_PAGE_BITS) << EMU_PAGE_BITS;
while( len > 0 )
{
if( len > EMU_PAGE_SIZE )
{
len -= EMU_PAGE_SIZE;
page_alloc(m, addr);
addr += EMU_PAGE_SIZE;
}
else
{
len -= len;
page_alloc(m, addr);
}
}
return 0;
}*/
int32_t emu_memory_alloc(struct emu_memory *m, uint32_t *addr, size_t len)
{
*addr = 0x00200000;
uint32_t pages = len / EMU_PAGE_SIZE;
if( len % EMU_PAGE_SIZE != 0 )
{
pages++;
}
int i;
/* TODO: ensure termination */
for( ; ; )
{
for( i = 0; i < pages; i++ )
{
if( page_is_alloc(m, *addr + i * EMU_PAGE_SIZE) != 0 )
{
break;
}
}
if( i == pages )
{
for( i = 0; i < pages; i++ )
{
if( page_alloc(m, *addr + i * EMU_PAGE_SIZE) )
{
return -1;
}
}
return 0;
}
*addr += len + EMU_PAGE_SIZE;
}
return -1;
}
void emu_memory_mode_ro(struct emu_memory *m)
{
m->read_only_access = true;
}
void emu_memory_mode_rw(struct emu_memory *m)
{
m->read_only_access = false;
}
/* Everything below this is ugly. */
struct emu_breakpoint *emu_memory_get_breakpoint(struct emu_memory *m)
{
return m->breakpoint;
}
struct emu *emu_memory_get_emu(struct emu_memory *m)
{
return m->emu;
}