forked from arjunjm/SimpleScalar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.h
292 lines (247 loc) · 11.6 KB
/
memory.h
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
/* memory.h - flat memory space interfaces */
/* SimpleScalar(TM) Tool Suite
* Copyright (C) 1994-2003 by Todd M. Austin, Ph.D. and SimpleScalar, LLC.
* All Rights Reserved.
*
* THIS IS A LEGAL DOCUMENT, BY USING SIMPLESCALAR,
* YOU ARE AGREEING TO THESE TERMS AND CONDITIONS.
*
* No portion of this work may be used by any commercial entity, or for any
* commercial purpose, without the prior, written permission of SimpleScalar,
* LLC ([email protected]). Nonprofit and noncommercial use is permitted
* as described below.
*
* 1. SimpleScalar is provided AS IS, with no warranty of any kind, express
* or implied. The user of the program accepts full responsibility for the
* application of the program and the use of any results.
*
* 2. Nonprofit and noncommercial use is encouraged. SimpleScalar may be
* downloaded, compiled, executed, copied, and modified solely for nonprofit,
* educational, noncommercial research, and noncommercial scholarship
* purposes provided that this notice in its entirety accompanies all copies.
* Copies of the modified software can be delivered to persons who use it
* solely for nonprofit, educational, noncommercial research, and
* noncommercial scholarship purposes provided that this notice in its
* entirety accompanies all copies.
*
* 3. ALL COMMERCIAL USE, AND ALL USE BY FOR PROFIT ENTITIES, IS EXPRESSLY
* PROHIBITED WITHOUT A LICENSE FROM SIMPLESCALAR, LLC ([email protected]).
*
* 4. No nonprofit user may place any restrictions on the use of this software,
* including as modified by the user, by any other authorized user.
*
* 5. Noncommercial and nonprofit users may distribute copies of SimpleScalar
* in compiled or executable form as set forth in Section 2, provided that
* either: (A) it is accompanied by the corresponding machine-readable source
* code, or (B) it is accompanied by a written offer, with no time limit, to
* give anyone a machine-readable copy of the corresponding source code in
* return for reimbursement of the cost of distribution. This written offer
* must permit verbatim duplication by anyone, or (C) it is distributed by
* someone who received only the executable form, and is accompanied by a
* copy of the written offer of source code.
*
* 6. SimpleScalar was developed by Todd M. Austin, Ph.D. The tool suite is
* currently maintained by SimpleScalar LLC ([email protected]). US Mail:
* 2395 Timbercrest Court, Ann Arbor, MI 48105.
*
* Copyright (C) 1994-2003 by Todd M. Austin, Ph.D. and SimpleScalar, LLC.
*/
#ifndef MEMORY_H
#define MEMORY_H
#include <stdio.h>
#include "host.h"
#include "misc.h"
#include "machine.h"
#include "options.h"
#include "stats.h"
/* number of entries in page translation hash table (must be power-of-two) */
#define MEM_PTAB_SIZE (32*1024)
#define MEM_LOG_PTAB_SIZE 15
/* page table entry */
struct mem_pte_t {
struct mem_pte_t *next; /* next translation in this bucket */
md_addr_t tag; /* virtual page number tag */
byte_t *page; /* page pointer */
};
/* memory object */
struct mem_t {
/* memory object state */
char *name; /* name of this memory space */
struct mem_pte_t *ptab[MEM_PTAB_SIZE];/* inverted page table */
/* memory object stats */
counter_t page_count; /* total number of pages allocated */
counter_t ptab_misses; /* total first level page tbl misses */
counter_t ptab_accesses; /* total page table accesses */
};
/* memory access command */
enum mem_cmd {
Read, /* read memory from target (simulated prog) to host */
Write /* write memory from host (simulator) to target */
};
/* memory access function type, this is a generic function exported for the
purpose of access the simulated vitual memory space */
typedef enum md_fault_type
(*mem_access_fn)(struct mem_t *mem, /* memory space to access */
enum mem_cmd cmd, /* Read or Write */
md_addr_t addr, /* target memory address to access */
void *p, /* where to copy to/from */
int nbytes); /* transfer length in bytes */
/*
* virtual to host page translation macros
*/
/* compute page table set */
#define MEM_PTAB_SET(ADDR) \
(((ADDR) >> MD_LOG_PAGE_SIZE) & (MEM_PTAB_SIZE - 1))
/* compute page table tag */
#define MEM_PTAB_TAG(ADDR) \
((ADDR) >> (MD_LOG_PAGE_SIZE + MEM_LOG_PTAB_SIZE))
/* convert a pte entry at idx to a block address */
#define MEM_PTE_ADDR(PTE, IDX) \
(((PTE)->tag << (MD_LOG_PAGE_SIZE + MEM_LOG_PTAB_SIZE)) \
| ((IDX) << MD_LOG_PAGE_SIZE))
/* locate host page for virtual address ADDR, returns NULL if unallocated */
#define MEM_PAGE(MEM, ADDR) \
(/* first attempt to hit in first entry, otherwise call xlation fn */ \
((MEM)->ptab[MEM_PTAB_SET(ADDR)] \
&& (MEM)->ptab[MEM_PTAB_SET(ADDR)]->tag == MEM_PTAB_TAG(ADDR)) \
? (/* hit - return the page address on host */ \
(MEM)->ptab_accesses++, \
(MEM)->ptab[MEM_PTAB_SET(ADDR)]->page) \
: (/* first level miss - call the translation helper function */ \
mem_translate((MEM), (ADDR))))
/* compute address of access within a host page */
#define MEM_OFFSET(ADDR) ((ADDR) & (MD_PAGE_SIZE - 1))
/* memory tickle function, allocates pages when they are first written */
#define MEM_TICKLE(MEM, ADDR) \
(!MEM_PAGE(MEM, ADDR) \
? (/* allocate page at address ADDR */ \
mem_newpage(MEM, ADDR)) \
: (/* nada... */ (void)0))
/* memory page iterator */
#define MEM_FORALL(MEM, ITER, PTE) \
for ((ITER)=0; (ITER) < MEM_PTAB_SIZE; (ITER)++) \
for ((PTE)=(MEM)->ptab[i]; (PTE) != NULL; (PTE)=(PTE)->next)
/*
* memory accessors macros, fast but difficult to debug...
*/
/* safe version, works only with scalar types */
/* FIXME: write a more efficient GNU C expression for this... */
#define MEM_READ(MEM, ADDR, TYPE) \
(MEM_PAGE(MEM, (md_addr_t)(ADDR)) \
? *((TYPE *)(MEM_PAGE(MEM, (md_addr_t)(ADDR)) + MEM_OFFSET(ADDR))) \
: /* page not yet allocated, return zero value */ 0)
/* unsafe version, works with any type */
#define __UNCHK_MEM_READ(MEM, ADDR, TYPE) \
(*((TYPE *)(MEM_PAGE(MEM, (md_addr_t)(ADDR)) + MEM_OFFSET(ADDR))))
/* safe version, works only with scalar types */
/* FIXME: write a more efficient GNU C expression for this... */
#define MEM_WRITE(MEM, ADDR, TYPE, VAL) \
(MEM_TICKLE(MEM, (md_addr_t)(ADDR)), \
*((TYPE *)(MEM_PAGE(MEM, (md_addr_t)(ADDR)) + MEM_OFFSET(ADDR))) = (VAL))
/* unsafe version, works with any type */
#define __UNCHK_MEM_WRITE(MEM, ADDR, TYPE, VAL) \
(*((TYPE *)(MEM_PAGE(MEM, (md_addr_t)(ADDR)) + MEM_OFFSET(ADDR))) = (VAL))
/* fast memory accessor macros, typed versions */
#define MEM_READ_BYTE(MEM, ADDR) MEM_READ(MEM, ADDR, byte_t)
#define MEM_READ_SBYTE(MEM, ADDR) MEM_READ(MEM, ADDR, sbyte_t)
#define MEM_READ_HALF(MEM, ADDR) MD_SWAPH(MEM_READ(MEM, ADDR, half_t))
#define MEM_READ_SHALF(MEM, ADDR) MD_SWAPH(MEM_READ(MEM, ADDR, shalf_t))
#define MEM_READ_WORD(MEM, ADDR) MD_SWAPW(MEM_READ(MEM, ADDR, word_t))
#define MEM_READ_SWORD(MEM, ADDR) MD_SWAPW(MEM_READ(MEM, ADDR, sword_t))
#ifdef HOST_HAS_QWORD
#define MEM_READ_QWORD(MEM, ADDR) MD_SWAPQ(MEM_READ(MEM, ADDR, qword_t))
#define MEM_READ_SQWORD(MEM, ADDR) MD_SWAPQ(MEM_READ(MEM, ADDR, sqword_t))
#endif /* HOST_HAS_QWORD */
#define MEM_WRITE_BYTE(MEM, ADDR, VAL) MEM_WRITE(MEM, ADDR, byte_t, VAL)
#define MEM_WRITE_SBYTE(MEM, ADDR, VAL) MEM_WRITE(MEM, ADDR, sbyte_t, VAL)
#define MEM_WRITE_HALF(MEM, ADDR, VAL) \
MEM_WRITE(MEM, ADDR, half_t, MD_SWAPH(VAL))
#define MEM_WRITE_SHALF(MEM, ADDR, VAL) \
MEM_WRITE(MEM, ADDR, shalf_t, MD_SWAPH(VAL))
#define MEM_WRITE_WORD(MEM, ADDR, VAL) \
MEM_WRITE(MEM, ADDR, word_t, MD_SWAPW(VAL))
#define MEM_WRITE_SWORD(MEM, ADDR, VAL) \
MEM_WRITE(MEM, ADDR, sword_t, MD_SWAPW(VAL))
#define MEM_WRITE_SFLOAT(MEM, ADDR, VAL) \
MEM_WRITE(MEM, ADDR, sfloat_t, MD_SWAPW(VAL))
#define MEM_WRITE_DFLOAT(MEM, ADDR, VAL) \
MEM_WRITE(MEM, ADDR, dfloat_t, MD_SWAPQ(VAL))
#ifdef HOST_HAS_QWORD
#define MEM_WRITE_QWORD(MEM, ADDR, VAL) \
MEM_WRITE(MEM, ADDR, qword_t, MD_SWAPQ(VAL))
#define MEM_WRITE_SQWORD(MEM, ADDR, VAL) \
MEM_WRITE(MEM, ADDR, sqword_t, MD_SWAPQ(VAL))
#endif /* HOST_HAS_QWORD */
/* create a flat memory space */
struct mem_t *
mem_create(char *name); /* name of the memory space */
/* translate address ADDR in memory space MEM, returns pointer to host page */
byte_t *
mem_translate(struct mem_t *mem, /* memory space to access */
md_addr_t addr); /* virtual address to translate */
/* allocate a memory page */
void
mem_newpage(struct mem_t *mem, /* memory space to allocate in */
md_addr_t addr); /* virtual address to allocate */
/* generic memory access function, it's safe because alignments and permissions
are checked, handles any natural transfer sizes; note, faults out if nbytes
is not a power-of-two or larger then MD_PAGE_SIZE */
enum md_fault_type
mem_access(struct mem_t *mem, /* memory space to access */
enum mem_cmd cmd, /* Read (from sim mem) or Write */
md_addr_t addr, /* target address to access */
void *vp, /* host memory address to access */
int nbytes); /* number of bytes to access */
/* register memory system-specific statistics */
void
mem_reg_stats(struct mem_t *mem, /* memory space to declare */
struct stat_sdb_t *sdb); /* stats data base */
/* initialize memory system, call before loader.c */
void
mem_init(struct mem_t *mem); /* memory space to initialize */
/* dump a block of memory, returns any faults encountered */
enum md_fault_type
mem_dump(struct mem_t *mem, /* memory space to display */
md_addr_t addr, /* target address to dump */
int len, /* number bytes to dump */
FILE *stream); /* output stream */
/*
* memory accessor routines, these routines require a memory access function
* definition to access memory, the memory access function provides a "hook"
* for programs to instrument memory accesses, this is used by various
* simulators for various reasons; for the default operation - direct access
* to the memory system, pass mem_access() as the memory access function
*/
/* copy a '\0' terminated string to/from simulated memory space, returns
the number of bytes copied, returns any fault encountered */
enum md_fault_type
mem_strcpy(mem_access_fn mem_fn, /* user-specified memory accessor */
struct mem_t *mem, /* memory space to access */
enum mem_cmd cmd, /* Read (from sim mem) or Write */
md_addr_t addr, /* target address to access */
char *s); /* host memory string buffer */
/* copy NBYTES to/from simulated memory space, returns any faults */
enum md_fault_type
mem_bcopy(mem_access_fn mem_fn, /* user-specified memory accessor */
struct mem_t *mem, /* memory space to access */
enum mem_cmd cmd, /* Read (from sim mem) or Write */
md_addr_t addr, /* target address to access */
void *vp, /* host memory address to access */
int nbytes); /* number of bytes to access */
/* copy NBYTES to/from simulated memory space, NBYTES must be a multiple
of 4 bytes, this function is faster than mem_bcopy(), returns any
faults encountered */
enum md_fault_type
mem_bcopy4(mem_access_fn mem_fn, /* user-specified memory accessor */
struct mem_t *mem, /* memory space to access */
enum mem_cmd cmd, /* Read (from sim mem) or Write */
md_addr_t addr, /* target address to access */
void *vp, /* host memory address to access */
int nbytes); /* number of bytes to access */
/* zero out NBYTES of simulated memory, returns any faults encountered */
enum md_fault_type
mem_bzero(mem_access_fn mem_fn, /* user-specified memory accessor */
struct mem_t *mem, /* memory space to access */
md_addr_t addr, /* target address to access */
int nbytes); /* number of bytes to clear */
#endif /* MEMORY_H */