blob: 7e49e8e555e5e63ec4db6d36738e7c422b7128d7 [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{
bellard3a7d9292005-08-21 09:26:42 +00001488 int dirty_flags;
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09001489 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
bellard3a7d9292005-08-21 09:26:42 +00001490 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
Avi Kivity0e0df1e2012-01-02 00:32:15 +02001491 tb_invalidate_phys_page_fast(ram_addr, size);
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09001492 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
bellard3a7d9292005-08-21 09:26:42 +00001493 }
Avi Kivity0e0df1e2012-01-02 00:32:15 +02001494 switch (size) {
1495 case 1:
1496 stb_p(qemu_get_ram_ptr(ram_addr), val);
1497 break;
1498 case 2:
1499 stw_p(qemu_get_ram_ptr(ram_addr), val);
1500 break;
1501 case 4:
1502 stl_p(qemu_get_ram_ptr(ram_addr), val);
1503 break;
1504 default:
1505 abort();
1506 }
bellardf23db162005-08-21 19:12:28 +00001507 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09001508 cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags);
bellardf23db162005-08-21 19:12:28 +00001509 /* we remove the notdirty callback only if the code has been
1510 flushed */
Andreas Färber4917cf42013-05-27 05:17:50 +02001511 if (dirty_flags == 0xff) {
1512 CPUArchState *env = current_cpu->env_ptr;
1513 tlb_set_dirty(env, env->mem_io_vaddr);
1514 }
bellard1ccde1c2004-02-06 19:46:14 +00001515}
1516
Paolo Bonzinib018ddf2013-05-24 14:48:38 +02001517static bool notdirty_mem_accepts(void *opaque, hwaddr addr,
1518 unsigned size, bool is_write)
1519{
1520 return is_write;
1521}
1522
Avi Kivity0e0df1e2012-01-02 00:32:15 +02001523static const MemoryRegionOps notdirty_mem_ops = {
Avi Kivity0e0df1e2012-01-02 00:32:15 +02001524 .write = notdirty_mem_write,
Paolo Bonzinib018ddf2013-05-24 14:48:38 +02001525 .valid.accepts = notdirty_mem_accepts,
Avi Kivity0e0df1e2012-01-02 00:32:15 +02001526 .endianness = DEVICE_NATIVE_ENDIAN,
bellard1ccde1c2004-02-06 19:46:14 +00001527};
1528
pbrook0f459d12008-06-09 00:20:13 +00001529/* Generate a debug exception if a watchpoint has been hit. */
aliguorib4051332008-11-18 20:14:20 +00001530static void check_watchpoint(int offset, int len_mask, int flags)
pbrook0f459d12008-06-09 00:20:13 +00001531{
Andreas Färber4917cf42013-05-27 05:17:50 +02001532 CPUArchState *env = current_cpu->env_ptr;
aliguori06d55cc2008-11-18 20:24:06 +00001533 target_ulong pc, cs_base;
pbrook0f459d12008-06-09 00:20:13 +00001534 target_ulong vaddr;
aliguoria1d1bb32008-11-18 20:07:32 +00001535 CPUWatchpoint *wp;
aliguori06d55cc2008-11-18 20:24:06 +00001536 int cpu_flags;
pbrook0f459d12008-06-09 00:20:13 +00001537
aliguori06d55cc2008-11-18 20:24:06 +00001538 if (env->watchpoint_hit) {
1539 /* We re-entered the check after replacing the TB. Now raise
1540 * the debug interrupt so that is will trigger after the
1541 * current instruction. */
Andreas Färberc3affe52013-01-18 15:03:43 +01001542 cpu_interrupt(ENV_GET_CPU(env), CPU_INTERRUPT_DEBUG);
aliguori06d55cc2008-11-18 20:24:06 +00001543 return;
1544 }
pbrook2e70f6e2008-06-29 01:03:05 +00001545 vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001546 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
aliguorib4051332008-11-18 20:14:20 +00001547 if ((vaddr == (wp->vaddr & len_mask) ||
1548 (vaddr & wp->len_mask) == wp->vaddr) && (wp->flags & flags)) {
aliguori6e140f22008-11-18 20:37:55 +00001549 wp->flags |= BP_WATCHPOINT_HIT;
1550 if (!env->watchpoint_hit) {
1551 env->watchpoint_hit = wp;
Blue Swirl5a316522012-12-02 21:28:09 +00001552 tb_check_watchpoint(env);
aliguori6e140f22008-11-18 20:37:55 +00001553 if (wp->flags & BP_STOP_BEFORE_ACCESS) {
1554 env->exception_index = EXCP_DEBUG;
Max Filippov488d6572012-01-29 02:24:39 +04001555 cpu_loop_exit(env);
aliguori6e140f22008-11-18 20:37:55 +00001556 } else {
1557 cpu_get_tb_cpu_state(env, &pc, &cs_base, &cpu_flags);
1558 tb_gen_code(env, pc, cs_base, cpu_flags, 1);
Max Filippov488d6572012-01-29 02:24:39 +04001559 cpu_resume_from_signal(env, NULL);
aliguori6e140f22008-11-18 20:37:55 +00001560 }
aliguori06d55cc2008-11-18 20:24:06 +00001561 }
aliguori6e140f22008-11-18 20:37:55 +00001562 } else {
1563 wp->flags &= ~BP_WATCHPOINT_HIT;
pbrook0f459d12008-06-09 00:20:13 +00001564 }
1565 }
1566}
1567
pbrook6658ffb2007-03-16 23:58:11 +00001568/* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
1569 so these check for a hit then pass through to the normal out-of-line
1570 phys routines. */
Avi Kivitya8170e52012-10-23 12:30:10 +02001571static uint64_t watch_mem_read(void *opaque, hwaddr addr,
Avi Kivity1ec9b902012-01-02 12:47:48 +02001572 unsigned size)
pbrook6658ffb2007-03-16 23:58:11 +00001573{
Avi Kivity1ec9b902012-01-02 12:47:48 +02001574 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~(size - 1), BP_MEM_READ);
1575 switch (size) {
1576 case 1: return ldub_phys(addr);
1577 case 2: return lduw_phys(addr);
1578 case 4: return ldl_phys(addr);
1579 default: abort();
1580 }
pbrook6658ffb2007-03-16 23:58:11 +00001581}
1582
Avi Kivitya8170e52012-10-23 12:30:10 +02001583static void watch_mem_write(void *opaque, hwaddr addr,
Avi Kivity1ec9b902012-01-02 12:47:48 +02001584 uint64_t val, unsigned size)
pbrook6658ffb2007-03-16 23:58:11 +00001585{
Avi Kivity1ec9b902012-01-02 12:47:48 +02001586 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~(size - 1), BP_MEM_WRITE);
1587 switch (size) {
Max Filippov67364152012-01-29 00:01:40 +04001588 case 1:
1589 stb_phys(addr, val);
1590 break;
1591 case 2:
1592 stw_phys(addr, val);
1593 break;
1594 case 4:
1595 stl_phys(addr, val);
1596 break;
Avi Kivity1ec9b902012-01-02 12:47:48 +02001597 default: abort();
1598 }
pbrook6658ffb2007-03-16 23:58:11 +00001599}
1600
Avi Kivity1ec9b902012-01-02 12:47:48 +02001601static const MemoryRegionOps watch_mem_ops = {
1602 .read = watch_mem_read,
1603 .write = watch_mem_write,
1604 .endianness = DEVICE_NATIVE_ENDIAN,
pbrook6658ffb2007-03-16 23:58:11 +00001605};
pbrook6658ffb2007-03-16 23:58:11 +00001606
Avi Kivitya8170e52012-10-23 12:30:10 +02001607static uint64_t subpage_read(void *opaque, hwaddr addr,
Avi Kivity70c68e42012-01-02 12:32:48 +02001608 unsigned len)
blueswir1db7b5422007-05-26 17:36:03 +00001609{
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001610 subpage_t *subpage = opaque;
1611 uint8_t buf[4];
Paolo Bonzini791af8c2013-05-24 16:10:39 +02001612
blueswir1db7b5422007-05-26 17:36:03 +00001613#if defined(DEBUG_SUBPAGE)
Amos Kong016e9d62013-09-27 09:25:38 +08001614 printf("%s: subpage %p len %u addr " TARGET_FMT_plx "\n", __func__,
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001615 subpage, len, addr);
blueswir1db7b5422007-05-26 17:36:03 +00001616#endif
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001617 address_space_read(subpage->as, addr + subpage->base, buf, len);
1618 switch (len) {
1619 case 1:
1620 return ldub_p(buf);
1621 case 2:
1622 return lduw_p(buf);
1623 case 4:
1624 return ldl_p(buf);
1625 default:
1626 abort();
1627 }
blueswir1db7b5422007-05-26 17:36:03 +00001628}
1629
Avi Kivitya8170e52012-10-23 12:30:10 +02001630static void subpage_write(void *opaque, hwaddr addr,
Avi Kivity70c68e42012-01-02 12:32:48 +02001631 uint64_t value, unsigned len)
blueswir1db7b5422007-05-26 17:36:03 +00001632{
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001633 subpage_t *subpage = opaque;
1634 uint8_t buf[4];
1635
blueswir1db7b5422007-05-26 17:36:03 +00001636#if defined(DEBUG_SUBPAGE)
Amos Kong016e9d62013-09-27 09:25:38 +08001637 printf("%s: subpage %p len %u addr " TARGET_FMT_plx
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001638 " value %"PRIx64"\n",
1639 __func__, subpage, len, addr, value);
blueswir1db7b5422007-05-26 17:36:03 +00001640#endif
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001641 switch (len) {
1642 case 1:
1643 stb_p(buf, value);
1644 break;
1645 case 2:
1646 stw_p(buf, value);
1647 break;
1648 case 4:
1649 stl_p(buf, value);
1650 break;
1651 default:
1652 abort();
1653 }
1654 address_space_write(subpage->as, addr + subpage->base, buf, len);
blueswir1db7b5422007-05-26 17:36:03 +00001655}
1656
Paolo Bonzinic353e4c2013-05-24 14:02:39 +02001657static bool subpage_accepts(void *opaque, hwaddr addr,
Amos Kong016e9d62013-09-27 09:25:38 +08001658 unsigned len, bool is_write)
Paolo Bonzinic353e4c2013-05-24 14:02:39 +02001659{
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001660 subpage_t *subpage = opaque;
Paolo Bonzinic353e4c2013-05-24 14:02:39 +02001661#if defined(DEBUG_SUBPAGE)
Amos Kong016e9d62013-09-27 09:25:38 +08001662 printf("%s: subpage %p %c len %u addr " TARGET_FMT_plx "\n",
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001663 __func__, subpage, is_write ? 'w' : 'r', len, addr);
Paolo Bonzinic353e4c2013-05-24 14:02:39 +02001664#endif
1665
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001666 return address_space_access_valid(subpage->as, addr + subpage->base,
Amos Kong016e9d62013-09-27 09:25:38 +08001667 len, is_write);
Paolo Bonzinic353e4c2013-05-24 14:02:39 +02001668}
1669
Avi Kivity70c68e42012-01-02 12:32:48 +02001670static const MemoryRegionOps subpage_ops = {
1671 .read = subpage_read,
1672 .write = subpage_write,
Paolo Bonzinic353e4c2013-05-24 14:02:39 +02001673 .valid.accepts = subpage_accepts,
Avi Kivity70c68e42012-01-02 12:32:48 +02001674 .endianness = DEVICE_NATIVE_ENDIAN,
blueswir1db7b5422007-05-26 17:36:03 +00001675};
1676
Anthony Liguoric227f092009-10-01 16:12:16 -05001677static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
Avi Kivity5312bd82012-02-12 18:32:55 +02001678 uint16_t section)
blueswir1db7b5422007-05-26 17:36:03 +00001679{
1680 int idx, eidx;
1681
1682 if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
1683 return -1;
1684 idx = SUBPAGE_IDX(start);
1685 eidx = SUBPAGE_IDX(end);
1686#if defined(DEBUG_SUBPAGE)
Amos Kong016e9d62013-09-27 09:25:38 +08001687 printf("%s: %p start %08x end %08x idx %08x eidx %08x section %d\n",
1688 __func__, mmio, start, end, idx, eidx, section);
blueswir1db7b5422007-05-26 17:36:03 +00001689#endif
blueswir1db7b5422007-05-26 17:36:03 +00001690 for (; idx <= eidx; idx++) {
Avi Kivity5312bd82012-02-12 18:32:55 +02001691 mmio->sub_section[idx] = section;
blueswir1db7b5422007-05-26 17:36:03 +00001692 }
1693
1694 return 0;
1695}
1696
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001697static subpage_t *subpage_init(AddressSpace *as, hwaddr base)
blueswir1db7b5422007-05-26 17:36:03 +00001698{
Anthony Liguoric227f092009-10-01 16:12:16 -05001699 subpage_t *mmio;
blueswir1db7b5422007-05-26 17:36:03 +00001700
Anthony Liguori7267c092011-08-20 22:09:37 -05001701 mmio = g_malloc0(sizeof(subpage_t));
aliguori1eec6142009-02-05 22:06:18 +00001702
Jan Kiszkaacc9d802013-05-26 21:55:37 +02001703 mmio->as = as;
aliguori1eec6142009-02-05 22:06:18 +00001704 mmio->base = base;
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -04001705 memory_region_init_io(&mmio->iomem, NULL, &subpage_ops, mmio,
Avi Kivity70c68e42012-01-02 12:32:48 +02001706 "subpage", TARGET_PAGE_SIZE);
Avi Kivityb3b00c72012-01-02 13:20:11 +02001707 mmio->iomem.subpage = true;
blueswir1db7b5422007-05-26 17:36:03 +00001708#if defined(DEBUG_SUBPAGE)
Amos Kong016e9d62013-09-27 09:25:38 +08001709 printf("%s: %p base " TARGET_FMT_plx " len %08x\n", __func__,
1710 mmio, base, TARGET_PAGE_SIZE);
blueswir1db7b5422007-05-26 17:36:03 +00001711#endif
Liu Ping Fanb41aac42013-05-29 11:09:17 +02001712 subpage_register(mmio, 0, TARGET_PAGE_SIZE-1, PHYS_SECTION_UNASSIGNED);
blueswir1db7b5422007-05-26 17:36:03 +00001713
1714 return mmio;
1715}
1716
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +02001717static uint16_t dummy_section(PhysPageMap *map, MemoryRegion *mr)
Avi Kivity5312bd82012-02-12 18:32:55 +02001718{
1719 MemoryRegionSection section = {
1720 .mr = mr,
1721 .offset_within_address_space = 0,
1722 .offset_within_region = 0,
Paolo Bonzini052e87b2013-05-27 10:08:27 +02001723 .size = int128_2_64(),
Avi Kivity5312bd82012-02-12 18:32:55 +02001724 };
1725
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +02001726 return phys_section_add(map, &section);
Avi Kivity5312bd82012-02-12 18:32:55 +02001727}
1728
Avi Kivitya8170e52012-10-23 12:30:10 +02001729MemoryRegion *iotlb_to_region(hwaddr index)
Avi Kivityaa102232012-03-08 17:06:55 +02001730{
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +02001731 return address_space_memory.dispatch->map.sections[
1732 index & ~TARGET_PAGE_MASK].mr;
Avi Kivityaa102232012-03-08 17:06:55 +02001733}
1734
Avi Kivitye9179ce2009-06-14 11:38:52 +03001735static void io_mem_init(void)
1736{
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -04001737 memory_region_init_io(&io_mem_rom, NULL, &unassigned_mem_ops, NULL, "rom", UINT64_MAX);
1738 memory_region_init_io(&io_mem_unassigned, NULL, &unassigned_mem_ops, NULL,
Avi Kivity0e0df1e2012-01-02 00:32:15 +02001739 "unassigned", UINT64_MAX);
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -04001740 memory_region_init_io(&io_mem_notdirty, NULL, &notdirty_mem_ops, NULL,
Avi Kivity0e0df1e2012-01-02 00:32:15 +02001741 "notdirty", UINT64_MAX);
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -04001742 memory_region_init_io(&io_mem_watch, NULL, &watch_mem_ops, NULL,
Avi Kivity1ec9b902012-01-02 12:47:48 +02001743 "watch", UINT64_MAX);
Avi Kivitye9179ce2009-06-14 11:38:52 +03001744}
1745
Avi Kivityac1970f2012-10-03 16:22:53 +02001746static void mem_begin(MemoryListener *listener)
1747{
Paolo Bonzini89ae3372013-06-02 10:39:07 +02001748 AddressSpace *as = container_of(listener, AddressSpace, dispatch_listener);
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +02001749 AddressSpaceDispatch *d = g_new0(AddressSpaceDispatch, 1);
1750 uint16_t n;
1751
1752 n = dummy_section(&d->map, &io_mem_unassigned);
1753 assert(n == PHYS_SECTION_UNASSIGNED);
1754 n = dummy_section(&d->map, &io_mem_notdirty);
1755 assert(n == PHYS_SECTION_NOTDIRTY);
1756 n = dummy_section(&d->map, &io_mem_rom);
1757 assert(n == PHYS_SECTION_ROM);
1758 n = dummy_section(&d->map, &io_mem_watch);
1759 assert(n == PHYS_SECTION_WATCH);
Paolo Bonzini00752702013-05-29 12:13:54 +02001760
Michael S. Tsirkin9736e552013-11-11 14:42:43 +02001761 d->phys_map = (PhysPageEntry) { .ptr = PHYS_MAP_NODE_NIL, .skip = 1 };
Paolo Bonzini00752702013-05-29 12:13:54 +02001762 d->as = as;
1763 as->next_dispatch = d;
1764}
1765
1766static void mem_commit(MemoryListener *listener)
1767{
1768 AddressSpace *as = container_of(listener, AddressSpace, dispatch_listener);
Paolo Bonzini0475d942013-05-29 12:28:21 +02001769 AddressSpaceDispatch *cur = as->dispatch;
1770 AddressSpaceDispatch *next = as->next_dispatch;
Avi Kivityac1970f2012-10-03 16:22:53 +02001771
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +02001772 phys_page_compact_all(next, next->map.nodes_nb);
Michael S. Tsirkinb35ba302013-11-11 17:52:07 +02001773
Paolo Bonzini0475d942013-05-29 12:28:21 +02001774 as->dispatch = next;
Avi Kivityac1970f2012-10-03 16:22:53 +02001775
Marcel Apfelbaum53cb28c2013-12-01 14:02:23 +02001776 if (cur) {
1777 phys_sections_free(&cur->map);
1778 g_free(cur);
1779 }
Paolo Bonzini9affd6f2013-05-29 12:09:47 +02001780}
1781
Avi Kivity1d711482012-10-02 18:54:45 +02001782static void tcg_commit(MemoryListener *listener)
Avi Kivity50c1e142012-02-08 21:36:02 +02001783{
Andreas Färber182735e2013-05-29 22:29:20 +02001784 CPUState *cpu;
Avi Kivity117712c2012-02-12 21:23:17 +02001785
1786 /* since each CPU stores ram addresses in its TLB cache, we must
1787 reset the modified entries */
1788 /* XXX: slow ! */
Andreas Färberbdc44642013-06-24 23:50:24 +02001789 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001790 CPUArchState *env = cpu->env_ptr;
1791
Avi Kivity117712c2012-02-12 21:23:17 +02001792 tlb_flush(env, 1);
1793 }
Avi Kivity50c1e142012-02-08 21:36:02 +02001794}
1795
Avi Kivity93632742012-02-08 16:54:16 +02001796static void core_log_global_start(MemoryListener *listener)
1797{
1798 cpu_physical_memory_set_dirty_tracking(1);
1799}
1800
1801static void core_log_global_stop(MemoryListener *listener)
1802{
1803 cpu_physical_memory_set_dirty_tracking(0);
1804}
1805
Avi Kivity93632742012-02-08 16:54:16 +02001806static MemoryListener core_memory_listener = {
Avi Kivity93632742012-02-08 16:54:16 +02001807 .log_global_start = core_log_global_start,
1808 .log_global_stop = core_log_global_stop,
Avi Kivityac1970f2012-10-03 16:22:53 +02001809 .priority = 1,
Avi Kivity93632742012-02-08 16:54:16 +02001810};
1811
Avi Kivity1d711482012-10-02 18:54:45 +02001812static MemoryListener tcg_memory_listener = {
1813 .commit = tcg_commit,
1814};
1815
Avi Kivityac1970f2012-10-03 16:22:53 +02001816void address_space_init_dispatch(AddressSpace *as)
1817{
Paolo Bonzini00752702013-05-29 12:13:54 +02001818 as->dispatch = NULL;
Paolo Bonzini89ae3372013-06-02 10:39:07 +02001819 as->dispatch_listener = (MemoryListener) {
Avi Kivityac1970f2012-10-03 16:22:53 +02001820 .begin = mem_begin,
Paolo Bonzini00752702013-05-29 12:13:54 +02001821 .commit = mem_commit,
Avi Kivityac1970f2012-10-03 16:22:53 +02001822 .region_add = mem_add,
1823 .region_nop = mem_add,
1824 .priority = 0,
1825 };
Paolo Bonzini89ae3372013-06-02 10:39:07 +02001826 memory_listener_register(&as->dispatch_listener, as);
Avi Kivityac1970f2012-10-03 16:22:53 +02001827}
1828
Avi Kivity83f3c252012-10-07 12:59:55 +02001829void address_space_destroy_dispatch(AddressSpace *as)
1830{
1831 AddressSpaceDispatch *d = as->dispatch;
1832
Paolo Bonzini89ae3372013-06-02 10:39:07 +02001833 memory_listener_unregister(&as->dispatch_listener);
Avi Kivity83f3c252012-10-07 12:59:55 +02001834 g_free(d);
1835 as->dispatch = NULL;
1836}
1837
Avi Kivity62152b82011-07-26 14:26:14 +03001838static void memory_map_init(void)
1839{
Anthony Liguori7267c092011-08-20 22:09:37 -05001840 system_memory = g_malloc(sizeof(*system_memory));
Paolo Bonzini03f49952013-11-07 17:14:36 +01001841
Paolo Bonzini57271d62013-11-07 17:14:37 +01001842 memory_region_init(system_memory, NULL, "system", UINT64_MAX);
Alexey Kardashevskiy7dca8042013-04-29 16:25:51 +00001843 address_space_init(&address_space_memory, system_memory, "memory");
Avi Kivity309cb472011-08-08 16:09:03 +03001844
Anthony Liguori7267c092011-08-20 22:09:37 -05001845 system_io = g_malloc(sizeof(*system_io));
Jan Kiszka3bb28b72013-09-02 18:43:30 +02001846 memory_region_init_io(system_io, NULL, &unassigned_io_ops, NULL, "io",
1847 65536);
Alexey Kardashevskiy7dca8042013-04-29 16:25:51 +00001848 address_space_init(&address_space_io, system_io, "I/O");
Avi Kivity93632742012-02-08 16:54:16 +02001849
Avi Kivityf6790af2012-10-02 20:13:51 +02001850 memory_listener_register(&core_memory_listener, &address_space_memory);
liguang26416892013-09-04 14:37:33 +08001851 if (tcg_enabled()) {
1852 memory_listener_register(&tcg_memory_listener, &address_space_memory);
1853 }
Avi Kivity62152b82011-07-26 14:26:14 +03001854}
1855
1856MemoryRegion *get_system_memory(void)
1857{
1858 return system_memory;
1859}
1860
Avi Kivity309cb472011-08-08 16:09:03 +03001861MemoryRegion *get_system_io(void)
1862{
1863 return system_io;
1864}
1865
pbrooke2eef172008-06-08 01:09:01 +00001866#endif /* !defined(CONFIG_USER_ONLY) */
1867
bellard13eb76e2004-01-24 15:23:36 +00001868/* physical memory access (slow version, mainly for debug) */
1869#if defined(CONFIG_USER_ONLY)
Andreas Färberf17ec442013-06-29 19:40:58 +02001870int cpu_memory_rw_debug(CPUState *cpu, target_ulong addr,
Paul Brooka68fe892010-03-01 00:08:59 +00001871 uint8_t *buf, int len, int is_write)
bellard13eb76e2004-01-24 15:23:36 +00001872{
1873 int l, flags;
1874 target_ulong page;
pbrook53a59602006-03-25 19:31:22 +00001875 void * p;
bellard13eb76e2004-01-24 15:23:36 +00001876
1877 while (len > 0) {
1878 page = addr & TARGET_PAGE_MASK;
1879 l = (page + TARGET_PAGE_SIZE) - addr;
1880 if (l > len)
1881 l = len;
1882 flags = page_get_flags(page);
1883 if (!(flags & PAGE_VALID))
Paul Brooka68fe892010-03-01 00:08:59 +00001884 return -1;
bellard13eb76e2004-01-24 15:23:36 +00001885 if (is_write) {
1886 if (!(flags & PAGE_WRITE))
Paul Brooka68fe892010-03-01 00:08:59 +00001887 return -1;
bellard579a97f2007-11-11 14:26:47 +00001888 /* XXX: this code should not depend on lock_user */
aurel3272fb7da2008-04-27 23:53:45 +00001889 if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
Paul Brooka68fe892010-03-01 00:08:59 +00001890 return -1;
aurel3272fb7da2008-04-27 23:53:45 +00001891 memcpy(p, buf, l);
1892 unlock_user(p, addr, l);
bellard13eb76e2004-01-24 15:23:36 +00001893 } else {
1894 if (!(flags & PAGE_READ))
Paul Brooka68fe892010-03-01 00:08:59 +00001895 return -1;
bellard579a97f2007-11-11 14:26:47 +00001896 /* XXX: this code should not depend on lock_user */
aurel3272fb7da2008-04-27 23:53:45 +00001897 if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
Paul Brooka68fe892010-03-01 00:08:59 +00001898 return -1;
aurel3272fb7da2008-04-27 23:53:45 +00001899 memcpy(buf, p, l);
aurel325b257572008-04-28 08:54:59 +00001900 unlock_user(p, addr, 0);
bellard13eb76e2004-01-24 15:23:36 +00001901 }
1902 len -= l;
1903 buf += l;
1904 addr += l;
1905 }
Paul Brooka68fe892010-03-01 00:08:59 +00001906 return 0;
bellard13eb76e2004-01-24 15:23:36 +00001907}
bellard8df1cd02005-01-28 22:37:22 +00001908
bellard13eb76e2004-01-24 15:23:36 +00001909#else
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00001910
Avi Kivitya8170e52012-10-23 12:30:10 +02001911static void invalidate_and_set_dirty(hwaddr addr,
1912 hwaddr length)
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00001913{
1914 if (!cpu_physical_memory_is_dirty(addr)) {
1915 /* invalidate code */
1916 tb_invalidate_phys_page_range(addr, addr + length, 0);
1917 /* set dirty bit */
1918 cpu_physical_memory_set_dirty_flags(addr, (0xff & ~CODE_DIRTY_FLAG));
1919 }
Anthony PERARDe2269392012-10-03 13:49:22 +00001920 xen_modified_memory(addr, length);
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00001921}
1922
Paolo Bonzini2bbfa052013-05-24 12:29:54 +02001923static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
1924{
1925 if (memory_region_is_ram(mr)) {
1926 return !(is_write && mr->readonly);
1927 }
1928 if (memory_region_is_romd(mr)) {
1929 return !is_write;
1930 }
1931
1932 return false;
1933}
1934
Richard Henderson23326162013-07-08 14:55:59 -07001935static int memory_access_size(MemoryRegion *mr, unsigned l, hwaddr addr)
Paolo Bonzini82f25632013-05-24 11:59:43 +02001936{
Paolo Bonzinie1622f42013-07-17 13:17:41 +02001937 unsigned access_size_max = mr->ops->valid.max_access_size;
Richard Henderson23326162013-07-08 14:55:59 -07001938
1939 /* Regions are assumed to support 1-4 byte accesses unless
1940 otherwise specified. */
Richard Henderson23326162013-07-08 14:55:59 -07001941 if (access_size_max == 0) {
1942 access_size_max = 4;
Paolo Bonzini82f25632013-05-24 11:59:43 +02001943 }
Richard Henderson23326162013-07-08 14:55:59 -07001944
1945 /* Bound the maximum access by the alignment of the address. */
1946 if (!mr->ops->impl.unaligned) {
1947 unsigned align_size_max = addr & -addr;
1948 if (align_size_max != 0 && align_size_max < access_size_max) {
1949 access_size_max = align_size_max;
1950 }
1951 }
1952
1953 /* Don't attempt accesses larger than the maximum. */
1954 if (l > access_size_max) {
1955 l = access_size_max;
1956 }
Paolo Bonzini098178f2013-07-29 14:27:39 +02001957 if (l & (l - 1)) {
1958 l = 1 << (qemu_fls(l) - 1);
1959 }
Richard Henderson23326162013-07-08 14:55:59 -07001960
1961 return l;
Paolo Bonzini82f25632013-05-24 11:59:43 +02001962}
1963
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02001964bool address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
Avi Kivityac1970f2012-10-03 16:22:53 +02001965 int len, bool is_write)
bellard13eb76e2004-01-24 15:23:36 +00001966{
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001967 hwaddr l;
bellard13eb76e2004-01-24 15:23:36 +00001968 uint8_t *ptr;
Paolo Bonzini791af8c2013-05-24 16:10:39 +02001969 uint64_t val;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001970 hwaddr addr1;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02001971 MemoryRegion *mr;
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02001972 bool error = false;
ths3b46e622007-09-17 08:09:54 +00001973
bellard13eb76e2004-01-24 15:23:36 +00001974 while (len > 0) {
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001975 l = len;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02001976 mr = address_space_translate(as, addr, &addr1, &l, is_write);
ths3b46e622007-09-17 08:09:54 +00001977
bellard13eb76e2004-01-24 15:23:36 +00001978 if (is_write) {
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02001979 if (!memory_access_is_direct(mr, is_write)) {
1980 l = memory_access_size(mr, l, addr1);
Andreas Färber4917cf42013-05-27 05:17:50 +02001981 /* XXX: could force current_cpu to NULL to avoid
bellard6a00d602005-11-21 23:25:50 +00001982 potential bugs */
Richard Henderson23326162013-07-08 14:55:59 -07001983 switch (l) {
1984 case 8:
1985 /* 64 bit write access */
1986 val = ldq_p(buf);
1987 error |= io_mem_write(mr, addr1, val, 8);
1988 break;
1989 case 4:
bellard1c213d12005-09-03 10:49:04 +00001990 /* 32 bit write access */
bellardc27004e2005-01-03 23:35:10 +00001991 val = ldl_p(buf);
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02001992 error |= io_mem_write(mr, addr1, val, 4);
Richard Henderson23326162013-07-08 14:55:59 -07001993 break;
1994 case 2:
bellard1c213d12005-09-03 10:49:04 +00001995 /* 16 bit write access */
bellardc27004e2005-01-03 23:35:10 +00001996 val = lduw_p(buf);
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02001997 error |= io_mem_write(mr, addr1, val, 2);
Richard Henderson23326162013-07-08 14:55:59 -07001998 break;
1999 case 1:
bellard1c213d12005-09-03 10:49:04 +00002000 /* 8 bit write access */
bellardc27004e2005-01-03 23:35:10 +00002001 val = ldub_p(buf);
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002002 error |= io_mem_write(mr, addr1, val, 1);
Richard Henderson23326162013-07-08 14:55:59 -07002003 break;
2004 default:
2005 abort();
bellard13eb76e2004-01-24 15:23:36 +00002006 }
Paolo Bonzini2bbfa052013-05-24 12:29:54 +02002007 } else {
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002008 addr1 += memory_region_get_ram_addr(mr);
bellard13eb76e2004-01-24 15:23:36 +00002009 /* RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00002010 ptr = qemu_get_ram_ptr(addr1);
bellard13eb76e2004-01-24 15:23:36 +00002011 memcpy(ptr, buf, l);
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00002012 invalidate_and_set_dirty(addr1, l);
bellard13eb76e2004-01-24 15:23:36 +00002013 }
2014 } else {
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002015 if (!memory_access_is_direct(mr, is_write)) {
bellard13eb76e2004-01-24 15:23:36 +00002016 /* I/O case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002017 l = memory_access_size(mr, l, addr1);
Richard Henderson23326162013-07-08 14:55:59 -07002018 switch (l) {
2019 case 8:
2020 /* 64 bit read access */
2021 error |= io_mem_read(mr, addr1, &val, 8);
2022 stq_p(buf, val);
2023 break;
2024 case 4:
bellard13eb76e2004-01-24 15:23:36 +00002025 /* 32 bit read access */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002026 error |= io_mem_read(mr, addr1, &val, 4);
bellardc27004e2005-01-03 23:35:10 +00002027 stl_p(buf, val);
Richard Henderson23326162013-07-08 14:55:59 -07002028 break;
2029 case 2:
bellard13eb76e2004-01-24 15:23:36 +00002030 /* 16 bit read access */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002031 error |= io_mem_read(mr, addr1, &val, 2);
bellardc27004e2005-01-03 23:35:10 +00002032 stw_p(buf, val);
Richard Henderson23326162013-07-08 14:55:59 -07002033 break;
2034 case 1:
bellard1c213d12005-09-03 10:49:04 +00002035 /* 8 bit read access */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002036 error |= io_mem_read(mr, addr1, &val, 1);
bellardc27004e2005-01-03 23:35:10 +00002037 stb_p(buf, val);
Richard Henderson23326162013-07-08 14:55:59 -07002038 break;
2039 default:
2040 abort();
bellard13eb76e2004-01-24 15:23:36 +00002041 }
2042 } else {
2043 /* RAM case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002044 ptr = qemu_get_ram_ptr(mr->ram_addr + addr1);
Avi Kivityf3705d52012-03-08 16:16:34 +02002045 memcpy(buf, ptr, l);
bellard13eb76e2004-01-24 15:23:36 +00002046 }
2047 }
2048 len -= l;
2049 buf += l;
2050 addr += l;
2051 }
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02002052
2053 return error;
bellard13eb76e2004-01-24 15:23:36 +00002054}
bellard8df1cd02005-01-28 22:37:22 +00002055
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02002056bool address_space_write(AddressSpace *as, hwaddr addr,
Avi Kivityac1970f2012-10-03 16:22:53 +02002057 const uint8_t *buf, int len)
2058{
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02002059 return address_space_rw(as, addr, (uint8_t *)buf, len, true);
Avi Kivityac1970f2012-10-03 16:22:53 +02002060}
2061
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02002062bool address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len)
Avi Kivityac1970f2012-10-03 16:22:53 +02002063{
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02002064 return address_space_rw(as, addr, buf, len, false);
Avi Kivityac1970f2012-10-03 16:22:53 +02002065}
2066
2067
Avi Kivitya8170e52012-10-23 12:30:10 +02002068void cpu_physical_memory_rw(hwaddr addr, uint8_t *buf,
Avi Kivityac1970f2012-10-03 16:22:53 +02002069 int len, int is_write)
2070{
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02002071 address_space_rw(&address_space_memory, addr, buf, len, is_write);
Avi Kivityac1970f2012-10-03 16:22:53 +02002072}
2073
Alexander Graf582b55a2013-12-11 14:17:44 +01002074enum write_rom_type {
2075 WRITE_DATA,
2076 FLUSH_CACHE,
2077};
2078
2079static inline void cpu_physical_memory_write_rom_internal(
2080 hwaddr addr, const uint8_t *buf, int len, enum write_rom_type type)
bellardd0ecd2a2006-04-23 17:14:48 +00002081{
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002082 hwaddr l;
bellardd0ecd2a2006-04-23 17:14:48 +00002083 uint8_t *ptr;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002084 hwaddr addr1;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002085 MemoryRegion *mr;
ths3b46e622007-09-17 08:09:54 +00002086
bellardd0ecd2a2006-04-23 17:14:48 +00002087 while (len > 0) {
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002088 l = len;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002089 mr = address_space_translate(&address_space_memory,
2090 addr, &addr1, &l, true);
ths3b46e622007-09-17 08:09:54 +00002091
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002092 if (!(memory_region_is_ram(mr) ||
2093 memory_region_is_romd(mr))) {
bellardd0ecd2a2006-04-23 17:14:48 +00002094 /* do nothing */
2095 } else {
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002096 addr1 += memory_region_get_ram_addr(mr);
bellardd0ecd2a2006-04-23 17:14:48 +00002097 /* ROM/RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00002098 ptr = qemu_get_ram_ptr(addr1);
Alexander Graf582b55a2013-12-11 14:17:44 +01002099 switch (type) {
2100 case WRITE_DATA:
2101 memcpy(ptr, buf, l);
2102 invalidate_and_set_dirty(addr1, l);
2103 break;
2104 case FLUSH_CACHE:
2105 flush_icache_range((uintptr_t)ptr, (uintptr_t)ptr + l);
2106 break;
2107 }
bellardd0ecd2a2006-04-23 17:14:48 +00002108 }
2109 len -= l;
2110 buf += l;
2111 addr += l;
2112 }
2113}
2114
Alexander Graf582b55a2013-12-11 14:17:44 +01002115/* used for ROM loading : can write in RAM and ROM */
2116void cpu_physical_memory_write_rom(hwaddr addr,
2117 const uint8_t *buf, int len)
2118{
2119 cpu_physical_memory_write_rom_internal(addr, buf, len, WRITE_DATA);
2120}
2121
2122void cpu_flush_icache_range(hwaddr start, int len)
2123{
2124 /*
2125 * This function should do the same thing as an icache flush that was
2126 * triggered from within the guest. For TCG we are always cache coherent,
2127 * so there is no need to flush anything. For KVM / Xen we need to flush
2128 * the host's instruction cache at least.
2129 */
2130 if (tcg_enabled()) {
2131 return;
2132 }
2133
2134 cpu_physical_memory_write_rom_internal(start, NULL, len, FLUSH_CACHE);
2135}
2136
aliguori6d16c2f2009-01-22 16:59:11 +00002137typedef struct {
Paolo Bonzinid3e71552013-06-28 17:33:29 +02002138 MemoryRegion *mr;
aliguori6d16c2f2009-01-22 16:59:11 +00002139 void *buffer;
Avi Kivitya8170e52012-10-23 12:30:10 +02002140 hwaddr addr;
2141 hwaddr len;
aliguori6d16c2f2009-01-22 16:59:11 +00002142} BounceBuffer;
2143
2144static BounceBuffer bounce;
2145
aliguoriba223c22009-01-22 16:59:16 +00002146typedef struct MapClient {
2147 void *opaque;
2148 void (*callback)(void *opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002149 QLIST_ENTRY(MapClient) link;
aliguoriba223c22009-01-22 16:59:16 +00002150} MapClient;
2151
Blue Swirl72cf2d42009-09-12 07:36:22 +00002152static QLIST_HEAD(map_client_list, MapClient) map_client_list
2153 = QLIST_HEAD_INITIALIZER(map_client_list);
aliguoriba223c22009-01-22 16:59:16 +00002154
2155void *cpu_register_map_client(void *opaque, void (*callback)(void *opaque))
2156{
Anthony Liguori7267c092011-08-20 22:09:37 -05002157 MapClient *client = g_malloc(sizeof(*client));
aliguoriba223c22009-01-22 16:59:16 +00002158
2159 client->opaque = opaque;
2160 client->callback = callback;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002161 QLIST_INSERT_HEAD(&map_client_list, client, link);
aliguoriba223c22009-01-22 16:59:16 +00002162 return client;
2163}
2164
Blue Swirl8b9c99d2012-10-28 11:04:51 +00002165static void cpu_unregister_map_client(void *_client)
aliguoriba223c22009-01-22 16:59:16 +00002166{
2167 MapClient *client = (MapClient *)_client;
2168
Blue Swirl72cf2d42009-09-12 07:36:22 +00002169 QLIST_REMOVE(client, link);
Anthony Liguori7267c092011-08-20 22:09:37 -05002170 g_free(client);
aliguoriba223c22009-01-22 16:59:16 +00002171}
2172
2173static void cpu_notify_map_clients(void)
2174{
2175 MapClient *client;
2176
Blue Swirl72cf2d42009-09-12 07:36:22 +00002177 while (!QLIST_EMPTY(&map_client_list)) {
2178 client = QLIST_FIRST(&map_client_list);
aliguoriba223c22009-01-22 16:59:16 +00002179 client->callback(client->opaque);
Isaku Yamahata34d5e942009-06-26 18:57:18 +09002180 cpu_unregister_map_client(client);
aliguoriba223c22009-01-22 16:59:16 +00002181 }
2182}
2183
Paolo Bonzini51644ab2013-04-11 15:40:59 +02002184bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write)
2185{
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002186 MemoryRegion *mr;
Paolo Bonzini51644ab2013-04-11 15:40:59 +02002187 hwaddr l, xlat;
2188
2189 while (len > 0) {
2190 l = len;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002191 mr = address_space_translate(as, addr, &xlat, &l, is_write);
2192 if (!memory_access_is_direct(mr, is_write)) {
2193 l = memory_access_size(mr, l, addr);
2194 if (!memory_region_access_valid(mr, xlat, l, is_write)) {
Paolo Bonzini51644ab2013-04-11 15:40:59 +02002195 return false;
2196 }
2197 }
2198
2199 len -= l;
2200 addr += l;
2201 }
2202 return true;
2203}
2204
aliguori6d16c2f2009-01-22 16:59:11 +00002205/* Map a physical memory region into a host virtual address.
2206 * May map a subset of the requested range, given by and returned in *plen.
2207 * May return NULL if resources needed to perform the mapping are exhausted.
2208 * Use only for reads OR writes - not for read-modify-write operations.
aliguoriba223c22009-01-22 16:59:16 +00002209 * Use cpu_register_map_client() to know when retrying the map operation is
2210 * likely to succeed.
aliguori6d16c2f2009-01-22 16:59:11 +00002211 */
Avi Kivityac1970f2012-10-03 16:22:53 +02002212void *address_space_map(AddressSpace *as,
Avi Kivitya8170e52012-10-23 12:30:10 +02002213 hwaddr addr,
2214 hwaddr *plen,
Avi Kivityac1970f2012-10-03 16:22:53 +02002215 bool is_write)
aliguori6d16c2f2009-01-22 16:59:11 +00002216{
Avi Kivitya8170e52012-10-23 12:30:10 +02002217 hwaddr len = *plen;
Paolo Bonzinie3127ae2013-06-28 17:29:27 +02002218 hwaddr done = 0;
2219 hwaddr l, xlat, base;
2220 MemoryRegion *mr, *this_mr;
2221 ram_addr_t raddr;
aliguori6d16c2f2009-01-22 16:59:11 +00002222
Paolo Bonzinie3127ae2013-06-28 17:29:27 +02002223 if (len == 0) {
2224 return NULL;
2225 }
aliguori6d16c2f2009-01-22 16:59:11 +00002226
Paolo Bonzinie3127ae2013-06-28 17:29:27 +02002227 l = len;
2228 mr = address_space_translate(as, addr, &xlat, &l, is_write);
2229 if (!memory_access_is_direct(mr, is_write)) {
2230 if (bounce.buffer) {
2231 return NULL;
aliguori6d16c2f2009-01-22 16:59:11 +00002232 }
Kevin Wolfe85d9db2013-07-22 14:30:23 +02002233 /* Avoid unbounded allocations */
2234 l = MIN(l, TARGET_PAGE_SIZE);
2235 bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, l);
Paolo Bonzinie3127ae2013-06-28 17:29:27 +02002236 bounce.addr = addr;
2237 bounce.len = l;
Paolo Bonzinid3e71552013-06-28 17:33:29 +02002238
2239 memory_region_ref(mr);
2240 bounce.mr = mr;
Paolo Bonzinie3127ae2013-06-28 17:29:27 +02002241 if (!is_write) {
2242 address_space_read(as, addr, bounce.buffer, l);
Stefano Stabellini8ab934f2011-06-27 18:26:06 +01002243 }
aliguori6d16c2f2009-01-22 16:59:11 +00002244
Paolo Bonzinie3127ae2013-06-28 17:29:27 +02002245 *plen = l;
2246 return bounce.buffer;
2247 }
2248
2249 base = xlat;
2250 raddr = memory_region_get_ram_addr(mr);
2251
2252 for (;;) {
aliguori6d16c2f2009-01-22 16:59:11 +00002253 len -= l;
2254 addr += l;
Paolo Bonzinie3127ae2013-06-28 17:29:27 +02002255 done += l;
2256 if (len == 0) {
2257 break;
2258 }
2259
2260 l = len;
2261 this_mr = address_space_translate(as, addr, &xlat, &l, is_write);
2262 if (this_mr != mr || xlat != base + done) {
2263 break;
2264 }
aliguori6d16c2f2009-01-22 16:59:11 +00002265 }
Paolo Bonzinie3127ae2013-06-28 17:29:27 +02002266
Paolo Bonzinid3e71552013-06-28 17:33:29 +02002267 memory_region_ref(mr);
Paolo Bonzinie3127ae2013-06-28 17:29:27 +02002268 *plen = done;
2269 return qemu_ram_ptr_length(raddr + base, plen);
aliguori6d16c2f2009-01-22 16:59:11 +00002270}
2271
Avi Kivityac1970f2012-10-03 16:22:53 +02002272/* Unmaps a memory region previously mapped by address_space_map().
aliguori6d16c2f2009-01-22 16:59:11 +00002273 * Will also mark the memory as dirty if is_write == 1. access_len gives
2274 * the amount of memory that was actually read or written by the caller.
2275 */
Avi Kivitya8170e52012-10-23 12:30:10 +02002276void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
2277 int is_write, hwaddr access_len)
aliguori6d16c2f2009-01-22 16:59:11 +00002278{
2279 if (buffer != bounce.buffer) {
Paolo Bonzinid3e71552013-06-28 17:33:29 +02002280 MemoryRegion *mr;
2281 ram_addr_t addr1;
2282
2283 mr = qemu_ram_addr_from_host(buffer, &addr1);
2284 assert(mr != NULL);
aliguori6d16c2f2009-01-22 16:59:11 +00002285 if (is_write) {
aliguori6d16c2f2009-01-22 16:59:11 +00002286 while (access_len) {
2287 unsigned l;
2288 l = TARGET_PAGE_SIZE;
2289 if (l > access_len)
2290 l = access_len;
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00002291 invalidate_and_set_dirty(addr1, l);
aliguori6d16c2f2009-01-22 16:59:11 +00002292 addr1 += l;
2293 access_len -= l;
2294 }
2295 }
Jan Kiszka868bb332011-06-21 22:59:09 +02002296 if (xen_enabled()) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02002297 xen_invalidate_map_cache_entry(buffer);
Anthony PERARD050a0dd2010-09-16 13:57:49 +01002298 }
Paolo Bonzinid3e71552013-06-28 17:33:29 +02002299 memory_region_unref(mr);
aliguori6d16c2f2009-01-22 16:59:11 +00002300 return;
2301 }
2302 if (is_write) {
Avi Kivityac1970f2012-10-03 16:22:53 +02002303 address_space_write(as, bounce.addr, bounce.buffer, access_len);
aliguori6d16c2f2009-01-22 16:59:11 +00002304 }
Herve Poussineauf8a83242010-01-24 21:23:56 +00002305 qemu_vfree(bounce.buffer);
aliguori6d16c2f2009-01-22 16:59:11 +00002306 bounce.buffer = NULL;
Paolo Bonzinid3e71552013-06-28 17:33:29 +02002307 memory_region_unref(bounce.mr);
aliguoriba223c22009-01-22 16:59:16 +00002308 cpu_notify_map_clients();
aliguori6d16c2f2009-01-22 16:59:11 +00002309}
bellardd0ecd2a2006-04-23 17:14:48 +00002310
Avi Kivitya8170e52012-10-23 12:30:10 +02002311void *cpu_physical_memory_map(hwaddr addr,
2312 hwaddr *plen,
Avi Kivityac1970f2012-10-03 16:22:53 +02002313 int is_write)
2314{
2315 return address_space_map(&address_space_memory, addr, plen, is_write);
2316}
2317
Avi Kivitya8170e52012-10-23 12:30:10 +02002318void cpu_physical_memory_unmap(void *buffer, hwaddr len,
2319 int is_write, hwaddr access_len)
Avi Kivityac1970f2012-10-03 16:22:53 +02002320{
2321 return address_space_unmap(&address_space_memory, buffer, len, is_write, access_len);
2322}
2323
bellard8df1cd02005-01-28 22:37:22 +00002324/* warning: addr must be aligned */
Avi Kivitya8170e52012-10-23 12:30:10 +02002325static inline uint32_t ldl_phys_internal(hwaddr addr,
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002326 enum device_endian endian)
bellard8df1cd02005-01-28 22:37:22 +00002327{
bellard8df1cd02005-01-28 22:37:22 +00002328 uint8_t *ptr;
Paolo Bonzini791af8c2013-05-24 16:10:39 +02002329 uint64_t val;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002330 MemoryRegion *mr;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002331 hwaddr l = 4;
2332 hwaddr addr1;
bellard8df1cd02005-01-28 22:37:22 +00002333
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002334 mr = address_space_translate(&address_space_memory, addr, &addr1, &l,
2335 false);
2336 if (l < 4 || !memory_access_is_direct(mr, false)) {
bellard8df1cd02005-01-28 22:37:22 +00002337 /* I/O case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002338 io_mem_read(mr, addr1, &val, 4);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002339#if defined(TARGET_WORDS_BIGENDIAN)
2340 if (endian == DEVICE_LITTLE_ENDIAN) {
2341 val = bswap32(val);
2342 }
2343#else
2344 if (endian == DEVICE_BIG_ENDIAN) {
2345 val = bswap32(val);
2346 }
2347#endif
bellard8df1cd02005-01-28 22:37:22 +00002348 } else {
2349 /* RAM case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002350 ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(mr)
Avi Kivity06ef3522012-02-13 16:11:22 +02002351 & TARGET_PAGE_MASK)
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002352 + addr1);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002353 switch (endian) {
2354 case DEVICE_LITTLE_ENDIAN:
2355 val = ldl_le_p(ptr);
2356 break;
2357 case DEVICE_BIG_ENDIAN:
2358 val = ldl_be_p(ptr);
2359 break;
2360 default:
2361 val = ldl_p(ptr);
2362 break;
2363 }
bellard8df1cd02005-01-28 22:37:22 +00002364 }
2365 return val;
2366}
2367
Avi Kivitya8170e52012-10-23 12:30:10 +02002368uint32_t ldl_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002369{
2370 return ldl_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
2371}
2372
Avi Kivitya8170e52012-10-23 12:30:10 +02002373uint32_t ldl_le_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002374{
2375 return ldl_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
2376}
2377
Avi Kivitya8170e52012-10-23 12:30:10 +02002378uint32_t ldl_be_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002379{
2380 return ldl_phys_internal(addr, DEVICE_BIG_ENDIAN);
2381}
2382
bellard84b7b8e2005-11-28 21:19:04 +00002383/* warning: addr must be aligned */
Avi Kivitya8170e52012-10-23 12:30:10 +02002384static inline uint64_t ldq_phys_internal(hwaddr addr,
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002385 enum device_endian endian)
bellard84b7b8e2005-11-28 21:19:04 +00002386{
bellard84b7b8e2005-11-28 21:19:04 +00002387 uint8_t *ptr;
2388 uint64_t val;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002389 MemoryRegion *mr;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002390 hwaddr l = 8;
2391 hwaddr addr1;
bellard84b7b8e2005-11-28 21:19:04 +00002392
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002393 mr = address_space_translate(&address_space_memory, addr, &addr1, &l,
2394 false);
2395 if (l < 8 || !memory_access_is_direct(mr, false)) {
bellard84b7b8e2005-11-28 21:19:04 +00002396 /* I/O case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002397 io_mem_read(mr, addr1, &val, 8);
Paolo Bonzini968a5622013-05-24 17:58:37 +02002398#if defined(TARGET_WORDS_BIGENDIAN)
2399 if (endian == DEVICE_LITTLE_ENDIAN) {
2400 val = bswap64(val);
2401 }
2402#else
2403 if (endian == DEVICE_BIG_ENDIAN) {
2404 val = bswap64(val);
2405 }
2406#endif
bellard84b7b8e2005-11-28 21:19:04 +00002407 } else {
2408 /* RAM case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002409 ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(mr)
Avi Kivity06ef3522012-02-13 16:11:22 +02002410 & TARGET_PAGE_MASK)
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002411 + addr1);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002412 switch (endian) {
2413 case DEVICE_LITTLE_ENDIAN:
2414 val = ldq_le_p(ptr);
2415 break;
2416 case DEVICE_BIG_ENDIAN:
2417 val = ldq_be_p(ptr);
2418 break;
2419 default:
2420 val = ldq_p(ptr);
2421 break;
2422 }
bellard84b7b8e2005-11-28 21:19:04 +00002423 }
2424 return val;
2425}
2426
Avi Kivitya8170e52012-10-23 12:30:10 +02002427uint64_t ldq_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002428{
2429 return ldq_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
2430}
2431
Avi Kivitya8170e52012-10-23 12:30:10 +02002432uint64_t ldq_le_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002433{
2434 return ldq_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
2435}
2436
Avi Kivitya8170e52012-10-23 12:30:10 +02002437uint64_t ldq_be_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002438{
2439 return ldq_phys_internal(addr, DEVICE_BIG_ENDIAN);
2440}
2441
bellardaab33092005-10-30 20:48:42 +00002442/* XXX: optimize */
Avi Kivitya8170e52012-10-23 12:30:10 +02002443uint32_t ldub_phys(hwaddr addr)
bellardaab33092005-10-30 20:48:42 +00002444{
2445 uint8_t val;
2446 cpu_physical_memory_read(addr, &val, 1);
2447 return val;
2448}
2449
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002450/* warning: addr must be aligned */
Avi Kivitya8170e52012-10-23 12:30:10 +02002451static inline uint32_t lduw_phys_internal(hwaddr addr,
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002452 enum device_endian endian)
bellardaab33092005-10-30 20:48:42 +00002453{
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002454 uint8_t *ptr;
2455 uint64_t val;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002456 MemoryRegion *mr;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002457 hwaddr l = 2;
2458 hwaddr addr1;
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002459
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002460 mr = address_space_translate(&address_space_memory, addr, &addr1, &l,
2461 false);
2462 if (l < 2 || !memory_access_is_direct(mr, false)) {
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002463 /* I/O case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002464 io_mem_read(mr, addr1, &val, 2);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002465#if defined(TARGET_WORDS_BIGENDIAN)
2466 if (endian == DEVICE_LITTLE_ENDIAN) {
2467 val = bswap16(val);
2468 }
2469#else
2470 if (endian == DEVICE_BIG_ENDIAN) {
2471 val = bswap16(val);
2472 }
2473#endif
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002474 } else {
2475 /* RAM case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002476 ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(mr)
Avi Kivity06ef3522012-02-13 16:11:22 +02002477 & TARGET_PAGE_MASK)
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002478 + addr1);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002479 switch (endian) {
2480 case DEVICE_LITTLE_ENDIAN:
2481 val = lduw_le_p(ptr);
2482 break;
2483 case DEVICE_BIG_ENDIAN:
2484 val = lduw_be_p(ptr);
2485 break;
2486 default:
2487 val = lduw_p(ptr);
2488 break;
2489 }
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002490 }
2491 return val;
bellardaab33092005-10-30 20:48:42 +00002492}
2493
Avi Kivitya8170e52012-10-23 12:30:10 +02002494uint32_t lduw_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002495{
2496 return lduw_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
2497}
2498
Avi Kivitya8170e52012-10-23 12:30:10 +02002499uint32_t lduw_le_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002500{
2501 return lduw_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
2502}
2503
Avi Kivitya8170e52012-10-23 12:30:10 +02002504uint32_t lduw_be_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002505{
2506 return lduw_phys_internal(addr, DEVICE_BIG_ENDIAN);
2507}
2508
bellard8df1cd02005-01-28 22:37:22 +00002509/* warning: addr must be aligned. The ram page is not masked as dirty
2510 and the code inside is not invalidated. It is useful if the dirty
2511 bits are used to track modified PTEs */
Avi Kivitya8170e52012-10-23 12:30:10 +02002512void stl_phys_notdirty(hwaddr addr, uint32_t val)
bellard8df1cd02005-01-28 22:37:22 +00002513{
bellard8df1cd02005-01-28 22:37:22 +00002514 uint8_t *ptr;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002515 MemoryRegion *mr;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002516 hwaddr l = 4;
2517 hwaddr addr1;
bellard8df1cd02005-01-28 22:37:22 +00002518
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002519 mr = address_space_translate(&address_space_memory, addr, &addr1, &l,
2520 true);
2521 if (l < 4 || !memory_access_is_direct(mr, true)) {
2522 io_mem_write(mr, addr1, val, 4);
bellard8df1cd02005-01-28 22:37:22 +00002523 } else {
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002524 addr1 += memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK;
pbrook5579c7f2009-04-11 14:47:08 +00002525 ptr = qemu_get_ram_ptr(addr1);
bellard8df1cd02005-01-28 22:37:22 +00002526 stl_p(ptr, val);
aliguori74576192008-10-06 14:02:03 +00002527
2528 if (unlikely(in_migration)) {
2529 if (!cpu_physical_memory_is_dirty(addr1)) {
2530 /* invalidate code */
2531 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
2532 /* set dirty bit */
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09002533 cpu_physical_memory_set_dirty_flags(
2534 addr1, (0xff & ~CODE_DIRTY_FLAG));
aliguori74576192008-10-06 14:02:03 +00002535 }
2536 }
bellard8df1cd02005-01-28 22:37:22 +00002537 }
2538}
2539
2540/* warning: addr must be aligned */
Avi Kivitya8170e52012-10-23 12:30:10 +02002541static inline void stl_phys_internal(hwaddr addr, uint32_t val,
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002542 enum device_endian endian)
bellard8df1cd02005-01-28 22:37:22 +00002543{
bellard8df1cd02005-01-28 22:37:22 +00002544 uint8_t *ptr;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002545 MemoryRegion *mr;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002546 hwaddr l = 4;
2547 hwaddr addr1;
bellard8df1cd02005-01-28 22:37:22 +00002548
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002549 mr = address_space_translate(&address_space_memory, addr, &addr1, &l,
2550 true);
2551 if (l < 4 || !memory_access_is_direct(mr, true)) {
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002552#if defined(TARGET_WORDS_BIGENDIAN)
2553 if (endian == DEVICE_LITTLE_ENDIAN) {
2554 val = bswap32(val);
2555 }
2556#else
2557 if (endian == DEVICE_BIG_ENDIAN) {
2558 val = bswap32(val);
2559 }
2560#endif
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002561 io_mem_write(mr, addr1, val, 4);
bellard8df1cd02005-01-28 22:37:22 +00002562 } else {
bellard8df1cd02005-01-28 22:37:22 +00002563 /* RAM case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002564 addr1 += memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK;
pbrook5579c7f2009-04-11 14:47:08 +00002565 ptr = qemu_get_ram_ptr(addr1);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002566 switch (endian) {
2567 case DEVICE_LITTLE_ENDIAN:
2568 stl_le_p(ptr, val);
2569 break;
2570 case DEVICE_BIG_ENDIAN:
2571 stl_be_p(ptr, val);
2572 break;
2573 default:
2574 stl_p(ptr, val);
2575 break;
2576 }
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00002577 invalidate_and_set_dirty(addr1, 4);
bellard8df1cd02005-01-28 22:37:22 +00002578 }
2579}
2580
Avi Kivitya8170e52012-10-23 12:30:10 +02002581void stl_phys(hwaddr addr, uint32_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002582{
2583 stl_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN);
2584}
2585
Avi Kivitya8170e52012-10-23 12:30:10 +02002586void stl_le_phys(hwaddr addr, uint32_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002587{
2588 stl_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN);
2589}
2590
Avi Kivitya8170e52012-10-23 12:30:10 +02002591void stl_be_phys(hwaddr addr, uint32_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002592{
2593 stl_phys_internal(addr, val, DEVICE_BIG_ENDIAN);
2594}
2595
bellardaab33092005-10-30 20:48:42 +00002596/* XXX: optimize */
Avi Kivitya8170e52012-10-23 12:30:10 +02002597void stb_phys(hwaddr addr, uint32_t val)
bellardaab33092005-10-30 20:48:42 +00002598{
2599 uint8_t v = val;
2600 cpu_physical_memory_write(addr, &v, 1);
2601}
2602
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002603/* warning: addr must be aligned */
Avi Kivitya8170e52012-10-23 12:30:10 +02002604static inline void stw_phys_internal(hwaddr addr, uint32_t val,
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002605 enum device_endian endian)
bellardaab33092005-10-30 20:48:42 +00002606{
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002607 uint8_t *ptr;
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002608 MemoryRegion *mr;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002609 hwaddr l = 2;
2610 hwaddr addr1;
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002611
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002612 mr = address_space_translate(&address_space_memory, addr, &addr1, &l,
2613 true);
2614 if (l < 2 || !memory_access_is_direct(mr, true)) {
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002615#if defined(TARGET_WORDS_BIGENDIAN)
2616 if (endian == DEVICE_LITTLE_ENDIAN) {
2617 val = bswap16(val);
2618 }
2619#else
2620 if (endian == DEVICE_BIG_ENDIAN) {
2621 val = bswap16(val);
2622 }
2623#endif
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002624 io_mem_write(mr, addr1, val, 2);
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002625 } else {
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002626 /* RAM case */
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002627 addr1 += memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK;
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002628 ptr = qemu_get_ram_ptr(addr1);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002629 switch (endian) {
2630 case DEVICE_LITTLE_ENDIAN:
2631 stw_le_p(ptr, val);
2632 break;
2633 case DEVICE_BIG_ENDIAN:
2634 stw_be_p(ptr, val);
2635 break;
2636 default:
2637 stw_p(ptr, val);
2638 break;
2639 }
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00002640 invalidate_and_set_dirty(addr1, 2);
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002641 }
bellardaab33092005-10-30 20:48:42 +00002642}
2643
Avi Kivitya8170e52012-10-23 12:30:10 +02002644void stw_phys(hwaddr addr, uint32_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002645{
2646 stw_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN);
2647}
2648
Avi Kivitya8170e52012-10-23 12:30:10 +02002649void stw_le_phys(hwaddr addr, uint32_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002650{
2651 stw_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN);
2652}
2653
Avi Kivitya8170e52012-10-23 12:30:10 +02002654void stw_be_phys(hwaddr addr, uint32_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002655{
2656 stw_phys_internal(addr, val, DEVICE_BIG_ENDIAN);
2657}
2658
bellardaab33092005-10-30 20:48:42 +00002659/* XXX: optimize */
Avi Kivitya8170e52012-10-23 12:30:10 +02002660void stq_phys(hwaddr addr, uint64_t val)
bellardaab33092005-10-30 20:48:42 +00002661{
2662 val = tswap64(val);
Stefan Weil71d2b722011-03-26 21:06:56 +01002663 cpu_physical_memory_write(addr, &val, 8);
bellardaab33092005-10-30 20:48:42 +00002664}
2665
Avi Kivitya8170e52012-10-23 12:30:10 +02002666void stq_le_phys(hwaddr addr, uint64_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002667{
2668 val = cpu_to_le64(val);
2669 cpu_physical_memory_write(addr, &val, 8);
2670}
2671
Avi Kivitya8170e52012-10-23 12:30:10 +02002672void stq_be_phys(hwaddr addr, uint64_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002673{
2674 val = cpu_to_be64(val);
2675 cpu_physical_memory_write(addr, &val, 8);
2676}
2677
aliguori5e2972f2009-03-28 17:51:36 +00002678/* virtual memory access for debug (includes writing to ROM) */
Andreas Färberf17ec442013-06-29 19:40:58 +02002679int cpu_memory_rw_debug(CPUState *cpu, target_ulong addr,
bellardb448f2f2004-02-25 23:24:04 +00002680 uint8_t *buf, int len, int is_write)
bellard13eb76e2004-01-24 15:23:36 +00002681{
2682 int l;
Avi Kivitya8170e52012-10-23 12:30:10 +02002683 hwaddr phys_addr;
j_mayer9b3c35e2007-04-07 11:21:28 +00002684 target_ulong page;
bellard13eb76e2004-01-24 15:23:36 +00002685
2686 while (len > 0) {
2687 page = addr & TARGET_PAGE_MASK;
Andreas Färberf17ec442013-06-29 19:40:58 +02002688 phys_addr = cpu_get_phys_page_debug(cpu, page);
bellard13eb76e2004-01-24 15:23:36 +00002689 /* if no physical page mapped, return an error */
2690 if (phys_addr == -1)
2691 return -1;
2692 l = (page + TARGET_PAGE_SIZE) - addr;
2693 if (l > len)
2694 l = len;
aliguori5e2972f2009-03-28 17:51:36 +00002695 phys_addr += (addr & ~TARGET_PAGE_MASK);
aliguori5e2972f2009-03-28 17:51:36 +00002696 if (is_write)
2697 cpu_physical_memory_write_rom(phys_addr, buf, l);
2698 else
aliguori5e2972f2009-03-28 17:51:36 +00002699 cpu_physical_memory_rw(phys_addr, buf, l, is_write);
bellard13eb76e2004-01-24 15:23:36 +00002700 len -= l;
2701 buf += l;
2702 addr += l;
2703 }
2704 return 0;
2705}
Paul Brooka68fe892010-03-01 00:08:59 +00002706#endif
bellard13eb76e2004-01-24 15:23:36 +00002707
Blue Swirl8e4a4242013-01-06 18:30:17 +00002708#if !defined(CONFIG_USER_ONLY)
2709
2710/*
2711 * A helper function for the _utterly broken_ virtio device model to find out if
2712 * it's running on a big endian machine. Don't do this at home kids!
2713 */
2714bool virtio_is_big_endian(void);
2715bool virtio_is_big_endian(void)
2716{
2717#if defined(TARGET_WORDS_BIGENDIAN)
2718 return true;
2719#else
2720 return false;
2721#endif
2722}
2723
2724#endif
2725
Wen Congyang76f35532012-05-07 12:04:18 +08002726#ifndef CONFIG_USER_ONLY
Avi Kivitya8170e52012-10-23 12:30:10 +02002727bool cpu_physical_memory_is_io(hwaddr phys_addr)
Wen Congyang76f35532012-05-07 12:04:18 +08002728{
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002729 MemoryRegion*mr;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002730 hwaddr l = 1;
Wen Congyang76f35532012-05-07 12:04:18 +08002731
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002732 mr = address_space_translate(&address_space_memory,
2733 phys_addr, &phys_addr, &l, false);
Wen Congyang76f35532012-05-07 12:04:18 +08002734
Paolo Bonzini5c8a00c2013-05-29 12:42:00 +02002735 return !(memory_region_is_ram(mr) ||
2736 memory_region_is_romd(mr));
Wen Congyang76f35532012-05-07 12:04:18 +08002737}
Michael R. Hinesbd2fa512013-06-25 21:35:34 -04002738
2739void qemu_ram_foreach_block(RAMBlockIterFunc func, void *opaque)
2740{
2741 RAMBlock *block;
2742
2743 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
2744 func(block->host, block->offset, block->length, opaque);
2745 }
2746}
Peter Maydellec3f8c92013-06-27 20:53:38 +01002747#endif