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