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"
27 typedef struct MemoryRegionOps MemoryRegionOps
;
28 typedef struct MemoryRegion MemoryRegion
;
30 /* Must match *_DIRTY_FLAGS in cpu-all.h. To be replaced with dynamic
33 #define DIRTY_MEMORY_VGA 0
34 #define DIRTY_MEMORY_CODE 1
35 #define DIRTY_MEMORY_MIGRATION 3
38 * Memory region callbacks
40 struct MemoryRegionOps
{
41 /* Read from the memory region. @addr is relative to @mr; @size is
43 uint64_t (*read
)(void *opaque
,
44 target_phys_addr_t addr
,
46 /* Write to the memory region. @addr is relative to @mr; @size is
48 void (*write
)(void *opaque
,
49 target_phys_addr_t addr
,
53 enum device_endian endianness
;
54 /* Guest-visible constraints: */
56 /* If nonzero, specify bounds on access sizes beyond which a machine
59 unsigned min_access_size
;
60 unsigned max_access_size
;
61 /* If true, unaligned accesses are supported. Otherwise unaligned
62 * accesses throw machine checks.
66 /* Internal implementation constraints: */
68 /* If nonzero, specifies the minimum size implemented. Smaller sizes
69 * will be rounded upwards and a partial result will be returned.
71 unsigned min_access_size
;
72 /* If nonzero, specifies the maximum size implemented. Larger sizes
73 * will be done as a series of accesses with smaller sizes.
75 unsigned max_access_size
;
76 /* If true, unaligned accesses are supported. Otherwise all accesses
77 * are converted to (possibly multiple) naturally aligned accesses.
83 typedef struct CoalescedMemoryRange CoalescedMemoryRange
;
86 /* All fields are private - violators will be prosecuted */
87 const MemoryRegionOps
*ops
;
91 target_phys_addr_t addr
;
92 target_phys_addr_t offset
;
93 bool backend_registered
;
98 target_phys_addr_t alias_offset
;
101 QTAILQ_HEAD(subregions
, MemoryRegion
) subregions
;
102 QTAILQ_ENTRY(MemoryRegion
) subregions_link
;
103 QTAILQ_HEAD(coalesced_ranges
, CoalescedMemoryRange
) coalesced
;
105 uint8_t dirty_log_mask
;
109 * memory_region_init: Initialize a memory region
111 * The region typically acts as a container for other memory regions. Us
112 * memory_region_add_subregion() to add subregions.
114 * @mr: the #MemoryRegion to be initialized
115 * @name: used for debugging; not visible to the user or ABI
116 * @size: size of the region; any subregions beyond this size will be clipped
118 void memory_region_init(MemoryRegion
*mr
,
122 * memory_region_init_io: Initialize an I/O memory region.
124 * Accesses into the region will be cause the callbacks in @ops to be called.
125 * if @size is nonzero, subregions will be clipped to @size.
127 * @mr: the #MemoryRegion to be initialized.
128 * @ops: a structure containing read and write callbacks to be used when
129 * I/O is performed on the region.
130 * @opaque: passed to to the read and write callbacks of the @ops structure.
131 * @name: used for debugging; not visible to the user or ABI
132 * @size: size of the region.
134 void memory_region_init_io(MemoryRegion
*mr
,
135 const MemoryRegionOps
*ops
,
141 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
142 * region will be modify memory directly.
144 * @mr: the #MemoryRegion to be initialized.
145 * @dev: a device associated with the region; may be %NULL.
146 * @name: the name of the region; the pair (@dev, @name) must be globally
147 * unique. The name is part of the save/restore ABI and so cannot be
149 * @size: size of the region.
151 void memory_region_init_ram(MemoryRegion
*mr
,
152 DeviceState
*dev
, /* FIXME: layering violation */
157 * memory_region_init_ram: Initialize RAM memory region from a user-provided.
158 * pointer. Accesses into the region will be modify
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.
167 * @ptr: memory to be mapped; must contain at least @size bytes.
169 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
170 DeviceState
*dev
, /* FIXME: layering violation */
176 * memory_region_init_alias: Initialize a memory region that aliases all or a
177 * part of another memory region.
179 * @mr: the #MemoryRegion to be initialized.
180 * @name: used for debugging; not visible to the user or ABI
181 * @orig: the region to be referenced; @mr will be equivalent to
182 * @orig between @offset and @offset + @size - 1.
183 * @offset: start of the section in @orig to be referenced.
184 * @size: size of the region.
186 void memory_region_init_alias(MemoryRegion
*mr
,
189 target_phys_addr_t offset
,
192 * memory_region_destroy: Destroy a memory region and relaim all resources.
194 * @mr: the region to be destroyed. May not currently be a subregion
195 * (see memory_region_add_subregion()) or referenced in an alias
196 * (see memory_region_init_alias()).
198 void memory_region_destroy(MemoryRegion
*mr
);
201 * memory_region_size: get a memory region's size.
203 * @mr: the memory region being queried.
205 uint64_t memory_region_size(MemoryRegion
*mr
);
208 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
210 * Returns a host pointer to a RAM memory region (created with
211 * memory_region_init_ram() or memory_region_init_ram_ptr()). Use with
214 * @mr: the memory region being queried.
216 void *memory_region_get_ram_ptr(MemoryRegion
*mr
);
219 * memory_region_set_offset: Sets an offset to be added to MemoryRegionOps
222 * This function is deprecated and should not be used in new code.
224 void memory_region_set_offset(MemoryRegion
*mr
, target_phys_addr_t offset
);
227 * memory_region_set_log: Turn dirty logging on or off for a region.
229 * Turns dirty logging on or off for a specified client (display, migration).
230 * Only meaningful for RAM regions.
232 * @mr: the memory region being updated.
233 * @log: whether dirty logging is to be enabled or disabled.
234 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
237 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
);
240 * memory_region_get_dirty: Check whether a page is dirty for a specified
243 * Checks whether a page has been written to since the last
244 * call to memory_region_reset_dirty() with the same @client. Dirty logging
247 * @mr: the memory region being queried.
248 * @addr: the address (relative to the start of the region) being queried.
249 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
252 bool memory_region_get_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
256 * memory_region_set_dirty: Mark a page as dirty in a memory region.
258 * Marks a page as dirty, after it has been dirtied outside guest code.
260 * @mr: the memory region being queried.
261 * @addr: the address (relative to the start of the region) being dirtied.
263 void memory_region_set_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
);
266 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
267 * any external TLBs (e.g. kvm)
269 * Flushes dirty information from accelerators such as kvm and vhost-net
270 * and makes it available to users of the memory API.
272 * @mr: the region being flushed.
274 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
);
277 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
280 * Marks a range of pages as no longer dirty.
282 * @mr: the region being updated.
283 * @addr: the start of the subrange being cleaned.
284 * @size: the size of the subrange being cleaned.
285 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
288 void memory_region_reset_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
289 target_phys_addr_t size
, unsigned client
);
292 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
294 * Allows a memory region to be marked as read-only (turning it into a ROM).
295 * only useful on RAM regions.
297 * @mr: the region being updated.
298 * @readonly: whether rhe region is to be ROM or RAM.
300 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
);
303 * memory_region_set_coalescing: Enable memory coalescing for the region.
305 * Enabled writes to a region to be queued for later processing. MMIO ->write
306 * callbacks may be delayed until a non-coalesced MMIO is issued.
307 * Only useful for IO regions. Roughly similar to write-combining hardware.
309 * @mr: the memory region to be write coalesced
311 void memory_region_set_coalescing(MemoryRegion
*mr
);
314 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
317 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
318 * Multiple calls can be issued coalesced disjoint ranges.
320 * @mr: the memory region to be updated.
321 * @offset: the start of the range within the region to be coalesced.
322 * @size: the size of the subrange to be coalesced.
324 void memory_region_add_coalescing(MemoryRegion
*mr
,
325 target_phys_addr_t offset
,
329 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
331 * Disables any coalescing caused by memory_region_set_coalescing() or
332 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
335 * @mr: the memory region to be updated.
337 void memory_region_clear_coalescing(MemoryRegion
*mr
);
340 * memory_region_add_subregion: Add a sub-region to a container.
342 * Adds a sub-region at @offset. The sub-region may not overlap with other
343 * subregions (except for those explicitly marked as overlapping). A region
344 * may only be added once as a subregion (unless removed with
345 * memory_region_del_subregion()); use memory_region_init_alias() if you
346 * want a region to be a subregion in multiple locations.
348 * @mr: the region to contain the new subregion; must be a container
349 * initialized with memory_region_init().
350 * @offset: the offset relative to @mr where @subregion is added.
351 * @subregion: the subregion to be added.
353 void memory_region_add_subregion(MemoryRegion
*mr
,
354 target_phys_addr_t offset
,
355 MemoryRegion
*subregion
);
357 * memory_region_add_subregion: Add a sub-region to a container, with overlap.
359 * Adds a sub-region at @offset. The sub-region may overlap with other
360 * subregions. Conflicts are resolved by having a higher @priority hide a
361 * lower @priority. Subregions without priority are taken as @priority 0.
362 * A region may only be added once as a subregion (unless removed with
363 * memory_region_del_subregion()); use memory_region_init_alias() if you
364 * want a region to be a subregion in multiple locations.
366 * @mr: the region to contain the new subregion; must be a container
367 * initialized with memory_region_init().
368 * @offset: the offset relative to @mr where @subregion is added.
369 * @subregion: the subregion to be added.
370 * @priority: used for resolving overlaps; highest priority wins.
372 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
373 target_phys_addr_t offset
,
374 MemoryRegion
*subregion
,
377 * memory_region_del_subregion: Remove a subregion.
379 * Removes a subregion from its container.
381 * @mr: the container to be updated.
382 * @subregion: the region being removed; must be a current subregion of @mr.
384 void memory_region_del_subregion(MemoryRegion
*mr
,
385 MemoryRegion
*subregion
);