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