exec.c 71.0 KB
Newer Older
B
bellard 已提交
1
/*
2
 *  Virtual page mapping
3
 *
B
bellard 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16
 *  Copyright (c) 2003 Fabrice Bellard
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
B
bellard 已提交
18
 */
B
bellard 已提交
19
#include "config.h"
B
bellard 已提交
20 21 22
#ifdef _WIN32
#include <windows.h>
#else
B
bellard 已提交
23
#include <sys/types.h>
B
bellard 已提交
24 25
#include <sys/mman.h>
#endif
B
bellard 已提交
26

27
#include "qemu-common.h"
B
bellard 已提交
28
#include "cpu.h"
B
bellard 已提交
29
#include "tcg.h"
30
#include "hw/hw.h"
31
#include "hw/qdev.h"
32
#include "qemu/osdep.h"
33
#include "sysemu/kvm.h"
P
Paolo Bonzini 已提交
34
#include "hw/xen/xen.h"
35 36
#include "qemu/timer.h"
#include "qemu/config-file.h"
37
#include "exec/memory.h"
38
#include "sysemu/dma.h"
39
#include "exec/address-spaces.h"
40 41
#if defined(CONFIG_USER_ONLY)
#include <qemu.h>
J
Jun Nakajima 已提交
42
#else /* !CONFIG_USER_ONLY */
43
#include "sysemu/xen-mapcache.h"
44
#include "trace.h"
45
#endif
46
#include "exec/cpu-all.h"
B
bellard 已提交
47

48
#include "exec/cputlb.h"
49
#include "translate-all.h"
50

51
#include "exec/memory-internal.h"
52

53
//#define DEBUG_SUBPAGE
T
ths 已提交
54

55
#if !defined(CONFIG_USER_ONLY)
56
int phys_ram_fd;
A
aliguori 已提交
57
static int in_migration;
P
pbrook 已提交
58

P
Paolo Bonzini 已提交
59
RAMList ram_list = { .blocks = QTAILQ_HEAD_INITIALIZER(ram_list.blocks) };
A
Avi Kivity 已提交
60 61

static MemoryRegion *system_memory;
62
static MemoryRegion *system_io;
A
Avi Kivity 已提交
63

64 65
AddressSpace address_space_io;
AddressSpace address_space_memory;
66
DMAContext dma_context_memory;
67

68 69
MemoryRegion io_mem_rom, io_mem_notdirty;
static MemoryRegion io_mem_unassigned, io_mem_subpage_ram;
70

71
#endif
72

73
CPUArchState *first_cpu;
B
bellard 已提交
74 75
/* current CPU in the current thread. It is only valid inside
   cpu_exec() */
76
DEFINE_TLS(CPUArchState *,cpu_single_env);
P
pbrook 已提交
77
/* 0 = Do not count executed instructions.
T
ths 已提交
78
   1 = Precise instruction counting.
P
pbrook 已提交
79
   2 = Adaptive rate instruction counting.  */
80
int use_icount;
B
bellard 已提交
81

82
#if !defined(CONFIG_USER_ONLY)
83

84 85 86
static MemoryRegionSection *phys_sections;
static unsigned phys_sections_nb, phys_sections_nb_alloc;
static uint16_t phys_section_unassigned;
87 88 89
static uint16_t phys_section_notdirty;
static uint16_t phys_section_rom;
static uint16_t phys_section_watch;
90

91 92 93 94
/* Simple allocator for PhysPageEntry nodes */
static PhysPageEntry (*phys_map_nodes)[L2_SIZE];
static unsigned phys_map_nodes_nb, phys_map_nodes_nb_alloc;

95
#define PHYS_MAP_NODE_NIL (((uint16_t)~0) >> 1)
96

97
static void io_mem_init(void);
A
Avi Kivity 已提交
98
static void memory_map_init(void);
B
Blue Swirl 已提交
99
static void *qemu_safe_ram_ptr(ram_addr_t addr);
100

101
static MemoryRegion io_mem_watch;
102
#endif
B
bellard 已提交
103

104
#if !defined(CONFIG_USER_ONLY)
105

106
static void phys_map_node_reserve(unsigned nodes)
107
{
108
    if (phys_map_nodes_nb + nodes > phys_map_nodes_nb_alloc) {
109 110
        typedef PhysPageEntry Node[L2_SIZE];
        phys_map_nodes_nb_alloc = MAX(phys_map_nodes_nb_alloc * 2, 16);
111 112
        phys_map_nodes_nb_alloc = MAX(phys_map_nodes_nb_alloc,
                                      phys_map_nodes_nb + nodes);
113 114 115
        phys_map_nodes = g_renew(Node, phys_map_nodes,
                                 phys_map_nodes_nb_alloc);
    }
116 117 118 119 120 121 122 123 124 125
}

static uint16_t phys_map_node_alloc(void)
{
    unsigned i;
    uint16_t ret;

    ret = phys_map_nodes_nb++;
    assert(ret != PHYS_MAP_NODE_NIL);
    assert(ret != phys_map_nodes_nb_alloc);
126
    for (i = 0; i < L2_SIZE; ++i) {
127
        phys_map_nodes[ret][i].is_leaf = 0;
128
        phys_map_nodes[ret][i].ptr = PHYS_MAP_NODE_NIL;
129
    }
130
    return ret;
131 132 133 134 135 136 137
}

static void phys_map_nodes_reset(void)
{
    phys_map_nodes_nb = 0;
}

B
bellard 已提交
138

A
Avi Kivity 已提交
139 140
static void phys_page_set_level(PhysPageEntry *lp, hwaddr *index,
                                hwaddr *nb, uint16_t leaf,
141
                                int level)
142 143 144
{
    PhysPageEntry *p;
    int i;
A
Avi Kivity 已提交
145
    hwaddr step = (hwaddr)1 << (level * L2_BITS);
146

147
    if (!lp->is_leaf && lp->ptr == PHYS_MAP_NODE_NIL) {
148 149
        lp->ptr = phys_map_node_alloc();
        p = phys_map_nodes[lp->ptr];
150 151
        if (level == 0) {
            for (i = 0; i < L2_SIZE; i++) {
152
                p[i].is_leaf = 1;
153
                p[i].ptr = phys_section_unassigned;
154
            }
P
pbrook 已提交
155
        }
156
    } else {
157
        p = phys_map_nodes[lp->ptr];
B
bellard 已提交
158
    }
159
    lp = &p[(*index >> (level * L2_BITS)) & (L2_SIZE - 1)];
160

161
    while (*nb && lp < &p[L2_SIZE]) {
162 163
        if ((*index & (step - 1)) == 0 && *nb >= step) {
            lp->is_leaf = true;
164
            lp->ptr = leaf;
165 166
            *index += step;
            *nb -= step;
167 168 169 170
        } else {
            phys_page_set_level(lp, index, nb, leaf, level - 1);
        }
        ++lp;
171 172 173
    }
}

A
Avi Kivity 已提交
174
static void phys_page_set(AddressSpaceDispatch *d,
A
Avi Kivity 已提交
175
                          hwaddr index, hwaddr nb,
176
                          uint16_t leaf)
177
{
178
    /* Wildly overreserve - it doesn't matter much. */
179
    phys_map_node_reserve(3 * P_L2_LEVELS);
180

A
Avi Kivity 已提交
181
    phys_page_set_level(&d->phys_map, &index, &nb, leaf, P_L2_LEVELS - 1);
B
bellard 已提交
182 183
}

184
static MemoryRegionSection *phys_page_find(AddressSpaceDispatch *d, hwaddr index)
B
bellard 已提交
185
{
A
Avi Kivity 已提交
186
    PhysPageEntry lp = d->phys_map;
187 188
    PhysPageEntry *p;
    int i;
189

190
    for (i = P_L2_LEVELS - 1; i >= 0 && !lp.is_leaf; i--) {
191
        if (lp.ptr == PHYS_MAP_NODE_NIL) {
P
Paolo Bonzini 已提交
192
            return &phys_sections[phys_section_unassigned];
193
        }
194
        p = phys_map_nodes[lp.ptr];
195
        lp = p[(index >> (i * L2_BITS)) & (L2_SIZE - 1)];
196
    }
P
Paolo Bonzini 已提交
197
    return &phys_sections[lp.ptr];
198 199
}

B
Blue Swirl 已提交
200 201
bool memory_region_is_unassigned(MemoryRegion *mr)
{
P
Paolo Bonzini 已提交
202
    return mr != &io_mem_rom && mr != &io_mem_notdirty && !mr->rom_device
203
        && mr != &io_mem_watch;
B
bellard 已提交
204
}
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

MemoryRegionSection *address_space_translate(AddressSpace *as, hwaddr addr,
                                             hwaddr *xlat, hwaddr *plen,
                                             bool is_write)
{
    MemoryRegionSection *section;
    Int128 diff;

    section = phys_page_find(as->dispatch, addr >> TARGET_PAGE_BITS);
    /* Compute offset within MemoryRegionSection */
    addr -= section->offset_within_address_space;

    /* Compute offset within MemoryRegion */
    *xlat = addr + section->offset_within_region;

    diff = int128_sub(section->mr->size, int128_make64(addr));
    *plen = MIN(int128_get64(diff), *plen);
    return section;
}
224
#endif
B
bellard 已提交
225

226
void cpu_exec_init_all(void)
227
{
228
#if !defined(CONFIG_USER_ONLY)
229
    qemu_mutex_init(&ram_list.mutex);
230 231
    memory_map_init();
    io_mem_init();
232
#endif
233
}
234

235
#if !defined(CONFIG_USER_ONLY)
236 237

static int cpu_common_post_load(void *opaque, int version_id)
B
bellard 已提交
238
{
239
    CPUState *cpu = opaque;
B
bellard 已提交
240

241 242
    /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
       version_id is increased. */
243 244
    cpu->interrupt_request &= ~0x01;
    tlb_flush(cpu->env_ptr, 1);
245 246

    return 0;
B
bellard 已提交
247
}
B
bellard 已提交
248

249 250 251 252 253 254 255
static const VMStateDescription vmstate_cpu_common = {
    .name = "cpu_common",
    .version_id = 1,
    .minimum_version_id = 1,
    .minimum_version_id_old = 1,
    .post_load = cpu_common_post_load,
    .fields      = (VMStateField []) {
256 257
        VMSTATE_UINT32(halted, CPUState),
        VMSTATE_UINT32(interrupt_request, CPUState),
258 259 260
        VMSTATE_END_OF_LIST()
    }
};
261 262
#else
#define vmstate_cpu_common vmstate_dummy
263
#endif
B
bellard 已提交
264

265
CPUState *qemu_get_cpu(int index)
B
bellard 已提交
266
{
267
    CPUArchState *env = first_cpu;
268
    CPUState *cpu = NULL;
B
bellard 已提交
269

270
    while (env) {
271 272
        cpu = ENV_GET_CPU(env);
        if (cpu->cpu_index == index) {
273
            break;
274
        }
275
        env = env->next_cpu;
B
bellard 已提交
276
    }
277

278
    return env ? cpu : NULL;
B
bellard 已提交
279 280
}

281 282 283 284 285 286 287 288 289 290
void qemu_for_each_cpu(void (*func)(CPUState *cpu, void *data), void *data)
{
    CPUArchState *env = first_cpu;

    while (env) {
        func(ENV_GET_CPU(env), data);
        env = env->next_cpu;
    }
}

291
void cpu_exec_init(CPUArchState *env)
B
bellard 已提交
292
{
293
    CPUState *cpu = ENV_GET_CPU(env);
294
    CPUClass *cc = CPU_GET_CLASS(cpu);
295 296 297 298 299 300 301 302 303 304 305 306 307
    CPUArchState **penv;
    int cpu_index;

#if defined(CONFIG_USER_ONLY)
    cpu_list_lock();
#endif
    env->next_cpu = NULL;
    penv = &first_cpu;
    cpu_index = 0;
    while (*penv != NULL) {
        penv = &(*penv)->next_cpu;
        cpu_index++;
    }
308
    cpu->cpu_index = cpu_index;
309
    cpu->numa_node = 0;
310 311 312 313 314 315 316 317 318
    QTAILQ_INIT(&env->breakpoints);
    QTAILQ_INIT(&env->watchpoints);
#ifndef CONFIG_USER_ONLY
    cpu->thread_id = qemu_get_thread_id();
#endif
    *penv = env;
#if defined(CONFIG_USER_ONLY)
    cpu_list_unlock();
#endif
319
    vmstate_register(NULL, cpu_index, &vmstate_cpu_common, cpu);
320 321 322
#if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
    register_savevm(NULL, "cpu", cpu_index, CPU_SAVE_VERSION,
                    cpu_save, cpu_load, env);
323
    assert(cc->vmsd == NULL);
324
#endif
325 326 327
    if (cc->vmsd != NULL) {
        vmstate_register(NULL, cpu_index, cc->vmsd, cpu);
    }
B
bellard 已提交
328 329
}

