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