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