B
bellard 已提交
330
#if defined(TARGET_HAS_ICE)
331
#if defined(CONFIG_USER_ONLY)
332
static void breakpoint_invalidate(CPUArchState *env, target_ulong pc)
333 334 335 336
{
    tb_invalidate_phys_page_range(pc, pc + 1, 0);
}
#else
337 338
static void breakpoint_invalidate(CPUArchState *env, target_ulong pc)
{
339 340
    tb_invalidate_phys_addr(cpu_get_phys_page_debug(env, pc) |
            (pc & ~TARGET_PAGE_MASK));
341
}
B
bellard 已提交
342
#endif
343
#endif /* TARGET_HAS_ICE */
B
bellard 已提交
344

345
#if defined(CONFIG_USER_ONLY)
346
void cpu_watchpoint_remove_all(CPUArchState *env, int mask)
347 348 349 350

{
}

351
int cpu_watchpoint_insert(CPUArchState *env, target_ulong addr, target_ulong len,
352 353 354 355 356
                          int flags, CPUWatchpoint **watchpoint)
{
    return -ENOSYS;
}
#else
357
/* Add a watchpoint.  */
358
int cpu_watchpoint_insert(CPUArchState *env, target_ulong addr, target_ulong len,
359
                          int flags, CPUWatchpoint **watchpoint)
360
{
361
    target_ulong len_mask = ~(len - 1);
362
    CPUWatchpoint *wp;
363

364
    /* sanity checks: allow power-of-2 lengths, deny unaligned watchpoints */
365 366
    if ((len & (len - 1)) || (addr & ~len_mask) ||
            len == 0 || len > TARGET_PAGE_SIZE) {
367 368 369 370
        fprintf(stderr, "qemu: tried to set invalid watchpoint at "
                TARGET_FMT_lx ", len=" TARGET_FMT_lu "\n", addr, len);
        return -EINVAL;
    }
371
    wp = g_malloc(sizeof(*wp));
372 373

    wp->vaddr = addr;
374
    wp->len_mask = len_mask;
375 376
    wp->flags = flags;

377
    /* keep all GDB-injected watchpoints in front */
378
    if (flags & BP_GDB)
B
Blue Swirl 已提交
379
        QTAILQ_INSERT_HEAD(&env->watchpoints, wp, entry);
380
    else
B
Blue Swirl 已提交
381
        QTAILQ_INSERT_TAIL(&env->watchpoints, wp, entry);
382 383

    tlb_flush_page(env, addr);
384 385 386 387

    if (watchpoint)
        *watchpoint = wp;
    return 0;
388 389
}

390
/* Remove a specific watchpoint.  */
391
int cpu_watchpoint_remove(CPUArchState *env, target_ulong addr, target_ulong len,
392
                          int flags)
393
{
394
    target_ulong len_mask = ~(len - 1);
395
    CPUWatchpoint *wp;
396

B
Blue Swirl 已提交
397
    QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
398
        if (addr == wp->vaddr && len_mask == wp->len_mask
399
                && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) {
400
            cpu_watchpoint_remove_by_ref(env, wp);
401 402 403
            return 0;
        }
    }
404
    return -ENOENT;
405 406
}

407
/* Remove a specific watchpoint by reference.  */
408
void cpu_watchpoint_remove_by_ref(CPUArchState *env, CPUWatchpoint *watchpoint)
409
{
B
Blue Swirl 已提交
410
    QTAILQ_REMOVE(&env->watchpoints, watchpoint, entry);
411

412 413
    tlb_flush_page(env, watchpoint->vaddr);

414
    g_free(watchpoint);
415 416 417
}

/* Remove all matching watchpoints.  */
418
void cpu_watchpoint_remove_all(CPUArchState *env, int mask)
419
{
420
    CPUWatchpoint *wp, *next;
421

B
Blue Swirl 已提交
422
    QTAILQ_FOREACH_SAFE(wp, &env->watchpoints, entry, next) {
423 424
        if (wp->flags & mask)
            cpu_watchpoint_remove_by_ref(env, wp);
425
    }
426
}
427
#endif
428

429
/* Add a breakpoint.  */
430
int cpu_breakpoint_insert(CPUArchState *env, target_ulong pc, int flags,
431
                          CPUBreakpoint **breakpoint)
B
bellard 已提交
432
{
B
bellard 已提交
433
#if defined(TARGET_HAS_ICE)
434
    CPUBreakpoint *bp;
435

436
    bp = g_malloc(sizeof(*bp));
B
bellard 已提交
437

438 439 440
    bp->pc = pc;
    bp->flags = flags;

441
    /* keep all GDB-injected breakpoints in front */
442
    if (flags & BP_GDB)
B
Blue Swirl 已提交
443
        QTAILQ_INSERT_HEAD(&env->breakpoints, bp, entry);
444
    else
B
Blue Swirl 已提交
445
        QTAILQ_INSERT_TAIL(&env->breakpoints, bp, entry);
446

B
bellard 已提交
447
    breakpoint_invalidate(env, pc);
448 449 450

    if (breakpoint)
        *breakpoint = bp;
B
bellard 已提交
451 452
    return 0;
#else
453
    return -ENOSYS;
B
bellard 已提交
454 455 456
#endif
}

457
/* Remove a specific breakpoint.  */
458
int cpu_breakpoint_remove(CPUArchState *env, target_ulong pc, int flags)
459
{
460
#if defined(TARGET_HAS_ICE)
461 462
    CPUBreakpoint *bp;

B
Blue Swirl 已提交
463
    QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
464 465 466 467
        if (bp->pc == pc && bp->flags == flags) {
            cpu_breakpoint_remove_by_ref(env, bp);
            return 0;
        }
468
    }
469 470 471
    return -ENOENT;
#else
    return -ENOSYS;
472 473 474
#endif
}

475
/* Remove a specific breakpoint by reference.  */
476
void cpu_breakpoint_remove_by_ref(CPUArchState *env, CPUBreakpoint *breakpoint)
B
bellard 已提交
477
{
B
bellard 已提交
478
#if defined(TARGET_HAS_ICE)
B
Blue Swirl 已提交
479
    QTAILQ_REMOVE(&env->breakpoints, breakpoint, entry);
B
bellard 已提交
480

481 482
    breakpoint_invalidate(env, breakpoint->pc);

483
    g_free(breakpoint);
484 485 486 487
#endif
}

/* Remove all matching breakpoints. */
488
void cpu_breakpoint_remove_all(CPUArchState *env, int mask)
489 490
{
#if defined(TARGET_HAS_ICE)
491
    CPUBreakpoint *bp, *next;
492

B
Blue Swirl 已提交
493
    QTAILQ_FOREACH_SAFE(bp, &env->breakpoints, entry, next) {
494 495
        if (bp->flags & mask)
            cpu_breakpoint_remove_by_ref(env, bp);
496
    }
B
bellard 已提交
497 498 499
#endif
}

B
bellard 已提交
500 501
/* enable or disable single step mode. EXCP_DEBUG is returned by the
   CPU loop after each instruction */
502
void cpu_single_step(CPUArchState *env, int enabled)
B
bellard 已提交
503
{
B
bellard 已提交
504
#if defined(TARGET_HAS_ICE)
B
bellard 已提交
505 506
    if (env->singlestep_enabled != enabled) {
        env->singlestep_enabled = enabled;
507 508 509
        if (kvm_enabled())
            kvm_update_guest_debug(env, 0);
        else {
S
Stuart Brady 已提交
510
            /* must flush all the translated code to avoid inconsistencies */
511 512 513
            /* XXX: only flush what is necessary */
            tb_flush(env);
        }
B
bellard 已提交
514 515 516 517
    }
#endif
}

518
void cpu_exit(CPUArchState *env)
519
{
520 521 522
    CPUState *cpu = ENV_GET_CPU(env);

    cpu->exit_request = 1;
523
    cpu->tcg_exit_req = 1;
524 525
}

526
void cpu_abort(CPUArchState *env, const char *fmt, ...)
B
bellard 已提交
527 528
{
    va_list ap;
P
pbrook 已提交
529
    va_list ap2;
B
bellard 已提交
530 531

    va_start(ap, fmt);
P
pbrook 已提交
532
    va_copy(ap2, ap);
B
bellard 已提交
533 534 535
    fprintf(stderr, "qemu: fatal: ");
    vfprintf(stderr, fmt, ap);
    fprintf(stderr, "\n");
536
    cpu_dump_state(env, stderr, fprintf, CPU_DUMP_FPU | CPU_DUMP_CCOP);
537 538 539 540
    if (qemu_log_enabled()) {
        qemu_log("qemu: fatal: ");
        qemu_log_vprintf(fmt, ap2);
        qemu_log("\n");
541
        log_cpu_state(env, CPU_DUMP_FPU | CPU_DUMP_CCOP);
542
        qemu_log_flush();
543
        qemu_log_close();
544
    }
P
pbrook 已提交
545
    va_end(ap2);
546
    va_end(ap);
547 548 549 550 551 552 553 554
#if defined(CONFIG_USER_ONLY)
    {
        struct sigaction act;
        sigfillset(&act.sa_mask);
        act.sa_handler = SIG_DFL;
        sigaction(SIGABRT, &act, NULL);
    }
#endif
B
bellard 已提交
555 556 557
    abort();
}

558
CPUArchState *cpu_copy(CPUArchState *env)
559
{
560 561
    CPUArchState *new_env = cpu_init(env->cpu_model_str);
    CPUArchState *next_cpu = new_env->next_cpu;
562 563 564 565 566
#if defined(TARGET_HAS_ICE)
    CPUBreakpoint *bp;
    CPUWatchpoint *wp;
#endif

567
    memcpy(new_env, env, sizeof(CPUArchState));
568

569
    /* Preserve chaining. */
570
    new_env->next_cpu = next_cpu;
571 572 573 574

    /* Clone all break/watchpoints.
       Note: Once we support ptrace with hw-debug register access, make sure
       BP_CPU break/watchpoints are handled correctly on clone. */
B
Blue Swirl 已提交
575 576
    QTAILQ_INIT(&env->breakpoints);
    QTAILQ_INIT(&env->watchpoints);
577
#if defined(TARGET_HAS_ICE)
B
Blue Swirl 已提交
578
    QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
579 580
        cpu_breakpoint_insert(new_env, bp->pc, bp->flags, NULL);
    }
B
Blue Swirl 已提交
581
    QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
582 583 584 585 586
        cpu_watchpoint_insert(new_env, wp->vaddr, (~wp->len_mask) + 1,
                              wp->flags, NULL);
    }
#endif

587 588 589
    return new_env;
}

590
#if !defined(CONFIG_USER_ONLY)
J
Juan Quintela 已提交
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
static void tlb_reset_dirty_range_all(ram_addr_t start, ram_addr_t end,
                                      uintptr_t length)
{
    uintptr_t start1;

    /* we modify the TLB cache so that the dirty bit will be set again
       when accessing the range */
    start1 = (uintptr_t)qemu_safe_ram_ptr(start);
    /* Check that we don't span multiple blocks - this breaks the
       address comparisons below.  */
    if ((uintptr_t)qemu_safe_ram_ptr(end - 1) - start1
            != (end - 1) - start) {
        abort();
    }
    cpu_tlb_reset_dirty_all(start1, length);

}

P
pbrook 已提交
609
/* Note: start and end must be within the same ram block.  */
A
Anthony Liguori 已提交
610
void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
B
bellard 已提交
611
                                     int dirty_flags)
612
{
J
Juan Quintela 已提交
613
    uintptr_t length;
614 615 616 617 618 619 620

    start &= TARGET_PAGE_MASK;
    end = TARGET_PAGE_ALIGN(end);

    length = end - start;
    if (length == 0)
        return;
621
    cpu_physical_memory_mask_dirty_range(start, length, dirty_flags);
B
bellard 已提交
622

J
Juan Quintela 已提交
623 624
    if (tcg_enabled()) {
        tlb_reset_dirty_range_all(start, end, length);
P
pbrook 已提交
625
    }
626 627
}

B
Blue Swirl 已提交
628
static int cpu_physical_memory_set_dirty_tracking(int enable)
A
aliguori 已提交
629
{
M
Michael S. Tsirkin 已提交
630
    int ret = 0;
A
aliguori 已提交
631
    in_migration = enable;
M
Michael S. Tsirkin 已提交
632
    return ret;
A
aliguori 已提交
633 634
}

A
Avi Kivity 已提交
635
hwaddr memory_region_section_get_iotlb(CPUArchState *env,
636 637 638 639 640
                                       MemoryRegionSection *section,
                                       target_ulong vaddr,
                                       hwaddr paddr, hwaddr xlat,
                                       int prot,
                                       target_ulong *address)
