memory.c 57 KB
Newer Older
Avi Kivity's avatar
Avi Kivity committed
1 2 3 4 5 6 7 8 9 10 11
/*
 * Physical memory management
 *
 * Copyright 2011 Red Hat, Inc. and/or its affiliates
 *
 * Authors:
 *  Avi Kivity <avi@redhat.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 * the COPYING file in the top-level directory.
 *
12 13
 * Contributions after 2012-01-13 are licensed under the terms of the
 * GNU GPL, version 2 or (at your option) any later version.
Avi Kivity's avatar
Avi Kivity committed
14 15
 */

16 17 18
#include "exec/memory.h"
#include "exec/address-spaces.h"
#include "exec/ioport.h"
19
#include "qemu/bitops.h"
20
#include "qom/object.h"
21
#include "sysemu/kvm.h"
Avi Kivity's avatar
Avi Kivity committed
22 23
#include <assert.h>

24
#include "exec/memory-internal.h"
25

26 27
//#define DEBUG_UNASSIGNED

28 29
static unsigned memory_region_transaction_depth;
static bool memory_region_update_pending;
30 31
static bool global_dirty_log = false;

32 33 34 35 36 37 38 39 40
/* flat_view_mutex is taken around reading as->current_map; the critical
 * section is extremely short, so I'm using a single mutex for every AS.
 * We could also RCU for the read-side.
 *
 * The BQL is taken around transaction commits, hence both locks are taken
 * while writing to as->current_map (with the BQL taken outside).
 */
static QemuMutex flat_view_mutex;

41 42
static QTAILQ_HEAD(memory_listeners, MemoryListener) memory_listeners
    = QTAILQ_HEAD_INITIALIZER(memory_listeners);
Avi Kivity's avatar
Avi Kivity committed
43

44 45 46
static QTAILQ_HEAD(, AddressSpace) address_spaces
    = QTAILQ_HEAD_INITIALIZER(address_spaces);

47 48 49 50 51
static void memory_init(void)
{
    qemu_mutex_init(&flat_view_mutex);
}

Avi Kivity's avatar
Avi Kivity committed
52 53
typedef struct AddrRange AddrRange;

Avi Kivity's avatar
Avi Kivity committed
54 55 56 57 58
/*
 * Note using signed integers limits us to physical addresses at most
 * 63 bits wide.  They are needed for negative offsetting in aliases
 * (large MemoryRegion::alias_offset).
 */
Avi Kivity's avatar
Avi Kivity committed
59
struct AddrRange {
60 61
    Int128 start;
    Int128 size;
Avi Kivity's avatar
Avi Kivity committed
62 63
};

64
static AddrRange addrrange_make(Int128 start, Int128 size)
Avi Kivity's avatar
Avi Kivity committed
65 66 67 68 69 70
{
    return (AddrRange) { start, size };
}

static bool addrrange_equal(AddrRange r1, AddrRange r2)
{
71
    return int128_eq(r1.start, r2.start) && int128_eq(r1.size, r2.size);
Avi Kivity's avatar
Avi Kivity committed
72 73
}

74
static Int128 addrrange_end(AddrRange r)
Avi Kivity's avatar
Avi Kivity committed
75
{
76
    return int128_add(r.start, r.size);
Avi Kivity's avatar
Avi Kivity committed
77 78
}

79
static AddrRange addrrange_shift(AddrRange range, Int128 delta)
Avi Kivity's avatar
Avi Kivity committed
80
{
81
    int128_addto(&range.start, delta);
Avi Kivity's avatar
Avi Kivity committed
82 83 84
    return range;
}

85 86 87 88 89 90
static bool addrrange_contains(AddrRange range, Int128 addr)
{
    return int128_ge(addr, range.start)
        && int128_lt(addr, addrrange_end(range));
}

Avi Kivity's avatar
Avi Kivity committed
91 92
static bool addrrange_intersects(AddrRange r1, AddrRange r2)
{
93 94
    return addrrange_contains(r1, r2.start)
        || addrrange_contains(r2, r1.start);
Avi Kivity's avatar
Avi Kivity committed
95 96 97 98
}

static AddrRange addrrange_intersection(AddrRange r1, AddrRange r2)
{
99 100 101
    Int128 start = int128_max(r1.start, r2.start);
    Int128 end = int128_min(addrrange_end(r1), addrrange_end(r2));
    return addrrange_make(start, int128_sub(end, start));
Avi Kivity's avatar
Avi Kivity committed
102 103
}

104 105
enum ListenerDirection { Forward, Reverse };

106 107 108 109 110 111 112 113
static bool memory_listener_match(MemoryListener *listener,
                                  MemoryRegionSection *section)
{
    return !listener->address_space_filter
        || listener->address_space_filter == section->address_space;
}

