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