B
Blue Swirl 已提交
641
{
A
Avi Kivity 已提交
642
    hwaddr iotlb;
B
Blue Swirl 已提交
643 644
    CPUWatchpoint *wp;

645
    if (memory_region_is_ram(section->mr)) {
B
Blue Swirl 已提交
646 647
        /* Normal RAM.  */
        iotlb = (memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK)
648
            + xlat;
B
Blue Swirl 已提交
649 650 651 652 653 654 655
        if (!section->readonly) {
            iotlb |= phys_section_notdirty;
        } else {
            iotlb |= phys_section_rom;
        }
    } else {
        iotlb = section - phys_sections;
656
        iotlb += xlat;
B
Blue Swirl 已提交
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
    }

    /* Make accesses to pages with watchpoints go via the
       watchpoint trap routines.  */
    QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
        if (vaddr == (wp->vaddr & TARGET_PAGE_MASK)) {
            /* Avoid trapping reads of pages with a write breakpoint. */
            if ((prot & PAGE_WRITE) || (wp->flags & BP_MEM_READ)) {
                iotlb = phys_section_watch + paddr;
                *address |= TLB_MMIO;
                break;
            }
        }
    }

    return iotlb;
}
674 675
#endif /* defined(CONFIG_USER_ONLY) */

676
#if !defined(CONFIG_USER_ONLY)
677

P
Paul Brook 已提交
678 679
#define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
typedef struct subpage_t {
680
    MemoryRegion iomem;
A
Avi Kivity 已提交
681
    hwaddr base;
682
    uint16_t sub_section[TARGET_PAGE_SIZE];
P
Paul Brook 已提交
683 684
} subpage_t;

A
Anthony Liguori 已提交
685
static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
686
                             uint16_t section);
A
Avi Kivity 已提交
687
static subpage_t *subpage_init(hwaddr base);
688
static void destroy_page_desc(uint16_t section_index)
689
{
690 691
    MemoryRegionSection *section = &phys_sections[section_index];
    MemoryRegion *mr = section->mr;
692 693 694 695 696 697 698 699

    if (mr->subpage) {
        subpage_t *subpage = container_of(mr, subpage_t, iomem);
        memory_region_destroy(&subpage->iomem);
        g_free(subpage);
    }
}

700
static void destroy_l2_mapping(PhysPageEntry *lp, unsigned level)
701 702
{
    unsigned i;
703
    PhysPageEntry *p;
704

705
    if (lp->ptr == PHYS_MAP_NODE_NIL) {
706 707 708
        return;
    }

709
    p = phys_map_nodes[lp->ptr];
710
    for (i = 0; i < L2_SIZE; ++i) {
711
        if (!p[i].is_leaf) {
712
            destroy_l2_mapping(&p[i], level - 1);
713
        } else {
714
            destroy_page_desc(p[i].ptr);
715 716
        }
    }
717
    lp->is_leaf = 0;
718
    lp->ptr = PHYS_MAP_NODE_NIL;
719 720
}

A
Avi Kivity 已提交
721
static void destroy_all_mappings(AddressSpaceDispatch *d)
722
{
A
Avi Kivity 已提交
723
    destroy_l2_mapping(&d->phys_map, P_L2_LEVELS - 1);
724
    phys_map_nodes_reset();
725 726
}

727 728
static uint16_t phys_section_add(MemoryRegionSection *section)
{
729 730 731 732 733 734
    /* The physical section number is ORed with a page-aligned
     * pointer to produce the iotlb entries.  Thus it should
     * never overflow into the page-aligned value.
     */
    assert(phys_sections_nb < TARGET_PAGE_SIZE);

735 736 737 738 739 740 741 742 743 744 745 746 747 748
    if (phys_sections_nb == phys_sections_nb_alloc) {
        phys_sections_nb_alloc = MAX(phys_sections_nb_alloc * 2, 16);
        phys_sections = g_renew(MemoryRegionSection, phys_sections,
                                phys_sections_nb_alloc);
    }
    phys_sections[phys_sections_nb] = *section;
    return phys_sections_nb++;
}

static void phys_sections_clear(void)
{
    phys_sections_nb = 0;
}

A
Avi Kivity 已提交
749
static void register_subpage(AddressSpaceDispatch *d, MemoryRegionSection *section)
750 751
{
    subpage_t *subpage;
A
Avi Kivity 已提交
752
    hwaddr base = section->offset_within_address_space
753
        & TARGET_PAGE_MASK;
A
Avi Kivity 已提交
754
    MemoryRegionSection *existing = phys_page_find(d, base >> TARGET_PAGE_BITS);
755 756 757 758
    MemoryRegionSection subsection = {
        .offset_within_address_space = base,
        .size = TARGET_PAGE_SIZE,
    };
A
Avi Kivity 已提交
759
    hwaddr start, end;
760

761
    assert(existing->mr->subpage || existing->mr == &io_mem_unassigned);
762

763
    if (!(existing->mr->subpage)) {
764 765
        subpage = subpage_init(base);
        subsection.mr = &subpage->iomem;
A
Avi Kivity 已提交
766
        phys_page_set(d, base >> TARGET_PAGE_BITS, 1,
767
                      phys_section_add(&subsection));
768
    } else {
769
        subpage = container_of(existing->mr, subpage_t, iomem);
770 771
    }
    start = section->offset_within_address_space & ~TARGET_PAGE_MASK;
772
    end = start + section->size - 1;
773 774 775 776
    subpage_register(subpage, start, end, phys_section_add(section));
}


A
Avi Kivity 已提交
777
static void register_multipage(AddressSpaceDispatch *d, MemoryRegionSection *section)
778
{
A
Avi Kivity 已提交
779
    hwaddr start_addr = section->offset_within_address_space;
780
    ram_addr_t size = section->size;
A
Avi Kivity 已提交
781
    hwaddr addr;
782
    uint16_t section_index = phys_section_add(section);
783

784
    assert(size);
M
Michael S. Tsirkin 已提交
785

786
    addr = start_addr;
A
Avi Kivity 已提交
787
    phys_page_set(d, addr >> TARGET_PAGE_BITS, size >> TARGET_PAGE_BITS,
788
                  section_index);
789 790
}

791 792 793 794 795 796 797 798 799 800 801
QEMU_BUILD_BUG_ON(TARGET_PHYS_ADDR_SPACE_BITS > MAX_PHYS_ADDR_SPACE_BITS)

static MemoryRegionSection limit(MemoryRegionSection section)
{
    section.size = MIN(section.offset_within_address_space + section.size,
                       MAX_PHYS_ADDR + 1)
                   - section.offset_within_address_space;

    return section;
}

A
Avi Kivity 已提交
802
static void mem_add(MemoryListener *listener, MemoryRegionSection *section)
803
{
A
Avi Kivity 已提交
804
    AddressSpaceDispatch *d = container_of(listener, AddressSpaceDispatch, listener);
805
    MemoryRegionSection now = limit(*section), remain = limit(*section);
806 807 808 809 810 811

    if ((now.offset_within_address_space & ~TARGET_PAGE_MASK)
        || (now.size < TARGET_PAGE_SIZE)) {
        now.size = MIN(TARGET_PAGE_ALIGN(now.offset_within_address_space)
                       - now.offset_within_address_space,
                       now.size);
A
Avi Kivity 已提交
812
        register_subpage(d, &now);
813 814 815 816
        remain.size -= now.size;
        remain.offset_within_address_space += now.size;
        remain.offset_within_region += now.size;
    }
817 818 819 820
    while (remain.size >= TARGET_PAGE_SIZE) {
        now = remain;
        if (remain.offset_within_region & ~TARGET_PAGE_MASK) {
            now.size = TARGET_PAGE_SIZE;
A
Avi Kivity 已提交
821
            register_subpage(d, &now);
822 823
        } else {
            now.size &= TARGET_PAGE_MASK;
A
Avi Kivity 已提交
824
            register_multipage(d, &now);
825
        }
826 827 828 829 830 831
        remain.size -= now.size;
        remain.offset_within_address_space += now.size;
        remain.offset_within_region += now.size;
    }
    now = remain;
    if (now.size) {
A
Avi Kivity 已提交
832
        register_subpage(d, &now);
833 834 835
    }
}

836 837 838 839 840 841
void qemu_flush_coalesced_mmio_buffer(void)
{
    if (kvm_enabled())
        kvm_flush_coalesced_mmio_buffer();
}

842 843 844 845 846 847 848 849 850 851
void qemu_mutex_lock_ramlist(void)
{
    qemu_mutex_lock(&ram_list.mutex);
}

void qemu_mutex_unlock_ramlist(void)
{
    qemu_mutex_unlock(&ram_list.mutex);
}

852 853 854 855 856 857 858 859 860 861 862 863
#if defined(__linux__) && !defined(TARGET_S390X)

#include <sys/vfs.h>

#define HUGETLBFS_MAGIC       0x958458f6

static long gethugepagesize(const char *path)
{
    struct statfs fs;
    int ret;

    do {
Y
Yoshiaki Tamura 已提交
864
        ret = statfs(path, &fs);
865 866 867
    } while (ret != 0 && errno == EINTR);

    if (ret != 0) {
Y
Yoshiaki Tamura 已提交
868 869
        perror(path);
        return 0;
870 871 872
    }

    if (fs.f_type != HUGETLBFS_MAGIC)
Y
Yoshiaki Tamura 已提交
873
        fprintf(stderr, "Warning: path not on HugeTLBFS: %s\n", path);
874 875 876 877

    return fs.f_bsize;
}

A
Alex Williamson 已提交
878 879 880
static void *file_ram_alloc(RAMBlock *block,
                            ram_addr_t memory,
                            const char *path)
881 882
{
    char *filename;
883 884
    char *sanitized_name;
    char *c;
885 886 887 888 889 890 891 892 893
    void *area;
    int fd;
#ifdef MAP_POPULATE
    int flags;
#endif
    unsigned long hpagesize;

    hpagesize = gethugepagesize(path);
    if (!hpagesize) {
Y
Yoshiaki Tamura 已提交
894
        return NULL;
895 896 897 898 899 900 901 902 903 904 905
    }

    if (memory < hpagesize) {
        return NULL;
    }

    if (kvm_enabled() && !kvm_has_sync_mmu()) {
        fprintf(stderr, "host lacks kvm mmu notifiers, -mem-path unsupported\n");
        return NULL;
    }

906 907 908 909 910 911 912 913 914 915
    /* Make name safe to use with mkstemp by replacing '/' with '_'. */
    sanitized_name = g_strdup(block->mr->name);
    for (c = sanitized_name; *c != '\0'; c++) {
        if (*c == '/')
            *c = '_';
    }

    filename = g_strdup_printf("%s/qemu_back_mem.%s.XXXXXX", path,
                               sanitized_name);
    g_free(sanitized_name);
916 917 918

    fd = mkstemp(filename);
    if (fd < 0) {
Y
Yoshiaki Tamura 已提交
919
        perror("unable to create backing store for hugepages");
920
        g_free(filename);
Y
Yoshiaki Tamura 已提交
921
        return NULL;
922 923
    }
    unlink(filename);
924
    g_free(filename);
925 926 927 928 929 930 931 932 933 934

    memory = (memory+hpagesize-1) & ~(hpagesize-1);

    /*
     * ftruncate is not supported by hugetlbfs in older
     * hosts, so don't bother bailing out on errors.
     * If anything goes wrong with it under other filesystems,
     * mmap will fail.
     */
    if (ftruncate(fd, memory))
Y
Yoshiaki Tamura 已提交
935
        perror("ftruncate");
936 937 938 939 940 941 942 943 944 945 946 947

#ifdef MAP_POPULATE
    /* NB: MAP_POPULATE won't exhaustively alloc all phys pages in the case
     * MAP_PRIVATE is requested.  For mem_prealloc we mmap as MAP_SHARED
     * to sidestep this quirk.
     */
    flags = mem_prealloc ? MAP_POPULATE | MAP_SHARED : MAP_PRIVATE;
    area = mmap(0, memory, PROT_READ | PROT_WRITE, flags, fd, 0);
#else
    area = mmap(0, memory, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
#endif
    if (area == MAP_FAILED) {
Y
Yoshiaki Tamura 已提交
948 949 950
        perror("file_ram_alloc: can't mmap RAM pages");
        close(fd);
        return (NULL);
951
    }
A
Alex Williamson 已提交
952
    block->fd = fd;
953 954 955 956
    return area;
}
#endif

957
static ram_addr_t find_ram_offset(ram_addr_t size)
A
Alex Williamson 已提交
958 959
{
    RAMBlock *block, *next_block;
A
Alex Williamson 已提交
960
    ram_addr_t offset = RAM_ADDR_MAX, mingap = RAM_ADDR_MAX;
A
Alex Williamson 已提交
961

962 963
    assert(size != 0); /* it would hand out same offset multiple times */

P
Paolo Bonzini 已提交
964
    if (QTAILQ_EMPTY(&ram_list.blocks))
A
Alex Williamson 已提交
965 966
        return 0;

P
Paolo Bonzini 已提交
967
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
968
        ram_addr_t end, next = RAM_ADDR_MAX;
A
Alex Williamson 已提交
969 970 971

        end = block->offset + block->length;

P
Paolo Bonzini 已提交
972
        QTAILQ_FOREACH(next_block, &ram_list.blocks, next) {
A
Alex Williamson 已提交
973 974 975 976 977
            if (next_block->offset >= end) {
                next = MIN(next, next_block->offset);
            }
        }
        if (next - end >= size && next - end < mingap) {
A
Alex Williamson 已提交
978
            offset = end;
A
Alex Williamson 已提交
979 980 981
            mingap = next - end;
        }
    }
A
Alex Williamson 已提交
982 983 984 985 986 987 988

    if (offset == RAM_ADDR_MAX) {
        fprintf(stderr, "Failed to find gap of requested size: %" PRIu64 "\n",
                (uint64_t)size);
        abort();
    }

A
Alex Williamson 已提交
989 990 991
    return offset;
}

