memory: add error propagation to file-based RAM allocation
[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 <stdint.h>
25 #include <stdbool.h>
26 #include "qemu-common.h"
27 #include "exec/cpu-common.h"
28 #ifndef CONFIG_USER_ONLY
29 #include "exec/hwaddr.h"
30 #endif
31 #include "qemu/queue.h"
32 #include "qemu/int128.h"
33 #include "qemu/notify.h"
34 #include "qapi/error.h"
35
36 #define MAX_PHYS_ADDR_SPACE_BITS 62
37 #define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
38
39 typedef struct MemoryRegionOps MemoryRegionOps;
40 typedef struct MemoryRegionMmio MemoryRegionMmio;
41
42 struct MemoryRegionMmio {
43 CPUReadMemoryFunc *read[3];
44 CPUWriteMemoryFunc *write[3];
45 };
46
47 typedef struct IOMMUTLBEntry IOMMUTLBEntry;
48
49 /* See address_space_translate: bit 0 is read, bit 1 is write. */
50 typedef enum {
51 IOMMU_NONE = 0,
52 IOMMU_RO = 1,
53 IOMMU_WO = 2,
54 IOMMU_RW = 3,
55 } IOMMUAccessFlags;
56
57 struct IOMMUTLBEntry {
58 AddressSpace *target_as;
59 hwaddr iova;
60 hwaddr translated_addr;
61 hwaddr addr_mask; /* 0xfff = 4k translation */
62 IOMMUAccessFlags perm;
63 };
64
65 /*
66 * Memory region callbacks
67 */
68 struct MemoryRegionOps {
69 /* Read from the memory region. @addr is relative to @mr; @size is
70 * in bytes. */
71 uint64_t (*read)(void *opaque,
72 hwaddr addr,
73 unsigned size);
74 /* Write to the memory region. @addr is relative to @mr; @size is
75 * in bytes. */
76 void (*write)(void *opaque,
77 hwaddr addr,
78 uint64_t data,
79 unsigned size);
80
81 enum device_endian endianness;
82 /* Guest-visible constraints: */
83 struct {
84 /* If nonzero, specify bounds on access sizes beyond which a machine
85 * check is thrown.
86 */
87 unsigned min_access_size;
88 unsigned max_access_size;
89 /* If true, unaligned accesses are supported. Otherwise unaligned
90 * accesses throw machine checks.
91 */
92 bool unaligned;
93 /*
94 * If present, and returns #false, the transaction is not accepted
95 * by the device (and results in machine dependent behaviour such
96 * as a machine check exception).
97 */
98 bool (*accepts)(void *opaque, hwaddr addr,
99 unsigned size, bool is_write);
100 } valid;
101 /* Internal implementation constraints: */
102 struct {
103 /* If nonzero, specifies the minimum size implemented. Smaller sizes
104 * will be rounded upwards and a partial result will be returned.
105 */
106 unsigned min_access_size;
107 /* If nonzero, specifies the maximum size implemented. Larger sizes
108 * will be done as a series of accesses with smaller sizes.
109 */
110 unsigned max_access_size;
111 /* If true, unaligned accesses are supported. Otherwise all accesses
112 * are converted to (possibly multiple) naturally aligned accesses.
113 */
114 bool unaligned;
115 } impl;
116
117 /* If .read and .write are not present, old_mmio may be used for
118 * backwards compatibility with old mmio registration
119 */
120 const MemoryRegionMmio old_mmio;
121 };
122
123 typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps;
124
125 struct MemoryRegionIOMMUOps {
126 /* Return a TLB entry that contains a given address. */
127 IOMMUTLBEntry (*translate)(MemoryRegion *iommu, hwaddr addr);
128 };
129
130 typedef struct CoalescedMemoryRange CoalescedMemoryRange;
131 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
132
133 struct MemoryRegion {
134 /* All fields are private - violators will be prosecuted */
135 const MemoryRegionOps *ops;
136 const MemoryRegionIOMMUOps *iommu_ops;
137 void *opaque;
138 struct Object *owner;
139 MemoryRegion *container;
140 Int128 size;
141 hwaddr addr;
142 void (*destructor)(MemoryRegion *mr);
143 ram_addr_t ram_addr;
144 bool subpage;
145 bool terminates;
146 bool romd_mode;
147 bool ram;
148 bool readonly; /* For RAM regions */
149 bool enabled;
150 bool rom_device;
151 bool warning_printed; /* For reservations */
152 bool flush_coalesced_mmio;
153 MemoryRegion *alias;
154 hwaddr alias_offset;
155 int priority;
156 bool may_overlap;
157 QTAILQ_HEAD(subregions, MemoryRegion) subregions;
158 QTAILQ_ENTRY(MemoryRegion) subregions_link;
159 QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced;
160 const char *name;
161 uint8_t dirty_log_mask;
162 unsigned ioeventfd_nb;
163 MemoryRegionIoeventfd *ioeventfds;
164 NotifierList iommu_notify;
165 };
166
167 /**
168 * MemoryListener: callbacks structure for updates to the physical memory map
169 *
170 * Allows a component to adjust to changes in the guest-visible memory map.
171 * Use with memory_listener_register() and memory_listener_unregister().
172 */
173 struct MemoryListener {
174 void (*begin)(MemoryListener *listener);
175 void (*commit)(MemoryListener *listener);
176 void (*region_add)(MemoryListener *listener, MemoryRegionSection *section);
177 void (*region_del)(MemoryListener *listener, MemoryRegionSection *section);
178 void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section);
179 void (*log_start)(MemoryListener *listener, MemoryRegionSection *section);
180 void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section);
181 void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section);
182 void (*log_global_start)(MemoryListener *listener);
183 void (*log_global_stop)(MemoryListener *listener);
184 void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section,
185 bool match_data, uint64_t data, EventNotifier *e);
186 void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section,
187 bool match_data, uint64_t data, EventNotifier *e);
188 void (*coalesced_mmio_add)(MemoryListener *listener, MemoryRegionSection *section,
189 hwaddr addr, hwaddr len);
190 void (*coalesced_mmio_del)(MemoryListener *listener, MemoryRegionSection *section,
191 hwaddr addr, hwaddr len);
192 /* Lower = earlier (during add), later (during del) */
193 unsigned priority;
194 AddressSpace *address_space_filter;
195 QTAILQ_ENTRY(MemoryListener) link;
196 };
197
198 /**
199 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
200 */
201 struct AddressSpace {
202 /* All fields are private. */
203 char *name;
204 MemoryRegion *root;
205 struct FlatView *current_map;
206 int ioeventfd_nb;
207 struct MemoryRegionIoeventfd *ioeventfds;
208 struct AddressSpaceDispatch *dispatch;
209 struct AddressSpaceDispatch *next_dispatch;
210 MemoryListener dispatch_listener;
211
212 QTAILQ_ENTRY(AddressSpace) address_spaces_link;
213 };
214
215 /**
216 * MemoryRegionSection: describes a fragment of a #MemoryRegion
217 *
218 * @mr: the region, or %NULL if empty
219 * @address_space: the address space the region is mapped in
220 * @offset_within_region: the beginning of the section, relative to @mr's start
221 * @size: the size of the section; will not exceed @mr's boundaries
222 * @offset_within_address_space: the address of the first byte of the section
223 * relative to the region's address space
224 * @readonly: writes to this section are ignored
225 */
226 struct MemoryRegionSection {
227 MemoryRegion *mr;
228 AddressSpace *address_space;
229 hwaddr offset_within_region;
230 Int128 size;
231 hwaddr offset_within_address_space;
232 bool readonly;
233 };
234
235 /**
236 * memory_region_init: Initialize a memory region
237 *
238 * The region typically acts as a container for other memory regions. Use
239 * memory_region_add_subregion() to add subregions.
240 *
241 * @mr: the #MemoryRegion to be initialized
242 * @owner: the object that tracks the region's reference count
243 * @name: used for debugging; not visible to the user or ABI
244 * @size: size of the region; any subregions beyond this size will be clipped
245 */
246 void memory_region_init(MemoryRegion *mr,
247 struct Object *owner,
248 const char *name,
249 uint64_t size);
250
251 /**
252 * memory_region_ref: Add 1 to a memory region's reference count
253 *
254 * Whenever memory regions are accessed outside the BQL, they need to be
255 * preserved against hot-unplug. MemoryRegions actually do not have their
256 * own reference count; they piggyback on a QOM object, their "owner".
257 * This function adds a reference to the owner.
258 *
259 * All MemoryRegions must have an owner if they can disappear, even if the
260 * device they belong to operates exclusively under the BQL. This is because
261 * the region could be returned at any time by memory_region_find, and this
262 * is usually under guest control.
263 *
264 * @mr: the #MemoryRegion
265 */
266 void memory_region_ref(MemoryRegion *mr);
267
268 /**
269 * memory_region_unref: Remove 1 to a memory region's reference count
270 *
271 * Whenever memory regions are accessed outside the BQL, they need to be
272 * preserved against hot-unplug. MemoryRegions actually do not have their
273 * own reference count; they piggyback on a QOM object, their "owner".
274 * This function removes a reference to the owner and possibly destroys it.
275 *
276 * @mr: the #MemoryRegion
277 */
278 void memory_region_unref(MemoryRegion *mr);
279
280 /**
281 * memory_region_init_io: Initialize an I/O memory region.
282 *
283 * Accesses into the region will cause the callbacks in @ops to be called.
284 * if @size is nonzero, subregions will be clipped to @size.
285 *
286 * @mr: the #MemoryRegion to be initialized.
287 * @owner: the object that tracks the region's reference count
288 * @ops: a structure containing read and write callbacks to be used when
289 * I/O is performed on the region.
290 * @opaque: passed to to the read and write callbacks of the @ops structure.
291 * @name: used for debugging; not visible to the user or ABI
292 * @size: size of the region.
293 */
294 void memory_region_init_io(MemoryRegion *mr,
295 struct Object *owner,
296 const MemoryRegionOps *ops,
297 void *opaque,
298 const char *name,
299 uint64_t size);
300
301 /**
302 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
303 * region will modify memory directly.
304 *
305 * @mr: the #MemoryRegion to be initialized.
306 * @owner: the object that tracks the region's reference count
307 * @name: the name of the region.
308 * @size: size of the region.
309 */
310 void memory_region_init_ram(MemoryRegion *mr,
311 struct Object *owner,
312 const char *name,
313 uint64_t size);
314
315 #ifdef __linux__
316 /**
317 * memory_region_init_ram_from_file: Initialize RAM memory region with a
318 * mmap-ed backend.
319 *
320 * @mr: the #MemoryRegion to be initialized.
321 * @owner: the object that tracks the region's reference count
322 * @name: the name of the region.
323 * @size: size of the region.
324 * @path: the path in which to allocate the RAM.
325 * @errp: pointer to Error*, to store an error if it happens.
326 */
327 void memory_region_init_ram_from_file(MemoryRegion *mr,
328 struct Object *owner,
329 const char *name,
330 uint64_t size,
331 const char *path,
332 Error **errp);
333 #endif
334
335 /**
336 * memory_region_init_ram_ptr: Initialize RAM memory region from a
337 * user-provided pointer. Accesses into the
338 * region will modify memory directly.
339 *
340 * @mr: the #MemoryRegion to be initialized.
341 * @owner: the object that tracks the region's reference count
342 * @name: the name of the region.
343 * @size: size of the region.
344 * @ptr: memory to be mapped; must contain at least @size bytes.
345 */
346 void memory_region_init_ram_ptr(MemoryRegion *mr,
347 struct Object *owner,
348 const char *name,
349 uint64_t size,
350 void *ptr);
351
352 /**
353 * memory_region_init_alias: Initialize a memory region that aliases all or a
354 * part of another memory region.
355 *
356 * @mr: the #MemoryRegion to be initialized.
357 * @owner: the object that tracks the region's reference count
358 * @name: used for debugging; not visible to the user or ABI
359 * @orig: the region to be referenced; @mr will be equivalent to
360 * @orig between @offset and @offset + @size - 1.
361 * @offset: start of the section in @orig to be referenced.
362 * @size: size of the region.
363 */
364 void memory_region_init_alias(MemoryRegion *mr,
365 struct Object *owner,
366 const char *name,
367 MemoryRegion *orig,
368 hwaddr offset,
369 uint64_t size);
370
371 /**
372 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
373 * handled via callbacks.
374 *
375 * @mr: the #MemoryRegion to be initialized.
376 * @owner: the object that tracks the region's reference count
377 * @ops: callbacks for write access handling.
378 * @name: the name of the region.
379 * @size: size of the region.
380 */
381 void memory_region_init_rom_device(MemoryRegion *mr,
382 struct Object *owner,
383 const MemoryRegionOps *ops,
384 void *opaque,
385 const char *name,
386 uint64_t size);
387
388 /**
389 * memory_region_init_reservation: Initialize a memory region that reserves
390 * I/O space.
391 *
392 * A reservation region primariy serves debugging purposes. It claims I/O
393 * space that is not supposed to be handled by QEMU itself. Any access via
394 * the memory API will cause an abort().
395 *
396 * @mr: the #MemoryRegion to be initialized
397 * @owner: the object that tracks the region's reference count
398 * @name: used for debugging; not visible to the user or ABI
399 * @size: size of the region.
400 */
401 void memory_region_init_reservation(MemoryRegion *mr,
402 struct Object *owner,
403 const char *name,
404 uint64_t size);
405
406 /**
407 * memory_region_init_iommu: Initialize a memory region that translates
408 * addresses
409 *
410 * An IOMMU region translates addresses and forwards accesses to a target
411 * memory region.
412 *
413 * @mr: the #MemoryRegion to be initialized
414 * @owner: the object that tracks the region's reference count
415 * @ops: a function that translates addresses into the @target region
416 * @name: used for debugging; not visible to the user or ABI
417 * @size: size of the region.
418 */
419 void memory_region_init_iommu(MemoryRegion *mr,
420 struct Object *owner,
421 const MemoryRegionIOMMUOps *ops,
422 const char *name,
423 uint64_t size);
424
425 /**
426 * memory_region_destroy: Destroy a memory region and reclaim all resources.
427 *
428 * @mr: the region to be destroyed. May not currently be a subregion
429 * (see memory_region_add_subregion()) or referenced in an alias
430 * (see memory_region_init_alias()).
431 */
432 void memory_region_destroy(MemoryRegion *mr);
433
434 /**
435 * memory_region_owner: get a memory region's owner.
436 *
437 * @mr: the memory region being queried.
438 */
439 struct Object *memory_region_owner(MemoryRegion *mr);
440
441 /**
442 * memory_region_size: get a memory region's size.
443 *
444 * @mr: the memory region being queried.
445 */
446 uint64_t memory_region_size(MemoryRegion *mr);
447
448 /**
449 * memory_region_is_ram: check whether a memory region is random access
450 *
451 * Returns %true is a memory region is random access.
452 *
453 * @mr: the memory region being queried
454 */
455 bool memory_region_is_ram(MemoryRegion *mr);
456
457 /**
458 * memory_region_is_romd: check whether a memory region is in ROMD mode
459 *
460 * Returns %true if a memory region is a ROM device and currently set to allow
461 * direct reads.
462 *
463 * @mr: the memory region being queried
464 */
465 static inline bool memory_region_is_romd(MemoryRegion *mr)
466 {
467 return mr->rom_device && mr->romd_mode;
468 }
469
470 /**
471 * memory_region_is_iommu: check whether a memory region is an iommu
472 *
473 * Returns %true is a memory region is an iommu.
474 *
475 * @mr: the memory region being queried
476 */
477 bool memory_region_is_iommu(MemoryRegion *mr);
478
479 /**
480 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
481 *
482 * @mr: the memory region that was changed
483 * @entry: the new entry in the IOMMU translation table. The entry
484 * replaces all old entries for the same virtual I/O address range.
485 * Deleted entries have .@perm == 0.
486 */
487 void memory_region_notify_iommu(MemoryRegion *mr,
488 IOMMUTLBEntry entry);
489
490 /**
491 * memory_region_register_iommu_notifier: register a notifier for changes to
492 * IOMMU translation entries.
493 *
494 * @mr: the memory region to observe
495 * @n: the notifier to be added; the notifier receives a pointer to an
496 * #IOMMUTLBEntry as the opaque value; the pointer ceases to be
497 * valid on exit from the notifier.
498 */
499 void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n);
500
501 /**
502 * memory_region_unregister_iommu_notifier: unregister a notifier for
503 * changes to IOMMU translation entries.
504 *
505 * @n: the notifier to be removed.
506 */
507 void memory_region_unregister_iommu_notifier(Notifier *n);
508
509 /**
510 * memory_region_name: get a memory region's name
511 *
512 * Returns the string that was used to initialize the memory region.
513 *
514 * @mr: the memory region being queried
515 */
516 const char *memory_region_name(MemoryRegion *mr);
517
518 /**
519 * memory_region_is_logging: return whether a memory region is logging writes
520 *
521 * Returns %true if the memory region is logging writes
522 *
523 * @mr: the memory region being queried
524 */
525 bool memory_region_is_logging(MemoryRegion *mr);
526
527 /**
528 * memory_region_is_rom: check whether a memory region is ROM
529 *
530 * Returns %true is a memory region is read-only memory.
531 *
532 * @mr: the memory region being queried
533 */
534 bool memory_region_is_rom(MemoryRegion *mr);
535
536 /**
537 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
538 *
539 * Returns a host pointer to a RAM memory region (created with
540 * memory_region_init_ram() or memory_region_init_ram_ptr()). Use with
541 * care.
542 *
543 * @mr: the memory region being queried.
544 */
545 void *memory_region_get_ram_ptr(MemoryRegion *mr);
546
547 /**
548 * memory_region_set_log: Turn dirty logging on or off for a region.
549 *
550 * Turns dirty logging on or off for a specified client (display, migration).
551 * Only meaningful for RAM regions.
552 *
553 * @mr: the memory region being updated.
554 * @log: whether dirty logging is to be enabled or disabled.
555 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
556 * %DIRTY_MEMORY_VGA.
557 */
558 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
559
560 /**
561 * memory_region_get_dirty: Check whether a range of bytes is dirty
562 * for a specified client.
563 *
564 * Checks whether a range of bytes has been written to since the last
565 * call to memory_region_reset_dirty() with the same @client. Dirty logging
566 * must be enabled.
567 *
568 * @mr: the memory region being queried.
569 * @addr: the address (relative to the start of the region) being queried.
570 * @size: the size of the range being queried.
571 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
572 * %DIRTY_MEMORY_VGA.
573 */
574 bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
575 hwaddr size, unsigned client);
576
577 /**
578 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
579 *
580 * Marks a range of bytes as dirty, after it has been dirtied outside
581 * guest code.
582 *
583 * @mr: the memory region being dirtied.
584 * @addr: the address (relative to the start of the region) being dirtied.
585 * @size: size of the range being dirtied.
586 */
587 void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
588 hwaddr size);
589
590 /**
591 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty
592 * for a specified client. It clears them.
593 *
594 * Checks whether a range of bytes has been written to since the last
595 * call to memory_region_reset_dirty() with the same @client. Dirty logging
596 * must be enabled.
597 *
598 * @mr: the memory region being queried.
599 * @addr: the address (relative to the start of the region) being queried.
600 * @size: the size of the range being queried.
601 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
602 * %DIRTY_MEMORY_VGA.
603 */
604 bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr,
605 hwaddr size, unsigned client);
606 /**
607 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
608 * any external TLBs (e.g. kvm)
609 *
610 * Flushes dirty information from accelerators such as kvm and vhost-net
611 * and makes it available to users of the memory API.
612 *
613 * @mr: the region being flushed.
614 */
615 void memory_region_sync_dirty_bitmap(MemoryRegion *mr);
616
617 /**
618 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
619 * client.
620 *
621 * Marks a range of pages as no longer dirty.
622 *
623 * @mr: the region being updated.
624 * @addr: the start of the subrange being cleaned.
625 * @size: the size of the subrange being cleaned.
626 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
627 * %DIRTY_MEMORY_VGA.
628 */
629 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
630 hwaddr size, unsigned client);
631
632 /**
633 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
634 *
635 * Allows a memory region to be marked as read-only (turning it into a ROM).
636 * only useful on RAM regions.
637 *
638 * @mr: the region being updated.
639 * @readonly: whether rhe region is to be ROM or RAM.
640 */
641 void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
642
643 /**
644 * memory_region_rom_device_set_romd: enable/disable ROMD mode
645 *
646 * Allows a ROM device (initialized with memory_region_init_rom_device() to
647 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
648 * device is mapped to guest memory and satisfies read access directly.
649 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
650 * Writes are always handled by the #MemoryRegion.write function.
651 *
652 * @mr: the memory region to be updated
653 * @romd_mode: %true to put the region into ROMD mode
654 */
655 void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode);
656
657 /**
658 * memory_region_set_coalescing: Enable memory coalescing for the region.
659 *
660 * Enabled writes to a region to be queued for later processing. MMIO ->write
661 * callbacks may be delayed until a non-coalesced MMIO is issued.
662 * Only useful for IO regions. Roughly similar to write-combining hardware.
663 *
664 * @mr: the memory region to be write coalesced
665 */
666 void memory_region_set_coalescing(MemoryRegion *mr);
667
668 /**
669 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
670 * a region.
671 *
672 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
673 * Multiple calls can be issued coalesced disjoint ranges.
674 *
675 * @mr: the memory region to be updated.
676 * @offset: the start of the range within the region to be coalesced.
677 * @size: the size of the subrange to be coalesced.
678 */
679 void memory_region_add_coalescing(MemoryRegion *mr,
680 hwaddr offset,
681 uint64_t size);
682
683 /**
684 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
685 *
686 * Disables any coalescing caused by memory_region_set_coalescing() or
687 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
688 * hardware.
689 *
690 * @mr: the memory region to be updated.
691 */
692 void memory_region_clear_coalescing(MemoryRegion *mr);
693
694 /**
695 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
696 * accesses.
697 *
698 * Ensure that pending coalesced MMIO request are flushed before the memory
699 * region is accessed. This property is automatically enabled for all regions
700 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
701 *
702 * @mr: the memory region to be updated.
703 */
704 void memory_region_set_flush_coalesced(MemoryRegion *mr);
705
706 /**
707 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
708 * accesses.
709 *
710 * Clear the automatic coalesced MMIO flushing enabled via
711 * memory_region_set_flush_coalesced. Note that this service has no effect on
712 * memory regions that have MMIO coalescing enabled for themselves. For them,
713 * automatic flushing will stop once coalescing is disabled.
714 *
715 * @mr: the memory region to be updated.
716 */
717 void memory_region_clear_flush_coalesced(MemoryRegion *mr);
718
719 /**
720 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
721 * is written to a location.
722 *
723 * Marks a word in an IO region (initialized with memory_region_init_io())
724 * as a trigger for an eventfd event. The I/O callback will not be called.
725 * The caller must be prepared to handle failure (that is, take the required
726 * action if the callback _is_ called).
727 *
728 * @mr: the memory region being updated.
729 * @addr: the address within @mr that is to be monitored
730 * @size: the size of the access to trigger the eventfd
731 * @match_data: whether to match against @data, instead of just @addr
732 * @data: the data to match against the guest write
733 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
734 **/
735 void memory_region_add_eventfd(MemoryRegion *mr,
736 hwaddr addr,
737 unsigned size,
738 bool match_data,
739 uint64_t data,
740 EventNotifier *e);
741
742 /**
743 * memory_region_del_eventfd: Cancel an eventfd.
744 *
745 * Cancels an eventfd trigger requested by a previous
746 * memory_region_add_eventfd() call.
747 *
748 * @mr: the memory region being updated.
749 * @addr: the address within @mr that is to be monitored
750 * @size: the size of the access to trigger the eventfd
751 * @match_data: whether to match against @data, instead of just @addr
752 * @data: the data to match against the guest write
753 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
754 */
755 void memory_region_del_eventfd(MemoryRegion *mr,
756 hwaddr addr,
757 unsigned size,
758 bool match_data,
759 uint64_t data,
760 EventNotifier *e);
761
762 /**
763 * memory_region_add_subregion: Add a subregion to a container.
764 *
765 * Adds a subregion at @offset. The subregion may not overlap with other
766 * subregions (except for those explicitly marked as overlapping). A region
767 * may only be added once as a subregion (unless removed with
768 * memory_region_del_subregion()); use memory_region_init_alias() if you
769 * want a region to be a subregion in multiple locations.
770 *
771 * @mr: the region to contain the new subregion; must be a container
772 * initialized with memory_region_init().
773 * @offset: the offset relative to @mr where @subregion is added.
774 * @subregion: the subregion to be added.
775 */
776 void memory_region_add_subregion(MemoryRegion *mr,
777 hwaddr offset,
778 MemoryRegion *subregion);
779 /**
780 * memory_region_add_subregion_overlap: Add a subregion to a container
781 * with overlap.
782 *
783 * Adds a subregion at @offset. The subregion may overlap with other
784 * subregions. Conflicts are resolved by having a higher @priority hide a
785 * lower @priority. Subregions without priority are taken as @priority 0.
786 * A region may only be added once as a subregion (unless removed with
787 * memory_region_del_subregion()); use memory_region_init_alias() if you
788 * want a region to be a subregion in multiple locations.
789 *
790 * @mr: the region to contain the new subregion; must be a container
791 * initialized with memory_region_init().
792 * @offset: the offset relative to @mr where @subregion is added.
793 * @subregion: the subregion to be added.
794 * @priority: used for resolving overlaps; highest priority wins.
795 */
796 void memory_region_add_subregion_overlap(MemoryRegion *mr,
797 hwaddr offset,
798 MemoryRegion *subregion,
799 int priority);
800
801 /**
802 * memory_region_get_ram_addr: Get the ram address associated with a memory
803 * region
804 *
805 * DO NOT USE THIS FUNCTION. This is a temporary workaround while the Xen
806 * code is being reworked.
807 */
808 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr);
809
810 /**
811 * memory_region_del_subregion: Remove a subregion.
812 *
813 * Removes a subregion from its container.
814 *
815 * @mr: the container to be updated.
816 * @subregion: the region being removed; must be a current subregion of @mr.
817 */
818 void memory_region_del_subregion(MemoryRegion *mr,
819 MemoryRegion *subregion);
820
821 /*
822 * memory_region_set_enabled: dynamically enable or disable a region
823 *
824 * Enables or disables a memory region. A disabled memory region
825 * ignores all accesses to itself and its subregions. It does not
826 * obscure sibling subregions with lower priority - it simply behaves as
827 * if it was removed from the hierarchy.
828 *
829 * Regions default to being enabled.
830 *
831 * @mr: the region to be updated
832 * @enabled: whether to enable or disable the region
833 */
834 void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
835
836 /*
837 * memory_region_set_address: dynamically update the address of a region
838 *
839 * Dynamically updates the address of a region, relative to its container.
840 * May be used on regions are currently part of a memory hierarchy.
841 *
842 * @mr: the region to be updated
843 * @addr: new address, relative to container region
844 */
845 void memory_region_set_address(MemoryRegion *mr, hwaddr addr);
846
847 /*
848 * memory_region_set_alias_offset: dynamically update a memory alias's offset
849 *
850 * Dynamically updates the offset into the target region that an alias points
851 * to, as if the fourth argument to memory_region_init_alias() has changed.
852 *
853 * @mr: the #MemoryRegion to be updated; should be an alias.
854 * @offset: the new offset into the target memory region
855 */
856 void memory_region_set_alias_offset(MemoryRegion *mr,
857 hwaddr offset);
858
859 /**
860 * memory_region_present: checks if an address relative to a @container
861 * translates into #MemoryRegion within @container
862 *
863 * Answer whether a #MemoryRegion within @container covers the address
864 * @addr.
865 *
866 * @container: a #MemoryRegion within which @addr is a relative address
867 * @addr: the area within @container to be searched
868 */
869 bool memory_region_present(MemoryRegion *container, hwaddr addr);
870
871 /**
872 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
873 * into any address space.
874 *
875 * @mr: a #MemoryRegion which should be checked if it's mapped
876 */
877 bool memory_region_is_mapped(MemoryRegion *mr);
878
879 /**
880 * memory_region_find: translate an address/size relative to a
881 * MemoryRegion into a #MemoryRegionSection.
882 *
883 * Locates the first #MemoryRegion within @mr that overlaps the range
884 * given by @addr and @size.
885 *
886 * Returns a #MemoryRegionSection that describes a contiguous overlap.
887 * It will have the following characteristics:
888 * .@size = 0 iff no overlap was found
889 * .@mr is non-%NULL iff an overlap was found
890 *
891 * Remember that in the return value the @offset_within_region is
892 * relative to the returned region (in the .@mr field), not to the
893 * @mr argument.
894 *
895 * Similarly, the .@offset_within_address_space is relative to the
896 * address space that contains both regions, the passed and the
897 * returned one. However, in the special case where the @mr argument
898 * has no container (and thus is the root of the address space), the
899 * following will hold:
900 * .@offset_within_address_space >= @addr
901 * .@offset_within_address_space + .@size <= @addr + @size
902 *
903 * @mr: a MemoryRegion within which @addr is a relative address
904 * @addr: start of the area within @as to be searched
905 * @size: size of the area to be searched
906 */
907 MemoryRegionSection memory_region_find(MemoryRegion *mr,
908 hwaddr addr, uint64_t size);
909
910 /**
911 * address_space_sync_dirty_bitmap: synchronize the dirty log for all memory
912 *
913 * Synchronizes the dirty page log for an entire address space.
914 * @as: the address space that contains the memory being synchronized
915 */
916 void address_space_sync_dirty_bitmap(AddressSpace *as);
917
918 /**
919 * memory_region_transaction_begin: Start a transaction.
920 *
921 * During a transaction, changes will be accumulated and made visible
922 * only when the transaction ends (is committed).
923 */
924 void memory_region_transaction_begin(void);
925
926 /**
927 * memory_region_transaction_commit: Commit a transaction and make changes
928 * visible to the guest.
929 */
930 void memory_region_transaction_commit(void);
931
932 /**
933 * memory_listener_register: register callbacks to be called when memory
934 * sections are mapped or unmapped into an address
935 * space
936 *
937 * @listener: an object containing the callbacks to be called
938 * @filter: if non-%NULL, only regions in this address space will be observed
939 */
940 void memory_listener_register(MemoryListener *listener, AddressSpace *filter);
941
942 /**
943 * memory_listener_unregister: undo the effect of memory_listener_register()
944 *
945 * @listener: an object containing the callbacks to be removed
946 */
947 void memory_listener_unregister(MemoryListener *listener);
948
949 /**
950 * memory_global_dirty_log_start: begin dirty logging for all regions
951 */
952 void memory_global_dirty_log_start(void);
953
954 /**
955 * memory_global_dirty_log_stop: end dirty logging for all regions
956 */
957 void memory_global_dirty_log_stop(void);
958
959 void mtree_info(fprintf_function mon_printf, void *f);
960
961 /**
962 * address_space_init: initializes an address space
963 *
964 * @as: an uninitialized #AddressSpace
965 * @root: a #MemoryRegion that routes addesses for the address space
966 * @name: an address space name. The name is only used for debugging
967 * output.
968 */
969 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
970
971
972 /**
973 * address_space_destroy: destroy an address space
974 *
975 * Releases all resources associated with an address space. After an address space
976 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
977 * as well.
978 *
979 * @as: address space to be destroyed
980 */
981 void address_space_destroy(AddressSpace *as);
982
983 /**
984 * address_space_rw: read from or write to an address space.
985 *
986 * Return true if the operation hit any unassigned memory or encountered an
987 * IOMMU fault.
988 *
989 * @as: #AddressSpace to be accessed
990 * @addr: address within that address space
991 * @buf: buffer with the data transferred
992 * @is_write: indicates the transfer direction
993 */
994 bool address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
995 int len, bool is_write);
996
997 /**
998 * address_space_write: write to address space.
999 *
1000 * Return true if the operation hit any unassigned memory or encountered an
1001 * IOMMU fault.
1002 *
1003 * @as: #AddressSpace to be accessed
1004 * @addr: address within that address space
1005 * @buf: buffer with the data transferred
1006 */
1007 bool address_space_write(AddressSpace *as, hwaddr addr,
1008 const uint8_t *buf, int len);
1009
1010 /**
1011 * address_space_read: read from an address space.
1012 *
1013 * Return true if the operation hit any unassigned memory or encountered an
1014 * IOMMU fault.
1015 *
1016 * @as: #AddressSpace to be accessed
1017 * @addr: address within that address space
1018 * @buf: buffer with the data transferred
1019 */
1020 bool address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len);
1021
1022 /* address_space_translate: translate an address range into an address space
1023 * into a MemoryRegion and an address range into that section
1024 *
1025 * @as: #AddressSpace to be accessed
1026 * @addr: address within that address space
1027 * @xlat: pointer to address within the returned memory region section's
1028 * #MemoryRegion.
1029 * @len: pointer to length
1030 * @is_write: indicates the transfer direction
1031 */
1032 MemoryRegion *address_space_translate(AddressSpace *as, hwaddr addr,
1033 hwaddr *xlat, hwaddr *len,
1034 bool is_write);
1035
1036 /* address_space_access_valid: check for validity of accessing an address
1037 * space range
1038 *
1039 * Check whether memory is assigned to the given address space range, and
1040 * access is permitted by any IOMMU regions that are active for the address
1041 * space.
1042 *
1043 * For now, addr and len should be aligned to a page size. This limitation
1044 * will be lifted in the future.
1045 *
1046 * @as: #AddressSpace to be accessed
1047 * @addr: address within that address space
1048 * @len: length of the area to be checked
1049 * @is_write: indicates the transfer direction
1050 */
1051 bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write);
1052
1053 /* address_space_map: map a physical memory region into a host virtual address
1054 *
1055 * May map a subset of the requested range, given by and returned in @plen.
1056 * May return %NULL if resources needed to perform the mapping are exhausted.
1057 * Use only for reads OR writes - not for read-modify-write operations.
1058 * Use cpu_register_map_client() to know when retrying the map operation is
1059 * likely to succeed.
1060 *
1061 * @as: #AddressSpace to be accessed
1062 * @addr: address within that address space
1063 * @plen: pointer to length of buffer; updated on return
1064 * @is_write: indicates the transfer direction
1065 */
1066 void *address_space_map(AddressSpace *as, hwaddr addr,
1067 hwaddr *plen, bool is_write);
1068
1069 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1070 *
1071 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1072 * the amount of memory that was actually read or written by the caller.
1073 *
1074 * @as: #AddressSpace used
1075 * @addr: address within that address space
1076 * @len: buffer length as returned by address_space_map()
1077 * @access_len: amount of data actually transferred
1078 * @is_write: indicates the transfer direction
1079 */
1080 void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
1081 int is_write, hwaddr access_len);
1082
1083
1084 #endif
1085
1086 #endif