blob: 1d99cee0197232a5e13dae8a48a038d89c8afab1 [file] [log] [blame]
Avi Kivity093bc2c2011-07-26 14:26:01 +03001/*
2 * Physical memory management API
3 *
4 * Copyright 2011 Red Hat, Inc. and/or its affiliates
5 *
6 * Authors:
7 * Avi Kivity <avi@redhat.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
14#ifndef MEMORY_H
15#define MEMORY_H
16
17#ifndef CONFIG_USER_ONLY
18
19#include <stdint.h>
20#include <stdbool.h>
21#include "qemu-common.h"
22#include "cpu-common.h"
23#include "targphys.h"
24#include "qemu-queue.h"
Avi Kivity658b2222011-07-26 14:26:08 +030025#include "iorange.h"
Avi Kivity627a0e92011-07-26 14:26:09 +030026#include "ioport.h"
Avi Kivity08dafab2011-10-16 13:19:17 +020027#include "int128.h"
Avi Kivity093bc2c2011-07-26 14:26:01 +030028
29typedef struct MemoryRegionOps MemoryRegionOps;
30typedef struct MemoryRegion MemoryRegion;
Avi Kivity627a0e92011-07-26 14:26:09 +030031typedef struct MemoryRegionPortio MemoryRegionPortio;
Avi Kivity74901c32011-07-26 14:26:10 +030032typedef struct MemoryRegionMmio MemoryRegionMmio;
Avi Kivity093bc2c2011-07-26 14:26:01 +030033
34/* Must match *_DIRTY_FLAGS in cpu-all.h. To be replaced with dynamic
35 * registration.
36 */
37#define DIRTY_MEMORY_VGA 0
38#define DIRTY_MEMORY_CODE 1
39#define DIRTY_MEMORY_MIGRATION 3
40
Avi Kivity74901c32011-07-26 14:26:10 +030041struct MemoryRegionMmio {
42 CPUReadMemoryFunc *read[3];
43 CPUWriteMemoryFunc *write[3];
44};
45
Avi Kivity093bc2c2011-07-26 14:26:01 +030046/*
47 * Memory region callbacks
48 */
49struct MemoryRegionOps {
50 /* Read from the memory region. @addr is relative to @mr; @size is
51 * in bytes. */
52 uint64_t (*read)(void *opaque,
53 target_phys_addr_t addr,
54 unsigned size);
55 /* Write to the memory region. @addr is relative to @mr; @size is
56 * in bytes. */
57 void (*write)(void *opaque,
58 target_phys_addr_t addr,
59 uint64_t data,
60 unsigned size);
61
62 enum device_endian endianness;
63 /* Guest-visible constraints: */
64 struct {
65 /* If nonzero, specify bounds on access sizes beyond which a machine
66 * check is thrown.
67 */
68 unsigned min_access_size;
69 unsigned max_access_size;
70 /* If true, unaligned accesses are supported. Otherwise unaligned
71 * accesses throw machine checks.
72 */
73 bool unaligned;
Avi Kivity897fa7c2011-11-13 13:05:27 +020074 /*
75 * If present, and returns #false, the transaction is not accepted
76 * by the device (and results in machine dependent behaviour such
77 * as a machine check exception).
78 */
79 bool (*accepts)(void *opaque, target_phys_addr_t addr,
80 unsigned size, bool is_write);
Avi Kivity093bc2c2011-07-26 14:26:01 +030081 } valid;
82 /* Internal implementation constraints: */
83 struct {
84 /* If nonzero, specifies the minimum size implemented. Smaller sizes
85 * will be rounded upwards and a partial result will be returned.
86 */
87 unsigned min_access_size;
88 /* If nonzero, specifies the maximum size implemented. Larger sizes
89 * will be done as a series of accesses with smaller sizes.
90 */
91 unsigned max_access_size;
92 /* If true, unaligned accesses are supported. Otherwise all accesses
93 * are converted to (possibly multiple) naturally aligned accesses.
94 */
95 bool unaligned;
96 } impl;
Avi Kivity627a0e92011-07-26 14:26:09 +030097
98 /* If .read and .write are not present, old_portio may be used for
99 * backwards compatibility with old portio registration
100 */
101 const MemoryRegionPortio *old_portio;
Avi Kivity74901c32011-07-26 14:26:10 +0300102 /* If .read and .write are not present, old_mmio may be used for
103 * backwards compatibility with old mmio registration
104 */
105 const MemoryRegionMmio old_mmio;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300106};
107
108typedef struct CoalescedMemoryRange CoalescedMemoryRange;
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300109typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300110
111struct MemoryRegion {
112 /* All fields are private - violators will be prosecuted */
113 const MemoryRegionOps *ops;
114 void *opaque;
115 MemoryRegion *parent;
Avi Kivity08dafab2011-10-16 13:19:17 +0200116 Int128 size;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300117 target_phys_addr_t addr;
Avi Kivity545e92e2011-08-08 19:58:48 +0300118 void (*destructor)(MemoryRegion *mr);
Avi Kivity093bc2c2011-07-26 14:26:01 +0300119 ram_addr_t ram_addr;
Avi Kivity658b2222011-07-26 14:26:08 +0300120 IORange iorange;
Avi Kivityb3b00c72012-01-02 13:20:11 +0200121 bool subpage;
Avi Kivity14a3c102011-07-26 14:26:06 +0300122 bool terminates;
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300123 bool readable;
Avi Kivity8ea92522011-12-08 15:58:43 +0200124 bool ram;
Avi Kivityfb1cd6f2011-09-25 14:48:47 +0300125 bool readonly; /* For RAM regions */
Avi Kivity6bba19b2011-09-14 11:54:58 +0300126 bool enabled;
Avi Kivity75c578d2012-01-02 15:40:52 +0200127 bool rom_device;
Jan Kiszka1660e722011-10-23 16:01:19 +0200128 bool warning_printed; /* For reservations */
Avi Kivity093bc2c2011-07-26 14:26:01 +0300129 MemoryRegion *alias;
130 target_phys_addr_t alias_offset;
131 unsigned priority;
132 bool may_overlap;
133 QTAILQ_HEAD(subregions, MemoryRegion) subregions;
134 QTAILQ_ENTRY(MemoryRegion) subregions_link;
135 QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced;
136 const char *name;
Avi Kivity5a583342011-07-26 14:26:02 +0300137 uint8_t dirty_log_mask;
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300138 unsigned ioeventfd_nb;
139 MemoryRegionIoeventfd *ioeventfds;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300140};
141
Avi Kivity627a0e92011-07-26 14:26:09 +0300142struct MemoryRegionPortio {
143 uint32_t offset;
144 uint32_t len;
145 unsigned size;
146 IOPortReadFunc *read;
147 IOPortWriteFunc *write;
148};
149
Avi Kivity2dd30222011-08-08 16:08:54 +0300150#define PORTIO_END_OF_LIST() { }
Avi Kivity627a0e92011-07-26 14:26:09 +0300151
Avi Kivitye2177952011-12-08 15:00:18 +0200152typedef struct MemoryRegionSection MemoryRegionSection;
153
154/**
155 * MemoryRegionSection: describes a fragment of a #MemoryRegion
156 *
157 * @mr: the region, or %NULL if empty
Avi Kivity7664e802011-12-11 14:47:25 +0200158 * @address_space: the address space the region is mapped in
Avi Kivitye2177952011-12-08 15:00:18 +0200159 * @offset_within_region: the beginning of the section, relative to @mr's start
160 * @size: the size of the section; will not exceed @mr's boundaries
161 * @offset_within_address_space: the address of the first byte of the section
162 * relative to the region's address space
Avi Kivity7a8499e2012-02-08 17:01:23 +0200163 * @readonly: writes to this section are ignored
Avi Kivitye2177952011-12-08 15:00:18 +0200164 */
165struct MemoryRegionSection {
166 MemoryRegion *mr;
Avi Kivity7664e802011-12-11 14:47:25 +0200167 MemoryRegion *address_space;
Avi Kivitye2177952011-12-08 15:00:18 +0200168 target_phys_addr_t offset_within_region;
169 uint64_t size;
170 target_phys_addr_t offset_within_address_space;
Avi Kivity7a8499e2012-02-08 17:01:23 +0200171 bool readonly;
Avi Kivitye2177952011-12-08 15:00:18 +0200172};
173
Avi Kivity7664e802011-12-11 14:47:25 +0200174typedef struct MemoryListener MemoryListener;
175
176/**
177 * MemoryListener: callbacks structure for updates to the physical memory map
178 *
179 * Allows a component to adjust to changes in the guest-visible memory map.
180 * Use with memory_listener_register() and memory_listener_unregister().
181 */
182struct MemoryListener {
183 void (*region_add)(MemoryListener *listener, MemoryRegionSection *section);
184 void (*region_del)(MemoryListener *listener, MemoryRegionSection *section);
185 void (*log_start)(MemoryListener *listener, MemoryRegionSection *section);
186 void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section);
187 void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section);
188 void (*log_global_start)(MemoryListener *listener);
189 void (*log_global_stop)(MemoryListener *listener);
Avi Kivity80a1ea32012-02-08 16:39:06 +0200190 void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section,
191 bool match_data, uint64_t data, int fd);
192 void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section,
193 bool match_data, uint64_t data, int fd);
Avi Kivity72e22d22012-02-08 15:05:50 +0200194 /* Lower = earlier (during add), later (during del) */
195 unsigned priority;
196 QTAILQ_ENTRY(MemoryListener) link;
Avi Kivity7664e802011-12-11 14:47:25 +0200197};
198
Avi Kivity093bc2c2011-07-26 14:26:01 +0300199/**
200 * memory_region_init: Initialize a memory region
201 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300202 * The region typically acts as a container for other memory regions. Use
Avi Kivity093bc2c2011-07-26 14:26:01 +0300203 * memory_region_add_subregion() to add subregions.
204 *
205 * @mr: the #MemoryRegion to be initialized
206 * @name: used for debugging; not visible to the user or ABI
207 * @size: size of the region; any subregions beyond this size will be clipped
208 */
209void memory_region_init(MemoryRegion *mr,
210 const char *name,
211 uint64_t size);
212/**
213 * memory_region_init_io: Initialize an I/O memory region.
214 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300215 * Accesses into the region will cause the callbacks in @ops to be called.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300216 * if @size is nonzero, subregions will be clipped to @size.
217 *
218 * @mr: the #MemoryRegion to be initialized.
219 * @ops: a structure containing read and write callbacks to be used when
220 * I/O is performed on the region.
221 * @opaque: passed to to the read and write callbacks of the @ops structure.
222 * @name: used for debugging; not visible to the user or ABI
223 * @size: size of the region.
224 */
225void memory_region_init_io(MemoryRegion *mr,
226 const MemoryRegionOps *ops,
227 void *opaque,
228 const char *name,
229 uint64_t size);
230
231/**
232 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300233 * region will modify memory directly.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300234 *
235 * @mr: the #MemoryRegion to be initialized.
Avi Kivityc5705a72011-12-20 15:59:12 +0200236 * @name: the name of the region.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300237 * @size: size of the region.
238 */
239void memory_region_init_ram(MemoryRegion *mr,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300240 const char *name,
241 uint64_t size);
242
243/**
244 * memory_region_init_ram: Initialize RAM memory region from a user-provided.
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300245 * pointer. Accesses into the region will modify
Avi Kivity093bc2c2011-07-26 14:26:01 +0300246 * memory directly.
247 *
248 * @mr: the #MemoryRegion to be initialized.
Avi Kivityc5705a72011-12-20 15:59:12 +0200249 * @name: the name of the region.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300250 * @size: size of the region.
251 * @ptr: memory to be mapped; must contain at least @size bytes.
252 */
253void memory_region_init_ram_ptr(MemoryRegion *mr,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300254 const char *name,
255 uint64_t size,
256 void *ptr);
257
258/**
259 * memory_region_init_alias: Initialize a memory region that aliases all or a
260 * part of another memory region.
261 *
262 * @mr: the #MemoryRegion to be initialized.
263 * @name: used for debugging; not visible to the user or ABI
264 * @orig: the region to be referenced; @mr will be equivalent to
265 * @orig between @offset and @offset + @size - 1.
266 * @offset: start of the section in @orig to be referenced.
267 * @size: size of the region.
268 */
269void memory_region_init_alias(MemoryRegion *mr,
270 const char *name,
271 MemoryRegion *orig,
272 target_phys_addr_t offset,
273 uint64_t size);
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300274
275/**
276 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
277 * handled via callbacks.
278 *
279 * @mr: the #MemoryRegion to be initialized.
280 * @ops: callbacks for write access handling.
Avi Kivityc5705a72011-12-20 15:59:12 +0200281 * @name: the name of the region.
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300282 * @size: size of the region.
283 */
284void memory_region_init_rom_device(MemoryRegion *mr,
285 const MemoryRegionOps *ops,
Avi Kivity75f59412011-08-26 00:35:15 +0300286 void *opaque,
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300287 const char *name,
288 uint64_t size);
289
Avi Kivity093bc2c2011-07-26 14:26:01 +0300290/**
Jan Kiszka1660e722011-10-23 16:01:19 +0200291 * memory_region_init_reservation: Initialize a memory region that reserves
292 * I/O space.
293 *
294 * A reservation region primariy serves debugging purposes. It claims I/O
295 * space that is not supposed to be handled by QEMU itself. Any access via
296 * the memory API will cause an abort().
297 *
298 * @mr: the #MemoryRegion to be initialized
299 * @name: used for debugging; not visible to the user or ABI
300 * @size: size of the region.
301 */
302void memory_region_init_reservation(MemoryRegion *mr,
303 const char *name,
304 uint64_t size);
305/**
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300306 * memory_region_destroy: Destroy a memory region and reclaim all resources.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300307 *
308 * @mr: the region to be destroyed. May not currently be a subregion
309 * (see memory_region_add_subregion()) or referenced in an alias
310 * (see memory_region_init_alias()).
311 */
312void memory_region_destroy(MemoryRegion *mr);
313
314/**
315 * memory_region_size: get a memory region's size.
316 *
317 * @mr: the memory region being queried.
318 */
319uint64_t memory_region_size(MemoryRegion *mr);
320
321/**
Avi Kivity8ea92522011-12-08 15:58:43 +0200322 * memory_region_is_ram: check whether a memory region is random access
323 *
324 * Returns %true is a memory region is random access.
325 *
326 * @mr: the memory region being queried
327 */
328bool memory_region_is_ram(MemoryRegion *mr);
329
330/**
Avi Kivity8991c792011-12-20 15:53:11 +0200331 * memory_region_name: get a memory region's name
332 *
333 * Returns the string that was used to initialize the memory region.
334 *
335 * @mr: the memory region being queried
336 */
337const char *memory_region_name(MemoryRegion *mr);
338
339/**
Avi Kivity55043ba2011-12-15 17:20:34 +0200340 * memory_region_is_logging: return whether a memory region is logging writes
341 *
342 * Returns %true if the memory region is logging writes
343 *
344 * @mr: the memory region being queried
345 */
346bool memory_region_is_logging(MemoryRegion *mr);
347
348/**
Avi Kivityce7923d2011-12-08 16:05:11 +0200349 * memory_region_is_rom: check whether a memory region is ROM
350 *
351 * Returns %true is a memory region is read-only memory.
352 *
353 * @mr: the memory region being queried
354 */
355bool memory_region_is_rom(MemoryRegion *mr);
356
357/**
Avi Kivity093bc2c2011-07-26 14:26:01 +0300358 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
359 *
360 * Returns a host pointer to a RAM memory region (created with
361 * memory_region_init_ram() or memory_region_init_ram_ptr()). Use with
362 * care.
363 *
364 * @mr: the memory region being queried.
365 */
366void *memory_region_get_ram_ptr(MemoryRegion *mr);
367
368/**
Avi Kivity093bc2c2011-07-26 14:26:01 +0300369 * memory_region_set_log: Turn dirty logging on or off for a region.
370 *
371 * Turns dirty logging on or off for a specified client (display, migration).
372 * Only meaningful for RAM regions.
373 *
374 * @mr: the memory region being updated.
375 * @log: whether dirty logging is to be enabled or disabled.
376 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
377 * %DIRTY_MEMORY_VGA.
378 */
379void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
380
381/**
Blue Swirlcd7a45c2012-01-22 16:38:21 +0000382 * memory_region_get_dirty: Check whether a range of bytes is dirty
383 * for a specified client.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300384 *
Blue Swirlcd7a45c2012-01-22 16:38:21 +0000385 * Checks whether a range of bytes has been written to since the last
Avi Kivity093bc2c2011-07-26 14:26:01 +0300386 * call to memory_region_reset_dirty() with the same @client. Dirty logging
387 * must be enabled.
388 *
389 * @mr: the memory region being queried.
390 * @addr: the address (relative to the start of the region) being queried.
Blue Swirlcd7a45c2012-01-22 16:38:21 +0000391 * @size: the size of the range being queried.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300392 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
393 * %DIRTY_MEMORY_VGA.
394 */
395bool memory_region_get_dirty(MemoryRegion *mr, target_phys_addr_t addr,
Blue Swirlcd7a45c2012-01-22 16:38:21 +0000396 target_phys_addr_t size, unsigned client);
Avi Kivity093bc2c2011-07-26 14:26:01 +0300397
398/**
Blue Swirlfd4aa972011-10-16 16:04:59 +0000399 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300400 *
Blue Swirlfd4aa972011-10-16 16:04:59 +0000401 * Marks a range of bytes as dirty, after it has been dirtied outside
402 * guest code.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300403 *
Blue Swirlfd4aa972011-10-16 16:04:59 +0000404 * @mr: the memory region being dirtied.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300405 * @addr: the address (relative to the start of the region) being dirtied.
Blue Swirlfd4aa972011-10-16 16:04:59 +0000406 * @size: size of the range being dirtied.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300407 */
Blue Swirlfd4aa972011-10-16 16:04:59 +0000408void memory_region_set_dirty(MemoryRegion *mr, target_phys_addr_t addr,
409 target_phys_addr_t size);
Avi Kivity093bc2c2011-07-26 14:26:01 +0300410
411/**
412 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
413 * any external TLBs (e.g. kvm)
414 *
415 * Flushes dirty information from accelerators such as kvm and vhost-net
416 * and makes it available to users of the memory API.
417 *
418 * @mr: the region being flushed.
419 */
420void memory_region_sync_dirty_bitmap(MemoryRegion *mr);
421
422/**
423 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
424 * client.
425 *
426 * Marks a range of pages as no longer dirty.
427 *
428 * @mr: the region being updated.
429 * @addr: the start of the subrange being cleaned.
430 * @size: the size of the subrange being cleaned.
431 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
432 * %DIRTY_MEMORY_VGA.
433 */
434void memory_region_reset_dirty(MemoryRegion *mr, target_phys_addr_t addr,
435 target_phys_addr_t size, unsigned client);
436
437/**
438 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
439 *
440 * Allows a memory region to be marked as read-only (turning it into a ROM).
441 * only useful on RAM regions.
442 *
443 * @mr: the region being updated.
444 * @readonly: whether rhe region is to be ROM or RAM.
445 */
446void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
447
448/**
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300449 * memory_region_rom_device_set_readable: enable/disable ROM readability
450 *
451 * Allows a ROM device (initialized with memory_region_init_rom_device() to
452 * to be marked as readable (default) or not readable. When it is readable,
453 * the device is mapped to guest memory. When not readable, reads are
454 * forwarded to the #MemoryRegion.read function.
455 *
456 * @mr: the memory region to be updated
457 * @readable: whether reads are satisified directly (%true) or via callbacks
458 * (%false)
459 */
460void memory_region_rom_device_set_readable(MemoryRegion *mr, bool readable);
461
462/**
Avi Kivity093bc2c2011-07-26 14:26:01 +0300463 * memory_region_set_coalescing: Enable memory coalescing for the region.
464 *
465 * Enabled writes to a region to be queued for later processing. MMIO ->write
466 * callbacks may be delayed until a non-coalesced MMIO is issued.
467 * Only useful for IO regions. Roughly similar to write-combining hardware.
468 *
469 * @mr: the memory region to be write coalesced
470 */
471void memory_region_set_coalescing(MemoryRegion *mr);
472
473/**
474 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
475 * a region.
476 *
477 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
478 * Multiple calls can be issued coalesced disjoint ranges.
479 *
480 * @mr: the memory region to be updated.
481 * @offset: the start of the range within the region to be coalesced.
482 * @size: the size of the subrange to be coalesced.
483 */
484void memory_region_add_coalescing(MemoryRegion *mr,
485 target_phys_addr_t offset,
486 uint64_t size);
487
488/**
489 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
490 *
491 * Disables any coalescing caused by memory_region_set_coalescing() or
492 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
493 * hardware.
494 *
495 * @mr: the memory region to be updated.
496 */
497void memory_region_clear_coalescing(MemoryRegion *mr);
498
499/**
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300500 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
501 * is written to a location.
502 *
503 * Marks a word in an IO region (initialized with memory_region_init_io())
504 * as a trigger for an eventfd event. The I/O callback will not be called.
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300505 * The caller must be prepared to handle failure (that is, take the required
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300506 * action if the callback _is_ called).
507 *
508 * @mr: the memory region being updated.
509 * @addr: the address within @mr that is to be monitored
510 * @size: the size of the access to trigger the eventfd
511 * @match_data: whether to match against @data, instead of just @addr
512 * @data: the data to match against the guest write
513 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
514 **/
515void memory_region_add_eventfd(MemoryRegion *mr,
516 target_phys_addr_t addr,
517 unsigned size,
518 bool match_data,
519 uint64_t data,
520 int fd);
521
522/**
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300523 * memory_region_del_eventfd: Cancel an eventfd.
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300524 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300525 * Cancels an eventfd trigger requested by a previous
526 * memory_region_add_eventfd() call.
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300527 *
528 * @mr: the memory region being updated.
529 * @addr: the address within @mr that is to be monitored
530 * @size: the size of the access to trigger the eventfd
531 * @match_data: whether to match against @data, instead of just @addr
532 * @data: the data to match against the guest write
533 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
534 */
535void memory_region_del_eventfd(MemoryRegion *mr,
536 target_phys_addr_t addr,
537 unsigned size,
538 bool match_data,
539 uint64_t data,
540 int fd);
541/**
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300542 * memory_region_add_subregion: Add a subregion to a container.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300543 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300544 * Adds a subregion at @offset. The subregion may not overlap with other
Avi Kivity093bc2c2011-07-26 14:26:01 +0300545 * subregions (except for those explicitly marked as overlapping). A region
546 * may only be added once as a subregion (unless removed with
547 * memory_region_del_subregion()); use memory_region_init_alias() if you
548 * want a region to be a subregion in multiple locations.
549 *
550 * @mr: the region to contain the new subregion; must be a container
551 * initialized with memory_region_init().
552 * @offset: the offset relative to @mr where @subregion is added.
553 * @subregion: the subregion to be added.
554 */
555void memory_region_add_subregion(MemoryRegion *mr,
556 target_phys_addr_t offset,
557 MemoryRegion *subregion);
558/**
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300559 * memory_region_add_subregion: Add a subregion to a container, with overlap.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300560 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300561 * Adds a subregion at @offset. The subregion may overlap with other
Avi Kivity093bc2c2011-07-26 14:26:01 +0300562 * subregions. Conflicts are resolved by having a higher @priority hide a
563 * lower @priority. Subregions without priority are taken as @priority 0.
564 * A region may only be added once as a subregion (unless removed with
565 * memory_region_del_subregion()); use memory_region_init_alias() if you
566 * want a region to be a subregion in multiple locations.
567 *
568 * @mr: the region to contain the new subregion; must be a container
569 * initialized with memory_region_init().
570 * @offset: the offset relative to @mr where @subregion is added.
571 * @subregion: the subregion to be added.
572 * @priority: used for resolving overlaps; highest priority wins.
573 */
574void memory_region_add_subregion_overlap(MemoryRegion *mr,
575 target_phys_addr_t offset,
576 MemoryRegion *subregion,
577 unsigned priority);
Avi Kivitye34911c2011-12-19 12:06:23 +0200578
579/**
580 * memory_region_get_ram_addr: Get the ram address associated with a memory
581 * region
582 *
Stefan Weildabdf392012-01-08 19:35:09 +0100583 * DO NOT USE THIS FUNCTION. This is a temporary workaround while the Xen
Avi Kivitye34911c2011-12-19 12:06:23 +0200584 * code is being reworked.
585 */
586ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr);
587
Avi Kivity093bc2c2011-07-26 14:26:01 +0300588/**
589 * memory_region_del_subregion: Remove a subregion.
590 *
591 * Removes a subregion from its container.
592 *
593 * @mr: the container to be updated.
594 * @subregion: the region being removed; must be a current subregion of @mr.
595 */
596void memory_region_del_subregion(MemoryRegion *mr,
597 MemoryRegion *subregion);
598
Avi Kivity6bba19b2011-09-14 11:54:58 +0300599/*
600 * memory_region_set_enabled: dynamically enable or disable a region
601 *
602 * Enables or disables a memory region. A disabled memory region
603 * ignores all accesses to itself and its subregions. It does not
604 * obscure sibling subregions with lower priority - it simply behaves as
605 * if it was removed from the hierarchy.
606 *
607 * Regions default to being enabled.
608 *
609 * @mr: the region to be updated
610 * @enabled: whether to enable or disable the region
611 */
612void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
613
Avi Kivity2282e1a2011-09-14 12:10:12 +0300614/*
615 * memory_region_set_address: dynamically update the address of a region
616 *
617 * Dynamically updates the address of a region, relative to its parent.
618 * May be used on regions are currently part of a memory hierarchy.
619 *
620 * @mr: the region to be updated
621 * @addr: new address, relative to parent region
622 */
623void memory_region_set_address(MemoryRegion *mr, target_phys_addr_t addr);
624
Avi Kivity47033592011-12-04 19:16:50 +0200625/*
626 * memory_region_set_alias_offset: dynamically update a memory alias's offset
627 *
628 * Dynamically updates the offset into the target region that an alias points
629 * to, as if the fourth argument to memory_region_init_alias() has changed.
630 *
631 * @mr: the #MemoryRegion to be updated; should be an alias.
632 * @offset: the new offset into the target memory region
633 */
634void memory_region_set_alias_offset(MemoryRegion *mr,
635 target_phys_addr_t offset);
636
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300637/**
Avi Kivitye2177952011-12-08 15:00:18 +0200638 * memory_region_find: locate a MemoryRegion in an address space
639 *
640 * Locates the first #MemoryRegion within an address space given by
641 * @address_space that overlaps the range given by @addr and @size.
642 *
643 * Returns a #MemoryRegionSection that describes a contiguous overlap.
644 * It will have the following characteristics:
645 * .@offset_within_address_space >= @addr
646 * .@offset_within_address_space + .@size <= @addr + @size
647 * .@size = 0 iff no overlap was found
648 * .@mr is non-%NULL iff an overlap was found
649 *
650 * @address_space: a top-level (i.e. parentless) region that contains
651 * the region to be found
652 * @addr: start of the area within @address_space to be searched
653 * @size: size of the area to be searched
654 */
655MemoryRegionSection memory_region_find(MemoryRegion *address_space,
656 target_phys_addr_t addr, uint64_t size);
657
Avi Kivity86e775c2011-12-15 16:24:49 +0200658
659/**
660 * memory_global_sync_dirty_bitmap: synchronize the dirty log for all memory
661 *
662 * Synchronizes the dirty page log for an entire address space.
663 * @address_space: a top-level (i.e. parentless) region that contains the
664 * memory being synchronized
665 */
666void memory_global_sync_dirty_bitmap(MemoryRegion *address_space);
667
Avi Kivitye2177952011-12-08 15:00:18 +0200668/**
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300669 * memory_region_transaction_begin: Start a transaction.
670 *
671 * During a transaction, changes will be accumulated and made visible
Stefan Weildabdf392012-01-08 19:35:09 +0100672 * only when the transaction ends (is committed).
Avi Kivity4ef4db82011-07-26 14:26:13 +0300673 */
674void memory_region_transaction_begin(void);
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300675
676/**
677 * memory_region_transaction_commit: Commit a transaction and make changes
678 * visible to the guest.
Avi Kivity4ef4db82011-07-26 14:26:13 +0300679 */
680void memory_region_transaction_commit(void);
681
Avi Kivity7664e802011-12-11 14:47:25 +0200682/**
683 * memory_listener_register: register callbacks to be called when memory
684 * sections are mapped or unmapped into an address
685 * space
686 *
687 * @listener: an object containing the callbacks to be called
688 */
689void memory_listener_register(MemoryListener *listener);
690
691/**
692 * memory_listener_unregister: undo the effect of memory_listener_register()
693 *
694 * @listener: an object containing the callbacks to be removed
695 */
696void memory_listener_unregister(MemoryListener *listener);
697
698/**
699 * memory_global_dirty_log_start: begin dirty logging for all regions
700 */
701void memory_global_dirty_log_start(void);
702
703/**
704 * memory_global_dirty_log_stop: begin dirty logging for all regions
705 */
706void memory_global_dirty_log_stop(void);
707
Blue Swirl314e2982011-09-11 20:22:05 +0000708void mtree_info(fprintf_function mon_printf, void *f);
709
Avi Kivity093bc2c2011-07-26 14:26:01 +0300710#endif
711
712#endif