]>
Commit | Line | Data |
---|---|---|
093bc2cd AK |
1 | /* |
2 | * Physical memory management API | |
3 | * | |
4 | * Copyright 2011 Red Hat, Inc. and/or its affiliates | |
5 | * | |
6 | * Authors: | |
7 | * Avi Kivity <avi@redhat.com> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
10 | * the COPYING file in the top-level directory. | |
11 | * | |
12 | */ | |
13 | ||
14 | #ifndef MEMORY_H | |
15 | #define MEMORY_H | |
16 | ||
17 | #ifndef CONFIG_USER_ONLY | |
18 | ||
19 | #include <stdint.h> | |
20 | #include <stdbool.h> | |
21 | #include "qemu-common.h" | |
022c62cb PB |
22 | #include "exec/cpu-common.h" |
23 | #include "exec/hwaddr.h" | |
1de7afc9 | 24 | #include "qemu/queue.h" |
022c62cb PB |
25 | #include "exec/iorange.h" |
26 | #include "exec/ioport.h" | |
1de7afc9 | 27 | #include "qemu/int128.h" |
093bc2cd AK |
28 | |
29 | typedef struct MemoryRegionOps MemoryRegionOps; | |
627a0e90 | 30 | typedef struct MemoryRegionPortio MemoryRegionPortio; |
74901c3b | 31 | typedef struct MemoryRegionMmio MemoryRegionMmio; |
093bc2cd AK |
32 | |
33 | /* Must match *_DIRTY_FLAGS in cpu-all.h. To be replaced with dynamic | |
34 | * registration. | |
35 | */ | |
36 | #define DIRTY_MEMORY_VGA 0 | |
37 | #define DIRTY_MEMORY_CODE 1 | |
38 | #define DIRTY_MEMORY_MIGRATION 3 | |
39 | ||
74901c3b AK |
40 | struct MemoryRegionMmio { |
41 | CPUReadMemoryFunc *read[3]; | |
42 | CPUWriteMemoryFunc *write[3]; | |
43 | }; | |
44 | ||
a2d33521 AK |
45 | /* Internal use; thunks between old-style IORange and MemoryRegions. */ |
46 | typedef struct MemoryRegionIORange MemoryRegionIORange; | |
47 | struct MemoryRegionIORange { | |
48 | IORange iorange; | |
49 | MemoryRegion *mr; | |
a8170e5e | 50 | hwaddr offset; |
a2d33521 AK |
51 | }; |
52 | ||
093bc2cd AK |
53 | /* |
54 | * Memory region callbacks | |
55 | */ | |
56 | struct MemoryRegionOps { | |
57 | /* Read from the memory region. @addr is relative to @mr; @size is | |
58 | * in bytes. */ | |
59 | uint64_t (*read)(void *opaque, | |
a8170e5e | 60 | hwaddr addr, |
093bc2cd AK |
61 | unsigned size); |
62 | /* Write to the memory region. @addr is relative to @mr; @size is | |
63 | * in bytes. */ | |
64 | void (*write)(void *opaque, | |
a8170e5e | 65 | hwaddr addr, |
093bc2cd AK |
66 | uint64_t data, |
67 | unsigned size); | |
68 | ||
69 | enum device_endian endianness; | |
70 | /* Guest-visible constraints: */ | |
71 | struct { | |
72 | /* If nonzero, specify bounds on access sizes beyond which a machine | |
73 | * check is thrown. | |
74 | */ | |
75 | unsigned min_access_size; | |
76 | unsigned max_access_size; | |
77 | /* If true, unaligned accesses are supported. Otherwise unaligned | |
78 | * accesses throw machine checks. | |
79 | */ | |
80 | bool unaligned; | |
897fa7cf AK |
81 | /* |
82 | * If present, and returns #false, the transaction is not accepted | |
83 | * by the device (and results in machine dependent behaviour such | |
84 | * as a machine check exception). | |
85 | */ | |
a8170e5e | 86 | bool (*accepts)(void *opaque, hwaddr addr, |
897fa7cf | 87 | unsigned size, bool is_write); |
093bc2cd AK |
88 | } valid; |
89 | /* Internal implementation constraints: */ | |
90 | struct { | |
91 | /* If nonzero, specifies the minimum size implemented. Smaller sizes | |
92 | * will be rounded upwards and a partial result will be returned. | |
93 | */ | |
94 | unsigned min_access_size; | |
95 | /* If nonzero, specifies the maximum size implemented. Larger sizes | |
96 | * will be done as a series of accesses with smaller sizes. | |
97 | */ | |
98 | unsigned max_access_size; | |
99 | /* If true, unaligned accesses are supported. Otherwise all accesses | |
100 | * are converted to (possibly multiple) naturally aligned accesses. | |
101 | */ | |
102 | bool unaligned; | |
103 | } impl; | |
627a0e90 AK |
104 | |
105 | /* If .read and .write are not present, old_portio may be used for | |
106 | * backwards compatibility with old portio registration | |
107 | */ | |
108 | const MemoryRegionPortio *old_portio; | |
74901c3b AK |
109 | /* If .read and .write are not present, old_mmio may be used for |
110 | * backwards compatibility with old mmio registration | |
111 | */ | |
112 | const MemoryRegionMmio old_mmio; | |
093bc2cd AK |
113 | }; |
114 | ||
115 | typedef struct CoalescedMemoryRange CoalescedMemoryRange; | |
3e9d69e7 | 116 | typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd; |
093bc2cd AK |
117 | |
118 | struct MemoryRegion { | |
119 | /* All fields are private - violators will be prosecuted */ | |
120 | const MemoryRegionOps *ops; | |
121 | void *opaque; | |
122 | MemoryRegion *parent; | |
08dafab4 | 123 | Int128 size; |
a8170e5e | 124 | hwaddr addr; |
545e92e0 | 125 | void (*destructor)(MemoryRegion *mr); |
093bc2cd | 126 | ram_addr_t ram_addr; |
b3b00c78 | 127 | bool subpage; |
14a3c10a | 128 | bool terminates; |
d0a9b5bc | 129 | bool readable; |
8ea9252a | 130 | bool ram; |
fb1cd6f9 | 131 | bool readonly; /* For RAM regions */ |
6bba19ba | 132 | bool enabled; |
75c578dc | 133 | bool rom_device; |
1660e72d | 134 | bool warning_printed; /* For reservations */ |
d410515e | 135 | bool flush_coalesced_mmio; |
093bc2cd | 136 | MemoryRegion *alias; |
a8170e5e | 137 | hwaddr alias_offset; |
093bc2cd AK |
138 | unsigned priority; |
139 | bool may_overlap; | |
140 | QTAILQ_HEAD(subregions, MemoryRegion) subregions; | |
141 | QTAILQ_ENTRY(MemoryRegion) subregions_link; | |
142 | QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced; | |
143 | const char *name; | |
5a583347 | 144 | uint8_t dirty_log_mask; |
3e9d69e7 AK |
145 | unsigned ioeventfd_nb; |
146 | MemoryRegionIoeventfd *ioeventfds; | |
093bc2cd AK |
147 | }; |
148 | ||
627a0e90 AK |
149 | struct MemoryRegionPortio { |
150 | uint32_t offset; | |
151 | uint32_t len; | |
152 | unsigned size; | |
153 | IOPortReadFunc *read; | |
154 | IOPortWriteFunc *write; | |
155 | }; | |
156 | ||
2dd30228 | 157 | #define PORTIO_END_OF_LIST() { } |
627a0e90 | 158 | |
9ad2bbc1 AK |
159 | /** |
160 | * AddressSpace: describes a mapping of addresses to #MemoryRegion objects | |
161 | */ | |
162 | struct AddressSpace { | |
163 | /* All fields are private. */ | |
0d673e36 | 164 | const char *name; |
9ad2bbc1 AK |
165 | MemoryRegion *root; |
166 | struct FlatView *current_map; | |
167 | int ioeventfd_nb; | |
168 | struct MemoryRegionIoeventfd *ioeventfds; | |
ac1970fb | 169 | struct AddressSpaceDispatch *dispatch; |
0d673e36 | 170 | QTAILQ_ENTRY(AddressSpace) address_spaces_link; |
9ad2bbc1 AK |
171 | }; |
172 | ||
e2177955 AK |
173 | /** |
174 | * MemoryRegionSection: describes a fragment of a #MemoryRegion | |
175 | * | |
176 | * @mr: the region, or %NULL if empty | |
7664e80c | 177 | * @address_space: the address space the region is mapped in |
e2177955 AK |
178 | * @offset_within_region: the beginning of the section, relative to @mr's start |
179 | * @size: the size of the section; will not exceed @mr's boundaries | |
180 | * @offset_within_address_space: the address of the first byte of the section | |
181 | * relative to the region's address space | |
7a8499e8 | 182 | * @readonly: writes to this section are ignored |
e2177955 AK |
183 | */ |
184 | struct MemoryRegionSection { | |
185 | MemoryRegion *mr; | |
f6790af6 | 186 | AddressSpace *address_space; |
a8170e5e | 187 | hwaddr offset_within_region; |
e2177955 | 188 | uint64_t size; |
a8170e5e | 189 | hwaddr offset_within_address_space; |
7a8499e8 | 190 | bool readonly; |
e2177955 AK |
191 | }; |
192 | ||
7664e80c AK |
193 | typedef struct MemoryListener MemoryListener; |
194 | ||
195 | /** | |
196 | * MemoryListener: callbacks structure for updates to the physical memory map | |
197 | * | |
198 | * Allows a component to adjust to changes in the guest-visible memory map. | |
199 | * Use with memory_listener_register() and memory_listener_unregister(). | |
200 | */ | |
201 | struct MemoryListener { | |
50c1e149 AK |
202 | void (*begin)(MemoryListener *listener); |
203 | void (*commit)(MemoryListener *listener); | |
7664e80c AK |
204 | void (*region_add)(MemoryListener *listener, MemoryRegionSection *section); |
205 | void (*region_del)(MemoryListener *listener, MemoryRegionSection *section); | |
50c1e149 | 206 | void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section); |
7664e80c AK |
207 | void (*log_start)(MemoryListener *listener, MemoryRegionSection *section); |
208 | void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section); | |
209 | void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section); | |
210 | void (*log_global_start)(MemoryListener *listener); | |
211 | void (*log_global_stop)(MemoryListener *listener); | |
80a1ea37 | 212 | void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section, |
753d5e14 | 213 | bool match_data, uint64_t data, EventNotifier *e); |
80a1ea37 | 214 | void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section, |
753d5e14 | 215 | bool match_data, uint64_t data, EventNotifier *e); |
95d2994a | 216 | void (*coalesced_mmio_add)(MemoryListener *listener, MemoryRegionSection *section, |
a8170e5e | 217 | hwaddr addr, hwaddr len); |
95d2994a | 218 | void (*coalesced_mmio_del)(MemoryListener *listener, MemoryRegionSection *section, |
a8170e5e | 219 | hwaddr addr, hwaddr len); |
72e22d2f AK |
220 | /* Lower = earlier (during add), later (during del) */ |
221 | unsigned priority; | |
f6790af6 | 222 | AddressSpace *address_space_filter; |
72e22d2f | 223 | QTAILQ_ENTRY(MemoryListener) link; |
7664e80c AK |
224 | }; |
225 | ||
093bc2cd AK |
226 | /** |
227 | * memory_region_init: Initialize a memory region | |
228 | * | |
69ddaf66 | 229 | * The region typically acts as a container for other memory regions. Use |
093bc2cd AK |
230 | * memory_region_add_subregion() to add subregions. |
231 | * | |
232 | * @mr: the #MemoryRegion to be initialized | |
233 | * @name: used for debugging; not visible to the user or ABI | |
234 | * @size: size of the region; any subregions beyond this size will be clipped | |
235 | */ | |
236 | void memory_region_init(MemoryRegion *mr, | |
237 | const char *name, | |
238 | uint64_t size); | |
239 | /** | |
240 | * memory_region_init_io: Initialize an I/O memory region. | |
241 | * | |
69ddaf66 | 242 | * Accesses into the region will cause the callbacks in @ops to be called. |
093bc2cd AK |
243 | * if @size is nonzero, subregions will be clipped to @size. |
244 | * | |
245 | * @mr: the #MemoryRegion to be initialized. | |
246 | * @ops: a structure containing read and write callbacks to be used when | |
247 | * I/O is performed on the region. | |
248 | * @opaque: passed to to the read and write callbacks of the @ops structure. | |
249 | * @name: used for debugging; not visible to the user or ABI | |
250 | * @size: size of the region. | |
251 | */ | |
252 | void memory_region_init_io(MemoryRegion *mr, | |
253 | const MemoryRegionOps *ops, | |
254 | void *opaque, | |
255 | const char *name, | |
256 | uint64_t size); | |
257 | ||
258 | /** | |
259 | * memory_region_init_ram: Initialize RAM memory region. Accesses into the | |
69ddaf66 | 260 | * region will modify memory directly. |
093bc2cd AK |
261 | * |
262 | * @mr: the #MemoryRegion to be initialized. | |
c5705a77 | 263 | * @name: the name of the region. |
093bc2cd AK |
264 | * @size: size of the region. |
265 | */ | |
266 | void memory_region_init_ram(MemoryRegion *mr, | |
093bc2cd AK |
267 | const char *name, |
268 | uint64_t size); | |
269 | ||
270 | /** | |
1a7e8cae BZ |
271 | * memory_region_init_ram_ptr: Initialize RAM memory region from a |
272 | * user-provided pointer. Accesses into the | |
273 | * region will modify memory directly. | |
093bc2cd AK |
274 | * |
275 | * @mr: the #MemoryRegion to be initialized. | |
c5705a77 | 276 | * @name: the name of the region. |
093bc2cd AK |
277 | * @size: size of the region. |
278 | * @ptr: memory to be mapped; must contain at least @size bytes. | |
279 | */ | |
280 | void memory_region_init_ram_ptr(MemoryRegion *mr, | |
093bc2cd AK |
281 | const char *name, |
282 | uint64_t size, | |
283 | void *ptr); | |
284 | ||
285 | /** | |
286 | * memory_region_init_alias: Initialize a memory region that aliases all or a | |
287 | * part of another memory region. | |
288 | * | |
289 | * @mr: the #MemoryRegion to be initialized. | |
290 | * @name: used for debugging; not visible to the user or ABI | |
291 | * @orig: the region to be referenced; @mr will be equivalent to | |
292 | * @orig between @offset and @offset + @size - 1. | |
293 | * @offset: start of the section in @orig to be referenced. | |
294 | * @size: size of the region. | |
295 | */ | |
296 | void memory_region_init_alias(MemoryRegion *mr, | |
297 | const char *name, | |
298 | MemoryRegion *orig, | |
a8170e5e | 299 | hwaddr offset, |
093bc2cd | 300 | uint64_t size); |
d0a9b5bc AK |
301 | |
302 | /** | |
303 | * memory_region_init_rom_device: Initialize a ROM memory region. Writes are | |
304 | * handled via callbacks. | |
305 | * | |
306 | * @mr: the #MemoryRegion to be initialized. | |
307 | * @ops: callbacks for write access handling. | |
c5705a77 | 308 | * @name: the name of the region. |
d0a9b5bc AK |
309 | * @size: size of the region. |
310 | */ | |
311 | void memory_region_init_rom_device(MemoryRegion *mr, | |
312 | const MemoryRegionOps *ops, | |
75f5941c | 313 | void *opaque, |
d0a9b5bc AK |
314 | const char *name, |
315 | uint64_t size); | |
316 | ||
1660e72d JK |
317 | /** |
318 | * memory_region_init_reservation: Initialize a memory region that reserves | |
319 | * I/O space. | |
320 | * | |
321 | * A reservation region primariy serves debugging purposes. It claims I/O | |
322 | * space that is not supposed to be handled by QEMU itself. Any access via | |
323 | * the memory API will cause an abort(). | |
324 | * | |
325 | * @mr: the #MemoryRegion to be initialized | |
326 | * @name: used for debugging; not visible to the user or ABI | |
327 | * @size: size of the region. | |
328 | */ | |
329 | void memory_region_init_reservation(MemoryRegion *mr, | |
330 | const char *name, | |
331 | uint64_t size); | |
093bc2cd | 332 | /** |
69ddaf66 | 333 | * memory_region_destroy: Destroy a memory region and reclaim all resources. |
093bc2cd AK |
334 | * |
335 | * @mr: the region to be destroyed. May not currently be a subregion | |
336 | * (see memory_region_add_subregion()) or referenced in an alias | |
337 | * (see memory_region_init_alias()). | |
338 | */ | |
339 | void memory_region_destroy(MemoryRegion *mr); | |
340 | ||
341 | /** | |
342 | * memory_region_size: get a memory region's size. | |
343 | * | |
344 | * @mr: the memory region being queried. | |
345 | */ | |
346 | uint64_t memory_region_size(MemoryRegion *mr); | |
347 | ||
8ea9252a AK |
348 | /** |
349 | * memory_region_is_ram: check whether a memory region is random access | |
350 | * | |
351 | * Returns %true is a memory region is random access. | |
352 | * | |
353 | * @mr: the memory region being queried | |
354 | */ | |
355 | bool memory_region_is_ram(MemoryRegion *mr); | |
356 | ||
fd062573 BS |
357 | /** |
358 | * memory_region_is_romd: check whether a memory region is ROMD | |
359 | * | |
360 | * Returns %true is a memory region is ROMD and currently set to allow | |
361 | * direct reads. | |
362 | * | |
363 | * @mr: the memory region being queried | |
364 | */ | |
365 | static inline bool memory_region_is_romd(MemoryRegion *mr) | |
366 | { | |
367 | return mr->rom_device && mr->readable; | |
368 | } | |
369 | ||
8991c79b AK |
370 | /** |
371 | * memory_region_name: get a memory region's name | |
372 | * | |
373 | * Returns the string that was used to initialize the memory region. | |
374 | * | |
375 | * @mr: the memory region being queried | |
376 | */ | |
377 | const char *memory_region_name(MemoryRegion *mr); | |
378 | ||
55043ba3 AK |
379 | /** |
380 | * memory_region_is_logging: return whether a memory region is logging writes | |
381 | * | |
382 | * Returns %true if the memory region is logging writes | |
383 | * | |
384 | * @mr: the memory region being queried | |
385 | */ | |
386 | bool memory_region_is_logging(MemoryRegion *mr); | |
387 | ||
ce7923da AK |
388 | /** |
389 | * memory_region_is_rom: check whether a memory region is ROM | |
390 | * | |
391 | * Returns %true is a memory region is read-only memory. | |
392 | * | |
393 | * @mr: the memory region being queried | |
394 | */ | |
395 | bool memory_region_is_rom(MemoryRegion *mr); | |
396 | ||
093bc2cd AK |
397 | /** |
398 | * memory_region_get_ram_ptr: Get a pointer into a RAM memory region. | |
399 | * | |
400 | * Returns a host pointer to a RAM memory region (created with | |
401 | * memory_region_init_ram() or memory_region_init_ram_ptr()). Use with | |
402 | * care. | |
403 | * | |
404 | * @mr: the memory region being queried. | |
405 | */ | |
406 | void *memory_region_get_ram_ptr(MemoryRegion *mr); | |
407 | ||
093bc2cd AK |
408 | /** |
409 | * memory_region_set_log: Turn dirty logging on or off for a region. | |
410 | * | |
411 | * Turns dirty logging on or off for a specified client (display, migration). | |
412 | * Only meaningful for RAM regions. | |
413 | * | |
414 | * @mr: the memory region being updated. | |
415 | * @log: whether dirty logging is to be enabled or disabled. | |
416 | * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or | |
417 | * %DIRTY_MEMORY_VGA. | |
418 | */ | |
419 | void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client); | |
420 | ||
421 | /** | |
cd7a45c9 BS |
422 | * memory_region_get_dirty: Check whether a range of bytes is dirty |
423 | * for a specified client. | |
093bc2cd | 424 | * |
cd7a45c9 | 425 | * Checks whether a range of bytes has been written to since the last |
093bc2cd AK |
426 | * call to memory_region_reset_dirty() with the same @client. Dirty logging |
427 | * must be enabled. | |
428 | * | |
429 | * @mr: the memory region being queried. | |
430 | * @addr: the address (relative to the start of the region) being queried. | |
cd7a45c9 | 431 | * @size: the size of the range being queried. |
093bc2cd AK |
432 | * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or |
433 | * %DIRTY_MEMORY_VGA. | |
434 | */ | |
a8170e5e AK |
435 | bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr, |
436 | hwaddr size, unsigned client); | |
093bc2cd AK |
437 | |
438 | /** | |
fd4aa979 | 439 | * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region. |
093bc2cd | 440 | * |
fd4aa979 BS |
441 | * Marks a range of bytes as dirty, after it has been dirtied outside |
442 | * guest code. | |
093bc2cd | 443 | * |
fd4aa979 | 444 | * @mr: the memory region being dirtied. |
093bc2cd | 445 | * @addr: the address (relative to the start of the region) being dirtied. |
fd4aa979 | 446 | * @size: size of the range being dirtied. |
093bc2cd | 447 | */ |
a8170e5e AK |
448 | void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr, |
449 | hwaddr size); | |
093bc2cd | 450 | |
6c279db8 JQ |
451 | /** |
452 | * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty | |
453 | * for a specified client. It clears them. | |
454 | * | |
455 | * Checks whether a range of bytes has been written to since the last | |
456 | * call to memory_region_reset_dirty() with the same @client. Dirty logging | |
457 | * must be enabled. | |
458 | * | |
459 | * @mr: the memory region being queried. | |
460 | * @addr: the address (relative to the start of the region) being queried. | |
461 | * @size: the size of the range being queried. | |
462 | * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or | |
463 | * %DIRTY_MEMORY_VGA. | |
464 | */ | |
465 | bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr, | |
466 | hwaddr size, unsigned client); | |
093bc2cd AK |
467 | /** |
468 | * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with | |
469 | * any external TLBs (e.g. kvm) | |
470 | * | |
471 | * Flushes dirty information from accelerators such as kvm and vhost-net | |
472 | * and makes it available to users of the memory API. | |
473 | * | |
474 | * @mr: the region being flushed. | |
475 | */ | |
476 | void memory_region_sync_dirty_bitmap(MemoryRegion *mr); | |
477 | ||
478 | /** | |
479 | * memory_region_reset_dirty: Mark a range of pages as clean, for a specified | |
480 | * client. | |
481 | * | |
482 | * Marks a range of pages as no longer dirty. | |
483 | * | |
484 | * @mr: the region being updated. | |
485 | * @addr: the start of the subrange being cleaned. | |
486 | * @size: the size of the subrange being cleaned. | |
487 | * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or | |
488 | * %DIRTY_MEMORY_VGA. | |
489 | */ | |
a8170e5e AK |
490 | void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr, |
491 | hwaddr size, unsigned client); | |
093bc2cd AK |
492 | |
493 | /** | |
494 | * memory_region_set_readonly: Turn a memory region read-only (or read-write) | |
495 | * | |
496 | * Allows a memory region to be marked as read-only (turning it into a ROM). | |
497 | * only useful on RAM regions. | |
498 | * | |
499 | * @mr: the region being updated. | |
500 | * @readonly: whether rhe region is to be ROM or RAM. | |
501 | */ | |
502 | void memory_region_set_readonly(MemoryRegion *mr, bool readonly); | |
503 | ||
d0a9b5bc AK |
504 | /** |
505 | * memory_region_rom_device_set_readable: enable/disable ROM readability | |
506 | * | |
507 | * Allows a ROM device (initialized with memory_region_init_rom_device() to | |
508 | * to be marked as readable (default) or not readable. When it is readable, | |
509 | * the device is mapped to guest memory. When not readable, reads are | |
510 | * forwarded to the #MemoryRegion.read function. | |
511 | * | |
512 | * @mr: the memory region to be updated | |
513 | * @readable: whether reads are satisified directly (%true) or via callbacks | |
514 | * (%false) | |
515 | */ | |
516 | void memory_region_rom_device_set_readable(MemoryRegion *mr, bool readable); | |
517 | ||
093bc2cd AK |
518 | /** |
519 | * memory_region_set_coalescing: Enable memory coalescing for the region. | |
520 | * | |
521 | * Enabled writes to a region to be queued for later processing. MMIO ->write | |
522 | * callbacks may be delayed until a non-coalesced MMIO is issued. | |
523 | * Only useful for IO regions. Roughly similar to write-combining hardware. | |
524 | * | |
525 | * @mr: the memory region to be write coalesced | |
526 | */ | |
527 | void memory_region_set_coalescing(MemoryRegion *mr); | |
528 | ||
529 | /** | |
530 | * memory_region_add_coalescing: Enable memory coalescing for a sub-range of | |
531 | * a region. | |
532 | * | |
533 | * Like memory_region_set_coalescing(), but works on a sub-range of a region. | |
534 | * Multiple calls can be issued coalesced disjoint ranges. | |
535 | * | |
536 | * @mr: the memory region to be updated. | |
537 | * @offset: the start of the range within the region to be coalesced. | |
538 | * @size: the size of the subrange to be coalesced. | |
539 | */ | |
540 | void memory_region_add_coalescing(MemoryRegion *mr, | |
a8170e5e | 541 | hwaddr offset, |
093bc2cd AK |
542 | uint64_t size); |
543 | ||
544 | /** | |
545 | * memory_region_clear_coalescing: Disable MMIO coalescing for the region. | |
546 | * | |
547 | * Disables any coalescing caused by memory_region_set_coalescing() or | |
548 | * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory | |
549 | * hardware. | |
550 | * | |
551 | * @mr: the memory region to be updated. | |
552 | */ | |
553 | void memory_region_clear_coalescing(MemoryRegion *mr); | |
554 | ||
d410515e JK |
555 | /** |
556 | * memory_region_set_flush_coalesced: Enforce memory coalescing flush before | |
557 | * accesses. | |
558 | * | |
559 | * Ensure that pending coalesced MMIO request are flushed before the memory | |
560 | * region is accessed. This property is automatically enabled for all regions | |
561 | * passed to memory_region_set_coalescing() and memory_region_add_coalescing(). | |
562 | * | |
563 | * @mr: the memory region to be updated. | |
564 | */ | |
565 | void memory_region_set_flush_coalesced(MemoryRegion *mr); | |
566 | ||
567 | /** | |
568 | * memory_region_clear_flush_coalesced: Disable memory coalescing flush before | |
569 | * accesses. | |
570 | * | |
571 | * Clear the automatic coalesced MMIO flushing enabled via | |
572 | * memory_region_set_flush_coalesced. Note that this service has no effect on | |
573 | * memory regions that have MMIO coalescing enabled for themselves. For them, | |
574 | * automatic flushing will stop once coalescing is disabled. | |
575 | * | |
576 | * @mr: the memory region to be updated. | |
577 | */ | |
578 | void memory_region_clear_flush_coalesced(MemoryRegion *mr); | |
579 | ||
3e9d69e7 AK |
580 | /** |
581 | * memory_region_add_eventfd: Request an eventfd to be triggered when a word | |
582 | * is written to a location. | |
583 | * | |
584 | * Marks a word in an IO region (initialized with memory_region_init_io()) | |
585 | * as a trigger for an eventfd event. The I/O callback will not be called. | |
69ddaf66 | 586 | * The caller must be prepared to handle failure (that is, take the required |
3e9d69e7 AK |
587 | * action if the callback _is_ called). |
588 | * | |
589 | * @mr: the memory region being updated. | |
590 | * @addr: the address within @mr that is to be monitored | |
591 | * @size: the size of the access to trigger the eventfd | |
592 | * @match_data: whether to match against @data, instead of just @addr | |
593 | * @data: the data to match against the guest write | |
594 | * @fd: the eventfd to be triggered when @addr, @size, and @data all match. | |
595 | **/ | |
596 | void memory_region_add_eventfd(MemoryRegion *mr, | |
a8170e5e | 597 | hwaddr addr, |
3e9d69e7 AK |
598 | unsigned size, |
599 | bool match_data, | |
600 | uint64_t data, | |
753d5e14 | 601 | EventNotifier *e); |
3e9d69e7 AK |
602 | |
603 | /** | |
69ddaf66 | 604 | * memory_region_del_eventfd: Cancel an eventfd. |
3e9d69e7 | 605 | * |
69ddaf66 ASRJ |
606 | * Cancels an eventfd trigger requested by a previous |
607 | * memory_region_add_eventfd() call. | |
3e9d69e7 AK |
608 | * |
609 | * @mr: the memory region being updated. | |
610 | * @addr: the address within @mr that is to be monitored | |
611 | * @size: the size of the access to trigger the eventfd | |
612 | * @match_data: whether to match against @data, instead of just @addr | |
613 | * @data: the data to match against the guest write | |
614 | * @fd: the eventfd to be triggered when @addr, @size, and @data all match. | |
615 | */ | |
616 | void memory_region_del_eventfd(MemoryRegion *mr, | |
a8170e5e | 617 | hwaddr addr, |
3e9d69e7 AK |
618 | unsigned size, |
619 | bool match_data, | |
620 | uint64_t data, | |
753d5e14 PB |
621 | EventNotifier *e); |
622 | ||
093bc2cd | 623 | /** |
69ddaf66 | 624 | * memory_region_add_subregion: Add a subregion to a container. |
093bc2cd | 625 | * |
69ddaf66 | 626 | * Adds a subregion at @offset. The subregion may not overlap with other |
093bc2cd AK |
627 | * subregions (except for those explicitly marked as overlapping). A region |
628 | * may only be added once as a subregion (unless removed with | |
629 | * memory_region_del_subregion()); use memory_region_init_alias() if you | |
630 | * want a region to be a subregion in multiple locations. | |
631 | * | |
632 | * @mr: the region to contain the new subregion; must be a container | |
633 | * initialized with memory_region_init(). | |
634 | * @offset: the offset relative to @mr where @subregion is added. | |
635 | * @subregion: the subregion to be added. | |
636 | */ | |
637 | void memory_region_add_subregion(MemoryRegion *mr, | |
a8170e5e | 638 | hwaddr offset, |
093bc2cd AK |
639 | MemoryRegion *subregion); |
640 | /** | |
1a7e8cae BZ |
641 | * memory_region_add_subregion_overlap: Add a subregion to a container |
642 | * with overlap. | |
093bc2cd | 643 | * |
69ddaf66 | 644 | * Adds a subregion at @offset. The subregion may overlap with other |
093bc2cd AK |
645 | * subregions. Conflicts are resolved by having a higher @priority hide a |
646 | * lower @priority. Subregions without priority are taken as @priority 0. | |
647 | * A region may only be added once as a subregion (unless removed with | |
648 | * memory_region_del_subregion()); use memory_region_init_alias() if you | |
649 | * want a region to be a subregion in multiple locations. | |
650 | * | |
651 | * @mr: the region to contain the new subregion; must be a container | |
652 | * initialized with memory_region_init(). | |
653 | * @offset: the offset relative to @mr where @subregion is added. | |
654 | * @subregion: the subregion to be added. | |
655 | * @priority: used for resolving overlaps; highest priority wins. | |
656 | */ | |
657 | void memory_region_add_subregion_overlap(MemoryRegion *mr, | |
a8170e5e | 658 | hwaddr offset, |
093bc2cd AK |
659 | MemoryRegion *subregion, |
660 | unsigned priority); | |
e34911c4 AK |
661 | |
662 | /** | |
663 | * memory_region_get_ram_addr: Get the ram address associated with a memory | |
664 | * region | |
665 | * | |
dabdf394 | 666 | * DO NOT USE THIS FUNCTION. This is a temporary workaround while the Xen |
e34911c4 AK |
667 | * code is being reworked. |
668 | */ | |
669 | ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr); | |
670 | ||
093bc2cd AK |
671 | /** |
672 | * memory_region_del_subregion: Remove a subregion. | |
673 | * | |
674 | * Removes a subregion from its container. | |
675 | * | |
676 | * @mr: the container to be updated. | |
677 | * @subregion: the region being removed; must be a current subregion of @mr. | |
678 | */ | |
679 | void memory_region_del_subregion(MemoryRegion *mr, | |
680 | MemoryRegion *subregion); | |
681 | ||
6bba19ba AK |
682 | /* |
683 | * memory_region_set_enabled: dynamically enable or disable a region | |
684 | * | |
685 | * Enables or disables a memory region. A disabled memory region | |
686 | * ignores all accesses to itself and its subregions. It does not | |
687 | * obscure sibling subregions with lower priority - it simply behaves as | |
688 | * if it was removed from the hierarchy. | |
689 | * | |
690 | * Regions default to being enabled. | |
691 | * | |
692 | * @mr: the region to be updated | |
693 | * @enabled: whether to enable or disable the region | |
694 | */ | |
695 | void memory_region_set_enabled(MemoryRegion *mr, bool enabled); | |
696 | ||
2282e1af AK |
697 | /* |
698 | * memory_region_set_address: dynamically update the address of a region | |
699 | * | |
700 | * Dynamically updates the address of a region, relative to its parent. | |
701 | * May be used on regions are currently part of a memory hierarchy. | |
702 | * | |
703 | * @mr: the region to be updated | |
704 | * @addr: new address, relative to parent region | |
705 | */ | |
a8170e5e | 706 | void memory_region_set_address(MemoryRegion *mr, hwaddr addr); |
2282e1af | 707 | |
4703359e AK |
708 | /* |
709 | * memory_region_set_alias_offset: dynamically update a memory alias's offset | |
710 | * | |
711 | * Dynamically updates the offset into the target region that an alias points | |
712 | * to, as if the fourth argument to memory_region_init_alias() has changed. | |
713 | * | |
714 | * @mr: the #MemoryRegion to be updated; should be an alias. | |
715 | * @offset: the new offset into the target memory region | |
716 | */ | |
717 | void memory_region_set_alias_offset(MemoryRegion *mr, | |
a8170e5e | 718 | hwaddr offset); |
4703359e | 719 | |
e2177955 | 720 | /** |
73034e9e PB |
721 | * memory_region_find: translate an address/size relative to a |
722 | * MemoryRegion into a #MemoryRegionSection. | |
e2177955 | 723 | * |
73034e9e PB |
724 | * Locates the first #MemoryRegion within @mr that overlaps the range |
725 | * given by @addr and @size. | |
e2177955 AK |
726 | * |
727 | * Returns a #MemoryRegionSection that describes a contiguous overlap. | |
728 | * It will have the following characteristics: | |
e2177955 AK |
729 | * .@size = 0 iff no overlap was found |
730 | * .@mr is non-%NULL iff an overlap was found | |
731 | * | |
73034e9e PB |
732 | * Remember that in the return value the @offset_within_region is |
733 | * relative to the returned region (in the .@mr field), not to the | |
734 | * @mr argument. | |
735 | * | |
736 | * Similarly, the .@offset_within_address_space is relative to the | |
737 | * address space that contains both regions, the passed and the | |
738 | * returned one. However, in the special case where the @mr argument | |
739 | * has no parent (and thus is the root of the address space), the | |
740 | * following will hold: | |
741 | * .@offset_within_address_space >= @addr | |
742 | * .@offset_within_address_space + .@size <= @addr + @size | |
743 | * | |
744 | * @mr: a MemoryRegion within which @addr is a relative address | |
745 | * @addr: start of the area within @as to be searched | |
e2177955 AK |
746 | * @size: size of the area to be searched |
747 | */ | |
73034e9e | 748 | MemoryRegionSection memory_region_find(MemoryRegion *mr, |
a8170e5e | 749 | hwaddr addr, uint64_t size); |
e2177955 | 750 | |
fd062573 BS |
751 | /** |
752 | * memory_region_section_addr: get offset within MemoryRegionSection | |
753 | * | |
754 | * Returns offset within MemoryRegionSection | |
755 | * | |
756 | * @section: the memory region section being queried | |
757 | * @addr: address in address space | |
758 | */ | |
a8170e5e | 759 | static inline hwaddr |
fd062573 | 760 | memory_region_section_addr(MemoryRegionSection *section, |
a8170e5e | 761 | hwaddr addr) |
fd062573 BS |
762 | { |
763 | addr -= section->offset_within_address_space; | |
764 | addr += section->offset_within_region; | |
765 | return addr; | |
766 | } | |
86e775c6 AK |
767 | |
768 | /** | |
769 | * memory_global_sync_dirty_bitmap: synchronize the dirty log for all memory | |
770 | * | |
771 | * Synchronizes the dirty page log for an entire address space. | |
772 | * @address_space: a top-level (i.e. parentless) region that contains the | |
773 | * memory being synchronized | |
774 | */ | |
775 | void memory_global_sync_dirty_bitmap(MemoryRegion *address_space); | |
776 | ||
69ddaf66 ASRJ |
777 | /** |
778 | * memory_region_transaction_begin: Start a transaction. | |
779 | * | |
780 | * During a transaction, changes will be accumulated and made visible | |
dabdf394 | 781 | * only when the transaction ends (is committed). |
4ef4db86 AK |
782 | */ |
783 | void memory_region_transaction_begin(void); | |
69ddaf66 ASRJ |
784 | |
785 | /** | |
786 | * memory_region_transaction_commit: Commit a transaction and make changes | |
787 | * visible to the guest. | |
4ef4db86 AK |
788 | */ |
789 | void memory_region_transaction_commit(void); | |
790 | ||
7664e80c AK |
791 | /** |
792 | * memory_listener_register: register callbacks to be called when memory | |
793 | * sections are mapped or unmapped into an address | |
794 | * space | |
795 | * | |
796 | * @listener: an object containing the callbacks to be called | |
7376e582 | 797 | * @filter: if non-%NULL, only regions in this address space will be observed |
7664e80c | 798 | */ |
f6790af6 | 799 | void memory_listener_register(MemoryListener *listener, AddressSpace *filter); |
7664e80c AK |
800 | |
801 | /** | |
802 | * memory_listener_unregister: undo the effect of memory_listener_register() | |
803 | * | |
804 | * @listener: an object containing the callbacks to be removed | |
805 | */ | |
806 | void memory_listener_unregister(MemoryListener *listener); | |
807 | ||
808 | /** | |
809 | * memory_global_dirty_log_start: begin dirty logging for all regions | |
810 | */ | |
811 | void memory_global_dirty_log_start(void); | |
812 | ||
813 | /** | |
1a7e8cae | 814 | * memory_global_dirty_log_stop: end dirty logging for all regions |
7664e80c AK |
815 | */ |
816 | void memory_global_dirty_log_stop(void); | |
817 | ||
314e2987 BS |
818 | void mtree_info(fprintf_function mon_printf, void *f); |
819 | ||
9ad2bbc1 AK |
820 | /** |
821 | * address_space_init: initializes an address space | |
822 | * | |
823 | * @as: an uninitialized #AddressSpace | |
824 | * @root: a #MemoryRegion that routes addesses for the address space | |
825 | */ | |
826 | void address_space_init(AddressSpace *as, MemoryRegion *root); | |
827 | ||
83f3c251 AK |
828 | |
829 | /** | |
830 | * address_space_destroy: destroy an address space | |
831 | * | |
832 | * Releases all resources associated with an address space. After an address space | |
833 | * is destroyed, its root memory region (given by address_space_init()) may be destroyed | |
834 | * as well. | |
835 | * | |
836 | * @as: address space to be destroyed | |
837 | */ | |
838 | void address_space_destroy(AddressSpace *as); | |
839 | ||
ac1970fb AK |
840 | /** |
841 | * address_space_rw: read from or write to an address space. | |
842 | * | |
843 | * @as: #AddressSpace to be accessed | |
844 | * @addr: address within that address space | |
845 | * @buf: buffer with the data transferred | |
846 | * @is_write: indicates the transfer direction | |
847 | */ | |
a8170e5e | 848 | void address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf, |
ac1970fb AK |
849 | int len, bool is_write); |
850 | ||
851 | /** | |
852 | * address_space_write: write to address space. | |
853 | * | |
854 | * @as: #AddressSpace to be accessed | |
855 | * @addr: address within that address space | |
856 | * @buf: buffer with the data transferred | |
857 | */ | |
a8170e5e | 858 | void address_space_write(AddressSpace *as, hwaddr addr, |
ac1970fb AK |
859 | const uint8_t *buf, int len); |
860 | ||
861 | /** | |
862 | * address_space_read: read from an address space. | |
863 | * | |
864 | * @as: #AddressSpace to be accessed | |
865 | * @addr: address within that address space | |
866 | * @buf: buffer with the data transferred | |
867 | */ | |
a8170e5e | 868 | void address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len); |
ac1970fb AK |
869 | |
870 | /* address_space_map: map a physical memory region into a host virtual address | |
871 | * | |
872 | * May map a subset of the requested range, given by and returned in @plen. | |
873 | * May return %NULL if resources needed to perform the mapping are exhausted. | |
874 | * Use only for reads OR writes - not for read-modify-write operations. | |
875 | * Use cpu_register_map_client() to know when retrying the map operation is | |
876 | * likely to succeed. | |
877 | * | |
878 | * @as: #AddressSpace to be accessed | |
879 | * @addr: address within that address space | |
880 | * @plen: pointer to length of buffer; updated on return | |
881 | * @is_write: indicates the transfer direction | |
882 | */ | |
a8170e5e AK |
883 | void *address_space_map(AddressSpace *as, hwaddr addr, |
884 | hwaddr *plen, bool is_write); | |
ac1970fb AK |
885 | |
886 | /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map() | |
887 | * | |
888 | * Will also mark the memory as dirty if @is_write == %true. @access_len gives | |
889 | * the amount of memory that was actually read or written by the caller. | |
890 | * | |
891 | * @as: #AddressSpace used | |
892 | * @addr: address within that address space | |
893 | * @len: buffer length as returned by address_space_map() | |
894 | * @access_len: amount of data actually transferred | |
895 | * @is_write: indicates the transfer direction | |
896 | */ | |
a8170e5e AK |
897 | void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len, |
898 | int is_write, hwaddr access_len); | |
ac1970fb AK |
899 | |
900 | ||
093bc2cd AK |
901 | #endif |
902 | ||
903 | #endif |