]> git.proxmox.com Git - mirror_qemu.git/blame - memory.h
docs: document memory API interaction with migration
[mirror_qemu.git] / memory.h
CommitLineData
093bc2cd
AK
1/*
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"
658b2224 25#include "iorange.h"
627a0e90 26#include "ioport.h"
08dafab4 27#include "int128.h"
093bc2cd
AK
28
29typedef struct MemoryRegionOps MemoryRegionOps;
30typedef struct MemoryRegion MemoryRegion;
627a0e90 31typedef struct MemoryRegionPortio MemoryRegionPortio;
74901c3b 32typedef struct MemoryRegionMmio MemoryRegionMmio;
093bc2cd
AK
33
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
74901c3b
AK
41struct MemoryRegionMmio {
42 CPUReadMemoryFunc *read[3];
43 CPUWriteMemoryFunc *write[3];
44};
45
093bc2cd
AK
46/*
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;
897fa7cf
AK
74 /*
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);
093bc2cd
AK
81 } 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;
627a0e90
AK
97
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;
74901c3b
AK
102 /* 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;
093bc2cd
AK
106};
107
108typedef struct CoalescedMemoryRange CoalescedMemoryRange;
3e9d69e7 109typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
093bc2cd
AK
110
111struct MemoryRegion {
112 /* All fields are private - violators will be prosecuted */
113 const MemoryRegionOps *ops;
114 void *opaque;
115 MemoryRegion *parent;
08dafab4 116 Int128 size;
093bc2cd
AK
117 target_phys_addr_t addr;
118 target_phys_addr_t offset;
16ef61c9 119 bool backend_registered;
545e92e0 120 void (*destructor)(MemoryRegion *mr);
093bc2cd 121 ram_addr_t ram_addr;
658b2224 122 IORange iorange;
14a3c10a 123 bool terminates;
d0a9b5bc 124 bool readable;
fb1cd6f9 125 bool readonly; /* For RAM regions */
6bba19ba 126 bool enabled;
093bc2cd
AK
127 MemoryRegion *alias;
128 target_phys_addr_t alias_offset;
129 unsigned priority;
130 bool may_overlap;
131 QTAILQ_HEAD(subregions, MemoryRegion) subregions;
132 QTAILQ_ENTRY(MemoryRegion) subregions_link;
133 QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced;
134 const char *name;
5a583347 135 uint8_t dirty_log_mask;
3e9d69e7
AK
136 unsigned ioeventfd_nb;
137 MemoryRegionIoeventfd *ioeventfds;
093bc2cd
AK
138};
139
627a0e90
AK
140struct MemoryRegionPortio {
141 uint32_t offset;
142 uint32_t len;
143 unsigned size;
144 IOPortReadFunc *read;
145 IOPortWriteFunc *write;
146};
147
2dd30228 148#define PORTIO_END_OF_LIST() { }
627a0e90 149
093bc2cd
AK
150/**
151 * memory_region_init: Initialize a memory region
152 *
153 * The region typically acts as a container for other memory regions. Us
154 * memory_region_add_subregion() to add subregions.
155 *
156 * @mr: the #MemoryRegion to be initialized
157 * @name: used for debugging; not visible to the user or ABI
158 * @size: size of the region; any subregions beyond this size will be clipped
159 */
160void memory_region_init(MemoryRegion *mr,
161 const char *name,
162 uint64_t size);
163/**
164 * memory_region_init_io: Initialize an I/O memory region.
165 *
166 * Accesses into the region will be cause the callbacks in @ops to be called.
167 * if @size is nonzero, subregions will be clipped to @size.
168 *
169 * @mr: the #MemoryRegion to be initialized.
170 * @ops: a structure containing read and write callbacks to be used when
171 * I/O is performed on the region.
172 * @opaque: passed to to the read and write callbacks of the @ops structure.
173 * @name: used for debugging; not visible to the user or ABI
174 * @size: size of the region.
175 */
176void memory_region_init_io(MemoryRegion *mr,
177 const MemoryRegionOps *ops,
178 void *opaque,
179 const char *name,
180 uint64_t size);
181
182/**
183 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
184 * region will be modify memory directly.
185 *
186 * @mr: the #MemoryRegion to be initialized.
187 * @dev: a device associated with the region; may be %NULL.
188 * @name: the name of the region; the pair (@dev, @name) must be globally
189 * unique. The name is part of the save/restore ABI and so cannot be
190 * changed.
191 * @size: size of the region.
192 */
193void memory_region_init_ram(MemoryRegion *mr,
194 DeviceState *dev, /* FIXME: layering violation */
195 const char *name,
196 uint64_t size);
197
198/**
199 * memory_region_init_ram: Initialize RAM memory region from a user-provided.
200 * pointer. Accesses into the region will be modify
201 * memory directly.
202 *
203 * @mr: the #MemoryRegion to be initialized.
204 * @dev: a device associated with the region; may be %NULL.
205 * @name: the name of the region; the pair (@dev, @name) must be globally
206 * unique. The name is part of the save/restore ABI and so cannot be
207 * changed.
208 * @size: size of the region.
209 * @ptr: memory to be mapped; must contain at least @size bytes.
210 */
211void memory_region_init_ram_ptr(MemoryRegion *mr,
212 DeviceState *dev, /* FIXME: layering violation */
213 const char *name,
214 uint64_t size,
215 void *ptr);
216
217/**
218 * memory_region_init_alias: Initialize a memory region that aliases all or a
219 * part of another memory region.
220 *
221 * @mr: the #MemoryRegion to be initialized.
222 * @name: used for debugging; not visible to the user or ABI
223 * @orig: the region to be referenced; @mr will be equivalent to
224 * @orig between @offset and @offset + @size - 1.
225 * @offset: start of the section in @orig to be referenced.
226 * @size: size of the region.
227 */
228void memory_region_init_alias(MemoryRegion *mr,
229 const char *name,
230 MemoryRegion *orig,
231 target_phys_addr_t offset,
232 uint64_t size);
d0a9b5bc
AK
233
234/**
235 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
236 * handled via callbacks.
237 *
238 * @mr: the #MemoryRegion to be initialized.
239 * @ops: callbacks for write access handling.
240 * @dev: a device associated with the region; may be %NULL.
241 * @name: the name of the region; the pair (@dev, @name) must be globally
242 * unique. The name is part of the save/restore ABI and so cannot be
243 * changed.
244 * @size: size of the region.
245 */
246void memory_region_init_rom_device(MemoryRegion *mr,
247 const MemoryRegionOps *ops,
75f5941c 248 void *opaque,
d0a9b5bc
AK
249 DeviceState *dev, /* FIXME: layering violation */
250 const char *name,
251 uint64_t size);
252
093bc2cd
AK
253/**
254 * memory_region_destroy: Destroy a memory region and relaim all resources.
255 *
256 * @mr: the region to be destroyed. May not currently be a subregion
257 * (see memory_region_add_subregion()) or referenced in an alias
258 * (see memory_region_init_alias()).
259 */
260void memory_region_destroy(MemoryRegion *mr);
261
262/**
263 * memory_region_size: get a memory region's size.
264 *
265 * @mr: the memory region being queried.
266 */
267uint64_t memory_region_size(MemoryRegion *mr);
268
269/**
270 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
271 *
272 * Returns a host pointer to a RAM memory region (created with
273 * memory_region_init_ram() or memory_region_init_ram_ptr()). Use with
274 * care.
275 *
276 * @mr: the memory region being queried.
277 */
278void *memory_region_get_ram_ptr(MemoryRegion *mr);
279
280/**
281 * memory_region_set_offset: Sets an offset to be added to MemoryRegionOps
282 * callbacks.
283 *
284 * This function is deprecated and should not be used in new code.
285 */
286void memory_region_set_offset(MemoryRegion *mr, target_phys_addr_t offset);
287
288/**
289 * memory_region_set_log: Turn dirty logging on or off for a region.
290 *
291 * Turns dirty logging on or off for a specified client (display, migration).
292 * Only meaningful for RAM regions.
293 *
294 * @mr: the memory region being updated.
295 * @log: whether dirty logging is to be enabled or disabled.
296 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
297 * %DIRTY_MEMORY_VGA.
298 */
299void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
300
301/**
302 * memory_region_get_dirty: Check whether a page is dirty for a specified
303 * client.
304 *
305 * Checks whether a page has been written to since the last
306 * call to memory_region_reset_dirty() with the same @client. Dirty logging
307 * must be enabled.
308 *
309 * @mr: the memory region being queried.
310 * @addr: the address (relative to the start of the region) being queried.
311 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
312 * %DIRTY_MEMORY_VGA.
313 */
314bool memory_region_get_dirty(MemoryRegion *mr, target_phys_addr_t addr,
315 unsigned client);
316
317/**
318 * memory_region_set_dirty: Mark a page as dirty in a memory region.
319 *
320 * Marks a page as dirty, after it has been dirtied outside guest code.
321 *
322 * @mr: the memory region being queried.
323 * @addr: the address (relative to the start of the region) being dirtied.
324 */
325void memory_region_set_dirty(MemoryRegion *mr, target_phys_addr_t addr);
326
327/**
328 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
329 * any external TLBs (e.g. kvm)
330 *
331 * Flushes dirty information from accelerators such as kvm and vhost-net
332 * and makes it available to users of the memory API.
333 *
334 * @mr: the region being flushed.
335 */
336void memory_region_sync_dirty_bitmap(MemoryRegion *mr);
337
338/**
339 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
340 * client.
341 *
342 * Marks a range of pages as no longer dirty.
343 *
344 * @mr: the region being updated.
345 * @addr: the start of the subrange being cleaned.
346 * @size: the size of the subrange being cleaned.
347 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
348 * %DIRTY_MEMORY_VGA.
349 */
350void memory_region_reset_dirty(MemoryRegion *mr, target_phys_addr_t addr,
351 target_phys_addr_t size, unsigned client);
352
353/**
354 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
355 *
356 * Allows a memory region to be marked as read-only (turning it into a ROM).
357 * only useful on RAM regions.
358 *
359 * @mr: the region being updated.
360 * @readonly: whether rhe region is to be ROM or RAM.
361 */
362void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
363
d0a9b5bc
AK
364/**
365 * memory_region_rom_device_set_readable: enable/disable ROM readability
366 *
367 * Allows a ROM device (initialized with memory_region_init_rom_device() to
368 * to be marked as readable (default) or not readable. When it is readable,
369 * the device is mapped to guest memory. When not readable, reads are
370 * forwarded to the #MemoryRegion.read function.
371 *
372 * @mr: the memory region to be updated
373 * @readable: whether reads are satisified directly (%true) or via callbacks
374 * (%false)
375 */
376void memory_region_rom_device_set_readable(MemoryRegion *mr, bool readable);
377
093bc2cd
AK
378/**
379 * memory_region_set_coalescing: Enable memory coalescing for the region.
380 *
381 * Enabled writes to a region to be queued for later processing. MMIO ->write
382 * callbacks may be delayed until a non-coalesced MMIO is issued.
383 * Only useful for IO regions. Roughly similar to write-combining hardware.
384 *
385 * @mr: the memory region to be write coalesced
386 */
387void memory_region_set_coalescing(MemoryRegion *mr);
388
389/**
390 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
391 * a region.
392 *
393 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
394 * Multiple calls can be issued coalesced disjoint ranges.
395 *
396 * @mr: the memory region to be updated.
397 * @offset: the start of the range within the region to be coalesced.
398 * @size: the size of the subrange to be coalesced.
399 */
400void memory_region_add_coalescing(MemoryRegion *mr,
401 target_phys_addr_t offset,
402 uint64_t size);
403
404/**
405 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
406 *
407 * Disables any coalescing caused by memory_region_set_coalescing() or
408 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
409 * hardware.
410 *
411 * @mr: the memory region to be updated.
412 */
413void memory_region_clear_coalescing(MemoryRegion *mr);
414
3e9d69e7
AK
415/**
416 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
417 * is written to a location.
418 *
419 * Marks a word in an IO region (initialized with memory_region_init_io())
420 * as a trigger for an eventfd event. The I/O callback will not be called.
421 * The caller must be prepared to handle failure (hat is, take the required
422 * action if the callback _is_ called).
423 *
424 * @mr: the memory region being updated.
425 * @addr: the address within @mr that is to be monitored
426 * @size: the size of the access to trigger the eventfd
427 * @match_data: whether to match against @data, instead of just @addr
428 * @data: the data to match against the guest write
429 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
430 **/
431void memory_region_add_eventfd(MemoryRegion *mr,
432 target_phys_addr_t addr,
433 unsigned size,
434 bool match_data,
435 uint64_t data,
436 int fd);
437
438/**
439 * memory_region_del_eventfd: Cancel and eventfd.
440 *
441 * Cancels an eventfd trigger request by a previous memory_region_add_eventfd()
442 * call.
443 *
444 * @mr: the memory region being updated.
445 * @addr: the address within @mr that is to be monitored
446 * @size: the size of the access to trigger the eventfd
447 * @match_data: whether to match against @data, instead of just @addr
448 * @data: the data to match against the guest write
449 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
450 */
451void memory_region_del_eventfd(MemoryRegion *mr,
452 target_phys_addr_t addr,
453 unsigned size,
454 bool match_data,
455 uint64_t data,
456 int fd);
093bc2cd
AK
457/**
458 * memory_region_add_subregion: Add a sub-region to a container.
459 *
460 * Adds a sub-region at @offset. The sub-region may not overlap with other
461 * subregions (except for those explicitly marked as overlapping). A region
462 * may only be added once as a subregion (unless removed with
463 * memory_region_del_subregion()); use memory_region_init_alias() if you
464 * want a region to be a subregion in multiple locations.
465 *
466 * @mr: the region to contain the new subregion; must be a container
467 * initialized with memory_region_init().
468 * @offset: the offset relative to @mr where @subregion is added.
469 * @subregion: the subregion to be added.
470 */
471void memory_region_add_subregion(MemoryRegion *mr,
472 target_phys_addr_t offset,
473 MemoryRegion *subregion);
474/**
475 * memory_region_add_subregion: Add a sub-region to a container, with overlap.
476 *
477 * Adds a sub-region at @offset. The sub-region may overlap with other
478 * subregions. Conflicts are resolved by having a higher @priority hide a
479 * lower @priority. Subregions without priority are taken as @priority 0.
480 * A region may only be added once as a subregion (unless removed with
481 * memory_region_del_subregion()); use memory_region_init_alias() if you
482 * want a region to be a subregion in multiple locations.
483 *
484 * @mr: the region to contain the new subregion; must be a container
485 * initialized with memory_region_init().
486 * @offset: the offset relative to @mr where @subregion is added.
487 * @subregion: the subregion to be added.
488 * @priority: used for resolving overlaps; highest priority wins.
489 */
490void memory_region_add_subregion_overlap(MemoryRegion *mr,
491 target_phys_addr_t offset,
492 MemoryRegion *subregion,
493 unsigned priority);
494/**
495 * memory_region_del_subregion: Remove a subregion.
496 *
497 * Removes a subregion from its container.
498 *
499 * @mr: the container to be updated.
500 * @subregion: the region being removed; must be a current subregion of @mr.
501 */
502void memory_region_del_subregion(MemoryRegion *mr,
503 MemoryRegion *subregion);
504
6bba19ba
AK
505
506/*
507 * memory_region_set_enabled: dynamically enable or disable a region
508 *
509 * Enables or disables a memory region. A disabled memory region
510 * ignores all accesses to itself and its subregions. It does not
511 * obscure sibling subregions with lower priority - it simply behaves as
512 * if it was removed from the hierarchy.
513 *
514 * Regions default to being enabled.
515 *
516 * @mr: the region to be updated
517 * @enabled: whether to enable or disable the region
518 */
519void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
520
2282e1af
AK
521/*
522 * memory_region_set_address: dynamically update the address of a region
523 *
524 * Dynamically updates the address of a region, relative to its parent.
525 * May be used on regions are currently part of a memory hierarchy.
526 *
527 * @mr: the region to be updated
528 * @addr: new address, relative to parent region
529 */
530void memory_region_set_address(MemoryRegion *mr, target_phys_addr_t addr);
531
4703359e
AK
532/*
533 * memory_region_set_alias_offset: dynamically update a memory alias's offset
534 *
535 * Dynamically updates the offset into the target region that an alias points
536 * to, as if the fourth argument to memory_region_init_alias() has changed.
537 *
538 * @mr: the #MemoryRegion to be updated; should be an alias.
539 * @offset: the new offset into the target memory region
540 */
541void memory_region_set_alias_offset(MemoryRegion *mr,
542 target_phys_addr_t offset);
543
4ef4db86
AK
544/* Start a transaction; changes will be accumulated and made visible only
545 * when the transaction ends.
546 */
547void memory_region_transaction_begin(void);
548/* Commit a transaction and make changes visible to the guest.
549 */
550void memory_region_transaction_commit(void);
551
314e2987
BS
552void mtree_info(fprintf_function mon_printf, void *f);
553
093bc2cd
AK
554#endif
555
556#endif