File: | memory.c |
Location: | line 1696, column 9 |
Description: | Dereference of null pointer |
1 | /* | |||||
2 | * Physical memory management | |||||
3 | * | |||||
4 | * Copyright 2011 Red Hat, Inc. and/or its affiliates | |||||
5 | * | |||||
6 | * Authors: | |||||
7 | * Avi Kivity <avi@redhat.com> | |||||
8 | * | |||||
9 | * This work is licensed under the terms of the GNU GPL, version 2. See | |||||
10 | * the COPYING file in the top-level directory. | |||||
11 | * | |||||
12 | * Contributions after 2012-01-13 are licensed under the terms of the | |||||
13 | * GNU GPL, version 2 or (at your option) any later version. | |||||
14 | */ | |||||
15 | ||||||
16 | #include "exec/memory.h" | |||||
17 | #include "exec/address-spaces.h" | |||||
18 | #include "exec/ioport.h" | |||||
19 | #include "qemu/bitops.h" | |||||
20 | #include "qom/object.h" | |||||
21 | #include "trace.h" | |||||
22 | #include <assert.h> | |||||
23 | ||||||
24 | #include "exec/memory-internal.h" | |||||
25 | ||||||
26 | //#define DEBUG_UNASSIGNED | |||||
27 | ||||||
28 | static unsigned memory_region_transaction_depth; | |||||
29 | static bool_Bool memory_region_update_pending; | |||||
30 | static bool_Bool global_dirty_log = false0; | |||||
31 | ||||||
32 | /* flat_view_mutex is taken around reading as->current_map; the critical | |||||
33 | * section is extremely short, so I'm using a single mutex for every AS. | |||||
34 | * We could also RCU for the read-side. | |||||
35 | * | |||||
36 | * The BQL is taken around transaction commits, hence both locks are taken | |||||
37 | * while writing to as->current_map (with the BQL taken outside). | |||||
38 | */ | |||||
39 | static QemuMutex flat_view_mutex; | |||||
40 | ||||||
41 | static QTAILQ_HEAD(memory_listeners, MemoryListener)struct memory_listeners { struct MemoryListener *tqh_first; struct MemoryListener * *tqh_last; } memory_listeners | |||||
42 | = QTAILQ_HEAD_INITIALIZER(memory_listeners){ ((void*)0), &(memory_listeners).tqh_first }; | |||||
43 | ||||||
44 | static QTAILQ_HEAD(, AddressSpace)struct { struct AddressSpace *tqh_first; struct AddressSpace * *tqh_last; } address_spaces | |||||
45 | = QTAILQ_HEAD_INITIALIZER(address_spaces){ ((void*)0), &(address_spaces).tqh_first }; | |||||
46 | ||||||
47 | static void memory_init(void) | |||||
48 | { | |||||
49 | qemu_mutex_init(&flat_view_mutex); | |||||
50 | } | |||||
51 | ||||||
52 | typedef struct AddrRange AddrRange; | |||||
53 | ||||||
54 | /* | |||||
55 | * Note using signed integers limits us to physical addresses at most | |||||
56 | * 63 bits wide. They are needed for negative offsetting in aliases | |||||
57 | * (large MemoryRegion::alias_offset). | |||||
58 | */ | |||||
59 | struct AddrRange { | |||||
60 | Int128 start; | |||||
61 | Int128 size; | |||||
62 | }; | |||||
63 | ||||||
64 | static AddrRange addrrange_make(Int128 start, Int128 size) | |||||
65 | { | |||||
66 | return (AddrRange) { start, size }; | |||||
67 | } | |||||
68 | ||||||
69 | static bool_Bool addrrange_equal(AddrRange r1, AddrRange r2) | |||||
70 | { | |||||
71 | return int128_eq(r1.start, r2.start) && int128_eq(r1.size, r2.size); | |||||
72 | } | |||||
73 | ||||||
74 | static Int128 addrrange_end(AddrRange r) | |||||
75 | { | |||||
76 | return int128_add(r.start, r.size); | |||||
77 | } | |||||
78 | ||||||
79 | static AddrRange addrrange_shift(AddrRange range, Int128 delta) | |||||
80 | { | |||||
81 | int128_addto(&range.start, delta); | |||||
82 | return range; | |||||
83 | } | |||||
84 | ||||||
85 | static bool_Bool addrrange_contains(AddrRange range, Int128 addr) | |||||
86 | { | |||||
87 | return int128_ge(addr, range.start) | |||||
88 | && int128_lt(addr, addrrange_end(range)); | |||||
89 | } | |||||
90 | ||||||
91 | static bool_Bool addrrange_intersects(AddrRange r1, AddrRange r2) | |||||
92 | { | |||||
93 | return addrrange_contains(r1, r2.start) | |||||
94 | || addrrange_contains(r2, r1.start); | |||||
95 | } | |||||
96 | ||||||
97 | static AddrRange addrrange_intersection(AddrRange r1, AddrRange r2) | |||||
98 | { | |||||
99 | Int128 start = int128_max(r1.start, r2.start); | |||||
100 | Int128 end = int128_min(addrrange_end(r1), addrrange_end(r2)); | |||||
101 | return addrrange_make(start, int128_sub(end, start)); | |||||
102 | } | |||||
103 | ||||||
104 | enum ListenerDirection { Forward, Reverse }; | |||||
105 | ||||||
106 | static bool_Bool memory_listener_match(MemoryListener *listener, | |||||
107 | MemoryRegionSection *section) | |||||
108 | { | |||||
109 | return !listener->address_space_filter | |||||
110 | || listener->address_space_filter == section->address_space; | |||||
111 | } | |||||
112 | ||||||
113 | #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...)do { MemoryListener *_listener; switch (_direction) { case Forward : for ((_listener) = ((&memory_listeners)->tqh_first); (_listener); (_listener) = ((_listener)->link.tqe_next)) { if (_listener->_callback) { _listener->_callback(_listener , _args...); } } break; case Reverse: for ((_listener) = (*(( (struct memory_listeners *)((&memory_listeners)->tqh_last ))->tqh_last)); (_listener); (_listener) = (*(((struct memory_listeners *)((_listener)->link.tqe_prev))->tqh_last))) { if (_listener ->_callback) { _listener->_callback(_listener, _args... ); } } break; default: abort(); } } while (0) \ | |||||
114 | do { \ | |||||
115 | MemoryListener *_listener; \ | |||||
116 | \ | |||||
117 | switch (_direction) { \ | |||||
118 | case Forward: \ | |||||
119 | QTAILQ_FOREACH(_listener, &memory_listeners, link)for ((_listener) = ((&memory_listeners)->tqh_first); ( _listener); (_listener) = ((_listener)->link.tqe_next)) { \ | |||||
120 | if (_listener->_callback) { \ | |||||
121 | _listener->_callback(_listener, ##_args); \ | |||||
122 | } \ | |||||
123 | } \ | |||||
124 | break; \ | |||||
125 | case Reverse: \ | |||||
126 | QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \for ((_listener) = (*(((struct memory_listeners *)((&memory_listeners )->tqh_last))->tqh_last)); (_listener); (_listener) = ( *(((struct memory_listeners *)((_listener)->link.tqe_prev) )->tqh_last))) | |||||
127 | memory_listeners, link)for ((_listener) = (*(((struct memory_listeners *)((&memory_listeners )->tqh_last))->tqh_last)); (_listener); (_listener) = ( *(((struct memory_listeners *)((_listener)->link.tqe_prev) )->tqh_last))) { \ | |||||
128 | if (_listener->_callback) { \ | |||||
129 | _listener->_callback(_listener, ##_args); \ | |||||
130 | } \ | |||||
131 | } \ | |||||
132 | break; \ | |||||
133 | default: \ | |||||
134 | abort(); \ | |||||
135 | } \ | |||||
136 | } while (0) | |||||
137 | ||||||
138 | #define MEMORY_LISTENER_CALL(_callback, _direction, _section, _args...)do { MemoryListener *_listener; switch (_direction) { case Forward : for ((_listener) = ((&memory_listeners)->tqh_first); (_listener); (_listener) = ((_listener)->link.tqe_next)) { if (_listener->_callback && memory_listener_match (_listener, _section)) { _listener->_callback(_listener, _section , _args...); } } break; case Reverse: for ((_listener) = (*(( (struct memory_listeners *)((&memory_listeners)->tqh_last ))->tqh_last)); (_listener); (_listener) = (*(((struct memory_listeners *)((_listener)->link.tqe_prev))->tqh_last))) { if (_listener ->_callback && memory_listener_match(_listener, _section )) { _listener->_callback(_listener, _section, _args...); } } break; default: abort(); } } while (0) \ | |||||
139 | do { \ | |||||
140 | MemoryListener *_listener; \ | |||||
141 | \ | |||||
142 | switch (_direction) { \ | |||||
143 | case Forward: \ | |||||
144 | QTAILQ_FOREACH(_listener, &memory_listeners, link)for ((_listener) = ((&memory_listeners)->tqh_first); ( _listener); (_listener) = ((_listener)->link.tqe_next)) { \ | |||||
145 | if (_listener->_callback \ | |||||
146 | && memory_listener_match(_listener, _section)) { \ | |||||
147 | _listener->_callback(_listener, _section, ##_args); \ | |||||
148 | } \ | |||||
149 | } \ | |||||
150 | break; \ | |||||
151 | case Reverse: \ | |||||
152 | QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \for ((_listener) = (*(((struct memory_listeners *)((&memory_listeners )->tqh_last))->tqh_last)); (_listener); (_listener) = ( *(((struct memory_listeners *)((_listener)->link.tqe_prev) )->tqh_last))) | |||||
153 | memory_listeners, link)for ((_listener) = (*(((struct memory_listeners *)((&memory_listeners )->tqh_last))->tqh_last)); (_listener); (_listener) = ( *(((struct memory_listeners *)((_listener)->link.tqe_prev) )->tqh_last))) { \ | |||||
154 | if (_listener->_callback \ | |||||
155 | && memory_listener_match(_listener, _section)) { \ | |||||
156 | _listener->_callback(_listener, _section, ##_args); \ | |||||
157 | } \ | |||||
158 | } \ | |||||
159 | break; \ | |||||
160 | default: \ | |||||
161 | abort(); \ | |||||
162 | } \ | |||||
163 | } while (0) | |||||
164 | ||||||
165 | /* No need to ref/unref .mr, the FlatRange keeps it alive. */ | |||||
166 | #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback)do { MemoryListener *_listener; switch (dir) { case Forward: for ((_listener) = ((&memory_listeners)->tqh_first); (_listener ); (_listener) = ((_listener)->link.tqe_next)) { if (_listener ->callback && memory_listener_match(_listener, (& (MemoryRegionSection) { .mr = (fr)->mr, .address_space = ( as), .offset_within_region = (fr)->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64 ((fr)->addr.start), .readonly = (fr)->readonly, }))) { _listener ->callback(_listener, (&(MemoryRegionSection) { .mr = ( fr)->mr, .address_space = (as), .offset_within_region = (fr )->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64((fr)->addr.start), .readonly = (fr)->readonly , })); } } break; case Reverse: for ((_listener) = (*(((struct memory_listeners *)((&memory_listeners)->tqh_last))-> tqh_last)); (_listener); (_listener) = (*(((struct memory_listeners *)((_listener)->link.tqe_prev))->tqh_last))) { if (_listener ->callback && memory_listener_match(_listener, (& (MemoryRegionSection) { .mr = (fr)->mr, .address_space = ( as), .offset_within_region = (fr)->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64 ((fr)->addr.start), .readonly = (fr)->readonly, }))) { _listener ->callback(_listener, (&(MemoryRegionSection) { .mr = ( fr)->mr, .address_space = (as), .offset_within_region = (fr )->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64((fr)->addr.start), .readonly = (fr)->readonly , })); } } break; default: abort(); } } while (0) \ | |||||
167 | MEMORY_LISTENER_CALL(callback, dir, (&(MemoryRegionSection) { \do { MemoryListener *_listener; switch (dir) { case Forward: for ((_listener) = ((&memory_listeners)->tqh_first); (_listener ); (_listener) = ((_listener)->link.tqe_next)) { if (_listener ->callback && memory_listener_match(_listener, (& (MemoryRegionSection) { .mr = (fr)->mr, .address_space = ( as), .offset_within_region = (fr)->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64 ((fr)->addr.start), .readonly = (fr)->readonly, }))) { _listener ->callback(_listener, (&(MemoryRegionSection) { .mr = ( fr)->mr, .address_space = (as), .offset_within_region = (fr )->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64((fr)->addr.start), .readonly = (fr)->readonly , })); } } break; case Reverse: for ((_listener) = (*(((struct memory_listeners *)((&memory_listeners)->tqh_last))-> tqh_last)); (_listener); (_listener) = (*(((struct memory_listeners *)((_listener)->link.tqe_prev))->tqh_last))) { if (_listener ->callback && memory_listener_match(_listener, (& (MemoryRegionSection) { .mr = (fr)->mr, .address_space = ( as), .offset_within_region = (fr)->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64 ((fr)->addr.start), .readonly = (fr)->readonly, }))) { _listener ->callback(_listener, (&(MemoryRegionSection) { .mr = ( fr)->mr, .address_space = (as), .offset_within_region = (fr )->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64((fr)->addr.start), .readonly = (fr)->readonly , })); } } break; default: abort(); } } while (0) | |||||
168 | .mr = (fr)->mr, \do { MemoryListener *_listener; switch (dir) { case Forward: for ((_listener) = ((&memory_listeners)->tqh_first); (_listener ); (_listener) = ((_listener)->link.tqe_next)) { if (_listener ->callback && memory_listener_match(_listener, (& (MemoryRegionSection) { .mr = (fr)->mr, .address_space = ( as), .offset_within_region = (fr)->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64 ((fr)->addr.start), .readonly = (fr)->readonly, }))) { _listener ->callback(_listener, (&(MemoryRegionSection) { .mr = ( fr)->mr, .address_space = (as), .offset_within_region = (fr )->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64((fr)->addr.start), .readonly = (fr)->readonly , })); } } break; case Reverse: for ((_listener) = (*(((struct memory_listeners *)((&memory_listeners)->tqh_last))-> tqh_last)); (_listener); (_listener) = (*(((struct memory_listeners *)((_listener)->link.tqe_prev))->tqh_last))) { if (_listener ->callback && memory_listener_match(_listener, (& (MemoryRegionSection) { .mr = (fr)->mr, .address_space = ( as), .offset_within_region = (fr)->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64 ((fr)->addr.start), .readonly = (fr)->readonly, }))) { _listener ->callback(_listener, (&(MemoryRegionSection) { .mr = ( fr)->mr, .address_space = (as), .offset_within_region = (fr )->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64((fr)->addr.start), .readonly = (fr)->readonly , })); } } break; default: abort(); } } while (0) | |||||
169 | .address_space = (as), \do { MemoryListener *_listener; switch (dir) { case Forward: for ((_listener) = ((&memory_listeners)->tqh_first); (_listener ); (_listener) = ((_listener)->link.tqe_next)) { if (_listener ->callback && memory_listener_match(_listener, (& (MemoryRegionSection) { .mr = (fr)->mr, .address_space = ( as), .offset_within_region = (fr)->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64 ((fr)->addr.start), .readonly = (fr)->readonly, }))) { _listener ->callback(_listener, (&(MemoryRegionSection) { .mr = ( fr)->mr, .address_space = (as), .offset_within_region = (fr )->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64((fr)->addr.start), .readonly = (fr)->readonly , })); } } break; case Reverse: for ((_listener) = (*(((struct memory_listeners *)((&memory_listeners)->tqh_last))-> tqh_last)); (_listener); (_listener) = (*(((struct memory_listeners *)((_listener)->link.tqe_prev))->tqh_last))) { if (_listener ->callback && memory_listener_match(_listener, (& (MemoryRegionSection) { .mr = (fr)->mr, .address_space = ( as), .offset_within_region = (fr)->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64 ((fr)->addr.start), .readonly = (fr)->readonly, }))) { _listener ->callback(_listener, (&(MemoryRegionSection) { .mr = ( fr)->mr, .address_space = (as), .offset_within_region = (fr )->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64((fr)->addr.start), .readonly = (fr)->readonly , })); } } break; default: abort(); } } while (0) | |||||
170 | .offset_within_region = (fr)->offset_in_region, \do { MemoryListener *_listener; switch (dir) { case Forward: for ((_listener) = ((&memory_listeners)->tqh_first); (_listener ); (_listener) = ((_listener)->link.tqe_next)) { if (_listener ->callback && memory_listener_match(_listener, (& (MemoryRegionSection) { .mr = (fr)->mr, .address_space = ( as), .offset_within_region = (fr)->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64 ((fr)->addr.start), .readonly = (fr)->readonly, }))) { _listener ->callback(_listener, (&(MemoryRegionSection) { .mr = ( fr)->mr, .address_space = (as), .offset_within_region = (fr )->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64((fr)->addr.start), .readonly = (fr)->readonly , })); } } break; case Reverse: for ((_listener) = (*(((struct memory_listeners *)((&memory_listeners)->tqh_last))-> tqh_last)); (_listener); (_listener) = (*(((struct memory_listeners *)((_listener)->link.tqe_prev))->tqh_last))) { if (_listener ->callback && memory_listener_match(_listener, (& (MemoryRegionSection) { .mr = (fr)->mr, .address_space = ( as), .offset_within_region = (fr)->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64 ((fr)->addr.start), .readonly = (fr)->readonly, }))) { _listener ->callback(_listener, (&(MemoryRegionSection) { .mr = ( fr)->mr, .address_space = (as), .offset_within_region = (fr )->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64((fr)->addr.start), .readonly = (fr)->readonly , })); } } break; default: abort(); } } while (0) | |||||
171 | .size = (fr)->addr.size, \do { MemoryListener *_listener; switch (dir) { case Forward: for ((_listener) = ((&memory_listeners)->tqh_first); (_listener ); (_listener) = ((_listener)->link.tqe_next)) { if (_listener ->callback && memory_listener_match(_listener, (& (MemoryRegionSection) { .mr = (fr)->mr, .address_space = ( as), .offset_within_region = (fr)->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64 ((fr)->addr.start), .readonly = (fr)->readonly, }))) { _listener ->callback(_listener, (&(MemoryRegionSection) { .mr = ( fr)->mr, .address_space = (as), .offset_within_region = (fr )->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64((fr)->addr.start), .readonly = (fr)->readonly , })); } } break; case Reverse: for ((_listener) = (*(((struct memory_listeners *)((&memory_listeners)->tqh_last))-> tqh_last)); (_listener); (_listener) = (*(((struct memory_listeners *)((_listener)->link.tqe_prev))->tqh_last))) { if (_listener ->callback && memory_listener_match(_listener, (& (MemoryRegionSection) { .mr = (fr)->mr, .address_space = ( as), .offset_within_region = (fr)->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64 ((fr)->addr.start), .readonly = (fr)->readonly, }))) { _listener ->callback(_listener, (&(MemoryRegionSection) { .mr = ( fr)->mr, .address_space = (as), .offset_within_region = (fr )->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64((fr)->addr.start), .readonly = (fr)->readonly , })); } } break; default: abort(); } } while (0) | |||||
172 | .offset_within_address_space = int128_get64((fr)->addr.start), \do { MemoryListener *_listener; switch (dir) { case Forward: for ((_listener) = ((&memory_listeners)->tqh_first); (_listener ); (_listener) = ((_listener)->link.tqe_next)) { if (_listener ->callback && memory_listener_match(_listener, (& (MemoryRegionSection) { .mr = (fr)->mr, .address_space = ( as), .offset_within_region = (fr)->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64 ((fr)->addr.start), .readonly = (fr)->readonly, }))) { _listener ->callback(_listener, (&(MemoryRegionSection) { .mr = ( fr)->mr, .address_space = (as), .offset_within_region = (fr )->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64((fr)->addr.start), .readonly = (fr)->readonly , })); } } break; case Reverse: for ((_listener) = (*(((struct memory_listeners *)((&memory_listeners)->tqh_last))-> tqh_last)); (_listener); (_listener) = (*(((struct memory_listeners *)((_listener)->link.tqe_prev))->tqh_last))) { if (_listener ->callback && memory_listener_match(_listener, (& (MemoryRegionSection) { .mr = (fr)->mr, .address_space = ( as), .offset_within_region = (fr)->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64 ((fr)->addr.start), .readonly = (fr)->readonly, }))) { _listener ->callback(_listener, (&(MemoryRegionSection) { .mr = ( fr)->mr, .address_space = (as), .offset_within_region = (fr )->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64((fr)->addr.start), .readonly = (fr)->readonly , })); } } break; default: abort(); } } while (0) | |||||
173 | .readonly = (fr)->readonly, \do { MemoryListener *_listener; switch (dir) { case Forward: for ((_listener) = ((&memory_listeners)->tqh_first); (_listener ); (_listener) = ((_listener)->link.tqe_next)) { if (_listener ->callback && memory_listener_match(_listener, (& (MemoryRegionSection) { .mr = (fr)->mr, .address_space = ( as), .offset_within_region = (fr)->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64 ((fr)->addr.start), .readonly = (fr)->readonly, }))) { _listener ->callback(_listener, (&(MemoryRegionSection) { .mr = ( fr)->mr, .address_space = (as), .offset_within_region = (fr )->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64((fr)->addr.start), .readonly = (fr)->readonly , })); } } break; case Reverse: for ((_listener) = (*(((struct memory_listeners *)((&memory_listeners)->tqh_last))-> tqh_last)); (_listener); (_listener) = (*(((struct memory_listeners *)((_listener)->link.tqe_prev))->tqh_last))) { if (_listener ->callback && memory_listener_match(_listener, (& (MemoryRegionSection) { .mr = (fr)->mr, .address_space = ( as), .offset_within_region = (fr)->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64 ((fr)->addr.start), .readonly = (fr)->readonly, }))) { _listener ->callback(_listener, (&(MemoryRegionSection) { .mr = ( fr)->mr, .address_space = (as), .offset_within_region = (fr )->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64((fr)->addr.start), .readonly = (fr)->readonly , })); } } break; default: abort(); } } while (0) | |||||
174 | }))do { MemoryListener *_listener; switch (dir) { case Forward: for ((_listener) = ((&memory_listeners)->tqh_first); (_listener ); (_listener) = ((_listener)->link.tqe_next)) { if (_listener ->callback && memory_listener_match(_listener, (& (MemoryRegionSection) { .mr = (fr)->mr, .address_space = ( as), .offset_within_region = (fr)->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64 ((fr)->addr.start), .readonly = (fr)->readonly, }))) { _listener ->callback(_listener, (&(MemoryRegionSection) { .mr = ( fr)->mr, .address_space = (as), .offset_within_region = (fr )->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64((fr)->addr.start), .readonly = (fr)->readonly , })); } } break; case Reverse: for ((_listener) = (*(((struct memory_listeners *)((&memory_listeners)->tqh_last))-> tqh_last)); (_listener); (_listener) = (*(((struct memory_listeners *)((_listener)->link.tqe_prev))->tqh_last))) { if (_listener ->callback && memory_listener_match(_listener, (& (MemoryRegionSection) { .mr = (fr)->mr, .address_space = ( as), .offset_within_region = (fr)->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64 ((fr)->addr.start), .readonly = (fr)->readonly, }))) { _listener ->callback(_listener, (&(MemoryRegionSection) { .mr = ( fr)->mr, .address_space = (as), .offset_within_region = (fr )->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64((fr)->addr.start), .readonly = (fr)->readonly , })); } } break; default: abort(); } } while (0) | |||||
175 | ||||||
176 | struct CoalescedMemoryRange { | |||||
177 | AddrRange addr; | |||||
178 | QTAILQ_ENTRY(CoalescedMemoryRange)struct { struct CoalescedMemoryRange *tqe_next; struct CoalescedMemoryRange * *tqe_prev; } link; | |||||
179 | }; | |||||
180 | ||||||
181 | struct MemoryRegionIoeventfd { | |||||
182 | AddrRange addr; | |||||
183 | bool_Bool match_data; | |||||
184 | uint64_t data; | |||||
185 | EventNotifier *e; | |||||
186 | }; | |||||
187 | ||||||
188 | static bool_Bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a, | |||||
189 | MemoryRegionIoeventfd b) | |||||
190 | { | |||||
191 | if (int128_lt(a.addr.start, b.addr.start)) { | |||||
192 | return true1; | |||||
193 | } else if (int128_gt(a.addr.start, b.addr.start)) { | |||||
194 | return false0; | |||||
195 | } else if (int128_lt(a.addr.size, b.addr.size)) { | |||||
196 | return true1; | |||||
197 | } else if (int128_gt(a.addr.size, b.addr.size)) { | |||||
198 | return false0; | |||||
199 | } else if (a.match_data < b.match_data) { | |||||
200 | return true1; | |||||
201 | } else if (a.match_data > b.match_data) { | |||||
202 | return false0; | |||||
203 | } else if (a.match_data) { | |||||
204 | if (a.data < b.data) { | |||||
205 | return true1; | |||||
206 | } else if (a.data > b.data) { | |||||
207 | return false0; | |||||
208 | } | |||||
209 | } | |||||
210 | if (a.e < b.e) { | |||||
211 | return true1; | |||||
212 | } else if (a.e > b.e) { | |||||
213 | return false0; | |||||
214 | } | |||||
215 | return false0; | |||||
216 | } | |||||
217 | ||||||
218 | static bool_Bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a, | |||||
219 | MemoryRegionIoeventfd b) | |||||
220 | { | |||||
221 | return !memory_region_ioeventfd_before(a, b) | |||||
222 | && !memory_region_ioeventfd_before(b, a); | |||||
223 | } | |||||
224 | ||||||
225 | typedef struct FlatRange FlatRange; | |||||
226 | typedef struct FlatView FlatView; | |||||
227 | ||||||
228 | /* Range of memory in the global map. Addresses are absolute. */ | |||||
229 | struct FlatRange { | |||||
230 | MemoryRegion *mr; | |||||
231 | hwaddr offset_in_region; | |||||
232 | AddrRange addr; | |||||
233 | uint8_t dirty_log_mask; | |||||
234 | bool_Bool romd_mode; | |||||
235 | bool_Bool readonly; | |||||
236 | }; | |||||
237 | ||||||
238 | /* Flattened global view of current active memory hierarchy. Kept in sorted | |||||
239 | * order. | |||||
240 | */ | |||||
241 | struct FlatView { | |||||
242 | unsigned ref; | |||||
243 | FlatRange *ranges; | |||||
244 | unsigned nr; | |||||
245 | unsigned nr_allocated; | |||||
246 | }; | |||||
247 | ||||||
248 | typedef struct AddressSpaceOps AddressSpaceOps; | |||||
249 | ||||||
250 | #define FOR_EACH_FLAT_RANGE(var, view)for (var = (view)->ranges; var < (view)->ranges + (view )->nr; ++var) \ | |||||
251 | for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var) | |||||
252 | ||||||
253 | static bool_Bool flatrange_equal(FlatRange *a, FlatRange *b) | |||||
254 | { | |||||
255 | return a->mr == b->mr | |||||
256 | && 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; | |||||
260 | } | |||||
261 | ||||||
262 | static void flatview_init(FlatView *view) | |||||
263 | { | |||||
264 | view->ref = 1; | |||||
265 | view->ranges = NULL((void*)0); | |||||
266 | view->nr = 0; | |||||
267 | view->nr_allocated = 0; | |||||
268 | } | |||||
269 | ||||||
270 | /* Insert a range into a given position. Caller is responsible for maintaining | |||||
271 | * sorting order. | |||||
272 | */ | |||||
273 | static void flatview_insert(FlatView *view, unsigned pos, FlatRange *range) | |||||
274 | { | |||||
275 | if (view->nr == view->nr_allocated) { | |||||
276 | view->nr_allocated = MAX(2 * view->nr, 10)(((2 * view->nr) > (10)) ? (2 * view->nr) : (10)); | |||||
277 | view->ranges = g_realloc(view->ranges, | |||||
278 | view->nr_allocated * sizeof(*view->ranges)); | |||||
279 | } | |||||
280 | memmove(view->ranges + pos + 1, view->ranges + pos, | |||||
281 | (view->nr - pos) * sizeof(FlatRange)); | |||||
282 | view->ranges[pos] = *range; | |||||
283 | memory_region_ref(range->mr); | |||||
284 | ++view->nr; | |||||
285 | } | |||||
286 | ||||||
287 | static void flatview_destroy(FlatView *view) | |||||
288 | { | |||||
289 | int i; | |||||
290 | ||||||
291 | for (i = 0; i < view->nr; i++) { | |||||
292 | memory_region_unref(view->ranges[i].mr); | |||||
293 | } | |||||
294 | g_free(view->ranges); | |||||
295 | g_free(view); | |||||
296 | } | |||||
297 | ||||||
298 | static void flatview_ref(FlatView *view) | |||||
299 | { | |||||
300 | atomic_inc(&view->ref)((void) __sync_fetch_and_add(&view->ref, 1)); | |||||
301 | } | |||||
302 | ||||||
303 | static void flatview_unref(FlatView *view) | |||||
304 | { | |||||
305 | if (atomic_fetch_dec(&view->ref)__sync_fetch_and_add(&view->ref, -1) == 1) { | |||||
306 | flatview_destroy(view); | |||||
307 | } | |||||
308 | } | |||||
309 | ||||||
310 | static bool_Bool can_merge(FlatRange *r1, FlatRange *r2) | |||||
311 | { | |||||
312 | return int128_eq(addrrange_end(r1->addr), r2->addr.start) | |||||
313 | && r1->mr == r2->mr | |||||
314 | && int128_eq(int128_add(int128_make64(r1->offset_in_region), | |||||
315 | r1->addr.size), | |||||
316 | 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 | ||||||
322 | /* Attempt to simplify a view by merging adjacent ranges */ | |||||
323 | static void flatview_simplify(FlatView *view) | |||||
324 | { | |||||
325 | unsigned i, j; | |||||
326 | ||||||
327 | i = 0; | |||||
328 | while (i < view->nr) { | |||||
329 | j = i + 1; | |||||
330 | while (j < view->nr | |||||
331 | && can_merge(&view->ranges[j-1], &view->ranges[j])) { | |||||
332 | int128_addto(&view->ranges[i].addr.size, view->ranges[j].addr.size); | |||||
333 | ++j; | |||||
334 | } | |||||
335 | ++i; | |||||
336 | memmove(&view->ranges[i], &view->ranges[j], | |||||
337 | (view->nr - j) * sizeof(view->ranges[j])); | |||||
338 | view->nr -= j - i; | |||||
339 | } | |||||
340 | } | |||||
341 | ||||||
342 | static bool_Bool memory_region_big_endian(MemoryRegion *mr) | |||||
343 | { | |||||
344 | #ifdef TARGET_WORDS_BIGENDIAN | |||||
345 | return mr->ops->endianness != DEVICE_LITTLE_ENDIAN; | |||||
346 | #else | |||||
347 | return mr->ops->endianness == DEVICE_BIG_ENDIAN; | |||||
348 | #endif | |||||
349 | } | |||||
350 | ||||||
351 | static bool_Bool memory_region_wrong_endianness(MemoryRegion *mr) | |||||
352 | { | |||||
353 | #ifdef TARGET_WORDS_BIGENDIAN | |||||
354 | return mr->ops->endianness == DEVICE_LITTLE_ENDIAN; | |||||
355 | #else | |||||
356 | return mr->ops->endianness == DEVICE_BIG_ENDIAN; | |||||
357 | #endif | |||||
358 | } | |||||
359 | ||||||
360 | static void adjust_endianness(MemoryRegion *mr, uint64_t *data, unsigned size) | |||||
361 | { | |||||
362 | if (memory_region_wrong_endianness(mr)) { | |||||
363 | switch (size) { | |||||
364 | case 1: | |||||
365 | break; | |||||
366 | case 2: | |||||
367 | *data = bswap16(*data); | |||||
368 | break; | |||||
369 | case 4: | |||||
370 | *data = bswap32(*data); | |||||
371 | break; | |||||
372 | case 8: | |||||
373 | *data = bswap64(*data); | |||||
374 | break; | |||||
375 | default: | |||||
376 | abort(); | |||||
377 | } | |||||
378 | } | |||||
379 | } | |||||
380 | ||||||
381 | static void memory_region_oldmmio_read_accessor(MemoryRegion *mr, | |||||
382 | hwaddr addr, | |||||
383 | uint64_t *value, | |||||
384 | unsigned size, | |||||
385 | unsigned shift, | |||||
386 | uint64_t mask) | |||||
387 | { | |||||
388 | uint64_t tmp; | |||||
389 | ||||||
390 | tmp = mr->ops->old_mmio.read[ctz32(size)](mr->opaque, addr); | |||||
391 | trace_memory_region_ops_read(mr, addr, tmp, size); | |||||
392 | *value |= (tmp & mask) << shift; | |||||
393 | } | |||||
394 | ||||||
395 | static void memory_region_read_accessor(MemoryRegion *mr, | |||||
396 | hwaddr addr, | |||||
397 | uint64_t *value, | |||||
398 | unsigned size, | |||||
399 | unsigned shift, | |||||
400 | uint64_t mask) | |||||
401 | { | |||||
402 | uint64_t tmp; | |||||
403 | ||||||
404 | if (mr->flush_coalesced_mmio) { | |||||
405 | qemu_flush_coalesced_mmio_buffer(); | |||||
406 | } | |||||
407 | tmp = mr->ops->read(mr->opaque, addr, size); | |||||
408 | trace_memory_region_ops_read(mr, addr, tmp, size); | |||||
409 | *value |= (tmp & mask) << shift; | |||||
410 | } | |||||
411 | ||||||
412 | static void memory_region_oldmmio_write_accessor(MemoryRegion *mr, | |||||
413 | hwaddr addr, | |||||
414 | uint64_t *value, | |||||
415 | unsigned size, | |||||
416 | unsigned shift, | |||||
417 | uint64_t mask) | |||||
418 | { | |||||
419 | uint64_t tmp; | |||||
420 | ||||||
421 | tmp = (*value >> shift) & mask; | |||||
422 | trace_memory_region_ops_write(mr, addr, tmp, size); | |||||
423 | mr->ops->old_mmio.write[ctz32(size)](mr->opaque, addr, tmp); | |||||
424 | } | |||||
425 | ||||||
426 | static void memory_region_write_accessor(MemoryRegion *mr, | |||||
427 | hwaddr addr, | |||||
428 | uint64_t *value, | |||||
429 | unsigned size, | |||||
430 | unsigned shift, | |||||
431 | uint64_t mask) | |||||
432 | { | |||||
433 | uint64_t tmp; | |||||
434 | ||||||
435 | if (mr->flush_coalesced_mmio) { | |||||
436 | qemu_flush_coalesced_mmio_buffer(); | |||||
437 | } | |||||
438 | tmp = (*value >> shift) & mask; | |||||
439 | trace_memory_region_ops_write(mr, addr, tmp, size); | |||||
440 | mr->ops->write(mr->opaque, addr, tmp, size); | |||||
441 | } | |||||
442 | ||||||
443 | static void access_with_adjusted_size(hwaddr addr, | |||||
444 | uint64_t *value, | |||||
445 | unsigned size, | |||||
446 | unsigned access_size_min, | |||||
447 | unsigned access_size_max, | |||||
448 | void (*access)(MemoryRegion *mr, | |||||
449 | hwaddr addr, | |||||
450 | uint64_t *value, | |||||
451 | unsigned size, | |||||
452 | unsigned shift, | |||||
453 | uint64_t mask), | |||||
454 | MemoryRegion *mr) | |||||
455 | { | |||||
456 | uint64_t access_mask; | |||||
457 | unsigned access_size; | |||||
458 | unsigned i; | |||||
459 | ||||||
460 | if (!access_size_min) { | |||||
461 | access_size_min = 1; | |||||
462 | } | |||||
463 | if (!access_size_max) { | |||||
464 | access_size_max = 4; | |||||
465 | } | |||||
466 | ||||||
467 | /* FIXME: support unaligned access? */ | |||||
468 | access_size = MAX(MIN(size, access_size_max), access_size_min)((((((size) < (access_size_max)) ? (size) : (access_size_max ))) > (access_size_min)) ? ((((size) < (access_size_max )) ? (size) : (access_size_max))) : (access_size_min)); | |||||
469 | access_mask = -1ULL >> (64 - access_size * 8); | |||||
470 | if (memory_region_big_endian(mr)) { | |||||
471 | for (i = 0; i < size; i += access_size) { | |||||
472 | access(mr, addr + i, value, access_size, | |||||
473 | (size - access_size - i) * 8, access_mask); | |||||
474 | } | |||||
475 | } else { | |||||
476 | for (i = 0; i < size; i += access_size) { | |||||
477 | access(mr, addr + i, value, access_size, i * 8, access_mask); | |||||
478 | } | |||||
479 | } | |||||
480 | } | |||||
481 | ||||||
482 | static AddressSpace *memory_region_to_address_space(MemoryRegion *mr) | |||||
483 | { | |||||
484 | AddressSpace *as; | |||||
485 | ||||||
486 | while (mr->parent) { | |||||
487 | mr = mr->parent; | |||||
488 | } | |||||
489 | QTAILQ_FOREACH(as, &address_spaces, address_spaces_link)for ((as) = ((&address_spaces)->tqh_first); (as); (as) = ((as)->address_spaces_link.tqe_next)) { | |||||
490 | if (mr == as->root) { | |||||
491 | return as; | |||||
492 | } | |||||
493 | } | |||||
494 | abort(); | |||||
495 | } | |||||
496 | ||||||
497 | /* Render a memory region into the global view. Ranges in @view obscure | |||||
498 | * ranges in @mr. | |||||
499 | */ | |||||
500 | static void render_memory_region(FlatView *view, | |||||
501 | MemoryRegion *mr, | |||||
502 | Int128 base, | |||||
503 | AddrRange clip, | |||||
504 | bool_Bool readonly) | |||||
505 | { | |||||
506 | MemoryRegion *subregion; | |||||
507 | unsigned i; | |||||
508 | hwaddr offset_in_region; | |||||
509 | Int128 remain; | |||||
510 | Int128 now; | |||||
511 | FlatRange fr; | |||||
512 | AddrRange tmp; | |||||
513 | ||||||
514 | if (!mr->enabled) { | |||||
515 | return; | |||||
516 | } | |||||
517 | ||||||
518 | int128_addto(&base, int128_make64(mr->addr)); | |||||
519 | readonly |= mr->readonly; | |||||
520 | ||||||
521 | tmp = addrrange_make(base, mr->size); | |||||
522 | ||||||
523 | if (!addrrange_intersects(tmp, clip)) { | |||||
524 | return; | |||||
525 | } | |||||
526 | ||||||
527 | clip = addrrange_intersection(tmp, clip); | |||||
528 | ||||||
529 | if (mr->alias) { | |||||
530 | int128_subfrom(&base, int128_make64(mr->alias->addr)); | |||||
531 | int128_subfrom(&base, int128_make64(mr->alias_offset)); | |||||
532 | render_memory_region(view, mr->alias, base, clip, readonly); | |||||
533 | return; | |||||
534 | } | |||||
535 | ||||||
536 | /* Render subregions in priority order. */ | |||||
537 | QTAILQ_FOREACH(subregion, &mr->subregions, subregions_link)for ((subregion) = ((&mr->subregions)->tqh_first); ( subregion); (subregion) = ((subregion)->subregions_link.tqe_next )) { | |||||
538 | render_memory_region(view, subregion, base, clip, readonly); | |||||
539 | } | |||||
540 | ||||||
541 | if (!mr->terminates) { | |||||
542 | return; | |||||
543 | } | |||||
544 | ||||||
545 | offset_in_region = int128_get64(int128_sub(clip.start, base)); | |||||
546 | base = clip.start; | |||||
547 | remain = clip.size; | |||||
548 | ||||||
549 | fr.mr = mr; | |||||
550 | fr.dirty_log_mask = mr->dirty_log_mask; | |||||
551 | fr.romd_mode = mr->romd_mode; | |||||
552 | fr.readonly = readonly; | |||||
553 | ||||||
554 | /* Render the region itself into any gaps left by the current view. */ | |||||
555 | for (i = 0; i < view->nr && int128_nz(remain); ++i) { | |||||
556 | if (int128_ge(base, addrrange_end(view->ranges[i].addr))) { | |||||
557 | continue; | |||||
558 | } | |||||
559 | if (int128_lt(base, view->ranges[i].addr.start)) { | |||||
560 | now = int128_min(remain, | |||||
561 | int128_sub(view->ranges[i].addr.start, base)); | |||||
562 | fr.offset_in_region = offset_in_region; | |||||
563 | fr.addr = addrrange_make(base, now); | |||||
564 | flatview_insert(view, i, &fr); | |||||
565 | ++i; | |||||
566 | int128_addto(&base, now); | |||||
567 | offset_in_region += int128_get64(now); | |||||
568 | int128_subfrom(&remain, now); | |||||
569 | } | |||||
570 | now = int128_sub(int128_min(int128_add(base, remain), | |||||
571 | addrrange_end(view->ranges[i].addr)), | |||||
572 | base); | |||||
573 | int128_addto(&base, now); | |||||
574 | offset_in_region += int128_get64(now); | |||||
575 | int128_subfrom(&remain, now); | |||||
576 | } | |||||
577 | if (int128_nz(remain)) { | |||||
578 | fr.offset_in_region = offset_in_region; | |||||
579 | fr.addr = addrrange_make(base, remain); | |||||
580 | flatview_insert(view, i, &fr); | |||||
581 | } | |||||
582 | } | |||||
583 | ||||||
584 | /* Render a memory topology into a list of disjoint absolute ranges. */ | |||||
585 | static FlatView *generate_memory_topology(MemoryRegion *mr) | |||||
586 | { | |||||
587 | FlatView *view; | |||||
588 | ||||||
589 | view = g_new(FlatView, 1)((FlatView *) g_malloc_n ((1), sizeof (FlatView))); | |||||
590 | flatview_init(view); | |||||
591 | ||||||
592 | if (mr) { | |||||
593 | render_memory_region(view, mr, int128_zero(), | |||||
594 | addrrange_make(int128_zero(), int128_2_64()), false0); | |||||
595 | } | |||||
596 | flatview_simplify(view); | |||||
597 | ||||||
598 | return view; | |||||
599 | } | |||||
600 | ||||||
601 | static void address_space_add_del_ioeventfds(AddressSpace *as, | |||||
602 | MemoryRegionIoeventfd *fds_new, | |||||
603 | unsigned fds_new_nb, | |||||
604 | MemoryRegionIoeventfd *fds_old, | |||||
605 | unsigned fds_old_nb) | |||||
606 | { | |||||
607 | unsigned iold, inew; | |||||
608 | MemoryRegionIoeventfd *fd; | |||||
609 | MemoryRegionSection section; | |||||
610 | ||||||
611 | /* Generate a symmetric difference of the old and new fd sets, adding | |||||
612 | * and deleting as necessary. | |||||
613 | */ | |||||
614 | ||||||
615 | iold = inew = 0; | |||||
616 | while (iold < fds_old_nb || inew < fds_new_nb) { | |||||
617 | if (iold < fds_old_nb | |||||
618 | && (inew == fds_new_nb | |||||
619 | || memory_region_ioeventfd_before(fds_old[iold], | |||||
620 | fds_new[inew]))) { | |||||
621 | fd = &fds_old[iold]; | |||||
622 | section = (MemoryRegionSection) { | |||||
623 | .address_space = as, | |||||
624 | .offset_within_address_space = int128_get64(fd->addr.start), | |||||
625 | .size = fd->addr.size, | |||||
626 | }; | |||||
627 | MEMORY_LISTENER_CALL(eventfd_del, Forward, §ion,do { MemoryListener *_listener; switch (Forward) { case Forward : for ((_listener) = ((&memory_listeners)->tqh_first); (_listener); (_listener) = ((_listener)->link.tqe_next)) { if (_listener->eventfd_del && memory_listener_match (_listener, §ion)) { _listener->eventfd_del(_listener , §ion, fd->match_data, fd->data, fd->e); } } break; case Reverse: for ((_listener) = (*(((struct memory_listeners *)((&memory_listeners)->tqh_last))->tqh_last)); (_listener ); (_listener) = (*(((struct memory_listeners *)((_listener)-> link.tqe_prev))->tqh_last))) { if (_listener->eventfd_del && memory_listener_match(_listener, §ion)) { _listener->eventfd_del(_listener, §ion, fd->match_data , fd->data, fd->e); } } break; default: abort(); } } while (0) | |||||
628 | fd->match_data, fd->data, fd->e)do { MemoryListener *_listener; switch (Forward) { case Forward : for ((_listener) = ((&memory_listeners)->tqh_first); (_listener); (_listener) = ((_listener)->link.tqe_next)) { if (_listener->eventfd_del && memory_listener_match (_listener, §ion)) { _listener->eventfd_del(_listener , §ion, fd->match_data, fd->data, fd->e); } } break; case Reverse: for ((_listener) = (*(((struct memory_listeners *)((&memory_listeners)->tqh_last))->tqh_last)); (_listener ); (_listener) = (*(((struct memory_listeners *)((_listener)-> link.tqe_prev))->tqh_last))) { if (_listener->eventfd_del && memory_listener_match(_listener, §ion)) { _listener->eventfd_del(_listener, §ion, fd->match_data , fd->data, fd->e); } } break; default: abort(); } } while (0); | |||||
629 | ++iold; | |||||
630 | } else if (inew < fds_new_nb | |||||
631 | && (iold == fds_old_nb | |||||
632 | || memory_region_ioeventfd_before(fds_new[inew], | |||||
633 | fds_old[iold]))) { | |||||
634 | fd = &fds_new[inew]; | |||||
635 | section = (MemoryRegionSection) { | |||||
636 | .address_space = as, | |||||
637 | .offset_within_address_space = int128_get64(fd->addr.start), | |||||
638 | .size = fd->addr.size, | |||||
639 | }; | |||||
640 | MEMORY_LISTENER_CALL(eventfd_add, Reverse, §ion,do { MemoryListener *_listener; switch (Reverse) { case Forward : for ((_listener) = ((&memory_listeners)->tqh_first); (_listener); (_listener) = ((_listener)->link.tqe_next)) { if (_listener->eventfd_add && memory_listener_match (_listener, §ion)) { _listener->eventfd_add(_listener , §ion, fd->match_data, fd->data, fd->e); } } break; case Reverse: for ((_listener) = (*(((struct memory_listeners *)((&memory_listeners)->tqh_last))->tqh_last)); (_listener ); (_listener) = (*(((struct memory_listeners *)((_listener)-> link.tqe_prev))->tqh_last))) { if (_listener->eventfd_add && memory_listener_match(_listener, §ion)) { _listener->eventfd_add(_listener, §ion, fd->match_data , fd->data, fd->e); } } break; default: abort(); } } while (0) | |||||
641 | fd->match_data, fd->data, fd->e)do { MemoryListener *_listener; switch (Reverse) { case Forward : for ((_listener) = ((&memory_listeners)->tqh_first); (_listener); (_listener) = ((_listener)->link.tqe_next)) { if (_listener->eventfd_add && memory_listener_match (_listener, §ion)) { _listener->eventfd_add(_listener , §ion, fd->match_data, fd->data, fd->e); } } break; case Reverse: for ((_listener) = (*(((struct memory_listeners *)((&memory_listeners)->tqh_last))->tqh_last)); (_listener ); (_listener) = (*(((struct memory_listeners *)((_listener)-> link.tqe_prev))->tqh_last))) { if (_listener->eventfd_add && memory_listener_match(_listener, §ion)) { _listener->eventfd_add(_listener, §ion, fd->match_data , fd->data, fd->e); } } break; default: abort(); } } while (0); | |||||
642 | ++inew; | |||||
643 | } else { | |||||
644 | ++iold; | |||||
645 | ++inew; | |||||
646 | } | |||||
647 | } | |||||
648 | } | |||||
649 | ||||||
650 | static FlatView *address_space_get_flatview(AddressSpace *as) | |||||
651 | { | |||||
652 | FlatView *view; | |||||
653 | ||||||
654 | qemu_mutex_lock(&flat_view_mutex); | |||||
655 | view = as->current_map; | |||||
656 | flatview_ref(view); | |||||
657 | qemu_mutex_unlock(&flat_view_mutex); | |||||
658 | return view; | |||||
659 | } | |||||
660 | ||||||
661 | static void address_space_update_ioeventfds(AddressSpace *as) | |||||
662 | { | |||||
663 | FlatView *view; | |||||
664 | FlatRange *fr; | |||||
665 | unsigned ioeventfd_nb = 0; | |||||
666 | MemoryRegionIoeventfd *ioeventfds = NULL((void*)0); | |||||
667 | AddrRange tmp; | |||||
668 | unsigned i; | |||||
669 | ||||||
670 | view = address_space_get_flatview(as); | |||||
671 | FOR_EACH_FLAT_RANGE(fr, view)for (fr = (view)->ranges; fr < (view)->ranges + (view )->nr; ++fr) { | |||||
672 | for (i = 0; i < fr->mr->ioeventfd_nb; ++i) { | |||||
673 | tmp = addrrange_shift(fr->mr->ioeventfds[i].addr, | |||||
674 | int128_sub(fr->addr.start, | |||||
675 | int128_make64(fr->offset_in_region))); | |||||
676 | if (addrrange_intersects(fr->addr, tmp)) { | |||||
677 | ++ioeventfd_nb; | |||||
678 | ioeventfds = g_realloc(ioeventfds, | |||||
679 | ioeventfd_nb * sizeof(*ioeventfds)); | |||||
680 | ioeventfds[ioeventfd_nb-1] = fr->mr->ioeventfds[i]; | |||||
681 | ioeventfds[ioeventfd_nb-1].addr = tmp; | |||||
682 | } | |||||
683 | } | |||||
684 | } | |||||
685 | ||||||
686 | address_space_add_del_ioeventfds(as, ioeventfds, ioeventfd_nb, | |||||
687 | as->ioeventfds, as->ioeventfd_nb); | |||||
688 | ||||||
689 | g_free(as->ioeventfds); | |||||
690 | as->ioeventfds = ioeventfds; | |||||
691 | as->ioeventfd_nb = ioeventfd_nb; | |||||
692 | flatview_unref(view); | |||||
693 | } | |||||
694 | ||||||
695 | static void address_space_update_topology_pass(AddressSpace *as, | |||||
696 | const FlatView *old_view, | |||||
697 | const FlatView *new_view, | |||||
698 | bool_Bool adding) | |||||
699 | { | |||||
700 | unsigned iold, inew; | |||||
701 | FlatRange *frold, *frnew; | |||||
702 | ||||||
703 | /* Generate a symmetric difference of the old and new memory maps. | |||||
704 | * Kill ranges in the old map, and instantiate ranges in the new map. | |||||
705 | */ | |||||
706 | iold = inew = 0; | |||||
707 | while (iold < old_view->nr || inew < new_view->nr) { | |||||
708 | if (iold < old_view->nr) { | |||||
709 | frold = &old_view->ranges[iold]; | |||||
710 | } else { | |||||
711 | frold = NULL((void*)0); | |||||
712 | } | |||||
713 | if (inew < new_view->nr) { | |||||
714 | frnew = &new_view->ranges[inew]; | |||||
715 | } else { | |||||
716 | frnew = NULL((void*)0); | |||||
717 | } | |||||
718 | ||||||
719 | if (frold | |||||
720 | && (!frnew | |||||
721 | || int128_lt(frold->addr.start, frnew->addr.start) | |||||
722 | || (int128_eq(frold->addr.start, frnew->addr.start) | |||||
723 | && !flatrange_equal(frold, frnew)))) { | |||||
724 | /* In old but not in new, or in both but attributes changed. */ | |||||
725 | ||||||
726 | if (!adding) { | |||||
727 | MEMORY_LISTENER_UPDATE_REGION(frold, as, Reverse, region_del)do { MemoryListener *_listener; switch (Reverse) { case Forward : for ((_listener) = ((&memory_listeners)->tqh_first); (_listener); (_listener) = ((_listener)->link.tqe_next)) { if (_listener->region_del && memory_listener_match (_listener, (&(MemoryRegionSection) { .mr = (frold)->mr , .address_space = (as), .offset_within_region = (frold)-> offset_in_region, .size = (frold)->addr.size, .offset_within_address_space = int128_get64((frold)->addr.start), .readonly = (frold)-> readonly, }))) { _listener->region_del(_listener, (&(MemoryRegionSection ) { .mr = (frold)->mr, .address_space = (as), .offset_within_region = (frold)->offset_in_region, .size = (frold)->addr.size , .offset_within_address_space = int128_get64((frold)->addr .start), .readonly = (frold)->readonly, })); } } break; case Reverse: for ((_listener) = (*(((struct memory_listeners *)( (&memory_listeners)->tqh_last))->tqh_last)); (_listener ); (_listener) = (*(((struct memory_listeners *)((_listener)-> link.tqe_prev))->tqh_last))) { if (_listener->region_del && memory_listener_match(_listener, (&(MemoryRegionSection ) { .mr = (frold)->mr, .address_space = (as), .offset_within_region = (frold)->offset_in_region, .size = (frold)->addr.size , .offset_within_address_space = int128_get64((frold)->addr .start), .readonly = (frold)->readonly, }))) { _listener-> region_del(_listener, (&(MemoryRegionSection) { .mr = (frold )->mr, .address_space = (as), .offset_within_region = (frold )->offset_in_region, .size = (frold)->addr.size, .offset_within_address_space = int128_get64((frold)->addr.start), .readonly = (frold)-> readonly, })); } } break; default: abort(); } } while (0); | |||||
728 | } | |||||
729 | ||||||
730 | ++iold; | |||||
731 | } else if (frold && frnew && flatrange_equal(frold, frnew)) { | |||||
732 | /* In both and unchanged (except logging may have changed) */ | |||||
733 | ||||||
734 | if (adding) { | |||||
735 | MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_nop)do { MemoryListener *_listener; switch (Forward) { case Forward : for ((_listener) = ((&memory_listeners)->tqh_first); (_listener); (_listener) = ((_listener)->link.tqe_next)) { if (_listener->region_nop && memory_listener_match (_listener, (&(MemoryRegionSection) { .mr = (frnew)->mr , .address_space = (as), .offset_within_region = (frnew)-> offset_in_region, .size = (frnew)->addr.size, .offset_within_address_space = int128_get64((frnew)->addr.start), .readonly = (frnew)-> readonly, }))) { _listener->region_nop(_listener, (&(MemoryRegionSection ) { .mr = (frnew)->mr, .address_space = (as), .offset_within_region = (frnew)->offset_in_region, .size = (frnew)->addr.size , .offset_within_address_space = int128_get64((frnew)->addr .start), .readonly = (frnew)->readonly, })); } } break; case Reverse: for ((_listener) = (*(((struct memory_listeners *)( (&memory_listeners)->tqh_last))->tqh_last)); (_listener ); (_listener) = (*(((struct memory_listeners *)((_listener)-> link.tqe_prev))->tqh_last))) { if (_listener->region_nop && memory_listener_match(_listener, (&(MemoryRegionSection ) { .mr = (frnew)->mr, .address_space = (as), .offset_within_region = (frnew)->offset_in_region, .size = (frnew)->addr.size , .offset_within_address_space = int128_get64((frnew)->addr .start), .readonly = (frnew)->readonly, }))) { _listener-> region_nop(_listener, (&(MemoryRegionSection) { .mr = (frnew )->mr, .address_space = (as), .offset_within_region = (frnew )->offset_in_region, .size = (frnew)->addr.size, .offset_within_address_space = int128_get64((frnew)->addr.start), .readonly = (frnew)-> readonly, })); } } break; default: abort(); } } while (0); | |||||
736 | if (frold->dirty_log_mask && !frnew->dirty_log_mask) { | |||||
737 | MEMORY_LISTENER_UPDATE_REGION(frnew, as, Reverse, log_stop)do { MemoryListener *_listener; switch (Reverse) { case Forward : for ((_listener) = ((&memory_listeners)->tqh_first); (_listener); (_listener) = ((_listener)->link.tqe_next)) { if (_listener->log_stop && memory_listener_match( _listener, (&(MemoryRegionSection) { .mr = (frnew)->mr , .address_space = (as), .offset_within_region = (frnew)-> offset_in_region, .size = (frnew)->addr.size, .offset_within_address_space = int128_get64((frnew)->addr.start), .readonly = (frnew)-> readonly, }))) { _listener->log_stop(_listener, (&(MemoryRegionSection ) { .mr = (frnew)->mr, .address_space = (as), .offset_within_region = (frnew)->offset_in_region, .size = (frnew)->addr.size , .offset_within_address_space = int128_get64((frnew)->addr .start), .readonly = (frnew)->readonly, })); } } break; case Reverse: for ((_listener) = (*(((struct memory_listeners *)( (&memory_listeners)->tqh_last))->tqh_last)); (_listener ); (_listener) = (*(((struct memory_listeners *)((_listener)-> link.tqe_prev))->tqh_last))) { if (_listener->log_stop && memory_listener_match(_listener, (&(MemoryRegionSection) { .mr = (frnew)->mr, .address_space = (as), .offset_within_region = (frnew)->offset_in_region, .size = (frnew)->addr.size , .offset_within_address_space = int128_get64((frnew)->addr .start), .readonly = (frnew)->readonly, }))) { _listener-> log_stop(_listener, (&(MemoryRegionSection) { .mr = (frnew )->mr, .address_space = (as), .offset_within_region = (frnew )->offset_in_region, .size = (frnew)->addr.size, .offset_within_address_space = int128_get64((frnew)->addr.start), .readonly = (frnew)-> readonly, })); } } break; default: abort(); } } while (0); | |||||
738 | } else if (frnew->dirty_log_mask && !frold->dirty_log_mask) { | |||||
739 | MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, log_start)do { MemoryListener *_listener; switch (Forward) { case Forward : for ((_listener) = ((&memory_listeners)->tqh_first); (_listener); (_listener) = ((_listener)->link.tqe_next)) { if (_listener->log_start && memory_listener_match (_listener, (&(MemoryRegionSection) { .mr = (frnew)->mr , .address_space = (as), .offset_within_region = (frnew)-> offset_in_region, .size = (frnew)->addr.size, .offset_within_address_space = int128_get64((frnew)->addr.start), .readonly = (frnew)-> readonly, }))) { _listener->log_start(_listener, (&(MemoryRegionSection ) { .mr = (frnew)->mr, .address_space = (as), .offset_within_region = (frnew)->offset_in_region, .size = (frnew)->addr.size , .offset_within_address_space = int128_get64((frnew)->addr .start), .readonly = (frnew)->readonly, })); } } break; case Reverse: for ((_listener) = (*(((struct memory_listeners *)( (&memory_listeners)->tqh_last))->tqh_last)); (_listener ); (_listener) = (*(((struct memory_listeners *)((_listener)-> link.tqe_prev))->tqh_last))) { if (_listener->log_start && memory_listener_match(_listener, (&(MemoryRegionSection ) { .mr = (frnew)->mr, .address_space = (as), .offset_within_region = (frnew)->offset_in_region, .size = (frnew)->addr.size , .offset_within_address_space = int128_get64((frnew)->addr .start), .readonly = (frnew)->readonly, }))) { _listener-> log_start(_listener, (&(MemoryRegionSection) { .mr = (frnew )->mr, .address_space = (as), .offset_within_region = (frnew )->offset_in_region, .size = (frnew)->addr.size, .offset_within_address_space = int128_get64((frnew)->addr.start), .readonly = (frnew)-> readonly, })); } } break; default: abort(); } } while (0); | |||||
740 | } | |||||
741 | } | |||||
742 | ||||||
743 | ++iold; | |||||
744 | ++inew; | |||||
745 | } else { | |||||
746 | /* In new */ | |||||
747 | ||||||
748 | if (adding) { | |||||
749 | MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_add)do { MemoryListener *_listener; switch (Forward) { case Forward : for ((_listener) = ((&memory_listeners)->tqh_first); (_listener); (_listener) = ((_listener)->link.tqe_next)) { if (_listener->region_add && memory_listener_match (_listener, (&(MemoryRegionSection) { .mr = (frnew)->mr , .address_space = (as), .offset_within_region = (frnew)-> offset_in_region, .size = (frnew)->addr.size, .offset_within_address_space = int128_get64((frnew)->addr.start), .readonly = (frnew)-> readonly, }))) { _listener->region_add(_listener, (&(MemoryRegionSection ) { .mr = (frnew)->mr, .address_space = (as), .offset_within_region = (frnew)->offset_in_region, .size = (frnew)->addr.size , .offset_within_address_space = int128_get64((frnew)->addr .start), .readonly = (frnew)->readonly, })); } } break; case Reverse: for ((_listener) = (*(((struct memory_listeners *)( (&memory_listeners)->tqh_last))->tqh_last)); (_listener ); (_listener) = (*(((struct memory_listeners *)((_listener)-> link.tqe_prev))->tqh_last))) { if (_listener->region_add && memory_listener_match(_listener, (&(MemoryRegionSection ) { .mr = (frnew)->mr, .address_space = (as), .offset_within_region = (frnew)->offset_in_region, .size = (frnew)->addr.size , .offset_within_address_space = int128_get64((frnew)->addr .start), .readonly = (frnew)->readonly, }))) { _listener-> region_add(_listener, (&(MemoryRegionSection) { .mr = (frnew )->mr, .address_space = (as), .offset_within_region = (frnew )->offset_in_region, .size = (frnew)->addr.size, .offset_within_address_space = int128_get64((frnew)->addr.start), .readonly = (frnew)-> readonly, })); } } break; default: abort(); } } while (0); | |||||
750 | } | |||||
751 | ||||||
752 | ++inew; | |||||
753 | } | |||||
754 | } | |||||
755 | } | |||||
756 | ||||||
757 | ||||||
758 | static void address_space_update_topology(AddressSpace *as) | |||||
759 | { | |||||
760 | FlatView *old_view = address_space_get_flatview(as); | |||||
761 | FlatView *new_view = generate_memory_topology(as->root); | |||||
762 | ||||||
763 | address_space_update_topology_pass(as, old_view, new_view, false0); | |||||
764 | address_space_update_topology_pass(as, old_view, new_view, true1); | |||||
765 | ||||||
766 | qemu_mutex_lock(&flat_view_mutex); | |||||
767 | flatview_unref(as->current_map); | |||||
768 | as->current_map = new_view; | |||||
769 | qemu_mutex_unlock(&flat_view_mutex); | |||||
770 | ||||||
771 | /* Note that all the old MemoryRegions are still alive up to this | |||||
772 | * point. This relieves most MemoryListeners from the need to | |||||
773 | * ref/unref the MemoryRegions they get---unless they use them | |||||
774 | * outside the iothread mutex, in which case precise reference | |||||
775 | * counting is necessary. | |||||
776 | */ | |||||
777 | flatview_unref(old_view); | |||||
778 | ||||||
779 | address_space_update_ioeventfds(as); | |||||
780 | } | |||||
781 | ||||||
782 | void memory_region_transaction_begin(void) | |||||
783 | { | |||||
784 | qemu_flush_coalesced_mmio_buffer(); | |||||
785 | ++memory_region_transaction_depth; | |||||
786 | } | |||||
787 | ||||||
788 | void memory_region_transaction_commit(void) | |||||
789 | { | |||||
790 | AddressSpace *as; | |||||
791 | ||||||
792 | assert(memory_region_transaction_depth)((memory_region_transaction_depth) ? (void) (0) : __assert_fail ("memory_region_transaction_depth", "/home/stefan/src/qemu/qemu.org/qemu/memory.c" , 792, __PRETTY_FUNCTION__)); | |||||
793 | --memory_region_transaction_depth; | |||||
794 | if (!memory_region_transaction_depth && memory_region_update_pending) { | |||||
795 | memory_region_update_pending = false0; | |||||
796 | MEMORY_LISTENER_CALL_GLOBAL(begin, Forward)do { MemoryListener *_listener; switch (Forward) { case Forward : for ((_listener) = ((&memory_listeners)->tqh_first); (_listener); (_listener) = ((_listener)->link.tqe_next)) { if (_listener->begin) { _listener->begin(_listener); } } break; case Reverse: for ((_listener) = (*(((struct memory_listeners *)((&memory_listeners)->tqh_last))->tqh_last)); (_listener ); (_listener) = (*(((struct memory_listeners *)((_listener)-> link.tqe_prev))->tqh_last))) { if (_listener->begin) { _listener ->begin(_listener); } } break; default: abort(); } } while (0); | |||||
797 | ||||||
798 | QTAILQ_FOREACH(as, &address_spaces, address_spaces_link)for ((as) = ((&address_spaces)->tqh_first); (as); (as) = ((as)->address_spaces_link.tqe_next)) { | |||||
799 | address_space_update_topology(as); | |||||
800 | } | |||||
801 | ||||||
802 | MEMORY_LISTENER_CALL_GLOBAL(commit, Forward)do { MemoryListener *_listener; switch (Forward) { case Forward : for ((_listener) = ((&memory_listeners)->tqh_first); (_listener); (_listener) = ((_listener)->link.tqe_next)) { if (_listener->commit) { _listener->commit(_listener); } } break; case Reverse: for ((_listener) = (*(((struct memory_listeners *)((&memory_listeners)->tqh_last))->tqh_last)); (_listener ); (_listener) = (*(((struct memory_listeners *)((_listener)-> link.tqe_prev))->tqh_last))) { if (_listener->commit) { _listener->commit(_listener); } } break; default: abort() ; } } while (0); | |||||
803 | } | |||||
804 | } | |||||
805 | ||||||
806 | static void memory_region_destructor_none(MemoryRegion *mr) | |||||
807 | { | |||||
808 | } | |||||
809 | ||||||
810 | static void memory_region_destructor_ram(MemoryRegion *mr) | |||||
811 | { | |||||
812 | qemu_ram_free(mr->ram_addr); | |||||
813 | } | |||||
814 | ||||||
815 | static void memory_region_destructor_alias(MemoryRegion *mr) | |||||
816 | { | |||||
817 | memory_region_unref(mr->alias); | |||||
818 | } | |||||
819 | ||||||
820 | static void memory_region_destructor_ram_from_ptr(MemoryRegion *mr) | |||||
821 | { | |||||
822 | qemu_ram_free_from_ptr(mr->ram_addr); | |||||
823 | } | |||||
824 | ||||||
825 | static void memory_region_destructor_rom_device(MemoryRegion *mr) | |||||
826 | { | |||||
827 | qemu_ram_free(mr->ram_addr & TARGET_PAGE_MASK~((1 << 10) - 1)); | |||||
828 | } | |||||
829 | ||||||
830 | void memory_region_init(MemoryRegion *mr, | |||||
831 | Object *owner, | |||||
832 | const char *name, | |||||
833 | uint64_t size) | |||||
834 | { | |||||
835 | mr->ops = &unassigned_mem_ops; | |||||
836 | mr->opaque = NULL((void*)0); | |||||
837 | mr->owner = owner; | |||||
838 | mr->iommu_ops = NULL((void*)0); | |||||
839 | mr->parent = NULL((void*)0); | |||||
840 | mr->size = int128_make64(size); | |||||
841 | if (size == UINT64_MAX(18446744073709551615UL)) { | |||||
842 | mr->size = int128_2_64(); | |||||
843 | } | |||||
844 | mr->addr = 0; | |||||
845 | mr->subpage = false0; | |||||
846 | mr->enabled = true1; | |||||
847 | mr->terminates = false0; | |||||
848 | mr->ram = false0; | |||||
849 | mr->romd_mode = true1; | |||||
850 | mr->readonly = false0; | |||||
851 | mr->rom_device = false0; | |||||
852 | mr->destructor = memory_region_destructor_none; | |||||
853 | mr->priority = 0; | |||||
854 | mr->may_overlap = false0; | |||||
855 | mr->alias = NULL((void*)0); | |||||
856 | QTAILQ_INIT(&mr->subregions)do { (&mr->subregions)->tqh_first = ((void*)0); (& mr->subregions)->tqh_last = &(&mr->subregions )->tqh_first; } while ( 0); | |||||
857 | memset(&mr->subregions_link, 0, sizeof mr->subregions_link); | |||||
858 | QTAILQ_INIT(&mr->coalesced)do { (&mr->coalesced)->tqh_first = ((void*)0); (& mr->coalesced)->tqh_last = &(&mr->coalesced) ->tqh_first; } while ( 0); | |||||
859 | mr->name = g_strdup(name); | |||||
860 | mr->dirty_log_mask = 0; | |||||
861 | mr->ioeventfd_nb = 0; | |||||
862 | mr->ioeventfds = NULL((void*)0); | |||||
863 | mr->flush_coalesced_mmio = false0; | |||||
864 | } | |||||
865 | ||||||
866 | static uint64_t unassigned_mem_read(void *opaque, hwaddr addr, | |||||
867 | unsigned size) | |||||
868 | { | |||||
869 | #ifdef DEBUG_UNASSIGNED | |||||
870 | printf("Unassigned mem read " TARGET_FMT_plx"%016" "l" "x" "\n", addr); | |||||
871 | #endif | |||||
872 | if (current_cputls__current_cpu != NULL((void*)0)) { | |||||
873 | cpu_unassigned_access(current_cputls__current_cpu, addr, false0, false0, 0, size); | |||||
874 | } | |||||
875 | return 0; | |||||
876 | } | |||||
877 | ||||||
878 | static void unassigned_mem_write(void *opaque, hwaddr addr, | |||||
879 | uint64_t val, unsigned size) | |||||
880 | { | |||||
881 | #ifdef DEBUG_UNASSIGNED | |||||
882 | printf("Unassigned mem write " TARGET_FMT_plx"%016" "l" "x" " = 0x%"PRIx64"l" "x""\n", addr, val); | |||||
883 | #endif | |||||
884 | if (current_cputls__current_cpu != NULL((void*)0)) { | |||||
885 | cpu_unassigned_access(current_cputls__current_cpu, addr, true1, false0, 0, size); | |||||
886 | } | |||||
887 | } | |||||
888 | ||||||
889 | static bool_Bool unassigned_mem_accepts(void *opaque, hwaddr addr, | |||||
890 | unsigned size, bool_Bool is_write) | |||||
891 | { | |||||
892 | return false0; | |||||
893 | } | |||||
894 | ||||||
895 | const MemoryRegionOps unassigned_mem_ops = { | |||||
896 | .valid.accepts = unassigned_mem_accepts, | |||||
897 | .endianness = DEVICE_NATIVE_ENDIAN, | |||||
898 | }; | |||||
899 | ||||||
900 | bool_Bool memory_region_access_valid(MemoryRegion *mr, | |||||
901 | hwaddr addr, | |||||
902 | unsigned size, | |||||
903 | bool_Bool is_write) | |||||
904 | { | |||||
905 | int access_size_min, access_size_max; | |||||
906 | int access_size, i; | |||||
907 | ||||||
908 | if (!mr->ops->valid.unaligned && (addr & (size - 1))) { | |||||
909 | return false0; | |||||
910 | } | |||||
911 | ||||||
912 | if (!mr->ops->valid.accepts) { | |||||
913 | return true1; | |||||
914 | } | |||||
915 | ||||||
916 | access_size_min = mr->ops->valid.min_access_size; | |||||
917 | if (!mr->ops->valid.min_access_size) { | |||||
918 | access_size_min = 1; | |||||
919 | } | |||||
920 | ||||||
921 | access_size_max = mr->ops->valid.max_access_size; | |||||
922 | if (!mr->ops->valid.max_access_size) { | |||||
923 | access_size_max = 4; | |||||
924 | } | |||||
925 | ||||||
926 | access_size = MAX(MIN(size, access_size_max), access_size_min)((((((size) < (access_size_max)) ? (size) : (access_size_max ))) > (access_size_min)) ? ((((size) < (access_size_max )) ? (size) : (access_size_max))) : (access_size_min)); | |||||
927 | for (i = 0; i < size; i += access_size) { | |||||
928 | if (!mr->ops->valid.accepts(mr->opaque, addr + i, access_size, | |||||
929 | is_write)) { | |||||
930 | return false0; | |||||
931 | } | |||||
932 | } | |||||
933 | ||||||
934 | return true1; | |||||
935 | } | |||||
936 | ||||||
937 | static uint64_t memory_region_dispatch_read1(MemoryRegion *mr, | |||||
938 | hwaddr addr, | |||||
939 | unsigned size) | |||||
940 | { | |||||
941 | uint64_t data = 0; | |||||
942 | ||||||
943 | if (mr->ops->read) { | |||||
944 | access_with_adjusted_size(addr, &data, size, | |||||
945 | mr->ops->impl.min_access_size, | |||||
946 | mr->ops->impl.max_access_size, | |||||
947 | memory_region_read_accessor, mr); | |||||
948 | } else { | |||||
949 | access_with_adjusted_size(addr, &data, size, 1, 4, | |||||
950 | memory_region_oldmmio_read_accessor, mr); | |||||
951 | } | |||||
952 | ||||||
953 | return data; | |||||
954 | } | |||||
955 | ||||||
956 | static bool_Bool memory_region_dispatch_read(MemoryRegion *mr, | |||||
957 | hwaddr addr, | |||||
958 | uint64_t *pval, | |||||
959 | unsigned size) | |||||
960 | { | |||||
961 | if (!memory_region_access_valid(mr, addr, size, false0)) { | |||||
962 | *pval = unassigned_mem_read(mr, addr, size); | |||||
963 | return true1; | |||||
964 | } | |||||
965 | ||||||
966 | *pval = memory_region_dispatch_read1(mr, addr, size); | |||||
967 | adjust_endianness(mr, pval, size); | |||||
968 | return false0; | |||||
969 | } | |||||
970 | ||||||
971 | static bool_Bool memory_region_dispatch_write(MemoryRegion *mr, | |||||
972 | hwaddr addr, | |||||
973 | uint64_t data, | |||||
974 | unsigned size) | |||||
975 | { | |||||
976 | if (!memory_region_access_valid(mr, addr, size, true1)) { | |||||
977 | unassigned_mem_write(mr, addr, data, size); | |||||
978 | return true1; | |||||
979 | } | |||||
980 | ||||||
981 | adjust_endianness(mr, &data, size); | |||||
982 | ||||||
983 | if (mr->ops->write) { | |||||
984 | access_with_adjusted_size(addr, &data, size, | |||||
985 | mr->ops->impl.min_access_size, | |||||
986 | mr->ops->impl.max_access_size, | |||||
987 | memory_region_write_accessor, mr); | |||||
988 | } else { | |||||
989 | access_with_adjusted_size(addr, &data, size, 1, 4, | |||||
990 | memory_region_oldmmio_write_accessor, mr); | |||||
991 | } | |||||
992 | return false0; | |||||
993 | } | |||||
994 | ||||||
995 | void memory_region_init_io(MemoryRegion *mr, | |||||
996 | Object *owner, | |||||
997 | const MemoryRegionOps *ops, | |||||
998 | void *opaque, | |||||
999 | const char *name, | |||||
1000 | uint64_t size) | |||||
1001 | { | |||||
1002 | memory_region_init(mr, owner, name, size); | |||||
1003 | mr->ops = ops; | |||||
1004 | mr->opaque = opaque; | |||||
1005 | mr->terminates = true1; | |||||
1006 | mr->ram_addr = ~(ram_addr_t)0; | |||||
1007 | } | |||||
1008 | ||||||
1009 | void memory_region_init_ram(MemoryRegion *mr, | |||||
1010 | Object *owner, | |||||
1011 | const char *name, | |||||
1012 | uint64_t size) | |||||
1013 | { | |||||
1014 | memory_region_init(mr, owner, name, size); | |||||
1015 | mr->ram = true1; | |||||
1016 | mr->terminates = true1; | |||||
1017 | mr->destructor = memory_region_destructor_ram; | |||||
1018 | mr->ram_addr = qemu_ram_alloc(size, mr); | |||||
1019 | } | |||||
1020 | ||||||
1021 | void memory_region_init_ram_ptr(MemoryRegion *mr, | |||||
1022 | Object *owner, | |||||
1023 | const char *name, | |||||
1024 | uint64_t size, | |||||
1025 | void *ptr) | |||||
1026 | { | |||||
1027 | memory_region_init(mr, owner, name, size); | |||||
1028 | mr->ram = true1; | |||||
1029 | mr->terminates = true1; | |||||
1030 | mr->destructor = memory_region_destructor_ram_from_ptr; | |||||
1031 | mr->ram_addr = qemu_ram_alloc_from_ptr(size, ptr, mr); | |||||
1032 | } | |||||
1033 | ||||||
1034 | void memory_region_init_alias(MemoryRegion *mr, | |||||
1035 | Object *owner, | |||||
1036 | const char *name, | |||||
1037 | MemoryRegion *orig, | |||||
1038 | hwaddr offset, | |||||
1039 | uint64_t size) | |||||
1040 | { | |||||
1041 | memory_region_init(mr, owner, name, size); | |||||
1042 | memory_region_ref(orig); | |||||
1043 | mr->destructor = memory_region_destructor_alias; | |||||
1044 | mr->alias = orig; | |||||
1045 | mr->alias_offset = offset; | |||||
1046 | } | |||||
1047 | ||||||
1048 | void memory_region_init_rom_device(MemoryRegion *mr, | |||||
1049 | Object *owner, | |||||
1050 | const MemoryRegionOps *ops, | |||||
1051 | void *opaque, | |||||
1052 | const char *name, | |||||
1053 | uint64_t size) | |||||
1054 | { | |||||
1055 | memory_region_init(mr, owner, name, size); | |||||
1056 | mr->ops = ops; | |||||
1057 | mr->opaque = opaque; | |||||
1058 | mr->terminates = true1; | |||||
1059 | mr->rom_device = true1; | |||||
1060 | mr->destructor = memory_region_destructor_rom_device; | |||||
1061 | mr->ram_addr = qemu_ram_alloc(size, mr); | |||||
1062 | } | |||||
1063 | ||||||
1064 | void memory_region_init_iommu(MemoryRegion *mr, | |||||
1065 | Object *owner, | |||||
1066 | const MemoryRegionIOMMUOps *ops, | |||||
1067 | const char *name, | |||||
1068 | uint64_t size) | |||||
1069 | { | |||||
1070 | memory_region_init(mr, owner, name, size); | |||||
1071 | mr->iommu_ops = ops, | |||||
1072 | mr->terminates = true1; /* then re-forwards */ | |||||
1073 | notifier_list_init(&mr->iommu_notify); | |||||
1074 | } | |||||
1075 | ||||||
1076 | void memory_region_init_reservation(MemoryRegion *mr, | |||||
1077 | Object *owner, | |||||
1078 | const char *name, | |||||
1079 | uint64_t size) | |||||
1080 | { | |||||
1081 | memory_region_init_io(mr, owner, &unassigned_mem_ops, mr, name, size); | |||||
1082 | } | |||||
1083 | ||||||
1084 | void memory_region_destroy(MemoryRegion *mr) | |||||
1085 | { | |||||
1086 | assert(QTAILQ_EMPTY(&mr->subregions))((((&mr->subregions)->tqh_first == ((void*)0))) ? ( void) (0) : __assert_fail ("((&mr->subregions)->tqh_first == ((void*)0))" , "/home/stefan/src/qemu/qemu.org/qemu/memory.c", 1086, __PRETTY_FUNCTION__ )); | |||||
1087 | assert(memory_region_transaction_depth == 0)((memory_region_transaction_depth == 0) ? (void) (0) : __assert_fail ("memory_region_transaction_depth == 0", "/home/stefan/src/qemu/qemu.org/qemu/memory.c" , 1087, __PRETTY_FUNCTION__)); | |||||
1088 | mr->destructor(mr); | |||||
1089 | memory_region_clear_coalescing(mr); | |||||
1090 | g_free((char *)mr->name); | |||||
1091 | g_free(mr->ioeventfds); | |||||
1092 | } | |||||
1093 | ||||||
1094 | Object *memory_region_owner(MemoryRegion *mr) | |||||
1095 | { | |||||
1096 | return mr->owner; | |||||
1097 | } | |||||
1098 | ||||||
1099 | void memory_region_ref(MemoryRegion *mr) | |||||
1100 | { | |||||
1101 | if (mr && mr->owner) { | |||||
1102 | object_ref(mr->owner); | |||||
1103 | } | |||||
1104 | } | |||||
1105 | ||||||
1106 | void memory_region_unref(MemoryRegion *mr) | |||||
1107 | { | |||||
1108 | if (mr && mr->owner) { | |||||
1109 | object_unref(mr->owner); | |||||
1110 | } | |||||
1111 | } | |||||
1112 | ||||||
1113 | uint64_t memory_region_size(MemoryRegion *mr) | |||||
1114 | { | |||||
1115 | if (int128_eq(mr->size, int128_2_64())) { | |||||
1116 | return UINT64_MAX(18446744073709551615UL); | |||||
1117 | } | |||||
1118 | return int128_get64(mr->size); | |||||
1119 | } | |||||
1120 | ||||||
1121 | const char *memory_region_name(MemoryRegion *mr) | |||||
1122 | { | |||||
1123 | return mr->name; | |||||
1124 | } | |||||
1125 | ||||||
1126 | bool_Bool memory_region_is_ram(MemoryRegion *mr) | |||||
1127 | { | |||||
1128 | return mr->ram; | |||||
1129 | } | |||||
1130 | ||||||
1131 | bool_Bool memory_region_is_logging(MemoryRegion *mr) | |||||
1132 | { | |||||
1133 | return mr->dirty_log_mask; | |||||
1134 | } | |||||
1135 | ||||||
1136 | bool_Bool memory_region_is_rom(MemoryRegion *mr) | |||||
1137 | { | |||||
1138 | return mr->ram && mr->readonly; | |||||
1139 | } | |||||
1140 | ||||||
1141 | bool_Bool memory_region_is_iommu(MemoryRegion *mr) | |||||
1142 | { | |||||
1143 | return mr->iommu_ops; | |||||
1144 | } | |||||
1145 | ||||||
1146 | void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n) | |||||
1147 | { | |||||
1148 | notifier_list_add(&mr->iommu_notify, n); | |||||
1149 | } | |||||
1150 | ||||||
1151 | void memory_region_unregister_iommu_notifier(Notifier *n) | |||||
1152 | { | |||||
1153 | notifier_remove(n); | |||||
1154 | } | |||||
1155 | ||||||
1156 | void memory_region_notify_iommu(MemoryRegion *mr, | |||||
1157 | IOMMUTLBEntry entry) | |||||
1158 | { | |||||
1159 | assert(memory_region_is_iommu(mr))((memory_region_is_iommu(mr)) ? (void) (0) : __assert_fail ("memory_region_is_iommu(mr)" , "/home/stefan/src/qemu/qemu.org/qemu/memory.c", 1159, __PRETTY_FUNCTION__ )); | |||||
1160 | notifier_list_notify(&mr->iommu_notify, &entry); | |||||
1161 | } | |||||
1162 | ||||||
1163 | void memory_region_set_log(MemoryRegion *mr, bool_Bool log, unsigned client) | |||||
1164 | { | |||||
1165 | uint8_t mask = 1 << client; | |||||
1166 | ||||||
1167 | memory_region_transaction_begin(); | |||||
1168 | mr->dirty_log_mask = (mr->dirty_log_mask & ~mask) | (log * mask); | |||||
1169 | memory_region_update_pending |= mr->enabled; | |||||
1170 | memory_region_transaction_commit(); | |||||
1171 | } | |||||
1172 | ||||||
1173 | bool_Bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr, | |||||
1174 | hwaddr size, unsigned client) | |||||
1175 | { | |||||
1176 | assert(mr->terminates)((mr->terminates) ? (void) (0) : __assert_fail ("mr->terminates" , "/home/stefan/src/qemu/qemu.org/qemu/memory.c", 1176, __PRETTY_FUNCTION__ )); | |||||
1177 | return cpu_physical_memory_get_dirty(mr->ram_addr + addr, size, | |||||
1178 | 1 << client); | |||||
1179 | } | |||||
1180 | ||||||
1181 | void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr, | |||||
1182 | hwaddr size) | |||||
1183 | { | |||||
1184 | assert(mr->terminates)((mr->terminates) ? (void) (0) : __assert_fail ("mr->terminates" , "/home/stefan/src/qemu/qemu.org/qemu/memory.c", 1184, __PRETTY_FUNCTION__ )); | |||||
1185 | return cpu_physical_memory_set_dirty_range(mr->ram_addr + addr, size, -1); | |||||
1186 | } | |||||
1187 | ||||||
1188 | bool_Bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr, | |||||
1189 | hwaddr size, unsigned client) | |||||
1190 | { | |||||
1191 | bool_Bool ret; | |||||
1192 | assert(mr->terminates)((mr->terminates) ? (void) (0) : __assert_fail ("mr->terminates" , "/home/stefan/src/qemu/qemu.org/qemu/memory.c", 1192, __PRETTY_FUNCTION__ )); | |||||
1193 | ret = cpu_physical_memory_get_dirty(mr->ram_addr + addr, size, | |||||
1194 | 1 << client); | |||||
1195 | if (ret) { | |||||
1196 | cpu_physical_memory_reset_dirty(mr->ram_addr + addr, | |||||
1197 | mr->ram_addr + addr + size, | |||||
1198 | 1 << client); | |||||
1199 | } | |||||
1200 | return ret; | |||||
1201 | } | |||||
1202 | ||||||
1203 | ||||||
1204 | void memory_region_sync_dirty_bitmap(MemoryRegion *mr) | |||||
1205 | { | |||||
1206 | AddressSpace *as; | |||||
1207 | FlatRange *fr; | |||||
1208 | ||||||
1209 | QTAILQ_FOREACH(as, &address_spaces, address_spaces_link)for ((as) = ((&address_spaces)->tqh_first); (as); (as) = ((as)->address_spaces_link.tqe_next)) { | |||||
1210 | FlatView *view = address_space_get_flatview(as); | |||||
1211 | FOR_EACH_FLAT_RANGE(fr, view)for (fr = (view)->ranges; fr < (view)->ranges + (view )->nr; ++fr) { | |||||
1212 | if (fr->mr == mr) { | |||||
1213 | MEMORY_LISTENER_UPDATE_REGION(fr, as, Forward, log_sync)do { MemoryListener *_listener; switch (Forward) { case Forward : for ((_listener) = ((&memory_listeners)->tqh_first); (_listener); (_listener) = ((_listener)->link.tqe_next)) { if (_listener->log_sync && memory_listener_match( _listener, (&(MemoryRegionSection) { .mr = (fr)->mr, . address_space = (as), .offset_within_region = (fr)->offset_in_region , .size = (fr)->addr.size, .offset_within_address_space = int128_get64 ((fr)->addr.start), .readonly = (fr)->readonly, }))) { _listener ->log_sync(_listener, (&(MemoryRegionSection) { .mr = ( fr)->mr, .address_space = (as), .offset_within_region = (fr )->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64((fr)->addr.start), .readonly = (fr)->readonly , })); } } break; case Reverse: for ((_listener) = (*(((struct memory_listeners *)((&memory_listeners)->tqh_last))-> tqh_last)); (_listener); (_listener) = (*(((struct memory_listeners *)((_listener)->link.tqe_prev))->tqh_last))) { if (_listener ->log_sync && memory_listener_match(_listener, (& (MemoryRegionSection) { .mr = (fr)->mr, .address_space = ( as), .offset_within_region = (fr)->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64 ((fr)->addr.start), .readonly = (fr)->readonly, }))) { _listener ->log_sync(_listener, (&(MemoryRegionSection) { .mr = ( fr)->mr, .address_space = (as), .offset_within_region = (fr )->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64((fr)->addr.start), .readonly = (fr)->readonly , })); } } break; default: abort(); } } while (0); | |||||
1214 | } | |||||
1215 | } | |||||
1216 | flatview_unref(view); | |||||
1217 | } | |||||
1218 | } | |||||
1219 | ||||||
1220 | void memory_region_set_readonly(MemoryRegion *mr, bool_Bool readonly) | |||||
1221 | { | |||||
1222 | if (mr->readonly != readonly) { | |||||
1223 | memory_region_transaction_begin(); | |||||
1224 | mr->readonly = readonly; | |||||
1225 | memory_region_update_pending |= mr->enabled; | |||||
1226 | memory_region_transaction_commit(); | |||||
1227 | } | |||||
1228 | } | |||||
1229 | ||||||
1230 | void memory_region_rom_device_set_romd(MemoryRegion *mr, bool_Bool romd_mode) | |||||
1231 | { | |||||
1232 | if (mr->romd_mode != romd_mode) { | |||||
1233 | memory_region_transaction_begin(); | |||||
1234 | mr->romd_mode = romd_mode; | |||||
1235 | memory_region_update_pending |= mr->enabled; | |||||
1236 | memory_region_transaction_commit(); | |||||
1237 | } | |||||
1238 | } | |||||
1239 | ||||||
1240 | void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr, | |||||
1241 | hwaddr size, unsigned client) | |||||
1242 | { | |||||
1243 | assert(mr->terminates)((mr->terminates) ? (void) (0) : __assert_fail ("mr->terminates" , "/home/stefan/src/qemu/qemu.org/qemu/memory.c", 1243, __PRETTY_FUNCTION__ )); | |||||
1244 | cpu_physical_memory_reset_dirty(mr->ram_addr + addr, | |||||
1245 | mr->ram_addr + addr + size, | |||||
1246 | 1 << client); | |||||
1247 | } | |||||
1248 | ||||||
1249 | void *memory_region_get_ram_ptr(MemoryRegion *mr) | |||||
1250 | { | |||||
1251 | if (mr->alias) { | |||||
1252 | return memory_region_get_ram_ptr(mr->alias) + mr->alias_offset; | |||||
1253 | } | |||||
1254 | ||||||
1255 | assert(mr->terminates)((mr->terminates) ? (void) (0) : __assert_fail ("mr->terminates" , "/home/stefan/src/qemu/qemu.org/qemu/memory.c", 1255, __PRETTY_FUNCTION__ )); | |||||
1256 | ||||||
1257 | return qemu_get_ram_ptr(mr->ram_addr & TARGET_PAGE_MASK~((1 << 10) - 1)); | |||||
1258 | } | |||||
1259 | ||||||
1260 | static void memory_region_update_coalesced_range_as(MemoryRegion *mr, AddressSpace *as) | |||||
1261 | { | |||||
1262 | FlatView *view; | |||||
1263 | FlatRange *fr; | |||||
1264 | CoalescedMemoryRange *cmr; | |||||
1265 | AddrRange tmp; | |||||
1266 | MemoryRegionSection section; | |||||
1267 | ||||||
1268 | view = address_space_get_flatview(as); | |||||
1269 | FOR_EACH_FLAT_RANGE(fr, view)for (fr = (view)->ranges; fr < (view)->ranges + (view )->nr; ++fr) { | |||||
1270 | if (fr->mr == mr) { | |||||
1271 | section = (MemoryRegionSection) { | |||||
1272 | .address_space = as, | |||||
1273 | .offset_within_address_space = int128_get64(fr->addr.start), | |||||
1274 | .size = fr->addr.size, | |||||
1275 | }; | |||||
1276 | ||||||
1277 | MEMORY_LISTENER_CALL(coalesced_mmio_del, Reverse, §ion,do { MemoryListener *_listener; switch (Reverse) { case Forward : for ((_listener) = ((&memory_listeners)->tqh_first); (_listener); (_listener) = ((_listener)->link.tqe_next)) { if (_listener->coalesced_mmio_del && memory_listener_match (_listener, §ion)) { _listener->coalesced_mmio_del (_listener, §ion, int128_get64(fr->addr.start), int128_get64 (fr->addr.size)); } } break; case Reverse: for ((_listener ) = (*(((struct memory_listeners *)((&memory_listeners)-> tqh_last))->tqh_last)); (_listener); (_listener) = (*(((struct memory_listeners *)((_listener)->link.tqe_prev))->tqh_last ))) { if (_listener->coalesced_mmio_del && memory_listener_match (_listener, §ion)) { _listener->coalesced_mmio_del (_listener, §ion, int128_get64(fr->addr.start), int128_get64 (fr->addr.size)); } } break; default: abort(); } } while ( 0) | |||||
1278 | int128_get64(fr->addr.start),do { MemoryListener *_listener; switch (Reverse) { case Forward : for ((_listener) = ((&memory_listeners)->tqh_first); (_listener); (_listener) = ((_listener)->link.tqe_next)) { if (_listener->coalesced_mmio_del && memory_listener_match (_listener, §ion)) { _listener->coalesced_mmio_del (_listener, §ion, int128_get64(fr->addr.start), int128_get64 (fr->addr.size)); } } break; case Reverse: for ((_listener ) = (*(((struct memory_listeners *)((&memory_listeners)-> tqh_last))->tqh_last)); (_listener); (_listener) = (*(((struct memory_listeners *)((_listener)->link.tqe_prev))->tqh_last ))) { if (_listener->coalesced_mmio_del && memory_listener_match (_listener, §ion)) { _listener->coalesced_mmio_del (_listener, §ion, int128_get64(fr->addr.start), int128_get64 (fr->addr.size)); } } break; default: abort(); } } while ( 0) | |||||
1279 | int128_get64(fr->addr.size))do { MemoryListener *_listener; switch (Reverse) { case Forward : for ((_listener) = ((&memory_listeners)->tqh_first); (_listener); (_listener) = ((_listener)->link.tqe_next)) { if (_listener->coalesced_mmio_del && memory_listener_match (_listener, §ion)) { _listener->coalesced_mmio_del (_listener, §ion, int128_get64(fr->addr.start), int128_get64 (fr->addr.size)); } } break; case Reverse: for ((_listener ) = (*(((struct memory_listeners *)((&memory_listeners)-> tqh_last))->tqh_last)); (_listener); (_listener) = (*(((struct memory_listeners *)((_listener)->link.tqe_prev))->tqh_last ))) { if (_listener->coalesced_mmio_del && memory_listener_match (_listener, §ion)) { _listener->coalesced_mmio_del (_listener, §ion, int128_get64(fr->addr.start), int128_get64 (fr->addr.size)); } } break; default: abort(); } } while ( 0); | |||||
1280 | QTAILQ_FOREACH(cmr, &mr->coalesced, link)for ((cmr) = ((&mr->coalesced)->tqh_first); (cmr); ( cmr) = ((cmr)->link.tqe_next)) { | |||||
1281 | tmp = addrrange_shift(cmr->addr, | |||||
1282 | int128_sub(fr->addr.start, | |||||
1283 | int128_make64(fr->offset_in_region))); | |||||
1284 | if (!addrrange_intersects(tmp, fr->addr)) { | |||||
1285 | continue; | |||||
1286 | } | |||||
1287 | tmp = addrrange_intersection(tmp, fr->addr); | |||||
1288 | MEMORY_LISTENER_CALL(coalesced_mmio_add, Forward, §ion,do { MemoryListener *_listener; switch (Forward) { case Forward : for ((_listener) = ((&memory_listeners)->tqh_first); (_listener); (_listener) = ((_listener)->link.tqe_next)) { if (_listener->coalesced_mmio_add && memory_listener_match (_listener, §ion)) { _listener->coalesced_mmio_add (_listener, §ion, int128_get64(tmp.start), int128_get64 (tmp.size)); } } break; case Reverse: for ((_listener) = (*(( (struct memory_listeners *)((&memory_listeners)->tqh_last ))->tqh_last)); (_listener); (_listener) = (*(((struct memory_listeners *)((_listener)->link.tqe_prev))->tqh_last))) { if (_listener ->coalesced_mmio_add && memory_listener_match(_listener , §ion)) { _listener->coalesced_mmio_add(_listener , §ion, int128_get64(tmp.start), int128_get64(tmp.size )); } } break; default: abort(); } } while (0) | |||||
1289 | int128_get64(tmp.start),do { MemoryListener *_listener; switch (Forward) { case Forward : for ((_listener) = ((&memory_listeners)->tqh_first); (_listener); (_listener) = ((_listener)->link.tqe_next)) { if (_listener->coalesced_mmio_add && memory_listener_match (_listener, §ion)) { _listener->coalesced_mmio_add (_listener, §ion, int128_get64(tmp.start), int128_get64 (tmp.size)); } } break; case Reverse: for ((_listener) = (*(( (struct memory_listeners *)((&memory_listeners)->tqh_last ))->tqh_last)); (_listener); (_listener) = (*(((struct memory_listeners *)((_listener)->link.tqe_prev))->tqh_last))) { if (_listener ->coalesced_mmio_add && memory_listener_match(_listener , §ion)) { _listener->coalesced_mmio_add(_listener , §ion, int128_get64(tmp.start), int128_get64(tmp.size )); } } break; default: abort(); } } while (0) | |||||
1290 | int128_get64(tmp.size))do { MemoryListener *_listener; switch (Forward) { case Forward : for ((_listener) = ((&memory_listeners)->tqh_first); (_listener); (_listener) = ((_listener)->link.tqe_next)) { if (_listener->coalesced_mmio_add && memory_listener_match (_listener, §ion)) { _listener->coalesced_mmio_add (_listener, §ion, int128_get64(tmp.start), int128_get64 (tmp.size)); } } break; case Reverse: for ((_listener) = (*(( (struct memory_listeners *)((&memory_listeners)->tqh_last ))->tqh_last)); (_listener); (_listener) = (*(((struct memory_listeners *)((_listener)->link.tqe_prev))->tqh_last))) { if (_listener ->coalesced_mmio_add && memory_listener_match(_listener , §ion)) { _listener->coalesced_mmio_add(_listener , §ion, int128_get64(tmp.start), int128_get64(tmp.size )); } } break; default: abort(); } } while (0); | |||||
1291 | } | |||||
1292 | } | |||||
1293 | } | |||||
1294 | flatview_unref(view); | |||||
1295 | } | |||||
1296 | ||||||
1297 | static void memory_region_update_coalesced_range(MemoryRegion *mr) | |||||
1298 | { | |||||
1299 | AddressSpace *as; | |||||
1300 | ||||||
1301 | QTAILQ_FOREACH(as, &address_spaces, address_spaces_link)for ((as) = ((&address_spaces)->tqh_first); (as); (as) = ((as)->address_spaces_link.tqe_next)) { | |||||
1302 | memory_region_update_coalesced_range_as(mr, as); | |||||
1303 | } | |||||
1304 | } | |||||
1305 | ||||||
1306 | void memory_region_set_coalescing(MemoryRegion *mr) | |||||
1307 | { | |||||
1308 | memory_region_clear_coalescing(mr); | |||||
1309 | memory_region_add_coalescing(mr, 0, int128_get64(mr->size)); | |||||
1310 | } | |||||
1311 | ||||||
1312 | void memory_region_add_coalescing(MemoryRegion *mr, | |||||
1313 | hwaddr offset, | |||||
1314 | uint64_t size) | |||||
1315 | { | |||||
1316 | CoalescedMemoryRange *cmr = g_malloc(sizeof(*cmr)); | |||||
1317 | ||||||
1318 | cmr->addr = addrrange_make(int128_make64(offset), int128_make64(size)); | |||||
1319 | QTAILQ_INSERT_TAIL(&mr->coalesced, cmr, link)do { (cmr)->link.tqe_next = ((void*)0); (cmr)->link.tqe_prev = (&mr->coalesced)->tqh_last; *(&mr->coalesced )->tqh_last = (cmr); (&mr->coalesced)->tqh_last = &(cmr)->link.tqe_next; } while ( 0); | |||||
1320 | memory_region_update_coalesced_range(mr); | |||||
1321 | memory_region_set_flush_coalesced(mr); | |||||
1322 | } | |||||
1323 | ||||||
1324 | void memory_region_clear_coalescing(MemoryRegion *mr) | |||||
1325 | { | |||||
1326 | CoalescedMemoryRange *cmr; | |||||
1327 | ||||||
1328 | qemu_flush_coalesced_mmio_buffer(); | |||||
1329 | mr->flush_coalesced_mmio = false0; | |||||
1330 | ||||||
1331 | while (!QTAILQ_EMPTY(&mr->coalesced)((&mr->coalesced)->tqh_first == ((void*)0))) { | |||||
1332 | cmr = QTAILQ_FIRST(&mr->coalesced)((&mr->coalesced)->tqh_first); | |||||
1333 | QTAILQ_REMOVE(&mr->coalesced, cmr, link)do { if (((cmr)->link.tqe_next) != ((void*)0)) (cmr)->link .tqe_next->link.tqe_prev = (cmr)->link.tqe_prev; else ( &mr->coalesced)->tqh_last = (cmr)->link.tqe_prev ; *(cmr)->link.tqe_prev = (cmr)->link.tqe_next; } while ( 0); | |||||
1334 | g_free(cmr); | |||||
1335 | } | |||||
1336 | memory_region_update_coalesced_range(mr); | |||||
1337 | } | |||||
1338 | ||||||
1339 | void memory_region_set_flush_coalesced(MemoryRegion *mr) | |||||
1340 | { | |||||
1341 | mr->flush_coalesced_mmio = true1; | |||||
1342 | } | |||||
1343 | ||||||
1344 | void memory_region_clear_flush_coalesced(MemoryRegion *mr) | |||||
1345 | { | |||||
1346 | qemu_flush_coalesced_mmio_buffer(); | |||||
1347 | if (QTAILQ_EMPTY(&mr->coalesced)((&mr->coalesced)->tqh_first == ((void*)0))) { | |||||
1348 | mr->flush_coalesced_mmio = false0; | |||||
1349 | } | |||||
1350 | } | |||||
1351 | ||||||
1352 | void memory_region_add_eventfd(MemoryRegion *mr, | |||||
1353 | hwaddr addr, | |||||
1354 | unsigned size, | |||||
1355 | bool_Bool match_data, | |||||
1356 | uint64_t data, | |||||
1357 | EventNotifier *e) | |||||
1358 | { | |||||
1359 | MemoryRegionIoeventfd mrfd = { | |||||
1360 | .addr.start = int128_make64(addr), | |||||
1361 | .addr.size = int128_make64(size), | |||||
1362 | .match_data = match_data, | |||||
1363 | .data = data, | |||||
1364 | .e = e, | |||||
1365 | }; | |||||
1366 | unsigned i; | |||||
1367 | ||||||
1368 | adjust_endianness(mr, &mrfd.data, size); | |||||
1369 | memory_region_transaction_begin(); | |||||
1370 | for (i = 0; i < mr->ioeventfd_nb; ++i) { | |||||
1371 | if (memory_region_ioeventfd_before(mrfd, mr->ioeventfds[i])) { | |||||
1372 | break; | |||||
1373 | } | |||||
1374 | } | |||||
1375 | ++mr->ioeventfd_nb; | |||||
1376 | mr->ioeventfds = g_realloc(mr->ioeventfds, | |||||
1377 | sizeof(*mr->ioeventfds) * mr->ioeventfd_nb); | |||||
1378 | memmove(&mr->ioeventfds[i+1], &mr->ioeventfds[i], | |||||
1379 | sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb-1 - i)); | |||||
1380 | mr->ioeventfds[i] = mrfd; | |||||
1381 | memory_region_update_pending |= mr->enabled; | |||||
1382 | memory_region_transaction_commit(); | |||||
1383 | } | |||||
1384 | ||||||
1385 | void memory_region_del_eventfd(MemoryRegion *mr, | |||||
1386 | hwaddr addr, | |||||
1387 | unsigned size, | |||||
1388 | bool_Bool match_data, | |||||
1389 | uint64_t data, | |||||
1390 | EventNotifier *e) | |||||
1391 | { | |||||
1392 | MemoryRegionIoeventfd mrfd = { | |||||
1393 | .addr.start = int128_make64(addr), | |||||
1394 | .addr.size = int128_make64(size), | |||||
1395 | .match_data = match_data, | |||||
1396 | .data = data, | |||||
1397 | .e = e, | |||||
1398 | }; | |||||
1399 | unsigned i; | |||||
1400 | ||||||
1401 | adjust_endianness(mr, &mrfd.data, size); | |||||
1402 | memory_region_transaction_begin(); | |||||
1403 | for (i = 0; i < mr->ioeventfd_nb; ++i) { | |||||
1404 | if (memory_region_ioeventfd_equal(mrfd, mr->ioeventfds[i])) { | |||||
1405 | break; | |||||
1406 | } | |||||
1407 | } | |||||
1408 | assert(i != mr->ioeventfd_nb)((i != mr->ioeventfd_nb) ? (void) (0) : __assert_fail ("i != mr->ioeventfd_nb" , "/home/stefan/src/qemu/qemu.org/qemu/memory.c", 1408, __PRETTY_FUNCTION__ )); | |||||
1409 | memmove(&mr->ioeventfds[i], &mr->ioeventfds[i+1], | |||||
1410 | sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb - (i+1))); | |||||
1411 | --mr->ioeventfd_nb; | |||||
1412 | mr->ioeventfds = g_realloc(mr->ioeventfds, | |||||
1413 | sizeof(*mr->ioeventfds)*mr->ioeventfd_nb + 1); | |||||
1414 | memory_region_update_pending |= mr->enabled; | |||||
1415 | memory_region_transaction_commit(); | |||||
1416 | } | |||||
1417 | ||||||
1418 | static void memory_region_add_subregion_common(MemoryRegion *mr, | |||||
1419 | hwaddr offset, | |||||
1420 | MemoryRegion *subregion) | |||||
1421 | { | |||||
1422 | MemoryRegion *other; | |||||
1423 | ||||||
1424 | memory_region_transaction_begin(); | |||||
1425 | ||||||
1426 | assert(!subregion->parent)((!subregion->parent) ? (void) (0) : __assert_fail ("!subregion->parent" , "/home/stefan/src/qemu/qemu.org/qemu/memory.c", 1426, __PRETTY_FUNCTION__ )); | |||||
1427 | memory_region_ref(subregion); | |||||
1428 | subregion->parent = mr; | |||||
1429 | subregion->addr = offset; | |||||
1430 | QTAILQ_FOREACH(other, &mr->subregions, subregions_link)for ((other) = ((&mr->subregions)->tqh_first); (other ); (other) = ((other)->subregions_link.tqe_next)) { | |||||
1431 | if (subregion->may_overlap || other->may_overlap) { | |||||
1432 | continue; | |||||
1433 | } | |||||
1434 | if (int128_ge(int128_make64(offset), | |||||
1435 | int128_add(int128_make64(other->addr), other->size)) | |||||
1436 | || int128_le(int128_add(int128_make64(offset), subregion->size), | |||||
1437 | int128_make64(other->addr))) { | |||||
1438 | continue; | |||||
1439 | } | |||||
1440 | #if 0 | |||||
1441 | printf("warning: subregion collision %llx/%llx (%s) " | |||||
1442 | "vs %llx/%llx (%s)\n", | |||||
1443 | (unsigned long long)offset, | |||||
1444 | (unsigned long long)int128_get64(subregion->size), | |||||
1445 | subregion->name, | |||||
1446 | (unsigned long long)other->addr, | |||||
1447 | (unsigned long long)int128_get64(other->size), | |||||
1448 | other->name); | |||||
1449 | #endif | |||||
1450 | } | |||||
1451 | QTAILQ_FOREACH(other, &mr->subregions, subregions_link)for ((other) = ((&mr->subregions)->tqh_first); (other ); (other) = ((other)->subregions_link.tqe_next)) { | |||||
1452 | if (subregion->priority >= other->priority) { | |||||
1453 | QTAILQ_INSERT_BEFORE(other, subregion, subregions_link)do { (subregion)->subregions_link.tqe_prev = (other)->subregions_link .tqe_prev; (subregion)->subregions_link.tqe_next = (other) ; *(other)->subregions_link.tqe_prev = (subregion); (other )->subregions_link.tqe_prev = &(subregion)->subregions_link .tqe_next; } while ( 0); | |||||
1454 | goto done; | |||||
1455 | } | |||||
1456 | } | |||||
1457 | QTAILQ_INSERT_TAIL(&mr->subregions, subregion, subregions_link)do { (subregion)->subregions_link.tqe_next = ((void*)0); ( subregion)->subregions_link.tqe_prev = (&mr->subregions )->tqh_last; *(&mr->subregions)->tqh_last = (subregion ); (&mr->subregions)->tqh_last = &(subregion)-> subregions_link.tqe_next; } while ( 0); | |||||
1458 | done: | |||||
1459 | memory_region_update_pending |= mr->enabled && subregion->enabled; | |||||
1460 | memory_region_transaction_commit(); | |||||
1461 | } | |||||
1462 | ||||||
1463 | ||||||
1464 | void memory_region_add_subregion(MemoryRegion *mr, | |||||
1465 | hwaddr offset, | |||||
1466 | MemoryRegion *subregion) | |||||
1467 | { | |||||
1468 | subregion->may_overlap = false0; | |||||
1469 | subregion->priority = 0; | |||||
1470 | memory_region_add_subregion_common(mr, offset, subregion); | |||||
1471 | } | |||||
1472 | ||||||
1473 | void memory_region_add_subregion_overlap(MemoryRegion *mr, | |||||
1474 | hwaddr offset, | |||||
1475 | MemoryRegion *subregion, | |||||
1476 | int priority) | |||||
1477 | { | |||||
1478 | subregion->may_overlap = true1; | |||||
1479 | subregion->priority = priority; | |||||
1480 | memory_region_add_subregion_common(mr, offset, subregion); | |||||
1481 | } | |||||
1482 | ||||||
1483 | void memory_region_del_subregion(MemoryRegion *mr, | |||||
1484 | MemoryRegion *subregion) | |||||
1485 | { | |||||
1486 | memory_region_transaction_begin(); | |||||
1487 | assert(subregion->parent == mr)((subregion->parent == mr) ? (void) (0) : __assert_fail ("subregion->parent == mr" , "/home/stefan/src/qemu/qemu.org/qemu/memory.c", 1487, __PRETTY_FUNCTION__ )); | |||||
1488 | subregion->parent = NULL((void*)0); | |||||
1489 | QTAILQ_REMOVE(&mr->subregions, subregion, subregions_link)do { if (((subregion)->subregions_link.tqe_next) != ((void *)0)) (subregion)->subregions_link.tqe_next->subregions_link .tqe_prev = (subregion)->subregions_link.tqe_prev; else (& mr->subregions)->tqh_last = (subregion)->subregions_link .tqe_prev; *(subregion)->subregions_link.tqe_prev = (subregion )->subregions_link.tqe_next; } while ( 0); | |||||
1490 | memory_region_unref(subregion); | |||||
1491 | memory_region_update_pending |= mr->enabled && subregion->enabled; | |||||
1492 | memory_region_transaction_commit(); | |||||
1493 | } | |||||
1494 | ||||||
1495 | void memory_region_set_enabled(MemoryRegion *mr, bool_Bool enabled) | |||||
1496 | { | |||||
1497 | if (enabled == mr->enabled) { | |||||
1498 | return; | |||||
1499 | } | |||||
1500 | memory_region_transaction_begin(); | |||||
1501 | mr->enabled = enabled; | |||||
1502 | memory_region_update_pending = true1; | |||||
1503 | memory_region_transaction_commit(); | |||||
1504 | } | |||||
1505 | ||||||
1506 | void memory_region_set_address(MemoryRegion *mr, hwaddr addr) | |||||
1507 | { | |||||
1508 | MemoryRegion *parent = mr->parent; | |||||
1509 | int priority = mr->priority; | |||||
1510 | bool_Bool may_overlap = mr->may_overlap; | |||||
1511 | ||||||
1512 | if (addr == mr->addr || !parent) { | |||||
1513 | mr->addr = addr; | |||||
1514 | return; | |||||
1515 | } | |||||
1516 | ||||||
1517 | memory_region_transaction_begin(); | |||||
1518 | memory_region_ref(mr); | |||||
1519 | memory_region_del_subregion(parent, mr); | |||||
1520 | if (may_overlap) { | |||||
1521 | memory_region_add_subregion_overlap(parent, addr, mr, priority); | |||||
1522 | } else { | |||||
1523 | memory_region_add_subregion(parent, addr, mr); | |||||
1524 | } | |||||
1525 | memory_region_unref(mr); | |||||
1526 | memory_region_transaction_commit(); | |||||
1527 | } | |||||
1528 | ||||||
1529 | void memory_region_set_alias_offset(MemoryRegion *mr, hwaddr offset) | |||||
1530 | { | |||||
1531 | assert(mr->alias)((mr->alias) ? (void) (0) : __assert_fail ("mr->alias", "/home/stefan/src/qemu/qemu.org/qemu/memory.c", 1531, __PRETTY_FUNCTION__ )); | |||||
1532 | ||||||
1533 | if (offset == mr->alias_offset) { | |||||
1534 | return; | |||||
1535 | } | |||||
1536 | ||||||
1537 | memory_region_transaction_begin(); | |||||
1538 | mr->alias_offset = offset; | |||||
1539 | memory_region_update_pending |= mr->enabled; | |||||
1540 | memory_region_transaction_commit(); | |||||
1541 | } | |||||
1542 | ||||||
1543 | ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr) | |||||
1544 | { | |||||
1545 | return mr->ram_addr; | |||||
1546 | } | |||||
1547 | ||||||
1548 | static int cmp_flatrange_addr(const void *addr_, const void *fr_) | |||||
1549 | { | |||||
1550 | const AddrRange *addr = addr_; | |||||
1551 | const FlatRange *fr = fr_; | |||||
1552 | ||||||
1553 | if (int128_le(addrrange_end(*addr), fr->addr.start)) { | |||||
1554 | return -1; | |||||
1555 | } else if (int128_ge(addr->start, addrrange_end(fr->addr))) { | |||||
1556 | return 1; | |||||
1557 | } | |||||
1558 | return 0; | |||||
1559 | } | |||||
1560 | ||||||
1561 | static FlatRange *flatview_lookup(FlatView *view, AddrRange addr) | |||||
1562 | { | |||||
1563 | return bsearch(&addr, view->ranges, view->nr, | |||||
1564 | sizeof(FlatRange), cmp_flatrange_addr); | |||||
1565 | } | |||||
1566 | ||||||
1567 | bool_Bool memory_region_present(MemoryRegion *parent, hwaddr addr) | |||||
1568 | { | |||||
1569 | MemoryRegion *mr = memory_region_find(parent, addr, 1).mr; | |||||
1570 | if (!mr) { | |||||
1571 | return false0; | |||||
1572 | } | |||||
1573 | memory_region_unref(mr); | |||||
1574 | return true1; | |||||
1575 | } | |||||
1576 | ||||||
1577 | MemoryRegionSection memory_region_find(MemoryRegion *mr, | |||||
1578 | hwaddr addr, uint64_t size) | |||||
1579 | { | |||||
1580 | MemoryRegionSection ret = { .mr = NULL((void*)0) }; | |||||
1581 | MemoryRegion *root; | |||||
1582 | AddressSpace *as; | |||||
1583 | AddrRange range; | |||||
1584 | FlatView *view; | |||||
1585 | FlatRange *fr; | |||||
1586 | ||||||
1587 | addr += mr->addr; | |||||
1588 | for (root = mr; root->parent; ) { | |||||
1589 | root = root->parent; | |||||
1590 | addr += root->addr; | |||||
1591 | } | |||||
1592 | ||||||
1593 | as = memory_region_to_address_space(root); | |||||
1594 | range = addrrange_make(int128_make64(addr), int128_make64(size)); | |||||
1595 | ||||||
1596 | view = address_space_get_flatview(as); | |||||
1597 | fr = flatview_lookup(view, range); | |||||
1598 | if (!fr) { | |||||
1599 | flatview_unref(view); | |||||
1600 | return ret; | |||||
1601 | } | |||||
1602 | ||||||
1603 | while (fr > view->ranges && addrrange_intersects(fr[-1].addr, range)) { | |||||
1604 | --fr; | |||||
1605 | } | |||||
1606 | ||||||
1607 | ret.mr = fr->mr; | |||||
1608 | ret.address_space = as; | |||||
1609 | range = addrrange_intersection(range, fr->addr); | |||||
1610 | ret.offset_within_region = fr->offset_in_region; | |||||
1611 | ret.offset_within_region += int128_get64(int128_sub(range.start, | |||||
1612 | fr->addr.start)); | |||||
1613 | ret.size = range.size; | |||||
1614 | ret.offset_within_address_space = int128_get64(range.start); | |||||
1615 | ret.readonly = fr->readonly; | |||||
1616 | memory_region_ref(ret.mr); | |||||
1617 | ||||||
1618 | flatview_unref(view); | |||||
1619 | return ret; | |||||
1620 | } | |||||
1621 | ||||||
1622 | void address_space_sync_dirty_bitmap(AddressSpace *as) | |||||
1623 | { | |||||
1624 | FlatView *view; | |||||
1625 | FlatRange *fr; | |||||
1626 | ||||||
1627 | view = address_space_get_flatview(as); | |||||
1628 | FOR_EACH_FLAT_RANGE(fr, view)for (fr = (view)->ranges; fr < (view)->ranges + (view )->nr; ++fr) { | |||||
1629 | MEMORY_LISTENER_UPDATE_REGION(fr, as, Forward, log_sync)do { MemoryListener *_listener; switch (Forward) { case Forward : for ((_listener) = ((&memory_listeners)->tqh_first); (_listener); (_listener) = ((_listener)->link.tqe_next)) { if (_listener->log_sync && memory_listener_match( _listener, (&(MemoryRegionSection) { .mr = (fr)->mr, . address_space = (as), .offset_within_region = (fr)->offset_in_region , .size = (fr)->addr.size, .offset_within_address_space = int128_get64 ((fr)->addr.start), .readonly = (fr)->readonly, }))) { _listener ->log_sync(_listener, (&(MemoryRegionSection) { .mr = ( fr)->mr, .address_space = (as), .offset_within_region = (fr )->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64((fr)->addr.start), .readonly = (fr)->readonly , })); } } break; case Reverse: for ((_listener) = (*(((struct memory_listeners *)((&memory_listeners)->tqh_last))-> tqh_last)); (_listener); (_listener) = (*(((struct memory_listeners *)((_listener)->link.tqe_prev))->tqh_last))) { if (_listener ->log_sync && memory_listener_match(_listener, (& (MemoryRegionSection) { .mr = (fr)->mr, .address_space = ( as), .offset_within_region = (fr)->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64 ((fr)->addr.start), .readonly = (fr)->readonly, }))) { _listener ->log_sync(_listener, (&(MemoryRegionSection) { .mr = ( fr)->mr, .address_space = (as), .offset_within_region = (fr )->offset_in_region, .size = (fr)->addr.size, .offset_within_address_space = int128_get64((fr)->addr.start), .readonly = (fr)->readonly , })); } } break; default: abort(); } } while (0); | |||||
1630 | } | |||||
1631 | flatview_unref(view); | |||||
1632 | } | |||||
1633 | ||||||
1634 | void memory_global_dirty_log_start(void) | |||||
1635 | { | |||||
1636 | global_dirty_log = true1; | |||||
1637 | MEMORY_LISTENER_CALL_GLOBAL(log_global_start, Forward)do { MemoryListener *_listener; switch (Forward) { case Forward : for ((_listener) = ((&memory_listeners)->tqh_first); (_listener); (_listener) = ((_listener)->link.tqe_next)) { if (_listener->log_global_start) { _listener->log_global_start (_listener); } } break; case Reverse: for ((_listener) = (*(( (struct memory_listeners *)((&memory_listeners)->tqh_last ))->tqh_last)); (_listener); (_listener) = (*(((struct memory_listeners *)((_listener)->link.tqe_prev))->tqh_last))) { if (_listener ->log_global_start) { _listener->log_global_start(_listener ); } } break; default: abort(); } } while (0); | |||||
1638 | } | |||||
1639 | ||||||
1640 | void memory_global_dirty_log_stop(void) | |||||
1641 | { | |||||
1642 | global_dirty_log = false0; | |||||
1643 | MEMORY_LISTENER_CALL_GLOBAL(log_global_stop, Reverse)do { MemoryListener *_listener; switch (Reverse) { case Forward : for ((_listener) = ((&memory_listeners)->tqh_first); (_listener); (_listener) = ((_listener)->link.tqe_next)) { if (_listener->log_global_stop) { _listener->log_global_stop (_listener); } } break; case Reverse: for ((_listener) = (*(( (struct memory_listeners *)((&memory_listeners)->tqh_last ))->tqh_last)); (_listener); (_listener) = (*(((struct memory_listeners *)((_listener)->link.tqe_prev))->tqh_last))) { if (_listener ->log_global_stop) { _listener->log_global_stop(_listener ); } } break; default: abort(); } } while (0); | |||||
1644 | } | |||||
1645 | ||||||
1646 | static void listener_add_address_space(MemoryListener *listener, | |||||
1647 | AddressSpace *as) | |||||
1648 | { | |||||
1649 | FlatView *view; | |||||
1650 | FlatRange *fr; | |||||
1651 | ||||||
1652 | if (listener->address_space_filter | |||||
1653 | && listener->address_space_filter != as) { | |||||
1654 | return; | |||||
1655 | } | |||||
1656 | ||||||
1657 | if (global_dirty_log) { | |||||
1658 | if (listener->log_global_start) { | |||||
1659 | listener->log_global_start(listener); | |||||
1660 | } | |||||
1661 | } | |||||
1662 | ||||||
1663 | view = address_space_get_flatview(as); | |||||
1664 | FOR_EACH_FLAT_RANGE(fr, view)for (fr = (view)->ranges; fr < (view)->ranges + (view )->nr; ++fr) { | |||||
1665 | MemoryRegionSection section = { | |||||
1666 | .mr = fr->mr, | |||||
1667 | .address_space = as, | |||||
1668 | .offset_within_region = fr->offset_in_region, | |||||
1669 | .size = fr->addr.size, | |||||
1670 | .offset_within_address_space = int128_get64(fr->addr.start), | |||||
1671 | .readonly = fr->readonly, | |||||
1672 | }; | |||||
1673 | if (listener->region_add) { | |||||
1674 | listener->region_add(listener, §ion); | |||||
1675 | } | |||||
1676 | } | |||||
1677 | flatview_unref(view); | |||||
1678 | } | |||||
1679 | ||||||
1680 | void memory_listener_register(MemoryListener *listener, AddressSpace *filter) | |||||
1681 | { | |||||
1682 | MemoryListener *other = NULL((void*)0); | |||||
1683 | AddressSpace *as; | |||||
1684 | ||||||
1685 | listener->address_space_filter = filter; | |||||
1686 | if (QTAILQ_EMPTY(&memory_listeners)((&memory_listeners)->tqh_first == ((void*)0)) | |||||
| ||||||
1687 | || listener->priority >= QTAILQ_LAST(&memory_listeners,(*(((struct memory_listeners *)((&memory_listeners)->tqh_last ))->tqh_last)) | |||||
1688 | memory_listeners)(*(((struct memory_listeners *)((&memory_listeners)->tqh_last ))->tqh_last))->priority) { | |||||
1689 | QTAILQ_INSERT_TAIL(&memory_listeners, listener, link)do { (listener)->link.tqe_next = ((void*)0); (listener)-> link.tqe_prev = (&memory_listeners)->tqh_last; *(& memory_listeners)->tqh_last = (listener); (&memory_listeners )->tqh_last = &(listener)->link.tqe_next; } while ( 0); | |||||
1690 | } else { | |||||
1691 | QTAILQ_FOREACH(other, &memory_listeners, link)for ((other) = ((&memory_listeners)->tqh_first); (other ); (other) = ((other)->link.tqe_next)) { | |||||
1692 | if (listener->priority < other->priority) { | |||||
1693 | break; | |||||
1694 | } | |||||
1695 | } | |||||
1696 | QTAILQ_INSERT_BEFORE(other, listener, link)do { (listener)->link.tqe_prev = (other)->link.tqe_prev ; (listener)->link.tqe_next = (other); *(other)->link.tqe_prev = (listener); (other)->link.tqe_prev = &(listener)-> link.tqe_next; } while ( 0); | |||||
| ||||||
1697 | } | |||||
1698 | ||||||
1699 | QTAILQ_FOREACH(as, &address_spaces, address_spaces_link)for ((as) = ((&address_spaces)->tqh_first); (as); (as) = ((as)->address_spaces_link.tqe_next)) { | |||||
1700 | listener_add_address_space(listener, as); | |||||
1701 | } | |||||
1702 | } | |||||
1703 | ||||||
1704 | void memory_listener_unregister(MemoryListener *listener) | |||||
1705 | { | |||||
1706 | QTAILQ_REMOVE(&memory_listeners, listener, link)do { if (((listener)->link.tqe_next) != ((void*)0)) (listener )->link.tqe_next->link.tqe_prev = (listener)->link.tqe_prev ; else (&memory_listeners)->tqh_last = (listener)-> link.tqe_prev; *(listener)->link.tqe_prev = (listener)-> link.tqe_next; } while ( 0); | |||||
1707 | } | |||||
1708 | ||||||
1709 | void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name) | |||||
1710 | { | |||||
1711 | if (QTAILQ_EMPTY(&address_spaces)((&address_spaces)->tqh_first == ((void*)0))) { | |||||
1712 | memory_init(); | |||||
1713 | } | |||||
1714 | ||||||
1715 | memory_region_transaction_begin(); | |||||
1716 | as->root = root; | |||||
1717 | as->current_map = g_new(FlatView, 1)((FlatView *) g_malloc_n ((1), sizeof (FlatView))); | |||||
1718 | flatview_init(as->current_map); | |||||
1719 | as->ioeventfd_nb = 0; | |||||
1720 | as->ioeventfds = NULL((void*)0); | |||||
1721 | QTAILQ_INSERT_TAIL(&address_spaces, as, address_spaces_link)do { (as)->address_spaces_link.tqe_next = ((void*)0); (as) ->address_spaces_link.tqe_prev = (&address_spaces)-> tqh_last; *(&address_spaces)->tqh_last = (as); (&address_spaces )->tqh_last = &(as)->address_spaces_link.tqe_next; } while ( 0); | |||||
1722 | as->name = g_strdup(name ? name : "anonymous"); | |||||
1723 | address_space_init_dispatch(as); | |||||
1724 | memory_region_update_pending |= root->enabled; | |||||
1725 | memory_region_transaction_commit(); | |||||
1726 | } | |||||
1727 | ||||||
1728 | void address_space_destroy(AddressSpace *as) | |||||
1729 | { | |||||
1730 | /* Flush out anything from MemoryListeners listening in on this */ | |||||
1731 | memory_region_transaction_begin(); | |||||
1732 | as->root = NULL((void*)0); | |||||
1733 | memory_region_transaction_commit(); | |||||
1734 | QTAILQ_REMOVE(&address_spaces, as, address_spaces_link)do { if (((as)->address_spaces_link.tqe_next) != ((void*)0 )) (as)->address_spaces_link.tqe_next->address_spaces_link .tqe_prev = (as)->address_spaces_link.tqe_prev; else (& address_spaces)->tqh_last = (as)->address_spaces_link.tqe_prev ; *(as)->address_spaces_link.tqe_prev = (as)->address_spaces_link .tqe_next; } while ( 0); | |||||
1735 | address_space_destroy_dispatch(as); | |||||
1736 | flatview_unref(as->current_map); | |||||
1737 | g_free(as->name); | |||||
1738 | g_free(as->ioeventfds); | |||||
1739 | } | |||||
1740 | ||||||
1741 | bool_Bool io_mem_read(MemoryRegion *mr, hwaddr addr, uint64_t *pval, unsigned size) | |||||
1742 | { | |||||
1743 | return memory_region_dispatch_read(mr, addr, pval, size); | |||||
1744 | } | |||||
1745 | ||||||
1746 | bool_Bool io_mem_write(MemoryRegion *mr, hwaddr addr, | |||||
1747 | uint64_t val, unsigned size) | |||||
1748 | { | |||||
1749 | return memory_region_dispatch_write(mr, addr, val, size); | |||||
1750 | } | |||||
1751 | ||||||
1752 | typedef struct MemoryRegionList MemoryRegionList; | |||||
1753 | ||||||
1754 | struct MemoryRegionList { | |||||
1755 | const MemoryRegion *mr; | |||||
1756 | bool_Bool printed; | |||||
1757 | QTAILQ_ENTRY(MemoryRegionList)struct { struct MemoryRegionList *tqe_next; struct MemoryRegionList * *tqe_prev; } queue; | |||||
1758 | }; | |||||
1759 | ||||||
1760 | typedef QTAILQ_HEAD(queue, MemoryRegionList)struct queue { struct MemoryRegionList *tqh_first; struct MemoryRegionList * *tqh_last; } MemoryRegionListHead; | |||||
1761 | ||||||
1762 | static void mtree_print_mr(fprintf_function mon_printf, void *f, | |||||
1763 | const MemoryRegion *mr, unsigned int level, | |||||
1764 | hwaddr base, | |||||
1765 | MemoryRegionListHead *alias_print_queue) | |||||
1766 | { | |||||
1767 | MemoryRegionList *new_ml, *ml, *next_ml; | |||||
1768 | MemoryRegionListHead submr_print_queue; | |||||
1769 | const MemoryRegion *submr; | |||||
1770 | unsigned int i; | |||||
1771 | ||||||
1772 | if (!mr || !mr->enabled) { | |||||
1773 | return; | |||||
1774 | } | |||||
1775 | ||||||
1776 | for (i = 0; i < level; i++) { | |||||
1777 | mon_printf(f, " "); | |||||
1778 | } | |||||
1779 | ||||||
1780 | if (mr->alias) { | |||||
1781 | MemoryRegionList *ml; | |||||
1782 | bool_Bool found = false0; | |||||
1783 | ||||||
1784 | /* check if the alias is already in the queue */ | |||||
1785 | QTAILQ_FOREACH(ml, alias_print_queue, queue)for ((ml) = ((alias_print_queue)->tqh_first); (ml); (ml) = ((ml)->queue.tqe_next)) { | |||||
1786 | if (ml->mr == mr->alias && !ml->printed) { | |||||
1787 | found = true1; | |||||
1788 | } | |||||
1789 | } | |||||
1790 | ||||||
1791 | if (!found) { | |||||
1792 | ml = g_new(MemoryRegionList, 1)((MemoryRegionList *) g_malloc_n ((1), sizeof (MemoryRegionList ))); | |||||
1793 | ml->mr = mr->alias; | |||||
1794 | ml->printed = false0; | |||||
1795 | QTAILQ_INSERT_TAIL(alias_print_queue, ml, queue)do { (ml)->queue.tqe_next = ((void*)0); (ml)->queue.tqe_prev = (alias_print_queue)->tqh_last; *(alias_print_queue)-> tqh_last = (ml); (alias_print_queue)->tqh_last = &(ml) ->queue.tqe_next; } while ( 0); | |||||
1796 | } | |||||
1797 | mon_printf(f, TARGET_FMT_plx"%016" "l" "x" "-" TARGET_FMT_plx"%016" "l" "x" | |||||
1798 | " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx"%016" "l" "x" | |||||
1799 | "-" TARGET_FMT_plx"%016" "l" "x" "\n", | |||||
1800 | base + mr->addr, | |||||
1801 | base + mr->addr | |||||
1802 | + (int128_nz(mr->size) ? | |||||
1803 | (hwaddr)int128_get64(int128_sub(mr->size, | |||||
1804 | int128_one())) : 0), | |||||
1805 | mr->priority, | |||||
1806 | mr->romd_mode ? 'R' : '-', | |||||
1807 | !mr->readonly && !(mr->rom_device && mr->romd_mode) ? 'W' | |||||
1808 | : '-', | |||||
1809 | mr->name, | |||||
1810 | mr->alias->name, | |||||
1811 | mr->alias_offset, | |||||
1812 | mr->alias_offset | |||||
1813 | + (int128_nz(mr->size) ? | |||||
1814 | (hwaddr)int128_get64(int128_sub(mr->size, | |||||
1815 | int128_one())) : 0)); | |||||
1816 | } else { | |||||
1817 | mon_printf(f, | |||||
1818 | TARGET_FMT_plx"%016" "l" "x" "-" TARGET_FMT_plx"%016" "l" "x" " (prio %d, %c%c): %s\n", | |||||
1819 | base + mr->addr, | |||||
1820 | base + mr->addr | |||||
1821 | + (int128_nz(mr->size) ? | |||||
1822 | (hwaddr)int128_get64(int128_sub(mr->size, | |||||
1823 | int128_one())) : 0), | |||||
1824 | mr->priority, | |||||
1825 | mr->romd_mode ? 'R' : '-', | |||||
1826 | !mr->readonly && !(mr->rom_device && mr->romd_mode) ? 'W' | |||||
1827 | : '-', | |||||
1828 | mr->name); | |||||
1829 | } | |||||
1830 | ||||||
1831 | QTAILQ_INIT(&submr_print_queue)do { (&submr_print_queue)->tqh_first = ((void*)0); (& submr_print_queue)->tqh_last = &(&submr_print_queue )->tqh_first; } while ( 0); | |||||
1832 | ||||||
1833 | QTAILQ_FOREACH(submr, &mr->subregions, subregions_link)for ((submr) = ((&mr->subregions)->tqh_first); (submr ); (submr) = ((submr)->subregions_link.tqe_next)) { | |||||
1834 | new_ml = g_new(MemoryRegionList, 1)((MemoryRegionList *) g_malloc_n ((1), sizeof (MemoryRegionList ))); | |||||
1835 | new_ml->mr = submr; | |||||
1836 | QTAILQ_FOREACH(ml, &submr_print_queue, queue)for ((ml) = ((&submr_print_queue)->tqh_first); (ml); ( ml) = ((ml)->queue.tqe_next)) { | |||||
1837 | if (new_ml->mr->addr < ml->mr->addr || | |||||
1838 | (new_ml->mr->addr == ml->mr->addr && | |||||
1839 | new_ml->mr->priority > ml->mr->priority)) { | |||||
1840 | QTAILQ_INSERT_BEFORE(ml, new_ml, queue)do { (new_ml)->queue.tqe_prev = (ml)->queue.tqe_prev; ( new_ml)->queue.tqe_next = (ml); *(ml)->queue.tqe_prev = (new_ml); (ml)->queue.tqe_prev = &(new_ml)->queue. tqe_next; } while ( 0); | |||||
1841 | new_ml = NULL((void*)0); | |||||
1842 | break; | |||||
1843 | } | |||||
1844 | } | |||||
1845 | if (new_ml) { | |||||
1846 | QTAILQ_INSERT_TAIL(&submr_print_queue, new_ml, queue)do { (new_ml)->queue.tqe_next = ((void*)0); (new_ml)->queue .tqe_prev = (&submr_print_queue)->tqh_last; *(&submr_print_queue )->tqh_last = (new_ml); (&submr_print_queue)->tqh_last = &(new_ml)->queue.tqe_next; } while ( 0); | |||||
1847 | } | |||||
1848 | } | |||||
1849 | ||||||
1850 | QTAILQ_FOREACH(ml, &submr_print_queue, queue)for ((ml) = ((&submr_print_queue)->tqh_first); (ml); ( ml) = ((ml)->queue.tqe_next)) { | |||||
1851 | mtree_print_mr(mon_printf, f, ml->mr, level + 1, base + mr->addr, | |||||
1852 | alias_print_queue); | |||||
1853 | } | |||||
1854 | ||||||
1855 | QTAILQ_FOREACH_SAFE(ml, &submr_print_queue, queue, next_ml)for ((ml) = ((&submr_print_queue)->tqh_first); (ml) && ((next_ml) = ((ml)->queue.tqe_next), 1); (ml) = (next_ml) ) { | |||||
1856 | g_free(ml); | |||||
1857 | } | |||||
1858 | } | |||||
1859 | ||||||
1860 | void mtree_info(fprintf_function mon_printf, void *f) | |||||
1861 | { | |||||
1862 | MemoryRegionListHead ml_head; | |||||
1863 | MemoryRegionList *ml, *ml2; | |||||
1864 | AddressSpace *as; | |||||
1865 | ||||||
1866 | QTAILQ_INIT(&ml_head)do { (&ml_head)->tqh_first = ((void*)0); (&ml_head )->tqh_last = &(&ml_head)->tqh_first; } while ( 0); | |||||
1867 | ||||||
1868 | QTAILQ_FOREACH(as, &address_spaces, address_spaces_link)for ((as) = ((&address_spaces)->tqh_first); (as); (as) = ((as)->address_spaces_link.tqe_next)) { | |||||
1869 | mon_printf(f, "%s\n", as->name); | |||||
1870 | mtree_print_mr(mon_printf, f, as->root, 0, 0, &ml_head); | |||||
1871 | } | |||||
1872 | ||||||
1873 | mon_printf(f, "aliases\n"); | |||||
1874 | /* print aliased regions */ | |||||
1875 | QTAILQ_FOREACH(ml, &ml_head, queue)for ((ml) = ((&ml_head)->tqh_first); (ml); (ml) = ((ml )->queue.tqe_next)) { | |||||
1876 | if (!ml->printed) { | |||||
1877 | mon_printf(f, "%s\n", ml->mr->name); | |||||
1878 | mtree_print_mr(mon_printf, f, ml->mr, 0, 0, &ml_head); | |||||
1879 | } | |||||
1880 | } | |||||
1881 | ||||||
1882 | QTAILQ_FOREACH_SAFE(ml, &ml_head, queue, ml2)for ((ml) = ((&ml_head)->tqh_first); (ml) && ( (ml2) = ((ml)->queue.tqe_next), 1); (ml) = (ml2)) { | |||||
1883 | g_free(ml); | |||||
1884 | } | |||||
1885 | } |