root / exec.c @ 91dbed4b
History | View | Annotate | Download (118.5 kB)
1 | 54936004 | bellard | /*
|
---|---|---|---|
2 | fd6ce8f6 | bellard | * virtual page mapping and translated block handling
|
3 | 5fafdf24 | ths | *
|
4 | 54936004 | bellard | * Copyright (c) 2003 Fabrice Bellard
|
5 | 54936004 | bellard | *
|
6 | 54936004 | bellard | * This library is free software; you can redistribute it and/or
|
7 | 54936004 | bellard | * modify it under the terms of the GNU Lesser General Public
|
8 | 54936004 | bellard | * License as published by the Free Software Foundation; either
|
9 | 54936004 | bellard | * version 2 of the License, or (at your option) any later version.
|
10 | 54936004 | bellard | *
|
11 | 54936004 | bellard | * This library is distributed in the hope that it will be useful,
|
12 | 54936004 | bellard | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 54936004 | bellard | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 54936004 | bellard | * Lesser General Public License for more details.
|
15 | 54936004 | bellard | *
|
16 | 54936004 | bellard | * You should have received a copy of the GNU Lesser General Public
|
17 | 8167ee88 | Blue Swirl | * License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
18 | 54936004 | bellard | */
|
19 | 67b915a5 | bellard | #include "config.h" |
20 | d5a8f07c | bellard | #ifdef _WIN32
|
21 | d5a8f07c | bellard | #include <windows.h> |
22 | d5a8f07c | bellard | #else
|
23 | a98d49b1 | bellard | #include <sys/types.h> |
24 | d5a8f07c | bellard | #include <sys/mman.h> |
25 | d5a8f07c | bellard | #endif
|
26 | 54936004 | bellard | #include <stdlib.h> |
27 | 54936004 | bellard | #include <stdio.h> |
28 | 54936004 | bellard | #include <stdarg.h> |
29 | 54936004 | bellard | #include <string.h> |
30 | 54936004 | bellard | #include <errno.h> |
31 | 54936004 | bellard | #include <unistd.h> |
32 | 54936004 | bellard | #include <inttypes.h> |
33 | 54936004 | bellard | |
34 | 6180a181 | bellard | #include "cpu.h" |
35 | 6180a181 | bellard | #include "exec-all.h" |
36 | ca10f867 | aurel32 | #include "qemu-common.h" |
37 | b67d9a52 | bellard | #include "tcg.h" |
38 | b3c7724c | pbrook | #include "hw/hw.h" |
39 | 74576198 | aliguori | #include "osdep.h" |
40 | 7ba1e619 | aliguori | #include "kvm.h" |
41 | 53a5960a | pbrook | #if defined(CONFIG_USER_ONLY)
|
42 | 53a5960a | pbrook | #include <qemu.h> |
43 | fd052bf6 | Riku Voipio | #include <signal.h> |
44 | 53a5960a | pbrook | #endif
|
45 | 54936004 | bellard | |
46 | fd6ce8f6 | bellard | //#define DEBUG_TB_INVALIDATE
|
47 | 66e85a21 | bellard | //#define DEBUG_FLUSH
|
48 | 9fa3e853 | bellard | //#define DEBUG_TLB
|
49 | 67d3b957 | pbrook | //#define DEBUG_UNASSIGNED
|
50 | fd6ce8f6 | bellard | |
51 | fd6ce8f6 | bellard | /* make various TB consistency checks */
|
52 | 5fafdf24 | ths | //#define DEBUG_TB_CHECK
|
53 | 5fafdf24 | ths | //#define DEBUG_TLB_CHECK
|
54 | fd6ce8f6 | bellard | |
55 | 1196be37 | ths | //#define DEBUG_IOPORT
|
56 | db7b5426 | blueswir1 | //#define DEBUG_SUBPAGE
|
57 | 1196be37 | ths | |
58 | 99773bd4 | pbrook | #if !defined(CONFIG_USER_ONLY)
|
59 | 99773bd4 | pbrook | /* TB consistency checks only implemented for usermode emulation. */
|
60 | 99773bd4 | pbrook | #undef DEBUG_TB_CHECK
|
61 | 99773bd4 | pbrook | #endif
|
62 | 99773bd4 | pbrook | |
63 | 9fa3e853 | bellard | #define SMC_BITMAP_USE_THRESHOLD 10 |
64 | 9fa3e853 | bellard | |
65 | bdaf78e0 | blueswir1 | static TranslationBlock *tbs;
|
66 | 26a5f13b | bellard | int code_gen_max_blocks;
|
67 | 9fa3e853 | bellard | TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE]; |
68 | bdaf78e0 | blueswir1 | static int nb_tbs; |
69 | eb51d102 | bellard | /* any access to the tbs or the page table must use this lock */
|
70 | c227f099 | Anthony Liguori | spinlock_t tb_lock = SPIN_LOCK_UNLOCKED; |
71 | fd6ce8f6 | bellard | |
72 | 141ac468 | blueswir1 | #if defined(__arm__) || defined(__sparc_v9__)
|
73 | 141ac468 | blueswir1 | /* The prologue must be reachable with a direct jump. ARM and Sparc64
|
74 | 141ac468 | blueswir1 | have limited branch ranges (possibly also PPC) so place it in a
|
75 | d03d860b | blueswir1 | section close to code segment. */
|
76 | d03d860b | blueswir1 | #define code_gen_section \
|
77 | d03d860b | blueswir1 | __attribute__((__section__(".gen_code"))) \
|
78 | d03d860b | blueswir1 | __attribute__((aligned (32)))
|
79 | f8e2af11 | Stefan Weil | #elif defined(_WIN32)
|
80 | f8e2af11 | Stefan Weil | /* Maximum alignment for Win32 is 16. */
|
81 | f8e2af11 | Stefan Weil | #define code_gen_section \
|
82 | f8e2af11 | Stefan Weil | __attribute__((aligned (16)))
|
83 | d03d860b | blueswir1 | #else
|
84 | d03d860b | blueswir1 | #define code_gen_section \
|
85 | d03d860b | blueswir1 | __attribute__((aligned (32)))
|
86 | d03d860b | blueswir1 | #endif
|
87 | d03d860b | blueswir1 | |
88 | d03d860b | blueswir1 | uint8_t code_gen_prologue[1024] code_gen_section;
|
89 | bdaf78e0 | blueswir1 | static uint8_t *code_gen_buffer;
|
90 | bdaf78e0 | blueswir1 | static unsigned long code_gen_buffer_size; |
91 | 26a5f13b | bellard | /* threshold to flush the translated code buffer */
|
92 | bdaf78e0 | blueswir1 | static unsigned long code_gen_buffer_max_size; |
93 | fd6ce8f6 | bellard | uint8_t *code_gen_ptr; |
94 | fd6ce8f6 | bellard | |
95 | e2eef170 | pbrook | #if !defined(CONFIG_USER_ONLY)
|
96 | 9fa3e853 | bellard | int phys_ram_fd;
|
97 | 1ccde1cb | bellard | uint8_t *phys_ram_dirty; |
98 | 74576198 | aliguori | static int in_migration; |
99 | 94a6b54f | pbrook | |
100 | 94a6b54f | pbrook | typedef struct RAMBlock { |
101 | 94a6b54f | pbrook | uint8_t *host; |
102 | c227f099 | Anthony Liguori | ram_addr_t offset; |
103 | c227f099 | Anthony Liguori | ram_addr_t length; |
104 | 94a6b54f | pbrook | struct RAMBlock *next;
|
105 | 94a6b54f | pbrook | } RAMBlock; |
106 | 94a6b54f | pbrook | |
107 | 94a6b54f | pbrook | static RAMBlock *ram_blocks;
|
108 | 94a6b54f | pbrook | /* TODO: When we implement (and use) ram deallocation (e.g. for hotplug)
|
109 | ccbb4d44 | Stuart Brady | then we can no longer assume contiguous ram offsets, and external uses
|
110 | 94a6b54f | pbrook | of this variable will break. */
|
111 | c227f099 | Anthony Liguori | ram_addr_t last_ram_offset; |
112 | e2eef170 | pbrook | #endif
|
113 | 9fa3e853 | bellard | |
114 | 6a00d601 | bellard | CPUState *first_cpu; |
115 | 6a00d601 | bellard | /* current CPU in the current thread. It is only valid inside
|
116 | 6a00d601 | bellard | cpu_exec() */
|
117 | 5fafdf24 | ths | CPUState *cpu_single_env; |
118 | 2e70f6ef | pbrook | /* 0 = Do not count executed instructions.
|
119 | bf20dc07 | ths | 1 = Precise instruction counting.
|
120 | 2e70f6ef | pbrook | 2 = Adaptive rate instruction counting. */
|
121 | 2e70f6ef | pbrook | int use_icount = 0; |
122 | 2e70f6ef | pbrook | /* Current instruction counter. While executing translated code this may
|
123 | 2e70f6ef | pbrook | include some instructions that have not yet been executed. */
|
124 | 2e70f6ef | pbrook | int64_t qemu_icount; |
125 | 6a00d601 | bellard | |
126 | 54936004 | bellard | typedef struct PageDesc { |
127 | 92e873b9 | bellard | /* list of TBs intersecting this ram page */
|
128 | fd6ce8f6 | bellard | TranslationBlock *first_tb; |
129 | 9fa3e853 | bellard | /* in order to optimize self modifying code, we count the number
|
130 | 9fa3e853 | bellard | of lookups we do to a given page to use a bitmap */
|
131 | 9fa3e853 | bellard | unsigned int code_write_count; |
132 | 9fa3e853 | bellard | uint8_t *code_bitmap; |
133 | 9fa3e853 | bellard | #if defined(CONFIG_USER_ONLY)
|
134 | 9fa3e853 | bellard | unsigned long flags; |
135 | 9fa3e853 | bellard | #endif
|
136 | 54936004 | bellard | } PageDesc; |
137 | 54936004 | bellard | |
138 | 41c1b1c9 | Paul Brook | /* In system mode we want L1_MAP to be based on ram offsets,
|
139 | 5cd2c5b6 | Richard Henderson | while in user mode we want it to be based on virtual addresses. */
|
140 | 5cd2c5b6 | Richard Henderson | #if !defined(CONFIG_USER_ONLY)
|
141 | 41c1b1c9 | Paul Brook | #if HOST_LONG_BITS < TARGET_PHYS_ADDR_SPACE_BITS
|
142 | 41c1b1c9 | Paul Brook | # define L1_MAP_ADDR_SPACE_BITS HOST_LONG_BITS
|
143 | 41c1b1c9 | Paul Brook | #else
|
144 | 5cd2c5b6 | Richard Henderson | # define L1_MAP_ADDR_SPACE_BITS TARGET_PHYS_ADDR_SPACE_BITS
|
145 | 41c1b1c9 | Paul Brook | #endif
|
146 | bedb69ea | j_mayer | #else
|
147 | 5cd2c5b6 | Richard Henderson | # define L1_MAP_ADDR_SPACE_BITS TARGET_VIRT_ADDR_SPACE_BITS
|
148 | bedb69ea | j_mayer | #endif
|
149 | 54936004 | bellard | |
150 | 5cd2c5b6 | Richard Henderson | /* Size of the L2 (and L3, etc) page tables. */
|
151 | 5cd2c5b6 | Richard Henderson | #define L2_BITS 10 |
152 | 54936004 | bellard | #define L2_SIZE (1 << L2_BITS) |
153 | 54936004 | bellard | |
154 | 5cd2c5b6 | Richard Henderson | /* The bits remaining after N lower levels of page tables. */
|
155 | 5cd2c5b6 | Richard Henderson | #define P_L1_BITS_REM \
|
156 | 5cd2c5b6 | Richard Henderson | ((TARGET_PHYS_ADDR_SPACE_BITS - TARGET_PAGE_BITS) % L2_BITS) |
157 | 5cd2c5b6 | Richard Henderson | #define V_L1_BITS_REM \
|
158 | 5cd2c5b6 | Richard Henderson | ((L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS) % L2_BITS) |
159 | 5cd2c5b6 | Richard Henderson | |
160 | 5cd2c5b6 | Richard Henderson | /* Size of the L1 page table. Avoid silly small sizes. */
|
161 | 5cd2c5b6 | Richard Henderson | #if P_L1_BITS_REM < 4 |
162 | 5cd2c5b6 | Richard Henderson | #define P_L1_BITS (P_L1_BITS_REM + L2_BITS)
|
163 | 5cd2c5b6 | Richard Henderson | #else
|
164 | 5cd2c5b6 | Richard Henderson | #define P_L1_BITS P_L1_BITS_REM
|
165 | 5cd2c5b6 | Richard Henderson | #endif
|
166 | 5cd2c5b6 | Richard Henderson | |
167 | 5cd2c5b6 | Richard Henderson | #if V_L1_BITS_REM < 4 |
168 | 5cd2c5b6 | Richard Henderson | #define V_L1_BITS (V_L1_BITS_REM + L2_BITS)
|
169 | 5cd2c5b6 | Richard Henderson | #else
|
170 | 5cd2c5b6 | Richard Henderson | #define V_L1_BITS V_L1_BITS_REM
|
171 | 5cd2c5b6 | Richard Henderson | #endif
|
172 | 5cd2c5b6 | Richard Henderson | |
173 | 5cd2c5b6 | Richard Henderson | #define P_L1_SIZE ((target_phys_addr_t)1 << P_L1_BITS) |
174 | 5cd2c5b6 | Richard Henderson | #define V_L1_SIZE ((target_ulong)1 << V_L1_BITS) |
175 | 5cd2c5b6 | Richard Henderson | |
176 | 5cd2c5b6 | Richard Henderson | #define P_L1_SHIFT (TARGET_PHYS_ADDR_SPACE_BITS - TARGET_PAGE_BITS - P_L1_BITS)
|
177 | 5cd2c5b6 | Richard Henderson | #define V_L1_SHIFT (L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS - V_L1_BITS)
|
178 | 5cd2c5b6 | Richard Henderson | |
179 | 83fb7adf | bellard | unsigned long qemu_real_host_page_size; |
180 | 83fb7adf | bellard | unsigned long qemu_host_page_bits; |
181 | 83fb7adf | bellard | unsigned long qemu_host_page_size; |
182 | 83fb7adf | bellard | unsigned long qemu_host_page_mask; |
183 | 54936004 | bellard | |
184 | 5cd2c5b6 | Richard Henderson | /* This is a multi-level map on the virtual address space.
|
185 | 5cd2c5b6 | Richard Henderson | The bottom level has pointers to PageDesc. */
|
186 | 5cd2c5b6 | Richard Henderson | static void *l1_map[V_L1_SIZE]; |
187 | 54936004 | bellard | |
188 | e2eef170 | pbrook | #if !defined(CONFIG_USER_ONLY)
|
189 | 41c1b1c9 | Paul Brook | typedef struct PhysPageDesc { |
190 | 41c1b1c9 | Paul Brook | /* offset in host memory of the page + io_index in the low bits */
|
191 | 41c1b1c9 | Paul Brook | ram_addr_t phys_offset; |
192 | 41c1b1c9 | Paul Brook | ram_addr_t region_offset; |
193 | 41c1b1c9 | Paul Brook | } PhysPageDesc; |
194 | 41c1b1c9 | Paul Brook | |
195 | 5cd2c5b6 | Richard Henderson | /* This is a multi-level map on the physical address space.
|
196 | 5cd2c5b6 | Richard Henderson | The bottom level has pointers to PhysPageDesc. */
|
197 | 5cd2c5b6 | Richard Henderson | static void *l1_phys_map[P_L1_SIZE]; |
198 | 6d9a1304 | Paul Brook | |
199 | e2eef170 | pbrook | static void io_mem_init(void); |
200 | e2eef170 | pbrook | |
201 | 33417e70 | bellard | /* io memory support */
|
202 | 33417e70 | bellard | CPUWriteMemoryFunc *io_mem_write[IO_MEM_NB_ENTRIES][4];
|
203 | 33417e70 | bellard | CPUReadMemoryFunc *io_mem_read[IO_MEM_NB_ENTRIES][4];
|
204 | a4193c8a | bellard | void *io_mem_opaque[IO_MEM_NB_ENTRIES];
|
205 | 511d2b14 | blueswir1 | static char io_mem_used[IO_MEM_NB_ENTRIES]; |
206 | 6658ffb8 | pbrook | static int io_mem_watch; |
207 | 6658ffb8 | pbrook | #endif
|
208 | 33417e70 | bellard | |
209 | 34865134 | bellard | /* log support */
|
210 | 1e8b27ca | Juha Riihimรคki | #ifdef WIN32
|
211 | 1e8b27ca | Juha Riihimรคki | static const char *logfilename = "qemu.log"; |
212 | 1e8b27ca | Juha Riihimรคki | #else
|
213 | d9b630fd | blueswir1 | static const char *logfilename = "/tmp/qemu.log"; |
214 | 1e8b27ca | Juha Riihimรคki | #endif
|
215 | 34865134 | bellard | FILE *logfile; |
216 | 34865134 | bellard | int loglevel;
|
217 | e735b91c | pbrook | static int log_append = 0; |
218 | 34865134 | bellard | |
219 | e3db7226 | bellard | /* statistics */
|
220 | b3755a91 | Paul Brook | #if !defined(CONFIG_USER_ONLY)
|
221 | e3db7226 | bellard | static int tlb_flush_count; |
222 | b3755a91 | Paul Brook | #endif
|
223 | e3db7226 | bellard | static int tb_flush_count; |
224 | e3db7226 | bellard | static int tb_phys_invalidate_count; |
225 | e3db7226 | bellard | |
226 | 7cb69cae | bellard | #ifdef _WIN32
|
227 | 7cb69cae | bellard | static void map_exec(void *addr, long size) |
228 | 7cb69cae | bellard | { |
229 | 7cb69cae | bellard | DWORD old_protect; |
230 | 7cb69cae | bellard | VirtualProtect(addr, size, |
231 | 7cb69cae | bellard | PAGE_EXECUTE_READWRITE, &old_protect); |
232 | 7cb69cae | bellard | |
233 | 7cb69cae | bellard | } |
234 | 7cb69cae | bellard | #else
|
235 | 7cb69cae | bellard | static void map_exec(void *addr, long size) |
236 | 7cb69cae | bellard | { |
237 | 4369415f | bellard | unsigned long start, end, page_size; |
238 | 7cb69cae | bellard | |
239 | 4369415f | bellard | page_size = getpagesize(); |
240 | 7cb69cae | bellard | start = (unsigned long)addr; |
241 | 4369415f | bellard | start &= ~(page_size - 1);
|
242 | 7cb69cae | bellard | |
243 | 7cb69cae | bellard | end = (unsigned long)addr + size; |
244 | 4369415f | bellard | end += page_size - 1;
|
245 | 4369415f | bellard | end &= ~(page_size - 1);
|
246 | 7cb69cae | bellard | |
247 | 7cb69cae | bellard | mprotect((void *)start, end - start,
|
248 | 7cb69cae | bellard | PROT_READ | PROT_WRITE | PROT_EXEC); |
249 | 7cb69cae | bellard | } |
250 | 7cb69cae | bellard | #endif
|
251 | 7cb69cae | bellard | |
252 | b346ff46 | bellard | static void page_init(void) |
253 | 54936004 | bellard | { |
254 | 83fb7adf | bellard | /* NOTE: we can always suppose that qemu_host_page_size >=
|
255 | 54936004 | bellard | TARGET_PAGE_SIZE */
|
256 | c2b48b69 | aliguori | #ifdef _WIN32
|
257 | c2b48b69 | aliguori | { |
258 | c2b48b69 | aliguori | SYSTEM_INFO system_info; |
259 | c2b48b69 | aliguori | |
260 | c2b48b69 | aliguori | GetSystemInfo(&system_info); |
261 | c2b48b69 | aliguori | qemu_real_host_page_size = system_info.dwPageSize; |
262 | c2b48b69 | aliguori | } |
263 | c2b48b69 | aliguori | #else
|
264 | c2b48b69 | aliguori | qemu_real_host_page_size = getpagesize(); |
265 | c2b48b69 | aliguori | #endif
|
266 | 83fb7adf | bellard | if (qemu_host_page_size == 0) |
267 | 83fb7adf | bellard | qemu_host_page_size = qemu_real_host_page_size; |
268 | 83fb7adf | bellard | if (qemu_host_page_size < TARGET_PAGE_SIZE)
|
269 | 83fb7adf | bellard | qemu_host_page_size = TARGET_PAGE_SIZE; |
270 | 83fb7adf | bellard | qemu_host_page_bits = 0;
|
271 | 83fb7adf | bellard | while ((1 << qemu_host_page_bits) < qemu_host_page_size) |
272 | 83fb7adf | bellard | qemu_host_page_bits++; |
273 | 83fb7adf | bellard | qemu_host_page_mask = ~(qemu_host_page_size - 1);
|
274 | 50a9569b | balrog | |
275 | 50a9569b | balrog | #if !defined(_WIN32) && defined(CONFIG_USER_ONLY)
|
276 | 50a9569b | balrog | { |
277 | 50a9569b | balrog | FILE *f; |
278 | 50a9569b | balrog | |
279 | 0776590d | pbrook | last_brk = (unsigned long)sbrk(0); |
280 | 5cd2c5b6 | Richard Henderson | |
281 | 50a9569b | balrog | f = fopen("/proc/self/maps", "r"); |
282 | 50a9569b | balrog | if (f) {
|
283 | 5cd2c5b6 | Richard Henderson | mmap_lock(); |
284 | 5cd2c5b6 | Richard Henderson | |
285 | 50a9569b | balrog | do {
|
286 | 5cd2c5b6 | Richard Henderson | unsigned long startaddr, endaddr; |
287 | 5cd2c5b6 | Richard Henderson | int n;
|
288 | 5cd2c5b6 | Richard Henderson | |
289 | 5cd2c5b6 | Richard Henderson | n = fscanf (f, "%lx-%lx %*[^\n]\n", &startaddr, &endaddr);
|
290 | 5cd2c5b6 | Richard Henderson | |
291 | 5cd2c5b6 | Richard Henderson | if (n == 2 && h2g_valid(startaddr)) { |
292 | 5cd2c5b6 | Richard Henderson | startaddr = h2g(startaddr) & TARGET_PAGE_MASK; |
293 | 5cd2c5b6 | Richard Henderson | |
294 | 5cd2c5b6 | Richard Henderson | if (h2g_valid(endaddr)) {
|
295 | 5cd2c5b6 | Richard Henderson | endaddr = h2g(endaddr); |
296 | 5cd2c5b6 | Richard Henderson | } else {
|
297 | 5cd2c5b6 | Richard Henderson | endaddr = ~0ul;
|
298 | 5cd2c5b6 | Richard Henderson | } |
299 | 5cd2c5b6 | Richard Henderson | page_set_flags(startaddr, endaddr, PAGE_RESERVED); |
300 | 50a9569b | balrog | } |
301 | 50a9569b | balrog | } while (!feof(f));
|
302 | 5cd2c5b6 | Richard Henderson | |
303 | 50a9569b | balrog | fclose(f); |
304 | 5cd2c5b6 | Richard Henderson | mmap_unlock(); |
305 | 50a9569b | balrog | } |
306 | 50a9569b | balrog | } |
307 | 50a9569b | balrog | #endif
|
308 | 54936004 | bellard | } |
309 | 54936004 | bellard | |
310 | 41c1b1c9 | Paul Brook | static PageDesc *page_find_alloc(tb_page_addr_t index, int alloc) |
311 | 54936004 | bellard | { |
312 | 41c1b1c9 | Paul Brook | PageDesc *pd; |
313 | 41c1b1c9 | Paul Brook | void **lp;
|
314 | 41c1b1c9 | Paul Brook | int i;
|
315 | 41c1b1c9 | Paul Brook | |
316 | 5cd2c5b6 | Richard Henderson | #if defined(CONFIG_USER_ONLY)
|
317 | 5cd2c5b6 | Richard Henderson | /* We can't use qemu_malloc because it may recurse into a locked mutex.
|
318 | 5cd2c5b6 | Richard Henderson | Neither can we record the new pages we reserve while allocating a
|
319 | 5cd2c5b6 | Richard Henderson | given page because that may recurse into an unallocated page table
|
320 | 5cd2c5b6 | Richard Henderson | entry. Stuff the allocations we do make into a queue and process
|
321 | 5cd2c5b6 | Richard Henderson | them after having completed one entire page table allocation. */
|
322 | 5cd2c5b6 | Richard Henderson | |
323 | 5cd2c5b6 | Richard Henderson | unsigned long reserve[2 * (V_L1_SHIFT / L2_BITS)]; |
324 | 5cd2c5b6 | Richard Henderson | int reserve_idx = 0; |
325 | 5cd2c5b6 | Richard Henderson | |
326 | 5cd2c5b6 | Richard Henderson | # define ALLOC(P, SIZE) \
|
327 | 5cd2c5b6 | Richard Henderson | do { \
|
328 | 5cd2c5b6 | Richard Henderson | P = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, \
|
329 | 5cd2c5b6 | Richard Henderson | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); \ |
330 | 5cd2c5b6 | Richard Henderson | if (h2g_valid(P)) { \
|
331 | 5cd2c5b6 | Richard Henderson | reserve[reserve_idx] = h2g(P); \ |
332 | 5cd2c5b6 | Richard Henderson | reserve[reserve_idx + 1] = SIZE; \
|
333 | 5cd2c5b6 | Richard Henderson | reserve_idx += 2; \
|
334 | 5cd2c5b6 | Richard Henderson | } \ |
335 | 5cd2c5b6 | Richard Henderson | } while (0) |
336 | 5cd2c5b6 | Richard Henderson | #else
|
337 | 5cd2c5b6 | Richard Henderson | # define ALLOC(P, SIZE) \
|
338 | 5cd2c5b6 | Richard Henderson | do { P = qemu_mallocz(SIZE); } while (0) |
339 | 17e2377a | pbrook | #endif
|
340 | 434929bf | aliguori | |
341 | 5cd2c5b6 | Richard Henderson | /* Level 1. Always allocated. */
|
342 | 5cd2c5b6 | Richard Henderson | lp = l1_map + ((index >> V_L1_SHIFT) & (V_L1_SIZE - 1));
|
343 | 5cd2c5b6 | Richard Henderson | |
344 | 5cd2c5b6 | Richard Henderson | /* Level 2..N-1. */
|
345 | 5cd2c5b6 | Richard Henderson | for (i = V_L1_SHIFT / L2_BITS - 1; i > 0; i--) { |
346 | 5cd2c5b6 | Richard Henderson | void **p = *lp;
|
347 | 5cd2c5b6 | Richard Henderson | |
348 | 5cd2c5b6 | Richard Henderson | if (p == NULL) { |
349 | 5cd2c5b6 | Richard Henderson | if (!alloc) {
|
350 | 5cd2c5b6 | Richard Henderson | return NULL; |
351 | 5cd2c5b6 | Richard Henderson | } |
352 | 5cd2c5b6 | Richard Henderson | ALLOC(p, sizeof(void *) * L2_SIZE); |
353 | 5cd2c5b6 | Richard Henderson | *lp = p; |
354 | 17e2377a | pbrook | } |
355 | 5cd2c5b6 | Richard Henderson | |
356 | 5cd2c5b6 | Richard Henderson | lp = p + ((index >> (i * L2_BITS)) & (L2_SIZE - 1));
|
357 | 5cd2c5b6 | Richard Henderson | } |
358 | 5cd2c5b6 | Richard Henderson | |
359 | 5cd2c5b6 | Richard Henderson | pd = *lp; |
360 | 5cd2c5b6 | Richard Henderson | if (pd == NULL) { |
361 | 5cd2c5b6 | Richard Henderson | if (!alloc) {
|
362 | 5cd2c5b6 | Richard Henderson | return NULL; |
363 | 5cd2c5b6 | Richard Henderson | } |
364 | 5cd2c5b6 | Richard Henderson | ALLOC(pd, sizeof(PageDesc) * L2_SIZE);
|
365 | 5cd2c5b6 | Richard Henderson | *lp = pd; |
366 | 54936004 | bellard | } |
367 | 5cd2c5b6 | Richard Henderson | |
368 | 5cd2c5b6 | Richard Henderson | #undef ALLOC
|
369 | 5cd2c5b6 | Richard Henderson | #if defined(CONFIG_USER_ONLY)
|
370 | 5cd2c5b6 | Richard Henderson | for (i = 0; i < reserve_idx; i += 2) { |
371 | 5cd2c5b6 | Richard Henderson | unsigned long addr = reserve[i]; |
372 | 5cd2c5b6 | Richard Henderson | unsigned long len = reserve[i + 1]; |
373 | 5cd2c5b6 | Richard Henderson | |
374 | 5cd2c5b6 | Richard Henderson | page_set_flags(addr & TARGET_PAGE_MASK, |
375 | 5cd2c5b6 | Richard Henderson | TARGET_PAGE_ALIGN(addr + len), |
376 | 5cd2c5b6 | Richard Henderson | PAGE_RESERVED); |
377 | 5cd2c5b6 | Richard Henderson | } |
378 | 5cd2c5b6 | Richard Henderson | #endif
|
379 | 5cd2c5b6 | Richard Henderson | |
380 | 5cd2c5b6 | Richard Henderson | return pd + (index & (L2_SIZE - 1)); |
381 | 54936004 | bellard | } |
382 | 54936004 | bellard | |
383 | 41c1b1c9 | Paul Brook | static inline PageDesc *page_find(tb_page_addr_t index) |
384 | 54936004 | bellard | { |
385 | 5cd2c5b6 | Richard Henderson | return page_find_alloc(index, 0); |
386 | fd6ce8f6 | bellard | } |
387 | fd6ce8f6 | bellard | |
388 | 6d9a1304 | Paul Brook | #if !defined(CONFIG_USER_ONLY)
|
389 | c227f099 | Anthony Liguori | static PhysPageDesc *phys_page_find_alloc(target_phys_addr_t index, int alloc) |
390 | 92e873b9 | bellard | { |
391 | e3f4e2a4 | pbrook | PhysPageDesc *pd; |
392 | 5cd2c5b6 | Richard Henderson | void **lp;
|
393 | 5cd2c5b6 | Richard Henderson | int i;
|
394 | 92e873b9 | bellard | |
395 | 5cd2c5b6 | Richard Henderson | /* Level 1. Always allocated. */
|
396 | 5cd2c5b6 | Richard Henderson | lp = l1_phys_map + ((index >> P_L1_SHIFT) & (P_L1_SIZE - 1));
|
397 | 108c49b8 | bellard | |
398 | 5cd2c5b6 | Richard Henderson | /* Level 2..N-1. */
|
399 | 5cd2c5b6 | Richard Henderson | for (i = P_L1_SHIFT / L2_BITS - 1; i > 0; i--) { |
400 | 5cd2c5b6 | Richard Henderson | void **p = *lp;
|
401 | 5cd2c5b6 | Richard Henderson | if (p == NULL) { |
402 | 5cd2c5b6 | Richard Henderson | if (!alloc) {
|
403 | 5cd2c5b6 | Richard Henderson | return NULL; |
404 | 5cd2c5b6 | Richard Henderson | } |
405 | 5cd2c5b6 | Richard Henderson | *lp = p = qemu_mallocz(sizeof(void *) * L2_SIZE); |
406 | 5cd2c5b6 | Richard Henderson | } |
407 | 5cd2c5b6 | Richard Henderson | lp = p + ((index >> (i * L2_BITS)) & (L2_SIZE - 1));
|
408 | 108c49b8 | bellard | } |
409 | 5cd2c5b6 | Richard Henderson | |
410 | e3f4e2a4 | pbrook | pd = *lp; |
411 | 5cd2c5b6 | Richard Henderson | if (pd == NULL) { |
412 | e3f4e2a4 | pbrook | int i;
|
413 | 5cd2c5b6 | Richard Henderson | |
414 | 5cd2c5b6 | Richard Henderson | if (!alloc) {
|
415 | 108c49b8 | bellard | return NULL; |
416 | 5cd2c5b6 | Richard Henderson | } |
417 | 5cd2c5b6 | Richard Henderson | |
418 | 5cd2c5b6 | Richard Henderson | *lp = pd = qemu_malloc(sizeof(PhysPageDesc) * L2_SIZE);
|
419 | 5cd2c5b6 | Richard Henderson | |
420 | 67c4d23c | pbrook | for (i = 0; i < L2_SIZE; i++) { |
421 | 5cd2c5b6 | Richard Henderson | pd[i].phys_offset = IO_MEM_UNASSIGNED; |
422 | 5cd2c5b6 | Richard Henderson | pd[i].region_offset = (index + i) << TARGET_PAGE_BITS; |
423 | 67c4d23c | pbrook | } |
424 | 92e873b9 | bellard | } |
425 | 5cd2c5b6 | Richard Henderson | |
426 | 5cd2c5b6 | Richard Henderson | return pd + (index & (L2_SIZE - 1)); |
427 | 92e873b9 | bellard | } |
428 | 92e873b9 | bellard | |
429 | c227f099 | Anthony Liguori | static inline PhysPageDesc *phys_page_find(target_phys_addr_t index) |
430 | 92e873b9 | bellard | { |
431 | 108c49b8 | bellard | return phys_page_find_alloc(index, 0); |
432 | 92e873b9 | bellard | } |
433 | 92e873b9 | bellard | |
434 | c227f099 | Anthony Liguori | static void tlb_protect_code(ram_addr_t ram_addr); |
435 | c227f099 | Anthony Liguori | static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr, |
436 | 3a7d929e | bellard | target_ulong vaddr); |
437 | c8a706fe | pbrook | #define mmap_lock() do { } while(0) |
438 | c8a706fe | pbrook | #define mmap_unlock() do { } while(0) |
439 | 9fa3e853 | bellard | #endif
|
440 | fd6ce8f6 | bellard | |
441 | 4369415f | bellard | #define DEFAULT_CODE_GEN_BUFFER_SIZE (32 * 1024 * 1024) |
442 | 4369415f | bellard | |
443 | 4369415f | bellard | #if defined(CONFIG_USER_ONLY)
|
444 | ccbb4d44 | Stuart Brady | /* Currently it is not recommended to allocate big chunks of data in
|
445 | 4369415f | bellard | user mode. It will change when a dedicated libc will be used */
|
446 | 4369415f | bellard | #define USE_STATIC_CODE_GEN_BUFFER
|
447 | 4369415f | bellard | #endif
|
448 | 4369415f | bellard | |
449 | 4369415f | bellard | #ifdef USE_STATIC_CODE_GEN_BUFFER
|
450 | 4369415f | bellard | static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE];
|
451 | 4369415f | bellard | #endif
|
452 | 4369415f | bellard | |
453 | 8fcd3692 | blueswir1 | static void code_gen_alloc(unsigned long tb_size) |
454 | 26a5f13b | bellard | { |
455 | 4369415f | bellard | #ifdef USE_STATIC_CODE_GEN_BUFFER
|
456 | 4369415f | bellard | code_gen_buffer = static_code_gen_buffer; |
457 | 4369415f | bellard | code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE; |
458 | 4369415f | bellard | map_exec(code_gen_buffer, code_gen_buffer_size); |
459 | 4369415f | bellard | #else
|
460 | 26a5f13b | bellard | code_gen_buffer_size = tb_size; |
461 | 26a5f13b | bellard | if (code_gen_buffer_size == 0) { |
462 | 4369415f | bellard | #if defined(CONFIG_USER_ONLY)
|
463 | 4369415f | bellard | /* in user mode, phys_ram_size is not meaningful */
|
464 | 4369415f | bellard | code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE; |
465 | 4369415f | bellard | #else
|
466 | ccbb4d44 | Stuart Brady | /* XXX: needs adjustments */
|
467 | 94a6b54f | pbrook | code_gen_buffer_size = (unsigned long)(ram_size / 4); |
468 | 4369415f | bellard | #endif
|
469 | 26a5f13b | bellard | } |
470 | 26a5f13b | bellard | if (code_gen_buffer_size < MIN_CODE_GEN_BUFFER_SIZE)
|
471 | 26a5f13b | bellard | code_gen_buffer_size = MIN_CODE_GEN_BUFFER_SIZE; |
472 | 26a5f13b | bellard | /* The code gen buffer location may have constraints depending on
|
473 | 26a5f13b | bellard | the host cpu and OS */
|
474 | 26a5f13b | bellard | #if defined(__linux__)
|
475 | 26a5f13b | bellard | { |
476 | 26a5f13b | bellard | int flags;
|
477 | 141ac468 | blueswir1 | void *start = NULL; |
478 | 141ac468 | blueswir1 | |
479 | 26a5f13b | bellard | flags = MAP_PRIVATE | MAP_ANONYMOUS; |
480 | 26a5f13b | bellard | #if defined(__x86_64__)
|
481 | 26a5f13b | bellard | flags |= MAP_32BIT; |
482 | 26a5f13b | bellard | /* Cannot map more than that */
|
483 | 26a5f13b | bellard | if (code_gen_buffer_size > (800 * 1024 * 1024)) |
484 | 26a5f13b | bellard | code_gen_buffer_size = (800 * 1024 * 1024); |
485 | 141ac468 | blueswir1 | #elif defined(__sparc_v9__)
|
486 | 141ac468 | blueswir1 | // Map the buffer below 2G, so we can use direct calls and branches
|
487 | 141ac468 | blueswir1 | flags |= MAP_FIXED; |
488 | 141ac468 | blueswir1 | start = (void *) 0x60000000UL; |
489 | 141ac468 | blueswir1 | if (code_gen_buffer_size > (512 * 1024 * 1024)) |
490 | 141ac468 | blueswir1 | code_gen_buffer_size = (512 * 1024 * 1024); |
491 | 1cb0661e | balrog | #elif defined(__arm__)
|
492 | 63d41246 | balrog | /* Map the buffer below 32M, so we can use direct calls and branches */
|
493 | 1cb0661e | balrog | flags |= MAP_FIXED; |
494 | 1cb0661e | balrog | start = (void *) 0x01000000UL; |
495 | 1cb0661e | balrog | if (code_gen_buffer_size > 16 * 1024 * 1024) |
496 | 1cb0661e | balrog | code_gen_buffer_size = 16 * 1024 * 1024; |
497 | 26a5f13b | bellard | #endif
|
498 | 141ac468 | blueswir1 | code_gen_buffer = mmap(start, code_gen_buffer_size, |
499 | 141ac468 | blueswir1 | PROT_WRITE | PROT_READ | PROT_EXEC, |
500 | 26a5f13b | bellard | flags, -1, 0); |
501 | 26a5f13b | bellard | if (code_gen_buffer == MAP_FAILED) {
|
502 | 26a5f13b | bellard | fprintf(stderr, "Could not allocate dynamic translator buffer\n");
|
503 | 26a5f13b | bellard | exit(1);
|
504 | 26a5f13b | bellard | } |
505 | 26a5f13b | bellard | } |
506 | a167ba50 | Aurelien Jarno | #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
|
507 | 06e67a82 | aliguori | { |
508 | 06e67a82 | aliguori | int flags;
|
509 | 06e67a82 | aliguori | void *addr = NULL; |
510 | 06e67a82 | aliguori | flags = MAP_PRIVATE | MAP_ANONYMOUS; |
511 | 06e67a82 | aliguori | #if defined(__x86_64__)
|
512 | 06e67a82 | aliguori | /* FreeBSD doesn't have MAP_32BIT, use MAP_FIXED and assume
|
513 | 06e67a82 | aliguori | * 0x40000000 is free */
|
514 | 06e67a82 | aliguori | flags |= MAP_FIXED; |
515 | 06e67a82 | aliguori | addr = (void *)0x40000000; |
516 | 06e67a82 | aliguori | /* Cannot map more than that */
|
517 | 06e67a82 | aliguori | if (code_gen_buffer_size > (800 * 1024 * 1024)) |
518 | 06e67a82 | aliguori | code_gen_buffer_size = (800 * 1024 * 1024); |
519 | 06e67a82 | aliguori | #endif
|
520 | 06e67a82 | aliguori | code_gen_buffer = mmap(addr, code_gen_buffer_size, |
521 | 06e67a82 | aliguori | PROT_WRITE | PROT_READ | PROT_EXEC, |
522 | 06e67a82 | aliguori | flags, -1, 0); |
523 | 06e67a82 | aliguori | if (code_gen_buffer == MAP_FAILED) {
|
524 | 06e67a82 | aliguori | fprintf(stderr, "Could not allocate dynamic translator buffer\n");
|
525 | 06e67a82 | aliguori | exit(1);
|
526 | 06e67a82 | aliguori | } |
527 | 06e67a82 | aliguori | } |
528 | 26a5f13b | bellard | #else
|
529 | 26a5f13b | bellard | code_gen_buffer = qemu_malloc(code_gen_buffer_size); |
530 | 26a5f13b | bellard | map_exec(code_gen_buffer, code_gen_buffer_size); |
531 | 26a5f13b | bellard | #endif
|
532 | 4369415f | bellard | #endif /* !USE_STATIC_CODE_GEN_BUFFER */ |
533 | 26a5f13b | bellard | map_exec(code_gen_prologue, sizeof(code_gen_prologue));
|
534 | 26a5f13b | bellard | code_gen_buffer_max_size = code_gen_buffer_size - |
535 | 26a5f13b | bellard | code_gen_max_block_size(); |
536 | 26a5f13b | bellard | code_gen_max_blocks = code_gen_buffer_size / CODE_GEN_AVG_BLOCK_SIZE; |
537 | 26a5f13b | bellard | tbs = qemu_malloc(code_gen_max_blocks * sizeof(TranslationBlock));
|
538 | 26a5f13b | bellard | } |
539 | 26a5f13b | bellard | |
540 | 26a5f13b | bellard | /* Must be called before using the QEMU cpus. 'tb_size' is the size
|
541 | 26a5f13b | bellard | (in bytes) allocated to the translation buffer. Zero means default
|
542 | 26a5f13b | bellard | size. */
|
543 | 26a5f13b | bellard | void cpu_exec_init_all(unsigned long tb_size) |
544 | 26a5f13b | bellard | { |
545 | 26a5f13b | bellard | cpu_gen_init(); |
546 | 26a5f13b | bellard | code_gen_alloc(tb_size); |
547 | 26a5f13b | bellard | code_gen_ptr = code_gen_buffer; |
548 | 4369415f | bellard | page_init(); |
549 | e2eef170 | pbrook | #if !defined(CONFIG_USER_ONLY)
|
550 | 26a5f13b | bellard | io_mem_init(); |
551 | e2eef170 | pbrook | #endif
|
552 | 26a5f13b | bellard | } |
553 | 26a5f13b | bellard | |
554 | 9656f324 | pbrook | #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
|
555 | 9656f324 | pbrook | |
556 | e59fb374 | Juan Quintela | static int cpu_common_post_load(void *opaque, int version_id) |
557 | e7f4eff7 | Juan Quintela | { |
558 | e7f4eff7 | Juan Quintela | CPUState *env = opaque; |
559 | 9656f324 | pbrook | |
560 | 3098dba0 | aurel32 | /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
|
561 | 3098dba0 | aurel32 | version_id is increased. */
|
562 | 3098dba0 | aurel32 | env->interrupt_request &= ~0x01;
|
563 | 9656f324 | pbrook | tlb_flush(env, 1);
|
564 | 9656f324 | pbrook | |
565 | 9656f324 | pbrook | return 0; |
566 | 9656f324 | pbrook | } |
567 | e7f4eff7 | Juan Quintela | |
568 | e7f4eff7 | Juan Quintela | static const VMStateDescription vmstate_cpu_common = { |
569 | e7f4eff7 | Juan Quintela | .name = "cpu_common",
|
570 | e7f4eff7 | Juan Quintela | .version_id = 1,
|
571 | e7f4eff7 | Juan Quintela | .minimum_version_id = 1,
|
572 | e7f4eff7 | Juan Quintela | .minimum_version_id_old = 1,
|
573 | e7f4eff7 | Juan Quintela | .post_load = cpu_common_post_load, |
574 | e7f4eff7 | Juan Quintela | .fields = (VMStateField []) { |
575 | e7f4eff7 | Juan Quintela | VMSTATE_UINT32(halted, CPUState), |
576 | e7f4eff7 | Juan Quintela | VMSTATE_UINT32(interrupt_request, CPUState), |
577 | e7f4eff7 | Juan Quintela | VMSTATE_END_OF_LIST() |
578 | e7f4eff7 | Juan Quintela | } |
579 | e7f4eff7 | Juan Quintela | }; |
580 | 9656f324 | pbrook | #endif
|
581 | 9656f324 | pbrook | |
582 | 950f1472 | Glauber Costa | CPUState *qemu_get_cpu(int cpu)
|
583 | 950f1472 | Glauber Costa | { |
584 | 950f1472 | Glauber Costa | CPUState *env = first_cpu; |
585 | 950f1472 | Glauber Costa | |
586 | 950f1472 | Glauber Costa | while (env) {
|
587 | 950f1472 | Glauber Costa | if (env->cpu_index == cpu)
|
588 | 950f1472 | Glauber Costa | break;
|
589 | 950f1472 | Glauber Costa | env = env->next_cpu; |
590 | 950f1472 | Glauber Costa | } |
591 | 950f1472 | Glauber Costa | |
592 | 950f1472 | Glauber Costa | return env;
|
593 | 950f1472 | Glauber Costa | } |
594 | 950f1472 | Glauber Costa | |
595 | 6a00d601 | bellard | void cpu_exec_init(CPUState *env)
|
596 | fd6ce8f6 | bellard | { |
597 | 6a00d601 | bellard | CPUState **penv; |
598 | 6a00d601 | bellard | int cpu_index;
|
599 | 6a00d601 | bellard | |
600 | c2764719 | pbrook | #if defined(CONFIG_USER_ONLY)
|
601 | c2764719 | pbrook | cpu_list_lock(); |
602 | c2764719 | pbrook | #endif
|
603 | 6a00d601 | bellard | env->next_cpu = NULL;
|
604 | 6a00d601 | bellard | penv = &first_cpu; |
605 | 6a00d601 | bellard | cpu_index = 0;
|
606 | 6a00d601 | bellard | while (*penv != NULL) { |
607 | 1e9fa730 | Nathan Froyd | penv = &(*penv)->next_cpu; |
608 | 6a00d601 | bellard | cpu_index++; |
609 | 6a00d601 | bellard | } |
610 | 6a00d601 | bellard | env->cpu_index = cpu_index; |
611 | 268a362c | aliguori | env->numa_node = 0;
|
612 | 72cf2d4f | Blue Swirl | QTAILQ_INIT(&env->breakpoints); |
613 | 72cf2d4f | Blue Swirl | QTAILQ_INIT(&env->watchpoints); |
614 | 6a00d601 | bellard | *penv = env; |
615 | c2764719 | pbrook | #if defined(CONFIG_USER_ONLY)
|
616 | c2764719 | pbrook | cpu_list_unlock(); |
617 | c2764719 | pbrook | #endif
|
618 | b3c7724c | pbrook | #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
|
619 | e7f4eff7 | Juan Quintela | vmstate_register(cpu_index, &vmstate_cpu_common, env); |
620 | b3c7724c | pbrook | register_savevm("cpu", cpu_index, CPU_SAVE_VERSION,
|
621 | b3c7724c | pbrook | cpu_save, cpu_load, env); |
622 | b3c7724c | pbrook | #endif
|
623 | fd6ce8f6 | bellard | } |
624 | fd6ce8f6 | bellard | |
625 | 9fa3e853 | bellard | static inline void invalidate_page_bitmap(PageDesc *p) |
626 | 9fa3e853 | bellard | { |
627 | 9fa3e853 | bellard | if (p->code_bitmap) {
|
628 | 59817ccb | bellard | qemu_free(p->code_bitmap); |
629 | 9fa3e853 | bellard | p->code_bitmap = NULL;
|
630 | 9fa3e853 | bellard | } |
631 | 9fa3e853 | bellard | p->code_write_count = 0;
|
632 | 9fa3e853 | bellard | } |
633 | 9fa3e853 | bellard | |
634 | 5cd2c5b6 | Richard Henderson | /* Set to NULL all the 'first_tb' fields in all PageDescs. */
|
635 | 5cd2c5b6 | Richard Henderson | |
636 | 5cd2c5b6 | Richard Henderson | static void page_flush_tb_1 (int level, void **lp) |
637 | fd6ce8f6 | bellard | { |
638 | 5cd2c5b6 | Richard Henderson | int i;
|
639 | fd6ce8f6 | bellard | |
640 | 5cd2c5b6 | Richard Henderson | if (*lp == NULL) { |
641 | 5cd2c5b6 | Richard Henderson | return;
|
642 | 5cd2c5b6 | Richard Henderson | } |
643 | 5cd2c5b6 | Richard Henderson | if (level == 0) { |
644 | 5cd2c5b6 | Richard Henderson | PageDesc *pd = *lp; |
645 | 7296abac | Paul Brook | for (i = 0; i < L2_SIZE; ++i) { |
646 | 5cd2c5b6 | Richard Henderson | pd[i].first_tb = NULL;
|
647 | 5cd2c5b6 | Richard Henderson | invalidate_page_bitmap(pd + i); |
648 | fd6ce8f6 | bellard | } |
649 | 5cd2c5b6 | Richard Henderson | } else {
|
650 | 5cd2c5b6 | Richard Henderson | void **pp = *lp;
|
651 | 7296abac | Paul Brook | for (i = 0; i < L2_SIZE; ++i) { |
652 | 5cd2c5b6 | Richard Henderson | page_flush_tb_1 (level - 1, pp + i);
|
653 | 5cd2c5b6 | Richard Henderson | } |
654 | 5cd2c5b6 | Richard Henderson | } |
655 | 5cd2c5b6 | Richard Henderson | } |
656 | 5cd2c5b6 | Richard Henderson | |
657 | 5cd2c5b6 | Richard Henderson | static void page_flush_tb(void) |
658 | 5cd2c5b6 | Richard Henderson | { |
659 | 5cd2c5b6 | Richard Henderson | int i;
|
660 | 5cd2c5b6 | Richard Henderson | for (i = 0; i < V_L1_SIZE; i++) { |
661 | 5cd2c5b6 | Richard Henderson | page_flush_tb_1(V_L1_SHIFT / L2_BITS - 1, l1_map + i);
|
662 | fd6ce8f6 | bellard | } |
663 | fd6ce8f6 | bellard | } |
664 | fd6ce8f6 | bellard | |
665 | fd6ce8f6 | bellard | /* flush all the translation blocks */
|
666 | d4e8164f | bellard | /* XXX: tb_flush is currently not thread safe */
|
667 | 6a00d601 | bellard | void tb_flush(CPUState *env1)
|
668 | fd6ce8f6 | bellard | { |
669 | 6a00d601 | bellard | CPUState *env; |
670 | 0124311e | bellard | #if defined(DEBUG_FLUSH)
|
671 | ab3d1727 | blueswir1 | printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
|
672 | ab3d1727 | blueswir1 | (unsigned long)(code_gen_ptr - code_gen_buffer), |
673 | ab3d1727 | blueswir1 | nb_tbs, nb_tbs > 0 ?
|
674 | ab3d1727 | blueswir1 | ((unsigned long)(code_gen_ptr - code_gen_buffer)) / nb_tbs : 0); |
675 | fd6ce8f6 | bellard | #endif
|
676 | 26a5f13b | bellard | if ((unsigned long)(code_gen_ptr - code_gen_buffer) > code_gen_buffer_size) |
677 | a208e54a | pbrook | cpu_abort(env1, "Internal error: code buffer overflow\n");
|
678 | a208e54a | pbrook | |
679 | fd6ce8f6 | bellard | nb_tbs = 0;
|
680 | 3b46e624 | ths | |
681 | 6a00d601 | bellard | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
682 | 6a00d601 | bellard | memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *)); |
683 | 6a00d601 | bellard | } |
684 | 9fa3e853 | bellard | |
685 | 8a8a608f | bellard | memset (tb_phys_hash, 0, CODE_GEN_PHYS_HASH_SIZE * sizeof (void *)); |
686 | fd6ce8f6 | bellard | page_flush_tb(); |
687 | 9fa3e853 | bellard | |
688 | fd6ce8f6 | bellard | code_gen_ptr = code_gen_buffer; |
689 | d4e8164f | bellard | /* XXX: flush processor icache at this point if cache flush is
|
690 | d4e8164f | bellard | expensive */
|
691 | e3db7226 | bellard | tb_flush_count++; |
692 | fd6ce8f6 | bellard | } |
693 | fd6ce8f6 | bellard | |
694 | fd6ce8f6 | bellard | #ifdef DEBUG_TB_CHECK
|
695 | fd6ce8f6 | bellard | |
696 | bc98a7ef | j_mayer | static void tb_invalidate_check(target_ulong address) |
697 | fd6ce8f6 | bellard | { |
698 | fd6ce8f6 | bellard | TranslationBlock *tb; |
699 | fd6ce8f6 | bellard | int i;
|
700 | fd6ce8f6 | bellard | address &= TARGET_PAGE_MASK; |
701 | 99773bd4 | pbrook | for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) { |
702 | 99773bd4 | pbrook | for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) { |
703 | fd6ce8f6 | bellard | if (!(address + TARGET_PAGE_SIZE <= tb->pc ||
|
704 | fd6ce8f6 | bellard | address >= tb->pc + tb->size)) { |
705 | 0bf9e31a | Blue Swirl | printf("ERROR invalidate: address=" TARGET_FMT_lx
|
706 | 0bf9e31a | Blue Swirl | " PC=%08lx size=%04x\n",
|
707 | 99773bd4 | pbrook | address, (long)tb->pc, tb->size);
|
708 | fd6ce8f6 | bellard | } |
709 | fd6ce8f6 | bellard | } |
710 | fd6ce8f6 | bellard | } |
711 | fd6ce8f6 | bellard | } |
712 | fd6ce8f6 | bellard | |
713 | fd6ce8f6 | bellard | /* verify that all the pages have correct rights for code */
|
714 | fd6ce8f6 | bellard | static void tb_page_check(void) |
715 | fd6ce8f6 | bellard | { |
716 | fd6ce8f6 | bellard | TranslationBlock *tb; |
717 | fd6ce8f6 | bellard | int i, flags1, flags2;
|
718 | 3b46e624 | ths | |
719 | 99773bd4 | pbrook | for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) { |
720 | 99773bd4 | pbrook | for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) { |
721 | fd6ce8f6 | bellard | flags1 = page_get_flags(tb->pc); |
722 | fd6ce8f6 | bellard | flags2 = page_get_flags(tb->pc + tb->size - 1);
|
723 | fd6ce8f6 | bellard | if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) {
|
724 | fd6ce8f6 | bellard | printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
|
725 | 99773bd4 | pbrook | (long)tb->pc, tb->size, flags1, flags2);
|
726 | fd6ce8f6 | bellard | } |
727 | fd6ce8f6 | bellard | } |
728 | fd6ce8f6 | bellard | } |
729 | fd6ce8f6 | bellard | } |
730 | fd6ce8f6 | bellard | |
731 | fd6ce8f6 | bellard | #endif
|
732 | fd6ce8f6 | bellard | |
733 | fd6ce8f6 | bellard | /* invalidate one TB */
|
734 | fd6ce8f6 | bellard | static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb, |
735 | fd6ce8f6 | bellard | int next_offset)
|
736 | fd6ce8f6 | bellard | { |
737 | fd6ce8f6 | bellard | TranslationBlock *tb1; |
738 | fd6ce8f6 | bellard | for(;;) {
|
739 | fd6ce8f6 | bellard | tb1 = *ptb; |
740 | fd6ce8f6 | bellard | if (tb1 == tb) {
|
741 | fd6ce8f6 | bellard | *ptb = *(TranslationBlock **)((char *)tb1 + next_offset);
|
742 | fd6ce8f6 | bellard | break;
|
743 | fd6ce8f6 | bellard | } |
744 | fd6ce8f6 | bellard | ptb = (TranslationBlock **)((char *)tb1 + next_offset);
|
745 | fd6ce8f6 | bellard | } |
746 | fd6ce8f6 | bellard | } |
747 | fd6ce8f6 | bellard | |
748 | 9fa3e853 | bellard | static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb) |
749 | 9fa3e853 | bellard | { |
750 | 9fa3e853 | bellard | TranslationBlock *tb1; |
751 | 9fa3e853 | bellard | unsigned int n1; |
752 | 9fa3e853 | bellard | |
753 | 9fa3e853 | bellard | for(;;) {
|
754 | 9fa3e853 | bellard | tb1 = *ptb; |
755 | 9fa3e853 | bellard | n1 = (long)tb1 & 3; |
756 | 9fa3e853 | bellard | tb1 = (TranslationBlock *)((long)tb1 & ~3); |
757 | 9fa3e853 | bellard | if (tb1 == tb) {
|
758 | 9fa3e853 | bellard | *ptb = tb1->page_next[n1]; |
759 | 9fa3e853 | bellard | break;
|
760 | 9fa3e853 | bellard | } |
761 | 9fa3e853 | bellard | ptb = &tb1->page_next[n1]; |
762 | 9fa3e853 | bellard | } |
763 | 9fa3e853 | bellard | } |
764 | 9fa3e853 | bellard | |
765 | d4e8164f | bellard | static inline void tb_jmp_remove(TranslationBlock *tb, int n) |
766 | d4e8164f | bellard | { |
767 | d4e8164f | bellard | TranslationBlock *tb1, **ptb; |
768 | d4e8164f | bellard | unsigned int n1; |
769 | d4e8164f | bellard | |
770 | d4e8164f | bellard | ptb = &tb->jmp_next[n]; |
771 | d4e8164f | bellard | tb1 = *ptb; |
772 | d4e8164f | bellard | if (tb1) {
|
773 | d4e8164f | bellard | /* find tb(n) in circular list */
|
774 | d4e8164f | bellard | for(;;) {
|
775 | d4e8164f | bellard | tb1 = *ptb; |
776 | d4e8164f | bellard | n1 = (long)tb1 & 3; |
777 | d4e8164f | bellard | tb1 = (TranslationBlock *)((long)tb1 & ~3); |
778 | d4e8164f | bellard | if (n1 == n && tb1 == tb)
|
779 | d4e8164f | bellard | break;
|
780 | d4e8164f | bellard | if (n1 == 2) { |
781 | d4e8164f | bellard | ptb = &tb1->jmp_first; |
782 | d4e8164f | bellard | } else {
|
783 | d4e8164f | bellard | ptb = &tb1->jmp_next[n1]; |
784 | d4e8164f | bellard | } |
785 | d4e8164f | bellard | } |
786 | d4e8164f | bellard | /* now we can suppress tb(n) from the list */
|
787 | d4e8164f | bellard | *ptb = tb->jmp_next[n]; |
788 | d4e8164f | bellard | |
789 | d4e8164f | bellard | tb->jmp_next[n] = NULL;
|
790 | d4e8164f | bellard | } |
791 | d4e8164f | bellard | } |
792 | d4e8164f | bellard | |
793 | d4e8164f | bellard | /* reset the jump entry 'n' of a TB so that it is not chained to
|
794 | d4e8164f | bellard | another TB */
|
795 | d4e8164f | bellard | static inline void tb_reset_jump(TranslationBlock *tb, int n) |
796 | d4e8164f | bellard | { |
797 | d4e8164f | bellard | tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n])); |
798 | d4e8164f | bellard | } |
799 | d4e8164f | bellard | |
800 | 41c1b1c9 | Paul Brook | void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr)
|
801 | fd6ce8f6 | bellard | { |
802 | 6a00d601 | bellard | CPUState *env; |
803 | 8a40a180 | bellard | PageDesc *p; |
804 | d4e8164f | bellard | unsigned int h, n1; |
805 | 41c1b1c9 | Paul Brook | tb_page_addr_t phys_pc; |
806 | 8a40a180 | bellard | TranslationBlock *tb1, *tb2; |
807 | 3b46e624 | ths | |
808 | 8a40a180 | bellard | /* remove the TB from the hash list */
|
809 | 8a40a180 | bellard | phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
|
810 | 8a40a180 | bellard | h = tb_phys_hash_func(phys_pc); |
811 | 5fafdf24 | ths | tb_remove(&tb_phys_hash[h], tb, |
812 | 8a40a180 | bellard | offsetof(TranslationBlock, phys_hash_next)); |
813 | 8a40a180 | bellard | |
814 | 8a40a180 | bellard | /* remove the TB from the page list */
|
815 | 8a40a180 | bellard | if (tb->page_addr[0] != page_addr) { |
816 | 8a40a180 | bellard | p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
|
817 | 8a40a180 | bellard | tb_page_remove(&p->first_tb, tb); |
818 | 8a40a180 | bellard | invalidate_page_bitmap(p); |
819 | 8a40a180 | bellard | } |
820 | 8a40a180 | bellard | if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) { |
821 | 8a40a180 | bellard | p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
|
822 | 8a40a180 | bellard | tb_page_remove(&p->first_tb, tb); |
823 | 8a40a180 | bellard | invalidate_page_bitmap(p); |
824 | 8a40a180 | bellard | } |
825 | 8a40a180 | bellard | |
826 | 36bdbe54 | bellard | tb_invalidated_flag = 1;
|
827 | 59817ccb | bellard | |
828 | fd6ce8f6 | bellard | /* remove the TB from the hash list */
|
829 | 8a40a180 | bellard | h = tb_jmp_cache_hash_func(tb->pc); |
830 | 6a00d601 | bellard | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
831 | 6a00d601 | bellard | if (env->tb_jmp_cache[h] == tb)
|
832 | 6a00d601 | bellard | env->tb_jmp_cache[h] = NULL;
|
833 | 6a00d601 | bellard | } |
834 | d4e8164f | bellard | |
835 | d4e8164f | bellard | /* suppress this TB from the two jump lists */
|
836 | d4e8164f | bellard | tb_jmp_remove(tb, 0);
|
837 | d4e8164f | bellard | tb_jmp_remove(tb, 1);
|
838 | d4e8164f | bellard | |
839 | d4e8164f | bellard | /* suppress any remaining jumps to this TB */
|
840 | d4e8164f | bellard | tb1 = tb->jmp_first; |
841 | d4e8164f | bellard | for(;;) {
|
842 | d4e8164f | bellard | n1 = (long)tb1 & 3; |
843 | d4e8164f | bellard | if (n1 == 2) |
844 | d4e8164f | bellard | break;
|
845 | d4e8164f | bellard | tb1 = (TranslationBlock *)((long)tb1 & ~3); |
846 | d4e8164f | bellard | tb2 = tb1->jmp_next[n1]; |
847 | d4e8164f | bellard | tb_reset_jump(tb1, n1); |
848 | d4e8164f | bellard | tb1->jmp_next[n1] = NULL;
|
849 | d4e8164f | bellard | tb1 = tb2; |
850 | d4e8164f | bellard | } |
851 | d4e8164f | bellard | tb->jmp_first = (TranslationBlock *)((long)tb | 2); /* fail safe */ |
852 | 9fa3e853 | bellard | |
853 | e3db7226 | bellard | tb_phys_invalidate_count++; |
854 | 9fa3e853 | bellard | } |
855 | 9fa3e853 | bellard | |
856 | 9fa3e853 | bellard | static inline void set_bits(uint8_t *tab, int start, int len) |
857 | 9fa3e853 | bellard | { |
858 | 9fa3e853 | bellard | int end, mask, end1;
|
859 | 9fa3e853 | bellard | |
860 | 9fa3e853 | bellard | end = start + len; |
861 | 9fa3e853 | bellard | tab += start >> 3;
|
862 | 9fa3e853 | bellard | mask = 0xff << (start & 7); |
863 | 9fa3e853 | bellard | if ((start & ~7) == (end & ~7)) { |
864 | 9fa3e853 | bellard | if (start < end) {
|
865 | 9fa3e853 | bellard | mask &= ~(0xff << (end & 7)); |
866 | 9fa3e853 | bellard | *tab |= mask; |
867 | 9fa3e853 | bellard | } |
868 | 9fa3e853 | bellard | } else {
|
869 | 9fa3e853 | bellard | *tab++ |= mask; |
870 | 9fa3e853 | bellard | start = (start + 8) & ~7; |
871 | 9fa3e853 | bellard | end1 = end & ~7;
|
872 | 9fa3e853 | bellard | while (start < end1) {
|
873 | 9fa3e853 | bellard | *tab++ = 0xff;
|
874 | 9fa3e853 | bellard | start += 8;
|
875 | 9fa3e853 | bellard | } |
876 | 9fa3e853 | bellard | if (start < end) {
|
877 | 9fa3e853 | bellard | mask = ~(0xff << (end & 7)); |
878 | 9fa3e853 | bellard | *tab |= mask; |
879 | 9fa3e853 | bellard | } |
880 | 9fa3e853 | bellard | } |
881 | 9fa3e853 | bellard | } |
882 | 9fa3e853 | bellard | |
883 | 9fa3e853 | bellard | static void build_page_bitmap(PageDesc *p) |
884 | 9fa3e853 | bellard | { |
885 | 9fa3e853 | bellard | int n, tb_start, tb_end;
|
886 | 9fa3e853 | bellard | TranslationBlock *tb; |
887 | 3b46e624 | ths | |
888 | b2a7081a | pbrook | p->code_bitmap = qemu_mallocz(TARGET_PAGE_SIZE / 8);
|
889 | 9fa3e853 | bellard | |
890 | 9fa3e853 | bellard | tb = p->first_tb; |
891 | 9fa3e853 | bellard | while (tb != NULL) { |
892 | 9fa3e853 | bellard | n = (long)tb & 3; |
893 | 9fa3e853 | bellard | tb = (TranslationBlock *)((long)tb & ~3); |
894 | 9fa3e853 | bellard | /* NOTE: this is subtle as a TB may span two physical pages */
|
895 | 9fa3e853 | bellard | if (n == 0) { |
896 | 9fa3e853 | bellard | /* NOTE: tb_end may be after the end of the page, but
|
897 | 9fa3e853 | bellard | it is not a problem */
|
898 | 9fa3e853 | bellard | tb_start = tb->pc & ~TARGET_PAGE_MASK; |
899 | 9fa3e853 | bellard | tb_end = tb_start + tb->size; |
900 | 9fa3e853 | bellard | if (tb_end > TARGET_PAGE_SIZE)
|
901 | 9fa3e853 | bellard | tb_end = TARGET_PAGE_SIZE; |
902 | 9fa3e853 | bellard | } else {
|
903 | 9fa3e853 | bellard | tb_start = 0;
|
904 | 9fa3e853 | bellard | tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK); |
905 | 9fa3e853 | bellard | } |
906 | 9fa3e853 | bellard | set_bits(p->code_bitmap, tb_start, tb_end - tb_start); |
907 | 9fa3e853 | bellard | tb = tb->page_next[n]; |
908 | 9fa3e853 | bellard | } |
909 | 9fa3e853 | bellard | } |
910 | 9fa3e853 | bellard | |
911 | 2e70f6ef | pbrook | TranslationBlock *tb_gen_code(CPUState *env, |
912 | 2e70f6ef | pbrook | target_ulong pc, target_ulong cs_base, |
913 | 2e70f6ef | pbrook | int flags, int cflags) |
914 | d720b93d | bellard | { |
915 | d720b93d | bellard | TranslationBlock *tb; |
916 | d720b93d | bellard | uint8_t *tc_ptr; |
917 | 41c1b1c9 | Paul Brook | tb_page_addr_t phys_pc, phys_page2; |
918 | 41c1b1c9 | Paul Brook | target_ulong virt_page2; |
919 | d720b93d | bellard | int code_gen_size;
|
920 | d720b93d | bellard | |
921 | 41c1b1c9 | Paul Brook | phys_pc = get_page_addr_code(env, pc); |
922 | c27004ec | bellard | tb = tb_alloc(pc); |
923 | d720b93d | bellard | if (!tb) {
|
924 | d720b93d | bellard | /* flush must be done */
|
925 | d720b93d | bellard | tb_flush(env); |
926 | d720b93d | bellard | /* cannot fail at this point */
|
927 | c27004ec | bellard | tb = tb_alloc(pc); |
928 | 2e70f6ef | pbrook | /* Don't forget to invalidate previous TB info. */
|
929 | 2e70f6ef | pbrook | tb_invalidated_flag = 1;
|
930 | d720b93d | bellard | } |
931 | d720b93d | bellard | tc_ptr = code_gen_ptr; |
932 | d720b93d | bellard | tb->tc_ptr = tc_ptr; |
933 | d720b93d | bellard | tb->cs_base = cs_base; |
934 | d720b93d | bellard | tb->flags = flags; |
935 | d720b93d | bellard | tb->cflags = cflags; |
936 | d07bde88 | blueswir1 | cpu_gen_code(env, tb, &code_gen_size); |
937 | d720b93d | bellard | code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1)); |
938 | 3b46e624 | ths | |
939 | d720b93d | bellard | /* check next page if needed */
|
940 | c27004ec | bellard | virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
|
941 | d720b93d | bellard | phys_page2 = -1;
|
942 | c27004ec | bellard | if ((pc & TARGET_PAGE_MASK) != virt_page2) {
|
943 | 41c1b1c9 | Paul Brook | phys_page2 = get_page_addr_code(env, virt_page2); |
944 | d720b93d | bellard | } |
945 | 41c1b1c9 | Paul Brook | tb_link_page(tb, phys_pc, phys_page2); |
946 | 2e70f6ef | pbrook | return tb;
|
947 | d720b93d | bellard | } |
948 | 3b46e624 | ths | |
949 | 9fa3e853 | bellard | /* invalidate all TBs which intersect with the target physical page
|
950 | 9fa3e853 | bellard | starting in range [start;end[. NOTE: start and end must refer to
|
951 | d720b93d | bellard | the same physical page. 'is_cpu_write_access' should be true if called
|
952 | d720b93d | bellard | from a real cpu write access: the virtual CPU will exit the current
|
953 | d720b93d | bellard | TB if code is modified inside this TB. */
|
954 | 41c1b1c9 | Paul Brook | void tb_invalidate_phys_page_range(tb_page_addr_t start, tb_page_addr_t end,
|
955 | d720b93d | bellard | int is_cpu_write_access)
|
956 | d720b93d | bellard | { |
957 | 6b917547 | aliguori | TranslationBlock *tb, *tb_next, *saved_tb; |
958 | d720b93d | bellard | CPUState *env = cpu_single_env; |
959 | 41c1b1c9 | Paul Brook | tb_page_addr_t tb_start, tb_end; |
960 | 6b917547 | aliguori | PageDesc *p; |
961 | 6b917547 | aliguori | int n;
|
962 | 6b917547 | aliguori | #ifdef TARGET_HAS_PRECISE_SMC
|
963 | 6b917547 | aliguori | int current_tb_not_found = is_cpu_write_access;
|
964 | 6b917547 | aliguori | TranslationBlock *current_tb = NULL;
|
965 | 6b917547 | aliguori | int current_tb_modified = 0; |
966 | 6b917547 | aliguori | target_ulong current_pc = 0;
|
967 | 6b917547 | aliguori | target_ulong current_cs_base = 0;
|
968 | 6b917547 | aliguori | int current_flags = 0; |
969 | 6b917547 | aliguori | #endif /* TARGET_HAS_PRECISE_SMC */ |
970 | 9fa3e853 | bellard | |
971 | 9fa3e853 | bellard | p = page_find(start >> TARGET_PAGE_BITS); |
972 | 5fafdf24 | ths | if (!p)
|
973 | 9fa3e853 | bellard | return;
|
974 | 5fafdf24 | ths | if (!p->code_bitmap &&
|
975 | d720b93d | bellard | ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD && |
976 | d720b93d | bellard | is_cpu_write_access) { |
977 | 9fa3e853 | bellard | /* build code bitmap */
|
978 | 9fa3e853 | bellard | build_page_bitmap(p); |
979 | 9fa3e853 | bellard | } |
980 | 9fa3e853 | bellard | |
981 | 9fa3e853 | bellard | /* we remove all the TBs in the range [start, end[ */
|
982 | 9fa3e853 | bellard | /* XXX: see if in some cases it could be faster to invalidate all the code */
|
983 | 9fa3e853 | bellard | tb = p->first_tb; |
984 | 9fa3e853 | bellard | while (tb != NULL) { |
985 | 9fa3e853 | bellard | n = (long)tb & 3; |
986 | 9fa3e853 | bellard | tb = (TranslationBlock *)((long)tb & ~3); |
987 | 9fa3e853 | bellard | tb_next = tb->page_next[n]; |
988 | 9fa3e853 | bellard | /* NOTE: this is subtle as a TB may span two physical pages */
|
989 | 9fa3e853 | bellard | if (n == 0) { |
990 | 9fa3e853 | bellard | /* NOTE: tb_end may be after the end of the page, but
|
991 | 9fa3e853 | bellard | it is not a problem */
|
992 | 9fa3e853 | bellard | tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
|
993 | 9fa3e853 | bellard | tb_end = tb_start + tb->size; |
994 | 9fa3e853 | bellard | } else {
|
995 | 9fa3e853 | bellard | tb_start = tb->page_addr[1];
|
996 | 9fa3e853 | bellard | tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK); |
997 | 9fa3e853 | bellard | } |
998 | 9fa3e853 | bellard | if (!(tb_end <= start || tb_start >= end)) {
|
999 | d720b93d | bellard | #ifdef TARGET_HAS_PRECISE_SMC
|
1000 | d720b93d | bellard | if (current_tb_not_found) {
|
1001 | d720b93d | bellard | current_tb_not_found = 0;
|
1002 | d720b93d | bellard | current_tb = NULL;
|
1003 | 2e70f6ef | pbrook | if (env->mem_io_pc) {
|
1004 | d720b93d | bellard | /* now we have a real cpu fault */
|
1005 | 2e70f6ef | pbrook | current_tb = tb_find_pc(env->mem_io_pc); |
1006 | d720b93d | bellard | } |
1007 | d720b93d | bellard | } |
1008 | d720b93d | bellard | if (current_tb == tb &&
|
1009 | 2e70f6ef | pbrook | (current_tb->cflags & CF_COUNT_MASK) != 1) {
|
1010 | d720b93d | bellard | /* If we are modifying the current TB, we must stop
|
1011 | d720b93d | bellard | its execution. We could be more precise by checking
|
1012 | d720b93d | bellard | that the modification is after the current PC, but it
|
1013 | d720b93d | bellard | would require a specialized function to partially
|
1014 | d720b93d | bellard | restore the CPU state */
|
1015 | 3b46e624 | ths | |
1016 | d720b93d | bellard | current_tb_modified = 1;
|
1017 | 5fafdf24 | ths | cpu_restore_state(current_tb, env, |
1018 | 2e70f6ef | pbrook | env->mem_io_pc, NULL);
|
1019 | 6b917547 | aliguori | cpu_get_tb_cpu_state(env, ¤t_pc, ¤t_cs_base, |
1020 | 6b917547 | aliguori | ¤t_flags); |
1021 | d720b93d | bellard | } |
1022 | d720b93d | bellard | #endif /* TARGET_HAS_PRECISE_SMC */ |
1023 | 6f5a9f7e | bellard | /* we need to do that to handle the case where a signal
|
1024 | 6f5a9f7e | bellard | occurs while doing tb_phys_invalidate() */
|
1025 | 6f5a9f7e | bellard | saved_tb = NULL;
|
1026 | 6f5a9f7e | bellard | if (env) {
|
1027 | 6f5a9f7e | bellard | saved_tb = env->current_tb; |
1028 | 6f5a9f7e | bellard | env->current_tb = NULL;
|
1029 | 6f5a9f7e | bellard | } |
1030 | 9fa3e853 | bellard | tb_phys_invalidate(tb, -1);
|
1031 | 6f5a9f7e | bellard | if (env) {
|
1032 | 6f5a9f7e | bellard | env->current_tb = saved_tb; |
1033 | 6f5a9f7e | bellard | if (env->interrupt_request && env->current_tb)
|
1034 | 6f5a9f7e | bellard | cpu_interrupt(env, env->interrupt_request); |
1035 | 6f5a9f7e | bellard | } |
1036 | 9fa3e853 | bellard | } |
1037 | 9fa3e853 | bellard | tb = tb_next; |
1038 | 9fa3e853 | bellard | } |
1039 | 9fa3e853 | bellard | #if !defined(CONFIG_USER_ONLY)
|
1040 | 9fa3e853 | bellard | /* if no code remaining, no need to continue to use slow writes */
|
1041 | 9fa3e853 | bellard | if (!p->first_tb) {
|
1042 | 9fa3e853 | bellard | invalidate_page_bitmap(p); |
1043 | d720b93d | bellard | if (is_cpu_write_access) {
|
1044 | 2e70f6ef | pbrook | tlb_unprotect_code_phys(env, start, env->mem_io_vaddr); |
1045 | d720b93d | bellard | } |
1046 | d720b93d | bellard | } |
1047 | d720b93d | bellard | #endif
|
1048 | d720b93d | bellard | #ifdef TARGET_HAS_PRECISE_SMC
|
1049 | d720b93d | bellard | if (current_tb_modified) {
|
1050 | d720b93d | bellard | /* we generate a block containing just the instruction
|
1051 | d720b93d | bellard | modifying the memory. It will ensure that it cannot modify
|
1052 | d720b93d | bellard | itself */
|
1053 | ea1c1802 | bellard | env->current_tb = NULL;
|
1054 | 2e70f6ef | pbrook | tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
|
1055 | d720b93d | bellard | cpu_resume_from_signal(env, NULL);
|
1056 | 9fa3e853 | bellard | } |
1057 | fd6ce8f6 | bellard | #endif
|
1058 | 9fa3e853 | bellard | } |
1059 | fd6ce8f6 | bellard | |
1060 | 9fa3e853 | bellard | /* len must be <= 8 and start must be a multiple of len */
|
1061 | 41c1b1c9 | Paul Brook | static inline void tb_invalidate_phys_page_fast(tb_page_addr_t start, int len) |
1062 | 9fa3e853 | bellard | { |
1063 | 9fa3e853 | bellard | PageDesc *p; |
1064 | 9fa3e853 | bellard | int offset, b;
|
1065 | 59817ccb | bellard | #if 0
|
1066 | a4193c8a | bellard | if (1) {
|
1067 | 93fcfe39 | aliguori | qemu_log("modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
|
1068 | 93fcfe39 | aliguori | cpu_single_env->mem_io_vaddr, len,
|
1069 | 93fcfe39 | aliguori | cpu_single_env->eip,
|
1070 | 93fcfe39 | aliguori | cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base);
|
1071 | 59817ccb | bellard | }
|
1072 | 59817ccb | bellard | #endif
|
1073 | 9fa3e853 | bellard | p = page_find(start >> TARGET_PAGE_BITS); |
1074 | 5fafdf24 | ths | if (!p)
|
1075 | 9fa3e853 | bellard | return;
|
1076 | 9fa3e853 | bellard | if (p->code_bitmap) {
|
1077 | 9fa3e853 | bellard | offset = start & ~TARGET_PAGE_MASK; |
1078 | 9fa3e853 | bellard | b = p->code_bitmap[offset >> 3] >> (offset & 7); |
1079 | 9fa3e853 | bellard | if (b & ((1 << len) - 1)) |
1080 | 9fa3e853 | bellard | goto do_invalidate;
|
1081 | 9fa3e853 | bellard | } else {
|
1082 | 9fa3e853 | bellard | do_invalidate:
|
1083 | d720b93d | bellard | tb_invalidate_phys_page_range(start, start + len, 1);
|
1084 | 9fa3e853 | bellard | } |
1085 | 9fa3e853 | bellard | } |
1086 | 9fa3e853 | bellard | |
1087 | 9fa3e853 | bellard | #if !defined(CONFIG_SOFTMMU)
|
1088 | 41c1b1c9 | Paul Brook | static void tb_invalidate_phys_page(tb_page_addr_t addr, |
1089 | d720b93d | bellard | unsigned long pc, void *puc) |
1090 | 9fa3e853 | bellard | { |
1091 | 6b917547 | aliguori | TranslationBlock *tb; |
1092 | 9fa3e853 | bellard | PageDesc *p; |
1093 | 6b917547 | aliguori | int n;
|
1094 | d720b93d | bellard | #ifdef TARGET_HAS_PRECISE_SMC
|
1095 | 6b917547 | aliguori | TranslationBlock *current_tb = NULL;
|
1096 | d720b93d | bellard | CPUState *env = cpu_single_env; |
1097 | 6b917547 | aliguori | int current_tb_modified = 0; |
1098 | 6b917547 | aliguori | target_ulong current_pc = 0;
|
1099 | 6b917547 | aliguori | target_ulong current_cs_base = 0;
|
1100 | 6b917547 | aliguori | int current_flags = 0; |
1101 | d720b93d | bellard | #endif
|
1102 | 9fa3e853 | bellard | |
1103 | 9fa3e853 | bellard | addr &= TARGET_PAGE_MASK; |
1104 | 9fa3e853 | bellard | p = page_find(addr >> TARGET_PAGE_BITS); |
1105 | 5fafdf24 | ths | if (!p)
|
1106 | 9fa3e853 | bellard | return;
|
1107 | 9fa3e853 | bellard | tb = p->first_tb; |
1108 | d720b93d | bellard | #ifdef TARGET_HAS_PRECISE_SMC
|
1109 | d720b93d | bellard | if (tb && pc != 0) { |
1110 | d720b93d | bellard | current_tb = tb_find_pc(pc); |
1111 | d720b93d | bellard | } |
1112 | d720b93d | bellard | #endif
|
1113 | 9fa3e853 | bellard | while (tb != NULL) { |
1114 | 9fa3e853 | bellard | n = (long)tb & 3; |
1115 | 9fa3e853 | bellard | tb = (TranslationBlock *)((long)tb & ~3); |
1116 | d720b93d | bellard | #ifdef TARGET_HAS_PRECISE_SMC
|
1117 | d720b93d | bellard | if (current_tb == tb &&
|
1118 | 2e70f6ef | pbrook | (current_tb->cflags & CF_COUNT_MASK) != 1) {
|
1119 | d720b93d | bellard | /* If we are modifying the current TB, we must stop
|
1120 | d720b93d | bellard | its execution. We could be more precise by checking
|
1121 | d720b93d | bellard | that the modification is after the current PC, but it
|
1122 | d720b93d | bellard | would require a specialized function to partially
|
1123 | d720b93d | bellard | restore the CPU state */
|
1124 | 3b46e624 | ths | |
1125 | d720b93d | bellard | current_tb_modified = 1;
|
1126 | d720b93d | bellard | cpu_restore_state(current_tb, env, pc, puc); |
1127 | 6b917547 | aliguori | cpu_get_tb_cpu_state(env, ¤t_pc, ¤t_cs_base, |
1128 | 6b917547 | aliguori | ¤t_flags); |
1129 | d720b93d | bellard | } |
1130 | d720b93d | bellard | #endif /* TARGET_HAS_PRECISE_SMC */ |
1131 | 9fa3e853 | bellard | tb_phys_invalidate(tb, addr); |
1132 | 9fa3e853 | bellard | tb = tb->page_next[n]; |
1133 | 9fa3e853 | bellard | } |
1134 | fd6ce8f6 | bellard | p->first_tb = NULL;
|
1135 | d720b93d | bellard | #ifdef TARGET_HAS_PRECISE_SMC
|
1136 | d720b93d | bellard | if (current_tb_modified) {
|
1137 | d720b93d | bellard | /* we generate a block containing just the instruction
|
1138 | d720b93d | bellard | modifying the memory. It will ensure that it cannot modify
|
1139 | d720b93d | bellard | itself */
|
1140 | ea1c1802 | bellard | env->current_tb = NULL;
|
1141 | 2e70f6ef | pbrook | tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
|
1142 | d720b93d | bellard | cpu_resume_from_signal(env, puc); |
1143 | d720b93d | bellard | } |
1144 | d720b93d | bellard | #endif
|
1145 | fd6ce8f6 | bellard | } |
1146 | 9fa3e853 | bellard | #endif
|
1147 | fd6ce8f6 | bellard | |
1148 | fd6ce8f6 | bellard | /* add the tb in the target page and protect it if necessary */
|
1149 | 5fafdf24 | ths | static inline void tb_alloc_page(TranslationBlock *tb, |
1150 | 41c1b1c9 | Paul Brook | unsigned int n, tb_page_addr_t page_addr) |
1151 | fd6ce8f6 | bellard | { |
1152 | fd6ce8f6 | bellard | PageDesc *p; |
1153 | 9fa3e853 | bellard | TranslationBlock *last_first_tb; |
1154 | 9fa3e853 | bellard | |
1155 | 9fa3e853 | bellard | tb->page_addr[n] = page_addr; |
1156 | 5cd2c5b6 | Richard Henderson | p = page_find_alloc(page_addr >> TARGET_PAGE_BITS, 1);
|
1157 | 9fa3e853 | bellard | tb->page_next[n] = p->first_tb; |
1158 | 9fa3e853 | bellard | last_first_tb = p->first_tb; |
1159 | 9fa3e853 | bellard | p->first_tb = (TranslationBlock *)((long)tb | n);
|
1160 | 9fa3e853 | bellard | invalidate_page_bitmap(p); |
1161 | fd6ce8f6 | bellard | |
1162 | 107db443 | bellard | #if defined(TARGET_HAS_SMC) || 1 |
1163 | d720b93d | bellard | |
1164 | 9fa3e853 | bellard | #if defined(CONFIG_USER_ONLY)
|
1165 | fd6ce8f6 | bellard | if (p->flags & PAGE_WRITE) {
|
1166 | 53a5960a | pbrook | target_ulong addr; |
1167 | 53a5960a | pbrook | PageDesc *p2; |
1168 | 9fa3e853 | bellard | int prot;
|
1169 | 9fa3e853 | bellard | |
1170 | fd6ce8f6 | bellard | /* force the host page as non writable (writes will have a
|
1171 | fd6ce8f6 | bellard | page fault + mprotect overhead) */
|
1172 | 53a5960a | pbrook | page_addr &= qemu_host_page_mask; |
1173 | fd6ce8f6 | bellard | prot = 0;
|
1174 | 53a5960a | pbrook | for(addr = page_addr; addr < page_addr + qemu_host_page_size;
|
1175 | 53a5960a | pbrook | addr += TARGET_PAGE_SIZE) { |
1176 | 53a5960a | pbrook | |
1177 | 53a5960a | pbrook | p2 = page_find (addr >> TARGET_PAGE_BITS); |
1178 | 53a5960a | pbrook | if (!p2)
|
1179 | 53a5960a | pbrook | continue;
|
1180 | 53a5960a | pbrook | prot |= p2->flags; |
1181 | 53a5960a | pbrook | p2->flags &= ~PAGE_WRITE; |
1182 | 53a5960a | pbrook | } |
1183 | 5fafdf24 | ths | mprotect(g2h(page_addr), qemu_host_page_size, |
1184 | fd6ce8f6 | bellard | (prot & PAGE_BITS) & ~PAGE_WRITE); |
1185 | fd6ce8f6 | bellard | #ifdef DEBUG_TB_INVALIDATE
|
1186 | ab3d1727 | blueswir1 | printf("protecting code page: 0x" TARGET_FMT_lx "\n", |
1187 | 53a5960a | pbrook | page_addr); |
1188 | fd6ce8f6 | bellard | #endif
|
1189 | fd6ce8f6 | bellard | } |
1190 | 9fa3e853 | bellard | #else
|
1191 | 9fa3e853 | bellard | /* if some code is already present, then the pages are already
|
1192 | 9fa3e853 | bellard | protected. So we handle the case where only the first TB is
|
1193 | 9fa3e853 | bellard | allocated in a physical page */
|
1194 | 9fa3e853 | bellard | if (!last_first_tb) {
|
1195 | 6a00d601 | bellard | tlb_protect_code(page_addr); |
1196 | 9fa3e853 | bellard | } |
1197 | 9fa3e853 | bellard | #endif
|
1198 | d720b93d | bellard | |
1199 | d720b93d | bellard | #endif /* TARGET_HAS_SMC */ |
1200 | fd6ce8f6 | bellard | } |
1201 | fd6ce8f6 | bellard | |
1202 | fd6ce8f6 | bellard | /* Allocate a new translation block. Flush the translation buffer if
|
1203 | fd6ce8f6 | bellard | too many translation blocks or too much generated code. */
|
1204 | c27004ec | bellard | TranslationBlock *tb_alloc(target_ulong pc) |
1205 | fd6ce8f6 | bellard | { |
1206 | fd6ce8f6 | bellard | TranslationBlock *tb; |
1207 | fd6ce8f6 | bellard | |
1208 | 26a5f13b | bellard | if (nb_tbs >= code_gen_max_blocks ||
|
1209 | 26a5f13b | bellard | (code_gen_ptr - code_gen_buffer) >= code_gen_buffer_max_size) |
1210 | d4e8164f | bellard | return NULL; |
1211 | fd6ce8f6 | bellard | tb = &tbs[nb_tbs++]; |
1212 | fd6ce8f6 | bellard | tb->pc = pc; |
1213 | b448f2f3 | bellard | tb->cflags = 0;
|
1214 | d4e8164f | bellard | return tb;
|
1215 | d4e8164f | bellard | } |
1216 | d4e8164f | bellard | |
1217 | 2e70f6ef | pbrook | void tb_free(TranslationBlock *tb)
|
1218 | 2e70f6ef | pbrook | { |
1219 | bf20dc07 | ths | /* In practice this is mostly used for single use temporary TB
|
1220 | 2e70f6ef | pbrook | Ignore the hard cases and just back up if this TB happens to
|
1221 | 2e70f6ef | pbrook | be the last one generated. */
|
1222 | 2e70f6ef | pbrook | if (nb_tbs > 0 && tb == &tbs[nb_tbs - 1]) { |
1223 | 2e70f6ef | pbrook | code_gen_ptr = tb->tc_ptr; |
1224 | 2e70f6ef | pbrook | nb_tbs--; |
1225 | 2e70f6ef | pbrook | } |
1226 | 2e70f6ef | pbrook | } |
1227 | 2e70f6ef | pbrook | |
1228 | 9fa3e853 | bellard | /* add a new TB and link it to the physical page tables. phys_page2 is
|
1229 | 9fa3e853 | bellard | (-1) to indicate that only one page contains the TB. */
|
1230 | 41c1b1c9 | Paul Brook | void tb_link_page(TranslationBlock *tb,
|
1231 | 41c1b1c9 | Paul Brook | tb_page_addr_t phys_pc, tb_page_addr_t phys_page2) |
1232 | d4e8164f | bellard | { |
1233 | 9fa3e853 | bellard | unsigned int h; |
1234 | 9fa3e853 | bellard | TranslationBlock **ptb; |
1235 | 9fa3e853 | bellard | |
1236 | c8a706fe | pbrook | /* Grab the mmap lock to stop another thread invalidating this TB
|
1237 | c8a706fe | pbrook | before we are done. */
|
1238 | c8a706fe | pbrook | mmap_lock(); |
1239 | 9fa3e853 | bellard | /* add in the physical hash table */
|
1240 | 9fa3e853 | bellard | h = tb_phys_hash_func(phys_pc); |
1241 | 9fa3e853 | bellard | ptb = &tb_phys_hash[h]; |
1242 | 9fa3e853 | bellard | tb->phys_hash_next = *ptb; |
1243 | 9fa3e853 | bellard | *ptb = tb; |
1244 | fd6ce8f6 | bellard | |
1245 | fd6ce8f6 | bellard | /* add in the page list */
|
1246 | 9fa3e853 | bellard | tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK);
|
1247 | 9fa3e853 | bellard | if (phys_page2 != -1) |
1248 | 9fa3e853 | bellard | tb_alloc_page(tb, 1, phys_page2);
|
1249 | 9fa3e853 | bellard | else
|
1250 | 9fa3e853 | bellard | tb->page_addr[1] = -1; |
1251 | 9fa3e853 | bellard | |
1252 | d4e8164f | bellard | tb->jmp_first = (TranslationBlock *)((long)tb | 2); |
1253 | d4e8164f | bellard | tb->jmp_next[0] = NULL; |
1254 | d4e8164f | bellard | tb->jmp_next[1] = NULL; |
1255 | d4e8164f | bellard | |
1256 | d4e8164f | bellard | /* init original jump addresses */
|
1257 | d4e8164f | bellard | if (tb->tb_next_offset[0] != 0xffff) |
1258 | d4e8164f | bellard | tb_reset_jump(tb, 0);
|
1259 | d4e8164f | bellard | if (tb->tb_next_offset[1] != 0xffff) |
1260 | d4e8164f | bellard | tb_reset_jump(tb, 1);
|
1261 | 8a40a180 | bellard | |
1262 | 8a40a180 | bellard | #ifdef DEBUG_TB_CHECK
|
1263 | 8a40a180 | bellard | tb_page_check(); |
1264 | 8a40a180 | bellard | #endif
|
1265 | c8a706fe | pbrook | mmap_unlock(); |
1266 | fd6ce8f6 | bellard | } |
1267 | fd6ce8f6 | bellard | |
1268 | 9fa3e853 | bellard | /* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
|
1269 | 9fa3e853 | bellard | tb[1].tc_ptr. Return NULL if not found */
|
1270 | 9fa3e853 | bellard | TranslationBlock *tb_find_pc(unsigned long tc_ptr) |
1271 | fd6ce8f6 | bellard | { |
1272 | 9fa3e853 | bellard | int m_min, m_max, m;
|
1273 | 9fa3e853 | bellard | unsigned long v; |
1274 | 9fa3e853 | bellard | TranslationBlock *tb; |
1275 | a513fe19 | bellard | |
1276 | a513fe19 | bellard | if (nb_tbs <= 0) |
1277 | a513fe19 | bellard | return NULL; |
1278 | a513fe19 | bellard | if (tc_ptr < (unsigned long)code_gen_buffer || |
1279 | a513fe19 | bellard | tc_ptr >= (unsigned long)code_gen_ptr) |
1280 | a513fe19 | bellard | return NULL; |
1281 | a513fe19 | bellard | /* binary search (cf Knuth) */
|
1282 | a513fe19 | bellard | m_min = 0;
|
1283 | a513fe19 | bellard | m_max = nb_tbs - 1;
|
1284 | a513fe19 | bellard | while (m_min <= m_max) {
|
1285 | a513fe19 | bellard | m = (m_min + m_max) >> 1;
|
1286 | a513fe19 | bellard | tb = &tbs[m]; |
1287 | a513fe19 | bellard | v = (unsigned long)tb->tc_ptr; |
1288 | a513fe19 | bellard | if (v == tc_ptr)
|
1289 | a513fe19 | bellard | return tb;
|
1290 | a513fe19 | bellard | else if (tc_ptr < v) { |
1291 | a513fe19 | bellard | m_max = m - 1;
|
1292 | a513fe19 | bellard | } else {
|
1293 | a513fe19 | bellard | m_min = m + 1;
|
1294 | a513fe19 | bellard | } |
1295 | 5fafdf24 | ths | } |
1296 | a513fe19 | bellard | return &tbs[m_max];
|
1297 | a513fe19 | bellard | } |
1298 | 7501267e | bellard | |
1299 | ea041c0e | bellard | static void tb_reset_jump_recursive(TranslationBlock *tb); |
1300 | ea041c0e | bellard | |
1301 | ea041c0e | bellard | static inline void tb_reset_jump_recursive2(TranslationBlock *tb, int n) |
1302 | ea041c0e | bellard | { |
1303 | ea041c0e | bellard | TranslationBlock *tb1, *tb_next, **ptb; |
1304 | ea041c0e | bellard | unsigned int n1; |
1305 | ea041c0e | bellard | |
1306 | ea041c0e | bellard | tb1 = tb->jmp_next[n]; |
1307 | ea041c0e | bellard | if (tb1 != NULL) { |
1308 | ea041c0e | bellard | /* find head of list */
|
1309 | ea041c0e | bellard | for(;;) {
|
1310 | ea041c0e | bellard | n1 = (long)tb1 & 3; |
1311 | ea041c0e | bellard | tb1 = (TranslationBlock *)((long)tb1 & ~3); |
1312 | ea041c0e | bellard | if (n1 == 2) |
1313 | ea041c0e | bellard | break;
|
1314 | ea041c0e | bellard | tb1 = tb1->jmp_next[n1]; |
1315 | ea041c0e | bellard | } |
1316 | ea041c0e | bellard | /* we are now sure now that tb jumps to tb1 */
|
1317 | ea041c0e | bellard | tb_next = tb1; |
1318 | ea041c0e | bellard | |
1319 | ea041c0e | bellard | /* remove tb from the jmp_first list */
|
1320 | ea041c0e | bellard | ptb = &tb_next->jmp_first; |
1321 | ea041c0e | bellard | for(;;) {
|
1322 | ea041c0e | bellard | tb1 = *ptb; |
1323 | ea041c0e | bellard | n1 = (long)tb1 & 3; |
1324 | ea041c0e | bellard | tb1 = (TranslationBlock *)((long)tb1 & ~3); |
1325 | ea041c0e | bellard | if (n1 == n && tb1 == tb)
|
1326 | ea041c0e | bellard | break;
|
1327 | ea041c0e | bellard | ptb = &tb1->jmp_next[n1]; |
1328 | ea041c0e | bellard | } |
1329 | ea041c0e | bellard | *ptb = tb->jmp_next[n]; |
1330 | ea041c0e | bellard | tb->jmp_next[n] = NULL;
|
1331 | 3b46e624 | ths | |
1332 | ea041c0e | bellard | /* suppress the jump to next tb in generated code */
|
1333 | ea041c0e | bellard | tb_reset_jump(tb, n); |
1334 | ea041c0e | bellard | |
1335 | 0124311e | bellard | /* suppress jumps in the tb on which we could have jumped */
|
1336 | ea041c0e | bellard | tb_reset_jump_recursive(tb_next); |
1337 | ea041c0e | bellard | } |
1338 | ea041c0e | bellard | } |
1339 | ea041c0e | bellard | |
1340 | ea041c0e | bellard | static void tb_reset_jump_recursive(TranslationBlock *tb) |
1341 | ea041c0e | bellard | { |
1342 | ea041c0e | bellard | tb_reset_jump_recursive2(tb, 0);
|
1343 | ea041c0e | bellard | tb_reset_jump_recursive2(tb, 1);
|
1344 | ea041c0e | bellard | } |
1345 | ea041c0e | bellard | |
1346 | 1fddef4b | bellard | #if defined(TARGET_HAS_ICE)
|
1347 | 94df27fd | Paul Brook | #if defined(CONFIG_USER_ONLY)
|
1348 | 94df27fd | Paul Brook | static void breakpoint_invalidate(CPUState *env, target_ulong pc) |
1349 | 94df27fd | Paul Brook | { |
1350 | 94df27fd | Paul Brook | tb_invalidate_phys_page_range(pc, pc + 1, 0); |
1351 | 94df27fd | Paul Brook | } |
1352 | 94df27fd | Paul Brook | #else
|
1353 | d720b93d | bellard | static void breakpoint_invalidate(CPUState *env, target_ulong pc) |
1354 | d720b93d | bellard | { |
1355 | c227f099 | Anthony Liguori | target_phys_addr_t addr; |
1356 | 9b3c35e0 | j_mayer | target_ulong pd; |
1357 | c227f099 | Anthony Liguori | ram_addr_t ram_addr; |
1358 | c2f07f81 | pbrook | PhysPageDesc *p; |
1359 | d720b93d | bellard | |
1360 | c2f07f81 | pbrook | addr = cpu_get_phys_page_debug(env, pc); |
1361 | c2f07f81 | pbrook | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
1362 | c2f07f81 | pbrook | if (!p) {
|
1363 | c2f07f81 | pbrook | pd = IO_MEM_UNASSIGNED; |
1364 | c2f07f81 | pbrook | } else {
|
1365 | c2f07f81 | pbrook | pd = p->phys_offset; |
1366 | c2f07f81 | pbrook | } |
1367 | c2f07f81 | pbrook | ram_addr = (pd & TARGET_PAGE_MASK) | (pc & ~TARGET_PAGE_MASK); |
1368 | 706cd4b5 | pbrook | tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0); |
1369 | d720b93d | bellard | } |
1370 | c27004ec | bellard | #endif
|
1371 | 94df27fd | Paul Brook | #endif /* TARGET_HAS_ICE */ |
1372 | d720b93d | bellard | |
1373 | c527ee8f | Paul Brook | #if defined(CONFIG_USER_ONLY)
|
1374 | c527ee8f | Paul Brook | void cpu_watchpoint_remove_all(CPUState *env, int mask) |
1375 | c527ee8f | Paul Brook | |
1376 | c527ee8f | Paul Brook | { |
1377 | c527ee8f | Paul Brook | } |
1378 | c527ee8f | Paul Brook | |
1379 | c527ee8f | Paul Brook | int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len,
|
1380 | c527ee8f | Paul Brook | int flags, CPUWatchpoint **watchpoint)
|
1381 | c527ee8f | Paul Brook | { |
1382 | c527ee8f | Paul Brook | return -ENOSYS;
|
1383 | c527ee8f | Paul Brook | } |
1384 | c527ee8f | Paul Brook | #else
|
1385 | 6658ffb8 | pbrook | /* Add a watchpoint. */
|
1386 | a1d1bb31 | aliguori | int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len,
|
1387 | a1d1bb31 | aliguori | int flags, CPUWatchpoint **watchpoint)
|
1388 | 6658ffb8 | pbrook | { |
1389 | b4051334 | aliguori | target_ulong len_mask = ~(len - 1);
|
1390 | c0ce998e | aliguori | CPUWatchpoint *wp; |
1391 | 6658ffb8 | pbrook | |
1392 | b4051334 | aliguori | /* sanity checks: allow power-of-2 lengths, deny unaligned watchpoints */
|
1393 | b4051334 | aliguori | if ((len != 1 && len != 2 && len != 4 && len != 8) || (addr & ~len_mask)) { |
1394 | b4051334 | aliguori | fprintf(stderr, "qemu: tried to set invalid watchpoint at "
|
1395 | b4051334 | aliguori | TARGET_FMT_lx ", len=" TARGET_FMT_lu "\n", addr, len); |
1396 | b4051334 | aliguori | return -EINVAL;
|
1397 | b4051334 | aliguori | } |
1398 | a1d1bb31 | aliguori | wp = qemu_malloc(sizeof(*wp));
|
1399 | a1d1bb31 | aliguori | |
1400 | a1d1bb31 | aliguori | wp->vaddr = addr; |
1401 | b4051334 | aliguori | wp->len_mask = len_mask; |
1402 | a1d1bb31 | aliguori | wp->flags = flags; |
1403 | a1d1bb31 | aliguori | |
1404 | 2dc9f411 | aliguori | /* keep all GDB-injected watchpoints in front */
|
1405 | c0ce998e | aliguori | if (flags & BP_GDB)
|
1406 | 72cf2d4f | Blue Swirl | QTAILQ_INSERT_HEAD(&env->watchpoints, wp, entry); |
1407 | c0ce998e | aliguori | else
|
1408 | 72cf2d4f | Blue Swirl | QTAILQ_INSERT_TAIL(&env->watchpoints, wp, entry); |
1409 | 6658ffb8 | pbrook | |
1410 | 6658ffb8 | pbrook | tlb_flush_page(env, addr); |
1411 | a1d1bb31 | aliguori | |
1412 | a1d1bb31 | aliguori | if (watchpoint)
|
1413 | a1d1bb31 | aliguori | *watchpoint = wp; |
1414 | a1d1bb31 | aliguori | return 0; |
1415 | 6658ffb8 | pbrook | } |
1416 | 6658ffb8 | pbrook | |
1417 | a1d1bb31 | aliguori | /* Remove a specific watchpoint. */
|
1418 | a1d1bb31 | aliguori | int cpu_watchpoint_remove(CPUState *env, target_ulong addr, target_ulong len,
|
1419 | a1d1bb31 | aliguori | int flags)
|
1420 | 6658ffb8 | pbrook | { |
1421 | b4051334 | aliguori | target_ulong len_mask = ~(len - 1);
|
1422 | a1d1bb31 | aliguori | CPUWatchpoint *wp; |
1423 | 6658ffb8 | pbrook | |
1424 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(wp, &env->watchpoints, entry) { |
1425 | b4051334 | aliguori | if (addr == wp->vaddr && len_mask == wp->len_mask
|
1426 | 6e140f28 | aliguori | && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) { |
1427 | a1d1bb31 | aliguori | cpu_watchpoint_remove_by_ref(env, wp); |
1428 | 6658ffb8 | pbrook | return 0; |
1429 | 6658ffb8 | pbrook | } |
1430 | 6658ffb8 | pbrook | } |
1431 | a1d1bb31 | aliguori | return -ENOENT;
|
1432 | 6658ffb8 | pbrook | } |
1433 | 6658ffb8 | pbrook | |
1434 | a1d1bb31 | aliguori | /* Remove a specific watchpoint by reference. */
|
1435 | a1d1bb31 | aliguori | void cpu_watchpoint_remove_by_ref(CPUState *env, CPUWatchpoint *watchpoint)
|
1436 | a1d1bb31 | aliguori | { |
1437 | 72cf2d4f | Blue Swirl | QTAILQ_REMOVE(&env->watchpoints, watchpoint, entry); |
1438 | 7d03f82f | edgar_igl | |
1439 | a1d1bb31 | aliguori | tlb_flush_page(env, watchpoint->vaddr); |
1440 | a1d1bb31 | aliguori | |
1441 | a1d1bb31 | aliguori | qemu_free(watchpoint); |
1442 | a1d1bb31 | aliguori | } |
1443 | a1d1bb31 | aliguori | |
1444 | a1d1bb31 | aliguori | /* Remove all matching watchpoints. */
|
1445 | a1d1bb31 | aliguori | void cpu_watchpoint_remove_all(CPUState *env, int mask) |
1446 | a1d1bb31 | aliguori | { |
1447 | c0ce998e | aliguori | CPUWatchpoint *wp, *next; |
1448 | a1d1bb31 | aliguori | |
1449 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH_SAFE(wp, &env->watchpoints, entry, next) { |
1450 | a1d1bb31 | aliguori | if (wp->flags & mask)
|
1451 | a1d1bb31 | aliguori | cpu_watchpoint_remove_by_ref(env, wp); |
1452 | c0ce998e | aliguori | } |
1453 | 7d03f82f | edgar_igl | } |
1454 | c527ee8f | Paul Brook | #endif
|
1455 | 7d03f82f | edgar_igl | |
1456 | a1d1bb31 | aliguori | /* Add a breakpoint. */
|
1457 | a1d1bb31 | aliguori | int cpu_breakpoint_insert(CPUState *env, target_ulong pc, int flags, |
1458 | a1d1bb31 | aliguori | CPUBreakpoint **breakpoint) |
1459 | 4c3a88a2 | bellard | { |
1460 | 1fddef4b | bellard | #if defined(TARGET_HAS_ICE)
|
1461 | c0ce998e | aliguori | CPUBreakpoint *bp; |
1462 | 3b46e624 | ths | |
1463 | a1d1bb31 | aliguori | bp = qemu_malloc(sizeof(*bp));
|
1464 | 4c3a88a2 | bellard | |
1465 | a1d1bb31 | aliguori | bp->pc = pc; |
1466 | a1d1bb31 | aliguori | bp->flags = flags; |
1467 | a1d1bb31 | aliguori | |
1468 | 2dc9f411 | aliguori | /* keep all GDB-injected breakpoints in front */
|
1469 | c0ce998e | aliguori | if (flags & BP_GDB)
|
1470 | 72cf2d4f | Blue Swirl | QTAILQ_INSERT_HEAD(&env->breakpoints, bp, entry); |
1471 | c0ce998e | aliguori | else
|
1472 | 72cf2d4f | Blue Swirl | QTAILQ_INSERT_TAIL(&env->breakpoints, bp, entry); |
1473 | 3b46e624 | ths | |
1474 | d720b93d | bellard | breakpoint_invalidate(env, pc); |
1475 | a1d1bb31 | aliguori | |
1476 | a1d1bb31 | aliguori | if (breakpoint)
|
1477 | a1d1bb31 | aliguori | *breakpoint = bp; |
1478 | 4c3a88a2 | bellard | return 0; |
1479 | 4c3a88a2 | bellard | #else
|
1480 | a1d1bb31 | aliguori | return -ENOSYS;
|
1481 | 4c3a88a2 | bellard | #endif
|
1482 | 4c3a88a2 | bellard | } |
1483 | 4c3a88a2 | bellard | |
1484 | a1d1bb31 | aliguori | /* Remove a specific breakpoint. */
|
1485 | a1d1bb31 | aliguori | int cpu_breakpoint_remove(CPUState *env, target_ulong pc, int flags) |
1486 | a1d1bb31 | aliguori | { |
1487 | 7d03f82f | edgar_igl | #if defined(TARGET_HAS_ICE)
|
1488 | a1d1bb31 | aliguori | CPUBreakpoint *bp; |
1489 | a1d1bb31 | aliguori | |
1490 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(bp, &env->breakpoints, entry) { |
1491 | a1d1bb31 | aliguori | if (bp->pc == pc && bp->flags == flags) {
|
1492 | a1d1bb31 | aliguori | cpu_breakpoint_remove_by_ref(env, bp); |
1493 | a1d1bb31 | aliguori | return 0; |
1494 | a1d1bb31 | aliguori | } |
1495 | 7d03f82f | edgar_igl | } |
1496 | a1d1bb31 | aliguori | return -ENOENT;
|
1497 | a1d1bb31 | aliguori | #else
|
1498 | a1d1bb31 | aliguori | return -ENOSYS;
|
1499 | 7d03f82f | edgar_igl | #endif
|
1500 | 7d03f82f | edgar_igl | } |
1501 | 7d03f82f | edgar_igl | |
1502 | a1d1bb31 | aliguori | /* Remove a specific breakpoint by reference. */
|
1503 | a1d1bb31 | aliguori | void cpu_breakpoint_remove_by_ref(CPUState *env, CPUBreakpoint *breakpoint)
|
1504 | 4c3a88a2 | bellard | { |
1505 | 1fddef4b | bellard | #if defined(TARGET_HAS_ICE)
|
1506 | 72cf2d4f | Blue Swirl | QTAILQ_REMOVE(&env->breakpoints, breakpoint, entry); |
1507 | d720b93d | bellard | |
1508 | a1d1bb31 | aliguori | breakpoint_invalidate(env, breakpoint->pc); |
1509 | a1d1bb31 | aliguori | |
1510 | a1d1bb31 | aliguori | qemu_free(breakpoint); |
1511 | a1d1bb31 | aliguori | #endif
|
1512 | a1d1bb31 | aliguori | } |
1513 | a1d1bb31 | aliguori | |
1514 | a1d1bb31 | aliguori | /* Remove all matching breakpoints. */
|
1515 | a1d1bb31 | aliguori | void cpu_breakpoint_remove_all(CPUState *env, int mask) |
1516 | a1d1bb31 | aliguori | { |
1517 | a1d1bb31 | aliguori | #if defined(TARGET_HAS_ICE)
|
1518 | c0ce998e | aliguori | CPUBreakpoint *bp, *next; |
1519 | a1d1bb31 | aliguori | |
1520 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH_SAFE(bp, &env->breakpoints, entry, next) { |
1521 | a1d1bb31 | aliguori | if (bp->flags & mask)
|
1522 | a1d1bb31 | aliguori | cpu_breakpoint_remove_by_ref(env, bp); |
1523 | c0ce998e | aliguori | } |
1524 | 4c3a88a2 | bellard | #endif
|
1525 | 4c3a88a2 | bellard | } |
1526 | 4c3a88a2 | bellard | |
1527 | c33a346e | bellard | /* enable or disable single step mode. EXCP_DEBUG is returned by the
|
1528 | c33a346e | bellard | CPU loop after each instruction */
|
1529 | c33a346e | bellard | void cpu_single_step(CPUState *env, int enabled) |
1530 | c33a346e | bellard | { |
1531 | 1fddef4b | bellard | #if defined(TARGET_HAS_ICE)
|
1532 | c33a346e | bellard | if (env->singlestep_enabled != enabled) {
|
1533 | c33a346e | bellard | env->singlestep_enabled = enabled; |
1534 | e22a25c9 | aliguori | if (kvm_enabled())
|
1535 | e22a25c9 | aliguori | kvm_update_guest_debug(env, 0);
|
1536 | e22a25c9 | aliguori | else {
|
1537 | ccbb4d44 | Stuart Brady | /* must flush all the translated code to avoid inconsistencies */
|
1538 | e22a25c9 | aliguori | /* XXX: only flush what is necessary */
|
1539 | e22a25c9 | aliguori | tb_flush(env); |
1540 | e22a25c9 | aliguori | } |
1541 | c33a346e | bellard | } |
1542 | c33a346e | bellard | #endif
|
1543 | c33a346e | bellard | } |
1544 | c33a346e | bellard | |
1545 | 34865134 | bellard | /* enable or disable low levels log */
|
1546 | 34865134 | bellard | void cpu_set_log(int log_flags) |
1547 | 34865134 | bellard | { |
1548 | 34865134 | bellard | loglevel = log_flags; |
1549 | 34865134 | bellard | if (loglevel && !logfile) {
|
1550 | 11fcfab4 | pbrook | logfile = fopen(logfilename, log_append ? "a" : "w"); |
1551 | 34865134 | bellard | if (!logfile) {
|
1552 | 34865134 | bellard | perror(logfilename); |
1553 | 34865134 | bellard | _exit(1);
|
1554 | 34865134 | bellard | } |
1555 | 9fa3e853 | bellard | #if !defined(CONFIG_SOFTMMU)
|
1556 | 9fa3e853 | bellard | /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
|
1557 | 9fa3e853 | bellard | { |
1558 | b55266b5 | blueswir1 | static char logfile_buf[4096]; |
1559 | 9fa3e853 | bellard | setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
|
1560 | 9fa3e853 | bellard | } |
1561 | bf65f53f | Filip Navara | #elif !defined(_WIN32)
|
1562 | bf65f53f | Filip Navara | /* Win32 doesn't support line-buffering and requires size >= 2 */
|
1563 | 34865134 | bellard | setvbuf(logfile, NULL, _IOLBF, 0); |
1564 | 9fa3e853 | bellard | #endif
|
1565 | e735b91c | pbrook | log_append = 1;
|
1566 | e735b91c | pbrook | } |
1567 | e735b91c | pbrook | if (!loglevel && logfile) {
|
1568 | e735b91c | pbrook | fclose(logfile); |
1569 | e735b91c | pbrook | logfile = NULL;
|
1570 | 34865134 | bellard | } |
1571 | 34865134 | bellard | } |
1572 | 34865134 | bellard | |
1573 | 34865134 | bellard | void cpu_set_log_filename(const char *filename) |
1574 | 34865134 | bellard | { |
1575 | 34865134 | bellard | logfilename = strdup(filename); |
1576 | e735b91c | pbrook | if (logfile) {
|
1577 | e735b91c | pbrook | fclose(logfile); |
1578 | e735b91c | pbrook | logfile = NULL;
|
1579 | e735b91c | pbrook | } |
1580 | e735b91c | pbrook | cpu_set_log(loglevel); |
1581 | 34865134 | bellard | } |
1582 | c33a346e | bellard | |
1583 | 3098dba0 | aurel32 | static void cpu_unlink_tb(CPUState *env) |
1584 | ea041c0e | bellard | { |
1585 | 3098dba0 | aurel32 | /* FIXME: TB unchaining isn't SMP safe. For now just ignore the
|
1586 | 3098dba0 | aurel32 | problem and hope the cpu will stop of its own accord. For userspace
|
1587 | 3098dba0 | aurel32 | emulation this often isn't actually as bad as it sounds. Often
|
1588 | 3098dba0 | aurel32 | signals are used primarily to interrupt blocking syscalls. */
|
1589 | ea041c0e | bellard | TranslationBlock *tb; |
1590 | c227f099 | Anthony Liguori | static spinlock_t interrupt_lock = SPIN_LOCK_UNLOCKED;
|
1591 | 59817ccb | bellard | |
1592 | cab1b4bd | Riku Voipio | spin_lock(&interrupt_lock); |
1593 | 3098dba0 | aurel32 | tb = env->current_tb; |
1594 | 3098dba0 | aurel32 | /* if the cpu is currently executing code, we must unlink it and
|
1595 | 3098dba0 | aurel32 | all the potentially executing TB */
|
1596 | f76cfe56 | Riku Voipio | if (tb) {
|
1597 | 3098dba0 | aurel32 | env->current_tb = NULL;
|
1598 | 3098dba0 | aurel32 | tb_reset_jump_recursive(tb); |
1599 | be214e6c | aurel32 | } |
1600 | cab1b4bd | Riku Voipio | spin_unlock(&interrupt_lock); |
1601 | 3098dba0 | aurel32 | } |
1602 | 3098dba0 | aurel32 | |
1603 | 3098dba0 | aurel32 | /* mask must never be zero, except for A20 change call */
|
1604 | 3098dba0 | aurel32 | void cpu_interrupt(CPUState *env, int mask) |
1605 | 3098dba0 | aurel32 | { |
1606 | 3098dba0 | aurel32 | int old_mask;
|
1607 | be214e6c | aurel32 | |
1608 | 2e70f6ef | pbrook | old_mask = env->interrupt_request; |
1609 | 68a79315 | bellard | env->interrupt_request |= mask; |
1610 | 3098dba0 | aurel32 | |
1611 | 8edac960 | aliguori | #ifndef CONFIG_USER_ONLY
|
1612 | 8edac960 | aliguori | /*
|
1613 | 8edac960 | aliguori | * If called from iothread context, wake the target cpu in
|
1614 | 8edac960 | aliguori | * case its halted.
|
1615 | 8edac960 | aliguori | */
|
1616 | 8edac960 | aliguori | if (!qemu_cpu_self(env)) {
|
1617 | 8edac960 | aliguori | qemu_cpu_kick(env); |
1618 | 8edac960 | aliguori | return;
|
1619 | 8edac960 | aliguori | } |
1620 | 8edac960 | aliguori | #endif
|
1621 | 8edac960 | aliguori | |
1622 | 2e70f6ef | pbrook | if (use_icount) {
|
1623 | 266910c4 | pbrook | env->icount_decr.u16.high = 0xffff;
|
1624 | 2e70f6ef | pbrook | #ifndef CONFIG_USER_ONLY
|
1625 | 2e70f6ef | pbrook | if (!can_do_io(env)
|
1626 | be214e6c | aurel32 | && (mask & ~old_mask) != 0) {
|
1627 | 2e70f6ef | pbrook | cpu_abort(env, "Raised interrupt while not in I/O function");
|
1628 | 2e70f6ef | pbrook | } |
1629 | 2e70f6ef | pbrook | #endif
|
1630 | 2e70f6ef | pbrook | } else {
|
1631 | 3098dba0 | aurel32 | cpu_unlink_tb(env); |
1632 | ea041c0e | bellard | } |
1633 | ea041c0e | bellard | } |
1634 | ea041c0e | bellard | |
1635 | b54ad049 | bellard | void cpu_reset_interrupt(CPUState *env, int mask) |
1636 | b54ad049 | bellard | { |
1637 | b54ad049 | bellard | env->interrupt_request &= ~mask; |
1638 | b54ad049 | bellard | } |
1639 | b54ad049 | bellard | |
1640 | 3098dba0 | aurel32 | void cpu_exit(CPUState *env)
|
1641 | 3098dba0 | aurel32 | { |
1642 | 3098dba0 | aurel32 | env->exit_request = 1;
|
1643 | 3098dba0 | aurel32 | cpu_unlink_tb(env); |
1644 | 3098dba0 | aurel32 | } |
1645 | 3098dba0 | aurel32 | |
1646 | c7cd6a37 | blueswir1 | const CPULogItem cpu_log_items[] = {
|
1647 | 5fafdf24 | ths | { CPU_LOG_TB_OUT_ASM, "out_asm",
|
1648 | f193c797 | bellard | "show generated host assembly code for each compiled TB" },
|
1649 | f193c797 | bellard | { CPU_LOG_TB_IN_ASM, "in_asm",
|
1650 | f193c797 | bellard | "show target assembly code for each compiled TB" },
|
1651 | 5fafdf24 | ths | { CPU_LOG_TB_OP, "op",
|
1652 | 57fec1fe | bellard | "show micro ops for each compiled TB" },
|
1653 | f193c797 | bellard | { CPU_LOG_TB_OP_OPT, "op_opt",
|
1654 | e01a1157 | blueswir1 | "show micro ops "
|
1655 | e01a1157 | blueswir1 | #ifdef TARGET_I386
|
1656 | e01a1157 | blueswir1 | "before eflags optimization and "
|
1657 | f193c797 | bellard | #endif
|
1658 | e01a1157 | blueswir1 | "after liveness analysis" },
|
1659 | f193c797 | bellard | { CPU_LOG_INT, "int",
|
1660 | f193c797 | bellard | "show interrupts/exceptions in short format" },
|
1661 | f193c797 | bellard | { CPU_LOG_EXEC, "exec",
|
1662 | f193c797 | bellard | "show trace before each executed TB (lots of logs)" },
|
1663 | 9fddaa0c | bellard | { CPU_LOG_TB_CPU, "cpu",
|
1664 | e91c8a77 | ths | "show CPU state before block translation" },
|
1665 | f193c797 | bellard | #ifdef TARGET_I386
|
1666 | f193c797 | bellard | { CPU_LOG_PCALL, "pcall",
|
1667 | f193c797 | bellard | "show protected mode far calls/returns/exceptions" },
|
1668 | eca1bdf4 | aliguori | { CPU_LOG_RESET, "cpu_reset",
|
1669 | eca1bdf4 | aliguori | "show CPU state before CPU resets" },
|
1670 | f193c797 | bellard | #endif
|
1671 | 8e3a9fd2 | bellard | #ifdef DEBUG_IOPORT
|
1672 | fd872598 | bellard | { CPU_LOG_IOPORT, "ioport",
|
1673 | fd872598 | bellard | "show all i/o ports accesses" },
|
1674 | 8e3a9fd2 | bellard | #endif
|
1675 | f193c797 | bellard | { 0, NULL, NULL }, |
1676 | f193c797 | bellard | }; |
1677 | f193c797 | bellard | |
1678 | f6f3fbca | Michael S. Tsirkin | #ifndef CONFIG_USER_ONLY
|
1679 | f6f3fbca | Michael S. Tsirkin | static QLIST_HEAD(memory_client_list, CPUPhysMemoryClient) memory_client_list
|
1680 | f6f3fbca | Michael S. Tsirkin | = QLIST_HEAD_INITIALIZER(memory_client_list); |
1681 | f6f3fbca | Michael S. Tsirkin | |
1682 | f6f3fbca | Michael S. Tsirkin | static void cpu_notify_set_memory(target_phys_addr_t start_addr, |
1683 | f6f3fbca | Michael S. Tsirkin | ram_addr_t size, |
1684 | f6f3fbca | Michael S. Tsirkin | ram_addr_t phys_offset) |
1685 | f6f3fbca | Michael S. Tsirkin | { |
1686 | f6f3fbca | Michael S. Tsirkin | CPUPhysMemoryClient *client; |
1687 | f6f3fbca | Michael S. Tsirkin | QLIST_FOREACH(client, &memory_client_list, list) { |
1688 | f6f3fbca | Michael S. Tsirkin | client->set_memory(client, start_addr, size, phys_offset); |
1689 | f6f3fbca | Michael S. Tsirkin | } |
1690 | f6f3fbca | Michael S. Tsirkin | } |
1691 | f6f3fbca | Michael S. Tsirkin | |
1692 | f6f3fbca | Michael S. Tsirkin | static int cpu_notify_sync_dirty_bitmap(target_phys_addr_t start, |
1693 | f6f3fbca | Michael S. Tsirkin | target_phys_addr_t end) |
1694 | f6f3fbca | Michael S. Tsirkin | { |
1695 | f6f3fbca | Michael S. Tsirkin | CPUPhysMemoryClient *client; |
1696 | f6f3fbca | Michael S. Tsirkin | QLIST_FOREACH(client, &memory_client_list, list) { |
1697 | f6f3fbca | Michael S. Tsirkin | int r = client->sync_dirty_bitmap(client, start, end);
|
1698 | f6f3fbca | Michael S. Tsirkin | if (r < 0) |
1699 | f6f3fbca | Michael S. Tsirkin | return r;
|
1700 | f6f3fbca | Michael S. Tsirkin | } |
1701 | f6f3fbca | Michael S. Tsirkin | return 0; |
1702 | f6f3fbca | Michael S. Tsirkin | } |
1703 | f6f3fbca | Michael S. Tsirkin | |
1704 | f6f3fbca | Michael S. Tsirkin | static int cpu_notify_migration_log(int enable) |
1705 | f6f3fbca | Michael S. Tsirkin | { |
1706 | f6f3fbca | Michael S. Tsirkin | CPUPhysMemoryClient *client; |
1707 | f6f3fbca | Michael S. Tsirkin | QLIST_FOREACH(client, &memory_client_list, list) { |
1708 | f6f3fbca | Michael S. Tsirkin | int r = client->migration_log(client, enable);
|
1709 | f6f3fbca | Michael S. Tsirkin | if (r < 0) |
1710 | f6f3fbca | Michael S. Tsirkin | return r;
|
1711 | f6f3fbca | Michael S. Tsirkin | } |
1712 | f6f3fbca | Michael S. Tsirkin | return 0; |
1713 | f6f3fbca | Michael S. Tsirkin | } |
1714 | f6f3fbca | Michael S. Tsirkin | |
1715 | 5cd2c5b6 | Richard Henderson | static void phys_page_for_each_1(CPUPhysMemoryClient *client, |
1716 | 5cd2c5b6 | Richard Henderson | int level, void **lp) |
1717 | f6f3fbca | Michael S. Tsirkin | { |
1718 | 5cd2c5b6 | Richard Henderson | int i;
|
1719 | f6f3fbca | Michael S. Tsirkin | |
1720 | 5cd2c5b6 | Richard Henderson | if (*lp == NULL) { |
1721 | 5cd2c5b6 | Richard Henderson | return;
|
1722 | 5cd2c5b6 | Richard Henderson | } |
1723 | 5cd2c5b6 | Richard Henderson | if (level == 0) { |
1724 | 5cd2c5b6 | Richard Henderson | PhysPageDesc *pd = *lp; |
1725 | 7296abac | Paul Brook | for (i = 0; i < L2_SIZE; ++i) { |
1726 | 5cd2c5b6 | Richard Henderson | if (pd[i].phys_offset != IO_MEM_UNASSIGNED) {
|
1727 | 5cd2c5b6 | Richard Henderson | client->set_memory(client, pd[i].region_offset, |
1728 | 5cd2c5b6 | Richard Henderson | TARGET_PAGE_SIZE, pd[i].phys_offset); |
1729 | f6f3fbca | Michael S. Tsirkin | } |
1730 | 5cd2c5b6 | Richard Henderson | } |
1731 | 5cd2c5b6 | Richard Henderson | } else {
|
1732 | 5cd2c5b6 | Richard Henderson | void **pp = *lp;
|
1733 | 7296abac | Paul Brook | for (i = 0; i < L2_SIZE; ++i) { |
1734 | 5cd2c5b6 | Richard Henderson | phys_page_for_each_1(client, level - 1, pp + i);
|
1735 | f6f3fbca | Michael S. Tsirkin | } |
1736 | f6f3fbca | Michael S. Tsirkin | } |
1737 | f6f3fbca | Michael S. Tsirkin | } |
1738 | f6f3fbca | Michael S. Tsirkin | |
1739 | f6f3fbca | Michael S. Tsirkin | static void phys_page_for_each(CPUPhysMemoryClient *client) |
1740 | f6f3fbca | Michael S. Tsirkin | { |
1741 | 5cd2c5b6 | Richard Henderson | int i;
|
1742 | 5cd2c5b6 | Richard Henderson | for (i = 0; i < P_L1_SIZE; ++i) { |
1743 | 5cd2c5b6 | Richard Henderson | phys_page_for_each_1(client, P_L1_SHIFT / L2_BITS - 1,
|
1744 | 5cd2c5b6 | Richard Henderson | l1_phys_map + 1);
|
1745 | f6f3fbca | Michael S. Tsirkin | } |
1746 | f6f3fbca | Michael S. Tsirkin | } |
1747 | f6f3fbca | Michael S. Tsirkin | |
1748 | f6f3fbca | Michael S. Tsirkin | void cpu_register_phys_memory_client(CPUPhysMemoryClient *client)
|
1749 | f6f3fbca | Michael S. Tsirkin | { |
1750 | f6f3fbca | Michael S. Tsirkin | QLIST_INSERT_HEAD(&memory_client_list, client, list); |
1751 | f6f3fbca | Michael S. Tsirkin | phys_page_for_each(client); |
1752 | f6f3fbca | Michael S. Tsirkin | } |
1753 | f6f3fbca | Michael S. Tsirkin | |
1754 | f6f3fbca | Michael S. Tsirkin | void cpu_unregister_phys_memory_client(CPUPhysMemoryClient *client)
|
1755 | f6f3fbca | Michael S. Tsirkin | { |
1756 | f6f3fbca | Michael S. Tsirkin | QLIST_REMOVE(client, list); |
1757 | f6f3fbca | Michael S. Tsirkin | } |
1758 | f6f3fbca | Michael S. Tsirkin | #endif
|
1759 | f6f3fbca | Michael S. Tsirkin | |
1760 | f193c797 | bellard | static int cmp1(const char *s1, int n, const char *s2) |
1761 | f193c797 | bellard | { |
1762 | f193c797 | bellard | if (strlen(s2) != n)
|
1763 | f193c797 | bellard | return 0; |
1764 | f193c797 | bellard | return memcmp(s1, s2, n) == 0; |
1765 | f193c797 | bellard | } |
1766 | 3b46e624 | ths | |
1767 | f193c797 | bellard | /* takes a comma separated list of log masks. Return 0 if error. */
|
1768 | f193c797 | bellard | int cpu_str_to_log_mask(const char *str) |
1769 | f193c797 | bellard | { |
1770 | c7cd6a37 | blueswir1 | const CPULogItem *item;
|
1771 | f193c797 | bellard | int mask;
|
1772 | f193c797 | bellard | const char *p, *p1; |
1773 | f193c797 | bellard | |
1774 | f193c797 | bellard | p = str; |
1775 | f193c797 | bellard | mask = 0;
|
1776 | f193c797 | bellard | for(;;) {
|
1777 | f193c797 | bellard | p1 = strchr(p, ',');
|
1778 | f193c797 | bellard | if (!p1)
|
1779 | f193c797 | bellard | p1 = p + strlen(p); |
1780 | 8e3a9fd2 | bellard | if(cmp1(p,p1-p,"all")) { |
1781 | 8e3a9fd2 | bellard | for(item = cpu_log_items; item->mask != 0; item++) { |
1782 | 8e3a9fd2 | bellard | mask |= item->mask; |
1783 | 8e3a9fd2 | bellard | } |
1784 | 8e3a9fd2 | bellard | } else {
|
1785 | f193c797 | bellard | for(item = cpu_log_items; item->mask != 0; item++) { |
1786 | f193c797 | bellard | if (cmp1(p, p1 - p, item->name))
|
1787 | f193c797 | bellard | goto found;
|
1788 | f193c797 | bellard | } |
1789 | f193c797 | bellard | return 0; |
1790 | 8e3a9fd2 | bellard | } |
1791 | f193c797 | bellard | found:
|
1792 | f193c797 | bellard | mask |= item->mask; |
1793 | f193c797 | bellard | if (*p1 != ',') |
1794 | f193c797 | bellard | break;
|
1795 | f193c797 | bellard | p = p1 + 1;
|
1796 | f193c797 | bellard | } |
1797 | f193c797 | bellard | return mask;
|
1798 | f193c797 | bellard | } |
1799 | ea041c0e | bellard | |
1800 | 7501267e | bellard | void cpu_abort(CPUState *env, const char *fmt, ...) |
1801 | 7501267e | bellard | { |
1802 | 7501267e | bellard | va_list ap; |
1803 | 493ae1f0 | pbrook | va_list ap2; |
1804 | 7501267e | bellard | |
1805 | 7501267e | bellard | va_start(ap, fmt); |
1806 | 493ae1f0 | pbrook | va_copy(ap2, ap); |
1807 | 7501267e | bellard | fprintf(stderr, "qemu: fatal: ");
|
1808 | 7501267e | bellard | vfprintf(stderr, fmt, ap); |
1809 | 7501267e | bellard | fprintf(stderr, "\n");
|
1810 | 7501267e | bellard | #ifdef TARGET_I386
|
1811 | 7fe48483 | bellard | cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP); |
1812 | 7fe48483 | bellard | #else
|
1813 | 7fe48483 | bellard | cpu_dump_state(env, stderr, fprintf, 0);
|
1814 | 7501267e | bellard | #endif
|
1815 | 93fcfe39 | aliguori | if (qemu_log_enabled()) {
|
1816 | 93fcfe39 | aliguori | qemu_log("qemu: fatal: ");
|
1817 | 93fcfe39 | aliguori | qemu_log_vprintf(fmt, ap2); |
1818 | 93fcfe39 | aliguori | qemu_log("\n");
|
1819 | f9373291 | j_mayer | #ifdef TARGET_I386
|
1820 | 93fcfe39 | aliguori | log_cpu_state(env, X86_DUMP_FPU | X86_DUMP_CCOP); |
1821 | f9373291 | j_mayer | #else
|
1822 | 93fcfe39 | aliguori | log_cpu_state(env, 0);
|
1823 | f9373291 | j_mayer | #endif
|
1824 | 31b1a7b4 | aliguori | qemu_log_flush(); |
1825 | 93fcfe39 | aliguori | qemu_log_close(); |
1826 | 924edcae | balrog | } |
1827 | 493ae1f0 | pbrook | va_end(ap2); |
1828 | f9373291 | j_mayer | va_end(ap); |
1829 | fd052bf6 | Riku Voipio | #if defined(CONFIG_USER_ONLY)
|
1830 | fd052bf6 | Riku Voipio | { |
1831 | fd052bf6 | Riku Voipio | struct sigaction act;
|
1832 | fd052bf6 | Riku Voipio | sigfillset(&act.sa_mask); |
1833 | fd052bf6 | Riku Voipio | act.sa_handler = SIG_DFL; |
1834 | fd052bf6 | Riku Voipio | sigaction(SIGABRT, &act, NULL);
|
1835 | fd052bf6 | Riku Voipio | } |
1836 | fd052bf6 | Riku Voipio | #endif
|
1837 | 7501267e | bellard | abort(); |
1838 | 7501267e | bellard | } |
1839 | 7501267e | bellard | |
1840 | c5be9f08 | ths | CPUState *cpu_copy(CPUState *env) |
1841 | c5be9f08 | ths | { |
1842 | 01ba9816 | ths | CPUState *new_env = cpu_init(env->cpu_model_str); |
1843 | c5be9f08 | ths | CPUState *next_cpu = new_env->next_cpu; |
1844 | c5be9f08 | ths | int cpu_index = new_env->cpu_index;
|
1845 | 5a38f081 | aliguori | #if defined(TARGET_HAS_ICE)
|
1846 | 5a38f081 | aliguori | CPUBreakpoint *bp; |
1847 | 5a38f081 | aliguori | CPUWatchpoint *wp; |
1848 | 5a38f081 | aliguori | #endif
|
1849 | 5a38f081 | aliguori | |
1850 | c5be9f08 | ths | memcpy(new_env, env, sizeof(CPUState));
|
1851 | 5a38f081 | aliguori | |
1852 | 5a38f081 | aliguori | /* Preserve chaining and index. */
|
1853 | c5be9f08 | ths | new_env->next_cpu = next_cpu; |
1854 | c5be9f08 | ths | new_env->cpu_index = cpu_index; |
1855 | 5a38f081 | aliguori | |
1856 | 5a38f081 | aliguori | /* Clone all break/watchpoints.
|
1857 | 5a38f081 | aliguori | Note: Once we support ptrace with hw-debug register access, make sure
|
1858 | 5a38f081 | aliguori | BP_CPU break/watchpoints are handled correctly on clone. */
|
1859 | 72cf2d4f | Blue Swirl | QTAILQ_INIT(&env->breakpoints); |
1860 | 72cf2d4f | Blue Swirl | QTAILQ_INIT(&env->watchpoints); |
1861 | 5a38f081 | aliguori | #if defined(TARGET_HAS_ICE)
|
1862 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(bp, &env->breakpoints, entry) { |
1863 | 5a38f081 | aliguori | cpu_breakpoint_insert(new_env, bp->pc, bp->flags, NULL);
|
1864 | 5a38f081 | aliguori | } |
1865 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(wp, &env->watchpoints, entry) { |
1866 | 5a38f081 | aliguori | cpu_watchpoint_insert(new_env, wp->vaddr, (~wp->len_mask) + 1,
|
1867 | 5a38f081 | aliguori | wp->flags, NULL);
|
1868 | 5a38f081 | aliguori | } |
1869 | 5a38f081 | aliguori | #endif
|
1870 | 5a38f081 | aliguori | |
1871 | c5be9f08 | ths | return new_env;
|
1872 | c5be9f08 | ths | } |
1873 | c5be9f08 | ths | |
1874 | 0124311e | bellard | #if !defined(CONFIG_USER_ONLY)
|
1875 | 0124311e | bellard | |
1876 | 5c751e99 | edgar_igl | static inline void tlb_flush_jmp_cache(CPUState *env, target_ulong addr) |
1877 | 5c751e99 | edgar_igl | { |
1878 | 5c751e99 | edgar_igl | unsigned int i; |
1879 | 5c751e99 | edgar_igl | |
1880 | 5c751e99 | edgar_igl | /* Discard jump cache entries for any tb which might potentially
|
1881 | 5c751e99 | edgar_igl | overlap the flushed page. */
|
1882 | 5c751e99 | edgar_igl | i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE); |
1883 | 5c751e99 | edgar_igl | memset (&env->tb_jmp_cache[i], 0,
|
1884 | 5c751e99 | edgar_igl | TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
|
1885 | 5c751e99 | edgar_igl | |
1886 | 5c751e99 | edgar_igl | i = tb_jmp_cache_hash_page(addr); |
1887 | 5c751e99 | edgar_igl | memset (&env->tb_jmp_cache[i], 0,
|
1888 | 5c751e99 | edgar_igl | TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
|
1889 | 5c751e99 | edgar_igl | } |
1890 | 5c751e99 | edgar_igl | |
1891 | 08738984 | Igor Kovalenko | static CPUTLBEntry s_cputlb_empty_entry = {
|
1892 | 08738984 | Igor Kovalenko | .addr_read = -1,
|
1893 | 08738984 | Igor Kovalenko | .addr_write = -1,
|
1894 | 08738984 | Igor Kovalenko | .addr_code = -1,
|
1895 | 08738984 | Igor Kovalenko | .addend = -1,
|
1896 | 08738984 | Igor Kovalenko | }; |
1897 | 08738984 | Igor Kovalenko | |
1898 | ee8b7021 | bellard | /* NOTE: if flush_global is true, also flush global entries (not
|
1899 | ee8b7021 | bellard | implemented yet) */
|
1900 | ee8b7021 | bellard | void tlb_flush(CPUState *env, int flush_global) |
1901 | 33417e70 | bellard | { |
1902 | 33417e70 | bellard | int i;
|
1903 | 0124311e | bellard | |
1904 | 9fa3e853 | bellard | #if defined(DEBUG_TLB)
|
1905 | 9fa3e853 | bellard | printf("tlb_flush:\n");
|
1906 | 9fa3e853 | bellard | #endif
|
1907 | 0124311e | bellard | /* must reset current TB so that interrupts cannot modify the
|
1908 | 0124311e | bellard | links while we are modifying them */
|
1909 | 0124311e | bellard | env->current_tb = NULL;
|
1910 | 0124311e | bellard | |
1911 | 33417e70 | bellard | for(i = 0; i < CPU_TLB_SIZE; i++) { |
1912 | cfde4bd9 | Isaku Yamahata | int mmu_idx;
|
1913 | cfde4bd9 | Isaku Yamahata | for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) { |
1914 | 08738984 | Igor Kovalenko | env->tlb_table[mmu_idx][i] = s_cputlb_empty_entry; |
1915 | cfde4bd9 | Isaku Yamahata | } |
1916 | 33417e70 | bellard | } |
1917 | 9fa3e853 | bellard | |
1918 | 8a40a180 | bellard | memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *)); |
1919 | 9fa3e853 | bellard | |
1920 | d4c430a8 | Paul Brook | env->tlb_flush_addr = -1;
|
1921 | d4c430a8 | Paul Brook | env->tlb_flush_mask = 0;
|
1922 | e3db7226 | bellard | tlb_flush_count++; |
1923 | 33417e70 | bellard | } |
1924 | 33417e70 | bellard | |
1925 | 274da6b2 | bellard | static inline void tlb_flush_entry(CPUTLBEntry *tlb_entry, target_ulong addr) |
1926 | 61382a50 | bellard | { |
1927 | 5fafdf24 | ths | if (addr == (tlb_entry->addr_read &
|
1928 | 84b7b8e7 | bellard | (TARGET_PAGE_MASK | TLB_INVALID_MASK)) || |
1929 | 5fafdf24 | ths | addr == (tlb_entry->addr_write & |
1930 | 84b7b8e7 | bellard | (TARGET_PAGE_MASK | TLB_INVALID_MASK)) || |
1931 | 5fafdf24 | ths | addr == (tlb_entry->addr_code & |
1932 | 84b7b8e7 | bellard | (TARGET_PAGE_MASK | TLB_INVALID_MASK))) { |
1933 | 08738984 | Igor Kovalenko | *tlb_entry = s_cputlb_empty_entry; |
1934 | 84b7b8e7 | bellard | } |
1935 | 61382a50 | bellard | } |
1936 | 61382a50 | bellard | |
1937 | 2e12669a | bellard | void tlb_flush_page(CPUState *env, target_ulong addr)
|
1938 | 33417e70 | bellard | { |
1939 | 8a40a180 | bellard | int i;
|
1940 | cfde4bd9 | Isaku Yamahata | int mmu_idx;
|
1941 | 0124311e | bellard | |
1942 | 9fa3e853 | bellard | #if defined(DEBUG_TLB)
|
1943 | 108c49b8 | bellard | printf("tlb_flush_page: " TARGET_FMT_lx "\n", addr); |
1944 | 9fa3e853 | bellard | #endif
|
1945 | d4c430a8 | Paul Brook | /* Check if we need to flush due to large pages. */
|
1946 | d4c430a8 | Paul Brook | if ((addr & env->tlb_flush_mask) == env->tlb_flush_addr) {
|
1947 | d4c430a8 | Paul Brook | #if defined(DEBUG_TLB)
|
1948 | d4c430a8 | Paul Brook | printf("tlb_flush_page: forced full flush ("
|
1949 | d4c430a8 | Paul Brook | TARGET_FMT_lx "/" TARGET_FMT_lx ")\n", |
1950 | d4c430a8 | Paul Brook | env->tlb_flush_addr, env->tlb_flush_mask); |
1951 | d4c430a8 | Paul Brook | #endif
|
1952 | d4c430a8 | Paul Brook | tlb_flush(env, 1);
|
1953 | d4c430a8 | Paul Brook | return;
|
1954 | d4c430a8 | Paul Brook | } |
1955 | 0124311e | bellard | /* must reset current TB so that interrupts cannot modify the
|
1956 | 0124311e | bellard | links while we are modifying them */
|
1957 | 0124311e | bellard | env->current_tb = NULL;
|
1958 | 61382a50 | bellard | |
1959 | 61382a50 | bellard | addr &= TARGET_PAGE_MASK; |
1960 | 61382a50 | bellard | i = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
|
1961 | cfde4bd9 | Isaku Yamahata | for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) |
1962 | cfde4bd9 | Isaku Yamahata | tlb_flush_entry(&env->tlb_table[mmu_idx][i], addr); |
1963 | 0124311e | bellard | |
1964 | 5c751e99 | edgar_igl | tlb_flush_jmp_cache(env, addr); |
1965 | 9fa3e853 | bellard | } |
1966 | 9fa3e853 | bellard | |
1967 | 9fa3e853 | bellard | /* update the TLBs so that writes to code in the virtual page 'addr'
|
1968 | 9fa3e853 | bellard | can be detected */
|
1969 | c227f099 | Anthony Liguori | static void tlb_protect_code(ram_addr_t ram_addr) |
1970 | 9fa3e853 | bellard | { |
1971 | 5fafdf24 | ths | cpu_physical_memory_reset_dirty(ram_addr, |
1972 | 6a00d601 | bellard | ram_addr + TARGET_PAGE_SIZE, |
1973 | 6a00d601 | bellard | CODE_DIRTY_FLAG); |
1974 | 9fa3e853 | bellard | } |
1975 | 9fa3e853 | bellard | |
1976 | 9fa3e853 | bellard | /* update the TLB so that writes in physical page 'phys_addr' are no longer
|
1977 | 3a7d929e | bellard | tested for self modifying code */
|
1978 | c227f099 | Anthony Liguori | static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr, |
1979 | 3a7d929e | bellard | target_ulong vaddr) |
1980 | 9fa3e853 | bellard | { |
1981 | 3a7d929e | bellard | phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] |= CODE_DIRTY_FLAG; |
1982 | 1ccde1cb | bellard | } |
1983 | 1ccde1cb | bellard | |
1984 | 5fafdf24 | ths | static inline void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry, |
1985 | 1ccde1cb | bellard | unsigned long start, unsigned long length) |
1986 | 1ccde1cb | bellard | { |
1987 | 1ccde1cb | bellard | unsigned long addr; |
1988 | 84b7b8e7 | bellard | if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
|
1989 | 84b7b8e7 | bellard | addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend; |
1990 | 1ccde1cb | bellard | if ((addr - start) < length) {
|
1991 | 0f459d16 | pbrook | tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | TLB_NOTDIRTY; |
1992 | 1ccde1cb | bellard | } |
1993 | 1ccde1cb | bellard | } |
1994 | 1ccde1cb | bellard | } |
1995 | 1ccde1cb | bellard | |
1996 | 5579c7f3 | pbrook | /* Note: start and end must be within the same ram block. */
|
1997 | c227f099 | Anthony Liguori | void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
|
1998 | 0a962c02 | bellard | int dirty_flags)
|
1999 | 1ccde1cb | bellard | { |
2000 | 1ccde1cb | bellard | CPUState *env; |
2001 | 4f2ac237 | bellard | unsigned long length, start1; |
2002 | 0a962c02 | bellard | int i, mask, len;
|
2003 | 0a962c02 | bellard | uint8_t *p; |
2004 | 1ccde1cb | bellard | |
2005 | 1ccde1cb | bellard | start &= TARGET_PAGE_MASK; |
2006 | 1ccde1cb | bellard | end = TARGET_PAGE_ALIGN(end); |
2007 | 1ccde1cb | bellard | |
2008 | 1ccde1cb | bellard | length = end - start; |
2009 | 1ccde1cb | bellard | if (length == 0) |
2010 | 1ccde1cb | bellard | return;
|
2011 | 0a962c02 | bellard | len = length >> TARGET_PAGE_BITS; |
2012 | f23db169 | bellard | mask = ~dirty_flags; |
2013 | f23db169 | bellard | p = phys_ram_dirty + (start >> TARGET_PAGE_BITS); |
2014 | f23db169 | bellard | for(i = 0; i < len; i++) |
2015 | f23db169 | bellard | p[i] &= mask; |
2016 | f23db169 | bellard | |
2017 | 1ccde1cb | bellard | /* we modify the TLB cache so that the dirty bit will be set again
|
2018 | 1ccde1cb | bellard | when accessing the range */
|
2019 | 5579c7f3 | pbrook | start1 = (unsigned long)qemu_get_ram_ptr(start); |
2020 | 5579c7f3 | pbrook | /* Chek that we don't span multiple blocks - this breaks the
|
2021 | 5579c7f3 | pbrook | address comparisons below. */
|
2022 | 5579c7f3 | pbrook | if ((unsigned long)qemu_get_ram_ptr(end - 1) - start1 |
2023 | 5579c7f3 | pbrook | != (end - 1) - start) {
|
2024 | 5579c7f3 | pbrook | abort(); |
2025 | 5579c7f3 | pbrook | } |
2026 | 5579c7f3 | pbrook | |
2027 | 6a00d601 | bellard | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
2028 | cfde4bd9 | Isaku Yamahata | int mmu_idx;
|
2029 | cfde4bd9 | Isaku Yamahata | for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) { |
2030 | cfde4bd9 | Isaku Yamahata | for(i = 0; i < CPU_TLB_SIZE; i++) |
2031 | cfde4bd9 | Isaku Yamahata | tlb_reset_dirty_range(&env->tlb_table[mmu_idx][i], |
2032 | cfde4bd9 | Isaku Yamahata | start1, length); |
2033 | cfde4bd9 | Isaku Yamahata | } |
2034 | 6a00d601 | bellard | } |
2035 | 1ccde1cb | bellard | } |
2036 | 1ccde1cb | bellard | |
2037 | 74576198 | aliguori | int cpu_physical_memory_set_dirty_tracking(int enable) |
2038 | 74576198 | aliguori | { |
2039 | f6f3fbca | Michael S. Tsirkin | int ret = 0; |
2040 | 74576198 | aliguori | in_migration = enable; |
2041 | f6f3fbca | Michael S. Tsirkin | ret = cpu_notify_migration_log(!!enable); |
2042 | f6f3fbca | Michael S. Tsirkin | return ret;
|
2043 | 74576198 | aliguori | } |
2044 | 74576198 | aliguori | |
2045 | 74576198 | aliguori | int cpu_physical_memory_get_dirty_tracking(void) |
2046 | 74576198 | aliguori | { |
2047 | 74576198 | aliguori | return in_migration;
|
2048 | 74576198 | aliguori | } |
2049 | 74576198 | aliguori | |
2050 | c227f099 | Anthony Liguori | int cpu_physical_sync_dirty_bitmap(target_phys_addr_t start_addr,
|
2051 | c227f099 | Anthony Liguori | target_phys_addr_t end_addr) |
2052 | 2bec46dc | aliguori | { |
2053 | 7b8f3b78 | Michael S. Tsirkin | int ret;
|
2054 | 151f7749 | Jan Kiszka | |
2055 | f6f3fbca | Michael S. Tsirkin | ret = cpu_notify_sync_dirty_bitmap(start_addr, end_addr); |
2056 | 151f7749 | Jan Kiszka | return ret;
|
2057 | 2bec46dc | aliguori | } |
2058 | 2bec46dc | aliguori | |
2059 | 3a7d929e | bellard | static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry) |
2060 | 3a7d929e | bellard | { |
2061 | c227f099 | Anthony Liguori | ram_addr_t ram_addr; |
2062 | 5579c7f3 | pbrook | void *p;
|
2063 | 3a7d929e | bellard | |
2064 | 84b7b8e7 | bellard | if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
|
2065 | 5579c7f3 | pbrook | p = (void *)(unsigned long)((tlb_entry->addr_write & TARGET_PAGE_MASK) |
2066 | 5579c7f3 | pbrook | + tlb_entry->addend); |
2067 | 5579c7f3 | pbrook | ram_addr = qemu_ram_addr_from_host(p); |
2068 | 3a7d929e | bellard | if (!cpu_physical_memory_is_dirty(ram_addr)) {
|
2069 | 0f459d16 | pbrook | tlb_entry->addr_write |= TLB_NOTDIRTY; |
2070 | 3a7d929e | bellard | } |
2071 | 3a7d929e | bellard | } |
2072 | 3a7d929e | bellard | } |
2073 | 3a7d929e | bellard | |
2074 | 3a7d929e | bellard | /* update the TLB according to the current state of the dirty bits */
|
2075 | 3a7d929e | bellard | void cpu_tlb_update_dirty(CPUState *env)
|
2076 | 3a7d929e | bellard | { |
2077 | 3a7d929e | bellard | int i;
|
2078 | cfde4bd9 | Isaku Yamahata | int mmu_idx;
|
2079 | cfde4bd9 | Isaku Yamahata | for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) { |
2080 | cfde4bd9 | Isaku Yamahata | for(i = 0; i < CPU_TLB_SIZE; i++) |
2081 | cfde4bd9 | Isaku Yamahata | tlb_update_dirty(&env->tlb_table[mmu_idx][i]); |
2082 | cfde4bd9 | Isaku Yamahata | } |
2083 | 3a7d929e | bellard | } |
2084 | 3a7d929e | bellard | |
2085 | 0f459d16 | pbrook | static inline void tlb_set_dirty1(CPUTLBEntry *tlb_entry, target_ulong vaddr) |
2086 | 1ccde1cb | bellard | { |
2087 | 0f459d16 | pbrook | if (tlb_entry->addr_write == (vaddr | TLB_NOTDIRTY))
|
2088 | 0f459d16 | pbrook | tlb_entry->addr_write = vaddr; |
2089 | 1ccde1cb | bellard | } |
2090 | 1ccde1cb | bellard | |
2091 | 0f459d16 | pbrook | /* update the TLB corresponding to virtual page vaddr
|
2092 | 0f459d16 | pbrook | so that it is no longer dirty */
|
2093 | 0f459d16 | pbrook | static inline void tlb_set_dirty(CPUState *env, target_ulong vaddr) |
2094 | 1ccde1cb | bellard | { |
2095 | 1ccde1cb | bellard | int i;
|
2096 | cfde4bd9 | Isaku Yamahata | int mmu_idx;
|
2097 | 1ccde1cb | bellard | |
2098 | 0f459d16 | pbrook | vaddr &= TARGET_PAGE_MASK; |
2099 | 1ccde1cb | bellard | i = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
|
2100 | cfde4bd9 | Isaku Yamahata | for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) |
2101 | cfde4bd9 | Isaku Yamahata | tlb_set_dirty1(&env->tlb_table[mmu_idx][i], vaddr); |
2102 | 9fa3e853 | bellard | } |
2103 | 9fa3e853 | bellard | |
2104 | d4c430a8 | Paul Brook | /* Our TLB does not support large pages, so remember the area covered by
|
2105 | d4c430a8 | Paul Brook | large pages and trigger a full TLB flush if these are invalidated. */
|
2106 | d4c430a8 | Paul Brook | static void tlb_add_large_page(CPUState *env, target_ulong vaddr, |
2107 | d4c430a8 | Paul Brook | target_ulong size) |
2108 | d4c430a8 | Paul Brook | { |
2109 | d4c430a8 | Paul Brook | target_ulong mask = ~(size - 1);
|
2110 | d4c430a8 | Paul Brook | |
2111 | d4c430a8 | Paul Brook | if (env->tlb_flush_addr == (target_ulong)-1) { |
2112 | d4c430a8 | Paul Brook | env->tlb_flush_addr = vaddr & mask; |
2113 | d4c430a8 | Paul Brook | env->tlb_flush_mask = mask; |
2114 | d4c430a8 | Paul Brook | return;
|
2115 | d4c430a8 | Paul Brook | } |
2116 | d4c430a8 | Paul Brook | /* Extend the existing region to include the new page.
|
2117 | d4c430a8 | Paul Brook | This is a compromise between unnecessary flushes and the cost
|
2118 | d4c430a8 | Paul Brook | of maintaining a full variable size TLB. */
|
2119 | d4c430a8 | Paul Brook | mask &= env->tlb_flush_mask; |
2120 | d4c430a8 | Paul Brook | while (((env->tlb_flush_addr ^ vaddr) & mask) != 0) { |
2121 | d4c430a8 | Paul Brook | mask <<= 1;
|
2122 | d4c430a8 | Paul Brook | } |
2123 | d4c430a8 | Paul Brook | env->tlb_flush_addr &= mask; |
2124 | d4c430a8 | Paul Brook | env->tlb_flush_mask = mask; |
2125 | d4c430a8 | Paul Brook | } |
2126 | d4c430a8 | Paul Brook | |
2127 | d4c430a8 | Paul Brook | /* Add a new TLB entry. At most one entry for a given virtual address
|
2128 | d4c430a8 | Paul Brook | is permitted. Only a single TARGET_PAGE_SIZE region is mapped, the
|
2129 | d4c430a8 | Paul Brook | supplied size is only used by tlb_flush_page. */
|
2130 | d4c430a8 | Paul Brook | void tlb_set_page(CPUState *env, target_ulong vaddr,
|
2131 | d4c430a8 | Paul Brook | target_phys_addr_t paddr, int prot,
|
2132 | d4c430a8 | Paul Brook | int mmu_idx, target_ulong size)
|
2133 | 9fa3e853 | bellard | { |
2134 | 92e873b9 | bellard | PhysPageDesc *p; |
2135 | 4f2ac237 | bellard | unsigned long pd; |
2136 | 9fa3e853 | bellard | unsigned int index; |
2137 | 4f2ac237 | bellard | target_ulong address; |
2138 | 0f459d16 | pbrook | target_ulong code_address; |
2139 | c227f099 | Anthony Liguori | target_phys_addr_t addend; |
2140 | 84b7b8e7 | bellard | CPUTLBEntry *te; |
2141 | a1d1bb31 | aliguori | CPUWatchpoint *wp; |
2142 | c227f099 | Anthony Liguori | target_phys_addr_t iotlb; |
2143 | 9fa3e853 | bellard | |
2144 | d4c430a8 | Paul Brook | assert(size >= TARGET_PAGE_SIZE); |
2145 | d4c430a8 | Paul Brook | if (size != TARGET_PAGE_SIZE) {
|
2146 | d4c430a8 | Paul Brook | tlb_add_large_page(env, vaddr, size); |
2147 | d4c430a8 | Paul Brook | } |
2148 | 92e873b9 | bellard | p = phys_page_find(paddr >> TARGET_PAGE_BITS); |
2149 | 9fa3e853 | bellard | if (!p) {
|
2150 | 9fa3e853 | bellard | pd = IO_MEM_UNASSIGNED; |
2151 | 9fa3e853 | bellard | } else {
|
2152 | 9fa3e853 | bellard | pd = p->phys_offset; |
2153 | 9fa3e853 | bellard | } |
2154 | 9fa3e853 | bellard | #if defined(DEBUG_TLB)
|
2155 | 6ebbf390 | j_mayer | printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x%08x prot=%x idx=%d smmu=%d pd=0x%08lx\n", |
2156 | 6ebbf390 | j_mayer | vaddr, (int)paddr, prot, mmu_idx, is_softmmu, pd);
|
2157 | 9fa3e853 | bellard | #endif
|
2158 | 9fa3e853 | bellard | |
2159 | 0f459d16 | pbrook | address = vaddr; |
2160 | 0f459d16 | pbrook | if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) {
|
2161 | 0f459d16 | pbrook | /* IO memory case (romd handled later) */
|
2162 | 0f459d16 | pbrook | address |= TLB_MMIO; |
2163 | 0f459d16 | pbrook | } |
2164 | 5579c7f3 | pbrook | addend = (unsigned long)qemu_get_ram_ptr(pd & TARGET_PAGE_MASK); |
2165 | 0f459d16 | pbrook | if ((pd & ~TARGET_PAGE_MASK) <= IO_MEM_ROM) {
|
2166 | 0f459d16 | pbrook | /* Normal RAM. */
|
2167 | 0f459d16 | pbrook | iotlb = pd & TARGET_PAGE_MASK; |
2168 | 0f459d16 | pbrook | if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM)
|
2169 | 0f459d16 | pbrook | iotlb |= IO_MEM_NOTDIRTY; |
2170 | 0f459d16 | pbrook | else
|
2171 | 0f459d16 | pbrook | iotlb |= IO_MEM_ROM; |
2172 | 0f459d16 | pbrook | } else {
|
2173 | ccbb4d44 | Stuart Brady | /* IO handlers are currently passed a physical address.
|
2174 | 0f459d16 | pbrook | It would be nice to pass an offset from the base address
|
2175 | 0f459d16 | pbrook | of that region. This would avoid having to special case RAM,
|
2176 | 0f459d16 | pbrook | and avoid full address decoding in every device.
|
2177 | 0f459d16 | pbrook | We can't use the high bits of pd for this because
|
2178 | 0f459d16 | pbrook | IO_MEM_ROMD uses these as a ram address. */
|
2179 | 8da3ff18 | pbrook | iotlb = (pd & ~TARGET_PAGE_MASK); |
2180 | 8da3ff18 | pbrook | if (p) {
|
2181 | 8da3ff18 | pbrook | iotlb += p->region_offset; |
2182 | 8da3ff18 | pbrook | } else {
|
2183 | 8da3ff18 | pbrook | iotlb += paddr; |
2184 | 8da3ff18 | pbrook | } |
2185 | 0f459d16 | pbrook | } |
2186 | 0f459d16 | pbrook | |
2187 | 0f459d16 | pbrook | code_address = address; |
2188 | 0f459d16 | pbrook | /* Make accesses to pages with watchpoints go via the
|
2189 | 0f459d16 | pbrook | watchpoint trap routines. */
|
2190 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(wp, &env->watchpoints, entry) { |
2191 | a1d1bb31 | aliguori | if (vaddr == (wp->vaddr & TARGET_PAGE_MASK)) {
|
2192 | 0f459d16 | pbrook | iotlb = io_mem_watch + paddr; |
2193 | 0f459d16 | pbrook | /* TODO: The memory case can be optimized by not trapping
|
2194 | 0f459d16 | pbrook | reads of pages with a write breakpoint. */
|
2195 | 0f459d16 | pbrook | address |= TLB_MMIO; |
2196 | 6658ffb8 | pbrook | } |
2197 | 0f459d16 | pbrook | } |
2198 | d79acba4 | balrog | |
2199 | 0f459d16 | pbrook | index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
|
2200 | 0f459d16 | pbrook | env->iotlb[mmu_idx][index] = iotlb - vaddr; |
2201 | 0f459d16 | pbrook | te = &env->tlb_table[mmu_idx][index]; |
2202 | 0f459d16 | pbrook | te->addend = addend - vaddr; |
2203 | 0f459d16 | pbrook | if (prot & PAGE_READ) {
|
2204 | 0f459d16 | pbrook | te->addr_read = address; |
2205 | 0f459d16 | pbrook | } else {
|
2206 | 0f459d16 | pbrook | te->addr_read = -1;
|
2207 | 0f459d16 | pbrook | } |
2208 | 5c751e99 | edgar_igl | |
2209 | 0f459d16 | pbrook | if (prot & PAGE_EXEC) {
|
2210 | 0f459d16 | pbrook | te->addr_code = code_address; |
2211 | 0f459d16 | pbrook | } else {
|
2212 | 0f459d16 | pbrook | te->addr_code = -1;
|
2213 | 0f459d16 | pbrook | } |
2214 | 0f459d16 | pbrook | if (prot & PAGE_WRITE) {
|
2215 | 0f459d16 | pbrook | if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
|
2216 | 0f459d16 | pbrook | (pd & IO_MEM_ROMD)) { |
2217 | 0f459d16 | pbrook | /* Write access calls the I/O callback. */
|
2218 | 0f459d16 | pbrook | te->addr_write = address | TLB_MMIO; |
2219 | 0f459d16 | pbrook | } else if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM && |
2220 | 0f459d16 | pbrook | !cpu_physical_memory_is_dirty(pd)) { |
2221 | 0f459d16 | pbrook | te->addr_write = address | TLB_NOTDIRTY; |
2222 | 9fa3e853 | bellard | } else {
|
2223 | 0f459d16 | pbrook | te->addr_write = address; |
2224 | 9fa3e853 | bellard | } |
2225 | 0f459d16 | pbrook | } else {
|
2226 | 0f459d16 | pbrook | te->addr_write = -1;
|
2227 | 9fa3e853 | bellard | } |
2228 | 9fa3e853 | bellard | } |
2229 | 9fa3e853 | bellard | |
2230 | 0124311e | bellard | #else
|
2231 | 0124311e | bellard | |
2232 | ee8b7021 | bellard | void tlb_flush(CPUState *env, int flush_global) |
2233 | 0124311e | bellard | { |
2234 | 0124311e | bellard | } |
2235 | 0124311e | bellard | |
2236 | 2e12669a | bellard | void tlb_flush_page(CPUState *env, target_ulong addr)
|
2237 | 0124311e | bellard | { |
2238 | 0124311e | bellard | } |
2239 | 0124311e | bellard | |
2240 | edf8e2af | Mika Westerberg | /*
|
2241 | edf8e2af | Mika Westerberg | * Walks guest process memory "regions" one by one
|
2242 | edf8e2af | Mika Westerberg | * and calls callback function 'fn' for each region.
|
2243 | edf8e2af | Mika Westerberg | */
|
2244 | 5cd2c5b6 | Richard Henderson | |
2245 | 5cd2c5b6 | Richard Henderson | struct walk_memory_regions_data
|
2246 | 5cd2c5b6 | Richard Henderson | { |
2247 | 5cd2c5b6 | Richard Henderson | walk_memory_regions_fn fn; |
2248 | 5cd2c5b6 | Richard Henderson | void *priv;
|
2249 | 5cd2c5b6 | Richard Henderson | unsigned long start; |
2250 | 5cd2c5b6 | Richard Henderson | int prot;
|
2251 | 5cd2c5b6 | Richard Henderson | }; |
2252 | 5cd2c5b6 | Richard Henderson | |
2253 | 5cd2c5b6 | Richard Henderson | static int walk_memory_regions_end(struct walk_memory_regions_data *data, |
2254 | b480d9b7 | Paul Brook | abi_ulong end, int new_prot)
|
2255 | 5cd2c5b6 | Richard Henderson | { |
2256 | 5cd2c5b6 | Richard Henderson | if (data->start != -1ul) { |
2257 | 5cd2c5b6 | Richard Henderson | int rc = data->fn(data->priv, data->start, end, data->prot);
|
2258 | 5cd2c5b6 | Richard Henderson | if (rc != 0) { |
2259 | 5cd2c5b6 | Richard Henderson | return rc;
|
2260 | 5cd2c5b6 | Richard Henderson | } |
2261 | 5cd2c5b6 | Richard Henderson | } |
2262 | 5cd2c5b6 | Richard Henderson | |
2263 | 5cd2c5b6 | Richard Henderson | data->start = (new_prot ? end : -1ul);
|
2264 | 5cd2c5b6 | Richard Henderson | data->prot = new_prot; |
2265 | 5cd2c5b6 | Richard Henderson | |
2266 | 5cd2c5b6 | Richard Henderson | return 0; |
2267 | 5cd2c5b6 | Richard Henderson | } |
2268 | 5cd2c5b6 | Richard Henderson | |
2269 | 5cd2c5b6 | Richard Henderson | static int walk_memory_regions_1(struct walk_memory_regions_data *data, |
2270 | b480d9b7 | Paul Brook | abi_ulong base, int level, void **lp) |
2271 | 5cd2c5b6 | Richard Henderson | { |
2272 | b480d9b7 | Paul Brook | abi_ulong pa; |
2273 | 5cd2c5b6 | Richard Henderson | int i, rc;
|
2274 | 5cd2c5b6 | Richard Henderson | |
2275 | 5cd2c5b6 | Richard Henderson | if (*lp == NULL) { |
2276 | 5cd2c5b6 | Richard Henderson | return walk_memory_regions_end(data, base, 0); |
2277 | 5cd2c5b6 | Richard Henderson | } |
2278 | 5cd2c5b6 | Richard Henderson | |
2279 | 5cd2c5b6 | Richard Henderson | if (level == 0) { |
2280 | 5cd2c5b6 | Richard Henderson | PageDesc *pd = *lp; |
2281 | 7296abac | Paul Brook | for (i = 0; i < L2_SIZE; ++i) { |
2282 | 5cd2c5b6 | Richard Henderson | int prot = pd[i].flags;
|
2283 | 5cd2c5b6 | Richard Henderson | |
2284 | 5cd2c5b6 | Richard Henderson | pa = base | (i << TARGET_PAGE_BITS); |
2285 | 5cd2c5b6 | Richard Henderson | if (prot != data->prot) {
|
2286 | 5cd2c5b6 | Richard Henderson | rc = walk_memory_regions_end(data, pa, prot); |
2287 | 5cd2c5b6 | Richard Henderson | if (rc != 0) { |
2288 | 5cd2c5b6 | Richard Henderson | return rc;
|
2289 | 9fa3e853 | bellard | } |
2290 | 9fa3e853 | bellard | } |
2291 | 5cd2c5b6 | Richard Henderson | } |
2292 | 5cd2c5b6 | Richard Henderson | } else {
|
2293 | 5cd2c5b6 | Richard Henderson | void **pp = *lp;
|
2294 | 7296abac | Paul Brook | for (i = 0; i < L2_SIZE; ++i) { |
2295 | b480d9b7 | Paul Brook | pa = base | ((abi_ulong)i << |
2296 | b480d9b7 | Paul Brook | (TARGET_PAGE_BITS + L2_BITS * level)); |
2297 | 5cd2c5b6 | Richard Henderson | rc = walk_memory_regions_1(data, pa, level - 1, pp + i);
|
2298 | 5cd2c5b6 | Richard Henderson | if (rc != 0) { |
2299 | 5cd2c5b6 | Richard Henderson | return rc;
|
2300 | 5cd2c5b6 | Richard Henderson | } |
2301 | 5cd2c5b6 | Richard Henderson | } |
2302 | 5cd2c5b6 | Richard Henderson | } |
2303 | 5cd2c5b6 | Richard Henderson | |
2304 | 5cd2c5b6 | Richard Henderson | return 0; |
2305 | 5cd2c5b6 | Richard Henderson | } |
2306 | 5cd2c5b6 | Richard Henderson | |
2307 | 5cd2c5b6 | Richard Henderson | int walk_memory_regions(void *priv, walk_memory_regions_fn fn) |
2308 | 5cd2c5b6 | Richard Henderson | { |
2309 | 5cd2c5b6 | Richard Henderson | struct walk_memory_regions_data data;
|
2310 | 5cd2c5b6 | Richard Henderson | unsigned long i; |
2311 | 5cd2c5b6 | Richard Henderson | |
2312 | 5cd2c5b6 | Richard Henderson | data.fn = fn; |
2313 | 5cd2c5b6 | Richard Henderson | data.priv = priv; |
2314 | 5cd2c5b6 | Richard Henderson | data.start = -1ul;
|
2315 | 5cd2c5b6 | Richard Henderson | data.prot = 0;
|
2316 | 5cd2c5b6 | Richard Henderson | |
2317 | 5cd2c5b6 | Richard Henderson | for (i = 0; i < V_L1_SIZE; i++) { |
2318 | b480d9b7 | Paul Brook | int rc = walk_memory_regions_1(&data, (abi_ulong)i << V_L1_SHIFT,
|
2319 | 5cd2c5b6 | Richard Henderson | V_L1_SHIFT / L2_BITS - 1, l1_map + i);
|
2320 | 5cd2c5b6 | Richard Henderson | if (rc != 0) { |
2321 | 5cd2c5b6 | Richard Henderson | return rc;
|
2322 | 9fa3e853 | bellard | } |
2323 | 33417e70 | bellard | } |
2324 | 5cd2c5b6 | Richard Henderson | |
2325 | 5cd2c5b6 | Richard Henderson | return walk_memory_regions_end(&data, 0, 0); |
2326 | edf8e2af | Mika Westerberg | } |
2327 | edf8e2af | Mika Westerberg | |
2328 | b480d9b7 | Paul Brook | static int dump_region(void *priv, abi_ulong start, |
2329 | b480d9b7 | Paul Brook | abi_ulong end, unsigned long prot) |
2330 | edf8e2af | Mika Westerberg | { |
2331 | edf8e2af | Mika Westerberg | FILE *f = (FILE *)priv; |
2332 | edf8e2af | Mika Westerberg | |
2333 | b480d9b7 | Paul Brook | (void) fprintf(f, TARGET_ABI_FMT_lx"-"TARGET_ABI_FMT_lx |
2334 | b480d9b7 | Paul Brook | " "TARGET_ABI_FMT_lx" %c%c%c\n", |
2335 | edf8e2af | Mika Westerberg | start, end, end - start, |
2336 | edf8e2af | Mika Westerberg | ((prot & PAGE_READ) ? 'r' : '-'), |
2337 | edf8e2af | Mika Westerberg | ((prot & PAGE_WRITE) ? 'w' : '-'), |
2338 | edf8e2af | Mika Westerberg | ((prot & PAGE_EXEC) ? 'x' : '-')); |
2339 | edf8e2af | Mika Westerberg | |
2340 | edf8e2af | Mika Westerberg | return (0); |
2341 | edf8e2af | Mika Westerberg | } |
2342 | edf8e2af | Mika Westerberg | |
2343 | edf8e2af | Mika Westerberg | /* dump memory mappings */
|
2344 | edf8e2af | Mika Westerberg | void page_dump(FILE *f)
|
2345 | edf8e2af | Mika Westerberg | { |
2346 | edf8e2af | Mika Westerberg | (void) fprintf(f, "%-8s %-8s %-8s %s\n", |
2347 | edf8e2af | Mika Westerberg | "start", "end", "size", "prot"); |
2348 | edf8e2af | Mika Westerberg | walk_memory_regions(f, dump_region); |
2349 | 33417e70 | bellard | } |
2350 | 33417e70 | bellard | |
2351 | 53a5960a | pbrook | int page_get_flags(target_ulong address)
|
2352 | 33417e70 | bellard | { |
2353 | 9fa3e853 | bellard | PageDesc *p; |
2354 | 9fa3e853 | bellard | |
2355 | 9fa3e853 | bellard | p = page_find(address >> TARGET_PAGE_BITS); |
2356 | 33417e70 | bellard | if (!p)
|
2357 | 9fa3e853 | bellard | return 0; |
2358 | 9fa3e853 | bellard | return p->flags;
|
2359 | 9fa3e853 | bellard | } |
2360 | 9fa3e853 | bellard | |
2361 | 376a7909 | Richard Henderson | /* Modify the flags of a page and invalidate the code if necessary.
|
2362 | 376a7909 | Richard Henderson | The flag PAGE_WRITE_ORG is positioned automatically depending
|
2363 | 376a7909 | Richard Henderson | on PAGE_WRITE. The mmap_lock should already be held. */
|
2364 | 53a5960a | pbrook | void page_set_flags(target_ulong start, target_ulong end, int flags) |
2365 | 9fa3e853 | bellard | { |
2366 | 376a7909 | Richard Henderson | target_ulong addr, len; |
2367 | 376a7909 | Richard Henderson | |
2368 | 376a7909 | Richard Henderson | /* This function should never be called with addresses outside the
|
2369 | 376a7909 | Richard Henderson | guest address space. If this assert fires, it probably indicates
|
2370 | 376a7909 | Richard Henderson | a missing call to h2g_valid. */
|
2371 | b480d9b7 | Paul Brook | #if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS
|
2372 | b480d9b7 | Paul Brook | assert(end < ((abi_ulong)1 << L1_MAP_ADDR_SPACE_BITS));
|
2373 | 376a7909 | Richard Henderson | #endif
|
2374 | 376a7909 | Richard Henderson | assert(start < end); |
2375 | 9fa3e853 | bellard | |
2376 | 9fa3e853 | bellard | start = start & TARGET_PAGE_MASK; |
2377 | 9fa3e853 | bellard | end = TARGET_PAGE_ALIGN(end); |
2378 | 376a7909 | Richard Henderson | |
2379 | 376a7909 | Richard Henderson | if (flags & PAGE_WRITE) {
|
2380 | 9fa3e853 | bellard | flags |= PAGE_WRITE_ORG; |
2381 | 376a7909 | Richard Henderson | } |
2382 | 376a7909 | Richard Henderson | |
2383 | 376a7909 | Richard Henderson | for (addr = start, len = end - start;
|
2384 | 376a7909 | Richard Henderson | len != 0;
|
2385 | 376a7909 | Richard Henderson | len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) { |
2386 | 376a7909 | Richard Henderson | PageDesc *p = page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
|
2387 | 376a7909 | Richard Henderson | |
2388 | 376a7909 | Richard Henderson | /* If the write protection bit is set, then we invalidate
|
2389 | 376a7909 | Richard Henderson | the code inside. */
|
2390 | 5fafdf24 | ths | if (!(p->flags & PAGE_WRITE) &&
|
2391 | 9fa3e853 | bellard | (flags & PAGE_WRITE) && |
2392 | 9fa3e853 | bellard | p->first_tb) { |
2393 | d720b93d | bellard | tb_invalidate_phys_page(addr, 0, NULL); |
2394 | 9fa3e853 | bellard | } |
2395 | 9fa3e853 | bellard | p->flags = flags; |
2396 | 9fa3e853 | bellard | } |
2397 | 33417e70 | bellard | } |
2398 | 33417e70 | bellard | |
2399 | 3d97b40b | ths | int page_check_range(target_ulong start, target_ulong len, int flags) |
2400 | 3d97b40b | ths | { |
2401 | 3d97b40b | ths | PageDesc *p; |
2402 | 3d97b40b | ths | target_ulong end; |
2403 | 3d97b40b | ths | target_ulong addr; |
2404 | 3d97b40b | ths | |
2405 | 376a7909 | Richard Henderson | /* This function should never be called with addresses outside the
|
2406 | 376a7909 | Richard Henderson | guest address space. If this assert fires, it probably indicates
|
2407 | 376a7909 | Richard Henderson | a missing call to h2g_valid. */
|
2408 | 338e9e6c | Blue Swirl | #if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS
|
2409 | 338e9e6c | Blue Swirl | assert(start < ((abi_ulong)1 << L1_MAP_ADDR_SPACE_BITS));
|
2410 | 376a7909 | Richard Henderson | #endif
|
2411 | 376a7909 | Richard Henderson | |
2412 | 376a7909 | Richard Henderson | if (start + len - 1 < start) { |
2413 | 376a7909 | Richard Henderson | /* We've wrapped around. */
|
2414 | 55f280c9 | balrog | return -1; |
2415 | 376a7909 | Richard Henderson | } |
2416 | 55f280c9 | balrog | |
2417 | 3d97b40b | ths | end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */
|
2418 | 3d97b40b | ths | start = start & TARGET_PAGE_MASK; |
2419 | 3d97b40b | ths | |
2420 | 376a7909 | Richard Henderson | for (addr = start, len = end - start;
|
2421 | 376a7909 | Richard Henderson | len != 0;
|
2422 | 376a7909 | Richard Henderson | len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) { |
2423 | 3d97b40b | ths | p = page_find(addr >> TARGET_PAGE_BITS); |
2424 | 3d97b40b | ths | if( !p )
|
2425 | 3d97b40b | ths | return -1; |
2426 | 3d97b40b | ths | if( !(p->flags & PAGE_VALID) )
|
2427 | 3d97b40b | ths | return -1; |
2428 | 3d97b40b | ths | |
2429 | dae3270c | bellard | if ((flags & PAGE_READ) && !(p->flags & PAGE_READ))
|
2430 | 3d97b40b | ths | return -1; |
2431 | dae3270c | bellard | if (flags & PAGE_WRITE) {
|
2432 | dae3270c | bellard | if (!(p->flags & PAGE_WRITE_ORG))
|
2433 | dae3270c | bellard | return -1; |
2434 | dae3270c | bellard | /* unprotect the page if it was put read-only because it
|
2435 | dae3270c | bellard | contains translated code */
|
2436 | dae3270c | bellard | if (!(p->flags & PAGE_WRITE)) {
|
2437 | dae3270c | bellard | if (!page_unprotect(addr, 0, NULL)) |
2438 | dae3270c | bellard | return -1; |
2439 | dae3270c | bellard | } |
2440 | dae3270c | bellard | return 0; |
2441 | dae3270c | bellard | } |
2442 | 3d97b40b | ths | } |
2443 | 3d97b40b | ths | return 0; |
2444 | 3d97b40b | ths | } |
2445 | 3d97b40b | ths | |
2446 | 9fa3e853 | bellard | /* called from signal handler: invalidate the code and unprotect the
|
2447 | ccbb4d44 | Stuart Brady | page. Return TRUE if the fault was successfully handled. */
|
2448 | 53a5960a | pbrook | int page_unprotect(target_ulong address, unsigned long pc, void *puc) |
2449 | 9fa3e853 | bellard | { |
2450 | 9fa3e853 | bellard | unsigned int page_index, prot, pindex; |
2451 | 9fa3e853 | bellard | PageDesc *p, *p1; |
2452 | 53a5960a | pbrook | target_ulong host_start, host_end, addr; |
2453 | 9fa3e853 | bellard | |
2454 | c8a706fe | pbrook | /* Technically this isn't safe inside a signal handler. However we
|
2455 | c8a706fe | pbrook | know this only ever happens in a synchronous SEGV handler, so in
|
2456 | c8a706fe | pbrook | practice it seems to be ok. */
|
2457 | c8a706fe | pbrook | mmap_lock(); |
2458 | c8a706fe | pbrook | |
2459 | 83fb7adf | bellard | host_start = address & qemu_host_page_mask; |
2460 | 9fa3e853 | bellard | page_index = host_start >> TARGET_PAGE_BITS; |
2461 | 9fa3e853 | bellard | p1 = page_find(page_index); |
2462 | c8a706fe | pbrook | if (!p1) {
|
2463 | c8a706fe | pbrook | mmap_unlock(); |
2464 | 9fa3e853 | bellard | return 0; |
2465 | c8a706fe | pbrook | } |
2466 | 83fb7adf | bellard | host_end = host_start + qemu_host_page_size; |
2467 | 9fa3e853 | bellard | p = p1; |
2468 | 9fa3e853 | bellard | prot = 0;
|
2469 | 9fa3e853 | bellard | for(addr = host_start;addr < host_end; addr += TARGET_PAGE_SIZE) {
|
2470 | 9fa3e853 | bellard | prot |= p->flags; |
2471 | 9fa3e853 | bellard | p++; |
2472 | 9fa3e853 | bellard | } |
2473 | 9fa3e853 | bellard | /* if the page was really writable, then we change its
|
2474 | 9fa3e853 | bellard | protection back to writable */
|
2475 | 9fa3e853 | bellard | if (prot & PAGE_WRITE_ORG) {
|
2476 | 9fa3e853 | bellard | pindex = (address - host_start) >> TARGET_PAGE_BITS; |
2477 | 9fa3e853 | bellard | if (!(p1[pindex].flags & PAGE_WRITE)) {
|
2478 | 5fafdf24 | ths | mprotect((void *)g2h(host_start), qemu_host_page_size,
|
2479 | 9fa3e853 | bellard | (prot & PAGE_BITS) | PAGE_WRITE); |
2480 | 9fa3e853 | bellard | p1[pindex].flags |= PAGE_WRITE; |
2481 | 9fa3e853 | bellard | /* and since the content will be modified, we must invalidate
|
2482 | 9fa3e853 | bellard | the corresponding translated code. */
|
2483 | d720b93d | bellard | tb_invalidate_phys_page(address, pc, puc); |
2484 | 9fa3e853 | bellard | #ifdef DEBUG_TB_CHECK
|
2485 | 9fa3e853 | bellard | tb_invalidate_check(address); |
2486 | 9fa3e853 | bellard | #endif
|
2487 | c8a706fe | pbrook | mmap_unlock(); |
2488 | 9fa3e853 | bellard | return 1; |
2489 | 9fa3e853 | bellard | } |
2490 | 9fa3e853 | bellard | } |
2491 | c8a706fe | pbrook | mmap_unlock(); |
2492 | 9fa3e853 | bellard | return 0; |
2493 | 9fa3e853 | bellard | } |
2494 | 9fa3e853 | bellard | |
2495 | 6a00d601 | bellard | static inline void tlb_set_dirty(CPUState *env, |
2496 | 6a00d601 | bellard | unsigned long addr, target_ulong vaddr) |
2497 | 1ccde1cb | bellard | { |
2498 | 1ccde1cb | bellard | } |
2499 | 9fa3e853 | bellard | #endif /* defined(CONFIG_USER_ONLY) */ |
2500 | 9fa3e853 | bellard | |
2501 | e2eef170 | pbrook | #if !defined(CONFIG_USER_ONLY)
|
2502 | 8da3ff18 | pbrook | |
2503 | c04b2b78 | Paul Brook | #define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
|
2504 | c04b2b78 | Paul Brook | typedef struct subpage_t { |
2505 | c04b2b78 | Paul Brook | target_phys_addr_t base; |
2506 | c04b2b78 | Paul Brook | CPUReadMemoryFunc * const *mem_read[TARGET_PAGE_SIZE][4]; |
2507 | c04b2b78 | Paul Brook | CPUWriteMemoryFunc * const *mem_write[TARGET_PAGE_SIZE][4]; |
2508 | c04b2b78 | Paul Brook | void *opaque[TARGET_PAGE_SIZE][2][4]; |
2509 | c04b2b78 | Paul Brook | ram_addr_t region_offset[TARGET_PAGE_SIZE][2][4]; |
2510 | c04b2b78 | Paul Brook | } subpage_t; |
2511 | c04b2b78 | Paul Brook | |
2512 | c227f099 | Anthony Liguori | static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end, |
2513 | c227f099 | Anthony Liguori | ram_addr_t memory, ram_addr_t region_offset); |
2514 | c227f099 | Anthony Liguori | static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys, |
2515 | c227f099 | Anthony Liguori | ram_addr_t orig_memory, ram_addr_t region_offset); |
2516 | db7b5426 | blueswir1 | #define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \
|
2517 | db7b5426 | blueswir1 | need_subpage) \ |
2518 | db7b5426 | blueswir1 | do { \
|
2519 | db7b5426 | blueswir1 | if (addr > start_addr) \
|
2520 | db7b5426 | blueswir1 | start_addr2 = 0; \
|
2521 | db7b5426 | blueswir1 | else { \
|
2522 | db7b5426 | blueswir1 | start_addr2 = start_addr & ~TARGET_PAGE_MASK; \ |
2523 | db7b5426 | blueswir1 | if (start_addr2 > 0) \ |
2524 | db7b5426 | blueswir1 | need_subpage = 1; \
|
2525 | db7b5426 | blueswir1 | } \ |
2526 | db7b5426 | blueswir1 | \ |
2527 | 49e9fba2 | blueswir1 | if ((start_addr + orig_size) - addr >= TARGET_PAGE_SIZE) \
|
2528 | db7b5426 | blueswir1 | end_addr2 = TARGET_PAGE_SIZE - 1; \
|
2529 | db7b5426 | blueswir1 | else { \
|
2530 | db7b5426 | blueswir1 | end_addr2 = (start_addr + orig_size - 1) & ~TARGET_PAGE_MASK; \
|
2531 | db7b5426 | blueswir1 | if (end_addr2 < TARGET_PAGE_SIZE - 1) \ |
2532 | db7b5426 | blueswir1 | need_subpage = 1; \
|
2533 | db7b5426 | blueswir1 | } \ |
2534 | db7b5426 | blueswir1 | } while (0) |
2535 | db7b5426 | blueswir1 | |
2536 | 8f2498f9 | Michael S. Tsirkin | /* register physical memory.
|
2537 | 8f2498f9 | Michael S. Tsirkin | For RAM, 'size' must be a multiple of the target page size.
|
2538 | 8f2498f9 | Michael S. Tsirkin | If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
|
2539 | 8da3ff18 | pbrook | io memory page. The address used when calling the IO function is
|
2540 | 8da3ff18 | pbrook | the offset from the start of the region, plus region_offset. Both
|
2541 | ccbb4d44 | Stuart Brady | start_addr and region_offset are rounded down to a page boundary
|
2542 | 8da3ff18 | pbrook | before calculating this offset. This should not be a problem unless
|
2543 | 8da3ff18 | pbrook | the low bits of start_addr and region_offset differ. */
|
2544 | c227f099 | Anthony Liguori | void cpu_register_physical_memory_offset(target_phys_addr_t start_addr,
|
2545 | c227f099 | Anthony Liguori | ram_addr_t size, |
2546 | c227f099 | Anthony Liguori | ram_addr_t phys_offset, |
2547 | c227f099 | Anthony Liguori | ram_addr_t region_offset) |
2548 | 33417e70 | bellard | { |
2549 | c227f099 | Anthony Liguori | target_phys_addr_t addr, end_addr; |
2550 | 92e873b9 | bellard | PhysPageDesc *p; |
2551 | 9d42037b | bellard | CPUState *env; |
2552 | c227f099 | Anthony Liguori | ram_addr_t orig_size = size; |
2553 | db7b5426 | blueswir1 | void *subpage;
|
2554 | 33417e70 | bellard | |
2555 | f6f3fbca | Michael S. Tsirkin | cpu_notify_set_memory(start_addr, size, phys_offset); |
2556 | f6f3fbca | Michael S. Tsirkin | |
2557 | 67c4d23c | pbrook | if (phys_offset == IO_MEM_UNASSIGNED) {
|
2558 | 67c4d23c | pbrook | region_offset = start_addr; |
2559 | 67c4d23c | pbrook | } |
2560 | 8da3ff18 | pbrook | region_offset &= TARGET_PAGE_MASK; |
2561 | 5fd386f6 | bellard | size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
|
2562 | c227f099 | Anthony Liguori | end_addr = start_addr + (target_phys_addr_t)size; |
2563 | 49e9fba2 | blueswir1 | for(addr = start_addr; addr != end_addr; addr += TARGET_PAGE_SIZE) {
|
2564 | db7b5426 | blueswir1 | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
2565 | db7b5426 | blueswir1 | if (p && p->phys_offset != IO_MEM_UNASSIGNED) {
|
2566 | c227f099 | Anthony Liguori | ram_addr_t orig_memory = p->phys_offset; |
2567 | c227f099 | Anthony Liguori | target_phys_addr_t start_addr2, end_addr2; |
2568 | db7b5426 | blueswir1 | int need_subpage = 0; |
2569 | db7b5426 | blueswir1 | |
2570 | db7b5426 | blueswir1 | CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, |
2571 | db7b5426 | blueswir1 | need_subpage); |
2572 | 4254fab8 | blueswir1 | if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
|
2573 | db7b5426 | blueswir1 | if (!(orig_memory & IO_MEM_SUBPAGE)) {
|
2574 | db7b5426 | blueswir1 | subpage = subpage_init((addr & TARGET_PAGE_MASK), |
2575 | 8da3ff18 | pbrook | &p->phys_offset, orig_memory, |
2576 | 8da3ff18 | pbrook | p->region_offset); |
2577 | db7b5426 | blueswir1 | } else {
|
2578 | db7b5426 | blueswir1 | subpage = io_mem_opaque[(orig_memory & ~TARGET_PAGE_MASK) |
2579 | db7b5426 | blueswir1 | >> IO_MEM_SHIFT]; |
2580 | db7b5426 | blueswir1 | } |
2581 | 8da3ff18 | pbrook | subpage_register(subpage, start_addr2, end_addr2, phys_offset, |
2582 | 8da3ff18 | pbrook | region_offset); |
2583 | 8da3ff18 | pbrook | p->region_offset = 0;
|
2584 | db7b5426 | blueswir1 | } else {
|
2585 | db7b5426 | blueswir1 | p->phys_offset = phys_offset; |
2586 | db7b5426 | blueswir1 | if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
|
2587 | db7b5426 | blueswir1 | (phys_offset & IO_MEM_ROMD)) |
2588 | db7b5426 | blueswir1 | phys_offset += TARGET_PAGE_SIZE; |
2589 | db7b5426 | blueswir1 | } |
2590 | db7b5426 | blueswir1 | } else {
|
2591 | db7b5426 | blueswir1 | p = phys_page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
|
2592 | db7b5426 | blueswir1 | p->phys_offset = phys_offset; |
2593 | 8da3ff18 | pbrook | p->region_offset = region_offset; |
2594 | db7b5426 | blueswir1 | if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
|
2595 | 8da3ff18 | pbrook | (phys_offset & IO_MEM_ROMD)) { |
2596 | db7b5426 | blueswir1 | phys_offset += TARGET_PAGE_SIZE; |
2597 | 0e8f0967 | pbrook | } else {
|
2598 | c227f099 | Anthony Liguori | target_phys_addr_t start_addr2, end_addr2; |
2599 | db7b5426 | blueswir1 | int need_subpage = 0; |
2600 | db7b5426 | blueswir1 | |
2601 | db7b5426 | blueswir1 | CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, |
2602 | db7b5426 | blueswir1 | end_addr2, need_subpage); |
2603 | db7b5426 | blueswir1 | |
2604 | 4254fab8 | blueswir1 | if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
|
2605 | db7b5426 | blueswir1 | subpage = subpage_init((addr & TARGET_PAGE_MASK), |
2606 | 8da3ff18 | pbrook | &p->phys_offset, IO_MEM_UNASSIGNED, |
2607 | 67c4d23c | pbrook | addr & TARGET_PAGE_MASK); |
2608 | db7b5426 | blueswir1 | subpage_register(subpage, start_addr2, end_addr2, |
2609 | 8da3ff18 | pbrook | phys_offset, region_offset); |
2610 | 8da3ff18 | pbrook | p->region_offset = 0;
|
2611 | db7b5426 | blueswir1 | } |
2612 | db7b5426 | blueswir1 | } |
2613 | db7b5426 | blueswir1 | } |
2614 | 8da3ff18 | pbrook | region_offset += TARGET_PAGE_SIZE; |
2615 | 33417e70 | bellard | } |
2616 | 3b46e624 | ths | |
2617 | 9d42037b | bellard | /* since each CPU stores ram addresses in its TLB cache, we must
|
2618 | 9d42037b | bellard | reset the modified entries */
|
2619 | 9d42037b | bellard | /* XXX: slow ! */
|
2620 | 9d42037b | bellard | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
2621 | 9d42037b | bellard | tlb_flush(env, 1);
|
2622 | 9d42037b | bellard | } |
2623 | 33417e70 | bellard | } |
2624 | 33417e70 | bellard | |
2625 | ba863458 | bellard | /* XXX: temporary until new memory mapping API */
|
2626 | c227f099 | Anthony Liguori | ram_addr_t cpu_get_physical_page_desc(target_phys_addr_t addr) |
2627 | ba863458 | bellard | { |
2628 | ba863458 | bellard | PhysPageDesc *p; |
2629 | ba863458 | bellard | |
2630 | ba863458 | bellard | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
2631 | ba863458 | bellard | if (!p)
|
2632 | ba863458 | bellard | return IO_MEM_UNASSIGNED;
|
2633 | ba863458 | bellard | return p->phys_offset;
|
2634 | ba863458 | bellard | } |
2635 | ba863458 | bellard | |
2636 | c227f099 | Anthony Liguori | void qemu_register_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size)
|
2637 | f65ed4c1 | aliguori | { |
2638 | f65ed4c1 | aliguori | if (kvm_enabled())
|
2639 | f65ed4c1 | aliguori | kvm_coalesce_mmio_region(addr, size); |
2640 | f65ed4c1 | aliguori | } |
2641 | f65ed4c1 | aliguori | |
2642 | c227f099 | Anthony Liguori | void qemu_unregister_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size)
|
2643 | f65ed4c1 | aliguori | { |
2644 | f65ed4c1 | aliguori | if (kvm_enabled())
|
2645 | f65ed4c1 | aliguori | kvm_uncoalesce_mmio_region(addr, size); |
2646 | f65ed4c1 | aliguori | } |
2647 | f65ed4c1 | aliguori | |
2648 | 62a2744c | Sheng Yang | void qemu_flush_coalesced_mmio_buffer(void) |
2649 | 62a2744c | Sheng Yang | { |
2650 | 62a2744c | Sheng Yang | if (kvm_enabled())
|
2651 | 62a2744c | Sheng Yang | kvm_flush_coalesced_mmio_buffer(); |
2652 | 62a2744c | Sheng Yang | } |
2653 | 62a2744c | Sheng Yang | |
2654 | c902760f | Marcelo Tosatti | #if defined(__linux__) && !defined(TARGET_S390X)
|
2655 | c902760f | Marcelo Tosatti | |
2656 | c902760f | Marcelo Tosatti | #include <sys/vfs.h> |
2657 | c902760f | Marcelo Tosatti | |
2658 | c902760f | Marcelo Tosatti | #define HUGETLBFS_MAGIC 0x958458f6 |
2659 | c902760f | Marcelo Tosatti | |
2660 | c902760f | Marcelo Tosatti | static long gethugepagesize(const char *path) |
2661 | c902760f | Marcelo Tosatti | { |
2662 | c902760f | Marcelo Tosatti | struct statfs fs;
|
2663 | c902760f | Marcelo Tosatti | int ret;
|
2664 | c902760f | Marcelo Tosatti | |
2665 | c902760f | Marcelo Tosatti | do {
|
2666 | c902760f | Marcelo Tosatti | ret = statfs(path, &fs); |
2667 | c902760f | Marcelo Tosatti | } while (ret != 0 && errno == EINTR); |
2668 | c902760f | Marcelo Tosatti | |
2669 | c902760f | Marcelo Tosatti | if (ret != 0) { |
2670 | 6adc0549 | Michael Tokarev | perror(path); |
2671 | c902760f | Marcelo Tosatti | return 0; |
2672 | c902760f | Marcelo Tosatti | } |
2673 | c902760f | Marcelo Tosatti | |
2674 | c902760f | Marcelo Tosatti | if (fs.f_type != HUGETLBFS_MAGIC)
|
2675 | c902760f | Marcelo Tosatti | fprintf(stderr, "Warning: path not on HugeTLBFS: %s\n", path);
|
2676 | c902760f | Marcelo Tosatti | |
2677 | c902760f | Marcelo Tosatti | return fs.f_bsize;
|
2678 | c902760f | Marcelo Tosatti | } |
2679 | c902760f | Marcelo Tosatti | |
2680 | c902760f | Marcelo Tosatti | static void *file_ram_alloc(ram_addr_t memory, const char *path) |
2681 | c902760f | Marcelo Tosatti | { |
2682 | c902760f | Marcelo Tosatti | char *filename;
|
2683 | c902760f | Marcelo Tosatti | void *area;
|
2684 | c902760f | Marcelo Tosatti | int fd;
|
2685 | c902760f | Marcelo Tosatti | #ifdef MAP_POPULATE
|
2686 | c902760f | Marcelo Tosatti | int flags;
|
2687 | c902760f | Marcelo Tosatti | #endif
|
2688 | c902760f | Marcelo Tosatti | unsigned long hpagesize; |
2689 | c902760f | Marcelo Tosatti | |
2690 | c902760f | Marcelo Tosatti | hpagesize = gethugepagesize(path); |
2691 | c902760f | Marcelo Tosatti | if (!hpagesize) {
|
2692 | c902760f | Marcelo Tosatti | return NULL; |
2693 | c902760f | Marcelo Tosatti | } |
2694 | c902760f | Marcelo Tosatti | |
2695 | c902760f | Marcelo Tosatti | if (memory < hpagesize) {
|
2696 | c902760f | Marcelo Tosatti | return NULL; |
2697 | c902760f | Marcelo Tosatti | } |
2698 | c902760f | Marcelo Tosatti | |
2699 | c902760f | Marcelo Tosatti | if (kvm_enabled() && !kvm_has_sync_mmu()) {
|
2700 | c902760f | Marcelo Tosatti | fprintf(stderr, "host lacks kvm mmu notifiers, -mem-path unsupported\n");
|
2701 | c902760f | Marcelo Tosatti | return NULL; |
2702 | c902760f | Marcelo Tosatti | } |
2703 | c902760f | Marcelo Tosatti | |
2704 | c902760f | Marcelo Tosatti | if (asprintf(&filename, "%s/qemu_back_mem.XXXXXX", path) == -1) { |
2705 | c902760f | Marcelo Tosatti | return NULL; |
2706 | c902760f | Marcelo Tosatti | } |
2707 | c902760f | Marcelo Tosatti | |
2708 | c902760f | Marcelo Tosatti | fd = mkstemp(filename); |
2709 | c902760f | Marcelo Tosatti | if (fd < 0) { |
2710 | 6adc0549 | Michael Tokarev | perror("unable to create backing store for hugepages");
|
2711 | c902760f | Marcelo Tosatti | free(filename); |
2712 | c902760f | Marcelo Tosatti | return NULL; |
2713 | c902760f | Marcelo Tosatti | } |
2714 | c902760f | Marcelo Tosatti | unlink(filename); |
2715 | c902760f | Marcelo Tosatti | free(filename); |
2716 | c902760f | Marcelo Tosatti | |
2717 | c902760f | Marcelo Tosatti | memory = (memory+hpagesize-1) & ~(hpagesize-1); |
2718 | c902760f | Marcelo Tosatti | |
2719 | c902760f | Marcelo Tosatti | /*
|
2720 | c902760f | Marcelo Tosatti | * ftruncate is not supported by hugetlbfs in older
|
2721 | c902760f | Marcelo Tosatti | * hosts, so don't bother bailing out on errors.
|
2722 | c902760f | Marcelo Tosatti | * If anything goes wrong with it under other filesystems,
|
2723 | c902760f | Marcelo Tosatti | * mmap will fail.
|
2724 | c902760f | Marcelo Tosatti | */
|
2725 | c902760f | Marcelo Tosatti | if (ftruncate(fd, memory))
|
2726 | c902760f | Marcelo Tosatti | perror("ftruncate");
|
2727 | c902760f | Marcelo Tosatti | |
2728 | c902760f | Marcelo Tosatti | #ifdef MAP_POPULATE
|
2729 | c902760f | Marcelo Tosatti | /* NB: MAP_POPULATE won't exhaustively alloc all phys pages in the case
|
2730 | c902760f | Marcelo Tosatti | * MAP_PRIVATE is requested. For mem_prealloc we mmap as MAP_SHARED
|
2731 | c902760f | Marcelo Tosatti | * to sidestep this quirk.
|
2732 | c902760f | Marcelo Tosatti | */
|
2733 | c902760f | Marcelo Tosatti | flags = mem_prealloc ? MAP_POPULATE | MAP_SHARED : MAP_PRIVATE; |
2734 | c902760f | Marcelo Tosatti | area = mmap(0, memory, PROT_READ | PROT_WRITE, flags, fd, 0); |
2735 | c902760f | Marcelo Tosatti | #else
|
2736 | c902760f | Marcelo Tosatti | area = mmap(0, memory, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
2737 | c902760f | Marcelo Tosatti | #endif
|
2738 | c902760f | Marcelo Tosatti | if (area == MAP_FAILED) {
|
2739 | c902760f | Marcelo Tosatti | perror("file_ram_alloc: can't mmap RAM pages");
|
2740 | c902760f | Marcelo Tosatti | close(fd); |
2741 | c902760f | Marcelo Tosatti | return (NULL); |
2742 | c902760f | Marcelo Tosatti | } |
2743 | c902760f | Marcelo Tosatti | return area;
|
2744 | c902760f | Marcelo Tosatti | } |
2745 | c902760f | Marcelo Tosatti | #endif
|
2746 | c902760f | Marcelo Tosatti | |
2747 | c227f099 | Anthony Liguori | ram_addr_t qemu_ram_alloc(ram_addr_t size) |
2748 | 94a6b54f | pbrook | { |
2749 | 94a6b54f | pbrook | RAMBlock *new_block; |
2750 | 94a6b54f | pbrook | |
2751 | 94a6b54f | pbrook | size = TARGET_PAGE_ALIGN(size); |
2752 | 94a6b54f | pbrook | new_block = qemu_malloc(sizeof(*new_block));
|
2753 | 94a6b54f | pbrook | |
2754 | c902760f | Marcelo Tosatti | if (mem_path) {
|
2755 | c902760f | Marcelo Tosatti | #if defined (__linux__) && !defined(TARGET_S390X)
|
2756 | c902760f | Marcelo Tosatti | new_block->host = file_ram_alloc(size, mem_path); |
2757 | c902760f | Marcelo Tosatti | if (!new_block->host)
|
2758 | c902760f | Marcelo Tosatti | exit(1);
|
2759 | c902760f | Marcelo Tosatti | #else
|
2760 | c902760f | Marcelo Tosatti | fprintf(stderr, "-mem-path option unsupported\n");
|
2761 | c902760f | Marcelo Tosatti | exit(1);
|
2762 | c902760f | Marcelo Tosatti | #endif
|
2763 | c902760f | Marcelo Tosatti | } else {
|
2764 | 6b02494d | Alexander Graf | #if defined(TARGET_S390X) && defined(CONFIG_KVM)
|
2765 | c902760f | Marcelo Tosatti | /* XXX S390 KVM requires the topmost vma of the RAM to be < 256GB */
|
2766 | c902760f | Marcelo Tosatti | new_block->host = mmap((void*)0x1000000, size, |
2767 | c902760f | Marcelo Tosatti | PROT_EXEC|PROT_READ|PROT_WRITE, |
2768 | c902760f | Marcelo Tosatti | MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
2769 | 6b02494d | Alexander Graf | #else
|
2770 | c902760f | Marcelo Tosatti | new_block->host = qemu_vmalloc(size); |
2771 | 6b02494d | Alexander Graf | #endif
|
2772 | ccb167e9 | Izik Eidus | #ifdef MADV_MERGEABLE
|
2773 | c902760f | Marcelo Tosatti | madvise(new_block->host, size, MADV_MERGEABLE); |
2774 | ccb167e9 | Izik Eidus | #endif
|
2775 | c902760f | Marcelo Tosatti | } |
2776 | 94a6b54f | pbrook | new_block->offset = last_ram_offset; |
2777 | 94a6b54f | pbrook | new_block->length = size; |
2778 | 94a6b54f | pbrook | |
2779 | 94a6b54f | pbrook | new_block->next = ram_blocks; |
2780 | 94a6b54f | pbrook | ram_blocks = new_block; |
2781 | 94a6b54f | pbrook | |
2782 | 94a6b54f | pbrook | phys_ram_dirty = qemu_realloc(phys_ram_dirty, |
2783 | 94a6b54f | pbrook | (last_ram_offset + size) >> TARGET_PAGE_BITS); |
2784 | 94a6b54f | pbrook | memset(phys_ram_dirty + (last_ram_offset >> TARGET_PAGE_BITS), |
2785 | 94a6b54f | pbrook | 0xff, size >> TARGET_PAGE_BITS);
|
2786 | 94a6b54f | pbrook | |
2787 | 94a6b54f | pbrook | last_ram_offset += size; |
2788 | 94a6b54f | pbrook | |
2789 | 6f0437e8 | Jan Kiszka | if (kvm_enabled())
|
2790 | 6f0437e8 | Jan Kiszka | kvm_setup_guest_memory(new_block->host, size); |
2791 | 6f0437e8 | Jan Kiszka | |
2792 | 94a6b54f | pbrook | return new_block->offset;
|
2793 | 94a6b54f | pbrook | } |
2794 | e9a1ab19 | bellard | |
2795 | c227f099 | Anthony Liguori | void qemu_ram_free(ram_addr_t addr)
|
2796 | e9a1ab19 | bellard | { |
2797 | 94a6b54f | pbrook | /* TODO: implement this. */
|
2798 | e9a1ab19 | bellard | } |
2799 | e9a1ab19 | bellard | |
2800 | dc828ca1 | pbrook | /* Return a host pointer to ram allocated with qemu_ram_alloc.
|
2801 | 5579c7f3 | pbrook | With the exception of the softmmu code in this file, this should
|
2802 | 5579c7f3 | pbrook | only be used for local memory (e.g. video ram) that the device owns,
|
2803 | 5579c7f3 | pbrook | and knows it isn't going to access beyond the end of the block.
|
2804 | 5579c7f3 | pbrook | |
2805 | 5579c7f3 | pbrook | It should not be used for general purpose DMA.
|
2806 | 5579c7f3 | pbrook | Use cpu_physical_memory_map/cpu_physical_memory_rw instead.
|
2807 | 5579c7f3 | pbrook | */
|
2808 | c227f099 | Anthony Liguori | void *qemu_get_ram_ptr(ram_addr_t addr)
|
2809 | dc828ca1 | pbrook | { |
2810 | 94a6b54f | pbrook | RAMBlock *prev; |
2811 | 94a6b54f | pbrook | RAMBlock **prevp; |
2812 | 94a6b54f | pbrook | RAMBlock *block; |
2813 | 94a6b54f | pbrook | |
2814 | 94a6b54f | pbrook | prev = NULL;
|
2815 | 94a6b54f | pbrook | prevp = &ram_blocks; |
2816 | 94a6b54f | pbrook | block = ram_blocks; |
2817 | 94a6b54f | pbrook | while (block && (block->offset > addr
|
2818 | 94a6b54f | pbrook | || block->offset + block->length <= addr)) { |
2819 | 94a6b54f | pbrook | if (prev)
|
2820 | 94a6b54f | pbrook | prevp = &prev->next; |
2821 | 94a6b54f | pbrook | prev = block; |
2822 | 94a6b54f | pbrook | block = block->next; |
2823 | 94a6b54f | pbrook | } |
2824 | 94a6b54f | pbrook | if (!block) {
|
2825 | 94a6b54f | pbrook | fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr); |
2826 | 94a6b54f | pbrook | abort(); |
2827 | 94a6b54f | pbrook | } |
2828 | 94a6b54f | pbrook | /* Move this entry to to start of the list. */
|
2829 | 94a6b54f | pbrook | if (prev) {
|
2830 | 94a6b54f | pbrook | prev->next = block->next; |
2831 | 94a6b54f | pbrook | block->next = *prevp; |
2832 | 94a6b54f | pbrook | *prevp = block; |
2833 | 94a6b54f | pbrook | } |
2834 | 94a6b54f | pbrook | return block->host + (addr - block->offset);
|
2835 | dc828ca1 | pbrook | } |
2836 | dc828ca1 | pbrook | |
2837 | 5579c7f3 | pbrook | /* Some of the softmmu routines need to translate from a host pointer
|
2838 | 5579c7f3 | pbrook | (typically a TLB entry) back to a ram offset. */
|
2839 | c227f099 | Anthony Liguori | ram_addr_t qemu_ram_addr_from_host(void *ptr)
|
2840 | 5579c7f3 | pbrook | { |
2841 | 94a6b54f | pbrook | RAMBlock *prev; |
2842 | 94a6b54f | pbrook | RAMBlock *block; |
2843 | 94a6b54f | pbrook | uint8_t *host = ptr; |
2844 | 94a6b54f | pbrook | |
2845 | 94a6b54f | pbrook | prev = NULL;
|
2846 | 94a6b54f | pbrook | block = ram_blocks; |
2847 | 94a6b54f | pbrook | while (block && (block->host > host
|
2848 | 94a6b54f | pbrook | || block->host + block->length <= host)) { |
2849 | 94a6b54f | pbrook | prev = block; |
2850 | 94a6b54f | pbrook | block = block->next; |
2851 | 94a6b54f | pbrook | } |
2852 | 94a6b54f | pbrook | if (!block) {
|
2853 | 94a6b54f | pbrook | fprintf(stderr, "Bad ram pointer %p\n", ptr);
|
2854 | 94a6b54f | pbrook | abort(); |
2855 | 94a6b54f | pbrook | } |
2856 | 94a6b54f | pbrook | return block->offset + (host - block->host);
|
2857 | 5579c7f3 | pbrook | } |
2858 | 5579c7f3 | pbrook | |
2859 | c227f099 | Anthony Liguori | static uint32_t unassigned_mem_readb(void *opaque, target_phys_addr_t addr) |
2860 | 33417e70 | bellard | { |
2861 | 67d3b957 | pbrook | #ifdef DEBUG_UNASSIGNED
|
2862 | ab3d1727 | blueswir1 | printf("Unassigned mem read " TARGET_FMT_plx "\n", addr); |
2863 | 67d3b957 | pbrook | #endif
|
2864 | faed1c2a | Edgar E. Iglesias | #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
|
2865 | e18231a3 | blueswir1 | do_unassigned_access(addr, 0, 0, 0, 1); |
2866 | e18231a3 | blueswir1 | #endif
|
2867 | e18231a3 | blueswir1 | return 0; |
2868 | e18231a3 | blueswir1 | } |
2869 | e18231a3 | blueswir1 | |
2870 | c227f099 | Anthony Liguori | static uint32_t unassigned_mem_readw(void *opaque, target_phys_addr_t addr) |
2871 | e18231a3 | blueswir1 | { |
2872 | e18231a3 | blueswir1 | #ifdef DEBUG_UNASSIGNED
|
2873 | e18231a3 | blueswir1 | printf("Unassigned mem read " TARGET_FMT_plx "\n", addr); |
2874 | e18231a3 | blueswir1 | #endif
|
2875 | faed1c2a | Edgar E. Iglesias | #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
|
2876 | e18231a3 | blueswir1 | do_unassigned_access(addr, 0, 0, 0, 2); |
2877 | e18231a3 | blueswir1 | #endif
|
2878 | e18231a3 | blueswir1 | return 0; |
2879 | e18231a3 | blueswir1 | } |
2880 | e18231a3 | blueswir1 | |
2881 | c227f099 | Anthony Liguori | static uint32_t unassigned_mem_readl(void *opaque, target_phys_addr_t addr) |
2882 | e18231a3 | blueswir1 | { |
2883 | e18231a3 | blueswir1 | #ifdef DEBUG_UNASSIGNED
|
2884 | e18231a3 | blueswir1 | printf("Unassigned mem read " TARGET_FMT_plx "\n", addr); |
2885 | e18231a3 | blueswir1 | #endif
|
2886 | faed1c2a | Edgar E. Iglesias | #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
|
2887 | e18231a3 | blueswir1 | do_unassigned_access(addr, 0, 0, 0, 4); |
2888 | b4f0a316 | blueswir1 | #endif
|
2889 | 33417e70 | bellard | return 0; |
2890 | 33417e70 | bellard | } |
2891 | 33417e70 | bellard | |
2892 | c227f099 | Anthony Liguori | static void unassigned_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val) |
2893 | 33417e70 | bellard | { |
2894 | 67d3b957 | pbrook | #ifdef DEBUG_UNASSIGNED
|
2895 | ab3d1727 | blueswir1 | printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val); |
2896 | 67d3b957 | pbrook | #endif
|
2897 | faed1c2a | Edgar E. Iglesias | #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
|
2898 | e18231a3 | blueswir1 | do_unassigned_access(addr, 1, 0, 0, 1); |
2899 | e18231a3 | blueswir1 | #endif
|
2900 | e18231a3 | blueswir1 | } |
2901 | e18231a3 | blueswir1 | |
2902 | c227f099 | Anthony Liguori | static void unassigned_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val) |
2903 | e18231a3 | blueswir1 | { |
2904 | e18231a3 | blueswir1 | #ifdef DEBUG_UNASSIGNED
|
2905 | e18231a3 | blueswir1 | printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val); |
2906 | e18231a3 | blueswir1 | #endif
|
2907 | faed1c2a | Edgar E. Iglesias | #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
|
2908 | e18231a3 | blueswir1 | do_unassigned_access(addr, 1, 0, 0, 2); |
2909 | e18231a3 | blueswir1 | #endif
|
2910 | e18231a3 | blueswir1 | } |
2911 | e18231a3 | blueswir1 | |
2912 | c227f099 | Anthony Liguori | static void unassigned_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val) |
2913 | e18231a3 | blueswir1 | { |
2914 | e18231a3 | blueswir1 | #ifdef DEBUG_UNASSIGNED
|
2915 | e18231a3 | blueswir1 | printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val); |
2916 | e18231a3 | blueswir1 | #endif
|
2917 | faed1c2a | Edgar E. Iglesias | #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
|
2918 | e18231a3 | blueswir1 | do_unassigned_access(addr, 1, 0, 0, 4); |
2919 | b4f0a316 | blueswir1 | #endif
|
2920 | 33417e70 | bellard | } |
2921 | 33417e70 | bellard | |
2922 | d60efc6b | Blue Swirl | static CPUReadMemoryFunc * const unassigned_mem_read[3] = { |
2923 | 33417e70 | bellard | unassigned_mem_readb, |
2924 | e18231a3 | blueswir1 | unassigned_mem_readw, |
2925 | e18231a3 | blueswir1 | unassigned_mem_readl, |
2926 | 33417e70 | bellard | }; |
2927 | 33417e70 | bellard | |
2928 | d60efc6b | Blue Swirl | static CPUWriteMemoryFunc * const unassigned_mem_write[3] = { |
2929 | 33417e70 | bellard | unassigned_mem_writeb, |
2930 | e18231a3 | blueswir1 | unassigned_mem_writew, |
2931 | e18231a3 | blueswir1 | unassigned_mem_writel, |
2932 | 33417e70 | bellard | }; |
2933 | 33417e70 | bellard | |
2934 | c227f099 | Anthony Liguori | static void notdirty_mem_writeb(void *opaque, target_phys_addr_t ram_addr, |
2935 | 0f459d16 | pbrook | uint32_t val) |
2936 | 9fa3e853 | bellard | { |
2937 | 3a7d929e | bellard | int dirty_flags;
|
2938 | 3a7d929e | bellard | dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS]; |
2939 | 3a7d929e | bellard | if (!(dirty_flags & CODE_DIRTY_FLAG)) {
|
2940 | 9fa3e853 | bellard | #if !defined(CONFIG_USER_ONLY)
|
2941 | 3a7d929e | bellard | tb_invalidate_phys_page_fast(ram_addr, 1);
|
2942 | 3a7d929e | bellard | dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS]; |
2943 | 9fa3e853 | bellard | #endif
|
2944 | 3a7d929e | bellard | } |
2945 | 5579c7f3 | pbrook | stb_p(qemu_get_ram_ptr(ram_addr), val); |
2946 | f23db169 | bellard | dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
|
2947 | f23db169 | bellard | phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags; |
2948 | f23db169 | bellard | /* we remove the notdirty callback only if the code has been
|
2949 | f23db169 | bellard | flushed */
|
2950 | f23db169 | bellard | if (dirty_flags == 0xff) |
2951 | 2e70f6ef | pbrook | tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr); |
2952 | 9fa3e853 | bellard | } |
2953 | 9fa3e853 | bellard | |
2954 | c227f099 | Anthony Liguori | static void notdirty_mem_writew(void *opaque, target_phys_addr_t ram_addr, |
2955 | 0f459d16 | pbrook | uint32_t val) |
2956 | 9fa3e853 | bellard | { |
2957 | 3a7d929e | bellard | int dirty_flags;
|
2958 | 3a7d929e | bellard | dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS]; |
2959 | 3a7d929e | bellard | if (!(dirty_flags & CODE_DIRTY_FLAG)) {
|
2960 | 9fa3e853 | bellard | #if !defined(CONFIG_USER_ONLY)
|
2961 | 3a7d929e | bellard | tb_invalidate_phys_page_fast(ram_addr, 2);
|
2962 | 3a7d929e | bellard | dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS]; |
2963 | 9fa3e853 | bellard | #endif
|
2964 | 3a7d929e | bellard | } |
2965 | 5579c7f3 | pbrook | stw_p(qemu_get_ram_ptr(ram_addr), val); |
2966 | f23db169 | bellard | dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
|
2967 | f23db169 | bellard | phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags; |
2968 | f23db169 | bellard | /* we remove the notdirty callback only if the code has been
|
2969 | f23db169 | bellard | flushed */
|
2970 | f23db169 | bellard | if (dirty_flags == 0xff) |
2971 | 2e70f6ef | pbrook | tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr); |
2972 | 9fa3e853 | bellard | } |
2973 | 9fa3e853 | bellard | |
2974 | c227f099 | Anthony Liguori | static void notdirty_mem_writel(void *opaque, target_phys_addr_t ram_addr, |
2975 | 0f459d16 | pbrook | uint32_t val) |
2976 | 9fa3e853 | bellard | { |
2977 | 3a7d929e | bellard | int dirty_flags;
|
2978 | 3a7d929e | bellard | dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS]; |
2979 | 3a7d929e | bellard | if (!(dirty_flags & CODE_DIRTY_FLAG)) {
|
2980 | 9fa3e853 | bellard | #if !defined(CONFIG_USER_ONLY)
|
2981 | 3a7d929e | bellard | tb_invalidate_phys_page_fast(ram_addr, 4);
|
2982 | 3a7d929e | bellard | dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS]; |
2983 | 9fa3e853 | bellard | #endif
|
2984 | 3a7d929e | bellard | } |
2985 | 5579c7f3 | pbrook | stl_p(qemu_get_ram_ptr(ram_addr), val); |
2986 | f23db169 | bellard | dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
|
2987 | f23db169 | bellard | phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags; |
2988 | f23db169 | bellard | /* we remove the notdirty callback only if the code has been
|
2989 | f23db169 | bellard | flushed */
|
2990 | f23db169 | bellard | if (dirty_flags == 0xff) |
2991 | 2e70f6ef | pbrook | tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr); |
2992 | 9fa3e853 | bellard | } |
2993 | 9fa3e853 | bellard | |
2994 | d60efc6b | Blue Swirl | static CPUReadMemoryFunc * const error_mem_read[3] = { |
2995 | 9fa3e853 | bellard | NULL, /* never used */ |
2996 | 9fa3e853 | bellard | NULL, /* never used */ |
2997 | 9fa3e853 | bellard | NULL, /* never used */ |
2998 | 9fa3e853 | bellard | }; |
2999 | 9fa3e853 | bellard | |
3000 | d60efc6b | Blue Swirl | static CPUWriteMemoryFunc * const notdirty_mem_write[3] = { |
3001 | 1ccde1cb | bellard | notdirty_mem_writeb, |
3002 | 1ccde1cb | bellard | notdirty_mem_writew, |
3003 | 1ccde1cb | bellard | notdirty_mem_writel, |
3004 | 1ccde1cb | bellard | }; |
3005 | 1ccde1cb | bellard | |
3006 | 0f459d16 | pbrook | /* Generate a debug exception if a watchpoint has been hit. */
|
3007 | b4051334 | aliguori | static void check_watchpoint(int offset, int len_mask, int flags) |
3008 | 0f459d16 | pbrook | { |
3009 | 0f459d16 | pbrook | CPUState *env = cpu_single_env; |
3010 | 06d55cc1 | aliguori | target_ulong pc, cs_base; |
3011 | 06d55cc1 | aliguori | TranslationBlock *tb; |
3012 | 0f459d16 | pbrook | target_ulong vaddr; |
3013 | a1d1bb31 | aliguori | CPUWatchpoint *wp; |
3014 | 06d55cc1 | aliguori | int cpu_flags;
|
3015 | 0f459d16 | pbrook | |
3016 | 06d55cc1 | aliguori | if (env->watchpoint_hit) {
|
3017 | 06d55cc1 | aliguori | /* We re-entered the check after replacing the TB. Now raise
|
3018 | 06d55cc1 | aliguori | * the debug interrupt so that is will trigger after the
|
3019 | 06d55cc1 | aliguori | * current instruction. */
|
3020 | 06d55cc1 | aliguori | cpu_interrupt(env, CPU_INTERRUPT_DEBUG); |
3021 | 06d55cc1 | aliguori | return;
|
3022 | 06d55cc1 | aliguori | } |
3023 | 2e70f6ef | pbrook | vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset; |
3024 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(wp, &env->watchpoints, entry) { |
3025 | b4051334 | aliguori | if ((vaddr == (wp->vaddr & len_mask) ||
|
3026 | b4051334 | aliguori | (vaddr & wp->len_mask) == wp->vaddr) && (wp->flags & flags)) { |
3027 | 6e140f28 | aliguori | wp->flags |= BP_WATCHPOINT_HIT; |
3028 | 6e140f28 | aliguori | if (!env->watchpoint_hit) {
|
3029 | 6e140f28 | aliguori | env->watchpoint_hit = wp; |
3030 | 6e140f28 | aliguori | tb = tb_find_pc(env->mem_io_pc); |
3031 | 6e140f28 | aliguori | if (!tb) {
|
3032 | 6e140f28 | aliguori | cpu_abort(env, "check_watchpoint: could not find TB for "
|
3033 | 6e140f28 | aliguori | "pc=%p", (void *)env->mem_io_pc); |
3034 | 6e140f28 | aliguori | } |
3035 | 6e140f28 | aliguori | cpu_restore_state(tb, env, env->mem_io_pc, NULL);
|
3036 | 6e140f28 | aliguori | tb_phys_invalidate(tb, -1);
|
3037 | 6e140f28 | aliguori | if (wp->flags & BP_STOP_BEFORE_ACCESS) {
|
3038 | 6e140f28 | aliguori | env->exception_index = EXCP_DEBUG; |
3039 | 6e140f28 | aliguori | } else {
|
3040 | 6e140f28 | aliguori | cpu_get_tb_cpu_state(env, &pc, &cs_base, &cpu_flags); |
3041 | 6e140f28 | aliguori | tb_gen_code(env, pc, cs_base, cpu_flags, 1);
|
3042 | 6e140f28 | aliguori | } |
3043 | 6e140f28 | aliguori | cpu_resume_from_signal(env, NULL);
|
3044 | 06d55cc1 | aliguori | } |
3045 | 6e140f28 | aliguori | } else {
|
3046 | 6e140f28 | aliguori | wp->flags &= ~BP_WATCHPOINT_HIT; |
3047 | 0f459d16 | pbrook | } |
3048 | 0f459d16 | pbrook | } |
3049 | 0f459d16 | pbrook | } |
3050 | 0f459d16 | pbrook | |
3051 | 6658ffb8 | pbrook | /* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
|
3052 | 6658ffb8 | pbrook | so these check for a hit then pass through to the normal out-of-line
|
3053 | 6658ffb8 | pbrook | phys routines. */
|
3054 | c227f099 | Anthony Liguori | static uint32_t watch_mem_readb(void *opaque, target_phys_addr_t addr) |
3055 | 6658ffb8 | pbrook | { |
3056 | b4051334 | aliguori | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_READ);
|
3057 | 6658ffb8 | pbrook | return ldub_phys(addr);
|
3058 | 6658ffb8 | pbrook | } |
3059 | 6658ffb8 | pbrook | |
3060 | c227f099 | Anthony Liguori | static uint32_t watch_mem_readw(void *opaque, target_phys_addr_t addr) |
3061 | 6658ffb8 | pbrook | { |
3062 | b4051334 | aliguori | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_READ);
|
3063 | 6658ffb8 | pbrook | return lduw_phys(addr);
|
3064 | 6658ffb8 | pbrook | } |
3065 | 6658ffb8 | pbrook | |
3066 | c227f099 | Anthony Liguori | static uint32_t watch_mem_readl(void *opaque, target_phys_addr_t addr) |
3067 | 6658ffb8 | pbrook | { |
3068 | b4051334 | aliguori | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_READ);
|
3069 | 6658ffb8 | pbrook | return ldl_phys(addr);
|
3070 | 6658ffb8 | pbrook | } |
3071 | 6658ffb8 | pbrook | |
3072 | c227f099 | Anthony Liguori | static void watch_mem_writeb(void *opaque, target_phys_addr_t addr, |
3073 | 6658ffb8 | pbrook | uint32_t val) |
3074 | 6658ffb8 | pbrook | { |
3075 | b4051334 | aliguori | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_WRITE);
|
3076 | 6658ffb8 | pbrook | stb_phys(addr, val); |
3077 | 6658ffb8 | pbrook | } |
3078 | 6658ffb8 | pbrook | |
3079 | c227f099 | Anthony Liguori | static void watch_mem_writew(void *opaque, target_phys_addr_t addr, |
3080 | 6658ffb8 | pbrook | uint32_t val) |
3081 | 6658ffb8 | pbrook | { |
3082 | b4051334 | aliguori | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_WRITE);
|
3083 | 6658ffb8 | pbrook | stw_phys(addr, val); |
3084 | 6658ffb8 | pbrook | } |
3085 | 6658ffb8 | pbrook | |
3086 | c227f099 | Anthony Liguori | static void watch_mem_writel(void *opaque, target_phys_addr_t addr, |
3087 | 6658ffb8 | pbrook | uint32_t val) |
3088 | 6658ffb8 | pbrook | { |
3089 | b4051334 | aliguori | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_WRITE);
|
3090 | 6658ffb8 | pbrook | stl_phys(addr, val); |
3091 | 6658ffb8 | pbrook | } |
3092 | 6658ffb8 | pbrook | |
3093 | d60efc6b | Blue Swirl | static CPUReadMemoryFunc * const watch_mem_read[3] = { |
3094 | 6658ffb8 | pbrook | watch_mem_readb, |
3095 | 6658ffb8 | pbrook | watch_mem_readw, |
3096 | 6658ffb8 | pbrook | watch_mem_readl, |
3097 | 6658ffb8 | pbrook | }; |
3098 | 6658ffb8 | pbrook | |
3099 | d60efc6b | Blue Swirl | static CPUWriteMemoryFunc * const watch_mem_write[3] = { |
3100 | 6658ffb8 | pbrook | watch_mem_writeb, |
3101 | 6658ffb8 | pbrook | watch_mem_writew, |
3102 | 6658ffb8 | pbrook | watch_mem_writel, |
3103 | 6658ffb8 | pbrook | }; |
3104 | 6658ffb8 | pbrook | |
3105 | c227f099 | Anthony Liguori | static inline uint32_t subpage_readlen (subpage_t *mmio, target_phys_addr_t addr, |
3106 | db7b5426 | blueswir1 | unsigned int len) |
3107 | db7b5426 | blueswir1 | { |
3108 | db7b5426 | blueswir1 | uint32_t ret; |
3109 | db7b5426 | blueswir1 | unsigned int idx; |
3110 | db7b5426 | blueswir1 | |
3111 | 8da3ff18 | pbrook | idx = SUBPAGE_IDX(addr); |
3112 | db7b5426 | blueswir1 | #if defined(DEBUG_SUBPAGE)
|
3113 | db7b5426 | blueswir1 | printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__, |
3114 | db7b5426 | blueswir1 | mmio, len, addr, idx); |
3115 | db7b5426 | blueswir1 | #endif
|
3116 | 8da3ff18 | pbrook | ret = (**mmio->mem_read[idx][len])(mmio->opaque[idx][0][len],
|
3117 | 8da3ff18 | pbrook | addr + mmio->region_offset[idx][0][len]);
|
3118 | db7b5426 | blueswir1 | |
3119 | db7b5426 | blueswir1 | return ret;
|
3120 | db7b5426 | blueswir1 | } |
3121 | db7b5426 | blueswir1 | |
3122 | c227f099 | Anthony Liguori | static inline void subpage_writelen (subpage_t *mmio, target_phys_addr_t addr, |
3123 | db7b5426 | blueswir1 | uint32_t value, unsigned int len) |
3124 | db7b5426 | blueswir1 | { |
3125 | db7b5426 | blueswir1 | unsigned int idx; |
3126 | db7b5426 | blueswir1 | |
3127 | 8da3ff18 | pbrook | idx = SUBPAGE_IDX(addr); |
3128 | db7b5426 | blueswir1 | #if defined(DEBUG_SUBPAGE)
|
3129 | db7b5426 | blueswir1 | printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d value %08x\n", __func__, |
3130 | db7b5426 | blueswir1 | mmio, len, addr, idx, value); |
3131 | db7b5426 | blueswir1 | #endif
|
3132 | 8da3ff18 | pbrook | (**mmio->mem_write[idx][len])(mmio->opaque[idx][1][len],
|
3133 | 8da3ff18 | pbrook | addr + mmio->region_offset[idx][1][len],
|
3134 | 8da3ff18 | pbrook | value); |
3135 | db7b5426 | blueswir1 | } |
3136 | db7b5426 | blueswir1 | |
3137 | c227f099 | Anthony Liguori | static uint32_t subpage_readb (void *opaque, target_phys_addr_t addr) |
3138 | db7b5426 | blueswir1 | { |
3139 | db7b5426 | blueswir1 | #if defined(DEBUG_SUBPAGE)
|
3140 | db7b5426 | blueswir1 | printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr); |
3141 | db7b5426 | blueswir1 | #endif
|
3142 | db7b5426 | blueswir1 | |
3143 | db7b5426 | blueswir1 | return subpage_readlen(opaque, addr, 0); |
3144 | db7b5426 | blueswir1 | } |
3145 | db7b5426 | blueswir1 | |
3146 | c227f099 | Anthony Liguori | static void subpage_writeb (void *opaque, target_phys_addr_t addr, |
3147 | db7b5426 | blueswir1 | uint32_t value) |
3148 | db7b5426 | blueswir1 | { |
3149 | db7b5426 | blueswir1 | #if defined(DEBUG_SUBPAGE)
|
3150 | db7b5426 | blueswir1 | printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value); |
3151 | db7b5426 | blueswir1 | #endif
|
3152 | db7b5426 | blueswir1 | subpage_writelen(opaque, addr, value, 0);
|
3153 | db7b5426 | blueswir1 | } |
3154 | db7b5426 | blueswir1 | |
3155 | c227f099 | Anthony Liguori | static uint32_t subpage_readw (void *opaque, target_phys_addr_t addr) |
3156 | db7b5426 | blueswir1 | { |
3157 | db7b5426 | blueswir1 | #if defined(DEBUG_SUBPAGE)
|
3158 | db7b5426 | blueswir1 | printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr); |
3159 | db7b5426 | blueswir1 | #endif
|
3160 | db7b5426 | blueswir1 | |
3161 | db7b5426 | blueswir1 | return subpage_readlen(opaque, addr, 1); |
3162 | db7b5426 | blueswir1 | } |
3163 | db7b5426 | blueswir1 | |
3164 | c227f099 | Anthony Liguori | static void subpage_writew (void *opaque, target_phys_addr_t addr, |
3165 | db7b5426 | blueswir1 | uint32_t value) |
3166 | db7b5426 | blueswir1 | { |
3167 | db7b5426 | blueswir1 | #if defined(DEBUG_SUBPAGE)
|
3168 | db7b5426 | blueswir1 | printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value); |
3169 | db7b5426 | blueswir1 | #endif
|
3170 | db7b5426 | blueswir1 | subpage_writelen(opaque, addr, value, 1);
|
3171 | db7b5426 | blueswir1 | } |
3172 | db7b5426 | blueswir1 | |
3173 | c227f099 | Anthony Liguori | static uint32_t subpage_readl (void *opaque, target_phys_addr_t addr) |
3174 | db7b5426 | blueswir1 | { |
3175 | db7b5426 | blueswir1 | #if defined(DEBUG_SUBPAGE)
|
3176 | db7b5426 | blueswir1 | printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr); |
3177 | db7b5426 | blueswir1 | #endif
|
3178 | db7b5426 | blueswir1 | |
3179 | db7b5426 | blueswir1 | return subpage_readlen(opaque, addr, 2); |
3180 | db7b5426 | blueswir1 | } |
3181 | db7b5426 | blueswir1 | |
3182 | db7b5426 | blueswir1 | static void subpage_writel (void *opaque, |
3183 | c227f099 | Anthony Liguori | target_phys_addr_t addr, uint32_t value) |
3184 | db7b5426 | blueswir1 | { |
3185 | db7b5426 | blueswir1 | #if defined(DEBUG_SUBPAGE)
|
3186 | db7b5426 | blueswir1 | printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value); |
3187 | db7b5426 | blueswir1 | #endif
|
3188 | db7b5426 | blueswir1 | subpage_writelen(opaque, addr, value, 2);
|
3189 | db7b5426 | blueswir1 | } |
3190 | db7b5426 | blueswir1 | |
3191 | d60efc6b | Blue Swirl | static CPUReadMemoryFunc * const subpage_read[] = { |
3192 | db7b5426 | blueswir1 | &subpage_readb, |
3193 | db7b5426 | blueswir1 | &subpage_readw, |
3194 | db7b5426 | blueswir1 | &subpage_readl, |
3195 | db7b5426 | blueswir1 | }; |
3196 | db7b5426 | blueswir1 | |
3197 | d60efc6b | Blue Swirl | static CPUWriteMemoryFunc * const subpage_write[] = { |
3198 | db7b5426 | blueswir1 | &subpage_writeb, |
3199 | db7b5426 | blueswir1 | &subpage_writew, |
3200 | db7b5426 | blueswir1 | &subpage_writel, |
3201 | db7b5426 | blueswir1 | }; |
3202 | db7b5426 | blueswir1 | |
3203 | c227f099 | Anthony Liguori | static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end, |
3204 | c227f099 | Anthony Liguori | ram_addr_t memory, ram_addr_t region_offset) |
3205 | db7b5426 | blueswir1 | { |
3206 | db7b5426 | blueswir1 | int idx, eidx;
|
3207 | 4254fab8 | blueswir1 | unsigned int i; |
3208 | db7b5426 | blueswir1 | |
3209 | db7b5426 | blueswir1 | if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
|
3210 | db7b5426 | blueswir1 | return -1; |
3211 | db7b5426 | blueswir1 | idx = SUBPAGE_IDX(start); |
3212 | db7b5426 | blueswir1 | eidx = SUBPAGE_IDX(end); |
3213 | db7b5426 | blueswir1 | #if defined(DEBUG_SUBPAGE)
|
3214 | 0bf9e31a | Blue Swirl | printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %ld\n", __func__,
|
3215 | db7b5426 | blueswir1 | mmio, start, end, idx, eidx, memory); |
3216 | db7b5426 | blueswir1 | #endif
|
3217 | db7b5426 | blueswir1 | memory >>= IO_MEM_SHIFT; |
3218 | db7b5426 | blueswir1 | for (; idx <= eidx; idx++) {
|
3219 | 4254fab8 | blueswir1 | for (i = 0; i < 4; i++) { |
3220 | 3ee89922 | blueswir1 | if (io_mem_read[memory][i]) {
|
3221 | 3ee89922 | blueswir1 | mmio->mem_read[idx][i] = &io_mem_read[memory][i]; |
3222 | 3ee89922 | blueswir1 | mmio->opaque[idx][0][i] = io_mem_opaque[memory];
|
3223 | 8da3ff18 | pbrook | mmio->region_offset[idx][0][i] = region_offset;
|
3224 | 3ee89922 | blueswir1 | } |
3225 | 3ee89922 | blueswir1 | if (io_mem_write[memory][i]) {
|
3226 | 3ee89922 | blueswir1 | mmio->mem_write[idx][i] = &io_mem_write[memory][i]; |
3227 | 3ee89922 | blueswir1 | mmio->opaque[idx][1][i] = io_mem_opaque[memory];
|
3228 | 8da3ff18 | pbrook | mmio->region_offset[idx][1][i] = region_offset;
|
3229 | 3ee89922 | blueswir1 | } |
3230 | 4254fab8 | blueswir1 | } |
3231 | db7b5426 | blueswir1 | } |
3232 | db7b5426 | blueswir1 | |
3233 | db7b5426 | blueswir1 | return 0; |
3234 | db7b5426 | blueswir1 | } |
3235 | db7b5426 | blueswir1 | |
3236 | c227f099 | Anthony Liguori | static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys, |
3237 | c227f099 | Anthony Liguori | ram_addr_t orig_memory, ram_addr_t region_offset) |
3238 | db7b5426 | blueswir1 | { |
3239 | c227f099 | Anthony Liguori | subpage_t *mmio; |
3240 | db7b5426 | blueswir1 | int subpage_memory;
|
3241 | db7b5426 | blueswir1 | |
3242 | c227f099 | Anthony Liguori | mmio = qemu_mallocz(sizeof(subpage_t));
|
3243 | 1eec614b | aliguori | |
3244 | 1eec614b | aliguori | mmio->base = base; |
3245 | 1eed09cb | Avi Kivity | subpage_memory = cpu_register_io_memory(subpage_read, subpage_write, mmio); |
3246 | db7b5426 | blueswir1 | #if defined(DEBUG_SUBPAGE)
|
3247 | 1eec614b | aliguori | printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__, |
3248 | 1eec614b | aliguori | mmio, base, TARGET_PAGE_SIZE, subpage_memory); |
3249 | db7b5426 | blueswir1 | #endif
|
3250 | 1eec614b | aliguori | *phys = subpage_memory | IO_MEM_SUBPAGE; |
3251 | 1eec614b | aliguori | subpage_register(mmio, 0, TARGET_PAGE_SIZE - 1, orig_memory, |
3252 | 8da3ff18 | pbrook | region_offset); |
3253 | db7b5426 | blueswir1 | |
3254 | db7b5426 | blueswir1 | return mmio;
|
3255 | db7b5426 | blueswir1 | } |
3256 | db7b5426 | blueswir1 | |
3257 | 88715657 | aliguori | static int get_free_io_mem_idx(void) |
3258 | 88715657 | aliguori | { |
3259 | 88715657 | aliguori | int i;
|
3260 | 88715657 | aliguori | |
3261 | 88715657 | aliguori | for (i = 0; i<IO_MEM_NB_ENTRIES; i++) |
3262 | 88715657 | aliguori | if (!io_mem_used[i]) {
|
3263 | 88715657 | aliguori | io_mem_used[i] = 1;
|
3264 | 88715657 | aliguori | return i;
|
3265 | 88715657 | aliguori | } |
3266 | c6703b47 | Riku Voipio | fprintf(stderr, "RAN out out io_mem_idx, max %d !\n", IO_MEM_NB_ENTRIES);
|
3267 | 88715657 | aliguori | return -1; |
3268 | 88715657 | aliguori | } |
3269 | 88715657 | aliguori | |
3270 | 33417e70 | bellard | /* mem_read and mem_write are arrays of functions containing the
|
3271 | 33417e70 | bellard | function to access byte (index 0), word (index 1) and dword (index
|
3272 | 0b4e6e3e | Paul Brook | 2). Functions can be omitted with a NULL function pointer.
|
3273 | 3ee89922 | blueswir1 | If io_index is non zero, the corresponding io zone is
|
3274 | 4254fab8 | blueswir1 | modified. If it is zero, a new io zone is allocated. The return
|
3275 | 4254fab8 | blueswir1 | value can be used with cpu_register_physical_memory(). (-1) is
|
3276 | 4254fab8 | blueswir1 | returned if error. */
|
3277 | 1eed09cb | Avi Kivity | static int cpu_register_io_memory_fixed(int io_index, |
3278 | d60efc6b | Blue Swirl | CPUReadMemoryFunc * const *mem_read,
|
3279 | d60efc6b | Blue Swirl | CPUWriteMemoryFunc * const *mem_write,
|
3280 | 1eed09cb | Avi Kivity | void *opaque)
|
3281 | 33417e70 | bellard | { |
3282 | 4254fab8 | blueswir1 | int i, subwidth = 0; |
3283 | 33417e70 | bellard | |
3284 | 33417e70 | bellard | if (io_index <= 0) { |
3285 | 88715657 | aliguori | io_index = get_free_io_mem_idx(); |
3286 | 88715657 | aliguori | if (io_index == -1) |
3287 | 88715657 | aliguori | return io_index;
|
3288 | 33417e70 | bellard | } else {
|
3289 | 1eed09cb | Avi Kivity | io_index >>= IO_MEM_SHIFT; |
3290 | 33417e70 | bellard | if (io_index >= IO_MEM_NB_ENTRIES)
|
3291 | 33417e70 | bellard | return -1; |
3292 | 33417e70 | bellard | } |
3293 | b5ff1b31 | bellard | |
3294 | 33417e70 | bellard | for(i = 0;i < 3; i++) { |
3295 | 4254fab8 | blueswir1 | if (!mem_read[i] || !mem_write[i])
|
3296 | 4254fab8 | blueswir1 | subwidth = IO_MEM_SUBWIDTH; |
3297 | 33417e70 | bellard | io_mem_read[io_index][i] = mem_read[i]; |
3298 | 33417e70 | bellard | io_mem_write[io_index][i] = mem_write[i]; |
3299 | 33417e70 | bellard | } |
3300 | a4193c8a | bellard | io_mem_opaque[io_index] = opaque; |
3301 | 4254fab8 | blueswir1 | return (io_index << IO_MEM_SHIFT) | subwidth;
|
3302 | 33417e70 | bellard | } |
3303 | 61382a50 | bellard | |
3304 | d60efc6b | Blue Swirl | int cpu_register_io_memory(CPUReadMemoryFunc * const *mem_read, |
3305 | d60efc6b | Blue Swirl | CPUWriteMemoryFunc * const *mem_write,
|
3306 | 1eed09cb | Avi Kivity | void *opaque)
|
3307 | 1eed09cb | Avi Kivity | { |
3308 | 1eed09cb | Avi Kivity | return cpu_register_io_memory_fixed(0, mem_read, mem_write, opaque); |
3309 | 1eed09cb | Avi Kivity | } |
3310 | 1eed09cb | Avi Kivity | |
3311 | 88715657 | aliguori | void cpu_unregister_io_memory(int io_table_address) |
3312 | 88715657 | aliguori | { |
3313 | 88715657 | aliguori | int i;
|
3314 | 88715657 | aliguori | int io_index = io_table_address >> IO_MEM_SHIFT;
|
3315 | 88715657 | aliguori | |
3316 | 88715657 | aliguori | for (i=0;i < 3; i++) { |
3317 | 88715657 | aliguori | io_mem_read[io_index][i] = unassigned_mem_read[i]; |
3318 | 88715657 | aliguori | io_mem_write[io_index][i] = unassigned_mem_write[i]; |
3319 | 88715657 | aliguori | } |
3320 | 88715657 | aliguori | io_mem_opaque[io_index] = NULL;
|
3321 | 88715657 | aliguori | io_mem_used[io_index] = 0;
|
3322 | 88715657 | aliguori | } |
3323 | 88715657 | aliguori | |
3324 | e9179ce1 | Avi Kivity | static void io_mem_init(void) |
3325 | e9179ce1 | Avi Kivity | { |
3326 | e9179ce1 | Avi Kivity | int i;
|
3327 | e9179ce1 | Avi Kivity | |
3328 | e9179ce1 | Avi Kivity | cpu_register_io_memory_fixed(IO_MEM_ROM, error_mem_read, unassigned_mem_write, NULL);
|
3329 | e9179ce1 | Avi Kivity | cpu_register_io_memory_fixed(IO_MEM_UNASSIGNED, unassigned_mem_read, unassigned_mem_write, NULL);
|
3330 | e9179ce1 | Avi Kivity | cpu_register_io_memory_fixed(IO_MEM_NOTDIRTY, error_mem_read, notdirty_mem_write, NULL);
|
3331 | e9179ce1 | Avi Kivity | for (i=0; i<5; i++) |
3332 | e9179ce1 | Avi Kivity | io_mem_used[i] = 1;
|
3333 | e9179ce1 | Avi Kivity | |
3334 | e9179ce1 | Avi Kivity | io_mem_watch = cpu_register_io_memory(watch_mem_read, |
3335 | e9179ce1 | Avi Kivity | watch_mem_write, NULL);
|
3336 | e9179ce1 | Avi Kivity | } |
3337 | e9179ce1 | Avi Kivity | |
3338 | e2eef170 | pbrook | #endif /* !defined(CONFIG_USER_ONLY) */ |
3339 | e2eef170 | pbrook | |
3340 | 13eb76e0 | bellard | /* physical memory access (slow version, mainly for debug) */
|
3341 | 13eb76e0 | bellard | #if defined(CONFIG_USER_ONLY)
|
3342 | a68fe89c | Paul Brook | int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
|
3343 | a68fe89c | Paul Brook | uint8_t *buf, int len, int is_write) |
3344 | 13eb76e0 | bellard | { |
3345 | 13eb76e0 | bellard | int l, flags;
|
3346 | 13eb76e0 | bellard | target_ulong page; |
3347 | 53a5960a | pbrook | void * p;
|
3348 | 13eb76e0 | bellard | |
3349 | 13eb76e0 | bellard | while (len > 0) { |
3350 | 13eb76e0 | bellard | page = addr & TARGET_PAGE_MASK; |
3351 | 13eb76e0 | bellard | l = (page + TARGET_PAGE_SIZE) - addr; |
3352 | 13eb76e0 | bellard | if (l > len)
|
3353 | 13eb76e0 | bellard | l = len; |
3354 | 13eb76e0 | bellard | flags = page_get_flags(page); |
3355 | 13eb76e0 | bellard | if (!(flags & PAGE_VALID))
|
3356 | a68fe89c | Paul Brook | return -1; |
3357 | 13eb76e0 | bellard | if (is_write) {
|
3358 | 13eb76e0 | bellard | if (!(flags & PAGE_WRITE))
|
3359 | a68fe89c | Paul Brook | return -1; |
3360 | 579a97f7 | bellard | /* XXX: this code should not depend on lock_user */
|
3361 | 72fb7daa | aurel32 | if (!(p = lock_user(VERIFY_WRITE, addr, l, 0))) |
3362 | a68fe89c | Paul Brook | return -1; |
3363 | 72fb7daa | aurel32 | memcpy(p, buf, l); |
3364 | 72fb7daa | aurel32 | unlock_user(p, addr, l); |
3365 | 13eb76e0 | bellard | } else {
|
3366 | 13eb76e0 | bellard | if (!(flags & PAGE_READ))
|
3367 | a68fe89c | Paul Brook | return -1; |
3368 | 579a97f7 | bellard | /* XXX: this code should not depend on lock_user */
|
3369 | 72fb7daa | aurel32 | if (!(p = lock_user(VERIFY_READ, addr, l, 1))) |
3370 | a68fe89c | Paul Brook | return -1; |
3371 | 72fb7daa | aurel32 | memcpy(buf, p, l); |
3372 | 5b257578 | aurel32 | unlock_user(p, addr, 0);
|
3373 | 13eb76e0 | bellard | } |
3374 | 13eb76e0 | bellard | len -= l; |
3375 | 13eb76e0 | bellard | buf += l; |
3376 | 13eb76e0 | bellard | addr += l; |
3377 | 13eb76e0 | bellard | } |
3378 | a68fe89c | Paul Brook | return 0; |
3379 | 13eb76e0 | bellard | } |
3380 | 8df1cd07 | bellard | |
3381 | 13eb76e0 | bellard | #else
|
3382 | c227f099 | Anthony Liguori | void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
|
3383 | 13eb76e0 | bellard | int len, int is_write) |
3384 | 13eb76e0 | bellard | { |
3385 | 13eb76e0 | bellard | int l, io_index;
|
3386 | 13eb76e0 | bellard | uint8_t *ptr; |
3387 | 13eb76e0 | bellard | uint32_t val; |
3388 | c227f099 | Anthony Liguori | target_phys_addr_t page; |
3389 | 2e12669a | bellard | unsigned long pd; |
3390 | 92e873b9 | bellard | PhysPageDesc *p; |
3391 | 3b46e624 | ths | |
3392 | 13eb76e0 | bellard | while (len > 0) { |
3393 | 13eb76e0 | bellard | page = addr & TARGET_PAGE_MASK; |
3394 | 13eb76e0 | bellard | l = (page + TARGET_PAGE_SIZE) - addr; |
3395 | 13eb76e0 | bellard | if (l > len)
|
3396 | 13eb76e0 | bellard | l = len; |
3397 | 92e873b9 | bellard | p = phys_page_find(page >> TARGET_PAGE_BITS); |
3398 | 13eb76e0 | bellard | if (!p) {
|
3399 | 13eb76e0 | bellard | pd = IO_MEM_UNASSIGNED; |
3400 | 13eb76e0 | bellard | } else {
|
3401 | 13eb76e0 | bellard | pd = p->phys_offset; |
3402 | 13eb76e0 | bellard | } |
3403 | 3b46e624 | ths | |
3404 | 13eb76e0 | bellard | if (is_write) {
|
3405 | 3a7d929e | bellard | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
|
3406 | c227f099 | Anthony Liguori | target_phys_addr_t addr1 = addr; |
3407 | 13eb76e0 | bellard | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
|
3408 | 8da3ff18 | pbrook | if (p)
|
3409 | 6c2934db | aurel32 | addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset; |
3410 | 6a00d601 | bellard | /* XXX: could force cpu_single_env to NULL to avoid
|
3411 | 6a00d601 | bellard | potential bugs */
|
3412 | 6c2934db | aurel32 | if (l >= 4 && ((addr1 & 3) == 0)) { |
3413 | 1c213d19 | bellard | /* 32 bit write access */
|
3414 | c27004ec | bellard | val = ldl_p(buf); |
3415 | 6c2934db | aurel32 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr1, val);
|
3416 | 13eb76e0 | bellard | l = 4;
|
3417 | 6c2934db | aurel32 | } else if (l >= 2 && ((addr1 & 1) == 0)) { |
3418 | 1c213d19 | bellard | /* 16 bit write access */
|
3419 | c27004ec | bellard | val = lduw_p(buf); |
3420 | 6c2934db | aurel32 | io_mem_write[io_index][1](io_mem_opaque[io_index], addr1, val);
|
3421 | 13eb76e0 | bellard | l = 2;
|
3422 | 13eb76e0 | bellard | } else {
|
3423 | 1c213d19 | bellard | /* 8 bit write access */
|
3424 | c27004ec | bellard | val = ldub_p(buf); |
3425 | 6c2934db | aurel32 | io_mem_write[io_index][0](io_mem_opaque[io_index], addr1, val);
|
3426 | 13eb76e0 | bellard | l = 1;
|
3427 | 13eb76e0 | bellard | } |
3428 | 13eb76e0 | bellard | } else {
|
3429 | b448f2f3 | bellard | unsigned long addr1; |
3430 | b448f2f3 | bellard | addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); |
3431 | 13eb76e0 | bellard | /* RAM case */
|
3432 | 5579c7f3 | pbrook | ptr = qemu_get_ram_ptr(addr1); |
3433 | 13eb76e0 | bellard | memcpy(ptr, buf, l); |
3434 | 3a7d929e | bellard | if (!cpu_physical_memory_is_dirty(addr1)) {
|
3435 | 3a7d929e | bellard | /* invalidate code */
|
3436 | 3a7d929e | bellard | tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
|
3437 | 3a7d929e | bellard | /* set dirty bit */
|
3438 | 5fafdf24 | ths | phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |= |
3439 | f23db169 | bellard | (0xff & ~CODE_DIRTY_FLAG);
|
3440 | 3a7d929e | bellard | } |
3441 | 13eb76e0 | bellard | } |
3442 | 13eb76e0 | bellard | } else {
|
3443 | 5fafdf24 | ths | if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
|
3444 | 2a4188a3 | bellard | !(pd & IO_MEM_ROMD)) { |
3445 | c227f099 | Anthony Liguori | target_phys_addr_t addr1 = addr; |
3446 | 13eb76e0 | bellard | /* I/O case */
|
3447 | 13eb76e0 | bellard | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
|
3448 | 8da3ff18 | pbrook | if (p)
|
3449 | 6c2934db | aurel32 | addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset; |
3450 | 6c2934db | aurel32 | if (l >= 4 && ((addr1 & 3) == 0)) { |
3451 | 13eb76e0 | bellard | /* 32 bit read access */
|
3452 | 6c2934db | aurel32 | val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr1);
|
3453 | c27004ec | bellard | stl_p(buf, val); |
3454 | 13eb76e0 | bellard | l = 4;
|
3455 | 6c2934db | aurel32 | } else if (l >= 2 && ((addr1 & 1) == 0)) { |
3456 | 13eb76e0 | bellard | /* 16 bit read access */
|
3457 | 6c2934db | aurel32 | val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr1);
|
3458 | c27004ec | bellard | stw_p(buf, val); |
3459 | 13eb76e0 | bellard | l = 2;
|
3460 | 13eb76e0 | bellard | } else {
|
3461 | 1c213d19 | bellard | /* 8 bit read access */
|
3462 | 6c2934db | aurel32 | val = io_mem_read[io_index][0](io_mem_opaque[io_index], addr1);
|
3463 | c27004ec | bellard | stb_p(buf, val); |
3464 | 13eb76e0 | bellard | l = 1;
|
3465 | 13eb76e0 | bellard | } |
3466 | 13eb76e0 | bellard | } else {
|
3467 | 13eb76e0 | bellard | /* RAM case */
|
3468 | 5579c7f3 | pbrook | ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) + |
3469 | 13eb76e0 | bellard | (addr & ~TARGET_PAGE_MASK); |
3470 | 13eb76e0 | bellard | memcpy(buf, ptr, l); |
3471 | 13eb76e0 | bellard | } |
3472 | 13eb76e0 | bellard | } |
3473 | 13eb76e0 | bellard | len -= l; |
3474 | 13eb76e0 | bellard | buf += l; |
3475 | 13eb76e0 | bellard | addr += l; |
3476 | 13eb76e0 | bellard | } |
3477 | 13eb76e0 | bellard | } |
3478 | 8df1cd07 | bellard | |
3479 | d0ecd2aa | bellard | /* used for ROM loading : can write in RAM and ROM */
|
3480 | c227f099 | Anthony Liguori | void cpu_physical_memory_write_rom(target_phys_addr_t addr,
|
3481 | d0ecd2aa | bellard | const uint8_t *buf, int len) |
3482 | d0ecd2aa | bellard | { |
3483 | d0ecd2aa | bellard | int l;
|
3484 | d0ecd2aa | bellard | uint8_t *ptr; |
3485 | c227f099 | Anthony Liguori | target_phys_addr_t page; |
3486 | d0ecd2aa | bellard | unsigned long pd; |
3487 | d0ecd2aa | bellard | PhysPageDesc *p; |
3488 | 3b46e624 | ths | |
3489 | d0ecd2aa | bellard | while (len > 0) { |
3490 | d0ecd2aa | bellard | page = addr & TARGET_PAGE_MASK; |
3491 | d0ecd2aa | bellard | l = (page + TARGET_PAGE_SIZE) - addr; |
3492 | d0ecd2aa | bellard | if (l > len)
|
3493 | d0ecd2aa | bellard | l = len; |
3494 | d0ecd2aa | bellard | p = phys_page_find(page >> TARGET_PAGE_BITS); |
3495 | d0ecd2aa | bellard | if (!p) {
|
3496 | d0ecd2aa | bellard | pd = IO_MEM_UNASSIGNED; |
3497 | d0ecd2aa | bellard | } else {
|
3498 | d0ecd2aa | bellard | pd = p->phys_offset; |
3499 | d0ecd2aa | bellard | } |
3500 | 3b46e624 | ths | |
3501 | d0ecd2aa | bellard | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM &&
|
3502 | 2a4188a3 | bellard | (pd & ~TARGET_PAGE_MASK) != IO_MEM_ROM && |
3503 | 2a4188a3 | bellard | !(pd & IO_MEM_ROMD)) { |
3504 | d0ecd2aa | bellard | /* do nothing */
|
3505 | d0ecd2aa | bellard | } else {
|
3506 | d0ecd2aa | bellard | unsigned long addr1; |
3507 | d0ecd2aa | bellard | addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); |
3508 | d0ecd2aa | bellard | /* ROM/RAM case */
|
3509 | 5579c7f3 | pbrook | ptr = qemu_get_ram_ptr(addr1); |
3510 | d0ecd2aa | bellard | memcpy(ptr, buf, l); |
3511 | d0ecd2aa | bellard | } |
3512 | d0ecd2aa | bellard | len -= l; |
3513 | d0ecd2aa | bellard | buf += l; |
3514 | d0ecd2aa | bellard | addr += l; |
3515 | d0ecd2aa | bellard | } |
3516 | d0ecd2aa | bellard | } |
3517 | d0ecd2aa | bellard | |
3518 | 6d16c2f8 | aliguori | typedef struct { |
3519 | 6d16c2f8 | aliguori | void *buffer;
|
3520 | c227f099 | Anthony Liguori | target_phys_addr_t addr; |
3521 | c227f099 | Anthony Liguori | target_phys_addr_t len; |
3522 | 6d16c2f8 | aliguori | } BounceBuffer; |
3523 | 6d16c2f8 | aliguori | |
3524 | 6d16c2f8 | aliguori | static BounceBuffer bounce;
|
3525 | 6d16c2f8 | aliguori | |
3526 | ba223c29 | aliguori | typedef struct MapClient { |
3527 | ba223c29 | aliguori | void *opaque;
|
3528 | ba223c29 | aliguori | void (*callback)(void *opaque); |
3529 | 72cf2d4f | Blue Swirl | QLIST_ENTRY(MapClient) link; |
3530 | ba223c29 | aliguori | } MapClient; |
3531 | ba223c29 | aliguori | |
3532 | 72cf2d4f | Blue Swirl | static QLIST_HEAD(map_client_list, MapClient) map_client_list
|
3533 | 72cf2d4f | Blue Swirl | = QLIST_HEAD_INITIALIZER(map_client_list); |
3534 | ba223c29 | aliguori | |
3535 | ba223c29 | aliguori | void *cpu_register_map_client(void *opaque, void (*callback)(void *opaque)) |
3536 | ba223c29 | aliguori | { |
3537 | ba223c29 | aliguori | MapClient *client = qemu_malloc(sizeof(*client));
|
3538 | ba223c29 | aliguori | |
3539 | ba223c29 | aliguori | client->opaque = opaque; |
3540 | ba223c29 | aliguori | client->callback = callback; |
3541 | 72cf2d4f | Blue Swirl | QLIST_INSERT_HEAD(&map_client_list, client, link); |
3542 | ba223c29 | aliguori | return client;
|
3543 | ba223c29 | aliguori | } |
3544 | ba223c29 | aliguori | |
3545 | ba223c29 | aliguori | void cpu_unregister_map_client(void *_client) |
3546 | ba223c29 | aliguori | { |
3547 | ba223c29 | aliguori | MapClient *client = (MapClient *)_client; |
3548 | ba223c29 | aliguori | |
3549 | 72cf2d4f | Blue Swirl | QLIST_REMOVE(client, link); |
3550 | 34d5e948 | Isaku Yamahata | qemu_free(client); |
3551 | ba223c29 | aliguori | } |
3552 | ba223c29 | aliguori | |
3553 | ba223c29 | aliguori | static void cpu_notify_map_clients(void) |
3554 | ba223c29 | aliguori | { |
3555 | ba223c29 | aliguori | MapClient *client; |
3556 | ba223c29 | aliguori | |
3557 | 72cf2d4f | Blue Swirl | while (!QLIST_EMPTY(&map_client_list)) {
|
3558 | 72cf2d4f | Blue Swirl | client = QLIST_FIRST(&map_client_list); |
3559 | ba223c29 | aliguori | client->callback(client->opaque); |
3560 | 34d5e948 | Isaku Yamahata | cpu_unregister_map_client(client); |
3561 | ba223c29 | aliguori | } |
3562 | ba223c29 | aliguori | } |
3563 | ba223c29 | aliguori | |
3564 | 6d16c2f8 | aliguori | /* Map a physical memory region into a host virtual address.
|
3565 | 6d16c2f8 | aliguori | * May map a subset of the requested range, given by and returned in *plen.
|
3566 | 6d16c2f8 | aliguori | * May return NULL if resources needed to perform the mapping are exhausted.
|
3567 | 6d16c2f8 | aliguori | * Use only for reads OR writes - not for read-modify-write operations.
|
3568 | ba223c29 | aliguori | * Use cpu_register_map_client() to know when retrying the map operation is
|
3569 | ba223c29 | aliguori | * likely to succeed.
|
3570 | 6d16c2f8 | aliguori | */
|
3571 | c227f099 | Anthony Liguori | void *cpu_physical_memory_map(target_phys_addr_t addr,
|
3572 | c227f099 | Anthony Liguori | target_phys_addr_t *plen, |
3573 | 6d16c2f8 | aliguori | int is_write)
|
3574 | 6d16c2f8 | aliguori | { |
3575 | c227f099 | Anthony Liguori | target_phys_addr_t len = *plen; |
3576 | c227f099 | Anthony Liguori | target_phys_addr_t done = 0;
|
3577 | 6d16c2f8 | aliguori | int l;
|
3578 | 6d16c2f8 | aliguori | uint8_t *ret = NULL;
|
3579 | 6d16c2f8 | aliguori | uint8_t *ptr; |
3580 | c227f099 | Anthony Liguori | target_phys_addr_t page; |
3581 | 6d16c2f8 | aliguori | unsigned long pd; |
3582 | 6d16c2f8 | aliguori | PhysPageDesc *p; |
3583 | 6d16c2f8 | aliguori | unsigned long addr1; |
3584 | 6d16c2f8 | aliguori | |
3585 | 6d16c2f8 | aliguori | while (len > 0) { |
3586 | 6d16c2f8 | aliguori | page = addr & TARGET_PAGE_MASK; |
3587 | 6d16c2f8 | aliguori | l = (page + TARGET_PAGE_SIZE) - addr; |
3588 | 6d16c2f8 | aliguori | if (l > len)
|
3589 | 6d16c2f8 | aliguori | l = len; |
3590 | 6d16c2f8 | aliguori | p = phys_page_find(page >> TARGET_PAGE_BITS); |
3591 | 6d16c2f8 | aliguori | if (!p) {
|
3592 | 6d16c2f8 | aliguori | pd = IO_MEM_UNASSIGNED; |
3593 | 6d16c2f8 | aliguori | } else {
|
3594 | 6d16c2f8 | aliguori | pd = p->phys_offset; |
3595 | 6d16c2f8 | aliguori | } |
3596 | 6d16c2f8 | aliguori | |
3597 | 6d16c2f8 | aliguori | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
|
3598 | 6d16c2f8 | aliguori | if (done || bounce.buffer) {
|
3599 | 6d16c2f8 | aliguori | break;
|
3600 | 6d16c2f8 | aliguori | } |
3601 | 6d16c2f8 | aliguori | bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, TARGET_PAGE_SIZE); |
3602 | 6d16c2f8 | aliguori | bounce.addr = addr; |
3603 | 6d16c2f8 | aliguori | bounce.len = l; |
3604 | 6d16c2f8 | aliguori | if (!is_write) {
|
3605 | 6d16c2f8 | aliguori | cpu_physical_memory_rw(addr, bounce.buffer, l, 0);
|
3606 | 6d16c2f8 | aliguori | } |
3607 | 6d16c2f8 | aliguori | ptr = bounce.buffer; |
3608 | 6d16c2f8 | aliguori | } else {
|
3609 | 6d16c2f8 | aliguori | addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); |
3610 | 5579c7f3 | pbrook | ptr = qemu_get_ram_ptr(addr1); |
3611 | 6d16c2f8 | aliguori | } |
3612 | 6d16c2f8 | aliguori | if (!done) {
|
3613 | 6d16c2f8 | aliguori | ret = ptr; |
3614 | 6d16c2f8 | aliguori | } else if (ret + done != ptr) { |
3615 | 6d16c2f8 | aliguori | break;
|
3616 | 6d16c2f8 | aliguori | } |
3617 | 6d16c2f8 | aliguori | |
3618 | 6d16c2f8 | aliguori | len -= l; |
3619 | 6d16c2f8 | aliguori | addr += l; |
3620 | 6d16c2f8 | aliguori | done += l; |
3621 | 6d16c2f8 | aliguori | } |
3622 | 6d16c2f8 | aliguori | *plen = done; |
3623 | 6d16c2f8 | aliguori | return ret;
|
3624 | 6d16c2f8 | aliguori | } |
3625 | 6d16c2f8 | aliguori | |
3626 | 6d16c2f8 | aliguori | /* Unmaps a memory region previously mapped by cpu_physical_memory_map().
|
3627 | 6d16c2f8 | aliguori | * Will also mark the memory as dirty if is_write == 1. access_len gives
|
3628 | 6d16c2f8 | aliguori | * the amount of memory that was actually read or written by the caller.
|
3629 | 6d16c2f8 | aliguori | */
|
3630 | c227f099 | Anthony Liguori | void cpu_physical_memory_unmap(void *buffer, target_phys_addr_t len, |
3631 | c227f099 | Anthony Liguori | int is_write, target_phys_addr_t access_len)
|
3632 | 6d16c2f8 | aliguori | { |
3633 | 6d16c2f8 | aliguori | if (buffer != bounce.buffer) {
|
3634 | 6d16c2f8 | aliguori | if (is_write) {
|
3635 | c227f099 | Anthony Liguori | ram_addr_t addr1 = qemu_ram_addr_from_host(buffer); |
3636 | 6d16c2f8 | aliguori | while (access_len) {
|
3637 | 6d16c2f8 | aliguori | unsigned l;
|
3638 | 6d16c2f8 | aliguori | l = TARGET_PAGE_SIZE; |
3639 | 6d16c2f8 | aliguori | if (l > access_len)
|
3640 | 6d16c2f8 | aliguori | l = access_len; |
3641 | 6d16c2f8 | aliguori | if (!cpu_physical_memory_is_dirty(addr1)) {
|
3642 | 6d16c2f8 | aliguori | /* invalidate code */
|
3643 | 6d16c2f8 | aliguori | tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
|
3644 | 6d16c2f8 | aliguori | /* set dirty bit */
|
3645 | 6d16c2f8 | aliguori | phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |= |
3646 | 6d16c2f8 | aliguori | (0xff & ~CODE_DIRTY_FLAG);
|
3647 | 6d16c2f8 | aliguori | } |
3648 | 6d16c2f8 | aliguori | addr1 += l; |
3649 | 6d16c2f8 | aliguori | access_len -= l; |
3650 | 6d16c2f8 | aliguori | } |
3651 | 6d16c2f8 | aliguori | } |
3652 | 6d16c2f8 | aliguori | return;
|
3653 | 6d16c2f8 | aliguori | } |
3654 | 6d16c2f8 | aliguori | if (is_write) {
|
3655 | 6d16c2f8 | aliguori | cpu_physical_memory_write(bounce.addr, bounce.buffer, access_len); |
3656 | 6d16c2f8 | aliguori | } |
3657 | f8a83245 | Herve Poussineau | qemu_vfree(bounce.buffer); |
3658 | 6d16c2f8 | aliguori | bounce.buffer = NULL;
|
3659 | ba223c29 | aliguori | cpu_notify_map_clients(); |
3660 | 6d16c2f8 | aliguori | } |
3661 | d0ecd2aa | bellard | |
3662 | 8df1cd07 | bellard | /* warning: addr must be aligned */
|
3663 | c227f099 | Anthony Liguori | uint32_t ldl_phys(target_phys_addr_t addr) |
3664 | 8df1cd07 | bellard | { |
3665 | 8df1cd07 | bellard | int io_index;
|
3666 | 8df1cd07 | bellard | uint8_t *ptr; |
3667 | 8df1cd07 | bellard | uint32_t val; |
3668 | 8df1cd07 | bellard | unsigned long pd; |
3669 | 8df1cd07 | bellard | PhysPageDesc *p; |
3670 | 8df1cd07 | bellard | |
3671 | 8df1cd07 | bellard | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
3672 | 8df1cd07 | bellard | if (!p) {
|
3673 | 8df1cd07 | bellard | pd = IO_MEM_UNASSIGNED; |
3674 | 8df1cd07 | bellard | } else {
|
3675 | 8df1cd07 | bellard | pd = p->phys_offset; |
3676 | 8df1cd07 | bellard | } |
3677 | 3b46e624 | ths | |
3678 | 5fafdf24 | ths | if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
|
3679 | 2a4188a3 | bellard | !(pd & IO_MEM_ROMD)) { |
3680 | 8df1cd07 | bellard | /* I/O case */
|
3681 | 8df1cd07 | bellard | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
|
3682 | 8da3ff18 | pbrook | if (p)
|
3683 | 8da3ff18 | pbrook | addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset; |
3684 | 8df1cd07 | bellard | val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
|
3685 | 8df1cd07 | bellard | } else {
|
3686 | 8df1cd07 | bellard | /* RAM case */
|
3687 | 5579c7f3 | pbrook | ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) + |
3688 | 8df1cd07 | bellard | (addr & ~TARGET_PAGE_MASK); |
3689 | 8df1cd07 | bellard | val = ldl_p(ptr); |
3690 | 8df1cd07 | bellard | } |
3691 | 8df1cd07 | bellard | return val;
|
3692 | 8df1cd07 | bellard | } |
3693 | 8df1cd07 | bellard | |
3694 | 84b7b8e7 | bellard | /* warning: addr must be aligned */
|
3695 | c227f099 | Anthony Liguori | uint64_t ldq_phys(target_phys_addr_t addr) |
3696 | 84b7b8e7 | bellard | { |
3697 | 84b7b8e7 | bellard | int io_index;
|
3698 | 84b7b8e7 | bellard | uint8_t *ptr; |
3699 | 84b7b8e7 | bellard | uint64_t val; |
3700 | 84b7b8e7 | bellard | unsigned long pd; |
3701 | 84b7b8e7 | bellard | PhysPageDesc *p; |
3702 | 84b7b8e7 | bellard | |
3703 | 84b7b8e7 | bellard | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
3704 | 84b7b8e7 | bellard | if (!p) {
|
3705 | 84b7b8e7 | bellard | pd = IO_MEM_UNASSIGNED; |
3706 | 84b7b8e7 | bellard | } else {
|
3707 | 84b7b8e7 | bellard | pd = p->phys_offset; |
3708 | 84b7b8e7 | bellard | } |
3709 | 3b46e624 | ths | |
3710 | 2a4188a3 | bellard | if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
|
3711 | 2a4188a3 | bellard | !(pd & IO_MEM_ROMD)) { |
3712 | 84b7b8e7 | bellard | /* I/O case */
|
3713 | 84b7b8e7 | bellard | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
|
3714 | 8da3ff18 | pbrook | if (p)
|
3715 | 8da3ff18 | pbrook | addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset; |
3716 | 84b7b8e7 | bellard | #ifdef TARGET_WORDS_BIGENDIAN
|
3717 | 84b7b8e7 | bellard | val = (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr) << 32; |
3718 | 84b7b8e7 | bellard | val |= io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4); |
3719 | 84b7b8e7 | bellard | #else
|
3720 | 84b7b8e7 | bellard | val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
|
3721 | 84b7b8e7 | bellard | val |= (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4) << 32; |
3722 | 84b7b8e7 | bellard | #endif
|
3723 | 84b7b8e7 | bellard | } else {
|
3724 | 84b7b8e7 | bellard | /* RAM case */
|
3725 | 5579c7f3 | pbrook | ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) + |
3726 | 84b7b8e7 | bellard | (addr & ~TARGET_PAGE_MASK); |
3727 | 84b7b8e7 | bellard | val = ldq_p(ptr); |
3728 | 84b7b8e7 | bellard | } |
3729 | 84b7b8e7 | bellard | return val;
|
3730 | 84b7b8e7 | bellard | } |
3731 | 84b7b8e7 | bellard | |
3732 | aab33094 | bellard | /* XXX: optimize */
|
3733 | c227f099 | Anthony Liguori | uint32_t ldub_phys(target_phys_addr_t addr) |
3734 | aab33094 | bellard | { |
3735 | aab33094 | bellard | uint8_t val; |
3736 | aab33094 | bellard | cpu_physical_memory_read(addr, &val, 1);
|
3737 | aab33094 | bellard | return val;
|
3738 | aab33094 | bellard | } |
3739 | aab33094 | bellard | |
3740 | aab33094 | bellard | /* XXX: optimize */
|
3741 | c227f099 | Anthony Liguori | uint32_t lduw_phys(target_phys_addr_t addr) |
3742 | aab33094 | bellard | { |
3743 | aab33094 | bellard | uint16_t val; |
3744 | aab33094 | bellard | cpu_physical_memory_read(addr, (uint8_t *)&val, 2);
|
3745 | aab33094 | bellard | return tswap16(val);
|
3746 | aab33094 | bellard | } |
3747 | aab33094 | bellard | |
3748 | 8df1cd07 | bellard | /* warning: addr must be aligned. The ram page is not masked as dirty
|
3749 | 8df1cd07 | bellard | and the code inside is not invalidated. It is useful if the dirty
|
3750 | 8df1cd07 | bellard | bits are used to track modified PTEs */
|
3751 | c227f099 | Anthony Liguori | void stl_phys_notdirty(target_phys_addr_t addr, uint32_t val)
|
3752 | 8df1cd07 | bellard | { |
3753 | 8df1cd07 | bellard | int io_index;
|
3754 | 8df1cd07 | bellard | uint8_t *ptr; |
3755 | 8df1cd07 | bellard | unsigned long pd; |
3756 | 8df1cd07 | bellard | PhysPageDesc *p; |
3757 | 8df1cd07 | bellard | |
3758 | 8df1cd07 | bellard | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
3759 | 8df1cd07 | bellard | if (!p) {
|
3760 | 8df1cd07 | bellard | pd = IO_MEM_UNASSIGNED; |
3761 | 8df1cd07 | bellard | } else {
|
3762 | 8df1cd07 | bellard | pd = p->phys_offset; |
3763 | 8df1cd07 | bellard | } |
3764 | 3b46e624 | ths | |
3765 | 3a7d929e | bellard | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
|
3766 | 8df1cd07 | bellard | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
|
3767 | 8da3ff18 | pbrook | if (p)
|
3768 | 8da3ff18 | pbrook | addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset; |
3769 | 8df1cd07 | bellard | io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
|
3770 | 8df1cd07 | bellard | } else {
|
3771 | 74576198 | aliguori | unsigned long addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); |
3772 | 5579c7f3 | pbrook | ptr = qemu_get_ram_ptr(addr1); |
3773 | 8df1cd07 | bellard | stl_p(ptr, val); |
3774 | 74576198 | aliguori | |
3775 | 74576198 | aliguori | if (unlikely(in_migration)) {
|
3776 | 74576198 | aliguori | if (!cpu_physical_memory_is_dirty(addr1)) {
|
3777 | 74576198 | aliguori | /* invalidate code */
|
3778 | 74576198 | aliguori | tb_invalidate_phys_page_range(addr1, addr1 + 4, 0); |
3779 | 74576198 | aliguori | /* set dirty bit */
|
3780 | 74576198 | aliguori | phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |= |
3781 | 74576198 | aliguori | (0xff & ~CODE_DIRTY_FLAG);
|
3782 | 74576198 | aliguori | } |
3783 | 74576198 | aliguori | } |
3784 | 8df1cd07 | bellard | } |
3785 | 8df1cd07 | bellard | } |
3786 | 8df1cd07 | bellard | |
3787 | c227f099 | Anthony Liguori | void stq_phys_notdirty(target_phys_addr_t addr, uint64_t val)
|
3788 | bc98a7ef | j_mayer | { |
3789 | bc98a7ef | j_mayer | int io_index;
|
3790 | bc98a7ef | j_mayer | uint8_t *ptr; |
3791 | bc98a7ef | j_mayer | unsigned long pd; |
3792 | bc98a7ef | j_mayer | PhysPageDesc *p; |
3793 | bc98a7ef | j_mayer | |
3794 | bc98a7ef | j_mayer | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
3795 | bc98a7ef | j_mayer | if (!p) {
|
3796 | bc98a7ef | j_mayer | pd = IO_MEM_UNASSIGNED; |
3797 | bc98a7ef | j_mayer | } else {
|
3798 | bc98a7ef | j_mayer | pd = p->phys_offset; |
3799 | bc98a7ef | j_mayer | } |
3800 | 3b46e624 | ths | |
3801 | bc98a7ef | j_mayer | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
|
3802 | bc98a7ef | j_mayer | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
|
3803 | 8da3ff18 | pbrook | if (p)
|
3804 | 8da3ff18 | pbrook | addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset; |
3805 | bc98a7ef | j_mayer | #ifdef TARGET_WORDS_BIGENDIAN
|
3806 | bc98a7ef | j_mayer | io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val >> 32); |
3807 | bc98a7ef | j_mayer | io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val); |
3808 | bc98a7ef | j_mayer | #else
|
3809 | bc98a7ef | j_mayer | io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
|
3810 | bc98a7ef | j_mayer | io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val >> 32); |
3811 | bc98a7ef | j_mayer | #endif
|
3812 | bc98a7ef | j_mayer | } else {
|
3813 | 5579c7f3 | pbrook | ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) + |
3814 | bc98a7ef | j_mayer | (addr & ~TARGET_PAGE_MASK); |
3815 | bc98a7ef | j_mayer | stq_p(ptr, val); |
3816 | bc98a7ef | j_mayer | } |
3817 | bc98a7ef | j_mayer | } |
3818 | bc98a7ef | j_mayer | |
3819 | 8df1cd07 | bellard | /* warning: addr must be aligned */
|
3820 | c227f099 | Anthony Liguori | void stl_phys(target_phys_addr_t addr, uint32_t val)
|
3821 | 8df1cd07 | bellard | { |
3822 | 8df1cd07 | bellard | int io_index;
|
3823 | 8df1cd07 | bellard | uint8_t *ptr; |
3824 | 8df1cd07 | bellard | unsigned long pd; |
3825 | 8df1cd07 | bellard | PhysPageDesc *p; |
3826 | 8df1cd07 | bellard | |
3827 | 8df1cd07 | bellard | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
3828 | 8df1cd07 | bellard | if (!p) {
|
3829 | 8df1cd07 | bellard | pd = IO_MEM_UNASSIGNED; |
3830 | 8df1cd07 | bellard | } else {
|
3831 | 8df1cd07 | bellard | pd = p->phys_offset; |
3832 | 8df1cd07 | bellard | } |
3833 | 3b46e624 | ths | |
3834 | 3a7d929e | bellard | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
|
3835 | 8df1cd07 | bellard | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
|
3836 | 8da3ff18 | pbrook | if (p)
|
3837 | 8da3ff18 | pbrook | addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset; |
3838 | 8df1cd07 | bellard | io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
|
3839 | 8df1cd07 | bellard | } else {
|
3840 | 8df1cd07 | bellard | unsigned long addr1; |
3841 | 8df1cd07 | bellard | addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); |
3842 | 8df1cd07 | bellard | /* RAM case */
|
3843 | 5579c7f3 | pbrook | ptr = qemu_get_ram_ptr(addr1); |
3844 | 8df1cd07 | bellard | stl_p(ptr, val); |
3845 | 3a7d929e | bellard | if (!cpu_physical_memory_is_dirty(addr1)) {
|
3846 | 3a7d929e | bellard | /* invalidate code */
|
3847 | 3a7d929e | bellard | tb_invalidate_phys_page_range(addr1, addr1 + 4, 0); |
3848 | 3a7d929e | bellard | /* set dirty bit */
|
3849 | f23db169 | bellard | phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |= |
3850 | f23db169 | bellard | (0xff & ~CODE_DIRTY_FLAG);
|
3851 | 3a7d929e | bellard | } |
3852 | 8df1cd07 | bellard | } |
3853 | 8df1cd07 | bellard | } |
3854 | 8df1cd07 | bellard | |
3855 | aab33094 | bellard | /* XXX: optimize */
|
3856 | c227f099 | Anthony Liguori | void stb_phys(target_phys_addr_t addr, uint32_t val)
|
3857 | aab33094 | bellard | { |
3858 | aab33094 | bellard | uint8_t v = val; |
3859 | aab33094 | bellard | cpu_physical_memory_write(addr, &v, 1);
|
3860 | aab33094 | bellard | } |
3861 | aab33094 | bellard | |
3862 | aab33094 | bellard | /* XXX: optimize */
|
3863 | c227f099 | Anthony Liguori | void stw_phys(target_phys_addr_t addr, uint32_t val)
|
3864 | aab33094 | bellard | { |
3865 | aab33094 | bellard | uint16_t v = tswap16(val); |
3866 | aab33094 | bellard | cpu_physical_memory_write(addr, (const uint8_t *)&v, 2); |
3867 | aab33094 | bellard | } |
3868 | aab33094 | bellard | |
3869 | aab33094 | bellard | /* XXX: optimize */
|
3870 | c227f099 | Anthony Liguori | void stq_phys(target_phys_addr_t addr, uint64_t val)
|
3871 | aab33094 | bellard | { |
3872 | aab33094 | bellard | val = tswap64(val); |
3873 | aab33094 | bellard | cpu_physical_memory_write(addr, (const uint8_t *)&val, 8); |
3874 | aab33094 | bellard | } |
3875 | aab33094 | bellard | |
3876 | 5e2972fd | aliguori | /* virtual memory access for debug (includes writing to ROM) */
|
3877 | 5fafdf24 | ths | int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
|
3878 | b448f2f3 | bellard | uint8_t *buf, int len, int is_write) |
3879 | 13eb76e0 | bellard | { |
3880 | 13eb76e0 | bellard | int l;
|
3881 | c227f099 | Anthony Liguori | target_phys_addr_t phys_addr; |
3882 | 9b3c35e0 | j_mayer | target_ulong page; |
3883 | 13eb76e0 | bellard | |
3884 | 13eb76e0 | bellard | while (len > 0) { |
3885 | 13eb76e0 | bellard | page = addr & TARGET_PAGE_MASK; |
3886 | 13eb76e0 | bellard | phys_addr = cpu_get_phys_page_debug(env, page); |
3887 | 13eb76e0 | bellard | /* if no physical page mapped, return an error */
|
3888 | 13eb76e0 | bellard | if (phys_addr == -1) |
3889 | 13eb76e0 | bellard | return -1; |
3890 | 13eb76e0 | bellard | l = (page + TARGET_PAGE_SIZE) - addr; |
3891 | 13eb76e0 | bellard | if (l > len)
|
3892 | 13eb76e0 | bellard | l = len; |
3893 | 5e2972fd | aliguori | phys_addr += (addr & ~TARGET_PAGE_MASK); |
3894 | 5e2972fd | aliguori | if (is_write)
|
3895 | 5e2972fd | aliguori | cpu_physical_memory_write_rom(phys_addr, buf, l); |
3896 | 5e2972fd | aliguori | else
|
3897 | 5e2972fd | aliguori | cpu_physical_memory_rw(phys_addr, buf, l, is_write); |
3898 | 13eb76e0 | bellard | len -= l; |
3899 | 13eb76e0 | bellard | buf += l; |
3900 | 13eb76e0 | bellard | addr += l; |
3901 | 13eb76e0 | bellard | } |
3902 | 13eb76e0 | bellard | return 0; |
3903 | 13eb76e0 | bellard | } |
3904 | a68fe89c | Paul Brook | #endif
|
3905 | 13eb76e0 | bellard | |
3906 | 2e70f6ef | pbrook | /* in deterministic execution mode, instructions doing device I/Os
|
3907 | 2e70f6ef | pbrook | must be at the end of the TB */
|
3908 | 2e70f6ef | pbrook | void cpu_io_recompile(CPUState *env, void *retaddr) |
3909 | 2e70f6ef | pbrook | { |
3910 | 2e70f6ef | pbrook | TranslationBlock *tb; |
3911 | 2e70f6ef | pbrook | uint32_t n, cflags; |
3912 | 2e70f6ef | pbrook | target_ulong pc, cs_base; |
3913 | 2e70f6ef | pbrook | uint64_t flags; |
3914 | 2e70f6ef | pbrook | |
3915 | 2e70f6ef | pbrook | tb = tb_find_pc((unsigned long)retaddr); |
3916 | 2e70f6ef | pbrook | if (!tb) {
|
3917 | 2e70f6ef | pbrook | cpu_abort(env, "cpu_io_recompile: could not find TB for pc=%p",
|
3918 | 2e70f6ef | pbrook | retaddr); |
3919 | 2e70f6ef | pbrook | } |
3920 | 2e70f6ef | pbrook | n = env->icount_decr.u16.low + tb->icount; |
3921 | 2e70f6ef | pbrook | cpu_restore_state(tb, env, (unsigned long)retaddr, NULL); |
3922 | 2e70f6ef | pbrook | /* Calculate how many instructions had been executed before the fault
|
3923 | bf20dc07 | ths | occurred. */
|
3924 | 2e70f6ef | pbrook | n = n - env->icount_decr.u16.low; |
3925 | 2e70f6ef | pbrook | /* Generate a new TB ending on the I/O insn. */
|
3926 | 2e70f6ef | pbrook | n++; |
3927 | 2e70f6ef | pbrook | /* On MIPS and SH, delay slot instructions can only be restarted if
|
3928 | 2e70f6ef | pbrook | they were already the first instruction in the TB. If this is not
|
3929 | bf20dc07 | ths | the first instruction in a TB then re-execute the preceding
|
3930 | 2e70f6ef | pbrook | branch. */
|
3931 | 2e70f6ef | pbrook | #if defined(TARGET_MIPS)
|
3932 | 2e70f6ef | pbrook | if ((env->hflags & MIPS_HFLAG_BMASK) != 0 && n > 1) { |
3933 | 2e70f6ef | pbrook | env->active_tc.PC -= 4;
|
3934 | 2e70f6ef | pbrook | env->icount_decr.u16.low++; |
3935 | 2e70f6ef | pbrook | env->hflags &= ~MIPS_HFLAG_BMASK; |
3936 | 2e70f6ef | pbrook | } |
3937 | 2e70f6ef | pbrook | #elif defined(TARGET_SH4)
|
3938 | 2e70f6ef | pbrook | if ((env->flags & ((DELAY_SLOT | DELAY_SLOT_CONDITIONAL))) != 0 |
3939 | 2e70f6ef | pbrook | && n > 1) {
|
3940 | 2e70f6ef | pbrook | env->pc -= 2;
|
3941 | 2e70f6ef | pbrook | env->icount_decr.u16.low++; |
3942 | 2e70f6ef | pbrook | env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL); |
3943 | 2e70f6ef | pbrook | } |
3944 | 2e70f6ef | pbrook | #endif
|
3945 | 2e70f6ef | pbrook | /* This should never happen. */
|
3946 | 2e70f6ef | pbrook | if (n > CF_COUNT_MASK)
|
3947 | 2e70f6ef | pbrook | cpu_abort(env, "TB too big during recompile");
|
3948 | 2e70f6ef | pbrook | |
3949 | 2e70f6ef | pbrook | cflags = n | CF_LAST_IO; |
3950 | 2e70f6ef | pbrook | pc = tb->pc; |
3951 | 2e70f6ef | pbrook | cs_base = tb->cs_base; |
3952 | 2e70f6ef | pbrook | flags = tb->flags; |
3953 | 2e70f6ef | pbrook | tb_phys_invalidate(tb, -1);
|
3954 | 2e70f6ef | pbrook | /* FIXME: In theory this could raise an exception. In practice
|
3955 | 2e70f6ef | pbrook | we have already translated the block once so it's probably ok. */
|
3956 | 2e70f6ef | pbrook | tb_gen_code(env, pc, cs_base, flags, cflags); |
3957 | bf20dc07 | ths | /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not
|
3958 | 2e70f6ef | pbrook | the first in the TB) then we end up generating a whole new TB and
|
3959 | 2e70f6ef | pbrook | repeating the fault, which is horribly inefficient.
|
3960 | 2e70f6ef | pbrook | Better would be to execute just this insn uncached, or generate a
|
3961 | 2e70f6ef | pbrook | second new TB. */
|
3962 | 2e70f6ef | pbrook | cpu_resume_from_signal(env, NULL);
|
3963 | 2e70f6ef | pbrook | } |
3964 | 2e70f6ef | pbrook | |
3965 | b3755a91 | Paul Brook | #if !defined(CONFIG_USER_ONLY)
|
3966 | b3755a91 | Paul Brook | |
3967 | e3db7226 | bellard | void dump_exec_info(FILE *f,
|
3968 | e3db7226 | bellard | int (*cpu_fprintf)(FILE *f, const char *fmt, ...)) |
3969 | e3db7226 | bellard | { |
3970 | e3db7226 | bellard | int i, target_code_size, max_target_code_size;
|
3971 | e3db7226 | bellard | int direct_jmp_count, direct_jmp2_count, cross_page;
|
3972 | e3db7226 | bellard | TranslationBlock *tb; |
3973 | 3b46e624 | ths | |
3974 | e3db7226 | bellard | target_code_size = 0;
|
3975 | e3db7226 | bellard | max_target_code_size = 0;
|
3976 | e3db7226 | bellard | cross_page = 0;
|
3977 | e3db7226 | bellard | direct_jmp_count = 0;
|
3978 | e3db7226 | bellard | direct_jmp2_count = 0;
|
3979 | e3db7226 | bellard | for(i = 0; i < nb_tbs; i++) { |
3980 | e3db7226 | bellard | tb = &tbs[i]; |
3981 | e3db7226 | bellard | target_code_size += tb->size; |
3982 | e3db7226 | bellard | if (tb->size > max_target_code_size)
|
3983 | e3db7226 | bellard | max_target_code_size = tb->size; |
3984 | e3db7226 | bellard | if (tb->page_addr[1] != -1) |
3985 | e3db7226 | bellard | cross_page++; |
3986 | e3db7226 | bellard | if (tb->tb_next_offset[0] != 0xffff) { |
3987 | e3db7226 | bellard | direct_jmp_count++; |
3988 | e3db7226 | bellard | if (tb->tb_next_offset[1] != 0xffff) { |
3989 | e3db7226 | bellard | direct_jmp2_count++; |
3990 | e3db7226 | bellard | } |
3991 | e3db7226 | bellard | } |
3992 | e3db7226 | bellard | } |
3993 | e3db7226 | bellard | /* XXX: avoid using doubles ? */
|
3994 | 57fec1fe | bellard | cpu_fprintf(f, "Translation buffer state:\n");
|
3995 | 26a5f13b | bellard | cpu_fprintf(f, "gen code size %ld/%ld\n",
|
3996 | 26a5f13b | bellard | code_gen_ptr - code_gen_buffer, code_gen_buffer_max_size); |
3997 | 26a5f13b | bellard | cpu_fprintf(f, "TB count %d/%d\n",
|
3998 | 26a5f13b | bellard | nb_tbs, code_gen_max_blocks); |
3999 | 5fafdf24 | ths | cpu_fprintf(f, "TB avg target size %d max=%d bytes\n",
|
4000 | e3db7226 | bellard | nb_tbs ? target_code_size / nb_tbs : 0,
|
4001 | e3db7226 | bellard | max_target_code_size); |
4002 | 5fafdf24 | ths | cpu_fprintf(f, "TB avg host size %d bytes (expansion ratio: %0.1f)\n",
|
4003 | e3db7226 | bellard | nb_tbs ? (code_gen_ptr - code_gen_buffer) / nb_tbs : 0,
|
4004 | e3db7226 | bellard | target_code_size ? (double) (code_gen_ptr - code_gen_buffer) / target_code_size : 0); |
4005 | 5fafdf24 | ths | cpu_fprintf(f, "cross page TB count %d (%d%%)\n",
|
4006 | 5fafdf24 | ths | cross_page, |
4007 | e3db7226 | bellard | nb_tbs ? (cross_page * 100) / nb_tbs : 0); |
4008 | e3db7226 | bellard | cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
|
4009 | 5fafdf24 | ths | direct_jmp_count, |
4010 | e3db7226 | bellard | nb_tbs ? (direct_jmp_count * 100) / nb_tbs : 0, |
4011 | e3db7226 | bellard | direct_jmp2_count, |
4012 | e3db7226 | bellard | nb_tbs ? (direct_jmp2_count * 100) / nb_tbs : 0); |
4013 | 57fec1fe | bellard | cpu_fprintf(f, "\nStatistics:\n");
|
4014 | e3db7226 | bellard | cpu_fprintf(f, "TB flush count %d\n", tb_flush_count);
|
4015 | e3db7226 | bellard | cpu_fprintf(f, "TB invalidate count %d\n", tb_phys_invalidate_count);
|
4016 | e3db7226 | bellard | cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count);
|
4017 | b67d9a52 | bellard | tcg_dump_info(f, cpu_fprintf); |
4018 | e3db7226 | bellard | } |
4019 | e3db7226 | bellard | |
4020 | 61382a50 | bellard | #define MMUSUFFIX _cmmu
|
4021 | 61382a50 | bellard | #define GETPC() NULL |
4022 | 61382a50 | bellard | #define env cpu_single_env
|
4023 | b769d8fe | bellard | #define SOFTMMU_CODE_ACCESS
|
4024 | 61382a50 | bellard | |
4025 | 61382a50 | bellard | #define SHIFT 0 |
4026 | 61382a50 | bellard | #include "softmmu_template.h" |
4027 | 61382a50 | bellard | |
4028 | 61382a50 | bellard | #define SHIFT 1 |
4029 | 61382a50 | bellard | #include "softmmu_template.h" |
4030 | 61382a50 | bellard | |
4031 | 61382a50 | bellard | #define SHIFT 2 |
4032 | 61382a50 | bellard | #include "softmmu_template.h" |
4033 | 61382a50 | bellard | |
4034 | 61382a50 | bellard | #define SHIFT 3 |
4035 | 61382a50 | bellard | #include "softmmu_template.h" |
4036 | 61382a50 | bellard | |
4037 | 61382a50 | bellard | #undef env
|
4038 | 61382a50 | bellard | |
4039 | 61382a50 | bellard | #endif |