#define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...)    \
114 115 116 117 118 119
    do {                                                                \
        MemoryListener *_listener;                                      \
                                                                        \
        switch (_direction) {                                           \
        case Forward:                                                   \
            QTAILQ_FOREACH(_listener, &memory_listeners, link) {        \
120 121 122
                if (_listener->_callback) {                             \
                    _listener->_callback(_listener, ##_args);           \
                }                                                       \
123 124 125 126 127
            }                                                           \
            break;                                                      \
        case Reverse:                                                   \
            QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners,        \
                                   memory_listeners, link) {            \
128 129 130
                if (_listener->_callback) {                             \
                    _listener->_callback(_listener, ##_args);           \
                }                                                       \
131 132 133 134 135 136 137
            }                                                           \
            break;                                                      \
        default:                                                        \
            abort();                                                    \
        }                                                               \
    } while (0)

138 139 140 141 142 143 144
#define MEMORY_LISTENER_CALL(_callback, _direction, _section, _args...) \
    do {                                                                \
        MemoryListener *_listener;                                      \
                                                                        \
        switch (_direction) {                                           \
        case Forward:                                                   \
            QTAILQ_FOREACH(_listener, &memory_listeners, link) {        \
145 146
                if (_listener->_callback                                \
                    && memory_listener_match(_listener, _section)) {    \
147 148 149 150 151 152 153
                    _listener->_callback(_listener, _section, ##_args); \
                }                                                       \
            }                                                           \
            break;                                                      \
        case Reverse:                                                   \
            QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners,        \
                                   memory_listeners, link) {            \
154 155
                if (_listener->_callback                                \
                    && memory_listener_match(_listener, _section)) {    \
156 157 158 159 160 161 162 163 164
                    _listener->_callback(_listener, _section, ##_args); \
                }                                                       \
            }                                                           \
            break;                                                      \
        default:                                                        \
            abort();                                                    \
        }                                                               \
    } while (0)

Paolo Bonzini's avatar
Paolo Bonzini committed
165
/* No need to ref/unref .mr, the FlatRange keeps it alive.  */
166
#define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback)            \
167
    MEMORY_LISTENER_CALL(callback, dir, (&(MemoryRegionSection) {       \
168
        .mr = (fr)->mr,                                                 \
169
        .address_space = (as),                                          \
170
        .offset_within_region = (fr)->offset_in_region,                 \
171
        .size = (fr)->addr.size,                                        \
172
        .offset_within_address_space = int128_get64((fr)->addr.start),  \
173
        .readonly = (fr)->readonly,                                     \
174
              }))
175

Avi Kivity's avatar
Avi Kivity committed
176 177 178 179 180
struct CoalescedMemoryRange {
    AddrRange addr;
    QTAILQ_ENTRY(CoalescedMemoryRange) link;
};

Avi Kivity's avatar
Avi Kivity committed
181 182 183 184
struct MemoryRegionIoeventfd {
    AddrRange addr;
    bool match_data;
    uint64_t data;
185
    EventNotifier *e;
Avi Kivity's avatar
Avi Kivity committed
186 187 188 189 190
};

static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a,
                                           MemoryRegionIoeventfd b)
{
191
    if (int128_lt(a.addr.start, b.addr.start)) {
Avi Kivity's avatar
Avi Kivity committed
192
        return true;
193
    } else if (int128_gt(a.addr.start, b.addr.start)) {
Avi Kivity's avatar
Avi Kivity committed
194
        return false;
195
    } else if (int128_lt(a.addr.size, b.addr.size)) {
Avi Kivity's avatar
Avi Kivity committed
196
        return true;
197
    } else if (int128_gt(a.addr.size, b.addr.size)) {
Avi Kivity's avatar
Avi Kivity committed
198 199 200 201 202 203 204 205 206 207 208 209
        return false;
    } else if (a.match_data < b.match_data) {
        return true;
    } else  if (a.match_data > b.match_data) {
        return false;
    } else if (a.match_data) {
        if (a.data < b.data) {
            return true;
        } else if (a.data > b.data) {
            return false;
        }
    }
210
    if (a.e < b.e) {
Avi Kivity's avatar
Avi Kivity committed
211
        return true;
212
    } else if (a.e > b.e) {
Avi Kivity's avatar
Avi Kivity committed
213 214 215 216 217 218 219 220 221 222 223 224
        return false;
    }
    return false;
}

static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a,
                                          MemoryRegionIoeventfd b)
{
    return !memory_region_ioeventfd_before(a, b)
        && !memory_region_ioeventfd_before(b, a);
}

Avi Kivity's avatar
Avi Kivity committed
225 226 227 228 229 230
typedef struct FlatRange FlatRange;
typedef struct FlatView FlatView;

/* Range of memory in the global map.  Addresses are absolute. */
struct FlatRange {
    MemoryRegion *mr;
231
    hwaddr offset_in_region;
Avi Kivity's avatar
Avi Kivity committed
232
    AddrRange addr;
233
    uint8_t dirty_log_mask;
234
    bool romd_mode;
235
    bool readonly;
Avi Kivity's avatar
Avi Kivity committed
236 237 238 239 240 241
};

/* Flattened global view of current active memory hierarchy.  Kept in sorted
 * order.
 */
struct FlatView {
242
    unsigned ref;
Avi Kivity's avatar
Avi Kivity committed
243 244 245 246 247
    FlatRange *ranges;
    unsigned nr;
    unsigned nr_allocated;
};

248 249
typedef struct AddressSpaceOps AddressSpaceOps;

Avi Kivity's avatar
Avi Kivity committed
250 251 252 253 254 255 256
#define FOR_EACH_FLAT_RANGE(var, view)          \
    for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)

static bool flatrange_equal(FlatRange *a, FlatRange *b)
{
    return a->mr == b->mr
        && addrrange_equal(a->addr, b->addr)
257
        && a->offset_in_region == b->offset_in_region
258
        && a->romd_mode == b->romd_mode
259
        && a->readonly == b->readonly;
Avi Kivity's avatar
Avi Kivity committed
260 261 262 263
}

static void flatview_init(FlatView *view)
{
264
    view->ref = 1;
Avi Kivity's avatar
Avi Kivity committed
265 266 267 268 269 270 271 272 273 274 275 276
    view->ranges = NULL;
    view->nr = 0;
    view->nr_allocated = 0;
}

/* Insert a range into a given position.  Caller is responsible for maintaining
 * sorting order.
 */
static void flatview_insert(FlatView *view, unsigned pos, FlatRange *range)
{
    if (view->nr == view->nr_allocated) {
        view->nr_allocated = MAX(2 * view->nr, 10);
277
        view->ranges = g_realloc(view->ranges,
Avi Kivity's avatar
Avi Kivity committed
278 279 280 281 282
                                    view->nr_allocated * sizeof(*view->ranges));
    }
    memmove(view->ranges + pos + 1, view->ranges + pos,
            (view->nr - pos) * sizeof(FlatRange));
    view->ranges[pos] = *range;
Paolo Bonzini's avatar
Paolo Bonzini committed
283
    memory_region_ref(range->mr);
Avi Kivity's avatar
Avi Kivity committed
284 285 286 287 288
    ++view->nr;
}

