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