Make address_space_translate{, _cached}() take a MemTxAttrs argument
[qemu.git] / include / exec / memory.h
blobb95ceb272b79fab6333015b0da4beca68a5ef480
1 /*
2 * Physical memory management API
4 * Copyright 2011 Red Hat, Inc. and/or its affiliates
6 * Authors:
7 * Avi Kivity <avi@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #ifndef MEMORY_H
15 #define MEMORY_H
17 #ifndef CONFIG_USER_ONLY
19 #include "exec/cpu-common.h"
20 #include "exec/hwaddr.h"
21 #include "exec/memattrs.h"
22 #include "exec/ramlist.h"
23 #include "qemu/queue.h"
24 #include "qemu/int128.h"
25 #include "qemu/notify.h"
26 #include "qom/object.h"
27 #include "qemu/rcu.h"
28 #include "hw/qdev-core.h"
30 #define RAM_ADDR_INVALID (~(ram_addr_t)0)
32 #define MAX_PHYS_ADDR_SPACE_BITS 62
33 #define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
35 #define TYPE_MEMORY_REGION "qemu:memory-region"
36 #define MEMORY_REGION(obj) \
37 OBJECT_CHECK(MemoryRegion, (obj), TYPE_MEMORY_REGION)
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)
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)
49 typedef struct MemoryRegionOps MemoryRegionOps;
50 typedef struct MemoryRegionMmio MemoryRegionMmio;
52 struct MemoryRegionMmio {
53 CPUReadMemoryFunc *read[3];
54 CPUWriteMemoryFunc *write[3];
57 typedef struct IOMMUTLBEntry IOMMUTLBEntry;
59 /* See address_space_translate: bit 0 is read, bit 1 is write. */
60 typedef enum {
61 IOMMU_NONE = 0,
62 IOMMU_RO = 1,
63 IOMMU_WO = 2,
64 IOMMU_RW = 3,
65 } IOMMUAccessFlags;
67 #define IOMMU_ACCESS_FLAG(r, w) (((r) ? IOMMU_RO : 0) | ((w) ? IOMMU_WO : 0))
69 struct IOMMUTLBEntry {
70 AddressSpace *target_as;
71 hwaddr iova;
72 hwaddr translated_addr;
73 hwaddr addr_mask; /* 0xfff = 4k translation */
74 IOMMUAccessFlags perm;
78 * Bitmap for different IOMMUNotifier capabilities. Each notifier can
79 * register with one or multiple IOMMU Notifier capability bit(s).
81 typedef 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;
89 #define IOMMU_NOTIFIER_ALL (IOMMU_NOTIFIER_MAP | IOMMU_NOTIFIER_UNMAP)
91 struct IOMMUNotifier;
92 typedef void (*IOMMUNotify)(struct IOMMUNotifier *notifier,
93 IOMMUTLBEntry *data);
95 struct IOMMUNotifier {
96 IOMMUNotify notify;
97 IOMMUNotifierFlag notifier_flags;
98 /* Notify for address space range start <= addr <= end */
99 hwaddr start;
100 hwaddr end;
101 QLIST_ENTRY(IOMMUNotifier) node;
103 typedef struct IOMMUNotifier IOMMUNotifier;
105 static inline void iommu_notifier_init(IOMMUNotifier *n, IOMMUNotify fn,
106 IOMMUNotifierFlag flags,
107 hwaddr start, hwaddr end)
109 n->notify = fn;
110 n->notifier_flags = flags;
111 n->start = start;
112 n->end = end;
116 * Memory region callbacks
118 struct MemoryRegionOps {
119 /* Read from the memory region. @addr is relative to @mr; @size is
120 * in bytes. */
121 uint64_t (*read)(void *opaque,
122 hwaddr addr,
123 unsigned size);
124 /* Write to the memory region. @addr is relative to @mr; @size is
125 * in bytes. */
126 void (*write)(void *opaque,
127 hwaddr addr,
128 uint64_t data,
129 unsigned size);
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);
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.
146 * Returns a pointer to a location which contains guest code.
148 void *(*request_ptr)(void *opaque, hwaddr addr, unsigned *size,
149 unsigned *offset);
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.
157 unsigned min_access_size;
158 unsigned max_access_size;
159 /* If true, unaligned accesses are supported. Otherwise unaligned
160 * accesses throw machine checks.
162 bool unaligned;
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).
168 bool (*accepts)(void *opaque, hwaddr addr,
169 unsigned size, bool is_write);
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.
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.
180 unsigned max_access_size;
181 /* If true, unaligned accesses are supported. Otherwise all accesses
182 * are converted to (possibly multiple) naturally aligned accesses.
184 bool unaligned;
185 } impl;
187 /* If .read and .write are not present, old_mmio may be used for
188 * backwards compatibility with old mmio registration
190 const MemoryRegionMmio old_mmio;
193 enum IOMMUMemoryRegionAttr {
194 IOMMU_ATTR_SPAPR_TCE_FD
198 * IOMMUMemoryRegionClass:
200 * All IOMMU implementations need to subclass TYPE_IOMMU_MEMORY_REGION
201 * and provide an implementation of at least the @translate method here
202 * to handle requests to the memory region. Other methods are optional.
204 * The IOMMU implementation must use the IOMMU notifier infrastructure
205 * to report whenever mappings are changed, by calling
206 * memory_region_notify_iommu() (or, if necessary, by calling
207 * memory_region_notify_one() for each registered notifier).
209 typedef struct IOMMUMemoryRegionClass {
210 /* private */
211 struct DeviceClass parent_class;
214 * Return a TLB entry that contains a given address.
216 * The IOMMUAccessFlags indicated via @flag are optional and may
217 * be specified as IOMMU_NONE to indicate that the caller needs
218 * the full translation information for both reads and writes. If
219 * the access flags are specified then the IOMMU implementation
220 * may use this as an optimization, to stop doing a page table
221 * walk as soon as it knows that the requested permissions are not
222 * allowed. If IOMMU_NONE is passed then the IOMMU must do the
223 * full page table walk and report the permissions in the returned
224 * IOMMUTLBEntry. (Note that this implies that an IOMMU may not
225 * return different mappings for reads and writes.)
227 * The returned information remains valid while the caller is
228 * holding the big QEMU lock or is inside an RCU critical section;
229 * if the caller wishes to cache the mapping beyond that it must
230 * register an IOMMU notifier so it can invalidate its cached
231 * information when the IOMMU mapping changes.
233 * @iommu: the IOMMUMemoryRegion
234 * @hwaddr: address to be translated within the memory region
235 * @flag: requested access permissions
237 IOMMUTLBEntry (*translate)(IOMMUMemoryRegion *iommu, hwaddr addr,
238 IOMMUAccessFlags flag);
239 /* Returns minimum supported page size in bytes.
240 * If this method is not provided then the minimum is assumed to
241 * be TARGET_PAGE_SIZE.
243 * @iommu: the IOMMUMemoryRegion
245 uint64_t (*get_min_page_size)(IOMMUMemoryRegion *iommu);
246 /* Called when IOMMU Notifier flag changes (ie when the set of
247 * events which IOMMU users are requesting notification for changes).
248 * Optional method -- need not be provided if the IOMMU does not
249 * need to know exactly which events must be notified.
251 * @iommu: the IOMMUMemoryRegion
252 * @old_flags: events which previously needed to be notified
253 * @new_flags: events which now need to be notified
255 void (*notify_flag_changed)(IOMMUMemoryRegion *iommu,
256 IOMMUNotifierFlag old_flags,
257 IOMMUNotifierFlag new_flags);
258 /* Called to handle memory_region_iommu_replay().
260 * The default implementation of memory_region_iommu_replay() is to
261 * call the IOMMU translate method for every page in the address space
262 * with flag == IOMMU_NONE and then call the notifier if translate
263 * returns a valid mapping. If this method is implemented then it
264 * overrides the default behaviour, and must provide the full semantics
265 * of memory_region_iommu_replay(), by calling @notifier for every
266 * translation present in the IOMMU.
268 * Optional method -- an IOMMU only needs to provide this method
269 * if the default is inefficient or produces undesirable side effects.
271 * Note: this is not related to record-and-replay functionality.
273 void (*replay)(IOMMUMemoryRegion *iommu, IOMMUNotifier *notifier);
275 /* Get IOMMU misc attributes. This is an optional method that
276 * can be used to allow users of the IOMMU to get implementation-specific
277 * information. The IOMMU implements this method to handle calls
278 * by IOMMU users to memory_region_iommu_get_attr() by filling in
279 * the arbitrary data pointer for any IOMMUMemoryRegionAttr values that
280 * the IOMMU supports. If the method is unimplemented then
281 * memory_region_iommu_get_attr() will always return -EINVAL.
283 * @iommu: the IOMMUMemoryRegion
284 * @attr: attribute being queried
285 * @data: memory to fill in with the attribute data
287 * Returns 0 on success, or a negative errno; in particular
288 * returns -EINVAL for unrecognized or unimplemented attribute types.
290 int (*get_attr)(IOMMUMemoryRegion *iommu, enum IOMMUMemoryRegionAttr attr,
291 void *data);
292 } IOMMUMemoryRegionClass;
294 typedef struct CoalescedMemoryRange CoalescedMemoryRange;
295 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
297 struct MemoryRegion {
298 Object parent_obj;
300 /* All fields are private - violators will be prosecuted */
302 /* The following fields should fit in a cache line */
303 bool romd_mode;
304 bool ram;
305 bool subpage;
306 bool readonly; /* For RAM regions */
307 bool rom_device;
308 bool flush_coalesced_mmio;
309 bool global_locking;
310 uint8_t dirty_log_mask;
311 bool is_iommu;
312 RAMBlock *ram_block;
313 Object *owner;
315 const MemoryRegionOps *ops;
316 void *opaque;
317 MemoryRegion *container;
318 Int128 size;
319 hwaddr addr;
320 void (*destructor)(MemoryRegion *mr);
321 uint64_t align;
322 bool terminates;
323 bool ram_device;
324 bool enabled;
325 bool warning_printed; /* For reservations */
326 uint8_t vga_logging_count;
327 MemoryRegion *alias;
328 hwaddr alias_offset;
329 int32_t priority;
330 QTAILQ_HEAD(subregions, MemoryRegion) subregions;
331 QTAILQ_ENTRY(MemoryRegion) subregions_link;
332 QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced;
333 const char *name;
334 unsigned ioeventfd_nb;
335 MemoryRegionIoeventfd *ioeventfds;
338 struct IOMMUMemoryRegion {
339 MemoryRegion parent_obj;
341 QLIST_HEAD(, IOMMUNotifier) iommu_notify;
342 IOMMUNotifierFlag iommu_notify_flags;
345 #define IOMMU_NOTIFIER_FOREACH(n, mr) \
346 QLIST_FOREACH((n), &(mr)->iommu_notify, node)
349 * MemoryListener: callbacks structure for updates to the physical memory map
351 * Allows a component to adjust to changes in the guest-visible memory map.
352 * Use with memory_listener_register() and memory_listener_unregister().
354 struct MemoryListener {
355 void (*begin)(MemoryListener *listener);
356 void (*commit)(MemoryListener *listener);
357 void (*region_add)(MemoryListener *listener, MemoryRegionSection *section);
358 void (*region_del)(MemoryListener *listener, MemoryRegionSection *section);
359 void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section);
360 void (*log_start)(MemoryListener *listener, MemoryRegionSection *section,
361 int old, int new);
362 void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section,
363 int old, int new);
364 void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section);
365 void (*log_global_start)(MemoryListener *listener);
366 void (*log_global_stop)(MemoryListener *listener);
367 void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section,
368 bool match_data, uint64_t data, EventNotifier *e);
369 void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section,
370 bool match_data, uint64_t data, EventNotifier *e);
371 void (*coalesced_mmio_add)(MemoryListener *listener, MemoryRegionSection *section,
372 hwaddr addr, hwaddr len);
373 void (*coalesced_mmio_del)(MemoryListener *listener, MemoryRegionSection *section,
374 hwaddr addr, hwaddr len);
375 /* Lower = earlier (during add), later (during del) */
376 unsigned priority;
377 AddressSpace *address_space;
378 QTAILQ_ENTRY(MemoryListener) link;
379 QTAILQ_ENTRY(MemoryListener) link_as;
383 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
385 struct AddressSpace {
386 /* All fields are private. */
387 struct rcu_head rcu;
388 char *name;
389 MemoryRegion *root;
391 /* Accessed via RCU. */
392 struct FlatView *current_map;
394 int ioeventfd_nb;
395 struct MemoryRegionIoeventfd *ioeventfds;
396 QTAILQ_HEAD(memory_listeners_as, MemoryListener) listeners;
397 QTAILQ_ENTRY(AddressSpace) address_spaces_link;
400 typedef struct AddressSpaceDispatch AddressSpaceDispatch;
401 typedef struct FlatRange FlatRange;
403 /* Flattened global view of current active memory hierarchy. Kept in sorted
404 * order.
406 struct FlatView {
407 struct rcu_head rcu;
408 unsigned ref;
409 FlatRange *ranges;
410 unsigned nr;
411 unsigned nr_allocated;
412 struct AddressSpaceDispatch *dispatch;
413 MemoryRegion *root;
416 static inline FlatView *address_space_to_flatview(AddressSpace *as)
418 return atomic_rcu_read(&as->current_map);
423 * MemoryRegionSection: describes a fragment of a #MemoryRegion
425 * @mr: the region, or %NULL if empty
426 * @fv: the flat view of the address space the region is mapped in
427 * @offset_within_region: the beginning of the section, relative to @mr's start
428 * @size: the size of the section; will not exceed @mr's boundaries
429 * @offset_within_address_space: the address of the first byte of the section
430 * relative to the region's address space
431 * @readonly: writes to this section are ignored
433 struct MemoryRegionSection {
434 MemoryRegion *mr;
435 FlatView *fv;
436 hwaddr offset_within_region;
437 Int128 size;
438 hwaddr offset_within_address_space;
439 bool readonly;
443 * memory_region_init: Initialize a memory region
445 * The region typically acts as a container for other memory regions. Use
446 * memory_region_add_subregion() to add subregions.
448 * @mr: the #MemoryRegion to be initialized
449 * @owner: the object that tracks the region's reference count
450 * @name: used for debugging; not visible to the user or ABI
451 * @size: size of the region; any subregions beyond this size will be clipped
453 void memory_region_init(MemoryRegion *mr,
454 struct Object *owner,
455 const char *name,
456 uint64_t size);
459 * memory_region_ref: Add 1 to a memory region's reference count
461 * Whenever memory regions are accessed outside the BQL, they need to be
462 * preserved against hot-unplug. MemoryRegions actually do not have their
463 * own reference count; they piggyback on a QOM object, their "owner".
464 * This function adds a reference to the owner.
466 * All MemoryRegions must have an owner if they can disappear, even if the
467 * device they belong to operates exclusively under the BQL. This is because
468 * the region could be returned at any time by memory_region_find, and this
469 * is usually under guest control.
471 * @mr: the #MemoryRegion
473 void memory_region_ref(MemoryRegion *mr);
476 * memory_region_unref: Remove 1 to a memory region's reference count
478 * Whenever memory regions are accessed outside the BQL, they need to be
479 * preserved against hot-unplug. MemoryRegions actually do not have their
480 * own reference count; they piggyback on a QOM object, their "owner".
481 * This function removes a reference to the owner and possibly destroys it.
483 * @mr: the #MemoryRegion
485 void memory_region_unref(MemoryRegion *mr);
488 * memory_region_init_io: Initialize an I/O memory region.
490 * Accesses into the region will cause the callbacks in @ops to be called.
491 * if @size is nonzero, subregions will be clipped to @size.
493 * @mr: the #MemoryRegion to be initialized.
494 * @owner: the object that tracks the region's reference count
495 * @ops: a structure containing read and write callbacks to be used when
496 * I/O is performed on the region.
497 * @opaque: passed to the read and write callbacks of the @ops structure.
498 * @name: used for debugging; not visible to the user or ABI
499 * @size: size of the region.
501 void memory_region_init_io(MemoryRegion *mr,
502 struct Object *owner,
503 const MemoryRegionOps *ops,
504 void *opaque,
505 const char *name,
506 uint64_t size);
509 * memory_region_init_ram_nomigrate: Initialize RAM memory region. Accesses
510 * into the region will modify memory
511 * directly.
513 * @mr: the #MemoryRegion to be initialized.
514 * @owner: the object that tracks the region's reference count
515 * @name: Region name, becomes part of RAMBlock name used in migration stream
516 * must be unique within any device
517 * @size: size of the region.
518 * @errp: pointer to Error*, to store an error if it happens.
520 * Note that this function does not do anything to cause the data in the
521 * RAM memory region to be migrated; that is the responsibility of the caller.
523 void memory_region_init_ram_nomigrate(MemoryRegion *mr,
524 struct Object *owner,
525 const char *name,
526 uint64_t size,
527 Error **errp);
530 * memory_region_init_ram_shared_nomigrate: Initialize RAM memory region.
531 * Accesses into the region will
532 * modify memory directly.
534 * @mr: the #MemoryRegion to be initialized.
535 * @owner: the object that tracks the region's reference count
536 * @name: Region name, becomes part of RAMBlock name used in migration stream
537 * must be unique within any device
538 * @size: size of the region.
539 * @share: allow remapping RAM to different addresses
540 * @errp: pointer to Error*, to store an error if it happens.
542 * Note that this function is similar to memory_region_init_ram_nomigrate.
543 * The only difference is part of the RAM region can be remapped.
545 void memory_region_init_ram_shared_nomigrate(MemoryRegion *mr,
546 struct Object *owner,
547 const char *name,
548 uint64_t size,
549 bool share,
550 Error **errp);
553 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
554 * RAM. Accesses into the region will
555 * modify memory directly. Only an initial
556 * portion of this RAM is actually used.
557 * The used size can change across reboots.
559 * @mr: the #MemoryRegion to be initialized.
560 * @owner: the object that tracks the region's reference count
561 * @name: Region name, becomes part of RAMBlock name used in migration stream
562 * must be unique within any device
563 * @size: used size of the region.
564 * @max_size: max size of the region.
565 * @resized: callback to notify owner about used size change.
566 * @errp: pointer to Error*, to store an error if it happens.
568 * Note that this function does not do anything to cause the data in the
569 * RAM memory region to be migrated; that is the responsibility of the caller.
571 void memory_region_init_resizeable_ram(MemoryRegion *mr,
572 struct Object *owner,
573 const char *name,
574 uint64_t size,
575 uint64_t max_size,
576 void (*resized)(const char*,
577 uint64_t length,
578 void *host),
579 Error **errp);
580 #ifdef __linux__
582 * memory_region_init_ram_from_file: Initialize RAM memory region with a
583 * mmap-ed backend.
585 * @mr: the #MemoryRegion to be initialized.
586 * @owner: the object that tracks the region's reference count
587 * @name: Region name, becomes part of RAMBlock name used in migration stream
588 * must be unique within any device
589 * @size: size of the region.
590 * @align: alignment of the region base address; if 0, the default alignment
591 * (getpagesize()) will be used.
592 * @share: %true if memory must be mmaped with the MAP_SHARED flag
593 * @path: the path in which to allocate the RAM.
594 * @errp: pointer to Error*, to store an error if it happens.
596 * Note that this function does not do anything to cause the data in the
597 * RAM memory region to be migrated; that is the responsibility of the caller.
599 void memory_region_init_ram_from_file(MemoryRegion *mr,
600 struct Object *owner,
601 const char *name,
602 uint64_t size,
603 uint64_t align,
604 bool share,
605 const char *path,
606 Error **errp);
609 * memory_region_init_ram_from_fd: Initialize RAM memory region with a
610 * mmap-ed backend.
612 * @mr: the #MemoryRegion to be initialized.
613 * @owner: the object that tracks the region's reference count
614 * @name: the name of the region.
615 * @size: size of the region.
616 * @share: %true if memory must be mmaped with the MAP_SHARED flag
617 * @fd: the fd to mmap.
618 * @errp: pointer to Error*, to store an error if it happens.
620 * Note that this function does not do anything to cause the data in the
621 * RAM memory region to be migrated; that is the responsibility of the caller.
623 void memory_region_init_ram_from_fd(MemoryRegion *mr,
624 struct Object *owner,
625 const char *name,
626 uint64_t size,
627 bool share,
628 int fd,
629 Error **errp);
630 #endif
633 * memory_region_init_ram_ptr: Initialize RAM memory region from a
634 * user-provided pointer. Accesses into the
635 * region will modify memory directly.
637 * @mr: the #MemoryRegion to be initialized.
638 * @owner: the object that tracks the region's reference count
639 * @name: Region name, becomes part of RAMBlock name used in migration stream
640 * must be unique within any device
641 * @size: size of the region.
642 * @ptr: memory to be mapped; must contain at least @size bytes.
644 * Note that this function does not do anything to cause the data in the
645 * RAM memory region to be migrated; that is the responsibility of the caller.
647 void memory_region_init_ram_ptr(MemoryRegion *mr,
648 struct Object *owner,
649 const char *name,
650 uint64_t size,
651 void *ptr);
654 * memory_region_init_ram_device_ptr: Initialize RAM device memory region from
655 * a user-provided pointer.
657 * A RAM device represents a mapping to a physical device, such as to a PCI
658 * MMIO BAR of an vfio-pci assigned device. The memory region may be mapped
659 * into the VM address space and access to the region will modify memory
660 * directly. However, the memory region should not be included in a memory
661 * dump (device may not be enabled/mapped at the time of the dump), and
662 * operations incompatible with manipulating MMIO should be avoided. Replaces
663 * skip_dump flag.
665 * @mr: the #MemoryRegion to be initialized.
666 * @owner: the object that tracks the region's reference count
667 * @name: the name of the region.
668 * @size: size of the region.
669 * @ptr: memory to be mapped; must contain at least @size bytes.
671 * Note that this function does not do anything to cause the data in the
672 * RAM memory region to be migrated; that is the responsibility of the caller.
673 * (For RAM device memory regions, migrating the contents rarely makes sense.)
675 void memory_region_init_ram_device_ptr(MemoryRegion *mr,
676 struct Object *owner,
677 const char *name,
678 uint64_t size,
679 void *ptr);
682 * memory_region_init_alias: Initialize a memory region that aliases all or a
683 * part of another memory region.
685 * @mr: the #MemoryRegion to be initialized.
686 * @owner: the object that tracks the region's reference count
687 * @name: used for debugging; not visible to the user or ABI
688 * @orig: the region to be referenced; @mr will be equivalent to
689 * @orig between @offset and @offset + @size - 1.
690 * @offset: start of the section in @orig to be referenced.
691 * @size: size of the region.
693 void memory_region_init_alias(MemoryRegion *mr,
694 struct Object *owner,
695 const char *name,
696 MemoryRegion *orig,
697 hwaddr offset,
698 uint64_t size);
701 * memory_region_init_rom_nomigrate: Initialize a ROM memory region.
703 * This has the same effect as calling memory_region_init_ram_nomigrate()
704 * and then marking the resulting region read-only with
705 * memory_region_set_readonly().
707 * Note that this function does not do anything to cause the data in the
708 * RAM side of the memory region to be migrated; that is the responsibility
709 * of the caller.
711 * @mr: the #MemoryRegion to be initialized.
712 * @owner: the object that tracks the region's reference count
713 * @name: Region name, becomes part of RAMBlock name used in migration stream
714 * must be unique within any device
715 * @size: size of the region.
716 * @errp: pointer to Error*, to store an error if it happens.
718 void memory_region_init_rom_nomigrate(MemoryRegion *mr,
719 struct Object *owner,
720 const char *name,
721 uint64_t size,
722 Error **errp);
725 * memory_region_init_rom_device_nomigrate: Initialize a ROM memory region.
726 * Writes are handled via callbacks.
728 * Note that this function does not do anything to cause the data in the
729 * RAM side of the memory region to be migrated; that is the responsibility
730 * of the caller.
732 * @mr: the #MemoryRegion to be initialized.
733 * @owner: the object that tracks the region's reference count
734 * @ops: callbacks for write access handling (must not be NULL).
735 * @opaque: passed to the read and write callbacks of the @ops structure.
736 * @name: Region name, becomes part of RAMBlock name used in migration stream
737 * must be unique within any device
738 * @size: size of the region.
739 * @errp: pointer to Error*, to store an error if it happens.
741 void memory_region_init_rom_device_nomigrate(MemoryRegion *mr,
742 struct Object *owner,
743 const MemoryRegionOps *ops,
744 void *opaque,
745 const char *name,
746 uint64_t size,
747 Error **errp);
750 * memory_region_init_reservation: Initialize a memory region that reserves
751 * I/O space.
753 * A reservation region primariy serves debugging purposes. It claims I/O
754 * space that is not supposed to be handled by QEMU itself. Any access via
755 * the memory API will cause an abort().
756 * This function is deprecated. Use memory_region_init_io() with NULL
757 * callbacks instead.
759 * @mr: the #MemoryRegion to be initialized
760 * @owner: the object that tracks the region's reference count
761 * @name: used for debugging; not visible to the user or ABI
762 * @size: size of the region.
764 static inline void memory_region_init_reservation(MemoryRegion *mr,
765 Object *owner,
766 const char *name,
767 uint64_t size)
769 memory_region_init_io(mr, owner, NULL, mr, name, size);
773 * memory_region_init_iommu: Initialize a memory region of a custom type
774 * that translates addresses
776 * An IOMMU region translates addresses and forwards accesses to a target
777 * memory region.
779 * The IOMMU implementation must define a subclass of TYPE_IOMMU_MEMORY_REGION.
780 * @_iommu_mr should be a pointer to enough memory for an instance of
781 * that subclass, @instance_size is the size of that subclass, and
782 * @mrtypename is its name. This function will initialize @_iommu_mr as an
783 * instance of the subclass, and its methods will then be called to handle
784 * accesses to the memory region. See the documentation of
785 * #IOMMUMemoryRegionClass for further details.
787 * @_iommu_mr: the #IOMMUMemoryRegion to be initialized
788 * @instance_size: the IOMMUMemoryRegion subclass instance size
789 * @mrtypename: the type name of the #IOMMUMemoryRegion
790 * @owner: the object that tracks the region's reference count
791 * @name: used for debugging; not visible to the user or ABI
792 * @size: size of the region.
794 void memory_region_init_iommu(void *_iommu_mr,
795 size_t instance_size,
796 const char *mrtypename,
797 Object *owner,
798 const char *name,
799 uint64_t size);
802 * memory_region_init_ram - Initialize RAM memory region. Accesses into the
803 * region will modify memory directly.
805 * @mr: the #MemoryRegion to be initialized
806 * @owner: the object that tracks the region's reference count (must be
807 * TYPE_DEVICE or a subclass of TYPE_DEVICE, or NULL)
808 * @name: name of the memory region
809 * @size: size of the region in bytes
810 * @errp: pointer to Error*, to store an error if it happens.
812 * This function allocates RAM for a board model or device, and
813 * arranges for it to be migrated (by calling vmstate_register_ram()
814 * if @owner is a DeviceState, or vmstate_register_ram_global() if
815 * @owner is NULL).
817 * TODO: Currently we restrict @owner to being either NULL (for
818 * global RAM regions with no owner) or devices, so that we can
819 * give the RAM block a unique name for migration purposes.
820 * We should lift this restriction and allow arbitrary Objects.
821 * If you pass a non-NULL non-device @owner then we will assert.
823 void memory_region_init_ram(MemoryRegion *mr,
824 struct Object *owner,
825 const char *name,
826 uint64_t size,
827 Error **errp);
830 * memory_region_init_rom: Initialize a ROM memory region.
832 * This has the same effect as calling memory_region_init_ram()
833 * and then marking the resulting region read-only with
834 * memory_region_set_readonly(). This includes arranging for the
835 * contents to be migrated.
837 * TODO: Currently we restrict @owner to being either NULL (for
838 * global RAM regions with no owner) or devices, so that we can
839 * give the RAM block a unique name for migration purposes.
840 * We should lift this restriction and allow arbitrary Objects.
841 * If you pass a non-NULL non-device @owner then we will assert.
843 * @mr: the #MemoryRegion to be initialized.
844 * @owner: the object that tracks the region's reference count
845 * @name: Region name, becomes part of RAMBlock name used in migration stream
846 * must be unique within any device
847 * @size: size of the region.
848 * @errp: pointer to Error*, to store an error if it happens.
850 void memory_region_init_rom(MemoryRegion *mr,
851 struct Object *owner,
852 const char *name,
853 uint64_t size,
854 Error **errp);
857 * memory_region_init_rom_device: Initialize a ROM memory region.
858 * Writes are handled via callbacks.
860 * This function initializes a memory region backed by RAM for reads
861 * and callbacks for writes, and arranges for the RAM backing to
862 * be migrated (by calling vmstate_register_ram()
863 * if @owner is a DeviceState, or vmstate_register_ram_global() if
864 * @owner is NULL).
866 * TODO: Currently we restrict @owner to being either NULL (for
867 * global RAM regions with no owner) or devices, so that we can
868 * give the RAM block a unique name for migration purposes.
869 * We should lift this restriction and allow arbitrary Objects.
870 * If you pass a non-NULL non-device @owner then we will assert.
872 * @mr: the #MemoryRegion to be initialized.
873 * @owner: the object that tracks the region's reference count
874 * @ops: callbacks for write access handling (must not be NULL).
875 * @name: Region name, becomes part of RAMBlock name used in migration stream
876 * must be unique within any device
877 * @size: size of the region.
878 * @errp: pointer to Error*, to store an error if it happens.
880 void memory_region_init_rom_device(MemoryRegion *mr,
881 struct Object *owner,
882 const MemoryRegionOps *ops,
883 void *opaque,
884 const char *name,
885 uint64_t size,
886 Error **errp);
890 * memory_region_owner: get a memory region's owner.
892 * @mr: the memory region being queried.
894 struct Object *memory_region_owner(MemoryRegion *mr);
897 * memory_region_size: get a memory region's size.
899 * @mr: the memory region being queried.
901 uint64_t memory_region_size(MemoryRegion *mr);
904 * memory_region_is_ram: check whether a memory region is random access
906 * Returns %true is a memory region is random access.
908 * @mr: the memory region being queried
910 static inline bool memory_region_is_ram(MemoryRegion *mr)
912 return mr->ram;
916 * memory_region_is_ram_device: check whether a memory region is a ram device
918 * Returns %true is a memory region is a device backed ram region
920 * @mr: the memory region being queried
922 bool memory_region_is_ram_device(MemoryRegion *mr);
925 * memory_region_is_romd: check whether a memory region is in ROMD mode
927 * Returns %true if a memory region is a ROM device and currently set to allow
928 * direct reads.
930 * @mr: the memory region being queried
932 static inline bool memory_region_is_romd(MemoryRegion *mr)
934 return mr->rom_device && mr->romd_mode;
938 * memory_region_get_iommu: check whether a memory region is an iommu
940 * Returns pointer to IOMMUMemoryRegion if a memory region is an iommu,
941 * otherwise NULL.
943 * @mr: the memory region being queried
945 static inline IOMMUMemoryRegion *memory_region_get_iommu(MemoryRegion *mr)
947 if (mr->alias) {
948 return memory_region_get_iommu(mr->alias);
950 if (mr->is_iommu) {
951 return (IOMMUMemoryRegion *) mr;
953 return NULL;
957 * memory_region_get_iommu_class_nocheck: returns iommu memory region class
958 * if an iommu or NULL if not
960 * Returns pointer to IOMMUMemoryRegionClass if a memory region is an iommu,
961 * otherwise NULL. This is fast path avoiding QOM checking, use with caution.
963 * @mr: the memory region being queried
965 static inline IOMMUMemoryRegionClass *memory_region_get_iommu_class_nocheck(
966 IOMMUMemoryRegion *iommu_mr)
968 return (IOMMUMemoryRegionClass *) (((Object *)iommu_mr)->class);
971 #define memory_region_is_iommu(mr) (memory_region_get_iommu(mr) != NULL)
974 * memory_region_iommu_get_min_page_size: get minimum supported page size
975 * for an iommu
977 * Returns minimum supported page size for an iommu.
979 * @iommu_mr: the memory region being queried
981 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion *iommu_mr);
984 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
986 * The notification type will be decided by entry.perm bits:
988 * - For UNMAP (cache invalidation) notifies: set entry.perm to IOMMU_NONE.
989 * - For MAP (newly added entry) notifies: set entry.perm to the
990 * permission of the page (which is definitely !IOMMU_NONE).
992 * Note: for any IOMMU implementation, an in-place mapping change
993 * should be notified with an UNMAP followed by a MAP.
995 * @iommu_mr: the memory region that was changed
996 * @entry: the new entry in the IOMMU translation table. The entry
997 * replaces all old entries for the same virtual I/O address range.
998 * Deleted entries have .@perm == 0.
1000 void memory_region_notify_iommu(IOMMUMemoryRegion *iommu_mr,
1001 IOMMUTLBEntry entry);
1004 * memory_region_notify_one: notify a change in an IOMMU translation
1005 * entry to a single notifier
1007 * This works just like memory_region_notify_iommu(), but it only
1008 * notifies a specific notifier, not all of them.
1010 * @notifier: the notifier to be notified
1011 * @entry: the new entry in the IOMMU translation table. The entry
1012 * replaces all old entries for the same virtual I/O address range.
1013 * Deleted entries have .@perm == 0.
1015 void memory_region_notify_one(IOMMUNotifier *notifier,
1016 IOMMUTLBEntry *entry);
1019 * memory_region_register_iommu_notifier: register a notifier for changes to
1020 * IOMMU translation entries.
1022 * @mr: the memory region to observe
1023 * @n: the IOMMUNotifier to be added; the notify callback receives a
1024 * pointer to an #IOMMUTLBEntry as the opaque value; the pointer
1025 * ceases to be valid on exit from the notifier.
1027 void memory_region_register_iommu_notifier(MemoryRegion *mr,
1028 IOMMUNotifier *n);
1031 * memory_region_iommu_replay: replay existing IOMMU translations to
1032 * a notifier with the minimum page granularity returned by
1033 * mr->iommu_ops->get_page_size().
1035 * Note: this is not related to record-and-replay functionality.
1037 * @iommu_mr: the memory region to observe
1038 * @n: the notifier to which to replay iommu mappings
1040 void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n);
1043 * memory_region_iommu_replay_all: replay existing IOMMU translations
1044 * to all the notifiers registered.
1046 * Note: this is not related to record-and-replay functionality.
1048 * @iommu_mr: the memory region to observe
1050 void memory_region_iommu_replay_all(IOMMUMemoryRegion *iommu_mr);
1053 * memory_region_unregister_iommu_notifier: unregister a notifier for
1054 * changes to IOMMU translation entries.
1056 * @mr: the memory region which was observed and for which notity_stopped()
1057 * needs to be called
1058 * @n: the notifier to be removed.
1060 void memory_region_unregister_iommu_notifier(MemoryRegion *mr,
1061 IOMMUNotifier *n);
1064 * memory_region_iommu_get_attr: return an IOMMU attr if get_attr() is
1065 * defined on the IOMMU.
1067 * Returns 0 on success, or a negative errno otherwise. In particular,
1068 * -EINVAL indicates that the IOMMU does not support the requested
1069 * attribute.
1071 * @iommu_mr: the memory region
1072 * @attr: the requested attribute
1073 * @data: a pointer to the requested attribute data
1075 int memory_region_iommu_get_attr(IOMMUMemoryRegion *iommu_mr,
1076 enum IOMMUMemoryRegionAttr attr,
1077 void *data);
1080 * memory_region_name: get a memory region's name
1082 * Returns the string that was used to initialize the memory region.
1084 * @mr: the memory region being queried
1086 const char *memory_region_name(const MemoryRegion *mr);
1089 * memory_region_is_logging: return whether a memory region is logging writes
1091 * Returns %true if the memory region is logging writes for the given client
1093 * @mr: the memory region being queried
1094 * @client: the client being queried
1096 bool memory_region_is_logging(MemoryRegion *mr, uint8_t client);
1099 * memory_region_get_dirty_log_mask: return the clients for which a
1100 * memory region is logging writes.
1102 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
1103 * are the bit indices.
1105 * @mr: the memory region being queried
1107 uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr);
1110 * memory_region_is_rom: check whether a memory region is ROM
1112 * Returns %true is a memory region is read-only memory.
1114 * @mr: the memory region being queried
1116 static inline bool memory_region_is_rom(MemoryRegion *mr)
1118 return mr->ram && mr->readonly;
1123 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
1125 * Returns a file descriptor backing a file-based RAM memory region,
1126 * or -1 if the region is not a file-based RAM memory region.
1128 * @mr: the RAM or alias memory region being queried.
1130 int memory_region_get_fd(MemoryRegion *mr);
1133 * memory_region_from_host: Convert a pointer into a RAM memory region
1134 * and an offset within it.
1136 * Given a host pointer inside a RAM memory region (created with
1137 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
1138 * the MemoryRegion and the offset within it.
1140 * Use with care; by the time this function returns, the returned pointer is
1141 * not protected by RCU anymore. If the caller is not within an RCU critical
1142 * section and does not hold the iothread lock, it must have other means of
1143 * protecting the pointer, such as a reference to the region that includes
1144 * the incoming ram_addr_t.
1146 * @ptr: the host pointer to be converted
1147 * @offset: the offset within memory region
1149 MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset);
1152 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
1154 * Returns a host pointer to a RAM memory region (created with
1155 * memory_region_init_ram() or memory_region_init_ram_ptr()).
1157 * Use with care; by the time this function returns, the returned pointer is
1158 * not protected by RCU anymore. If the caller is not within an RCU critical
1159 * section and does not hold the iothread lock, it must have other means of
1160 * protecting the pointer, such as a reference to the region that includes
1161 * the incoming ram_addr_t.
1163 * @mr: the memory region being queried.
1165 void *memory_region_get_ram_ptr(MemoryRegion *mr);
1167 /* memory_region_ram_resize: Resize a RAM region.
1169 * Only legal before guest might have detected the memory size: e.g. on
1170 * incoming migration, or right after reset.
1172 * @mr: a memory region created with @memory_region_init_resizeable_ram.
1173 * @newsize: the new size the region
1174 * @errp: pointer to Error*, to store an error if it happens.
1176 void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize,
1177 Error **errp);
1180 * memory_region_set_log: Turn dirty logging on or off for a region.
1182 * Turns dirty logging on or off for a specified client (display, migration).
1183 * Only meaningful for RAM regions.
1185 * @mr: the memory region being updated.
1186 * @log: whether dirty logging is to be enabled or disabled.
1187 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
1189 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
1192 * memory_region_get_dirty: Check whether a range of bytes is dirty
1193 * for a specified client.
1195 * Checks whether a range of bytes has been written to since the last
1196 * call to memory_region_reset_dirty() with the same @client. Dirty logging
1197 * must be enabled.
1199 * @mr: the memory region being queried.
1200 * @addr: the address (relative to the start of the region) being queried.
1201 * @size: the size of the range being queried.
1202 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
1203 * %DIRTY_MEMORY_VGA.
1205 bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
1206 hwaddr size, unsigned client);
1209 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
1211 * Marks a range of bytes as dirty, after it has been dirtied outside
1212 * guest code.
1214 * @mr: the memory region being dirtied.
1215 * @addr: the address (relative to the start of the region) being dirtied.
1216 * @size: size of the range being dirtied.
1218 void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
1219 hwaddr size);
1222 * memory_region_snapshot_and_clear_dirty: Get a snapshot of the dirty
1223 * bitmap and clear it.
1225 * Creates a snapshot of the dirty bitmap, clears the dirty bitmap and
1226 * returns the snapshot. The snapshot can then be used to query dirty
1227 * status, using memory_region_snapshot_get_dirty. Snapshotting allows
1228 * querying the same page multiple times, which is especially useful for
1229 * display updates where the scanlines often are not page aligned.
1231 * The dirty bitmap region which gets copyed into the snapshot (and
1232 * cleared afterwards) can be larger than requested. The boundaries
1233 * are rounded up/down so complete bitmap longs (covering 64 pages on
1234 * 64bit hosts) can be copied over into the bitmap snapshot. Which
1235 * isn't a problem for display updates as the extra pages are outside
1236 * the visible area, and in case the visible area changes a full
1237 * display redraw is due anyway. Should other use cases for this
1238 * function emerge we might have to revisit this implementation
1239 * detail.
1241 * Use g_free to release DirtyBitmapSnapshot.
1243 * @mr: the memory region being queried.
1244 * @addr: the address (relative to the start of the region) being queried.
1245 * @size: the size of the range being queried.
1246 * @client: the user of the logging information; typically %DIRTY_MEMORY_VGA.
1248 DirtyBitmapSnapshot *memory_region_snapshot_and_clear_dirty(MemoryRegion *mr,
1249 hwaddr addr,
1250 hwaddr size,
1251 unsigned client);
1254 * memory_region_snapshot_get_dirty: Check whether a range of bytes is dirty
1255 * in the specified dirty bitmap snapshot.
1257 * @mr: the memory region being queried.
1258 * @snap: the dirty bitmap snapshot
1259 * @addr: the address (relative to the start of the region) being queried.
1260 * @size: the size of the range being queried.
1262 bool memory_region_snapshot_get_dirty(MemoryRegion *mr,
1263 DirtyBitmapSnapshot *snap,
1264 hwaddr addr, hwaddr size);
1267 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
1268 * client.
1270 * Marks a range of pages as no longer dirty.
1272 * @mr: the region being updated.
1273 * @addr: the start of the subrange being cleaned.
1274 * @size: the size of the subrange being cleaned.
1275 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
1276 * %DIRTY_MEMORY_VGA.
1278 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
1279 hwaddr size, unsigned client);
1282 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
1284 * Allows a memory region to be marked as read-only (turning it into a ROM).
1285 * only useful on RAM regions.
1287 * @mr: the region being updated.
1288 * @readonly: whether rhe region is to be ROM or RAM.
1290 void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
1293 * memory_region_rom_device_set_romd: enable/disable ROMD mode
1295 * Allows a ROM device (initialized with memory_region_init_rom_device() to
1296 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
1297 * device is mapped to guest memory and satisfies read access directly.
1298 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
1299 * Writes are always handled by the #MemoryRegion.write function.
1301 * @mr: the memory region to be updated
1302 * @romd_mode: %true to put the region into ROMD mode
1304 void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode);
1307 * memory_region_set_coalescing: Enable memory coalescing for the region.
1309 * Enabled writes to a region to be queued for later processing. MMIO ->write
1310 * callbacks may be delayed until a non-coalesced MMIO is issued.
1311 * Only useful for IO regions. Roughly similar to write-combining hardware.
1313 * @mr: the memory region to be write coalesced
1315 void memory_region_set_coalescing(MemoryRegion *mr);
1318 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
1319 * a region.
1321 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
1322 * Multiple calls can be issued coalesced disjoint ranges.
1324 * @mr: the memory region to be updated.
1325 * @offset: the start of the range within the region to be coalesced.
1326 * @size: the size of the subrange to be coalesced.
1328 void memory_region_add_coalescing(MemoryRegion *mr,
1329 hwaddr offset,
1330 uint64_t size);
1333 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
1335 * Disables any coalescing caused by memory_region_set_coalescing() or
1336 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
1337 * hardware.
1339 * @mr: the memory region to be updated.
1341 void memory_region_clear_coalescing(MemoryRegion *mr);
1344 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
1345 * accesses.
1347 * Ensure that pending coalesced MMIO request are flushed before the memory
1348 * region is accessed. This property is automatically enabled for all regions
1349 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
1351 * @mr: the memory region to be updated.
1353 void memory_region_set_flush_coalesced(MemoryRegion *mr);
1356 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
1357 * accesses.
1359 * Clear the automatic coalesced MMIO flushing enabled via
1360 * memory_region_set_flush_coalesced. Note that this service has no effect on
1361 * memory regions that have MMIO coalescing enabled for themselves. For them,
1362 * automatic flushing will stop once coalescing is disabled.
1364 * @mr: the memory region to be updated.
1366 void memory_region_clear_flush_coalesced(MemoryRegion *mr);
1369 * memory_region_clear_global_locking: Declares that access processing does
1370 * not depend on the QEMU global lock.
1372 * By clearing this property, accesses to the memory region will be processed
1373 * outside of QEMU's global lock (unless the lock is held on when issuing the
1374 * access request). In this case, the device model implementing the access
1375 * handlers is responsible for synchronization of concurrency.
1377 * @mr: the memory region to be updated.
1379 void memory_region_clear_global_locking(MemoryRegion *mr);
1382 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
1383 * is written to a location.
1385 * Marks a word in an IO region (initialized with memory_region_init_io())
1386 * as a trigger for an eventfd event. The I/O callback will not be called.
1387 * The caller must be prepared to handle failure (that is, take the required
1388 * action if the callback _is_ called).
1390 * @mr: the memory region being updated.
1391 * @addr: the address within @mr that is to be monitored
1392 * @size: the size of the access to trigger the eventfd
1393 * @match_data: whether to match against @data, instead of just @addr
1394 * @data: the data to match against the guest write
1395 * @e: event notifier to be triggered when @addr, @size, and @data all match.
1397 void memory_region_add_eventfd(MemoryRegion *mr,
1398 hwaddr addr,
1399 unsigned size,
1400 bool match_data,
1401 uint64_t data,
1402 EventNotifier *e);
1405 * memory_region_del_eventfd: Cancel an eventfd.
1407 * Cancels an eventfd trigger requested by a previous
1408 * memory_region_add_eventfd() call.
1410 * @mr: the memory region being updated.
1411 * @addr: the address within @mr that is to be monitored
1412 * @size: the size of the access to trigger the eventfd
1413 * @match_data: whether to match against @data, instead of just @addr
1414 * @data: the data to match against the guest write
1415 * @e: event notifier to be triggered when @addr, @size, and @data all match.
1417 void memory_region_del_eventfd(MemoryRegion *mr,
1418 hwaddr addr,
1419 unsigned size,
1420 bool match_data,
1421 uint64_t data,
1422 EventNotifier *e);
1425 * memory_region_add_subregion: Add a subregion to a container.
1427 * Adds a subregion at @offset. The subregion may not overlap with other
1428 * subregions (except for those explicitly marked as overlapping). A region
1429 * may only be added once as a subregion (unless removed with
1430 * memory_region_del_subregion()); use memory_region_init_alias() if you
1431 * want a region to be a subregion in multiple locations.
1433 * @mr: the region to contain the new subregion; must be a container
1434 * initialized with memory_region_init().
1435 * @offset: the offset relative to @mr where @subregion is added.
1436 * @subregion: the subregion to be added.
1438 void memory_region_add_subregion(MemoryRegion *mr,
1439 hwaddr offset,
1440 MemoryRegion *subregion);
1442 * memory_region_add_subregion_overlap: Add a subregion to a container
1443 * with overlap.
1445 * Adds a subregion at @offset. The subregion may overlap with other
1446 * subregions. Conflicts are resolved by having a higher @priority hide a
1447 * lower @priority. Subregions without priority are taken as @priority 0.
1448 * A region may only be added once as a subregion (unless removed with
1449 * memory_region_del_subregion()); use memory_region_init_alias() if you
1450 * want a region to be a subregion in multiple locations.
1452 * @mr: the region to contain the new subregion; must be a container
1453 * initialized with memory_region_init().
1454 * @offset: the offset relative to @mr where @subregion is added.
1455 * @subregion: the subregion to be added.
1456 * @priority: used for resolving overlaps; highest priority wins.
1458 void memory_region_add_subregion_overlap(MemoryRegion *mr,
1459 hwaddr offset,
1460 MemoryRegion *subregion,
1461 int priority);
1464 * memory_region_get_ram_addr: Get the ram address associated with a memory
1465 * region
1467 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr);
1469 uint64_t memory_region_get_alignment(const MemoryRegion *mr);
1471 * memory_region_del_subregion: Remove a subregion.
1473 * Removes a subregion from its container.
1475 * @mr: the container to be updated.
1476 * @subregion: the region being removed; must be a current subregion of @mr.
1478 void memory_region_del_subregion(MemoryRegion *mr,
1479 MemoryRegion *subregion);
1482 * memory_region_set_enabled: dynamically enable or disable a region
1484 * Enables or disables a memory region. A disabled memory region
1485 * ignores all accesses to itself and its subregions. It does not
1486 * obscure sibling subregions with lower priority - it simply behaves as
1487 * if it was removed from the hierarchy.
1489 * Regions default to being enabled.
1491 * @mr: the region to be updated
1492 * @enabled: whether to enable or disable the region
1494 void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
1497 * memory_region_set_address: dynamically update the address of a region
1499 * Dynamically updates the address of a region, relative to its container.
1500 * May be used on regions are currently part of a memory hierarchy.
1502 * @mr: the region to be updated
1503 * @addr: new address, relative to container region
1505 void memory_region_set_address(MemoryRegion *mr, hwaddr addr);
1508 * memory_region_set_size: dynamically update the size of a region.
1510 * Dynamically updates the size of a region.
1512 * @mr: the region to be updated
1513 * @size: used size of the region.
1515 void memory_region_set_size(MemoryRegion *mr, uint64_t size);
1518 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1520 * Dynamically updates the offset into the target region that an alias points
1521 * to, as if the fourth argument to memory_region_init_alias() has changed.
1523 * @mr: the #MemoryRegion to be updated; should be an alias.
1524 * @offset: the new offset into the target memory region
1526 void memory_region_set_alias_offset(MemoryRegion *mr,
1527 hwaddr offset);
1530 * memory_region_present: checks if an address relative to a @container
1531 * translates into #MemoryRegion within @container
1533 * Answer whether a #MemoryRegion within @container covers the address
1534 * @addr.
1536 * @container: a #MemoryRegion within which @addr is a relative address
1537 * @addr: the area within @container to be searched
1539 bool memory_region_present(MemoryRegion *container, hwaddr addr);
1542 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1543 * into any address space.
1545 * @mr: a #MemoryRegion which should be checked if it's mapped
1547 bool memory_region_is_mapped(MemoryRegion *mr);
1550 * memory_region_find: translate an address/size relative to a
1551 * MemoryRegion into a #MemoryRegionSection.
1553 * Locates the first #MemoryRegion within @mr that overlaps the range
1554 * given by @addr and @size.
1556 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1557 * It will have the following characteristics:
1558 * .@size = 0 iff no overlap was found
1559 * .@mr is non-%NULL iff an overlap was found
1561 * Remember that in the return value the @offset_within_region is
1562 * relative to the returned region (in the .@mr field), not to the
1563 * @mr argument.
1565 * Similarly, the .@offset_within_address_space is relative to the
1566 * address space that contains both regions, the passed and the
1567 * returned one. However, in the special case where the @mr argument
1568 * has no container (and thus is the root of the address space), the
1569 * following will hold:
1570 * .@offset_within_address_space >= @addr
1571 * .@offset_within_address_space + .@size <= @addr + @size
1573 * @mr: a MemoryRegion within which @addr is a relative address
1574 * @addr: start of the area within @as to be searched
1575 * @size: size of the area to be searched
1577 MemoryRegionSection memory_region_find(MemoryRegion *mr,
1578 hwaddr addr, uint64_t size);
1581 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
1583 * Synchronizes the dirty page log for all address spaces.
1585 void memory_global_dirty_log_sync(void);
1588 * memory_region_transaction_begin: Start a transaction.
1590 * During a transaction, changes will be accumulated and made visible
1591 * only when the transaction ends (is committed).
1593 void memory_region_transaction_begin(void);
1596 * memory_region_transaction_commit: Commit a transaction and make changes
1597 * visible to the guest.
1599 void memory_region_transaction_commit(void);
1602 * memory_listener_register: register callbacks to be called when memory
1603 * sections are mapped or unmapped into an address
1604 * space
1606 * @listener: an object containing the callbacks to be called
1607 * @filter: if non-%NULL, only regions in this address space will be observed
1609 void memory_listener_register(MemoryListener *listener, AddressSpace *filter);
1612 * memory_listener_unregister: undo the effect of memory_listener_register()
1614 * @listener: an object containing the callbacks to be removed
1616 void memory_listener_unregister(MemoryListener *listener);
1619 * memory_global_dirty_log_start: begin dirty logging for all regions
1621 void memory_global_dirty_log_start(void);
1624 * memory_global_dirty_log_stop: end dirty logging for all regions
1626 void memory_global_dirty_log_stop(void);
1628 void mtree_info(fprintf_function mon_printf, void *f, bool flatview,
1629 bool dispatch_tree);
1632 * memory_region_request_mmio_ptr: request a pointer to an mmio
1633 * MemoryRegion. If it is possible map a RAM MemoryRegion with this pointer.
1634 * When the device wants to invalidate the pointer it will call
1635 * memory_region_invalidate_mmio_ptr.
1637 * @mr: #MemoryRegion to check
1638 * @addr: address within that region
1640 * Returns true on success, false otherwise.
1642 bool memory_region_request_mmio_ptr(MemoryRegion *mr, hwaddr addr);
1645 * memory_region_invalidate_mmio_ptr: invalidate the pointer to an mmio
1646 * previously requested.
1647 * In the end that means that if something wants to execute from this area it
1648 * will need to request the pointer again.
1650 * @mr: #MemoryRegion associated to the pointer.
1651 * @offset: offset within the memory region
1652 * @size: size of that area.
1654 void memory_region_invalidate_mmio_ptr(MemoryRegion *mr, hwaddr offset,
1655 unsigned size);
1658 * memory_region_dispatch_read: perform a read directly to the specified
1659 * MemoryRegion.
1661 * @mr: #MemoryRegion to access
1662 * @addr: address within that region
1663 * @pval: pointer to uint64_t which the data is written to
1664 * @size: size of the access in bytes
1665 * @attrs: memory transaction attributes to use for the access
1667 MemTxResult memory_region_dispatch_read(MemoryRegion *mr,
1668 hwaddr addr,
1669 uint64_t *pval,
1670 unsigned size,
1671 MemTxAttrs attrs);
1673 * memory_region_dispatch_write: perform a write directly to the specified
1674 * MemoryRegion.
1676 * @mr: #MemoryRegion to access
1677 * @addr: address within that region
1678 * @data: data to write
1679 * @size: size of the access in bytes
1680 * @attrs: memory transaction attributes to use for the access
1682 MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
1683 hwaddr addr,
1684 uint64_t data,
1685 unsigned size,
1686 MemTxAttrs attrs);
1689 * address_space_init: initializes an address space
1691 * @as: an uninitialized #AddressSpace
1692 * @root: a #MemoryRegion that routes addresses for the address space
1693 * @name: an address space name. The name is only used for debugging
1694 * output.
1696 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
1699 * address_space_destroy: destroy an address space
1701 * Releases all resources associated with an address space. After an address space
1702 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1703 * as well.
1705 * @as: address space to be destroyed
1707 void address_space_destroy(AddressSpace *as);
1710 * address_space_rw: read from or write to an address space.
1712 * Return a MemTxResult indicating whether the operation succeeded
1713 * or failed (eg unassigned memory, device rejected the transaction,
1714 * IOMMU fault).
1716 * @as: #AddressSpace to be accessed
1717 * @addr: address within that address space
1718 * @attrs: memory transaction attributes
1719 * @buf: buffer with the data transferred
1720 * @len: the number of bytes to read or write
1721 * @is_write: indicates the transfer direction
1723 MemTxResult address_space_rw(AddressSpace *as, hwaddr addr,
1724 MemTxAttrs attrs, uint8_t *buf,
1725 int len, bool is_write);
1728 * address_space_write: write to address space.
1730 * Return a MemTxResult indicating whether the operation succeeded
1731 * or failed (eg unassigned memory, device rejected the transaction,
1732 * IOMMU fault).
1734 * @as: #AddressSpace to be accessed
1735 * @addr: address within that address space
1736 * @attrs: memory transaction attributes
1737 * @buf: buffer with the data transferred
1738 * @len: the number of bytes to write
1740 MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
1741 MemTxAttrs attrs,
1742 const uint8_t *buf, int len);
1744 /* address_space_ld*: load from an address space
1745 * address_space_st*: store to an address space
1747 * These functions perform a load or store of the byte, word,
1748 * longword or quad to the specified address within the AddressSpace.
1749 * The _le suffixed functions treat the data as little endian;
1750 * _be indicates big endian; no suffix indicates "same endianness
1751 * as guest CPU".
1753 * The "guest CPU endianness" accessors are deprecated for use outside
1754 * target-* code; devices should be CPU-agnostic and use either the LE
1755 * or the BE accessors.
1757 * @as #AddressSpace to be accessed
1758 * @addr: address within that address space
1759 * @val: data value, for stores
1760 * @attrs: memory transaction attributes
1761 * @result: location to write the success/failure of the transaction;
1762 * if NULL, this information is discarded
1765 #define SUFFIX
1766 #define ARG1 as
1767 #define ARG1_DECL AddressSpace *as
1768 #include "exec/memory_ldst.inc.h"
1770 #define SUFFIX
1771 #define ARG1 as
1772 #define ARG1_DECL AddressSpace *as
1773 #include "exec/memory_ldst_phys.inc.h"
1775 struct MemoryRegionCache {
1776 void *ptr;
1777 hwaddr xlat;
1778 hwaddr len;
1779 FlatView *fv;
1780 MemoryRegionSection mrs;
1781 bool is_write;
1784 #define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .mrs.mr = NULL })
1787 /* address_space_ld*_cached: load from a cached #MemoryRegion
1788 * address_space_st*_cached: store into a cached #MemoryRegion
1790 * These functions perform a load or store of the byte, word,
1791 * longword or quad to the specified address. The address is
1792 * a physical address in the AddressSpace, but it must lie within
1793 * a #MemoryRegion that was mapped with address_space_cache_init.
1795 * The _le suffixed functions treat the data as little endian;
1796 * _be indicates big endian; no suffix indicates "same endianness
1797 * as guest CPU".
1799 * The "guest CPU endianness" accessors are deprecated for use outside
1800 * target-* code; devices should be CPU-agnostic and use either the LE
1801 * or the BE accessors.
1803 * @cache: previously initialized #MemoryRegionCache to be accessed
1804 * @addr: address within the address space
1805 * @val: data value, for stores
1806 * @attrs: memory transaction attributes
1807 * @result: location to write the success/failure of the transaction;
1808 * if NULL, this information is discarded
1811 #define SUFFIX _cached_slow
1812 #define ARG1 cache
1813 #define ARG1_DECL MemoryRegionCache *cache
1814 #include "exec/memory_ldst.inc.h"
1816 /* Inline fast path for direct RAM access. */
1817 static inline uint8_t address_space_ldub_cached(MemoryRegionCache *cache,
1818 hwaddr addr, MemTxAttrs attrs, MemTxResult *result)
1820 assert(addr < cache->len);
1821 if (likely(cache->ptr)) {
1822 return ldub_p(cache->ptr + addr);
1823 } else {
1824 return address_space_ldub_cached_slow(cache, addr, attrs, result);
1828 static inline void address_space_stb_cached(MemoryRegionCache *cache,
1829 hwaddr addr, uint32_t val, MemTxAttrs attrs, MemTxResult *result)
1831 assert(addr < cache->len);
1832 if (likely(cache->ptr)) {
1833 stb_p(cache->ptr + addr, val);
1834 } else {
1835 address_space_stb_cached_slow(cache, addr, val, attrs, result);
1839 #define ENDIANNESS _le
1840 #include "exec/memory_ldst_cached.inc.h"
1842 #define ENDIANNESS _be
1843 #include "exec/memory_ldst_cached.inc.h"
1845 #define SUFFIX _cached
1846 #define ARG1 cache
1847 #define ARG1_DECL MemoryRegionCache *cache
1848 #include "exec/memory_ldst_phys.inc.h"
1850 /* address_space_cache_init: prepare for repeated access to a physical
1851 * memory region
1853 * @cache: #MemoryRegionCache to be filled
1854 * @as: #AddressSpace to be accessed
1855 * @addr: address within that address space
1856 * @len: length of buffer
1857 * @is_write: indicates the transfer direction
1859 * Will only work with RAM, and may map a subset of the requested range by
1860 * returning a value that is less than @len. On failure, return a negative
1861 * errno value.
1863 * Because it only works with RAM, this function can be used for
1864 * read-modify-write operations. In this case, is_write should be %true.
1866 * Note that addresses passed to the address_space_*_cached functions
1867 * are relative to @addr.
1869 int64_t address_space_cache_init(MemoryRegionCache *cache,
1870 AddressSpace *as,
1871 hwaddr addr,
1872 hwaddr len,
1873 bool is_write);
1876 * address_space_cache_invalidate: complete a write to a #MemoryRegionCache
1878 * @cache: The #MemoryRegionCache to operate on.
1879 * @addr: The first physical address that was written, relative to the
1880 * address that was passed to @address_space_cache_init.
1881 * @access_len: The number of bytes that were written starting at @addr.
1883 void address_space_cache_invalidate(MemoryRegionCache *cache,
1884 hwaddr addr,
1885 hwaddr access_len);
1888 * address_space_cache_destroy: free a #MemoryRegionCache
1890 * @cache: The #MemoryRegionCache whose memory should be released.
1892 void address_space_cache_destroy(MemoryRegionCache *cache);
1894 /* address_space_get_iotlb_entry: translate an address into an IOTLB
1895 * entry. Should be called from an RCU critical section.
1897 IOMMUTLBEntry address_space_get_iotlb_entry(AddressSpace *as, hwaddr addr,
1898 bool is_write);
1900 /* address_space_translate: translate an address range into an address space
1901 * into a MemoryRegion and an address range into that section. Should be
1902 * called from an RCU critical section, to avoid that the last reference
1903 * to the returned region disappears after address_space_translate returns.
1905 * @fv: #FlatView to be accessed
1906 * @addr: address within that address space
1907 * @xlat: pointer to address within the returned memory region section's
1908 * #MemoryRegion.
1909 * @len: pointer to length
1910 * @is_write: indicates the transfer direction
1911 * @attrs: memory attributes
1913 MemoryRegion *flatview_translate(FlatView *fv,
1914 hwaddr addr, hwaddr *xlat,
1915 hwaddr *len, bool is_write);
1917 static inline MemoryRegion *address_space_translate(AddressSpace *as,
1918 hwaddr addr, hwaddr *xlat,
1919 hwaddr *len, bool is_write,
1920 MemTxAttrs attrs)
1922 return flatview_translate(address_space_to_flatview(as),
1923 addr, xlat, len, is_write);
1926 /* address_space_access_valid: check for validity of accessing an address
1927 * space range
1929 * Check whether memory is assigned to the given address space range, and
1930 * access is permitted by any IOMMU regions that are active for the address
1931 * space.
1933 * For now, addr and len should be aligned to a page size. This limitation
1934 * will be lifted in the future.
1936 * @as: #AddressSpace to be accessed
1937 * @addr: address within that address space
1938 * @len: length of the area to be checked
1939 * @is_write: indicates the transfer direction
1941 bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write);
1943 /* address_space_map: map a physical memory region into a host virtual address
1945 * May map a subset of the requested range, given by and returned in @plen.
1946 * May return %NULL if resources needed to perform the mapping are exhausted.
1947 * Use only for reads OR writes - not for read-modify-write operations.
1948 * Use cpu_register_map_client() to know when retrying the map operation is
1949 * likely to succeed.
1951 * @as: #AddressSpace to be accessed
1952 * @addr: address within that address space
1953 * @plen: pointer to length of buffer; updated on return
1954 * @is_write: indicates the transfer direction
1956 void *address_space_map(AddressSpace *as, hwaddr addr,
1957 hwaddr *plen, bool is_write);
1959 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1961 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1962 * the amount of memory that was actually read or written by the caller.
1964 * @as: #AddressSpace used
1965 * @buffer: host pointer as returned by address_space_map()
1966 * @len: buffer length as returned by address_space_map()
1967 * @access_len: amount of data actually transferred
1968 * @is_write: indicates the transfer direction
1970 void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
1971 int is_write, hwaddr access_len);
1974 /* Internal functions, part of the implementation of address_space_read. */
1975 MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr,
1976 MemTxAttrs attrs, uint8_t *buf, int len);
1977 MemTxResult flatview_read_continue(FlatView *fv, hwaddr addr,
1978 MemTxAttrs attrs, uint8_t *buf,
1979 int len, hwaddr addr1, hwaddr l,
1980 MemoryRegion *mr);
1981 void *qemu_map_ram_ptr(RAMBlock *ram_block, ram_addr_t addr);
1983 /* Internal functions, part of the implementation of address_space_read_cached
1984 * and address_space_write_cached. */
1985 void address_space_read_cached_slow(MemoryRegionCache *cache,
1986 hwaddr addr, void *buf, int len);
1987 void address_space_write_cached_slow(MemoryRegionCache *cache,
1988 hwaddr addr, const void *buf, int len);
1990 static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
1992 if (is_write) {
1993 return memory_region_is_ram(mr) &&
1994 !mr->readonly && !memory_region_is_ram_device(mr);
1995 } else {
1996 return (memory_region_is_ram(mr) && !memory_region_is_ram_device(mr)) ||
1997 memory_region_is_romd(mr);
2002 * address_space_read: read from an address space.
2004 * Return a MemTxResult indicating whether the operation succeeded
2005 * or failed (eg unassigned memory, device rejected the transaction,
2006 * IOMMU fault). Called within RCU critical section.
2008 * @as: #AddressSpace to be accessed
2009 * @addr: address within that address space
2010 * @attrs: memory transaction attributes
2011 * @buf: buffer with the data transferred
2013 static inline __attribute__((__always_inline__))
2014 MemTxResult address_space_read(AddressSpace *as, hwaddr addr,
2015 MemTxAttrs attrs, uint8_t *buf,
2016 int len)
2018 MemTxResult result = MEMTX_OK;
2019 hwaddr l, addr1;
2020 void *ptr;
2021 MemoryRegion *mr;
2022 FlatView *fv;
2024 if (__builtin_constant_p(len)) {
2025 if (len) {
2026 rcu_read_lock();
2027 fv = address_space_to_flatview(as);
2028 l = len;
2029 mr = flatview_translate(fv, addr, &addr1, &l, false);
2030 if (len == l && memory_access_is_direct(mr, false)) {
2031 ptr = qemu_map_ram_ptr(mr->ram_block, addr1);
2032 memcpy(buf, ptr, len);
2033 } else {
2034 result = flatview_read_continue(fv, addr, attrs, buf, len,
2035 addr1, l, mr);
2037 rcu_read_unlock();
2039 } else {
2040 result = address_space_read_full(as, addr, attrs, buf, len);
2042 return result;
2046 * address_space_read_cached: read from a cached RAM region
2048 * @cache: Cached region to be addressed
2049 * @addr: address relative to the base of the RAM region
2050 * @buf: buffer with the data transferred
2051 * @len: length of the data transferred
2053 static inline void
2054 address_space_read_cached(MemoryRegionCache *cache, hwaddr addr,
2055 void *buf, int len)
2057 assert(addr < cache->len && len <= cache->len - addr);
2058 if (likely(cache->ptr)) {
2059 memcpy(buf, cache->ptr + addr, len);
2060 } else {
2061 address_space_read_cached_slow(cache, addr, buf, len);
2066 * address_space_write_cached: write to a cached RAM region
2068 * @cache: Cached region to be addressed
2069 * @addr: address relative to the base of the RAM region
2070 * @buf: buffer with the data transferred
2071 * @len: length of the data transferred
2073 static inline void
2074 address_space_write_cached(MemoryRegionCache *cache, hwaddr addr,
2075 void *buf, int len)
2077 assert(addr < cache->len && len <= cache->len - addr);
2078 if (likely(cache->ptr)) {
2079 memcpy(cache->ptr + addr, buf, len);
2080 } else {
2081 address_space_write_cached_slow(cache, addr, buf, len);
2085 #endif
2087 #endif