J
Juan Quintela 已提交
992
ram_addr_t last_ram_offset(void)
993 994 995 996
{
    RAMBlock *block;
    ram_addr_t last = 0;

P
Paolo Bonzini 已提交
997
    QTAILQ_FOREACH(block, &ram_list.blocks, next)
998 999 1000 1001 1002
        last = MAX(last, block->offset + block->length);

    return last;
}

1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
static void qemu_ram_setup_dump(void *addr, ram_addr_t size)
{
    int ret;
    QemuOpts *machine_opts;

    /* Use MADV_DONTDUMP, if user doesn't want the guest memory in the core */
    machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
    if (machine_opts &&
        !qemu_opt_get_bool(machine_opts, "dump-guest-core", true)) {
        ret = qemu_madvise(addr, size, QEMU_MADV_DONTDUMP);
        if (ret) {
            perror("qemu_madvise");
            fprintf(stderr, "madvise doesn't support MADV_DONTDUMP, "
                            "but dump_guest_core=off specified\n");
        }
    }
}

1021
void qemu_ram_set_idstr(ram_addr_t addr, const char *name, DeviceState *dev)
1022 1023 1024
{
    RAMBlock *new_block, *block;

1025
    new_block = NULL;
P
Paolo Bonzini 已提交
1026
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
1027 1028 1029 1030 1031 1032 1033
        if (block->offset == addr) {
            new_block = block;
            break;
        }
    }
    assert(new_block);
    assert(!new_block->idstr[0]);
1034

1035 1036
    if (dev) {
        char *id = qdev_get_dev_path(dev);
1037 1038
        if (id) {
            snprintf(new_block->idstr, sizeof(new_block->idstr), "%s/", id);
1039
            g_free(id);
1040 1041 1042 1043
        }
    }
    pstrcat(new_block->idstr, sizeof(new_block->idstr), name);

1044 1045
    /* This assumes the iothread lock is taken here too.  */
    qemu_mutex_lock_ramlist();
P
Paolo Bonzini 已提交
1046
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
1047
        if (block != new_block && !strcmp(block->idstr, new_block->idstr)) {
1048 1049 1050 1051 1052
            fprintf(stderr, "RAMBlock \"%s\" already registered, abort!\n",
                    new_block->idstr);
            abort();
        }
    }
1053
    qemu_mutex_unlock_ramlist();
1054 1055
}

1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
static int memory_try_enable_merging(void *addr, size_t len)
{
    QemuOpts *opts;

    opts = qemu_opts_find(qemu_find_opts("machine"), 0);
    if (opts && !qemu_opt_get_bool(opts, "mem-merge", true)) {
        /* disabled by the user */
        return 0;
    }

    return qemu_madvise(addr, len, QEMU_MADV_MERGEABLE);
}

1069 1070 1071
ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
                                   MemoryRegion *mr)
{
1072
    RAMBlock *block, *new_block;
1073 1074 1075

    size = TARGET_PAGE_ALIGN(size);
    new_block = g_malloc0(sizeof(*new_block));
1076

1077 1078
    /* This assumes the iothread lock is taken here too.  */
    qemu_mutex_lock_ramlist();
A
Avi Kivity 已提交
1079
    new_block->mr = mr;
J
Jun Nakajima 已提交
1080
    new_block->offset = find_ram_offset(size);
1081 1082
    if (host) {
        new_block->host = host;
H
Huang Ying 已提交
1083
        new_block->flags |= RAM_PREALLOC_MASK;
1084 1085
    } else {
        if (mem_path) {
1086
#if defined (__linux__) && !defined(TARGET_S390X)
1087 1088
            new_block->host = file_ram_alloc(new_block, size, mem_path);
            if (!new_block->host) {
1089
                new_block->host = qemu_anon_ram_alloc(size);
1090
                memory_try_enable_merging(new_block->host, size);
1091
            }
1092
#else
1093 1094
            fprintf(stderr, "-mem-path option unsupported\n");
            exit(1);
1095
#endif
1096
        } else {
1097
            if (xen_enabled()) {
1098
                xen_ram_alloc(new_block->offset, size, mr);
1099 1100
            } else if (kvm_enabled()) {
                /* some s390/kvm configurations have special constraints */
1101
                new_block->host = kvm_ram_alloc(size);
J
Jun Nakajima 已提交
1102
            } else {
1103
                new_block->host = qemu_anon_ram_alloc(size);
J
Jun Nakajima 已提交
1104
            }
1105
            memory_try_enable_merging(new_block->host, size);
1106
        }
1107
    }
P
pbrook 已提交
1108 1109
    new_block->length = size;

1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
    /* Keep the list sorted from biggest to smallest block.  */
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
        if (block->length < new_block->length) {
            break;
        }
    }
    if (block) {
        QTAILQ_INSERT_BEFORE(block, new_block, next);
    } else {
        QTAILQ_INSERT_TAIL(&ram_list.blocks, new_block, next);
    }
1121
    ram_list.mru_block = NULL;
P
pbrook 已提交
1122

U
Umesh Deshpande 已提交
1123
    ram_list.version++;
1124
    qemu_mutex_unlock_ramlist();
U
Umesh Deshpande 已提交
1125

1126
    ram_list.phys_dirty = g_realloc(ram_list.phys_dirty,
A
Alex Williamson 已提交
1127
                                       last_ram_offset() >> TARGET_PAGE_BITS);
1128 1129
    memset(ram_list.phys_dirty + (new_block->offset >> TARGET_PAGE_BITS),
           0, size >> TARGET_PAGE_BITS);
J
Juan Quintela 已提交
1130
    cpu_physical_memory_set_dirty_range(new_block->offset, size, 0xff);
P
pbrook 已提交
1131

1132
    qemu_ram_setup_dump(new_block->host, size);
1133
    qemu_madvise(new_block->host, size, QEMU_MADV_HUGEPAGE);
1134

1135 1136 1137
    if (kvm_enabled())
        kvm_setup_guest_memory(new_block->host, size);

P
pbrook 已提交
1138 1139
    return new_block->offset;
}
B
bellard 已提交
1140

1141
ram_addr_t qemu_ram_alloc(ram_addr_t size, MemoryRegion *mr)
1142
{
1143
    return qemu_ram_alloc_from_ptr(size, NULL, mr);
1144 1145
}

1146 1147 1148 1149
void qemu_ram_free_from_ptr(ram_addr_t addr)
{
    RAMBlock *block;

1150 1151
    /* This assumes the iothread lock is taken here too.  */
    qemu_mutex_lock_ramlist();
P
Paolo Bonzini 已提交
1152
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
1153
        if (addr == block->offset) {
P
Paolo Bonzini 已提交
1154
            QTAILQ_REMOVE(&ram_list.blocks, block, next);
1155
            ram_list.mru_block = NULL;
U
Umesh Deshpande 已提交
1156
            ram_list.version++;
1157
            g_free(block);
1158
            break;
1159 1160
        }
    }
1161
    qemu_mutex_unlock_ramlist();
1162 1163
}

A
Anthony Liguori 已提交
1164
void qemu_ram_free(ram_addr_t addr)
B
bellard 已提交
1165
{
A
Alex Williamson 已提交
1166 1167
    RAMBlock *block;

1168 1169
    /* This assumes the iothread lock is taken here too.  */
    qemu_mutex_lock_ramlist();
P
Paolo Bonzini 已提交
1170
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
A
Alex Williamson 已提交
1171
        if (addr == block->offset) {
P
Paolo Bonzini 已提交
1172
            QTAILQ_REMOVE(&ram_list.blocks, block, next);
1173
            ram_list.mru_block = NULL;
U
Umesh Deshpande 已提交
1174
            ram_list.version++;
H
Huang Ying 已提交
1175 1176 1177
            if (block->flags & RAM_PREALLOC_MASK) {
                ;
            } else if (mem_path) {
A
Alex Williamson 已提交
1178 1179 1180 1181 1182
#if defined (__linux__) && !defined(TARGET_S390X)
                if (block->fd) {
                    munmap(block->host, block->length);
                    close(block->fd);
                } else {
1183
                    qemu_anon_ram_free(block->host, block->length);
A
Alex Williamson 已提交
1184
                }
1185 1186
#else
                abort();
A
Alex Williamson 已提交
1187 1188
#endif
            } else {
1189
                if (xen_enabled()) {
J
Jan Kiszka 已提交
1190
                    xen_invalidate_map_cache_entry(block->host);
J
Jun Nakajima 已提交
1191
                } else {
1192
                    qemu_anon_ram_free(block->host, block->length);
J
Jun Nakajima 已提交
1193
                }
A
Alex Williamson 已提交
1194
            }
1195
            g_free(block);
1196
            break;
A
Alex Williamson 已提交
1197 1198
        }
    }
1199
    qemu_mutex_unlock_ramlist();
A
Alex Williamson 已提交
1200

B
bellard 已提交
1201 1202
}

H
Huang Ying 已提交
1203 1204 1205 1206 1207 1208 1209 1210
#ifndef _WIN32
void qemu_ram_remap(ram_addr_t addr, ram_addr_t length)
{
    RAMBlock *block;
    ram_addr_t offset;
    int flags;
    void *area, *vaddr;

P
Paolo Bonzini 已提交
1211
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
H
Huang Ying 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
        offset = addr - block->offset;
        if (offset < block->length) {
            vaddr = block->host + offset;
            if (block->flags & RAM_PREALLOC_MASK) {
                ;
            } else {
                flags = MAP_FIXED;
                munmap(vaddr, length);
                if (mem_path) {
#if defined(__linux__) && !defined(TARGET_S390X)
                    if (block->fd) {
#ifdef MAP_POPULATE
                        flags |= mem_prealloc ? MAP_POPULATE | MAP_SHARED :
                            MAP_PRIVATE;
#else
                        flags |= MAP_PRIVATE;
#endif
                        area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
                                    flags, block->fd, offset);
                    } else {
                        flags |= MAP_PRIVATE | MAP_ANONYMOUS;
                        area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
                                    flags, -1, 0);
                    }
1236 1237
#else
                    abort();
H
Huang Ying 已提交
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
#endif
                } else {
#if defined(TARGET_S390X) && defined(CONFIG_KVM)
                    flags |= MAP_SHARED | MAP_ANONYMOUS;
                    area = mmap(vaddr, length, PROT_EXEC|PROT_READ|PROT_WRITE,
                                flags, -1, 0);
#else
                    flags |= MAP_PRIVATE | MAP_ANONYMOUS;
                    area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
                                flags, -1, 0);
#endif
                }
                if (area != vaddr) {
1251 1252
                    fprintf(stderr, "Could not remap addr: "
                            RAM_ADDR_FMT "@" RAM_ADDR_FMT "\n",
H
Huang Ying 已提交
1253 1254 1255
                            length, addr);
                    exit(1);
                }
1256
                memory_try_enable_merging(vaddr, length);
1257
                qemu_ram_setup_dump(vaddr, length);
H
Huang Ying 已提交
1258 1259 1260 1261 1262 1263 1264
            }
            return;
        }
    }
}
#endif /* !_WIN32 */

1265
/* Return a host pointer to ram allocated with qemu_ram_alloc.
P
pbrook 已提交
1266 1267 1268 1269 1270 1271 1272
   With the exception of the softmmu code in this file, this should
   only be used for local memory (e.g. video ram) that the device owns,
   and knows it isn't going to access beyond the end of the block.

   It should not be used for general purpose DMA.
   Use cpu_physical_memory_map/cpu_physical_memory_rw instead.
 */
A
Anthony Liguori 已提交
1273
void *qemu_get_ram_ptr(ram_addr_t addr)
1274
{
P
pbrook 已提交
1275 1276
    RAMBlock *block;

1277
    /* The list is protected by the iothread lock here.  */
1278 1279 1280 1281
    block = ram_list.mru_block;
    if (block && addr - block->offset < block->length) {
        goto found;
    }
P
Paolo Bonzini 已提交
1282
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
A
Alex Williamson 已提交
1283
        if (addr - block->offset < block->length) {
1284
            goto found;
A
Alex Williamson 已提交
1285
        }
P
pbrook 已提交
1286
    }
