blob: f35b6125ab18233cc8720ad5e45a11811ac8f7a1 [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 Xucdb30812016-09-23 13:02:26 +080070/*
71 * Bitmap for different IOMMUNotifier capabilities. Each notifier can
72 * register with one or multiple IOMMU Notifier capability bit(s).
73 */
74typedef enum {
75 IOMMU_NOTIFIER_NONE = 0,
76 /* Notify cache invalidations */
77 IOMMU_NOTIFIER_UNMAP = 0x1,
78 /* Notify entry changes (newly created entries) */
79 IOMMU_NOTIFIER_MAP = 0x2,
80} IOMMUNotifierFlag;
81
82#define IOMMU_NOTIFIER_ALL (IOMMU_NOTIFIER_MAP | IOMMU_NOTIFIER_UNMAP)
83
84struct IOMMUNotifier {
85 void (*notify)(struct IOMMUNotifier *notifier, IOMMUTLBEntry *data);
86 IOMMUNotifierFlag notifier_flags;
87 QLIST_ENTRY(IOMMUNotifier) node;
88};
89typedef struct IOMMUNotifier IOMMUNotifier;
90
Peter Maydellcc05c432015-04-26 16:49:23 +010091/* New-style MMIO accessors can indicate that the transaction failed.
92 * A zero (MEMTX_OK) response means success; anything else is a failure
93 * of some kind. The memory subsystem will bitwise-OR together results
94 * if it is synthesizing an operation from multiple smaller accesses.
95 */
96#define MEMTX_OK 0
97#define MEMTX_ERROR (1U << 0) /* device returned an error */
98#define MEMTX_DECODE_ERROR (1U << 1) /* nothing at that address */
99typedef uint32_t MemTxResult;
100
Avi Kivity093bc2c2011-07-26 14:26:01 +0300101/*
102 * Memory region callbacks
103 */
104struct MemoryRegionOps {
105 /* Read from the memory region. @addr is relative to @mr; @size is
106 * in bytes. */
107 uint64_t (*read)(void *opaque,
Avi Kivitya8170e52012-10-23 12:30:10 +0200108 hwaddr addr,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300109 unsigned size);
110 /* Write to the memory region. @addr is relative to @mr; @size is
111 * in bytes. */
112 void (*write)(void *opaque,
Avi Kivitya8170e52012-10-23 12:30:10 +0200113 hwaddr addr,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300114 uint64_t data,
115 unsigned size);
116
Peter Maydellcc05c432015-04-26 16:49:23 +0100117 MemTxResult (*read_with_attrs)(void *opaque,
118 hwaddr addr,
119 uint64_t *data,
120 unsigned size,
121 MemTxAttrs attrs);
122 MemTxResult (*write_with_attrs)(void *opaque,
123 hwaddr addr,
124 uint64_t data,
125 unsigned size,
126 MemTxAttrs attrs);
127
Avi Kivity093bc2c2011-07-26 14:26:01 +0300128 enum device_endian endianness;
129 /* Guest-visible constraints: */
130 struct {
131 /* If nonzero, specify bounds on access sizes beyond which a machine
132 * check is thrown.
133 */
134 unsigned min_access_size;
135 unsigned max_access_size;
136 /* If true, unaligned accesses are supported. Otherwise unaligned
137 * accesses throw machine checks.
138 */
139 bool unaligned;
Avi Kivity897fa7c2011-11-13 13:05:27 +0200140 /*
141 * If present, and returns #false, the transaction is not accepted
142 * by the device (and results in machine dependent behaviour such
143 * as a machine check exception).
144 */
Avi Kivitya8170e52012-10-23 12:30:10 +0200145 bool (*accepts)(void *opaque, hwaddr addr,
Avi Kivity897fa7c2011-11-13 13:05:27 +0200146 unsigned size, bool is_write);
Avi Kivity093bc2c2011-07-26 14:26:01 +0300147 } valid;
148 /* Internal implementation constraints: */
149 struct {
150 /* If nonzero, specifies the minimum size implemented. Smaller sizes
151 * will be rounded upwards and a partial result will be returned.
152 */
153 unsigned min_access_size;
154 /* If nonzero, specifies the maximum size implemented. Larger sizes
155 * will be done as a series of accesses with smaller sizes.
156 */
157 unsigned max_access_size;
158 /* If true, unaligned accesses are supported. Otherwise all accesses
159 * are converted to (possibly multiple) naturally aligned accesses.
160 */
Fam Zhengedc1ba72014-05-05 15:53:41 +0800161 bool unaligned;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300162 } impl;
Avi Kivity627a0e92011-07-26 14:26:09 +0300163
Avi Kivity74901c32011-07-26 14:26:10 +0300164 /* If .read and .write are not present, old_mmio may be used for
165 * backwards compatibility with old mmio registration
166 */
167 const MemoryRegionMmio old_mmio;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300168};
169
Avi Kivity30951152012-10-30 13:47:46 +0200170typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps;
171
172struct MemoryRegionIOMMUOps {
173 /* Return a TLB entry that contains a given address. */
Le Tan8d7b8cb2014-08-16 13:55:37 +0800174 IOMMUTLBEntry (*translate)(MemoryRegion *iommu, hwaddr addr, bool is_write);
Alexey Kardashevskiyf682e9c2016-06-21 11:14:01 +1000175 /* Returns minimum supported page size */
176 uint64_t (*get_min_page_size)(MemoryRegion *iommu);
Peter Xu5bf3d312016-09-23 13:02:27 +0800177 /* Called when IOMMU Notifier flag changed */
178 void (*notify_flag_changed)(MemoryRegion *iommu,
179 IOMMUNotifierFlag old_flags,
180 IOMMUNotifierFlag new_flags);
Avi Kivity30951152012-10-30 13:47:46 +0200181};
182
Avi Kivity093bc2c2011-07-26 14:26:01 +0300183typedef struct CoalescedMemoryRange CoalescedMemoryRange;
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300184typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300185
186struct MemoryRegion {
Peter Crosthwaiteb4fefef2014-06-05 23:15:52 -0700187 Object parent_obj;
Paolo Bonzinia6768542015-12-09 11:40:14 +0100188
Avi Kivity093bc2c2011-07-26 14:26:01 +0300189 /* All fields are private - violators will be prosecuted */
Paolo Bonzinia6768542015-12-09 11:40:14 +0100190
191 /* The following fields should fit in a cache line */
192 bool romd_mode;
193 bool ram;
194 bool subpage;
195 bool readonly; /* For RAM regions */
196 bool rom_device;
197 bool flush_coalesced_mmio;
198 bool global_locking;
199 uint8_t dirty_log_mask;
Gonglei58eaa212016-02-22 16:34:55 +0800200 RAMBlock *ram_block;
Paolo Bonzini612263c2015-12-09 11:44:25 +0100201 Object *owner;
Avi Kivity30951152012-10-30 13:47:46 +0200202 const MemoryRegionIOMMUOps *iommu_ops;
Paolo Bonzinia6768542015-12-09 11:40:14 +0100203
204 const MemoryRegionOps *ops;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300205 void *opaque;
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +0200206 MemoryRegion *container;
Avi Kivity08dafab2011-10-16 13:19:17 +0200207 Int128 size;
Avi Kivitya8170e52012-10-23 12:30:10 +0200208 hwaddr addr;
Avi Kivity545e92e2011-08-08 19:58:48 +0300209 void (*destructor)(MemoryRegion *mr);
Igor Mammedova2b257d2014-10-31 16:38:37 +0000210 uint64_t align;
Avi Kivity14a3c102011-07-26 14:26:06 +0300211 bool terminates;
Alex Williamson21e00fa2016-10-31 09:53:03 -0600212 bool ram_device;
Avi Kivity6bba19b2011-09-14 11:54:58 +0300213 bool enabled;
Jan Kiszka1660e722011-10-23 16:01:19 +0200214 bool warning_printed; /* For reservations */
Paolo Bonzinideb809e2015-07-14 13:56:53 +0200215 uint8_t vga_logging_count;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300216 MemoryRegion *alias;
Avi Kivitya8170e52012-10-23 12:30:10 +0200217 hwaddr alias_offset;
Peter Crosthwaited33382d2014-06-05 23:17:01 -0700218 int32_t priority;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300219 QTAILQ_HEAD(subregions, MemoryRegion) subregions;
220 QTAILQ_ENTRY(MemoryRegion) subregions_link;
221 QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced;
Peter Maydell302fa282014-08-19 20:05:46 +0100222 const char *name;
Avi Kivity3e9d69e2011-07-26 14:26:11 +0300223 unsigned ioeventfd_nb;
224 MemoryRegionIoeventfd *ioeventfds;
Peter Xucdb30812016-09-23 13:02:26 +0800225 QLIST_HEAD(, IOMMUNotifier) iommu_notify;
Peter Xu5bf3d312016-09-23 13:02:27 +0800226 IOMMUNotifierFlag iommu_notify_flags;
Avi Kivity093bc2c2011-07-26 14:26:01 +0300227};
228
Paolo Bonzinic2fc83e2013-06-02 15:20:47 +0200229/**
230 * MemoryListener: callbacks structure for updates to the physical memory map
231 *
232 * Allows a component to adjust to changes in the guest-visible memory map.
233 * Use with memory_listener_register() and memory_listener_unregister().
234 */
235struct MemoryListener {
236 void (*begin)(MemoryListener *listener);
237 void (*commit)(MemoryListener *listener);
238 void (*region_add)(MemoryListener *listener, MemoryRegionSection *section);
239 void (*region_del)(MemoryListener *listener, MemoryRegionSection *section);
240 void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section);
Paolo Bonzinib2dfd712015-04-25 14:38:30 +0200241 void (*log_start)(MemoryListener *listener, MemoryRegionSection *section,
242 int old, int new);
243 void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section,
244 int old, int new);
Paolo Bonzinic2fc83e2013-06-02 15:20:47 +0200245 void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section);
246 void (*log_global_start)(MemoryListener *listener);
247 void (*log_global_stop)(MemoryListener *listener);
248 void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section,
249 bool match_data, uint64_t data, EventNotifier *e);
250 void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section,
251 bool match_data, uint64_t data, EventNotifier *e);
252 void (*coalesced_mmio_add)(MemoryListener *listener, MemoryRegionSection *section,
253 hwaddr addr, hwaddr len);
254 void (*coalesced_mmio_del)(MemoryListener *listener, MemoryRegionSection *section,
255 hwaddr addr, hwaddr len);
256 /* Lower = earlier (during add), later (during del) */
257 unsigned priority;
Paolo Bonzinid45fa782016-09-22 16:11:54 +0200258 AddressSpace *address_space;
Paolo Bonzinic2fc83e2013-06-02 15:20:47 +0200259 QTAILQ_ENTRY(MemoryListener) link;
Paolo Bonzini9a546352016-09-22 16:23:06 +0200260 QTAILQ_ENTRY(MemoryListener) link_as;
Paolo Bonzinic2fc83e2013-06-02 15:20:47 +0200261};
262
Avi Kivity9ad2bbc2012-10-02 14:59:23 +0200263/**
264 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
265 */
266struct AddressSpace {
267 /* All fields are private. */
Paolo Bonzini374f2982013-05-17 12:37:03 +0200268 struct rcu_head rcu;
Alexey Kardashevskiy7dca8042013-04-29 16:25:51 +0000269 char *name;
Avi Kivity9ad2bbc2012-10-02 14:59:23 +0200270 MemoryRegion *root;
Peter Crosthwaitef0c02d12016-01-21 14:15:06 +0000271 int ref_count;
272 bool malloced;
Paolo Bonzini374f2982013-05-17 12:37:03 +0200273
274 /* Accessed via RCU. */
Avi Kivity9ad2bbc2012-10-02 14:59:23 +0200275 struct FlatView *current_map;
Paolo Bonzini374f2982013-05-17 12:37:03 +0200276
Avi Kivity9ad2bbc2012-10-02 14:59:23 +0200277 int ioeventfd_nb;
278 struct MemoryRegionIoeventfd *ioeventfds;
Avi Kivityac1970f2012-10-03 16:22:53 +0200279 struct AddressSpaceDispatch *dispatch;
Paolo Bonzini00752702013-05-29 12:13:54 +0200280 struct AddressSpaceDispatch *next_dispatch;
Paolo Bonzini89ae3372013-06-02 10:39:07 +0200281 MemoryListener dispatch_listener;
Paolo Bonzini9a546352016-09-22 16:23:06 +0200282 QTAILQ_HEAD(memory_listeners_as, MemoryListener) listeners;
Avi Kivity0d673e32012-10-02 15:28:50 +0200283 QTAILQ_ENTRY(AddressSpace) address_spaces_link;
Avi Kivity9ad2bbc2012-10-02 14:59:23 +0200284};
285
Avi Kivitye2177952011-12-08 15:00:18 +0200286/**
287 * MemoryRegionSection: describes a fragment of a #MemoryRegion
288 *
289 * @mr: the region, or %NULL if empty
Avi Kivity7664e802011-12-11 14:47:25 +0200290 * @address_space: the address space the region is mapped in
Avi Kivitye2177952011-12-08 15:00:18 +0200291 * @offset_within_region: the beginning of the section, relative to @mr's start
292 * @size: the size of the section; will not exceed @mr's boundaries
293 * @offset_within_address_space: the address of the first byte of the section
294 * relative to the region's address space
Avi Kivity7a8499e2012-02-08 17:01:23 +0200295 * @readonly: writes to this section are ignored
Avi Kivitye2177952011-12-08 15:00:18 +0200296 */
297struct MemoryRegionSection {
298 MemoryRegion *mr;
Avi Kivityf6790af2012-10-02 20:13:51 +0200299 AddressSpace *address_space;
Avi Kivitya8170e52012-10-23 12:30:10 +0200300 hwaddr offset_within_region;
Paolo Bonzini052e87b2013-05-27 10:08:27 +0200301 Int128 size;
Avi Kivitya8170e52012-10-23 12:30:10 +0200302 hwaddr offset_within_address_space;
Avi Kivity7a8499e2012-02-08 17:01:23 +0200303 bool readonly;
Avi Kivitye2177952011-12-08 15:00:18 +0200304};
305
Avi Kivity093bc2c2011-07-26 14:26:01 +0300306/**
307 * memory_region_init: Initialize a memory region
308 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300309 * The region typically acts as a container for other memory regions. Use
Avi Kivity093bc2c2011-07-26 14:26:01 +0300310 * memory_region_add_subregion() to add subregions.
311 *
312 * @mr: the #MemoryRegion to be initialized
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400313 * @owner: the object that tracks the region's reference count
Avi Kivity093bc2c2011-07-26 14:26:01 +0300314 * @name: used for debugging; not visible to the user or ABI
315 * @size: size of the region; any subregions beyond this size will be clipped
316 */
317void memory_region_init(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400318 struct Object *owner,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300319 const char *name,
320 uint64_t size);
Paolo Bonzini46637be2013-05-07 09:06:00 +0200321
322/**
323 * memory_region_ref: Add 1 to a memory region's reference count
324 *
325 * Whenever memory regions are accessed outside the BQL, they need to be
326 * preserved against hot-unplug. MemoryRegions actually do not have their
327 * own reference count; they piggyback on a QOM object, their "owner".
328 * This function adds a reference to the owner.
329 *
330 * All MemoryRegions must have an owner if they can disappear, even if the
331 * device they belong to operates exclusively under the BQL. This is because
332 * the region could be returned at any time by memory_region_find, and this
333 * is usually under guest control.
334 *
335 * @mr: the #MemoryRegion
336 */
337void memory_region_ref(MemoryRegion *mr);
338
339/**
340 * memory_region_unref: Remove 1 to a memory region's reference count
341 *
342 * Whenever memory regions are accessed outside the BQL, they need to be
343 * preserved against hot-unplug. MemoryRegions actually do not have their
344 * own reference count; they piggyback on a QOM object, their "owner".
345 * This function removes a reference to the owner and possibly destroys it.
346 *
347 * @mr: the #MemoryRegion
348 */
349void memory_region_unref(MemoryRegion *mr);
350
Avi Kivity093bc2c2011-07-26 14:26:01 +0300351/**
352 * memory_region_init_io: Initialize an I/O memory region.
353 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300354 * Accesses into the region will cause the callbacks in @ops to be called.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300355 * if @size is nonzero, subregions will be clipped to @size.
356 *
357 * @mr: the #MemoryRegion to be initialized.
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400358 * @owner: the object that tracks the region's reference count
Avi Kivity093bc2c2011-07-26 14:26:01 +0300359 * @ops: a structure containing read and write callbacks to be used when
360 * I/O is performed on the region.
Daniel P. Berrangeb6af0972015-08-26 12:17:13 +0100361 * @opaque: passed to the read and write callbacks of the @ops structure.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300362 * @name: used for debugging; not visible to the user or ABI
363 * @size: size of the region.
364 */
365void memory_region_init_io(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400366 struct Object *owner,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300367 const MemoryRegionOps *ops,
368 void *opaque,
369 const char *name,
370 uint64_t size);
371
372/**
373 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -0300374 * region will modify memory directly.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300375 *
376 * @mr: the #MemoryRegion to be initialized.
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400377 * @owner: the object that tracks the region's reference count
Avi Kivityc5705a72011-12-20 15:59:12 +0200378 * @name: the name of the region.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300379 * @size: size of the region.
Hu Tao49946532014-09-09 13:27:55 +0800380 * @errp: pointer to Error*, to store an error if it happens.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300381 */
382void memory_region_init_ram(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400383 struct Object *owner,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300384 const char *name,
Hu Tao49946532014-09-09 13:27:55 +0800385 uint64_t size,
386 Error **errp);
Avi Kivity093bc2c2011-07-26 14:26:01 +0300387
Michael S. Tsirkin60786ef2014-11-17 00:24:36 +0200388/**
389 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
390 * RAM. Accesses into the region will
391 * modify memory directly. Only an initial
392 * portion of this RAM is actually used.
393 * The used size can change across reboots.
394 *
395 * @mr: the #MemoryRegion to be initialized.
396 * @owner: the object that tracks the region's reference count
397 * @name: the name of the region.
398 * @size: used size of the region.
399 * @max_size: max size of the region.
400 * @resized: callback to notify owner about used size change.
401 * @errp: pointer to Error*, to store an error if it happens.
402 */
403void memory_region_init_resizeable_ram(MemoryRegion *mr,
404 struct Object *owner,
405 const char *name,
406 uint64_t size,
407 uint64_t max_size,
408 void (*resized)(const char*,
409 uint64_t length,
410 void *host),
411 Error **errp);
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800412#ifdef __linux__
413/**
414 * memory_region_init_ram_from_file: Initialize RAM memory region with a
415 * mmap-ed backend.
416 *
417 * @mr: the #MemoryRegion to be initialized.
418 * @owner: the object that tracks the region's reference count
419 * @name: the name of the region.
420 * @size: size of the region.
Paolo Bonzinidbcb8982014-06-10 19:15:24 +0800421 * @share: %true if memory must be mmaped with the MAP_SHARED flag
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800422 * @path: the path in which to allocate the RAM.
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800423 * @errp: pointer to Error*, to store an error if it happens.
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800424 */
425void memory_region_init_ram_from_file(MemoryRegion *mr,
426 struct Object *owner,
427 const char *name,
428 uint64_t size,
Paolo Bonzinidbcb8982014-06-10 19:15:24 +0800429 bool share,
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800430 const char *path,
431 Error **errp);
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800432#endif
433
Avi Kivity093bc2c2011-07-26 14:26:01 +0300434/**
BALATON Zoltan1a7e8ca2012-08-22 17:18:38 +0200435 * memory_region_init_ram_ptr: Initialize RAM memory region from a
436 * user-provided pointer. Accesses into the
437 * region will modify memory directly.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300438 *
439 * @mr: the #MemoryRegion to be initialized.
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400440 * @owner: the object that tracks the region's reference count
Avi Kivityc5705a72011-12-20 15:59:12 +0200441 * @name: the name of the region.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300442 * @size: size of the region.
443 * @ptr: memory to be mapped; must contain at least @size bytes.
444 */
445void memory_region_init_ram_ptr(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400446 struct Object *owner,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300447 const char *name,
448 uint64_t size,
449 void *ptr);
450
451/**
Alex Williamson21e00fa2016-10-31 09:53:03 -0600452 * memory_region_init_ram_device_ptr: Initialize RAM device memory region from
453 * a user-provided pointer.
454 *
455 * A RAM device represents a mapping to a physical device, such as to a PCI
456 * MMIO BAR of an vfio-pci assigned device. The memory region may be mapped
457 * into the VM address space and access to the region will modify memory
458 * directly. However, the memory region should not be included in a memory
459 * dump (device may not be enabled/mapped at the time of the dump), and
460 * operations incompatible with manipulating MMIO should be avoided. Replaces
461 * skip_dump flag.
462 *
463 * @mr: the #MemoryRegion to be initialized.
464 * @owner: the object that tracks the region's reference count
465 * @name: the name of the region.
466 * @size: size of the region.
467 * @ptr: memory to be mapped; must contain at least @size bytes.
468 */
469void memory_region_init_ram_device_ptr(MemoryRegion *mr,
470 struct Object *owner,
471 const char *name,
472 uint64_t size,
473 void *ptr);
474
475/**
Avi Kivity093bc2c2011-07-26 14:26:01 +0300476 * memory_region_init_alias: Initialize a memory region that aliases all or a
477 * part of another memory region.
478 *
479 * @mr: the #MemoryRegion to be initialized.
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400480 * @owner: the object that tracks the region's reference count
Avi Kivity093bc2c2011-07-26 14:26:01 +0300481 * @name: used for debugging; not visible to the user or ABI
482 * @orig: the region to be referenced; @mr will be equivalent to
483 * @orig between @offset and @offset + @size - 1.
484 * @offset: start of the section in @orig to be referenced.
485 * @size: size of the region.
486 */
487void memory_region_init_alias(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400488 struct Object *owner,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300489 const char *name,
490 MemoryRegion *orig,
Avi Kivitya8170e52012-10-23 12:30:10 +0200491 hwaddr offset,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300492 uint64_t size);
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300493
494/**
Peter Maydella1777f72016-07-04 13:06:35 +0100495 * memory_region_init_rom: Initialize a ROM memory region.
496 *
497 * This has the same effect as calling memory_region_init_ram()
498 * and then marking the resulting region read-only with
499 * memory_region_set_readonly().
500 *
501 * @mr: the #MemoryRegion to be initialized.
502 * @owner: the object that tracks the region's reference count
503 * @name: the name of the region.
504 * @size: size of the region.
505 * @errp: pointer to Error*, to store an error if it happens.
506 */
507void memory_region_init_rom(MemoryRegion *mr,
508 struct Object *owner,
509 const char *name,
510 uint64_t size,
511 Error **errp);
512
513/**
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300514 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
515 * handled via callbacks.
516 *
517 * @mr: the #MemoryRegion to be initialized.
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400518 * @owner: the object that tracks the region's reference count
Peter Maydell39e0b032016-07-04 13:06:35 +0100519 * @ops: callbacks for write access handling (must not be NULL).
Avi Kivityc5705a72011-12-20 15:59:12 +0200520 * @name: the name of the region.
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300521 * @size: size of the region.
Hu Tao33e0eb52014-09-09 13:27:57 +0800522 * @errp: pointer to Error*, to store an error if it happens.
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300523 */
524void memory_region_init_rom_device(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400525 struct Object *owner,
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300526 const MemoryRegionOps *ops,
Avi Kivity75f59412011-08-26 00:35:15 +0300527 void *opaque,
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300528 const char *name,
Hu Tao33e0eb52014-09-09 13:27:57 +0800529 uint64_t size,
530 Error **errp);
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300531
Avi Kivity093bc2c2011-07-26 14:26:01 +0300532/**
Jan Kiszka1660e722011-10-23 16:01:19 +0200533 * memory_region_init_reservation: Initialize a memory region that reserves
534 * I/O space.
535 *
536 * A reservation region primariy serves debugging purposes. It claims I/O
537 * space that is not supposed to be handled by QEMU itself. Any access via
538 * the memory API will cause an abort().
Pavel Fedin6d6d2ab2015-08-13 11:26:21 +0100539 * This function is deprecated. Use memory_region_init_io() with NULL
540 * callbacks instead.
Jan Kiszka1660e722011-10-23 16:01:19 +0200541 *
542 * @mr: the #MemoryRegion to be initialized
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400543 * @owner: the object that tracks the region's reference count
Jan Kiszka1660e722011-10-23 16:01:19 +0200544 * @name: used for debugging; not visible to the user or ABI
545 * @size: size of the region.
546 */
Pavel Fedin6d6d2ab2015-08-13 11:26:21 +0100547static inline void memory_region_init_reservation(MemoryRegion *mr,
548 Object *owner,
Jan Kiszka1660e722011-10-23 16:01:19 +0200549 const char *name,
Pavel Fedin6d6d2ab2015-08-13 11:26:21 +0100550 uint64_t size)
551{
552 memory_region_init_io(mr, owner, NULL, mr, name, size);
553}
Avi Kivity30951152012-10-30 13:47:46 +0200554
555/**
556 * memory_region_init_iommu: Initialize a memory region that translates
557 * addresses
558 *
559 * An IOMMU region translates addresses and forwards accesses to a target
560 * memory region.
561 *
562 * @mr: the #MemoryRegion to be initialized
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400563 * @owner: the object that tracks the region's reference count
Avi Kivity30951152012-10-30 13:47:46 +0200564 * @ops: a function that translates addresses into the @target region
565 * @name: used for debugging; not visible to the user or ABI
566 * @size: size of the region.
567 */
568void memory_region_init_iommu(MemoryRegion *mr,
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400569 struct Object *owner,
Avi Kivity30951152012-10-30 13:47:46 +0200570 const MemoryRegionIOMMUOps *ops,
571 const char *name,
572 uint64_t size);
573
Jan Kiszka1660e722011-10-23 16:01:19 +0200574/**
Paolo Bonzini803c0812013-05-07 06:59:09 +0200575 * memory_region_owner: get a memory region's owner.
576 *
577 * @mr: the memory region being queried.
578 */
579struct Object *memory_region_owner(MemoryRegion *mr);
580
581/**
Avi Kivity093bc2c2011-07-26 14:26:01 +0300582 * memory_region_size: get a memory region's size.
583 *
584 * @mr: the memory region being queried.
585 */
586uint64_t memory_region_size(MemoryRegion *mr);
587
588/**
Avi Kivity8ea92522011-12-08 15:58:43 +0200589 * memory_region_is_ram: check whether a memory region is random access
590 *
591 * Returns %true is a memory region is random access.
592 *
593 * @mr: the memory region being queried
594 */
Paolo Bonzini1619d1f2015-12-09 17:47:39 +0100595static inline bool memory_region_is_ram(MemoryRegion *mr)
596{
597 return mr->ram;
598}
Avi Kivity8ea92522011-12-08 15:58:43 +0200599
600/**
Alex Williamson21e00fa2016-10-31 09:53:03 -0600601 * memory_region_is_ram_device: check whether a memory region is a ram device
Nikunj A Dadhaniae4dc3f52014-09-15 09:28:23 +0530602 *
Alex Williamson21e00fa2016-10-31 09:53:03 -0600603 * Returns %true is a memory region is a device backed ram region
Nikunj A Dadhaniae4dc3f52014-09-15 09:28:23 +0530604 *
605 * @mr: the memory region being queried
606 */
Alex Williamson21e00fa2016-10-31 09:53:03 -0600607bool memory_region_is_ram_device(MemoryRegion *mr);
Nikunj A Dadhaniae4dc3f52014-09-15 09:28:23 +0530608
609/**
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +0200610 * memory_region_is_romd: check whether a memory region is in ROMD mode
Blue Swirlfd062572012-04-09 17:38:52 +0000611 *
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +0200612 * Returns %true if a memory region is a ROM device and currently set to allow
Blue Swirlfd062572012-04-09 17:38:52 +0000613 * direct reads.
614 *
615 * @mr: the memory region being queried
616 */
617static inline bool memory_region_is_romd(MemoryRegion *mr)
618{
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +0200619 return mr->rom_device && mr->romd_mode;
Blue Swirlfd062572012-04-09 17:38:52 +0000620}
621
622/**
Avi Kivity30951152012-10-30 13:47:46 +0200623 * memory_region_is_iommu: check whether a memory region is an iommu
624 *
625 * Returns %true is a memory region is an iommu.
626 *
627 * @mr: the memory region being queried
628 */
Paolo Bonzini1619d1f2015-12-09 17:47:39 +0100629static inline bool memory_region_is_iommu(MemoryRegion *mr)
630{
631 return mr->iommu_ops;
632}
633
Avi Kivity30951152012-10-30 13:47:46 +0200634
635/**
Alexey Kardashevskiyf682e9c2016-06-21 11:14:01 +1000636 * memory_region_iommu_get_min_page_size: get minimum supported page size
637 * for an iommu
638 *
639 * Returns minimum supported page size for an iommu.
640 *
641 * @mr: the memory region being queried
642 */
643uint64_t memory_region_iommu_get_min_page_size(MemoryRegion *mr);
644
645/**
David Gibson06866572013-05-14 19:13:56 +1000646 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
647 *
Peter Xucdb30812016-09-23 13:02:26 +0800648 * The notification type will be decided by entry.perm bits:
649 *
650 * - For UNMAP (cache invalidation) notifies: set entry.perm to IOMMU_NONE.
651 * - For MAP (newly added entry) notifies: set entry.perm to the
652 * permission of the page (which is definitely !IOMMU_NONE).
653 *
654 * Note: for any IOMMU implementation, an in-place mapping change
655 * should be notified with an UNMAP followed by a MAP.
656 *
David Gibson06866572013-05-14 19:13:56 +1000657 * @mr: the memory region that was changed
658 * @entry: the new entry in the IOMMU translation table. The entry
659 * replaces all old entries for the same virtual I/O address range.
660 * Deleted entries have .@perm == 0.
661 */
662void memory_region_notify_iommu(MemoryRegion *mr,
663 IOMMUTLBEntry entry);
664
665/**
666 * memory_region_register_iommu_notifier: register a notifier for changes to
667 * IOMMU translation entries.
668 *
669 * @mr: the memory region to observe
Peter Xucdb30812016-09-23 13:02:26 +0800670 * @n: the IOMMUNotifier to be added; the notify callback receives a
671 * pointer to an #IOMMUTLBEntry as the opaque value; the pointer
672 * ceases to be valid on exit from the notifier.
David Gibson06866572013-05-14 19:13:56 +1000673 */
Peter Xucdb30812016-09-23 13:02:26 +0800674void memory_region_register_iommu_notifier(MemoryRegion *mr,
675 IOMMUNotifier *n);
David Gibson06866572013-05-14 19:13:56 +1000676
677/**
David Gibsona788f222015-09-30 12:13:55 +1000678 * memory_region_iommu_replay: replay existing IOMMU translations to
Alexey Kardashevskiyf682e9c2016-06-21 11:14:01 +1000679 * a notifier with the minimum page granularity returned by
680 * mr->iommu_ops->get_page_size().
David Gibsona788f222015-09-30 12:13:55 +1000681 *
682 * @mr: the memory region to observe
683 * @n: the notifier to which to replay iommu mappings
David Gibsona788f222015-09-30 12:13:55 +1000684 * @is_write: Whether to treat the replay as a translate "write"
685 * through the iommu
686 */
Peter Xucdb30812016-09-23 13:02:26 +0800687void memory_region_iommu_replay(MemoryRegion *mr, IOMMUNotifier *n,
688 bool is_write);
David Gibsona788f222015-09-30 12:13:55 +1000689
690/**
David Gibson06866572013-05-14 19:13:56 +1000691 * memory_region_unregister_iommu_notifier: unregister a notifier for
692 * changes to IOMMU translation entries.
693 *
Alexey Kardashevskiyd22d8952016-06-30 13:00:23 -0600694 * @mr: the memory region which was observed and for which notity_stopped()
695 * needs to be called
David Gibson06866572013-05-14 19:13:56 +1000696 * @n: the notifier to be removed.
697 */
Peter Xucdb30812016-09-23 13:02:26 +0800698void memory_region_unregister_iommu_notifier(MemoryRegion *mr,
699 IOMMUNotifier *n);
David Gibson06866572013-05-14 19:13:56 +1000700
701/**
Avi Kivity8991c792011-12-20 15:53:11 +0200702 * memory_region_name: get a memory region's name
703 *
704 * Returns the string that was used to initialize the memory region.
705 *
706 * @mr: the memory region being queried
707 */
Peter Crosthwaite5d546d42014-08-14 23:55:03 -0700708const char *memory_region_name(const MemoryRegion *mr);
Avi Kivity8991c792011-12-20 15:53:11 +0200709
710/**
Avi Kivity55043ba2011-12-15 17:20:34 +0200711 * memory_region_is_logging: return whether a memory region is logging writes
712 *
Paolo Bonzini2d1a35b2015-03-23 10:50:57 +0100713 * Returns %true if the memory region is logging writes for the given client
714 *
715 * @mr: the memory region being queried
716 * @client: the client being queried
717 */
718bool memory_region_is_logging(MemoryRegion *mr, uint8_t client);
719
720/**
721 * memory_region_get_dirty_log_mask: return the clients for which a
722 * memory region is logging writes.
723 *
Paolo Bonzini677e7802015-03-23 10:53:21 +0100724 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
725 * are the bit indices.
Avi Kivity55043ba2011-12-15 17:20:34 +0200726 *
727 * @mr: the memory region being queried
728 */
Paolo Bonzini2d1a35b2015-03-23 10:50:57 +0100729uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr);
Avi Kivity55043ba2011-12-15 17:20:34 +0200730
731/**
Avi Kivityce7923d2011-12-08 16:05:11 +0200732 * memory_region_is_rom: check whether a memory region is ROM
733 *
734 * Returns %true is a memory region is read-only memory.
735 *
736 * @mr: the memory region being queried
737 */
Paolo Bonzini1619d1f2015-12-09 17:47:39 +0100738static inline bool memory_region_is_rom(MemoryRegion *mr)
739{
740 return mr->ram && mr->readonly;
741}
742
Avi Kivityce7923d2011-12-08 16:05:11 +0200743
744/**
Paolo Bonzinia35ba7b2014-06-10 19:15:23 +0800745 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
746 *
747 * Returns a file descriptor backing a file-based RAM memory region,
748 * or -1 if the region is not a file-based RAM memory region.
749 *
750 * @mr: the RAM or alias memory region being queried.
751 */
752int memory_region_get_fd(MemoryRegion *mr);
753
754/**
Paolo Bonzini4ff87572016-03-25 12:30:16 +0100755 * memory_region_set_fd: Mark a RAM memory region as backed by a
756 * file descriptor.
757 *
758 * This function is typically used after memory_region_init_ram_ptr().
759 *
760 * @mr: the memory region being queried.
761 * @fd: the file descriptor that backs @mr.
762 */
763void memory_region_set_fd(MemoryRegion *mr, int fd);
764
765/**
Paolo Bonzini07bdaa42016-03-25 12:55:08 +0100766 * memory_region_from_host: Convert a pointer into a RAM memory region
767 * and an offset within it.
768 *
769 * Given a host pointer inside a RAM memory region (created with
770 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
771 * the MemoryRegion and the offset within it.
772 *
773 * Use with care; by the time this function returns, the returned pointer is
774 * not protected by RCU anymore. If the caller is not within an RCU critical
775 * section and does not hold the iothread lock, it must have other means of
776 * protecting the pointer, such as a reference to the region that includes
777 * the incoming ram_addr_t.
778 *
779 * @mr: the memory region being queried.
780 */
781MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset);
782
783/**
Avi Kivity093bc2c2011-07-26 14:26:01 +0300784 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
785 *
786 * Returns a host pointer to a RAM memory region (created with
Paolo Bonzini49b24af2015-12-16 10:30:47 +0100787 * memory_region_init_ram() or memory_region_init_ram_ptr()).
788 *
789 * Use with care; by the time this function returns, the returned pointer is
790 * not protected by RCU anymore. If the caller is not within an RCU critical
791 * section and does not hold the iothread lock, it must have other means of
792 * protecting the pointer, such as a reference to the region that includes
793 * the incoming ram_addr_t.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300794 *
795 * @mr: the memory region being queried.
796 */
797void *memory_region_get_ram_ptr(MemoryRegion *mr);
798
Paolo Bonzini37d7c082015-03-23 10:21:46 +0100799/* memory_region_ram_resize: Resize a RAM region.
800 *
801 * Only legal before guest might have detected the memory size: e.g. on
802 * incoming migration, or right after reset.
803 *
804 * @mr: a memory region created with @memory_region_init_resizeable_ram.
805 * @newsize: the new size the region
806 * @errp: pointer to Error*, to store an error if it happens.
807 */
808void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize,
809 Error **errp);
810
Avi Kivity093bc2c2011-07-26 14:26:01 +0300811/**
Avi Kivity093bc2c2011-07-26 14:26:01 +0300812 * memory_region_set_log: Turn dirty logging on or off for a region.
813 *
814 * Turns dirty logging on or off for a specified client (display, migration).
815 * Only meaningful for RAM regions.
816 *
817 * @mr: the memory region being updated.
818 * @log: whether dirty logging is to be enabled or disabled.
Paolo Bonzinidbddac62015-03-23 10:31:53 +0100819 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300820 */
821void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
822
823/**
Blue Swirlcd7a45c2012-01-22 16:38:21 +0000824 * memory_region_get_dirty: Check whether a range of bytes is dirty
825 * for a specified client.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300826 *
Blue Swirlcd7a45c2012-01-22 16:38:21 +0000827 * Checks whether a range of bytes has been written to since the last
Avi Kivity093bc2c2011-07-26 14:26:01 +0300828 * call to memory_region_reset_dirty() with the same @client. Dirty logging
829 * must be enabled.
830 *
831 * @mr: the memory region being queried.
832 * @addr: the address (relative to the start of the region) being queried.
Blue Swirlcd7a45c2012-01-22 16:38:21 +0000833 * @size: the size of the range being queried.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300834 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
835 * %DIRTY_MEMORY_VGA.
836 */
Avi Kivitya8170e52012-10-23 12:30:10 +0200837bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
838 hwaddr size, unsigned client);
Avi Kivity093bc2c2011-07-26 14:26:01 +0300839
840/**
Blue Swirlfd4aa972011-10-16 16:04:59 +0000841 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300842 *
Blue Swirlfd4aa972011-10-16 16:04:59 +0000843 * Marks a range of bytes as dirty, after it has been dirtied outside
844 * guest code.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300845 *
Blue Swirlfd4aa972011-10-16 16:04:59 +0000846 * @mr: the memory region being dirtied.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300847 * @addr: the address (relative to the start of the region) being dirtied.
Blue Swirlfd4aa972011-10-16 16:04:59 +0000848 * @size: size of the range being dirtied.
Avi Kivity093bc2c2011-07-26 14:26:01 +0300849 */
Avi Kivitya8170e52012-10-23 12:30:10 +0200850void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
851 hwaddr size);
Avi Kivity093bc2c2011-07-26 14:26:01 +0300852
853/**
Juan Quintela6c279db2012-10-17 20:24:28 +0200854 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty
855 * for a specified client. It clears them.
856 *
857 * Checks whether a range of bytes has been written to since the last
858 * call to memory_region_reset_dirty() with the same @client. Dirty logging
859 * must be enabled.
860 *
861 * @mr: the memory region being queried.
862 * @addr: the address (relative to the start of the region) being queried.
863 * @size: the size of the range being queried.
864 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
865 * %DIRTY_MEMORY_VGA.
866 */
867bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr,
868 hwaddr size, unsigned client);
869/**
Avi Kivity093bc2c2011-07-26 14:26:01 +0300870 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
871 * any external TLBs (e.g. kvm)
872 *
873 * Flushes dirty information from accelerators such as kvm and vhost-net
874 * and makes it available to users of the memory API.
875 *
876 * @mr: the region being flushed.
877 */
878void memory_region_sync_dirty_bitmap(MemoryRegion *mr);
879
880/**
881 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
882 * client.
883 *
884 * Marks a range of pages as no longer dirty.
885 *
886 * @mr: the region being updated.
887 * @addr: the start of the subrange being cleaned.
888 * @size: the size of the subrange being cleaned.
889 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
890 * %DIRTY_MEMORY_VGA.
891 */
Avi Kivitya8170e52012-10-23 12:30:10 +0200892void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
893 hwaddr size, unsigned client);
Avi Kivity093bc2c2011-07-26 14:26:01 +0300894
895/**
896 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
897 *
898 * Allows a memory region to be marked as read-only (turning it into a ROM).
899 * only useful on RAM regions.
900 *
901 * @mr: the region being updated.
902 * @readonly: whether rhe region is to be ROM or RAM.
903 */
904void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
905
906/**
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +0200907 * memory_region_rom_device_set_romd: enable/disable ROMD mode
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300908 *
909 * Allows a ROM device (initialized with memory_region_init_rom_device() to
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +0200910 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
911 * device is mapped to guest memory and satisfies read access directly.
912 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
913 * Writes are always handled by the #MemoryRegion.write function.
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300914 *
915 * @mr: the memory region to be updated
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +0200916 * @romd_mode: %true to put the region into ROMD mode
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300917 */
Jan Kiszka5f9a5ea2013-05-07 19:04:25 +0200918void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode);
Avi Kivityd0a9b5b2011-08-08 19:58:49 +0300919
920/**
Avi Kivity093bc2c2011-07-26 14:26:01 +0300921 * memory_region_set_coalescing: Enable memory coalescing for the region.
922 *
923 * Enabled writes to a region to be queued for later processing. MMIO ->write
924 * callbacks may be delayed until a non-coalesced MMIO is issued.
925 * Only useful for IO regions. Roughly similar to write-combining hardware.
926 *
927 * @mr: the memory region to be write coalesced
928 */
929void memory_region_set_coalescing(MemoryRegion *mr);
930
931/**
932 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
933 * a region.
934 *
935 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
936 * Multiple calls can be issued coalesced disjoint ranges.
937 *
938 * @mr: the memory region to be updated.
939 * @offset: the start of the range within the region to be coalesced.
940 * @size: the size of the subrange to be coalesced.
941 */
942void memory_region_add_coalescing(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +0200943 hwaddr offset,
Avi Kivity093bc2c2011-07-26 14:26:01 +0300944 uint64_t size);
945
946/**
947 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
948 *
949 * Disables any coalescing caused by memory_region_set_coalescing() or
950 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
951 * hardware.
952 *
953 * @mr: the memory region to be updated.
954 */
955void memory_region_clear_coalescing(MemoryRegion *mr);
956
957/**
Jan Kiszkad4105152012-08-23 13:02:29 +0200958 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
959 * accesses.
960 *
961 * Ensure that pending coalesced MMIO request are flushed before the memory
962 * region is accessed. This property is automatically enabled for all regions
963 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
964 *
965 * @mr: the memory region to be updated.
966 */
967void memory_region_set_flush_coalesced(MemoryRegion *mr);
968
969/**
970 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
971 * accesses.
972 *
973 * Clear the automatic coalesced MMIO flushing enabled via
974 * memory_region_set_flush_coalesced. Note that this service has no effect on
975 * memory regions that have MMIO coalescing enabled for themselves. For them,
976 * automatic flushing will stop once coalescing is disabled.
977 *
978 * @mr: the memory region to be updated.
979 */
980void memory_region_clear_flush_coalesced(MemoryRegion *mr);
981
982/**
Jan Kiszka196ea132015-06-18 18:47:20 +0200983 * memory_region_set_global_locking: Declares the access processing requires
984 * QEMU's global lock.
985 *
986 * When this is invoked, accesses to the memory region will be processed while
987 * holding the global lock of QEMU. This is the default behavior of memory
988 * regions.
989 *
990 * @mr: the memory region to be updated.
991 */
992void memory_region_set_global_locking(MemoryRegion *mr);
993
994/**
995 * memory_region_clear_global_locking: Declares that access processing does
996 * not depend on the QEMU global lock.
997 *
998 * By clearing this property, accesses to the memory region will be processed
999 * outside of QEMU's global lock (unless the lock is held on when issuing the
1000 * access request). In this case, the device model implementing the access
1001 * handlers is responsible for synchronization of concurrency.
1002 *
1003 * @mr: the memory region to be updated.
1004 */
1005void memory_region_clear_global_locking(MemoryRegion *mr);
1006
1007/**
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001008 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
1009 * is written to a location.
1010 *
1011 * Marks a word in an IO region (initialized with memory_region_init_io())
1012 * 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 -03001013 * The caller must be prepared to handle failure (that is, take the required
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001014 * action if the callback _is_ called).
1015 *
1016 * @mr: the memory region being updated.
1017 * @addr: the address within @mr that is to be monitored
1018 * @size: the size of the access to trigger the eventfd
1019 * @match_data: whether to match against @data, instead of just @addr
1020 * @data: the data to match against the guest write
1021 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
1022 **/
1023void memory_region_add_eventfd(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +02001024 hwaddr addr,
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001025 unsigned size,
1026 bool match_data,
1027 uint64_t data,
Paolo Bonzini753d5e12012-07-05 17:16:27 +02001028 EventNotifier *e);
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001029
1030/**
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -03001031 * memory_region_del_eventfd: Cancel an eventfd.
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001032 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -03001033 * Cancels an eventfd trigger requested by a previous
1034 * memory_region_add_eventfd() call.
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001035 *
1036 * @mr: the memory region being updated.
1037 * @addr: the address within @mr that is to be monitored
1038 * @size: the size of the access to trigger the eventfd
1039 * @match_data: whether to match against @data, instead of just @addr
1040 * @data: the data to match against the guest write
1041 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
1042 */
1043void memory_region_del_eventfd(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +02001044 hwaddr addr,
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001045 unsigned size,
1046 bool match_data,
1047 uint64_t data,
Paolo Bonzini753d5e12012-07-05 17:16:27 +02001048 EventNotifier *e);
1049
Avi Kivity3e9d69e2011-07-26 14:26:11 +03001050/**
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -03001051 * memory_region_add_subregion: Add a subregion to a container.
Avi Kivity093bc2c2011-07-26 14:26:01 +03001052 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -03001053 * Adds a subregion at @offset. The subregion may not overlap with other
Avi Kivity093bc2c2011-07-26 14:26:01 +03001054 * subregions (except for those explicitly marked as overlapping). A region
1055 * may only be added once as a subregion (unless removed with
1056 * memory_region_del_subregion()); use memory_region_init_alias() if you
1057 * want a region to be a subregion in multiple locations.
1058 *
1059 * @mr: the region to contain the new subregion; must be a container
1060 * initialized with memory_region_init().
1061 * @offset: the offset relative to @mr where @subregion is added.
1062 * @subregion: the subregion to be added.
1063 */
1064void memory_region_add_subregion(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +02001065 hwaddr offset,
Avi Kivity093bc2c2011-07-26 14:26:01 +03001066 MemoryRegion *subregion);
1067/**
BALATON Zoltan1a7e8ca2012-08-22 17:18:38 +02001068 * memory_region_add_subregion_overlap: Add a subregion to a container
1069 * with overlap.
Avi Kivity093bc2c2011-07-26 14:26:01 +03001070 *
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -03001071 * Adds a subregion at @offset. The subregion may overlap with other
Avi Kivity093bc2c2011-07-26 14:26:01 +03001072 * subregions. Conflicts are resolved by having a higher @priority hide a
1073 * lower @priority. Subregions without priority are taken as @priority 0.
1074 * A region may only be added once as a subregion (unless removed with
1075 * memory_region_del_subregion()); use memory_region_init_alias() if you
1076 * want a region to be a subregion in multiple locations.
1077 *
1078 * @mr: the region to contain the new subregion; must be a container
1079 * initialized with memory_region_init().
1080 * @offset: the offset relative to @mr where @subregion is added.
1081 * @subregion: the subregion to be added.
1082 * @priority: used for resolving overlaps; highest priority wins.
1083 */
1084void memory_region_add_subregion_overlap(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +02001085 hwaddr offset,
Avi Kivity093bc2c2011-07-26 14:26:01 +03001086 MemoryRegion *subregion,
Marcel Apfelbauma1ff8ae2013-09-16 11:21:14 +03001087 int priority);
Avi Kivitye34911c2011-12-19 12:06:23 +02001088
1089/**
1090 * memory_region_get_ram_addr: Get the ram address associated with a memory
1091 * region
Avi Kivitye34911c2011-12-19 12:06:23 +02001092 */
Fam Zheng7ebb2742016-03-01 14:18:20 +08001093ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr);
Avi Kivitye34911c2011-12-19 12:06:23 +02001094
Igor Mammedova2b257d2014-10-31 16:38:37 +00001095uint64_t memory_region_get_alignment(const MemoryRegion *mr);
Avi Kivity093bc2c2011-07-26 14:26:01 +03001096/**
1097 * memory_region_del_subregion: Remove a subregion.
1098 *
1099 * Removes a subregion from its container.
1100 *
1101 * @mr: the container to be updated.
1102 * @subregion: the region being removed; must be a current subregion of @mr.
1103 */
1104void memory_region_del_subregion(MemoryRegion *mr,
1105 MemoryRegion *subregion);
1106
Avi Kivity6bba19b2011-09-14 11:54:58 +03001107/*
1108 * memory_region_set_enabled: dynamically enable or disable a region
1109 *
1110 * Enables or disables a memory region. A disabled memory region
1111 * ignores all accesses to itself and its subregions. It does not
1112 * obscure sibling subregions with lower priority - it simply behaves as
1113 * if it was removed from the hierarchy.
1114 *
1115 * Regions default to being enabled.
1116 *
1117 * @mr: the region to be updated
1118 * @enabled: whether to enable or disable the region
1119 */
1120void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
1121
Avi Kivity2282e1a2011-09-14 12:10:12 +03001122/*
1123 * memory_region_set_address: dynamically update the address of a region
1124 *
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +02001125 * Dynamically updates the address of a region, relative to its container.
Avi Kivity2282e1a2011-09-14 12:10:12 +03001126 * May be used on regions are currently part of a memory hierarchy.
1127 *
1128 * @mr: the region to be updated
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +02001129 * @addr: new address, relative to container region
Avi Kivity2282e1a2011-09-14 12:10:12 +03001130 */
Avi Kivitya8170e52012-10-23 12:30:10 +02001131void memory_region_set_address(MemoryRegion *mr, hwaddr addr);
Avi Kivity2282e1a2011-09-14 12:10:12 +03001132
Avi Kivity47033592011-12-04 19:16:50 +02001133/*
Michael S. Tsirkine7af4c62014-12-16 11:21:23 +02001134 * memory_region_set_size: dynamically update the size of a region.
1135 *
1136 * Dynamically updates the size of a region.
1137 *
1138 * @mr: the region to be updated
1139 * @size: used size of the region.
1140 */
1141void memory_region_set_size(MemoryRegion *mr, uint64_t size);
1142
1143/*
Avi Kivity47033592011-12-04 19:16:50 +02001144 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1145 *
1146 * Dynamically updates the offset into the target region that an alias points
1147 * to, as if the fourth argument to memory_region_init_alias() has changed.
1148 *
1149 * @mr: the #MemoryRegion to be updated; should be an alias.
1150 * @offset: the new offset into the target memory region
1151 */
1152void memory_region_set_alias_offset(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +02001153 hwaddr offset);
Avi Kivity47033592011-12-04 19:16:50 +02001154
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -03001155/**
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +02001156 * memory_region_present: checks if an address relative to a @container
1157 * translates into #MemoryRegion within @container
Paolo Bonzini3ce10902013-07-02 13:40:48 +02001158 *
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +02001159 * Answer whether a #MemoryRegion within @container covers the address
Paolo Bonzini3ce10902013-07-02 13:40:48 +02001160 * @addr.
1161 *
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +02001162 * @container: a #MemoryRegion within which @addr is a relative address
1163 * @addr: the area within @container to be searched
Paolo Bonzini3ce10902013-07-02 13:40:48 +02001164 */
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +02001165bool memory_region_present(MemoryRegion *container, hwaddr addr);
Paolo Bonzini3ce10902013-07-02 13:40:48 +02001166
1167/**
Igor Mammedoveed2bac2014-06-02 15:25:06 +02001168 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1169 * into any address space.
1170 *
1171 * @mr: a #MemoryRegion which should be checked if it's mapped
1172 */
1173bool memory_region_is_mapped(MemoryRegion *mr);
1174
1175/**
Paolo Bonzini73034e92013-05-07 15:48:28 +02001176 * memory_region_find: translate an address/size relative to a
1177 * MemoryRegion into a #MemoryRegionSection.
Avi Kivitye2177952011-12-08 15:00:18 +02001178 *
Paolo Bonzini73034e92013-05-07 15:48:28 +02001179 * Locates the first #MemoryRegion within @mr that overlaps the range
1180 * given by @addr and @size.
Avi Kivitye2177952011-12-08 15:00:18 +02001181 *
1182 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1183 * It will have the following characteristics:
Avi Kivitye2177952011-12-08 15:00:18 +02001184 * .@size = 0 iff no overlap was found
1185 * .@mr is non-%NULL iff an overlap was found
1186 *
Paolo Bonzini73034e92013-05-07 15:48:28 +02001187 * Remember that in the return value the @offset_within_region is
1188 * relative to the returned region (in the .@mr field), not to the
1189 * @mr argument.
1190 *
1191 * Similarly, the .@offset_within_address_space is relative to the
1192 * address space that contains both regions, the passed and the
1193 * returned one. However, in the special case where the @mr argument
Paolo Bonzinifeca4ac2014-06-11 11:18:09 +02001194 * has no container (and thus is the root of the address space), the
Paolo Bonzini73034e92013-05-07 15:48:28 +02001195 * following will hold:
1196 * .@offset_within_address_space >= @addr
1197 * .@offset_within_address_space + .@size <= @addr + @size
1198 *
1199 * @mr: a MemoryRegion within which @addr is a relative address
1200 * @addr: start of the area within @as to be searched
Avi Kivitye2177952011-12-08 15:00:18 +02001201 * @size: size of the area to be searched
1202 */
Paolo Bonzini73034e92013-05-07 15:48:28 +02001203MemoryRegionSection memory_region_find(MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +02001204 hwaddr addr, uint64_t size);
Avi Kivitye2177952011-12-08 15:00:18 +02001205
Blue Swirlfd062572012-04-09 17:38:52 +00001206/**
Paolo Bonzini9c1f8f42016-09-22 16:08:31 +02001207 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
Avi Kivity86e775c2011-12-15 16:24:49 +02001208 *
Paolo Bonzini9c1f8f42016-09-22 16:08:31 +02001209 * Synchronizes the dirty page log for all address spaces.
Avi Kivity86e775c2011-12-15 16:24:49 +02001210 */
Paolo Bonzini9c1f8f42016-09-22 16:08:31 +02001211void memory_global_dirty_log_sync(void);
Avi Kivity86e775c2011-12-15 16:24:49 +02001212
Avi Kivitye2177952011-12-08 15:00:18 +02001213/**
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -03001214 * memory_region_transaction_begin: Start a transaction.
1215 *
1216 * During a transaction, changes will be accumulated and made visible
Stefan Weildabdf392012-01-08 19:35:09 +01001217 * only when the transaction ends (is committed).
Avi Kivity4ef4db82011-07-26 14:26:13 +03001218 */
1219void memory_region_transaction_begin(void);
Ademar de Souza Reis Jr69ddaf62011-12-05 16:54:14 -03001220
1221/**
1222 * memory_region_transaction_commit: Commit a transaction and make changes
1223 * visible to the guest.
Avi Kivity4ef4db82011-07-26 14:26:13 +03001224 */
1225void memory_region_transaction_commit(void);
1226
Avi Kivity7664e802011-12-11 14:47:25 +02001227/**
1228 * memory_listener_register: register callbacks to be called when memory
1229 * sections are mapped or unmapped into an address
1230 * space
1231 *
1232 * @listener: an object containing the callbacks to be called
Avi Kivity7376e582012-02-08 21:05:17 +02001233 * @filter: if non-%NULL, only regions in this address space will be observed
Avi Kivity7664e802011-12-11 14:47:25 +02001234 */
Avi Kivityf6790af2012-10-02 20:13:51 +02001235void memory_listener_register(MemoryListener *listener, AddressSpace *filter);
Avi Kivity7664e802011-12-11 14:47:25 +02001236
1237/**
1238 * memory_listener_unregister: undo the effect of memory_listener_register()
1239 *
1240 * @listener: an object containing the callbacks to be removed
1241 */
1242void memory_listener_unregister(MemoryListener *listener);
1243
1244/**
1245 * memory_global_dirty_log_start: begin dirty logging for all regions
1246 */
1247void memory_global_dirty_log_start(void);
1248
1249/**
BALATON Zoltan1a7e8ca2012-08-22 17:18:38 +02001250 * memory_global_dirty_log_stop: end dirty logging for all regions
Avi Kivity7664e802011-12-11 14:47:25 +02001251 */
1252void memory_global_dirty_log_stop(void);
1253
Blue Swirl314e2982011-09-11 20:22:05 +00001254void mtree_info(fprintf_function mon_printf, void *f);
1255
Avi Kivity9ad2bbc2012-10-02 14:59:23 +02001256/**
Peter Maydell3b643492015-04-26 16:49:23 +01001257 * memory_region_dispatch_read: perform a read directly to the specified
1258 * MemoryRegion.
1259 *
1260 * @mr: #MemoryRegion to access
1261 * @addr: address within that region
1262 * @pval: pointer to uint64_t which the data is written to
1263 * @size: size of the access in bytes
1264 * @attrs: memory transaction attributes to use for the access
1265 */
1266MemTxResult memory_region_dispatch_read(MemoryRegion *mr,
1267 hwaddr addr,
1268 uint64_t *pval,
1269 unsigned size,
1270 MemTxAttrs attrs);
1271/**
1272 * memory_region_dispatch_write: perform a write directly to the specified
1273 * MemoryRegion.
1274 *
1275 * @mr: #MemoryRegion to access
1276 * @addr: address within that region
1277 * @data: data to write
1278 * @size: size of the access in bytes
1279 * @attrs: memory transaction attributes to use for the access
1280 */
1281MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
1282 hwaddr addr,
1283 uint64_t data,
1284 unsigned size,
1285 MemTxAttrs attrs);
1286
1287/**
Avi Kivity9ad2bbc2012-10-02 14:59:23 +02001288 * address_space_init: initializes an address space
1289 *
1290 * @as: an uninitialized #AddressSpace
Veres Lajos67cc32e2015-09-08 22:45:14 +01001291 * @root: a #MemoryRegion that routes addresses for the address space
Alexey Kardashevskiy7dca8042013-04-29 16:25:51 +00001292 * @name: an address space name. The name is only used for debugging
1293 * output.
Avi Kivity9ad2bbc2012-10-02 14:59:23 +02001294 */
Alexey Kardashevskiy7dca8042013-04-29 16:25:51 +00001295void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
Avi Kivity9ad2bbc2012-10-02 14:59:23 +02001296
Peter Crosthwaitef0c02d12016-01-21 14:15:06 +00001297/**
1298 * address_space_init_shareable: return an address space for a memory region,
1299 * creating it if it does not already exist
1300 *
1301 * @root: a #MemoryRegion that routes addresses for the address space
1302 * @name: an address space name. The name is only used for debugging
1303 * output.
1304 *
1305 * This function will return a pointer to an existing AddressSpace
1306 * which was initialized with the specified MemoryRegion, or it will
1307 * create and initialize one if it does not already exist. The ASes
1308 * are reference-counted, so the memory will be freed automatically
1309 * when the AddressSpace is destroyed via address_space_destroy.
1310 */
1311AddressSpace *address_space_init_shareable(MemoryRegion *root,
1312 const char *name);
Avi Kivity83f3c252012-10-07 12:59:55 +02001313
1314/**
1315 * address_space_destroy: destroy an address space
1316 *
1317 * Releases all resources associated with an address space. After an address space
1318 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1319 * as well.
1320 *
1321 * @as: address space to be destroyed
1322 */
1323void address_space_destroy(AddressSpace *as);
1324
Avi Kivityac1970f2012-10-03 16:22:53 +02001325/**
1326 * address_space_rw: read from or write to an address space.
1327 *
Peter Maydell5c9eb022015-04-26 16:49:24 +01001328 * Return a MemTxResult indicating whether the operation succeeded
1329 * or failed (eg unassigned memory, device rejected the transaction,
1330 * IOMMU fault).
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02001331 *
Avi Kivityac1970f2012-10-03 16:22:53 +02001332 * @as: #AddressSpace to be accessed
1333 * @addr: address within that address space
Peter Maydell5c9eb022015-04-26 16:49:24 +01001334 * @attrs: memory transaction attributes
Avi Kivityac1970f2012-10-03 16:22:53 +02001335 * @buf: buffer with the data transferred
1336 * @is_write: indicates the transfer direction
1337 */
Peter Maydell5c9eb022015-04-26 16:49:24 +01001338MemTxResult address_space_rw(AddressSpace *as, hwaddr addr,
1339 MemTxAttrs attrs, uint8_t *buf,
1340 int len, bool is_write);
Avi Kivityac1970f2012-10-03 16:22:53 +02001341
1342/**
1343 * address_space_write: write to address space.
1344 *
Peter Maydell5c9eb022015-04-26 16:49:24 +01001345 * Return a MemTxResult indicating whether the operation succeeded
1346 * or failed (eg unassigned memory, device rejected the transaction,
1347 * IOMMU fault).
Avi Kivityac1970f2012-10-03 16:22:53 +02001348 *
1349 * @as: #AddressSpace to be accessed
1350 * @addr: address within that address space
Peter Maydell5c9eb022015-04-26 16:49:24 +01001351 * @attrs: memory transaction attributes
Avi Kivityac1970f2012-10-03 16:22:53 +02001352 * @buf: buffer with the data transferred
1353 */
Peter Maydell5c9eb022015-04-26 16:49:24 +01001354MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
1355 MemTxAttrs attrs,
1356 const uint8_t *buf, int len);
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02001357
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01001358/* address_space_ld*: load from an address space
Peter Maydell50013112015-04-26 16:49:24 +01001359 * address_space_st*: store to an address space
1360 *
1361 * These functions perform a load or store of the byte, word,
1362 * longword or quad to the specified address within the AddressSpace.
1363 * The _le suffixed functions treat the data as little endian;
1364 * _be indicates big endian; no suffix indicates "same endianness
1365 * as guest CPU".
1366 *
1367 * The "guest CPU endianness" accessors are deprecated for use outside
1368 * target-* code; devices should be CPU-agnostic and use either the LE
1369 * or the BE accessors.
1370 *
1371 * @as #AddressSpace to be accessed
1372 * @addr: address within that address space
1373 * @val: data value, for stores
1374 * @attrs: memory transaction attributes
1375 * @result: location to write the success/failure of the transaction;
1376 * if NULL, this information is discarded
1377 */
1378uint32_t address_space_ldub(AddressSpace *as, hwaddr addr,
1379 MemTxAttrs attrs, MemTxResult *result);
1380uint32_t address_space_lduw_le(AddressSpace *as, hwaddr addr,
1381 MemTxAttrs attrs, MemTxResult *result);
1382uint32_t address_space_lduw_be(AddressSpace *as, hwaddr addr,
1383 MemTxAttrs attrs, MemTxResult *result);
1384uint32_t address_space_ldl_le(AddressSpace *as, hwaddr addr,
1385 MemTxAttrs attrs, MemTxResult *result);
1386uint32_t address_space_ldl_be(AddressSpace *as, hwaddr addr,
1387 MemTxAttrs attrs, MemTxResult *result);
1388uint64_t address_space_ldq_le(AddressSpace *as, hwaddr addr,
1389 MemTxAttrs attrs, MemTxResult *result);
1390uint64_t address_space_ldq_be(AddressSpace *as, hwaddr addr,
1391 MemTxAttrs attrs, MemTxResult *result);
1392void address_space_stb(AddressSpace *as, hwaddr addr, uint32_t val,
1393 MemTxAttrs attrs, MemTxResult *result);
1394void address_space_stw_le(AddressSpace *as, hwaddr addr, uint32_t val,
1395 MemTxAttrs attrs, MemTxResult *result);
1396void address_space_stw_be(AddressSpace *as, hwaddr addr, uint32_t val,
1397 MemTxAttrs attrs, MemTxResult *result);
1398void address_space_stl_le(AddressSpace *as, hwaddr addr, uint32_t val,
1399 MemTxAttrs attrs, MemTxResult *result);
1400void address_space_stl_be(AddressSpace *as, hwaddr addr, uint32_t val,
1401 MemTxAttrs attrs, MemTxResult *result);
1402void address_space_stq_le(AddressSpace *as, hwaddr addr, uint64_t val,
1403 MemTxAttrs attrs, MemTxResult *result);
1404void address_space_stq_be(AddressSpace *as, hwaddr addr, uint64_t val,
1405 MemTxAttrs attrs, MemTxResult *result);
1406
Paolo Bonzini0ce265f2016-11-22 11:34:02 +01001407uint32_t ldub_phys(AddressSpace *as, hwaddr addr);
1408uint32_t lduw_le_phys(AddressSpace *as, hwaddr addr);
1409uint32_t lduw_be_phys(AddressSpace *as, hwaddr addr);
1410uint32_t ldl_le_phys(AddressSpace *as, hwaddr addr);
1411uint32_t ldl_be_phys(AddressSpace *as, hwaddr addr);
1412uint64_t ldq_le_phys(AddressSpace *as, hwaddr addr);
1413uint64_t ldq_be_phys(AddressSpace *as, hwaddr addr);
1414void stb_phys(AddressSpace *as, hwaddr addr, uint32_t val);
1415void stw_le_phys(AddressSpace *as, hwaddr addr, uint32_t val);
1416void stw_be_phys(AddressSpace *as, hwaddr addr, uint32_t val);
1417void stl_le_phys(AddressSpace *as, hwaddr addr, uint32_t val);
1418void stl_be_phys(AddressSpace *as, hwaddr addr, uint32_t val);
1419void stq_le_phys(AddressSpace *as, hwaddr addr, uint64_t val);
1420void stq_be_phys(AddressSpace *as, hwaddr addr, uint64_t val);
1421
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001422/* address_space_translate: translate an address range into an address space
Paolo Bonzini41063e12015-03-18 14:21:43 +01001423 * into a MemoryRegion and an address range into that section. Should be
1424 * called from an RCU critical section, to avoid that the last reference
1425 * to the returned region disappears after address_space_translate returns.
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001426 *
1427 * @as: #AddressSpace to be accessed
1428 * @addr: address within that address space
1429 * @xlat: pointer to address within the returned memory region section's
1430 * #MemoryRegion.
1431 * @len: pointer to length
1432 * @is_write: indicates the transfer direction
1433 */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02001434MemoryRegion *address_space_translate(AddressSpace *as, hwaddr addr,
1435 hwaddr *xlat, hwaddr *len,
1436 bool is_write);
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001437
Paolo Bonzini51644ab2013-04-11 15:40:59 +02001438/* address_space_access_valid: check for validity of accessing an address
1439 * space range
1440 *
Avi Kivity30951152012-10-30 13:47:46 +02001441 * Check whether memory is assigned to the given address space range, and
1442 * access is permitted by any IOMMU regions that are active for the address
1443 * space.
Paolo Bonzini51644ab2013-04-11 15:40:59 +02001444 *
1445 * For now, addr and len should be aligned to a page size. This limitation
1446 * will be lifted in the future.
1447 *
1448 * @as: #AddressSpace to be accessed
1449 * @addr: address within that address space
1450 * @len: length of the area to be checked
1451 * @is_write: indicates the transfer direction
1452 */
1453bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write);
1454
Avi Kivityac1970f2012-10-03 16:22:53 +02001455/* address_space_map: map a physical memory region into a host virtual address
1456 *
1457 * May map a subset of the requested range, given by and returned in @plen.
1458 * May return %NULL if resources needed to perform the mapping are exhausted.
1459 * Use only for reads OR writes - not for read-modify-write operations.
1460 * Use cpu_register_map_client() to know when retrying the map operation is
1461 * likely to succeed.
1462 *
1463 * @as: #AddressSpace to be accessed
1464 * @addr: address within that address space
1465 * @plen: pointer to length of buffer; updated on return
1466 * @is_write: indicates the transfer direction
1467 */
Avi Kivitya8170e52012-10-23 12:30:10 +02001468void *address_space_map(AddressSpace *as, hwaddr addr,
1469 hwaddr *plen, bool is_write);
Avi Kivityac1970f2012-10-03 16:22:53 +02001470
1471/* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1472 *
1473 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1474 * the amount of memory that was actually read or written by the caller.
1475 *
1476 * @as: #AddressSpace used
1477 * @addr: address within that address space
1478 * @len: buffer length as returned by address_space_map()
1479 * @access_len: amount of data actually transferred
1480 * @is_write: indicates the transfer direction
1481 */
Avi Kivitya8170e52012-10-23 12:30:10 +02001482void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
1483 int is_write, hwaddr access_len);
Avi Kivityac1970f2012-10-03 16:22:53 +02001484
1485
Paolo Bonzinia203ac72015-12-09 10:18:57 +01001486/* Internal functions, part of the implementation of address_space_read. */
1487MemTxResult address_space_read_continue(AddressSpace *as, hwaddr addr,
1488 MemTxAttrs attrs, uint8_t *buf,
1489 int len, hwaddr addr1, hwaddr l,
1490 MemoryRegion *mr);
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01001491MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr,
1492 MemTxAttrs attrs, uint8_t *buf, int len);
Paolo Bonzini0878d0e2016-02-22 11:02:12 +01001493void *qemu_map_ram_ptr(RAMBlock *ram_block, ram_addr_t addr);
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01001494
1495static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
1496{
1497 if (is_write) {
Alex Williamson4a2e2422016-10-31 09:53:03 -06001498 return memory_region_is_ram(mr) &&
1499 !mr->readonly && !memory_region_is_ram_device(mr);
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01001500 } else {
Alex Williamson4a2e2422016-10-31 09:53:03 -06001501 return (memory_region_is_ram(mr) && !memory_region_is_ram_device(mr)) ||
1502 memory_region_is_romd(mr);
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01001503 }
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01001504}
1505
1506/**
1507 * address_space_read: read from an address space.
1508 *
1509 * Return a MemTxResult indicating whether the operation succeeded
1510 * or failed (eg unassigned memory, device rejected the transaction,
1511 * IOMMU fault).
1512 *
1513 * @as: #AddressSpace to be accessed
1514 * @addr: address within that address space
1515 * @attrs: memory transaction attributes
1516 * @buf: buffer with the data transferred
1517 */
1518static inline __attribute__((__always_inline__))
1519MemTxResult address_space_read(AddressSpace *as, hwaddr addr, MemTxAttrs attrs,
1520 uint8_t *buf, int len)
1521{
1522 MemTxResult result = MEMTX_OK;
1523 hwaddr l, addr1;
1524 void *ptr;
1525 MemoryRegion *mr;
1526
1527 if (__builtin_constant_p(len)) {
1528 if (len) {
1529 rcu_read_lock();
1530 l = len;
1531 mr = address_space_translate(as, addr, &addr1, &l, false);
1532 if (len == l && memory_access_is_direct(mr, false)) {
Paolo Bonzini0878d0e2016-02-22 11:02:12 +01001533 ptr = qemu_map_ram_ptr(mr->ram_block, addr1);
Paolo Bonzini3cc8f882015-12-09 10:34:13 +01001534 memcpy(buf, ptr, len);
1535 } else {
1536 result = address_space_read_continue(as, addr, attrs, buf, len,
1537 addr1, l, mr);
1538 }
1539 rcu_read_unlock();
1540 }
1541 } else {
1542 result = address_space_read_full(as, addr, attrs, buf, len);
1543 }
1544 return result;
1545}
Paolo Bonzinia203ac72015-12-09 10:18:57 +01001546
Avi Kivity093bc2c2011-07-26 14:26:01 +03001547#endif
1548
1549#endif