static void flatview_destroy(FlatView *view)
{
Paolo Bonzini's avatar
Paolo Bonzini committed
289 290 291 292 293
    int i;

    for (i = 0; i < view->nr; i++) {
        memory_region_unref(view->ranges[i].mr);
    }
294
    g_free(view->ranges);
295
    g_free(view);
Avi Kivity's avatar
Avi Kivity committed
296 297
}

298 299 300 301 302 303 304 305 306 307 308 309
static void flatview_ref(FlatView *view)
{
    atomic_inc(&view->ref);
}

static void flatview_unref(FlatView *view)
{
    if (atomic_fetch_dec(&view->ref) == 1) {
        flatview_destroy(view);
    }
}

310 311
static bool can_merge(FlatRange *r1, FlatRange *r2)
{
312
    return int128_eq(addrrange_end(r1->addr), r2->addr.start)
313
        && r1->mr == r2->mr
314 315 316
        && int128_eq(int128_add(int128_make64(r1->offset_in_region),
                                r1->addr.size),
                     int128_make64(r2->offset_in_region))
317
        && r1->dirty_log_mask == r2->dirty_log_mask
318
        && r1->romd_mode == r2->romd_mode
319
        && r1->readonly == r2->readonly;
320 321
}

Peter Crosthwaite's avatar
Peter Crosthwaite committed
322
/* Attempt to simplify a view by merging adjacent ranges */
323 324 325 326 327 328 329 330 331
static void flatview_simplify(FlatView *view)
{
    unsigned i, j;

    i = 0;
    while (i < view->nr) {
        j = i + 1;
        while (j < view->nr
               && can_merge(&view->ranges[j-1], &view->ranges[j])) {
332
            int128_addto(&view->ranges[i].addr.size, view->ranges[j].addr.size);
333 334 335 336 337 338 339 340 341
            ++j;
        }
        ++i;
        memmove(&view->ranges[i], &view->ranges[j],
                (view->nr - j) * sizeof(view->ranges[j]));
        view->nr -= j - i;
    }
}

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
static bool memory_region_wrong_endianness(MemoryRegion *mr)
{
#ifdef TARGET_WORDS_BIGENDIAN
    return mr->ops->endianness == DEVICE_LITTLE_ENDIAN;
#else
    return mr->ops->endianness == DEVICE_BIG_ENDIAN;
#endif
}

static void adjust_endianness(MemoryRegion *mr, uint64_t *data, unsigned size)
{
    if (memory_region_wrong_endianness(mr)) {
        switch (size) {
        case 1:
            break;
        case 2:
            *data = bswap16(*data);
            break;
        case 4:
            *data = bswap32(*data);
            break;
        case 8:
            *data = bswap64(*data);
            break;
        default:
            abort();
        }
    }
}

372 373 374 375 376 377 378 379 380 381 382 383 384 385
static void memory_region_oldmmio_read_accessor(void *opaque,
                                                hwaddr addr,
                                                uint64_t *value,
                                                unsigned size,
                                                unsigned shift,
                                                uint64_t mask)
{
    MemoryRegion *mr = opaque;
    uint64_t tmp;

    tmp = mr->ops->old_mmio.read[ctz32(size)](mr->opaque, addr);
    *value |= (tmp & mask) << shift;
}

386
static void memory_region_read_accessor(void *opaque,
387
                                        hwaddr addr,
388 389 390 391 392 393 394 395
                                        uint64_t *value,
                                        unsigned size,
                                        unsigned shift,
                                        uint64_t mask)
{
    MemoryRegion *mr = opaque;
    uint64_t tmp;

396 397 398
    if (mr->flush_coalesced_mmio) {
        qemu_flush_coalesced_mmio_buffer();
    }
399 400 401 402
    tmp = mr->ops->read(mr->opaque, addr, size);
    *value |= (tmp & mask) << shift;
}

403 404 405 406 407 408 409 410 411 412 413 414 415 416
static void memory_region_oldmmio_write_accessor(void *opaque,
                                                 hwaddr addr,
                                                 uint64_t *value,
                                                 unsigned size,
                                                 unsigned shift,
                                                 uint64_t mask)
{
    MemoryRegion *mr = opaque;
    uint64_t tmp;

    tmp = (*value >> shift) & mask;
    mr->ops->old_mmio.write[ctz32(size)](mr->opaque, addr, tmp);
}

417
static void memory_region_write_accessor(void *opaque,
418
                                         hwaddr addr,
419 420 421 422 423 424 425 426
                                         uint64_t *value,
                                         unsigned size,
                                         unsigned shift,
                                         uint64_t mask)
{
    MemoryRegion *mr = opaque;
    uint64_t tmp;

427 428 429
    if (mr->flush_coalesced_mmio) {
        qemu_flush_coalesced_mmio_buffer();
    }
430 431 432 433
    tmp = (*value >> shift) & mask;
    mr->ops->write(mr->opaque, addr, tmp, size);
}

434
static void access_with_adjusted_size(hwaddr addr,
435 436 437 438 439
                                      uint64_t *value,
                                      unsigned size,
                                      unsigned access_size_min,
                                      unsigned access_size_max,
                                      void (*access)(void *opaque,
440
                                                     hwaddr addr,
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
                                                     uint64_t *value,
                                                     unsigned size,
                                                     unsigned shift,
                                                     uint64_t mask),
                                      void *opaque)
{
    uint64_t access_mask;
    unsigned access_size;
    unsigned i;

    if (!access_size_min) {
        access_size_min = 1;
    }
    if (!access_size_max) {
        access_size_max = 4;
    }
457 458

    /* FIXME: support unaligned access? */
459 460 461
    access_size = MAX(MIN(size, access_size_max), access_size_min);
    access_mask = -1ULL >> (64 - access_size * 8);
    for (i = 0; i < size; i += access_size) {
462 463 464 465
#ifdef TARGET_WORDS_BIGENDIAN
        access(opaque, addr + i, value, access_size,
               (size - access_size - i) * 8, access_mask);
#else
466
        access(opaque, addr + i, value, access_size, i * 8, access_mask);
467
#endif
468 469 470
    }
}

