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