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