471 472
static AddressSpace *memory_region_to_address_space(MemoryRegion *mr)
{
473 474
    AddressSpace *as;

475 476 477
    while (mr->parent) {
        mr = mr->parent;
    }
478 479 480 481
    QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
        if (mr == as->root) {
            return as;
        }
482 483 484 485
    }
    abort();
}

Avi Kivity's avatar
Avi Kivity committed
486 487 488 489 490
/* Render a memory region into the global view.  Ranges in @view obscure
 * ranges in @mr.
 */
static void render_memory_region(FlatView *view,
                                 MemoryRegion *mr,
491
                                 Int128 base,
492 493
                                 AddrRange clip,
                                 bool readonly)
Avi Kivity's avatar
Avi Kivity committed
494 495 496
{
    MemoryRegion *subregion;
    unsigned i;
497
    hwaddr offset_in_region;
498 499
    Int128 remain;
    Int128 now;
Avi Kivity's avatar
Avi Kivity committed
500 501 502
    FlatRange fr;
    AddrRange tmp;

503 504 505 506
    if (!mr->enabled) {
        return;
    }

507
    int128_addto(&base, int128_make64(mr->addr));
508
    readonly |= mr->readonly;
Avi Kivity's avatar
Avi Kivity committed
509 510 511 512 513 514 515 516 517 518

    tmp = addrrange_make(base, mr->size);

    if (!addrrange_intersects(tmp, clip)) {
        return;
    }

    clip = addrrange_intersection(tmp, clip);

    if (mr->alias) {
519 520
        int128_subfrom(&base, int128_make64(mr->alias->addr));
        int128_subfrom(&base, int128_make64(mr->alias_offset));
521
        render_memory_region(view, mr->alias, base, clip, readonly);
Avi Kivity's avatar
Avi Kivity committed
522 523 524 525 526
        return;
    }

    /* Render subregions in priority order. */
    QTAILQ_FOREACH(subregion, &mr->subregions, subregions_link) {
527
        render_memory_region(view, subregion, base, clip, readonly);
Avi Kivity's avatar
Avi Kivity committed
528 529
    }

530
    if (!mr->terminates) {
Avi Kivity's avatar
Avi Kivity committed
531 532 533
        return;
    }

534
    offset_in_region = int128_get64(int128_sub(clip.start, base));
Avi Kivity's avatar
Avi Kivity committed
535 536 537
    base = clip.start;
    remain = clip.size;

538 539 540 541 542
    fr.mr = mr;
    fr.dirty_log_mask = mr->dirty_log_mask;
    fr.romd_mode = mr->romd_mode;
    fr.readonly = readonly;

Avi Kivity's avatar
Avi Kivity committed
543
    /* Render the region itself into any gaps left by the current view. */
544 545
    for (i = 0; i < view->nr && int128_nz(remain); ++i) {
        if (int128_ge(base, addrrange_end(view->ranges[i].addr))) {
Avi Kivity's avatar
Avi Kivity committed
546 547
            continue;
        }
548 549 550
        if (int128_lt(base, view->ranges[i].addr.start)) {
            now = int128_min(remain,
                             int128_sub(view->ranges[i].addr.start, base));
Avi Kivity's avatar
Avi Kivity committed
551 552 553 554
            fr.offset_in_region = offset_in_region;
            fr.addr = addrrange_make(base, now);
            flatview_insert(view, i, &fr);
            ++i;
555 556 557
            int128_addto(&base, now);
            offset_in_region += int128_get64(now);
            int128_subfrom(&remain, now);
Avi Kivity's avatar
Avi Kivity committed
558
        }
559 560 561 562 563 564
        now = int128_sub(int128_min(int128_add(base, remain),
                                    addrrange_end(view->ranges[i].addr)),
                         base);
        int128_addto(&base, now);
        offset_in_region += int128_get64(now);
        int128_subfrom(&remain, now);
Avi Kivity's avatar
Avi Kivity committed
565
    }
566
    if (int128_nz(remain)) {
Avi Kivity's avatar
Avi Kivity committed
567 568 569 570 571 572 573
        fr.offset_in_region = offset_in_region;
        fr.addr = addrrange_make(base, remain);
        flatview_insert(view, i, &fr);
    }
}

/* Render a memory topology into a list of disjoint absolute ranges. */
574
static FlatView *generate_memory_topology(MemoryRegion *mr)
Avi Kivity's avatar
Avi Kivity committed
575
{
576
    FlatView *view;
Avi Kivity's avatar
Avi Kivity committed
577

578 579
    view = g_new(FlatView, 1);
    flatview_init(view);
Avi Kivity's avatar
Avi Kivity committed
580

581
    if (mr) {
582
        render_memory_region(view, mr, int128_zero(),
583 584
                             addrrange_make(int128_zero(), int128_2_64()), false);
    }
585
    flatview_simplify(view);
Avi Kivity's avatar
Avi Kivity committed
586 587 588 589

    return view;
}