A
Alex Williamson 已提交
1287 1288 1289 1290

    fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
    abort();

1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
found:
    ram_list.mru_block = block;
    if (xen_enabled()) {
        /* We need to check if the requested address is in the RAM
         * because we don't want to map the entire memory in QEMU.
         * In that case just map until the end of the page.
         */
        if (block->offset == 0) {
            return xen_map_cache(addr, 0, 0);
        } else if (block->host == NULL) {
            block->host =
                xen_map_cache(block->offset, block->length, 1);
        }
    }
    return block->host + (addr - block->offset);
1306 1307
}

1308 1309 1310 1311
/* Return a host pointer to ram allocated with qemu_ram_alloc.  Same as
 * qemu_get_ram_ptr but do not touch ram_list.mru_block.
 *
 * ??? Is this still necessary?
1312
 */
B
Blue Swirl 已提交
1313
static void *qemu_safe_ram_ptr(ram_addr_t addr)
1314 1315 1316
{
    RAMBlock *block;

1317
    /* The list is protected by the iothread lock here.  */
P
Paolo Bonzini 已提交
1318
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
1319
        if (addr - block->offset < block->length) {
1320
            if (xen_enabled()) {
J
Jun Nakajima 已提交
1321 1322
                /* We need to check if the requested address is in the RAM
                 * because we don't want to map the entire memory in QEMU.
1323
                 * In that case just map until the end of the page.
J
Jun Nakajima 已提交
1324 1325
                 */
                if (block->offset == 0) {
J
Jan Kiszka 已提交
1326
                    return xen_map_cache(addr, 0, 0);
J
Jun Nakajima 已提交
1327
                } else if (block->host == NULL) {
J
Jan Kiszka 已提交
1328 1329
                    block->host =
                        xen_map_cache(block->offset, block->length, 1);
J
Jun Nakajima 已提交
1330 1331
                }
            }
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
            return block->host + (addr - block->offset);
        }
    }

    fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
    abort();

    return NULL;
}

1342 1343
/* Return a host pointer to guest's ram. Similar to qemu_get_ram_ptr
 * but takes a size argument */
B
Blue Swirl 已提交
1344
static void *qemu_ram_ptr_length(ram_addr_t addr, ram_addr_t *size)
1345
{
1346 1347 1348
    if (*size == 0) {
        return NULL;
    }
1349
    if (xen_enabled()) {
J
Jan Kiszka 已提交
1350
        return xen_map_cache(addr, *size, 1);
1351
    } else {
1352 1353
        RAMBlock *block;

P
Paolo Bonzini 已提交
1354
        QTAILQ_FOREACH(block, &ram_list.blocks, next) {
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
            if (addr - block->offset < block->length) {
                if (addr - block->offset + *size > block->length)
                    *size = block->length - addr + block->offset;
                return block->host + (addr - block->offset);
            }
        }

        fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
        abort();
    }
}

M
Marcelo Tosatti 已提交
1367
int qemu_ram_addr_from_host(void *ptr, ram_addr_t *ram_addr)
P
pbrook 已提交
1368
{
P
pbrook 已提交
1369 1370 1371
    RAMBlock *block;
    uint8_t *host = ptr;

1372
    if (xen_enabled()) {
J
Jan Kiszka 已提交
1373
        *ram_addr = xen_ram_addr_from_mapcache(ptr);
1374 1375 1376
        return 0;
    }

P
Paolo Bonzini 已提交
1377
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
J
Jun Nakajima 已提交
1378 1379 1380 1381
        /* This case append when the block is not mapped. */
        if (block->host == NULL) {
            continue;
        }
A
Alex Williamson 已提交
1382
        if (host - block->host < block->length) {
M
Marcelo Tosatti 已提交
1383 1384
            *ram_addr = block->offset + (host - block->host);
            return 0;
A
Alex Williamson 已提交
1385
        }
P
pbrook 已提交
1386
    }
J
Jun Nakajima 已提交
1387

M
Marcelo Tosatti 已提交
1388 1389
    return -1;
}
A
Alex Williamson 已提交
1390

M
Marcelo Tosatti 已提交
1391 1392 1393 1394 1395
/* Some of the softmmu routines need to translate from a host pointer
   (typically a TLB entry) back to a ram offset.  */
ram_addr_t qemu_ram_addr_from_host_nofail(void *ptr)
{
    ram_addr_t ram_addr;
A
Alex Williamson 已提交
1396

M
Marcelo Tosatti 已提交
1397 1398 1399 1400 1401
    if (qemu_ram_addr_from_host(ptr, &ram_addr)) {
        fprintf(stderr, "Bad ram pointer %p\n", ptr);
        abort();
    }
    return ram_addr;
P
pbrook 已提交
1402 1403
}

A
Avi Kivity 已提交
1404
static void notdirty_mem_write(void *opaque, hwaddr ram_addr,
1405
                               uint64_t val, unsigned size)
1406
{
1407
    int dirty_flags;
1408
    dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
1409
    if (!(dirty_flags & CODE_DIRTY_FLAG)) {
1410
        tb_invalidate_phys_page_fast(ram_addr, size);
1411
        dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
1412
    }
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
    switch (size) {
    case 1:
        stb_p(qemu_get_ram_ptr(ram_addr), val);
        break;
    case 2:
        stw_p(qemu_get_ram_ptr(ram_addr), val);
        break;
    case 4:
        stl_p(qemu_get_ram_ptr(ram_addr), val);
        break;
    default:
        abort();
1425
    }
B
bellard 已提交
1426
    dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
1427
    cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags);
B
bellard 已提交
1428 1429 1430
    /* we remove the notdirty callback only if the code has been
       flushed */
    if (dirty_flags == 0xff)
P
pbrook 已提交
1431
        tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
1432 1433
}

1434 1435 1436 1437 1438 1439
static bool notdirty_mem_accepts(void *opaque, hwaddr addr,
                                 unsigned size, bool is_write)
{
    return is_write;
}

1440 1441
static const MemoryRegionOps notdirty_mem_ops = {
    .write = notdirty_mem_write,
1442
    .valid.accepts = notdirty_mem_accepts,
1443
    .endianness = DEVICE_NATIVE_ENDIAN,
1444 1445
};

P
pbrook 已提交
1446
/* Generate a debug exception if a watchpoint has been hit.  */
1447
static void check_watchpoint(int offset, int len_mask, int flags)
P
pbrook 已提交
1448
{
1449
    CPUArchState *env = cpu_single_env;
1450
    target_ulong pc, cs_base;
P
pbrook 已提交
1451
    target_ulong vaddr;
1452
    CPUWatchpoint *wp;
1453
    int cpu_flags;
P
pbrook 已提交
1454

1455 1456 1457 1458
    if (env->watchpoint_hit) {
        /* We re-entered the check after replacing the TB. Now raise
         * the debug interrupt so that is will trigger after the
         * current instruction. */
1459
        cpu_interrupt(ENV_GET_CPU(env), CPU_INTERRUPT_DEBUG);
1460 1461
        return;
    }
P
pbrook 已提交
1462
    vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset;
B
Blue Swirl 已提交
1463
    QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
1464 1465
        if ((vaddr == (wp->vaddr & len_mask) ||
             (vaddr & wp->len_mask) == wp->vaddr) && (wp->flags & flags)) {
1466 1467 1468
            wp->flags |= BP_WATCHPOINT_HIT;
            if (!env->watchpoint_hit) {
                env->watchpoint_hit = wp;
B
Blue Swirl 已提交
1469
                tb_check_watchpoint(env);
1470 1471
                if (wp->flags & BP_STOP_BEFORE_ACCESS) {
                    env->exception_index = EXCP_DEBUG;
1472
                    cpu_loop_exit(env);
1473 1474 1475
                } else {
                    cpu_get_tb_cpu_state(env, &pc, &cs_base, &cpu_flags);
                    tb_gen_code(env, pc, cs_base, cpu_flags, 1);
1476
                    cpu_resume_from_signal(env, NULL);
1477
                }
1478
            }
1479 1480
        } else {
            wp->flags &= ~BP_WATCHPOINT_HIT;
P
pbrook 已提交
1481 1482 1483 1484
        }
    }
}

1485 1486 1487
/* Watchpoint access routines.  Watchpoints are inserted using TLB tricks,
   so these check for a hit then pass through to the normal out-of-line
   phys routines.  */
A
Avi Kivity 已提交
1488
static uint64_t watch_mem_read(void *opaque, hwaddr addr,
1489
                               unsigned size)
1490
{
1491 1492 1493 1494 1495 1496 1497
    check_watchpoint(addr & ~TARGET_PAGE_MASK, ~(size - 1), BP_MEM_READ);
    switch (size) {
    case 1: return ldub_phys(addr);
    case 2: return lduw_phys(addr);
    case 4: return ldl_phys(addr);
    default: abort();
    }
1498 1499
}

A
Avi Kivity 已提交
1500
static void watch_mem_write(void *opaque, hwaddr addr,
1501
                            uint64_t val, unsigned size)
1502
{
1503 1504
    check_watchpoint(addr & ~TARGET_PAGE_MASK, ~(size - 1), BP_MEM_WRITE);
    switch (size) {
1505 1506 1507 1508 1509 1510 1511 1512 1513
    case 1:
        stb_phys(addr, val);
        break;
    case 2:
        stw_phys(addr, val);
        break;
    case 4:
        stl_phys(addr, val);
        break;
1514 1515
    default: abort();
    }
1516 1517
}

1518 1519 1520 1521
static const MemoryRegionOps watch_mem_ops = {
    .read = watch_mem_read,
    .write = watch_mem_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
1522 1523
};

A
Avi Kivity 已提交
1524
static uint64_t subpage_read(void *opaque, hwaddr addr,
1525
                             unsigned len)
1526
{
1527
    subpage_t *mmio = opaque;
R
Richard Henderson 已提交
1528
    unsigned int idx = SUBPAGE_IDX(addr);
1529
    MemoryRegionSection *section;
1530 1531 1532 1533 1534
#if defined(DEBUG_SUBPAGE)
    printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__,
           mmio, len, addr, idx);
#endif

1535 1536 1537 1538
    section = &phys_sections[mmio->sub_section[idx]];
    addr += mmio->base;
    addr -= section->offset_within_address_space;
    addr += section->offset_within_region;
1539
    return io_mem_read(section->mr, addr, len);
1540 1541
}

A
Avi Kivity 已提交
1542
static void subpage_write(void *opaque, hwaddr addr,
1543
                          uint64_t value, unsigned len)
1544
{
1545
    subpage_t *mmio = opaque;
R
Richard Henderson 已提交
1546
    unsigned int idx = SUBPAGE_IDX(addr);
1547
    MemoryRegionSection *section;
1548
#if defined(DEBUG_SUBPAGE)
1549 1550
    printf("%s: subpage %p len %d addr " TARGET_FMT_plx
           " idx %d value %"PRIx64"\n",
R
Richard Henderson 已提交
1551
           __func__, mmio, len, addr, idx, value);
1552
#endif
R
Richard Henderson 已提交
1553

1554 1555 1556 1557
    section = &phys_sections[mmio->sub_section[idx]];
    addr += mmio->base;
    addr -= section->offset_within_address_space;
    addr += section->offset_within_region;
1558
    io_mem_write(section->mr, addr, value, len);
1559 1560
}

1561 1562 1563 1564
static const MemoryRegionOps subpage_ops = {
    .read = subpage_read,
    .write = subpage_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
1565 1566
};

A
Avi Kivity 已提交
1567
static uint64_t subpage_ram_read(void *opaque, hwaddr addr,
1568
                                 unsigned size)
1569 1570 1571
{
    ram_addr_t raddr = addr;
    void *ptr = qemu_get_ram_ptr(raddr);
1572 1573 1574 1575 1576 1577
    switch (size) {
    case 1: return ldub_p(ptr);
    case 2: return lduw_p(ptr);
    case 4: return ldl_p(ptr);
    default: abort();
    }
1578 1579
}

A
Avi Kivity 已提交
1580
static void subpage_ram_write(void *opaque, hwaddr addr,
1581
                              uint64_t value, unsigned size)
1582 1583 1584
{
    ram_addr_t raddr = addr;
    void *ptr = qemu_get_ram_ptr(raddr);
1585 1586 1587 1588 1589 1590
    switch (size) {
    case 1: return stb_p(ptr, value);
    case 2: return stw_p(ptr, value);
    case 4: return stl_p(ptr, value);
    default: abort();
    }
1591 1592
}

1593 1594 1595 1596
static const MemoryRegionOps subpage_ram_ops = {
    .read = subpage_ram_read,
    .write = subpage_ram_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
1597 1598
};

A
Anthony Liguori 已提交
1599
static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
1600
                             uint16_t section)
