blob: a8c79e5fdaf3464b1f0e4dc9867761f9e19836d1 [file] [log] [blame]
bellard54936002003-05-13 00:25:15 +00001/*
Blue Swirl5b6dd862012-12-02 16:04:43 +00002 * Virtual page mapping
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard54936002003-05-13 00:25:15 +00004 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
Blue Swirl8167ee82009-07-16 20:47:01 +000017 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
bellard54936002003-05-13 00:25:15 +000018 */
bellard67b915a2004-03-31 23:37:16 +000019#include "config.h"
bellardd5a8f072004-09-29 21:15:28 +000020#ifdef _WIN32
21#include <windows.h>
22#else
bellarda98d49b2004-11-14 16:22:05 +000023#include <sys/types.h>
bellardd5a8f072004-09-29 21:15:28 +000024#include <sys/mman.h>
25#endif
bellard54936002003-05-13 00:25:15 +000026
Stefan Weil055403b2010-10-22 23:03:32 +020027#include "qemu-common.h"
bellard6180a182003-09-30 21:04:53 +000028#include "cpu.h"
bellardb67d9a52008-05-23 09:57:34 +000029#include "tcg.h"
pbrookb3c77242008-06-30 16:31:04 +000030#include "hw/hw.h"
Alex Williamsoncc9e98c2010-06-25 11:09:43 -060031#include "hw/qdev.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010032#include "qemu/osdep.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010033#include "sysemu/kvm.h"
Markus Armbruster2ff3de62013-07-04 15:09:22 +020034#include "sysemu/sysemu.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010035#include "hw/xen/xen.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010036#include "qemu/timer.h"
37#include "qemu/config-file.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010038#include "exec/memory.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010039#include "sysemu/dma.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010040#include "exec/address-spaces.h"
pbrook53a59602006-03-25 19:31:22 +000041#if defined(CONFIG_USER_ONLY)
42#include <qemu.h>
Jun Nakajima432d2682010-08-31 16:41:25 +010043#else /* !CONFIG_USER_ONLY */
Paolo Bonzini9c17d612012-12-17 18:20:04 +010044#include "sysemu/xen-mapcache.h"
Stefano Stabellini6506e4f2011-05-19 18:35:44 +010045#include "trace.h"
pbrook53a59602006-03-25 19:31:22 +000046#endif
Paolo Bonzini0d6d3c82012-11-14 15:45:02 +010047#include "exec/cpu-all.h"
bellard54936002003-05-13 00:25:15 +000048
Paolo Bonzini022c62c2012-12-17 18:19:49 +010049#include "exec/cputlb.h"
Blue Swirl5b6dd862012-12-02 16:04:43 +000050#include "translate-all.h"
Blue Swirl0cac1b62012-04-09 16:50:52 +000051
Paolo Bonzini022c62c2012-12-17 18:19:49 +010052#include "exec/memory-internal.h"
Alexander Graf582b55a2013-12-11 14:17:44 +010053#include "qemu/cache-utils.h"
Avi Kivity67d95c12011-12-15 15:25:22 +020054
Michael S. Tsirkinb35ba302013-11-11 17:52:07 +020055#include "qemu/range.h"
56
blueswir1db7b5422007-05-26 17:36:03 +000057//#define DEBUG_SUBPAGE
ths1196be32007-03-17 15:17:58 +000058
pbrook99773bd2006-04-16 15:14:59 +000059#if !defined(CONFIG_USER_ONLY)
aliguori74576192008-10-06 14:02:03 +000060static int in_migration;
pbrook94a6b542009-04-11 17:15:54 +000061
Paolo Bonzinia3161032012-11-14 15:54:48 +010062RAMList ram_list = { .blocks = QTAILQ_HEAD_INITIALIZER(ram_list.blocks) };
Avi Kivity62152b82011-07-26 14:26:14 +030063
64static MemoryRegion *system_memory;
Avi Kivity309cb472011-08-08 16:09:03 +030065static MemoryRegion *system_io;
Avi Kivity62152b82011-07-26 14:26:14 +030066
Avi Kivityf6790af2012-10-02 20:13:51 +020067AddressSpace address_space_io;
68AddressSpace address_space_memory;
Avi Kivity2673a5d2012-10-02 18:49:28 +020069
Paolo Bonzini0844e002013-05-24 14:37:28 +020070MemoryRegion io_mem_rom, io_mem_notdirty;
Jan Kiszkaacc9d802013-05-26 21:55:37 +020071static MemoryRegion io_mem_unassigned;
Avi Kivity0e0df1e2012-01-02 00:32:15 +020072
pbrooke2eef172008-06-08 01:09:01 +000073#endif
bellard9fa3e852004-01-04 18:06:42 +000074
Andreas Färberbdc44642013-06-24 23:50:24 +020075struct CPUTailQ cpus = QTAILQ_HEAD_INITIALIZER(cpus);
bellard6a00d602005-11-21 23:25:50 +000076/* current CPU in the current thread. It is only valid inside
77 cpu_exec() */
Andreas Färber4917cf42013-05-27 05:17:50 +020078DEFINE_TLS(CPUState *, current_cpu);
pbrook2e70f6e2008-06-29 01:03:05 +000079/* 0 = Do not count executed instructions.
thsbf20dc02008-06-30 17:22:19 +000080 1 = Precise instruction counting.
pbrook2e70f6e2008-06-29 01:03:05 +000081 2 = Adaptive rate instruction counting. */
Paolo Bonzini5708fc62012-11-26 15:36:40 +010082int use_icount;
bellard6a00d602005-11-21 23:25:50 +000083
pbrooke2eef172008-06-08 01:09:01 +000084#if !defined(CONFIG_USER_ONLY)
Avi Kivity4346ae32012-02-10 17:00:01 +020085
Paolo Bonzini1db8abb2013-05-21 12:07:21 +020086typedef struct PhysPageEntry PhysPageEntry;
87
88struct PhysPageEntry {
Michael S. Tsirkin9736e552013-11-11 14:42:43 +020089 /* How many bits skip to next level (in units of L2_SIZE). 0 for a leaf. */
Michael S. Tsirkin8b795762013-11-11 14:51:56 +020090 uint32_t skip : 6;
Michael S. Tsirkin9736e552013-11-11 14:42:43 +020091 /* index into phys_sections (!skip) or phys_map_nodes (skip) */
Michael S. Tsirkin8b795762013-11-11 14:51:56 +020092 uint32_t ptr : 26;
Paolo Bonzini1db8abb2013-05-21 12:07:21 +020093};
94
Michael S. Tsirkin8b795762013-11-11 14:51:56 +020095#define PHYS_MAP_NODE_NIL (((uint32_t)~0) >> 6)
96
Paolo Bonzini03f49952013-11-07 17:14:36 +010097/* Size of the L2 (and L3, etc) page tables. */
Paolo Bonzini57271d62013-11-07 17:14:37 +010098#define ADDR_SPACE_BITS 64
Paolo Bonzini03f49952013-11-07 17:14:36 +010099
Michael S. Tsirkin026736c2013-11-13 20:13:03 +0200100#define P_L2_BITS 9
Paolo Bonzini03f49952013-11-07 17:14:36 +0100101#define P_L2_SIZE (1 << P_L2_BITS)
102
103#define P_L2_LEVELS (((ADDR_SPACE_BITS - TARGET_PAGE_BITS - 1) / P_L2_BITS) + 1)
104
105typedef PhysPageEntry Node[P_L2_SIZE];
Paolo Bonzini0475d942013-05-29 12:28:21 +0200106
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +0200107typedef struct PhysPageMap {
108 unsigned sections_nb;
109 unsigned sections_nb_alloc;
110 unsigned nodes_nb;
111 unsigned nodes_nb_alloc;
112 Node *nodes;
113 MemoryRegionSection *sections;
114} PhysPageMap;
115
Paolo Bonzini1db8abb2013-05-21 12:07:21 +0200116struct AddressSpaceDispatch {
117 /* This is a multi-level map on the physical address space.
118 * The bottom level has pointers to MemoryRegionSections.
119 */
120 PhysPageEntry phys_map;
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +0200121 PhysPageMap map;
Jan Kiszkaacc9d802013-05-26 21:55:37 +0200122 AddressSpace *as;
Paolo Bonzini1db8abb2013-05-21 12:07:21 +0200123};
124
Jan Kiszka90260c62013-05-26 21:46:51 +0200125#define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
126typedef struct subpage_t {
127 MemoryRegion iomem;
Jan Kiszkaacc9d802013-05-26 21:55:37 +0200128 AddressSpace *as;
Jan Kiszka90260c62013-05-26 21:46:51 +0200129 hwaddr base;
130 uint16_t sub_section[TARGET_PAGE_SIZE];
131} subpage_t;
132
Liu Ping Fanb41aac42013-05-29 11:09:17 +0200133#define PHYS_SECTION_UNASSIGNED 0
134#define PHYS_SECTION_NOTDIRTY 1
135#define PHYS_SECTION_ROM 2
136#define PHYS_SECTION_WATCH 3
Avi Kivity5312bd82012-02-12 18:32:55 +0200137
pbrooke2eef172008-06-08 01:09:01 +0000138static void io_mem_init(void);
Avi Kivity62152b82011-07-26 14:26:14 +0300139static void memory_map_init(void);
pbrooke2eef172008-06-08 01:09:01 +0000140
Avi Kivity1ec9b902012-01-02 12:47:48 +0200141static MemoryRegion io_mem_watch;
pbrook6658ffb2007-03-16 23:58:11 +0000142#endif
bellard54936002003-05-13 00:25:15 +0000143
Paul Brook6d9a1302010-02-28 23:55:53 +0000144#if !defined(CONFIG_USER_ONLY)
Avi Kivityd6f2ea22012-02-12 20:12:49 +0200145
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +0200146static void phys_map_node_reserve(PhysPageMap *map, unsigned nodes)
Avi Kivityf7bf5462012-02-13 20:12:05 +0200147{
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +0200148 if (map->nodes_nb + nodes > map->nodes_nb_alloc) {
149 map->nodes_nb_alloc = MAX(map->nodes_nb_alloc * 2, 16);
150 map->nodes_nb_alloc = MAX(map->nodes_nb_alloc, map->nodes_nb + nodes);
151 map->nodes = g_renew(Node, map->nodes, map->nodes_nb_alloc);
Avi Kivityf7bf5462012-02-13 20:12:05 +0200152 }
153}
154
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +0200155static uint32_t phys_map_node_alloc(PhysPageMap *map)
Avi Kivityd6f2ea22012-02-12 20:12:49 +0200156{
157 unsigned i;
Michael S. Tsirkin8b795762013-11-11 14:51:56 +0200158 uint32_t ret;
Avi Kivityd6f2ea22012-02-12 20:12:49 +0200159
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +0200160 ret = map->nodes_nb++;
Avi Kivityd6f2ea22012-02-12 20:12:49 +0200161 assert(ret != PHYS_MAP_NODE_NIL);
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +0200162 assert(ret != map->nodes_nb_alloc);
Paolo Bonzini03f49952013-11-07 17:14:36 +0100163 for (i = 0; i < P_L2_SIZE; ++i) {
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +0200164 map->nodes[ret][i].skip = 1;
165 map->nodes[ret][i].ptr = PHYS_MAP_NODE_NIL;
Avi Kivityd6f2ea22012-02-12 20:12:49 +0200166 }
Avi Kivityf7bf5462012-02-13 20:12:05 +0200167 return ret;
Avi Kivityd6f2ea22012-02-12 20:12:49 +0200168}
169
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +0200170static void phys_page_set_level(PhysPageMap *map, PhysPageEntry *lp,
171 hwaddr *index, hwaddr *nb, uint16_t leaf,
Avi Kivity29990972012-02-13 20:21:20 +0200172 int level)
Avi Kivityf7bf5462012-02-13 20:12:05 +0200173{
174 PhysPageEntry *p;
175 int i;
Paolo Bonzini03f49952013-11-07 17:14:36 +0100176 hwaddr step = (hwaddr)1 << (level * P_L2_BITS);
Avi Kivityf7bf5462012-02-13 20:12:05 +0200177
Michael S. Tsirkin9736e552013-11-11 14:42:43 +0200178 if (lp->skip && lp->ptr == PHYS_MAP_NODE_NIL) {
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +0200179 lp->ptr = phys_map_node_alloc(map);
180 p = map->nodes[lp->ptr];
Avi Kivityf7bf5462012-02-13 20:12:05 +0200181 if (level == 0) {
Paolo Bonzini03f49952013-11-07 17:14:36 +0100182 for (i = 0; i < P_L2_SIZE; i++) {
Michael S. Tsirkin9736e552013-11-11 14:42:43 +0200183 p[i].skip = 0;
Liu Ping Fanb41aac42013-05-29 11:09:17 +0200184 p[i].ptr = PHYS_SECTION_UNASSIGNED;
Avi Kivityf7bf5462012-02-13 20:12:05 +0200185 }
186 }
187 } else {
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +0200188 p = map->nodes[lp->ptr];
Avi Kivityf7bf5462012-02-13 20:12:05 +0200189 }
Paolo Bonzini03f49952013-11-07 17:14:36 +0100190 lp = &p[(*index >> (level * P_L2_BITS)) & (P_L2_SIZE - 1)];
Avi Kivityf7bf5462012-02-13 20:12:05 +0200191
Paolo Bonzini03f49952013-11-07 17:14:36 +0100192 while (*nb && lp < &p[P_L2_SIZE]) {
Avi Kivity07f07b32012-02-13 20:45:32 +0200193 if ((*index & (step - 1)) == 0 && *nb >= step) {
Michael S. Tsirkin9736e552013-11-11 14:42:43 +0200194 lp->skip = 0;
Avi Kivityc19e8802012-02-13 20:25:31 +0200195 lp->ptr = leaf;
Avi Kivity07f07b32012-02-13 20:45:32 +0200196 *index += step;
197 *nb -= step;
Avi Kivity29990972012-02-13 20:21:20 +0200198 } else {
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +0200199 phys_page_set_level(map, lp, index, nb, leaf, level - 1);
Avi Kivity29990972012-02-13 20:21:20 +0200200 }
201 ++lp;
Avi Kivityf7bf5462012-02-13 20:12:05 +0200202 }
203}
204
Avi Kivityac1970f2012-10-03 16:22:53 +0200205static void phys_page_set(AddressSpaceDispatch *d,
Avi Kivitya8170e52012-10-23 12:30:10 +0200206 hwaddr index, hwaddr nb,
Avi Kivity29990972012-02-13 20:21:20 +0200207 uint16_t leaf)
bellard92e873b2004-05-21 14:52:29 +0000208{
Avi Kivity29990972012-02-13 20:21:20 +0200209 /* Wildly overreserve - it doesn't matter much. */
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +0200210 phys_map_node_reserve(&d->map, 3 * P_L2_LEVELS);
bellard92e873b2004-05-21 14:52:29 +0000211
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +0200212 phys_page_set_level(&d->map, &d->phys_map, &index, &nb, leaf, P_L2_LEVELS - 1);
bellard92e873b2004-05-21 14:52:29 +0000213}
214
Michael S. Tsirkinb35ba302013-11-11 17:52:07 +0200215/* Compact a non leaf page entry. Simply detect that the entry has a single child,
216 * and update our entry so we can skip it and go directly to the destination.
217 */
218static void phys_page_compact(PhysPageEntry *lp, Node *nodes, unsigned long *compacted)
219{
220 unsigned valid_ptr = P_L2_SIZE;
221 int valid = 0;
222 PhysPageEntry *p;
223 int i;
224
225 if (lp->ptr == PHYS_MAP_NODE_NIL) {
226 return;
227 }
228
229 p = nodes[lp->ptr];
230 for (i = 0; i < P_L2_SIZE; i++) {
231 if (p[i].ptr == PHYS_MAP_NODE_NIL) {
232 continue;
233 }
234
235 valid_ptr = i;
236 valid++;
237 if (p[i].skip) {
238 phys_page_compact(&p[i], nodes, compacted);
239 }
240 }
241
242 /* We can only compress if there's only one child. */
243 if (valid != 1) {
244 return;
245 }
246
247 assert(valid_ptr < P_L2_SIZE);
248
249 /* Don't compress if it won't fit in the # of bits we have. */
250 if (lp->skip + p[valid_ptr].skip >= (1 << 3)) {
251 return;
252 }
253
254 lp->ptr = p[valid_ptr].ptr;
255 if (!p[valid_ptr].skip) {
256 /* If our only child is a leaf, make this a leaf. */
257 /* By design, we should have made this node a leaf to begin with so we
258 * should never reach here.
259 * But since it's so simple to handle this, let's do it just in case we
260 * change this rule.
261 */
262 lp->skip = 0;
263 } else {
264 lp->skip += p[valid_ptr].skip;
265 }
266}
267
268static void phys_page_compact_all(AddressSpaceDispatch *d, int nodes_nb)
269{
270 DECLARE_BITMAP(compacted, nodes_nb);
271
272 if (d->phys_map.skip) {
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +0200273 phys_page_compact(&d->phys_map, d->map.nodes, compacted);
Michael S. Tsirkinb35ba302013-11-11 17:52:07 +0200274 }
275}
276
Michael S. Tsirkin97115a82013-11-13 20:08:19 +0200277static MemoryRegionSection *phys_page_find(PhysPageEntry lp, hwaddr addr,
Paolo Bonzini9affd6f2013-05-29 12:09:47 +0200278 Node *nodes, MemoryRegionSection *sections)
bellard92e873b2004-05-21 14:52:29 +0000279{
Avi Kivity31ab2b42012-02-13 16:44:19 +0200280 PhysPageEntry *p;
Michael S. Tsirkin97115a82013-11-13 20:08:19 +0200281 hwaddr index = addr >> TARGET_PAGE_BITS;
Avi Kivity31ab2b42012-02-13 16:44:19 +0200282 int i;
Avi Kivityf1f6e3b2011-11-20 17:52:22 +0200283
Michael S. Tsirkin9736e552013-11-11 14:42:43 +0200284 for (i = P_L2_LEVELS; lp.skip && (i -= lp.skip) >= 0;) {
Avi Kivityc19e8802012-02-13 20:25:31 +0200285 if (lp.ptr == PHYS_MAP_NODE_NIL) {
Paolo Bonzini9affd6f2013-05-29 12:09:47 +0200286 return &sections[PHYS_SECTION_UNASSIGNED];
Avi Kivity31ab2b42012-02-13 16:44:19 +0200287 }
Paolo Bonzini9affd6f2013-05-29 12:09:47 +0200288 p = nodes[lp.ptr];
Paolo Bonzini03f49952013-11-07 17:14:36 +0100289 lp = p[(index >> (i * P_L2_BITS)) & (P_L2_SIZE - 1)];
Avi Kivityf1f6e3b2011-11-20 17:52:22 +0200290 }
Michael S. Tsirkinb35ba302013-11-11 17:52:07 +0200291
292 if (sections[lp.ptr].size.hi ||
293 range_covers_byte(sections[lp.ptr].offset_within_address_space,
294 sections[lp.ptr].size.lo, addr)) {
295 return &sections[lp.ptr];
296 } else {
297 return &sections[PHYS_SECTION_UNASSIGNED];
298 }
Avi Kivityf3705d52012-03-08 16:16:34 +0200299}
300
Blue Swirle5548612012-04-21 13:08:33 +0000301bool memory_region_is_unassigned(MemoryRegion *mr)
302{
Paolo Bonzini2a8e7492013-05-24 14:34:08 +0200303 return mr != &io_mem_rom && mr != &io_mem_notdirty && !mr->rom_device
Blue Swirle5548612012-04-21 13:08:33 +0000304 && mr != &io_mem_watch;
305}
Paolo Bonzini149f54b2013-05-24 12:59:37 +0200306
Paolo Bonzinic7086b42013-06-02 15:27:39 +0200307static MemoryRegionSection *address_space_lookup_region(AddressSpaceDispatch *d,
Jan Kiszka90260c62013-05-26 21:46:51 +0200308 hwaddr addr,
309 bool resolve_subpage)
Jan Kiszka9f029602013-05-06 16:48:02 +0200310{
Jan Kiszka90260c62013-05-26 21:46:51 +0200311 MemoryRegionSection *section;
312 subpage_t *subpage;
313
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +0200314 section = phys_page_find(d->phys_map, addr, d->map.nodes, d->map.sections);
Jan Kiszka90260c62013-05-26 21:46:51 +0200315 if (resolve_subpage && section->mr->subpage) {
316 subpage = container_of(section->mr, subpage_t, iomem);
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +0200317 section = &d->map.sections[subpage->sub_section[SUBPAGE_IDX(addr)]];
Jan Kiszka90260c62013-05-26 21:46:51 +0200318 }
319 return section;
Jan Kiszka9f029602013-05-06 16:48:02 +0200320}
321
Jan Kiszka90260c62013-05-26 21:46:51 +0200322static MemoryRegionSection *
Paolo Bonzinic7086b42013-06-02 15:27:39 +0200323address_space_translate_internal(AddressSpaceDispatch *d, hwaddr addr, hwaddr *xlat,
Jan Kiszka90260c62013-05-26 21:46:51 +0200324 hwaddr *plen, bool resolve_subpage)
Paolo Bonzini149f54b2013-05-24 12:59:37 +0200325{
326 MemoryRegionSection *section;
327 Int128 diff;
328
Paolo Bonzinic7086b42013-06-02 15:27:39 +0200329 section = address_space_lookup_region(d, addr, resolve_subpage);
Paolo Bonzini149f54b2013-05-24 12:59:37 +0200330 /* Compute offset within MemoryRegionSection */
331 addr -= section->offset_within_address_space;
332
333 /* Compute offset within MemoryRegion */
334 *xlat = addr + section->offset_within_region;
335
336 diff = int128_sub(section->mr->size, int128_make64(addr));
Peter Maydell3752a032013-06-20 15:18:04 +0100337 *plen = int128_get64(int128_min(diff, int128_make64(*plen)));
Paolo Bonzini149f54b2013-05-24 12:59:37 +0200338 return section;
339}
Jan Kiszka90260c62013-05-26 21:46:51 +0200340
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +0200341MemoryRegion *address_space_translate(AddressSpace *as, hwaddr addr,
342 hwaddr *xlat, hwaddr *plen,
343 bool is_write)
Jan Kiszka90260c62013-05-26 21:46:51 +0200344{
Avi Kivity30951152012-10-30 13:47:46 +0200345 IOMMUTLBEntry iotlb;
346 MemoryRegionSection *section;
347 MemoryRegion *mr;
348 hwaddr len = *plen;
349
350 for (;;) {
Paolo Bonzinic7086b42013-06-02 15:27:39 +0200351 section = address_space_translate_internal(as->dispatch, addr, &addr, plen, true);
Avi Kivity30951152012-10-30 13:47:46 +0200352 mr = section->mr;
353
354 if (!mr->iommu_ops) {
355 break;
356 }
357
358 iotlb = mr->iommu_ops->translate(mr, addr);
359 addr = ((iotlb.translated_addr & ~iotlb.addr_mask)
360 | (addr & iotlb.addr_mask));
361 len = MIN(len, (addr | iotlb.addr_mask) - addr + 1);
362 if (!(iotlb.perm & (1 << is_write))) {
363 mr = &io_mem_unassigned;
364 break;
365 }
366
367 as = iotlb.target_as;
368 }
369
370 *plen = len;
371 *xlat = addr;
372 return mr;
Jan Kiszka90260c62013-05-26 21:46:51 +0200373}
374
375MemoryRegionSection *
376address_space_translate_for_iotlb(AddressSpace *as, hwaddr addr, hwaddr *xlat,
377 hwaddr *plen)
378{
Avi Kivity30951152012-10-30 13:47:46 +0200379 MemoryRegionSection *section;
Paolo Bonzinic7086b42013-06-02 15:27:39 +0200380 section = address_space_translate_internal(as->dispatch, addr, xlat, plen, false);
Avi Kivity30951152012-10-30 13:47:46 +0200381
382 assert(!section->mr->iommu_ops);
383 return section;
Jan Kiszka90260c62013-05-26 21:46:51 +0200384}
bellard9fa3e852004-01-04 18:06:42 +0000385#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000386
Jan Kiszkad5ab9712011-08-02 16:10:21 +0200387void cpu_exec_init_all(void)
388{
389#if !defined(CONFIG_USER_ONLY)
Umesh Deshpandeb2a86582011-08-17 00:01:33 -0700390 qemu_mutex_init(&ram_list.mutex);
Jan Kiszkad5ab9712011-08-02 16:10:21 +0200391 memory_map_init();
392 io_mem_init();
393#endif
394}
395
Andreas Färberb170fce2013-01-20 20:23:22 +0100396#if !defined(CONFIG_USER_ONLY)
pbrook9656f322008-07-01 20:01:19 +0000397
Juan Quintelae59fb372009-09-29 22:48:21 +0200398static int cpu_common_post_load(void *opaque, int version_id)
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200399{
Andreas Färber259186a2013-01-17 18:51:17 +0100400 CPUState *cpu = opaque;
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200401
aurel323098dba2009-03-07 21:28:24 +0000402 /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
403 version_id is increased. */
Andreas Färber259186a2013-01-17 18:51:17 +0100404 cpu->interrupt_request &= ~0x01;
405 tlb_flush(cpu->env_ptr, 1);
pbrook9656f322008-07-01 20:01:19 +0000406
407 return 0;
408}
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200409
Andreas Färber1a1562f2013-06-17 04:09:11 +0200410const VMStateDescription vmstate_cpu_common = {
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200411 .name = "cpu_common",
412 .version_id = 1,
413 .minimum_version_id = 1,
414 .minimum_version_id_old = 1,
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200415 .post_load = cpu_common_post_load,
416 .fields = (VMStateField []) {
Andreas Färber259186a2013-01-17 18:51:17 +0100417 VMSTATE_UINT32(halted, CPUState),
418 VMSTATE_UINT32(interrupt_request, CPUState),
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200419 VMSTATE_END_OF_LIST()
420 }
421};
Andreas Färber1a1562f2013-06-17 04:09:11 +0200422
pbrook9656f322008-07-01 20:01:19 +0000423#endif
424
Andreas Färber38d8f5c2012-12-17 19:47:15 +0100425CPUState *qemu_get_cpu(int index)
Glauber Costa950f1472009-06-09 12:15:18 -0400426{
Andreas Färberbdc44642013-06-24 23:50:24 +0200427 CPUState *cpu;
Glauber Costa950f1472009-06-09 12:15:18 -0400428
Andreas Färberbdc44642013-06-24 23:50:24 +0200429 CPU_FOREACH(cpu) {
Andreas Färber55e5c282012-12-17 06:18:02 +0100430 if (cpu->cpu_index == index) {
Andreas Färberbdc44642013-06-24 23:50:24 +0200431 return cpu;
Andreas Färber55e5c282012-12-17 06:18:02 +0100432 }
Glauber Costa950f1472009-06-09 12:15:18 -0400433 }
434
Andreas Färberbdc44642013-06-24 23:50:24 +0200435 return NULL;
Glauber Costa950f1472009-06-09 12:15:18 -0400436}
437
Andreas Färber9349b4f2012-03-14 01:38:32 +0100438void cpu_exec_init(CPUArchState *env)
bellardfd6ce8f2003-05-14 19:00:11 +0000439{
Andreas Färber9f09e182012-05-03 06:59:07 +0200440 CPUState *cpu = ENV_GET_CPU(env);
Andreas Färberb170fce2013-01-20 20:23:22 +0100441 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färberbdc44642013-06-24 23:50:24 +0200442 CPUState *some_cpu;
bellard6a00d602005-11-21 23:25:50 +0000443 int cpu_index;
444
pbrookc2764712009-03-07 15:24:59 +0000445#if defined(CONFIG_USER_ONLY)
446 cpu_list_lock();
447#endif
bellard6a00d602005-11-21 23:25:50 +0000448 cpu_index = 0;
Andreas Färberbdc44642013-06-24 23:50:24 +0200449 CPU_FOREACH(some_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000450 cpu_index++;
451 }
Andreas Färber55e5c282012-12-17 06:18:02 +0100452 cpu->cpu_index = cpu_index;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +0100453 cpu->numa_node = 0;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000454 QTAILQ_INIT(&env->breakpoints);
455 QTAILQ_INIT(&env->watchpoints);
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100456#ifndef CONFIG_USER_ONLY
Andreas Färber9f09e182012-05-03 06:59:07 +0200457 cpu->thread_id = qemu_get_thread_id();
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100458#endif
Andreas Färberbdc44642013-06-24 23:50:24 +0200459 QTAILQ_INSERT_TAIL(&cpus, cpu, node);
pbrookc2764712009-03-07 15:24:59 +0000460#if defined(CONFIG_USER_ONLY)
461 cpu_list_unlock();
462#endif
Andreas Färbere0d47942013-07-29 04:07:50 +0200463 if (qdev_get_vmsd(DEVICE(cpu)) == NULL) {
464 vmstate_register(NULL, cpu_index, &vmstate_cpu_common, cpu);
465 }
pbrookb3c77242008-06-30 16:31:04 +0000466#if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
Alex Williamson0be71e32010-06-25 11:09:07 -0600467 register_savevm(NULL, "cpu", cpu_index, CPU_SAVE_VERSION,
pbrookb3c77242008-06-30 16:31:04 +0000468 cpu_save, cpu_load, env);
Andreas Färberb170fce2013-01-20 20:23:22 +0100469 assert(cc->vmsd == NULL);
Andreas Färbere0d47942013-07-29 04:07:50 +0200470 assert(qdev_get_vmsd(DEVICE(cpu)) == NULL);
pbrookb3c77242008-06-30 16:31:04 +0000471#endif
Andreas Färberb170fce2013-01-20 20:23:22 +0100472 if (cc->vmsd != NULL) {
473 vmstate_register(NULL, cpu_index, cc->vmsd, cpu);
474 }
bellardfd6ce8f2003-05-14 19:00:11 +0000475}
476
bellard1fddef42005-04-17 19:16:13 +0000477#if defined(TARGET_HAS_ICE)
Paul Brook94df27f2010-02-28 23:47:45 +0000478#if defined(CONFIG_USER_ONLY)
Andreas Färber00b941e2013-06-29 18:55:54 +0200479static void breakpoint_invalidate(CPUState *cpu, target_ulong pc)
Paul Brook94df27f2010-02-28 23:47:45 +0000480{
481 tb_invalidate_phys_page_range(pc, pc + 1, 0);
482}
483#else
Andreas Färber00b941e2013-06-29 18:55:54 +0200484static void breakpoint_invalidate(CPUState *cpu, target_ulong pc)
Max Filippov1e7855a2012-04-10 02:48:17 +0400485{
Max Filippove8262a12013-09-27 22:29:17 +0400486 hwaddr phys = cpu_get_phys_page_debug(cpu, pc);
487 if (phys != -1) {
488 tb_invalidate_phys_addr(phys | (pc & ~TARGET_PAGE_MASK));
489 }
Max Filippov1e7855a2012-04-10 02:48:17 +0400490}
bellardc27004e2005-01-03 23:35:10 +0000491#endif
Paul Brook94df27f2010-02-28 23:47:45 +0000492#endif /* TARGET_HAS_ICE */
bellardd720b932004-04-25 17:57:43 +0000493
Paul Brookc527ee82010-03-01 03:31:14 +0000494#if defined(CONFIG_USER_ONLY)
Andreas Färber9349b4f2012-03-14 01:38:32 +0100495void cpu_watchpoint_remove_all(CPUArchState *env, int mask)
Paul Brookc527ee82010-03-01 03:31:14 +0000496
497{
498}
499
Andreas Färber9349b4f2012-03-14 01:38:32 +0100500int cpu_watchpoint_insert(CPUArchState *env, target_ulong addr, target_ulong len,
Paul Brookc527ee82010-03-01 03:31:14 +0000501 int flags, CPUWatchpoint **watchpoint)
502{
503 return -ENOSYS;
504}
505#else
pbrook6658ffb2007-03-16 23:58:11 +0000506/* Add a watchpoint. */
Andreas Färber9349b4f2012-03-14 01:38:32 +0100507int cpu_watchpoint_insert(CPUArchState *env, target_ulong addr, target_ulong len,
aliguoria1d1bb32008-11-18 20:07:32 +0000508 int flags, CPUWatchpoint **watchpoint)
pbrook6658ffb2007-03-16 23:58:11 +0000509{
aliguorib4051332008-11-18 20:14:20 +0000510 target_ulong len_mask = ~(len - 1);
aliguoric0ce9982008-11-25 22:13:57 +0000511 CPUWatchpoint *wp;
pbrook6658ffb2007-03-16 23:58:11 +0000512
aliguorib4051332008-11-18 20:14:20 +0000513 /* sanity checks: allow power-of-2 lengths, deny unaligned watchpoints */
Max Filippov0dc23822012-01-29 03:15:23 +0400514 if ((len & (len - 1)) || (addr & ~len_mask) ||
515 len == 0 || len > TARGET_PAGE_SIZE) {
aliguorib4051332008-11-18 20:14:20 +0000516 fprintf(stderr, "qemu: tried to set invalid watchpoint at "
517 TARGET_FMT_lx ", len=" TARGET_FMT_lu "\n", addr, len);
518 return -EINVAL;
519 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500520 wp = g_malloc(sizeof(*wp));
pbrook6658ffb2007-03-16 23:58:11 +0000521
aliguoria1d1bb32008-11-18 20:07:32 +0000522 wp->vaddr = addr;
aliguorib4051332008-11-18 20:14:20 +0000523 wp->len_mask = len_mask;
aliguoria1d1bb32008-11-18 20:07:32 +0000524 wp->flags = flags;
525
aliguori2dc9f412008-11-18 20:56:59 +0000526 /* keep all GDB-injected watchpoints in front */
aliguoric0ce9982008-11-25 22:13:57 +0000527 if (flags & BP_GDB)
Blue Swirl72cf2d42009-09-12 07:36:22 +0000528 QTAILQ_INSERT_HEAD(&env->watchpoints, wp, entry);
aliguoric0ce9982008-11-25 22:13:57 +0000529 else
Blue Swirl72cf2d42009-09-12 07:36:22 +0000530 QTAILQ_INSERT_TAIL(&env->watchpoints, wp, entry);
aliguoria1d1bb32008-11-18 20:07:32 +0000531
pbrook6658ffb2007-03-16 23:58:11 +0000532 tlb_flush_page(env, addr);
aliguoria1d1bb32008-11-18 20:07:32 +0000533
534 if (watchpoint)
535 *watchpoint = wp;
536 return 0;
pbrook6658ffb2007-03-16 23:58:11 +0000537}
538
aliguoria1d1bb32008-11-18 20:07:32 +0000539/* Remove a specific watchpoint. */
Andreas Färber9349b4f2012-03-14 01:38:32 +0100540int cpu_watchpoint_remove(CPUArchState *env, target_ulong addr, target_ulong len,
aliguoria1d1bb32008-11-18 20:07:32 +0000541 int flags)
pbrook6658ffb2007-03-16 23:58:11 +0000542{
aliguorib4051332008-11-18 20:14:20 +0000543 target_ulong len_mask = ~(len - 1);
aliguoria1d1bb32008-11-18 20:07:32 +0000544 CPUWatchpoint *wp;
pbrook6658ffb2007-03-16 23:58:11 +0000545
Blue Swirl72cf2d42009-09-12 07:36:22 +0000546 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
aliguorib4051332008-11-18 20:14:20 +0000547 if (addr == wp->vaddr && len_mask == wp->len_mask
aliguori6e140f22008-11-18 20:37:55 +0000548 && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) {
aliguoria1d1bb32008-11-18 20:07:32 +0000549 cpu_watchpoint_remove_by_ref(env, wp);
pbrook6658ffb2007-03-16 23:58:11 +0000550 return 0;
551 }
552 }
aliguoria1d1bb32008-11-18 20:07:32 +0000553 return -ENOENT;
pbrook6658ffb2007-03-16 23:58:11 +0000554}
555
aliguoria1d1bb32008-11-18 20:07:32 +0000556/* Remove a specific watchpoint by reference. */
Andreas Färber9349b4f2012-03-14 01:38:32 +0100557void cpu_watchpoint_remove_by_ref(CPUArchState *env, CPUWatchpoint *watchpoint)
aliguoria1d1bb32008-11-18 20:07:32 +0000558{
Blue Swirl72cf2d42009-09-12 07:36:22 +0000559 QTAILQ_REMOVE(&env->watchpoints, watchpoint, entry);
edgar_igl7d03f822008-05-17 18:58:29 +0000560
aliguoria1d1bb32008-11-18 20:07:32 +0000561 tlb_flush_page(env, watchpoint->vaddr);
562
Anthony Liguori7267c092011-08-20 22:09:37 -0500563 g_free(watchpoint);
edgar_igl7d03f822008-05-17 18:58:29 +0000564}
565
aliguoria1d1bb32008-11-18 20:07:32 +0000566/* Remove all matching watchpoints. */
Andreas Färber9349b4f2012-03-14 01:38:32 +0100567void cpu_watchpoint_remove_all(CPUArchState *env, int mask)
aliguoria1d1bb32008-11-18 20:07:32 +0000568{
aliguoric0ce9982008-11-25 22:13:57 +0000569 CPUWatchpoint *wp, *next;
aliguoria1d1bb32008-11-18 20:07:32 +0000570
Blue Swirl72cf2d42009-09-12 07:36:22 +0000571 QTAILQ_FOREACH_SAFE(wp, &env->watchpoints, entry, next) {
aliguoria1d1bb32008-11-18 20:07:32 +0000572 if (wp->flags & mask)
573 cpu_watchpoint_remove_by_ref(env, wp);
aliguoric0ce9982008-11-25 22:13:57 +0000574 }
aliguoria1d1bb32008-11-18 20:07:32 +0000575}
Paul Brookc527ee82010-03-01 03:31:14 +0000576#endif
aliguoria1d1bb32008-11-18 20:07:32 +0000577
578/* Add a breakpoint. */
Andreas Färber9349b4f2012-03-14 01:38:32 +0100579int cpu_breakpoint_insert(CPUArchState *env, target_ulong pc, int flags,
aliguoria1d1bb32008-11-18 20:07:32 +0000580 CPUBreakpoint **breakpoint)
bellard4c3a88a2003-07-26 12:06:08 +0000581{
bellard1fddef42005-04-17 19:16:13 +0000582#if defined(TARGET_HAS_ICE)
aliguoric0ce9982008-11-25 22:13:57 +0000583 CPUBreakpoint *bp;
ths3b46e622007-09-17 08:09:54 +0000584
Anthony Liguori7267c092011-08-20 22:09:37 -0500585 bp = g_malloc(sizeof(*bp));
aliguoria1d1bb32008-11-18 20:07:32 +0000586
587 bp->pc = pc;
588 bp->flags = flags;
589
aliguori2dc9f412008-11-18 20:56:59 +0000590 /* keep all GDB-injected breakpoints in front */
Andreas Färber00b941e2013-06-29 18:55:54 +0200591 if (flags & BP_GDB) {
Blue Swirl72cf2d42009-09-12 07:36:22 +0000592 QTAILQ_INSERT_HEAD(&env->breakpoints, bp, entry);
Andreas Färber00b941e2013-06-29 18:55:54 +0200593 } else {
Blue Swirl72cf2d42009-09-12 07:36:22 +0000594 QTAILQ_INSERT_TAIL(&env->breakpoints, bp, entry);
Andreas Färber00b941e2013-06-29 18:55:54 +0200595 }
aliguoria1d1bb32008-11-18 20:07:32 +0000596
Andreas Färber00b941e2013-06-29 18:55:54 +0200597 breakpoint_invalidate(ENV_GET_CPU(env), pc);
aliguoria1d1bb32008-11-18 20:07:32 +0000598
Andreas Färber00b941e2013-06-29 18:55:54 +0200599 if (breakpoint) {
aliguoria1d1bb32008-11-18 20:07:32 +0000600 *breakpoint = bp;
Andreas Färber00b941e2013-06-29 18:55:54 +0200601 }
aliguoria1d1bb32008-11-18 20:07:32 +0000602 return 0;
603#else
604 return -ENOSYS;
605#endif
606}
607
608/* Remove a specific breakpoint. */
Andreas Färber9349b4f2012-03-14 01:38:32 +0100609int cpu_breakpoint_remove(CPUArchState *env, target_ulong pc, int flags)
aliguoria1d1bb32008-11-18 20:07:32 +0000610{
611#if defined(TARGET_HAS_ICE)
612 CPUBreakpoint *bp;
613
Blue Swirl72cf2d42009-09-12 07:36:22 +0000614 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
aliguoria1d1bb32008-11-18 20:07:32 +0000615 if (bp->pc == pc && bp->flags == flags) {
616 cpu_breakpoint_remove_by_ref(env, bp);
bellard4c3a88a2003-07-26 12:06:08 +0000617 return 0;
aliguoria1d1bb32008-11-18 20:07:32 +0000618 }
bellard4c3a88a2003-07-26 12:06:08 +0000619 }
aliguoria1d1bb32008-11-18 20:07:32 +0000620 return -ENOENT;
bellard4c3a88a2003-07-26 12:06:08 +0000621#else
aliguoria1d1bb32008-11-18 20:07:32 +0000622 return -ENOSYS;
bellard4c3a88a2003-07-26 12:06:08 +0000623#endif
624}
625
aliguoria1d1bb32008-11-18 20:07:32 +0000626/* Remove a specific breakpoint by reference. */
Andreas Färber9349b4f2012-03-14 01:38:32 +0100627void cpu_breakpoint_remove_by_ref(CPUArchState *env, CPUBreakpoint *breakpoint)
bellard4c3a88a2003-07-26 12:06:08 +0000628{
bellard1fddef42005-04-17 19:16:13 +0000629#if defined(TARGET_HAS_ICE)
Blue Swirl72cf2d42009-09-12 07:36:22 +0000630 QTAILQ_REMOVE(&env->breakpoints, breakpoint, entry);
bellardd720b932004-04-25 17:57:43 +0000631
Andreas Färber00b941e2013-06-29 18:55:54 +0200632 breakpoint_invalidate(ENV_GET_CPU(env), breakpoint->pc);
aliguoria1d1bb32008-11-18 20:07:32 +0000633
Anthony Liguori7267c092011-08-20 22:09:37 -0500634 g_free(breakpoint);
aliguoria1d1bb32008-11-18 20:07:32 +0000635#endif
636}
637
638/* Remove all matching breakpoints. */
Andreas Färber9349b4f2012-03-14 01:38:32 +0100639void cpu_breakpoint_remove_all(CPUArchState *env, int mask)
aliguoria1d1bb32008-11-18 20:07:32 +0000640{
641#if defined(TARGET_HAS_ICE)
aliguoric0ce9982008-11-25 22:13:57 +0000642 CPUBreakpoint *bp, *next;
aliguoria1d1bb32008-11-18 20:07:32 +0000643
Blue Swirl72cf2d42009-09-12 07:36:22 +0000644 QTAILQ_FOREACH_SAFE(bp, &env->breakpoints, entry, next) {
aliguoria1d1bb32008-11-18 20:07:32 +0000645 if (bp->flags & mask)
646 cpu_breakpoint_remove_by_ref(env, bp);
aliguoric0ce9982008-11-25 22:13:57 +0000647 }
bellard4c3a88a2003-07-26 12:06:08 +0000648#endif
649}
650
bellardc33a3462003-07-29 20:50:33 +0000651/* enable or disable single step mode. EXCP_DEBUG is returned by the
652 CPU loop after each instruction */
Andreas Färber3825b282013-06-24 18:41:06 +0200653void cpu_single_step(CPUState *cpu, int enabled)
bellardc33a3462003-07-29 20:50:33 +0000654{
bellard1fddef42005-04-17 19:16:13 +0000655#if defined(TARGET_HAS_ICE)
Andreas Färbered2803d2013-06-21 20:20:45 +0200656 if (cpu->singlestep_enabled != enabled) {
657 cpu->singlestep_enabled = enabled;
658 if (kvm_enabled()) {
Stefan Weil38e478e2013-07-25 20:50:21 +0200659 kvm_update_guest_debug(cpu, 0);
Andreas Färbered2803d2013-06-21 20:20:45 +0200660 } else {
Stuart Bradyccbb4d42009-05-03 12:15:06 +0100661 /* must flush all the translated code to avoid inconsistencies */
aliguorie22a25c2009-03-12 20:12:48 +0000662 /* XXX: only flush what is necessary */
Stefan Weil38e478e2013-07-25 20:50:21 +0200663 CPUArchState *env = cpu->env_ptr;
aliguorie22a25c2009-03-12 20:12:48 +0000664 tb_flush(env);
665 }
bellardc33a3462003-07-29 20:50:33 +0000666 }
667#endif
668}
669
Andreas Färber9349b4f2012-03-14 01:38:32 +0100670void cpu_abort(CPUArchState *env, const char *fmt, ...)
bellard75012672003-06-21 13:11:07 +0000671{
Andreas Färber878096e2013-05-27 01:33:50 +0200672 CPUState *cpu = ENV_GET_CPU(env);
bellard75012672003-06-21 13:11:07 +0000673 va_list ap;
pbrook493ae1f2007-11-23 16:53:59 +0000674 va_list ap2;
bellard75012672003-06-21 13:11:07 +0000675
676 va_start(ap, fmt);
pbrook493ae1f2007-11-23 16:53:59 +0000677 va_copy(ap2, ap);
bellard75012672003-06-21 13:11:07 +0000678 fprintf(stderr, "qemu: fatal: ");
679 vfprintf(stderr, fmt, ap);
680 fprintf(stderr, "\n");
Andreas Färber878096e2013-05-27 01:33:50 +0200681 cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_FPU | CPU_DUMP_CCOP);
aliguori93fcfe32009-01-15 22:34:14 +0000682 if (qemu_log_enabled()) {
683 qemu_log("qemu: fatal: ");
684 qemu_log_vprintf(fmt, ap2);
685 qemu_log("\n");
Andreas Färbera0762852013-06-16 07:28:50 +0200686 log_cpu_state(cpu, CPU_DUMP_FPU | CPU_DUMP_CCOP);
aliguori31b1a7b2009-01-15 22:35:09 +0000687 qemu_log_flush();
aliguori93fcfe32009-01-15 22:34:14 +0000688 qemu_log_close();
balrog924edca2007-06-10 14:07:13 +0000689 }
pbrook493ae1f2007-11-23 16:53:59 +0000690 va_end(ap2);
j_mayerf9373292007-09-29 12:18:20 +0000691 va_end(ap);
Riku Voipiofd052bf2010-01-25 14:30:49 +0200692#if defined(CONFIG_USER_ONLY)
693 {
694 struct sigaction act;
695 sigfillset(&act.sa_mask);
696 act.sa_handler = SIG_DFL;
697 sigaction(SIGABRT, &act, NULL);
698 }
699#endif
bellard75012672003-06-21 13:11:07 +0000700 abort();
701}
702
bellard01243112004-01-04 15:48:17 +0000703#if !defined(CONFIG_USER_ONLY)
Paolo Bonzini041603f2013-09-09 17:49:45 +0200704static RAMBlock *qemu_get_ram_block(ram_addr_t addr)
705{
706 RAMBlock *block;
707
708 /* The list is protected by the iothread lock here. */
709 block = ram_list.mru_block;
710 if (block && addr - block->offset < block->length) {
711 goto found;
712 }
713 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
714 if (addr - block->offset < block->length) {
715 goto found;
716 }
717 }
718
719 fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
720 abort();
721
722found:
723 ram_list.mru_block = block;
724 return block;
725}
726
Juan Quintelad24981d2012-05-22 00:42:40 +0200727static void tlb_reset_dirty_range_all(ram_addr_t start, ram_addr_t end,
728 uintptr_t length)
bellard1ccde1c2004-02-06 19:46:14 +0000729{
Paolo Bonzini041603f2013-09-09 17:49:45 +0200730 RAMBlock *block;
731 ram_addr_t start1;
bellardf23db162005-08-21 19:12:28 +0000732
Paolo Bonzini041603f2013-09-09 17:49:45 +0200733 block = qemu_get_ram_block(start);
734 assert(block == qemu_get_ram_block(end - 1));
735 start1 = (uintptr_t)block->host + (start - block->offset);
Blue Swirle5548612012-04-21 13:08:33 +0000736 cpu_tlb_reset_dirty_all(start1, length);
Juan Quintelad24981d2012-05-22 00:42:40 +0200737}
738
739/* Note: start and end must be within the same ram block. */
740void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
741 int dirty_flags)
742{
743 uintptr_t length;
744
745 start &= TARGET_PAGE_MASK;
746 end = TARGET_PAGE_ALIGN(end);
747
748 length = end - start;
749 if (length == 0)
750 return;
751 cpu_physical_memory_mask_dirty_range(start, length, dirty_flags);
752
753 if (tcg_enabled()) {
754 tlb_reset_dirty_range_all(start, end, length);
755 }
bellard1ccde1c2004-02-06 19:46:14 +0000756}
757
Blue Swirl8b9c99d2012-10-28 11:04:51 +0000758static int cpu_physical_memory_set_dirty_tracking(int enable)
aliguori74576192008-10-06 14:02:03 +0000759{
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +0200760 int ret = 0;
aliguori74576192008-10-06 14:02:03 +0000761 in_migration = enable;
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +0200762 return ret;
aliguori74576192008-10-06 14:02:03 +0000763}
764
Avi Kivitya8170e52012-10-23 12:30:10 +0200765hwaddr memory_region_section_get_iotlb(CPUArchState *env,
Paolo Bonzini149f54b2013-05-24 12:59:37 +0200766 MemoryRegionSection *section,
767 target_ulong vaddr,
768 hwaddr paddr, hwaddr xlat,
769 int prot,
770 target_ulong *address)
Blue Swirle5548612012-04-21 13:08:33 +0000771{
Avi Kivitya8170e52012-10-23 12:30:10 +0200772 hwaddr iotlb;
Blue Swirle5548612012-04-21 13:08:33 +0000773 CPUWatchpoint *wp;
774
Blue Swirlcc5bea62012-04-14 14:56:48 +0000775 if (memory_region_is_ram(section->mr)) {
Blue Swirle5548612012-04-21 13:08:33 +0000776 /* Normal RAM. */
777 iotlb = (memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK)
Paolo Bonzini149f54b2013-05-24 12:59:37 +0200778 + xlat;
Blue Swirle5548612012-04-21 13:08:33 +0000779 if (!section->readonly) {
Liu Ping Fanb41aac42013-05-29 11:09:17 +0200780 iotlb |= PHYS_SECTION_NOTDIRTY;
Blue Swirle5548612012-04-21 13:08:33 +0000781 } else {
Liu Ping Fanb41aac42013-05-29 11:09:17 +0200782 iotlb |= PHYS_SECTION_ROM;
Blue Swirle5548612012-04-21 13:08:33 +0000783 }
784 } else {
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +0200785 iotlb = section - address_space_memory.dispatch->map.sections;
Paolo Bonzini149f54b2013-05-24 12:59:37 +0200786 iotlb += xlat;
Blue Swirle5548612012-04-21 13:08:33 +0000787 }
788
789 /* Make accesses to pages with watchpoints go via the
790 watchpoint trap routines. */
791 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
792 if (vaddr == (wp->vaddr & TARGET_PAGE_MASK)) {
793 /* Avoid trapping reads of pages with a write breakpoint. */
794 if ((prot & PAGE_WRITE) || (wp->flags & BP_MEM_READ)) {
Liu Ping Fanb41aac42013-05-29 11:09:17 +0200795 iotlb = PHYS_SECTION_WATCH + paddr;
Blue Swirle5548612012-04-21 13:08:33 +0000796 *address |= TLB_MMIO;
797 break;
798 }
799 }
800 }
801
802 return iotlb;
803}
bellard9fa3e852004-01-04 18:06:42 +0000804#endif /* defined(CONFIG_USER_ONLY) */
805
pbrooke2eef172008-06-08 01:09:01 +0000806#if !defined(CONFIG_USER_ONLY)
pbrook8da3ff12008-12-01 18:59:50 +0000807
Anthony Liguoric227f092009-10-01 16:12:16 -0500808static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
Avi Kivity5312bd82012-02-12 18:32:55 +0200809 uint16_t section);
Jan Kiszkaacc9d802013-05-26 21:55:37 +0200810static subpage_t *subpage_init(AddressSpace *as, hwaddr base);
Avi Kivity54688b12012-02-09 17:34:32 +0200811
Stefan Weil575ddeb2013-09-29 20:56:45 +0200812static void *(*phys_mem_alloc)(size_t size) = qemu_anon_ram_alloc;
Markus Armbruster91138032013-07-31 15:11:08 +0200813
814/*
815 * Set a custom physical guest memory alloator.
816 * Accelerators with unusual needs may need this. Hopefully, we can
817 * get rid of it eventually.
818 */
Stefan Weil575ddeb2013-09-29 20:56:45 +0200819void phys_mem_set_alloc(void *(*alloc)(size_t))
Markus Armbruster91138032013-07-31 15:11:08 +0200820{
821 phys_mem_alloc = alloc;
822}
823
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +0200824static uint16_t phys_section_add(PhysPageMap *map,
825 MemoryRegionSection *section)
Avi Kivity5312bd82012-02-12 18:32:55 +0200826{
Paolo Bonzini68f3f652013-05-07 11:30:23 +0200827 /* The physical section number is ORed with a page-aligned
828 * pointer to produce the iotlb entries. Thus it should
829 * never overflow into the page-aligned value.
830 */
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +0200831 assert(map->sections_nb < TARGET_PAGE_SIZE);
Paolo Bonzini68f3f652013-05-07 11:30:23 +0200832
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +0200833 if (map->sections_nb == map->sections_nb_alloc) {
834 map->sections_nb_alloc = MAX(map->sections_nb_alloc * 2, 16);
835 map->sections = g_renew(MemoryRegionSection, map->sections,
836 map->sections_nb_alloc);
Avi Kivity5312bd82012-02-12 18:32:55 +0200837 }
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +0200838 map->sections[map->sections_nb] = *section;
Paolo Bonzinidfde4e62013-05-06 10:46:11 +0200839 memory_region_ref(section->mr);
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +0200840 return map->sections_nb++;
Avi Kivity5312bd82012-02-12 18:32:55 +0200841}
842
Paolo Bonzini058bc4b2013-06-25 09:30:48 +0200843static void phys_section_destroy(MemoryRegion *mr)
844{
Paolo Bonzinidfde4e62013-05-06 10:46:11 +0200845 memory_region_unref(mr);
846
Paolo Bonzini058bc4b2013-06-25 09:30:48 +0200847 if (mr->subpage) {
848 subpage_t *subpage = container_of(mr, subpage_t, iomem);
849 memory_region_destroy(&subpage->iomem);
850 g_free(subpage);
851 }
852}
853
Paolo Bonzini60926662013-05-29 12:30:26 +0200854static void phys_sections_free(PhysPageMap *map)
Avi Kivity5312bd82012-02-12 18:32:55 +0200855{
Paolo Bonzini9affd6f2013-05-29 12:09:47 +0200856 while (map->sections_nb > 0) {
857 MemoryRegionSection *section = &map->sections[--map->sections_nb];
Paolo Bonzini058bc4b2013-06-25 09:30:48 +0200858 phys_section_destroy(section->mr);
859 }
Paolo Bonzini9affd6f2013-05-29 12:09:47 +0200860 g_free(map->sections);
861 g_free(map->nodes);
Avi Kivity5312bd82012-02-12 18:32:55 +0200862}
863
Avi Kivityac1970f2012-10-03 16:22:53 +0200864static void register_subpage(AddressSpaceDispatch *d, MemoryRegionSection *section)
Avi Kivity0f0cb162012-02-13 17:14:32 +0200865{
866 subpage_t *subpage;
Avi Kivitya8170e52012-10-23 12:30:10 +0200867 hwaddr base = section->offset_within_address_space
Avi Kivity0f0cb162012-02-13 17:14:32 +0200868 & TARGET_PAGE_MASK;
Michael S. Tsirkin97115a82013-11-13 20:08:19 +0200869 MemoryRegionSection *existing = phys_page_find(d->phys_map, base,
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +0200870 d->map.nodes, d->map.sections);
Avi Kivity0f0cb162012-02-13 17:14:32 +0200871 MemoryRegionSection subsection = {
872 .offset_within_address_space = base,
Paolo Bonzini052e87b2013-05-27 10:08:27 +0200873 .size = int128_make64(TARGET_PAGE_SIZE),
Avi Kivity0f0cb162012-02-13 17:14:32 +0200874 };
Avi Kivitya8170e52012-10-23 12:30:10 +0200875 hwaddr start, end;
Avi Kivity0f0cb162012-02-13 17:14:32 +0200876
Avi Kivityf3705d52012-03-08 16:16:34 +0200877 assert(existing->mr->subpage || existing->mr == &io_mem_unassigned);
Avi Kivity0f0cb162012-02-13 17:14:32 +0200878
Avi Kivityf3705d52012-03-08 16:16:34 +0200879 if (!(existing->mr->subpage)) {
Jan Kiszkaacc9d802013-05-26 21:55:37 +0200880 subpage = subpage_init(d->as, base);
Avi Kivity0f0cb162012-02-13 17:14:32 +0200881 subsection.mr = &subpage->iomem;
Avi Kivityac1970f2012-10-03 16:22:53 +0200882 phys_page_set(d, base >> TARGET_PAGE_BITS, 1,
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +0200883 phys_section_add(&d->map, &subsection));
Avi Kivity0f0cb162012-02-13 17:14:32 +0200884 } else {
Avi Kivityf3705d52012-03-08 16:16:34 +0200885 subpage = container_of(existing->mr, subpage_t, iomem);
Avi Kivity0f0cb162012-02-13 17:14:32 +0200886 }
887 start = section->offset_within_address_space & ~TARGET_PAGE_MASK;
Paolo Bonzini052e87b2013-05-27 10:08:27 +0200888 end = start + int128_get64(section->size) - 1;
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +0200889 subpage_register(subpage, start, end,
890 phys_section_add(&d->map, section));
Avi Kivity0f0cb162012-02-13 17:14:32 +0200891}
892
893
Paolo Bonzini052e87b2013-05-27 10:08:27 +0200894static void register_multipage(AddressSpaceDispatch *d,
895 MemoryRegionSection *section)
bellard33417e72003-08-10 21:47:01 +0000896{
Avi Kivitya8170e52012-10-23 12:30:10 +0200897 hwaddr start_addr = section->offset_within_address_space;
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +0200898 uint16_t section_index = phys_section_add(&d->map, section);
Paolo Bonzini052e87b2013-05-27 10:08:27 +0200899 uint64_t num_pages = int128_get64(int128_rshift(section->size,
900 TARGET_PAGE_BITS));
Avi Kivitydd811242012-01-02 12:17:03 +0200901
Paolo Bonzini733d5ef2013-05-27 10:47:10 +0200902 assert(num_pages);
903 phys_page_set(d, start_addr >> TARGET_PAGE_BITS, num_pages, section_index);
bellard33417e72003-08-10 21:47:01 +0000904}
905
Avi Kivityac1970f2012-10-03 16:22:53 +0200906static void mem_add(MemoryListener *listener, MemoryRegionSection *section)
Avi Kivity0f0cb162012-02-13 17:14:32 +0200907{
Paolo Bonzini89ae3372013-06-02 10:39:07 +0200908 AddressSpace *as = container_of(listener, AddressSpace, dispatch_listener);
Paolo Bonzini00752702013-05-29 12:13:54 +0200909 AddressSpaceDispatch *d = as->next_dispatch;
Paolo Bonzini99b9cc02013-05-27 13:18:01 +0200910 MemoryRegionSection now = *section, remain = *section;
Paolo Bonzini052e87b2013-05-27 10:08:27 +0200911 Int128 page_size = int128_make64(TARGET_PAGE_SIZE);
Avi Kivity0f0cb162012-02-13 17:14:32 +0200912
Paolo Bonzini733d5ef2013-05-27 10:47:10 +0200913 if (now.offset_within_address_space & ~TARGET_PAGE_MASK) {
914 uint64_t left = TARGET_PAGE_ALIGN(now.offset_within_address_space)
915 - now.offset_within_address_space;
916
Paolo Bonzini052e87b2013-05-27 10:08:27 +0200917 now.size = int128_min(int128_make64(left), now.size);
Avi Kivityac1970f2012-10-03 16:22:53 +0200918 register_subpage(d, &now);
Paolo Bonzini733d5ef2013-05-27 10:47:10 +0200919 } else {
Paolo Bonzini052e87b2013-05-27 10:08:27 +0200920 now.size = int128_zero();
Paolo Bonzini733d5ef2013-05-27 10:47:10 +0200921 }
Paolo Bonzini052e87b2013-05-27 10:08:27 +0200922 while (int128_ne(remain.size, now.size)) {
923 remain.size = int128_sub(remain.size, now.size);
924 remain.offset_within_address_space += int128_get64(now.size);
925 remain.offset_within_region += int128_get64(now.size);
Tyler Hall69b67642012-07-25 18:45:04 -0400926 now = remain;
Paolo Bonzini052e87b2013-05-27 10:08:27 +0200927 if (int128_lt(remain.size, page_size)) {
Paolo Bonzini733d5ef2013-05-27 10:47:10 +0200928 register_subpage(d, &now);
Hu Tao88266242013-08-29 18:21:16 +0800929 } else if (remain.offset_within_address_space & ~TARGET_PAGE_MASK) {
Paolo Bonzini052e87b2013-05-27 10:08:27 +0200930 now.size = page_size;
Avi Kivityac1970f2012-10-03 16:22:53 +0200931 register_subpage(d, &now);
Tyler Hall69b67642012-07-25 18:45:04 -0400932 } else {
Paolo Bonzini052e87b2013-05-27 10:08:27 +0200933 now.size = int128_and(now.size, int128_neg(page_size));
Avi Kivityac1970f2012-10-03 16:22:53 +0200934 register_multipage(d, &now);
Tyler Hall69b67642012-07-25 18:45:04 -0400935 }
Avi Kivity0f0cb162012-02-13 17:14:32 +0200936 }
937}
938
Sheng Yang62a27442010-01-26 19:21:16 +0800939void qemu_flush_coalesced_mmio_buffer(void)
940{
941 if (kvm_enabled())
942 kvm_flush_coalesced_mmio_buffer();
943}
944
Umesh Deshpandeb2a86582011-08-17 00:01:33 -0700945void qemu_mutex_lock_ramlist(void)
946{
947 qemu_mutex_lock(&ram_list.mutex);
948}
949
950void qemu_mutex_unlock_ramlist(void)
951{
952 qemu_mutex_unlock(&ram_list.mutex);
953}
954
Markus Armbrustere1e84ba2013-07-31 15:11:10 +0200955#ifdef __linux__
Marcelo Tosattic9027602010-03-01 20:25:08 -0300956
957#include <sys/vfs.h>
958
959#define HUGETLBFS_MAGIC 0x958458f6
960
961static long gethugepagesize(const char *path)
962{
963 struct statfs fs;
964 int ret;
965
966 do {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +0900967 ret = statfs(path, &fs);
Marcelo Tosattic9027602010-03-01 20:25:08 -0300968 } while (ret != 0 && errno == EINTR);
969
970 if (ret != 0) {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +0900971 perror(path);
972 return 0;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300973 }
974
975 if (fs.f_type != HUGETLBFS_MAGIC)
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +0900976 fprintf(stderr, "Warning: path not on HugeTLBFS: %s\n", path);
Marcelo Tosattic9027602010-03-01 20:25:08 -0300977
978 return fs.f_bsize;
979}
980
Marcelo Tosattief36fa12013-10-28 18:51:46 -0200981static sigjmp_buf sigjump;
982
983static void sigbus_handler(int signal)
984{
985 siglongjmp(sigjump, 1);
986}
987
Alex Williamson04b16652010-07-02 11:13:17 -0600988static void *file_ram_alloc(RAMBlock *block,
989 ram_addr_t memory,
990 const char *path)
Marcelo Tosattic9027602010-03-01 20:25:08 -0300991{
992 char *filename;
Peter Feiner8ca761f2013-03-04 13:54:25 -0500993 char *sanitized_name;
994 char *c;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300995 void *area;
996 int fd;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300997 unsigned long hpagesize;
998
999 hpagesize = gethugepagesize(path);
1000 if (!hpagesize) {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09001001 return NULL;
Marcelo Tosattic9027602010-03-01 20:25:08 -03001002 }
1003
1004 if (memory < hpagesize) {
1005 return NULL;
1006 }
1007
1008 if (kvm_enabled() && !kvm_has_sync_mmu()) {
1009 fprintf(stderr, "host lacks kvm mmu notifiers, -mem-path unsupported\n");
1010 return NULL;
1011 }
1012
Peter Feiner8ca761f2013-03-04 13:54:25 -05001013 /* Make name safe to use with mkstemp by replacing '/' with '_'. */
1014 sanitized_name = g_strdup(block->mr->name);
1015 for (c = sanitized_name; *c != '\0'; c++) {
1016 if (*c == '/')
1017 *c = '_';
1018 }
1019
1020 filename = g_strdup_printf("%s/qemu_back_mem.%s.XXXXXX", path,
1021 sanitized_name);
1022 g_free(sanitized_name);
Marcelo Tosattic9027602010-03-01 20:25:08 -03001023
1024 fd = mkstemp(filename);
1025 if (fd < 0) {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09001026 perror("unable to create backing store for hugepages");
Stefan Weile4ada482013-01-16 18:37:23 +01001027 g_free(filename);
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09001028 return NULL;
Marcelo Tosattic9027602010-03-01 20:25:08 -03001029 }
1030 unlink(filename);
Stefan Weile4ada482013-01-16 18:37:23 +01001031 g_free(filename);
Marcelo Tosattic9027602010-03-01 20:25:08 -03001032
1033 memory = (memory+hpagesize-1) & ~(hpagesize-1);
1034
1035 /*
1036 * ftruncate is not supported by hugetlbfs in older
1037 * hosts, so don't bother bailing out on errors.
1038 * If anything goes wrong with it under other filesystems,
1039 * mmap will fail.
1040 */
1041 if (ftruncate(fd, memory))
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09001042 perror("ftruncate");
Marcelo Tosattic9027602010-03-01 20:25:08 -03001043
Marcelo Tosattic9027602010-03-01 20:25:08 -03001044 area = mmap(0, memory, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
Marcelo Tosattic9027602010-03-01 20:25:08 -03001045 if (area == MAP_FAILED) {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +09001046 perror("file_ram_alloc: can't mmap RAM pages");
1047 close(fd);
1048 return (NULL);
Marcelo Tosattic9027602010-03-01 20:25:08 -03001049 }
Marcelo Tosattief36fa12013-10-28 18:51:46 -02001050
1051 if (mem_prealloc) {
1052 int ret, i;
1053 struct sigaction act, oldact;
1054 sigset_t set, oldset;
1055
1056 memset(&act, 0, sizeof(act));
1057 act.sa_handler = &sigbus_handler;
1058 act.sa_flags = 0;
1059
1060 ret = sigaction(SIGBUS, &act, &oldact);
1061 if (ret) {
1062 perror("file_ram_alloc: failed to install signal handler");
1063 exit(1);
1064 }
1065
1066 /* unblock SIGBUS */
1067 sigemptyset(&set);
1068 sigaddset(&set, SIGBUS);
1069 pthread_sigmask(SIG_UNBLOCK, &set, &oldset);
1070
1071 if (sigsetjmp(sigjump, 1)) {
1072 fprintf(stderr, "file_ram_alloc: failed to preallocate pages\n");
1073 exit(1);
1074 }
1075
1076 /* MAP_POPULATE silently ignores failures */
1077 for (i = 0; i < (memory/hpagesize)-1; i++) {
1078 memset(area + (hpagesize*i), 0, 1);
1079 }
1080
1081 ret = sigaction(SIGBUS, &oldact, NULL);
1082 if (ret) {
1083 perror("file_ram_alloc: failed to reinstall signal handler");
1084 exit(1);
1085 }
1086
1087 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
1088 }
1089
Alex Williamson04b16652010-07-02 11:13:17 -06001090 block->fd = fd;
Marcelo Tosattic9027602010-03-01 20:25:08 -03001091 return area;
1092}
Markus Armbrustere1e84ba2013-07-31 15:11:10 +02001093#else
1094static void *file_ram_alloc(RAMBlock *block,
1095 ram_addr_t memory,
1096 const char *path)
1097{
1098 fprintf(stderr, "-mem-path not supported on this host\n");
1099 exit(1);
1100}
Marcelo Tosattic9027602010-03-01 20:25:08 -03001101#endif
1102
Alex Williamsond17b5282010-06-25 11:08:38 -06001103static ram_addr_t find_ram_offset(ram_addr_t size)
1104{
Alex Williamson04b16652010-07-02 11:13:17 -06001105 RAMBlock *block, *next_block;
Alex Williamson3e837b22011-10-31 08:54:09 -06001106 ram_addr_t offset = RAM_ADDR_MAX, mingap = RAM_ADDR_MAX;
Alex Williamson04b16652010-07-02 11:13:17 -06001107
Stefan Hajnoczi49cd9ac2013-03-11 10:20:21 +01001108 assert(size != 0); /* it would hand out same offset multiple times */
1109
Paolo Bonzinia3161032012-11-14 15:54:48 +01001110 if (QTAILQ_EMPTY(&ram_list.blocks))
Alex Williamson04b16652010-07-02 11:13:17 -06001111 return 0;
1112
Paolo Bonzinia3161032012-11-14 15:54:48 +01001113 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Anthony PERARDf15fbc42011-07-20 08:17:42 +00001114 ram_addr_t end, next = RAM_ADDR_MAX;
Alex Williamson04b16652010-07-02 11:13:17 -06001115
1116 end = block->offset + block->length;
1117
Paolo Bonzinia3161032012-11-14 15:54:48 +01001118 QTAILQ_FOREACH(next_block, &ram_list.blocks, next) {
Alex Williamson04b16652010-07-02 11:13:17 -06001119 if (next_block->offset >= end) {
1120 next = MIN(next, next_block->offset);
1121 }
1122 }
1123 if (next - end >= size && next - end < mingap) {
Alex Williamson3e837b22011-10-31 08:54:09 -06001124 offset = end;
Alex Williamson04b16652010-07-02 11:13:17 -06001125 mingap = next - end;
1126 }
1127 }
Alex Williamson3e837b22011-10-31 08:54:09 -06001128
1129 if (offset == RAM_ADDR_MAX) {
1130 fprintf(stderr, "Failed to find gap of requested size: %" PRIu64 "\n",
1131 (uint64_t)size);
1132 abort();
1133 }
1134
Alex Williamson04b16652010-07-02 11:13:17 -06001135 return offset;
1136}
1137
Juan Quintela652d7ec2012-07-20 10:37:54 +02001138ram_addr_t last_ram_offset(void)
Alex Williamson04b16652010-07-02 11:13:17 -06001139{
Alex Williamsond17b5282010-06-25 11:08:38 -06001140 RAMBlock *block;
1141 ram_addr_t last = 0;
1142
Paolo Bonzinia3161032012-11-14 15:54:48 +01001143 QTAILQ_FOREACH(block, &ram_list.blocks, next)
Alex Williamsond17b5282010-06-25 11:08:38 -06001144 last = MAX(last, block->offset + block->length);
1145
1146 return last;
1147}
1148
Jason Baronddb97f12012-08-02 15:44:16 -04001149static void qemu_ram_setup_dump(void *addr, ram_addr_t size)
1150{
1151 int ret;
Jason Baronddb97f12012-08-02 15:44:16 -04001152
1153 /* Use MADV_DONTDUMP, if user doesn't want the guest memory in the core */
Markus Armbruster2ff3de62013-07-04 15:09:22 +02001154 if (!qemu_opt_get_bool(qemu_get_machine_opts(),
1155 "dump-guest-core", true)) {
Jason Baronddb97f12012-08-02 15:44:16 -04001156 ret = qemu_madvise(addr, size, QEMU_MADV_DONTDUMP);
1157 if (ret) {
1158 perror("qemu_madvise");
1159 fprintf(stderr, "madvise doesn't support MADV_DONTDUMP, "
1160 "but dump_guest_core=off specified\n");
1161 }
1162 }
1163}
1164
Avi Kivityc5705a72011-12-20 15:59:12 +02001165void qemu_ram_set_idstr(ram_addr_t addr, const char *name, DeviceState *dev)
Cam Macdonell84b89d72010-07-26 18:10:57 -06001166{
1167 RAMBlock *new_block, *block;
1168
Avi Kivityc5705a72011-12-20 15:59:12 +02001169 new_block = NULL;
Paolo Bonzinia3161032012-11-14 15:54:48 +01001170 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Avi Kivityc5705a72011-12-20 15:59:12 +02001171 if (block->offset == addr) {
1172 new_block = block;
1173 break;
1174 }
1175 }
1176 assert(new_block);
1177 assert(!new_block->idstr[0]);
Cam Macdonell84b89d72010-07-26 18:10:57 -06001178
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001179 if (dev) {
1180 char *id = qdev_get_dev_path(dev);
Cam Macdonell84b89d72010-07-26 18:10:57 -06001181 if (id) {
1182 snprintf(new_block->idstr, sizeof(new_block->idstr), "%s/", id);
Anthony Liguori7267c092011-08-20 22:09:37 -05001183 g_free(id);
Cam Macdonell84b89d72010-07-26 18:10:57 -06001184 }
1185 }
1186 pstrcat(new_block->idstr, sizeof(new_block->idstr), name);
1187
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001188 /* This assumes the iothread lock is taken here too. */
1189 qemu_mutex_lock_ramlist();
Paolo Bonzinia3161032012-11-14 15:54:48 +01001190 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Avi Kivityc5705a72011-12-20 15:59:12 +02001191 if (block != new_block && !strcmp(block->idstr, new_block->idstr)) {
Cam Macdonell84b89d72010-07-26 18:10:57 -06001192 fprintf(stderr, "RAMBlock \"%s\" already registered, abort!\n",
1193 new_block->idstr);
1194 abort();
1195 }
1196 }
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001197 qemu_mutex_unlock_ramlist();
Avi Kivityc5705a72011-12-20 15:59:12 +02001198}
1199
Luiz Capitulino8490fc72012-09-05 16:50:16 -03001200static int memory_try_enable_merging(void *addr, size_t len)
1201{
Markus Armbruster2ff3de62013-07-04 15:09:22 +02001202 if (!qemu_opt_get_bool(qemu_get_machine_opts(), "mem-merge", true)) {
Luiz Capitulino8490fc72012-09-05 16:50:16 -03001203 /* disabled by the user */
1204 return 0;
1205 }
1206
1207 return qemu_madvise(addr, len, QEMU_MADV_MERGEABLE);
1208}
1209
Avi Kivityc5705a72011-12-20 15:59:12 +02001210ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
1211 MemoryRegion *mr)
1212{
Paolo Bonziniabb26d62012-11-14 16:00:51 +01001213 RAMBlock *block, *new_block;
Avi Kivityc5705a72011-12-20 15:59:12 +02001214
1215 size = TARGET_PAGE_ALIGN(size);
1216 new_block = g_malloc0(sizeof(*new_block));
Markus Armbruster3435f392013-07-31 15:11:07 +02001217 new_block->fd = -1;
Cam Macdonell84b89d72010-07-26 18:10:57 -06001218
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001219 /* This assumes the iothread lock is taken here too. */
1220 qemu_mutex_lock_ramlist();
Avi Kivity7c637362011-12-21 13:09:49 +02001221 new_block->mr = mr;
Jun Nakajima432d2682010-08-31 16:41:25 +01001222 new_block->offset = find_ram_offset(size);
Yoshiaki Tamura6977dfe2010-08-18 15:41:49 +09001223 if (host) {
1224 new_block->host = host;
Huang Yingcd19cfa2011-03-02 08:56:19 +01001225 new_block->flags |= RAM_PREALLOC_MASK;
Markus Armbrusterdfeaf2a2013-07-31 15:11:05 +02001226 } else if (xen_enabled()) {
1227 if (mem_path) {
1228 fprintf(stderr, "-mem-path not supported with Xen\n");
1229 exit(1);
1230 }
1231 xen_ram_alloc(new_block->offset, size, mr);
Yoshiaki Tamura6977dfe2010-08-18 15:41:49 +09001232 } else {
1233 if (mem_path) {
Markus Armbrustere1e84ba2013-07-31 15:11:10 +02001234 if (phys_mem_alloc != qemu_anon_ram_alloc) {
1235 /*
1236 * file_ram_alloc() needs to allocate just like
1237 * phys_mem_alloc, but we haven't bothered to provide
1238 * a hook there.
1239 */
1240 fprintf(stderr,
1241 "-mem-path not supported with this accelerator\n");
1242 exit(1);
1243 }
Yoshiaki Tamura6977dfe2010-08-18 15:41:49 +09001244 new_block->host = file_ram_alloc(new_block, size, mem_path);
Markus Armbruster0628c182013-07-31 15:11:06 +02001245 }
1246 if (!new_block->host) {
Markus Armbruster91138032013-07-31 15:11:08 +02001247 new_block->host = phys_mem_alloc(size);
Markus Armbruster39228252013-07-31 15:11:11 +02001248 if (!new_block->host) {
1249 fprintf(stderr, "Cannot set up guest memory '%s': %s\n",
1250 new_block->mr->name, strerror(errno));
1251 exit(1);
1252 }
Luiz Capitulino8490fc72012-09-05 16:50:16 -03001253 memory_try_enable_merging(new_block->host, size);
Yoshiaki Tamura6977dfe2010-08-18 15:41:49 +09001254 }
1255 }
Cam Macdonell84b89d72010-07-26 18:10:57 -06001256 new_block->length = size;
1257
Paolo Bonziniabb26d62012-11-14 16:00:51 +01001258 /* Keep the list sorted from biggest to smallest block. */
1259 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
1260 if (block->length < new_block->length) {
1261 break;
1262 }
1263 }
1264 if (block) {
1265 QTAILQ_INSERT_BEFORE(block, new_block, next);
1266 } else {
1267 QTAILQ_INSERT_TAIL(&ram_list.blocks, new_block, next);
1268 }
Paolo Bonzini0d6d3c82012-11-14 15:45:02 +01001269 ram_list.mru_block = NULL;
Cam Macdonell84b89d72010-07-26 18:10:57 -06001270
Umesh Deshpandef798b072011-08-18 11:41:17 -07001271 ram_list.version++;
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001272 qemu_mutex_unlock_ramlist();
Umesh Deshpandef798b072011-08-18 11:41:17 -07001273
Anthony Liguori7267c092011-08-20 22:09:37 -05001274 ram_list.phys_dirty = g_realloc(ram_list.phys_dirty,
Cam Macdonell84b89d72010-07-26 18:10:57 -06001275 last_ram_offset() >> TARGET_PAGE_BITS);
Igor Mitsyanko5fda0432012-08-10 18:45:11 +04001276 memset(ram_list.phys_dirty + (new_block->offset >> TARGET_PAGE_BITS),
1277 0, size >> TARGET_PAGE_BITS);
Juan Quintela1720aee2012-06-22 13:14:17 +02001278 cpu_physical_memory_set_dirty_range(new_block->offset, size, 0xff);
Cam Macdonell84b89d72010-07-26 18:10:57 -06001279
Jason Baronddb97f12012-08-02 15:44:16 -04001280 qemu_ram_setup_dump(new_block->host, size);
Luiz Capitulinoad0b5322012-10-05 16:47:57 -03001281 qemu_madvise(new_block->host, size, QEMU_MADV_HUGEPAGE);
Andrea Arcangeli3e469db2013-07-25 12:11:15 +02001282 qemu_madvise(new_block->host, size, QEMU_MADV_DONTFORK);
Jason Baronddb97f12012-08-02 15:44:16 -04001283
Cam Macdonell84b89d72010-07-26 18:10:57 -06001284 if (kvm_enabled())
1285 kvm_setup_guest_memory(new_block->host, size);
1286
1287 return new_block->offset;
1288}
1289
Avi Kivityc5705a72011-12-20 15:59:12 +02001290ram_addr_t qemu_ram_alloc(ram_addr_t size, MemoryRegion *mr)
pbrook94a6b542009-04-11 17:15:54 +00001291{
Avi Kivityc5705a72011-12-20 15:59:12 +02001292 return qemu_ram_alloc_from_ptr(size, NULL, mr);
pbrook94a6b542009-04-11 17:15:54 +00001293}
bellarde9a1ab12007-02-08 23:08:38 +00001294
Alex Williamson1f2e98b2011-05-03 12:48:09 -06001295void qemu_ram_free_from_ptr(ram_addr_t addr)
1296{
1297 RAMBlock *block;
1298
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001299 /* This assumes the iothread lock is taken here too. */
1300 qemu_mutex_lock_ramlist();
Paolo Bonzinia3161032012-11-14 15:54:48 +01001301 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Alex Williamson1f2e98b2011-05-03 12:48:09 -06001302 if (addr == block->offset) {
Paolo Bonzinia3161032012-11-14 15:54:48 +01001303 QTAILQ_REMOVE(&ram_list.blocks, block, next);
Paolo Bonzini0d6d3c82012-11-14 15:45:02 +01001304 ram_list.mru_block = NULL;
Umesh Deshpandef798b072011-08-18 11:41:17 -07001305 ram_list.version++;
Anthony Liguori7267c092011-08-20 22:09:37 -05001306 g_free(block);
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001307 break;
Alex Williamson1f2e98b2011-05-03 12:48:09 -06001308 }
1309 }
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001310 qemu_mutex_unlock_ramlist();
Alex Williamson1f2e98b2011-05-03 12:48:09 -06001311}
1312
Anthony Liguoric227f092009-10-01 16:12:16 -05001313void qemu_ram_free(ram_addr_t addr)
bellarde9a1ab12007-02-08 23:08:38 +00001314{
Alex Williamson04b16652010-07-02 11:13:17 -06001315 RAMBlock *block;
1316
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001317 /* This assumes the iothread lock is taken here too. */
1318 qemu_mutex_lock_ramlist();
Paolo Bonzinia3161032012-11-14 15:54:48 +01001319 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Alex Williamson04b16652010-07-02 11:13:17 -06001320 if (addr == block->offset) {
Paolo Bonzinia3161032012-11-14 15:54:48 +01001321 QTAILQ_REMOVE(&ram_list.blocks, block, next);
Paolo Bonzini0d6d3c82012-11-14 15:45:02 +01001322 ram_list.mru_block = NULL;
Umesh Deshpandef798b072011-08-18 11:41:17 -07001323 ram_list.version++;
Huang Yingcd19cfa2011-03-02 08:56:19 +01001324 if (block->flags & RAM_PREALLOC_MASK) {
1325 ;
Markus Armbrusterdfeaf2a2013-07-31 15:11:05 +02001326 } else if (xen_enabled()) {
1327 xen_invalidate_map_cache_entry(block->host);
Stefan Weil089f3f72013-09-18 07:48:15 +02001328#ifndef _WIN32
Markus Armbruster3435f392013-07-31 15:11:07 +02001329 } else if (block->fd >= 0) {
1330 munmap(block->host, block->length);
1331 close(block->fd);
Stefan Weil089f3f72013-09-18 07:48:15 +02001332#endif
Alex Williamson04b16652010-07-02 11:13:17 -06001333 } else {
Markus Armbrusterdfeaf2a2013-07-31 15:11:05 +02001334 qemu_anon_ram_free(block->host, block->length);
Alex Williamson04b16652010-07-02 11:13:17 -06001335 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001336 g_free(block);
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001337 break;
Alex Williamson04b16652010-07-02 11:13:17 -06001338 }
1339 }
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001340 qemu_mutex_unlock_ramlist();
Alex Williamson04b16652010-07-02 11:13:17 -06001341
bellarde9a1ab12007-02-08 23:08:38 +00001342}
1343
Huang Yingcd19cfa2011-03-02 08:56:19 +01001344#ifndef _WIN32
1345void qemu_ram_remap(ram_addr_t addr, ram_addr_t length)
1346{
1347 RAMBlock *block;
1348 ram_addr_t offset;
1349 int flags;
1350 void *area, *vaddr;
1351
Paolo Bonzinia3161032012-11-14 15:54:48 +01001352 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Huang Yingcd19cfa2011-03-02 08:56:19 +01001353 offset = addr - block->offset;
1354 if (offset < block->length) {
1355 vaddr = block->host + offset;
1356 if (block->flags & RAM_PREALLOC_MASK) {
1357 ;
Markus Armbrusterdfeaf2a2013-07-31 15:11:05 +02001358 } else if (xen_enabled()) {
1359 abort();
Huang Yingcd19cfa2011-03-02 08:56:19 +01001360 } else {
1361 flags = MAP_FIXED;
1362 munmap(vaddr, length);
Markus Armbruster3435f392013-07-31 15:11:07 +02001363 if (block->fd >= 0) {
Huang Yingcd19cfa2011-03-02 08:56:19 +01001364#ifdef MAP_POPULATE
Markus Armbruster3435f392013-07-31 15:11:07 +02001365 flags |= mem_prealloc ? MAP_POPULATE | MAP_SHARED :
1366 MAP_PRIVATE;
Huang Yingcd19cfa2011-03-02 08:56:19 +01001367#else
Markus Armbruster3435f392013-07-31 15:11:07 +02001368 flags |= MAP_PRIVATE;
Huang Yingcd19cfa2011-03-02 08:56:19 +01001369#endif
Markus Armbruster3435f392013-07-31 15:11:07 +02001370 area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
1371 flags, block->fd, offset);
Huang Yingcd19cfa2011-03-02 08:56:19 +01001372 } else {
Markus Armbruster2eb9fba2013-07-31 15:11:09 +02001373 /*
1374 * Remap needs to match alloc. Accelerators that
1375 * set phys_mem_alloc never remap. If they did,
1376 * we'd need a remap hook here.
1377 */
1378 assert(phys_mem_alloc == qemu_anon_ram_alloc);
1379
Huang Yingcd19cfa2011-03-02 08:56:19 +01001380 flags |= MAP_PRIVATE | MAP_ANONYMOUS;
1381 area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
1382 flags, -1, 0);
Huang Yingcd19cfa2011-03-02 08:56:19 +01001383 }
1384 if (area != vaddr) {
Anthony PERARDf15fbc42011-07-20 08:17:42 +00001385 fprintf(stderr, "Could not remap addr: "
1386 RAM_ADDR_FMT "@" RAM_ADDR_FMT "\n",
Huang Yingcd19cfa2011-03-02 08:56:19 +01001387 length, addr);
1388 exit(1);
1389 }
Luiz Capitulino8490fc72012-09-05 16:50:16 -03001390 memory_try_enable_merging(vaddr, length);
Jason Baronddb97f12012-08-02 15:44:16 -04001391 qemu_ram_setup_dump(vaddr, length);
Huang Yingcd19cfa2011-03-02 08:56:19 +01001392 }
1393 return;
1394 }
1395 }
1396}
1397#endif /* !_WIN32 */
1398
Paolo Bonzini1b5ec232013-05-06 14:36:15 +02001399/* Return a host pointer to ram allocated with qemu_ram_alloc.
1400 With the exception of the softmmu code in this file, this should
1401 only be used for local memory (e.g. video ram) that the device owns,
1402 and knows it isn't going to access beyond the end of the block.
1403
1404 It should not be used for general purpose DMA.
1405 Use cpu_physical_memory_map/cpu_physical_memory_rw instead.
1406 */
1407void *qemu_get_ram_ptr(ram_addr_t addr)
1408{
1409 RAMBlock *block = qemu_get_ram_block(addr);
1410
Paolo Bonzini0d6d3c82012-11-14 15:45:02 +01001411 if (xen_enabled()) {
1412 /* We need to check if the requested address is in the RAM
1413 * because we don't want to map the entire memory in QEMU.
1414 * In that case just map until the end of the page.
1415 */
1416 if (block->offset == 0) {
1417 return xen_map_cache(addr, 0, 0);
1418 } else if (block->host == NULL) {
1419 block->host =
1420 xen_map_cache(block->offset, block->length, 1);
1421 }
1422 }
1423 return block->host + (addr - block->offset);
pbrookdc828ca2009-04-09 22:21:07 +00001424}
1425
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01001426/* Return a host pointer to guest's ram. Similar to qemu_get_ram_ptr
1427 * but takes a size argument */
Peter Maydellcb85f7a2013-07-08 09:44:04 +01001428static void *qemu_ram_ptr_length(ram_addr_t addr, hwaddr *size)
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01001429{
Stefano Stabellini8ab934f2011-06-27 18:26:06 +01001430 if (*size == 0) {
1431 return NULL;
1432 }
Jan Kiszka868bb332011-06-21 22:59:09 +02001433 if (xen_enabled()) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02001434 return xen_map_cache(addr, *size, 1);
Jan Kiszka868bb332011-06-21 22:59:09 +02001435 } else {
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01001436 RAMBlock *block;
1437
Paolo Bonzinia3161032012-11-14 15:54:48 +01001438 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01001439 if (addr - block->offset < block->length) {
1440 if (addr - block->offset + *size > block->length)
1441 *size = block->length - addr + block->offset;
1442 return block->host + (addr - block->offset);
1443 }
1444 }
1445
1446 fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
1447 abort();
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01001448 }
1449}
1450
Paolo Bonzini7443b432013-06-03 12:44:02 +02001451/* Some of the softmmu routines need to translate from a host pointer
1452 (typically a TLB entry) back to a ram offset. */
Paolo Bonzini1b5ec232013-05-06 14:36:15 +02001453MemoryRegion *qemu_ram_addr_from_host(void *ptr, ram_addr_t *ram_addr)
pbrook5579c7f2009-04-11 14:47:08 +00001454{
pbrook94a6b542009-04-11 17:15:54 +00001455 RAMBlock *block;
1456 uint8_t *host = ptr;
1457
Jan Kiszka868bb332011-06-21 22:59:09 +02001458 if (xen_enabled()) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02001459 *ram_addr = xen_ram_addr_from_mapcache(ptr);
Paolo Bonzini1b5ec232013-05-06 14:36:15 +02001460 return qemu_get_ram_block(*ram_addr)->mr;
Stefano Stabellini712c2b42011-05-19 18:35:46 +01001461 }
1462
Paolo Bonzini23887b72013-05-06 14:28:39 +02001463 block = ram_list.mru_block;
1464 if (block && block->host && host - block->host < block->length) {
1465 goto found;
1466 }
1467
Paolo Bonzinia3161032012-11-14 15:54:48 +01001468 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Jun Nakajima432d2682010-08-31 16:41:25 +01001469 /* This case append when the block is not mapped. */
1470 if (block->host == NULL) {
1471 continue;
1472 }
Alex Williamsonf471a172010-06-11 11:11:42 -06001473 if (host - block->host < block->length) {
Paolo Bonzini23887b72013-05-06 14:28:39 +02001474 goto found;
Alex Williamsonf471a172010-06-11 11:11:42 -06001475 }
pbrook94a6b542009-04-11 17:15:54 +00001476 }
Jun Nakajima432d2682010-08-31 16:41:25 +01001477
Paolo Bonzini1b5ec232013-05-06 14:36:15 +02001478 return NULL;
Paolo Bonzini23887b72013-05-06 14:28:39 +02001479
1480found:
1481 *ram_addr = block->offset + (host - block->host);
Paolo Bonzini1b5ec232013-05-06 14:36:15 +02001482 return block->mr;
Marcelo Tosattie8902612010-10-11 15:31:19 -03001483}
Alex Williamsonf471a172010-06-11 11:11:42 -06001484
Avi Kivitya8170e52012-10-23 12:30:10 +02001485static void notdirty_mem_write(void *opaque, hwaddr ram_addr,
Avi Kivity0e0df1e2012-01-02 00:32:15 +02001486 uint64_t val, unsigned size)
bellard1ccde1c2004-02-06 19:46:14 +00001487{
Juan Quintela7e5609a2013-10-08 11:57:21 +02001488 if (!cpu_physical_memory_get_dirty_flag(ram_addr, CODE_DIRTY_FLAG)) {
Avi Kivity0e0df1e2012-01-02 00:32:15 +02001489 tb_invalidate_phys_page_fast(ram_addr, size);
bellard3a7d9292005-08-21 09:26:42 +00001490 }
Avi Kivity0e0df1e2012-01-02 00:32:15 +02001491 switch (size) {
1492 case 1:
1493 stb_p(qemu_get_ram_ptr(ram_addr), val);
1494 break;
1495 case 2:
1496 stw_p(qemu_get_ram_ptr(ram_addr), val);
1497 break;
1498 case 4:
1499 stl_p(qemu_get_ram_ptr(ram_addr), val);
1500 break;
1501 default:
1502 abort();
1503 }
Juan Quintela4f08cab2013-10-08 12:01:01 +02001504 cpu_physical_memory_set_dirty_flag(ram_addr, MIGRATION_DIRTY_FLAG);
1505 cpu_physical_memory_set_dirty_flag(ram_addr, VGA_DIRTY_FLAG);
bellardf23db162005-08-21 19:12:28 +00001506 /* we remove the notdirty callback only if the code has been
1507 flushed */
Juan Quintela06567942013-10-08 11:50:04 +02001508 if (cpu_physical_memory_is_dirty(ram_addr)) {
Andreas Färber4917cf42013-05-27 05:17:50 +02001509 CPUArchState *env = current_cpu->env_ptr;
1510 tlb_set_dirty(env, env->mem_io_vaddr);
1511 }
bellard1ccde1c2004-02-06 19:46:14 +00001512}
1513
Paolo Bonzinib018ddf2013-05-24 14:48:38 +02001514static bool notdirty_mem_accepts(void *opaque, hwaddr addr,
1515 unsigned size, bool is_write)
1516{
1517 return is_write;
1518}
1519
Avi Kivity0e0df1e2012-01-02 00:32:15 +02001520static const MemoryRegionOps notdirty_mem_ops = {
Avi Kivity0e0df1e2012-01-02 00:32:15 +02001521 .write = notdirty_mem_write,
Paolo Bonzinib018ddf2013-05-24 14:48:38 +02001522 .valid.accepts = notdirty_mem_accepts,
Avi Kivity0e0df1e2012-01-02 00:32:15 +02001523 .endianness = DEVICE_NATIVE_ENDIAN,
bellard1ccde1c2004-02-06 19:46:14 +00001524};
1525
pbrook0f459d12008-06-09 00:20:13 +00001526/* Generate a debug exception if a watchpoint has been hit. */
aliguorib4051332008-11-18 20:14:20 +00001527static void check_watchpoint(int offset, int len_mask, int flags)
pbrook0f459d12008-06-09 00:20:13 +00001528{
Andreas Färber4917cf42013-05-27 05:17:50 +02001529 CPUArchState *env = current_cpu->env_ptr;
aliguori06d55cc2008-11-18 20:24:06 +00001530 target_ulong pc, cs_base;
pbrook0f459d12008-06-09 00:20:13 +00001531 target_ulong vaddr;
aliguoria1d1bb32008-11-18 20:07:32 +00001532 CPUWatchpoint *wp;
aliguori06d55cc2008-11-18 20:24:06 +00001533 int cpu_flags;
pbrook0f459d12008-06-09 00:20:13 +00001534
aliguori06d55cc2008-11-18 20:24:06 +00001535 if (env->watchpoint_hit) {
1536 /* We re-entered the check after replacing the TB. Now raise
1537 * the debug interrupt so that is will trigger after the
1538 * current instruction. */
Andreas Färberc3affe52013-01-18 15:03:43 +01001539 cpu_interrupt(ENV_GET_CPU(env), CPU_INTERRUPT_DEBUG);
aliguori06d55cc2008-11-18 20:24:06 +00001540 return;
1541 }
pbrook2e70f6e2008-06-29 01:03:05 +00001542 vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001543 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
aliguorib4051332008-11-18 20:14:20 +00001544 if ((vaddr == (wp->vaddr & len_mask) ||
1545 (vaddr & wp->len_mask) == wp->vaddr) && (wp->flags & flags)) {
aliguori6e140f22008-11-18 20:37:55 +00001546 wp->flags |= BP_WATCHPOINT_HIT;
1547 if (!env->watchpoint_hit) {
1548 env->watchpoint_hit = wp;
Blue Swirl5a316522012-12-02 21:28:09 +00001549 tb_check_watchpoint(env);
aliguori6e140f22008-11-18 20:37:55 +00001550 if (wp->flags & BP_STOP_BEFORE_ACCESS) {
1551 env->exception_index = EXCP_DEBUG;
Max Filippov488d6572012-01-29 02:24:39 +04001552 cpu_loop_exit(env);
aliguori6e140f22008-11-18 20:37:55 +00001553 } else {
1554 cpu_get_tb_cpu_state(env, &pc, &cs_base, &cpu_flags);
1555 tb_gen_code(env, pc, cs_base, cpu_flags, 1);
Max Filippov488d6572012-01-29 02:24:39 +04001556 cpu_resume_from_signal(env, NULL);
aliguori6e140f22008-11-18 20:37:55 +00001557 }
aliguori06d55cc2008-11-18 20:24:06 +00001558 }
aliguori6e140f22008-11-18 20:37:55 +00001559 } else {
1560 wp->flags &= ~BP_WATCHPOINT_HIT;
pbrook0f459d12008-06-09 00:20:13 +00001561 }
1562 }
1563}
1564
pbrook6658ffb2007-03-16 23:58:11 +00001565/* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
1566 so these check for a hit then pass through to the normal out-of-line
1567 phys routines. */
Avi Kivitya8170e52012-10-23 12:30:10 +02001568static uint64_t watch_mem_read(void *opaque, hwaddr addr,
Avi Kivity1ec9b902012-01-02 12:47:48 +02001569 unsigned size)
pbrook6658ffb2007-03-16 23:58:11 +00001570{
Avi Kivity1ec9b902012-01-02 12:47:48 +02001571 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~(size - 1), BP_MEM_READ);
1572 switch (size) {
1573 case 1: return ldub_phys(addr);
1574 case 2: return lduw_phys(addr);
1575 case 4: return ldl_phys(addr);
1576 default: abort();
1577 }
pbrook6658ffb2007-03-16 23:58:11 +00001578}
1579
Avi Kivitya8170e52012-10-23 12:30:10 +02001580static void watch_mem_write(void *opaque, hwaddr addr,
Avi Kivity1ec9b902012-01-02 12:47:48 +02001581 uint64_t val, unsigned size)
pbrook6658ffb2007-03-16 23:58:11 +00001582{
Avi Kivity1ec9b902012-01-02 12:47:48 +02001583 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~(size - 1), BP_MEM_WRITE);
1584 switch (size) {
Max Filippov67364152012-01-29 00:01:40 +04001585 case 1:
1586 stb_phys(addr, val);
1587 break;
1588 case 2:
1589 stw_phys(addr, val);
1590 break;
1591 case 4:
1592 stl_phys(addr, val);
1593 break;
Avi Kivity1ec9b902012-01-02 12:47:48 +02001594 default: abort();
1595 }
pbrook6658ffb2007-03-16 23:58:11 +00001596}
1597
Avi Kivity1ec9b902012-01-02 12:47:48 +02001598static const MemoryRegionOps watch_mem_ops = {
1599 .read = watch_mem_read,
1600 .write = watch_mem_write,
1601 .endianness = DEVICE_NATIVE_ENDIAN,
pbrook6658ffb2007-03-16 23:58:11 +00001602};
pbrook6658ffb2007-03-16 23:58:11 +00001603
Avi Kivitya8170e52012-10-23 12:30:10 +02001604static uint64_t subpage_read(void *opaque, hwaddr addr,
Avi Kivity70c68e42012-01-02 12:32:48 +02001605 unsigned len)
blueswir1db7b5422007-05-26 17:36:03 +00001606{
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001607 subpage_t *subpage = opaque;
1608 uint8_t buf[4];
Paolo Bonzini791af8c2013-05-24 16:10:39 +02001609
blueswir1db7b5422007-05-26 17:36:03 +00001610#if defined(DEBUG_SUBPAGE)
Amos Kong016e9d62013-09-27 09:25:38 +08001611 printf("%s: subpage %p len %u addr " TARGET_FMT_plx "\n", __func__,
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001612 subpage, len, addr);
blueswir1db7b5422007-05-26 17:36:03 +00001613#endif
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001614 address_space_read(subpage->as, addr + subpage->base, buf, len);
1615 switch (len) {
1616 case 1:
1617 return ldub_p(buf);
1618 case 2:
1619 return lduw_p(buf);
1620 case 4:
1621 return ldl_p(buf);
1622 default:
1623 abort();
1624 }
blueswir1db7b5422007-05-26 17:36:03 +00001625}
1626
Avi Kivitya8170e52012-10-23 12:30:10 +02001627static void subpage_write(void *opaque, hwaddr addr,
Avi Kivity70c68e42012-01-02 12:32:48 +02001628 uint64_t value, unsigned len)
blueswir1db7b5422007-05-26 17:36:03 +00001629{
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001630 subpage_t *subpage = opaque;
1631 uint8_t buf[4];
1632
blueswir1db7b5422007-05-26 17:36:03 +00001633#if defined(DEBUG_SUBPAGE)
Amos Kong016e9d62013-09-27 09:25:38 +08001634 printf("%s: subpage %p len %u addr " TARGET_FMT_plx
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001635 " value %"PRIx64"\n",
1636 __func__, subpage, len, addr, value);
blueswir1db7b5422007-05-26 17:36:03 +00001637#endif
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001638 switch (len) {
1639 case 1:
1640 stb_p(buf, value);
1641 break;
1642 case 2:
1643 stw_p(buf, value);
1644 break;
1645 case 4:
1646 stl_p(buf, value);
1647 break;
1648 default:
1649 abort();
1650 }
1651 address_space_write(subpage->as, addr + subpage->base, buf, len);
blueswir1db7b5422007-05-26 17:36:03 +00001652}
1653
Paolo Bonzinic353e4c2013-05-24 14:02:39 +02001654static bool subpage_accepts(void *opaque, hwaddr addr,
Amos Kong016e9d62013-09-27 09:25:38 +08001655 unsigned len, bool is_write)
Paolo Bonzinic353e4c2013-05-24 14:02:39 +02001656{
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001657 subpage_t *subpage = opaque;
Paolo Bonzinic353e4c2013-05-24 14:02:39 +02001658#if defined(DEBUG_SUBPAGE)
Amos Kong016e9d62013-09-27 09:25:38 +08001659 printf("%s: subpage %p %c len %u addr " TARGET_FMT_plx "\n",
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001660 __func__, subpage, is_write ? 'w' : 'r', len, addr);
Paolo Bonzinic353e4c2013-05-24 14:02:39 +02001661#endif
1662
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001663 return address_space_access_valid(subpage->as, addr + subpage->base,
Amos Kong016e9d62013-09-27 09:25:38 +08001664 len, is_write);
Paolo Bonzinic353e4c2013-05-24 14:02:39 +02001665}
1666
Avi Kivity70c68e42012-01-02 12:32:48 +02001667static const MemoryRegionOps subpage_ops = {
1668 .read = subpage_read,
1669 .write = subpage_write,
Paolo Bonzinic353e4c2013-05-24 14:02:39 +02001670 .valid.accepts = subpage_accepts,
Avi Kivity70c68e42012-01-02 12:32:48 +02001671 .endianness = DEVICE_NATIVE_ENDIAN,
blueswir1db7b5422007-05-26 17:36:03 +00001672};
1673
Anthony Liguoric227f092009-10-01 16:12:16 -05001674static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
Avi Kivity5312bd82012-02-12 18:32:55 +02001675 uint16_t section)
blueswir1db7b5422007-05-26 17:36:03 +00001676{
1677 int idx, eidx;
1678
1679 if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
1680 return -1;
1681 idx = SUBPAGE_IDX(start);
1682 eidx = SUBPAGE_IDX(end);
1683#if defined(DEBUG_SUBPAGE)
Amos Kong016e9d62013-09-27 09:25:38 +08001684 printf("%s: %p start %08x end %08x idx %08x eidx %08x section %d\n",
1685 __func__, mmio, start, end, idx, eidx, section);
blueswir1db7b5422007-05-26 17:36:03 +00001686#endif
blueswir1db7b5422007-05-26 17:36:03 +00001687 for (; idx <= eidx; idx++) {
Avi Kivity5312bd82012-02-12 18:32:55 +02001688 mmio->sub_section[idx] = section;
blueswir1db7b5422007-05-26 17:36:03 +00001689 }
1690
1691 return 0;
1692}
1693
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001694static subpage_t *subpage_init(AddressSpace *as, hwaddr base)
blueswir1db7b5422007-05-26 17:36:03 +00001695{
Anthony Liguoric227f092009-10-01 16:12:16 -05001696 subpage_t *mmio;
blueswir1db7b5422007-05-26 17:36:03 +00001697
Anthony Liguori7267c092011-08-20 22:09:37 -05001698 mmio = g_malloc0(sizeof(subpage_t));
aliguori1eec6142009-02-05 22:06:18 +00001699
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001700 mmio->as = as;
aliguori1eec6142009-02-05 22:06:18 +00001701 mmio->base = base;
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -04001702 memory_region_init_io(&mmio->iomem, NULL, &subpage_ops, mmio,
Avi Kivity70c68e42012-01-02 12:32:48 +02001703 "subpage", TARGET_PAGE_SIZE);
Avi Kivityb3b00c72012-01-02 13:20:11 +02001704 mmio->iomem.subpage = true;
blueswir1db7b5422007-05-26 17:36:03 +00001705#if defined(DEBUG_SUBPAGE)
Amos Kong016e9d62013-09-27 09:25:38 +08001706 printf("%s: %p base " TARGET_FMT_plx " len %08x\n", __func__,
1707 mmio, base, TARGET_PAGE_SIZE);
blueswir1db7b5422007-05-26 17:36:03 +00001708#endif
Liu Ping Fanb41aac42013-05-29 11:09:17 +02001709 subpage_register(mmio, 0, TARGET_PAGE_SIZE-1, PHYS_SECTION_UNASSIGNED);
blueswir1db7b5422007-05-26 17:36:03 +00001710
1711 return mmio;
1712}
1713
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +02001714static uint16_t dummy_section(PhysPageMap *map, MemoryRegion *mr)
Avi Kivity5312bd82012-02-12 18:32:55 +02001715{
1716 MemoryRegionSection section = {
1717 .mr = mr,
1718 .offset_within_address_space = 0,
1719 .offset_within_region = 0,
Paolo Bonzini052e87b2013-05-27 10:08:27 +02001720 .size = int128_2_64(),
Avi Kivity5312bd82012-02-12 18:32:55 +02001721 };
1722
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +02001723 return phys_section_add(map, &section);
Avi Kivity5312bd82012-02-12 18:32:55 +02001724}
1725
Avi Kivitya8170e52012-10-23 12:30:10 +02001726MemoryRegion *iotlb_to_region(hwaddr index)
Avi Kivityaa102232012-03-08 17:06:55 +02001727{
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +02001728 return address_space_memory.dispatch->map.sections[
1729 index & ~TARGET_PAGE_MASK].mr;
Avi Kivityaa102232012-03-08 17:06:55 +02001730}
1731
Avi Kivitye9179ce2009-06-14 11:38:52 +03001732static void io_mem_init(void)
1733{
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -04001734 memory_region_init_io(&io_mem_rom, NULL, &unassigned_mem_ops, NULL, "rom", UINT64_MAX);
1735 memory_region_init_io(&io_mem_unassigned, NULL, &unassigned_mem_ops, NULL,
Avi Kivity0e0df1e2012-01-02 00:32:15 +02001736 "unassigned", UINT64_MAX);
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -04001737 memory_region_init_io(&io_mem_notdirty, NULL, &notdirty_mem_ops, NULL,
Avi Kivity0e0df1e2012-01-02 00:32:15 +02001738 "notdirty", UINT64_MAX);
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -04001739 memory_region_init_io(&io_mem_watch, NULL, &watch_mem_ops, NULL,
Avi Kivity1ec9b902012-01-02 12:47:48 +02001740 "watch", UINT64_MAX);
Avi Kivitye9179ce2009-06-14 11:38:52 +03001741}
1742
Avi Kivityac1970f2012-10-03 16:22:53 +02001743static void mem_begin(MemoryListener *listener)
1744{
Paolo Bonzini89ae3372013-06-02 10:39:07 +02001745 AddressSpace *as = container_of(listener, AddressSpace, dispatch_listener);
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +02001746 AddressSpaceDispatch *d = g_new0(AddressSpaceDispatch, 1);
1747 uint16_t n;
1748
1749 n = dummy_section(&d->map, &io_mem_unassigned);
1750 assert(n == PHYS_SECTION_UNASSIGNED);
1751 n = dummy_section(&d->map, &io_mem_notdirty);
1752 assert(n == PHYS_SECTION_NOTDIRTY);
1753 n = dummy_section(&d->map, &io_mem_rom);
1754 assert(n == PHYS_SECTION_ROM);
1755 n = dummy_section(&d->map, &io_mem_watch);
1756 assert(n == PHYS_SECTION_WATCH);
Paolo Bonzini00752702013-05-29 12:13:54 +02001757
Michael S. Tsirkin9736e552013-11-11 14:42:43 +02001758 d->phys_map = (PhysPageEntry) { .ptr = PHYS_MAP_NODE_NIL, .skip = 1 };
Paolo Bonzini00752702013-05-29 12:13:54 +02001759 d->as = as;
1760 as->next_dispatch = d;
1761}
1762
1763static void mem_commit(MemoryListener *listener)
1764{
1765 AddressSpace *as = container_of(listener, AddressSpace, dispatch_listener);
Paolo Bonzini0475d942013-05-29 12:28:21 +02001766 AddressSpaceDispatch *cur = as->dispatch;
1767 AddressSpaceDispatch *next = as->next_dispatch;
Avi Kivityac1970f2012-10-03 16:22:53 +02001768
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +02001769 phys_page_compact_all(next, next->map.nodes_nb);
Michael S. Tsirkinb35ba302013-11-11 17:52:07 +02001770
Paolo Bonzini0475d942013-05-29 12:28:21 +02001771 as->dispatch = next;
Avi Kivityac1970f2012-10-03 16:22:53 +02001772
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +02001773 if (cur) {
1774 phys_sections_free(&cur->map);
1775 g_free(cur);
1776 }
Paolo Bonzini9affd6f2013-05-29 12:09:47 +02001777}
1778
Avi Kivity1d711482012-10-02 18:54:45 +02001779static void tcg_commit(MemoryListener *listener)
Avi Kivity50c1e142012-02-08 21:36:02 +02001780{
Andreas Färber182735e2013-05-29 22:29:20 +02001781 CPUState *cpu;
Avi Kivity117712c2012-02-12 21:23:17 +02001782
1783 /* since each CPU stores ram addresses in its TLB cache, we must
1784 reset the modified entries */
1785 /* XXX: slow ! */
Andreas Färberbdc44642013-06-24 23:50:24 +02001786 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001787 CPUArchState *env = cpu->env_ptr;
1788
Avi Kivity117712c2012-02-12 21:23:17 +02001789 tlb_flush(env, 1);
1790 }
Avi Kivity50c1e142012-02-08 21:36:02 +02001791}
1792
Avi Kivity93632742012-02-08 16:54:16 +02001793static void core_log_global_start(MemoryListener *listener)
1794{
1795 cpu_physical_memory_set_dirty_tracking(1);
1796}
1797
1798static void core_log_global_stop(MemoryListener *listener)
1799{
1800 cpu_physical_memory_set_dirty_tracking(0);
1801}
1802
Avi Kivity93632742012-02-08 16:54:16 +02001803static MemoryListener core_memory_listener = {
Avi Kivity93632742012-02-08 16:54:16 +02001804 .log_global_start = core_log_global_start,
1805 .log_global_stop = core_log_global_stop,
Avi Kivityac1970f2012-10-03 16:22:53 +02001806 .priority = 1,
Avi Kivity93632742012-02-08 16:54:16 +02001807};
1808
Avi Kivity1d711482012-10-02 18:54:45 +02001809static MemoryListener tcg_memory_listener = {
1810 .commit = tcg_commit,
1811};
1812
Avi Kivityac1970f2012-10-03 16:22:53 +02001813void address_space_init_dispatch(AddressSpace *as)
1814{
Paolo Bonzini00752702013-05-29 12:13:54 +02001815 as->dispatch = NULL;
Paolo Bonzini89ae3372013-06-02 10:39:07 +02001816 as->dispatch_listener = (MemoryListener) {
Avi Kivityac1970f2012-10-03 16:22:53 +02001817 .begin = mem_begin,
Paolo Bonzini00752702013-05-29 12:13:54 +02001818 .commit = mem_commit,
Avi Kivityac1970f2012-10-03 16:22:53 +02001819 .region_add = mem_add,
1820 .region_nop = mem_add,
1821 .priority = 0,
1822 };
Paolo Bonzini89ae3372013-06-02 10:39:07 +02001823 memory_listener_register(&as->dispatch_listener, as);
Avi Kivityac1970f2012-10-03 16:22:53 +02001824}
1825
Avi Kivity83f3c252012-10-07 12:59:55 +02001826void address_space_destroy_dispatch(AddressSpace *as)
1827{
1828 AddressSpaceDispatch *d = as->dispatch;
1829
Paolo Bonzini89ae3372013-06-02 10:39:07 +02001830 memory_listener_unregister(&as->dispatch_listener);
Avi Kivity83f3c252012-10-07 12:59:55 +02001831 g_free(d);
1832 as->dispatch = NULL;
1833}
1834
Avi Kivity62152b82011-07-26 14:26:14 +03001835static void memory_map_init(void)
1836{
Anthony Liguori7267c092011-08-20 22:09:37 -05001837 system_memory = g_malloc(sizeof(*system_memory));
Paolo Bonzini03f49952013-11-07 17:14:36 +01001838
Paolo Bonzini57271d62013-11-07 17:14:37 +01001839 memory_region_init(system_memory, NULL, "system", UINT64_MAX);
Alexey Kardashevskiy7dca8042013-04-29 16:25:51 +00001840 address_space_init(&address_space_memory, system_memory, "memory");
Avi Kivity309cb472011-08-08 16:09:03 +03001841
Anthony Liguori7267c092011-08-20 22:09:37 -05001842 system_io = g_malloc(sizeof(*system_io));
Jan Kiszka3bb28b72013-09-02 18:43:30 +02001843 memory_region_init_io(system_io, NULL, &unassigned_io_ops, NULL, "io",
1844 65536);
Alexey Kardashevskiy7dca8042013-04-29 16:25:51 +00001845 address_space_init(&address_space_io, system_io, "I/O");
Avi Kivity93632742012-02-08 16:54:16 +02001846
Avi Kivityf6790af2012-10-02 20:13:51 +02001847 memory_listener_register(&core_memory_listener, &address_space_memory);
liguang26416892013-09-04 14:37:33 +08001848 if (tcg_enabled()) {
1849 memory_listener_register(&tcg_memory_listener, &address_space_memory);
1850 }
Avi Kivity62152b82011-07-26 14:26:14 +03001851}
1852
1853MemoryRegion *get_system_memory(void)
1854{
1855 return system_memory;
1856}
1857
Avi Kivity309cb472011-08-08 16:09:03 +03001858MemoryRegion *get_system_io(void)
1859{
1860 return system_io;
1861}
1862
pbrooke2eef172008-06-08 01:09:01 +00001863#endif /* !defined(CONFIG_USER_ONLY) */
1864
bellard13eb76e2004-01-24 15:23:36 +00001865/* physical memory access (slow version, mainly for debug) */
1866#if defined(CONFIG_USER_ONLY)
Andreas Färberf17ec442013-06-29 19:40:58 +02001867int cpu_memory_rw_debug(CPUState *cpu, target_ulong addr,
Paul Brooka68fe892010-03-01 00:08:59 +00001868 uint8_t *buf, int len, int is_write)
bellard13eb76e2004-01-24 15:23:36 +00001869{
1870 int l, flags;
1871 target_ulong page;
pbrook53a59602006-03-25 19:31:22 +00001872 void * p;
bellard13eb76e2004-01-24 15:23:36 +00001873
1874 while (len > 0) {
1875 page = addr & TARGET_PAGE_MASK;
1876 l = (page + TARGET_PAGE_SIZE) - addr;
1877 if (l > len)
1878 l = len;
1879 flags = page_get_flags(page);
1880 if (!(flags & PAGE_VALID))
Paul Brooka68fe892010-03-01 00:08:59 +00001881 return -1;
bellard13eb76e2004-01-24 15:23:36 +00001882 if (is_write) {
1883 if (!(flags & PAGE_WRITE))
Paul Brooka68fe892010-03-01 00:08:59 +00001884 return -1;
bellard579a97f2007-11-11 14:26:47 +00001885 /* XXX: this code should not depend on lock_user */
aurel3272fb7da2008-04-27 23:53:45 +00001886 if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
Paul Brooka68fe892010-03-01 00:08:59 +00001887 return -1;
aurel3272fb7da2008-04-27 23:53:45 +00001888 memcpy(p, buf, l);
1889 unlock_user(p, addr, l);
bellard13eb76e2004-01-24 15:23:36 +00001890 } else {
1891 if (!(flags & PAGE_READ))
Paul Brooka68fe892010-03-01 00:08:59 +00001892 return -1;
bellard579a97f2007-11-11 14:26:47 +00001893 /* XXX: this code should not depend on lock_user */
aurel3272fb7da2008-04-27 23:53:45 +00001894 if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
Paul Brooka68fe892010-03-01 00:08:59 +00001895 return -1;
aurel3272fb7da2008-04-27 23:53:45 +00001896 memcpy(buf, p, l);
aurel325b257572008-04-28 08:54:59 +00001897 unlock_user(p, addr, 0);
bellard13eb76e2004-01-24 15:23:36 +00001898 }
1899 len -= l;
1900 buf += l;
1901 addr += l;
1902 }
Paul Brooka68fe892010-03-01 00:08:59 +00001903 return 0;
bellard13eb76e2004-01-24 15:23:36 +00001904}
bellard8df1cd02005-01-28 22:37:22 +00001905
bellard13eb76e2004-01-24 15:23:36 +00001906#else
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00001907
Avi Kivitya8170e52012-10-23 12:30:10 +02001908static void invalidate_and_set_dirty(hwaddr addr,
1909 hwaddr length)
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00001910{
1911 if (!cpu_physical_memory_is_dirty(addr)) {
1912 /* invalidate code */
1913 tb_invalidate_phys_page_range(addr, addr + length, 0);
1914 /* set dirty bit */
1915 cpu_physical_memory_set_dirty_flags(addr, (0xff & ~CODE_DIRTY_FLAG));
1916 }
Anthony PERARDe2269392012-10-03 13:49:22 +00001917 xen_modified_memory(addr, length);
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00001918}
1919
Paolo Bonzini2bbfa052013-05-24 12:29:54 +02001920static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
1921{
1922 if (memory_region_is_ram(mr)) {
1923 return !(is_write && mr->readonly);
1924 }
1925 if (memory_region_is_romd(mr)) {
1926 return !is_write;
1927 }
1928
1929 return false;
1930}
1931
Richard Henderson23326162013-07-08 14:55:59 -07001932static int memory_access_size(MemoryRegion *mr, unsigned l, hwaddr addr)
Paolo Bonzini82f25632013-05-24 11:59:43 +02001933{
Paolo Bonzinie1622f42013-07-17 13:17:41 +02001934 unsigned access_size_max = mr->ops->valid.max_access_size;
Richard Henderson23326162013-07-08 14:55:59 -07001935
1936 /* Regions are assumed to support 1-4 byte accesses unless
1937 otherwise specified. */
Richard Henderson23326162013-07-08 14:55:59 -07001938 if (access_size_max == 0) {
1939 access_size_max = 4;
Paolo Bonzini82f25632013-05-24 11:59:43 +02001940 }
Richard Henderson23326162013-07-08 14:55:59 -07001941
1942 /* Bound the maximum access by the alignment of the address. */
1943 if (!mr->ops->impl.unaligned) {
1944 unsigned align_size_max = addr & -addr;
1945 if (align_size_max != 0 && align_size_max < access_size_max) {
1946 access_size_max = align_size_max;
1947 }
1948 }
1949
1950 /* Don't attempt accesses larger than the maximum. */
1951 if (l > access_size_max) {
1952 l = access_size_max;
1953 }
Paolo Bonzini098178f2013-07-29 14:27:39 +02001954 if (l & (l - 1)) {
1955 l = 1 << (qemu_fls(l) - 1);
1956 }
Richard Henderson23326162013-07-08 14:55:59 -07001957
1958 return l;
Paolo Bonzini82f25632013-05-24 11:59:43 +02001959}
1960
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02001961bool address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
Avi Kivityac1970f2012-10-03 16:22:53 +02001962 int len, bool is_write)
bellard13eb76e2004-01-24 15:23:36 +00001963{
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001964 hwaddr l;
bellard13eb76e2004-01-24 15:23:36 +00001965 uint8_t *ptr;
Paolo Bonzini791af8c2013-05-24 16:10:39 +02001966 uint64_t val;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001967 hwaddr addr1;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02001968 MemoryRegion *mr;
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02001969 bool error = false;
ths3b46e622007-09-17 08:09:54 +00001970
bellard13eb76e2004-01-24 15:23:36 +00001971 while (len > 0) {
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001972 l = len;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02001973 mr = address_space_translate(as, addr, &addr1, &l, is_write);
ths3b46e622007-09-17 08:09:54 +00001974
bellard13eb76e2004-01-24 15:23:36 +00001975 if (is_write) {
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02001976 if (!memory_access_is_direct(mr, is_write)) {
1977 l = memory_access_size(mr, l, addr1);
Andreas Färber4917cf42013-05-27 05:17:50 +02001978 /* XXX: could force current_cpu to NULL to avoid
bellard6a00d602005-11-21 23:25:50 +00001979 potential bugs */
Richard Henderson23326162013-07-08 14:55:59 -07001980 switch (l) {
1981 case 8:
1982 /* 64 bit write access */
1983 val = ldq_p(buf);
1984 error |= io_mem_write(mr, addr1, val, 8);
1985 break;
1986 case 4:
bellard1c213d12005-09-03 10:49:04 +00001987 /* 32 bit write access */
bellardc27004e2005-01-03 23:35:10 +00001988 val = ldl_p(buf);
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02001989 error |= io_mem_write(mr, addr1, val, 4);
Richard Henderson23326162013-07-08 14:55:59 -07001990 break;
1991 case 2:
bellard1c213d12005-09-03 10:49:04 +00001992 /* 16 bit write access */
bellardc27004e2005-01-03 23:35:10 +00001993 val = lduw_p(buf);
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02001994 error |= io_mem_write(mr, addr1, val, 2);
Richard Henderson23326162013-07-08 14:55:59 -07001995 break;
1996 case 1:
bellard1c213d12005-09-03 10:49:04 +00001997 /* 8 bit write access */
bellardc27004e2005-01-03 23:35:10 +00001998 val = ldub_p(buf);
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02001999 error |= io_mem_write(mr, addr1, val, 1);
Richard Henderson23326162013-07-08 14:55:59 -07002000 break;
2001 default:
2002 abort();
bellard13eb76e2004-01-24 15:23:36 +00002003 }
Paolo Bonzini2bbfa052013-05-24 12:29:54 +02002004 } else {
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002005 addr1 += memory_region_get_ram_addr(mr);
bellard13eb76e2004-01-24 15:23:36 +00002006 /* RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00002007 ptr = qemu_get_ram_ptr(addr1);
bellard13eb76e2004-01-24 15:23:36 +00002008 memcpy(ptr, buf, l);
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00002009 invalidate_and_set_dirty(addr1, l);
bellard13eb76e2004-01-24 15:23:36 +00002010 }
2011 } else {
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002012 if (!memory_access_is_direct(mr, is_write)) {
bellard13eb76e2004-01-24 15:23:36 +00002013 /* I/O case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002014 l = memory_access_size(mr, l, addr1);
Richard Henderson23326162013-07-08 14:55:59 -07002015 switch (l) {
2016 case 8:
2017 /* 64 bit read access */
2018 error |= io_mem_read(mr, addr1, &val, 8);
2019 stq_p(buf, val);
2020 break;
2021 case 4:
bellard13eb76e2004-01-24 15:23:36 +00002022 /* 32 bit read access */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002023 error |= io_mem_read(mr, addr1, &val, 4);
bellardc27004e2005-01-03 23:35:10 +00002024 stl_p(buf, val);
Richard Henderson23326162013-07-08 14:55:59 -07002025 break;
2026 case 2:
bellard13eb76e2004-01-24 15:23:36 +00002027 /* 16 bit read access */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002028 error |= io_mem_read(mr, addr1, &val, 2);
bellardc27004e2005-01-03 23:35:10 +00002029 stw_p(buf, val);
Richard Henderson23326162013-07-08 14:55:59 -07002030 break;
2031 case 1:
bellard1c213d12005-09-03 10:49:04 +00002032 /* 8 bit read access */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002033 error |= io_mem_read(mr, addr1, &val, 1);
bellardc27004e2005-01-03 23:35:10 +00002034 stb_p(buf, val);
Richard Henderson23326162013-07-08 14:55:59 -07002035 break;
2036 default:
2037 abort();
bellard13eb76e2004-01-24 15:23:36 +00002038 }
2039 } else {
2040 /* RAM case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002041 ptr = qemu_get_ram_ptr(mr->ram_addr + addr1);
Avi Kivityf3705d52012-03-08 16:16:34 +02002042 memcpy(buf, ptr, l);
bellard13eb76e2004-01-24 15:23:36 +00002043 }
2044 }
2045 len -= l;
2046 buf += l;
2047 addr += l;
2048 }
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02002049
2050 return error;
bellard13eb76e2004-01-24 15:23:36 +00002051}
bellard8df1cd02005-01-28 22:37:22 +00002052
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02002053bool address_space_write(AddressSpace *as, hwaddr addr,
Avi Kivityac1970f2012-10-03 16:22:53 +02002054 const uint8_t *buf, int len)
2055{
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02002056 return address_space_rw(as, addr, (uint8_t *)buf, len, true);
Avi Kivityac1970f2012-10-03 16:22:53 +02002057}
2058
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02002059bool address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len)
Avi Kivityac1970f2012-10-03 16:22:53 +02002060{
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02002061 return address_space_rw(as, addr, buf, len, false);
Avi Kivityac1970f2012-10-03 16:22:53 +02002062}
2063
2064
Avi Kivitya8170e52012-10-23 12:30:10 +02002065void cpu_physical_memory_rw(hwaddr addr, uint8_t *buf,
Avi Kivityac1970f2012-10-03 16:22:53 +02002066 int len, int is_write)
2067{
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02002068 address_space_rw(&address_space_memory, addr, buf, len, is_write);
Avi Kivityac1970f2012-10-03 16:22:53 +02002069}
2070
Alexander Graf582b55a2013-12-11 14:17:44 +01002071enum write_rom_type {
2072 WRITE_DATA,
2073 FLUSH_CACHE,
2074};
2075
2076static inline void cpu_physical_memory_write_rom_internal(
2077 hwaddr addr, const uint8_t *buf, int len, enum write_rom_type type)
bellardd0ecd2a2006-04-23 17:14:48 +00002078{
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002079 hwaddr l;
bellardd0ecd2a2006-04-23 17:14:48 +00002080 uint8_t *ptr;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002081 hwaddr addr1;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002082 MemoryRegion *mr;
ths3b46e622007-09-17 08:09:54 +00002083
bellardd0ecd2a2006-04-23 17:14:48 +00002084 while (len > 0) {
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002085 l = len;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002086 mr = address_space_translate(&address_space_memory,
2087 addr, &addr1, &l, true);
ths3b46e622007-09-17 08:09:54 +00002088
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002089 if (!(memory_region_is_ram(mr) ||
2090 memory_region_is_romd(mr))) {
bellardd0ecd2a2006-04-23 17:14:48 +00002091 /* do nothing */
2092 } else {
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002093 addr1 += memory_region_get_ram_addr(mr);
bellardd0ecd2a2006-04-23 17:14:48 +00002094 /* ROM/RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00002095 ptr = qemu_get_ram_ptr(addr1);
Alexander Graf582b55a2013-12-11 14:17:44 +01002096 switch (type) {
2097 case WRITE_DATA:
2098 memcpy(ptr, buf, l);
2099 invalidate_and_set_dirty(addr1, l);
2100 break;
2101 case FLUSH_CACHE:
2102 flush_icache_range((uintptr_t)ptr, (uintptr_t)ptr + l);
2103 break;
2104 }
bellardd0ecd2a2006-04-23 17:14:48 +00002105 }
2106 len -= l;
2107 buf += l;
2108 addr += l;
2109 }
2110}
2111
Alexander Graf582b55a2013-12-11 14:17:44 +01002112/* used for ROM loading : can write in RAM and ROM */
2113void cpu_physical_memory_write_rom(hwaddr addr,
2114 const uint8_t *buf, int len)
2115{
2116 cpu_physical_memory_write_rom_internal(addr, buf, len, WRITE_DATA);
2117}
2118
2119void cpu_flush_icache_range(hwaddr start, int len)
2120{
2121 /*
2122 * This function should do the same thing as an icache flush that was
2123 * triggered from within the guest. For TCG we are always cache coherent,
2124 * so there is no need to flush anything. For KVM / Xen we need to flush
2125 * the host's instruction cache at least.
2126 */
2127 if (tcg_enabled()) {
2128 return;
2129 }
2130
2131 cpu_physical_memory_write_rom_internal(start, NULL, len, FLUSH_CACHE);
2132}
2133
aliguori6d16c2f2009-01-22 16:59:11 +00002134typedef struct {
Paolo Bonzinid3e71552013-06-28 17:33:29 +02002135 MemoryRegion *mr;
aliguori6d16c2f2009-01-22 16:59:11 +00002136 void *buffer;
Avi Kivitya8170e52012-10-23 12:30:10 +02002137 hwaddr addr;
2138 hwaddr len;
aliguori6d16c2f2009-01-22 16:59:11 +00002139} BounceBuffer;
2140
2141static BounceBuffer bounce;
2142
aliguoriba223c22009-01-22 16:59:16 +00002143typedef struct MapClient {
2144 void *opaque;
2145 void (*callback)(void *opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002146 QLIST_ENTRY(MapClient) link;
aliguoriba223c22009-01-22 16:59:16 +00002147} MapClient;
2148
Blue Swirl72cf2d42009-09-12 07:36:22 +00002149static QLIST_HEAD(map_client_list, MapClient) map_client_list
2150 = QLIST_HEAD_INITIALIZER(map_client_list);
aliguoriba223c22009-01-22 16:59:16 +00002151
2152void *cpu_register_map_client(void *opaque, void (*callback)(void *opaque))
2153{
Anthony Liguori7267c092011-08-20 22:09:37 -05002154 MapClient *client = g_malloc(sizeof(*client));
aliguoriba223c22009-01-22 16:59:16 +00002155
2156 client->opaque = opaque;
2157 client->callback = callback;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002158 QLIST_INSERT_HEAD(&map_client_list, client, link);
aliguoriba223c22009-01-22 16:59:16 +00002159 return client;
2160}
2161
Blue Swirl8b9c99d2012-10-28 11:04:51 +00002162static void cpu_unregister_map_client(void *_client)
aliguoriba223c22009-01-22 16:59:16 +00002163{
2164 MapClient *client = (MapClient *)_client;
2165
Blue Swirl72cf2d42009-09-12 07:36:22 +00002166 QLIST_REMOVE(client, link);
Anthony Liguori7267c092011-08-20 22:09:37 -05002167 g_free(client);
aliguoriba223c22009-01-22 16:59:16 +00002168}
2169
2170static void cpu_notify_map_clients(void)
2171{
2172 MapClient *client;
2173
Blue Swirl72cf2d42009-09-12 07:36:22 +00002174 while (!QLIST_EMPTY(&map_client_list)) {
2175 client = QLIST_FIRST(&map_client_list);
aliguoriba223c22009-01-22 16:59:16 +00002176 client->callback(client->opaque);
Isaku Yamahata34d5e942009-06-26 18:57:18 +09002177 cpu_unregister_map_client(client);
aliguoriba223c22009-01-22 16:59:16 +00002178 }
2179}
2180
Paolo Bonzini51644ab2013-04-11 15:40:59 +02002181bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write)
2182{
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002183 MemoryRegion *mr;
Paolo Bonzini51644ab2013-04-11 15:40:59 +02002184 hwaddr l, xlat;
2185
2186 while (len > 0) {
2187 l = len;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002188 mr = address_space_translate(as, addr, &xlat, &l, is_write);
2189 if (!memory_access_is_direct(mr, is_write)) {
2190 l = memory_access_size(mr, l, addr);
2191 if (!memory_region_access_valid(mr, xlat, l, is_write)) {
Paolo Bonzini51644ab2013-04-11 15:40:59 +02002192 return false;
2193 }
2194 }
2195
2196 len -= l;
2197 addr += l;
2198 }
2199 return true;
2200}
2201
aliguori6d16c2f2009-01-22 16:59:11 +00002202/* Map a physical memory region into a host virtual address.
2203 * May map a subset of the requested range, given by and returned in *plen.
2204 * May return NULL if resources needed to perform the mapping are exhausted.
2205 * Use only for reads OR writes - not for read-modify-write operations.
aliguoriba223c22009-01-22 16:59:16 +00002206 * Use cpu_register_map_client() to know when retrying the map operation is
2207 * likely to succeed.
aliguori6d16c2f2009-01-22 16:59:11 +00002208 */
Avi Kivityac1970f2012-10-03 16:22:53 +02002209void *address_space_map(AddressSpace *as,
Avi Kivitya8170e52012-10-23 12:30:10 +02002210 hwaddr addr,
2211 hwaddr *plen,
Avi Kivityac1970f2012-10-03 16:22:53 +02002212 bool is_write)
aliguori6d16c2f2009-01-22 16:59:11 +00002213{
Avi Kivitya8170e52012-10-23 12:30:10 +02002214 hwaddr len = *plen;
Paolo Bonzinie3127ae2013-06-28 17:29:27 +02002215 hwaddr done = 0;
2216 hwaddr l, xlat, base;
2217 MemoryRegion *mr, *this_mr;
2218 ram_addr_t raddr;
aliguori6d16c2f2009-01-22 16:59:11 +00002219
Paolo Bonzinie3127ae2013-06-28 17:29:27 +02002220 if (len == 0) {
2221 return NULL;
2222 }
aliguori6d16c2f2009-01-22 16:59:11 +00002223
Paolo Bonzinie3127ae2013-06-28 17:29:27 +02002224 l = len;
2225 mr = address_space_translate(as, addr, &xlat, &l, is_write);
2226 if (!memory_access_is_direct(mr, is_write)) {
2227 if (bounce.buffer) {
2228 return NULL;
aliguori6d16c2f2009-01-22 16:59:11 +00002229 }
Kevin Wolfe85d9db2013-07-22 14:30:23 +02002230 /* Avoid unbounded allocations */
2231 l = MIN(l, TARGET_PAGE_SIZE);
2232 bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, l);
Paolo Bonzinie3127ae2013-06-28 17:29:27 +02002233 bounce.addr = addr;
2234 bounce.len = l;
Paolo Bonzinid3e71552013-06-28 17:33:29 +02002235
2236 memory_region_ref(mr);
2237 bounce.mr = mr;
Paolo Bonzinie3127ae2013-06-28 17:29:27 +02002238 if (!is_write) {
2239 address_space_read(as, addr, bounce.buffer, l);
Stefano Stabellini8ab934f2011-06-27 18:26:06 +01002240 }
aliguori6d16c2f2009-01-22 16:59:11 +00002241
Paolo Bonzinie3127ae2013-06-28 17:29:27 +02002242 *plen = l;
2243 return bounce.buffer;
2244 }
2245
2246 base = xlat;
2247 raddr = memory_region_get_ram_addr(mr);
2248
2249 for (;;) {
aliguori6d16c2f2009-01-22 16:59:11 +00002250 len -= l;
2251 addr += l;
Paolo Bonzinie3127ae2013-06-28 17:29:27 +02002252 done += l;
2253 if (len == 0) {
2254 break;
2255 }
2256
2257 l = len;
2258 this_mr = address_space_translate(as, addr, &xlat, &l, is_write);
2259 if (this_mr != mr || xlat != base + done) {
2260 break;
2261 }
aliguori6d16c2f2009-01-22 16:59:11 +00002262 }
Paolo Bonzinie3127ae2013-06-28 17:29:27 +02002263
Paolo Bonzinid3e71552013-06-28 17:33:29 +02002264 memory_region_ref(mr);
Paolo Bonzinie3127ae2013-06-28 17:29:27 +02002265 *plen = done;
2266 return qemu_ram_ptr_length(raddr + base, plen);
aliguori6d16c2f2009-01-22 16:59:11 +00002267}
2268
Avi Kivityac1970f2012-10-03 16:22:53 +02002269/* Unmaps a memory region previously mapped by address_space_map().
aliguori6d16c2f2009-01-22 16:59:11 +00002270 * Will also mark the memory as dirty if is_write == 1. access_len gives
2271 * the amount of memory that was actually read or written by the caller.
2272 */
Avi Kivitya8170e52012-10-23 12:30:10 +02002273void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
2274 int is_write, hwaddr access_len)
aliguori6d16c2f2009-01-22 16:59:11 +00002275{
2276 if (buffer != bounce.buffer) {
Paolo Bonzinid3e71552013-06-28 17:33:29 +02002277 MemoryRegion *mr;
2278 ram_addr_t addr1;
2279
2280 mr = qemu_ram_addr_from_host(buffer, &addr1);
2281 assert(mr != NULL);
aliguori6d16c2f2009-01-22 16:59:11 +00002282 if (is_write) {
aliguori6d16c2f2009-01-22 16:59:11 +00002283 while (access_len) {
2284 unsigned l;
2285 l = TARGET_PAGE_SIZE;
2286 if (l > access_len)
2287 l = access_len;
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00002288 invalidate_and_set_dirty(addr1, l);
aliguori6d16c2f2009-01-22 16:59:11 +00002289 addr1 += l;
2290 access_len -= l;
2291 }
2292 }
Jan Kiszka868bb332011-06-21 22:59:09 +02002293 if (xen_enabled()) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02002294 xen_invalidate_map_cache_entry(buffer);
Anthony PERARD050a0dd2010-09-16 13:57:49 +01002295 }
Paolo Bonzinid3e71552013-06-28 17:33:29 +02002296 memory_region_unref(mr);
aliguori6d16c2f2009-01-22 16:59:11 +00002297 return;
2298 }
2299 if (is_write) {
Avi Kivityac1970f2012-10-03 16:22:53 +02002300 address_space_write(as, bounce.addr, bounce.buffer, access_len);
aliguori6d16c2f2009-01-22 16:59:11 +00002301 }
Herve Poussineauf8a83242010-01-24 21:23:56 +00002302 qemu_vfree(bounce.buffer);
aliguori6d16c2f2009-01-22 16:59:11 +00002303 bounce.buffer = NULL;
Paolo Bonzinid3e71552013-06-28 17:33:29 +02002304 memory_region_unref(bounce.mr);
aliguoriba223c22009-01-22 16:59:16 +00002305 cpu_notify_map_clients();
aliguori6d16c2f2009-01-22 16:59:11 +00002306}
bellardd0ecd2a2006-04-23 17:14:48 +00002307
Avi Kivitya8170e52012-10-23 12:30:10 +02002308void *cpu_physical_memory_map(hwaddr addr,
2309 hwaddr *plen,
Avi Kivityac1970f2012-10-03 16:22:53 +02002310 int is_write)
2311{
2312 return address_space_map(&address_space_memory, addr, plen, is_write);
2313}
2314
Avi Kivitya8170e52012-10-23 12:30:10 +02002315void cpu_physical_memory_unmap(void *buffer, hwaddr len,
2316 int is_write, hwaddr access_len)
Avi Kivityac1970f2012-10-03 16:22:53 +02002317{
2318 return address_space_unmap(&address_space_memory, buffer, len, is_write, access_len);
2319}
2320
bellard8df1cd02005-01-28 22:37:22 +00002321/* warning: addr must be aligned */
Avi Kivitya8170e52012-10-23 12:30:10 +02002322static inline uint32_t ldl_phys_internal(hwaddr addr,
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002323 enum device_endian endian)
bellard8df1cd02005-01-28 22:37:22 +00002324{
bellard8df1cd02005-01-28 22:37:22 +00002325 uint8_t *ptr;
Paolo Bonzini791af8c2013-05-24 16:10:39 +02002326 uint64_t val;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002327 MemoryRegion *mr;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002328 hwaddr l = 4;
2329 hwaddr addr1;
bellard8df1cd02005-01-28 22:37:22 +00002330
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002331 mr = address_space_translate(&address_space_memory, addr, &addr1, &l,
2332 false);
2333 if (l < 4 || !memory_access_is_direct(mr, false)) {
bellard8df1cd02005-01-28 22:37:22 +00002334 /* I/O case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002335 io_mem_read(mr, addr1, &val, 4);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002336#if defined(TARGET_WORDS_BIGENDIAN)
2337 if (endian == DEVICE_LITTLE_ENDIAN) {
2338 val = bswap32(val);
2339 }
2340#else
2341 if (endian == DEVICE_BIG_ENDIAN) {
2342 val = bswap32(val);
2343 }
2344#endif
bellard8df1cd02005-01-28 22:37:22 +00002345 } else {
2346 /* RAM case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002347 ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(mr)
Avi Kivity06ef3522012-02-13 16:11:22 +02002348 & TARGET_PAGE_MASK)
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002349 + addr1);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002350 switch (endian) {
2351 case DEVICE_LITTLE_ENDIAN:
2352 val = ldl_le_p(ptr);
2353 break;
2354 case DEVICE_BIG_ENDIAN:
2355 val = ldl_be_p(ptr);
2356 break;
2357 default:
2358 val = ldl_p(ptr);
2359 break;
2360 }
bellard8df1cd02005-01-28 22:37:22 +00002361 }
2362 return val;
2363}
2364
Avi Kivitya8170e52012-10-23 12:30:10 +02002365uint32_t ldl_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002366{
2367 return ldl_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
2368}
2369
Avi Kivitya8170e52012-10-23 12:30:10 +02002370uint32_t ldl_le_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002371{
2372 return ldl_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
2373}
2374
Avi Kivitya8170e52012-10-23 12:30:10 +02002375uint32_t ldl_be_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002376{
2377 return ldl_phys_internal(addr, DEVICE_BIG_ENDIAN);
2378}
2379
bellard84b7b8e2005-11-28 21:19:04 +00002380/* warning: addr must be aligned */
Avi Kivitya8170e52012-10-23 12:30:10 +02002381static inline uint64_t ldq_phys_internal(hwaddr addr,
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002382 enum device_endian endian)
bellard84b7b8e2005-11-28 21:19:04 +00002383{
bellard84b7b8e2005-11-28 21:19:04 +00002384 uint8_t *ptr;
2385 uint64_t val;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002386 MemoryRegion *mr;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002387 hwaddr l = 8;
2388 hwaddr addr1;
bellard84b7b8e2005-11-28 21:19:04 +00002389
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002390 mr = address_space_translate(&address_space_memory, addr, &addr1, &l,
2391 false);
2392 if (l < 8 || !memory_access_is_direct(mr, false)) {
bellard84b7b8e2005-11-28 21:19:04 +00002393 /* I/O case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002394 io_mem_read(mr, addr1, &val, 8);
Paolo Bonzini968a5622013-05-24 17:58:37 +02002395#if defined(TARGET_WORDS_BIGENDIAN)
2396 if (endian == DEVICE_LITTLE_ENDIAN) {
2397 val = bswap64(val);
2398 }
2399#else
2400 if (endian == DEVICE_BIG_ENDIAN) {
2401 val = bswap64(val);
2402 }
2403#endif
bellard84b7b8e2005-11-28 21:19:04 +00002404 } else {
2405 /* RAM case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002406 ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(mr)
Avi Kivity06ef3522012-02-13 16:11:22 +02002407 & TARGET_PAGE_MASK)
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002408 + addr1);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002409 switch (endian) {
2410 case DEVICE_LITTLE_ENDIAN:
2411 val = ldq_le_p(ptr);
2412 break;
2413 case DEVICE_BIG_ENDIAN:
2414 val = ldq_be_p(ptr);
2415 break;
2416 default:
2417 val = ldq_p(ptr);
2418 break;
2419 }
bellard84b7b8e2005-11-28 21:19:04 +00002420 }
2421 return val;
2422}
2423
Avi Kivitya8170e52012-10-23 12:30:10 +02002424uint64_t ldq_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002425{
2426 return ldq_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
2427}
2428
Avi Kivitya8170e52012-10-23 12:30:10 +02002429uint64_t ldq_le_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002430{
2431 return ldq_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
2432}
2433
Avi Kivitya8170e52012-10-23 12:30:10 +02002434uint64_t ldq_be_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002435{
2436 return ldq_phys_internal(addr, DEVICE_BIG_ENDIAN);
2437}
2438
bellardaab33092005-10-30 20:48:42 +00002439/* XXX: optimize */
Avi Kivitya8170e52012-10-23 12:30:10 +02002440uint32_t ldub_phys(hwaddr addr)
bellardaab33092005-10-30 20:48:42 +00002441{
2442 uint8_t val;
2443 cpu_physical_memory_read(addr, &val, 1);
2444 return val;
2445}
2446
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002447/* warning: addr must be aligned */
Avi Kivitya8170e52012-10-23 12:30:10 +02002448static inline uint32_t lduw_phys_internal(hwaddr addr,
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002449 enum device_endian endian)
bellardaab33092005-10-30 20:48:42 +00002450{
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002451 uint8_t *ptr;
2452 uint64_t val;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002453 MemoryRegion *mr;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002454 hwaddr l = 2;
2455 hwaddr addr1;
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002456
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002457 mr = address_space_translate(&address_space_memory, addr, &addr1, &l,
2458 false);
2459 if (l < 2 || !memory_access_is_direct(mr, false)) {
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002460 /* I/O case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002461 io_mem_read(mr, addr1, &val, 2);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002462#if defined(TARGET_WORDS_BIGENDIAN)
2463 if (endian == DEVICE_LITTLE_ENDIAN) {
2464 val = bswap16(val);
2465 }
2466#else
2467 if (endian == DEVICE_BIG_ENDIAN) {
2468 val = bswap16(val);
2469 }
2470#endif
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002471 } else {
2472 /* RAM case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002473 ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(mr)
Avi Kivity06ef3522012-02-13 16:11:22 +02002474 & TARGET_PAGE_MASK)
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002475 + addr1);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002476 switch (endian) {
2477 case DEVICE_LITTLE_ENDIAN:
2478 val = lduw_le_p(ptr);
2479 break;
2480 case DEVICE_BIG_ENDIAN:
2481 val = lduw_be_p(ptr);
2482 break;
2483 default:
2484 val = lduw_p(ptr);
2485 break;
2486 }
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002487 }
2488 return val;
bellardaab33092005-10-30 20:48:42 +00002489}
2490
Avi Kivitya8170e52012-10-23 12:30:10 +02002491uint32_t lduw_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002492{
2493 return lduw_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
2494}
2495
Avi Kivitya8170e52012-10-23 12:30:10 +02002496uint32_t lduw_le_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002497{
2498 return lduw_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
2499}
2500
Avi Kivitya8170e52012-10-23 12:30:10 +02002501uint32_t lduw_be_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002502{
2503 return lduw_phys_internal(addr, DEVICE_BIG_ENDIAN);
2504}
2505
bellard8df1cd02005-01-28 22:37:22 +00002506/* warning: addr must be aligned. The ram page is not masked as dirty
2507 and the code inside is not invalidated. It is useful if the dirty
2508 bits are used to track modified PTEs */
Avi Kivitya8170e52012-10-23 12:30:10 +02002509void stl_phys_notdirty(hwaddr addr, uint32_t val)
bellard8df1cd02005-01-28 22:37:22 +00002510{
bellard8df1cd02005-01-28 22:37:22 +00002511 uint8_t *ptr;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002512 MemoryRegion *mr;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002513 hwaddr l = 4;
2514 hwaddr addr1;
bellard8df1cd02005-01-28 22:37:22 +00002515
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002516 mr = address_space_translate(&address_space_memory, addr, &addr1, &l,
2517 true);
2518 if (l < 4 || !memory_access_is_direct(mr, true)) {
2519 io_mem_write(mr, addr1, val, 4);
bellard8df1cd02005-01-28 22:37:22 +00002520 } else {
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002521 addr1 += memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK;
pbrook5579c7f2009-04-11 14:47:08 +00002522 ptr = qemu_get_ram_ptr(addr1);
bellard8df1cd02005-01-28 22:37:22 +00002523 stl_p(ptr, val);
aliguori74576192008-10-06 14:02:03 +00002524
2525 if (unlikely(in_migration)) {
2526 if (!cpu_physical_memory_is_dirty(addr1)) {
2527 /* invalidate code */
2528 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
2529 /* set dirty bit */
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09002530 cpu_physical_memory_set_dirty_flags(
2531 addr1, (0xff & ~CODE_DIRTY_FLAG));
aliguori74576192008-10-06 14:02:03 +00002532 }
2533 }
bellard8df1cd02005-01-28 22:37:22 +00002534 }
2535}
2536
2537/* warning: addr must be aligned */
Avi Kivitya8170e52012-10-23 12:30:10 +02002538static inline void stl_phys_internal(hwaddr addr, uint32_t val,
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002539 enum device_endian endian)
bellard8df1cd02005-01-28 22:37:22 +00002540{
bellard8df1cd02005-01-28 22:37:22 +00002541 uint8_t *ptr;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002542 MemoryRegion *mr;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002543 hwaddr l = 4;
2544 hwaddr addr1;
bellard8df1cd02005-01-28 22:37:22 +00002545
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002546 mr = address_space_translate(&address_space_memory, addr, &addr1, &l,
2547 true);
2548 if (l < 4 || !memory_access_is_direct(mr, true)) {
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002549#if defined(TARGET_WORDS_BIGENDIAN)
2550 if (endian == DEVICE_LITTLE_ENDIAN) {
2551 val = bswap32(val);
2552 }
2553#else
2554 if (endian == DEVICE_BIG_ENDIAN) {
2555 val = bswap32(val);
2556 }
2557#endif
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002558 io_mem_write(mr, addr1, val, 4);
bellard8df1cd02005-01-28 22:37:22 +00002559 } else {
bellard8df1cd02005-01-28 22:37:22 +00002560 /* RAM case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002561 addr1 += memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK;
pbrook5579c7f2009-04-11 14:47:08 +00002562 ptr = qemu_get_ram_ptr(addr1);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002563 switch (endian) {
2564 case DEVICE_LITTLE_ENDIAN:
2565 stl_le_p(ptr, val);
2566 break;
2567 case DEVICE_BIG_ENDIAN:
2568 stl_be_p(ptr, val);
2569 break;
2570 default:
2571 stl_p(ptr, val);
2572 break;
2573 }
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00002574 invalidate_and_set_dirty(addr1, 4);
bellard8df1cd02005-01-28 22:37:22 +00002575 }
2576}
2577
Avi Kivitya8170e52012-10-23 12:30:10 +02002578void stl_phys(hwaddr addr, uint32_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002579{
2580 stl_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN);
2581}
2582
Avi Kivitya8170e52012-10-23 12:30:10 +02002583void stl_le_phys(hwaddr addr, uint32_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002584{
2585 stl_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN);
2586}
2587
Avi Kivitya8170e52012-10-23 12:30:10 +02002588void stl_be_phys(hwaddr addr, uint32_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002589{
2590 stl_phys_internal(addr, val, DEVICE_BIG_ENDIAN);
2591}
2592
bellardaab33092005-10-30 20:48:42 +00002593/* XXX: optimize */
Avi Kivitya8170e52012-10-23 12:30:10 +02002594void stb_phys(hwaddr addr, uint32_t val)
bellardaab33092005-10-30 20:48:42 +00002595{
2596 uint8_t v = val;
2597 cpu_physical_memory_write(addr, &v, 1);
2598}
2599
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002600/* warning: addr must be aligned */
Avi Kivitya8170e52012-10-23 12:30:10 +02002601static inline void stw_phys_internal(hwaddr addr, uint32_t val,
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002602 enum device_endian endian)
bellardaab33092005-10-30 20:48:42 +00002603{
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002604 uint8_t *ptr;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002605 MemoryRegion *mr;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002606 hwaddr l = 2;
2607 hwaddr addr1;
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002608
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002609 mr = address_space_translate(&address_space_memory, addr, &addr1, &l,
2610 true);
2611 if (l < 2 || !memory_access_is_direct(mr, true)) {
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002612#if defined(TARGET_WORDS_BIGENDIAN)
2613 if (endian == DEVICE_LITTLE_ENDIAN) {
2614 val = bswap16(val);
2615 }
2616#else
2617 if (endian == DEVICE_BIG_ENDIAN) {
2618 val = bswap16(val);
2619 }
2620#endif
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002621 io_mem_write(mr, addr1, val, 2);
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002622 } else {
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002623 /* RAM case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002624 addr1 += memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK;
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002625 ptr = qemu_get_ram_ptr(addr1);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002626 switch (endian) {
2627 case DEVICE_LITTLE_ENDIAN:
2628 stw_le_p(ptr, val);
2629 break;
2630 case DEVICE_BIG_ENDIAN:
2631 stw_be_p(ptr, val);
2632 break;
2633 default:
2634 stw_p(ptr, val);
2635 break;
2636 }
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00002637 invalidate_and_set_dirty(addr1, 2);
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002638 }
bellardaab33092005-10-30 20:48:42 +00002639}
2640
Avi Kivitya8170e52012-10-23 12:30:10 +02002641void stw_phys(hwaddr addr, uint32_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002642{
2643 stw_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN);
2644}
2645
Avi Kivitya8170e52012-10-23 12:30:10 +02002646void stw_le_phys(hwaddr addr, uint32_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002647{
2648 stw_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN);
2649}
2650
Avi Kivitya8170e52012-10-23 12:30:10 +02002651void stw_be_phys(hwaddr addr, uint32_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002652{
2653 stw_phys_internal(addr, val, DEVICE_BIG_ENDIAN);
2654}
2655
bellardaab33092005-10-30 20:48:42 +00002656/* XXX: optimize */
Avi Kivitya8170e52012-10-23 12:30:10 +02002657void stq_phys(hwaddr addr, uint64_t val)
bellardaab33092005-10-30 20:48:42 +00002658{
2659 val = tswap64(val);
Stefan Weil71d2b722011-03-26 21:06:56 +01002660 cpu_physical_memory_write(addr, &val, 8);
bellardaab33092005-10-30 20:48:42 +00002661}
2662
Avi Kivitya8170e52012-10-23 12:30:10 +02002663void stq_le_phys(hwaddr addr, uint64_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002664{
2665 val = cpu_to_le64(val);
2666 cpu_physical_memory_write(addr, &val, 8);
2667}
2668
Avi Kivitya8170e52012-10-23 12:30:10 +02002669void stq_be_phys(hwaddr addr, uint64_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002670{
2671 val = cpu_to_be64(val);
2672 cpu_physical_memory_write(addr, &val, 8);
2673}
2674
aliguori5e2972f2009-03-28 17:51:36 +00002675/* virtual memory access for debug (includes writing to ROM) */
Andreas Färberf17ec442013-06-29 19:40:58 +02002676int cpu_memory_rw_debug(CPUState *cpu, target_ulong addr,
bellardb448f2f2004-02-25 23:24:04 +00002677 uint8_t *buf, int len, int is_write)
bellard13eb76e2004-01-24 15:23:36 +00002678{
2679 int l;
Avi Kivitya8170e52012-10-23 12:30:10 +02002680 hwaddr phys_addr;
j_mayer9b3c35e2007-04-07 11:21:28 +00002681 target_ulong page;
bellard13eb76e2004-01-24 15:23:36 +00002682
2683 while (len > 0) {
2684 page = addr & TARGET_PAGE_MASK;
Andreas Färberf17ec442013-06-29 19:40:58 +02002685 phys_addr = cpu_get_phys_page_debug(cpu, page);
bellard13eb76e2004-01-24 15:23:36 +00002686 /* if no physical page mapped, return an error */
2687 if (phys_addr == -1)
2688 return -1;
2689 l = (page + TARGET_PAGE_SIZE) - addr;
2690 if (l > len)
2691 l = len;
aliguori5e2972f2009-03-28 17:51:36 +00002692 phys_addr += (addr & ~TARGET_PAGE_MASK);
aliguori5e2972f2009-03-28 17:51:36 +00002693 if (is_write)
2694 cpu_physical_memory_write_rom(phys_addr, buf, l);
2695 else
aliguori5e2972f2009-03-28 17:51:36 +00002696 cpu_physical_memory_rw(phys_addr, buf, l, is_write);
bellard13eb76e2004-01-24 15:23:36 +00002697 len -= l;
2698 buf += l;
2699 addr += l;
2700 }
2701 return 0;
2702}
Paul Brooka68fe892010-03-01 00:08:59 +00002703#endif
bellard13eb76e2004-01-24 15:23:36 +00002704
Blue Swirl8e4a4242013-01-06 18:30:17 +00002705#if !defined(CONFIG_USER_ONLY)
2706
2707/*
2708 * A helper function for the _utterly broken_ virtio device model to find out if
2709 * it's running on a big endian machine. Don't do this at home kids!
2710 */
2711bool virtio_is_big_endian(void);
2712bool virtio_is_big_endian(void)
2713{
2714#if defined(TARGET_WORDS_BIGENDIAN)
2715 return true;
2716#else
2717 return false;
2718#endif
2719}
2720
2721#endif
2722
Wen Congyang76f35532012-05-07 12:04:18 +08002723#ifndef CONFIG_USER_ONLY
Avi Kivitya8170e52012-10-23 12:30:10 +02002724bool cpu_physical_memory_is_io(hwaddr phys_addr)
Wen Congyang76f35532012-05-07 12:04:18 +08002725{
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002726 MemoryRegion*mr;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002727 hwaddr l = 1;
Wen Congyang76f35532012-05-07 12:04:18 +08002728
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002729 mr = address_space_translate(&address_space_memory,
2730 phys_addr, &phys_addr, &l, false);
Wen Congyang76f35532012-05-07 12:04:18 +08002731
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002732 return !(memory_region_is_ram(mr) ||
2733 memory_region_is_romd(mr));
Wen Congyang76f35532012-05-07 12:04:18 +08002734}
Michael R. Hinesbd2fa512013-06-25 21:35:34 -04002735
2736void qemu_ram_foreach_block(RAMBlockIterFunc func, void *opaque)
2737{
2738 RAMBlock *block;
2739
2740 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
2741 func(block->host, block->offset, block->length, opaque);
2742 }
2743}
Peter Maydellec3f8c92013-06-27 20:53:38 +01002744#endif