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
;
32 /* Must match *_DIRTY_FLAGS in cpu-all.h. To be replaced with dynamic
35 #define DIRTY_MEMORY_VGA 0
36 #define DIRTY_MEMORY_CODE 1
37 #define DIRTY_MEMORY_MIGRATION 3
40 * Memory region callbacks
42 struct MemoryRegionOps
{
43 /* Read from the memory region. @addr is relative to @mr; @size is
45 uint64_t (*read
)(void *opaque
,
46 target_phys_addr_t addr
,
48 /* Write to the memory region. @addr is relative to @mr; @size is
50 void (*write
)(void *opaque
,
51 target_phys_addr_t addr
,
55 enum device_endian endianness
;
56 /* Guest-visible constraints: */
58 /* If nonzero, specify bounds on access sizes beyond which a machine
61 unsigned min_access_size
;
62 unsigned max_access_size
;
63 /* If true, unaligned accesses are supported. Otherwise unaligned
64 * accesses throw machine checks.
68 /* Internal implementation constraints: */
70 /* If nonzero, specifies the minimum size implemented. Smaller sizes
71 * will be rounded upwards and a partial result will be returned.
73 unsigned min_access_size
;
74 /* If nonzero, specifies the maximum size implemented. Larger sizes
75 * will be done as a series of accesses with smaller sizes.
77 unsigned max_access_size
;
78 /* If true, unaligned accesses are supported. Otherwise all accesses
79 * are converted to (possibly multiple) naturally aligned accesses.
84 /* If .read and .write are not present, old_portio may be used for
85 * backwards compatibility with old portio registration
87 const MemoryRegionPortio
*old_portio
;
90 typedef struct CoalescedMemoryRange CoalescedMemoryRange
;
93 /* All fields are private - violators will be prosecuted */
94 const MemoryRegionOps
*ops
;
98 target_phys_addr_t addr
;
99 target_phys_addr_t offset
;
100 bool backend_registered
;
105 target_phys_addr_t alias_offset
;
108 QTAILQ_HEAD(subregions
, MemoryRegion
) subregions
;
109 QTAILQ_ENTRY(MemoryRegion
) subregions_link
;
110 QTAILQ_HEAD(coalesced_ranges
, CoalescedMemoryRange
) coalesced
;
112 uint8_t dirty_log_mask
;
115 struct MemoryRegionPortio
{
119 IOPortReadFunc
*read
;
120 IOPortWriteFunc
*write
;
123 #define PORTIO_END { }
126 * memory_region_init: Initialize a memory region
128 * The region typically acts as a container for other memory regions. Us
129 * memory_region_add_subregion() to add subregions.
131 * @mr: the #MemoryRegion to be initialized
132 * @name: used for debugging; not visible to the user or ABI
133 * @size: size of the region; any subregions beyond this size will be clipped
135 void memory_region_init(MemoryRegion
*mr
,
139 * memory_region_init_io: Initialize an I/O memory region.
141 * Accesses into the region will be cause the callbacks in @ops to be called.
142 * if @size is nonzero, subregions will be clipped to @size.
144 * @mr: the #MemoryRegion to be initialized.
145 * @ops: a structure containing read and write callbacks to be used when
146 * I/O is performed on the region.
147 * @opaque: passed to to the read and write callbacks of the @ops structure.
148 * @name: used for debugging; not visible to the user or ABI
149 * @size: size of the region.
151 void memory_region_init_io(MemoryRegion
*mr
,
152 const MemoryRegionOps
*ops
,
158 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
159 * region will be modify memory directly.
161 * @mr: the #MemoryRegion to be initialized.
162 * @dev: a device associated with the region; may be %NULL.
163 * @name: the name of the region; the pair (@dev, @name) must be globally
164 * unique. The name is part of the save/restore ABI and so cannot be
166 * @size: size of the region.
168 void memory_region_init_ram(MemoryRegion
*mr
,
169 DeviceState
*dev
, /* FIXME: layering violation */
174 * memory_region_init_ram: Initialize RAM memory region from a user-provided.
175 * pointer. Accesses into the region will be modify
178 * @mr: the #MemoryRegion to be initialized.
179 * @dev: a device associated with the region; may be %NULL.
180 * @name: the name of the region; the pair (@dev, @name) must be globally
181 * unique. The name is part of the save/restore ABI and so cannot be
183 * @size: size of the region.
184 * @ptr: memory to be mapped; must contain at least @size bytes.
186 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
187 DeviceState
*dev
, /* FIXME: layering violation */
193 * memory_region_init_alias: Initialize a memory region that aliases all or a
194 * part of another memory region.
196 * @mr: the #MemoryRegion to be initialized.
197 * @name: used for debugging; not visible to the user or ABI
198 * @orig: the region to be referenced; @mr will be equivalent to
199 * @orig between @offset and @offset + @size - 1.
200 * @offset: start of the section in @orig to be referenced.
201 * @size: size of the region.
203 void memory_region_init_alias(MemoryRegion
*mr
,
206 target_phys_addr_t offset
,
209 * memory_region_destroy: Destroy a memory region and relaim all resources.
211 * @mr: the region to be destroyed. May not currently be a subregion
212 * (see memory_region_add_subregion()) or referenced in an alias
213 * (see memory_region_init_alias()).
215 void memory_region_destroy(MemoryRegion
*mr
);
218 * memory_region_size: get a memory region's size.
220 * @mr: the memory region being queried.
222 uint64_t memory_region_size(MemoryRegion
*mr
);
225 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
227 * Returns a host pointer to a RAM memory region (created with
228 * memory_region_init_ram() or memory_region_init_ram_ptr()). Use with
231 * @mr: the memory region being queried.
233 void *memory_region_get_ram_ptr(MemoryRegion
*mr
);
236 * memory_region_set_offset: Sets an offset to be added to MemoryRegionOps
239 * This function is deprecated and should not be used in new code.
241 void memory_region_set_offset(MemoryRegion
*mr
, target_phys_addr_t offset
);
244 * memory_region_set_log: Turn dirty logging on or off for a region.
246 * Turns dirty logging on or off for a specified client (display, migration).
247 * Only meaningful for RAM regions.
249 * @mr: the memory region being updated.
250 * @log: whether dirty logging is to be enabled or disabled.
251 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
254 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
);
257 * memory_region_get_dirty: Check whether a page is dirty for a specified
260 * Checks whether a page has been written to since the last
261 * call to memory_region_reset_dirty() with the same @client. Dirty logging
264 * @mr: the memory region being queried.
265 * @addr: the address (relative to the start of the region) being queried.
266 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
269 bool memory_region_get_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
273 * memory_region_set_dirty: Mark a page as dirty in a memory region.
275 * Marks a page as dirty, after it has been dirtied outside guest code.
277 * @mr: the memory region being queried.
278 * @addr: the address (relative to the start of the region) being dirtied.
280 void memory_region_set_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
);
283 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
284 * any external TLBs (e.g. kvm)
286 * Flushes dirty information from accelerators such as kvm and vhost-net
287 * and makes it available to users of the memory API.
289 * @mr: the region being flushed.
291 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
);
294 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
297 * Marks a range of pages as no longer dirty.
299 * @mr: the region being updated.
300 * @addr: the start of the subrange being cleaned.
301 * @size: the size of the subrange being cleaned.
302 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
305 void memory_region_reset_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
306 target_phys_addr_t size
, unsigned client
);
309 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
311 * Allows a memory region to be marked as read-only (turning it into a ROM).
312 * only useful on RAM regions.
314 * @mr: the region being updated.
315 * @readonly: whether rhe region is to be ROM or RAM.
317 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
);
320 * memory_region_set_coalescing: Enable memory coalescing for the region.
322 * Enabled writes to a region to be queued for later processing. MMIO ->write
323 * callbacks may be delayed until a non-coalesced MMIO is issued.
324 * Only useful for IO regions. Roughly similar to write-combining hardware.
326 * @mr: the memory region to be write coalesced
328 void memory_region_set_coalescing(MemoryRegion
*mr
);
331 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
334 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
335 * Multiple calls can be issued coalesced disjoint ranges.
337 * @mr: the memory region to be updated.
338 * @offset: the start of the range within the region to be coalesced.
339 * @size: the size of the subrange to be coalesced.
341 void memory_region_add_coalescing(MemoryRegion
*mr
,
342 target_phys_addr_t offset
,
346 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
348 * Disables any coalescing caused by memory_region_set_coalescing() or
349 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
352 * @mr: the memory region to be updated.
354 void memory_region_clear_coalescing(MemoryRegion
*mr
);
357 * memory_region_add_subregion: Add a sub-region to a container.
359 * Adds a sub-region at @offset. The sub-region may not overlap with other
360 * subregions (except for those explicitly marked as overlapping). A region
361 * may only be added once as a subregion (unless removed with
362 * memory_region_del_subregion()); use memory_region_init_alias() if you
363 * want a region to be a subregion in multiple locations.
365 * @mr: the region to contain the new subregion; must be a container
366 * initialized with memory_region_init().
367 * @offset: the offset relative to @mr where @subregion is added.
368 * @subregion: the subregion to be added.
370 void memory_region_add_subregion(MemoryRegion
*mr
,
371 target_phys_addr_t offset
,
372 MemoryRegion
*subregion
);
374 * memory_region_add_subregion: Add a sub-region to a container, with overlap.
376 * Adds a sub-region at @offset. The sub-region may overlap with other
377 * subregions. Conflicts are resolved by having a higher @priority hide a
378 * lower @priority. Subregions without priority are taken as @priority 0.
379 * A region may only be added once as a subregion (unless removed with
380 * memory_region_del_subregion()); use memory_region_init_alias() if you
381 * want a region to be a subregion in multiple locations.
383 * @mr: the region to contain the new subregion; must be a container
384 * initialized with memory_region_init().
385 * @offset: the offset relative to @mr where @subregion is added.
386 * @subregion: the subregion to be added.
387 * @priority: used for resolving overlaps; highest priority wins.
389 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
390 target_phys_addr_t offset
,
391 MemoryRegion
*subregion
,
394 * memory_region_del_subregion: Remove a subregion.
396 * Removes a subregion from its container.
398 * @mr: the container to be updated.
399 * @subregion: the region being removed; must be a current subregion of @mr.
401 void memory_region_del_subregion(MemoryRegion
*mr
,
402 MemoryRegion
*subregion
);