Stop including qemu-common.h in memory.h
[qemu/ar7.git] / include / exec / memory.h
blob139471500f1bce089e8ec4aaa19f21282c270e06
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 #define DIRTY_MEMORY_VGA 0
20 #define DIRTY_MEMORY_CODE 1
21 #define DIRTY_MEMORY_MIGRATION 2
22 #define DIRTY_MEMORY_NUM 3 /* num of dirty bits */
24 #include <stdint.h>
25 #include <stdbool.h>
26 #include "exec/cpu-common.h"
27 #ifndef CONFIG_USER_ONLY
28 #include "exec/hwaddr.h"
29 #endif
30 #include "exec/memattrs.h"
31 #include "qemu/queue.h"
32 #include "qemu/int128.h"
33 #include "qemu/notify.h"
34 #include "qapi/error.h"
35 #include "qom/object.h"
36 #include "qemu/rcu.h"
38 #define MAX_PHYS_ADDR_SPACE_BITS 62
39 #define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
41 #define TYPE_MEMORY_REGION "qemu:memory-region"
42 #define MEMORY_REGION(obj) \
43 OBJECT_CHECK(MemoryRegion, (obj), TYPE_MEMORY_REGION)
45 typedef struct MemoryRegionOps MemoryRegionOps;
46 typedef struct MemoryRegionMmio MemoryRegionMmio;
48 struct MemoryRegionMmio {
49 CPUReadMemoryFunc *read[3];
50 CPUWriteMemoryFunc *write[3];
53 typedef struct IOMMUTLBEntry IOMMUTLBEntry;
55 /* See address_space_translate: bit 0 is read, bit 1 is write. */
56 typedef enum {
57 IOMMU_NONE = 0,
58 IOMMU_RO = 1,
59 IOMMU_WO = 2,
60 IOMMU_RW = 3,
61 } IOMMUAccessFlags;
63 struct IOMMUTLBEntry {
64 AddressSpace *target_as;
65 hwaddr iova;
66 hwaddr translated_addr;
67 hwaddr addr_mask; /* 0xfff = 4k translation */
68 IOMMUAccessFlags perm;
71 /* New-style MMIO accessors can indicate that the transaction failed.
72 * A zero (MEMTX_OK) response means success; anything else is a failure
73 * of some kind. The memory subsystem will bitwise-OR together results
74 * if it is synthesizing an operation from multiple smaller accesses.
76 #define MEMTX_OK 0
77 #define MEMTX_ERROR (1U << 0) /* device returned an error */
78 #define MEMTX_DECODE_ERROR (1U << 1) /* nothing at that address */
79 typedef uint32_t MemTxResult;
82 * Memory region callbacks
84 struct MemoryRegionOps {
85 /* Read from the memory region. @addr is relative to @mr; @size is
86 * in bytes. */
87 uint64_t (*read)(void *opaque,
88 hwaddr addr,
89 unsigned size);
90 /* Write to the memory region. @addr is relative to @mr; @size is
91 * in bytes. */
92 void (*write)(void *opaque,
93 hwaddr addr,
94 uint64_t data,
95 unsigned size);
97 MemTxResult (*read_with_attrs)(void *opaque,
98 hwaddr addr,
99 uint64_t *data,
100 unsigned size,
101 MemTxAttrs attrs);
102 MemTxResult (*write_with_attrs)(void *opaque,
103 hwaddr addr,
104 uint64_t data,
105 unsigned size,
106 MemTxAttrs attrs);
108 enum device_endian endianness;
109 /* Guest-visible constraints: */
110 struct {
111 /* If nonzero, specify bounds on access sizes beyond which a machine
112 * check is thrown.
114 unsigned min_access_size;
115 unsigned max_access_size;
116 /* If true, unaligned accesses are supported. Otherwise unaligned
117 * accesses throw machine checks.
119 bool unaligned;
121 * If present, and returns #false, the transaction is not accepted
122 * by the device (and results in machine dependent behaviour such
123 * as a machine check exception).
125 bool (*accepts)(void *opaque, hwaddr addr,
126 unsigned size, bool is_write);
127 } valid;
128 /* Internal implementation constraints: */
129 struct {
130 /* If nonzero, specifies the minimum size implemented. Smaller sizes
131 * will be rounded upwards and a partial result will be returned.
133 unsigned min_access_size;
134 /* If nonzero, specifies the maximum size implemented. Larger sizes
135 * will be done as a series of accesses with smaller sizes.
137 unsigned max_access_size;
138 /* If true, unaligned accesses are supported. Otherwise all accesses
139 * are converted to (possibly multiple) naturally aligned accesses.
141 bool unaligned;
142 } impl;
144 /* If .read and .write are not present, old_mmio may be used for
145 * backwards compatibility with old mmio registration
147 const MemoryRegionMmio old_mmio;
150 typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps;
152 struct MemoryRegionIOMMUOps {
153 /* Return a TLB entry that contains a given address. */
154 IOMMUTLBEntry (*translate)(MemoryRegion *iommu, hwaddr addr, bool is_write);
157 typedef struct CoalescedMemoryRange CoalescedMemoryRange;
158 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
160 struct MemoryRegion {
161 Object parent_obj;
162 /* All fields are private - violators will be prosecuted */
163 const MemoryRegionOps *ops;
164 const MemoryRegionIOMMUOps *iommu_ops;
165 void *opaque;
166 MemoryRegion *container;
167 Int128 size;
168 hwaddr addr;
169 void (*destructor)(MemoryRegion *mr);
170 ram_addr_t ram_addr;
171 uint64_t align;
172 bool subpage;
173 bool terminates;
174 bool romd_mode;
175 bool ram;
176 bool skip_dump;
177 bool readonly; /* For RAM regions */
178 bool enabled;
179 bool rom_device;
180 bool warning_printed; /* For reservations */
181 bool flush_coalesced_mmio;
182 bool global_locking;
183 MemoryRegion *alias;
184 hwaddr alias_offset;
185 int32_t priority;
186 bool may_overlap;
187 QTAILQ_HEAD(subregions, MemoryRegion) subregions;
188 QTAILQ_ENTRY(MemoryRegion) subregions_link;
189 QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced;
190 const char *name;
191 uint8_t dirty_log_mask;
192 unsigned ioeventfd_nb;
193 MemoryRegionIoeventfd *ioeventfds;
194 NotifierList iommu_notify;
198 * MemoryListener: callbacks structure for updates to the physical memory map
200 * Allows a component to adjust to changes in the guest-visible memory map.
201 * Use with memory_listener_register() and memory_listener_unregister().
203 struct MemoryListener {
204 void (*begin)(MemoryListener *listener);
205 void (*commit)(MemoryListener *listener);
206 void (*region_add)(MemoryListener *listener, MemoryRegionSection *section);
207 void (*region_del)(MemoryListener *listener, MemoryRegionSection *section);
208 void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section);
209 void (*log_start)(MemoryListener *listener, MemoryRegionSection *section,
210 int old, int new);
211 void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section,
212 int old, int new);
213 void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section);
214 void (*log_global_start)(MemoryListener *listener);
215 void (*log_global_stop)(MemoryListener *listener);
216 void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section,
217 bool match_data, uint64_t data, EventNotifier *e);
218 void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section,
219 bool match_data, uint64_t data, EventNotifier *e);
220 void (*coalesced_mmio_add)(MemoryListener *listener, MemoryRegionSection *section,
221 hwaddr addr, hwaddr len);
222 void (*coalesced_mmio_del)(MemoryListener *listener, MemoryRegionSection *section,
223 hwaddr addr, hwaddr len);
224 /* Lower = earlier (during add), later (during del) */
225 unsigned priority;
226 AddressSpace *address_space_filter;
227 QTAILQ_ENTRY(MemoryListener) link;
231 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
233 struct AddressSpace {
234 /* All fields are private. */
235 struct rcu_head rcu;
236 char *name;
237 MemoryRegion *root;
239 /* Accessed via RCU. */
240 struct FlatView *current_map;
242 int ioeventfd_nb;
243 struct MemoryRegionIoeventfd *ioeventfds;
244 struct AddressSpaceDispatch *dispatch;
245 struct AddressSpaceDispatch *next_dispatch;
246 MemoryListener dispatch_listener;
248 QTAILQ_ENTRY(AddressSpace) address_spaces_link;
252 * MemoryRegionSection: describes a fragment of a #MemoryRegion
254 * @mr: the region, or %NULL if empty
255 * @address_space: the address space the region is mapped in
256 * @offset_within_region: the beginning of the section, relative to @mr's start
257 * @size: the size of the section; will not exceed @mr's boundaries
258 * @offset_within_address_space: the address of the first byte of the section
259 * relative to the region's address space
260 * @readonly: writes to this section are ignored
262 struct MemoryRegionSection {
263 MemoryRegion *mr;
264 AddressSpace *address_space;
265 hwaddr offset_within_region;
266 Int128 size;
267 hwaddr offset_within_address_space;
268 bool readonly;
272 * memory_region_init: Initialize a memory region
274 * The region typically acts as a container for other memory regions. Use
275 * memory_region_add_subregion() to add subregions.
277 * @mr: the #MemoryRegion to be initialized
278 * @owner: the object that tracks the region's reference count
279 * @name: used for debugging; not visible to the user or ABI
280 * @size: size of the region; any subregions beyond this size will be clipped
282 void memory_region_init(MemoryRegion *mr,
283 struct Object *owner,
284 const char *name,
285 uint64_t size);
288 * memory_region_ref: Add 1 to a memory region's reference count
290 * Whenever memory regions are accessed outside the BQL, they need to be
291 * preserved against hot-unplug. MemoryRegions actually do not have their
292 * own reference count; they piggyback on a QOM object, their "owner".
293 * This function adds a reference to the owner.
295 * All MemoryRegions must have an owner if they can disappear, even if the
296 * device they belong to operates exclusively under the BQL. This is because
297 * the region could be returned at any time by memory_region_find, and this
298 * is usually under guest control.
300 * @mr: the #MemoryRegion
302 void memory_region_ref(MemoryRegion *mr);
305 * memory_region_unref: Remove 1 to a memory region's reference count
307 * Whenever memory regions are accessed outside the BQL, they need to be
308 * preserved against hot-unplug. MemoryRegions actually do not have their
309 * own reference count; they piggyback on a QOM object, their "owner".
310 * This function removes a reference to the owner and possibly destroys it.
312 * @mr: the #MemoryRegion
314 void memory_region_unref(MemoryRegion *mr);
317 * memory_region_init_io: Initialize an I/O memory region.
319 * Accesses into the region will cause the callbacks in @ops to be called.
320 * if @size is nonzero, subregions will be clipped to @size.
322 * @mr: the #MemoryRegion to be initialized.
323 * @owner: the object that tracks the region's reference count
324 * @ops: a structure containing read and write callbacks to be used when
325 * I/O is performed on the region.
326 * @opaque: passed to to the read and write callbacks of the @ops structure.
327 * @name: used for debugging; not visible to the user or ABI
328 * @size: size of the region.
330 void memory_region_init_io(MemoryRegion *mr,
331 struct Object *owner,
332 const MemoryRegionOps *ops,
333 void *opaque,
334 const char *name,
335 uint64_t size);
338 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
339 * region will modify memory directly.
341 * @mr: the #MemoryRegion to be initialized.
342 * @owner: the object that tracks the region's reference count
343 * @name: the name of the region.
344 * @size: size of the region.
345 * @errp: pointer to Error*, to store an error if it happens.
347 void memory_region_init_ram(MemoryRegion *mr,
348 struct Object *owner,
349 const char *name,
350 uint64_t size,
351 Error **errp);
354 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
355 * RAM. Accesses into the region will
356 * modify memory directly. Only an initial
357 * portion of this RAM is actually used.
358 * The used size can change across reboots.
360 * @mr: the #MemoryRegion to be initialized.
361 * @owner: the object that tracks the region's reference count
362 * @name: the name of the region.
363 * @size: used size of the region.
364 * @max_size: max size of the region.
365 * @resized: callback to notify owner about used size change.
366 * @errp: pointer to Error*, to store an error if it happens.
368 void memory_region_init_resizeable_ram(MemoryRegion *mr,
369 struct Object *owner,
370 const char *name,
371 uint64_t size,
372 uint64_t max_size,
373 void (*resized)(const char*,
374 uint64_t length,
375 void *host),
376 Error **errp);
377 #ifdef __linux__
379 * memory_region_init_ram_from_file: Initialize RAM memory region with a
380 * mmap-ed backend.
382 * @mr: the #MemoryRegion to be initialized.
383 * @owner: the object that tracks the region's reference count
384 * @name: the name of the region.
385 * @size: size of the region.
386 * @share: %true if memory must be mmaped with the MAP_SHARED flag
387 * @path: the path in which to allocate the RAM.
388 * @errp: pointer to Error*, to store an error if it happens.
390 void memory_region_init_ram_from_file(MemoryRegion *mr,
391 struct Object *owner,
392 const char *name,
393 uint64_t size,
394 bool share,
395 const char *path,
396 Error **errp);
397 #endif
400 * memory_region_init_ram_ptr: Initialize RAM memory region from a
401 * user-provided pointer. Accesses into the
402 * region will modify memory directly.
404 * @mr: the #MemoryRegion to be initialized.
405 * @owner: the object that tracks the region's reference count
406 * @name: the name of the region.
407 * @size: size of the region.
408 * @ptr: memory to be mapped; must contain at least @size bytes.
410 void memory_region_init_ram_ptr(MemoryRegion *mr,
411 struct Object *owner,
412 const char *name,
413 uint64_t size,
414 void *ptr);
417 * memory_region_init_alias: Initialize a memory region that aliases all or a
418 * part of another memory region.
420 * @mr: the #MemoryRegion to be initialized.
421 * @owner: the object that tracks the region's reference count
422 * @name: used for debugging; not visible to the user or ABI
423 * @orig: the region to be referenced; @mr will be equivalent to
424 * @orig between @offset and @offset + @size - 1.
425 * @offset: start of the section in @orig to be referenced.
426 * @size: size of the region.
428 void memory_region_init_alias(MemoryRegion *mr,
429 struct Object *owner,
430 const char *name,
431 MemoryRegion *orig,
432 hwaddr offset,
433 uint64_t size);
436 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
437 * handled via callbacks.
439 * @mr: the #MemoryRegion to be initialized.
440 * @owner: the object that tracks the region's reference count
441 * @ops: callbacks for write access handling.
442 * @name: the name of the region.
443 * @size: size of the region.
444 * @errp: pointer to Error*, to store an error if it happens.
446 void memory_region_init_rom_device(MemoryRegion *mr,
447 struct Object *owner,
448 const MemoryRegionOps *ops,
449 void *opaque,
450 const char *name,
451 uint64_t size,
452 Error **errp);
455 * memory_region_init_reservation: Initialize a memory region that reserves
456 * I/O space.
458 * A reservation region primariy serves debugging purposes. It claims I/O
459 * space that is not supposed to be handled by QEMU itself. Any access via
460 * the memory API will cause an abort().
462 * @mr: the #MemoryRegion to be initialized
463 * @owner: the object that tracks the region's reference count
464 * @name: used for debugging; not visible to the user or ABI
465 * @size: size of the region.
467 void memory_region_init_reservation(MemoryRegion *mr,
468 struct Object *owner,
469 const char *name,
470 uint64_t size);
473 * memory_region_init_iommu: Initialize a memory region that translates
474 * addresses
476 * An IOMMU region translates addresses and forwards accesses to a target
477 * memory region.
479 * @mr: the #MemoryRegion to be initialized
480 * @owner: the object that tracks the region's reference count
481 * @ops: a function that translates addresses into the @target region
482 * @name: used for debugging; not visible to the user or ABI
483 * @size: size of the region.
485 void memory_region_init_iommu(MemoryRegion *mr,
486 struct Object *owner,
487 const MemoryRegionIOMMUOps *ops,
488 const char *name,
489 uint64_t size);
492 * memory_region_owner: get a memory region's owner.
494 * @mr: the memory region being queried.
496 struct Object *memory_region_owner(MemoryRegion *mr);
499 * memory_region_size: get a memory region's size.
501 * @mr: the memory region being queried.
503 uint64_t memory_region_size(MemoryRegion *mr);
506 * memory_region_is_ram: check whether a memory region is random access
508 * Returns %true is a memory region is random access.
510 * @mr: the memory region being queried
512 bool memory_region_is_ram(MemoryRegion *mr);
515 * memory_region_is_skip_dump: check whether a memory region should not be
516 * dumped
518 * Returns %true is a memory region should not be dumped(e.g. VFIO BAR MMAP).
520 * @mr: the memory region being queried
522 bool memory_region_is_skip_dump(MemoryRegion *mr);
525 * memory_region_set_skip_dump: Set skip_dump flag, dump will ignore this memory
526 * region
528 * @mr: the memory region being queried
530 void memory_region_set_skip_dump(MemoryRegion *mr);
533 * memory_region_is_romd: check whether a memory region is in ROMD mode
535 * Returns %true if a memory region is a ROM device and currently set to allow
536 * direct reads.
538 * @mr: the memory region being queried
540 static inline bool memory_region_is_romd(MemoryRegion *mr)
542 return mr->rom_device && mr->romd_mode;
546 * memory_region_is_iommu: check whether a memory region is an iommu
548 * Returns %true is a memory region is an iommu.
550 * @mr: the memory region being queried
552 bool memory_region_is_iommu(MemoryRegion *mr);
555 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
557 * @mr: the memory region that was changed
558 * @entry: the new entry in the IOMMU translation table. The entry
559 * replaces all old entries for the same virtual I/O address range.
560 * Deleted entries have .@perm == 0.
562 void memory_region_notify_iommu(MemoryRegion *mr,
563 IOMMUTLBEntry entry);
566 * memory_region_register_iommu_notifier: register a notifier for changes to
567 * IOMMU translation entries.
569 * @mr: the memory region to observe
570 * @n: the notifier to be added; the notifier receives a pointer to an
571 * #IOMMUTLBEntry as the opaque value; the pointer ceases to be
572 * valid on exit from the notifier.
574 void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n);
577 * memory_region_unregister_iommu_notifier: unregister a notifier for
578 * changes to IOMMU translation entries.
580 * @n: the notifier to be removed.
582 void memory_region_unregister_iommu_notifier(Notifier *n);
585 * memory_region_name: get a memory region's name
587 * Returns the string that was used to initialize the memory region.
589 * @mr: the memory region being queried
591 const char *memory_region_name(const MemoryRegion *mr);
594 * memory_region_is_logging: return whether a memory region is logging writes
596 * Returns %true if the memory region is logging writes for the given client
598 * @mr: the memory region being queried
599 * @client: the client being queried
601 bool memory_region_is_logging(MemoryRegion *mr, uint8_t client);
604 * memory_region_get_dirty_log_mask: return the clients for which a
605 * memory region is logging writes.
607 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
608 * are the bit indices.
610 * @mr: the memory region being queried
612 uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr);
615 * memory_region_is_rom: check whether a memory region is ROM
617 * Returns %true is a memory region is read-only memory.
619 * @mr: the memory region being queried
621 bool memory_region_is_rom(MemoryRegion *mr);
624 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
626 * Returns a file descriptor backing a file-based RAM memory region,
627 * or -1 if the region is not a file-based RAM memory region.
629 * @mr: the RAM or alias memory region being queried.
631 int memory_region_get_fd(MemoryRegion *mr);
634 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
636 * Returns a host pointer to a RAM memory region (created with
637 * memory_region_init_ram() or memory_region_init_ram_ptr()). Use with
638 * care.
640 * @mr: the memory region being queried.
642 void *memory_region_get_ram_ptr(MemoryRegion *mr);
644 /* memory_region_ram_resize: Resize a RAM region.
646 * Only legal before guest might have detected the memory size: e.g. on
647 * incoming migration, or right after reset.
649 * @mr: a memory region created with @memory_region_init_resizeable_ram.
650 * @newsize: the new size the region
651 * @errp: pointer to Error*, to store an error if it happens.
653 void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize,
654 Error **errp);
657 * memory_region_set_log: Turn dirty logging on or off for a region.
659 * Turns dirty logging on or off for a specified client (display, migration).
660 * Only meaningful for RAM regions.
662 * @mr: the memory region being updated.
663 * @log: whether dirty logging is to be enabled or disabled.
664 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
666 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
669 * memory_region_get_dirty: Check whether a range of bytes is dirty
670 * for a specified client.
672 * Checks whether a range of bytes has been written to since the last
673 * call to memory_region_reset_dirty() with the same @client. Dirty logging
674 * must be enabled.
676 * @mr: the memory region being queried.
677 * @addr: the address (relative to the start of the region) being queried.
678 * @size: the size of the range being queried.
679 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
680 * %DIRTY_MEMORY_VGA.
682 bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
683 hwaddr size, unsigned client);
686 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
688 * Marks a range of bytes as dirty, after it has been dirtied outside
689 * guest code.
691 * @mr: the memory region being dirtied.
692 * @addr: the address (relative to the start of the region) being dirtied.
693 * @size: size of the range being dirtied.
695 void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
696 hwaddr size);
699 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty
700 * for a specified client. It clears them.
702 * Checks whether a range of bytes has been written to since the last
703 * call to memory_region_reset_dirty() with the same @client. Dirty logging
704 * must be enabled.
706 * @mr: the memory region being queried.
707 * @addr: the address (relative to the start of the region) being queried.
708 * @size: the size of the range being queried.
709 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
710 * %DIRTY_MEMORY_VGA.
712 bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr,
713 hwaddr size, unsigned client);
715 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
716 * any external TLBs (e.g. kvm)
718 * Flushes dirty information from accelerators such as kvm and vhost-net
719 * and makes it available to users of the memory API.
721 * @mr: the region being flushed.
723 void memory_region_sync_dirty_bitmap(MemoryRegion *mr);
726 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
727 * client.
729 * Marks a range of pages as no longer dirty.
731 * @mr: the region being updated.
732 * @addr: the start of the subrange being cleaned.
733 * @size: the size of the subrange being cleaned.
734 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
735 * %DIRTY_MEMORY_VGA.
737 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
738 hwaddr size, unsigned client);
741 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
743 * Allows a memory region to be marked as read-only (turning it into a ROM).
744 * only useful on RAM regions.
746 * @mr: the region being updated.
747 * @readonly: whether rhe region is to be ROM or RAM.
749 void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
752 * memory_region_rom_device_set_romd: enable/disable ROMD mode
754 * Allows a ROM device (initialized with memory_region_init_rom_device() to
755 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
756 * device is mapped to guest memory and satisfies read access directly.
757 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
758 * Writes are always handled by the #MemoryRegion.write function.
760 * @mr: the memory region to be updated
761 * @romd_mode: %true to put the region into ROMD mode
763 void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode);
766 * memory_region_set_coalescing: Enable memory coalescing for the region.
768 * Enabled writes to a region to be queued for later processing. MMIO ->write
769 * callbacks may be delayed until a non-coalesced MMIO is issued.
770 * Only useful for IO regions. Roughly similar to write-combining hardware.
772 * @mr: the memory region to be write coalesced
774 void memory_region_set_coalescing(MemoryRegion *mr);
777 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
778 * a region.
780 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
781 * Multiple calls can be issued coalesced disjoint ranges.
783 * @mr: the memory region to be updated.
784 * @offset: the start of the range within the region to be coalesced.
785 * @size: the size of the subrange to be coalesced.
787 void memory_region_add_coalescing(MemoryRegion *mr,
788 hwaddr offset,
789 uint64_t size);
792 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
794 * Disables any coalescing caused by memory_region_set_coalescing() or
795 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
796 * hardware.
798 * @mr: the memory region to be updated.
800 void memory_region_clear_coalescing(MemoryRegion *mr);
803 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
804 * accesses.
806 * Ensure that pending coalesced MMIO request are flushed before the memory
807 * region is accessed. This property is automatically enabled for all regions
808 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
810 * @mr: the memory region to be updated.
812 void memory_region_set_flush_coalesced(MemoryRegion *mr);
815 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
816 * accesses.
818 * Clear the automatic coalesced MMIO flushing enabled via
819 * memory_region_set_flush_coalesced. Note that this service has no effect on
820 * memory regions that have MMIO coalescing enabled for themselves. For them,
821 * automatic flushing will stop once coalescing is disabled.
823 * @mr: the memory region to be updated.
825 void memory_region_clear_flush_coalesced(MemoryRegion *mr);
828 * memory_region_set_global_locking: Declares the access processing requires
829 * QEMU's global lock.
831 * When this is invoked, accesses to the memory region will be processed while
832 * holding the global lock of QEMU. This is the default behavior of memory
833 * regions.
835 * @mr: the memory region to be updated.
837 void memory_region_set_global_locking(MemoryRegion *mr);
840 * memory_region_clear_global_locking: Declares that access processing does
841 * not depend on the QEMU global lock.
843 * By clearing this property, accesses to the memory region will be processed
844 * outside of QEMU's global lock (unless the lock is held on when issuing the
845 * access request). In this case, the device model implementing the access
846 * handlers is responsible for synchronization of concurrency.
848 * @mr: the memory region to be updated.
850 void memory_region_clear_global_locking(MemoryRegion *mr);
853 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
854 * is written to a location.
856 * Marks a word in an IO region (initialized with memory_region_init_io())
857 * as a trigger for an eventfd event. The I/O callback will not be called.
858 * The caller must be prepared to handle failure (that is, take the required
859 * action if the callback _is_ called).
861 * @mr: the memory region being updated.
862 * @addr: the address within @mr that is to be monitored
863 * @size: the size of the access to trigger the eventfd
864 * @match_data: whether to match against @data, instead of just @addr
865 * @data: the data to match against the guest write
866 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
868 void memory_region_add_eventfd(MemoryRegion *mr,
869 hwaddr addr,
870 unsigned size,
871 bool match_data,
872 uint64_t data,
873 EventNotifier *e);
876 * memory_region_del_eventfd: Cancel an eventfd.
878 * Cancels an eventfd trigger requested by a previous
879 * memory_region_add_eventfd() call.
881 * @mr: the memory region being updated.
882 * @addr: the address within @mr that is to be monitored
883 * @size: the size of the access to trigger the eventfd
884 * @match_data: whether to match against @data, instead of just @addr
885 * @data: the data to match against the guest write
886 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
888 void memory_region_del_eventfd(MemoryRegion *mr,
889 hwaddr addr,
890 unsigned size,
891 bool match_data,
892 uint64_t data,
893 EventNotifier *e);
896 * memory_region_add_subregion: Add a subregion to a container.
898 * Adds a subregion at @offset. The subregion may not overlap with other
899 * subregions (except for those explicitly marked as overlapping). A region
900 * may only be added once as a subregion (unless removed with
901 * memory_region_del_subregion()); use memory_region_init_alias() if you
902 * want a region to be a subregion in multiple locations.
904 * @mr: the region to contain the new subregion; must be a container
905 * initialized with memory_region_init().
906 * @offset: the offset relative to @mr where @subregion is added.
907 * @subregion: the subregion to be added.
909 void memory_region_add_subregion(MemoryRegion *mr,
910 hwaddr offset,
911 MemoryRegion *subregion);
913 * memory_region_add_subregion_overlap: Add a subregion to a container
914 * with overlap.
916 * Adds a subregion at @offset. The subregion may overlap with other
917 * subregions. Conflicts are resolved by having a higher @priority hide a
918 * lower @priority. Subregions without priority are taken as @priority 0.
919 * A region may only be added once as a subregion (unless removed with
920 * memory_region_del_subregion()); use memory_region_init_alias() if you
921 * want a region to be a subregion in multiple locations.
923 * @mr: the region to contain the new subregion; must be a container
924 * initialized with memory_region_init().
925 * @offset: the offset relative to @mr where @subregion is added.
926 * @subregion: the subregion to be added.
927 * @priority: used for resolving overlaps; highest priority wins.
929 void memory_region_add_subregion_overlap(MemoryRegion *mr,
930 hwaddr offset,
931 MemoryRegion *subregion,
932 int priority);
935 * memory_region_get_ram_addr: Get the ram address associated with a memory
936 * region
938 * DO NOT USE THIS FUNCTION. This is a temporary workaround while the Xen
939 * code is being reworked.
941 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr);
943 uint64_t memory_region_get_alignment(const MemoryRegion *mr);
945 * memory_region_del_subregion: Remove a subregion.
947 * Removes a subregion from its container.
949 * @mr: the container to be updated.
950 * @subregion: the region being removed; must be a current subregion of @mr.
952 void memory_region_del_subregion(MemoryRegion *mr,
953 MemoryRegion *subregion);
956 * memory_region_set_enabled: dynamically enable or disable a region
958 * Enables or disables a memory region. A disabled memory region
959 * ignores all accesses to itself and its subregions. It does not
960 * obscure sibling subregions with lower priority - it simply behaves as
961 * if it was removed from the hierarchy.
963 * Regions default to being enabled.
965 * @mr: the region to be updated
966 * @enabled: whether to enable or disable the region
968 void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
971 * memory_region_set_address: dynamically update the address of a region
973 * Dynamically updates the address of a region, relative to its container.
974 * May be used on regions are currently part of a memory hierarchy.
976 * @mr: the region to be updated
977 * @addr: new address, relative to container region
979 void memory_region_set_address(MemoryRegion *mr, hwaddr addr);
982 * memory_region_set_size: dynamically update the size of a region.
984 * Dynamically updates the size of a region.
986 * @mr: the region to be updated
987 * @size: used size of the region.
989 void memory_region_set_size(MemoryRegion *mr, uint64_t size);
992 * memory_region_set_alias_offset: dynamically update a memory alias's offset
994 * Dynamically updates the offset into the target region that an alias points
995 * to, as if the fourth argument to memory_region_init_alias() has changed.
997 * @mr: the #MemoryRegion to be updated; should be an alias.
998 * @offset: the new offset into the target memory region
1000 void memory_region_set_alias_offset(MemoryRegion *mr,
1001 hwaddr offset);
1004 * memory_region_present: checks if an address relative to a @container
1005 * translates into #MemoryRegion within @container
1007 * Answer whether a #MemoryRegion within @container covers the address
1008 * @addr.
1010 * @container: a #MemoryRegion within which @addr is a relative address
1011 * @addr: the area within @container to be searched
1013 bool memory_region_present(MemoryRegion *container, hwaddr addr);
1016 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1017 * into any address space.
1019 * @mr: a #MemoryRegion which should be checked if it's mapped
1021 bool memory_region_is_mapped(MemoryRegion *mr);
1024 * memory_region_find: translate an address/size relative to a
1025 * MemoryRegion into a #MemoryRegionSection.
1027 * Locates the first #MemoryRegion within @mr that overlaps the range
1028 * given by @addr and @size.
1030 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1031 * It will have the following characteristics:
1032 * .@size = 0 iff no overlap was found
1033 * .@mr is non-%NULL iff an overlap was found
1035 * Remember that in the return value the @offset_within_region is
1036 * relative to the returned region (in the .@mr field), not to the
1037 * @mr argument.
1039 * Similarly, the .@offset_within_address_space is relative to the
1040 * address space that contains both regions, the passed and the
1041 * returned one. However, in the special case where the @mr argument
1042 * has no container (and thus is the root of the address space), the
1043 * following will hold:
1044 * .@offset_within_address_space >= @addr
1045 * .@offset_within_address_space + .@size <= @addr + @size
1047 * @mr: a MemoryRegion within which @addr is a relative address
1048 * @addr: start of the area within @as to be searched
1049 * @size: size of the area to be searched
1051 MemoryRegionSection memory_region_find(MemoryRegion *mr,
1052 hwaddr addr, uint64_t size);
1055 * address_space_sync_dirty_bitmap: synchronize the dirty log for all memory
1057 * Synchronizes the dirty page log for an entire address space.
1058 * @as: the address space that contains the memory being synchronized
1060 void address_space_sync_dirty_bitmap(AddressSpace *as);
1063 * memory_region_transaction_begin: Start a transaction.
1065 * During a transaction, changes will be accumulated and made visible
1066 * only when the transaction ends (is committed).
1068 void memory_region_transaction_begin(void);
1071 * memory_region_transaction_commit: Commit a transaction and make changes
1072 * visible to the guest.
1074 void memory_region_transaction_commit(void);
1077 * memory_listener_register: register callbacks to be called when memory
1078 * sections are mapped or unmapped into an address
1079 * space
1081 * @listener: an object containing the callbacks to be called
1082 * @filter: if non-%NULL, only regions in this address space will be observed
1084 void memory_listener_register(MemoryListener *listener, AddressSpace *filter);
1087 * memory_listener_unregister: undo the effect of memory_listener_register()
1089 * @listener: an object containing the callbacks to be removed
1091 void memory_listener_unregister(MemoryListener *listener);
1094 * memory_global_dirty_log_start: begin dirty logging for all regions
1096 void memory_global_dirty_log_start(void);
1099 * memory_global_dirty_log_stop: end dirty logging for all regions
1101 void memory_global_dirty_log_stop(void);
1103 void mtree_info(fprintf_function mon_printf, void *f);
1106 * memory_region_dispatch_read: perform a read directly to the specified
1107 * MemoryRegion.
1109 * @mr: #MemoryRegion to access
1110 * @addr: address within that region
1111 * @pval: pointer to uint64_t which the data is written to
1112 * @size: size of the access in bytes
1113 * @attrs: memory transaction attributes to use for the access
1115 MemTxResult memory_region_dispatch_read(MemoryRegion *mr,
1116 hwaddr addr,
1117 uint64_t *pval,
1118 unsigned size,
1119 MemTxAttrs attrs);
1121 * memory_region_dispatch_write: perform a write directly to the specified
1122 * MemoryRegion.
1124 * @mr: #MemoryRegion to access
1125 * @addr: address within that region
1126 * @data: data to write
1127 * @size: size of the access in bytes
1128 * @attrs: memory transaction attributes to use for the access
1130 MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
1131 hwaddr addr,
1132 uint64_t data,
1133 unsigned size,
1134 MemTxAttrs attrs);
1137 * address_space_init: initializes an address space
1139 * @as: an uninitialized #AddressSpace
1140 * @root: a #MemoryRegion that routes addesses for the address space
1141 * @name: an address space name. The name is only used for debugging
1142 * output.
1144 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
1148 * address_space_destroy: destroy an address space
1150 * Releases all resources associated with an address space. After an address space
1151 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1152 * as well.
1154 * @as: address space to be destroyed
1156 void address_space_destroy(AddressSpace *as);
1159 * address_space_rw: read from or write to an address space.
1161 * Return a MemTxResult indicating whether the operation succeeded
1162 * or failed (eg unassigned memory, device rejected the transaction,
1163 * IOMMU fault).
1165 * @as: #AddressSpace to be accessed
1166 * @addr: address within that address space
1167 * @attrs: memory transaction attributes
1168 * @buf: buffer with the data transferred
1169 * @is_write: indicates the transfer direction
1171 MemTxResult address_space_rw(AddressSpace *as, hwaddr addr,
1172 MemTxAttrs attrs, uint8_t *buf,
1173 int len, bool is_write);
1176 * address_space_write: write to address space.
1178 * Return a MemTxResult indicating whether the operation succeeded
1179 * or failed (eg unassigned memory, device rejected the transaction,
1180 * IOMMU fault).
1182 * @as: #AddressSpace to be accessed
1183 * @addr: address within that address space
1184 * @attrs: memory transaction attributes
1185 * @buf: buffer with the data transferred
1187 MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
1188 MemTxAttrs attrs,
1189 const uint8_t *buf, int len);
1192 * address_space_read: read from an address space.
1194 * Return a MemTxResult indicating whether the operation succeeded
1195 * or failed (eg unassigned memory, device rejected the transaction,
1196 * IOMMU fault).
1198 * @as: #AddressSpace to be accessed
1199 * @addr: address within that address space
1200 * @attrs: memory transaction attributes
1201 * @buf: buffer with the data transferred
1203 MemTxResult address_space_read(AddressSpace *as, hwaddr addr, MemTxAttrs attrs,
1204 uint8_t *buf, int len);
1207 * address_space_ld*: load from an address space
1208 * address_space_st*: store to an address space
1210 * These functions perform a load or store of the byte, word,
1211 * longword or quad to the specified address within the AddressSpace.
1212 * The _le suffixed functions treat the data as little endian;
1213 * _be indicates big endian; no suffix indicates "same endianness
1214 * as guest CPU".
1216 * The "guest CPU endianness" accessors are deprecated for use outside
1217 * target-* code; devices should be CPU-agnostic and use either the LE
1218 * or the BE accessors.
1220 * @as #AddressSpace to be accessed
1221 * @addr: address within that address space
1222 * @val: data value, for stores
1223 * @attrs: memory transaction attributes
1224 * @result: location to write the success/failure of the transaction;
1225 * if NULL, this information is discarded
1227 uint32_t address_space_ldub(AddressSpace *as, hwaddr addr,
1228 MemTxAttrs attrs, MemTxResult *result);
1229 uint32_t address_space_lduw_le(AddressSpace *as, hwaddr addr,
1230 MemTxAttrs attrs, MemTxResult *result);
1231 uint32_t address_space_lduw_be(AddressSpace *as, hwaddr addr,
1232 MemTxAttrs attrs, MemTxResult *result);
1233 uint32_t address_space_ldl_le(AddressSpace *as, hwaddr addr,
1234 MemTxAttrs attrs, MemTxResult *result);
1235 uint32_t address_space_ldl_be(AddressSpace *as, hwaddr addr,
1236 MemTxAttrs attrs, MemTxResult *result);
1237 uint64_t address_space_ldq_le(AddressSpace *as, hwaddr addr,
1238 MemTxAttrs attrs, MemTxResult *result);
1239 uint64_t address_space_ldq_be(AddressSpace *as, hwaddr addr,
1240 MemTxAttrs attrs, MemTxResult *result);
1241 void address_space_stb(AddressSpace *as, hwaddr addr, uint32_t val,
1242 MemTxAttrs attrs, MemTxResult *result);
1243 void address_space_stw_le(AddressSpace *as, hwaddr addr, uint32_t val,
1244 MemTxAttrs attrs, MemTxResult *result);
1245 void address_space_stw_be(AddressSpace *as, hwaddr addr, uint32_t val,
1246 MemTxAttrs attrs, MemTxResult *result);
1247 void address_space_stl_le(AddressSpace *as, hwaddr addr, uint32_t val,
1248 MemTxAttrs attrs, MemTxResult *result);
1249 void address_space_stl_be(AddressSpace *as, hwaddr addr, uint32_t val,
1250 MemTxAttrs attrs, MemTxResult *result);
1251 void address_space_stq_le(AddressSpace *as, hwaddr addr, uint64_t val,
1252 MemTxAttrs attrs, MemTxResult *result);
1253 void address_space_stq_be(AddressSpace *as, hwaddr addr, uint64_t val,
1254 MemTxAttrs attrs, MemTxResult *result);
1256 #ifdef NEED_CPU_H
1257 uint32_t address_space_lduw(AddressSpace *as, hwaddr addr,
1258 MemTxAttrs attrs, MemTxResult *result);
1259 uint32_t address_space_ldl(AddressSpace *as, hwaddr addr,
1260 MemTxAttrs attrs, MemTxResult *result);
1261 uint64_t address_space_ldq(AddressSpace *as, hwaddr addr,
1262 MemTxAttrs attrs, MemTxResult *result);
1263 void address_space_stl_notdirty(AddressSpace *as, hwaddr addr, uint32_t val,
1264 MemTxAttrs attrs, MemTxResult *result);
1265 void address_space_stw(AddressSpace *as, hwaddr addr, uint32_t val,
1266 MemTxAttrs attrs, MemTxResult *result);
1267 void address_space_stl(AddressSpace *as, hwaddr addr, uint32_t val,
1268 MemTxAttrs attrs, MemTxResult *result);
1269 void address_space_stq(AddressSpace *as, hwaddr addr, uint64_t val,
1270 MemTxAttrs attrs, MemTxResult *result);
1271 #endif
1273 /* address_space_translate: translate an address range into an address space
1274 * into a MemoryRegion and an address range into that section. Should be
1275 * called from an RCU critical section, to avoid that the last reference
1276 * to the returned region disappears after address_space_translate returns.
1278 * @as: #AddressSpace to be accessed
1279 * @addr: address within that address space
1280 * @xlat: pointer to address within the returned memory region section's
1281 * #MemoryRegion.
1282 * @len: pointer to length
1283 * @is_write: indicates the transfer direction
1285 MemoryRegion *address_space_translate(AddressSpace *as, hwaddr addr,
1286 hwaddr *xlat, hwaddr *len,
1287 bool is_write);
1289 /* address_space_access_valid: check for validity of accessing an address
1290 * space range
1292 * Check whether memory is assigned to the given address space range, and
1293 * access is permitted by any IOMMU regions that are active for the address
1294 * space.
1296 * For now, addr and len should be aligned to a page size. This limitation
1297 * will be lifted in the future.
1299 * @as: #AddressSpace to be accessed
1300 * @addr: address within that address space
1301 * @len: length of the area to be checked
1302 * @is_write: indicates the transfer direction
1304 bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write);
1306 /* address_space_map: map a physical memory region into a host virtual address
1308 * May map a subset of the requested range, given by and returned in @plen.
1309 * May return %NULL if resources needed to perform the mapping are exhausted.
1310 * Use only for reads OR writes - not for read-modify-write operations.
1311 * Use cpu_register_map_client() to know when retrying the map operation is
1312 * likely to succeed.
1314 * @as: #AddressSpace to be accessed
1315 * @addr: address within that address space
1316 * @plen: pointer to length of buffer; updated on return
1317 * @is_write: indicates the transfer direction
1319 void *address_space_map(AddressSpace *as, hwaddr addr,
1320 hwaddr *plen, bool is_write);
1322 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1324 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1325 * the amount of memory that was actually read or written by the caller.
1327 * @as: #AddressSpace used
1328 * @addr: address within that address space
1329 * @len: buffer length as returned by address_space_map()
1330 * @access_len: amount of data actually transferred
1331 * @is_write: indicates the transfer direction
1333 void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
1334 int is_write, hwaddr access_len);
1337 #endif
1339 #endif