1601 1602 1603 1604 1605 1606 1607 1608
{
    int idx, eidx;

    if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
        return -1;
    idx = SUBPAGE_IDX(start);
    eidx = SUBPAGE_IDX(end);
#if defined(DEBUG_SUBPAGE)
1609
    printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %ld\n", __func__,
1610 1611
           mmio, start, end, idx, eidx, memory);
#endif
1612 1613 1614 1615
    if (memory_region_is_ram(phys_sections[section].mr)) {
        MemoryRegionSection new_section = phys_sections[section];
        new_section.mr = &io_mem_subpage_ram;
        section = phys_section_add(&new_section);
1616
    }
1617
    for (; idx <= eidx; idx++) {
1618
        mmio->sub_section[idx] = section;
1619 1620 1621 1622 1623
    }

    return 0;
}

A
Avi Kivity 已提交
1624
static subpage_t *subpage_init(hwaddr base)
1625
{
A
Anthony Liguori 已提交
1626
    subpage_t *mmio;
1627

1628
    mmio = g_malloc0(sizeof(subpage_t));
1629 1630

    mmio->base = base;
1631 1632
    memory_region_init_io(&mmio->iomem, &subpage_ops, mmio,
                          "subpage", TARGET_PAGE_SIZE);
A
Avi Kivity 已提交
1633
    mmio->iomem.subpage = true;
1634
#if defined(DEBUG_SUBPAGE)
1635 1636
    printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__,
           mmio, base, TARGET_PAGE_SIZE, subpage_memory);
1637
#endif
1638
    subpage_register(mmio, 0, TARGET_PAGE_SIZE-1, phys_section_unassigned);
1639 1640 1641 1642

    return mmio;
}

1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654
static uint16_t dummy_section(MemoryRegion *mr)
{
    MemoryRegionSection section = {
        .mr = mr,
        .offset_within_address_space = 0,
        .offset_within_region = 0,
        .size = UINT64_MAX,
    };

    return phys_section_add(&section);
}

A
Avi Kivity 已提交
1655
MemoryRegion *iotlb_to_region(hwaddr index)
1656
{
1657
    return phys_sections[index & ~TARGET_PAGE_MASK].mr;
1658 1659
}

A
Avi Kivity 已提交
1660 1661
static void io_mem_init(void)
{
P
Paolo Bonzini 已提交
1662
    memory_region_init_io(&io_mem_rom, &unassigned_mem_ops, NULL, "rom", UINT64_MAX);
1663 1664 1665 1666
    memory_region_init_io(&io_mem_unassigned, &unassigned_mem_ops, NULL,
                          "unassigned", UINT64_MAX);
    memory_region_init_io(&io_mem_notdirty, &notdirty_mem_ops, NULL,
                          "notdirty", UINT64_MAX);
1667 1668
    memory_region_init_io(&io_mem_subpage_ram, &subpage_ram_ops, NULL,
                          "subpage-ram", UINT64_MAX);
1669 1670
    memory_region_init_io(&io_mem_watch, &watch_mem_ops, NULL,
                          "watch", UINT64_MAX);
A
Avi Kivity 已提交
1671 1672
}

A
Avi Kivity 已提交
1673 1674 1675 1676 1677 1678 1679 1680
static void mem_begin(MemoryListener *listener)
{
    AddressSpaceDispatch *d = container_of(listener, AddressSpaceDispatch, listener);

    destroy_all_mappings(d);
    d->phys_map.ptr = PHYS_MAP_NODE_NIL;
}

1681 1682
static void core_begin(MemoryListener *listener)
{
1683 1684
    phys_sections_clear();
    phys_section_unassigned = dummy_section(&io_mem_unassigned);
1685 1686 1687
    phys_section_notdirty = dummy_section(&io_mem_notdirty);
    phys_section_rom = dummy_section(&io_mem_rom);
    phys_section_watch = dummy_section(&io_mem_watch);
1688 1689
}

1690
static void tcg_commit(MemoryListener *listener)
1691
{
1692
    CPUArchState *env;
1693 1694 1695 1696 1697 1698 1699

    /* since each CPU stores ram addresses in its TLB cache, we must
       reset the modified entries */
    /* XXX: slow ! */
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
        tlb_flush(env, 1);
    }
1700 1701
}

1702 1703 1704 1705 1706 1707 1708 1709 1710 1711
static void core_log_global_start(MemoryListener *listener)
{
    cpu_physical_memory_set_dirty_tracking(1);
}

static void core_log_global_stop(MemoryListener *listener)
{
    cpu_physical_memory_set_dirty_tracking(0);
}

1712 1713 1714
static void io_region_add(MemoryListener *listener,
                          MemoryRegionSection *section)
{
A
Avi Kivity 已提交
1715 1716 1717 1718 1719
    MemoryRegionIORange *mrio = g_new(MemoryRegionIORange, 1);

    mrio->mr = section->mr;
    mrio->offset = section->offset_within_region;
    iorange_init(&mrio->iorange, &memory_region_iorange_ops,
1720
                 section->offset_within_address_space, section->size);
A
Avi Kivity 已提交
1721
    ioport_register(&mrio->iorange);
1722 1723 1724 1725 1726 1727 1728 1729
}

static void io_region_del(MemoryListener *listener,
                          MemoryRegionSection *section)
{
    isa_unassign_ioport(section->offset_within_address_space, section->size);
}

1730
static MemoryListener core_memory_listener = {
1731
    .begin = core_begin,
1732 1733
    .log_global_start = core_log_global_start,
    .log_global_stop = core_log_global_stop,
A
Avi Kivity 已提交
1734
    .priority = 1,
1735 1736
};

1737 1738 1739 1740 1741 1742
static MemoryListener io_memory_listener = {
    .region_add = io_region_add,
    .region_del = io_region_del,
    .priority = 0,
};

1743 1744 1745 1746
static MemoryListener tcg_memory_listener = {
    .commit = tcg_commit,
};

A
Avi Kivity 已提交
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
void address_space_init_dispatch(AddressSpace *as)
{
    AddressSpaceDispatch *d = g_new(AddressSpaceDispatch, 1);

    d->phys_map  = (PhysPageEntry) { .ptr = PHYS_MAP_NODE_NIL, .is_leaf = 0 };
    d->listener = (MemoryListener) {
        .begin = mem_begin,
        .region_add = mem_add,
        .region_nop = mem_add,
        .priority = 0,
    };
    as->dispatch = d;
    memory_listener_register(&d->listener, as);
}

A
Avi Kivity 已提交
1762 1763 1764 1765 1766 1767 1768 1769 1770 1771
void address_space_destroy_dispatch(AddressSpace *as)
{
    AddressSpaceDispatch *d = as->dispatch;

    memory_listener_unregister(&d->listener);
    destroy_l2_mapping(&d->phys_map, P_L2_LEVELS - 1);
    g_free(d);
    as->dispatch = NULL;
}

A
Avi Kivity 已提交
1772 1773
static void memory_map_init(void)
{
1774
    system_memory = g_malloc(sizeof(*system_memory));
A
Avi Kivity 已提交
1775
    memory_region_init(system_memory, "system", INT64_MAX);
1776 1777
    address_space_init(&address_space_memory, system_memory);
    address_space_memory.name = "memory";
1778

1779
    system_io = g_malloc(sizeof(*system_io));
1780
    memory_region_init(system_io, "io", 65536);
1781 1782
    address_space_init(&address_space_io, system_io);
    address_space_io.name = "I/O";
1783

1784 1785 1786
    memory_listener_register(&core_memory_listener, &address_space_memory);
    memory_listener_register(&io_memory_listener, &address_space_io);
    memory_listener_register(&tcg_memory_listener, &address_space_memory);
1787 1788 1789

    dma_context_init(&dma_context_memory, &address_space_memory,
                     NULL, NULL, NULL);
A
Avi Kivity 已提交
1790 1791 1792 1793 1794 1795 1796
}

MemoryRegion *get_system_memory(void)
{
    return system_memory;
}

1797 1798 1799 1800 1801
MemoryRegion *get_system_io(void)
{
    return system_io;
}

1802 1803
#endif /* !defined(CONFIG_USER_ONLY) */

B
bellard 已提交
1804 1805
/* physical memory access (slow version, mainly for debug) */
#if defined(CONFIG_USER_ONLY)
1806
int cpu_memory_rw_debug(CPUArchState *env, target_ulong addr,
P
Paul Brook 已提交
1807
                        uint8_t *buf, int len, int is_write)
B
bellard 已提交
1808 1809 1810
{
    int l, flags;
    target_ulong page;
1811
    void * p;
B
bellard 已提交
1812 1813 1814 1815 1816 1817 1818 1819

    while (len > 0) {
        page = addr & TARGET_PAGE_MASK;
        l = (page + TARGET_PAGE_SIZE) - addr;
        if (l > len)
            l = len;
        flags = page_get_flags(page);
        if (!(flags & PAGE_VALID))
P
Paul Brook 已提交
1820
            return -1;
B
bellard 已提交
1821 1822
        if (is_write) {
            if (!(flags & PAGE_WRITE))
P
Paul Brook 已提交
1823
                return -1;
1824
            /* XXX: this code should not depend on lock_user */
A
aurel32 已提交
1825
            if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
P
Paul Brook 已提交
1826
                return -1;
A
aurel32 已提交
1827 1828
            memcpy(p, buf, l);
            unlock_user(p, addr, l);
B
bellard 已提交
1829 1830
        } else {
            if (!(flags & PAGE_READ))
P
Paul Brook 已提交
1831
                return -1;
1832
            /* XXX: this code should not depend on lock_user */
A
aurel32 已提交
1833
            if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
P
Paul Brook 已提交
1834
                return -1;
A
aurel32 已提交
1835
            memcpy(buf, p, l);
A
aurel32 已提交
1836
            unlock_user(p, addr, 0);
B
bellard 已提交
1837 1838 1839 1840 1841
        }
        len -= l;
        buf += l;
        addr += l;
    }
P
Paul Brook 已提交
1842
    return 0;
B
bellard 已提交
1843
}
B
bellard 已提交
1844

B
bellard 已提交
1845
#else
1846

A
Avi Kivity 已提交
1847 1848
static void invalidate_and_set_dirty(hwaddr addr,
                                     hwaddr length)
1849 1850 1851 1852 1853 1854 1855
{
    if (!cpu_physical_memory_is_dirty(addr)) {
        /* invalidate code */
        tb_invalidate_phys_page_range(addr, addr + length, 0);
        /* set dirty bit */
        cpu_physical_memory_set_dirty_flags(addr, (0xff & ~CODE_DIRTY_FLAG));
    }
1856
    xen_modified_memory(addr, length);
1857 1858
}

A
Avi Kivity 已提交
1859
void address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
A
Avi Kivity 已提交
1860
                      int len, bool is_write)
B
bellard 已提交
1861
{
1862
    hwaddr l;
B
bellard 已提交
1863 1864
    uint8_t *ptr;
    uint32_t val;
1865
    hwaddr addr1;
1866
    MemoryRegionSection *section;
1867

B
bellard 已提交
1868
    while (len > 0) {
1869 1870
        l = len;
        section = address_space_translate(as, addr, &addr1, &l, is_write);
1871

B
bellard 已提交
1872
        if (is_write) {
1873
            if (!memory_region_is_ram(section->mr)) {
B
bellard 已提交
1874 1875
                /* XXX: could force cpu_single_env to NULL to avoid
                   potential bugs */
1876
                if (l >= 4 && ((addr1 & 3) == 0)) {
B
bellard 已提交
1877
                    /* 32 bit write access */
B
bellard 已提交
1878
                    val = ldl_p(buf);
1879
                    io_mem_write(section->mr, addr1, val, 4);
B
bellard 已提交
1880
                    l = 4;
1881
                } else if (l >= 2 && ((addr1 & 1) == 0)) {
B
bellard 已提交
1882
                    /* 16 bit write access */
B
bellard 已提交
1883
                    val = lduw_p(buf);
1884
                    io_mem_write(section->mr, addr1, val, 2);
B
bellard 已提交
1885 1886
                    l = 2;
                } else {
B
bellard 已提交
1887
                    /* 8 bit write access */
B
bellard 已提交
1888
                    val = ldub_p(buf);
1889
                    io_mem_write(section->mr, addr1, val, 1);
B
bellard 已提交
1890 1891
                    l = 1;
                }
1892
            } else if (!section->readonly) {
1893
                addr1 += memory_region_get_ram_addr(section->mr);
B
bellard 已提交
1894
                /* RAM case */
P
pbrook 已提交
1895
                ptr = qemu_get_ram_ptr(addr1);
B
bellard 已提交
1896
                memcpy(ptr, buf, l);
1897
                invalidate_and_set_dirty(addr1, l);
B
bellard 已提交
1898 1899
            }
        } else {
1900 1901
            if (!(memory_region_is_ram(section->mr) ||
                  memory_region_is_romd(section->mr))) {
B
bellard 已提交
1902
                /* I/O case */
1903
                if (l >= 4 && ((addr1 & 3) == 0)) {
B
bellard 已提交
1904
                    /* 32 bit read access */
1905
                    val = io_mem_read(section->mr, addr1, 4);
B
bellard 已提交
1906
                    stl_p(buf, val);
B
bellard 已提交
1907
                    l = 4;
1908
                } else if (l >= 2 && ((addr1 & 1) == 0)) {
B
bellard 已提交
1909
                    /* 16 bit read access */
1910
                    val = io_mem_read(section->mr, addr1, 2);
B
bellard 已提交
1911
                    stw_p(buf, val);
B
bellard 已提交
1912 1913
                    l = 2;
                } else {
B
bellard 已提交
1914
                    /* 8 bit read access */
1915
                    val = io_mem_read(section->mr, addr1, 1);
B
bellard 已提交
1916
                    stb_p(buf, val);
B
bellard 已提交
1917 1918 1919 1920
                    l = 1;
                }
            } else {
                /* RAM case */
1921
                ptr = qemu_get_ram_ptr(section->mr->ram_addr + addr1);
1922
                memcpy(buf, ptr, l);
B
bellard 已提交
1923 1924 1925 1926 1927 1928 1929
            }
        }
        len -= l;
        buf += l;
        addr += l;
    }
}
B
bellard 已提交
1930

