]> git.proxmox.com Git - mirror_qemu.git/blame - include/exec/memory.h
memory: introduce IOMMUNotifier and its caps
[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
022c62cb 24#include "exec/cpu-common.h"
ce927ed9 25#ifndef CONFIG_USER_ONLY
022c62cb 26#include "exec/hwaddr.h"
ce927ed9 27#endif
cc05c43a 28#include "exec/memattrs.h"
1de7afc9 29#include "qemu/queue.h"
1de7afc9 30#include "qemu/int128.h"
06866575 31#include "qemu/notify.h"
b4fefef9 32#include "qom/object.h"
374f2981 33#include "qemu/rcu.h"
093bc2cd 34
07bdaa41
PB
35#define RAM_ADDR_INVALID (~(ram_addr_t)0)
36
052e87b0
PB
37#define MAX_PHYS_ADDR_SPACE_BITS 62
38#define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
39
b4fefef9
PC
40#define TYPE_MEMORY_REGION "qemu:memory-region"
41#define MEMORY_REGION(obj) \
42 OBJECT_CHECK(MemoryRegion, (obj), TYPE_MEMORY_REGION)
43
093bc2cd 44typedef struct MemoryRegionOps MemoryRegionOps;
74901c3b 45typedef struct MemoryRegionMmio MemoryRegionMmio;
093bc2cd 46
74901c3b
AK
47struct MemoryRegionMmio {
48 CPUReadMemoryFunc *read[3];
49 CPUWriteMemoryFunc *write[3];
50};
51
30951157
AK
52typedef struct IOMMUTLBEntry IOMMUTLBEntry;
53
54/* See address_space_translate: bit 0 is read, bit 1 is write. */
55typedef enum {
56 IOMMU_NONE = 0,
57 IOMMU_RO = 1,
58 IOMMU_WO = 2,
59 IOMMU_RW = 3,
60} IOMMUAccessFlags;
61
62struct IOMMUTLBEntry {
63 AddressSpace *target_as;
64 hwaddr iova;
65 hwaddr translated_addr;
66 hwaddr addr_mask; /* 0xfff = 4k translation */
67 IOMMUAccessFlags perm;
68};
69
cdb30812
PX
70/*
71 * Bitmap for different IOMMUNotifier capabilities. Each notifier can
72 * register with one or multiple IOMMU Notifier capability bit(s).
73 */
74typedef enum {
75 IOMMU_NOTIFIER_NONE = 0,
76 /* Notify cache invalidations */
77 IOMMU_NOTIFIER_UNMAP = 0x1,
78 /* Notify entry changes (newly created entries) */
79 IOMMU_NOTIFIER_MAP = 0x2,
80} IOMMUNotifierFlag;
81
82#define IOMMU_NOTIFIER_ALL (IOMMU_NOTIFIER_MAP | IOMMU_NOTIFIER_UNMAP)
83
84struct IOMMUNotifier {
85 void (*notify)(struct IOMMUNotifier *notifier, IOMMUTLBEntry *data);
86 IOMMUNotifierFlag notifier_flags;
87 QLIST_ENTRY(IOMMUNotifier) node;
88};
89typedef struct IOMMUNotifier IOMMUNotifier;
90
cc05c43a
PM
91/* New-style MMIO accessors can indicate that the transaction failed.
92 * A zero (MEMTX_OK) response means success; anything else is a failure
93 * of some kind. The memory subsystem will bitwise-OR together results
94 * if it is synthesizing an operation from multiple smaller accesses.
95 */
96#define MEMTX_OK 0
97#define MEMTX_ERROR (1U << 0) /* device returned an error */
98#define MEMTX_DECODE_ERROR (1U << 1) /* nothing at that address */
99typedef uint32_t MemTxResult;
100
093bc2cd
AK
101/*
102 * Memory region callbacks
103 */
104struct MemoryRegionOps {
105 /* Read from the memory region. @addr is relative to @mr; @size is
106 * in bytes. */
107 uint64_t (*read)(void *opaque,
a8170e5e 108 hwaddr addr,
093bc2cd
AK
109 unsigned size);
110 /* Write to the memory region. @addr is relative to @mr; @size is
111 * in bytes. */
112 void (*write)(void *opaque,
a8170e5e 113 hwaddr addr,
093bc2cd
AK
114 uint64_t data,
115 unsigned size);
116
cc05c43a
PM
117 MemTxResult (*read_with_attrs)(void *opaque,
118 hwaddr addr,
119 uint64_t *data,
120 unsigned size,
121 MemTxAttrs attrs);
122 MemTxResult (*write_with_attrs)(void *opaque,
123 hwaddr addr,
124 uint64_t data,
125 unsigned size,
126 MemTxAttrs attrs);
127
093bc2cd
AK
128 enum device_endian endianness;
129 /* Guest-visible constraints: */
130 struct {
131 /* If nonzero, specify bounds on access sizes beyond which a machine
132 * check is thrown.
133 */
134 unsigned min_access_size;
135 unsigned max_access_size;
136 /* If true, unaligned accesses are supported. Otherwise unaligned
137 * accesses throw machine checks.
138 */
139 bool unaligned;
897fa7cf
AK
140 /*
141 * If present, and returns #false, the transaction is not accepted
142 * by the device (and results in machine dependent behaviour such
143 * as a machine check exception).
144 */
a8170e5e 145 bool (*accepts)(void *opaque, hwaddr addr,
897fa7cf 146 unsigned size, bool is_write);
093bc2cd
AK
147 } valid;
148 /* Internal implementation constraints: */
149 struct {
150 /* If nonzero, specifies the minimum size implemented. Smaller sizes
151 * will be rounded upwards and a partial result will be returned.
152 */
153 unsigned min_access_size;
154 /* If nonzero, specifies the maximum size implemented. Larger sizes
155 * will be done as a series of accesses with smaller sizes.
156 */
157 unsigned max_access_size;
158 /* If true, unaligned accesses are supported. Otherwise all accesses
159 * are converted to (possibly multiple) naturally aligned accesses.
160 */
edc1ba7a 161 bool unaligned;
093bc2cd 162 } impl;
627a0e90 163
74901c3b
AK
164 /* If .read and .write are not present, old_mmio may be used for
165 * backwards compatibility with old mmio registration
166 */
167 const MemoryRegionMmio old_mmio;
093bc2cd
AK
168};
169
30951157
AK
170typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps;
171
172struct MemoryRegionIOMMUOps {
173 /* Return a TLB entry that contains a given address. */
8d7b8cb9 174 IOMMUTLBEntry (*translate)(MemoryRegion *iommu, hwaddr addr, bool is_write);
f682e9c2
AK
175 /* Returns minimum supported page size */
176 uint64_t (*get_min_page_size)(MemoryRegion *iommu);
d22d8956
AK
177 /* Called when the first notifier is set */
178 void (*notify_started)(MemoryRegion *iommu);
179 /* Called when the last notifier is removed */
180 void (*notify_stopped)(MemoryRegion *iommu);
30951157
AK
181};
182
093bc2cd 183typedef struct CoalescedMemoryRange CoalescedMemoryRange;
3e9d69e7 184typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
093bc2cd
AK
185
186struct MemoryRegion {
b4fefef9 187 Object parent_obj;
a676854f 188
093bc2cd 189 /* All fields are private - violators will be prosecuted */
a676854f
PB
190
191 /* The following fields should fit in a cache line */
192 bool romd_mode;
193 bool ram;
194 bool subpage;
195 bool readonly; /* For RAM regions */
196 bool rom_device;
197 bool flush_coalesced_mmio;
198 bool global_locking;
199 uint8_t dirty_log_mask;
58eaa217 200 RAMBlock *ram_block;
612263cf 201 Object *owner;
30951157 202 const MemoryRegionIOMMUOps *iommu_ops;
a676854f
PB
203
204 const MemoryRegionOps *ops;
093bc2cd 205 void *opaque;
feca4ac1 206 MemoryRegion *container;
08dafab4 207 Int128 size;
a8170e5e 208 hwaddr addr;
545e92e0 209 void (*destructor)(MemoryRegion *mr);
a2b257d6 210 uint64_t align;
14a3c10a 211 bool terminates;
e4dc3f59 212 bool skip_dump;
6bba19ba 213 bool enabled;
1660e72d 214 bool warning_printed; /* For reservations */
deb809ed 215 uint8_t vga_logging_count;
093bc2cd 216 MemoryRegion *alias;
a8170e5e 217 hwaddr alias_offset;
d33382da 218 int32_t priority;
093bc2cd
AK
219 QTAILQ_HEAD(subregions, MemoryRegion) subregions;
220 QTAILQ_ENTRY(MemoryRegion) subregions_link;
221 QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced;
302fa283 222 const char *name;
3e9d69e7
AK
223 unsigned ioeventfd_nb;
224 MemoryRegionIoeventfd *ioeventfds;
cdb30812 225 QLIST_HEAD(, IOMMUNotifier) iommu_notify;
093bc2cd
AK
226};
227
c2fc83e8
PB
228/**
229 * MemoryListener: callbacks structure for updates to the physical memory map
230 *
231 * Allows a component to adjust to changes in the guest-visible memory map.
232 * Use with memory_listener_register() and memory_listener_unregister().
233 */
234struct MemoryListener {
235 void (*begin)(MemoryListener *listener);
236 void (*commit)(MemoryListener *listener);
237 void (*region_add)(MemoryListener *listener, MemoryRegionSection *section);
238 void (*region_del)(MemoryListener *listener, MemoryRegionSection *section);
239 void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section);
b2dfd71c
PB
240 void (*log_start)(MemoryListener *listener, MemoryRegionSection *section,
241 int old, int new);
242 void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section,
243 int old, int new);
c2fc83e8
PB
244 void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section);
245 void (*log_global_start)(MemoryListener *listener);
246 void (*log_global_stop)(MemoryListener *listener);
247 void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section,
248 bool match_data, uint64_t data, EventNotifier *e);
249 void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section,
250 bool match_data, uint64_t data, EventNotifier *e);
251 void (*coalesced_mmio_add)(MemoryListener *listener, MemoryRegionSection *section,
252 hwaddr addr, hwaddr len);
253 void (*coalesced_mmio_del)(MemoryListener *listener, MemoryRegionSection *section,
254 hwaddr addr, hwaddr len);
255 /* Lower = earlier (during add), later (during del) */
256 unsigned priority;
257 AddressSpace *address_space_filter;
258 QTAILQ_ENTRY(MemoryListener) link;
259};
260
9ad2bbc1
AK
261/**
262 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
263 */
264struct AddressSpace {
265 /* All fields are private. */
374f2981 266 struct rcu_head rcu;
7dca8043 267 char *name;
9ad2bbc1 268 MemoryRegion *root;
f0c02d15
PC
269 int ref_count;
270 bool malloced;
374f2981
PB
271
272 /* Accessed via RCU. */
9ad2bbc1 273 struct FlatView *current_map;
374f2981 274
9ad2bbc1
AK
275 int ioeventfd_nb;
276 struct MemoryRegionIoeventfd *ioeventfds;
ac1970fb 277 struct AddressSpaceDispatch *dispatch;
00752703 278 struct AddressSpaceDispatch *next_dispatch;
89ae337a
PB
279 MemoryListener dispatch_listener;
280
0d673e36 281 QTAILQ_ENTRY(AddressSpace) address_spaces_link;
9ad2bbc1
AK
282};
283
e2177955
AK
284/**
285 * MemoryRegionSection: describes a fragment of a #MemoryRegion
286 *
287 * @mr: the region, or %NULL if empty
7664e80c 288 * @address_space: the address space the region is mapped in
e2177955
AK
289 * @offset_within_region: the beginning of the section, relative to @mr's start
290 * @size: the size of the section; will not exceed @mr's boundaries
291 * @offset_within_address_space: the address of the first byte of the section
292 * relative to the region's address space
7a8499e8 293 * @readonly: writes to this section are ignored
e2177955
AK
294 */
295struct MemoryRegionSection {
296 MemoryRegion *mr;
f6790af6 297 AddressSpace *address_space;
a8170e5e 298 hwaddr offset_within_region;
052e87b0 299 Int128 size;
a8170e5e 300 hwaddr offset_within_address_space;
7a8499e8 301 bool readonly;
e2177955
AK
302};
303
093bc2cd
AK
304/**
305 * memory_region_init: Initialize a memory region
306 *
69ddaf66 307 * The region typically acts as a container for other memory regions. Use
093bc2cd
AK
308 * memory_region_add_subregion() to add subregions.
309 *
310 * @mr: the #MemoryRegion to be initialized
2c9b15ca 311 * @owner: the object that tracks the region's reference count
093bc2cd
AK
312 * @name: used for debugging; not visible to the user or ABI
313 * @size: size of the region; any subregions beyond this size will be clipped
314 */
315void memory_region_init(MemoryRegion *mr,
2c9b15ca 316 struct Object *owner,
093bc2cd
AK
317 const char *name,
318 uint64_t size);
46637be2
PB
319
320/**
321 * memory_region_ref: Add 1 to a memory region's reference count
322 *
323 * Whenever memory regions are accessed outside the BQL, they need to be
324 * preserved against hot-unplug. MemoryRegions actually do not have their
325 * own reference count; they piggyback on a QOM object, their "owner".
326 * This function adds a reference to the owner.
327 *
328 * All MemoryRegions must have an owner if they can disappear, even if the
329 * device they belong to operates exclusively under the BQL. This is because
330 * the region could be returned at any time by memory_region_find, and this
331 * is usually under guest control.
332 *
333 * @mr: the #MemoryRegion
334 */
335void memory_region_ref(MemoryRegion *mr);
336
337/**
338 * memory_region_unref: Remove 1 to a memory region's reference count
339 *
340 * Whenever memory regions are accessed outside the BQL, they need to be
341 * preserved against hot-unplug. MemoryRegions actually do not have their
342 * own reference count; they piggyback on a QOM object, their "owner".
343 * This function removes a reference to the owner and possibly destroys it.
344 *
345 * @mr: the #MemoryRegion
346 */
347void memory_region_unref(MemoryRegion *mr);
348
093bc2cd
AK
349/**
350 * memory_region_init_io: Initialize an I/O memory region.
351 *
69ddaf66 352 * Accesses into the region will cause the callbacks in @ops to be called.
093bc2cd
AK
353 * if @size is nonzero, subregions will be clipped to @size.
354 *
355 * @mr: the #MemoryRegion to be initialized.
2c9b15ca 356 * @owner: the object that tracks the region's reference count
093bc2cd
AK
357 * @ops: a structure containing read and write callbacks to be used when
358 * I/O is performed on the region.
b6af0975 359 * @opaque: passed to the read and write callbacks of the @ops structure.
093bc2cd
AK
360 * @name: used for debugging; not visible to the user or ABI
361 * @size: size of the region.
362 */
363void memory_region_init_io(MemoryRegion *mr,
2c9b15ca 364 struct Object *owner,
093bc2cd
AK
365 const MemoryRegionOps *ops,
366 void *opaque,
367 const char *name,
368 uint64_t size);
369
370/**
371 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
69ddaf66 372 * region will modify memory directly.
093bc2cd
AK
373 *
374 * @mr: the #MemoryRegion to be initialized.
2c9b15ca 375 * @owner: the object that tracks the region's reference count
c5705a77 376 * @name: the name of the region.
093bc2cd 377 * @size: size of the region.
49946538 378 * @errp: pointer to Error*, to store an error if it happens.
093bc2cd
AK
379 */
380void memory_region_init_ram(MemoryRegion *mr,
2c9b15ca 381 struct Object *owner,
093bc2cd 382 const char *name,
49946538
HT
383 uint64_t size,
384 Error **errp);
093bc2cd 385
60786ef3
MT
386/**
387 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
388 * RAM. Accesses into the region will
389 * modify memory directly. Only an initial
390 * portion of this RAM is actually used.
391 * The used size can change across reboots.
392 *
393 * @mr: the #MemoryRegion to be initialized.
394 * @owner: the object that tracks the region's reference count
395 * @name: the name of the region.
396 * @size: used size of the region.
397 * @max_size: max size of the region.
398 * @resized: callback to notify owner about used size change.
399 * @errp: pointer to Error*, to store an error if it happens.
400 */
401void memory_region_init_resizeable_ram(MemoryRegion *mr,
402 struct Object *owner,
403 const char *name,
404 uint64_t size,
405 uint64_t max_size,
406 void (*resized)(const char*,
407 uint64_t length,
408 void *host),
409 Error **errp);
0b183fc8
PB
410#ifdef __linux__
411/**
412 * memory_region_init_ram_from_file: Initialize RAM memory region with a
413 * mmap-ed backend.
414 *
415 * @mr: the #MemoryRegion to be initialized.
416 * @owner: the object that tracks the region's reference count
417 * @name: the name of the region.
418 * @size: size of the region.
dbcb8981 419 * @share: %true if memory must be mmaped with the MAP_SHARED flag
0b183fc8 420 * @path: the path in which to allocate the RAM.
7f56e740 421 * @errp: pointer to Error*, to store an error if it happens.
0b183fc8
PB
422 */
423void memory_region_init_ram_from_file(MemoryRegion *mr,
424 struct Object *owner,
425 const char *name,
426 uint64_t size,
dbcb8981 427 bool share,
7f56e740
PB
428 const char *path,
429 Error **errp);
0b183fc8
PB
430#endif
431
093bc2cd 432/**
1a7e8cae
BZ
433 * memory_region_init_ram_ptr: Initialize RAM memory region from a
434 * user-provided pointer. Accesses into the
435 * region will modify memory directly.
093bc2cd
AK
436 *
437 * @mr: the #MemoryRegion to be initialized.
2c9b15ca 438 * @owner: the object that tracks the region's reference count
c5705a77 439 * @name: the name of the region.
093bc2cd
AK
440 * @size: size of the region.
441 * @ptr: memory to be mapped; must contain at least @size bytes.
442 */
443void memory_region_init_ram_ptr(MemoryRegion *mr,
2c9b15ca 444 struct Object *owner,
093bc2cd
AK
445 const char *name,
446 uint64_t size,
447 void *ptr);
448
449/**
450 * memory_region_init_alias: Initialize a memory region that aliases all or a
451 * part of another memory region.
452 *
453 * @mr: the #MemoryRegion to be initialized.
2c9b15ca 454 * @owner: the object that tracks the region's reference count
093bc2cd
AK
455 * @name: used for debugging; not visible to the user or ABI
456 * @orig: the region to be referenced; @mr will be equivalent to
457 * @orig between @offset and @offset + @size - 1.
458 * @offset: start of the section in @orig to be referenced.
459 * @size: size of the region.
460 */
461void memory_region_init_alias(MemoryRegion *mr,
2c9b15ca 462 struct Object *owner,
093bc2cd
AK
463 const char *name,
464 MemoryRegion *orig,
a8170e5e 465 hwaddr offset,
093bc2cd 466 uint64_t size);
d0a9b5bc 467
a1777f7f
PM
468/**
469 * memory_region_init_rom: Initialize a ROM memory region.
470 *
471 * This has the same effect as calling memory_region_init_ram()
472 * and then marking the resulting region read-only with
473 * memory_region_set_readonly().
474 *
475 * @mr: the #MemoryRegion to be initialized.
476 * @owner: the object that tracks the region's reference count
477 * @name: the name of the region.
478 * @size: size of the region.
479 * @errp: pointer to Error*, to store an error if it happens.
480 */
481void memory_region_init_rom(MemoryRegion *mr,
482 struct Object *owner,
483 const char *name,
484 uint64_t size,
485 Error **errp);
486
d0a9b5bc
AK
487/**
488 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
489 * handled via callbacks.
490 *
491 * @mr: the #MemoryRegion to be initialized.
2c9b15ca 492 * @owner: the object that tracks the region's reference count
39e0b03d 493 * @ops: callbacks for write access handling (must not be NULL).
c5705a77 494 * @name: the name of the region.
d0a9b5bc 495 * @size: size of the region.
33e0eb52 496 * @errp: pointer to Error*, to store an error if it happens.
d0a9b5bc
AK
497 */
498void memory_region_init_rom_device(MemoryRegion *mr,
2c9b15ca 499 struct Object *owner,
d0a9b5bc 500 const MemoryRegionOps *ops,
75f5941c 501 void *opaque,
d0a9b5bc 502 const char *name,
33e0eb52
HT
503 uint64_t size,
504 Error **errp);
d0a9b5bc 505
1660e72d
JK
506/**
507 * memory_region_init_reservation: Initialize a memory region that reserves
508 * I/O space.
509 *
510 * A reservation region primariy serves debugging purposes. It claims I/O
511 * space that is not supposed to be handled by QEMU itself. Any access via
512 * the memory API will cause an abort().
6d6d2abf
PF
513 * This function is deprecated. Use memory_region_init_io() with NULL
514 * callbacks instead.
1660e72d
JK
515 *
516 * @mr: the #MemoryRegion to be initialized
2c9b15ca 517 * @owner: the object that tracks the region's reference count
1660e72d
JK
518 * @name: used for debugging; not visible to the user or ABI
519 * @size: size of the region.
520 */
6d6d2abf
PF
521static inline void memory_region_init_reservation(MemoryRegion *mr,
522 Object *owner,
1660e72d 523 const char *name,
6d6d2abf
PF
524 uint64_t size)
525{
526 memory_region_init_io(mr, owner, NULL, mr, name, size);
527}
30951157
AK
528
529/**
530 * memory_region_init_iommu: Initialize a memory region that translates
531 * addresses
532 *
533 * An IOMMU region translates addresses and forwards accesses to a target
534 * memory region.
535 *
536 * @mr: the #MemoryRegion to be initialized
2c9b15ca 537 * @owner: the object that tracks the region's reference count
30951157
AK
538 * @ops: a function that translates addresses into the @target region
539 * @name: used for debugging; not visible to the user or ABI
540 * @size: size of the region.
541 */
542void memory_region_init_iommu(MemoryRegion *mr,
2c9b15ca 543 struct Object *owner,
30951157
AK
544 const MemoryRegionIOMMUOps *ops,
545 const char *name,
546 uint64_t size);
547
803c0816
PB
548/**
549 * memory_region_owner: get a memory region's owner.
550 *
551 * @mr: the memory region being queried.
552 */
553struct Object *memory_region_owner(MemoryRegion *mr);
554
093bc2cd
AK
555/**
556 * memory_region_size: get a memory region's size.
557 *
558 * @mr: the memory region being queried.
559 */
560uint64_t memory_region_size(MemoryRegion *mr);
561
8ea9252a
AK
562/**
563 * memory_region_is_ram: check whether a memory region is random access
564 *
565 * Returns %true is a memory region is random access.
566 *
567 * @mr: the memory region being queried
568 */
1619d1fe
PB
569static inline bool memory_region_is_ram(MemoryRegion *mr)
570{
571 return mr->ram;
572}
8ea9252a 573
e4dc3f59
ND
574/**
575 * memory_region_is_skip_dump: check whether a memory region should not be
576 * dumped
577 *
578 * Returns %true is a memory region should not be dumped(e.g. VFIO BAR MMAP).
579 *
580 * @mr: the memory region being queried
581 */
582bool memory_region_is_skip_dump(MemoryRegion *mr);
583
584/**
585 * memory_region_set_skip_dump: Set skip_dump flag, dump will ignore this memory
586 * region
587 *
588 * @mr: the memory region being queried
589 */
590void memory_region_set_skip_dump(MemoryRegion *mr);
591
fd062573 592/**
5f9a5ea1 593 * memory_region_is_romd: check whether a memory region is in ROMD mode
fd062573 594 *
5f9a5ea1 595 * Returns %true if a memory region is a ROM device and currently set to allow
fd062573
BS
596 * direct reads.
597 *
598 * @mr: the memory region being queried
599 */
600static inline bool memory_region_is_romd(MemoryRegion *mr)
601{
5f9a5ea1 602 return mr->rom_device && mr->romd_mode;
fd062573
BS
603}
604
30951157
AK
605/**
606 * memory_region_is_iommu: check whether a memory region is an iommu
607 *
608 * Returns %true is a memory region is an iommu.
609 *
610 * @mr: the memory region being queried
611 */
1619d1fe
PB
612static inline bool memory_region_is_iommu(MemoryRegion *mr)
613{
614 return mr->iommu_ops;
615}
616
30951157 617
f682e9c2
AK
618/**
619 * memory_region_iommu_get_min_page_size: get minimum supported page size
620 * for an iommu
621 *
622 * Returns minimum supported page size for an iommu.
623 *
624 * @mr: the memory region being queried
625 */
626uint64_t memory_region_iommu_get_min_page_size(MemoryRegion *mr);
627
06866575
DG
628/**
629 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
630 *
cdb30812
PX
631 * The notification type will be decided by entry.perm bits:
632 *
633 * - For UNMAP (cache invalidation) notifies: set entry.perm to IOMMU_NONE.
634 * - For MAP (newly added entry) notifies: set entry.perm to the
635 * permission of the page (which is definitely !IOMMU_NONE).
636 *
637 * Note: for any IOMMU implementation, an in-place mapping change
638 * should be notified with an UNMAP followed by a MAP.
639 *
06866575
DG
640 * @mr: the memory region that was changed
641 * @entry: the new entry in the IOMMU translation table. The entry
642 * replaces all old entries for the same virtual I/O address range.
643 * Deleted entries have .@perm == 0.
644 */
645void memory_region_notify_iommu(MemoryRegion *mr,
646 IOMMUTLBEntry entry);
647
648/**
649 * memory_region_register_iommu_notifier: register a notifier for changes to
650 * IOMMU translation entries.
651 *
652 * @mr: the memory region to observe
cdb30812
PX
653 * @n: the IOMMUNotifier to be added; the notify callback receives a
654 * pointer to an #IOMMUTLBEntry as the opaque value; the pointer
655 * ceases to be valid on exit from the notifier.
06866575 656 */
cdb30812
PX
657void memory_region_register_iommu_notifier(MemoryRegion *mr,
658 IOMMUNotifier *n);
06866575 659
a788f227
DG
660/**
661 * memory_region_iommu_replay: replay existing IOMMU translations to
f682e9c2
AK
662 * a notifier with the minimum page granularity returned by
663 * mr->iommu_ops->get_page_size().
a788f227
DG
664 *
665 * @mr: the memory region to observe
666 * @n: the notifier to which to replay iommu mappings
a788f227
DG
667 * @is_write: Whether to treat the replay as a translate "write"
668 * through the iommu
669 */
cdb30812
PX
670void memory_region_iommu_replay(MemoryRegion *mr, IOMMUNotifier *n,
671 bool is_write);
a788f227 672
06866575
DG
673/**
674 * memory_region_unregister_iommu_notifier: unregister a notifier for
675 * changes to IOMMU translation entries.
676 *
d22d8956
AK
677 * @mr: the memory region which was observed and for which notity_stopped()
678 * needs to be called
06866575
DG
679 * @n: the notifier to be removed.
680 */
cdb30812
PX
681void memory_region_unregister_iommu_notifier(MemoryRegion *mr,
682 IOMMUNotifier *n);
06866575 683
8991c79b
AK
684/**
685 * memory_region_name: get a memory region's name
686 *
687 * Returns the string that was used to initialize the memory region.
688 *
689 * @mr: the memory region being queried
690 */
5d546d4b 691const char *memory_region_name(const MemoryRegion *mr);
8991c79b 692
55043ba3
AK
693/**
694 * memory_region_is_logging: return whether a memory region is logging writes
695 *
2d1a35be 696 * Returns %true if the memory region is logging writes for the given client
55043ba3
AK
697 *
698 * @mr: the memory region being queried
2d1a35be 699 * @client: the client being queried
55043ba3 700 */
2d1a35be
PB
701bool memory_region_is_logging(MemoryRegion *mr, uint8_t client);
702
703/**
704 * memory_region_get_dirty_log_mask: return the clients for which a
705 * memory region is logging writes.
706 *
677e7805
PB
707 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
708 * are the bit indices.
2d1a35be
PB
709 *
710 * @mr: the memory region being queried
711 */
712uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr);
55043ba3 713
ce7923da
AK
714/**
715 * memory_region_is_rom: check whether a memory region is ROM
716 *
717 * Returns %true is a memory region is read-only memory.
718 *
719 * @mr: the memory region being queried
720 */
1619d1fe
PB
721static inline bool memory_region_is_rom(MemoryRegion *mr)
722{
723 return mr->ram && mr->readonly;
724}
725
ce7923da 726
a35ba7be
PB
727/**
728 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
729 *
730 * Returns a file descriptor backing a file-based RAM memory region,
731 * or -1 if the region is not a file-based RAM memory region.
732 *
733 * @mr: the RAM or alias memory region being queried.
734 */
735int memory_region_get_fd(MemoryRegion *mr);
736
4ff87573
PB
737/**
738 * memory_region_set_fd: Mark a RAM memory region as backed by a
739 * file descriptor.
740 *
741 * This function is typically used after memory_region_init_ram_ptr().
742 *
743 * @mr: the memory region being queried.
744 * @fd: the file descriptor that backs @mr.
745 */
746void memory_region_set_fd(MemoryRegion *mr, int fd);
747
07bdaa41
PB
748/**
749 * memory_region_from_host: Convert a pointer into a RAM memory region
750 * and an offset within it.
751 *
752 * Given a host pointer inside a RAM memory region (created with
753 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
754 * the MemoryRegion and the offset within it.
755 *
756 * Use with care; by the time this function returns, the returned pointer is
757 * not protected by RCU anymore. If the caller is not within an RCU critical
758 * section and does not hold the iothread lock, it must have other means of
759 * protecting the pointer, such as a reference to the region that includes
760 * the incoming ram_addr_t.
761 *
762 * @mr: the memory region being queried.
763 */
764MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset);
765
093bc2cd
AK
766/**
767 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
768 *
769 * Returns a host pointer to a RAM memory region (created with
49b24afc
PB
770 * memory_region_init_ram() or memory_region_init_ram_ptr()).
771 *
772 * Use with care; by the time this function returns, the returned pointer is
773 * not protected by RCU anymore. If the caller is not within an RCU critical
774 * section and does not hold the iothread lock, it must have other means of
775 * protecting the pointer, such as a reference to the region that includes
776 * the incoming ram_addr_t.
093bc2cd
AK
777 *
778 * @mr: the memory region being queried.
779 */
780void *memory_region_get_ram_ptr(MemoryRegion *mr);
781
37d7c084
PB
782/* memory_region_ram_resize: Resize a RAM region.
783 *
784 * Only legal before guest might have detected the memory size: e.g. on
785 * incoming migration, or right after reset.
786 *
787 * @mr: a memory region created with @memory_region_init_resizeable_ram.
788 * @newsize: the new size the region
789 * @errp: pointer to Error*, to store an error if it happens.
790 */
791void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize,
792 Error **errp);
793
093bc2cd
AK
794/**
795 * memory_region_set_log: Turn dirty logging on or off for a region.
796 *
797 * Turns dirty logging on or off for a specified client (display, migration).
798 * Only meaningful for RAM regions.
799 *
800 * @mr: the memory region being updated.
801 * @log: whether dirty logging is to be enabled or disabled.
dbddac6d 802 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
093bc2cd
AK
803 */
804void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
805
806/**
cd7a45c9
BS
807 * memory_region_get_dirty: Check whether a range of bytes is dirty
808 * for a specified client.
093bc2cd 809 *
cd7a45c9 810 * Checks whether a range of bytes has been written to since the last
093bc2cd
AK
811 * call to memory_region_reset_dirty() with the same @client. Dirty logging
812 * must be enabled.
813 *
814 * @mr: the memory region being queried.
815 * @addr: the address (relative to the start of the region) being queried.
cd7a45c9 816 * @size: the size of the range being queried.
093bc2cd
AK
817 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
818 * %DIRTY_MEMORY_VGA.
819 */
a8170e5e
AK
820bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
821 hwaddr size, unsigned client);
093bc2cd
AK
822
823/**
fd4aa979 824 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
093bc2cd 825 *
fd4aa979
BS
826 * Marks a range of bytes as dirty, after it has been dirtied outside
827 * guest code.
093bc2cd 828 *
fd4aa979 829 * @mr: the memory region being dirtied.
093bc2cd 830 * @addr: the address (relative to the start of the region) being dirtied.
fd4aa979 831 * @size: size of the range being dirtied.
093bc2cd 832 */
a8170e5e
AK
833void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
834 hwaddr size);
093bc2cd 835
6c279db8
JQ
836/**
837 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty
838 * for a specified client. It clears them.
839 *
840 * Checks whether a range of bytes has been written to since the last
841 * call to memory_region_reset_dirty() with the same @client. Dirty logging
842 * must be enabled.
843 *
844 * @mr: the memory region being queried.
845 * @addr: the address (relative to the start of the region) being queried.
846 * @size: the size of the range being queried.
847 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
848 * %DIRTY_MEMORY_VGA.
849 */
850bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr,
851 hwaddr size, unsigned client);
093bc2cd
AK
852/**
853 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
854 * any external TLBs (e.g. kvm)
855 *
856 * Flushes dirty information from accelerators such as kvm and vhost-net
857 * and makes it available to users of the memory API.
858 *
859 * @mr: the region being flushed.
860 */
861void memory_region_sync_dirty_bitmap(MemoryRegion *mr);
862
863/**
864 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
865 * client.
866 *
867 * Marks a range of pages as no longer dirty.
868 *
869 * @mr: the region being updated.
870 * @addr: the start of the subrange being cleaned.
871 * @size: the size of the subrange being cleaned.
872 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
873 * %DIRTY_MEMORY_VGA.
874 */
a8170e5e
AK
875void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
876 hwaddr size, unsigned client);
093bc2cd
AK
877
878/**
879 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
880 *
881 * Allows a memory region to be marked as read-only (turning it into a ROM).
882 * only useful on RAM regions.
883 *
884 * @mr: the region being updated.
885 * @readonly: whether rhe region is to be ROM or RAM.
886 */
887void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
888
d0a9b5bc 889/**
5f9a5ea1 890 * memory_region_rom_device_set_romd: enable/disable ROMD mode
d0a9b5bc
AK
891 *
892 * Allows a ROM device (initialized with memory_region_init_rom_device() to
5f9a5ea1
JK
893 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
894 * device is mapped to guest memory and satisfies read access directly.
895 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
896 * Writes are always handled by the #MemoryRegion.write function.
d0a9b5bc
AK
897 *
898 * @mr: the memory region to be updated
5f9a5ea1 899 * @romd_mode: %true to put the region into ROMD mode
d0a9b5bc 900 */
5f9a5ea1 901void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode);
d0a9b5bc 902
093bc2cd
AK
903/**
904 * memory_region_set_coalescing: Enable memory coalescing for the region.
905 *
906 * Enabled writes to a region to be queued for later processing. MMIO ->write
907 * callbacks may be delayed until a non-coalesced MMIO is issued.
908 * Only useful for IO regions. Roughly similar to write-combining hardware.
909 *
910 * @mr: the memory region to be write coalesced
911 */
912void memory_region_set_coalescing(MemoryRegion *mr);
913
914/**
915 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
916 * a region.
917 *
918 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
919 * Multiple calls can be issued coalesced disjoint ranges.
920 *
921 * @mr: the memory region to be updated.
922 * @offset: the start of the range within the region to be coalesced.
923 * @size: the size of the subrange to be coalesced.
924 */
925void memory_region_add_coalescing(MemoryRegion *mr,
a8170e5e 926 hwaddr offset,
093bc2cd
AK
927 uint64_t size);
928
929/**
930 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
931 *
932 * Disables any coalescing caused by memory_region_set_coalescing() or
933 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
934 * hardware.
935 *
936 * @mr: the memory region to be updated.
937 */
938void memory_region_clear_coalescing(MemoryRegion *mr);
939
d410515e
JK
940/**
941 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
942 * accesses.
943 *
944 * Ensure that pending coalesced MMIO request are flushed before the memory
945 * region is accessed. This property is automatically enabled for all regions
946 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
947 *
948 * @mr: the memory region to be updated.
949 */
950void memory_region_set_flush_coalesced(MemoryRegion *mr);
951
952/**
953 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
954 * accesses.
955 *
956 * Clear the automatic coalesced MMIO flushing enabled via
957 * memory_region_set_flush_coalesced. Note that this service has no effect on
958 * memory regions that have MMIO coalescing enabled for themselves. For them,
959 * automatic flushing will stop once coalescing is disabled.
960 *
961 * @mr: the memory region to be updated.
962 */
963void memory_region_clear_flush_coalesced(MemoryRegion *mr);
964
196ea131
JK
965/**
966 * memory_region_set_global_locking: Declares the access processing requires
967 * QEMU's global lock.
968 *
969 * When this is invoked, accesses to the memory region will be processed while
970 * holding the global lock of QEMU. This is the default behavior of memory
971 * regions.
972 *
973 * @mr: the memory region to be updated.
974 */
975void memory_region_set_global_locking(MemoryRegion *mr);
976
977/**
978 * memory_region_clear_global_locking: Declares that access processing does
979 * not depend on the QEMU global lock.
980 *
981 * By clearing this property, accesses to the memory region will be processed
982 * outside of QEMU's global lock (unless the lock is held on when issuing the
983 * access request). In this case, the device model implementing the access
984 * handlers is responsible for synchronization of concurrency.
985 *
986 * @mr: the memory region to be updated.
987 */
988void memory_region_clear_global_locking(MemoryRegion *mr);
989
3e9d69e7
AK
990/**
991 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
992 * is written to a location.
993 *
994 * Marks a word in an IO region (initialized with memory_region_init_io())
995 * as a trigger for an eventfd event. The I/O callback will not be called.
69ddaf66 996 * The caller must be prepared to handle failure (that is, take the required
3e9d69e7
AK
997 * action if the callback _is_ called).
998 *
999 * @mr: the memory region being updated.
1000 * @addr: the address within @mr that is to be monitored
1001 * @size: the size of the access to trigger the eventfd
1002 * @match_data: whether to match against @data, instead of just @addr
1003 * @data: the data to match against the guest write
1004 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
1005 **/
1006void memory_region_add_eventfd(MemoryRegion *mr,
a8170e5e 1007 hwaddr addr,
3e9d69e7
AK
1008 unsigned size,
1009 bool match_data,
1010 uint64_t data,
753d5e14 1011 EventNotifier *e);
3e9d69e7
AK
1012
1013/**
69ddaf66 1014 * memory_region_del_eventfd: Cancel an eventfd.
3e9d69e7 1015 *
69ddaf66
ASRJ
1016 * Cancels an eventfd trigger requested by a previous
1017 * memory_region_add_eventfd() call.
3e9d69e7
AK
1018 *
1019 * @mr: the memory region being updated.
1020 * @addr: the address within @mr that is to be monitored
1021 * @size: the size of the access to trigger the eventfd
1022 * @match_data: whether to match against @data, instead of just @addr
1023 * @data: the data to match against the guest write
1024 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
1025 */
1026void memory_region_del_eventfd(MemoryRegion *mr,
a8170e5e 1027 hwaddr addr,
3e9d69e7
AK
1028 unsigned size,
1029 bool match_data,
1030 uint64_t data,
753d5e14
PB
1031 EventNotifier *e);
1032
093bc2cd 1033/**
69ddaf66 1034 * memory_region_add_subregion: Add a subregion to a container.
093bc2cd 1035 *
69ddaf66 1036 * Adds a subregion at @offset. The subregion may not overlap with other
093bc2cd
AK
1037 * subregions (except for those explicitly marked as overlapping). A region
1038 * may only be added once as a subregion (unless removed with
1039 * memory_region_del_subregion()); use memory_region_init_alias() if you
1040 * want a region to be a subregion in multiple locations.
1041 *
1042 * @mr: the region to contain the new subregion; must be a container
1043 * initialized with memory_region_init().
1044 * @offset: the offset relative to @mr where @subregion is added.
1045 * @subregion: the subregion to be added.
1046 */
1047void memory_region_add_subregion(MemoryRegion *mr,
a8170e5e 1048 hwaddr offset,
093bc2cd
AK
1049 MemoryRegion *subregion);
1050/**
1a7e8cae
BZ
1051 * memory_region_add_subregion_overlap: Add a subregion to a container
1052 * with overlap.
093bc2cd 1053 *
69ddaf66 1054 * Adds a subregion at @offset. The subregion may overlap with other
093bc2cd
AK
1055 * subregions. Conflicts are resolved by having a higher @priority hide a
1056 * lower @priority. Subregions without priority are taken as @priority 0.
1057 * A region may only be added once as a subregion (unless removed with
1058 * memory_region_del_subregion()); use memory_region_init_alias() if you
1059 * want a region to be a subregion in multiple locations.
1060 *
1061 * @mr: the region to contain the new subregion; must be a container
1062 * initialized with memory_region_init().
1063 * @offset: the offset relative to @mr where @subregion is added.
1064 * @subregion: the subregion to be added.
1065 * @priority: used for resolving overlaps; highest priority wins.
1066 */
1067void memory_region_add_subregion_overlap(MemoryRegion *mr,
a8170e5e 1068 hwaddr offset,
093bc2cd 1069 MemoryRegion *subregion,
a1ff8ae0 1070 int priority);
e34911c4
AK
1071
1072/**
1073 * memory_region_get_ram_addr: Get the ram address associated with a memory
1074 * region
e34911c4 1075 */
7ebb2745 1076ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr);
e34911c4 1077
a2b257d6 1078uint64_t memory_region_get_alignment(const MemoryRegion *mr);
093bc2cd
AK
1079/**
1080 * memory_region_del_subregion: Remove a subregion.
1081 *
1082 * Removes a subregion from its container.
1083 *
1084 * @mr: the container to be updated.
1085 * @subregion: the region being removed; must be a current subregion of @mr.
1086 */
1087void memory_region_del_subregion(MemoryRegion *mr,
1088 MemoryRegion *subregion);
1089
6bba19ba
AK
1090/*
1091 * memory_region_set_enabled: dynamically enable or disable a region
1092 *
1093 * Enables or disables a memory region. A disabled memory region
1094 * ignores all accesses to itself and its subregions. It does not
1095 * obscure sibling subregions with lower priority - it simply behaves as
1096 * if it was removed from the hierarchy.
1097 *
1098 * Regions default to being enabled.
1099 *
1100 * @mr: the region to be updated
1101 * @enabled: whether to enable or disable the region
1102 */
1103void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
1104
2282e1af
AK
1105/*
1106 * memory_region_set_address: dynamically update the address of a region
1107 *
feca4ac1 1108 * Dynamically updates the address of a region, relative to its container.
2282e1af
AK
1109 * May be used on regions are currently part of a memory hierarchy.
1110 *
1111 * @mr: the region to be updated
feca4ac1 1112 * @addr: new address, relative to container region
2282e1af 1113 */
a8170e5e 1114void memory_region_set_address(MemoryRegion *mr, hwaddr addr);
2282e1af 1115
e7af4c67
MT
1116/*
1117 * memory_region_set_size: dynamically update the size of a region.
1118 *
1119 * Dynamically updates the size of a region.
1120 *
1121 * @mr: the region to be updated
1122 * @size: used size of the region.
1123 */
1124void memory_region_set_size(MemoryRegion *mr, uint64_t size);
1125
4703359e
AK
1126/*
1127 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1128 *
1129 * Dynamically updates the offset into the target region that an alias points
1130 * to, as if the fourth argument to memory_region_init_alias() has changed.
1131 *
1132 * @mr: the #MemoryRegion to be updated; should be an alias.
1133 * @offset: the new offset into the target memory region
1134 */
1135void memory_region_set_alias_offset(MemoryRegion *mr,
a8170e5e 1136 hwaddr offset);
4703359e 1137
3ce10901 1138/**
feca4ac1
PB
1139 * memory_region_present: checks if an address relative to a @container
1140 * translates into #MemoryRegion within @container
3ce10901 1141 *
feca4ac1 1142 * Answer whether a #MemoryRegion within @container covers the address
3ce10901
PB
1143 * @addr.
1144 *
feca4ac1
PB
1145 * @container: a #MemoryRegion within which @addr is a relative address
1146 * @addr: the area within @container to be searched
3ce10901 1147 */
feca4ac1 1148bool memory_region_present(MemoryRegion *container, hwaddr addr);
3ce10901 1149
eed2bacf
IM
1150/**
1151 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1152 * into any address space.
1153 *
1154 * @mr: a #MemoryRegion which should be checked if it's mapped
1155 */
1156bool memory_region_is_mapped(MemoryRegion *mr);
1157
e2177955 1158/**
73034e9e
PB
1159 * memory_region_find: translate an address/size relative to a
1160 * MemoryRegion into a #MemoryRegionSection.
e2177955 1161 *
73034e9e
PB
1162 * Locates the first #MemoryRegion within @mr that overlaps the range
1163 * given by @addr and @size.
e2177955
AK
1164 *
1165 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1166 * It will have the following characteristics:
e2177955
AK
1167 * .@size = 0 iff no overlap was found
1168 * .@mr is non-%NULL iff an overlap was found
1169 *
73034e9e
PB
1170 * Remember that in the return value the @offset_within_region is
1171 * relative to the returned region (in the .@mr field), not to the
1172 * @mr argument.
1173 *
1174 * Similarly, the .@offset_within_address_space is relative to the
1175 * address space that contains both regions, the passed and the
1176 * returned one. However, in the special case where the @mr argument
feca4ac1 1177 * has no container (and thus is the root of the address space), the
73034e9e
PB
1178 * following will hold:
1179 * .@offset_within_address_space >= @addr
1180 * .@offset_within_address_space + .@size <= @addr + @size
1181 *
1182 * @mr: a MemoryRegion within which @addr is a relative address
1183 * @addr: start of the area within @as to be searched
e2177955
AK
1184 * @size: size of the area to be searched
1185 */
73034e9e 1186MemoryRegionSection memory_region_find(MemoryRegion *mr,
a8170e5e 1187 hwaddr addr, uint64_t size);
e2177955 1188
86e775c6 1189/**
1d671369 1190 * address_space_sync_dirty_bitmap: synchronize the dirty log for all memory
86e775c6
AK
1191 *
1192 * Synchronizes the dirty page log for an entire address space.
1d671369 1193 * @as: the address space that contains the memory being synchronized
86e775c6 1194 */
1d671369 1195void address_space_sync_dirty_bitmap(AddressSpace *as);
86e775c6 1196
69ddaf66
ASRJ
1197/**
1198 * memory_region_transaction_begin: Start a transaction.
1199 *
1200 * During a transaction, changes will be accumulated and made visible
dabdf394 1201 * only when the transaction ends (is committed).
4ef4db86
AK
1202 */
1203void memory_region_transaction_begin(void);
69ddaf66
ASRJ
1204
1205/**
1206 * memory_region_transaction_commit: Commit a transaction and make changes
1207 * visible to the guest.
4ef4db86
AK
1208 */
1209void memory_region_transaction_commit(void);
1210
7664e80c
AK
1211/**
1212 * memory_listener_register: register callbacks to be called when memory
1213 * sections are mapped or unmapped into an address
1214 * space
1215 *
1216 * @listener: an object containing the callbacks to be called
7376e582 1217 * @filter: if non-%NULL, only regions in this address space will be observed
7664e80c 1218 */
f6790af6 1219void memory_listener_register(MemoryListener *listener, AddressSpace *filter);
7664e80c
AK
1220
1221/**
1222 * memory_listener_unregister: undo the effect of memory_listener_register()
1223 *
1224 * @listener: an object containing the callbacks to be removed
1225 */
1226void memory_listener_unregister(MemoryListener *listener);
1227
1228/**
1229 * memory_global_dirty_log_start: begin dirty logging for all regions
1230 */
1231void memory_global_dirty_log_start(void);
1232
1233/**
1a7e8cae 1234 * memory_global_dirty_log_stop: end dirty logging for all regions
7664e80c
AK
1235 */
1236void memory_global_dirty_log_stop(void);
1237
314e2987
BS
1238void mtree_info(fprintf_function mon_printf, void *f);
1239
3b643495
PM
1240/**
1241 * memory_region_dispatch_read: perform a read directly to the specified
1242 * MemoryRegion.
1243 *
1244 * @mr: #MemoryRegion to access
1245 * @addr: address within that region
1246 * @pval: pointer to uint64_t which the data is written to
1247 * @size: size of the access in bytes
1248 * @attrs: memory transaction attributes to use for the access
1249 */
1250MemTxResult memory_region_dispatch_read(MemoryRegion *mr,
1251 hwaddr addr,
1252 uint64_t *pval,
1253 unsigned size,
1254 MemTxAttrs attrs);
1255/**
1256 * memory_region_dispatch_write: perform a write directly to the specified
1257 * MemoryRegion.
1258 *
1259 * @mr: #MemoryRegion to access
1260 * @addr: address within that region
1261 * @data: data to write
1262 * @size: size of the access in bytes
1263 * @attrs: memory transaction attributes to use for the access
1264 */
1265MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
1266 hwaddr addr,
1267 uint64_t data,
1268 unsigned size,
1269 MemTxAttrs attrs);
1270
9ad2bbc1
AK
1271/**
1272 * address_space_init: initializes an address space
1273 *
1274 * @as: an uninitialized #AddressSpace
67cc32eb 1275 * @root: a #MemoryRegion that routes addresses for the address space
7dca8043
AK
1276 * @name: an address space name. The name is only used for debugging
1277 * output.
9ad2bbc1 1278 */
7dca8043 1279void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
9ad2bbc1 1280
f0c02d15
PC
1281/**
1282 * address_space_init_shareable: return an address space for a memory region,
1283 * creating it if it does not already exist
1284 *
1285 * @root: a #MemoryRegion that routes addresses for the address space
1286 * @name: an address space name. The name is only used for debugging
1287 * output.
1288 *
1289 * This function will return a pointer to an existing AddressSpace
1290 * which was initialized with the specified MemoryRegion, or it will
1291 * create and initialize one if it does not already exist. The ASes
1292 * are reference-counted, so the memory will be freed automatically
1293 * when the AddressSpace is destroyed via address_space_destroy.
1294 */
1295AddressSpace *address_space_init_shareable(MemoryRegion *root,
1296 const char *name);
83f3c251
AK
1297
1298/**
1299 * address_space_destroy: destroy an address space
1300 *
1301 * Releases all resources associated with an address space. After an address space
1302 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1303 * as well.
1304 *
1305 * @as: address space to be destroyed
1306 */
1307void address_space_destroy(AddressSpace *as);
1308
ac1970fb
AK
1309/**
1310 * address_space_rw: read from or write to an address space.
1311 *
5c9eb028
PM
1312 * Return a MemTxResult indicating whether the operation succeeded
1313 * or failed (eg unassigned memory, device rejected the transaction,
1314 * IOMMU fault).
fd8aaa76 1315 *
ac1970fb
AK
1316 * @as: #AddressSpace to be accessed
1317 * @addr: address within that address space
5c9eb028 1318 * @attrs: memory transaction attributes
ac1970fb
AK
1319 * @buf: buffer with the data transferred
1320 * @is_write: indicates the transfer direction
1321 */
5c9eb028
PM
1322MemTxResult address_space_rw(AddressSpace *as, hwaddr addr,
1323 MemTxAttrs attrs, uint8_t *buf,
1324 int len, bool is_write);
ac1970fb
AK
1325
1326/**
1327 * address_space_write: write to address space.
1328 *
5c9eb028
PM
1329 * Return a MemTxResult indicating whether the operation succeeded
1330 * or failed (eg unassigned memory, device rejected the transaction,
1331 * IOMMU fault).
fd8aaa76 1332 *
ac1970fb
AK
1333 * @as: #AddressSpace to be accessed
1334 * @addr: address within that address space
5c9eb028 1335 * @attrs: memory transaction attributes
ac1970fb
AK
1336 * @buf: buffer with the data transferred
1337 */
5c9eb028
PM
1338MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
1339 MemTxAttrs attrs,
1340 const uint8_t *buf, int len);
ac1970fb 1341
3cc8f884 1342/* address_space_ld*: load from an address space
50013115
PM
1343 * address_space_st*: store to an address space
1344 *
1345 * These functions perform a load or store of the byte, word,
1346 * longword or quad to the specified address within the AddressSpace.
1347 * The _le suffixed functions treat the data as little endian;
1348 * _be indicates big endian; no suffix indicates "same endianness
1349 * as guest CPU".
1350 *
1351 * The "guest CPU endianness" accessors are deprecated for use outside
1352 * target-* code; devices should be CPU-agnostic and use either the LE
1353 * or the BE accessors.
1354 *
1355 * @as #AddressSpace to be accessed
1356 * @addr: address within that address space
1357 * @val: data value, for stores
1358 * @attrs: memory transaction attributes
1359 * @result: location to write the success/failure of the transaction;
1360 * if NULL, this information is discarded
1361 */
1362uint32_t address_space_ldub(AddressSpace *as, hwaddr addr,
1363 MemTxAttrs attrs, MemTxResult *result);
1364uint32_t address_space_lduw_le(AddressSpace *as, hwaddr addr,
1365 MemTxAttrs attrs, MemTxResult *result);
1366uint32_t address_space_lduw_be(AddressSpace *as, hwaddr addr,
1367 MemTxAttrs attrs, MemTxResult *result);
1368uint32_t address_space_ldl_le(AddressSpace *as, hwaddr addr,
1369 MemTxAttrs attrs, MemTxResult *result);
1370uint32_t address_space_ldl_be(AddressSpace *as, hwaddr addr,
1371 MemTxAttrs attrs, MemTxResult *result);
1372uint64_t address_space_ldq_le(AddressSpace *as, hwaddr addr,
1373 MemTxAttrs attrs, MemTxResult *result);
1374uint64_t address_space_ldq_be(AddressSpace *as, hwaddr addr,
1375 MemTxAttrs attrs, MemTxResult *result);
1376void address_space_stb(AddressSpace *as, hwaddr addr, uint32_t val,
1377 MemTxAttrs attrs, MemTxResult *result);
1378void address_space_stw_le(AddressSpace *as, hwaddr addr, uint32_t val,
1379 MemTxAttrs attrs, MemTxResult *result);
1380void address_space_stw_be(AddressSpace *as, hwaddr addr, uint32_t val,
1381 MemTxAttrs attrs, MemTxResult *result);
1382void address_space_stl_le(AddressSpace *as, hwaddr addr, uint32_t val,
1383 MemTxAttrs attrs, MemTxResult *result);
1384void address_space_stl_be(AddressSpace *as, hwaddr addr, uint32_t val,
1385 MemTxAttrs attrs, MemTxResult *result);
1386void address_space_stq_le(AddressSpace *as, hwaddr addr, uint64_t val,
1387 MemTxAttrs attrs, MemTxResult *result);
1388void address_space_stq_be(AddressSpace *as, hwaddr addr, uint64_t val,
1389 MemTxAttrs attrs, MemTxResult *result);
1390
149f54b5 1391/* address_space_translate: translate an address range into an address space
41063e1e
PB
1392 * into a MemoryRegion and an address range into that section. Should be
1393 * called from an RCU critical section, to avoid that the last reference
1394 * to the returned region disappears after address_space_translate returns.
149f54b5
PB
1395 *
1396 * @as: #AddressSpace to be accessed
1397 * @addr: address within that address space
1398 * @xlat: pointer to address within the returned memory region section's
1399 * #MemoryRegion.
1400 * @len: pointer to length
1401 * @is_write: indicates the transfer direction
1402 */
5c8a00ce
PB
1403MemoryRegion *address_space_translate(AddressSpace *as, hwaddr addr,
1404 hwaddr *xlat, hwaddr *len,
1405 bool is_write);
149f54b5 1406
51644ab7
PB
1407/* address_space_access_valid: check for validity of accessing an address
1408 * space range
1409 *
30951157
AK
1410 * Check whether memory is assigned to the given address space range, and
1411 * access is permitted by any IOMMU regions that are active for the address
1412 * space.
51644ab7
PB
1413 *
1414 * For now, addr and len should be aligned to a page size. This limitation
1415 * will be lifted in the future.
1416 *
1417 * @as: #AddressSpace to be accessed
1418 * @addr: address within that address space
1419 * @len: length of the area to be checked
1420 * @is_write: indicates the transfer direction
1421 */
1422bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write);
1423
ac1970fb
AK
1424/* address_space_map: map a physical memory region into a host virtual address
1425 *
1426 * May map a subset of the requested range, given by and returned in @plen.
1427 * May return %NULL if resources needed to perform the mapping are exhausted.
1428 * Use only for reads OR writes - not for read-modify-write operations.
1429 * Use cpu_register_map_client() to know when retrying the map operation is
1430 * likely to succeed.
1431 *
1432 * @as: #AddressSpace to be accessed
1433 * @addr: address within that address space
1434 * @plen: pointer to length of buffer; updated on return
1435 * @is_write: indicates the transfer direction
1436 */
a8170e5e
AK
1437void *address_space_map(AddressSpace *as, hwaddr addr,
1438 hwaddr *plen, bool is_write);
ac1970fb
AK
1439
1440/* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1441 *
1442 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1443 * the amount of memory that was actually read or written by the caller.
1444 *
1445 * @as: #AddressSpace used
1446 * @addr: address within that address space
1447 * @len: buffer length as returned by address_space_map()
1448 * @access_len: amount of data actually transferred
1449 * @is_write: indicates the transfer direction
1450 */
a8170e5e
AK
1451void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
1452 int is_write, hwaddr access_len);
ac1970fb
AK
1453
1454
a203ac70
PB
1455/* Internal functions, part of the implementation of address_space_read. */
1456MemTxResult address_space_read_continue(AddressSpace *as, hwaddr addr,
1457 MemTxAttrs attrs, uint8_t *buf,
1458 int len, hwaddr addr1, hwaddr l,
1459 MemoryRegion *mr);
3cc8f884
PB
1460MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr,
1461 MemTxAttrs attrs, uint8_t *buf, int len);
0878d0e1 1462void *qemu_map_ram_ptr(RAMBlock *ram_block, ram_addr_t addr);
3cc8f884
PB
1463
1464static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
1465{
1466 if (is_write) {
1467 return memory_region_is_ram(mr) && !mr->readonly;
1468 } else {
1469 return memory_region_is_ram(mr) || memory_region_is_romd(mr);
1470 }
3cc8f884
PB
1471}
1472
1473/**
1474 * address_space_read: read from an address space.
1475 *
1476 * Return a MemTxResult indicating whether the operation succeeded
1477 * or failed (eg unassigned memory, device rejected the transaction,
1478 * IOMMU fault).
1479 *
1480 * @as: #AddressSpace to be accessed
1481 * @addr: address within that address space
1482 * @attrs: memory transaction attributes
1483 * @buf: buffer with the data transferred
1484 */
1485static inline __attribute__((__always_inline__))
1486MemTxResult address_space_read(AddressSpace *as, hwaddr addr, MemTxAttrs attrs,
1487 uint8_t *buf, int len)
1488{
1489 MemTxResult result = MEMTX_OK;
1490 hwaddr l, addr1;
1491 void *ptr;
1492 MemoryRegion *mr;
1493
1494 if (__builtin_constant_p(len)) {
1495 if (len) {
1496 rcu_read_lock();
1497 l = len;
1498 mr = address_space_translate(as, addr, &addr1, &l, false);
1499 if (len == l && memory_access_is_direct(mr, false)) {
0878d0e1 1500 ptr = qemu_map_ram_ptr(mr->ram_block, addr1);
3cc8f884
PB
1501 memcpy(buf, ptr, len);
1502 } else {
1503 result = address_space_read_continue(as, addr, attrs, buf, len,
1504 addr1, l, mr);
1505 }
1506 rcu_read_unlock();
1507 }
1508 } else {
1509 result = address_space_read_full(as, addr, attrs, buf, len);
1510 }
1511 return result;
1512}
a203ac70 1513
093bc2cd
AK
1514#endif
1515
1516#endif