Avi Kivity's avatar
Avi Kivity committed
590 591 592 593 594 595 596
static void address_space_add_del_ioeventfds(AddressSpace *as,
                                             MemoryRegionIoeventfd *fds_new,
                                             unsigned fds_new_nb,
                                             MemoryRegionIoeventfd *fds_old,
                                             unsigned fds_old_nb)
{
    unsigned iold, inew;
597 598
    MemoryRegionIoeventfd *fd;
    MemoryRegionSection section;
Avi Kivity's avatar
Avi Kivity committed
599 600 601 602 603 604 605 606 607 608 609

    /* Generate a symmetric difference of the old and new fd sets, adding
     * and deleting as necessary.
     */

    iold = inew = 0;
    while (iold < fds_old_nb || inew < fds_new_nb) {
        if (iold < fds_old_nb
            && (inew == fds_new_nb
                || memory_region_ioeventfd_before(fds_old[iold],
                                                  fds_new[inew]))) {
610 611
            fd = &fds_old[iold];
            section = (MemoryRegionSection) {
612
                .address_space = as,
613
                .offset_within_address_space = int128_get64(fd->addr.start),
614
                .size = fd->addr.size,
615 616
            };
            MEMORY_LISTENER_CALL(eventfd_del, Forward, &section,
617
                                 fd->match_data, fd->data, fd->e);
Avi Kivity's avatar
Avi Kivity committed
618 619 620 621 622
            ++iold;
        } else if (inew < fds_new_nb
                   && (iold == fds_old_nb
                       || memory_region_ioeventfd_before(fds_new[inew],
                                                         fds_old[iold]))) {
623 624
            fd = &fds_new[inew];
            section = (MemoryRegionSection) {
625
                .address_space = as,
626
                .offset_within_address_space = int128_get64(fd->addr.start),
627
                .size = fd->addr.size,
628 629
            };
            MEMORY_LISTENER_CALL(eventfd_add, Reverse, &section,
630
                                 fd->match_data, fd->data, fd->e);
Avi Kivity's avatar
Avi Kivity committed
631 632 633 634 635 636 637 638
            ++inew;
        } else {
            ++iold;
            ++inew;
        }
    }
}

639 640 641 642 643 644 645 646 647 648 649
static FlatView *address_space_get_flatview(AddressSpace *as)
{
    FlatView *view;

    qemu_mutex_lock(&flat_view_mutex);
    view = as->current_map;
    flatview_ref(view);
    qemu_mutex_unlock(&flat_view_mutex);
    return view;
}

Avi Kivity's avatar
Avi Kivity committed
650 651
static void address_space_update_ioeventfds(AddressSpace *as)
{
652
    FlatView *view;
Avi Kivity's avatar
Avi Kivity committed
653 654 655 656 657 658
    FlatRange *fr;
    unsigned ioeventfd_nb = 0;
    MemoryRegionIoeventfd *ioeventfds = NULL;
    AddrRange tmp;
    unsigned i;

659
    view = address_space_get_flatview(as);
660
    FOR_EACH_FLAT_RANGE(fr, view) {
Avi Kivity's avatar
Avi Kivity committed
661 662
        for (i = 0; i < fr->mr->ioeventfd_nb; ++i) {
            tmp = addrrange_shift(fr->mr->ioeventfds[i].addr,
663 664
                                  int128_sub(fr->addr.start,
                                             int128_make64(fr->offset_in_region)));
Avi Kivity's avatar
Avi Kivity committed
665 666
            if (addrrange_intersects(fr->addr, tmp)) {
                ++ioeventfd_nb;
667
                ioeventfds = g_realloc(ioeventfds,
Avi Kivity's avatar
Avi Kivity committed
668 669 670 671 672 673 674 675 676 677
                                          ioeventfd_nb * sizeof(*ioeventfds));
                ioeventfds[ioeventfd_nb-1] = fr->mr->ioeventfds[i];
                ioeventfds[ioeventfd_nb-1].addr = tmp;
            }
        }
    }

    address_space_add_del_ioeventfds(as, ioeventfds, ioeventfd_nb,
                                     as->ioeventfds, as->ioeventfd_nb);

678
    g_free(as->ioeventfds);
Avi Kivity's avatar
Avi Kivity committed
679 680
    as->ioeventfds = ioeventfds;
    as->ioeventfd_nb = ioeventfd_nb;
681
    flatview_unref(view);
Avi Kivity's avatar
Avi Kivity committed
682 683
}

684
static void address_space_update_topology_pass(AddressSpace *as,
685 686
                                               const FlatView *old_view,
                                               const FlatView *new_view,
687
                                               bool adding)
Avi Kivity's avatar
Avi Kivity committed
688 689 690 691 692 693 694 695
{
    unsigned iold, inew;
    FlatRange *frold, *frnew;

    /* Generate a symmetric difference of the old and new memory maps.
     * Kill ranges in the old map, and instantiate ranges in the new map.
     */
    iold = inew = 0;
696 697 698
    while (iold < old_view->nr || inew < new_view->nr) {
        if (iold < old_view->nr) {
            frold = &old_view->ranges[iold];
Avi Kivity's avatar
Avi Kivity committed
699 700 701
        } else {
            frold = NULL;
        }
702 703
        if (inew < new_view->nr) {
            frnew = &new_view->ranges[inew];
Avi Kivity's avatar
Avi Kivity committed
704 705 706 707 708 709
        } else {
            frnew = NULL;
        }

        if (frold
            && (!frnew
710 711
                || int128_lt(frold->addr.start, frnew->addr.start)
                || (int128_eq(frold->addr.start, frnew->addr.start)
Avi Kivity's avatar
Avi Kivity committed
712
                    && !flatrange_equal(frold, frnew)))) {
713
            /* In old but not in new, or in both but attributes changed. */
Avi Kivity's avatar
Avi Kivity committed
714

715
            if (!adding) {
716
                MEMORY_LISTENER_UPDATE_REGION(frold, as, Reverse, region_del);
717 718
            }

Avi Kivity's avatar
Avi Kivity committed
719 720
            ++iold;
        } else if (frold && frnew && flatrange_equal(frold, frnew)) {
721
            /* In both and unchanged (except logging may have changed) */
Avi Kivity's avatar
Avi Kivity committed
722

723
            if (adding) {
724
                MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_nop);
725
                if (frold->dirty_log_mask && !frnew->dirty_log_mask) {
726
                    MEMORY_LISTENER_UPDATE_REGION(frnew, as, Reverse, log_stop);
727
                } else if (frnew->dirty_log_mask && !frold->dirty_log_mask) {
728
                    MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, log_start);
729
                }
730 731
            }

Avi Kivity's avatar
Avi Kivity committed
732 733 734 735 736
            ++iold;
            ++inew;
        } else {
            /* In new */

737
            if (adding) {
738
                MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_add);
739 740
            }

Avi Kivity's avatar
Avi Kivity committed
741 742 743
            ++inew;
        }
    }
744 745 746 747 748
}