A
Avi Kivity 已提交
1931
void address_space_write(AddressSpace *as, hwaddr addr,
A
Avi Kivity 已提交
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
                         const uint8_t *buf, int len)
{
    address_space_rw(as, addr, (uint8_t *)buf, len, true);
}

/**
 * address_space_read: read from an address space.
 *
 * @as: #AddressSpace to be accessed
 * @addr: address within that address space
 * @buf: buffer with the data transferred
 */
A
Avi Kivity 已提交
1944
void address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len)
A
Avi Kivity 已提交
1945 1946 1947 1948 1949
{
    address_space_rw(as, addr, buf, len, false);
}


A
Avi Kivity 已提交
1950
void cpu_physical_memory_rw(hwaddr addr, uint8_t *buf,
A
Avi Kivity 已提交
1951 1952 1953 1954 1955
                            int len, int is_write)
{
    return address_space_rw(&address_space_memory, addr, buf, len, is_write);
}

B
bellard 已提交
1956
/* used for ROM loading : can write in RAM and ROM */
A
Avi Kivity 已提交
1957
void cpu_physical_memory_write_rom(hwaddr addr,
B
bellard 已提交
1958 1959
                                   const uint8_t *buf, int len)
{
1960
    hwaddr l;
B
bellard 已提交
1961
    uint8_t *ptr;
1962
    hwaddr addr1;
1963
    MemoryRegionSection *section;
1964

B
bellard 已提交
1965
    while (len > 0) {
1966 1967 1968
        l = len;
        section = address_space_translate(&address_space_memory,
                                          addr, &addr1, &l, true);
1969

1970 1971
        if (!(memory_region_is_ram(section->mr) ||
              memory_region_is_romd(section->mr))) {
B
bellard 已提交
1972 1973
            /* do nothing */
        } else {
1974
            addr1 += memory_region_get_ram_addr(section->mr);
B
bellard 已提交
1975
            /* ROM/RAM case */
P
pbrook 已提交
1976
            ptr = qemu_get_ram_ptr(addr1);
B
bellard 已提交
1977
            memcpy(ptr, buf, l);
1978
            invalidate_and_set_dirty(addr1, l);
B
bellard 已提交
1979 1980 1981 1982 1983 1984 1985
        }
        len -= l;
        buf += l;
        addr += l;
    }
}

1986 1987
typedef struct {
    void *buffer;
A
Avi Kivity 已提交
1988 1989
    hwaddr addr;
    hwaddr len;
1990 1991 1992 1993
} BounceBuffer;

static BounceBuffer bounce;

1994 1995 1996
typedef struct MapClient {
    void *opaque;
    void (*callback)(void *opaque);
B
Blue Swirl 已提交
1997
    QLIST_ENTRY(MapClient) link;
1998 1999
} MapClient;

B
Blue Swirl 已提交
2000 2001
static QLIST_HEAD(map_client_list, MapClient) map_client_list
    = QLIST_HEAD_INITIALIZER(map_client_list);
2002 2003 2004

void *cpu_register_map_client(void *opaque, void (*callback)(void *opaque))
{
2005
    MapClient *client = g_malloc(sizeof(*client));
2006 2007 2008

    client->opaque = opaque;
    client->callback = callback;
B
Blue Swirl 已提交
2009
    QLIST_INSERT_HEAD(&map_client_list, client, link);
2010 2011 2012
    return client;
}

B
Blue Swirl 已提交
2013
static void cpu_unregister_map_client(void *_client)
2014 2015 2016
{
    MapClient *client = (MapClient *)_client;

B
Blue Swirl 已提交
2017
    QLIST_REMOVE(client, link);
2018
    g_free(client);
2019 2020 2021 2022 2023 2024
}

static void cpu_notify_map_clients(void)
{
    MapClient *client;

B
Blue Swirl 已提交
2025 2026
    while (!QLIST_EMPTY(&map_client_list)) {
        client = QLIST_FIRST(&map_client_list);
2027
        client->callback(client->opaque);
2028
        cpu_unregister_map_client(client);
2029 2030 2031
    }
}

2032 2033 2034 2035
/* Map a physical memory region into a host virtual address.
 * May map a subset of the requested range, given by and returned in *plen.
 * May return NULL if resources needed to perform the mapping are exhausted.
 * Use only for reads OR writes - not for read-modify-write operations.
2036 2037
 * Use cpu_register_map_client() to know when retrying the map operation is
 * likely to succeed.
2038
 */
A
Avi Kivity 已提交
2039
void *address_space_map(AddressSpace *as,
A
Avi Kivity 已提交
2040 2041
                        hwaddr addr,
                        hwaddr *plen,
A
Avi Kivity 已提交
2042
                        bool is_write)
2043
{
A
Avi Kivity 已提交
2044 2045
    hwaddr len = *plen;
    hwaddr todo = 0;
2046
    hwaddr l, xlat;
2047
    MemoryRegionSection *section;
2048
    ram_addr_t raddr = RAM_ADDR_MAX;
2049 2050
    ram_addr_t rlen;
    void *ret;
2051 2052

    while (len > 0) {
2053 2054
        l = len;
        section = address_space_translate(as, addr, &xlat, &l, is_write);
2055

2056
        if (!(memory_region_is_ram(section->mr) && !section->readonly)) {
2057
            if (todo || bounce.buffer) {
2058 2059 2060 2061 2062 2063
                break;
            }
            bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, TARGET_PAGE_SIZE);
            bounce.addr = addr;
            bounce.len = l;
            if (!is_write) {
A
Avi Kivity 已提交
2064
                address_space_read(as, addr, bounce.buffer, l);
2065
            }
2066 2067 2068

            *plen = l;
            return bounce.buffer;
2069
        }
2070
        if (!todo) {
2071 2072 2073 2074 2075
            raddr = memory_region_get_ram_addr(section->mr) + xlat;
        } else {
            if (memory_region_get_ram_addr(section->mr) + xlat != raddr + todo) {
                break;
            }
2076
        }
2077 2078 2079

        len -= l;
        addr += l;
2080
        todo += l;
2081
    }
2082 2083 2084 2085
    rlen = todo;
    ret = qemu_ram_ptr_length(raddr, &rlen);
    *plen = rlen;
    return ret;
2086 2087
}

A
Avi Kivity 已提交
2088
/* Unmaps a memory region previously mapped by address_space_map().
2089 2090 2091
 * Will also mark the memory as dirty if is_write == 1.  access_len gives
 * the amount of memory that was actually read or written by the caller.
 */
A
Avi Kivity 已提交
2092 2093
void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
                         int is_write, hwaddr access_len)
2094 2095 2096
{
    if (buffer != bounce.buffer) {
        if (is_write) {
M
Marcelo Tosatti 已提交
2097
            ram_addr_t addr1 = qemu_ram_addr_from_host_nofail(buffer);
2098 2099 2100 2101 2102
            while (access_len) {
                unsigned l;
                l = TARGET_PAGE_SIZE;
                if (l > access_len)
                    l = access_len;
2103
                invalidate_and_set_dirty(addr1, l);
2104 2105 2106 2107
                addr1 += l;
                access_len -= l;
            }
        }
2108
        if (xen_enabled()) {
J
Jan Kiszka 已提交
2109
            xen_invalidate_map_cache_entry(buffer);
A
Anthony PERARD 已提交
2110
        }
2111 2112 2113
        return;
    }
    if (is_write) {
A
Avi Kivity 已提交
2114
        address_space_write(as, bounce.addr, bounce.buffer, access_len);
2115
    }
2116
    qemu_vfree(bounce.buffer);
2117
    bounce.buffer = NULL;
2118
    cpu_notify_map_clients();
2119
}
B
bellard 已提交
2120

A
Avi Kivity 已提交
2121 2122
void *cpu_physical_memory_map(hwaddr addr,
                              hwaddr *plen,
A
Avi Kivity 已提交
2123 2124 2125 2126 2127
                              int is_write)
{
    return address_space_map(&address_space_memory, addr, plen, is_write);
}

A
Avi Kivity 已提交
2128 2129
void cpu_physical_memory_unmap(void *buffer, hwaddr len,
                               int is_write, hwaddr access_len)
A
Avi Kivity 已提交
2130 2131 2132 2133
{
    return address_space_unmap(&address_space_memory, buffer, len, is_write, access_len);
}

B
bellard 已提交
2134
/* warning: addr must be aligned */
A
Avi Kivity 已提交
2135
static inline uint32_t ldl_phys_internal(hwaddr addr,
2136
                                         enum device_endian endian)
B
bellard 已提交
2137 2138 2139
{
    uint8_t *ptr;
    uint32_t val;
2140
    MemoryRegionSection *section;
2141 2142
    hwaddr l = 4;
    hwaddr addr1;
B
bellard 已提交
2143

2144 2145 2146 2147
    section = address_space_translate(&address_space_memory, addr, &addr1, &l,
                                      false);
    if (l < 4 ||
        !(memory_region_is_ram(section->mr) ||
2148
          memory_region_is_romd(section->mr))) {
B
bellard 已提交
2149
        /* I/O case */
2150
        val = io_mem_read(section->mr, addr1, 4);
2151 2152 2153 2154 2155 2156 2157 2158 2159
#if defined(TARGET_WORDS_BIGENDIAN)
        if (endian == DEVICE_LITTLE_ENDIAN) {
            val = bswap32(val);
        }
#else
        if (endian == DEVICE_BIG_ENDIAN) {
            val = bswap32(val);
        }
#endif
B
bellard 已提交
2160 2161
    } else {
        /* RAM case */
2162
        ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(section->mr)
2163
                                & TARGET_PAGE_MASK)
2164
                               + addr1);
2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175
        switch (endian) {
        case DEVICE_LITTLE_ENDIAN:
            val = ldl_le_p(ptr);
            break;
        case DEVICE_BIG_ENDIAN:
            val = ldl_be_p(ptr);
            break;
        default:
            val = ldl_p(ptr);
            break;
        }
B
bellard 已提交
2176 2177 2178 2179
    }
    return val;
}

A
Avi Kivity 已提交
2180
uint32_t ldl_phys(hwaddr addr)
2181 2182 2183 2184
{
    return ldl_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
}

A
Avi Kivity 已提交
2185
uint32_t ldl_le_phys(hwaddr addr)
2186 2187 2188 2189
{
    return ldl_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
}

A
Avi Kivity 已提交
2190
uint32_t ldl_be_phys(hwaddr addr)
2191 2192 2193 2194
{
    return ldl_phys_internal(addr, DEVICE_BIG_ENDIAN);
}

B
bellard 已提交
2195
/* warning: addr must be aligned */
A
Avi Kivity 已提交
2196
static inline uint64_t ldq_phys_internal(hwaddr addr,
2197
                                         enum device_endian endian)
B
bellard 已提交
2198 2199 2200
{
    uint8_t *ptr;
    uint64_t val;
2201
    MemoryRegionSection *section;
2202 2203
    hwaddr l = 8;
    hwaddr addr1;
B
bellard 已提交
2204

2205 2206 2207 2208
    section = address_space_translate(&address_space_memory, addr, &addr1, &l,
                                      false);
    if (l < 8 ||
        !(memory_region_is_ram(section->mr) ||
2209
          memory_region_is_romd(section->mr))) {
B
bellard 已提交
2210
        /* I/O case */
2211 2212 2213

        /* XXX This is broken when device endian != cpu endian.
               Fix and add "endian" variable check */
B
bellard 已提交
2214
#ifdef TARGET_WORDS_BIGENDIAN
2215 2216
        val = io_mem_read(section->mr, addr1, 4) << 32;
        val |= io_mem_read(section->mr, addr1 + 4, 4);
B
bellard 已提交
2217
#else
2218 2219
        val = io_mem_read(section->mr, addr1, 4);
        val |= io_mem_read(section->mr, addr1 + 4, 4) << 32;
B
bellard 已提交
2220 2221 2222
#endif
    } else {
        /* RAM case */
2223
        ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(section->mr)
2224
                                & TARGET_PAGE_MASK)
2225
                               + addr1);
