blob: a59abc7a523727c382bbdd11017f695113cb4bdd [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"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010034#include "hw/xen/xen.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010035#include "qemu/timer.h"
36#include "qemu/config-file.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010037#include "exec/memory.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010038#include "sysemu/dma.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010039#include "exec/address-spaces.h"
pbrook53a59602006-03-25 19:31:22 +000040#if defined(CONFIG_USER_ONLY)
41#include <qemu.h>
Jun Nakajima432d2682010-08-31 16:41:25 +010042#else /* !CONFIG_USER_ONLY */
Paolo Bonzini9c17d612012-12-17 18:20:04 +010043#include "sysemu/xen-mapcache.h"
Stefano Stabellini6506e4f2011-05-19 18:35:44 +010044#include "trace.h"
pbrook53a59602006-03-25 19:31:22 +000045#endif
Paolo Bonzini0d6d3c82012-11-14 15:45:02 +010046#include "exec/cpu-all.h"
bellard54936002003-05-13 00:25:15 +000047
Paolo Bonzini022c62c2012-12-17 18:19:49 +010048#include "exec/cputlb.h"
Blue Swirl5b6dd862012-12-02 16:04:43 +000049#include "translate-all.h"
Blue Swirl0cac1b62012-04-09 16:50:52 +000050
Paolo Bonzini022c62c2012-12-17 18:19:49 +010051#include "exec/memory-internal.h"
Avi Kivity67d95c12011-12-15 15:25:22 +020052
blueswir1db7b5422007-05-26 17:36:03 +000053//#define DEBUG_SUBPAGE
ths1196be32007-03-17 15:17:58 +000054
pbrook99773bd2006-04-16 15:14:59 +000055#if !defined(CONFIG_USER_ONLY)
bellard9fa3e852004-01-04 18:06:42 +000056int phys_ram_fd;
aliguori74576192008-10-06 14:02:03 +000057static int in_migration;
pbrook94a6b542009-04-11 17:15:54 +000058
Paolo Bonzinia3161032012-11-14 15:54:48 +010059RAMList ram_list = { .blocks = QTAILQ_HEAD_INITIALIZER(ram_list.blocks) };
Avi Kivity62152b82011-07-26 14:26:14 +030060
61static MemoryRegion *system_memory;
Avi Kivity309cb472011-08-08 16:09:03 +030062static MemoryRegion *system_io;
Avi Kivity62152b82011-07-26 14:26:14 +030063
Avi Kivityf6790af2012-10-02 20:13:51 +020064AddressSpace address_space_io;
65AddressSpace address_space_memory;
Peter Maydell9e119082012-10-29 11:34:32 +100066DMAContext dma_context_memory;
Avi Kivity2673a5d2012-10-02 18:49:28 +020067
Paolo Bonzini0844e002013-05-24 14:37:28 +020068MemoryRegion io_mem_rom, io_mem_notdirty;
69static MemoryRegion io_mem_unassigned, io_mem_subpage_ram;
Avi Kivity0e0df1e2012-01-02 00:32:15 +020070
pbrooke2eef172008-06-08 01:09:01 +000071#endif
bellard9fa3e852004-01-04 18:06:42 +000072
Andreas Färber9349b4f2012-03-14 01:38:32 +010073CPUArchState *first_cpu;
bellard6a00d602005-11-21 23:25:50 +000074/* current CPU in the current thread. It is only valid inside
75 cpu_exec() */
Andreas Färber9349b4f2012-03-14 01:38:32 +010076DEFINE_TLS(CPUArchState *,cpu_single_env);
pbrook2e70f6e2008-06-29 01:03:05 +000077/* 0 = Do not count executed instructions.
thsbf20dc02008-06-30 17:22:19 +000078 1 = Precise instruction counting.
pbrook2e70f6e2008-06-29 01:03:05 +000079 2 = Adaptive rate instruction counting. */
Paolo Bonzini5708fc62012-11-26 15:36:40 +010080int use_icount;
bellard6a00d602005-11-21 23:25:50 +000081
pbrooke2eef172008-06-08 01:09:01 +000082#if !defined(CONFIG_USER_ONLY)
Avi Kivity4346ae32012-02-10 17:00:01 +020083
Paolo Bonzini1db8abb2013-05-21 12:07:21 +020084typedef struct PhysPageEntry PhysPageEntry;
85
86struct PhysPageEntry {
87 uint16_t is_leaf : 1;
88 /* index into phys_sections (is_leaf) or phys_map_nodes (!is_leaf) */
89 uint16_t ptr : 15;
90};
91
92struct AddressSpaceDispatch {
93 /* This is a multi-level map on the physical address space.
94 * The bottom level has pointers to MemoryRegionSections.
95 */
96 PhysPageEntry phys_map;
97 MemoryListener listener;
98};
99
Jan Kiszka90260c62013-05-26 21:46:51 +0200100#define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
101typedef struct subpage_t {
102 MemoryRegion iomem;
103 hwaddr base;
104 uint16_t sub_section[TARGET_PAGE_SIZE];
105} subpage_t;
106
Avi Kivity5312bd82012-02-12 18:32:55 +0200107static MemoryRegionSection *phys_sections;
108static unsigned phys_sections_nb, phys_sections_nb_alloc;
109static uint16_t phys_section_unassigned;
Avi Kivityaa102232012-03-08 17:06:55 +0200110static uint16_t phys_section_notdirty;
111static uint16_t phys_section_rom;
112static uint16_t phys_section_watch;
Avi Kivity5312bd82012-02-12 18:32:55 +0200113
Avi Kivityd6f2ea22012-02-12 20:12:49 +0200114/* Simple allocator for PhysPageEntry nodes */
115static PhysPageEntry (*phys_map_nodes)[L2_SIZE];
116static unsigned phys_map_nodes_nb, phys_map_nodes_nb_alloc;
117
Avi Kivity07f07b32012-02-13 20:45:32 +0200118#define PHYS_MAP_NODE_NIL (((uint16_t)~0) >> 1)
Avi Kivityd6f2ea22012-02-12 20:12:49 +0200119
pbrooke2eef172008-06-08 01:09:01 +0000120static void io_mem_init(void);
Avi Kivity62152b82011-07-26 14:26:14 +0300121static void memory_map_init(void);
Blue Swirl8b9c99d2012-10-28 11:04:51 +0000122static void *qemu_safe_ram_ptr(ram_addr_t addr);
pbrooke2eef172008-06-08 01:09:01 +0000123
Avi Kivity1ec9b902012-01-02 12:47:48 +0200124static MemoryRegion io_mem_watch;
pbrook6658ffb2007-03-16 23:58:11 +0000125#endif
bellard54936002003-05-13 00:25:15 +0000126
Paul Brook6d9a1302010-02-28 23:55:53 +0000127#if !defined(CONFIG_USER_ONLY)
Avi Kivityd6f2ea22012-02-12 20:12:49 +0200128
Avi Kivityf7bf5462012-02-13 20:12:05 +0200129static void phys_map_node_reserve(unsigned nodes)
130{
131 if (phys_map_nodes_nb + nodes > phys_map_nodes_nb_alloc) {
132 typedef PhysPageEntry Node[L2_SIZE];
133 phys_map_nodes_nb_alloc = MAX(phys_map_nodes_nb_alloc * 2, 16);
134 phys_map_nodes_nb_alloc = MAX(phys_map_nodes_nb_alloc,
135 phys_map_nodes_nb + nodes);
136 phys_map_nodes = g_renew(Node, phys_map_nodes,
137 phys_map_nodes_nb_alloc);
138 }
139}
140
141static uint16_t phys_map_node_alloc(void)
Avi Kivityd6f2ea22012-02-12 20:12:49 +0200142{
143 unsigned i;
144 uint16_t ret;
145
Avi Kivityf7bf5462012-02-13 20:12:05 +0200146 ret = phys_map_nodes_nb++;
Avi Kivityd6f2ea22012-02-12 20:12:49 +0200147 assert(ret != PHYS_MAP_NODE_NIL);
Avi Kivityf7bf5462012-02-13 20:12:05 +0200148 assert(ret != phys_map_nodes_nb_alloc);
Avi Kivityd6f2ea22012-02-12 20:12:49 +0200149 for (i = 0; i < L2_SIZE; ++i) {
Avi Kivity07f07b32012-02-13 20:45:32 +0200150 phys_map_nodes[ret][i].is_leaf = 0;
Avi Kivityc19e8802012-02-13 20:25:31 +0200151 phys_map_nodes[ret][i].ptr = PHYS_MAP_NODE_NIL;
Avi Kivityd6f2ea22012-02-12 20:12:49 +0200152 }
Avi Kivityf7bf5462012-02-13 20:12:05 +0200153 return ret;
Avi Kivityd6f2ea22012-02-12 20:12:49 +0200154}
155
156static void phys_map_nodes_reset(void)
157{
158 phys_map_nodes_nb = 0;
159}
160
Avi Kivityf7bf5462012-02-13 20:12:05 +0200161
Avi Kivitya8170e52012-10-23 12:30:10 +0200162static void phys_page_set_level(PhysPageEntry *lp, hwaddr *index,
163 hwaddr *nb, uint16_t leaf,
Avi Kivity29990972012-02-13 20:21:20 +0200164 int level)
Avi Kivityf7bf5462012-02-13 20:12:05 +0200165{
166 PhysPageEntry *p;
167 int i;
Avi Kivitya8170e52012-10-23 12:30:10 +0200168 hwaddr step = (hwaddr)1 << (level * L2_BITS);
Avi Kivityf7bf5462012-02-13 20:12:05 +0200169
Avi Kivity07f07b32012-02-13 20:45:32 +0200170 if (!lp->is_leaf && lp->ptr == PHYS_MAP_NODE_NIL) {
Avi Kivityc19e8802012-02-13 20:25:31 +0200171 lp->ptr = phys_map_node_alloc();
172 p = phys_map_nodes[lp->ptr];
Avi Kivityf7bf5462012-02-13 20:12:05 +0200173 if (level == 0) {
174 for (i = 0; i < L2_SIZE; i++) {
Avi Kivity07f07b32012-02-13 20:45:32 +0200175 p[i].is_leaf = 1;
Avi Kivityc19e8802012-02-13 20:25:31 +0200176 p[i].ptr = phys_section_unassigned;
Avi Kivityf7bf5462012-02-13 20:12:05 +0200177 }
178 }
179 } else {
Avi Kivityc19e8802012-02-13 20:25:31 +0200180 p = phys_map_nodes[lp->ptr];
Avi Kivityf7bf5462012-02-13 20:12:05 +0200181 }
Avi Kivity29990972012-02-13 20:21:20 +0200182 lp = &p[(*index >> (level * L2_BITS)) & (L2_SIZE - 1)];
Avi Kivityf7bf5462012-02-13 20:12:05 +0200183
Avi Kivity29990972012-02-13 20:21:20 +0200184 while (*nb && lp < &p[L2_SIZE]) {
Avi Kivity07f07b32012-02-13 20:45:32 +0200185 if ((*index & (step - 1)) == 0 && *nb >= step) {
186 lp->is_leaf = true;
Avi Kivityc19e8802012-02-13 20:25:31 +0200187 lp->ptr = leaf;
Avi Kivity07f07b32012-02-13 20:45:32 +0200188 *index += step;
189 *nb -= step;
Avi Kivity29990972012-02-13 20:21:20 +0200190 } else {
191 phys_page_set_level(lp, index, nb, leaf, level - 1);
192 }
193 ++lp;
Avi Kivityf7bf5462012-02-13 20:12:05 +0200194 }
195}
196
Avi Kivityac1970f2012-10-03 16:22:53 +0200197static void phys_page_set(AddressSpaceDispatch *d,
Avi Kivitya8170e52012-10-23 12:30:10 +0200198 hwaddr index, hwaddr nb,
Avi Kivity29990972012-02-13 20:21:20 +0200199 uint16_t leaf)
bellard92e873b2004-05-21 14:52:29 +0000200{
Avi Kivity29990972012-02-13 20:21:20 +0200201 /* Wildly overreserve - it doesn't matter much. */
Avi Kivity07f07b32012-02-13 20:45:32 +0200202 phys_map_node_reserve(3 * P_L2_LEVELS);
bellard92e873b2004-05-21 14:52:29 +0000203
Avi Kivityac1970f2012-10-03 16:22:53 +0200204 phys_page_set_level(&d->phys_map, &index, &nb, leaf, P_L2_LEVELS - 1);
bellard92e873b2004-05-21 14:52:29 +0000205}
206
Paolo Bonzini149f54b2013-05-24 12:59:37 +0200207static MemoryRegionSection *phys_page_find(AddressSpaceDispatch *d, hwaddr index)
bellard92e873b2004-05-21 14:52:29 +0000208{
Avi Kivityac1970f2012-10-03 16:22:53 +0200209 PhysPageEntry lp = d->phys_map;
Avi Kivity31ab2b42012-02-13 16:44:19 +0200210 PhysPageEntry *p;
211 int i;
Avi Kivityf1f6e3b2011-11-20 17:52:22 +0200212
Avi Kivity07f07b32012-02-13 20:45:32 +0200213 for (i = P_L2_LEVELS - 1; i >= 0 && !lp.is_leaf; i--) {
Avi Kivityc19e8802012-02-13 20:25:31 +0200214 if (lp.ptr == PHYS_MAP_NODE_NIL) {
Paolo Bonzinifd298932013-05-20 12:21:07 +0200215 return &phys_sections[phys_section_unassigned];
Avi Kivity31ab2b42012-02-13 16:44:19 +0200216 }
Avi Kivityc19e8802012-02-13 20:25:31 +0200217 p = phys_map_nodes[lp.ptr];
Avi Kivity31ab2b42012-02-13 16:44:19 +0200218 lp = p[(index >> (i * L2_BITS)) & (L2_SIZE - 1)];
Avi Kivityf1f6e3b2011-11-20 17:52:22 +0200219 }
Paolo Bonzinifd298932013-05-20 12:21:07 +0200220 return &phys_sections[lp.ptr];
Avi Kivityf3705d52012-03-08 16:16:34 +0200221}
222
Blue Swirle5548612012-04-21 13:08:33 +0000223bool memory_region_is_unassigned(MemoryRegion *mr)
224{
Paolo Bonzini2a8e7492013-05-24 14:34:08 +0200225 return mr != &io_mem_rom && mr != &io_mem_notdirty && !mr->rom_device
Blue Swirle5548612012-04-21 13:08:33 +0000226 && mr != &io_mem_watch;
227}
Paolo Bonzini149f54b2013-05-24 12:59:37 +0200228
Jan Kiszka9f029602013-05-06 16:48:02 +0200229static MemoryRegionSection *address_space_lookup_region(AddressSpace *as,
Jan Kiszka90260c62013-05-26 21:46:51 +0200230 hwaddr addr,
231 bool resolve_subpage)
Jan Kiszka9f029602013-05-06 16:48:02 +0200232{
Jan Kiszka90260c62013-05-26 21:46:51 +0200233 MemoryRegionSection *section;
234 subpage_t *subpage;
235
236 section = phys_page_find(as->dispatch, addr >> TARGET_PAGE_BITS);
237 if (resolve_subpage && section->mr->subpage) {
238 subpage = container_of(section->mr, subpage_t, iomem);
239 section = &phys_sections[subpage->sub_section[SUBPAGE_IDX(addr)]];
240 }
241 return section;
Jan Kiszka9f029602013-05-06 16:48:02 +0200242}
243
Jan Kiszka90260c62013-05-26 21:46:51 +0200244static MemoryRegionSection *
245address_space_translate_internal(AddressSpace *as, hwaddr addr, hwaddr *xlat,
246 hwaddr *plen, bool resolve_subpage)
Paolo Bonzini149f54b2013-05-24 12:59:37 +0200247{
248 MemoryRegionSection *section;
249 Int128 diff;
250
Jan Kiszka90260c62013-05-26 21:46:51 +0200251 section = address_space_lookup_region(as, addr, resolve_subpage);
Paolo Bonzini149f54b2013-05-24 12:59:37 +0200252 /* Compute offset within MemoryRegionSection */
253 addr -= section->offset_within_address_space;
254
255 /* Compute offset within MemoryRegion */
256 *xlat = addr + section->offset_within_region;
257
258 diff = int128_sub(section->mr->size, int128_make64(addr));
Peter Maydell3752a032013-06-20 15:18:04 +0100259 *plen = int128_get64(int128_min(diff, int128_make64(*plen)));
Paolo Bonzini149f54b2013-05-24 12:59:37 +0200260 return section;
261}
Jan Kiszka90260c62013-05-26 21:46:51 +0200262
263MemoryRegionSection *address_space_translate(AddressSpace *as, hwaddr addr,
264 hwaddr *xlat, hwaddr *plen,
265 bool is_write)
266{
267 return address_space_translate_internal(as, addr, xlat, plen, true);
268}
269
270MemoryRegionSection *
271address_space_translate_for_iotlb(AddressSpace *as, hwaddr addr, hwaddr *xlat,
272 hwaddr *plen)
273{
274 return address_space_translate_internal(as, addr, xlat, plen, false);
275}
bellard9fa3e852004-01-04 18:06:42 +0000276#endif
bellardfd6ce8f2003-05-14 19:00:11 +0000277
Jan Kiszkad5ab9712011-08-02 16:10:21 +0200278void cpu_exec_init_all(void)
279{
280#if !defined(CONFIG_USER_ONLY)
Umesh Deshpandeb2a86582011-08-17 00:01:33 -0700281 qemu_mutex_init(&ram_list.mutex);
Jan Kiszkad5ab9712011-08-02 16:10:21 +0200282 memory_map_init();
283 io_mem_init();
284#endif
285}
286
Andreas Färberb170fce2013-01-20 20:23:22 +0100287#if !defined(CONFIG_USER_ONLY)
pbrook9656f322008-07-01 20:01:19 +0000288
Juan Quintelae59fb372009-09-29 22:48:21 +0200289static int cpu_common_post_load(void *opaque, int version_id)
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200290{
Andreas Färber259186a2013-01-17 18:51:17 +0100291 CPUState *cpu = opaque;
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200292
aurel323098dba2009-03-07 21:28:24 +0000293 /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
294 version_id is increased. */
Andreas Färber259186a2013-01-17 18:51:17 +0100295 cpu->interrupt_request &= ~0x01;
296 tlb_flush(cpu->env_ptr, 1);
pbrook9656f322008-07-01 20:01:19 +0000297
298 return 0;
299}
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200300
301static const VMStateDescription vmstate_cpu_common = {
302 .name = "cpu_common",
303 .version_id = 1,
304 .minimum_version_id = 1,
305 .minimum_version_id_old = 1,
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200306 .post_load = cpu_common_post_load,
307 .fields = (VMStateField []) {
Andreas Färber259186a2013-01-17 18:51:17 +0100308 VMSTATE_UINT32(halted, CPUState),
309 VMSTATE_UINT32(interrupt_request, CPUState),
Juan Quintelae7f4eff2009-09-10 03:04:33 +0200310 VMSTATE_END_OF_LIST()
311 }
312};
Andreas Färberb170fce2013-01-20 20:23:22 +0100313#else
314#define vmstate_cpu_common vmstate_dummy
pbrook9656f322008-07-01 20:01:19 +0000315#endif
316
Andreas Färber38d8f5c2012-12-17 19:47:15 +0100317CPUState *qemu_get_cpu(int index)
Glauber Costa950f1472009-06-09 12:15:18 -0400318{
Andreas Färber9349b4f2012-03-14 01:38:32 +0100319 CPUArchState *env = first_cpu;
Andreas Färber38d8f5c2012-12-17 19:47:15 +0100320 CPUState *cpu = NULL;
Glauber Costa950f1472009-06-09 12:15:18 -0400321
322 while (env) {
Andreas Färber55e5c282012-12-17 06:18:02 +0100323 cpu = ENV_GET_CPU(env);
324 if (cpu->cpu_index == index) {
Glauber Costa950f1472009-06-09 12:15:18 -0400325 break;
Andreas Färber55e5c282012-12-17 06:18:02 +0100326 }
Glauber Costa950f1472009-06-09 12:15:18 -0400327 env = env->next_cpu;
328 }
329
Igor Mammedovd76fdda2013-03-07 19:12:43 +0100330 return env ? cpu : NULL;
Glauber Costa950f1472009-06-09 12:15:18 -0400331}
332
Michael S. Tsirkind6b9e0d2013-04-24 22:58:04 +0200333void qemu_for_each_cpu(void (*func)(CPUState *cpu, void *data), void *data)
334{
335 CPUArchState *env = first_cpu;
336
337 while (env) {
338 func(ENV_GET_CPU(env), data);
339 env = env->next_cpu;
340 }
341}
342
Andreas Färber9349b4f2012-03-14 01:38:32 +0100343void cpu_exec_init(CPUArchState *env)
bellardfd6ce8f2003-05-14 19:00:11 +0000344{
Andreas Färber9f09e182012-05-03 06:59:07 +0200345 CPUState *cpu = ENV_GET_CPU(env);
Andreas Färberb170fce2013-01-20 20:23:22 +0100346 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber9349b4f2012-03-14 01:38:32 +0100347 CPUArchState **penv;
bellard6a00d602005-11-21 23:25:50 +0000348 int cpu_index;
349
pbrookc2764712009-03-07 15:24:59 +0000350#if defined(CONFIG_USER_ONLY)
351 cpu_list_lock();
352#endif
bellard6a00d602005-11-21 23:25:50 +0000353 env->next_cpu = NULL;
354 penv = &first_cpu;
355 cpu_index = 0;
356 while (*penv != NULL) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700357 penv = &(*penv)->next_cpu;
bellard6a00d602005-11-21 23:25:50 +0000358 cpu_index++;
359 }
Andreas Färber55e5c282012-12-17 06:18:02 +0100360 cpu->cpu_index = cpu_index;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +0100361 cpu->numa_node = 0;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000362 QTAILQ_INIT(&env->breakpoints);
363 QTAILQ_INIT(&env->watchpoints);
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100364#ifndef CONFIG_USER_ONLY
Andreas Färber9f09e182012-05-03 06:59:07 +0200365 cpu->thread_id = qemu_get_thread_id();
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100366#endif
bellard6a00d602005-11-21 23:25:50 +0000367 *penv = env;
pbrookc2764712009-03-07 15:24:59 +0000368#if defined(CONFIG_USER_ONLY)
369 cpu_list_unlock();
370#endif
Andreas Färber259186a2013-01-17 18:51:17 +0100371 vmstate_register(NULL, cpu_index, &vmstate_cpu_common, cpu);
pbrookb3c77242008-06-30 16:31:04 +0000372#if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
Alex Williamson0be71e32010-06-25 11:09:07 -0600373 register_savevm(NULL, "cpu", cpu_index, CPU_SAVE_VERSION,
pbrookb3c77242008-06-30 16:31:04 +0000374 cpu_save, cpu_load, env);
Andreas Färberb170fce2013-01-20 20:23:22 +0100375 assert(cc->vmsd == NULL);
pbrookb3c77242008-06-30 16:31:04 +0000376#endif
Andreas Färberb170fce2013-01-20 20:23:22 +0100377 if (cc->vmsd != NULL) {
378 vmstate_register(NULL, cpu_index, cc->vmsd, cpu);
379 }
bellardfd6ce8f2003-05-14 19:00:11 +0000380}
381
bellard1fddef42005-04-17 19:16:13 +0000382#if defined(TARGET_HAS_ICE)
Paul Brook94df27f2010-02-28 23:47:45 +0000383#if defined(CONFIG_USER_ONLY)
Andreas Färber9349b4f2012-03-14 01:38:32 +0100384static void breakpoint_invalidate(CPUArchState *env, target_ulong pc)
Paul Brook94df27f2010-02-28 23:47:45 +0000385{
386 tb_invalidate_phys_page_range(pc, pc + 1, 0);
387}
388#else
Max Filippov1e7855a2012-04-10 02:48:17 +0400389static void breakpoint_invalidate(CPUArchState *env, target_ulong pc)
390{
Max Filippov9d70c4b2012-05-27 20:21:08 +0400391 tb_invalidate_phys_addr(cpu_get_phys_page_debug(env, pc) |
392 (pc & ~TARGET_PAGE_MASK));
Max Filippov1e7855a2012-04-10 02:48:17 +0400393}
bellardc27004e2005-01-03 23:35:10 +0000394#endif
Paul Brook94df27f2010-02-28 23:47:45 +0000395#endif /* TARGET_HAS_ICE */
bellardd720b932004-04-25 17:57:43 +0000396
Paul Brookc527ee82010-03-01 03:31:14 +0000397#if defined(CONFIG_USER_ONLY)
Andreas Färber9349b4f2012-03-14 01:38:32 +0100398void cpu_watchpoint_remove_all(CPUArchState *env, int mask)
Paul Brookc527ee82010-03-01 03:31:14 +0000399
400{
401}
402
Andreas Färber9349b4f2012-03-14 01:38:32 +0100403int cpu_watchpoint_insert(CPUArchState *env, target_ulong addr, target_ulong len,
Paul Brookc527ee82010-03-01 03:31:14 +0000404 int flags, CPUWatchpoint **watchpoint)
405{
406 return -ENOSYS;
407}
408#else
pbrook6658ffb2007-03-16 23:58:11 +0000409/* Add a watchpoint. */
Andreas Färber9349b4f2012-03-14 01:38:32 +0100410int cpu_watchpoint_insert(CPUArchState *env, target_ulong addr, target_ulong len,
aliguoria1d1bb32008-11-18 20:07:32 +0000411 int flags, CPUWatchpoint **watchpoint)
pbrook6658ffb2007-03-16 23:58:11 +0000412{
aliguorib4051332008-11-18 20:14:20 +0000413 target_ulong len_mask = ~(len - 1);
aliguoric0ce9982008-11-25 22:13:57 +0000414 CPUWatchpoint *wp;
pbrook6658ffb2007-03-16 23:58:11 +0000415
aliguorib4051332008-11-18 20:14:20 +0000416 /* sanity checks: allow power-of-2 lengths, deny unaligned watchpoints */
Max Filippov0dc23822012-01-29 03:15:23 +0400417 if ((len & (len - 1)) || (addr & ~len_mask) ||
418 len == 0 || len > TARGET_PAGE_SIZE) {
aliguorib4051332008-11-18 20:14:20 +0000419 fprintf(stderr, "qemu: tried to set invalid watchpoint at "
420 TARGET_FMT_lx ", len=" TARGET_FMT_lu "\n", addr, len);
421 return -EINVAL;
422 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500423 wp = g_malloc(sizeof(*wp));
pbrook6658ffb2007-03-16 23:58:11 +0000424
aliguoria1d1bb32008-11-18 20:07:32 +0000425 wp->vaddr = addr;
aliguorib4051332008-11-18 20:14:20 +0000426 wp->len_mask = len_mask;
aliguoria1d1bb32008-11-18 20:07:32 +0000427 wp->flags = flags;
428
aliguori2dc9f412008-11-18 20:56:59 +0000429 /* keep all GDB-injected watchpoints in front */
aliguoric0ce9982008-11-25 22:13:57 +0000430 if (flags & BP_GDB)
Blue Swirl72cf2d42009-09-12 07:36:22 +0000431 QTAILQ_INSERT_HEAD(&env->watchpoints, wp, entry);
aliguoric0ce9982008-11-25 22:13:57 +0000432 else
Blue Swirl72cf2d42009-09-12 07:36:22 +0000433 QTAILQ_INSERT_TAIL(&env->watchpoints, wp, entry);
aliguoria1d1bb32008-11-18 20:07:32 +0000434
pbrook6658ffb2007-03-16 23:58:11 +0000435 tlb_flush_page(env, addr);
aliguoria1d1bb32008-11-18 20:07:32 +0000436
437 if (watchpoint)
438 *watchpoint = wp;
439 return 0;
pbrook6658ffb2007-03-16 23:58:11 +0000440}
441
aliguoria1d1bb32008-11-18 20:07:32 +0000442/* Remove a specific watchpoint. */
Andreas Färber9349b4f2012-03-14 01:38:32 +0100443int cpu_watchpoint_remove(CPUArchState *env, target_ulong addr, target_ulong len,
aliguoria1d1bb32008-11-18 20:07:32 +0000444 int flags)
pbrook6658ffb2007-03-16 23:58:11 +0000445{
aliguorib4051332008-11-18 20:14:20 +0000446 target_ulong len_mask = ~(len - 1);
aliguoria1d1bb32008-11-18 20:07:32 +0000447 CPUWatchpoint *wp;
pbrook6658ffb2007-03-16 23:58:11 +0000448
Blue Swirl72cf2d42009-09-12 07:36:22 +0000449 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
aliguorib4051332008-11-18 20:14:20 +0000450 if (addr == wp->vaddr && len_mask == wp->len_mask
aliguori6e140f22008-11-18 20:37:55 +0000451 && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) {
aliguoria1d1bb32008-11-18 20:07:32 +0000452 cpu_watchpoint_remove_by_ref(env, wp);
pbrook6658ffb2007-03-16 23:58:11 +0000453 return 0;
454 }
455 }
aliguoria1d1bb32008-11-18 20:07:32 +0000456 return -ENOENT;
pbrook6658ffb2007-03-16 23:58:11 +0000457}
458
aliguoria1d1bb32008-11-18 20:07:32 +0000459/* Remove a specific watchpoint by reference. */
Andreas Färber9349b4f2012-03-14 01:38:32 +0100460void cpu_watchpoint_remove_by_ref(CPUArchState *env, CPUWatchpoint *watchpoint)
aliguoria1d1bb32008-11-18 20:07:32 +0000461{
Blue Swirl72cf2d42009-09-12 07:36:22 +0000462 QTAILQ_REMOVE(&env->watchpoints, watchpoint, entry);
edgar_igl7d03f822008-05-17 18:58:29 +0000463
aliguoria1d1bb32008-11-18 20:07:32 +0000464 tlb_flush_page(env, watchpoint->vaddr);
465
Anthony Liguori7267c092011-08-20 22:09:37 -0500466 g_free(watchpoint);
edgar_igl7d03f822008-05-17 18:58:29 +0000467}
468
aliguoria1d1bb32008-11-18 20:07:32 +0000469/* Remove all matching watchpoints. */
Andreas Färber9349b4f2012-03-14 01:38:32 +0100470void cpu_watchpoint_remove_all(CPUArchState *env, int mask)
aliguoria1d1bb32008-11-18 20:07:32 +0000471{
aliguoric0ce9982008-11-25 22:13:57 +0000472 CPUWatchpoint *wp, *next;
aliguoria1d1bb32008-11-18 20:07:32 +0000473
Blue Swirl72cf2d42009-09-12 07:36:22 +0000474 QTAILQ_FOREACH_SAFE(wp, &env->watchpoints, entry, next) {
aliguoria1d1bb32008-11-18 20:07:32 +0000475 if (wp->flags & mask)
476 cpu_watchpoint_remove_by_ref(env, wp);
aliguoric0ce9982008-11-25 22:13:57 +0000477 }
aliguoria1d1bb32008-11-18 20:07:32 +0000478}
Paul Brookc527ee82010-03-01 03:31:14 +0000479#endif
aliguoria1d1bb32008-11-18 20:07:32 +0000480
481/* Add a breakpoint. */
Andreas Färber9349b4f2012-03-14 01:38:32 +0100482int cpu_breakpoint_insert(CPUArchState *env, target_ulong pc, int flags,
aliguoria1d1bb32008-11-18 20:07:32 +0000483 CPUBreakpoint **breakpoint)
bellard4c3a88a2003-07-26 12:06:08 +0000484{
bellard1fddef42005-04-17 19:16:13 +0000485#if defined(TARGET_HAS_ICE)
aliguoric0ce9982008-11-25 22:13:57 +0000486 CPUBreakpoint *bp;
ths3b46e622007-09-17 08:09:54 +0000487
Anthony Liguori7267c092011-08-20 22:09:37 -0500488 bp = g_malloc(sizeof(*bp));
aliguoria1d1bb32008-11-18 20:07:32 +0000489
490 bp->pc = pc;
491 bp->flags = flags;
492
aliguori2dc9f412008-11-18 20:56:59 +0000493 /* keep all GDB-injected breakpoints in front */
aliguoric0ce9982008-11-25 22:13:57 +0000494 if (flags & BP_GDB)
Blue Swirl72cf2d42009-09-12 07:36:22 +0000495 QTAILQ_INSERT_HEAD(&env->breakpoints, bp, entry);
aliguoric0ce9982008-11-25 22:13:57 +0000496 else
Blue Swirl72cf2d42009-09-12 07:36:22 +0000497 QTAILQ_INSERT_TAIL(&env->breakpoints, bp, entry);
aliguoria1d1bb32008-11-18 20:07:32 +0000498
499 breakpoint_invalidate(env, pc);
500
501 if (breakpoint)
502 *breakpoint = bp;
503 return 0;
504#else
505 return -ENOSYS;
506#endif
507}
508
509/* Remove a specific breakpoint. */
Andreas Färber9349b4f2012-03-14 01:38:32 +0100510int cpu_breakpoint_remove(CPUArchState *env, target_ulong pc, int flags)
aliguoria1d1bb32008-11-18 20:07:32 +0000511{
512#if defined(TARGET_HAS_ICE)
513 CPUBreakpoint *bp;
514
Blue Swirl72cf2d42009-09-12 07:36:22 +0000515 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
aliguoria1d1bb32008-11-18 20:07:32 +0000516 if (bp->pc == pc && bp->flags == flags) {
517 cpu_breakpoint_remove_by_ref(env, bp);
bellard4c3a88a2003-07-26 12:06:08 +0000518 return 0;
aliguoria1d1bb32008-11-18 20:07:32 +0000519 }
bellard4c3a88a2003-07-26 12:06:08 +0000520 }
aliguoria1d1bb32008-11-18 20:07:32 +0000521 return -ENOENT;
bellard4c3a88a2003-07-26 12:06:08 +0000522#else
aliguoria1d1bb32008-11-18 20:07:32 +0000523 return -ENOSYS;
bellard4c3a88a2003-07-26 12:06:08 +0000524#endif
525}
526
aliguoria1d1bb32008-11-18 20:07:32 +0000527/* Remove a specific breakpoint by reference. */
Andreas Färber9349b4f2012-03-14 01:38:32 +0100528void cpu_breakpoint_remove_by_ref(CPUArchState *env, CPUBreakpoint *breakpoint)
bellard4c3a88a2003-07-26 12:06:08 +0000529{
bellard1fddef42005-04-17 19:16:13 +0000530#if defined(TARGET_HAS_ICE)
Blue Swirl72cf2d42009-09-12 07:36:22 +0000531 QTAILQ_REMOVE(&env->breakpoints, breakpoint, entry);
bellardd720b932004-04-25 17:57:43 +0000532
aliguoria1d1bb32008-11-18 20:07:32 +0000533 breakpoint_invalidate(env, breakpoint->pc);
534
Anthony Liguori7267c092011-08-20 22:09:37 -0500535 g_free(breakpoint);
aliguoria1d1bb32008-11-18 20:07:32 +0000536#endif
537}
538
539/* Remove all matching breakpoints. */
Andreas Färber9349b4f2012-03-14 01:38:32 +0100540void cpu_breakpoint_remove_all(CPUArchState *env, int mask)
aliguoria1d1bb32008-11-18 20:07:32 +0000541{
542#if defined(TARGET_HAS_ICE)
aliguoric0ce9982008-11-25 22:13:57 +0000543 CPUBreakpoint *bp, *next;
aliguoria1d1bb32008-11-18 20:07:32 +0000544
Blue Swirl72cf2d42009-09-12 07:36:22 +0000545 QTAILQ_FOREACH_SAFE(bp, &env->breakpoints, entry, next) {
aliguoria1d1bb32008-11-18 20:07:32 +0000546 if (bp->flags & mask)
547 cpu_breakpoint_remove_by_ref(env, bp);
aliguoric0ce9982008-11-25 22:13:57 +0000548 }
bellard4c3a88a2003-07-26 12:06:08 +0000549#endif
550}
551
bellardc33a3462003-07-29 20:50:33 +0000552/* enable or disable single step mode. EXCP_DEBUG is returned by the
553 CPU loop after each instruction */
Andreas Färber9349b4f2012-03-14 01:38:32 +0100554void cpu_single_step(CPUArchState *env, int enabled)
bellardc33a3462003-07-29 20:50:33 +0000555{
bellard1fddef42005-04-17 19:16:13 +0000556#if defined(TARGET_HAS_ICE)
bellardc33a3462003-07-29 20:50:33 +0000557 if (env->singlestep_enabled != enabled) {
558 env->singlestep_enabled = enabled;
aliguorie22a25c2009-03-12 20:12:48 +0000559 if (kvm_enabled())
560 kvm_update_guest_debug(env, 0);
561 else {
Stuart Bradyccbb4d42009-05-03 12:15:06 +0100562 /* must flush all the translated code to avoid inconsistencies */
aliguorie22a25c2009-03-12 20:12:48 +0000563 /* XXX: only flush what is necessary */
564 tb_flush(env);
565 }
bellardc33a3462003-07-29 20:50:33 +0000566 }
567#endif
568}
569
Andreas Färber9349b4f2012-03-14 01:38:32 +0100570void cpu_exit(CPUArchState *env)
aurel323098dba2009-03-07 21:28:24 +0000571{
Andreas Färberfcd7d002012-12-17 08:02:44 +0100572 CPUState *cpu = ENV_GET_CPU(env);
573
574 cpu->exit_request = 1;
Peter Maydell378df4b2013-02-22 18:10:03 +0000575 cpu->tcg_exit_req = 1;
aurel323098dba2009-03-07 21:28:24 +0000576}
577
Andreas Färber9349b4f2012-03-14 01:38:32 +0100578void cpu_abort(CPUArchState *env, const char *fmt, ...)
bellard75012672003-06-21 13:11:07 +0000579{
580 va_list ap;
pbrook493ae1f2007-11-23 16:53:59 +0000581 va_list ap2;
bellard75012672003-06-21 13:11:07 +0000582
583 va_start(ap, fmt);
pbrook493ae1f2007-11-23 16:53:59 +0000584 va_copy(ap2, ap);
bellard75012672003-06-21 13:11:07 +0000585 fprintf(stderr, "qemu: fatal: ");
586 vfprintf(stderr, fmt, ap);
587 fprintf(stderr, "\n");
Peter Maydell6fd2a022012-10-05 15:04:43 +0100588 cpu_dump_state(env, stderr, fprintf, CPU_DUMP_FPU | CPU_DUMP_CCOP);
aliguori93fcfe32009-01-15 22:34:14 +0000589 if (qemu_log_enabled()) {
590 qemu_log("qemu: fatal: ");
591 qemu_log_vprintf(fmt, ap2);
592 qemu_log("\n");
Peter Maydell6fd2a022012-10-05 15:04:43 +0100593 log_cpu_state(env, CPU_DUMP_FPU | CPU_DUMP_CCOP);
aliguori31b1a7b2009-01-15 22:35:09 +0000594 qemu_log_flush();
aliguori93fcfe32009-01-15 22:34:14 +0000595 qemu_log_close();
balrog924edca2007-06-10 14:07:13 +0000596 }
pbrook493ae1f2007-11-23 16:53:59 +0000597 va_end(ap2);
j_mayerf9373292007-09-29 12:18:20 +0000598 va_end(ap);
Riku Voipiofd052bf2010-01-25 14:30:49 +0200599#if defined(CONFIG_USER_ONLY)
600 {
601 struct sigaction act;
602 sigfillset(&act.sa_mask);
603 act.sa_handler = SIG_DFL;
604 sigaction(SIGABRT, &act, NULL);
605 }
606#endif
bellard75012672003-06-21 13:11:07 +0000607 abort();
608}
609
Andreas Färber9349b4f2012-03-14 01:38:32 +0100610CPUArchState *cpu_copy(CPUArchState *env)
thsc5be9f02007-02-28 20:20:53 +0000611{
Andreas Färber9349b4f2012-03-14 01:38:32 +0100612 CPUArchState *new_env = cpu_init(env->cpu_model_str);
613 CPUArchState *next_cpu = new_env->next_cpu;
aliguori5a38f082009-01-15 20:16:51 +0000614#if defined(TARGET_HAS_ICE)
615 CPUBreakpoint *bp;
616 CPUWatchpoint *wp;
617#endif
618
Andreas Färber9349b4f2012-03-14 01:38:32 +0100619 memcpy(new_env, env, sizeof(CPUArchState));
aliguori5a38f082009-01-15 20:16:51 +0000620
Andreas Färber55e5c282012-12-17 06:18:02 +0100621 /* Preserve chaining. */
thsc5be9f02007-02-28 20:20:53 +0000622 new_env->next_cpu = next_cpu;
aliguori5a38f082009-01-15 20:16:51 +0000623
624 /* Clone all break/watchpoints.
625 Note: Once we support ptrace with hw-debug register access, make sure
626 BP_CPU break/watchpoints are handled correctly on clone. */
Blue Swirl72cf2d42009-09-12 07:36:22 +0000627 QTAILQ_INIT(&env->breakpoints);
628 QTAILQ_INIT(&env->watchpoints);
aliguori5a38f082009-01-15 20:16:51 +0000629#if defined(TARGET_HAS_ICE)
Blue Swirl72cf2d42009-09-12 07:36:22 +0000630 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
aliguori5a38f082009-01-15 20:16:51 +0000631 cpu_breakpoint_insert(new_env, bp->pc, bp->flags, NULL);
632 }
Blue Swirl72cf2d42009-09-12 07:36:22 +0000633 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
aliguori5a38f082009-01-15 20:16:51 +0000634 cpu_watchpoint_insert(new_env, wp->vaddr, (~wp->len_mask) + 1,
635 wp->flags, NULL);
636 }
637#endif
638
thsc5be9f02007-02-28 20:20:53 +0000639 return new_env;
640}
641
bellard01243112004-01-04 15:48:17 +0000642#if !defined(CONFIG_USER_ONLY)
Juan Quintelad24981d2012-05-22 00:42:40 +0200643static void tlb_reset_dirty_range_all(ram_addr_t start, ram_addr_t end,
644 uintptr_t length)
bellard1ccde1c2004-02-06 19:46:14 +0000645{
Juan Quintelad24981d2012-05-22 00:42:40 +0200646 uintptr_t start1;
bellardf23db162005-08-21 19:12:28 +0000647
bellard1ccde1c2004-02-06 19:46:14 +0000648 /* we modify the TLB cache so that the dirty bit will be set again
649 when accessing the range */
Stefan Weil8efe0ca2012-04-12 15:42:19 +0200650 start1 = (uintptr_t)qemu_safe_ram_ptr(start);
Stefan Weila57d23e2011-04-30 22:49:26 +0200651 /* Check that we don't span multiple blocks - this breaks the
pbrook5579c7f2009-04-11 14:47:08 +0000652 address comparisons below. */
Stefan Weil8efe0ca2012-04-12 15:42:19 +0200653 if ((uintptr_t)qemu_safe_ram_ptr(end - 1) - start1
pbrook5579c7f2009-04-11 14:47:08 +0000654 != (end - 1) - start) {
655 abort();
656 }
Blue Swirle5548612012-04-21 13:08:33 +0000657 cpu_tlb_reset_dirty_all(start1, length);
Juan Quintelad24981d2012-05-22 00:42:40 +0200658
659}
660
661/* Note: start and end must be within the same ram block. */
662void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
663 int dirty_flags)
664{
665 uintptr_t length;
666
667 start &= TARGET_PAGE_MASK;
668 end = TARGET_PAGE_ALIGN(end);
669
670 length = end - start;
671 if (length == 0)
672 return;
673 cpu_physical_memory_mask_dirty_range(start, length, dirty_flags);
674
675 if (tcg_enabled()) {
676 tlb_reset_dirty_range_all(start, end, length);
677 }
bellard1ccde1c2004-02-06 19:46:14 +0000678}
679
Blue Swirl8b9c99d2012-10-28 11:04:51 +0000680static int cpu_physical_memory_set_dirty_tracking(int enable)
aliguori74576192008-10-06 14:02:03 +0000681{
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +0200682 int ret = 0;
aliguori74576192008-10-06 14:02:03 +0000683 in_migration = enable;
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +0200684 return ret;
aliguori74576192008-10-06 14:02:03 +0000685}
686
Avi Kivitya8170e52012-10-23 12:30:10 +0200687hwaddr memory_region_section_get_iotlb(CPUArchState *env,
Paolo Bonzini149f54b2013-05-24 12:59:37 +0200688 MemoryRegionSection *section,
689 target_ulong vaddr,
690 hwaddr paddr, hwaddr xlat,
691 int prot,
692 target_ulong *address)
Blue Swirle5548612012-04-21 13:08:33 +0000693{
Avi Kivitya8170e52012-10-23 12:30:10 +0200694 hwaddr iotlb;
Blue Swirle5548612012-04-21 13:08:33 +0000695 CPUWatchpoint *wp;
696
Blue Swirlcc5bea62012-04-14 14:56:48 +0000697 if (memory_region_is_ram(section->mr)) {
Blue Swirle5548612012-04-21 13:08:33 +0000698 /* Normal RAM. */
699 iotlb = (memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK)
Paolo Bonzini149f54b2013-05-24 12:59:37 +0200700 + xlat;
Blue Swirle5548612012-04-21 13:08:33 +0000701 if (!section->readonly) {
702 iotlb |= phys_section_notdirty;
703 } else {
704 iotlb |= phys_section_rom;
705 }
706 } else {
Blue Swirle5548612012-04-21 13:08:33 +0000707 iotlb = section - phys_sections;
Paolo Bonzini149f54b2013-05-24 12:59:37 +0200708 iotlb += xlat;
Blue Swirle5548612012-04-21 13:08:33 +0000709 }
710
711 /* Make accesses to pages with watchpoints go via the
712 watchpoint trap routines. */
713 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
714 if (vaddr == (wp->vaddr & TARGET_PAGE_MASK)) {
715 /* Avoid trapping reads of pages with a write breakpoint. */
716 if ((prot & PAGE_WRITE) || (wp->flags & BP_MEM_READ)) {
717 iotlb = phys_section_watch + paddr;
718 *address |= TLB_MMIO;
719 break;
720 }
721 }
722 }
723
724 return iotlb;
725}
bellard9fa3e852004-01-04 18:06:42 +0000726#endif /* defined(CONFIG_USER_ONLY) */
727
pbrooke2eef172008-06-08 01:09:01 +0000728#if !defined(CONFIG_USER_ONLY)
pbrook8da3ff12008-12-01 18:59:50 +0000729
Anthony Liguoric227f092009-10-01 16:12:16 -0500730static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
Avi Kivity5312bd82012-02-12 18:32:55 +0200731 uint16_t section);
Avi Kivitya8170e52012-10-23 12:30:10 +0200732static subpage_t *subpage_init(hwaddr base);
Avi Kivity5312bd82012-02-12 18:32:55 +0200733static void destroy_page_desc(uint16_t section_index)
Avi Kivity54688b12012-02-09 17:34:32 +0200734{
Avi Kivity5312bd82012-02-12 18:32:55 +0200735 MemoryRegionSection *section = &phys_sections[section_index];
736 MemoryRegion *mr = section->mr;
Avi Kivity54688b12012-02-09 17:34:32 +0200737
738 if (mr->subpage) {
739 subpage_t *subpage = container_of(mr, subpage_t, iomem);
740 memory_region_destroy(&subpage->iomem);
741 g_free(subpage);
742 }
743}
744
Avi Kivity4346ae32012-02-10 17:00:01 +0200745static void destroy_l2_mapping(PhysPageEntry *lp, unsigned level)
Avi Kivity54688b12012-02-09 17:34:32 +0200746{
747 unsigned i;
Avi Kivityd6f2ea22012-02-12 20:12:49 +0200748 PhysPageEntry *p;
Avi Kivity54688b12012-02-09 17:34:32 +0200749
Avi Kivityc19e8802012-02-13 20:25:31 +0200750 if (lp->ptr == PHYS_MAP_NODE_NIL) {
Avi Kivity54688b12012-02-09 17:34:32 +0200751 return;
752 }
753
Avi Kivityc19e8802012-02-13 20:25:31 +0200754 p = phys_map_nodes[lp->ptr];
Avi Kivity4346ae32012-02-10 17:00:01 +0200755 for (i = 0; i < L2_SIZE; ++i) {
Avi Kivity07f07b32012-02-13 20:45:32 +0200756 if (!p[i].is_leaf) {
Avi Kivity54688b12012-02-09 17:34:32 +0200757 destroy_l2_mapping(&p[i], level - 1);
Avi Kivity4346ae32012-02-10 17:00:01 +0200758 } else {
Avi Kivityc19e8802012-02-13 20:25:31 +0200759 destroy_page_desc(p[i].ptr);
Avi Kivity54688b12012-02-09 17:34:32 +0200760 }
Avi Kivity54688b12012-02-09 17:34:32 +0200761 }
Avi Kivity07f07b32012-02-13 20:45:32 +0200762 lp->is_leaf = 0;
Avi Kivityc19e8802012-02-13 20:25:31 +0200763 lp->ptr = PHYS_MAP_NODE_NIL;
Avi Kivity54688b12012-02-09 17:34:32 +0200764}
765
Avi Kivityac1970f2012-10-03 16:22:53 +0200766static void destroy_all_mappings(AddressSpaceDispatch *d)
Avi Kivity54688b12012-02-09 17:34:32 +0200767{
Avi Kivityac1970f2012-10-03 16:22:53 +0200768 destroy_l2_mapping(&d->phys_map, P_L2_LEVELS - 1);
Avi Kivityd6f2ea22012-02-12 20:12:49 +0200769 phys_map_nodes_reset();
Avi Kivity54688b12012-02-09 17:34:32 +0200770}
771
Avi Kivity5312bd82012-02-12 18:32:55 +0200772static uint16_t phys_section_add(MemoryRegionSection *section)
773{
Paolo Bonzini68f3f652013-05-07 11:30:23 +0200774 /* The physical section number is ORed with a page-aligned
775 * pointer to produce the iotlb entries. Thus it should
776 * never overflow into the page-aligned value.
777 */
778 assert(phys_sections_nb < TARGET_PAGE_SIZE);
779
Avi Kivity5312bd82012-02-12 18:32:55 +0200780 if (phys_sections_nb == phys_sections_nb_alloc) {
781 phys_sections_nb_alloc = MAX(phys_sections_nb_alloc * 2, 16);
782 phys_sections = g_renew(MemoryRegionSection, phys_sections,
783 phys_sections_nb_alloc);
784 }
785 phys_sections[phys_sections_nb] = *section;
786 return phys_sections_nb++;
787}
788
789static void phys_sections_clear(void)
790{
791 phys_sections_nb = 0;
792}
793
Avi Kivityac1970f2012-10-03 16:22:53 +0200794static void register_subpage(AddressSpaceDispatch *d, MemoryRegionSection *section)
Avi Kivity0f0cb162012-02-13 17:14:32 +0200795{
796 subpage_t *subpage;
Avi Kivitya8170e52012-10-23 12:30:10 +0200797 hwaddr base = section->offset_within_address_space
Avi Kivity0f0cb162012-02-13 17:14:32 +0200798 & TARGET_PAGE_MASK;
Avi Kivityac1970f2012-10-03 16:22:53 +0200799 MemoryRegionSection *existing = phys_page_find(d, base >> TARGET_PAGE_BITS);
Avi Kivity0f0cb162012-02-13 17:14:32 +0200800 MemoryRegionSection subsection = {
801 .offset_within_address_space = base,
802 .size = TARGET_PAGE_SIZE,
803 };
Avi Kivitya8170e52012-10-23 12:30:10 +0200804 hwaddr start, end;
Avi Kivity0f0cb162012-02-13 17:14:32 +0200805
Avi Kivityf3705d52012-03-08 16:16:34 +0200806 assert(existing->mr->subpage || existing->mr == &io_mem_unassigned);
Avi Kivity0f0cb162012-02-13 17:14:32 +0200807
Avi Kivityf3705d52012-03-08 16:16:34 +0200808 if (!(existing->mr->subpage)) {
Avi Kivity0f0cb162012-02-13 17:14:32 +0200809 subpage = subpage_init(base);
810 subsection.mr = &subpage->iomem;
Avi Kivityac1970f2012-10-03 16:22:53 +0200811 phys_page_set(d, base >> TARGET_PAGE_BITS, 1,
Avi Kivity29990972012-02-13 20:21:20 +0200812 phys_section_add(&subsection));
Avi Kivity0f0cb162012-02-13 17:14:32 +0200813 } else {
Avi Kivityf3705d52012-03-08 16:16:34 +0200814 subpage = container_of(existing->mr, subpage_t, iomem);
Avi Kivity0f0cb162012-02-13 17:14:32 +0200815 }
816 start = section->offset_within_address_space & ~TARGET_PAGE_MASK;
Tyler Halladb2a9b2012-07-25 18:45:03 -0400817 end = start + section->size - 1;
Avi Kivity0f0cb162012-02-13 17:14:32 +0200818 subpage_register(subpage, start, end, phys_section_add(section));
819}
820
821
Avi Kivityac1970f2012-10-03 16:22:53 +0200822static void register_multipage(AddressSpaceDispatch *d, MemoryRegionSection *section)
bellard33417e72003-08-10 21:47:01 +0000823{
Avi Kivitya8170e52012-10-23 12:30:10 +0200824 hwaddr start_addr = section->offset_within_address_space;
Avi Kivitydd811242012-01-02 12:17:03 +0200825 ram_addr_t size = section->size;
Avi Kivitya8170e52012-10-23 12:30:10 +0200826 hwaddr addr;
Avi Kivity5312bd82012-02-12 18:32:55 +0200827 uint16_t section_index = phys_section_add(section);
Avi Kivitydd811242012-01-02 12:17:03 +0200828
Edgar E. Iglesias3b8e6a22011-04-05 13:00:36 +0200829 assert(size);
Michael S. Tsirkinf6f3fbc2010-01-27 22:06:57 +0200830
Edgar E. Iglesias3b8e6a22011-04-05 13:00:36 +0200831 addr = start_addr;
Avi Kivityac1970f2012-10-03 16:22:53 +0200832 phys_page_set(d, addr >> TARGET_PAGE_BITS, size >> TARGET_PAGE_BITS,
Avi Kivity29990972012-02-13 20:21:20 +0200833 section_index);
bellard33417e72003-08-10 21:47:01 +0000834}
835
Avi Kivity86a86232012-10-30 13:47:45 +0200836QEMU_BUILD_BUG_ON(TARGET_PHYS_ADDR_SPACE_BITS > MAX_PHYS_ADDR_SPACE_BITS)
837
838static MemoryRegionSection limit(MemoryRegionSection section)
839{
840 section.size = MIN(section.offset_within_address_space + section.size,
841 MAX_PHYS_ADDR + 1)
842 - section.offset_within_address_space;
843
844 return section;
845}
846
Avi Kivityac1970f2012-10-03 16:22:53 +0200847static void mem_add(MemoryListener *listener, MemoryRegionSection *section)
Avi Kivity0f0cb162012-02-13 17:14:32 +0200848{
Avi Kivityac1970f2012-10-03 16:22:53 +0200849 AddressSpaceDispatch *d = container_of(listener, AddressSpaceDispatch, listener);
Avi Kivity86a86232012-10-30 13:47:45 +0200850 MemoryRegionSection now = limit(*section), remain = limit(*section);
Avi Kivity0f0cb162012-02-13 17:14:32 +0200851
852 if ((now.offset_within_address_space & ~TARGET_PAGE_MASK)
853 || (now.size < TARGET_PAGE_SIZE)) {
854 now.size = MIN(TARGET_PAGE_ALIGN(now.offset_within_address_space)
855 - now.offset_within_address_space,
856 now.size);
Avi Kivityac1970f2012-10-03 16:22:53 +0200857 register_subpage(d, &now);
Avi Kivity0f0cb162012-02-13 17:14:32 +0200858 remain.size -= now.size;
859 remain.offset_within_address_space += now.size;
860 remain.offset_within_region += now.size;
861 }
Tyler Hall69b67642012-07-25 18:45:04 -0400862 while (remain.size >= TARGET_PAGE_SIZE) {
863 now = remain;
864 if (remain.offset_within_region & ~TARGET_PAGE_MASK) {
865 now.size = TARGET_PAGE_SIZE;
Avi Kivityac1970f2012-10-03 16:22:53 +0200866 register_subpage(d, &now);
Tyler Hall69b67642012-07-25 18:45:04 -0400867 } else {
868 now.size &= TARGET_PAGE_MASK;
Avi Kivityac1970f2012-10-03 16:22:53 +0200869 register_multipage(d, &now);
Tyler Hall69b67642012-07-25 18:45:04 -0400870 }
Avi Kivity0f0cb162012-02-13 17:14:32 +0200871 remain.size -= now.size;
872 remain.offset_within_address_space += now.size;
873 remain.offset_within_region += now.size;
874 }
875 now = remain;
876 if (now.size) {
Avi Kivityac1970f2012-10-03 16:22:53 +0200877 register_subpage(d, &now);
Avi Kivity0f0cb162012-02-13 17:14:32 +0200878 }
879}
880
Sheng Yang62a27442010-01-26 19:21:16 +0800881void qemu_flush_coalesced_mmio_buffer(void)
882{
883 if (kvm_enabled())
884 kvm_flush_coalesced_mmio_buffer();
885}
886
Umesh Deshpandeb2a86582011-08-17 00:01:33 -0700887void qemu_mutex_lock_ramlist(void)
888{
889 qemu_mutex_lock(&ram_list.mutex);
890}
891
892void qemu_mutex_unlock_ramlist(void)
893{
894 qemu_mutex_unlock(&ram_list.mutex);
895}
896
Marcelo Tosattic9027602010-03-01 20:25:08 -0300897#if defined(__linux__) && !defined(TARGET_S390X)
898
899#include <sys/vfs.h>
900
901#define HUGETLBFS_MAGIC 0x958458f6
902
903static long gethugepagesize(const char *path)
904{
905 struct statfs fs;
906 int ret;
907
908 do {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +0900909 ret = statfs(path, &fs);
Marcelo Tosattic9027602010-03-01 20:25:08 -0300910 } while (ret != 0 && errno == EINTR);
911
912 if (ret != 0) {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +0900913 perror(path);
914 return 0;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300915 }
916
917 if (fs.f_type != HUGETLBFS_MAGIC)
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +0900918 fprintf(stderr, "Warning: path not on HugeTLBFS: %s\n", path);
Marcelo Tosattic9027602010-03-01 20:25:08 -0300919
920 return fs.f_bsize;
921}
922
Alex Williamson04b16652010-07-02 11:13:17 -0600923static void *file_ram_alloc(RAMBlock *block,
924 ram_addr_t memory,
925 const char *path)
Marcelo Tosattic9027602010-03-01 20:25:08 -0300926{
927 char *filename;
Peter Feiner8ca761f2013-03-04 13:54:25 -0500928 char *sanitized_name;
929 char *c;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300930 void *area;
931 int fd;
932#ifdef MAP_POPULATE
933 int flags;
934#endif
935 unsigned long hpagesize;
936
937 hpagesize = gethugepagesize(path);
938 if (!hpagesize) {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +0900939 return NULL;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300940 }
941
942 if (memory < hpagesize) {
943 return NULL;
944 }
945
946 if (kvm_enabled() && !kvm_has_sync_mmu()) {
947 fprintf(stderr, "host lacks kvm mmu notifiers, -mem-path unsupported\n");
948 return NULL;
949 }
950
Peter Feiner8ca761f2013-03-04 13:54:25 -0500951 /* Make name safe to use with mkstemp by replacing '/' with '_'. */
952 sanitized_name = g_strdup(block->mr->name);
953 for (c = sanitized_name; *c != '\0'; c++) {
954 if (*c == '/')
955 *c = '_';
956 }
957
958 filename = g_strdup_printf("%s/qemu_back_mem.%s.XXXXXX", path,
959 sanitized_name);
960 g_free(sanitized_name);
Marcelo Tosattic9027602010-03-01 20:25:08 -0300961
962 fd = mkstemp(filename);
963 if (fd < 0) {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +0900964 perror("unable to create backing store for hugepages");
Stefan Weile4ada482013-01-16 18:37:23 +0100965 g_free(filename);
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +0900966 return NULL;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300967 }
968 unlink(filename);
Stefan Weile4ada482013-01-16 18:37:23 +0100969 g_free(filename);
Marcelo Tosattic9027602010-03-01 20:25:08 -0300970
971 memory = (memory+hpagesize-1) & ~(hpagesize-1);
972
973 /*
974 * ftruncate is not supported by hugetlbfs in older
975 * hosts, so don't bother bailing out on errors.
976 * If anything goes wrong with it under other filesystems,
977 * mmap will fail.
978 */
979 if (ftruncate(fd, memory))
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +0900980 perror("ftruncate");
Marcelo Tosattic9027602010-03-01 20:25:08 -0300981
982#ifdef MAP_POPULATE
983 /* NB: MAP_POPULATE won't exhaustively alloc all phys pages in the case
984 * MAP_PRIVATE is requested. For mem_prealloc we mmap as MAP_SHARED
985 * to sidestep this quirk.
986 */
987 flags = mem_prealloc ? MAP_POPULATE | MAP_SHARED : MAP_PRIVATE;
988 area = mmap(0, memory, PROT_READ | PROT_WRITE, flags, fd, 0);
989#else
990 area = mmap(0, memory, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
991#endif
992 if (area == MAP_FAILED) {
Yoshiaki Tamura9742bf22010-08-18 13:30:13 +0900993 perror("file_ram_alloc: can't mmap RAM pages");
994 close(fd);
995 return (NULL);
Marcelo Tosattic9027602010-03-01 20:25:08 -0300996 }
Alex Williamson04b16652010-07-02 11:13:17 -0600997 block->fd = fd;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300998 return area;
999}
1000#endif
1001
Alex Williamsond17b5282010-06-25 11:08:38 -06001002static ram_addr_t find_ram_offset(ram_addr_t size)
1003{
Alex Williamson04b16652010-07-02 11:13:17 -06001004 RAMBlock *block, *next_block;
Alex Williamson3e837b22011-10-31 08:54:09 -06001005 ram_addr_t offset = RAM_ADDR_MAX, mingap = RAM_ADDR_MAX;
Alex Williamson04b16652010-07-02 11:13:17 -06001006
Stefan Hajnoczi49cd9ac2013-03-11 10:20:21 +01001007 assert(size != 0); /* it would hand out same offset multiple times */
1008
Paolo Bonzinia3161032012-11-14 15:54:48 +01001009 if (QTAILQ_EMPTY(&ram_list.blocks))
Alex Williamson04b16652010-07-02 11:13:17 -06001010 return 0;
1011
Paolo Bonzinia3161032012-11-14 15:54:48 +01001012 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Anthony PERARDf15fbc42011-07-20 08:17:42 +00001013 ram_addr_t end, next = RAM_ADDR_MAX;
Alex Williamson04b16652010-07-02 11:13:17 -06001014
1015 end = block->offset + block->length;
1016
Paolo Bonzinia3161032012-11-14 15:54:48 +01001017 QTAILQ_FOREACH(next_block, &ram_list.blocks, next) {
Alex Williamson04b16652010-07-02 11:13:17 -06001018 if (next_block->offset >= end) {
1019 next = MIN(next, next_block->offset);
1020 }
1021 }
1022 if (next - end >= size && next - end < mingap) {
Alex Williamson3e837b22011-10-31 08:54:09 -06001023 offset = end;
Alex Williamson04b16652010-07-02 11:13:17 -06001024 mingap = next - end;
1025 }
1026 }
Alex Williamson3e837b22011-10-31 08:54:09 -06001027
1028 if (offset == RAM_ADDR_MAX) {
1029 fprintf(stderr, "Failed to find gap of requested size: %" PRIu64 "\n",
1030 (uint64_t)size);
1031 abort();
1032 }
1033
Alex Williamson04b16652010-07-02 11:13:17 -06001034 return offset;
1035}
1036
Juan Quintela652d7ec2012-07-20 10:37:54 +02001037ram_addr_t last_ram_offset(void)
Alex Williamson04b16652010-07-02 11:13:17 -06001038{
Alex Williamsond17b5282010-06-25 11:08:38 -06001039 RAMBlock *block;
1040 ram_addr_t last = 0;
1041
Paolo Bonzinia3161032012-11-14 15:54:48 +01001042 QTAILQ_FOREACH(block, &ram_list.blocks, next)
Alex Williamsond17b5282010-06-25 11:08:38 -06001043 last = MAX(last, block->offset + block->length);
1044
1045 return last;
1046}
1047
Jason Baronddb97f12012-08-02 15:44:16 -04001048static void qemu_ram_setup_dump(void *addr, ram_addr_t size)
1049{
1050 int ret;
1051 QemuOpts *machine_opts;
1052
1053 /* Use MADV_DONTDUMP, if user doesn't want the guest memory in the core */
1054 machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
1055 if (machine_opts &&
1056 !qemu_opt_get_bool(machine_opts, "dump-guest-core", true)) {
1057 ret = qemu_madvise(addr, size, QEMU_MADV_DONTDUMP);
1058 if (ret) {
1059 perror("qemu_madvise");
1060 fprintf(stderr, "madvise doesn't support MADV_DONTDUMP, "
1061 "but dump_guest_core=off specified\n");
1062 }
1063 }
1064}
1065
Avi Kivityc5705a72011-12-20 15:59:12 +02001066void qemu_ram_set_idstr(ram_addr_t addr, const char *name, DeviceState *dev)
Cam Macdonell84b89d72010-07-26 18:10:57 -06001067{
1068 RAMBlock *new_block, *block;
1069
Avi Kivityc5705a72011-12-20 15:59:12 +02001070 new_block = NULL;
Paolo Bonzinia3161032012-11-14 15:54:48 +01001071 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Avi Kivityc5705a72011-12-20 15:59:12 +02001072 if (block->offset == addr) {
1073 new_block = block;
1074 break;
1075 }
1076 }
1077 assert(new_block);
1078 assert(!new_block->idstr[0]);
Cam Macdonell84b89d72010-07-26 18:10:57 -06001079
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001080 if (dev) {
1081 char *id = qdev_get_dev_path(dev);
Cam Macdonell84b89d72010-07-26 18:10:57 -06001082 if (id) {
1083 snprintf(new_block->idstr, sizeof(new_block->idstr), "%s/", id);
Anthony Liguori7267c092011-08-20 22:09:37 -05001084 g_free(id);
Cam Macdonell84b89d72010-07-26 18:10:57 -06001085 }
1086 }
1087 pstrcat(new_block->idstr, sizeof(new_block->idstr), name);
1088
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001089 /* This assumes the iothread lock is taken here too. */
1090 qemu_mutex_lock_ramlist();
Paolo Bonzinia3161032012-11-14 15:54:48 +01001091 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Avi Kivityc5705a72011-12-20 15:59:12 +02001092 if (block != new_block && !strcmp(block->idstr, new_block->idstr)) {
Cam Macdonell84b89d72010-07-26 18:10:57 -06001093 fprintf(stderr, "RAMBlock \"%s\" already registered, abort!\n",
1094 new_block->idstr);
1095 abort();
1096 }
1097 }
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001098 qemu_mutex_unlock_ramlist();
Avi Kivityc5705a72011-12-20 15:59:12 +02001099}
1100
Luiz Capitulino8490fc72012-09-05 16:50:16 -03001101static int memory_try_enable_merging(void *addr, size_t len)
1102{
1103 QemuOpts *opts;
1104
1105 opts = qemu_opts_find(qemu_find_opts("machine"), 0);
1106 if (opts && !qemu_opt_get_bool(opts, "mem-merge", true)) {
1107 /* disabled by the user */
1108 return 0;
1109 }
1110
1111 return qemu_madvise(addr, len, QEMU_MADV_MERGEABLE);
1112}
1113
Avi Kivityc5705a72011-12-20 15:59:12 +02001114ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
1115 MemoryRegion *mr)
1116{
Paolo Bonziniabb26d62012-11-14 16:00:51 +01001117 RAMBlock *block, *new_block;
Avi Kivityc5705a72011-12-20 15:59:12 +02001118
1119 size = TARGET_PAGE_ALIGN(size);
1120 new_block = g_malloc0(sizeof(*new_block));
Cam Macdonell84b89d72010-07-26 18:10:57 -06001121
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001122 /* This assumes the iothread lock is taken here too. */
1123 qemu_mutex_lock_ramlist();
Avi Kivity7c637362011-12-21 13:09:49 +02001124 new_block->mr = mr;
Jun Nakajima432d2682010-08-31 16:41:25 +01001125 new_block->offset = find_ram_offset(size);
Yoshiaki Tamura6977dfe2010-08-18 15:41:49 +09001126 if (host) {
1127 new_block->host = host;
Huang Yingcd19cfa2011-03-02 08:56:19 +01001128 new_block->flags |= RAM_PREALLOC_MASK;
Yoshiaki Tamura6977dfe2010-08-18 15:41:49 +09001129 } else {
1130 if (mem_path) {
1131#if defined (__linux__) && !defined(TARGET_S390X)
1132 new_block->host = file_ram_alloc(new_block, size, mem_path);
1133 if (!new_block->host) {
Paolo Bonzini6eebf952013-05-13 16:19:55 +02001134 new_block->host = qemu_anon_ram_alloc(size);
Luiz Capitulino8490fc72012-09-05 16:50:16 -03001135 memory_try_enable_merging(new_block->host, size);
Yoshiaki Tamura6977dfe2010-08-18 15:41:49 +09001136 }
1137#else
1138 fprintf(stderr, "-mem-path option unsupported\n");
1139 exit(1);
1140#endif
1141 } else {
Jan Kiszka868bb332011-06-21 22:59:09 +02001142 if (xen_enabled()) {
Avi Kivityfce537d2011-12-18 15:48:55 +02001143 xen_ram_alloc(new_block->offset, size, mr);
Christian Borntraegerfdec9912012-06-15 05:10:30 +00001144 } else if (kvm_enabled()) {
1145 /* some s390/kvm configurations have special constraints */
Paolo Bonzini6eebf952013-05-13 16:19:55 +02001146 new_block->host = kvm_ram_alloc(size);
Jun Nakajima432d2682010-08-31 16:41:25 +01001147 } else {
Paolo Bonzini6eebf952013-05-13 16:19:55 +02001148 new_block->host = qemu_anon_ram_alloc(size);
Jun Nakajima432d2682010-08-31 16:41:25 +01001149 }
Luiz Capitulino8490fc72012-09-05 16:50:16 -03001150 memory_try_enable_merging(new_block->host, size);
Yoshiaki Tamura6977dfe2010-08-18 15:41:49 +09001151 }
1152 }
Cam Macdonell84b89d72010-07-26 18:10:57 -06001153 new_block->length = size;
1154
Paolo Bonziniabb26d62012-11-14 16:00:51 +01001155 /* Keep the list sorted from biggest to smallest block. */
1156 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
1157 if (block->length < new_block->length) {
1158 break;
1159 }
1160 }
1161 if (block) {
1162 QTAILQ_INSERT_BEFORE(block, new_block, next);
1163 } else {
1164 QTAILQ_INSERT_TAIL(&ram_list.blocks, new_block, next);
1165 }
Paolo Bonzini0d6d3c82012-11-14 15:45:02 +01001166 ram_list.mru_block = NULL;
Cam Macdonell84b89d72010-07-26 18:10:57 -06001167
Umesh Deshpandef798b072011-08-18 11:41:17 -07001168 ram_list.version++;
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001169 qemu_mutex_unlock_ramlist();
Umesh Deshpandef798b072011-08-18 11:41:17 -07001170
Anthony Liguori7267c092011-08-20 22:09:37 -05001171 ram_list.phys_dirty = g_realloc(ram_list.phys_dirty,
Cam Macdonell84b89d72010-07-26 18:10:57 -06001172 last_ram_offset() >> TARGET_PAGE_BITS);
Igor Mitsyanko5fda0432012-08-10 18:45:11 +04001173 memset(ram_list.phys_dirty + (new_block->offset >> TARGET_PAGE_BITS),
1174 0, size >> TARGET_PAGE_BITS);
Juan Quintela1720aee2012-06-22 13:14:17 +02001175 cpu_physical_memory_set_dirty_range(new_block->offset, size, 0xff);
Cam Macdonell84b89d72010-07-26 18:10:57 -06001176
Jason Baronddb97f12012-08-02 15:44:16 -04001177 qemu_ram_setup_dump(new_block->host, size);
Luiz Capitulinoad0b5322012-10-05 16:47:57 -03001178 qemu_madvise(new_block->host, size, QEMU_MADV_HUGEPAGE);
Jason Baronddb97f12012-08-02 15:44:16 -04001179
Cam Macdonell84b89d72010-07-26 18:10:57 -06001180 if (kvm_enabled())
1181 kvm_setup_guest_memory(new_block->host, size);
1182
1183 return new_block->offset;
1184}
1185
Avi Kivityc5705a72011-12-20 15:59:12 +02001186ram_addr_t qemu_ram_alloc(ram_addr_t size, MemoryRegion *mr)
pbrook94a6b542009-04-11 17:15:54 +00001187{
Avi Kivityc5705a72011-12-20 15:59:12 +02001188 return qemu_ram_alloc_from_ptr(size, NULL, mr);
pbrook94a6b542009-04-11 17:15:54 +00001189}
bellarde9a1ab12007-02-08 23:08:38 +00001190
Alex Williamson1f2e98b2011-05-03 12:48:09 -06001191void qemu_ram_free_from_ptr(ram_addr_t addr)
1192{
1193 RAMBlock *block;
1194
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001195 /* This assumes the iothread lock is taken here too. */
1196 qemu_mutex_lock_ramlist();
Paolo Bonzinia3161032012-11-14 15:54:48 +01001197 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Alex Williamson1f2e98b2011-05-03 12:48:09 -06001198 if (addr == block->offset) {
Paolo Bonzinia3161032012-11-14 15:54:48 +01001199 QTAILQ_REMOVE(&ram_list.blocks, block, next);
Paolo Bonzini0d6d3c82012-11-14 15:45:02 +01001200 ram_list.mru_block = NULL;
Umesh Deshpandef798b072011-08-18 11:41:17 -07001201 ram_list.version++;
Anthony Liguori7267c092011-08-20 22:09:37 -05001202 g_free(block);
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001203 break;
Alex Williamson1f2e98b2011-05-03 12:48:09 -06001204 }
1205 }
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001206 qemu_mutex_unlock_ramlist();
Alex Williamson1f2e98b2011-05-03 12:48:09 -06001207}
1208
Anthony Liguoric227f092009-10-01 16:12:16 -05001209void qemu_ram_free(ram_addr_t addr)
bellarde9a1ab12007-02-08 23:08:38 +00001210{
Alex Williamson04b16652010-07-02 11:13:17 -06001211 RAMBlock *block;
1212
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001213 /* This assumes the iothread lock is taken here too. */
1214 qemu_mutex_lock_ramlist();
Paolo Bonzinia3161032012-11-14 15:54:48 +01001215 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Alex Williamson04b16652010-07-02 11:13:17 -06001216 if (addr == block->offset) {
Paolo Bonzinia3161032012-11-14 15:54:48 +01001217 QTAILQ_REMOVE(&ram_list.blocks, block, next);
Paolo Bonzini0d6d3c82012-11-14 15:45:02 +01001218 ram_list.mru_block = NULL;
Umesh Deshpandef798b072011-08-18 11:41:17 -07001219 ram_list.version++;
Huang Yingcd19cfa2011-03-02 08:56:19 +01001220 if (block->flags & RAM_PREALLOC_MASK) {
1221 ;
1222 } else if (mem_path) {
Alex Williamson04b16652010-07-02 11:13:17 -06001223#if defined (__linux__) && !defined(TARGET_S390X)
1224 if (block->fd) {
1225 munmap(block->host, block->length);
1226 close(block->fd);
1227 } else {
Paolo Bonzinie7a09b92013-05-13 16:19:56 +02001228 qemu_anon_ram_free(block->host, block->length);
Alex Williamson04b16652010-07-02 11:13:17 -06001229 }
Jan Kiszkafd28aa12011-03-15 12:26:14 +01001230#else
1231 abort();
Alex Williamson04b16652010-07-02 11:13:17 -06001232#endif
1233 } else {
Jan Kiszka868bb332011-06-21 22:59:09 +02001234 if (xen_enabled()) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02001235 xen_invalidate_map_cache_entry(block->host);
Jun Nakajima432d2682010-08-31 16:41:25 +01001236 } else {
Paolo Bonzinie7a09b92013-05-13 16:19:56 +02001237 qemu_anon_ram_free(block->host, block->length);
Jun Nakajima432d2682010-08-31 16:41:25 +01001238 }
Alex Williamson04b16652010-07-02 11:13:17 -06001239 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001240 g_free(block);
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001241 break;
Alex Williamson04b16652010-07-02 11:13:17 -06001242 }
1243 }
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001244 qemu_mutex_unlock_ramlist();
Alex Williamson04b16652010-07-02 11:13:17 -06001245
bellarde9a1ab12007-02-08 23:08:38 +00001246}
1247
Huang Yingcd19cfa2011-03-02 08:56:19 +01001248#ifndef _WIN32
1249void qemu_ram_remap(ram_addr_t addr, ram_addr_t length)
1250{
1251 RAMBlock *block;
1252 ram_addr_t offset;
1253 int flags;
1254 void *area, *vaddr;
1255
Paolo Bonzinia3161032012-11-14 15:54:48 +01001256 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Huang Yingcd19cfa2011-03-02 08:56:19 +01001257 offset = addr - block->offset;
1258 if (offset < block->length) {
1259 vaddr = block->host + offset;
1260 if (block->flags & RAM_PREALLOC_MASK) {
1261 ;
1262 } else {
1263 flags = MAP_FIXED;
1264 munmap(vaddr, length);
1265 if (mem_path) {
1266#if defined(__linux__) && !defined(TARGET_S390X)
1267 if (block->fd) {
1268#ifdef MAP_POPULATE
1269 flags |= mem_prealloc ? MAP_POPULATE | MAP_SHARED :
1270 MAP_PRIVATE;
1271#else
1272 flags |= MAP_PRIVATE;
1273#endif
1274 area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
1275 flags, block->fd, offset);
1276 } else {
1277 flags |= MAP_PRIVATE | MAP_ANONYMOUS;
1278 area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
1279 flags, -1, 0);
1280 }
Jan Kiszkafd28aa12011-03-15 12:26:14 +01001281#else
1282 abort();
Huang Yingcd19cfa2011-03-02 08:56:19 +01001283#endif
1284 } else {
1285#if defined(TARGET_S390X) && defined(CONFIG_KVM)
1286 flags |= MAP_SHARED | MAP_ANONYMOUS;
1287 area = mmap(vaddr, length, PROT_EXEC|PROT_READ|PROT_WRITE,
1288 flags, -1, 0);
1289#else
1290 flags |= MAP_PRIVATE | MAP_ANONYMOUS;
1291 area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
1292 flags, -1, 0);
1293#endif
1294 }
1295 if (area != vaddr) {
Anthony PERARDf15fbc42011-07-20 08:17:42 +00001296 fprintf(stderr, "Could not remap addr: "
1297 RAM_ADDR_FMT "@" RAM_ADDR_FMT "\n",
Huang Yingcd19cfa2011-03-02 08:56:19 +01001298 length, addr);
1299 exit(1);
1300 }
Luiz Capitulino8490fc72012-09-05 16:50:16 -03001301 memory_try_enable_merging(vaddr, length);
Jason Baronddb97f12012-08-02 15:44:16 -04001302 qemu_ram_setup_dump(vaddr, length);
Huang Yingcd19cfa2011-03-02 08:56:19 +01001303 }
1304 return;
1305 }
1306 }
1307}
1308#endif /* !_WIN32 */
1309
pbrookdc828ca2009-04-09 22:21:07 +00001310/* Return a host pointer to ram allocated with qemu_ram_alloc.
pbrook5579c7f2009-04-11 14:47:08 +00001311 With the exception of the softmmu code in this file, this should
1312 only be used for local memory (e.g. video ram) that the device owns,
1313 and knows it isn't going to access beyond the end of the block.
1314
1315 It should not be used for general purpose DMA.
1316 Use cpu_physical_memory_map/cpu_physical_memory_rw instead.
1317 */
Anthony Liguoric227f092009-10-01 16:12:16 -05001318void *qemu_get_ram_ptr(ram_addr_t addr)
pbrookdc828ca2009-04-09 22:21:07 +00001319{
pbrook94a6b542009-04-11 17:15:54 +00001320 RAMBlock *block;
1321
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001322 /* The list is protected by the iothread lock here. */
Paolo Bonzini0d6d3c82012-11-14 15:45:02 +01001323 block = ram_list.mru_block;
1324 if (block && addr - block->offset < block->length) {
1325 goto found;
1326 }
Paolo Bonzinia3161032012-11-14 15:54:48 +01001327 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Alex Williamsonf471a172010-06-11 11:11:42 -06001328 if (addr - block->offset < block->length) {
Paolo Bonzini0d6d3c82012-11-14 15:45:02 +01001329 goto found;
Alex Williamsonf471a172010-06-11 11:11:42 -06001330 }
pbrook94a6b542009-04-11 17:15:54 +00001331 }
Alex Williamsonf471a172010-06-11 11:11:42 -06001332
1333 fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
1334 abort();
1335
Paolo Bonzini0d6d3c82012-11-14 15:45:02 +01001336found:
1337 ram_list.mru_block = block;
1338 if (xen_enabled()) {
1339 /* We need to check if the requested address is in the RAM
1340 * because we don't want to map the entire memory in QEMU.
1341 * In that case just map until the end of the page.
1342 */
1343 if (block->offset == 0) {
1344 return xen_map_cache(addr, 0, 0);
1345 } else if (block->host == NULL) {
1346 block->host =
1347 xen_map_cache(block->offset, block->length, 1);
1348 }
1349 }
1350 return block->host + (addr - block->offset);
pbrookdc828ca2009-04-09 22:21:07 +00001351}
1352
Paolo Bonzini0d6d3c82012-11-14 15:45:02 +01001353/* Return a host pointer to ram allocated with qemu_ram_alloc. Same as
1354 * qemu_get_ram_ptr but do not touch ram_list.mru_block.
1355 *
1356 * ??? Is this still necessary?
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +02001357 */
Blue Swirl8b9c99d2012-10-28 11:04:51 +00001358static void *qemu_safe_ram_ptr(ram_addr_t addr)
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +02001359{
1360 RAMBlock *block;
1361
Umesh Deshpandeb2a86582011-08-17 00:01:33 -07001362 /* The list is protected by the iothread lock here. */
Paolo Bonzinia3161032012-11-14 15:54:48 +01001363 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +02001364 if (addr - block->offset < block->length) {
Jan Kiszka868bb332011-06-21 22:59:09 +02001365 if (xen_enabled()) {
Jun Nakajima432d2682010-08-31 16:41:25 +01001366 /* We need to check if the requested address is in the RAM
1367 * because we don't want to map the entire memory in QEMU.
Stefano Stabellini712c2b42011-05-19 18:35:46 +01001368 * In that case just map until the end of the page.
Jun Nakajima432d2682010-08-31 16:41:25 +01001369 */
1370 if (block->offset == 0) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02001371 return xen_map_cache(addr, 0, 0);
Jun Nakajima432d2682010-08-31 16:41:25 +01001372 } else if (block->host == NULL) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02001373 block->host =
1374 xen_map_cache(block->offset, block->length, 1);
Jun Nakajima432d2682010-08-31 16:41:25 +01001375 }
1376 }
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +02001377 return block->host + (addr - block->offset);
1378 }
1379 }
1380
1381 fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
1382 abort();
1383
1384 return NULL;
1385}
1386
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01001387/* Return a host pointer to guest's ram. Similar to qemu_get_ram_ptr
1388 * but takes a size argument */
Blue Swirl8b9c99d2012-10-28 11:04:51 +00001389static void *qemu_ram_ptr_length(ram_addr_t addr, ram_addr_t *size)
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01001390{
Stefano Stabellini8ab934f2011-06-27 18:26:06 +01001391 if (*size == 0) {
1392 return NULL;
1393 }
Jan Kiszka868bb332011-06-21 22:59:09 +02001394 if (xen_enabled()) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02001395 return xen_map_cache(addr, *size, 1);
Jan Kiszka868bb332011-06-21 22:59:09 +02001396 } else {
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01001397 RAMBlock *block;
1398
Paolo Bonzinia3161032012-11-14 15:54:48 +01001399 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01001400 if (addr - block->offset < block->length) {
1401 if (addr - block->offset + *size > block->length)
1402 *size = block->length - addr + block->offset;
1403 return block->host + (addr - block->offset);
1404 }
1405 }
1406
1407 fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
1408 abort();
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01001409 }
1410}
1411
Marcelo Tosattie8902612010-10-11 15:31:19 -03001412int qemu_ram_addr_from_host(void *ptr, ram_addr_t *ram_addr)
pbrook5579c7f2009-04-11 14:47:08 +00001413{
pbrook94a6b542009-04-11 17:15:54 +00001414 RAMBlock *block;
1415 uint8_t *host = ptr;
1416
Jan Kiszka868bb332011-06-21 22:59:09 +02001417 if (xen_enabled()) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02001418 *ram_addr = xen_ram_addr_from_mapcache(ptr);
Stefano Stabellini712c2b42011-05-19 18:35:46 +01001419 return 0;
1420 }
1421
Paolo Bonzinia3161032012-11-14 15:54:48 +01001422 QTAILQ_FOREACH(block, &ram_list.blocks, next) {
Jun Nakajima432d2682010-08-31 16:41:25 +01001423 /* This case append when the block is not mapped. */
1424 if (block->host == NULL) {
1425 continue;
1426 }
Alex Williamsonf471a172010-06-11 11:11:42 -06001427 if (host - block->host < block->length) {
Marcelo Tosattie8902612010-10-11 15:31:19 -03001428 *ram_addr = block->offset + (host - block->host);
1429 return 0;
Alex Williamsonf471a172010-06-11 11:11:42 -06001430 }
pbrook94a6b542009-04-11 17:15:54 +00001431 }
Jun Nakajima432d2682010-08-31 16:41:25 +01001432
Marcelo Tosattie8902612010-10-11 15:31:19 -03001433 return -1;
1434}
Alex Williamsonf471a172010-06-11 11:11:42 -06001435
Marcelo Tosattie8902612010-10-11 15:31:19 -03001436/* Some of the softmmu routines need to translate from a host pointer
1437 (typically a TLB entry) back to a ram offset. */
1438ram_addr_t qemu_ram_addr_from_host_nofail(void *ptr)
1439{
1440 ram_addr_t ram_addr;
Alex Williamsonf471a172010-06-11 11:11:42 -06001441
Marcelo Tosattie8902612010-10-11 15:31:19 -03001442 if (qemu_ram_addr_from_host(ptr, &ram_addr)) {
1443 fprintf(stderr, "Bad ram pointer %p\n", ptr);
1444 abort();
1445 }
1446 return ram_addr;
pbrook5579c7f2009-04-11 14:47:08 +00001447}
1448
Avi Kivitya8170e52012-10-23 12:30:10 +02001449static void notdirty_mem_write(void *opaque, hwaddr ram_addr,
Avi Kivity0e0df1e2012-01-02 00:32:15 +02001450 uint64_t val, unsigned size)
bellard1ccde1c2004-02-06 19:46:14 +00001451{
bellard3a7d9292005-08-21 09:26:42 +00001452 int dirty_flags;
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09001453 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
bellard3a7d9292005-08-21 09:26:42 +00001454 if (!(dirty_flags & CODE_DIRTY_FLAG)) {
Avi Kivity0e0df1e2012-01-02 00:32:15 +02001455 tb_invalidate_phys_page_fast(ram_addr, size);
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09001456 dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
bellard3a7d9292005-08-21 09:26:42 +00001457 }
Avi Kivity0e0df1e2012-01-02 00:32:15 +02001458 switch (size) {
1459 case 1:
1460 stb_p(qemu_get_ram_ptr(ram_addr), val);
1461 break;
1462 case 2:
1463 stw_p(qemu_get_ram_ptr(ram_addr), val);
1464 break;
1465 case 4:
1466 stl_p(qemu_get_ram_ptr(ram_addr), val);
1467 break;
1468 default:
1469 abort();
1470 }
bellardf23db162005-08-21 19:12:28 +00001471 dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09001472 cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags);
bellardf23db162005-08-21 19:12:28 +00001473 /* we remove the notdirty callback only if the code has been
1474 flushed */
1475 if (dirty_flags == 0xff)
pbrook2e70f6e2008-06-29 01:03:05 +00001476 tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
bellard1ccde1c2004-02-06 19:46:14 +00001477}
1478
Paolo Bonzinib018ddf2013-05-24 14:48:38 +02001479static bool notdirty_mem_accepts(void *opaque, hwaddr addr,
1480 unsigned size, bool is_write)
1481{
1482 return is_write;
1483}
1484
Avi Kivity0e0df1e2012-01-02 00:32:15 +02001485static const MemoryRegionOps notdirty_mem_ops = {
Avi Kivity0e0df1e2012-01-02 00:32:15 +02001486 .write = notdirty_mem_write,
Paolo Bonzinib018ddf2013-05-24 14:48:38 +02001487 .valid.accepts = notdirty_mem_accepts,
Avi Kivity0e0df1e2012-01-02 00:32:15 +02001488 .endianness = DEVICE_NATIVE_ENDIAN,
bellard1ccde1c2004-02-06 19:46:14 +00001489};
1490
pbrook0f459d12008-06-09 00:20:13 +00001491/* Generate a debug exception if a watchpoint has been hit. */
aliguorib4051332008-11-18 20:14:20 +00001492static void check_watchpoint(int offset, int len_mask, int flags)
pbrook0f459d12008-06-09 00:20:13 +00001493{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001494 CPUArchState *env = cpu_single_env;
aliguori06d55cc2008-11-18 20:24:06 +00001495 target_ulong pc, cs_base;
pbrook0f459d12008-06-09 00:20:13 +00001496 target_ulong vaddr;
aliguoria1d1bb32008-11-18 20:07:32 +00001497 CPUWatchpoint *wp;
aliguori06d55cc2008-11-18 20:24:06 +00001498 int cpu_flags;
pbrook0f459d12008-06-09 00:20:13 +00001499
aliguori06d55cc2008-11-18 20:24:06 +00001500 if (env->watchpoint_hit) {
1501 /* We re-entered the check after replacing the TB. Now raise
1502 * the debug interrupt so that is will trigger after the
1503 * current instruction. */
Andreas Färberc3affe52013-01-18 15:03:43 +01001504 cpu_interrupt(ENV_GET_CPU(env), CPU_INTERRUPT_DEBUG);
aliguori06d55cc2008-11-18 20:24:06 +00001505 return;
1506 }
pbrook2e70f6e2008-06-29 01:03:05 +00001507 vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001508 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
aliguorib4051332008-11-18 20:14:20 +00001509 if ((vaddr == (wp->vaddr & len_mask) ||
1510 (vaddr & wp->len_mask) == wp->vaddr) && (wp->flags & flags)) {
aliguori6e140f22008-11-18 20:37:55 +00001511 wp->flags |= BP_WATCHPOINT_HIT;
1512 if (!env->watchpoint_hit) {
1513 env->watchpoint_hit = wp;
Blue Swirl5a316522012-12-02 21:28:09 +00001514 tb_check_watchpoint(env);
aliguori6e140f22008-11-18 20:37:55 +00001515 if (wp->flags & BP_STOP_BEFORE_ACCESS) {
1516 env->exception_index = EXCP_DEBUG;
Max Filippov488d6572012-01-29 02:24:39 +04001517 cpu_loop_exit(env);
aliguori6e140f22008-11-18 20:37:55 +00001518 } else {
1519 cpu_get_tb_cpu_state(env, &pc, &cs_base, &cpu_flags);
1520 tb_gen_code(env, pc, cs_base, cpu_flags, 1);
Max Filippov488d6572012-01-29 02:24:39 +04001521 cpu_resume_from_signal(env, NULL);
aliguori6e140f22008-11-18 20:37:55 +00001522 }
aliguori06d55cc2008-11-18 20:24:06 +00001523 }
aliguori6e140f22008-11-18 20:37:55 +00001524 } else {
1525 wp->flags &= ~BP_WATCHPOINT_HIT;
pbrook0f459d12008-06-09 00:20:13 +00001526 }
1527 }
1528}
1529
pbrook6658ffb2007-03-16 23:58:11 +00001530/* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
1531 so these check for a hit then pass through to the normal out-of-line
1532 phys routines. */
Avi Kivitya8170e52012-10-23 12:30:10 +02001533static uint64_t watch_mem_read(void *opaque, hwaddr addr,
Avi Kivity1ec9b902012-01-02 12:47:48 +02001534 unsigned size)
pbrook6658ffb2007-03-16 23:58:11 +00001535{
Avi Kivity1ec9b902012-01-02 12:47:48 +02001536 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~(size - 1), BP_MEM_READ);
1537 switch (size) {
1538 case 1: return ldub_phys(addr);
1539 case 2: return lduw_phys(addr);
1540 case 4: return ldl_phys(addr);
1541 default: abort();
1542 }
pbrook6658ffb2007-03-16 23:58:11 +00001543}
1544
Avi Kivitya8170e52012-10-23 12:30:10 +02001545static void watch_mem_write(void *opaque, hwaddr addr,
Avi Kivity1ec9b902012-01-02 12:47:48 +02001546 uint64_t val, unsigned size)
pbrook6658ffb2007-03-16 23:58:11 +00001547{
Avi Kivity1ec9b902012-01-02 12:47:48 +02001548 check_watchpoint(addr & ~TARGET_PAGE_MASK, ~(size - 1), BP_MEM_WRITE);
1549 switch (size) {
Max Filippov67364152012-01-29 00:01:40 +04001550 case 1:
1551 stb_phys(addr, val);
1552 break;
1553 case 2:
1554 stw_phys(addr, val);
1555 break;
1556 case 4:
1557 stl_phys(addr, val);
1558 break;
Avi Kivity1ec9b902012-01-02 12:47:48 +02001559 default: abort();
1560 }
pbrook6658ffb2007-03-16 23:58:11 +00001561}
1562
Avi Kivity1ec9b902012-01-02 12:47:48 +02001563static const MemoryRegionOps watch_mem_ops = {
1564 .read = watch_mem_read,
1565 .write = watch_mem_write,
1566 .endianness = DEVICE_NATIVE_ENDIAN,
pbrook6658ffb2007-03-16 23:58:11 +00001567};
pbrook6658ffb2007-03-16 23:58:11 +00001568
Avi Kivitya8170e52012-10-23 12:30:10 +02001569static uint64_t subpage_read(void *opaque, hwaddr addr,
Avi Kivity70c68e42012-01-02 12:32:48 +02001570 unsigned len)
blueswir1db7b5422007-05-26 17:36:03 +00001571{
Avi Kivity70c68e42012-01-02 12:32:48 +02001572 subpage_t *mmio = opaque;
Richard Hendersonf6405242010-04-22 16:47:31 -07001573 unsigned int idx = SUBPAGE_IDX(addr);
Paolo Bonzini791af8c2013-05-24 16:10:39 +02001574 uint64_t val;
1575
Avi Kivity5312bd82012-02-12 18:32:55 +02001576 MemoryRegionSection *section;
blueswir1db7b5422007-05-26 17:36:03 +00001577#if defined(DEBUG_SUBPAGE)
1578 printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__,
1579 mmio, len, addr, idx);
1580#endif
blueswir1db7b5422007-05-26 17:36:03 +00001581
Avi Kivity5312bd82012-02-12 18:32:55 +02001582 section = &phys_sections[mmio->sub_section[idx]];
1583 addr += mmio->base;
1584 addr -= section->offset_within_address_space;
1585 addr += section->offset_within_region;
Paolo Bonzini791af8c2013-05-24 16:10:39 +02001586 io_mem_read(section->mr, addr, &val, len);
1587 return val;
blueswir1db7b5422007-05-26 17:36:03 +00001588}
1589
Avi Kivitya8170e52012-10-23 12:30:10 +02001590static void subpage_write(void *opaque, hwaddr addr,
Avi Kivity70c68e42012-01-02 12:32:48 +02001591 uint64_t value, unsigned len)
blueswir1db7b5422007-05-26 17:36:03 +00001592{
Avi Kivity70c68e42012-01-02 12:32:48 +02001593 subpage_t *mmio = opaque;
Richard Hendersonf6405242010-04-22 16:47:31 -07001594 unsigned int idx = SUBPAGE_IDX(addr);
Avi Kivity5312bd82012-02-12 18:32:55 +02001595 MemoryRegionSection *section;
blueswir1db7b5422007-05-26 17:36:03 +00001596#if defined(DEBUG_SUBPAGE)
Avi Kivity70c68e42012-01-02 12:32:48 +02001597 printf("%s: subpage %p len %d addr " TARGET_FMT_plx
1598 " idx %d value %"PRIx64"\n",
Richard Hendersonf6405242010-04-22 16:47:31 -07001599 __func__, mmio, len, addr, idx, value);
blueswir1db7b5422007-05-26 17:36:03 +00001600#endif
Richard Hendersonf6405242010-04-22 16:47:31 -07001601
Avi Kivity5312bd82012-02-12 18:32:55 +02001602 section = &phys_sections[mmio->sub_section[idx]];
1603 addr += mmio->base;
1604 addr -= section->offset_within_address_space;
1605 addr += section->offset_within_region;
Avi Kivity37ec01d2012-03-08 18:08:35 +02001606 io_mem_write(section->mr, addr, value, len);
blueswir1db7b5422007-05-26 17:36:03 +00001607}
1608
Paolo Bonzinic353e4c2013-05-24 14:02:39 +02001609static bool subpage_accepts(void *opaque, hwaddr addr,
1610 unsigned size, bool is_write)
1611{
1612 subpage_t *mmio = opaque;
1613 unsigned int idx = SUBPAGE_IDX(addr);
1614 MemoryRegionSection *section;
1615#if defined(DEBUG_SUBPAGE)
1616 printf("%s: subpage %p %c len %d addr " TARGET_FMT_plx
1617 " idx %d\n", __func__, mmio,
1618 is_write ? 'w' : 'r', len, addr, idx);
1619#endif
1620
1621 section = &phys_sections[mmio->sub_section[idx]];
1622 addr += mmio->base;
1623 addr -= section->offset_within_address_space;
1624 addr += section->offset_within_region;
1625 return memory_region_access_valid(section->mr, addr, size, is_write);
1626}
1627
Avi Kivity70c68e42012-01-02 12:32:48 +02001628static const MemoryRegionOps subpage_ops = {
1629 .read = subpage_read,
1630 .write = subpage_write,
Paolo Bonzinic353e4c2013-05-24 14:02:39 +02001631 .valid.accepts = subpage_accepts,
Avi Kivity70c68e42012-01-02 12:32:48 +02001632 .endianness = DEVICE_NATIVE_ENDIAN,
blueswir1db7b5422007-05-26 17:36:03 +00001633};
1634
Avi Kivitya8170e52012-10-23 12:30:10 +02001635static uint64_t subpage_ram_read(void *opaque, hwaddr addr,
Avi Kivityde712f92012-01-02 12:41:07 +02001636 unsigned size)
Andreas Färber56384e82011-11-30 16:26:21 +01001637{
1638 ram_addr_t raddr = addr;
1639 void *ptr = qemu_get_ram_ptr(raddr);
Avi Kivityde712f92012-01-02 12:41:07 +02001640 switch (size) {
1641 case 1: return ldub_p(ptr);
1642 case 2: return lduw_p(ptr);
1643 case 4: return ldl_p(ptr);
1644 default: abort();
1645 }
Andreas Färber56384e82011-11-30 16:26:21 +01001646}
1647
Avi Kivitya8170e52012-10-23 12:30:10 +02001648static void subpage_ram_write(void *opaque, hwaddr addr,
Avi Kivityde712f92012-01-02 12:41:07 +02001649 uint64_t value, unsigned size)
Andreas Färber56384e82011-11-30 16:26:21 +01001650{
1651 ram_addr_t raddr = addr;
1652 void *ptr = qemu_get_ram_ptr(raddr);
Avi Kivityde712f92012-01-02 12:41:07 +02001653 switch (size) {
1654 case 1: return stb_p(ptr, value);
1655 case 2: return stw_p(ptr, value);
1656 case 4: return stl_p(ptr, value);
1657 default: abort();
1658 }
Andreas Färber56384e82011-11-30 16:26:21 +01001659}
1660
Avi Kivityde712f92012-01-02 12:41:07 +02001661static const MemoryRegionOps subpage_ram_ops = {
1662 .read = subpage_ram_read,
1663 .write = subpage_ram_write,
1664 .endianness = DEVICE_NATIVE_ENDIAN,
Andreas Färber56384e82011-11-30 16:26:21 +01001665};
1666
Anthony Liguoric227f092009-10-01 16:12:16 -05001667static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
Avi Kivity5312bd82012-02-12 18:32:55 +02001668 uint16_t section)
blueswir1db7b5422007-05-26 17:36:03 +00001669{
1670 int idx, eidx;
1671
1672 if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
1673 return -1;
1674 idx = SUBPAGE_IDX(start);
1675 eidx = SUBPAGE_IDX(end);
1676#if defined(DEBUG_SUBPAGE)
Blue Swirl0bf9e312009-07-20 17:19:25 +00001677 printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %ld\n", __func__,
blueswir1db7b5422007-05-26 17:36:03 +00001678 mmio, start, end, idx, eidx, memory);
1679#endif
Avi Kivity5312bd82012-02-12 18:32:55 +02001680 if (memory_region_is_ram(phys_sections[section].mr)) {
1681 MemoryRegionSection new_section = phys_sections[section];
1682 new_section.mr = &io_mem_subpage_ram;
1683 section = phys_section_add(&new_section);
Andreas Färber56384e82011-11-30 16:26:21 +01001684 }
blueswir1db7b5422007-05-26 17:36:03 +00001685 for (; idx <= eidx; idx++) {
Avi Kivity5312bd82012-02-12 18:32:55 +02001686 mmio->sub_section[idx] = section;
blueswir1db7b5422007-05-26 17:36:03 +00001687 }
1688
1689 return 0;
1690}
1691
Avi Kivitya8170e52012-10-23 12:30:10 +02001692static subpage_t *subpage_init(hwaddr base)
blueswir1db7b5422007-05-26 17:36:03 +00001693{
Anthony Liguoric227f092009-10-01 16:12:16 -05001694 subpage_t *mmio;
blueswir1db7b5422007-05-26 17:36:03 +00001695
Anthony Liguori7267c092011-08-20 22:09:37 -05001696 mmio = g_malloc0(sizeof(subpage_t));
aliguori1eec6142009-02-05 22:06:18 +00001697
1698 mmio->base = base;
Avi Kivity70c68e42012-01-02 12:32:48 +02001699 memory_region_init_io(&mmio->iomem, &subpage_ops, mmio,
1700 "subpage", TARGET_PAGE_SIZE);
Avi Kivityb3b00c72012-01-02 13:20:11 +02001701 mmio->iomem.subpage = true;
blueswir1db7b5422007-05-26 17:36:03 +00001702#if defined(DEBUG_SUBPAGE)
aliguori1eec6142009-02-05 22:06:18 +00001703 printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__,
1704 mmio, base, TARGET_PAGE_SIZE, subpage_memory);
blueswir1db7b5422007-05-26 17:36:03 +00001705#endif
Avi Kivity0f0cb162012-02-13 17:14:32 +02001706 subpage_register(mmio, 0, TARGET_PAGE_SIZE-1, phys_section_unassigned);
blueswir1db7b5422007-05-26 17:36:03 +00001707
1708 return mmio;
1709}
1710
Avi Kivity5312bd82012-02-12 18:32:55 +02001711static uint16_t dummy_section(MemoryRegion *mr)
1712{
1713 MemoryRegionSection section = {
1714 .mr = mr,
1715 .offset_within_address_space = 0,
1716 .offset_within_region = 0,
1717 .size = UINT64_MAX,
1718 };
1719
1720 return phys_section_add(&section);
1721}
1722
Avi Kivitya8170e52012-10-23 12:30:10 +02001723MemoryRegion *iotlb_to_region(hwaddr index)
Avi Kivityaa102232012-03-08 17:06:55 +02001724{
Avi Kivity37ec01d2012-03-08 18:08:35 +02001725 return phys_sections[index & ~TARGET_PAGE_MASK].mr;
Avi Kivityaa102232012-03-08 17:06:55 +02001726}
1727
Avi Kivitye9179ce2009-06-14 11:38:52 +03001728static void io_mem_init(void)
1729{
Paolo Bonzinibf8d5162013-05-24 14:39:13 +02001730 memory_region_init_io(&io_mem_rom, &unassigned_mem_ops, NULL, "rom", UINT64_MAX);
Avi Kivity0e0df1e2012-01-02 00:32:15 +02001731 memory_region_init_io(&io_mem_unassigned, &unassigned_mem_ops, NULL,
1732 "unassigned", UINT64_MAX);
1733 memory_region_init_io(&io_mem_notdirty, &notdirty_mem_ops, NULL,
1734 "notdirty", UINT64_MAX);
Avi Kivityde712f92012-01-02 12:41:07 +02001735 memory_region_init_io(&io_mem_subpage_ram, &subpage_ram_ops, NULL,
1736 "subpage-ram", UINT64_MAX);
Avi Kivity1ec9b902012-01-02 12:47:48 +02001737 memory_region_init_io(&io_mem_watch, &watch_mem_ops, NULL,
1738 "watch", UINT64_MAX);
Avi Kivitye9179ce2009-06-14 11:38:52 +03001739}
1740
Avi Kivityac1970f2012-10-03 16:22:53 +02001741static void mem_begin(MemoryListener *listener)
1742{
1743 AddressSpaceDispatch *d = container_of(listener, AddressSpaceDispatch, listener);
1744
1745 destroy_all_mappings(d);
1746 d->phys_map.ptr = PHYS_MAP_NODE_NIL;
1747}
1748
Avi Kivity50c1e142012-02-08 21:36:02 +02001749static void core_begin(MemoryListener *listener)
1750{
Avi Kivity5312bd82012-02-12 18:32:55 +02001751 phys_sections_clear();
1752 phys_section_unassigned = dummy_section(&io_mem_unassigned);
Avi Kivityaa102232012-03-08 17:06:55 +02001753 phys_section_notdirty = dummy_section(&io_mem_notdirty);
1754 phys_section_rom = dummy_section(&io_mem_rom);
1755 phys_section_watch = dummy_section(&io_mem_watch);
Avi Kivity50c1e142012-02-08 21:36:02 +02001756}
1757
Avi Kivity1d711482012-10-02 18:54:45 +02001758static void tcg_commit(MemoryListener *listener)
Avi Kivity50c1e142012-02-08 21:36:02 +02001759{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001760 CPUArchState *env;
Avi Kivity117712c2012-02-12 21:23:17 +02001761
1762 /* since each CPU stores ram addresses in its TLB cache, we must
1763 reset the modified entries */
1764 /* XXX: slow ! */
1765 for(env = first_cpu; env != NULL; env = env->next_cpu) {
1766 tlb_flush(env, 1);
1767 }
Avi Kivity50c1e142012-02-08 21:36:02 +02001768}
1769
Avi Kivity93632742012-02-08 16:54:16 +02001770static void core_log_global_start(MemoryListener *listener)
1771{
1772 cpu_physical_memory_set_dirty_tracking(1);
1773}
1774
1775static void core_log_global_stop(MemoryListener *listener)
1776{
1777 cpu_physical_memory_set_dirty_tracking(0);
1778}
1779
Avi Kivity4855d412012-02-08 21:16:05 +02001780static void io_region_add(MemoryListener *listener,
1781 MemoryRegionSection *section)
1782{
Avi Kivitya2d33522012-03-05 17:40:12 +02001783 MemoryRegionIORange *mrio = g_new(MemoryRegionIORange, 1);
1784
1785 mrio->mr = section->mr;
1786 mrio->offset = section->offset_within_region;
1787 iorange_init(&mrio->iorange, &memory_region_iorange_ops,
Avi Kivity4855d412012-02-08 21:16:05 +02001788 section->offset_within_address_space, section->size);
Avi Kivitya2d33522012-03-05 17:40:12 +02001789 ioport_register(&mrio->iorange);
Avi Kivity4855d412012-02-08 21:16:05 +02001790}
1791
1792static void io_region_del(MemoryListener *listener,
1793 MemoryRegionSection *section)
1794{
1795 isa_unassign_ioport(section->offset_within_address_space, section->size);
1796}
1797
Avi Kivity93632742012-02-08 16:54:16 +02001798static MemoryListener core_memory_listener = {
Avi Kivity50c1e142012-02-08 21:36:02 +02001799 .begin = core_begin,
Avi Kivity93632742012-02-08 16:54:16 +02001800 .log_global_start = core_log_global_start,
1801 .log_global_stop = core_log_global_stop,
Avi Kivityac1970f2012-10-03 16:22:53 +02001802 .priority = 1,
Avi Kivity93632742012-02-08 16:54:16 +02001803};
1804
Avi Kivity4855d412012-02-08 21:16:05 +02001805static MemoryListener io_memory_listener = {
1806 .region_add = io_region_add,
1807 .region_del = io_region_del,
Avi Kivity4855d412012-02-08 21:16:05 +02001808 .priority = 0,
1809};
1810
Avi Kivity1d711482012-10-02 18:54:45 +02001811static MemoryListener tcg_memory_listener = {
1812 .commit = tcg_commit,
1813};
1814
Avi Kivityac1970f2012-10-03 16:22:53 +02001815void address_space_init_dispatch(AddressSpace *as)
1816{
1817 AddressSpaceDispatch *d = g_new(AddressSpaceDispatch, 1);
1818
1819 d->phys_map = (PhysPageEntry) { .ptr = PHYS_MAP_NODE_NIL, .is_leaf = 0 };
1820 d->listener = (MemoryListener) {
1821 .begin = mem_begin,
1822 .region_add = mem_add,
1823 .region_nop = mem_add,
1824 .priority = 0,
1825 };
1826 as->dispatch = d;
1827 memory_listener_register(&d->listener, as);
1828}
1829
Avi Kivity83f3c252012-10-07 12:59:55 +02001830void address_space_destroy_dispatch(AddressSpace *as)
1831{
1832 AddressSpaceDispatch *d = as->dispatch;
1833
1834 memory_listener_unregister(&d->listener);
1835 destroy_l2_mapping(&d->phys_map, P_L2_LEVELS - 1);
1836 g_free(d);
1837 as->dispatch = NULL;
1838}
1839
Avi Kivity62152b82011-07-26 14:26:14 +03001840static void memory_map_init(void)
1841{
Anthony Liguori7267c092011-08-20 22:09:37 -05001842 system_memory = g_malloc(sizeof(*system_memory));
Avi Kivity8417ceb2011-08-03 11:56:14 +03001843 memory_region_init(system_memory, "system", INT64_MAX);
Avi Kivity2673a5d2012-10-02 18:49:28 +02001844 address_space_init(&address_space_memory, system_memory);
1845 address_space_memory.name = "memory";
Avi Kivity309cb472011-08-08 16:09:03 +03001846
Anthony Liguori7267c092011-08-20 22:09:37 -05001847 system_io = g_malloc(sizeof(*system_io));
Avi Kivity309cb472011-08-08 16:09:03 +03001848 memory_region_init(system_io, "io", 65536);
Avi Kivity2673a5d2012-10-02 18:49:28 +02001849 address_space_init(&address_space_io, system_io);
1850 address_space_io.name = "I/O";
Avi Kivity93632742012-02-08 16:54:16 +02001851
Avi Kivityf6790af2012-10-02 20:13:51 +02001852 memory_listener_register(&core_memory_listener, &address_space_memory);
1853 memory_listener_register(&io_memory_listener, &address_space_io);
1854 memory_listener_register(&tcg_memory_listener, &address_space_memory);
Peter Maydell9e119082012-10-29 11:34:32 +10001855
1856 dma_context_init(&dma_context_memory, &address_space_memory,
1857 NULL, NULL, NULL);
Avi Kivity62152b82011-07-26 14:26:14 +03001858}
1859
1860MemoryRegion *get_system_memory(void)
1861{
1862 return system_memory;
1863}
1864
Avi Kivity309cb472011-08-08 16:09:03 +03001865MemoryRegion *get_system_io(void)
1866{
1867 return system_io;
1868}
1869
pbrooke2eef172008-06-08 01:09:01 +00001870#endif /* !defined(CONFIG_USER_ONLY) */
1871
bellard13eb76e2004-01-24 15:23:36 +00001872/* physical memory access (slow version, mainly for debug) */
1873#if defined(CONFIG_USER_ONLY)
Andreas Färber9349b4f2012-03-14 01:38:32 +01001874int cpu_memory_rw_debug(CPUArchState *env, target_ulong addr,
Paul Brooka68fe892010-03-01 00:08:59 +00001875 uint8_t *buf, int len, int is_write)
bellard13eb76e2004-01-24 15:23:36 +00001876{
1877 int l, flags;
1878 target_ulong page;
pbrook53a59602006-03-25 19:31:22 +00001879 void * p;
bellard13eb76e2004-01-24 15:23:36 +00001880
1881 while (len > 0) {
1882 page = addr & TARGET_PAGE_MASK;
1883 l = (page + TARGET_PAGE_SIZE) - addr;
1884 if (l > len)
1885 l = len;
1886 flags = page_get_flags(page);
1887 if (!(flags & PAGE_VALID))
Paul Brooka68fe892010-03-01 00:08:59 +00001888 return -1;
bellard13eb76e2004-01-24 15:23:36 +00001889 if (is_write) {
1890 if (!(flags & PAGE_WRITE))
Paul Brooka68fe892010-03-01 00:08:59 +00001891 return -1;
bellard579a97f2007-11-11 14:26:47 +00001892 /* XXX: this code should not depend on lock_user */
aurel3272fb7da2008-04-27 23:53:45 +00001893 if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
Paul Brooka68fe892010-03-01 00:08:59 +00001894 return -1;
aurel3272fb7da2008-04-27 23:53:45 +00001895 memcpy(p, buf, l);
1896 unlock_user(p, addr, l);
bellard13eb76e2004-01-24 15:23:36 +00001897 } else {
1898 if (!(flags & PAGE_READ))
Paul Brooka68fe892010-03-01 00:08:59 +00001899 return -1;
bellard579a97f2007-11-11 14:26:47 +00001900 /* XXX: this code should not depend on lock_user */
aurel3272fb7da2008-04-27 23:53:45 +00001901 if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
Paul Brooka68fe892010-03-01 00:08:59 +00001902 return -1;
aurel3272fb7da2008-04-27 23:53:45 +00001903 memcpy(buf, p, l);
aurel325b257572008-04-28 08:54:59 +00001904 unlock_user(p, addr, 0);
bellard13eb76e2004-01-24 15:23:36 +00001905 }
1906 len -= l;
1907 buf += l;
1908 addr += l;
1909 }
Paul Brooka68fe892010-03-01 00:08:59 +00001910 return 0;
bellard13eb76e2004-01-24 15:23:36 +00001911}
bellard8df1cd02005-01-28 22:37:22 +00001912
bellard13eb76e2004-01-24 15:23:36 +00001913#else
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00001914
Avi Kivitya8170e52012-10-23 12:30:10 +02001915static void invalidate_and_set_dirty(hwaddr addr,
1916 hwaddr length)
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00001917{
1918 if (!cpu_physical_memory_is_dirty(addr)) {
1919 /* invalidate code */
1920 tb_invalidate_phys_page_range(addr, addr + length, 0);
1921 /* set dirty bit */
1922 cpu_physical_memory_set_dirty_flags(addr, (0xff & ~CODE_DIRTY_FLAG));
1923 }
Anthony PERARDe2269392012-10-03 13:49:22 +00001924 xen_modified_memory(addr, length);
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00001925}
1926
Paolo Bonzini2bbfa052013-05-24 12:29:54 +02001927static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
1928{
1929 if (memory_region_is_ram(mr)) {
1930 return !(is_write && mr->readonly);
1931 }
1932 if (memory_region_is_romd(mr)) {
1933 return !is_write;
1934 }
1935
1936 return false;
1937}
1938
Jan Kiszkaf52cc462013-05-26 21:42:40 +02001939static inline int memory_access_size(MemoryRegion *mr, int l, hwaddr addr)
Paolo Bonzini82f25632013-05-24 11:59:43 +02001940{
Jan Kiszkaf52cc462013-05-26 21:42:40 +02001941 if (l >= 4 && (((addr & 3) == 0 || mr->ops->impl.unaligned))) {
Paolo Bonzini82f25632013-05-24 11:59:43 +02001942 return 4;
1943 }
Jan Kiszkaf52cc462013-05-26 21:42:40 +02001944 if (l >= 2 && (((addr & 1) == 0) || mr->ops->impl.unaligned)) {
Paolo Bonzini82f25632013-05-24 11:59:43 +02001945 return 2;
1946 }
1947 return 1;
1948}
1949
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02001950bool address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
Avi Kivityac1970f2012-10-03 16:22:53 +02001951 int len, bool is_write)
bellard13eb76e2004-01-24 15:23:36 +00001952{
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001953 hwaddr l;
bellard13eb76e2004-01-24 15:23:36 +00001954 uint8_t *ptr;
Paolo Bonzini791af8c2013-05-24 16:10:39 +02001955 uint64_t val;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001956 hwaddr addr1;
Avi Kivityf3705d52012-03-08 16:16:34 +02001957 MemoryRegionSection *section;
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02001958 bool error = false;
ths3b46e622007-09-17 08:09:54 +00001959
bellard13eb76e2004-01-24 15:23:36 +00001960 while (len > 0) {
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001961 l = len;
1962 section = address_space_translate(as, addr, &addr1, &l, is_write);
ths3b46e622007-09-17 08:09:54 +00001963
bellard13eb76e2004-01-24 15:23:36 +00001964 if (is_write) {
Paolo Bonzini2bbfa052013-05-24 12:29:54 +02001965 if (!memory_access_is_direct(section->mr, is_write)) {
Jan Kiszkaf52cc462013-05-26 21:42:40 +02001966 l = memory_access_size(section->mr, l, addr1);
bellard6a00d602005-11-21 23:25:50 +00001967 /* XXX: could force cpu_single_env to NULL to avoid
1968 potential bugs */
Paolo Bonzini82f25632013-05-24 11:59:43 +02001969 if (l == 4) {
bellard1c213d12005-09-03 10:49:04 +00001970 /* 32 bit write access */
bellardc27004e2005-01-03 23:35:10 +00001971 val = ldl_p(buf);
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02001972 error |= io_mem_write(section->mr, addr1, val, 4);
Paolo Bonzini82f25632013-05-24 11:59:43 +02001973 } else if (l == 2) {
bellard1c213d12005-09-03 10:49:04 +00001974 /* 16 bit write access */
bellardc27004e2005-01-03 23:35:10 +00001975 val = lduw_p(buf);
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02001976 error |= io_mem_write(section->mr, addr1, val, 2);
bellard13eb76e2004-01-24 15:23:36 +00001977 } else {
bellard1c213d12005-09-03 10:49:04 +00001978 /* 8 bit write access */
bellardc27004e2005-01-03 23:35:10 +00001979 val = ldub_p(buf);
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02001980 error |= io_mem_write(section->mr, addr1, val, 1);
bellard13eb76e2004-01-24 15:23:36 +00001981 }
Paolo Bonzini2bbfa052013-05-24 12:29:54 +02001982 } else {
Paolo Bonzini149f54b2013-05-24 12:59:37 +02001983 addr1 += memory_region_get_ram_addr(section->mr);
bellard13eb76e2004-01-24 15:23:36 +00001984 /* RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00001985 ptr = qemu_get_ram_ptr(addr1);
bellard13eb76e2004-01-24 15:23:36 +00001986 memcpy(ptr, buf, l);
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00001987 invalidate_and_set_dirty(addr1, l);
bellard13eb76e2004-01-24 15:23:36 +00001988 }
1989 } else {
Paolo Bonzini2bbfa052013-05-24 12:29:54 +02001990 if (!memory_access_is_direct(section->mr, is_write)) {
bellard13eb76e2004-01-24 15:23:36 +00001991 /* I/O case */
Jan Kiszkaf52cc462013-05-26 21:42:40 +02001992 l = memory_access_size(section->mr, l, addr1);
Paolo Bonzini82f25632013-05-24 11:59:43 +02001993 if (l == 4) {
bellard13eb76e2004-01-24 15:23:36 +00001994 /* 32 bit read access */
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02001995 error |= io_mem_read(section->mr, addr1, &val, 4);
bellardc27004e2005-01-03 23:35:10 +00001996 stl_p(buf, val);
Paolo Bonzini82f25632013-05-24 11:59:43 +02001997 } else if (l == 2) {
bellard13eb76e2004-01-24 15:23:36 +00001998 /* 16 bit read access */
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02001999 error |= io_mem_read(section->mr, addr1, &val, 2);
bellardc27004e2005-01-03 23:35:10 +00002000 stw_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00002001 } else {
bellard1c213d12005-09-03 10:49:04 +00002002 /* 8 bit read access */
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02002003 error |= io_mem_read(section->mr, addr1, &val, 1);
bellardc27004e2005-01-03 23:35:10 +00002004 stb_p(buf, val);
bellard13eb76e2004-01-24 15:23:36 +00002005 }
2006 } else {
2007 /* RAM case */
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002008 ptr = qemu_get_ram_ptr(section->mr->ram_addr + addr1);
Avi Kivityf3705d52012-03-08 16:16:34 +02002009 memcpy(buf, ptr, l);
bellard13eb76e2004-01-24 15:23:36 +00002010 }
2011 }
2012 len -= l;
2013 buf += l;
2014 addr += l;
2015 }
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02002016
2017 return error;
bellard13eb76e2004-01-24 15:23:36 +00002018}
bellard8df1cd02005-01-28 22:37:22 +00002019
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02002020bool address_space_write(AddressSpace *as, hwaddr addr,
Avi Kivityac1970f2012-10-03 16:22:53 +02002021 const uint8_t *buf, int len)
2022{
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02002023 return address_space_rw(as, addr, (uint8_t *)buf, len, true);
Avi Kivityac1970f2012-10-03 16:22:53 +02002024}
2025
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02002026bool address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len)
Avi Kivityac1970f2012-10-03 16:22:53 +02002027{
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02002028 return address_space_rw(as, addr, buf, len, false);
Avi Kivityac1970f2012-10-03 16:22:53 +02002029}
2030
2031
Avi Kivitya8170e52012-10-23 12:30:10 +02002032void cpu_physical_memory_rw(hwaddr addr, uint8_t *buf,
Avi Kivityac1970f2012-10-03 16:22:53 +02002033 int len, int is_write)
2034{
Paolo Bonzinifd8aaa72013-05-21 09:56:55 +02002035 address_space_rw(&address_space_memory, addr, buf, len, is_write);
Avi Kivityac1970f2012-10-03 16:22:53 +02002036}
2037
bellardd0ecd2a2006-04-23 17:14:48 +00002038/* used for ROM loading : can write in RAM and ROM */
Avi Kivitya8170e52012-10-23 12:30:10 +02002039void cpu_physical_memory_write_rom(hwaddr addr,
bellardd0ecd2a2006-04-23 17:14:48 +00002040 const uint8_t *buf, int len)
2041{
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002042 hwaddr l;
bellardd0ecd2a2006-04-23 17:14:48 +00002043 uint8_t *ptr;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002044 hwaddr addr1;
Avi Kivityf3705d52012-03-08 16:16:34 +02002045 MemoryRegionSection *section;
ths3b46e622007-09-17 08:09:54 +00002046
bellardd0ecd2a2006-04-23 17:14:48 +00002047 while (len > 0) {
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002048 l = len;
2049 section = address_space_translate(&address_space_memory,
2050 addr, &addr1, &l, true);
ths3b46e622007-09-17 08:09:54 +00002051
Blue Swirlcc5bea62012-04-14 14:56:48 +00002052 if (!(memory_region_is_ram(section->mr) ||
2053 memory_region_is_romd(section->mr))) {
bellardd0ecd2a2006-04-23 17:14:48 +00002054 /* do nothing */
2055 } else {
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002056 addr1 += memory_region_get_ram_addr(section->mr);
bellardd0ecd2a2006-04-23 17:14:48 +00002057 /* ROM/RAM case */
pbrook5579c7f2009-04-11 14:47:08 +00002058 ptr = qemu_get_ram_ptr(addr1);
bellardd0ecd2a2006-04-23 17:14:48 +00002059 memcpy(ptr, buf, l);
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00002060 invalidate_and_set_dirty(addr1, l);
bellardd0ecd2a2006-04-23 17:14:48 +00002061 }
2062 len -= l;
2063 buf += l;
2064 addr += l;
2065 }
2066}
2067
aliguori6d16c2f2009-01-22 16:59:11 +00002068typedef struct {
2069 void *buffer;
Avi Kivitya8170e52012-10-23 12:30:10 +02002070 hwaddr addr;
2071 hwaddr len;
aliguori6d16c2f2009-01-22 16:59:11 +00002072} BounceBuffer;
2073
2074static BounceBuffer bounce;
2075
aliguoriba223c22009-01-22 16:59:16 +00002076typedef struct MapClient {
2077 void *opaque;
2078 void (*callback)(void *opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002079 QLIST_ENTRY(MapClient) link;
aliguoriba223c22009-01-22 16:59:16 +00002080} MapClient;
2081
Blue Swirl72cf2d42009-09-12 07:36:22 +00002082static QLIST_HEAD(map_client_list, MapClient) map_client_list
2083 = QLIST_HEAD_INITIALIZER(map_client_list);
aliguoriba223c22009-01-22 16:59:16 +00002084
2085void *cpu_register_map_client(void *opaque, void (*callback)(void *opaque))
2086{
Anthony Liguori7267c092011-08-20 22:09:37 -05002087 MapClient *client = g_malloc(sizeof(*client));
aliguoriba223c22009-01-22 16:59:16 +00002088
2089 client->opaque = opaque;
2090 client->callback = callback;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002091 QLIST_INSERT_HEAD(&map_client_list, client, link);
aliguoriba223c22009-01-22 16:59:16 +00002092 return client;
2093}
2094
Blue Swirl8b9c99d2012-10-28 11:04:51 +00002095static void cpu_unregister_map_client(void *_client)
aliguoriba223c22009-01-22 16:59:16 +00002096{
2097 MapClient *client = (MapClient *)_client;
2098
Blue Swirl72cf2d42009-09-12 07:36:22 +00002099 QLIST_REMOVE(client, link);
Anthony Liguori7267c092011-08-20 22:09:37 -05002100 g_free(client);
aliguoriba223c22009-01-22 16:59:16 +00002101}
2102
2103static void cpu_notify_map_clients(void)
2104{
2105 MapClient *client;
2106
Blue Swirl72cf2d42009-09-12 07:36:22 +00002107 while (!QLIST_EMPTY(&map_client_list)) {
2108 client = QLIST_FIRST(&map_client_list);
aliguoriba223c22009-01-22 16:59:16 +00002109 client->callback(client->opaque);
Isaku Yamahata34d5e942009-06-26 18:57:18 +09002110 cpu_unregister_map_client(client);
aliguoriba223c22009-01-22 16:59:16 +00002111 }
2112}
2113
Paolo Bonzini51644ab2013-04-11 15:40:59 +02002114bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write)
2115{
2116 MemoryRegionSection *section;
2117 hwaddr l, xlat;
2118
2119 while (len > 0) {
2120 l = len;
2121 section = address_space_translate(as, addr, &xlat, &l, is_write);
2122 if (!memory_access_is_direct(section->mr, is_write)) {
Jan Kiszkaf52cc462013-05-26 21:42:40 +02002123 l = memory_access_size(section->mr, l, addr);
Paolo Bonzini51644ab2013-04-11 15:40:59 +02002124 if (!memory_region_access_valid(section->mr, xlat, l, is_write)) {
2125 return false;
2126 }
2127 }
2128
2129 len -= l;
2130 addr += l;
2131 }
2132 return true;
2133}
2134
aliguori6d16c2f2009-01-22 16:59:11 +00002135/* Map a physical memory region into a host virtual address.
2136 * May map a subset of the requested range, given by and returned in *plen.
2137 * May return NULL if resources needed to perform the mapping are exhausted.
2138 * Use only for reads OR writes - not for read-modify-write operations.
aliguoriba223c22009-01-22 16:59:16 +00002139 * Use cpu_register_map_client() to know when retrying the map operation is
2140 * likely to succeed.
aliguori6d16c2f2009-01-22 16:59:11 +00002141 */
Avi Kivityac1970f2012-10-03 16:22:53 +02002142void *address_space_map(AddressSpace *as,
Avi Kivitya8170e52012-10-23 12:30:10 +02002143 hwaddr addr,
2144 hwaddr *plen,
Avi Kivityac1970f2012-10-03 16:22:53 +02002145 bool is_write)
aliguori6d16c2f2009-01-22 16:59:11 +00002146{
Avi Kivitya8170e52012-10-23 12:30:10 +02002147 hwaddr len = *plen;
2148 hwaddr todo = 0;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002149 hwaddr l, xlat;
Avi Kivityf3705d52012-03-08 16:16:34 +02002150 MemoryRegionSection *section;
Anthony PERARDf15fbc42011-07-20 08:17:42 +00002151 ram_addr_t raddr = RAM_ADDR_MAX;
Stefano Stabellini8ab934f2011-06-27 18:26:06 +01002152 ram_addr_t rlen;
2153 void *ret;
aliguori6d16c2f2009-01-22 16:59:11 +00002154
2155 while (len > 0) {
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002156 l = len;
2157 section = address_space_translate(as, addr, &xlat, &l, is_write);
aliguori6d16c2f2009-01-22 16:59:11 +00002158
Paolo Bonzini2bbfa052013-05-24 12:29:54 +02002159 if (!memory_access_is_direct(section->mr, is_write)) {
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01002160 if (todo || bounce.buffer) {
aliguori6d16c2f2009-01-22 16:59:11 +00002161 break;
2162 }
2163 bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, TARGET_PAGE_SIZE);
2164 bounce.addr = addr;
2165 bounce.len = l;
2166 if (!is_write) {
Avi Kivityac1970f2012-10-03 16:22:53 +02002167 address_space_read(as, addr, bounce.buffer, l);
aliguori6d16c2f2009-01-22 16:59:11 +00002168 }
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01002169
2170 *plen = l;
2171 return bounce.buffer;
aliguori6d16c2f2009-01-22 16:59:11 +00002172 }
Stefano Stabellini8ab934f2011-06-27 18:26:06 +01002173 if (!todo) {
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002174 raddr = memory_region_get_ram_addr(section->mr) + xlat;
2175 } else {
2176 if (memory_region_get_ram_addr(section->mr) + xlat != raddr + todo) {
2177 break;
2178 }
Stefano Stabellini8ab934f2011-06-27 18:26:06 +01002179 }
aliguori6d16c2f2009-01-22 16:59:11 +00002180
2181 len -= l;
2182 addr += l;
Stefano Stabellini38bee5d2011-05-19 18:35:45 +01002183 todo += l;
aliguori6d16c2f2009-01-22 16:59:11 +00002184 }
Stefano Stabellini8ab934f2011-06-27 18:26:06 +01002185 rlen = todo;
2186 ret = qemu_ram_ptr_length(raddr, &rlen);
2187 *plen = rlen;
2188 return ret;
aliguori6d16c2f2009-01-22 16:59:11 +00002189}
2190
Avi Kivityac1970f2012-10-03 16:22:53 +02002191/* Unmaps a memory region previously mapped by address_space_map().
aliguori6d16c2f2009-01-22 16:59:11 +00002192 * Will also mark the memory as dirty if is_write == 1. access_len gives
2193 * the amount of memory that was actually read or written by the caller.
2194 */
Avi Kivitya8170e52012-10-23 12:30:10 +02002195void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
2196 int is_write, hwaddr access_len)
aliguori6d16c2f2009-01-22 16:59:11 +00002197{
2198 if (buffer != bounce.buffer) {
2199 if (is_write) {
Marcelo Tosattie8902612010-10-11 15:31:19 -03002200 ram_addr_t addr1 = qemu_ram_addr_from_host_nofail(buffer);
aliguori6d16c2f2009-01-22 16:59:11 +00002201 while (access_len) {
2202 unsigned l;
2203 l = TARGET_PAGE_SIZE;
2204 if (l > access_len)
2205 l = access_len;
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00002206 invalidate_and_set_dirty(addr1, l);
aliguori6d16c2f2009-01-22 16:59:11 +00002207 addr1 += l;
2208 access_len -= l;
2209 }
2210 }
Jan Kiszka868bb332011-06-21 22:59:09 +02002211 if (xen_enabled()) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +02002212 xen_invalidate_map_cache_entry(buffer);
Anthony PERARD050a0dd2010-09-16 13:57:49 +01002213 }
aliguori6d16c2f2009-01-22 16:59:11 +00002214 return;
2215 }
2216 if (is_write) {
Avi Kivityac1970f2012-10-03 16:22:53 +02002217 address_space_write(as, bounce.addr, bounce.buffer, access_len);
aliguori6d16c2f2009-01-22 16:59:11 +00002218 }
Herve Poussineauf8a83242010-01-24 21:23:56 +00002219 qemu_vfree(bounce.buffer);
aliguori6d16c2f2009-01-22 16:59:11 +00002220 bounce.buffer = NULL;
aliguoriba223c22009-01-22 16:59:16 +00002221 cpu_notify_map_clients();
aliguori6d16c2f2009-01-22 16:59:11 +00002222}
bellardd0ecd2a2006-04-23 17:14:48 +00002223
Avi Kivitya8170e52012-10-23 12:30:10 +02002224void *cpu_physical_memory_map(hwaddr addr,
2225 hwaddr *plen,
Avi Kivityac1970f2012-10-03 16:22:53 +02002226 int is_write)
2227{
2228 return address_space_map(&address_space_memory, addr, plen, is_write);
2229}
2230
Avi Kivitya8170e52012-10-23 12:30:10 +02002231void cpu_physical_memory_unmap(void *buffer, hwaddr len,
2232 int is_write, hwaddr access_len)
Avi Kivityac1970f2012-10-03 16:22:53 +02002233{
2234 return address_space_unmap(&address_space_memory, buffer, len, is_write, access_len);
2235}
2236
bellard8df1cd02005-01-28 22:37:22 +00002237/* warning: addr must be aligned */
Avi Kivitya8170e52012-10-23 12:30:10 +02002238static inline uint32_t ldl_phys_internal(hwaddr addr,
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002239 enum device_endian endian)
bellard8df1cd02005-01-28 22:37:22 +00002240{
bellard8df1cd02005-01-28 22:37:22 +00002241 uint8_t *ptr;
Paolo Bonzini791af8c2013-05-24 16:10:39 +02002242 uint64_t val;
Avi Kivityf3705d52012-03-08 16:16:34 +02002243 MemoryRegionSection *section;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002244 hwaddr l = 4;
2245 hwaddr addr1;
bellard8df1cd02005-01-28 22:37:22 +00002246
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002247 section = address_space_translate(&address_space_memory, addr, &addr1, &l,
2248 false);
Paolo Bonzini2bbfa052013-05-24 12:29:54 +02002249 if (l < 4 || !memory_access_is_direct(section->mr, false)) {
bellard8df1cd02005-01-28 22:37:22 +00002250 /* I/O case */
Paolo Bonzini791af8c2013-05-24 16:10:39 +02002251 io_mem_read(section->mr, addr1, &val, 4);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002252#if defined(TARGET_WORDS_BIGENDIAN)
2253 if (endian == DEVICE_LITTLE_ENDIAN) {
2254 val = bswap32(val);
2255 }
2256#else
2257 if (endian == DEVICE_BIG_ENDIAN) {
2258 val = bswap32(val);
2259 }
2260#endif
bellard8df1cd02005-01-28 22:37:22 +00002261 } else {
2262 /* RAM case */
Avi Kivityf3705d52012-03-08 16:16:34 +02002263 ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(section->mr)
Avi Kivity06ef3522012-02-13 16:11:22 +02002264 & TARGET_PAGE_MASK)
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002265 + addr1);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002266 switch (endian) {
2267 case DEVICE_LITTLE_ENDIAN:
2268 val = ldl_le_p(ptr);
2269 break;
2270 case DEVICE_BIG_ENDIAN:
2271 val = ldl_be_p(ptr);
2272 break;
2273 default:
2274 val = ldl_p(ptr);
2275 break;
2276 }
bellard8df1cd02005-01-28 22:37:22 +00002277 }
2278 return val;
2279}
2280
Avi Kivitya8170e52012-10-23 12:30:10 +02002281uint32_t ldl_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002282{
2283 return ldl_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
2284}
2285
Avi Kivitya8170e52012-10-23 12:30:10 +02002286uint32_t ldl_le_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002287{
2288 return ldl_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
2289}
2290
Avi Kivitya8170e52012-10-23 12:30:10 +02002291uint32_t ldl_be_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002292{
2293 return ldl_phys_internal(addr, DEVICE_BIG_ENDIAN);
2294}
2295
bellard84b7b8e2005-11-28 21:19:04 +00002296/* warning: addr must be aligned */
Avi Kivitya8170e52012-10-23 12:30:10 +02002297static inline uint64_t ldq_phys_internal(hwaddr addr,
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002298 enum device_endian endian)
bellard84b7b8e2005-11-28 21:19:04 +00002299{
bellard84b7b8e2005-11-28 21:19:04 +00002300 uint8_t *ptr;
2301 uint64_t val;
Avi Kivityf3705d52012-03-08 16:16:34 +02002302 MemoryRegionSection *section;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002303 hwaddr l = 8;
2304 hwaddr addr1;
bellard84b7b8e2005-11-28 21:19:04 +00002305
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002306 section = address_space_translate(&address_space_memory, addr, &addr1, &l,
2307 false);
Paolo Bonzini2bbfa052013-05-24 12:29:54 +02002308 if (l < 8 || !memory_access_is_direct(section->mr, false)) {
bellard84b7b8e2005-11-28 21:19:04 +00002309 /* I/O case */
Paolo Bonzini791af8c2013-05-24 16:10:39 +02002310 io_mem_read(section->mr, addr1, &val, 8);
Paolo Bonzini968a5622013-05-24 17:58:37 +02002311#if defined(TARGET_WORDS_BIGENDIAN)
2312 if (endian == DEVICE_LITTLE_ENDIAN) {
2313 val = bswap64(val);
2314 }
2315#else
2316 if (endian == DEVICE_BIG_ENDIAN) {
2317 val = bswap64(val);
2318 }
2319#endif
bellard84b7b8e2005-11-28 21:19:04 +00002320 } else {
2321 /* RAM case */
Avi Kivityf3705d52012-03-08 16:16:34 +02002322 ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(section->mr)
Avi Kivity06ef3522012-02-13 16:11:22 +02002323 & TARGET_PAGE_MASK)
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002324 + addr1);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002325 switch (endian) {
2326 case DEVICE_LITTLE_ENDIAN:
2327 val = ldq_le_p(ptr);
2328 break;
2329 case DEVICE_BIG_ENDIAN:
2330 val = ldq_be_p(ptr);
2331 break;
2332 default:
2333 val = ldq_p(ptr);
2334 break;
2335 }
bellard84b7b8e2005-11-28 21:19:04 +00002336 }
2337 return val;
2338}
2339
Avi Kivitya8170e52012-10-23 12:30:10 +02002340uint64_t ldq_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002341{
2342 return ldq_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
2343}
2344
Avi Kivitya8170e52012-10-23 12:30:10 +02002345uint64_t ldq_le_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002346{
2347 return ldq_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
2348}
2349
Avi Kivitya8170e52012-10-23 12:30:10 +02002350uint64_t ldq_be_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002351{
2352 return ldq_phys_internal(addr, DEVICE_BIG_ENDIAN);
2353}
2354
bellardaab33092005-10-30 20:48:42 +00002355/* XXX: optimize */
Avi Kivitya8170e52012-10-23 12:30:10 +02002356uint32_t ldub_phys(hwaddr addr)
bellardaab33092005-10-30 20:48:42 +00002357{
2358 uint8_t val;
2359 cpu_physical_memory_read(addr, &val, 1);
2360 return val;
2361}
2362
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002363/* warning: addr must be aligned */
Avi Kivitya8170e52012-10-23 12:30:10 +02002364static inline uint32_t lduw_phys_internal(hwaddr addr,
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002365 enum device_endian endian)
bellardaab33092005-10-30 20:48:42 +00002366{
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002367 uint8_t *ptr;
2368 uint64_t val;
Avi Kivityf3705d52012-03-08 16:16:34 +02002369 MemoryRegionSection *section;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002370 hwaddr l = 2;
2371 hwaddr addr1;
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002372
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002373 section = address_space_translate(&address_space_memory, addr, &addr1, &l,
2374 false);
Paolo Bonzini2bbfa052013-05-24 12:29:54 +02002375 if (l < 2 || !memory_access_is_direct(section->mr, false)) {
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002376 /* I/O case */
Paolo Bonzini791af8c2013-05-24 16:10:39 +02002377 io_mem_read(section->mr, addr1, &val, 2);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002378#if defined(TARGET_WORDS_BIGENDIAN)
2379 if (endian == DEVICE_LITTLE_ENDIAN) {
2380 val = bswap16(val);
2381 }
2382#else
2383 if (endian == DEVICE_BIG_ENDIAN) {
2384 val = bswap16(val);
2385 }
2386#endif
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002387 } else {
2388 /* RAM case */
Avi Kivityf3705d52012-03-08 16:16:34 +02002389 ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(section->mr)
Avi Kivity06ef3522012-02-13 16:11:22 +02002390 & TARGET_PAGE_MASK)
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002391 + addr1);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002392 switch (endian) {
2393 case DEVICE_LITTLE_ENDIAN:
2394 val = lduw_le_p(ptr);
2395 break;
2396 case DEVICE_BIG_ENDIAN:
2397 val = lduw_be_p(ptr);
2398 break;
2399 default:
2400 val = lduw_p(ptr);
2401 break;
2402 }
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002403 }
2404 return val;
bellardaab33092005-10-30 20:48:42 +00002405}
2406
Avi Kivitya8170e52012-10-23 12:30:10 +02002407uint32_t lduw_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002408{
2409 return lduw_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
2410}
2411
Avi Kivitya8170e52012-10-23 12:30:10 +02002412uint32_t lduw_le_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002413{
2414 return lduw_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
2415}
2416
Avi Kivitya8170e52012-10-23 12:30:10 +02002417uint32_t lduw_be_phys(hwaddr addr)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002418{
2419 return lduw_phys_internal(addr, DEVICE_BIG_ENDIAN);
2420}
2421
bellard8df1cd02005-01-28 22:37:22 +00002422/* warning: addr must be aligned. The ram page is not masked as dirty
2423 and the code inside is not invalidated. It is useful if the dirty
2424 bits are used to track modified PTEs */
Avi Kivitya8170e52012-10-23 12:30:10 +02002425void stl_phys_notdirty(hwaddr addr, uint32_t val)
bellard8df1cd02005-01-28 22:37:22 +00002426{
bellard8df1cd02005-01-28 22:37:22 +00002427 uint8_t *ptr;
Avi Kivityf3705d52012-03-08 16:16:34 +02002428 MemoryRegionSection *section;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002429 hwaddr l = 4;
2430 hwaddr addr1;
bellard8df1cd02005-01-28 22:37:22 +00002431
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002432 section = address_space_translate(&address_space_memory, addr, &addr1, &l,
2433 true);
Paolo Bonzini2bbfa052013-05-24 12:29:54 +02002434 if (l < 4 || !memory_access_is_direct(section->mr, true)) {
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002435 io_mem_write(section->mr, addr1, val, 4);
bellard8df1cd02005-01-28 22:37:22 +00002436 } else {
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002437 addr1 += memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK;
pbrook5579c7f2009-04-11 14:47:08 +00002438 ptr = qemu_get_ram_ptr(addr1);
bellard8df1cd02005-01-28 22:37:22 +00002439 stl_p(ptr, val);
aliguori74576192008-10-06 14:02:03 +00002440
2441 if (unlikely(in_migration)) {
2442 if (!cpu_physical_memory_is_dirty(addr1)) {
2443 /* invalidate code */
2444 tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
2445 /* set dirty bit */
Yoshiaki Tamuraf7c11b52010-03-23 16:39:53 +09002446 cpu_physical_memory_set_dirty_flags(
2447 addr1, (0xff & ~CODE_DIRTY_FLAG));
aliguori74576192008-10-06 14:02:03 +00002448 }
2449 }
bellard8df1cd02005-01-28 22:37:22 +00002450 }
2451}
2452
2453/* warning: addr must be aligned */
Avi Kivitya8170e52012-10-23 12:30:10 +02002454static inline void stl_phys_internal(hwaddr addr, uint32_t val,
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002455 enum device_endian endian)
bellard8df1cd02005-01-28 22:37:22 +00002456{
bellard8df1cd02005-01-28 22:37:22 +00002457 uint8_t *ptr;
Avi Kivityf3705d52012-03-08 16:16:34 +02002458 MemoryRegionSection *section;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002459 hwaddr l = 4;
2460 hwaddr addr1;
bellard8df1cd02005-01-28 22:37:22 +00002461
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002462 section = address_space_translate(&address_space_memory, addr, &addr1, &l,
2463 true);
Paolo Bonzini2bbfa052013-05-24 12:29:54 +02002464 if (l < 4 || !memory_access_is_direct(section->mr, true)) {
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002465#if defined(TARGET_WORDS_BIGENDIAN)
2466 if (endian == DEVICE_LITTLE_ENDIAN) {
2467 val = bswap32(val);
2468 }
2469#else
2470 if (endian == DEVICE_BIG_ENDIAN) {
2471 val = bswap32(val);
2472 }
2473#endif
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002474 io_mem_write(section->mr, addr1, val, 4);
bellard8df1cd02005-01-28 22:37:22 +00002475 } else {
bellard8df1cd02005-01-28 22:37:22 +00002476 /* RAM case */
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002477 addr1 += memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK;
pbrook5579c7f2009-04-11 14:47:08 +00002478 ptr = qemu_get_ram_ptr(addr1);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002479 switch (endian) {
2480 case DEVICE_LITTLE_ENDIAN:
2481 stl_le_p(ptr, val);
2482 break;
2483 case DEVICE_BIG_ENDIAN:
2484 stl_be_p(ptr, val);
2485 break;
2486 default:
2487 stl_p(ptr, val);
2488 break;
2489 }
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00002490 invalidate_and_set_dirty(addr1, 4);
bellard8df1cd02005-01-28 22:37:22 +00002491 }
2492}
2493
Avi Kivitya8170e52012-10-23 12:30:10 +02002494void stl_phys(hwaddr addr, uint32_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002495{
2496 stl_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN);
2497}
2498
Avi Kivitya8170e52012-10-23 12:30:10 +02002499void stl_le_phys(hwaddr addr, uint32_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002500{
2501 stl_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN);
2502}
2503
Avi Kivitya8170e52012-10-23 12:30:10 +02002504void stl_be_phys(hwaddr addr, uint32_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002505{
2506 stl_phys_internal(addr, val, DEVICE_BIG_ENDIAN);
2507}
2508
bellardaab33092005-10-30 20:48:42 +00002509/* XXX: optimize */
Avi Kivitya8170e52012-10-23 12:30:10 +02002510void stb_phys(hwaddr addr, uint32_t val)
bellardaab33092005-10-30 20:48:42 +00002511{
2512 uint8_t v = val;
2513 cpu_physical_memory_write(addr, &v, 1);
2514}
2515
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002516/* warning: addr must be aligned */
Avi Kivitya8170e52012-10-23 12:30:10 +02002517static inline void stw_phys_internal(hwaddr addr, uint32_t val,
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002518 enum device_endian endian)
bellardaab33092005-10-30 20:48:42 +00002519{
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002520 uint8_t *ptr;
Avi Kivityf3705d52012-03-08 16:16:34 +02002521 MemoryRegionSection *section;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002522 hwaddr l = 2;
2523 hwaddr addr1;
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002524
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002525 section = address_space_translate(&address_space_memory, addr, &addr1, &l,
2526 true);
Paolo Bonzini2bbfa052013-05-24 12:29:54 +02002527 if (l < 2 || !memory_access_is_direct(section->mr, true)) {
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002528#if defined(TARGET_WORDS_BIGENDIAN)
2529 if (endian == DEVICE_LITTLE_ENDIAN) {
2530 val = bswap16(val);
2531 }
2532#else
2533 if (endian == DEVICE_BIG_ENDIAN) {
2534 val = bswap16(val);
2535 }
2536#endif
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002537 io_mem_write(section->mr, addr1, val, 2);
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002538 } else {
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002539 /* RAM case */
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002540 addr1 += memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK;
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002541 ptr = qemu_get_ram_ptr(addr1);
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002542 switch (endian) {
2543 case DEVICE_LITTLE_ENDIAN:
2544 stw_le_p(ptr, val);
2545 break;
2546 case DEVICE_BIG_ENDIAN:
2547 stw_be_p(ptr, val);
2548 break;
2549 default:
2550 stw_p(ptr, val);
2551 break;
2552 }
Anthony PERARD51d7a9e2012-10-03 13:49:05 +00002553 invalidate_and_set_dirty(addr1, 2);
Michael S. Tsirkin733f0b02010-04-06 14:18:19 +03002554 }
bellardaab33092005-10-30 20:48:42 +00002555}
2556
Avi Kivitya8170e52012-10-23 12:30:10 +02002557void stw_phys(hwaddr addr, uint32_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002558{
2559 stw_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN);
2560}
2561
Avi Kivitya8170e52012-10-23 12:30:10 +02002562void stw_le_phys(hwaddr addr, uint32_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002563{
2564 stw_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN);
2565}
2566
Avi Kivitya8170e52012-10-23 12:30:10 +02002567void stw_be_phys(hwaddr addr, uint32_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002568{
2569 stw_phys_internal(addr, val, DEVICE_BIG_ENDIAN);
2570}
2571
bellardaab33092005-10-30 20:48:42 +00002572/* XXX: optimize */
Avi Kivitya8170e52012-10-23 12:30:10 +02002573void stq_phys(hwaddr addr, uint64_t val)
bellardaab33092005-10-30 20:48:42 +00002574{
2575 val = tswap64(val);
Stefan Weil71d2b722011-03-26 21:06:56 +01002576 cpu_physical_memory_write(addr, &val, 8);
bellardaab33092005-10-30 20:48:42 +00002577}
2578
Avi Kivitya8170e52012-10-23 12:30:10 +02002579void stq_le_phys(hwaddr addr, uint64_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002580{
2581 val = cpu_to_le64(val);
2582 cpu_physical_memory_write(addr, &val, 8);
2583}
2584
Avi Kivitya8170e52012-10-23 12:30:10 +02002585void stq_be_phys(hwaddr addr, uint64_t val)
Alexander Graf1e78bcc2011-07-06 09:09:23 +02002586{
2587 val = cpu_to_be64(val);
2588 cpu_physical_memory_write(addr, &val, 8);
2589}
2590
aliguori5e2972f2009-03-28 17:51:36 +00002591/* virtual memory access for debug (includes writing to ROM) */
Andreas Färber9349b4f2012-03-14 01:38:32 +01002592int cpu_memory_rw_debug(CPUArchState *env, target_ulong addr,
bellardb448f2f2004-02-25 23:24:04 +00002593 uint8_t *buf, int len, int is_write)
bellard13eb76e2004-01-24 15:23:36 +00002594{
2595 int l;
Avi Kivitya8170e52012-10-23 12:30:10 +02002596 hwaddr phys_addr;
j_mayer9b3c35e2007-04-07 11:21:28 +00002597 target_ulong page;
bellard13eb76e2004-01-24 15:23:36 +00002598
2599 while (len > 0) {
2600 page = addr & TARGET_PAGE_MASK;
2601 phys_addr = cpu_get_phys_page_debug(env, page);
2602 /* if no physical page mapped, return an error */
2603 if (phys_addr == -1)
2604 return -1;
2605 l = (page + TARGET_PAGE_SIZE) - addr;
2606 if (l > len)
2607 l = len;
aliguori5e2972f2009-03-28 17:51:36 +00002608 phys_addr += (addr & ~TARGET_PAGE_MASK);
aliguori5e2972f2009-03-28 17:51:36 +00002609 if (is_write)
2610 cpu_physical_memory_write_rom(phys_addr, buf, l);
2611 else
aliguori5e2972f2009-03-28 17:51:36 +00002612 cpu_physical_memory_rw(phys_addr, buf, l, is_write);
bellard13eb76e2004-01-24 15:23:36 +00002613 len -= l;
2614 buf += l;
2615 addr += l;
2616 }
2617 return 0;
2618}
Paul Brooka68fe892010-03-01 00:08:59 +00002619#endif
bellard13eb76e2004-01-24 15:23:36 +00002620
Blue Swirl8e4a4242013-01-06 18:30:17 +00002621#if !defined(CONFIG_USER_ONLY)
2622
2623/*
2624 * A helper function for the _utterly broken_ virtio device model to find out if
2625 * it's running on a big endian machine. Don't do this at home kids!
2626 */
2627bool virtio_is_big_endian(void);
2628bool virtio_is_big_endian(void)
2629{
2630#if defined(TARGET_WORDS_BIGENDIAN)
2631 return true;
2632#else
2633 return false;
2634#endif
2635}
2636
2637#endif
2638
Wen Congyang76f35532012-05-07 12:04:18 +08002639#ifndef CONFIG_USER_ONLY
Avi Kivitya8170e52012-10-23 12:30:10 +02002640bool cpu_physical_memory_is_io(hwaddr phys_addr)
Wen Congyang76f35532012-05-07 12:04:18 +08002641{
2642 MemoryRegionSection *section;
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002643 hwaddr l = 1;
Wen Congyang76f35532012-05-07 12:04:18 +08002644
Paolo Bonzini149f54b2013-05-24 12:59:37 +02002645 section = address_space_translate(&address_space_memory,
2646 phys_addr, &phys_addr, &l, false);
Wen Congyang76f35532012-05-07 12:04:18 +08002647
2648 return !(memory_region_is_ram(section->mr) ||
2649 memory_region_is_romd(section->mr));
2650}
2651#endif