1 #ifndef _library__memoryspace__hpp__included__
2 #define _library__memoryspace__hpp__included__
9 #include "threadtypes.hpp"
12 * Information about region of memory.
19 virtual ~memory_region() throw();
21 * Name of the region (mainly for debugging and showing to user).
25 * Base address of the region.
33 * Last address in region.
35 uint64_t last_address() const throw() { return base
+ size
- 1; }
37 * Endianess of region (-1 => little, 0 => host, 1 => big).
47 * Signals that this region is not RAM/ROM-like, but is special I/O region where reads and writes might not be
52 * Direct mapping for the region. If not NULL, read/write will not be used, instead all operations directly
53 * manipulate this buffer (faster). Must be NULL for special regions.
55 unsigned char* direct_map
;
59 * Parameter offset: Offset to start the read.
60 * Parameter buffer: Buffer to store the data to.
61 * Parameter tsize: Amount to read.
63 * Note: The default implementation reads from direct_map.
64 * Note: Needs to be overridden if direct_map is NULL.
65 * Note: Must be able to read the whole region at once.
67 virtual void read(uint64_t offset
, void* buffer
, size_t tsize
);
69 * Write to region (writes to readonly regions are ignored).
71 * Parameter offset: Offset to start the write.
72 * Parameter buffer: Buffer to read the data from.
73 * Parameter tsize: Amount to write.
74 * Returns: True on success, false on failure.
76 * Note: The default implementation writes to direct_map if available and readwrite. Otherwise fails.
77 * Note: Must be able to write the whole region at once.
79 virtual bool write(uint64_t offset
, const void* buffer
, size_t tsize
);
83 * Direct-mapped region.
85 struct memory_region_direct
: public memory_region
88 * Create new direct-mapped region.
90 * Parameter name: Name of the region.
91 * Parameter base: Base address of the region.
92 * Parameter endian: Endianess of the region.
93 * Parameter memory: Memory backing the region.
94 * Parameter size: Size of the region.
95 * Parameter _readonly: If true, region is readonly.
97 memory_region_direct(const std::string
& name
, uint64_t base
, int endian
, unsigned char* memory
,
98 size_t size
, bool _readonly
= false);
102 ~memory_region_direct() throw();
106 * A whole memory space.
112 * Get system endianess.
114 #if (defined(__i386__) || defined(__x86_64__))
115 static int get_system_endian() throw() { return -1; }
117 static int get_system_endian() throw() { if(!sysendian
) sysendian
= _get_system_endian(); return sysendian
; }
120 * Do native unaligned reads work?
122 #if (defined(__i386__) || defined(__x86_64__))
123 static int can_read_unaligned() throw() { return true; }
125 static int can_read_unaligned() throw() { return false; }
128 * Lookup region covering address.
130 * Parameter address: The address to look up.
131 * Returns: The region/offset pair, or NULL/0 if that address is unmapped.
133 std::pair
<memory_region
*, uint64_t> lookup(uint64_t address
);
135 * Lookup region covering linear address.
137 * Parameter linear: The linear address to look up.
138 * Returns: The region/offset pair, or NULL/0 if that address is unmapped.
140 std::pair
<memory_region
*, uint64_t> lookup_linear(uint64_t linear
);
142 * Lookup region with specified index..
144 * Parameter n: The index to look up.
145 * Returns: The region, or NULL if index is invalid.
147 memory_region
* lookup_n(size_t n
);
149 * Get number of regions.
151 size_t get_region_count() { return u_regions
.size(); }
153 * Get linear RAM size.
155 * Returns: The linear RAM size in bytes.
157 uint64_t get_linear_size() { return linear_size
; }
159 * Get list of all regions in memory space.
161 std::list
<memory_region
*> get_regions();
163 * Set list of all regions in memory space.
165 void set_regions(const std::list
<memory_region
*>& regions
);
167 * Read an element (primitive type) from memory.
169 * Parameter address: The address to read.
170 * Returns: The read value.
172 template<typename T
> T
read(uint64_t address
);
174 * Write an element (primitive type) to memory.
176 * Parameter address: The address to write.
177 * Parameter value: The value to write.
178 * Returns: True on success, false on failure.
180 template<typename T
> bool write(uint64_t address
, T value
);
182 * Read a byte range (not across regions).
184 * Parameter address: Base address to start the read from.
185 * Parameter buffer: Buffer to store the data to.
186 * Parameter bsize: Size of buffer.
188 void read_range(uint64_t address
, void* buffer
, size_t bsize
);
190 * Write a byte range (not across regions).
192 * Parameter address: Base address to start the write from.
193 * Parameter buffer: Buffer to read the data from.
194 * Parameter bsize: Size of buffer.
195 * Returns: True on success, false on failure.
197 bool write_range(uint64_t address
, const void* buffer
, size_t bsize
);
199 * Read an element (primitive type) from memory.
201 * Parameter linear: The linear address to read.
202 * Returns: The read value.
204 template<typename T
> T
read_linear(uint64_t linear
);
206 * Write an element (primitive type) to memory.
208 * Parameter linear: The linear address to write.
209 * Parameter value: The value to write.
210 * Returns: True on success, false on failure.
212 template<typename T
> bool write_linear(uint64_t linear
, T value
);
214 * Read a byte range (not across regions).
216 * Parameter linear: Base linear address to start the read from.
217 * Parameter buffer: Buffer to store the data to.
218 * Parameter bsize: Size of buffer.
220 void read_range_linear(uint64_t linear
, void* buffer
, size_t bsize
);
222 * Write a byte range (not across regions).
224 * Parameter linear: Base linear address to start the write from.
225 * Parameter buffer: Buffer to read the data from.
226 * Parameter bsize: Size of buffer.
227 * Returns: True on success, false on failure.
229 bool write_range_linear(uint64_t linear
, const void* buffer
, size_t bsize
);
231 * Read complete linear memory.
233 * Parameter buffer: Buffer to store to (get_linear_size() bytes).
235 void read_all_linear_memory(uint8_t* buffer
);
238 std::vector
<memory_region
*> u_regions
;
239 std::vector
<memory_region
*> u_lregions
;
240 std::vector
<uint64_t> linear_bases
;
241 uint64_t linear_size
;
242 static int _get_system_endian();
243 static int sysendian
;