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"
26 typedef struct MemoryRegionOps MemoryRegionOps
;
27 typedef struct MemoryRegion MemoryRegion
;
29 /* Must match *_DIRTY_FLAGS in cpu-all.h. To be replaced with dynamic
32 #define DIRTY_MEMORY_VGA 0
33 #define DIRTY_MEMORY_CODE 1
34 #define DIRTY_MEMORY_MIGRATION 3
37 * Memory region callbacks
39 struct MemoryRegionOps
{
40 /* Read from the memory region. @addr is relative to @mr; @size is
42 uint64_t (*read
)(void *opaque
,
43 target_phys_addr_t addr
,
45 /* Write to the memory region. @addr is relative to @mr; @size is
47 void (*write
)(void *opaque
,
48 target_phys_addr_t addr
,
52 enum device_endian endianness
;
53 /* Guest-visible constraints: */
55 /* If nonzero, specify bounds on access sizes beyond which a machine
58 unsigned min_access_size
;
59 unsigned max_access_size
;
60 /* If true, unaligned accesses are supported. Otherwise unaligned
61 * accesses throw machine checks.
65 /* Internal implementation constraints: */
67 /* If nonzero, specifies the minimum size implemented. Smaller sizes
68 * will be rounded upwards and a partial result will be returned.
70 unsigned min_access_size
;
71 /* If nonzero, specifies the maximum size implemented. Larger sizes
72 * will be done as a series of accesses with smaller sizes.
74 unsigned max_access_size
;
75 /* If true, unaligned accesses are supported. Otherwise all accesses
76 * are converted to (possibly multiple) naturally aligned accesses.
82 typedef struct CoalescedMemoryRange CoalescedMemoryRange
;
85 /* All fields are private - violators will be prosecuted */
86 const MemoryRegionOps
*ops
;
90 target_phys_addr_t addr
;
91 target_phys_addr_t offset
;
92 bool backend_registered
;
96 target_phys_addr_t alias_offset
;
99 QTAILQ_HEAD(subregions
, MemoryRegion
) subregions
;
100 QTAILQ_ENTRY(MemoryRegion
) subregions_link
;
101 QTAILQ_HEAD(coalesced_ranges
, CoalescedMemoryRange
) coalesced
;
103 uint8_t dirty_log_mask
;
107 * memory_region_init: Initialize a memory region
109 * The region typically acts as a container for other memory regions. Us
110 * memory_region_add_subregion() to add subregions.
112 * @mr: the #MemoryRegion to be initialized
113 * @name: used for debugging; not visible to the user or ABI
114 * @size: size of the region; any subregions beyond this size will be clipped
116 void memory_region_init(MemoryRegion
*mr
,
120 * memory_region_init_io: Initialize an I/O memory region.
122 * Accesses into the region will be cause the callbacks in @ops to be called.
123 * if @size is nonzero, subregions will be clipped to @size.
125 * @mr: the #MemoryRegion to be initialized.
126 * @ops: a structure containing read and write callbacks to be used when
127 * I/O is performed on the region.
128 * @opaque: passed to to the read and write callbacks of the @ops structure.
129 * @name: used for debugging; not visible to the user or ABI
130 * @size: size of the region.
132 void memory_region_init_io(MemoryRegion
*mr
,
133 const MemoryRegionOps
*ops
,
139 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
140 * region will be modify memory directly.
142 * @mr: the #MemoryRegion to be initialized.
143 * @dev: a device associated with the region; may be %NULL.
144 * @name: the name of the region; the pair (@dev, @name) must be globally
145 * unique. The name is part of the save/restore ABI and so cannot be
147 * @size: size of the region.
149 void memory_region_init_ram(MemoryRegion
*mr
,
150 DeviceState
*dev
, /* FIXME: layering violation */
155 * memory_region_init_ram: Initialize RAM memory region from a user-provided.
156 * pointer. Accesses into the region will be modify
159 * @mr: the #MemoryRegion to be initialized.
160 * @dev: a device associated with the region; may be %NULL.
161 * @name: the name of the region; the pair (@dev, @name) must be globally
162 * unique. The name is part of the save/restore ABI and so cannot be
164 * @size: size of the region.
165 * @ptr: memory to be mapped; must contain at least @size bytes.
167 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
168 DeviceState
*dev
, /* FIXME: layering violation */
174 * memory_region_init_alias: Initialize a memory region that aliases all or a
175 * part of another memory region.
177 * @mr: the #MemoryRegion to be initialized.
178 * @name: used for debugging; not visible to the user or ABI
179 * @orig: the region to be referenced; @mr will be equivalent to
180 * @orig between @offset and @offset + @size - 1.
181 * @offset: start of the section in @orig to be referenced.
182 * @size: size of the region.
184 void memory_region_init_alias(MemoryRegion
*mr
,
187 target_phys_addr_t offset
,
190 * memory_region_destroy: Destroy a memory region and relaim all resources.
192 * @mr: the region to be destroyed. May not currently be a subregion
193 * (see memory_region_add_subregion()) or referenced in an alias
194 * (see memory_region_init_alias()).
196 void memory_region_destroy(MemoryRegion
*mr
);
199 * memory_region_size: get a memory region's size.
201 * @mr: the memory region being queried.
203 uint64_t memory_region_size(MemoryRegion
*mr
);
206 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
208 * Returns a host pointer to a RAM memory region (created with
209 * memory_region_init_ram() or memory_region_init_ram_ptr()). Use with
212 * @mr: the memory region being queried.
214 void *memory_region_get_ram_ptr(MemoryRegion
*mr
);
217 * memory_region_set_offset: Sets an offset to be added to MemoryRegionOps
220 * This function is deprecated and should not be used in new code.
222 void memory_region_set_offset(MemoryRegion
*mr
, target_phys_addr_t offset
);
225 * memory_region_set_log: Turn dirty logging on or off for a region.
227 * Turns dirty logging on or off for a specified client (display, migration).
228 * Only meaningful for RAM regions.
230 * @mr: the memory region being updated.
231 * @log: whether dirty logging is to be enabled or disabled.
232 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
235 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
);
238 * memory_region_get_dirty: Check whether a page is dirty for a specified
241 * Checks whether a page has been written to since the last
242 * call to memory_region_reset_dirty() with the same @client. Dirty logging
245 * @mr: the memory region being queried.
246 * @addr: the address (relative to the start of the region) being queried.
247 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
250 bool memory_region_get_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
254 * memory_region_set_dirty: Mark a page as dirty in a memory region.
256 * Marks a page as dirty, after it has been dirtied outside guest code.
258 * @mr: the memory region being queried.
259 * @addr: the address (relative to the start of the region) being dirtied.
261 void memory_region_set_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
);
264 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
265 * any external TLBs (e.g. kvm)
267 * Flushes dirty information from accelerators such as kvm and vhost-net
268 * and makes it available to users of the memory API.
270 * @mr: the region being flushed.
272 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
);
275 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
278 * Marks a range of pages as no longer dirty.
280 * @mr: the region being updated.
281 * @addr: the start of the subrange being cleaned.
282 * @size: the size of the subrange being cleaned.
283 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
286 void memory_region_reset_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
287 target_phys_addr_t size
, unsigned client
);
290 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
292 * Allows a memory region to be marked as read-only (turning it into a ROM).
293 * only useful on RAM regions.
295 * @mr: the region being updated.
296 * @readonly: whether rhe region is to be ROM or RAM.
298 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
);
301 * memory_region_set_coalescing: Enable memory coalescing for the region.
303 * Enabled writes to a region to be queued for later processing. MMIO ->write
304 * callbacks may be delayed until a non-coalesced MMIO is issued.
305 * Only useful for IO regions. Roughly similar to write-combining hardware.
307 * @mr: the memory region to be write coalesced
309 void memory_region_set_coalescing(MemoryRegion
*mr
);
312 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
315 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
316 * Multiple calls can be issued coalesced disjoint ranges.
318 * @mr: the memory region to be updated.
319 * @offset: the start of the range within the region to be coalesced.
320 * @size: the size of the subrange to be coalesced.
322 void memory_region_add_coalescing(MemoryRegion
*mr
,
323 target_phys_addr_t offset
,
327 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
329 * Disables any coalescing caused by memory_region_set_coalescing() or
330 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
333 * @mr: the memory region to be updated.
335 void memory_region_clear_coalescing(MemoryRegion
*mr
);
338 * memory_region_add_subregion: Add a sub-region to a container.
340 * Adds a sub-region at @offset. The sub-region may not overlap with other
341 * subregions (except for those explicitly marked as overlapping). A region
342 * may only be added once as a subregion (unless removed with
343 * memory_region_del_subregion()); use memory_region_init_alias() if you
344 * want a region to be a subregion in multiple locations.
346 * @mr: the region to contain the new subregion; must be a container
347 * initialized with memory_region_init().
348 * @offset: the offset relative to @mr where @subregion is added.
349 * @subregion: the subregion to be added.
351 void memory_region_add_subregion(MemoryRegion
*mr
,
352 target_phys_addr_t offset
,
353 MemoryRegion
*subregion
);
355 * memory_region_add_subregion: Add a sub-region to a container, with overlap.
357 * Adds a sub-region at @offset. The sub-region may overlap with other
358 * subregions. Conflicts are resolved by having a higher @priority hide a
359 * lower @priority. Subregions without priority are taken as @priority 0.
360 * A region may only be added once as a subregion (unless removed with
361 * memory_region_del_subregion()); use memory_region_init_alias() if you
362 * want a region to be a subregion in multiple locations.
364 * @mr: the region to contain the new subregion; must be a container
365 * initialized with memory_region_init().
366 * @offset: the offset relative to @mr where @subregion is added.
367 * @subregion: the subregion to be added.
368 * @priority: used for resolving overlaps; highest priority wins.
370 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
371 target_phys_addr_t offset
,
372 MemoryRegion
*subregion
,
375 * memory_region_del_subregion: Remove a subregion.
377 * Removes a subregion from its container.
379 * @mr: the container to be updated.
380 * @subregion: the region being removed; must be a current subregion of @mr.
382 void memory_region_del_subregion(MemoryRegion
*mr
,
383 MemoryRegion
*subregion
);