]> git.proxmox.com Git - mirror_qemu.git/blob - include/exec/memory.h
memory: provide iommu_replay_all()
[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_register_iommu_notifier: register a notifier for changes to
692 * IOMMU translation entries.
693 *
694 * @mr: the memory region to observe
695 * @n: the IOMMUNotifier to be added; the notify callback receives a
696 * pointer to an #IOMMUTLBEntry as the opaque value; the pointer
697 * ceases to be valid on exit from the notifier.
698 */
699 void memory_region_register_iommu_notifier(MemoryRegion *mr,
700 IOMMUNotifier *n);
701
702 /**
703 * memory_region_iommu_replay: replay existing IOMMU translations to
704 * a notifier with the minimum page granularity returned by
705 * mr->iommu_ops->get_page_size().
706 *
707 * @mr: the memory region to observe
708 * @n: the notifier to which to replay iommu mappings
709 * @is_write: Whether to treat the replay as a translate "write"
710 * through the iommu
711 */
712 void memory_region_iommu_replay(MemoryRegion *mr, IOMMUNotifier *n,
713 bool is_write);
714
715 /**
716 * memory_region_iommu_replay_all: replay existing IOMMU translations
717 * to all the notifiers registered.
718 *
719 * @mr: the memory region to observe
720 */
721 void memory_region_iommu_replay_all(MemoryRegion *mr);
722
723 /**
724 * memory_region_unregister_iommu_notifier: unregister a notifier for
725 * changes to IOMMU translation entries.
726 *
727 * @mr: the memory region which was observed and for which notity_stopped()
728 * needs to be called
729 * @n: the notifier to be removed.
730 */
731 void memory_region_unregister_iommu_notifier(MemoryRegion *mr,
732 IOMMUNotifier *n);
733
734 /**
735 * memory_region_name: get a memory region's name
736 *
737 * Returns the string that was used to initialize the memory region.
738 *
739 * @mr: the memory region being queried
740 */
741 const char *memory_region_name(const MemoryRegion *mr);
742
743 /**
744 * memory_region_is_logging: return whether a memory region is logging writes
745 *
746 * Returns %true if the memory region is logging writes for the given client
747 *
748 * @mr: the memory region being queried
749 * @client: the client being queried
750 */
751 bool memory_region_is_logging(MemoryRegion *mr, uint8_t client);
752
753 /**
754 * memory_region_get_dirty_log_mask: return the clients for which a
755 * memory region is logging writes.
756 *
757 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
758 * are the bit indices.
759 *
760 * @mr: the memory region being queried
761 */
762 uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr);
763
764 /**
765 * memory_region_is_rom: check whether a memory region is ROM
766 *
767 * Returns %true is a memory region is read-only memory.
768 *
769 * @mr: the memory region being queried
770 */
771 static inline bool memory_region_is_rom(MemoryRegion *mr)
772 {
773 return mr->ram && mr->readonly;
774 }
775
776
777 /**
778 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
779 *
780 * Returns a file descriptor backing a file-based RAM memory region,
781 * or -1 if the region is not a file-based RAM memory region.
782 *
783 * @mr: the RAM or alias memory region being queried.
784 */
785 int memory_region_get_fd(MemoryRegion *mr);
786
787 /**
788 * memory_region_set_fd: Mark a RAM memory region as backed by a
789 * file descriptor.
790 *
791 * This function is typically used after memory_region_init_ram_ptr().
792 *
793 * @mr: the memory region being queried.
794 * @fd: the file descriptor that backs @mr.
795 */
796 void memory_region_set_fd(MemoryRegion *mr, int fd);
797
798 /**
799 * memory_region_from_host: Convert a pointer into a RAM memory region
800 * and an offset within it.
801 *
802 * Given a host pointer inside a RAM memory region (created with
803 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
804 * the MemoryRegion and the offset within it.
805 *
806 * Use with care; by the time this function returns, the returned pointer is
807 * not protected by RCU anymore. If the caller is not within an RCU critical
808 * section and does not hold the iothread lock, it must have other means of
809 * protecting the pointer, such as a reference to the region that includes
810 * the incoming ram_addr_t.
811 *
812 * @mr: the memory region being queried.
813 */
814 MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset);
815
816 /**
817 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
818 *
819 * Returns a host pointer to a RAM memory region (created with
820 * memory_region_init_ram() or memory_region_init_ram_ptr()).
821 *
822 * Use with care; by the time this function returns, the returned pointer is
823 * not protected by RCU anymore. If the caller is not within an RCU critical
824 * section and does not hold the iothread lock, it must have other means of
825 * protecting the pointer, such as a reference to the region that includes
826 * the incoming ram_addr_t.
827 *
828 * @mr: the memory region being queried.
829 */
830 void *memory_region_get_ram_ptr(MemoryRegion *mr);
831
832 /* memory_region_ram_resize: Resize a RAM region.
833 *
834 * Only legal before guest might have detected the memory size: e.g. on
835 * incoming migration, or right after reset.
836 *
837 * @mr: a memory region created with @memory_region_init_resizeable_ram.
838 * @newsize: the new size the region
839 * @errp: pointer to Error*, to store an error if it happens.
840 */
841 void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize,
842 Error **errp);
843
844 /**
845 * memory_region_set_log: Turn dirty logging on or off for a region.
846 *
847 * Turns dirty logging on or off for a specified client (display, migration).
848 * Only meaningful for RAM regions.
849 *
850 * @mr: the memory region being updated.
851 * @log: whether dirty logging is to be enabled or disabled.
852 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
853 */
854 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
855
856 /**
857 * memory_region_get_dirty: Check whether a range of bytes is dirty
858 * for a specified client.
859 *
860 * Checks whether a range of bytes has been written to since the last
861 * call to memory_region_reset_dirty() with the same @client. Dirty logging
862 * must be enabled.
863 *
864 * @mr: the memory region being queried.
865 * @addr: the address (relative to the start of the region) being queried.
866 * @size: the size of the range being queried.
867 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
868 * %DIRTY_MEMORY_VGA.
869 */
870 bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
871 hwaddr size, unsigned client);
872
873 /**
874 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
875 *
876 * Marks a range of bytes as dirty, after it has been dirtied outside
877 * guest code.
878 *
879 * @mr: the memory region being dirtied.
880 * @addr: the address (relative to the start of the region) being dirtied.
881 * @size: size of the range being dirtied.
882 */
883 void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
884 hwaddr size);
885
886 /**
887 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty
888 * for a specified client. It clears them.
889 *
890 * Checks whether a range of bytes has been written to since the last
891 * call to memory_region_reset_dirty() with the same @client. Dirty logging
892 * must be enabled.
893 *
894 * @mr: the memory region being queried.
895 * @addr: the address (relative to the start of the region) being queried.
896 * @size: the size of the range being queried.
897 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
898 * %DIRTY_MEMORY_VGA.
899 */
900 bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr,
901 hwaddr size, unsigned client);
902 /**
903 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
904 * any external TLBs (e.g. kvm)
905 *
906 * Flushes dirty information from accelerators such as kvm and vhost-net
907 * and makes it available to users of the memory API.
908 *
909 * @mr: the region being flushed.
910 */
911 void memory_region_sync_dirty_bitmap(MemoryRegion *mr);
912
913 /**
914 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
915 * client.
916 *
917 * Marks a range of pages as no longer dirty.
918 *
919 * @mr: the region being updated.
920 * @addr: the start of the subrange being cleaned.
921 * @size: the size of the subrange being cleaned.
922 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
923 * %DIRTY_MEMORY_VGA.
924 */
925 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
926 hwaddr size, unsigned client);
927
928 /**
929 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
930 *
931 * Allows a memory region to be marked as read-only (turning it into a ROM).
932 * only useful on RAM regions.
933 *
934 * @mr: the region being updated.
935 * @readonly: whether rhe region is to be ROM or RAM.
936 */
937 void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
938
939 /**
940 * memory_region_rom_device_set_romd: enable/disable ROMD mode
941 *
942 * Allows a ROM device (initialized with memory_region_init_rom_device() to
943 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
944 * device is mapped to guest memory and satisfies read access directly.
945 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
946 * Writes are always handled by the #MemoryRegion.write function.
947 *
948 * @mr: the memory region to be updated
949 * @romd_mode: %true to put the region into ROMD mode
950 */
951 void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode);
952
953 /**
954 * memory_region_set_coalescing: Enable memory coalescing for the region.
955 *
956 * Enabled writes to a region to be queued for later processing. MMIO ->write
957 * callbacks may be delayed until a non-coalesced MMIO is issued.
958 * Only useful for IO regions. Roughly similar to write-combining hardware.
959 *
960 * @mr: the memory region to be write coalesced
961 */
962 void memory_region_set_coalescing(MemoryRegion *mr);
963
964 /**
965 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
966 * a region.
967 *
968 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
969 * Multiple calls can be issued coalesced disjoint ranges.
970 *
971 * @mr: the memory region to be updated.
972 * @offset: the start of the range within the region to be coalesced.
973 * @size: the size of the subrange to be coalesced.
974 */
975 void memory_region_add_coalescing(MemoryRegion *mr,
976 hwaddr offset,
977 uint64_t size);
978
979 /**
980 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
981 *
982 * Disables any coalescing caused by memory_region_set_coalescing() or
983 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
984 * hardware.
985 *
986 * @mr: the memory region to be updated.
987 */
988 void memory_region_clear_coalescing(MemoryRegion *mr);
989
990 /**
991 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
992 * accesses.
993 *
994 * Ensure that pending coalesced MMIO request are flushed before the memory
995 * region is accessed. This property is automatically enabled for all regions
996 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
997 *
998 * @mr: the memory region to be updated.
999 */
1000 void memory_region_set_flush_coalesced(MemoryRegion *mr);
1001
1002 /**
1003 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
1004 * accesses.
1005 *
1006 * Clear the automatic coalesced MMIO flushing enabled via
1007 * memory_region_set_flush_coalesced. Note that this service has no effect on
1008 * memory regions that have MMIO coalescing enabled for themselves. For them,
1009 * automatic flushing will stop once coalescing is disabled.
1010 *
1011 * @mr: the memory region to be updated.
1012 */
1013 void memory_region_clear_flush_coalesced(MemoryRegion *mr);
1014
1015 /**
1016 * memory_region_set_global_locking: Declares the access processing requires
1017 * QEMU's global lock.
1018 *
1019 * When this is invoked, accesses to the memory region will be processed while
1020 * holding the global lock of QEMU. This is the default behavior of memory
1021 * regions.
1022 *
1023 * @mr: the memory region to be updated.
1024 */
1025 void memory_region_set_global_locking(MemoryRegion *mr);
1026
1027 /**
1028 * memory_region_clear_global_locking: Declares that access processing does
1029 * not depend on the QEMU global lock.
1030 *
1031 * By clearing this property, accesses to the memory region will be processed
1032 * outside of QEMU's global lock (unless the lock is held on when issuing the
1033 * access request). In this case, the device model implementing the access
1034 * handlers is responsible for synchronization of concurrency.
1035 *
1036 * @mr: the memory region to be updated.
1037 */
1038 void memory_region_clear_global_locking(MemoryRegion *mr);
1039
1040 /**
1041 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
1042 * is written to a location.
1043 *
1044 * Marks a word in an IO region (initialized with memory_region_init_io())
1045 * as a trigger for an eventfd event. The I/O callback will not be called.
1046 * The caller must be prepared to handle failure (that is, take the required
1047 * action if the callback _is_ called).
1048 *
1049 * @mr: the memory region being updated.
1050 * @addr: the address within @mr that is to be monitored
1051 * @size: the size of the access to trigger the eventfd
1052 * @match_data: whether to match against @data, instead of just @addr
1053 * @data: the data to match against the guest write
1054 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
1055 **/
1056 void memory_region_add_eventfd(MemoryRegion *mr,
1057 hwaddr addr,
1058 unsigned size,
1059 bool match_data,
1060 uint64_t data,
1061 EventNotifier *e);
1062
1063 /**
1064 * memory_region_del_eventfd: Cancel an eventfd.
1065 *
1066 * Cancels an eventfd trigger requested by a previous
1067 * memory_region_add_eventfd() call.
1068 *
1069 * @mr: the memory region being updated.
1070 * @addr: the address within @mr that is to be monitored
1071 * @size: the size of the access to trigger the eventfd
1072 * @match_data: whether to match against @data, instead of just @addr
1073 * @data: the data to match against the guest write
1074 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
1075 */
1076 void memory_region_del_eventfd(MemoryRegion *mr,
1077 hwaddr addr,
1078 unsigned size,
1079 bool match_data,
1080 uint64_t data,
1081 EventNotifier *e);
1082
1083 /**
1084 * memory_region_add_subregion: Add a subregion to a container.
1085 *
1086 * Adds a subregion at @offset. The subregion may not overlap with other
1087 * subregions (except for those explicitly marked as overlapping). A region
1088 * may only be added once as a subregion (unless removed with
1089 * memory_region_del_subregion()); use memory_region_init_alias() if you
1090 * want a region to be a subregion in multiple locations.
1091 *
1092 * @mr: the region to contain the new subregion; must be a container
1093 * initialized with memory_region_init().
1094 * @offset: the offset relative to @mr where @subregion is added.
1095 * @subregion: the subregion to be added.
1096 */
1097 void memory_region_add_subregion(MemoryRegion *mr,
1098 hwaddr offset,
1099 MemoryRegion *subregion);
1100 /**
1101 * memory_region_add_subregion_overlap: Add a subregion to a container
1102 * with overlap.
1103 *
1104 * Adds a subregion at @offset. The subregion may overlap with other
1105 * subregions. Conflicts are resolved by having a higher @priority hide a
1106 * lower @priority. Subregions without priority are taken as @priority 0.
1107 * A region may only be added once as a subregion (unless removed with
1108 * memory_region_del_subregion()); use memory_region_init_alias() if you
1109 * want a region to be a subregion in multiple locations.
1110 *
1111 * @mr: the region to contain the new subregion; must be a container
1112 * initialized with memory_region_init().
1113 * @offset: the offset relative to @mr where @subregion is added.
1114 * @subregion: the subregion to be added.
1115 * @priority: used for resolving overlaps; highest priority wins.
1116 */
1117 void memory_region_add_subregion_overlap(MemoryRegion *mr,
1118 hwaddr offset,
1119 MemoryRegion *subregion,
1120 int priority);
1121
1122 /**
1123 * memory_region_get_ram_addr: Get the ram address associated with a memory
1124 * region
1125 */
1126 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr);
1127
1128 uint64_t memory_region_get_alignment(const MemoryRegion *mr);
1129 /**
1130 * memory_region_del_subregion: Remove a subregion.
1131 *
1132 * Removes a subregion from its container.
1133 *
1134 * @mr: the container to be updated.
1135 * @subregion: the region being removed; must be a current subregion of @mr.
1136 */
1137 void memory_region_del_subregion(MemoryRegion *mr,
1138 MemoryRegion *subregion);
1139
1140 /*
1141 * memory_region_set_enabled: dynamically enable or disable a region
1142 *
1143 * Enables or disables a memory region. A disabled memory region
1144 * ignores all accesses to itself and its subregions. It does not
1145 * obscure sibling subregions with lower priority - it simply behaves as
1146 * if it was removed from the hierarchy.
1147 *
1148 * Regions default to being enabled.
1149 *
1150 * @mr: the region to be updated
1151 * @enabled: whether to enable or disable the region
1152 */
1153 void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
1154
1155 /*
1156 * memory_region_set_address: dynamically update the address of a region
1157 *
1158 * Dynamically updates the address of a region, relative to its container.
1159 * May be used on regions are currently part of a memory hierarchy.
1160 *
1161 * @mr: the region to be updated
1162 * @addr: new address, relative to container region
1163 */
1164 void memory_region_set_address(MemoryRegion *mr, hwaddr addr);
1165
1166 /*
1167 * memory_region_set_size: dynamically update the size of a region.
1168 *
1169 * Dynamically updates the size of a region.
1170 *
1171 * @mr: the region to be updated
1172 * @size: used size of the region.
1173 */
1174 void memory_region_set_size(MemoryRegion *mr, uint64_t size);
1175
1176 /*
1177 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1178 *
1179 * Dynamically updates the offset into the target region that an alias points
1180 * to, as if the fourth argument to memory_region_init_alias() has changed.
1181 *
1182 * @mr: the #MemoryRegion to be updated; should be an alias.
1183 * @offset: the new offset into the target memory region
1184 */
1185 void memory_region_set_alias_offset(MemoryRegion *mr,
1186 hwaddr offset);
1187
1188 /**
1189 * memory_region_present: checks if an address relative to a @container
1190 * translates into #MemoryRegion within @container
1191 *
1192 * Answer whether a #MemoryRegion within @container covers the address
1193 * @addr.
1194 *
1195 * @container: a #MemoryRegion within which @addr is a relative address
1196 * @addr: the area within @container to be searched
1197 */
1198 bool memory_region_present(MemoryRegion *container, hwaddr addr);
1199
1200 /**
1201 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1202 * into any address space.
1203 *
1204 * @mr: a #MemoryRegion which should be checked if it's mapped
1205 */
1206 bool memory_region_is_mapped(MemoryRegion *mr);
1207
1208 /**
1209 * memory_region_find: translate an address/size relative to a
1210 * MemoryRegion into a #MemoryRegionSection.
1211 *
1212 * Locates the first #MemoryRegion within @mr that overlaps the range
1213 * given by @addr and @size.
1214 *
1215 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1216 * It will have the following characteristics:
1217 * .@size = 0 iff no overlap was found
1218 * .@mr is non-%NULL iff an overlap was found
1219 *
1220 * Remember that in the return value the @offset_within_region is
1221 * relative to the returned region (in the .@mr field), not to the
1222 * @mr argument.
1223 *
1224 * Similarly, the .@offset_within_address_space is relative to the
1225 * address space that contains both regions, the passed and the
1226 * returned one. However, in the special case where the @mr argument
1227 * has no container (and thus is the root of the address space), the
1228 * following will hold:
1229 * .@offset_within_address_space >= @addr
1230 * .@offset_within_address_space + .@size <= @addr + @size
1231 *
1232 * @mr: a MemoryRegion within which @addr is a relative address
1233 * @addr: start of the area within @as to be searched
1234 * @size: size of the area to be searched
1235 */
1236 MemoryRegionSection memory_region_find(MemoryRegion *mr,
1237 hwaddr addr, uint64_t size);
1238
1239 /**
1240 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
1241 *
1242 * Synchronizes the dirty page log for all address spaces.
1243 */
1244 void memory_global_dirty_log_sync(void);
1245
1246 /**
1247 * memory_region_transaction_begin: Start a transaction.
1248 *
1249 * During a transaction, changes will be accumulated and made visible
1250 * only when the transaction ends (is committed).
1251 */
1252 void memory_region_transaction_begin(void);
1253
1254 /**
1255 * memory_region_transaction_commit: Commit a transaction and make changes
1256 * visible to the guest.
1257 */
1258 void memory_region_transaction_commit(void);
1259
1260 /**
1261 * memory_listener_register: register callbacks to be called when memory
1262 * sections are mapped or unmapped into an address
1263 * space
1264 *
1265 * @listener: an object containing the callbacks to be called
1266 * @filter: if non-%NULL, only regions in this address space will be observed
1267 */
1268 void memory_listener_register(MemoryListener *listener, AddressSpace *filter);
1269
1270 /**
1271 * memory_listener_unregister: undo the effect of memory_listener_register()
1272 *
1273 * @listener: an object containing the callbacks to be removed
1274 */
1275 void memory_listener_unregister(MemoryListener *listener);
1276
1277 /**
1278 * memory_global_dirty_log_start: begin dirty logging for all regions
1279 */
1280 void memory_global_dirty_log_start(void);
1281
1282 /**
1283 * memory_global_dirty_log_stop: end dirty logging for all regions
1284 */
1285 void memory_global_dirty_log_stop(void);
1286
1287 void mtree_info(fprintf_function mon_printf, void *f, bool flatview);
1288
1289 /**
1290 * memory_region_dispatch_read: perform a read directly to the specified
1291 * MemoryRegion.
1292 *
1293 * @mr: #MemoryRegion to access
1294 * @addr: address within that region
1295 * @pval: pointer to uint64_t which the data is written to
1296 * @size: size of the access in bytes
1297 * @attrs: memory transaction attributes to use for the access
1298 */
1299 MemTxResult memory_region_dispatch_read(MemoryRegion *mr,
1300 hwaddr addr,
1301 uint64_t *pval,
1302 unsigned size,
1303 MemTxAttrs attrs);
1304 /**
1305 * memory_region_dispatch_write: perform a write directly to the specified
1306 * MemoryRegion.
1307 *
1308 * @mr: #MemoryRegion to access
1309 * @addr: address within that region
1310 * @data: data to write
1311 * @size: size of the access in bytes
1312 * @attrs: memory transaction attributes to use for the access
1313 */
1314 MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
1315 hwaddr addr,
1316 uint64_t data,
1317 unsigned size,
1318 MemTxAttrs attrs);
1319
1320 /**
1321 * address_space_init: initializes an address space
1322 *
1323 * @as: an uninitialized #AddressSpace
1324 * @root: a #MemoryRegion that routes addresses for the address space
1325 * @name: an address space name. The name is only used for debugging
1326 * output.
1327 */
1328 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
1329
1330 /**
1331 * address_space_init_shareable: return an address space for a memory region,
1332 * creating it if it does not already exist
1333 *
1334 * @root: a #MemoryRegion that routes addresses for the address space
1335 * @name: an address space name. The name is only used for debugging
1336 * output.
1337 *
1338 * This function will return a pointer to an existing AddressSpace
1339 * which was initialized with the specified MemoryRegion, or it will
1340 * create and initialize one if it does not already exist. The ASes
1341 * are reference-counted, so the memory will be freed automatically
1342 * when the AddressSpace is destroyed via address_space_destroy.
1343 */
1344 AddressSpace *address_space_init_shareable(MemoryRegion *root,
1345 const char *name);
1346
1347 /**
1348 * address_space_destroy: destroy an address space
1349 *
1350 * Releases all resources associated with an address space. After an address space
1351 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1352 * as well.
1353 *
1354 * @as: address space to be destroyed
1355 */
1356 void address_space_destroy(AddressSpace *as);
1357
1358 /**
1359 * address_space_rw: read from or write to an address space.
1360 *
1361 * Return a MemTxResult indicating whether the operation succeeded
1362 * or failed (eg unassigned memory, device rejected the transaction,
1363 * IOMMU fault).
1364 *
1365 * @as: #AddressSpace to be accessed
1366 * @addr: address within that address space
1367 * @attrs: memory transaction attributes
1368 * @buf: buffer with the data transferred
1369 * @is_write: indicates the transfer direction
1370 */
1371 MemTxResult address_space_rw(AddressSpace *as, hwaddr addr,
1372 MemTxAttrs attrs, uint8_t *buf,
1373 int len, bool is_write);
1374
1375 /**
1376 * address_space_write: write to address space.
1377 *
1378 * Return a MemTxResult indicating whether the operation succeeded
1379 * or failed (eg unassigned memory, device rejected the transaction,
1380 * IOMMU fault).
1381 *
1382 * @as: #AddressSpace to be accessed
1383 * @addr: address within that address space
1384 * @attrs: memory transaction attributes
1385 * @buf: buffer with the data transferred
1386 */
1387 MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
1388 MemTxAttrs attrs,
1389 const uint8_t *buf, int len);
1390
1391 /* address_space_ld*: load from an address space
1392 * address_space_st*: store to an address space
1393 *
1394 * These functions perform a load or store of the byte, word,
1395 * longword or quad to the specified address within the AddressSpace.
1396 * The _le suffixed functions treat the data as little endian;
1397 * _be indicates big endian; no suffix indicates "same endianness
1398 * as guest CPU".
1399 *
1400 * The "guest CPU endianness" accessors are deprecated for use outside
1401 * target-* code; devices should be CPU-agnostic and use either the LE
1402 * or the BE accessors.
1403 *
1404 * @as #AddressSpace to be accessed
1405 * @addr: address within that address space
1406 * @val: data value, for stores
1407 * @attrs: memory transaction attributes
1408 * @result: location to write the success/failure of the transaction;
1409 * if NULL, this information is discarded
1410 */
1411 uint32_t address_space_ldub(AddressSpace *as, hwaddr addr,
1412 MemTxAttrs attrs, MemTxResult *result);
1413 uint32_t address_space_lduw_le(AddressSpace *as, hwaddr addr,
1414 MemTxAttrs attrs, MemTxResult *result);
1415 uint32_t address_space_lduw_be(AddressSpace *as, hwaddr addr,
1416 MemTxAttrs attrs, MemTxResult *result);
1417 uint32_t address_space_ldl_le(AddressSpace *as, hwaddr addr,
1418 MemTxAttrs attrs, MemTxResult *result);
1419 uint32_t address_space_ldl_be(AddressSpace *as, hwaddr addr,
1420 MemTxAttrs attrs, MemTxResult *result);
1421 uint64_t address_space_ldq_le(AddressSpace *as, hwaddr addr,
1422 MemTxAttrs attrs, MemTxResult *result);
1423 uint64_t address_space_ldq_be(AddressSpace *as, hwaddr addr,
1424 MemTxAttrs attrs, MemTxResult *result);
1425 void address_space_stb(AddressSpace *as, hwaddr addr, uint32_t val,
1426 MemTxAttrs attrs, MemTxResult *result);
1427 void address_space_stw_le(AddressSpace *as, hwaddr addr, uint32_t val,
1428 MemTxAttrs attrs, MemTxResult *result);
1429 void address_space_stw_be(AddressSpace *as, hwaddr addr, uint32_t val,
1430 MemTxAttrs attrs, MemTxResult *result);
1431 void address_space_stl_le(AddressSpace *as, hwaddr addr, uint32_t val,
1432 MemTxAttrs attrs, MemTxResult *result);
1433 void address_space_stl_be(AddressSpace *as, hwaddr addr, uint32_t val,
1434 MemTxAttrs attrs, MemTxResult *result);
1435 void address_space_stq_le(AddressSpace *as, hwaddr addr, uint64_t val,
1436 MemTxAttrs attrs, MemTxResult *result);
1437 void address_space_stq_be(AddressSpace *as, hwaddr addr, uint64_t val,
1438 MemTxAttrs attrs, MemTxResult *result);
1439
1440 uint32_t ldub_phys(AddressSpace *as, hwaddr addr);
1441 uint32_t lduw_le_phys(AddressSpace *as, hwaddr addr);
1442 uint32_t lduw_be_phys(AddressSpace *as, hwaddr addr);
1443 uint32_t ldl_le_phys(AddressSpace *as, hwaddr addr);
1444 uint32_t ldl_be_phys(AddressSpace *as, hwaddr addr);
1445 uint64_t ldq_le_phys(AddressSpace *as, hwaddr addr);
1446 uint64_t ldq_be_phys(AddressSpace *as, hwaddr addr);
1447 void stb_phys(AddressSpace *as, hwaddr addr, uint32_t val);
1448 void stw_le_phys(AddressSpace *as, hwaddr addr, uint32_t val);
1449 void stw_be_phys(AddressSpace *as, hwaddr addr, uint32_t val);
1450 void stl_le_phys(AddressSpace *as, hwaddr addr, uint32_t val);
1451 void stl_be_phys(AddressSpace *as, hwaddr addr, uint32_t val);
1452 void stq_le_phys(AddressSpace *as, hwaddr addr, uint64_t val);
1453 void stq_be_phys(AddressSpace *as, hwaddr addr, uint64_t val);
1454
1455 struct MemoryRegionCache {
1456 hwaddr xlat;
1457 hwaddr len;
1458 AddressSpace *as;
1459 };
1460
1461 #define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .as = NULL })
1462
1463 /* address_space_cache_init: prepare for repeated access to a physical
1464 * memory region
1465 *
1466 * @cache: #MemoryRegionCache to be filled
1467 * @as: #AddressSpace to be accessed
1468 * @addr: address within that address space
1469 * @len: length of buffer
1470 * @is_write: indicates the transfer direction
1471 *
1472 * Will only work with RAM, and may map a subset of the requested range by
1473 * returning a value that is less than @len. On failure, return a negative
1474 * errno value.
1475 *
1476 * Because it only works with RAM, this function can be used for
1477 * read-modify-write operations. In this case, is_write should be %true.
1478 *
1479 * Note that addresses passed to the address_space_*_cached functions
1480 * are relative to @addr.
1481 */
1482 int64_t address_space_cache_init(MemoryRegionCache *cache,
1483 AddressSpace *as,
1484 hwaddr addr,
1485 hwaddr len,
1486 bool is_write);
1487
1488 /**
1489 * address_space_cache_invalidate: complete a write to a #MemoryRegionCache
1490 *
1491 * @cache: The #MemoryRegionCache to operate on.
1492 * @addr: The first physical address that was written, relative to the
1493 * address that was passed to @address_space_cache_init.
1494 * @access_len: The number of bytes that were written starting at @addr.
1495 */
1496 void address_space_cache_invalidate(MemoryRegionCache *cache,
1497 hwaddr addr,
1498 hwaddr access_len);
1499
1500 /**
1501 * address_space_cache_destroy: free a #MemoryRegionCache
1502 *
1503 * @cache: The #MemoryRegionCache whose memory should be released.
1504 */
1505 void address_space_cache_destroy(MemoryRegionCache *cache);
1506
1507 /* address_space_ld*_cached: load from a cached #MemoryRegion
1508 * address_space_st*_cached: store into a cached #MemoryRegion
1509 *
1510 * These functions perform a load or store of the byte, word,
1511 * longword or quad to the specified address. The address is
1512 * a physical address in the AddressSpace, but it must lie within
1513 * a #MemoryRegion that was mapped with address_space_cache_init.
1514 *
1515 * The _le suffixed functions treat the data as little endian;
1516 * _be indicates big endian; no suffix indicates "same endianness
1517 * as guest CPU".
1518 *
1519 * The "guest CPU endianness" accessors are deprecated for use outside
1520 * target-* code; devices should be CPU-agnostic and use either the LE
1521 * or the BE accessors.
1522 *
1523 * @cache: previously initialized #MemoryRegionCache to be accessed
1524 * @addr: address within the address space
1525 * @val: data value, for stores
1526 * @attrs: memory transaction attributes
1527 * @result: location to write the success/failure of the transaction;
1528 * if NULL, this information is discarded
1529 */
1530 uint32_t address_space_ldub_cached(MemoryRegionCache *cache, hwaddr addr,
1531 MemTxAttrs attrs, MemTxResult *result);
1532 uint32_t address_space_lduw_le_cached(MemoryRegionCache *cache, hwaddr addr,
1533 MemTxAttrs attrs, MemTxResult *result);
1534 uint32_t address_space_lduw_be_cached(MemoryRegionCache *cache, hwaddr addr,
1535 MemTxAttrs attrs, MemTxResult *result);
1536 uint32_t address_space_ldl_le_cached(MemoryRegionCache *cache, hwaddr addr,
1537 MemTxAttrs attrs, MemTxResult *result);
1538 uint32_t address_space_ldl_be_cached(MemoryRegionCache *cache, hwaddr addr,
1539 MemTxAttrs attrs, MemTxResult *result);
1540 uint64_t address_space_ldq_le_cached(MemoryRegionCache *cache, hwaddr addr,
1541 MemTxAttrs attrs, MemTxResult *result);
1542 uint64_t address_space_ldq_be_cached(MemoryRegionCache *cache, hwaddr addr,
1543 MemTxAttrs attrs, MemTxResult *result);
1544 void address_space_stb_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val,
1545 MemTxAttrs attrs, MemTxResult *result);
1546 void address_space_stw_le_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val,
1547 MemTxAttrs attrs, MemTxResult *result);
1548 void address_space_stw_be_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val,
1549 MemTxAttrs attrs, MemTxResult *result);
1550 void address_space_stl_le_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val,
1551 MemTxAttrs attrs, MemTxResult *result);
1552 void address_space_stl_be_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val,
1553 MemTxAttrs attrs, MemTxResult *result);
1554 void address_space_stq_le_cached(MemoryRegionCache *cache, hwaddr addr, uint64_t val,
1555 MemTxAttrs attrs, MemTxResult *result);
1556 void address_space_stq_be_cached(MemoryRegionCache *cache, hwaddr addr, uint64_t val,
1557 MemTxAttrs attrs, MemTxResult *result);
1558
1559 uint32_t ldub_phys_cached(MemoryRegionCache *cache, hwaddr addr);
1560 uint32_t lduw_le_phys_cached(MemoryRegionCache *cache, hwaddr addr);
1561 uint32_t lduw_be_phys_cached(MemoryRegionCache *cache, hwaddr addr);
1562 uint32_t ldl_le_phys_cached(MemoryRegionCache *cache, hwaddr addr);
1563 uint32_t ldl_be_phys_cached(MemoryRegionCache *cache, hwaddr addr);
1564 uint64_t ldq_le_phys_cached(MemoryRegionCache *cache, hwaddr addr);
1565 uint64_t ldq_be_phys_cached(MemoryRegionCache *cache, hwaddr addr);
1566 void stb_phys_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val);
1567 void stw_le_phys_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val);
1568 void stw_be_phys_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val);
1569 void stl_le_phys_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val);
1570 void stl_be_phys_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val);
1571 void stq_le_phys_cached(MemoryRegionCache *cache, hwaddr addr, uint64_t val);
1572 void stq_be_phys_cached(MemoryRegionCache *cache, hwaddr addr, uint64_t val);
1573 /* address_space_get_iotlb_entry: translate an address into an IOTLB
1574 * entry. Should be called from an RCU critical section.
1575 */
1576 IOMMUTLBEntry address_space_get_iotlb_entry(AddressSpace *as, hwaddr addr,
1577 bool is_write);
1578
1579 /* address_space_translate: translate an address range into an address space
1580 * into a MemoryRegion and an address range into that section. Should be
1581 * called from an RCU critical section, to avoid that the last reference
1582 * to the returned region disappears after address_space_translate returns.
1583 *
1584 * @as: #AddressSpace to be accessed
1585 * @addr: address within that address space
1586 * @xlat: pointer to address within the returned memory region section's
1587 * #MemoryRegion.
1588 * @len: pointer to length
1589 * @is_write: indicates the transfer direction
1590 */
1591 MemoryRegion *address_space_translate(AddressSpace *as, hwaddr addr,
1592 hwaddr *xlat, hwaddr *len,
1593 bool is_write);
1594
1595 /* address_space_access_valid: check for validity of accessing an address
1596 * space range
1597 *
1598 * Check whether memory is assigned to the given address space range, and
1599 * access is permitted by any IOMMU regions that are active for the address
1600 * space.
1601 *
1602 * For now, addr and len should be aligned to a page size. This limitation
1603 * will be lifted in the future.
1604 *
1605 * @as: #AddressSpace to be accessed
1606 * @addr: address within that address space
1607 * @len: length of the area to be checked
1608 * @is_write: indicates the transfer direction
1609 */
1610 bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write);
1611
1612 /* address_space_map: map a physical memory region into a host virtual address
1613 *
1614 * May map a subset of the requested range, given by and returned in @plen.
1615 * May return %NULL if resources needed to perform the mapping are exhausted.
1616 * Use only for reads OR writes - not for read-modify-write operations.
1617 * Use cpu_register_map_client() to know when retrying the map operation is
1618 * likely to succeed.
1619 *
1620 * @as: #AddressSpace to be accessed
1621 * @addr: address within that address space
1622 * @plen: pointer to length of buffer; updated on return
1623 * @is_write: indicates the transfer direction
1624 */
1625 void *address_space_map(AddressSpace *as, hwaddr addr,
1626 hwaddr *plen, bool is_write);
1627
1628 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1629 *
1630 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1631 * the amount of memory that was actually read or written by the caller.
1632 *
1633 * @as: #AddressSpace used
1634 * @addr: address within that address space
1635 * @len: buffer length as returned by address_space_map()
1636 * @access_len: amount of data actually transferred
1637 * @is_write: indicates the transfer direction
1638 */
1639 void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
1640 int is_write, hwaddr access_len);
1641
1642
1643 /* Internal functions, part of the implementation of address_space_read. */
1644 MemTxResult address_space_read_continue(AddressSpace *as, hwaddr addr,
1645 MemTxAttrs attrs, uint8_t *buf,
1646 int len, hwaddr addr1, hwaddr l,
1647 MemoryRegion *mr);
1648 MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr,
1649 MemTxAttrs attrs, uint8_t *buf, int len);
1650 void *qemu_map_ram_ptr(RAMBlock *ram_block, ram_addr_t addr);
1651
1652 static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
1653 {
1654 if (is_write) {
1655 return memory_region_is_ram(mr) &&
1656 !mr->readonly && !memory_region_is_ram_device(mr);
1657 } else {
1658 return (memory_region_is_ram(mr) && !memory_region_is_ram_device(mr)) ||
1659 memory_region_is_romd(mr);
1660 }
1661 }
1662
1663 /**
1664 * address_space_read: read from an address space.
1665 *
1666 * Return a MemTxResult indicating whether the operation succeeded
1667 * or failed (eg unassigned memory, device rejected the transaction,
1668 * IOMMU fault).
1669 *
1670 * @as: #AddressSpace to be accessed
1671 * @addr: address within that address space
1672 * @attrs: memory transaction attributes
1673 * @buf: buffer with the data transferred
1674 */
1675 static inline __attribute__((__always_inline__))
1676 MemTxResult address_space_read(AddressSpace *as, hwaddr addr, MemTxAttrs attrs,
1677 uint8_t *buf, int len)
1678 {
1679 MemTxResult result = MEMTX_OK;
1680 hwaddr l, addr1;
1681 void *ptr;
1682 MemoryRegion *mr;
1683
1684 if (__builtin_constant_p(len)) {
1685 if (len) {
1686 rcu_read_lock();
1687 l = len;
1688 mr = address_space_translate(as, addr, &addr1, &l, false);
1689 if (len == l && memory_access_is_direct(mr, false)) {
1690 ptr = qemu_map_ram_ptr(mr->ram_block, addr1);
1691 memcpy(buf, ptr, len);
1692 } else {
1693 result = address_space_read_continue(as, addr, attrs, buf, len,
1694 addr1, l, mr);
1695 }
1696 rcu_read_unlock();
1697 }
1698 } else {
1699 result = address_space_read_full(as, addr, attrs, buf, len);
1700 }
1701 return result;
1702 }
1703
1704 /**
1705 * address_space_read_cached: read from a cached RAM region
1706 *
1707 * @cache: Cached region to be addressed
1708 * @addr: address relative to the base of the RAM region
1709 * @buf: buffer with the data transferred
1710 * @len: length of the data transferred
1711 */
1712 static inline void
1713 address_space_read_cached(MemoryRegionCache *cache, hwaddr addr,
1714 void *buf, int len)
1715 {
1716 assert(addr < cache->len && len <= cache->len - addr);
1717 address_space_read(cache->as, cache->xlat + addr, MEMTXATTRS_UNSPECIFIED, buf, len);
1718 }
1719
1720 /**
1721 * address_space_write_cached: write to a cached RAM region
1722 *
1723 * @cache: Cached region to be addressed
1724 * @addr: address relative to the base of the RAM region
1725 * @buf: buffer with the data transferred
1726 * @len: length of the data transferred
1727 */
1728 static inline void
1729 address_space_write_cached(MemoryRegionCache *cache, hwaddr addr,
1730 void *buf, int len)
1731 {
1732 assert(addr < cache->len && len <= cache->len - addr);
1733 address_space_write(cache->as, cache->xlat + addr, MEMTXATTRS_UNSPECIFIED, buf, len);
1734 }
1735
1736 #endif
1737
1738 #endif