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