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