2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236
        switch (endian) {
        case DEVICE_LITTLE_ENDIAN:
            val = ldq_le_p(ptr);
            break;
        case DEVICE_BIG_ENDIAN:
            val = ldq_be_p(ptr);
            break;
        default:
            val = ldq_p(ptr);
            break;
        }
B
bellard 已提交
2237 2238 2239 2240
    }
    return val;
}

A
Avi Kivity 已提交
2241
uint64_t ldq_phys(hwaddr addr)
2242 2243 2244 2245
{
    return ldq_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
}

A
Avi Kivity 已提交
2246
uint64_t ldq_le_phys(hwaddr addr)
2247 2248 2249 2250
{
    return ldq_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
}

A
Avi Kivity 已提交
2251
uint64_t ldq_be_phys(hwaddr addr)
2252 2253 2254 2255
{
    return ldq_phys_internal(addr, DEVICE_BIG_ENDIAN);
}

B
bellard 已提交
2256
/* XXX: optimize */
A
Avi Kivity 已提交
2257
uint32_t ldub_phys(hwaddr addr)
B
bellard 已提交
2258 2259 2260 2261 2262 2263
{
    uint8_t val;
    cpu_physical_memory_read(addr, &val, 1);
    return val;
}

2264
/* warning: addr must be aligned */
A
Avi Kivity 已提交
2265
static inline uint32_t lduw_phys_internal(hwaddr addr,
2266
                                          enum device_endian endian)
B
bellard 已提交
2267
{
2268 2269
    uint8_t *ptr;
    uint64_t val;
2270
    MemoryRegionSection *section;
2271 2272
    hwaddr l = 2;
    hwaddr addr1;
2273

2274 2275 2276 2277
    section = address_space_translate(&address_space_memory, addr, &addr1, &l,
                                      false);
    if (l < 2 ||
        !(memory_region_is_ram(section->mr) ||
2278
          memory_region_is_romd(section->mr))) {
2279
        /* I/O case */
2280
        val = io_mem_read(section->mr, addr1, 2);
2281 2282 2283 2284 2285 2286 2287 2288 2289
#if defined(TARGET_WORDS_BIGENDIAN)
        if (endian == DEVICE_LITTLE_ENDIAN) {
            val = bswap16(val);
        }
#else
        if (endian == DEVICE_BIG_ENDIAN) {
            val = bswap16(val);
        }
#endif
2290 2291
    } else {
        /* RAM case */
2292
        ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(section->mr)
2293
                                & TARGET_PAGE_MASK)
2294
                               + addr1);
2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305
        switch (endian) {
        case DEVICE_LITTLE_ENDIAN:
            val = lduw_le_p(ptr);
            break;
        case DEVICE_BIG_ENDIAN:
            val = lduw_be_p(ptr);
            break;
        default:
            val = lduw_p(ptr);
            break;
        }
2306 2307
    }
    return val;
B
bellard 已提交
2308 2309
}

A
Avi Kivity 已提交
2310
uint32_t lduw_phys(hwaddr addr)
2311 2312 2313 2314
{
    return lduw_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
}

A
Avi Kivity 已提交
2315
uint32_t lduw_le_phys(hwaddr addr)
2316 2317 2318 2319
{
    return lduw_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
}

A
Avi Kivity 已提交
2320
uint32_t lduw_be_phys(hwaddr addr)
2321 2322 2323 2324
{
    return lduw_phys_internal(addr, DEVICE_BIG_ENDIAN);
}

B
bellard 已提交
2325 2326 2327
/* warning: addr must be aligned. The ram page is not masked as dirty
   and the code inside is not invalidated. It is useful if the dirty
   bits are used to track modified PTEs */
A
Avi Kivity 已提交
2328
void stl_phys_notdirty(hwaddr addr, uint32_t val)
B
bellard 已提交
2329 2330
{
    uint8_t *ptr;
2331
    MemoryRegionSection *section;
2332 2333
    hwaddr l = 4;
    hwaddr addr1;
B
bellard 已提交
2334

2335 2336 2337 2338
    section = address_space_translate(&address_space_memory, addr, &addr1, &l,
                                      true);
    if (l < 4 || !memory_region_is_ram(section->mr) || section->readonly) {
        io_mem_write(section->mr, addr1, val, 4);
B
bellard 已提交
2339
    } else {
2340
        addr1 += memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK;
P
pbrook 已提交
2341
        ptr = qemu_get_ram_ptr(addr1);
B
bellard 已提交
2342
        stl_p(ptr, val);
A
aliguori 已提交
2343 2344 2345 2346 2347 2348

        if (unlikely(in_migration)) {
            if (!cpu_physical_memory_is_dirty(addr1)) {
                /* invalidate code */
                tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
                /* set dirty bit */
2349 2350
                cpu_physical_memory_set_dirty_flags(
                    addr1, (0xff & ~CODE_DIRTY_FLAG));
A
aliguori 已提交
2351 2352
            }
        }
B
bellard 已提交
2353 2354 2355 2356
    }
}

/* warning: addr must be aligned */
A
Avi Kivity 已提交
2357
static inline void stl_phys_internal(hwaddr addr, uint32_t val,
2358
                                     enum device_endian endian)
B
bellard 已提交
2359 2360
{
    uint8_t *ptr;
2361
    MemoryRegionSection *section;
2362 2363
    hwaddr l = 4;
    hwaddr addr1;
B
bellard 已提交
2364

2365 2366 2367
    section = address_space_translate(&address_space_memory, addr, &addr1, &l,
                                      true);
    if (l < 4 || !memory_region_is_ram(section->mr) || section->readonly) {
2368 2369 2370 2371 2372 2373 2374 2375 2376
#if defined(TARGET_WORDS_BIGENDIAN)
        if (endian == DEVICE_LITTLE_ENDIAN) {
            val = bswap32(val);
        }
#else
        if (endian == DEVICE_BIG_ENDIAN) {
            val = bswap32(val);
        }
#endif
2377
        io_mem_write(section->mr, addr1, val, 4);
B
bellard 已提交
2378 2379
    } else {
        /* RAM case */
2380
        addr1 += memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK;
P
pbrook 已提交
2381
        ptr = qemu_get_ram_ptr(addr1);
2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392
        switch (endian) {
        case DEVICE_LITTLE_ENDIAN:
            stl_le_p(ptr, val);
            break;
        case DEVICE_BIG_ENDIAN:
            stl_be_p(ptr, val);
            break;
        default:
            stl_p(ptr, val);
            break;
        }
2393
        invalidate_and_set_dirty(addr1, 4);
B
bellard 已提交
2394 2395 2396
    }
}

A
Avi Kivity 已提交
2397
void stl_phys(hwaddr addr, uint32_t val)
2398 2399 2400 2401
{
    stl_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN);
}

A
Avi Kivity 已提交
2402
void stl_le_phys(hwaddr addr, uint32_t val)
2403 2404 2405 2406
{
    stl_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN);
}

A
Avi Kivity 已提交
2407
void stl_be_phys(hwaddr addr, uint32_t val)
2408 2409 2410 2411
{
    stl_phys_internal(addr, val, DEVICE_BIG_ENDIAN);
}

B
bellard 已提交
2412
/* XXX: optimize */
A
Avi Kivity 已提交
2413
void stb_phys(hwaddr addr, uint32_t val)
B
bellard 已提交
2414 2415 2416 2417 2418
{
    uint8_t v = val;
    cpu_physical_memory_write(addr, &v, 1);
}

2419
/* warning: addr must be aligned */
A
Avi Kivity 已提交
2420
static inline void stw_phys_internal(hwaddr addr, uint32_t val,
2421
                                     enum device_endian endian)
B
bellard 已提交
2422
{
2423
    uint8_t *ptr;
2424
    MemoryRegionSection *section;
2425 2426
    hwaddr l = 2;
    hwaddr addr1;
2427

2428 2429 2430
    section = address_space_translate(&address_space_memory, addr, &addr1, &l,
                                      true);
    if (l < 2 || !memory_region_is_ram(section->mr) || section->readonly) {
2431 2432 2433 2434 2435 2436 2437 2438 2439
#if defined(TARGET_WORDS_BIGENDIAN)
        if (endian == DEVICE_LITTLE_ENDIAN) {
            val = bswap16(val);
        }
#else
        if (endian == DEVICE_BIG_ENDIAN) {
            val = bswap16(val);
        }
#endif
2440
        io_mem_write(section->mr, addr1, val, 2);
2441 2442
    } else {
        /* RAM case */
2443
        addr1 += memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK;
2444
        ptr = qemu_get_ram_ptr(addr1);
2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455
        switch (endian) {
        case DEVICE_LITTLE_ENDIAN:
            stw_le_p(ptr, val);
            break;
        case DEVICE_BIG_ENDIAN:
            stw_be_p(ptr, val);
            break;
        default:
            stw_p(ptr, val);
            break;
        }
2456
        invalidate_and_set_dirty(addr1, 2);
2457
    }
B
bellard 已提交
2458 2459
}

A
Avi Kivity 已提交
2460
void stw_phys(hwaddr addr, uint32_t val)
2461 2462 2463 2464
{
    stw_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN);
}

A
Avi Kivity 已提交
2465
void stw_le_phys(hwaddr addr, uint32_t val)
2466 2467 2468 2469
{
    stw_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN);
}

A
Avi Kivity 已提交
2470
void stw_be_phys(hwaddr addr, uint32_t val)
2471 2472 2473 2474
{
    stw_phys_internal(addr, val, DEVICE_BIG_ENDIAN);
}

B
bellard 已提交
2475
/* XXX: optimize */
A
Avi Kivity 已提交
2476
void stq_phys(hwaddr addr, uint64_t val)
B
bellard 已提交
2477 2478
{
    val = tswap64(val);
2479
    cpu_physical_memory_write(addr, &val, 8);
B
bellard 已提交
2480 2481
}

A
Avi Kivity 已提交
2482
void stq_le_phys(hwaddr addr, uint64_t val)
2483 2484 2485 2486 2487
{
    val = cpu_to_le64(val);
    cpu_physical_memory_write(addr, &val, 8);
}

A
Avi Kivity 已提交
2488
void stq_be_phys(hwaddr addr, uint64_t val)
2489 2490 2491 2492 2493
{
    val = cpu_to_be64(val);
    cpu_physical_memory_write(addr, &val, 8);
}

2494
/* virtual memory access for debug (includes writing to ROM) */
2495
int cpu_memory_rw_debug(CPUArchState *env, target_ulong addr,
2496
                        uint8_t *buf, int len, int is_write)
B
bellard 已提交
2497 2498
{
    int l;
A
Avi Kivity 已提交
2499
    hwaddr phys_addr;
2500
    target_ulong page;
B
bellard 已提交
2501 2502 2503 2504 2505 2506 2507 2508 2509 2510

    while (len > 0) {
        page = addr & TARGET_PAGE_MASK;
        phys_addr = cpu_get_phys_page_debug(env, page);
        /* if no physical page mapped, return an error */
        if (phys_addr == -1)
            return -1;
        l = (page + TARGET_PAGE_SIZE) - addr;
        if (l > len)
            l = len;
2511 2512 2513 2514 2515
        phys_addr += (addr & ~TARGET_PAGE_MASK);
        if (is_write)
            cpu_physical_memory_write_rom(phys_addr, buf, l);
        else
            cpu_physical_memory_rw(phys_addr, buf, l, is_write);
B
bellard 已提交
2516 2517 2518 2519 2520 2521
        len -= l;
        buf += l;
        addr += l;
    }
    return 0;
}
P
Paul Brook 已提交
2522
#endif
B
bellard 已提交
2523

2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541
#if !defined(CONFIG_USER_ONLY)

/*
 * A helper function for the _utterly broken_ virtio device model to find out if
 * it's running on a big endian machine. Don't do this at home kids!
 */
bool virtio_is_big_endian(void);
bool virtio_is_big_endian(void)
{
#if defined(TARGET_WORDS_BIGENDIAN)
    return true;
#else
    return false;
#endif
}

#endif

2542
#ifndef CONFIG_USER_ONLY
A
Avi Kivity 已提交
2543
bool cpu_physical_memory_is_io(hwaddr phys_addr)
2544 2545
{
    MemoryRegionSection *section;
2546
    hwaddr l = 1;
2547

2548 2549
    section = address_space_translate(&address_space_memory,
                                      phys_addr, &phys_addr, &l, false);
2550 2551 2552 2553 2554

    return !(memory_region_is_ram(section->mr) ||
             memory_region_is_romd(section->mr));
}
#endif