2 * Physical memory management API
4 * Copyright 2011 Red Hat, Inc. and/or its affiliates
7 * Avi Kivity <avi@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
17 #ifndef CONFIG_USER_ONLY
21 #include "qemu-common.h"
22 #include "cpu-common.h"
24 #include "qemu-queue.h"
28 typedef struct MemoryRegionOps MemoryRegionOps
;
29 typedef struct MemoryRegion MemoryRegion
;
30 typedef struct MemoryRegionPortio MemoryRegionPortio
;
31 typedef struct MemoryRegionMmio MemoryRegionMmio
;
33 /* Must match *_DIRTY_FLAGS in cpu-all.h. To be replaced with dynamic
36 #define DIRTY_MEMORY_VGA 0
37 #define DIRTY_MEMORY_CODE 1
38 #define DIRTY_MEMORY_MIGRATION 3
40 struct MemoryRegionMmio
{
41 CPUReadMemoryFunc
*read
[3];
42 CPUWriteMemoryFunc
*write
[3];
46 * Memory region callbacks
48 struct MemoryRegionOps
{
49 /* Read from the memory region. @addr is relative to @mr; @size is
51 uint64_t (*read
)(void *opaque
,
52 target_phys_addr_t addr
,
54 /* Write to the memory region. @addr is relative to @mr; @size is
56 void (*write
)(void *opaque
,
57 target_phys_addr_t addr
,
61 enum device_endian endianness
;
62 /* Guest-visible constraints: */
64 /* If nonzero, specify bounds on access sizes beyond which a machine
67 unsigned min_access_size
;
68 unsigned max_access_size
;
69 /* If true, unaligned accesses are supported. Otherwise unaligned
70 * accesses throw machine checks.
74 /* Internal implementation constraints: */
76 /* If nonzero, specifies the minimum size implemented. Smaller sizes
77 * will be rounded upwards and a partial result will be returned.
79 unsigned min_access_size
;
80 /* If nonzero, specifies the maximum size implemented. Larger sizes
81 * will be done as a series of accesses with smaller sizes.
83 unsigned max_access_size
;
84 /* If true, unaligned accesses are supported. Otherwise all accesses
85 * are converted to (possibly multiple) naturally aligned accesses.
90 /* If .read and .write are not present, old_portio may be used for
91 * backwards compatibility with old portio registration
93 const MemoryRegionPortio
*old_portio
;
94 /* If .read and .write are not present, old_mmio may be used for
95 * backwards compatibility with old mmio registration
97 const MemoryRegionMmio old_mmio
;
100 typedef struct CoalescedMemoryRange CoalescedMemoryRange
;
101 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd
;
103 struct MemoryRegion
{
104 /* All fields are private - violators will be prosecuted */
105 const MemoryRegionOps
*ops
;
107 MemoryRegion
*parent
;
109 target_phys_addr_t addr
;
110 target_phys_addr_t offset
;
111 bool backend_registered
;
112 void (*destructor
)(MemoryRegion
*mr
);
117 bool readonly
; /* For RAM regions */
119 target_phys_addr_t alias_offset
;
122 QTAILQ_HEAD(subregions
, MemoryRegion
) subregions
;
123 QTAILQ_ENTRY(MemoryRegion
) subregions_link
;
124 QTAILQ_HEAD(coalesced_ranges
, CoalescedMemoryRange
) coalesced
;
126 uint8_t dirty_log_mask
;
127 unsigned ioeventfd_nb
;
128 MemoryRegionIoeventfd
*ioeventfds
;
131 struct MemoryRegionPortio
{
135 IOPortReadFunc
*read
;
136 IOPortWriteFunc
*write
;
139 #define PORTIO_END_OF_LIST() { }
142 * memory_region_init: Initialize a memory region
144 * The region typically acts as a container for other memory regions. Us
145 * memory_region_add_subregion() to add subregions.
147 * @mr: the #MemoryRegion to be initialized
148 * @name: used for debugging; not visible to the user or ABI
149 * @size: size of the region; any subregions beyond this size will be clipped
151 void memory_region_init(MemoryRegion
*mr
,
155 * memory_region_init_io: Initialize an I/O memory region.
157 * Accesses into the region will be cause the callbacks in @ops to be called.
158 * if @size is nonzero, subregions will be clipped to @size.
160 * @mr: the #MemoryRegion to be initialized.
161 * @ops: a structure containing read and write callbacks to be used when
162 * I/O is performed on the region.
163 * @opaque: passed to to the read and write callbacks of the @ops structure.
164 * @name: used for debugging; not visible to the user or ABI
165 * @size: size of the region.
167 void memory_region_init_io(MemoryRegion
*mr
,
168 const MemoryRegionOps
*ops
,
174 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
175 * region will be modify memory directly.
177 * @mr: the #MemoryRegion to be initialized.
178 * @dev: a device associated with the region; may be %NULL.
179 * @name: the name of the region; the pair (@dev, @name) must be globally
180 * unique. The name is part of the save/restore ABI and so cannot be
182 * @size: size of the region.
184 void memory_region_init_ram(MemoryRegion
*mr
,
185 DeviceState
*dev
, /* FIXME: layering violation */
190 * memory_region_init_ram: Initialize RAM memory region from a user-provided.
191 * pointer. Accesses into the region will be modify
194 * @mr: the #MemoryRegion to be initialized.
195 * @dev: a device associated with the region; may be %NULL.
196 * @name: the name of the region; the pair (@dev, @name) must be globally
197 * unique. The name is part of the save/restore ABI and so cannot be
199 * @size: size of the region.
200 * @ptr: memory to be mapped; must contain at least @size bytes.
202 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
203 DeviceState
*dev
, /* FIXME: layering violation */
209 * memory_region_init_alias: Initialize a memory region that aliases all or a
210 * part of another memory region.
212 * @mr: the #MemoryRegion to be initialized.
213 * @name: used for debugging; not visible to the user or ABI
214 * @orig: the region to be referenced; @mr will be equivalent to
215 * @orig between @offset and @offset + @size - 1.
216 * @offset: start of the section in @orig to be referenced.
217 * @size: size of the region.
219 void memory_region_init_alias(MemoryRegion
*mr
,
222 target_phys_addr_t offset
,
226 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
227 * handled via callbacks.
229 * @mr: the #MemoryRegion to be initialized.
230 * @ops: callbacks for write access handling.
231 * @dev: a device associated with the region; may be %NULL.
232 * @name: the name of the region; the pair (@dev, @name) must be globally
233 * unique. The name is part of the save/restore ABI and so cannot be
235 * @size: size of the region.
237 void memory_region_init_rom_device(MemoryRegion
*mr
,
238 const MemoryRegionOps
*ops
,
240 DeviceState
*dev
, /* FIXME: layering violation */
245 * memory_region_destroy: Destroy a memory region and relaim all resources.
247 * @mr: the region to be destroyed. May not currently be a subregion
248 * (see memory_region_add_subregion()) or referenced in an alias
249 * (see memory_region_init_alias()).
251 void memory_region_destroy(MemoryRegion
*mr
);
254 * memory_region_size: get a memory region's size.
256 * @mr: the memory region being queried.
258 uint64_t memory_region_size(MemoryRegion
*mr
);
261 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
263 * Returns a host pointer to a RAM memory region (created with
264 * memory_region_init_ram() or memory_region_init_ram_ptr()). Use with
267 * @mr: the memory region being queried.
269 void *memory_region_get_ram_ptr(MemoryRegion
*mr
);
272 * memory_region_set_offset: Sets an offset to be added to MemoryRegionOps
275 * This function is deprecated and should not be used in new code.
277 void memory_region_set_offset(MemoryRegion
*mr
, target_phys_addr_t offset
);
280 * memory_region_set_log: Turn dirty logging on or off for a region.
282 * Turns dirty logging on or off for a specified client (display, migration).
283 * Only meaningful for RAM regions.
285 * @mr: the memory region being updated.
286 * @log: whether dirty logging is to be enabled or disabled.
287 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
290 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
);
293 * memory_region_get_dirty: Check whether a page is dirty for a specified
296 * Checks whether a page has been written to since the last
297 * call to memory_region_reset_dirty() with the same @client. Dirty logging
300 * @mr: the memory region being queried.
301 * @addr: the address (relative to the start of the region) being queried.
302 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
305 bool memory_region_get_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
309 * memory_region_set_dirty: Mark a page as dirty in a memory region.
311 * Marks a page as dirty, after it has been dirtied outside guest code.
313 * @mr: the memory region being queried.
314 * @addr: the address (relative to the start of the region) being dirtied.
316 void memory_region_set_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
);
319 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
320 * any external TLBs (e.g. kvm)
322 * Flushes dirty information from accelerators such as kvm and vhost-net
323 * and makes it available to users of the memory API.
325 * @mr: the region being flushed.
327 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
);
330 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
333 * Marks a range of pages as no longer dirty.
335 * @mr: the region being updated.
336 * @addr: the start of the subrange being cleaned.
337 * @size: the size of the subrange being cleaned.
338 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
341 void memory_region_reset_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
342 target_phys_addr_t size
, unsigned client
);
345 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
347 * Allows a memory region to be marked as read-only (turning it into a ROM).
348 * only useful on RAM regions.
350 * @mr: the region being updated.
351 * @readonly: whether rhe region is to be ROM or RAM.
353 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
);
356 * memory_region_rom_device_set_readable: enable/disable ROM readability
358 * Allows a ROM device (initialized with memory_region_init_rom_device() to
359 * to be marked as readable (default) or not readable. When it is readable,
360 * the device is mapped to guest memory. When not readable, reads are
361 * forwarded to the #MemoryRegion.read function.
363 * @mr: the memory region to be updated
364 * @readable: whether reads are satisified directly (%true) or via callbacks
367 void memory_region_rom_device_set_readable(MemoryRegion
*mr
, bool readable
);
370 * memory_region_set_coalescing: Enable memory coalescing for the region.
372 * Enabled writes to a region to be queued for later processing. MMIO ->write
373 * callbacks may be delayed until a non-coalesced MMIO is issued.
374 * Only useful for IO regions. Roughly similar to write-combining hardware.
376 * @mr: the memory region to be write coalesced
378 void memory_region_set_coalescing(MemoryRegion
*mr
);
381 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
384 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
385 * Multiple calls can be issued coalesced disjoint ranges.
387 * @mr: the memory region to be updated.
388 * @offset: the start of the range within the region to be coalesced.
389 * @size: the size of the subrange to be coalesced.
391 void memory_region_add_coalescing(MemoryRegion
*mr
,
392 target_phys_addr_t offset
,
396 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
398 * Disables any coalescing caused by memory_region_set_coalescing() or
399 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
402 * @mr: the memory region to be updated.
404 void memory_region_clear_coalescing(MemoryRegion
*mr
);
407 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
408 * is written to a location.
410 * Marks a word in an IO region (initialized with memory_region_init_io())
411 * as a trigger for an eventfd event. The I/O callback will not be called.
412 * The caller must be prepared to handle failure (hat is, take the required
413 * action if the callback _is_ called).
415 * @mr: the memory region being updated.
416 * @addr: the address within @mr that is to be monitored
417 * @size: the size of the access to trigger the eventfd
418 * @match_data: whether to match against @data, instead of just @addr
419 * @data: the data to match against the guest write
420 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
422 void memory_region_add_eventfd(MemoryRegion
*mr
,
423 target_phys_addr_t addr
,
430 * memory_region_del_eventfd: Cancel and eventfd.
432 * Cancels an eventfd trigger request by a previous memory_region_add_eventfd()
435 * @mr: the memory region being updated.
436 * @addr: the address within @mr that is to be monitored
437 * @size: the size of the access to trigger the eventfd
438 * @match_data: whether to match against @data, instead of just @addr
439 * @data: the data to match against the guest write
440 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
442 void memory_region_del_eventfd(MemoryRegion
*mr
,
443 target_phys_addr_t addr
,
449 * memory_region_add_subregion: Add a sub-region to a container.
451 * Adds a sub-region at @offset. The sub-region may not overlap with other
452 * subregions (except for those explicitly marked as overlapping). A region
453 * may only be added once as a subregion (unless removed with
454 * memory_region_del_subregion()); use memory_region_init_alias() if you
455 * want a region to be a subregion in multiple locations.
457 * @mr: the region to contain the new subregion; must be a container
458 * initialized with memory_region_init().
459 * @offset: the offset relative to @mr where @subregion is added.
460 * @subregion: the subregion to be added.
462 void memory_region_add_subregion(MemoryRegion
*mr
,
463 target_phys_addr_t offset
,
464 MemoryRegion
*subregion
);
466 * memory_region_add_subregion: Add a sub-region to a container, with overlap.
468 * Adds a sub-region at @offset. The sub-region may overlap with other
469 * subregions. Conflicts are resolved by having a higher @priority hide a
470 * lower @priority. Subregions without priority are taken as @priority 0.
471 * A region may only be added once as a subregion (unless removed with
472 * memory_region_del_subregion()); use memory_region_init_alias() if you
473 * want a region to be a subregion in multiple locations.
475 * @mr: the region to contain the new subregion; must be a container
476 * initialized with memory_region_init().
477 * @offset: the offset relative to @mr where @subregion is added.
478 * @subregion: the subregion to be added.
479 * @priority: used for resolving overlaps; highest priority wins.
481 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
482 target_phys_addr_t offset
,
483 MemoryRegion
*subregion
,
486 * memory_region_del_subregion: Remove a subregion.
488 * Removes a subregion from its container.
490 * @mr: the container to be updated.
491 * @subregion: the region being removed; must be a current subregion of @mr.
493 void memory_region_del_subregion(MemoryRegion
*mr
,
494 MemoryRegion
*subregion
);
496 /* Start a transaction; changes will be accumulated and made visible only
497 * when the transaction ends.
499 void memory_region_transaction_begin(void);
500 /* Commit a transaction and make changes visible to the guest.
502 void memory_region_transaction_commit(void);
504 void mtree_info(fprintf_function mon_printf
, void *f
);