esp: check command buffer length before write(CVE-2016-4439)
[qemu/ar7.git] / include / exec / memory.h
blobf649697ee9b5682fa586b01ef8cdc1a3bed8f11b
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 "exec/cpu-common.h"
25 #ifndef CONFIG_USER_ONLY
26 #include "exec/hwaddr.h"
27 #endif
28 #include "exec/memattrs.h"
29 #include "qemu/queue.h"
30 #include "qemu/int128.h"
31 #include "qemu/notify.h"
32 #include "qom/object.h"
33 #include "qemu/rcu.h"
35 #define MAX_PHYS_ADDR_SPACE_BITS 62
36 #define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
38 #define TYPE_MEMORY_REGION "qemu:memory-region"
39 #define MEMORY_REGION(obj) \
40 OBJECT_CHECK(MemoryRegion, (obj), TYPE_MEMORY_REGION)
42 typedef struct MemoryRegionOps MemoryRegionOps;
43 typedef struct MemoryRegionMmio MemoryRegionMmio;
45 struct MemoryRegionMmio {
46 CPUReadMemoryFunc *read[3];
47 CPUWriteMemoryFunc *write[3];
50 typedef struct IOMMUTLBEntry IOMMUTLBEntry;
52 /* See address_space_translate: bit 0 is read, bit 1 is write. */
53 typedef enum {
54 IOMMU_NONE = 0,
55 IOMMU_RO = 1,
56 IOMMU_WO = 2,
57 IOMMU_RW = 3,
58 } IOMMUAccessFlags;
60 struct IOMMUTLBEntry {
61 AddressSpace *target_as;
62 hwaddr iova;
63 hwaddr translated_addr;
64 hwaddr addr_mask; /* 0xfff = 4k translation */
65 IOMMUAccessFlags perm;
68 /* New-style MMIO accessors can indicate that the transaction failed.
69 * A zero (MEMTX_OK) response means success; anything else is a failure
70 * of some kind. The memory subsystem will bitwise-OR together results
71 * if it is synthesizing an operation from multiple smaller accesses.
73 #define MEMTX_OK 0
74 #define MEMTX_ERROR (1U << 0) /* device returned an error */
75 #define MEMTX_DECODE_ERROR (1U << 1) /* nothing at that address */
76 typedef uint32_t MemTxResult;
79 * Memory region callbacks
81 struct MemoryRegionOps {
82 /* Read from the memory region. @addr is relative to @mr; @size is
83 * in bytes. */
84 uint64_t (*read)(void *opaque,
85 hwaddr addr,
86 unsigned size);
87 /* Write to the memory region. @addr is relative to @mr; @size is
88 * in bytes. */
89 void (*write)(void *opaque,
90 hwaddr addr,
91 uint64_t data,
92 unsigned size);
94 MemTxResult (*read_with_attrs)(void *opaque,
95 hwaddr addr,
96 uint64_t *data,
97 unsigned size,
98 MemTxAttrs attrs);
99 MemTxResult (*write_with_attrs)(void *opaque,
100 hwaddr addr,
101 uint64_t data,
102 unsigned size,
103 MemTxAttrs attrs);
105 enum device_endian endianness;
106 /* Guest-visible constraints: */
107 struct {
108 /* If nonzero, specify bounds on access sizes beyond which a machine
109 * check is thrown.
111 unsigned min_access_size;
112 unsigned max_access_size;
113 /* If true, unaligned accesses are supported. Otherwise unaligned
114 * accesses throw machine checks.
116 bool unaligned;
118 * If present, and returns #false, the transaction is not accepted
119 * by the device (and results in machine dependent behaviour such
120 * as a machine check exception).
122 bool (*accepts)(void *opaque, hwaddr addr,
123 unsigned size, bool is_write);
124 } valid;
125 /* Internal implementation constraints: */
126 struct {
127 /* If nonzero, specifies the minimum size implemented. Smaller sizes
128 * will be rounded upwards and a partial result will be returned.
130 unsigned min_access_size;
131 /* If nonzero, specifies the maximum size implemented. Larger sizes
132 * will be done as a series of accesses with smaller sizes.
134 unsigned max_access_size;
135 /* If true, unaligned accesses are supported. Otherwise all accesses
136 * are converted to (possibly multiple) naturally aligned accesses.
138 bool unaligned;
139 } impl;
141 /* If .read and .write are not present, old_mmio may be used for
142 * backwards compatibility with old mmio registration
144 const MemoryRegionMmio old_mmio;
147 typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps;
149 struct MemoryRegionIOMMUOps {
150 /* Return a TLB entry that contains a given address. */
151 IOMMUTLBEntry (*translate)(MemoryRegion *iommu, hwaddr addr, bool is_write);
154 typedef struct CoalescedMemoryRange CoalescedMemoryRange;
155 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
157 struct MemoryRegion {
158 Object parent_obj;
160 /* All fields are private - violators will be prosecuted */
162 /* The following fields should fit in a cache line */
163 bool romd_mode;
164 bool ram;
165 bool subpage;
166 bool readonly; /* For RAM regions */
167 bool rom_device;
168 bool flush_coalesced_mmio;
169 bool global_locking;
170 uint8_t dirty_log_mask;
171 RAMBlock *ram_block;
172 Object *owner;
173 const MemoryRegionIOMMUOps *iommu_ops;
175 const MemoryRegionOps *ops;
176 void *opaque;
177 MemoryRegion *container;
178 Int128 size;
179 hwaddr addr;
180 void (*destructor)(MemoryRegion *mr);
181 uint64_t align;
182 bool terminates;
183 bool skip_dump;
184 bool enabled;
185 bool warning_printed; /* For reservations */
186 uint8_t vga_logging_count;
187 MemoryRegion *alias;
188 hwaddr alias_offset;
189 int32_t priority;
190 QTAILQ_HEAD(subregions, MemoryRegion) subregions;
191 QTAILQ_ENTRY(MemoryRegion) subregions_link;
192 QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced;
193 const char *name;
194 unsigned ioeventfd_nb;
195 MemoryRegionIoeventfd *ioeventfds;
196 NotifierList iommu_notify;
200 * MemoryListener: callbacks structure for updates to the physical memory map
202 * Allows a component to adjust to changes in the guest-visible memory map.
203 * Use with memory_listener_register() and memory_listener_unregister().
205 struct MemoryListener {
206 void (*begin)(MemoryListener *listener);
207 void (*commit)(MemoryListener *listener);
208 void (*region_add)(MemoryListener *listener, MemoryRegionSection *section);
209 void (*region_del)(MemoryListener *listener, MemoryRegionSection *section);
210 void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section);
211 void (*log_start)(MemoryListener *listener, MemoryRegionSection *section,
212 int old, int new);
213 void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section,
214 int old, int new);
215 void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section);
216 void (*log_global_start)(MemoryListener *listener);
217 void (*log_global_stop)(MemoryListener *listener);
218 void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section,
219 bool match_data, uint64_t data, EventNotifier *e);
220 void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section,
221 bool match_data, uint64_t data, EventNotifier *e);
222 void (*coalesced_mmio_add)(MemoryListener *listener, MemoryRegionSection *section,
223 hwaddr addr, hwaddr len);
224 void (*coalesced_mmio_del)(MemoryListener *listener, MemoryRegionSection *section,
225 hwaddr addr, hwaddr len);
226 /* Lower = earlier (during add), later (during del) */
227 unsigned priority;
228 AddressSpace *address_space_filter;
229 QTAILQ_ENTRY(MemoryListener) link;
233 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
235 struct AddressSpace {
236 /* All fields are private. */
237 struct rcu_head rcu;
238 char *name;
239 MemoryRegion *root;
240 int ref_count;
241 bool malloced;
243 /* Accessed via RCU. */
244 struct FlatView *current_map;
246 int ioeventfd_nb;
247 struct MemoryRegionIoeventfd *ioeventfds;
248 struct AddressSpaceDispatch *dispatch;
249 struct AddressSpaceDispatch *next_dispatch;
250 MemoryListener dispatch_listener;
252 QTAILQ_ENTRY(AddressSpace) address_spaces_link;
256 * MemoryRegionSection: describes a fragment of a #MemoryRegion
258 * @mr: the region, or %NULL if empty
259 * @address_space: the address space the region is mapped in
260 * @offset_within_region: the beginning of the section, relative to @mr's start
261 * @size: the size of the section; will not exceed @mr's boundaries
262 * @offset_within_address_space: the address of the first byte of the section
263 * relative to the region's address space
264 * @readonly: writes to this section are ignored
266 struct MemoryRegionSection {
267 MemoryRegion *mr;
268 AddressSpace *address_space;
269 hwaddr offset_within_region;
270 Int128 size;
271 hwaddr offset_within_address_space;
272 bool readonly;
276 * memory_region_init: Initialize a memory region
278 * The region typically acts as a container for other memory regions. Use
279 * memory_region_add_subregion() to add subregions.
281 * @mr: the #MemoryRegion to be initialized
282 * @owner: the object that tracks the region's reference count
283 * @name: used for debugging; not visible to the user or ABI
284 * @size: size of the region; any subregions beyond this size will be clipped
286 void memory_region_init(MemoryRegion *mr,
287 struct Object *owner,
288 const char *name,
289 uint64_t size);
292 * memory_region_ref: Add 1 to a memory region's reference count
294 * Whenever memory regions are accessed outside the BQL, they need to be
295 * preserved against hot-unplug. MemoryRegions actually do not have their
296 * own reference count; they piggyback on a QOM object, their "owner".
297 * This function adds a reference to the owner.
299 * All MemoryRegions must have an owner if they can disappear, even if the
300 * device they belong to operates exclusively under the BQL. This is because
301 * the region could be returned at any time by memory_region_find, and this
302 * is usually under guest control.
304 * @mr: the #MemoryRegion
306 void memory_region_ref(MemoryRegion *mr);
309 * memory_region_unref: Remove 1 to a memory region's reference count
311 * Whenever memory regions are accessed outside the BQL, they need to be
312 * preserved against hot-unplug. MemoryRegions actually do not have their
313 * own reference count; they piggyback on a QOM object, their "owner".
314 * This function removes a reference to the owner and possibly destroys it.
316 * @mr: the #MemoryRegion
318 void memory_region_unref(MemoryRegion *mr);
321 * memory_region_init_io: Initialize an I/O memory region.
323 * Accesses into the region will cause the callbacks in @ops to be called.
324 * if @size is nonzero, subregions will be clipped to @size.
326 * @mr: the #MemoryRegion to be initialized.
327 * @owner: the object that tracks the region's reference count
328 * @ops: a structure containing read and write callbacks to be used when
329 * I/O is performed on the region.
330 * @opaque: passed to the read and write callbacks of the @ops structure.
331 * @name: used for debugging; not visible to the user or ABI
332 * @size: size of the region.
334 void memory_region_init_io(MemoryRegion *mr,
335 struct Object *owner,
336 const MemoryRegionOps *ops,
337 void *opaque,
338 const char *name,
339 uint64_t size);
342 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
343 * region will modify memory directly.
345 * @mr: the #MemoryRegion to be initialized.
346 * @owner: the object that tracks the region's reference count
347 * @name: the name of the region.
348 * @size: size of the region.
349 * @errp: pointer to Error*, to store an error if it happens.
351 void memory_region_init_ram(MemoryRegion *mr,
352 struct Object *owner,
353 const char *name,
354 uint64_t size,
355 Error **errp);
358 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
359 * RAM. Accesses into the region will
360 * modify memory directly. Only an initial
361 * portion of this RAM is actually used.
362 * The used size can change across reboots.
364 * @mr: the #MemoryRegion to be initialized.
365 * @owner: the object that tracks the region's reference count
366 * @name: the name of the region.
367 * @size: used size of the region.
368 * @max_size: max size of the region.
369 * @resized: callback to notify owner about used size change.
370 * @errp: pointer to Error*, to store an error if it happens.
372 void memory_region_init_resizeable_ram(MemoryRegion *mr,
373 struct Object *owner,
374 const char *name,
375 uint64_t size,
376 uint64_t max_size,
377 void (*resized)(const char*,
378 uint64_t length,
379 void *host),
380 Error **errp);
381 #ifdef __linux__
383 * memory_region_init_ram_from_file: Initialize RAM memory region with a
384 * mmap-ed backend.
386 * @mr: the #MemoryRegion to be initialized.
387 * @owner: the object that tracks the region's reference count
388 * @name: the name of the region.
389 * @size: size of the region.
390 * @share: %true if memory must be mmaped with the MAP_SHARED flag
391 * @path: the path in which to allocate the RAM.
392 * @errp: pointer to Error*, to store an error if it happens.
394 void memory_region_init_ram_from_file(MemoryRegion *mr,
395 struct Object *owner,
396 const char *name,
397 uint64_t size,
398 bool share,
399 const char *path,
400 Error **errp);
401 #endif
404 * memory_region_init_ram_ptr: Initialize RAM memory region from a
405 * user-provided pointer. Accesses into the
406 * region will modify memory directly.
408 * @mr: the #MemoryRegion to be initialized.
409 * @owner: the object that tracks the region's reference count
410 * @name: the name of the region.
411 * @size: size of the region.
412 * @ptr: memory to be mapped; must contain at least @size bytes.
414 void memory_region_init_ram_ptr(MemoryRegion *mr,
415 struct Object *owner,
416 const char *name,
417 uint64_t size,
418 void *ptr);
421 * memory_region_init_alias: Initialize a memory region that aliases all or a
422 * part of another memory region.
424 * @mr: the #MemoryRegion to be initialized.
425 * @owner: the object that tracks the region's reference count
426 * @name: used for debugging; not visible to the user or ABI
427 * @orig: the region to be referenced; @mr will be equivalent to
428 * @orig between @offset and @offset + @size - 1.
429 * @offset: start of the section in @orig to be referenced.
430 * @size: size of the region.
432 void memory_region_init_alias(MemoryRegion *mr,
433 struct Object *owner,
434 const char *name,
435 MemoryRegion *orig,
436 hwaddr offset,
437 uint64_t size);
440 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
441 * handled via callbacks.
443 * If NULL callbacks pointer is given, then I/O space is not supposed to be
444 * handled by QEMU itself. Any access via the memory API will cause an abort().
446 * @mr: the #MemoryRegion to be initialized.
447 * @owner: the object that tracks the region's reference count
448 * @ops: callbacks for write access handling.
449 * @name: the name of the region.
450 * @size: size of the region.
451 * @errp: pointer to Error*, to store an error if it happens.
453 void memory_region_init_rom_device(MemoryRegion *mr,
454 struct Object *owner,
455 const MemoryRegionOps *ops,
456 void *opaque,
457 const char *name,
458 uint64_t size,
459 Error **errp);
462 * memory_region_init_reservation: Initialize a memory region that reserves
463 * I/O space.
465 * A reservation region primariy serves debugging purposes. It claims I/O
466 * space that is not supposed to be handled by QEMU itself. Any access via
467 * the memory API will cause an abort().
468 * This function is deprecated. Use memory_region_init_io() with NULL
469 * callbacks instead.
471 * @mr: the #MemoryRegion to be initialized
472 * @owner: the object that tracks the region's reference count
473 * @name: used for debugging; not visible to the user or ABI
474 * @size: size of the region.
476 static inline void memory_region_init_reservation(MemoryRegion *mr,
477 Object *owner,
478 const char *name,
479 uint64_t size)
481 memory_region_init_io(mr, owner, NULL, mr, name, size);
485 * memory_region_init_iommu: Initialize a memory region that translates
486 * addresses
488 * An IOMMU region translates addresses and forwards accesses to a target
489 * memory region.
491 * @mr: the #MemoryRegion to be initialized
492 * @owner: the object that tracks the region's reference count
493 * @ops: a function that translates addresses into the @target region
494 * @name: used for debugging; not visible to the user or ABI
495 * @size: size of the region.
497 void memory_region_init_iommu(MemoryRegion *mr,
498 struct Object *owner,
499 const MemoryRegionIOMMUOps *ops,
500 const char *name,
501 uint64_t size);
504 * memory_region_owner: get a memory region's owner.
506 * @mr: the memory region being queried.
508 struct Object *memory_region_owner(MemoryRegion *mr);
511 * memory_region_size: get a memory region's size.
513 * @mr: the memory region being queried.
515 uint64_t memory_region_size(MemoryRegion *mr);
518 * memory_region_is_ram: check whether a memory region is random access
520 * Returns %true is a memory region is random access.
522 * @mr: the memory region being queried
524 static inline bool memory_region_is_ram(MemoryRegion *mr)
526 return mr->ram;
530 * memory_region_is_skip_dump: check whether a memory region should not be
531 * dumped
533 * Returns %true is a memory region should not be dumped(e.g. VFIO BAR MMAP).
535 * @mr: the memory region being queried
537 bool memory_region_is_skip_dump(MemoryRegion *mr);
540 * memory_region_set_skip_dump: Set skip_dump flag, dump will ignore this memory
541 * region
543 * @mr: the memory region being queried
545 void memory_region_set_skip_dump(MemoryRegion *mr);
548 * memory_region_is_romd: check whether a memory region is in ROMD mode
550 * Returns %true if a memory region is a ROM device and currently set to allow
551 * direct reads.
553 * @mr: the memory region being queried
555 static inline bool memory_region_is_romd(MemoryRegion *mr)
557 return mr->rom_device && mr->romd_mode;
561 * memory_region_is_iommu: check whether a memory region is an iommu
563 * Returns %true is a memory region is an iommu.
565 * @mr: the memory region being queried
567 static inline bool memory_region_is_iommu(MemoryRegion *mr)
569 return mr->iommu_ops;
574 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
576 * @mr: the memory region that was changed
577 * @entry: the new entry in the IOMMU translation table. The entry
578 * replaces all old entries for the same virtual I/O address range.
579 * Deleted entries have .@perm == 0.
581 void memory_region_notify_iommu(MemoryRegion *mr,
582 IOMMUTLBEntry entry);
585 * memory_region_register_iommu_notifier: register a notifier for changes to
586 * IOMMU translation entries.
588 * @mr: the memory region to observe
589 * @n: the notifier to be added; the notifier receives a pointer to an
590 * #IOMMUTLBEntry as the opaque value; the pointer ceases to be
591 * valid on exit from the notifier.
593 void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n);
596 * memory_region_iommu_replay: replay existing IOMMU translations to
597 * a notifier
599 * @mr: the memory region to observe
600 * @n: the notifier to which to replay iommu mappings
601 * @granularity: Minimum page granularity to replay notifications for
602 * @is_write: Whether to treat the replay as a translate "write"
603 * through the iommu
605 void memory_region_iommu_replay(MemoryRegion *mr, Notifier *n,
606 hwaddr granularity, bool is_write);
609 * memory_region_unregister_iommu_notifier: unregister a notifier for
610 * changes to IOMMU translation entries.
612 * @n: the notifier to be removed.
614 void memory_region_unregister_iommu_notifier(Notifier *n);
617 * memory_region_name: get a memory region's name
619 * Returns the string that was used to initialize the memory region.
621 * @mr: the memory region being queried
623 const char *memory_region_name(const MemoryRegion *mr);
626 * memory_region_is_logging: return whether a memory region is logging writes
628 * Returns %true if the memory region is logging writes for the given client
630 * @mr: the memory region being queried
631 * @client: the client being queried
633 bool memory_region_is_logging(MemoryRegion *mr, uint8_t client);
636 * memory_region_get_dirty_log_mask: return the clients for which a
637 * memory region is logging writes.
639 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
640 * are the bit indices.
642 * @mr: the memory region being queried
644 uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr);
647 * memory_region_is_rom: check whether a memory region is ROM
649 * Returns %true is a memory region is read-only memory.
651 * @mr: the memory region being queried
653 static inline bool memory_region_is_rom(MemoryRegion *mr)
655 return mr->ram && mr->readonly;
660 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
662 * Returns a file descriptor backing a file-based RAM memory region,
663 * or -1 if the region is not a file-based RAM memory region.
665 * @mr: the RAM or alias memory region being queried.
667 int memory_region_get_fd(MemoryRegion *mr);
670 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
672 * Returns a host pointer to a RAM memory region (created with
673 * memory_region_init_ram() or memory_region_init_ram_ptr()).
675 * Use with care; by the time this function returns, the returned pointer is
676 * not protected by RCU anymore. If the caller is not within an RCU critical
677 * section and does not hold the iothread lock, it must have other means of
678 * protecting the pointer, such as a reference to the region that includes
679 * the incoming ram_addr_t.
681 * @mr: the memory region being queried.
683 void *memory_region_get_ram_ptr(MemoryRegion *mr);
685 /* memory_region_ram_resize: Resize a RAM region.
687 * Only legal before guest might have detected the memory size: e.g. on
688 * incoming migration, or right after reset.
690 * @mr: a memory region created with @memory_region_init_resizeable_ram.
691 * @newsize: the new size the region
692 * @errp: pointer to Error*, to store an error if it happens.
694 void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize,
695 Error **errp);
698 * memory_region_set_log: Turn dirty logging on or off for a region.
700 * Turns dirty logging on or off for a specified client (display, migration).
701 * Only meaningful for RAM regions.
703 * @mr: the memory region being updated.
704 * @log: whether dirty logging is to be enabled or disabled.
705 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
707 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
710 * memory_region_get_dirty: Check whether a range of bytes is dirty
711 * for a specified client.
713 * Checks whether a range of bytes has been written to since the last
714 * call to memory_region_reset_dirty() with the same @client. Dirty logging
715 * must be enabled.
717 * @mr: the memory region being queried.
718 * @addr: the address (relative to the start of the region) being queried.
719 * @size: the size of the range being queried.
720 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
721 * %DIRTY_MEMORY_VGA.
723 bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
724 hwaddr size, unsigned client);
727 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
729 * Marks a range of bytes as dirty, after it has been dirtied outside
730 * guest code.
732 * @mr: the memory region being dirtied.
733 * @addr: the address (relative to the start of the region) being dirtied.
734 * @size: size of the range being dirtied.
736 void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
737 hwaddr size);
740 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty
741 * for a specified client. It clears them.
743 * Checks whether a range of bytes has been written to since the last
744 * call to memory_region_reset_dirty() with the same @client. Dirty logging
745 * must be enabled.
747 * @mr: the memory region being queried.
748 * @addr: the address (relative to the start of the region) being queried.
749 * @size: the size of the range being queried.
750 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
751 * %DIRTY_MEMORY_VGA.
753 bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr,
754 hwaddr size, unsigned client);
756 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
757 * any external TLBs (e.g. kvm)
759 * Flushes dirty information from accelerators such as kvm and vhost-net
760 * and makes it available to users of the memory API.
762 * @mr: the region being flushed.
764 void memory_region_sync_dirty_bitmap(MemoryRegion *mr);
767 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
768 * client.
770 * Marks a range of pages as no longer dirty.
772 * @mr: the region being updated.
773 * @addr: the start of the subrange being cleaned.
774 * @size: the size of the subrange being cleaned.
775 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
776 * %DIRTY_MEMORY_VGA.
778 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
779 hwaddr size, unsigned client);
782 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
784 * Allows a memory region to be marked as read-only (turning it into a ROM).
785 * only useful on RAM regions.
787 * @mr: the region being updated.
788 * @readonly: whether rhe region is to be ROM or RAM.
790 void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
793 * memory_region_rom_device_set_romd: enable/disable ROMD mode
795 * Allows a ROM device (initialized with memory_region_init_rom_device() to
796 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
797 * device is mapped to guest memory and satisfies read access directly.
798 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
799 * Writes are always handled by the #MemoryRegion.write function.
801 * @mr: the memory region to be updated
802 * @romd_mode: %true to put the region into ROMD mode
804 void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode);
807 * memory_region_set_coalescing: Enable memory coalescing for the region.
809 * Enabled writes to a region to be queued for later processing. MMIO ->write
810 * callbacks may be delayed until a non-coalesced MMIO is issued.
811 * Only useful for IO regions. Roughly similar to write-combining hardware.
813 * @mr: the memory region to be write coalesced
815 void memory_region_set_coalescing(MemoryRegion *mr);
818 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
819 * a region.
821 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
822 * Multiple calls can be issued coalesced disjoint ranges.
824 * @mr: the memory region to be updated.
825 * @offset: the start of the range within the region to be coalesced.
826 * @size: the size of the subrange to be coalesced.
828 void memory_region_add_coalescing(MemoryRegion *mr,
829 hwaddr offset,
830 uint64_t size);
833 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
835 * Disables any coalescing caused by memory_region_set_coalescing() or
836 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
837 * hardware.
839 * @mr: the memory region to be updated.
841 void memory_region_clear_coalescing(MemoryRegion *mr);
844 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
845 * accesses.
847 * Ensure that pending coalesced MMIO request are flushed before the memory
848 * region is accessed. This property is automatically enabled for all regions
849 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
851 * @mr: the memory region to be updated.
853 void memory_region_set_flush_coalesced(MemoryRegion *mr);
856 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
857 * accesses.
859 * Clear the automatic coalesced MMIO flushing enabled via
860 * memory_region_set_flush_coalesced. Note that this service has no effect on
861 * memory regions that have MMIO coalescing enabled for themselves. For them,
862 * automatic flushing will stop once coalescing is disabled.
864 * @mr: the memory region to be updated.
866 void memory_region_clear_flush_coalesced(MemoryRegion *mr);
869 * memory_region_set_global_locking: Declares the access processing requires
870 * QEMU's global lock.
872 * When this is invoked, accesses to the memory region will be processed while
873 * holding the global lock of QEMU. This is the default behavior of memory
874 * regions.
876 * @mr: the memory region to be updated.
878 void memory_region_set_global_locking(MemoryRegion *mr);
881 * memory_region_clear_global_locking: Declares that access processing does
882 * not depend on the QEMU global lock.
884 * By clearing this property, accesses to the memory region will be processed
885 * outside of QEMU's global lock (unless the lock is held on when issuing the
886 * access request). In this case, the device model implementing the access
887 * handlers is responsible for synchronization of concurrency.
889 * @mr: the memory region to be updated.
891 void memory_region_clear_global_locking(MemoryRegion *mr);
894 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
895 * is written to a location.
897 * Marks a word in an IO region (initialized with memory_region_init_io())
898 * as a trigger for an eventfd event. The I/O callback will not be called.
899 * The caller must be prepared to handle failure (that is, take the required
900 * action if the callback _is_ called).
902 * @mr: the memory region being updated.
903 * @addr: the address within @mr that is to be monitored
904 * @size: the size of the access to trigger the eventfd
905 * @match_data: whether to match against @data, instead of just @addr
906 * @data: the data to match against the guest write
907 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
909 void memory_region_add_eventfd(MemoryRegion *mr,
910 hwaddr addr,
911 unsigned size,
912 bool match_data,
913 uint64_t data,
914 EventNotifier *e);
917 * memory_region_del_eventfd: Cancel an eventfd.
919 * Cancels an eventfd trigger requested by a previous
920 * memory_region_add_eventfd() call.
922 * @mr: the memory region being updated.
923 * @addr: the address within @mr that is to be monitored
924 * @size: the size of the access to trigger the eventfd
925 * @match_data: whether to match against @data, instead of just @addr
926 * @data: the data to match against the guest write
927 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
929 void memory_region_del_eventfd(MemoryRegion *mr,
930 hwaddr addr,
931 unsigned size,
932 bool match_data,
933 uint64_t data,
934 EventNotifier *e);
937 * memory_region_add_subregion: Add a subregion to a container.
939 * Adds a subregion at @offset. The subregion may not overlap with other
940 * subregions (except for those explicitly marked as overlapping). A region
941 * may only be added once as a subregion (unless removed with
942 * memory_region_del_subregion()); use memory_region_init_alias() if you
943 * want a region to be a subregion in multiple locations.
945 * @mr: the region to contain the new subregion; must be a container
946 * initialized with memory_region_init().
947 * @offset: the offset relative to @mr where @subregion is added.
948 * @subregion: the subregion to be added.
950 void memory_region_add_subregion(MemoryRegion *mr,
951 hwaddr offset,
952 MemoryRegion *subregion);
954 * memory_region_add_subregion_overlap: Add a subregion to a container
955 * with overlap.
957 * Adds a subregion at @offset. The subregion may overlap with other
958 * subregions. Conflicts are resolved by having a higher @priority hide a
959 * lower @priority. Subregions without priority are taken as @priority 0.
960 * A region may only be added once as a subregion (unless removed with
961 * memory_region_del_subregion()); use memory_region_init_alias() if you
962 * want a region to be a subregion in multiple locations.
964 * @mr: the region to contain the new subregion; must be a container
965 * initialized with memory_region_init().
966 * @offset: the offset relative to @mr where @subregion is added.
967 * @subregion: the subregion to be added.
968 * @priority: used for resolving overlaps; highest priority wins.
970 void memory_region_add_subregion_overlap(MemoryRegion *mr,
971 hwaddr offset,
972 MemoryRegion *subregion,
973 int priority);
976 * memory_region_get_ram_addr: Get the ram address associated with a memory
977 * region
979 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr);
981 uint64_t memory_region_get_alignment(const MemoryRegion *mr);
983 * memory_region_del_subregion: Remove a subregion.
985 * Removes a subregion from its container.
987 * @mr: the container to be updated.
988 * @subregion: the region being removed; must be a current subregion of @mr.
990 void memory_region_del_subregion(MemoryRegion *mr,
991 MemoryRegion *subregion);
994 * memory_region_set_enabled: dynamically enable or disable a region
996 * Enables or disables a memory region. A disabled memory region
997 * ignores all accesses to itself and its subregions. It does not
998 * obscure sibling subregions with lower priority - it simply behaves as
999 * if it was removed from the hierarchy.
1001 * Regions default to being enabled.
1003 * @mr: the region to be updated
1004 * @enabled: whether to enable or disable the region
1006 void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
1009 * memory_region_set_address: dynamically update the address of a region
1011 * Dynamically updates the address of a region, relative to its container.
1012 * May be used on regions are currently part of a memory hierarchy.
1014 * @mr: the region to be updated
1015 * @addr: new address, relative to container region
1017 void memory_region_set_address(MemoryRegion *mr, hwaddr addr);
1020 * memory_region_set_size: dynamically update the size of a region.
1022 * Dynamically updates the size of a region.
1024 * @mr: the region to be updated
1025 * @size: used size of the region.
1027 void memory_region_set_size(MemoryRegion *mr, uint64_t size);
1030 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1032 * Dynamically updates the offset into the target region that an alias points
1033 * to, as if the fourth argument to memory_region_init_alias() has changed.
1035 * @mr: the #MemoryRegion to be updated; should be an alias.
1036 * @offset: the new offset into the target memory region
1038 void memory_region_set_alias_offset(MemoryRegion *mr,
1039 hwaddr offset);
1042 * memory_region_present: checks if an address relative to a @container
1043 * translates into #MemoryRegion within @container
1045 * Answer whether a #MemoryRegion within @container covers the address
1046 * @addr.
1048 * @container: a #MemoryRegion within which @addr is a relative address
1049 * @addr: the area within @container to be searched
1051 bool memory_region_present(MemoryRegion *container, hwaddr addr);
1054 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1055 * into any address space.
1057 * @mr: a #MemoryRegion which should be checked if it's mapped
1059 bool memory_region_is_mapped(MemoryRegion *mr);
1062 * memory_region_find: translate an address/size relative to a
1063 * MemoryRegion into a #MemoryRegionSection.
1065 * Locates the first #MemoryRegion within @mr that overlaps the range
1066 * given by @addr and @size.
1068 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1069 * It will have the following characteristics:
1070 * .@size = 0 iff no overlap was found
1071 * .@mr is non-%NULL iff an overlap was found
1073 * Remember that in the return value the @offset_within_region is
1074 * relative to the returned region (in the .@mr field), not to the
1075 * @mr argument.
1077 * Similarly, the .@offset_within_address_space is relative to the
1078 * address space that contains both regions, the passed and the
1079 * returned one. However, in the special case where the @mr argument
1080 * has no container (and thus is the root of the address space), the
1081 * following will hold:
1082 * .@offset_within_address_space >= @addr
1083 * .@offset_within_address_space + .@size <= @addr + @size
1085 * @mr: a MemoryRegion within which @addr is a relative address
1086 * @addr: start of the area within @as to be searched
1087 * @size: size of the area to be searched
1089 MemoryRegionSection memory_region_find(MemoryRegion *mr,
1090 hwaddr addr, uint64_t size);
1093 * address_space_sync_dirty_bitmap: synchronize the dirty log for all memory
1095 * Synchronizes the dirty page log for an entire address space.
1096 * @as: the address space that contains the memory being synchronized
1098 void address_space_sync_dirty_bitmap(AddressSpace *as);
1101 * memory_region_transaction_begin: Start a transaction.
1103 * During a transaction, changes will be accumulated and made visible
1104 * only when the transaction ends (is committed).
1106 void memory_region_transaction_begin(void);
1109 * memory_region_transaction_commit: Commit a transaction and make changes
1110 * visible to the guest.
1112 void memory_region_transaction_commit(void);
1115 * memory_listener_register: register callbacks to be called when memory
1116 * sections are mapped or unmapped into an address
1117 * space
1119 * @listener: an object containing the callbacks to be called
1120 * @filter: if non-%NULL, only regions in this address space will be observed
1122 void memory_listener_register(MemoryListener *listener, AddressSpace *filter);
1125 * memory_listener_unregister: undo the effect of memory_listener_register()
1127 * @listener: an object containing the callbacks to be removed
1129 void memory_listener_unregister(MemoryListener *listener);
1132 * memory_global_dirty_log_start: begin dirty logging for all regions
1134 void memory_global_dirty_log_start(void);
1137 * memory_global_dirty_log_stop: end dirty logging for all regions
1139 void memory_global_dirty_log_stop(void);
1141 void mtree_info(fprintf_function mon_printf, void *f);
1144 * memory_region_dispatch_read: perform a read directly to the specified
1145 * MemoryRegion.
1147 * @mr: #MemoryRegion to access
1148 * @addr: address within that region
1149 * @pval: pointer to uint64_t which the data is written to
1150 * @size: size of the access in bytes
1151 * @attrs: memory transaction attributes to use for the access
1153 MemTxResult memory_region_dispatch_read(MemoryRegion *mr,
1154 hwaddr addr,
1155 uint64_t *pval,
1156 unsigned size,
1157 MemTxAttrs attrs);
1159 * memory_region_dispatch_write: perform a write directly to the specified
1160 * MemoryRegion.
1162 * @mr: #MemoryRegion to access
1163 * @addr: address within that region
1164 * @data: data to write
1165 * @size: size of the access in bytes
1166 * @attrs: memory transaction attributes to use for the access
1168 MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
1169 hwaddr addr,
1170 uint64_t data,
1171 unsigned size,
1172 MemTxAttrs attrs);
1175 * address_space_init: initializes an address space
1177 * @as: an uninitialized #AddressSpace
1178 * @root: a #MemoryRegion that routes addresses for the address space
1179 * @name: an address space name. The name is only used for debugging
1180 * output.
1182 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
1185 * address_space_init_shareable: return an address space for a memory region,
1186 * creating it if it does not already exist
1188 * @root: a #MemoryRegion that routes addresses for the address space
1189 * @name: an address space name. The name is only used for debugging
1190 * output.
1192 * This function will return a pointer to an existing AddressSpace
1193 * which was initialized with the specified MemoryRegion, or it will
1194 * create and initialize one if it does not already exist. The ASes
1195 * are reference-counted, so the memory will be freed automatically
1196 * when the AddressSpace is destroyed via address_space_destroy.
1198 AddressSpace *address_space_init_shareable(MemoryRegion *root,
1199 const char *name);
1202 * address_space_destroy: destroy an address space
1204 * Releases all resources associated with an address space. After an address space
1205 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1206 * as well.
1208 * @as: address space to be destroyed
1210 void address_space_destroy(AddressSpace *as);
1213 * address_space_rw: read from or write to an address space.
1215 * Return a MemTxResult indicating whether the operation succeeded
1216 * or failed (eg unassigned memory, device rejected the transaction,
1217 * IOMMU fault).
1219 * @as: #AddressSpace to be accessed
1220 * @addr: address within that address space
1221 * @attrs: memory transaction attributes
1222 * @buf: buffer with the data transferred
1223 * @is_write: indicates the transfer direction
1225 MemTxResult address_space_rw(AddressSpace *as, hwaddr addr,
1226 MemTxAttrs attrs, uint8_t *buf,
1227 int len, bool is_write);
1230 * address_space_write: write to address space.
1232 * Return a MemTxResult indicating whether the operation succeeded
1233 * or failed (eg unassigned memory, device rejected the transaction,
1234 * IOMMU fault).
1236 * @as: #AddressSpace to be accessed
1237 * @addr: address within that address space
1238 * @attrs: memory transaction attributes
1239 * @buf: buffer with the data transferred
1241 MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
1242 MemTxAttrs attrs,
1243 const uint8_t *buf, int len);
1245 /* address_space_ld*: load from an address space
1246 * address_space_st*: store to an address space
1248 * These functions perform a load or store of the byte, word,
1249 * longword or quad to the specified address within the AddressSpace.
1250 * The _le suffixed functions treat the data as little endian;
1251 * _be indicates big endian; no suffix indicates "same endianness
1252 * as guest CPU".
1254 * The "guest CPU endianness" accessors are deprecated for use outside
1255 * target-* code; devices should be CPU-agnostic and use either the LE
1256 * or the BE accessors.
1258 * @as #AddressSpace to be accessed
1259 * @addr: address within that address space
1260 * @val: data value, for stores
1261 * @attrs: memory transaction attributes
1262 * @result: location to write the success/failure of the transaction;
1263 * if NULL, this information is discarded
1265 uint32_t address_space_ldub(AddressSpace *as, hwaddr addr,
1266 MemTxAttrs attrs, MemTxResult *result);
1267 uint32_t address_space_lduw_le(AddressSpace *as, hwaddr addr,
1268 MemTxAttrs attrs, MemTxResult *result);
1269 uint32_t address_space_lduw_be(AddressSpace *as, hwaddr addr,
1270 MemTxAttrs attrs, MemTxResult *result);
1271 uint32_t address_space_ldl_le(AddressSpace *as, hwaddr addr,
1272 MemTxAttrs attrs, MemTxResult *result);
1273 uint32_t address_space_ldl_be(AddressSpace *as, hwaddr addr,
1274 MemTxAttrs attrs, MemTxResult *result);
1275 uint64_t address_space_ldq_le(AddressSpace *as, hwaddr addr,
1276 MemTxAttrs attrs, MemTxResult *result);
1277 uint64_t address_space_ldq_be(AddressSpace *as, hwaddr addr,
1278 MemTxAttrs attrs, MemTxResult *result);
1279 void address_space_stb(AddressSpace *as, hwaddr addr, uint32_t val,
1280 MemTxAttrs attrs, MemTxResult *result);
1281 void address_space_stw_le(AddressSpace *as, hwaddr addr, uint32_t val,
1282 MemTxAttrs attrs, MemTxResult *result);
1283 void address_space_stw_be(AddressSpace *as, hwaddr addr, uint32_t val,
1284 MemTxAttrs attrs, MemTxResult *result);
1285 void address_space_stl_le(AddressSpace *as, hwaddr addr, uint32_t val,
1286 MemTxAttrs attrs, MemTxResult *result);
1287 void address_space_stl_be(AddressSpace *as, hwaddr addr, uint32_t val,
1288 MemTxAttrs attrs, MemTxResult *result);
1289 void address_space_stq_le(AddressSpace *as, hwaddr addr, uint64_t val,
1290 MemTxAttrs attrs, MemTxResult *result);
1291 void address_space_stq_be(AddressSpace *as, hwaddr addr, uint64_t val,
1292 MemTxAttrs attrs, MemTxResult *result);
1294 /* address_space_translate: translate an address range into an address space
1295 * into a MemoryRegion and an address range into that section. Should be
1296 * called from an RCU critical section, to avoid that the last reference
1297 * to the returned region disappears after address_space_translate returns.
1299 * @as: #AddressSpace to be accessed
1300 * @addr: address within that address space
1301 * @xlat: pointer to address within the returned memory region section's
1302 * #MemoryRegion.
1303 * @len: pointer to length
1304 * @is_write: indicates the transfer direction
1306 MemoryRegion *address_space_translate(AddressSpace *as, hwaddr addr,
1307 hwaddr *xlat, hwaddr *len,
1308 bool is_write);
1310 /* address_space_access_valid: check for validity of accessing an address
1311 * space range
1313 * Check whether memory is assigned to the given address space range, and
1314 * access is permitted by any IOMMU regions that are active for the address
1315 * space.
1317 * For now, addr and len should be aligned to a page size. This limitation
1318 * will be lifted in the future.
1320 * @as: #AddressSpace to be accessed
1321 * @addr: address within that address space
1322 * @len: length of the area to be checked
1323 * @is_write: indicates the transfer direction
1325 bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write);
1327 /* address_space_map: map a physical memory region into a host virtual address
1329 * May map a subset of the requested range, given by and returned in @plen.
1330 * May return %NULL if resources needed to perform the mapping are exhausted.
1331 * Use only for reads OR writes - not for read-modify-write operations.
1332 * Use cpu_register_map_client() to know when retrying the map operation is
1333 * likely to succeed.
1335 * @as: #AddressSpace to be accessed
1336 * @addr: address within that address space
1337 * @plen: pointer to length of buffer; updated on return
1338 * @is_write: indicates the transfer direction
1340 void *address_space_map(AddressSpace *as, hwaddr addr,
1341 hwaddr *plen, bool is_write);
1343 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1345 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1346 * the amount of memory that was actually read or written by the caller.
1348 * @as: #AddressSpace used
1349 * @addr: address within that address space
1350 * @len: buffer length as returned by address_space_map()
1351 * @access_len: amount of data actually transferred
1352 * @is_write: indicates the transfer direction
1354 void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
1355 int is_write, hwaddr access_len);
1358 /* Internal functions, part of the implementation of address_space_read. */
1359 MemTxResult address_space_read_continue(AddressSpace *as, hwaddr addr,
1360 MemTxAttrs attrs, uint8_t *buf,
1361 int len, hwaddr addr1, hwaddr l,
1362 MemoryRegion *mr);
1363 MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr,
1364 MemTxAttrs attrs, uint8_t *buf, int len);
1365 void *qemu_get_ram_ptr(RAMBlock *ram_block, ram_addr_t addr);
1367 static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
1369 if (is_write) {
1370 return memory_region_is_ram(mr) && !mr->readonly;
1371 } else {
1372 return memory_region_is_ram(mr) || memory_region_is_romd(mr);
1377 * address_space_read: read from an address space.
1379 * Return a MemTxResult indicating whether the operation succeeded
1380 * or failed (eg unassigned memory, device rejected the transaction,
1381 * IOMMU fault).
1383 * @as: #AddressSpace to be accessed
1384 * @addr: address within that address space
1385 * @attrs: memory transaction attributes
1386 * @buf: buffer with the data transferred
1388 static inline __attribute__((__always_inline__))
1389 MemTxResult address_space_read(AddressSpace *as, hwaddr addr, MemTxAttrs attrs,
1390 uint8_t *buf, int len)
1392 MemTxResult result = MEMTX_OK;
1393 hwaddr l, addr1;
1394 void *ptr;
1395 MemoryRegion *mr;
1397 if (__builtin_constant_p(len)) {
1398 if (len) {
1399 rcu_read_lock();
1400 l = len;
1401 mr = address_space_translate(as, addr, &addr1, &l, false);
1402 if (len == l && memory_access_is_direct(mr, false)) {
1403 addr1 += memory_region_get_ram_addr(mr);
1404 ptr = qemu_get_ram_ptr(mr->ram_block, addr1);
1405 memcpy(buf, ptr, len);
1406 } else {
1407 result = address_space_read_continue(as, addr, attrs, buf, len,
1408 addr1, l, mr);
1410 rcu_read_unlock();
1412 } else {
1413 result = address_space_read_full(as, addr, attrs, buf, len);
1415 return result;
1418 #endif
1420 #endif