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