cpu: move endian-dependent load/store functions to cpu-all.h
[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 #define DIRTY_MEMORY_VGA 0
20 #define DIRTY_MEMORY_CODE 1
21 #define DIRTY_MEMORY_MIGRATION 2
22 #define DIRTY_MEMORY_NUM 3 /* num of dirty bits */
23
24 #include "exec/cpu-common.h"
25 #ifndef CONFIG_USER_ONLY
26 #include "exec/hwaddr.h"
27 #endif
28 #include "exec/memattrs.h"
29 #include "qemu/queue.h"
30 #include "qemu/int128.h"
31 #include "qemu/notify.h"
32 #include "qom/object.h"
33 #include "qemu/rcu.h"
34
35 #define MAX_PHYS_ADDR_SPACE_BITS 62
36 #define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
37
38 #define TYPE_MEMORY_REGION "qemu:memory-region"
39 #define MEMORY_REGION(obj) \
40 OBJECT_CHECK(MemoryRegion, (obj), TYPE_MEMORY_REGION)
41
42 typedef struct MemoryRegionOps MemoryRegionOps;
43 typedef struct MemoryRegionMmio MemoryRegionMmio;
44
45 struct MemoryRegionMmio {
46 CPUReadMemoryFunc *read[3];
47 CPUWriteMemoryFunc *write[3];
48 };
49
50 typedef struct IOMMUTLBEntry IOMMUTLBEntry;
51
52 /* See address_space_translate: bit 0 is read, bit 1 is write. */
53 typedef enum {
54 IOMMU_NONE = 0,
55 IOMMU_RO = 1,
56 IOMMU_WO = 2,
57 IOMMU_RW = 3,
58 } IOMMUAccessFlags;
59
60 struct IOMMUTLBEntry {
61 AddressSpace *target_as;
62 hwaddr iova;
63 hwaddr translated_addr;
64 hwaddr addr_mask; /* 0xfff = 4k translation */
65 IOMMUAccessFlags perm;
66 };
67
68 /* New-style MMIO accessors can indicate that the transaction failed.
69 * A zero (MEMTX_OK) response means success; anything else is a failure
70 * of some kind. The memory subsystem will bitwise-OR together results
71 * if it is synthesizing an operation from multiple smaller accesses.
72 */
73 #define MEMTX_OK 0
74 #define MEMTX_ERROR (1U << 0) /* device returned an error */
75 #define MEMTX_DECODE_ERROR (1U << 1) /* nothing at that address */
76 typedef uint32_t MemTxResult;
77
78 /*
79 * Memory region callbacks
80 */
81 struct MemoryRegionOps {
82 /* Read from the memory region. @addr is relative to @mr; @size is
83 * in bytes. */
84 uint64_t (*read)(void *opaque,
85 hwaddr addr,
86 unsigned size);
87 /* Write to the memory region. @addr is relative to @mr; @size is
88 * in bytes. */
89 void (*write)(void *opaque,
90 hwaddr addr,
91 uint64_t data,
92 unsigned size);
93
94 MemTxResult (*read_with_attrs)(void *opaque,
95 hwaddr addr,
96 uint64_t *data,
97 unsigned size,
98 MemTxAttrs attrs);
99 MemTxResult (*write_with_attrs)(void *opaque,
100 hwaddr addr,
101 uint64_t data,
102 unsigned size,
103 MemTxAttrs attrs);
104
105 enum device_endian endianness;
106 /* Guest-visible constraints: */
107 struct {
108 /* If nonzero, specify bounds on access sizes beyond which a machine
109 * check is thrown.
110 */
111 unsigned min_access_size;
112 unsigned max_access_size;
113 /* If true, unaligned accesses are supported. Otherwise unaligned
114 * accesses throw machine checks.
115 */
116 bool unaligned;
117 /*
118 * If present, and returns #false, the transaction is not accepted
119 * by the device (and results in machine dependent behaviour such
120 * as a machine check exception).
121 */
122 bool (*accepts)(void *opaque, hwaddr addr,
123 unsigned size, bool is_write);
124 } valid;
125 /* Internal implementation constraints: */
126 struct {
127 /* If nonzero, specifies the minimum size implemented. Smaller sizes
128 * will be rounded upwards and a partial result will be returned.
129 */
130 unsigned min_access_size;
131 /* If nonzero, specifies the maximum size implemented. Larger sizes
132 * will be done as a series of accesses with smaller sizes.
133 */
134 unsigned max_access_size;
135 /* If true, unaligned accesses are supported. Otherwise all accesses
136 * are converted to (possibly multiple) naturally aligned accesses.
137 */
138 bool unaligned;
139 } impl;
140
141 /* If .read and .write are not present, old_mmio may be used for
142 * backwards compatibility with old mmio registration
143 */
144 const MemoryRegionMmio old_mmio;
145 };
146
147 typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps;
148
149 struct MemoryRegionIOMMUOps {
150 /* Return a TLB entry that contains a given address. */
151 IOMMUTLBEntry (*translate)(MemoryRegion *iommu, hwaddr addr, bool is_write);
152 };
153
154 typedef struct CoalescedMemoryRange CoalescedMemoryRange;
155 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
156
157 struct MemoryRegion {
158 Object parent_obj;
159
160 /* All fields are private - violators will be prosecuted */
161
162 /* The following fields should fit in a cache line */
163 bool romd_mode;
164 bool ram;
165 bool subpage;
166 bool readonly; /* For RAM regions */
167 bool rom_device;
168 bool flush_coalesced_mmio;
169 bool global_locking;
170 uint8_t dirty_log_mask;
171 RAMBlock *ram_block;
172 Object *owner;
173 const MemoryRegionIOMMUOps *iommu_ops;
174
175 const MemoryRegionOps *ops;
176 void *opaque;
177 MemoryRegion *container;
178 Int128 size;
179 hwaddr addr;
180 void (*destructor)(MemoryRegion *mr);
181 uint64_t align;
182 bool terminates;
183 bool skip_dump;
184 bool enabled;
185 bool warning_printed; /* For reservations */
186 uint8_t vga_logging_count;
187 MemoryRegion *alias;
188 hwaddr alias_offset;
189 int32_t priority;
190 bool may_overlap;
191 QTAILQ_HEAD(subregions, MemoryRegion) subregions;
192 QTAILQ_ENTRY(MemoryRegion) subregions_link;
193 QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced;
194 const char *name;
195 unsigned ioeventfd_nb;
196 MemoryRegionIoeventfd *ioeventfds;
197 NotifierList iommu_notify;
198 };
199
200 /**
201 * MemoryListener: callbacks structure for updates to the physical memory map
202 *
203 * Allows a component to adjust to changes in the guest-visible memory map.
204 * Use with memory_listener_register() and memory_listener_unregister().
205 */
206 struct MemoryListener {
207 void (*begin)(MemoryListener *listener);
208 void (*commit)(MemoryListener *listener);
209 void (*region_add)(MemoryListener *listener, MemoryRegionSection *section);
210 void (*region_del)(MemoryListener *listener, MemoryRegionSection *section);
211 void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section);
212 void (*log_start)(MemoryListener *listener, MemoryRegionSection *section,
213 int old, int new);
214 void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section,
215 int old, int new);
216 void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section);
217 void (*log_global_start)(MemoryListener *listener);
218 void (*log_global_stop)(MemoryListener *listener);
219 void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section,
220 bool match_data, uint64_t data, EventNotifier *e);
221 void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section,
222 bool match_data, uint64_t data, EventNotifier *e);
223 void (*coalesced_mmio_add)(MemoryListener *listener, MemoryRegionSection *section,
224 hwaddr addr, hwaddr len);
225 void (*coalesced_mmio_del)(MemoryListener *listener, MemoryRegionSection *section,
226 hwaddr addr, hwaddr len);
227 /* Lower = earlier (during add), later (during del) */
228 unsigned priority;
229 AddressSpace *address_space_filter;
230 QTAILQ_ENTRY(MemoryListener) link;
231 };
232
233 /**
234 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
235 */
236 struct AddressSpace {
237 /* All fields are private. */
238 struct rcu_head rcu;
239 char *name;
240 MemoryRegion *root;
241 int ref_count;
242 bool malloced;
243
244 /* Accessed via RCU. */
245 struct FlatView *current_map;
246
247 int ioeventfd_nb;
248 struct MemoryRegionIoeventfd *ioeventfds;
249 struct AddressSpaceDispatch *dispatch;
250 struct AddressSpaceDispatch *next_dispatch;
251 MemoryListener dispatch_listener;
252
253 QTAILQ_ENTRY(AddressSpace) address_spaces_link;
254 };
255
256 /**
257 * MemoryRegionSection: describes a fragment of a #MemoryRegion
258 *
259 * @mr: the region, or %NULL if empty
260 * @address_space: the address space the region is mapped in
261 * @offset_within_region: the beginning of the section, relative to @mr's start
262 * @size: the size of the section; will not exceed @mr's boundaries
263 * @offset_within_address_space: the address of the first byte of the section
264 * relative to the region's address space
265 * @readonly: writes to this section are ignored
266 */
267 struct MemoryRegionSection {
268 MemoryRegion *mr;
269 AddressSpace *address_space;
270 hwaddr offset_within_region;
271 Int128 size;
272 hwaddr offset_within_address_space;
273 bool readonly;
274 };
275
276 /**
277 * memory_region_init: Initialize a memory region
278 *
279 * The region typically acts as a container for other memory regions. Use
280 * memory_region_add_subregion() to add subregions.
281 *
282 * @mr: the #MemoryRegion to be initialized
283 * @owner: the object that tracks the region's reference count
284 * @name: used for debugging; not visible to the user or ABI
285 * @size: size of the region; any subregions beyond this size will be clipped
286 */
287 void memory_region_init(MemoryRegion *mr,
288 struct Object *owner,
289 const char *name,
290 uint64_t size);
291
292 /**
293 * memory_region_ref: Add 1 to a memory region's reference count
294 *
295 * Whenever memory regions are accessed outside the BQL, they need to be
296 * preserved against hot-unplug. MemoryRegions actually do not have their
297 * own reference count; they piggyback on a QOM object, their "owner".
298 * This function adds a reference to the owner.
299 *
300 * All MemoryRegions must have an owner if they can disappear, even if the
301 * device they belong to operates exclusively under the BQL. This is because
302 * the region could be returned at any time by memory_region_find, and this
303 * is usually under guest control.
304 *
305 * @mr: the #MemoryRegion
306 */
307 void memory_region_ref(MemoryRegion *mr);
308
309 /**
310 * memory_region_unref: Remove 1 to a memory region's reference count
311 *
312 * Whenever memory regions are accessed outside the BQL, they need to be
313 * preserved against hot-unplug. MemoryRegions actually do not have their
314 * own reference count; they piggyback on a QOM object, their "owner".
315 * This function removes a reference to the owner and possibly destroys it.
316 *
317 * @mr: the #MemoryRegion
318 */
319 void memory_region_unref(MemoryRegion *mr);
320
321 /**
322 * memory_region_init_io: Initialize an I/O memory region.
323 *
324 * Accesses into the region will cause the callbacks in @ops to be called.
325 * if @size is nonzero, subregions will be clipped to @size.
326 *
327 * @mr: the #MemoryRegion to be initialized.
328 * @owner: the object that tracks the region's reference count
329 * @ops: a structure containing read and write callbacks to be used when
330 * I/O is performed on the region.
331 * @opaque: passed to the read and write callbacks of the @ops structure.
332 * @name: used for debugging; not visible to the user or ABI
333 * @size: size of the region.
334 */
335 void memory_region_init_io(MemoryRegion *mr,
336 struct Object *owner,
337 const MemoryRegionOps *ops,
338 void *opaque,
339 const char *name,
340 uint64_t size);
341
342 /**
343 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
344 * region will modify memory directly.
345 *
346 * @mr: the #MemoryRegion to be initialized.
347 * @owner: the object that tracks the region's reference count
348 * @name: the name of the region.
349 * @size: size of the region.
350 * @errp: pointer to Error*, to store an error if it happens.
351 */
352 void memory_region_init_ram(MemoryRegion *mr,
353 struct Object *owner,
354 const char *name,
355 uint64_t size,
356 Error **errp);
357
358 /**
359 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
360 * RAM. Accesses into the region will
361 * modify memory directly. Only an initial
362 * portion of this RAM is actually used.
363 * The used size can change across reboots.
364 *
365 * @mr: the #MemoryRegion to be initialized.
366 * @owner: the object that tracks the region's reference count
367 * @name: the name of the region.
368 * @size: used size of the region.
369 * @max_size: max size of the region.
370 * @resized: callback to notify owner about used size change.
371 * @errp: pointer to Error*, to store an error if it happens.
372 */
373 void memory_region_init_resizeable_ram(MemoryRegion *mr,
374 struct Object *owner,
375 const char *name,
376 uint64_t size,
377 uint64_t max_size,
378 void (*resized)(const char*,
379 uint64_t length,
380 void *host),
381 Error **errp);
382 #ifdef __linux__
383 /**
384 * memory_region_init_ram_from_file: Initialize RAM memory region with a
385 * mmap-ed backend.
386 *
387 * @mr: the #MemoryRegion to be initialized.
388 * @owner: the object that tracks the region's reference count
389 * @name: the name of the region.
390 * @size: size of the region.
391 * @share: %true if memory must be mmaped with the MAP_SHARED flag
392 * @path: the path in which to allocate the RAM.
393 * @errp: pointer to Error*, to store an error if it happens.
394 */
395 void memory_region_init_ram_from_file(MemoryRegion *mr,
396 struct Object *owner,
397 const char *name,
398 uint64_t size,
399 bool share,
400 const char *path,
401 Error **errp);
402 #endif
403
404 /**
405 * memory_region_init_ram_ptr: Initialize RAM memory region from a
406 * user-provided pointer. Accesses into the
407 * region will modify memory directly.
408 *
409 * @mr: the #MemoryRegion to be initialized.
410 * @owner: the object that tracks the region's reference count
411 * @name: the name of the region.
412 * @size: size of the region.
413 * @ptr: memory to be mapped; must contain at least @size bytes.
414 */
415 void memory_region_init_ram_ptr(MemoryRegion *mr,
416 struct Object *owner,
417 const char *name,
418 uint64_t size,
419 void *ptr);
420
421 /**
422 * memory_region_init_alias: Initialize a memory region that aliases all or a
423 * part of another memory region.
424 *
425 * @mr: the #MemoryRegion to be initialized.
426 * @owner: the object that tracks the region's reference count
427 * @name: used for debugging; not visible to the user or ABI
428 * @orig: the region to be referenced; @mr will be equivalent to
429 * @orig between @offset and @offset + @size - 1.
430 * @offset: start of the section in @orig to be referenced.
431 * @size: size of the region.
432 */
433 void memory_region_init_alias(MemoryRegion *mr,
434 struct Object *owner,
435 const char *name,
436 MemoryRegion *orig,
437 hwaddr offset,
438 uint64_t size);
439
440 /**
441 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
442 * handled via callbacks.
443 *
444 * If NULL callbacks pointer is given, then I/O space is not supposed to be
445 * handled by QEMU itself. Any access via the memory API will cause an abort().
446 *
447 * @mr: the #MemoryRegion to be initialized.
448 * @owner: the object that tracks the region's reference count
449 * @ops: callbacks for write access handling.
450 * @name: the name of the region.
451 * @size: size of the region.
452 * @errp: pointer to Error*, to store an error if it happens.
453 */
454 void memory_region_init_rom_device(MemoryRegion *mr,
455 struct Object *owner,
456 const MemoryRegionOps *ops,
457 void *opaque,
458 const char *name,
459 uint64_t size,
460 Error **errp);
461
462 /**
463 * memory_region_init_reservation: Initialize a memory region that reserves
464 * I/O space.
465 *
466 * A reservation region primariy serves debugging purposes. It claims I/O
467 * space that is not supposed to be handled by QEMU itself. Any access via
468 * the memory API will cause an abort().
469 * This function is deprecated. Use memory_region_init_io() with NULL
470 * callbacks instead.
471 *
472 * @mr: the #MemoryRegion to be initialized
473 * @owner: the object that tracks the region's reference count
474 * @name: used for debugging; not visible to the user or ABI
475 * @size: size of the region.
476 */
477 static inline void memory_region_init_reservation(MemoryRegion *mr,
478 Object *owner,
479 const char *name,
480 uint64_t size)
481 {
482 memory_region_init_io(mr, owner, NULL, mr, name, size);
483 }
484
485 /**
486 * memory_region_init_iommu: Initialize a memory region that translates
487 * addresses
488 *
489 * An IOMMU region translates addresses and forwards accesses to a target
490 * memory region.
491 *
492 * @mr: the #MemoryRegion to be initialized
493 * @owner: the object that tracks the region's reference count
494 * @ops: a function that translates addresses into the @target region
495 * @name: used for debugging; not visible to the user or ABI
496 * @size: size of the region.
497 */
498 void memory_region_init_iommu(MemoryRegion *mr,
499 struct Object *owner,
500 const MemoryRegionIOMMUOps *ops,
501 const char *name,
502 uint64_t size);
503
504 /**
505 * memory_region_owner: get a memory region's owner.
506 *
507 * @mr: the memory region being queried.
508 */
509 struct Object *memory_region_owner(MemoryRegion *mr);
510
511 /**
512 * memory_region_size: get a memory region's size.
513 *
514 * @mr: the memory region being queried.
515 */
516 uint64_t memory_region_size(MemoryRegion *mr);
517
518 /**
519 * memory_region_is_ram: check whether a memory region is random access
520 *
521 * Returns %true is a memory region is random access.
522 *
523 * @mr: the memory region being queried
524 */
525 static inline bool memory_region_is_ram(MemoryRegion *mr)
526 {
527 return mr->ram;
528 }
529
530 /**
531 * memory_region_is_skip_dump: check whether a memory region should not be
532 * dumped
533 *
534 * Returns %true is a memory region should not be dumped(e.g. VFIO BAR MMAP).
535 *
536 * @mr: the memory region being queried
537 */
538 bool memory_region_is_skip_dump(MemoryRegion *mr);
539
540 /**
541 * memory_region_set_skip_dump: Set skip_dump flag, dump will ignore this memory
542 * region
543 *
544 * @mr: the memory region being queried
545 */
546 void memory_region_set_skip_dump(MemoryRegion *mr);
547
548 /**
549 * memory_region_is_romd: check whether a memory region is in ROMD mode
550 *
551 * Returns %true if a memory region is a ROM device and currently set to allow
552 * direct reads.
553 *
554 * @mr: the memory region being queried
555 */
556 static inline bool memory_region_is_romd(MemoryRegion *mr)
557 {
558 return mr->rom_device && mr->romd_mode;
559 }
560
561 /**
562 * memory_region_is_iommu: check whether a memory region is an iommu
563 *
564 * Returns %true is a memory region is an iommu.
565 *
566 * @mr: the memory region being queried
567 */
568 static inline bool memory_region_is_iommu(MemoryRegion *mr)
569 {
570 return mr->iommu_ops;
571 }
572
573
574 /**
575 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
576 *
577 * @mr: the memory region that was changed
578 * @entry: the new entry in the IOMMU translation table. The entry
579 * replaces all old entries for the same virtual I/O address range.
580 * Deleted entries have .@perm == 0.
581 */
582 void memory_region_notify_iommu(MemoryRegion *mr,
583 IOMMUTLBEntry entry);
584
585 /**
586 * memory_region_register_iommu_notifier: register a notifier for changes to
587 * IOMMU translation entries.
588 *
589 * @mr: the memory region to observe
590 * @n: the notifier to be added; the notifier receives a pointer to an
591 * #IOMMUTLBEntry as the opaque value; the pointer ceases to be
592 * valid on exit from the notifier.
593 */
594 void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n);
595
596 /**
597 * memory_region_iommu_replay: replay existing IOMMU translations to
598 * a notifier
599 *
600 * @mr: the memory region to observe
601 * @n: the notifier to which to replay iommu mappings
602 * @granularity: Minimum page granularity to replay notifications for
603 * @is_write: Whether to treat the replay as a translate "write"
604 * through the iommu
605 */
606 void memory_region_iommu_replay(MemoryRegion *mr, Notifier *n,
607 hwaddr granularity, bool is_write);
608
609 /**
610 * memory_region_unregister_iommu_notifier: unregister a notifier for
611 * changes to IOMMU translation entries.
612 *
613 * @n: the notifier to be removed.
614 */
615 void memory_region_unregister_iommu_notifier(Notifier *n);
616
617 /**
618 * memory_region_name: get a memory region's name
619 *
620 * Returns the string that was used to initialize the memory region.
621 *
622 * @mr: the memory region being queried
623 */
624 const char *memory_region_name(const MemoryRegion *mr);
625
626 /**
627 * memory_region_is_logging: return whether a memory region is logging writes
628 *
629 * Returns %true if the memory region is logging writes for the given client
630 *
631 * @mr: the memory region being queried
632 * @client: the client being queried
633 */
634 bool memory_region_is_logging(MemoryRegion *mr, uint8_t client);
635
636 /**
637 * memory_region_get_dirty_log_mask: return the clients for which a
638 * memory region is logging writes.
639 *
640 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
641 * are the bit indices.
642 *
643 * @mr: the memory region being queried
644 */
645 uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr);
646
647 /**
648 * memory_region_is_rom: check whether a memory region is ROM
649 *
650 * Returns %true is a memory region is read-only memory.
651 *
652 * @mr: the memory region being queried
653 */
654 static inline bool memory_region_is_rom(MemoryRegion *mr)
655 {
656 return mr->ram && mr->readonly;
657 }
658
659
660 /**
661 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
662 *
663 * Returns a file descriptor backing a file-based RAM memory region,
664 * or -1 if the region is not a file-based RAM memory region.
665 *
666 * @mr: the RAM or alias memory region being queried.
667 */
668 int memory_region_get_fd(MemoryRegion *mr);
669
670 /**
671 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
672 *
673 * Returns a host pointer to a RAM memory region (created with
674 * memory_region_init_ram() or memory_region_init_ram_ptr()).
675 *
676 * Use with care; by the time this function returns, the returned pointer is
677 * not protected by RCU anymore. If the caller is not within an RCU critical
678 * section and does not hold the iothread lock, it must have other means of
679 * protecting the pointer, such as a reference to the region that includes
680 * the incoming ram_addr_t.
681 *
682 * @mr: the memory region being queried.
683 */
684 void *memory_region_get_ram_ptr(MemoryRegion *mr);
685
686 /* memory_region_ram_resize: Resize a RAM region.
687 *
688 * Only legal before guest might have detected the memory size: e.g. on
689 * incoming migration, or right after reset.
690 *
691 * @mr: a memory region created with @memory_region_init_resizeable_ram.
692 * @newsize: the new size the region
693 * @errp: pointer to Error*, to store an error if it happens.
694 */
695 void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize,
696 Error **errp);
697
698 /**
699 * memory_region_set_log: Turn dirty logging on or off for a region.
700 *
701 * Turns dirty logging on or off for a specified client (display, migration).
702 * Only meaningful for RAM regions.
703 *
704 * @mr: the memory region being updated.
705 * @log: whether dirty logging is to be enabled or disabled.
706 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
707 */
708 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
709
710 /**
711 * memory_region_get_dirty: Check whether a range of bytes is dirty
712 * for a specified client.
713 *
714 * Checks whether a range of bytes has been written to since the last
715 * call to memory_region_reset_dirty() with the same @client. Dirty logging
716 * must be enabled.
717 *
718 * @mr: the memory region being queried.
719 * @addr: the address (relative to the start of the region) being queried.
720 * @size: the size of the range being queried.
721 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
722 * %DIRTY_MEMORY_VGA.
723 */
724 bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
725 hwaddr size, unsigned client);
726
727 /**
728 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
729 *
730 * Marks a range of bytes as dirty, after it has been dirtied outside
731 * guest code.
732 *
733 * @mr: the memory region being dirtied.
734 * @addr: the address (relative to the start of the region) being dirtied.
735 * @size: size of the range being dirtied.
736 */
737 void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
738 hwaddr size);
739
740 /**
741 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty
742 * for a specified client. It clears them.
743 *
744 * Checks whether a range of bytes has been written to since the last
745 * call to memory_region_reset_dirty() with the same @client. Dirty logging
746 * must be enabled.
747 *
748 * @mr: the memory region being queried.
749 * @addr: the address (relative to the start of the region) being queried.
750 * @size: the size of the range being queried.
751 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
752 * %DIRTY_MEMORY_VGA.
753 */
754 bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr,
755 hwaddr size, unsigned client);
756 /**
757 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
758 * any external TLBs (e.g. kvm)
759 *
760 * Flushes dirty information from accelerators such as kvm and vhost-net
761 * and makes it available to users of the memory API.
762 *
763 * @mr: the region being flushed.
764 */
765 void memory_region_sync_dirty_bitmap(MemoryRegion *mr);
766
767 /**
768 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
769 * client.
770 *
771 * Marks a range of pages as no longer dirty.
772 *
773 * @mr: the region being updated.
774 * @addr: the start of the subrange being cleaned.
775 * @size: the size of the subrange being cleaned.
776 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
777 * %DIRTY_MEMORY_VGA.
778 */
779 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
780 hwaddr size, unsigned client);
781
782 /**
783 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
784 *
785 * Allows a memory region to be marked as read-only (turning it into a ROM).
786 * only useful on RAM regions.
787 *
788 * @mr: the region being updated.
789 * @readonly: whether rhe region is to be ROM or RAM.
790 */
791 void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
792
793 /**
794 * memory_region_rom_device_set_romd: enable/disable ROMD mode
795 *
796 * Allows a ROM device (initialized with memory_region_init_rom_device() to
797 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
798 * device is mapped to guest memory and satisfies read access directly.
799 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
800 * Writes are always handled by the #MemoryRegion.write function.
801 *
802 * @mr: the memory region to be updated
803 * @romd_mode: %true to put the region into ROMD mode
804 */
805 void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode);
806
807 /**
808 * memory_region_set_coalescing: Enable memory coalescing for the region.
809 *
810 * Enabled writes to a region to be queued for later processing. MMIO ->write
811 * callbacks may be delayed until a non-coalesced MMIO is issued.
812 * Only useful for IO regions. Roughly similar to write-combining hardware.
813 *
814 * @mr: the memory region to be write coalesced
815 */
816 void memory_region_set_coalescing(MemoryRegion *mr);
817
818 /**
819 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
820 * a region.
821 *
822 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
823 * Multiple calls can be issued coalesced disjoint ranges.
824 *
825 * @mr: the memory region to be updated.
826 * @offset: the start of the range within the region to be coalesced.
827 * @size: the size of the subrange to be coalesced.
828 */
829 void memory_region_add_coalescing(MemoryRegion *mr,
830 hwaddr offset,
831 uint64_t size);
832
833 /**
834 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
835 *
836 * Disables any coalescing caused by memory_region_set_coalescing() or
837 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
838 * hardware.
839 *
840 * @mr: the memory region to be updated.
841 */
842 void memory_region_clear_coalescing(MemoryRegion *mr);
843
844 /**
845 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
846 * accesses.
847 *
848 * Ensure that pending coalesced MMIO request are flushed before the memory
849 * region is accessed. This property is automatically enabled for all regions
850 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
851 *
852 * @mr: the memory region to be updated.
853 */
854 void memory_region_set_flush_coalesced(MemoryRegion *mr);
855
856 /**
857 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
858 * accesses.
859 *
860 * Clear the automatic coalesced MMIO flushing enabled via
861 * memory_region_set_flush_coalesced. Note that this service has no effect on
862 * memory regions that have MMIO coalescing enabled for themselves. For them,
863 * automatic flushing will stop once coalescing is disabled.
864 *
865 * @mr: the memory region to be updated.
866 */
867 void memory_region_clear_flush_coalesced(MemoryRegion *mr);
868
869 /**
870 * memory_region_set_global_locking: Declares the access processing requires
871 * QEMU's global lock.
872 *
873 * When this is invoked, accesses to the memory region will be processed while
874 * holding the global lock of QEMU. This is the default behavior of memory
875 * regions.
876 *
877 * @mr: the memory region to be updated.
878 */
879 void memory_region_set_global_locking(MemoryRegion *mr);
880
881 /**
882 * memory_region_clear_global_locking: Declares that access processing does
883 * not depend on the QEMU global lock.
884 *
885 * By clearing this property, accesses to the memory region will be processed
886 * outside of QEMU's global lock (unless the lock is held on when issuing the
887 * access request). In this case, the device model implementing the access
888 * handlers is responsible for synchronization of concurrency.
889 *
890 * @mr: the memory region to be updated.
891 */
892 void memory_region_clear_global_locking(MemoryRegion *mr);
893
894 /**
895 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
896 * is written to a location.
897 *
898 * Marks a word in an IO region (initialized with memory_region_init_io())
899 * as a trigger for an eventfd event. The I/O callback will not be called.
900 * The caller must be prepared to handle failure (that is, take the required
901 * action if the callback _is_ called).
902 *
903 * @mr: the memory region being updated.
904 * @addr: the address within @mr that is to be monitored
905 * @size: the size of the access to trigger the eventfd
906 * @match_data: whether to match against @data, instead of just @addr
907 * @data: the data to match against the guest write
908 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
909 **/
910 void memory_region_add_eventfd(MemoryRegion *mr,
911 hwaddr addr,
912 unsigned size,
913 bool match_data,
914 uint64_t data,
915 EventNotifier *e);
916
917 /**
918 * memory_region_del_eventfd: Cancel an eventfd.
919 *
920 * Cancels an eventfd trigger requested by a previous
921 * memory_region_add_eventfd() call.
922 *
923 * @mr: the memory region being updated.
924 * @addr: the address within @mr that is to be monitored
925 * @size: the size of the access to trigger the eventfd
926 * @match_data: whether to match against @data, instead of just @addr
927 * @data: the data to match against the guest write
928 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
929 */
930 void memory_region_del_eventfd(MemoryRegion *mr,
931 hwaddr addr,
932 unsigned size,
933 bool match_data,
934 uint64_t data,
935 EventNotifier *e);
936
937 /**
938 * memory_region_add_subregion: Add a subregion to a container.
939 *
940 * Adds a subregion at @offset. The subregion may not overlap with other
941 * subregions (except for those explicitly marked as overlapping). A region
942 * may only be added once as a subregion (unless removed with
943 * memory_region_del_subregion()); use memory_region_init_alias() if you
944 * want a region to be a subregion in multiple locations.
945 *
946 * @mr: the region to contain the new subregion; must be a container
947 * initialized with memory_region_init().
948 * @offset: the offset relative to @mr where @subregion is added.
949 * @subregion: the subregion to be added.
950 */
951 void memory_region_add_subregion(MemoryRegion *mr,
952 hwaddr offset,
953 MemoryRegion *subregion);
954 /**
955 * memory_region_add_subregion_overlap: Add a subregion to a container
956 * with overlap.
957 *
958 * Adds a subregion at @offset. The subregion may overlap with other
959 * subregions. Conflicts are resolved by having a higher @priority hide a
960 * lower @priority. Subregions without priority are taken as @priority 0.
961 * A region may only be added once as a subregion (unless removed with
962 * memory_region_del_subregion()); use memory_region_init_alias() if you
963 * want a region to be a subregion in multiple locations.
964 *
965 * @mr: the region to contain the new subregion; must be a container
966 * initialized with memory_region_init().
967 * @offset: the offset relative to @mr where @subregion is added.
968 * @subregion: the subregion to be added.
969 * @priority: used for resolving overlaps; highest priority wins.
970 */
971 void memory_region_add_subregion_overlap(MemoryRegion *mr,
972 hwaddr offset,
973 MemoryRegion *subregion,
974 int priority);
975
976 /**
977 * memory_region_get_ram_addr: Get the ram address associated with a memory
978 * region
979 */
980 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr);
981
982 uint64_t memory_region_get_alignment(const MemoryRegion *mr);
983 /**
984 * memory_region_del_subregion: Remove a subregion.
985 *
986 * Removes a subregion from its container.
987 *
988 * @mr: the container to be updated.
989 * @subregion: the region being removed; must be a current subregion of @mr.
990 */
991 void memory_region_del_subregion(MemoryRegion *mr,
992 MemoryRegion *subregion);
993
994 /*
995 * memory_region_set_enabled: dynamically enable or disable a region
996 *
997 * Enables or disables a memory region. A disabled memory region
998 * ignores all accesses to itself and its subregions. It does not
999 * obscure sibling subregions with lower priority - it simply behaves as
1000 * if it was removed from the hierarchy.
1001 *
1002 * Regions default to being enabled.
1003 *
1004 * @mr: the region to be updated
1005 * @enabled: whether to enable or disable the region
1006 */
1007 void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
1008
1009 /*
1010 * memory_region_set_address: dynamically update the address of a region
1011 *
1012 * Dynamically updates the address of a region, relative to its container.
1013 * May be used on regions are currently part of a memory hierarchy.
1014 *
1015 * @mr: the region to be updated
1016 * @addr: new address, relative to container region
1017 */
1018 void memory_region_set_address(MemoryRegion *mr, hwaddr addr);
1019
1020 /*
1021 * memory_region_set_size: dynamically update the size of a region.
1022 *
1023 * Dynamically updates the size of a region.
1024 *
1025 * @mr: the region to be updated
1026 * @size: used size of the region.
1027 */
1028 void memory_region_set_size(MemoryRegion *mr, uint64_t size);
1029
1030 /*
1031 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1032 *
1033 * Dynamically updates the offset into the target region that an alias points
1034 * to, as if the fourth argument to memory_region_init_alias() has changed.
1035 *
1036 * @mr: the #MemoryRegion to be updated; should be an alias.
1037 * @offset: the new offset into the target memory region
1038 */
1039 void memory_region_set_alias_offset(MemoryRegion *mr,
1040 hwaddr offset);
1041
1042 /**
1043 * memory_region_present: checks if an address relative to a @container
1044 * translates into #MemoryRegion within @container
1045 *
1046 * Answer whether a #MemoryRegion within @container covers the address
1047 * @addr.
1048 *
1049 * @container: a #MemoryRegion within which @addr is a relative address
1050 * @addr: the area within @container to be searched
1051 */
1052 bool memory_region_present(MemoryRegion *container, hwaddr addr);
1053
1054 /**
1055 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1056 * into any address space.
1057 *
1058 * @mr: a #MemoryRegion which should be checked if it's mapped
1059 */
1060 bool memory_region_is_mapped(MemoryRegion *mr);
1061
1062 /**
1063 * memory_region_find: translate an address/size relative to a
1064 * MemoryRegion into a #MemoryRegionSection.
1065 *
1066 * Locates the first #MemoryRegion within @mr that overlaps the range
1067 * given by @addr and @size.
1068 *
1069 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1070 * It will have the following characteristics:
1071 * .@size = 0 iff no overlap was found
1072 * .@mr is non-%NULL iff an overlap was found
1073 *
1074 * Remember that in the return value the @offset_within_region is
1075 * relative to the returned region (in the .@mr field), not to the
1076 * @mr argument.
1077 *
1078 * Similarly, the .@offset_within_address_space is relative to the
1079 * address space that contains both regions, the passed and the
1080 * returned one. However, in the special case where the @mr argument
1081 * has no container (and thus is the root of the address space), the
1082 * following will hold:
1083 * .@offset_within_address_space >= @addr
1084 * .@offset_within_address_space + .@size <= @addr + @size
1085 *
1086 * @mr: a MemoryRegion within which @addr is a relative address
1087 * @addr: start of the area within @as to be searched
1088 * @size: size of the area to be searched
1089 */
1090 MemoryRegionSection memory_region_find(MemoryRegion *mr,
1091 hwaddr addr, uint64_t size);
1092
1093 /**
1094 * address_space_sync_dirty_bitmap: synchronize the dirty log for all memory
1095 *
1096 * Synchronizes the dirty page log for an entire address space.
1097 * @as: the address space that contains the memory being synchronized
1098 */
1099 void address_space_sync_dirty_bitmap(AddressSpace *as);
1100
1101 /**
1102 * memory_region_transaction_begin: Start a transaction.
1103 *
1104 * During a transaction, changes will be accumulated and made visible
1105 * only when the transaction ends (is committed).
1106 */
1107 void memory_region_transaction_begin(void);
1108
1109 /**
1110 * memory_region_transaction_commit: Commit a transaction and make changes
1111 * visible to the guest.
1112 */
1113 void memory_region_transaction_commit(void);
1114
1115 /**
1116 * memory_listener_register: register callbacks to be called when memory
1117 * sections are mapped or unmapped into an address
1118 * space
1119 *
1120 * @listener: an object containing the callbacks to be called
1121 * @filter: if non-%NULL, only regions in this address space will be observed
1122 */
1123 void memory_listener_register(MemoryListener *listener, AddressSpace *filter);
1124
1125 /**
1126 * memory_listener_unregister: undo the effect of memory_listener_register()
1127 *
1128 * @listener: an object containing the callbacks to be removed
1129 */
1130 void memory_listener_unregister(MemoryListener *listener);
1131
1132 /**
1133 * memory_global_dirty_log_start: begin dirty logging for all regions
1134 */
1135 void memory_global_dirty_log_start(void);
1136
1137 /**
1138 * memory_global_dirty_log_stop: end dirty logging for all regions
1139 */
1140 void memory_global_dirty_log_stop(void);
1141
1142 void mtree_info(fprintf_function mon_printf, void *f);
1143
1144 /**
1145 * memory_region_dispatch_read: perform a read directly to the specified
1146 * MemoryRegion.
1147 *
1148 * @mr: #MemoryRegion to access
1149 * @addr: address within that region
1150 * @pval: pointer to uint64_t which the data is written to
1151 * @size: size of the access in bytes
1152 * @attrs: memory transaction attributes to use for the access
1153 */
1154 MemTxResult memory_region_dispatch_read(MemoryRegion *mr,
1155 hwaddr addr,
1156 uint64_t *pval,
1157 unsigned size,
1158 MemTxAttrs attrs);
1159 /**
1160 * memory_region_dispatch_write: perform a write directly to the specified
1161 * MemoryRegion.
1162 *
1163 * @mr: #MemoryRegion to access
1164 * @addr: address within that region
1165 * @data: data to write
1166 * @size: size of the access in bytes
1167 * @attrs: memory transaction attributes to use for the access
1168 */
1169 MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
1170 hwaddr addr,
1171 uint64_t data,
1172 unsigned size,
1173 MemTxAttrs attrs);
1174
1175 /**
1176 * address_space_init: initializes an address space
1177 *
1178 * @as: an uninitialized #AddressSpace
1179 * @root: a #MemoryRegion that routes addresses for the address space
1180 * @name: an address space name. The name is only used for debugging
1181 * output.
1182 */
1183 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
1184
1185 /**
1186 * address_space_init_shareable: return an address space for a memory region,
1187 * creating it if it does not already exist
1188 *
1189 * @root: a #MemoryRegion that routes addresses for the address space
1190 * @name: an address space name. The name is only used for debugging
1191 * output.
1192 *
1193 * This function will return a pointer to an existing AddressSpace
1194 * which was initialized with the specified MemoryRegion, or it will
1195 * create and initialize one if it does not already exist. The ASes
1196 * are reference-counted, so the memory will be freed automatically
1197 * when the AddressSpace is destroyed via address_space_destroy.
1198 */
1199 AddressSpace *address_space_init_shareable(MemoryRegion *root,
1200 const char *name);
1201
1202 /**
1203 * address_space_destroy: destroy an address space
1204 *
1205 * Releases all resources associated with an address space. After an address space
1206 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1207 * as well.
1208 *
1209 * @as: address space to be destroyed
1210 */
1211 void address_space_destroy(AddressSpace *as);
1212
1213 /**
1214 * address_space_rw: read from or write to an address space.
1215 *
1216 * Return a MemTxResult indicating whether the operation succeeded
1217 * or failed (eg unassigned memory, device rejected the transaction,
1218 * IOMMU fault).
1219 *
1220 * @as: #AddressSpace to be accessed
1221 * @addr: address within that address space
1222 * @attrs: memory transaction attributes
1223 * @buf: buffer with the data transferred
1224 * @is_write: indicates the transfer direction
1225 */
1226 MemTxResult address_space_rw(AddressSpace *as, hwaddr addr,
1227 MemTxAttrs attrs, uint8_t *buf,
1228 int len, bool is_write);
1229
1230 /**
1231 * address_space_write: write to address space.
1232 *
1233 * Return a MemTxResult indicating whether the operation succeeded
1234 * or failed (eg unassigned memory, device rejected the transaction,
1235 * IOMMU fault).
1236 *
1237 * @as: #AddressSpace to be accessed
1238 * @addr: address within that address space
1239 * @attrs: memory transaction attributes
1240 * @buf: buffer with the data transferred
1241 */
1242 MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
1243 MemTxAttrs attrs,
1244 const uint8_t *buf, int len);
1245
1246 /* address_space_ld*: load from an address space
1247 * address_space_st*: store to an address space
1248 *
1249 * These functions perform a load or store of the byte, word,
1250 * longword or quad to the specified address within the AddressSpace.
1251 * The _le suffixed functions treat the data as little endian;
1252 * _be indicates big endian; no suffix indicates "same endianness
1253 * as guest CPU".
1254 *
1255 * The "guest CPU endianness" accessors are deprecated for use outside
1256 * target-* code; devices should be CPU-agnostic and use either the LE
1257 * or the BE accessors.
1258 *
1259 * @as #AddressSpace to be accessed
1260 * @addr: address within that address space
1261 * @val: data value, for stores
1262 * @attrs: memory transaction attributes
1263 * @result: location to write the success/failure of the transaction;
1264 * if NULL, this information is discarded
1265 */
1266 uint32_t address_space_ldub(AddressSpace *as, hwaddr addr,
1267 MemTxAttrs attrs, MemTxResult *result);
1268 uint32_t address_space_lduw_le(AddressSpace *as, hwaddr addr,
1269 MemTxAttrs attrs, MemTxResult *result);
1270 uint32_t address_space_lduw_be(AddressSpace *as, hwaddr addr,
1271 MemTxAttrs attrs, MemTxResult *result);
1272 uint32_t address_space_ldl_le(AddressSpace *as, hwaddr addr,
1273 MemTxAttrs attrs, MemTxResult *result);
1274 uint32_t address_space_ldl_be(AddressSpace *as, hwaddr addr,
1275 MemTxAttrs attrs, MemTxResult *result);
1276 uint64_t address_space_ldq_le(AddressSpace *as, hwaddr addr,
1277 MemTxAttrs attrs, MemTxResult *result);
1278 uint64_t address_space_ldq_be(AddressSpace *as, hwaddr addr,
1279 MemTxAttrs attrs, MemTxResult *result);
1280 void address_space_stb(AddressSpace *as, hwaddr addr, uint32_t val,
1281 MemTxAttrs attrs, MemTxResult *result);
1282 void address_space_stw_le(AddressSpace *as, hwaddr addr, uint32_t val,
1283 MemTxAttrs attrs, MemTxResult *result);
1284 void address_space_stw_be(AddressSpace *as, hwaddr addr, uint32_t val,
1285 MemTxAttrs attrs, MemTxResult *result);
1286 void address_space_stl_le(AddressSpace *as, hwaddr addr, uint32_t val,
1287 MemTxAttrs attrs, MemTxResult *result);
1288 void address_space_stl_be(AddressSpace *as, hwaddr addr, uint32_t val,
1289 MemTxAttrs attrs, MemTxResult *result);
1290 void address_space_stq_le(AddressSpace *as, hwaddr addr, uint64_t val,
1291 MemTxAttrs attrs, MemTxResult *result);
1292 void address_space_stq_be(AddressSpace *as, hwaddr addr, uint64_t val,
1293 MemTxAttrs attrs, MemTxResult *result);
1294
1295 /* address_space_translate: translate an address range into an address space
1296 * into a MemoryRegion and an address range into that section. Should be
1297 * called from an RCU critical section, to avoid that the last reference
1298 * to the returned region disappears after address_space_translate returns.
1299 *
1300 * @as: #AddressSpace to be accessed
1301 * @addr: address within that address space
1302 * @xlat: pointer to address within the returned memory region section's
1303 * #MemoryRegion.
1304 * @len: pointer to length
1305 * @is_write: indicates the transfer direction
1306 */
1307 MemoryRegion *address_space_translate(AddressSpace *as, hwaddr addr,
1308 hwaddr *xlat, hwaddr *len,
1309 bool is_write);
1310
1311 /* address_space_access_valid: check for validity of accessing an address
1312 * space range
1313 *
1314 * Check whether memory is assigned to the given address space range, and
1315 * access is permitted by any IOMMU regions that are active for the address
1316 * space.
1317 *
1318 * For now, addr and len should be aligned to a page size. This limitation
1319 * will be lifted in the future.
1320 *
1321 * @as: #AddressSpace to be accessed
1322 * @addr: address within that address space
1323 * @len: length of the area to be checked
1324 * @is_write: indicates the transfer direction
1325 */
1326 bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write);
1327
1328 /* address_space_map: map a physical memory region into a host virtual address
1329 *
1330 * May map a subset of the requested range, given by and returned in @plen.
1331 * May return %NULL if resources needed to perform the mapping are exhausted.
1332 * Use only for reads OR writes - not for read-modify-write operations.
1333 * Use cpu_register_map_client() to know when retrying the map operation is
1334 * likely to succeed.
1335 *
1336 * @as: #AddressSpace to be accessed
1337 * @addr: address within that address space
1338 * @plen: pointer to length of buffer; updated on return
1339 * @is_write: indicates the transfer direction
1340 */
1341 void *address_space_map(AddressSpace *as, hwaddr addr,
1342 hwaddr *plen, bool is_write);
1343
1344 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1345 *
1346 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1347 * the amount of memory that was actually read or written by the caller.
1348 *
1349 * @as: #AddressSpace used
1350 * @addr: address within that address space
1351 * @len: buffer length as returned by address_space_map()
1352 * @access_len: amount of data actually transferred
1353 * @is_write: indicates the transfer direction
1354 */
1355 void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
1356 int is_write, hwaddr access_len);
1357
1358
1359 /* Internal functions, part of the implementation of address_space_read. */
1360 MemTxResult address_space_read_continue(AddressSpace *as, hwaddr addr,
1361 MemTxAttrs attrs, uint8_t *buf,
1362 int len, hwaddr addr1, hwaddr l,
1363 MemoryRegion *mr);
1364 MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr,
1365 MemTxAttrs attrs, uint8_t *buf, int len);
1366 void *qemu_get_ram_ptr(RAMBlock *ram_block, ram_addr_t addr);
1367
1368 static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
1369 {
1370 if (is_write) {
1371 return memory_region_is_ram(mr) && !mr->readonly;
1372 } else {
1373 return memory_region_is_ram(mr) || memory_region_is_romd(mr);
1374 }
1375 }
1376
1377 /**
1378 * address_space_read: read from an address space.
1379 *
1380 * Return a MemTxResult indicating whether the operation succeeded
1381 * or failed (eg unassigned memory, device rejected the transaction,
1382 * IOMMU fault).
1383 *
1384 * @as: #AddressSpace to be accessed
1385 * @addr: address within that address space
1386 * @attrs: memory transaction attributes
1387 * @buf: buffer with the data transferred
1388 */
1389 static inline __attribute__((__always_inline__))
1390 MemTxResult address_space_read(AddressSpace *as, hwaddr addr, MemTxAttrs attrs,
1391 uint8_t *buf, int len)
1392 {
1393 MemTxResult result = MEMTX_OK;
1394 hwaddr l, addr1;
1395 void *ptr;
1396 MemoryRegion *mr;
1397
1398 if (__builtin_constant_p(len)) {
1399 if (len) {
1400 rcu_read_lock();
1401 l = len;
1402 mr = address_space_translate(as, addr, &addr1, &l, false);
1403 if (len == l && memory_access_is_direct(mr, false)) {
1404 addr1 += memory_region_get_ram_addr(mr);
1405 ptr = qemu_get_ram_ptr(mr->ram_block, addr1);
1406 memcpy(buf, ptr, len);
1407 } else {
1408 result = address_space_read_continue(as, addr, attrs, buf, len,
1409 addr1, l, mr);
1410 }
1411 rcu_read_unlock();
1412 }
1413 } else {
1414 result = address_space_read_full(as, addr, attrs, buf, len);
1415 }
1416 return result;
1417 }
1418
1419 #endif
1420
1421 #endif