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