static void address_space_update_topology(AddressSpace *as)
{
749
    FlatView *old_view = address_space_get_flatview(as);
750
    FlatView *new_view = generate_memory_topology(as->root);
751 752 753 754

    address_space_update_topology_pass(as, old_view, new_view, false);
    address_space_update_topology_pass(as, old_view, new_view, true);

755 756
    qemu_mutex_lock(&flat_view_mutex);
    flatview_unref(as->current_map);
757
    as->current_map = new_view;
758 759 760 761 762 763 764 765 766 767
    qemu_mutex_unlock(&flat_view_mutex);

    /* Note that all the old MemoryRegions are still alive up to this
     * point.  This relieves most MemoryListeners from the need to
     * ref/unref the MemoryRegions they get---unless they use them
     * outside the iothread mutex, in which case precise reference
     * counting is necessary.
     */
    flatview_unref(old_view);

Avi Kivity's avatar
Avi Kivity committed
768
    address_space_update_ioeventfds(as);
Avi Kivity's avatar
Avi Kivity committed
769 770
}

Avi Kivity's avatar
Avi Kivity committed
771 772
void memory_region_transaction_begin(void)
{
773
    qemu_flush_coalesced_mmio_buffer();
Avi Kivity's avatar
Avi Kivity committed
774 775 776 777 778
    ++memory_region_transaction_depth;
}

void memory_region_transaction_commit(void)
{
779 780
    AddressSpace *as;

Avi Kivity's avatar
Avi Kivity committed
781 782
    assert(memory_region_transaction_depth);
    --memory_region_transaction_depth;
783 784
    if (!memory_region_transaction_depth && memory_region_update_pending) {
        memory_region_update_pending = false;
785 786
        MEMORY_LISTENER_CALL_GLOBAL(begin, Forward);

787 788
        QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
            address_space_update_topology(as);
789 790 791
        }

        MEMORY_LISTENER_CALL_GLOBAL(commit, Forward);
792
    }
Avi Kivity's avatar
Avi Kivity committed
793 794
}

795 796 797 798 799 800 801 802 803
static void memory_region_destructor_none(MemoryRegion *mr)
{
}

static void memory_region_destructor_ram(MemoryRegion *mr)
{
    qemu_ram_free(mr->ram_addr);
}

Paolo Bonzini's avatar
Paolo Bonzini committed
804 805 806 807 808
static void memory_region_destructor_alias(MemoryRegion *mr)
{
    memory_region_unref(mr->alias);
}

809 810 811 812 813
static void memory_region_destructor_ram_from_ptr(MemoryRegion *mr)
{
    qemu_ram_free_from_ptr(mr->ram_addr);
}

814 815 816 817 818
static void memory_region_destructor_rom_device(MemoryRegion *mr)
{
    qemu_ram_free(mr->ram_addr & TARGET_PAGE_MASK);
}

Avi Kivity's avatar
Avi Kivity committed
819
void memory_region_init(MemoryRegion *mr,
820
                        Object *owner,
Avi Kivity's avatar
Avi Kivity committed
821 822 823
                        const char *name,
                        uint64_t size)
{
824 825
    mr->ops = &unassigned_mem_ops;
    mr->opaque = NULL;
826
    mr->owner = owner;
Avi Kivity's avatar
Avi Kivity committed
827
    mr->iommu_ops = NULL;
Avi Kivity's avatar
Avi Kivity committed
828
    mr->parent = NULL;
829 830 831 832
    mr->size = int128_make64(size);
    if (size == UINT64_MAX) {
        mr->size = int128_2_64();
    }
Avi Kivity's avatar
Avi Kivity committed
833
    mr->addr = 0;
Avi Kivity's avatar
Avi Kivity committed
834
    mr->subpage = false;
835
    mr->enabled = true;
836
    mr->terminates = false;
837
    mr->ram = false;
838
    mr->romd_mode = true;
839
    mr->readonly = false;
Avi Kivity's avatar
Avi Kivity committed
840
    mr->rom_device = false;
841
    mr->destructor = memory_region_destructor_none;
Avi Kivity's avatar
Avi Kivity committed
842 843 844 845 846 847
    mr->priority = 0;
    mr->may_overlap = false;
    mr->alias = NULL;
    QTAILQ_INIT(&mr->subregions);
    memset(&mr->subregions_link, 0, sizeof mr->subregions_link);
    QTAILQ_INIT(&mr->coalesced);
848
    mr->name = g_strdup(name);
849
    mr->dirty_log_mask = 0;
Avi Kivity's avatar
Avi Kivity committed
850 851
    mr->ioeventfd_nb = 0;
    mr->ioeventfds = NULL;
852
    mr->flush_coalesced_mmio = false;
Avi Kivity's avatar
Avi Kivity committed
853 854
}

855 856 857 858 859 860
static uint64_t unassigned_mem_read(void *opaque, hwaddr addr,
                                    unsigned size)
{
#ifdef DEBUG_UNASSIGNED
    printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
#endif
861 862
    if (current_cpu != NULL) {
        cpu_unassigned_access(current_cpu, addr, false, false, 0, size);
863
    }
864
    return -1ULL;
865 866 867 868 869 870 871 872
}

static void unassigned_mem_write(void *opaque, hwaddr addr,
                                 uint64_t val, unsigned size)
{
#ifdef DEBUG_UNASSIGNED
    printf("Unassigned mem write " TARGET_FMT_plx " = 0x%"PRIx64"\n", addr, val);
#endif
873 874
    if (current_cpu != NULL) {
        cpu_unassigned_access(current_cpu, addr, true, false, 0, size);
875
    }
876 877
}

878 879 880 881 882 883 884 885 886 887 888
static bool unassigned_mem_accepts(void *opaque, hwaddr addr,
                                   unsigned size, bool is_write)
{
    return false;
}

const MemoryRegionOps unassigned_mem_ops = {
    .valid.accepts = unassigned_mem_accepts,
    .endianness = DEVICE_NATIVE_ENDIAN,
};

889 890 891 892
bool memory_region_access_valid(MemoryRegion *mr,
                                hwaddr addr,
                                unsigned size,
                                bool is_write)
