blob: ebe0d24182cabbe01ee75e603765556e52883fb0 [file] [log] [blame]
Avi Kivity093bc2c2011-07-26 14:26:01 +03001/*
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"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010022#include "exec/cpu-common.h"
Andreas Färberce927ed2013-05-28 14:02:38 +020023#ifndef CONFIG_USER_ONLY
Paolo Bonzini022c62c2012-12-17 18:19:49 +010024#include "exec/hwaddr.h"
Andreas Färberce927ed2013-05-28 14:02:38 +020025#endif
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010026#include "qemu/queue.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010027#include "qemu/int128.h"
David Gibson06866572013-05-14 19:13:56 +100028#include "qemu/notify.h"
Avi Kivity093bc2c2011-07-26 14:26:01 +030029
Paolo Bonzini052e87b2013-05-27 10:08:27 +020030#define MAX_PHYS_ADDR_SPACE_BITS 62
31#define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
32
Avi Kivity093bc2c2011-07-26 14:26:01 +030033typedef struct MemoryRegionOps MemoryRegionOps;
Avi Kivity74901c32011-07-26 14:26:10 +030034typedef struct MemoryRegionMmio MemoryRegionMmio;
Avi Kivity093bc2c2011-07-26 14:26:01 +030035
36/* Must match *_DIRTY_FLAGS in cpu-all.h. To be replaced with dynamic
37 * registration.
38 */
39#define DIRTY_MEMORY_VGA 0
40#define DIRTY_MEMORY_CODE 1
41#define DIRTY_MEMORY_MIGRATION 3
42
Avi Kivity74901c32011-07-26 14:26:10 +030043struct MemoryRegionMmio {
44 CPUReadMemoryFunc *read[3];
45 CPUWriteMemoryFunc *write[3];
46};
47
Avi Kivity30951152012-10-30 13:47:46 +020048typedef struct IOMMUTLBEntry IOMMUTLBEntry;
49
50/* See address_space_translate: bit 0 is read, bit 1 is write. */
51typedef enum {
52 IOMMU_NONE = 0,
53 IOMMU_RO = 1,
54 IOMMU_WO = 2,
55 IOMMU_RW = 3,
56} IOMMUAccessFlags;
57
58struct IOMMUTLBEntry {
59 AddressSpace *target_as;
60 hwaddr iova;
61 hwaddr translated_addr;
62 hwaddr addr_mask; /* 0xfff = 4k translation */
63 IOMMUAccessFlags perm;
64};
65
Avi Kivity093bc2c2011-07-26 14:26:01 +030066/*
67 * Memory region callbacks
68 */
69struct MemoryRegionOps {
70 /* Read from the memory region. @addr is relative to @mr; @size is
71 * in bytes. */
72 uint64_t (*read)(void *opaque,
Avi Kivitya8170e52012-10-23 12:30:10 +020073 hwaddr addr,
Avi Kivity093bc2c2011-07-26 14:26:01 +030074 unsigned size);
75 /* Write to the memory region. @addr is relative to @mr; @size is
76 * in bytes. */
77 void (*write)(void *opaque,
Avi Kivitya8170e52012-10-23 12:30:10 +020078 hwaddr addr,
Avi Kivity093bc2c2011-07-26 14:26:01 +030079 uint64_t data,
80 unsigned size);
81
82 enum device_endian endianness;
83 /* Guest-visible constraints: */
84 struct {
85 /* If nonzero, specify bounds on access sizes beyond which a machine
86 * check is thrown.
87 */
88 unsigned min_access_size;
89 unsigned max_access_size;
90 /* If true, unaligned accesses are supported. Otherwise unaligned
91 * accesses throw machine checks.
92 */
93 bool unaligned;
Avi Kivity897fa7c2011-11-13 13:05:27 +020094 /*
95 * If present, and returns #false, the transaction is not accepted
96 * by the device (and results in machine dependent behaviour such
97 * as a machine check exception).
98 */
Avi Kivitya8170e52012-10-23 12:30:10 +020099 bool (*accepts)(void *opaque, hwaddr addr,
Avi Kivity897fa7c2011-11-13 13:05:27 +0200100 unsigned size, bool is_write);
Avi Kivity093bc2c2011-07-26 14:26:01 +0300101 } valid;
102 /* Internal implementation constraints: */
103 struct {
104 /* If nonzero, specifies the minimum size implemented. Smaller sizes
105 * will be rounded upwards and a partial result will be returned.
106 */
107 unsigned min_access_size;
108 /* If nonzero, specifies the maximum size implemented. Larger sizes
109 * will be done as a series of accesses with smaller sizes.
110 */
111 unsigned max_access_size;
112 /* If true, unaligned accesses are supported. Otherwise all accesses
113 * are converted to (possibly multiple) naturally aligned accesses.
114 */
115 bool unaligned;
116 } impl;
Avi Kivity627a0e92011-07-26 14:26:09 +0300117
Avi Kivity74901c32011-07-26 14:26:10 +0300118 /* If .read and .write are not present, old_mmio may be used for
119 * backwards compatibility with old mmio registration
120 */
121 const MemoryRegionMmio old_mmio;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300122};
123
Avi Kivity30951152012-10-30 13:47:46 +0200124typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps;
125
126struct MemoryRegionIOMMUOps {
127 /* Return a TLB entry that contains a given address. */
128 IOMMUTLBEntry (*translate)(MemoryRegion *iommu, hwaddr addr);
129};
130
Avi Kivity093bc2c2011-07-26 14:26:01 +0300131typedef struct CoalescedMemoryRange CoalescedMemoryRange;
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300132typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300133
134struct MemoryRegion {
135 /* All fields are private - violators will be prosecuted */
136 const MemoryRegionOps *ops;
Avi Kivity30951152012-10-30 13:47:46 +0200137 const MemoryRegionIOMMUOps *iommu_ops;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300138 void *opaque;
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400139 struct Object *owner;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300140 MemoryRegion *parent;
Avi Kivity08dafab2011-10-16 13:19:17 +0200141 Int128 size;
Avi Kivitya8170e52012-10-23 12:30:10 +0200142 hwaddr addr;
Avi Kivity545e92e2011-08-08 19:58:48 +0300143 void (*destructor)(MemoryRegion *mr);
Avi Kivity093bc2c2011-07-26 14:26:01 +0300144 ram_addr_t ram_addr;
Avi Kivityb3b00c72012-01-02 13:20:11 +0200145 bool subpage;
Avi Kivity14a3c102011-07-26 14:26:06 +0300146 bool terminates;
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +0200147 bool romd_mode;
Avi Kivity8ea92522011-12-08 15:58:43 +0200148 bool ram;
Avi Kivityfb1cd6f2011-09-25 14:48:47 +0300149 bool readonly; /* For RAM regions */
Avi Kivity6bba19b2011-09-14 11:54:58 +0300150 bool enabled;
Avi Kivity75c578d2012-01-02 15:40:52 +0200151 bool rom_device;
Jan Kiszka1660e722011-10-23 16:01:19 +0200152 bool warning_printed; /* For reservations */
Jan Kiszkad4105152012-08-23 13:02:29 +0200153 bool flush_coalesced_mmio;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300154 MemoryRegion *alias;
Avi Kivitya8170e52012-10-23 12:30:10 +0200155 hwaddr alias_offset;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300156 unsigned priority;
157 bool may_overlap;
158 QTAILQ_HEAD(subregions, MemoryRegion) subregions;
159 QTAILQ_ENTRY(MemoryRegion) subregions_link;
160 QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced;
161 const char *name;
Avi Kivity5a583342011-07-26 14:26:02 +0300162 uint8_t dirty_log_mask;
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300163 unsigned ioeventfd_nb;
164 MemoryRegionIoeventfd *ioeventfds;
David Gibson06866572013-05-14 19:13:56 +1000165 NotifierList iommu_notify;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300166};
167
Paolo Bonzinic2fc83e2013-06-02 15:20:47 +0200168typedef struct MemoryListener MemoryListener;
169
170/**
171 * MemoryListener: callbacks structure for updates to the physical memory map
172 *
173 * Allows a component to adjust to changes in the guest-visible memory map.
174 * Use with memory_listener_register() and memory_listener_unregister().
175 */
176struct MemoryListener {
177 void (*begin)(MemoryListener *listener);
178 void (*commit)(MemoryListener *listener);
179 void (*region_add)(MemoryListener *listener, MemoryRegionSection *section);
180 void (*region_del)(MemoryListener *listener, MemoryRegionSection *section);
181 void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section);
182 void (*log_start)(MemoryListener *listener, MemoryRegionSection *section);
183 void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section);
184 void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section);
185 void (*log_global_start)(MemoryListener *listener);
186 void (*log_global_stop)(MemoryListener *listener);
187 void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section,
188 bool match_data, uint64_t data, EventNotifier *e);
189 void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section,
190 bool match_data, uint64_t data, EventNotifier *e);
191 void (*coalesced_mmio_add)(MemoryListener *listener, MemoryRegionSection *section,
192 hwaddr addr, hwaddr len);
193 void (*coalesced_mmio_del)(MemoryListener *listener, MemoryRegionSection *section,
194 hwaddr addr, hwaddr len);
195 /* Lower = earlier (during add), later (during del) */
196 unsigned priority;
197 AddressSpace *address_space_filter;
198 QTAILQ_ENTRY(MemoryListener) link;
199};
200
Avi Kivity9ad2bbc2012-10-02 14:59:23 +0200201/**
202 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
203 */
204struct AddressSpace {
205 /* All fields are private. */
Alexey Kardashevskiy7dca8042013-04-29 16:25:51 +0000206 char *name;
Avi Kivity9ad2bbc2012-10-02 14:59:23 +0200207 MemoryRegion *root;
208 struct FlatView *current_map;
209 int ioeventfd_nb;
210 struct MemoryRegionIoeventfd *ioeventfds;
Avi Kivityac1970f2012-10-03 16:22:53 +0200211 struct AddressSpaceDispatch *dispatch;
Paolo Bonzini00752702013-05-29 12:13:54 +0200212 struct AddressSpaceDispatch *next_dispatch;
Paolo Bonzini89ae3372013-06-02 10:39:07 +0200213 MemoryListener dispatch_listener;
214
Avi Kivity0d673e32012-10-02 15:28:50 +0200215 QTAILQ_ENTRY(AddressSpace) address_spaces_link;
Avi Kivity9ad2bbc2012-10-02 14:59:23 +0200216};
217
Avi Kivitye2177952011-12-08 15:00:18 +0200218/**
219 * MemoryRegionSection: describes a fragment of a #MemoryRegion
220 *
221 * @mr: the region, or %NULL if empty
Avi Kivity7664e802011-12-11 14:47:25 +0200222 * @address_space: the address space the region is mapped in
Avi Kivitye2177952011-12-08 15:00:18 +0200223 * @offset_within_region: the beginning of the section, relative to @mr's start
224 * @size: the size of the section; will not exceed @mr's boundaries
225 * @offset_within_address_space: the address of the first byte of the section
226 * relative to the region's address space
Avi Kivity7a8499e2012-02-08 17:01:23 +0200227 * @readonly: writes to this section are ignored
Avi Kivitye2177952011-12-08 15:00:18 +0200228 */
229struct MemoryRegionSection {
230 MemoryRegion *mr;
Avi Kivityf6790af2012-10-02 20:13:51 +0200231 AddressSpace *address_space;
Avi Kivitya8170e52012-10-23 12:30:10 +0200232 hwaddr offset_within_region;
Paolo Bonzini052e87b2013-05-27 10:08:27 +0200233 Int128 size;
Avi Kivitya8170e52012-10-23 12:30:10 +0200234 hwaddr offset_within_address_space;
Avi Kivity7a8499e2012-02-08 17:01:23 +0200235 bool readonly;
Avi Kivitye2177952011-12-08 15:00:18 +0200236};
237
Avi Kivity093bc2c2011-07-26 14:26:01 +0300238/**
239 * memory_region_init: Initialize a memory region
240 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300241 * The region typically acts as a container for other memory regions. Use
Avi Kivity093bc2c2011-07-26 14:26:01 +0300242 * memory_region_add_subregion() to add subregions.
243 *
244 * @mr: the #MemoryRegion to be initialized
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400245 * @owner: the object that tracks the region's reference count
Avi Kivity093bc2c2011-07-26 14:26:01 +0300246 * @name: used for debugging; not visible to the user or ABI
247 * @size: size of the region; any subregions beyond this size will be clipped
248 */
249void memory_region_init(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400250 struct Object *owner,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300251 const char *name,
252 uint64_t size);
Paolo Bonzini46637be2013-05-07 09:06:00 +0200253
254/**
255 * memory_region_ref: Add 1 to a memory region's reference count
256 *
257 * Whenever memory regions are accessed outside the BQL, they need to be
258 * preserved against hot-unplug. MemoryRegions actually do not have their
259 * own reference count; they piggyback on a QOM object, their "owner".
260 * This function adds a reference to the owner.
261 *
262 * All MemoryRegions must have an owner if they can disappear, even if the
263 * device they belong to operates exclusively under the BQL. This is because
264 * the region could be returned at any time by memory_region_find, and this
265 * is usually under guest control.
266 *
267 * @mr: the #MemoryRegion
268 */
269void memory_region_ref(MemoryRegion *mr);
270
271/**
272 * memory_region_unref: Remove 1 to a memory region's reference count
273 *
274 * Whenever memory regions are accessed outside the BQL, they need to be
275 * preserved against hot-unplug. MemoryRegions actually do not have their
276 * own reference count; they piggyback on a QOM object, their "owner".
277 * This function removes a reference to the owner and possibly destroys it.
278 *
279 * @mr: the #MemoryRegion
280 */
281void memory_region_unref(MemoryRegion *mr);
282
Avi Kivity093bc2c2011-07-26 14:26:01 +0300283/**
284 * memory_region_init_io: Initialize an I/O memory region.
285 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300286 * Accesses into the region will cause the callbacks in @ops to be called.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300287 * if @size is nonzero, subregions will be clipped to @size.
288 *
289 * @mr: the #MemoryRegion to be initialized.
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400290 * @owner: the object that tracks the region's reference count
Avi Kivity093bc2c2011-07-26 14:26:01 +0300291 * @ops: a structure containing read and write callbacks to be used when
292 * I/O is performed on the region.
293 * @opaque: passed to to the read and write callbacks of the @ops structure.
294 * @name: used for debugging; not visible to the user or ABI
295 * @size: size of the region.
296 */
297void memory_region_init_io(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400298 struct Object *owner,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300299 const MemoryRegionOps *ops,
300 void *opaque,
301 const char *name,
302 uint64_t size);
303
304/**
305 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300306 * region will modify memory directly.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300307 *
308 * @mr: the #MemoryRegion to be initialized.
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400309 * @owner: the object that tracks the region's reference count
Avi Kivityc5705a72011-12-20 15:59:12 +0200310 * @name: the name of the region.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300311 * @size: size of the region.
312 */
313void memory_region_init_ram(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400314 struct Object *owner,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300315 const char *name,
316 uint64_t size);
317
318/**
BALATON Zoltan1a7e8ca2012-08-22 17:18:38 +0200319 * memory_region_init_ram_ptr: Initialize RAM memory region from a
320 * user-provided pointer. Accesses into the
321 * region will modify memory directly.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300322 *
323 * @mr: the #MemoryRegion to be initialized.
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400324 * @owner: the object that tracks the region's reference count
Avi Kivityc5705a72011-12-20 15:59:12 +0200325 * @name: the name of the region.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300326 * @size: size of the region.
327 * @ptr: memory to be mapped; must contain at least @size bytes.
328 */
329void memory_region_init_ram_ptr(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400330 struct Object *owner,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300331 const char *name,
332 uint64_t size,
333 void *ptr);
334
335/**
336 * memory_region_init_alias: Initialize a memory region that aliases all or a
337 * part of another memory region.
338 *
339 * @mr: the #MemoryRegion to be initialized.
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400340 * @owner: the object that tracks the region's reference count
Avi Kivity093bc2c2011-07-26 14:26:01 +0300341 * @name: used for debugging; not visible to the user or ABI
342 * @orig: the region to be referenced; @mr will be equivalent to
343 * @orig between @offset and @offset + @size - 1.
344 * @offset: start of the section in @orig to be referenced.
345 * @size: size of the region.
346 */
347void memory_region_init_alias(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400348 struct Object *owner,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300349 const char *name,
350 MemoryRegion *orig,
Avi Kivitya8170e52012-10-23 12:30:10 +0200351 hwaddr offset,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300352 uint64_t size);
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300353
354/**
355 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
356 * handled via callbacks.
357 *
358 * @mr: the #MemoryRegion to be initialized.
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400359 * @owner: the object that tracks the region's reference count
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300360 * @ops: callbacks for write access handling.
Avi Kivityc5705a72011-12-20 15:59:12 +0200361 * @name: the name of the region.
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300362 * @size: size of the region.
363 */
364void memory_region_init_rom_device(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400365 struct Object *owner,
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300366 const MemoryRegionOps *ops,
Avi Kivity75f59412011-08-26 00:35:15 +0300367 void *opaque,
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300368 const char *name,
369 uint64_t size);
370
Avi Kivity093bc2c2011-07-26 14:26:01 +0300371/**
Jan Kiszka1660e722011-10-23 16:01:19 +0200372 * memory_region_init_reservation: Initialize a memory region that reserves
373 * I/O space.
374 *
375 * A reservation region primariy serves debugging purposes. It claims I/O
376 * space that is not supposed to be handled by QEMU itself. Any access via
377 * the memory API will cause an abort().
378 *
379 * @mr: the #MemoryRegion to be initialized
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400380 * @owner: the object that tracks the region's reference count
Jan Kiszka1660e722011-10-23 16:01:19 +0200381 * @name: used for debugging; not visible to the user or ABI
382 * @size: size of the region.
383 */
384void memory_region_init_reservation(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400385 struct Object *owner,
Jan Kiszka1660e722011-10-23 16:01:19 +0200386 const char *name,
387 uint64_t size);
Avi Kivity30951152012-10-30 13:47:46 +0200388
389/**
390 * memory_region_init_iommu: Initialize a memory region that translates
391 * addresses
392 *
393 * An IOMMU region translates addresses and forwards accesses to a target
394 * memory region.
395 *
396 * @mr: the #MemoryRegion to be initialized
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400397 * @owner: the object that tracks the region's reference count
Avi Kivity30951152012-10-30 13:47:46 +0200398 * @ops: a function that translates addresses into the @target region
399 * @name: used for debugging; not visible to the user or ABI
400 * @size: size of the region.
401 */
402void memory_region_init_iommu(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400403 struct Object *owner,
Avi Kivity30951152012-10-30 13:47:46 +0200404 const MemoryRegionIOMMUOps *ops,
405 const char *name,
406 uint64_t size);
407
Jan Kiszka1660e722011-10-23 16:01:19 +0200408/**
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300409 * memory_region_destroy: Destroy a memory region and reclaim all resources.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300410 *
411 * @mr: the region to be destroyed. May not currently be a subregion
412 * (see memory_region_add_subregion()) or referenced in an alias
413 * (see memory_region_init_alias()).
414 */
415void memory_region_destroy(MemoryRegion *mr);
416
417/**
Paolo Bonzini803c0812013-05-07 06:59:09 +0200418 * memory_region_owner: get a memory region's owner.
419 *
420 * @mr: the memory region being queried.
421 */
422struct Object *memory_region_owner(MemoryRegion *mr);
423
424/**
Avi Kivity093bc2c2011-07-26 14:26:01 +0300425 * memory_region_size: get a memory region's size.
426 *
427 * @mr: the memory region being queried.
428 */
429uint64_t memory_region_size(MemoryRegion *mr);
430
431/**
Avi Kivity8ea92522011-12-08 15:58:43 +0200432 * memory_region_is_ram: check whether a memory region is random access
433 *
434 * Returns %true is a memory region is random access.
435 *
436 * @mr: the memory region being queried
437 */
438bool memory_region_is_ram(MemoryRegion *mr);
439
440/**
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +0200441 * memory_region_is_romd: check whether a memory region is in ROMD mode
Blue Swirlfd062572012-04-09 17:38:52 +0000442 *
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +0200443 * Returns %true if a memory region is a ROM device and currently set to allow
Blue Swirlfd062572012-04-09 17:38:52 +0000444 * direct reads.
445 *
446 * @mr: the memory region being queried
447 */
448static inline bool memory_region_is_romd(MemoryRegion *mr)
449{
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +0200450 return mr->rom_device && mr->romd_mode;
Blue Swirlfd062572012-04-09 17:38:52 +0000451}
452
453/**
Avi Kivity30951152012-10-30 13:47:46 +0200454 * memory_region_is_iommu: check whether a memory region is an iommu
455 *
456 * Returns %true is a memory region is an iommu.
457 *
458 * @mr: the memory region being queried
459 */
460bool memory_region_is_iommu(MemoryRegion *mr);
461
462/**
David Gibson06866572013-05-14 19:13:56 +1000463 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
464 *
465 * @mr: the memory region that was changed
466 * @entry: the new entry in the IOMMU translation table. The entry
467 * replaces all old entries for the same virtual I/O address range.
468 * Deleted entries have .@perm == 0.
469 */
470void memory_region_notify_iommu(MemoryRegion *mr,
471 IOMMUTLBEntry entry);
472
473/**
474 * memory_region_register_iommu_notifier: register a notifier for changes to
475 * IOMMU translation entries.
476 *
477 * @mr: the memory region to observe
478 * @n: the notifier to be added; the notifier receives a pointer to an
479 * #IOMMUTLBEntry as the opaque value; the pointer ceases to be
480 * valid on exit from the notifier.
481 */
482void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n);
483
484/**
485 * memory_region_unregister_iommu_notifier: unregister a notifier for
486 * changes to IOMMU translation entries.
487 *
488 * @n: the notifier to be removed.
489 */
490void memory_region_unregister_iommu_notifier(Notifier *n);
491
492/**
Avi Kivity8991c792011-12-20 15:53:11 +0200493 * memory_region_name: get a memory region's name
494 *
495 * Returns the string that was used to initialize the memory region.
496 *
497 * @mr: the memory region being queried
498 */
499const char *memory_region_name(MemoryRegion *mr);
500
501/**
Avi Kivity55043ba2011-12-15 17:20:34 +0200502 * memory_region_is_logging: return whether a memory region is logging writes
503 *
504 * Returns %true if the memory region is logging writes
505 *
506 * @mr: the memory region being queried
507 */
508bool memory_region_is_logging(MemoryRegion *mr);
509
510/**
Avi Kivityce7923d2011-12-08 16:05:11 +0200511 * memory_region_is_rom: check whether a memory region is ROM
512 *
513 * Returns %true is a memory region is read-only memory.
514 *
515 * @mr: the memory region being queried
516 */
517bool memory_region_is_rom(MemoryRegion *mr);
518
519/**
Avi Kivity093bc2c2011-07-26 14:26:01 +0300520 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
521 *
522 * Returns a host pointer to a RAM memory region (created with
523 * memory_region_init_ram() or memory_region_init_ram_ptr()). Use with
524 * care.
525 *
526 * @mr: the memory region being queried.
527 */
528void *memory_region_get_ram_ptr(MemoryRegion *mr);
529
530/**
Avi Kivity093bc2c2011-07-26 14:26:01 +0300531 * memory_region_set_log: Turn dirty logging on or off for a region.
532 *
533 * Turns dirty logging on or off for a specified client (display, migration).
534 * Only meaningful for RAM regions.
535 *
536 * @mr: the memory region being updated.
537 * @log: whether dirty logging is to be enabled or disabled.
538 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
539 * %DIRTY_MEMORY_VGA.
540 */
541void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
542
543/**
Blue Swirlcd7a45c2012-01-22 16:38:21 +0000544 * memory_region_get_dirty: Check whether a range of bytes is dirty
545 * for a specified client.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300546 *
Blue Swirlcd7a45c2012-01-22 16:38:21 +0000547 * Checks whether a range of bytes has been written to since the last
Avi Kivity093bc2c2011-07-26 14:26:01 +0300548 * call to memory_region_reset_dirty() with the same @client. Dirty logging
549 * must be enabled.
550 *
551 * @mr: the memory region being queried.
552 * @addr: the address (relative to the start of the region) being queried.
Blue Swirlcd7a45c2012-01-22 16:38:21 +0000553 * @size: the size of the range being queried.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300554 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
555 * %DIRTY_MEMORY_VGA.
556 */
Avi Kivitya8170e52012-10-23 12:30:10 +0200557bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
558 hwaddr size, unsigned client);
Avi Kivity093bc2c2011-07-26 14:26:01 +0300559
560/**
Blue Swirlfd4aa972011-10-16 16:04:59 +0000561 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300562 *
Blue Swirlfd4aa972011-10-16 16:04:59 +0000563 * Marks a range of bytes as dirty, after it has been dirtied outside
564 * guest code.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300565 *
Blue Swirlfd4aa972011-10-16 16:04:59 +0000566 * @mr: the memory region being dirtied.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300567 * @addr: the address (relative to the start of the region) being dirtied.
Blue Swirlfd4aa972011-10-16 16:04:59 +0000568 * @size: size of the range being dirtied.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300569 */
Avi Kivitya8170e52012-10-23 12:30:10 +0200570void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
571 hwaddr size);
Avi Kivity093bc2c2011-07-26 14:26:01 +0300572
573/**
Juan Quintela6c279db2012-10-17 20:24:28 +0200574 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty
575 * for a specified client. It clears them.
576 *
577 * Checks whether a range of bytes has been written to since the last
578 * call to memory_region_reset_dirty() with the same @client. Dirty logging
579 * must be enabled.
580 *
581 * @mr: the memory region being queried.
582 * @addr: the address (relative to the start of the region) being queried.
583 * @size: the size of the range being queried.
584 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
585 * %DIRTY_MEMORY_VGA.
586 */
587bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr,
588 hwaddr size, unsigned client);
589/**
Avi Kivity093bc2c2011-07-26 14:26:01 +0300590 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
591 * any external TLBs (e.g. kvm)
592 *
593 * Flushes dirty information from accelerators such as kvm and vhost-net
594 * and makes it available to users of the memory API.
595 *
596 * @mr: the region being flushed.
597 */
598void memory_region_sync_dirty_bitmap(MemoryRegion *mr);
599
600/**
601 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
602 * client.
603 *
604 * Marks a range of pages as no longer dirty.
605 *
606 * @mr: the region being updated.
607 * @addr: the start of the subrange being cleaned.
608 * @size: the size of the subrange being cleaned.
609 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
610 * %DIRTY_MEMORY_VGA.
611 */
Avi Kivitya8170e52012-10-23 12:30:10 +0200612void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
613 hwaddr size, unsigned client);
Avi Kivity093bc2c2011-07-26 14:26:01 +0300614
615/**
616 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
617 *
618 * Allows a memory region to be marked as read-only (turning it into a ROM).
619 * only useful on RAM regions.
620 *
621 * @mr: the region being updated.
622 * @readonly: whether rhe region is to be ROM or RAM.
623 */
624void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
625
626/**
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +0200627 * memory_region_rom_device_set_romd: enable/disable ROMD mode
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300628 *
629 * Allows a ROM device (initialized with memory_region_init_rom_device() to
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +0200630 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
631 * device is mapped to guest memory and satisfies read access directly.
632 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
633 * Writes are always handled by the #MemoryRegion.write function.
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300634 *
635 * @mr: the memory region to be updated
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +0200636 * @romd_mode: %true to put the region into ROMD mode
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300637 */
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +0200638void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode);
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300639
640/**
Avi Kivity093bc2c2011-07-26 14:26:01 +0300641 * memory_region_set_coalescing: Enable memory coalescing for the region.
642 *
643 * Enabled writes to a region to be queued for later processing. MMIO ->write
644 * callbacks may be delayed until a non-coalesced MMIO is issued.
645 * Only useful for IO regions. Roughly similar to write-combining hardware.
646 *
647 * @mr: the memory region to be write coalesced
648 */
649void memory_region_set_coalescing(MemoryRegion *mr);
650
651/**
652 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
653 * a region.
654 *
655 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
656 * Multiple calls can be issued coalesced disjoint ranges.
657 *
658 * @mr: the memory region to be updated.
659 * @offset: the start of the range within the region to be coalesced.
660 * @size: the size of the subrange to be coalesced.
661 */
662void memory_region_add_coalescing(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +0200663 hwaddr offset,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300664 uint64_t size);
665
666/**
667 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
668 *
669 * Disables any coalescing caused by memory_region_set_coalescing() or
670 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
671 * hardware.
672 *
673 * @mr: the memory region to be updated.
674 */
675void memory_region_clear_coalescing(MemoryRegion *mr);
676
677/**
Jan Kiszkad4105152012-08-23 13:02:29 +0200678 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
679 * accesses.
680 *
681 * Ensure that pending coalesced MMIO request are flushed before the memory
682 * region is accessed. This property is automatically enabled for all regions
683 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
684 *
685 * @mr: the memory region to be updated.
686 */
687void memory_region_set_flush_coalesced(MemoryRegion *mr);
688
689/**
690 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
691 * accesses.
692 *
693 * Clear the automatic coalesced MMIO flushing enabled via
694 * memory_region_set_flush_coalesced. Note that this service has no effect on
695 * memory regions that have MMIO coalescing enabled for themselves. For them,
696 * automatic flushing will stop once coalescing is disabled.
697 *
698 * @mr: the memory region to be updated.
699 */
700void memory_region_clear_flush_coalesced(MemoryRegion *mr);
701
702/**
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300703 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
704 * is written to a location.
705 *
706 * Marks a word in an IO region (initialized with memory_region_init_io())
707 * as a trigger for an eventfd event. The I/O callback will not be called.
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300708 * The caller must be prepared to handle failure (that is, take the required
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300709 * action if the callback _is_ called).
710 *
711 * @mr: the memory region being updated.
712 * @addr: the address within @mr that is to be monitored
713 * @size: the size of the access to trigger the eventfd
714 * @match_data: whether to match against @data, instead of just @addr
715 * @data: the data to match against the guest write
716 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
717 **/
718void memory_region_add_eventfd(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +0200719 hwaddr addr,
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300720 unsigned size,
721 bool match_data,
722 uint64_t data,
Paolo Bonzini753d5e12012-07-05 17:16:27 +0200723 EventNotifier *e);
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300724
725/**
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300726 * memory_region_del_eventfd: Cancel an eventfd.
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300727 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300728 * Cancels an eventfd trigger requested by a previous
729 * memory_region_add_eventfd() call.
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300730 *
731 * @mr: the memory region being updated.
732 * @addr: the address within @mr that is to be monitored
733 * @size: the size of the access to trigger the eventfd
734 * @match_data: whether to match against @data, instead of just @addr
735 * @data: the data to match against the guest write
736 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
737 */
738void memory_region_del_eventfd(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +0200739 hwaddr addr,
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300740 unsigned size,
741 bool match_data,
742 uint64_t data,
Paolo Bonzini753d5e12012-07-05 17:16:27 +0200743 EventNotifier *e);
744
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300745/**
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300746 * memory_region_add_subregion: Add a subregion to a container.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300747 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300748 * Adds a subregion at @offset. The subregion may not overlap with other
Avi Kivity093bc2c2011-07-26 14:26:01 +0300749 * subregions (except for those explicitly marked as overlapping). A region
750 * may only be added once as a subregion (unless removed with
751 * memory_region_del_subregion()); use memory_region_init_alias() if you
752 * want a region to be a subregion in multiple locations.
753 *
754 * @mr: the region to contain the new subregion; must be a container
755 * initialized with memory_region_init().
756 * @offset: the offset relative to @mr where @subregion is added.
757 * @subregion: the subregion to be added.
758 */
759void memory_region_add_subregion(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +0200760 hwaddr offset,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300761 MemoryRegion *subregion);
762/**
BALATON Zoltan1a7e8ca2012-08-22 17:18:38 +0200763 * memory_region_add_subregion_overlap: Add a subregion to a container
764 * with overlap.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300765 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300766 * Adds a subregion at @offset. The subregion may overlap with other
Avi Kivity093bc2c2011-07-26 14:26:01 +0300767 * subregions. Conflicts are resolved by having a higher @priority hide a
768 * lower @priority. Subregions without priority are taken as @priority 0.
769 * A region may only be added once as a subregion (unless removed with
770 * memory_region_del_subregion()); use memory_region_init_alias() if you
771 * want a region to be a subregion in multiple locations.
772 *
773 * @mr: the region to contain the new subregion; must be a container
774 * initialized with memory_region_init().
775 * @offset: the offset relative to @mr where @subregion is added.
776 * @subregion: the subregion to be added.
777 * @priority: used for resolving overlaps; highest priority wins.
778 */
779void memory_region_add_subregion_overlap(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +0200780 hwaddr offset,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300781 MemoryRegion *subregion,
782 unsigned priority);
Avi Kivitye34911c2011-12-19 12:06:23 +0200783
784/**
785 * memory_region_get_ram_addr: Get the ram address associated with a memory
786 * region
787 *
Stefan Weildabdf392012-01-08 19:35:09 +0100788 * DO NOT USE THIS FUNCTION. This is a temporary workaround while the Xen
Avi Kivitye34911c2011-12-19 12:06:23 +0200789 * code is being reworked.
790 */
791ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr);
792
Avi Kivity093bc2c2011-07-26 14:26:01 +0300793/**
794 * memory_region_del_subregion: Remove a subregion.
795 *
796 * Removes a subregion from its container.
797 *
798 * @mr: the container to be updated.
799 * @subregion: the region being removed; must be a current subregion of @mr.
800 */
801void memory_region_del_subregion(MemoryRegion *mr,
802 MemoryRegion *subregion);
803
Avi Kivity6bba19b2011-09-14 11:54:58 +0300804/*
805 * memory_region_set_enabled: dynamically enable or disable a region
806 *
807 * Enables or disables a memory region. A disabled memory region
808 * ignores all accesses to itself and its subregions. It does not
809 * obscure sibling subregions with lower priority - it simply behaves as
810 * if it was removed from the hierarchy.
811 *
812 * Regions default to being enabled.
813 *
814 * @mr: the region to be updated
815 * @enabled: whether to enable or disable the region
816 */
817void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
818
Avi Kivity2282e1a2011-09-14 12:10:12 +0300819/*
820 * memory_region_set_address: dynamically update the address of a region
821 *
822 * Dynamically updates the address of a region, relative to its parent.
823 * May be used on regions are currently part of a memory hierarchy.
824 *
825 * @mr: the region to be updated
826 * @addr: new address, relative to parent region
827 */
Avi Kivitya8170e52012-10-23 12:30:10 +0200828void memory_region_set_address(MemoryRegion *mr, hwaddr addr);
Avi Kivity2282e1a2011-09-14 12:10:12 +0300829
Avi Kivity47033592011-12-04 19:16:50 +0200830/*
831 * memory_region_set_alias_offset: dynamically update a memory alias's offset
832 *
833 * Dynamically updates the offset into the target region that an alias points
834 * to, as if the fourth argument to memory_region_init_alias() has changed.
835 *
836 * @mr: the #MemoryRegion to be updated; should be an alias.
837 * @offset: the new offset into the target memory region
838 */
839void memory_region_set_alias_offset(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +0200840 hwaddr offset);
Avi Kivity47033592011-12-04 19:16:50 +0200841
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300842/**
Paolo Bonzini3ce10902013-07-02 13:40:48 +0200843 * memory_region_present: translate an address/size relative to a
844 * MemoryRegion into a #MemoryRegionSection.
845 *
846 * Answer whether a #MemoryRegion within @parent covers the address
847 * @addr.
848 *
849 * @parent: a MemoryRegion within which @addr is a relative address
850 * @addr: the area within @parent to be searched
851 */
852bool memory_region_present(MemoryRegion *parent, hwaddr addr);
853
854/**
Paolo Bonzini73034e92013-05-07 15:48:28 +0200855 * memory_region_find: translate an address/size relative to a
856 * MemoryRegion into a #MemoryRegionSection.
Avi Kivitye2177952011-12-08 15:00:18 +0200857 *
Paolo Bonzini73034e92013-05-07 15:48:28 +0200858 * Locates the first #MemoryRegion within @mr that overlaps the range
859 * given by @addr and @size.
Avi Kivitye2177952011-12-08 15:00:18 +0200860 *
861 * Returns a #MemoryRegionSection that describes a contiguous overlap.
862 * It will have the following characteristics:
Avi Kivitye2177952011-12-08 15:00:18 +0200863 * .@size = 0 iff no overlap was found
864 * .@mr is non-%NULL iff an overlap was found
865 *
Paolo Bonzini73034e92013-05-07 15:48:28 +0200866 * Remember that in the return value the @offset_within_region is
867 * relative to the returned region (in the .@mr field), not to the
868 * @mr argument.
869 *
870 * Similarly, the .@offset_within_address_space is relative to the
871 * address space that contains both regions, the passed and the
872 * returned one. However, in the special case where the @mr argument
873 * has no parent (and thus is the root of the address space), the
874 * following will hold:
875 * .@offset_within_address_space >= @addr
876 * .@offset_within_address_space + .@size <= @addr + @size
877 *
878 * @mr: a MemoryRegion within which @addr is a relative address
879 * @addr: start of the area within @as to be searched
Avi Kivitye2177952011-12-08 15:00:18 +0200880 * @size: size of the area to be searched
881 */
Paolo Bonzini73034e92013-05-07 15:48:28 +0200882MemoryRegionSection memory_region_find(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +0200883 hwaddr addr, uint64_t size);
Avi Kivitye2177952011-12-08 15:00:18 +0200884
Blue Swirlfd062572012-04-09 17:38:52 +0000885/**
Paolo Bonzini1d671362013-04-24 10:46:55 +0200886 * address_space_sync_dirty_bitmap: synchronize the dirty log for all memory
Avi Kivity86e775c2011-12-15 16:24:49 +0200887 *
888 * Synchronizes the dirty page log for an entire address space.
Paolo Bonzini1d671362013-04-24 10:46:55 +0200889 * @as: the address space that contains the memory being synchronized
Avi Kivity86e775c2011-12-15 16:24:49 +0200890 */
Paolo Bonzini1d671362013-04-24 10:46:55 +0200891void address_space_sync_dirty_bitmap(AddressSpace *as);
Avi Kivity86e775c2011-12-15 16:24:49 +0200892
Avi Kivitye2177952011-12-08 15:00:18 +0200893/**
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300894 * memory_region_transaction_begin: Start a transaction.
895 *
896 * During a transaction, changes will be accumulated and made visible
Stefan Weildabdf392012-01-08 19:35:09 +0100897 * only when the transaction ends (is committed).
Avi Kivity4ef4db82011-07-26 14:26:13 +0300898 */
899void memory_region_transaction_begin(void);
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300900
901/**
902 * memory_region_transaction_commit: Commit a transaction and make changes
903 * visible to the guest.
Avi Kivity4ef4db82011-07-26 14:26:13 +0300904 */
905void memory_region_transaction_commit(void);
906
Avi Kivity7664e802011-12-11 14:47:25 +0200907/**
908 * memory_listener_register: register callbacks to be called when memory
909 * sections are mapped or unmapped into an address
910 * space
911 *
912 * @listener: an object containing the callbacks to be called
Avi Kivity7376e582012-02-08 21:05:17 +0200913 * @filter: if non-%NULL, only regions in this address space will be observed
Avi Kivity7664e802011-12-11 14:47:25 +0200914 */
Avi Kivityf6790af2012-10-02 20:13:51 +0200915void memory_listener_register(MemoryListener *listener, AddressSpace *filter);
Avi Kivity7664e802011-12-11 14:47:25 +0200916
917/**
918 * memory_listener_unregister: undo the effect of memory_listener_register()
919 *
920 * @listener: an object containing the callbacks to be removed
921 */
922void memory_listener_unregister(MemoryListener *listener);
923
924/**
925 * memory_global_dirty_log_start: begin dirty logging for all regions
926 */
927void memory_global_dirty_log_start(void);
928
929/**
BALATON Zoltan1a7e8ca2012-08-22 17:18:38 +0200930 * memory_global_dirty_log_stop: end dirty logging for all regions
Avi Kivity7664e802011-12-11 14:47:25 +0200931 */
932void memory_global_dirty_log_stop(void);
933
Blue Swirl314e2982011-09-11 20:22:05 +0000934void mtree_info(fprintf_function mon_printf, void *f);
935
Avi Kivity9ad2bbc2012-10-02 14:59:23 +0200936/**
937 * address_space_init: initializes an address space
938 *
939 * @as: an uninitialized #AddressSpace
940 * @root: a #MemoryRegion that routes addesses for the address space
Alexey Kardashevskiy7dca8042013-04-29 16:25:51 +0000941 * @name: an address space name. The name is only used for debugging
942 * output.
Avi Kivity9ad2bbc2012-10-02 14:59:23 +0200943 */
Alexey Kardashevskiy7dca8042013-04-29 16:25:51 +0000944void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
Avi Kivity9ad2bbc2012-10-02 14:59:23 +0200945
Avi Kivity83f3c252012-10-07 12:59:55 +0200946
947/**
948 * address_space_destroy: destroy an address space
949 *
950 * Releases all resources associated with an address space. After an address space
951 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
952 * as well.
953 *
954 * @as: address space to be destroyed
955 */
956void address_space_destroy(AddressSpace *as);
957
Avi Kivityac1970f2012-10-03 16:22:53 +0200958/**
959 * address_space_rw: read from or write to an address space.
960 *
Avi Kivity30951152012-10-30 13:47:46 +0200961 * Return true if the operation hit any unassigned memory or encountered an
962 * IOMMU fault.
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +0200963 *
Avi Kivityac1970f2012-10-03 16:22:53 +0200964 * @as: #AddressSpace to be accessed
965 * @addr: address within that address space
966 * @buf: buffer with the data transferred
967 * @is_write: indicates the transfer direction
968 */
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +0200969bool address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
Avi Kivityac1970f2012-10-03 16:22:53 +0200970 int len, bool is_write);
971
972/**
973 * address_space_write: write to address space.
974 *
Avi Kivity30951152012-10-30 13:47:46 +0200975 * Return true if the operation hit any unassigned memory or encountered an
976 * IOMMU fault.
Avi Kivityac1970f2012-10-03 16:22:53 +0200977 *
978 * @as: #AddressSpace to be accessed
979 * @addr: address within that address space
980 * @buf: buffer with the data transferred
981 */
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +0200982bool address_space_write(AddressSpace *as, hwaddr addr,
983 const uint8_t *buf, int len);
984
985/**
986 * address_space_read: read from an address space.
987 *
Avi Kivity30951152012-10-30 13:47:46 +0200988 * Return true if the operation hit any unassigned memory or encountered an
989 * IOMMU fault.
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +0200990 *
991 * @as: #AddressSpace to be accessed
992 * @addr: address within that address space
993 * @buf: buffer with the data transferred
994 */
995bool address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len);
Avi Kivityac1970f2012-10-03 16:22:53 +0200996
Paolo Bonzini149f54b2013-05-24 12:59:37 +0200997/* address_space_translate: translate an address range into an address space
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +0200998 * into a MemoryRegion and an address range into that section
Paolo Bonzini149f54b2013-05-24 12:59:37 +0200999 *
1000 * @as: #AddressSpace to be accessed
1001 * @addr: address within that address space
1002 * @xlat: pointer to address within the returned memory region section's
1003 * #MemoryRegion.
1004 * @len: pointer to length
1005 * @is_write: indicates the transfer direction
1006 */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02001007MemoryRegion *address_space_translate(AddressSpace *as, hwaddr addr,
1008 hwaddr *xlat, hwaddr *len,
1009 bool is_write);
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001010
Paolo Bonzini51644ab2013-04-11 15:40:59 +02001011/* address_space_access_valid: check for validity of accessing an address
1012 * space range
1013 *
Avi Kivity30951152012-10-30 13:47:46 +02001014 * Check whether memory is assigned to the given address space range, and
1015 * access is permitted by any IOMMU regions that are active for the address
1016 * space.
Paolo Bonzini51644ab2013-04-11 15:40:59 +02001017 *
1018 * For now, addr and len should be aligned to a page size. This limitation
1019 * will be lifted in the future.
1020 *
1021 * @as: #AddressSpace to be accessed
1022 * @addr: address within that address space
1023 * @len: length of the area to be checked
1024 * @is_write: indicates the transfer direction
1025 */
1026bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write);
1027
Avi Kivityac1970f2012-10-03 16:22:53 +02001028/* address_space_map: map a physical memory region into a host virtual address
1029 *
1030 * May map a subset of the requested range, given by and returned in @plen.
1031 * May return %NULL if resources needed to perform the mapping are exhausted.
1032 * Use only for reads OR writes - not for read-modify-write operations.
1033 * Use cpu_register_map_client() to know when retrying the map operation is
1034 * likely to succeed.
1035 *
1036 * @as: #AddressSpace to be accessed
1037 * @addr: address within that address space
1038 * @plen: pointer to length of buffer; updated on return
1039 * @is_write: indicates the transfer direction
1040 */
Avi Kivitya8170e52012-10-23 12:30:10 +02001041void *address_space_map(AddressSpace *as, hwaddr addr,
1042 hwaddr *plen, bool is_write);
Avi Kivityac1970f2012-10-03 16:22:53 +02001043
1044/* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1045 *
1046 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1047 * the amount of memory that was actually read or written by the caller.
1048 *
1049 * @as: #AddressSpace used
1050 * @addr: address within that address space
1051 * @len: buffer length as returned by address_space_map()
1052 * @access_len: amount of data actually transferred
1053 * @is_write: indicates the transfer direction
1054 */
Avi Kivitya8170e52012-10-23 12:30:10 +02001055void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
1056 int is_write, hwaddr access_len);
Avi Kivityac1970f2012-10-03 16:22:53 +02001057
1058
Avi Kivity093bc2c2011-07-26 14:26:01 +03001059#endif
1060
1061#endif