blob: 71a27ab10f69fd979212500dc62561d44cb64059 [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
Juan Quintela1ab4c8c2013-10-08 16:14:39 +020019#define DIRTY_MEMORY_VGA 0
20#define DIRTY_MEMORY_CODE 1
21#define DIRTY_MEMORY_MIGRATION 2
22#define DIRTY_MEMORY_NUM 3 /* num of dirty bits */
23
Paolo Bonzini022c62c2012-12-17 18:19:49 +010024#include "exec/cpu-common.h"
Andreas Färberce927ed2013-05-28 14:02:38 +020025#ifndef CONFIG_USER_ONLY
Paolo Bonzini022c62c2012-12-17 18:19:49 +010026#include "exec/hwaddr.h"
Andreas Färberce927ed2013-05-28 14:02:38 +020027#endif
Peter Maydellcc05c432015-04-26 16:49:23 +010028#include "exec/memattrs.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010029#include "qemu/queue.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010030#include "qemu/int128.h"
David Gibson06866572013-05-14 19:13:56 +100031#include "qemu/notify.h"
Peter Crosthwaiteb4fefef2014-06-05 23:15:52 -070032#include "qom/object.h"
Paolo Bonzini374f2982013-05-17 12:37:03 +020033#include "qemu/rcu.h"
Avi Kivity093bc2c2011-07-26 14:26:01 +030034
Paolo Bonzini07bdaa42016-03-25 12:55:08 +010035#define RAM_ADDR_INVALID (~(ram_addr_t)0)
36
Paolo Bonzini052e87b2013-05-27 10:08:27 +020037#define MAX_PHYS_ADDR_SPACE_BITS 62
38#define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
39
Peter Crosthwaiteb4fefef2014-06-05 23:15:52 -070040#define TYPE_MEMORY_REGION "qemu:memory-region"
41#define MEMORY_REGION(obj) \
42 OBJECT_CHECK(MemoryRegion, (obj), TYPE_MEMORY_REGION)
43
Avi Kivity093bc2c2011-07-26 14:26:01 +030044typedef struct MemoryRegionOps MemoryRegionOps;
Avi Kivity74901c32011-07-26 14:26:10 +030045typedef struct MemoryRegionMmio MemoryRegionMmio;
Avi Kivity093bc2c2011-07-26 14:26:01 +030046
Avi Kivity74901c32011-07-26 14:26:10 +030047struct MemoryRegionMmio {
48 CPUReadMemoryFunc *read[3];
49 CPUWriteMemoryFunc *write[3];
50};
51
Avi Kivity30951152012-10-30 13:47:46 +020052typedef struct IOMMUTLBEntry IOMMUTLBEntry;
53
54/* See address_space_translate: bit 0 is read, bit 1 is write. */
55typedef enum {
56 IOMMU_NONE = 0,
57 IOMMU_RO = 1,
58 IOMMU_WO = 2,
59 IOMMU_RW = 3,
60} IOMMUAccessFlags;
61
62struct IOMMUTLBEntry {
63 AddressSpace *target_as;
64 hwaddr iova;
65 hwaddr translated_addr;
66 hwaddr addr_mask; /* 0xfff = 4k translation */
67 IOMMUAccessFlags perm;
68};
69
Peter Maydellcc05c432015-04-26 16:49:23 +010070/* New-style MMIO accessors can indicate that the transaction failed.
71 * A zero (MEMTX_OK) response means success; anything else is a failure
72 * of some kind. The memory subsystem will bitwise-OR together results
73 * if it is synthesizing an operation from multiple smaller accesses.
74 */
75#define MEMTX_OK 0
76#define MEMTX_ERROR (1U << 0) /* device returned an error */
77#define MEMTX_DECODE_ERROR (1U << 1) /* nothing at that address */
78typedef uint32_t MemTxResult;
79
Avi Kivity093bc2c2011-07-26 14:26:01 +030080/*
81 * Memory region callbacks
82 */
83struct MemoryRegionOps {
84 /* Read from the memory region. @addr is relative to @mr; @size is
85 * in bytes. */
86 uint64_t (*read)(void *opaque,
Avi Kivitya8170e52012-10-23 12:30:10 +020087 hwaddr addr,
Avi Kivity093bc2c2011-07-26 14:26:01 +030088 unsigned size);
89 /* Write to the memory region. @addr is relative to @mr; @size is
90 * in bytes. */
91 void (*write)(void *opaque,
Avi Kivitya8170e52012-10-23 12:30:10 +020092 hwaddr addr,
Avi Kivity093bc2c2011-07-26 14:26:01 +030093 uint64_t data,
94 unsigned size);
95
Peter Maydellcc05c432015-04-26 16:49:23 +010096 MemTxResult (*read_with_attrs)(void *opaque,
97 hwaddr addr,
98 uint64_t *data,
99 unsigned size,
100 MemTxAttrs attrs);
101 MemTxResult (*write_with_attrs)(void *opaque,
102 hwaddr addr,
103 uint64_t data,
104 unsigned size,
105 MemTxAttrs attrs);
106
Avi Kivity093bc2c2011-07-26 14:26:01 +0300107 enum device_endian endianness;
108 /* Guest-visible constraints: */
109 struct {
110 /* If nonzero, specify bounds on access sizes beyond which a machine
111 * check is thrown.
112 */
113 unsigned min_access_size;
114 unsigned max_access_size;
115 /* If true, unaligned accesses are supported. Otherwise unaligned
116 * accesses throw machine checks.
117 */
118 bool unaligned;
Avi Kivity897fa7c2011-11-13 13:05:27 +0200119 /*
120 * If present, and returns #false, the transaction is not accepted
121 * by the device (and results in machine dependent behaviour such
122 * as a machine check exception).
123 */
Avi Kivitya8170e52012-10-23 12:30:10 +0200124 bool (*accepts)(void *opaque, hwaddr addr,
Avi Kivity897fa7c2011-11-13 13:05:27 +0200125 unsigned size, bool is_write);
Avi Kivity093bc2c2011-07-26 14:26:01 +0300126 } valid;
127 /* Internal implementation constraints: */
128 struct {
129 /* If nonzero, specifies the minimum size implemented. Smaller sizes
130 * will be rounded upwards and a partial result will be returned.
131 */
132 unsigned min_access_size;
133 /* If nonzero, specifies the maximum size implemented. Larger sizes
134 * will be done as a series of accesses with smaller sizes.
135 */
136 unsigned max_access_size;
137 /* If true, unaligned accesses are supported. Otherwise all accesses
138 * are converted to (possibly multiple) naturally aligned accesses.
139 */
Fam Zhengedc1ba72014-05-05 15:53:41 +0800140 bool unaligned;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300141 } impl;
Avi Kivity627a0e92011-07-26 14:26:09 +0300142
Avi Kivity74901c32011-07-26 14:26:10 +0300143 /* If .read and .write are not present, old_mmio may be used for
144 * backwards compatibility with old mmio registration
145 */
146 const MemoryRegionMmio old_mmio;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300147};
148
Avi Kivity30951152012-10-30 13:47:46 +0200149typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps;
150
151struct MemoryRegionIOMMUOps {
152 /* Return a TLB entry that contains a given address. */
Le Tan8d7b8cb2014-08-16 13:55:37 +0800153 IOMMUTLBEntry (*translate)(MemoryRegion *iommu, hwaddr addr, bool is_write);
Avi Kivity30951152012-10-30 13:47:46 +0200154};
155
Avi Kivity093bc2c2011-07-26 14:26:01 +0300156typedef struct CoalescedMemoryRange CoalescedMemoryRange;
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300157typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300158
159struct MemoryRegion {
Peter Crosthwaiteb4fefef2014-06-05 23:15:52 -0700160 Object parent_obj;
Paolo Bonzinia6768542015-12-09 11:40:14 +0100161
Avi Kivity093bc2c2011-07-26 14:26:01 +0300162 /* All fields are private - violators will be prosecuted */
Paolo Bonzinia6768542015-12-09 11:40:14 +0100163
164 /* The following fields should fit in a cache line */
165 bool romd_mode;
166 bool ram;
167 bool subpage;
168 bool readonly; /* For RAM regions */
169 bool rom_device;
170 bool flush_coalesced_mmio;
171 bool global_locking;
172 uint8_t dirty_log_mask;
Gonglei58eaa212016-02-22 16:34:55 +0800173 RAMBlock *ram_block;
Paolo Bonzini612263c2015-12-09 11:44:25 +0100174 Object *owner;
Avi Kivity30951152012-10-30 13:47:46 +0200175 const MemoryRegionIOMMUOps *iommu_ops;
Paolo Bonzinia6768542015-12-09 11:40:14 +0100176
177 const MemoryRegionOps *ops;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300178 void *opaque;
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +0200179 MemoryRegion *container;
Avi Kivity08dafab2011-10-16 13:19:17 +0200180 Int128 size;
Avi Kivitya8170e52012-10-23 12:30:10 +0200181 hwaddr addr;
Avi Kivity545e92e2011-08-08 19:58:48 +0300182 void (*destructor)(MemoryRegion *mr);
Igor Mammedova2b257d2014-10-31 16:38:37 +0000183 uint64_t align;
Avi Kivity14a3c102011-07-26 14:26:06 +0300184 bool terminates;
Nikunj A Dadhaniae4dc3f52014-09-15 09:28:23 +0530185 bool skip_dump;
Avi Kivity6bba19b2011-09-14 11:54:58 +0300186 bool enabled;
Jan Kiszka1660e722011-10-23 16:01:19 +0200187 bool warning_printed; /* For reservations */
Paolo Bonzinideb809e2015-07-14 13:56:53 +0200188 uint8_t vga_logging_count;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300189 MemoryRegion *alias;
Avi Kivitya8170e52012-10-23 12:30:10 +0200190 hwaddr alias_offset;
Peter Crosthwaited33382d2014-06-05 23:17:01 -0700191 int32_t priority;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300192 QTAILQ_HEAD(subregions, MemoryRegion) subregions;
193 QTAILQ_ENTRY(MemoryRegion) subregions_link;
194 QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced;
Peter Maydell302fa282014-08-19 20:05:46 +0100195 const char *name;
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300196 unsigned ioeventfd_nb;
197 MemoryRegionIoeventfd *ioeventfds;
David Gibson06866572013-05-14 19:13:56 +1000198 NotifierList iommu_notify;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300199};
200
Paolo Bonzinic2fc83e2013-06-02 15:20:47 +0200201/**
202 * MemoryListener: callbacks structure for updates to the physical memory map
203 *
204 * Allows a component to adjust to changes in the guest-visible memory map.
205 * Use with memory_listener_register() and memory_listener_unregister().
206 */
207struct MemoryListener {
208 void (*begin)(MemoryListener *listener);
209 void (*commit)(MemoryListener *listener);
210 void (*region_add)(MemoryListener *listener, MemoryRegionSection *section);
211 void (*region_del)(MemoryListener *listener, MemoryRegionSection *section);
212 void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section);
Paolo Bonzinib2dfd712015-04-25 14:38:30 +0200213 void (*log_start)(MemoryListener *listener, MemoryRegionSection *section,
214 int old, int new);
215 void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section,
216 int old, int new);
Paolo Bonzinic2fc83e2013-06-02 15:20:47 +0200217 void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section);
218 void (*log_global_start)(MemoryListener *listener);
219 void (*log_global_stop)(MemoryListener *listener);
220 void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section,
221 bool match_data, uint64_t data, EventNotifier *e);
222 void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section,
223 bool match_data, uint64_t data, EventNotifier *e);
224 void (*coalesced_mmio_add)(MemoryListener *listener, MemoryRegionSection *section,
225 hwaddr addr, hwaddr len);
226 void (*coalesced_mmio_del)(MemoryListener *listener, MemoryRegionSection *section,
227 hwaddr addr, hwaddr len);
228 /* Lower = earlier (during add), later (during del) */
229 unsigned priority;
230 AddressSpace *address_space_filter;
231 QTAILQ_ENTRY(MemoryListener) link;
232};
233
Avi Kivity9ad2bbc2012-10-02 14:59:23 +0200234/**
235 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
236 */
237struct AddressSpace {
238 /* All fields are private. */
Paolo Bonzini374f2982013-05-17 12:37:03 +0200239 struct rcu_head rcu;
Alexey Kardashevskiy7dca8042013-04-29 16:25:51 +0000240 char *name;
Avi Kivity9ad2bbc2012-10-02 14:59:23 +0200241 MemoryRegion *root;
Peter Crosthwaitef0c02d12016-01-21 14:15:06 +0000242 int ref_count;
243 bool malloced;
Paolo Bonzini374f2982013-05-17 12:37:03 +0200244
245 /* Accessed via RCU. */
Avi Kivity9ad2bbc2012-10-02 14:59:23 +0200246 struct FlatView *current_map;
Paolo Bonzini374f2982013-05-17 12:37:03 +0200247
Avi Kivity9ad2bbc2012-10-02 14:59:23 +0200248 int ioeventfd_nb;
249 struct MemoryRegionIoeventfd *ioeventfds;
Avi Kivityac1970f2012-10-03 16:22:53 +0200250 struct AddressSpaceDispatch *dispatch;
Paolo Bonzini00752702013-05-29 12:13:54 +0200251 struct AddressSpaceDispatch *next_dispatch;
Paolo Bonzini89ae3372013-06-02 10:39:07 +0200252 MemoryListener dispatch_listener;
253
Avi Kivity0d673e32012-10-02 15:28:50 +0200254 QTAILQ_ENTRY(AddressSpace) address_spaces_link;
Avi Kivity9ad2bbc2012-10-02 14:59:23 +0200255};
256
Avi Kivitye2177952011-12-08 15:00:18 +0200257/**
258 * MemoryRegionSection: describes a fragment of a #MemoryRegion
259 *
260 * @mr: the region, or %NULL if empty
Avi Kivity7664e802011-12-11 14:47:25 +0200261 * @address_space: the address space the region is mapped in
Avi Kivitye2177952011-12-08 15:00:18 +0200262 * @offset_within_region: the beginning of the section, relative to @mr's start
263 * @size: the size of the section; will not exceed @mr's boundaries
264 * @offset_within_address_space: the address of the first byte of the section
265 * relative to the region's address space
Avi Kivity7a8499e2012-02-08 17:01:23 +0200266 * @readonly: writes to this section are ignored
Avi Kivitye2177952011-12-08 15:00:18 +0200267 */
268struct MemoryRegionSection {
269 MemoryRegion *mr;
Avi Kivityf6790af2012-10-02 20:13:51 +0200270 AddressSpace *address_space;
Avi Kivitya8170e52012-10-23 12:30:10 +0200271 hwaddr offset_within_region;
Paolo Bonzini052e87b2013-05-27 10:08:27 +0200272 Int128 size;
Avi Kivitya8170e52012-10-23 12:30:10 +0200273 hwaddr offset_within_address_space;
Avi Kivity7a8499e2012-02-08 17:01:23 +0200274 bool readonly;
Avi Kivitye2177952011-12-08 15:00:18 +0200275};
276
Avi Kivity093bc2c2011-07-26 14:26:01 +0300277/**
278 * memory_region_init: Initialize a memory region
279 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300280 * The region typically acts as a container for other memory regions. Use
Avi Kivity093bc2c2011-07-26 14:26:01 +0300281 * memory_region_add_subregion() to add subregions.
282 *
283 * @mr: the #MemoryRegion to be initialized
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400284 * @owner: the object that tracks the region's reference count
Avi Kivity093bc2c2011-07-26 14:26:01 +0300285 * @name: used for debugging; not visible to the user or ABI
286 * @size: size of the region; any subregions beyond this size will be clipped
287 */
288void memory_region_init(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400289 struct Object *owner,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300290 const char *name,
291 uint64_t size);
Paolo Bonzini46637be2013-05-07 09:06:00 +0200292
293/**
294 * memory_region_ref: Add 1 to a memory region's reference count
295 *
296 * Whenever memory regions are accessed outside the BQL, they need to be
297 * preserved against hot-unplug. MemoryRegions actually do not have their
298 * own reference count; they piggyback on a QOM object, their "owner".
299 * This function adds a reference to the owner.
300 *
301 * All MemoryRegions must have an owner if they can disappear, even if the
302 * device they belong to operates exclusively under the BQL. This is because
303 * the region could be returned at any time by memory_region_find, and this
304 * is usually under guest control.
305 *
306 * @mr: the #MemoryRegion
307 */
308void memory_region_ref(MemoryRegion *mr);
309
310/**
311 * memory_region_unref: Remove 1 to a memory region's reference count
312 *
313 * Whenever memory regions are accessed outside the BQL, they need to be
314 * preserved against hot-unplug. MemoryRegions actually do not have their
315 * own reference count; they piggyback on a QOM object, their "owner".
316 * This function removes a reference to the owner and possibly destroys it.
317 *
318 * @mr: the #MemoryRegion
319 */
320void memory_region_unref(MemoryRegion *mr);
321
Avi Kivity093bc2c2011-07-26 14:26:01 +0300322/**
323 * memory_region_init_io: Initialize an I/O memory region.
324 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300325 * Accesses into the region will cause the callbacks in @ops to be called.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300326 * if @size is nonzero, subregions will be clipped to @size.
327 *
328 * @mr: the #MemoryRegion to be initialized.
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400329 * @owner: the object that tracks the region's reference count
Avi Kivity093bc2c2011-07-26 14:26:01 +0300330 * @ops: a structure containing read and write callbacks to be used when
331 * I/O is performed on the region.
Daniel P. Berrangeb6af0972015-08-26 12:17:13 +0100332 * @opaque: passed to the read and write callbacks of the @ops structure.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300333 * @name: used for debugging; not visible to the user or ABI
334 * @size: size of the region.
335 */
336void memory_region_init_io(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400337 struct Object *owner,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300338 const MemoryRegionOps *ops,
339 void *opaque,
340 const char *name,
341 uint64_t size);
342
343/**
344 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300345 * region will modify memory directly.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300346 *
347 * @mr: the #MemoryRegion to be initialized.
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400348 * @owner: the object that tracks the region's reference count
Avi Kivityc5705a72011-12-20 15:59:12 +0200349 * @name: the name of the region.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300350 * @size: size of the region.
Hu Tao49946532014-09-09 13:27:55 +0800351 * @errp: pointer to Error*, to store an error if it happens.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300352 */
353void memory_region_init_ram(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400354 struct Object *owner,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300355 const char *name,
Hu Tao49946532014-09-09 13:27:55 +0800356 uint64_t size,
357 Error **errp);
Avi Kivity093bc2c2011-07-26 14:26:01 +0300358
Michael S. Tsirkin60786ef2014-11-17 00:24:36 +0200359/**
360 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
361 * RAM. Accesses into the region will
362 * modify memory directly. Only an initial
363 * portion of this RAM is actually used.
364 * The used size can change across reboots.
365 *
366 * @mr: the #MemoryRegion to be initialized.
367 * @owner: the object that tracks the region's reference count
368 * @name: the name of the region.
369 * @size: used size of the region.
370 * @max_size: max size of the region.
371 * @resized: callback to notify owner about used size change.
372 * @errp: pointer to Error*, to store an error if it happens.
373 */
374void memory_region_init_resizeable_ram(MemoryRegion *mr,
375 struct Object *owner,
376 const char *name,
377 uint64_t size,
378 uint64_t max_size,
379 void (*resized)(const char*,
380 uint64_t length,
381 void *host),
382 Error **errp);
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800383#ifdef __linux__
384/**
385 * memory_region_init_ram_from_file: Initialize RAM memory region with a
386 * mmap-ed backend.
387 *
388 * @mr: the #MemoryRegion to be initialized.
389 * @owner: the object that tracks the region's reference count
390 * @name: the name of the region.
391 * @size: size of the region.
Paolo Bonzinidbcb8982014-06-10 19:15:24 +0800392 * @share: %true if memory must be mmaped with the MAP_SHARED flag
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800393 * @path: the path in which to allocate the RAM.
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800394 * @errp: pointer to Error*, to store an error if it happens.
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800395 */
396void memory_region_init_ram_from_file(MemoryRegion *mr,
397 struct Object *owner,
398 const char *name,
399 uint64_t size,
Paolo Bonzinidbcb8982014-06-10 19:15:24 +0800400 bool share,
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800401 const char *path,
402 Error **errp);
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800403#endif
404
Avi Kivity093bc2c2011-07-26 14:26:01 +0300405/**
BALATON Zoltan1a7e8ca2012-08-22 17:18:38 +0200406 * memory_region_init_ram_ptr: Initialize RAM memory region from a
407 * user-provided pointer. Accesses into the
408 * region will modify memory directly.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300409 *
410 * @mr: the #MemoryRegion to be initialized.
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400411 * @owner: the object that tracks the region's reference count
Avi Kivityc5705a72011-12-20 15:59:12 +0200412 * @name: the name of the region.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300413 * @size: size of the region.
414 * @ptr: memory to be mapped; must contain at least @size bytes.
415 */
416void memory_region_init_ram_ptr(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400417 struct Object *owner,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300418 const char *name,
419 uint64_t size,
420 void *ptr);
421
422/**
423 * memory_region_init_alias: Initialize a memory region that aliases all or a
424 * part of another memory region.
425 *
426 * @mr: the #MemoryRegion to be initialized.
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400427 * @owner: the object that tracks the region's reference count
Avi Kivity093bc2c2011-07-26 14:26:01 +0300428 * @name: used for debugging; not visible to the user or ABI
429 * @orig: the region to be referenced; @mr will be equivalent to
430 * @orig between @offset and @offset + @size - 1.
431 * @offset: start of the section in @orig to be referenced.
432 * @size: size of the region.
433 */
434void memory_region_init_alias(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400435 struct Object *owner,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300436 const char *name,
437 MemoryRegion *orig,
Avi Kivitya8170e52012-10-23 12:30:10 +0200438 hwaddr offset,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300439 uint64_t size);
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300440
441/**
442 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
443 * handled via callbacks.
444 *
Pavel Fedin6d6d2ab2015-08-13 11:26:21 +0100445 * If NULL callbacks pointer is given, then I/O space is not supposed to be
446 * handled by QEMU itself. Any access via the memory API will cause an abort().
447 *
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300448 * @mr: the #MemoryRegion to be initialized.
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400449 * @owner: the object that tracks the region's reference count
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300450 * @ops: callbacks for write access handling.
Avi Kivityc5705a72011-12-20 15:59:12 +0200451 * @name: the name of the region.
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300452 * @size: size of the region.
Hu Tao33e0eb52014-09-09 13:27:57 +0800453 * @errp: pointer to Error*, to store an error if it happens.
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300454 */
455void memory_region_init_rom_device(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400456 struct Object *owner,
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300457 const MemoryRegionOps *ops,
Avi Kivity75f59412011-08-26 00:35:15 +0300458 void *opaque,
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300459 const char *name,
Hu Tao33e0eb52014-09-09 13:27:57 +0800460 uint64_t size,
461 Error **errp);
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300462
Avi Kivity093bc2c2011-07-26 14:26:01 +0300463/**
Jan Kiszka1660e722011-10-23 16:01:19 +0200464 * memory_region_init_reservation: Initialize a memory region that reserves
465 * I/O space.
466 *
467 * A reservation region primariy serves debugging purposes. It claims I/O
468 * space that is not supposed to be handled by QEMU itself. Any access via
469 * the memory API will cause an abort().
Pavel Fedin6d6d2ab2015-08-13 11:26:21 +0100470 * This function is deprecated. Use memory_region_init_io() with NULL
471 * callbacks instead.
Jan Kiszka1660e722011-10-23 16:01:19 +0200472 *
473 * @mr: the #MemoryRegion to be initialized
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400474 * @owner: the object that tracks the region's reference count
Jan Kiszka1660e722011-10-23 16:01:19 +0200475 * @name: used for debugging; not visible to the user or ABI
476 * @size: size of the region.
477 */
Pavel Fedin6d6d2ab2015-08-13 11:26:21 +0100478static inline void memory_region_init_reservation(MemoryRegion *mr,
479 Object *owner,
Jan Kiszka1660e722011-10-23 16:01:19 +0200480 const char *name,
Pavel Fedin6d6d2ab2015-08-13 11:26:21 +0100481 uint64_t size)
482{
483 memory_region_init_io(mr, owner, NULL, mr, name, size);
484}
Avi Kivity30951152012-10-30 13:47:46 +0200485
486/**
487 * memory_region_init_iommu: Initialize a memory region that translates
488 * addresses
489 *
490 * An IOMMU region translates addresses and forwards accesses to a target
491 * memory region.
492 *
493 * @mr: the #MemoryRegion to be initialized
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400494 * @owner: the object that tracks the region's reference count
Avi Kivity30951152012-10-30 13:47:46 +0200495 * @ops: a function that translates addresses into the @target region
496 * @name: used for debugging; not visible to the user or ABI
497 * @size: size of the region.
498 */
499void memory_region_init_iommu(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400500 struct Object *owner,
Avi Kivity30951152012-10-30 13:47:46 +0200501 const MemoryRegionIOMMUOps *ops,
502 const char *name,
503 uint64_t size);
504
Jan Kiszka1660e722011-10-23 16:01:19 +0200505/**
Paolo Bonzini803c0812013-05-07 06:59:09 +0200506 * memory_region_owner: get a memory region's owner.
507 *
508 * @mr: the memory region being queried.
509 */
510struct Object *memory_region_owner(MemoryRegion *mr);
511
512/**
Avi Kivity093bc2c2011-07-26 14:26:01 +0300513 * memory_region_size: get a memory region's size.
514 *
515 * @mr: the memory region being queried.
516 */
517uint64_t memory_region_size(MemoryRegion *mr);
518
519/**
Avi Kivity8ea92522011-12-08 15:58:43 +0200520 * memory_region_is_ram: check whether a memory region is random access
521 *
522 * Returns %true is a memory region is random access.
523 *
524 * @mr: the memory region being queried
525 */
Paolo Bonzini1619d1f2015-12-09 17:47:39 +0100526static inline bool memory_region_is_ram(MemoryRegion *mr)
527{
528 return mr->ram;
529}
Avi Kivity8ea92522011-12-08 15:58:43 +0200530
531/**
Nikunj A Dadhaniae4dc3f52014-09-15 09:28:23 +0530532 * memory_region_is_skip_dump: check whether a memory region should not be
533 * dumped
534 *
535 * Returns %true is a memory region should not be dumped(e.g. VFIO BAR MMAP).
536 *
537 * @mr: the memory region being queried
538 */
539bool memory_region_is_skip_dump(MemoryRegion *mr);
540
541/**
542 * memory_region_set_skip_dump: Set skip_dump flag, dump will ignore this memory
543 * region
544 *
545 * @mr: the memory region being queried
546 */
547void memory_region_set_skip_dump(MemoryRegion *mr);
548
549/**
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +0200550 * memory_region_is_romd: check whether a memory region is in ROMD mode
Blue Swirlfd062572012-04-09 17:38:52 +0000551 *
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +0200552 * Returns %true if a memory region is a ROM device and currently set to allow
Blue Swirlfd062572012-04-09 17:38:52 +0000553 * direct reads.
554 *
555 * @mr: the memory region being queried
556 */
557static inline bool memory_region_is_romd(MemoryRegion *mr)
558{
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +0200559 return mr->rom_device && mr->romd_mode;
Blue Swirlfd062572012-04-09 17:38:52 +0000560}
561
562/**
Avi Kivity30951152012-10-30 13:47:46 +0200563 * memory_region_is_iommu: check whether a memory region is an iommu
564 *
565 * Returns %true is a memory region is an iommu.
566 *
567 * @mr: the memory region being queried
568 */
Paolo Bonzini1619d1f2015-12-09 17:47:39 +0100569static inline bool memory_region_is_iommu(MemoryRegion *mr)
570{
571 return mr->iommu_ops;
572}
573
Avi Kivity30951152012-10-30 13:47:46 +0200574
575/**
David Gibson06866572013-05-14 19:13:56 +1000576 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
577 *
578 * @mr: the memory region that was changed
579 * @entry: the new entry in the IOMMU translation table. The entry
580 * replaces all old entries for the same virtual I/O address range.
581 * Deleted entries have .@perm == 0.
582 */
583void memory_region_notify_iommu(MemoryRegion *mr,
584 IOMMUTLBEntry entry);
585
586/**
587 * memory_region_register_iommu_notifier: register a notifier for changes to
588 * IOMMU translation entries.
589 *
590 * @mr: the memory region to observe
591 * @n: the notifier to be added; the notifier receives a pointer to an
592 * #IOMMUTLBEntry as the opaque value; the pointer ceases to be
593 * valid on exit from the notifier.
594 */
595void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n);
596
597/**
David Gibsona788f222015-09-30 12:13:55 +1000598 * memory_region_iommu_replay: replay existing IOMMU translations to
599 * a notifier
600 *
601 * @mr: the memory region to observe
602 * @n: the notifier to which to replay iommu mappings
603 * @granularity: Minimum page granularity to replay notifications for
604 * @is_write: Whether to treat the replay as a translate "write"
605 * through the iommu
606 */
607void memory_region_iommu_replay(MemoryRegion *mr, Notifier *n,
608 hwaddr granularity, bool is_write);
609
610/**
David Gibson06866572013-05-14 19:13:56 +1000611 * memory_region_unregister_iommu_notifier: unregister a notifier for
612 * changes to IOMMU translation entries.
613 *
614 * @n: the notifier to be removed.
615 */
616void memory_region_unregister_iommu_notifier(Notifier *n);
617
618/**
Avi Kivity8991c792011-12-20 15:53:11 +0200619 * memory_region_name: get a memory region's name
620 *
621 * Returns the string that was used to initialize the memory region.
622 *
623 * @mr: the memory region being queried
624 */
Peter Crosthwaite5d546d42014-08-14 23:55:03 -0700625const char *memory_region_name(const MemoryRegion *mr);
Avi Kivity8991c792011-12-20 15:53:11 +0200626
627/**
Avi Kivity55043ba2011-12-15 17:20:34 +0200628 * memory_region_is_logging: return whether a memory region is logging writes
629 *
Paolo Bonzini2d1a35b2015-03-23 10:50:57 +0100630 * Returns %true if the memory region is logging writes for the given client
631 *
632 * @mr: the memory region being queried
633 * @client: the client being queried
634 */
635bool memory_region_is_logging(MemoryRegion *mr, uint8_t client);
636
637/**
638 * memory_region_get_dirty_log_mask: return the clients for which a
639 * memory region is logging writes.
640 *
Paolo Bonzini677e7802015-03-23 10:53:21 +0100641 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
642 * are the bit indices.
Avi Kivity55043ba2011-12-15 17:20:34 +0200643 *
644 * @mr: the memory region being queried
645 */
Paolo Bonzini2d1a35b2015-03-23 10:50:57 +0100646uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr);
Avi Kivity55043ba2011-12-15 17:20:34 +0200647
648/**
Avi Kivityce7923d2011-12-08 16:05:11 +0200649 * memory_region_is_rom: check whether a memory region is ROM
650 *
651 * Returns %true is a memory region is read-only memory.
652 *
653 * @mr: the memory region being queried
654 */
Paolo Bonzini1619d1f2015-12-09 17:47:39 +0100655static inline bool memory_region_is_rom(MemoryRegion *mr)
656{
657 return mr->ram && mr->readonly;
658}
659
Avi Kivityce7923d2011-12-08 16:05:11 +0200660
661/**
Paolo Bonzinia35ba7b2014-06-10 19:15:23 +0800662 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
663 *
664 * Returns a file descriptor backing a file-based RAM memory region,
665 * or -1 if the region is not a file-based RAM memory region.
666 *
667 * @mr: the RAM or alias memory region being queried.
668 */
669int memory_region_get_fd(MemoryRegion *mr);
670
671/**
Paolo Bonzini4ff87572016-03-25 12:30:16 +0100672 * memory_region_set_fd: Mark a RAM memory region as backed by a
673 * file descriptor.
674 *
675 * This function is typically used after memory_region_init_ram_ptr().
676 *
677 * @mr: the memory region being queried.
678 * @fd: the file descriptor that backs @mr.
679 */
680void memory_region_set_fd(MemoryRegion *mr, int fd);
681
682/**
Paolo Bonzini07bdaa42016-03-25 12:55:08 +0100683 * memory_region_from_host: Convert a pointer into a RAM memory region
684 * and an offset within it.
685 *
686 * Given a host pointer inside a RAM memory region (created with
687 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
688 * the MemoryRegion and the offset within it.
689 *
690 * Use with care; by the time this function returns, the returned pointer is
691 * not protected by RCU anymore. If the caller is not within an RCU critical
692 * section and does not hold the iothread lock, it must have other means of
693 * protecting the pointer, such as a reference to the region that includes
694 * the incoming ram_addr_t.
695 *
696 * @mr: the memory region being queried.
697 */
698MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset);
699
700/**
Avi Kivity093bc2c2011-07-26 14:26:01 +0300701 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
702 *
703 * Returns a host pointer to a RAM memory region (created with
Paolo Bonzini49b24af2015-12-16 10:30:47 +0100704 * memory_region_init_ram() or memory_region_init_ram_ptr()).
705 *
706 * Use with care; by the time this function returns, the returned pointer is
707 * not protected by RCU anymore. If the caller is not within an RCU critical
708 * section and does not hold the iothread lock, it must have other means of
709 * protecting the pointer, such as a reference to the region that includes
710 * the incoming ram_addr_t.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300711 *
712 * @mr: the memory region being queried.
713 */
714void *memory_region_get_ram_ptr(MemoryRegion *mr);
715
Paolo Bonzini37d7c082015-03-23 10:21:46 +0100716/* memory_region_ram_resize: Resize a RAM region.
717 *
718 * Only legal before guest might have detected the memory size: e.g. on
719 * incoming migration, or right after reset.
720 *
721 * @mr: a memory region created with @memory_region_init_resizeable_ram.
722 * @newsize: the new size the region
723 * @errp: pointer to Error*, to store an error if it happens.
724 */
725void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize,
726 Error **errp);
727
Avi Kivity093bc2c2011-07-26 14:26:01 +0300728/**
Avi Kivity093bc2c2011-07-26 14:26:01 +0300729 * memory_region_set_log: Turn dirty logging on or off for a region.
730 *
731 * Turns dirty logging on or off for a specified client (display, migration).
732 * Only meaningful for RAM regions.
733 *
734 * @mr: the memory region being updated.
735 * @log: whether dirty logging is to be enabled or disabled.
Paolo Bonzinidbddac62015-03-23 10:31:53 +0100736 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300737 */
738void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
739
740/**
Blue Swirlcd7a45c2012-01-22 16:38:21 +0000741 * memory_region_get_dirty: Check whether a range of bytes is dirty
742 * for a specified client.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300743 *
Blue Swirlcd7a45c2012-01-22 16:38:21 +0000744 * Checks whether a range of bytes has been written to since the last
Avi Kivity093bc2c2011-07-26 14:26:01 +0300745 * call to memory_region_reset_dirty() with the same @client. Dirty logging
746 * must be enabled.
747 *
748 * @mr: the memory region being queried.
749 * @addr: the address (relative to the start of the region) being queried.
Blue Swirlcd7a45c2012-01-22 16:38:21 +0000750 * @size: the size of the range being queried.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300751 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
752 * %DIRTY_MEMORY_VGA.
753 */
Avi Kivitya8170e52012-10-23 12:30:10 +0200754bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
755 hwaddr size, unsigned client);
Avi Kivity093bc2c2011-07-26 14:26:01 +0300756
757/**
Blue Swirlfd4aa972011-10-16 16:04:59 +0000758 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300759 *
Blue Swirlfd4aa972011-10-16 16:04:59 +0000760 * Marks a range of bytes as dirty, after it has been dirtied outside
761 * guest code.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300762 *
Blue Swirlfd4aa972011-10-16 16:04:59 +0000763 * @mr: the memory region being dirtied.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300764 * @addr: the address (relative to the start of the region) being dirtied.
Blue Swirlfd4aa972011-10-16 16:04:59 +0000765 * @size: size of the range being dirtied.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300766 */
Avi Kivitya8170e52012-10-23 12:30:10 +0200767void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
768 hwaddr size);
Avi Kivity093bc2c2011-07-26 14:26:01 +0300769
770/**
Juan Quintela6c279db2012-10-17 20:24:28 +0200771 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty
772 * for a specified client. It clears them.
773 *
774 * Checks whether a range of bytes has been written to since the last
775 * call to memory_region_reset_dirty() with the same @client. Dirty logging
776 * must be enabled.
777 *
778 * @mr: the memory region being queried.
779 * @addr: the address (relative to the start of the region) being queried.
780 * @size: the size of the range being queried.
781 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
782 * %DIRTY_MEMORY_VGA.
783 */
784bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr,
785 hwaddr size, unsigned client);
786/**
Avi Kivity093bc2c2011-07-26 14:26:01 +0300787 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
788 * any external TLBs (e.g. kvm)
789 *
790 * Flushes dirty information from accelerators such as kvm and vhost-net
791 * and makes it available to users of the memory API.
792 *
793 * @mr: the region being flushed.
794 */
795void memory_region_sync_dirty_bitmap(MemoryRegion *mr);
796
797/**
798 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
799 * client.
800 *
801 * Marks a range of pages as no longer dirty.
802 *
803 * @mr: the region being updated.
804 * @addr: the start of the subrange being cleaned.
805 * @size: the size of the subrange being cleaned.
806 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
807 * %DIRTY_MEMORY_VGA.
808 */
Avi Kivitya8170e52012-10-23 12:30:10 +0200809void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
810 hwaddr size, unsigned client);
Avi Kivity093bc2c2011-07-26 14:26:01 +0300811
812/**
813 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
814 *
815 * Allows a memory region to be marked as read-only (turning it into a ROM).
816 * only useful on RAM regions.
817 *
818 * @mr: the region being updated.
819 * @readonly: whether rhe region is to be ROM or RAM.
820 */
821void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
822
823/**
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +0200824 * memory_region_rom_device_set_romd: enable/disable ROMD mode
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300825 *
826 * Allows a ROM device (initialized with memory_region_init_rom_device() to
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +0200827 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
828 * device is mapped to guest memory and satisfies read access directly.
829 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
830 * Writes are always handled by the #MemoryRegion.write function.
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300831 *
832 * @mr: the memory region to be updated
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +0200833 * @romd_mode: %true to put the region into ROMD mode
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300834 */
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +0200835void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode);
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300836
837/**
Avi Kivity093bc2c2011-07-26 14:26:01 +0300838 * memory_region_set_coalescing: Enable memory coalescing for the region.
839 *
840 * Enabled writes to a region to be queued for later processing. MMIO ->write
841 * callbacks may be delayed until a non-coalesced MMIO is issued.
842 * Only useful for IO regions. Roughly similar to write-combining hardware.
843 *
844 * @mr: the memory region to be write coalesced
845 */
846void memory_region_set_coalescing(MemoryRegion *mr);
847
848/**
849 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
850 * a region.
851 *
852 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
853 * Multiple calls can be issued coalesced disjoint ranges.
854 *
855 * @mr: the memory region to be updated.
856 * @offset: the start of the range within the region to be coalesced.
857 * @size: the size of the subrange to be coalesced.
858 */
859void memory_region_add_coalescing(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +0200860 hwaddr offset,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300861 uint64_t size);
862
863/**
864 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
865 *
866 * Disables any coalescing caused by memory_region_set_coalescing() or
867 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
868 * hardware.
869 *
870 * @mr: the memory region to be updated.
871 */
872void memory_region_clear_coalescing(MemoryRegion *mr);
873
874/**
Jan Kiszkad4105152012-08-23 13:02:29 +0200875 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
876 * accesses.
877 *
878 * Ensure that pending coalesced MMIO request are flushed before the memory
879 * region is accessed. This property is automatically enabled for all regions
880 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
881 *
882 * @mr: the memory region to be updated.
883 */
884void memory_region_set_flush_coalesced(MemoryRegion *mr);
885
886/**
887 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
888 * accesses.
889 *
890 * Clear the automatic coalesced MMIO flushing enabled via
891 * memory_region_set_flush_coalesced. Note that this service has no effect on
892 * memory regions that have MMIO coalescing enabled for themselves. For them,
893 * automatic flushing will stop once coalescing is disabled.
894 *
895 * @mr: the memory region to be updated.
896 */
897void memory_region_clear_flush_coalesced(MemoryRegion *mr);
898
899/**
Jan Kiszka196ea132015-06-18 18:47:20 +0200900 * memory_region_set_global_locking: Declares the access processing requires
901 * QEMU's global lock.
902 *
903 * When this is invoked, accesses to the memory region will be processed while
904 * holding the global lock of QEMU. This is the default behavior of memory
905 * regions.
906 *
907 * @mr: the memory region to be updated.
908 */
909void memory_region_set_global_locking(MemoryRegion *mr);
910
911/**
912 * memory_region_clear_global_locking: Declares that access processing does
913 * not depend on the QEMU global lock.
914 *
915 * By clearing this property, accesses to the memory region will be processed
916 * outside of QEMU's global lock (unless the lock is held on when issuing the
917 * access request). In this case, the device model implementing the access
918 * handlers is responsible for synchronization of concurrency.
919 *
920 * @mr: the memory region to be updated.
921 */
922void memory_region_clear_global_locking(MemoryRegion *mr);
923
924/**
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300925 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
926 * is written to a location.
927 *
928 * Marks a word in an IO region (initialized with memory_region_init_io())
929 * 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 -0300930 * The caller must be prepared to handle failure (that is, take the required
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300931 * action if the callback _is_ called).
932 *
933 * @mr: the memory region being updated.
934 * @addr: the address within @mr that is to be monitored
935 * @size: the size of the access to trigger the eventfd
936 * @match_data: whether to match against @data, instead of just @addr
937 * @data: the data to match against the guest write
938 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
939 **/
940void memory_region_add_eventfd(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +0200941 hwaddr addr,
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300942 unsigned size,
943 bool match_data,
944 uint64_t data,
Paolo Bonzini753d5e12012-07-05 17:16:27 +0200945 EventNotifier *e);
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300946
947/**
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300948 * memory_region_del_eventfd: Cancel an eventfd.
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300949 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300950 * Cancels an eventfd trigger requested by a previous
951 * memory_region_add_eventfd() call.
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300952 *
953 * @mr: the memory region being updated.
954 * @addr: the address within @mr that is to be monitored
955 * @size: the size of the access to trigger the eventfd
956 * @match_data: whether to match against @data, instead of just @addr
957 * @data: the data to match against the guest write
958 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
959 */
960void memory_region_del_eventfd(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +0200961 hwaddr addr,
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300962 unsigned size,
963 bool match_data,
964 uint64_t data,
Paolo Bonzini753d5e12012-07-05 17:16:27 +0200965 EventNotifier *e);
966
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300967/**
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300968 * memory_region_add_subregion: Add a subregion to a container.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300969 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300970 * Adds a subregion at @offset. The subregion may not overlap with other
Avi Kivity093bc2c2011-07-26 14:26:01 +0300971 * subregions (except for those explicitly marked as overlapping). A region
972 * may only be added once as a subregion (unless removed with
973 * memory_region_del_subregion()); use memory_region_init_alias() if you
974 * want a region to be a subregion in multiple locations.
975 *
976 * @mr: the region to contain the new subregion; must be a container
977 * initialized with memory_region_init().
978 * @offset: the offset relative to @mr where @subregion is added.
979 * @subregion: the subregion to be added.
980 */
981void memory_region_add_subregion(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +0200982 hwaddr offset,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300983 MemoryRegion *subregion);
984/**
BALATON Zoltan1a7e8ca2012-08-22 17:18:38 +0200985 * memory_region_add_subregion_overlap: Add a subregion to a container
986 * with overlap.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300987 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300988 * Adds a subregion at @offset. The subregion may overlap with other
Avi Kivity093bc2c2011-07-26 14:26:01 +0300989 * subregions. Conflicts are resolved by having a higher @priority hide a
990 * lower @priority. Subregions without priority are taken as @priority 0.
991 * A region may only be added once as a subregion (unless removed with
992 * memory_region_del_subregion()); use memory_region_init_alias() if you
993 * want a region to be a subregion in multiple locations.
994 *
995 * @mr: the region to contain the new subregion; must be a container
996 * initialized with memory_region_init().
997 * @offset: the offset relative to @mr where @subregion is added.
998 * @subregion: the subregion to be added.
999 * @priority: used for resolving overlaps; highest priority wins.
1000 */
1001void memory_region_add_subregion_overlap(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +02001002 hwaddr offset,
Avi Kivity093bc2c2011-07-26 14:26:01 +03001003 MemoryRegion *subregion,
Marcel Apfelbauma1ff8ae2013-09-16 11:21:14 +03001004 int priority);
Avi Kivitye34911c2011-12-19 12:06:23 +02001005
1006/**
1007 * memory_region_get_ram_addr: Get the ram address associated with a memory
1008 * region
Avi Kivitye34911c2011-12-19 12:06:23 +02001009 */
Fam Zheng7ebb2742016-03-01 14:18:20 +08001010ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr);
Avi Kivitye34911c2011-12-19 12:06:23 +02001011
Igor Mammedova2b257d2014-10-31 16:38:37 +00001012uint64_t memory_region_get_alignment(const MemoryRegion *mr);
Avi Kivity093bc2c2011-07-26 14:26:01 +03001013/**
1014 * memory_region_del_subregion: Remove a subregion.
1015 *
1016 * Removes a subregion from its container.
1017 *
1018 * @mr: the container to be updated.
1019 * @subregion: the region being removed; must be a current subregion of @mr.
1020 */
1021void memory_region_del_subregion(MemoryRegion *mr,
1022 MemoryRegion *subregion);
1023
Avi Kivity6bba19b2011-09-14 11:54:58 +03001024/*
1025 * memory_region_set_enabled: dynamically enable or disable a region
1026 *
1027 * Enables or disables a memory region. A disabled memory region
1028 * ignores all accesses to itself and its subregions. It does not
1029 * obscure sibling subregions with lower priority - it simply behaves as
1030 * if it was removed from the hierarchy.
1031 *
1032 * Regions default to being enabled.
1033 *
1034 * @mr: the region to be updated
1035 * @enabled: whether to enable or disable the region
1036 */
1037void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
1038
Avi Kivity2282e1a2011-09-14 12:10:12 +03001039/*
1040 * memory_region_set_address: dynamically update the address of a region
1041 *
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +02001042 * Dynamically updates the address of a region, relative to its container.
Avi Kivity2282e1a2011-09-14 12:10:12 +03001043 * May be used on regions are currently part of a memory hierarchy.
1044 *
1045 * @mr: the region to be updated
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +02001046 * @addr: new address, relative to container region
Avi Kivity2282e1a2011-09-14 12:10:12 +03001047 */
Avi Kivitya8170e52012-10-23 12:30:10 +02001048void memory_region_set_address(MemoryRegion *mr, hwaddr addr);
Avi Kivity2282e1a2011-09-14 12:10:12 +03001049
Avi Kivity47033592011-12-04 19:16:50 +02001050/*
Michael S. Tsirkine7af4c62014-12-16 11:21:23 +02001051 * memory_region_set_size: dynamically update the size of a region.
1052 *
1053 * Dynamically updates the size of a region.
1054 *
1055 * @mr: the region to be updated
1056 * @size: used size of the region.
1057 */
1058void memory_region_set_size(MemoryRegion *mr, uint64_t size);
1059
1060/*
Avi Kivity47033592011-12-04 19:16:50 +02001061 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1062 *
1063 * Dynamically updates the offset into the target region that an alias points
1064 * to, as if the fourth argument to memory_region_init_alias() has changed.
1065 *
1066 * @mr: the #MemoryRegion to be updated; should be an alias.
1067 * @offset: the new offset into the target memory region
1068 */
1069void memory_region_set_alias_offset(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +02001070 hwaddr offset);
Avi Kivity47033592011-12-04 19:16:50 +02001071
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -03001072/**
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +02001073 * memory_region_present: checks if an address relative to a @container
1074 * translates into #MemoryRegion within @container
Paolo Bonzini3ce10902013-07-02 13:40:48 +02001075 *
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +02001076 * Answer whether a #MemoryRegion within @container covers the address
Paolo Bonzini3ce10902013-07-02 13:40:48 +02001077 * @addr.
1078 *
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +02001079 * @container: a #MemoryRegion within which @addr is a relative address
1080 * @addr: the area within @container to be searched
Paolo Bonzini3ce10902013-07-02 13:40:48 +02001081 */
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +02001082bool memory_region_present(MemoryRegion *container, hwaddr addr);
Paolo Bonzini3ce10902013-07-02 13:40:48 +02001083
1084/**
Igor Mammedoveed2bac2014-06-02 15:25:06 +02001085 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1086 * into any address space.
1087 *
1088 * @mr: a #MemoryRegion which should be checked if it's mapped
1089 */
1090bool memory_region_is_mapped(MemoryRegion *mr);
1091
1092/**
Paolo Bonzini73034e92013-05-07 15:48:28 +02001093 * memory_region_find: translate an address/size relative to a
1094 * MemoryRegion into a #MemoryRegionSection.
Avi Kivitye2177952011-12-08 15:00:18 +02001095 *
Paolo Bonzini73034e92013-05-07 15:48:28 +02001096 * Locates the first #MemoryRegion within @mr that overlaps the range
1097 * given by @addr and @size.
Avi Kivitye2177952011-12-08 15:00:18 +02001098 *
1099 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1100 * It will have the following characteristics:
Avi Kivitye2177952011-12-08 15:00:18 +02001101 * .@size = 0 iff no overlap was found
1102 * .@mr is non-%NULL iff an overlap was found
1103 *
Paolo Bonzini73034e92013-05-07 15:48:28 +02001104 * Remember that in the return value the @offset_within_region is
1105 * relative to the returned region (in the .@mr field), not to the
1106 * @mr argument.
1107 *
1108 * Similarly, the .@offset_within_address_space is relative to the
1109 * address space that contains both regions, the passed and the
1110 * returned one. However, in the special case where the @mr argument
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +02001111 * has no container (and thus is the root of the address space), the
Paolo Bonzini73034e92013-05-07 15:48:28 +02001112 * following will hold:
1113 * .@offset_within_address_space >= @addr
1114 * .@offset_within_address_space + .@size <= @addr + @size
1115 *
1116 * @mr: a MemoryRegion within which @addr is a relative address
1117 * @addr: start of the area within @as to be searched
Avi Kivitye2177952011-12-08 15:00:18 +02001118 * @size: size of the area to be searched
1119 */
Paolo Bonzini73034e92013-05-07 15:48:28 +02001120MemoryRegionSection memory_region_find(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +02001121 hwaddr addr, uint64_t size);
Avi Kivitye2177952011-12-08 15:00:18 +02001122
Blue Swirlfd062572012-04-09 17:38:52 +00001123/**
Paolo Bonzini1d671362013-04-24 10:46:55 +02001124 * address_space_sync_dirty_bitmap: synchronize the dirty log for all memory
Avi Kivity86e775c2011-12-15 16:24:49 +02001125 *
1126 * Synchronizes the dirty page log for an entire address space.
Paolo Bonzini1d671362013-04-24 10:46:55 +02001127 * @as: the address space that contains the memory being synchronized
Avi Kivity86e775c2011-12-15 16:24:49 +02001128 */
Paolo Bonzini1d671362013-04-24 10:46:55 +02001129void address_space_sync_dirty_bitmap(AddressSpace *as);
Avi Kivity86e775c2011-12-15 16:24:49 +02001130
Avi Kivitye2177952011-12-08 15:00:18 +02001131/**
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -03001132 * memory_region_transaction_begin: Start a transaction.
1133 *
1134 * During a transaction, changes will be accumulated and made visible
Stefan Weildabdf392012-01-08 19:35:09 +01001135 * only when the transaction ends (is committed).
Avi Kivity4ef4db82011-07-26 14:26:13 +03001136 */
1137void memory_region_transaction_begin(void);
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -03001138
1139/**
1140 * memory_region_transaction_commit: Commit a transaction and make changes
1141 * visible to the guest.
Avi Kivity4ef4db82011-07-26 14:26:13 +03001142 */
1143void memory_region_transaction_commit(void);
1144
Avi Kivity7664e802011-12-11 14:47:25 +02001145/**
1146 * memory_listener_register: register callbacks to be called when memory
1147 * sections are mapped or unmapped into an address
1148 * space
1149 *
1150 * @listener: an object containing the callbacks to be called
Avi Kivity7376e582012-02-08 21:05:17 +02001151 * @filter: if non-%NULL, only regions in this address space will be observed
Avi Kivity7664e802011-12-11 14:47:25 +02001152 */
Avi Kivityf6790af2012-10-02 20:13:51 +02001153void memory_listener_register(MemoryListener *listener, AddressSpace *filter);
Avi Kivity7664e802011-12-11 14:47:25 +02001154
1155/**
1156 * memory_listener_unregister: undo the effect of memory_listener_register()
1157 *
1158 * @listener: an object containing the callbacks to be removed
1159 */
1160void memory_listener_unregister(MemoryListener *listener);
1161
1162/**
1163 * memory_global_dirty_log_start: begin dirty logging for all regions
1164 */
1165void memory_global_dirty_log_start(void);
1166
1167/**
BALATON Zoltan1a7e8ca2012-08-22 17:18:38 +02001168 * memory_global_dirty_log_stop: end dirty logging for all regions
Avi Kivity7664e802011-12-11 14:47:25 +02001169 */
1170void memory_global_dirty_log_stop(void);
1171
Blue Swirl314e2982011-09-11 20:22:05 +00001172void mtree_info(fprintf_function mon_printf, void *f);
1173
Avi Kivity9ad2bbc2012-10-02 14:59:23 +02001174/**
Peter Maydell3b643492015-04-26 16:49:23 +01001175 * memory_region_dispatch_read: perform a read directly to the specified
1176 * MemoryRegion.
1177 *
1178 * @mr: #MemoryRegion to access
1179 * @addr: address within that region
1180 * @pval: pointer to uint64_t which the data is written to
1181 * @size: size of the access in bytes
1182 * @attrs: memory transaction attributes to use for the access
1183 */
1184MemTxResult memory_region_dispatch_read(MemoryRegion *mr,
1185 hwaddr addr,
1186 uint64_t *pval,
1187 unsigned size,
1188 MemTxAttrs attrs);
1189/**
1190 * memory_region_dispatch_write: perform a write directly to the specified
1191 * MemoryRegion.
1192 *
1193 * @mr: #MemoryRegion to access
1194 * @addr: address within that region
1195 * @data: data to write
1196 * @size: size of the access in bytes
1197 * @attrs: memory transaction attributes to use for the access
1198 */
1199MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
1200 hwaddr addr,
1201 uint64_t data,
1202 unsigned size,
1203 MemTxAttrs attrs);
1204
1205/**
Avi Kivity9ad2bbc2012-10-02 14:59:23 +02001206 * address_space_init: initializes an address space
1207 *
1208 * @as: an uninitialized #AddressSpace
Veres Lajos67cc32e2015-09-08 22:45:14 +01001209 * @root: a #MemoryRegion that routes addresses for the address space
Alexey Kardashevskiy7dca8042013-04-29 16:25:51 +00001210 * @name: an address space name. The name is only used for debugging
1211 * output.
Avi Kivity9ad2bbc2012-10-02 14:59:23 +02001212 */
Alexey Kardashevskiy7dca8042013-04-29 16:25:51 +00001213void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
Avi Kivity9ad2bbc2012-10-02 14:59:23 +02001214
Peter Crosthwaitef0c02d12016-01-21 14:15:06 +00001215/**
1216 * address_space_init_shareable: return an address space for a memory region,
1217 * creating it if it does not already exist
1218 *
1219 * @root: a #MemoryRegion that routes addresses for the address space
1220 * @name: an address space name. The name is only used for debugging
1221 * output.
1222 *
1223 * This function will return a pointer to an existing AddressSpace
1224 * which was initialized with the specified MemoryRegion, or it will
1225 * create and initialize one if it does not already exist. The ASes
1226 * are reference-counted, so the memory will be freed automatically
1227 * when the AddressSpace is destroyed via address_space_destroy.
1228 */
1229AddressSpace *address_space_init_shareable(MemoryRegion *root,
1230 const char *name);
Avi Kivity83f3c252012-10-07 12:59:55 +02001231
1232/**
1233 * address_space_destroy: destroy an address space
1234 *
1235 * Releases all resources associated with an address space. After an address space
1236 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1237 * as well.
1238 *
1239 * @as: address space to be destroyed
1240 */
1241void address_space_destroy(AddressSpace *as);
1242
Avi Kivityac1970f2012-10-03 16:22:53 +02001243/**
1244 * address_space_rw: read from or write to an address space.
1245 *
Peter Maydell5c9eb022015-04-26 16:49:24 +01001246 * Return a MemTxResult indicating whether the operation succeeded
1247 * or failed (eg unassigned memory, device rejected the transaction,
1248 * IOMMU fault).
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02001249 *
Avi Kivityac1970f2012-10-03 16:22:53 +02001250 * @as: #AddressSpace to be accessed
1251 * @addr: address within that address space
Peter Maydell5c9eb022015-04-26 16:49:24 +01001252 * @attrs: memory transaction attributes
Avi Kivityac1970f2012-10-03 16:22:53 +02001253 * @buf: buffer with the data transferred
1254 * @is_write: indicates the transfer direction
1255 */
Peter Maydell5c9eb022015-04-26 16:49:24 +01001256MemTxResult address_space_rw(AddressSpace *as, hwaddr addr,
1257 MemTxAttrs attrs, uint8_t *buf,
1258 int len, bool is_write);
Avi Kivityac1970f2012-10-03 16:22:53 +02001259
1260/**
1261 * address_space_write: write to address space.
1262 *
Peter Maydell5c9eb022015-04-26 16:49:24 +01001263 * Return a MemTxResult indicating whether the operation succeeded
1264 * or failed (eg unassigned memory, device rejected the transaction,
1265 * IOMMU fault).
Avi Kivityac1970f2012-10-03 16:22:53 +02001266 *
1267 * @as: #AddressSpace to be accessed
1268 * @addr: address within that address space
Peter Maydell5c9eb022015-04-26 16:49:24 +01001269 * @attrs: memory transaction attributes
Avi Kivityac1970f2012-10-03 16:22:53 +02001270 * @buf: buffer with the data transferred
1271 */
Peter Maydell5c9eb022015-04-26 16:49:24 +01001272MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
1273 MemTxAttrs attrs,
1274 const uint8_t *buf, int len);
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02001275
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01001276/* address_space_ld*: load from an address space
Peter Maydell50013112015-04-26 16:49:24 +01001277 * address_space_st*: store to an address space
1278 *
1279 * These functions perform a load or store of the byte, word,
1280 * longword or quad to the specified address within the AddressSpace.
1281 * The _le suffixed functions treat the data as little endian;
1282 * _be indicates big endian; no suffix indicates "same endianness
1283 * as guest CPU".
1284 *
1285 * The "guest CPU endianness" accessors are deprecated for use outside
1286 * target-* code; devices should be CPU-agnostic and use either the LE
1287 * or the BE accessors.
1288 *
1289 * @as #AddressSpace to be accessed
1290 * @addr: address within that address space
1291 * @val: data value, for stores
1292 * @attrs: memory transaction attributes
1293 * @result: location to write the success/failure of the transaction;
1294 * if NULL, this information is discarded
1295 */
1296uint32_t address_space_ldub(AddressSpace *as, hwaddr addr,
1297 MemTxAttrs attrs, MemTxResult *result);
1298uint32_t address_space_lduw_le(AddressSpace *as, hwaddr addr,
1299 MemTxAttrs attrs, MemTxResult *result);
1300uint32_t address_space_lduw_be(AddressSpace *as, hwaddr addr,
1301 MemTxAttrs attrs, MemTxResult *result);
1302uint32_t address_space_ldl_le(AddressSpace *as, hwaddr addr,
1303 MemTxAttrs attrs, MemTxResult *result);
1304uint32_t address_space_ldl_be(AddressSpace *as, hwaddr addr,
1305 MemTxAttrs attrs, MemTxResult *result);
1306uint64_t address_space_ldq_le(AddressSpace *as, hwaddr addr,
1307 MemTxAttrs attrs, MemTxResult *result);
1308uint64_t address_space_ldq_be(AddressSpace *as, hwaddr addr,
1309 MemTxAttrs attrs, MemTxResult *result);
1310void address_space_stb(AddressSpace *as, hwaddr addr, uint32_t val,
1311 MemTxAttrs attrs, MemTxResult *result);
1312void address_space_stw_le(AddressSpace *as, hwaddr addr, uint32_t val,
1313 MemTxAttrs attrs, MemTxResult *result);
1314void address_space_stw_be(AddressSpace *as, hwaddr addr, uint32_t val,
1315 MemTxAttrs attrs, MemTxResult *result);
1316void address_space_stl_le(AddressSpace *as, hwaddr addr, uint32_t val,
1317 MemTxAttrs attrs, MemTxResult *result);
1318void address_space_stl_be(AddressSpace *as, hwaddr addr, uint32_t val,
1319 MemTxAttrs attrs, MemTxResult *result);
1320void address_space_stq_le(AddressSpace *as, hwaddr addr, uint64_t val,
1321 MemTxAttrs attrs, MemTxResult *result);
1322void address_space_stq_be(AddressSpace *as, hwaddr addr, uint64_t val,
1323 MemTxAttrs attrs, MemTxResult *result);
1324
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001325/* address_space_translate: translate an address range into an address space
Paolo Bonzini41063e12015-03-18 14:21:43 +01001326 * into a MemoryRegion and an address range into that section. Should be
1327 * called from an RCU critical section, to avoid that the last reference
1328 * to the returned region disappears after address_space_translate returns.
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001329 *
1330 * @as: #AddressSpace to be accessed
1331 * @addr: address within that address space
1332 * @xlat: pointer to address within the returned memory region section's
1333 * #MemoryRegion.
1334 * @len: pointer to length
1335 * @is_write: indicates the transfer direction
1336 */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02001337MemoryRegion *address_space_translate(AddressSpace *as, hwaddr addr,
1338 hwaddr *xlat, hwaddr *len,
1339 bool is_write);
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001340
Paolo Bonzini51644ab2013-04-11 15:40:59 +02001341/* address_space_access_valid: check for validity of accessing an address
1342 * space range
1343 *
Avi Kivity30951152012-10-30 13:47:46 +02001344 * Check whether memory is assigned to the given address space range, and
1345 * access is permitted by any IOMMU regions that are active for the address
1346 * space.
Paolo Bonzini51644ab2013-04-11 15:40:59 +02001347 *
1348 * For now, addr and len should be aligned to a page size. This limitation
1349 * will be lifted in the future.
1350 *
1351 * @as: #AddressSpace to be accessed
1352 * @addr: address within that address space
1353 * @len: length of the area to be checked
1354 * @is_write: indicates the transfer direction
1355 */
1356bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write);
1357
Avi Kivityac1970f2012-10-03 16:22:53 +02001358/* address_space_map: map a physical memory region into a host virtual address
1359 *
1360 * May map a subset of the requested range, given by and returned in @plen.
1361 * May return %NULL if resources needed to perform the mapping are exhausted.
1362 * Use only for reads OR writes - not for read-modify-write operations.
1363 * Use cpu_register_map_client() to know when retrying the map operation is
1364 * likely to succeed.
1365 *
1366 * @as: #AddressSpace to be accessed
1367 * @addr: address within that address space
1368 * @plen: pointer to length of buffer; updated on return
1369 * @is_write: indicates the transfer direction
1370 */
Avi Kivitya8170e52012-10-23 12:30:10 +02001371void *address_space_map(AddressSpace *as, hwaddr addr,
1372 hwaddr *plen, bool is_write);
Avi Kivityac1970f2012-10-03 16:22:53 +02001373
1374/* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1375 *
1376 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1377 * the amount of memory that was actually read or written by the caller.
1378 *
1379 * @as: #AddressSpace used
1380 * @addr: address within that address space
1381 * @len: buffer length as returned by address_space_map()
1382 * @access_len: amount of data actually transferred
1383 * @is_write: indicates the transfer direction
1384 */
Avi Kivitya8170e52012-10-23 12:30:10 +02001385void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
1386 int is_write, hwaddr access_len);
Avi Kivityac1970f2012-10-03 16:22:53 +02001387
1388
Paolo Bonzinia203ac72015-12-09 10:18:57 +01001389/* Internal functions, part of the implementation of address_space_read. */
1390MemTxResult address_space_read_continue(AddressSpace *as, hwaddr addr,
1391 MemTxAttrs attrs, uint8_t *buf,
1392 int len, hwaddr addr1, hwaddr l,
1393 MemoryRegion *mr);
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01001394MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr,
1395 MemTxAttrs attrs, uint8_t *buf, int len);
Gonglei3655cb92016-02-20 10:35:20 +08001396void *qemu_get_ram_ptr(RAMBlock *ram_block, ram_addr_t addr);
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01001397
1398static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
1399{
1400 if (is_write) {
1401 return memory_region_is_ram(mr) && !mr->readonly;
1402 } else {
1403 return memory_region_is_ram(mr) || memory_region_is_romd(mr);
1404 }
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01001405}
1406
1407/**
1408 * address_space_read: read from an address space.
1409 *
1410 * Return a MemTxResult indicating whether the operation succeeded
1411 * or failed (eg unassigned memory, device rejected the transaction,
1412 * IOMMU fault).
1413 *
1414 * @as: #AddressSpace to be accessed
1415 * @addr: address within that address space
1416 * @attrs: memory transaction attributes
1417 * @buf: buffer with the data transferred
1418 */
1419static inline __attribute__((__always_inline__))
1420MemTxResult address_space_read(AddressSpace *as, hwaddr addr, MemTxAttrs attrs,
1421 uint8_t *buf, int len)
1422{
1423 MemTxResult result = MEMTX_OK;
1424 hwaddr l, addr1;
1425 void *ptr;
1426 MemoryRegion *mr;
1427
1428 if (__builtin_constant_p(len)) {
1429 if (len) {
1430 rcu_read_lock();
1431 l = len;
1432 mr = address_space_translate(as, addr, &addr1, &l, false);
1433 if (len == l && memory_access_is_direct(mr, false)) {
1434 addr1 += memory_region_get_ram_addr(mr);
Gonglei3655cb92016-02-20 10:35:20 +08001435 ptr = qemu_get_ram_ptr(mr->ram_block, addr1);
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01001436 memcpy(buf, ptr, len);
1437 } else {
1438 result = address_space_read_continue(as, addr, attrs, buf, len,
1439 addr1, l, mr);
1440 }
1441 rcu_read_unlock();
1442 }
1443 } else {
1444 result = address_space_read_full(as, addr, attrs, buf, len);
1445 }
1446 return result;
1447}
Paolo Bonzinia203ac72015-12-09 10:18:57 +01001448
Avi Kivity093bc2c2011-07-26 14:26:01 +03001449#endif
1450
1451#endif