Avi Kivity's avatar
Avi Kivity committed
893
{
894 895
    int access_size_min, access_size_max;
    int access_size, i;
896

Avi Kivity's avatar
Avi Kivity committed
897 898 899 900
    if (!mr->ops->valid.unaligned && (addr & (size - 1))) {
        return false;
    }

901
    if (!mr->ops->valid.accepts) {
Avi Kivity's avatar
Avi Kivity committed
902 903 904
        return true;
    }

905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
    access_size_min = mr->ops->valid.min_access_size;
    if (!mr->ops->valid.min_access_size) {
        access_size_min = 1;
    }

    access_size_max = mr->ops->valid.max_access_size;
    if (!mr->ops->valid.max_access_size) {
        access_size_max = 4;
    }

    access_size = MAX(MIN(size, access_size_max), access_size_min);
    for (i = 0; i < size; i += access_size) {
        if (!mr->ops->valid.accepts(mr->opaque, addr + i, access_size,
                                    is_write)) {
            return false;
        }
Avi Kivity's avatar
Avi Kivity committed
921
    }
922

Avi Kivity's avatar
Avi Kivity committed
923 924 925
    return true;
}

926
static uint64_t memory_region_dispatch_read1(MemoryRegion *mr,
927
                                             hwaddr addr,
928
                                             unsigned size)
Avi Kivity's avatar
Avi Kivity committed
929
{
930
    uint64_t data = 0;
Avi Kivity's avatar
Avi Kivity committed
931

932 933 934 935 936 937 938 939
    if (mr->ops->read) {
        access_with_adjusted_size(addr, &data, size,
                                  mr->ops->impl.min_access_size,
                                  mr->ops->impl.max_access_size,
                                  memory_region_read_accessor, mr);
    } else {
        access_with_adjusted_size(addr, &data, size, 1, 4,
                                  memory_region_oldmmio_read_accessor, mr);
940 941
    }

Avi Kivity's avatar
Avi Kivity committed
942 943 944
    return data;
}

945 946 947 948
static bool memory_region_dispatch_read(MemoryRegion *mr,
                                        hwaddr addr,
                                        uint64_t *pval,
                                        unsigned size)
949
{
950 951 952 953
    if (!memory_region_access_valid(mr, addr, size, false)) {
        *pval = unassigned_mem_read(mr, addr, size);
        return true;
    }
954

955 956 957
    *pval = memory_region_dispatch_read1(mr, addr, size);
    adjust_endianness(mr, pval, size);
    return false;
958
}
Avi Kivity's avatar
Avi Kivity committed
959

960
static bool memory_region_dispatch_write(MemoryRegion *mr,
961
                                         hwaddr addr,
962 963 964
                                         uint64_t data,
                                         unsigned size)
{
965
    if (!memory_region_access_valid(mr, addr, size, true)) {
966
        unassigned_mem_write(mr, addr, data, size);
967
        return true;
Avi Kivity's avatar
Avi Kivity committed
968 969
    }

970 971
    adjust_endianness(mr, &data, size);

972 973 974 975 976 977 978 979
    if (mr->ops->write) {
        access_with_adjusted_size(addr, &data, size,
                                  mr->ops->impl.min_access_size,
                                  mr->ops->impl.max_access_size,
                                  memory_region_write_accessor, mr);
    } else {
        access_with_adjusted_size(addr, &data, size, 1, 4,
                                  memory_region_oldmmio_write_accessor, mr);
980
    }
981
    return false;
Avi Kivity's avatar
Avi Kivity committed
982 983 984
}

void memory_region_init_io(MemoryRegion *mr,
985
                           Object *owner,
Avi Kivity's avatar
Avi Kivity committed
986 987 988 989 990
                           const MemoryRegionOps *ops,
                           void *opaque,
                           const char *name,
                           uint64_t size)
{
991
    memory_region_init(mr, owner, name, size);
Avi Kivity's avatar
Avi Kivity committed
992 993
    mr->ops = ops;
    mr->opaque = opaque;
994
    mr->terminates = true;
995
    mr->ram_addr = ~(ram_addr_t)0;
Avi Kivity's avatar
Avi Kivity committed
996 997 998
}

void memory_region_init_ram(MemoryRegion *mr,
999
                            Object *owner,
Avi Kivity's avatar
Avi Kivity committed
1000 1001 1002
                            const char *name,
                            uint64_t size)
{
1003
    memory_region_init(mr, owner, name, size);
1004
    mr->ram = true;
1005
    mr->terminates = true;
1006
    mr->destructor = memory_region_destructor_ram;
1007
    mr->ram_addr = qemu_ram_alloc(size, mr);
Avi Kivity's avatar
Avi Kivity committed
1008 1009 1010
}

void memory_region_init_ram_ptr(MemoryRegion *mr,
1011
                                Object *owner,
Avi Kivity's avatar
Avi Kivity committed
1012 1013 1014 1015
                                const char *name,
                                uint64_t size,
                                void *ptr)
{
1016
    memory_region_init(mr, owner, name, size);
1017
    mr->ram = true;
1018
    mr->terminates = true;
1019
    mr->destructor = memory_region_destructor_ram_from_ptr;
1020
    mr->ram_addr = qemu_ram_alloc_from_ptr(size, ptr, mr);
Avi Kivity's avatar
Avi Kivity committed
1021 1022 1023
}

void memory_region_init_alias(MemoryRegion *mr,
1024
                              Object *owner,
Avi Kivity's avatar
Avi Kivity committed
1025 1026
                              const char *name,
                              MemoryRegion *orig,
1027
                              hwaddr offset,
Avi Kivity's avatar
Avi Kivity committed
1028 1029
                              uint64_t size)
{
1030
    memory_region_init(mr, owner, name, size);
Paolo Bonzini's avatar
Paolo Bonzini committed
1031 1032
    memory_region_ref(orig);
    mr->destructor = memory_region_destructor_alias;
Avi Kivity's avatar
Avi Kivity committed
1033 1034 1035 1036
    mr->alias = orig;
    mr->alias_offset = offset;
}

