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