1037
void memory_region_init_rom_device(MemoryRegion *mr,
1038
                                   Object *owner,
1039
                                   const MemoryRegionOps *ops,
1040
                                   void *opaque,
1041 1042 1043
                                   const char *name,
                                   uint64_t size)
{
1044
    memory_region_init(mr, owner, name, size);
1045
    mr->ops = ops;
1046
    mr->opaque = opaque;
1047
    mr->terminates = true;
Avi Kivity's avatar
Avi Kivity committed
1048
    mr->rom_device = true;
1049
    mr->destructor = memory_region_destructor_rom_device;
1050
    mr->ram_addr = qemu_ram_alloc(size, mr);
1051 1052
}

Avi Kivity's avatar
Avi Kivity committed
1053
void memory_region_init_iommu(MemoryRegion *mr,
1054
                              Object *owner,
Avi Kivity's avatar
Avi Kivity committed
1055 1056 1057 1058
                              const MemoryRegionIOMMUOps *ops,
                              const char *name,
                              uint64_t size)
{
1059
    memory_region_init(mr, owner, name, size);
Avi Kivity's avatar
Avi Kivity committed
1060 1061
    mr->iommu_ops = ops,
    mr->terminates = true;  /* then re-forwards */
1062
    notifier_list_init(&mr->iommu_notify);
Avi Kivity's avatar
Avi Kivity committed
1063 1064
}

1065
void memory_region_init_reservation(MemoryRegion *mr,
1066
                                    Object *owner,
1067 1068 1069
                                    const char *name,
                                    uint64_t size)
{
1070
    memory_region_init_io(mr, owner, &unassigned_mem_ops, mr, name, size);
1071 1072
}

Avi Kivity's avatar
Avi Kivity committed
1073 1074 1075
void memory_region_destroy(MemoryRegion *mr)
{
    assert(QTAILQ_EMPTY(&mr->subregions));
1076
    assert(memory_region_transaction_depth == 0);
1077
    mr->destructor(mr);
Avi Kivity's avatar
Avi Kivity committed
1078
    memory_region_clear_coalescing(mr);
1079 1080
    g_free((char *)mr->name);
    g_free(mr->ioeventfds);
Avi Kivity's avatar
Avi Kivity committed
1081 1082
}

Paolo Bonzini's avatar
Paolo Bonzini committed
1083 1084 1085 1086 1087
Object *memory_region_owner(MemoryRegion *mr)
{
    return mr->owner;
}

Paolo Bonzini's avatar
Paolo Bonzini committed
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
void memory_region_ref(MemoryRegion *mr)
{
    if (mr && mr->owner) {
        object_ref(mr->owner);
    }
}

void memory_region_unref(MemoryRegion *mr)
{
    if (mr && mr->owner) {
        object_unref(mr->owner);
    }
}

Avi Kivity's avatar
Avi Kivity committed
1102 1103
uint64_t memory_region_size(MemoryRegion *mr)
{
1104 1105 1106 1107
    if (int128_eq(mr->size, int128_2_64())) {
        return UINT64_MAX;
    }
    return int128_get64(mr->size);
Avi Kivity's avatar
Avi Kivity committed
1108 1109
}

1110 1111 1112 1113 1114
const char *memory_region_name(MemoryRegion *mr)
{
    return mr->name;
}

1115 1116 1117 1118 1119
bool memory_region_is_ram(MemoryRegion *mr)
{
    return mr->ram;
}

1120 1121 1122 1123 1124
bool memory_region_is_logging(MemoryRegion *mr)
{
    return mr->dirty_log_mask;
}

1125 1126 1127 1128 1129
bool memory_region_is_rom(MemoryRegion *mr)
{
    return mr->ram && mr->readonly;
}

Avi Kivity's avatar
Avi Kivity committed
1130 1131 1132 1133 1134
bool memory_region_is_iommu(MemoryRegion *mr)
{
    return mr->iommu_ops;
}

1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n)
{
    notifier_list_add(&mr->iommu_notify, n);
}

void memory_region_unregister_iommu_notifier(Notifier *n)
{
    notifier_remove(n);
}

void memory_region_notify_iommu(MemoryRegion *mr,
                                IOMMUTLBEntry entry)
{
    assert(memory_region_is_iommu(mr));
    notifier_list_notify(&mr->iommu_notify, &entry);
}

Avi Kivity's avatar
Avi Kivity committed
1152 1153
void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client)
{
1154 1155
    uint8_t mask = 1 << client;

1156
    memory_region_transaction_begin();
1157
    mr->dirty_log_mask = (mr->dirty_log_mask & ~mask) | (log * mask);
1158
    memory_region_update_pending |= mr->enabled;
1159
    memory_region_transaction_commit();
Avi Kivity's avatar
Avi Kivity committed
1160 1161
}

1162 1163
bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
                             hwaddr size, unsigned client)
Avi Kivity's avatar
Avi Kivity committed
1164
{
1165
    assert(mr->terminates);
1166 1167
    return cpu_physical_memory_get_dirty(mr->ram_addr + addr, size,
                                         1 << client);
Avi Kivity's avatar
Avi Kivity committed
1168 1169
}

1170 1171
void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
                             hwaddr size)
Avi Kivity's avatar
Avi Kivity committed
1172
{
1173
    assert(mr->terminates);
1174
    return cpu_physical_memory_set_dirty_range(mr->ram_addr + addr, size, -1);
Avi Kivity's avatar
Avi Kivity committed
1175 1176
}

1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr,
                                        hwaddr size, unsigned client)
{
    bool ret;
    assert(mr->terminates);
    ret = cpu_physical_memory_get_dirty(mr->ram_addr + addr, size,
                                        1 << client);
    if (ret) {
        cpu_physical_memory_reset_dirty(mr->ram_addr + addr,
                                        mr->ram_addr + addr + size,
                                        1 << client);
    }
    return ret;
}


Avi Kivity's avatar