]>
Commit | Line | Data |
---|---|---|
54936004 | 1 | /* |
fd6ce8f6 | 2 | * virtual page mapping and translated block handling |
54936004 FB |
3 | * |
4 | * Copyright (c) 2003 Fabrice Bellard | |
5 | * | |
6 | * This library is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License as published by the Free Software Foundation; either | |
9 | * version 2 of the License, or (at your option) any later version. | |
10 | * | |
11 | * This library is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU Lesser General Public | |
17 | * License along with this library; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | */ | |
20 | #include <stdlib.h> | |
21 | #include <stdio.h> | |
22 | #include <stdarg.h> | |
23 | #include <string.h> | |
24 | #include <errno.h> | |
25 | #include <unistd.h> | |
26 | #include <inttypes.h> | |
fd6ce8f6 | 27 | #include <sys/mman.h> |
54936004 | 28 | |
ea041c0e | 29 | #include "config.h" |
6180a181 FB |
30 | #include "cpu.h" |
31 | #include "exec-all.h" | |
54936004 | 32 | |
fd6ce8f6 | 33 | //#define DEBUG_TB_INVALIDATE |
66e85a21 | 34 | //#define DEBUG_FLUSH |
fd6ce8f6 FB |
35 | |
36 | /* make various TB consistency checks */ | |
37 | //#define DEBUG_TB_CHECK | |
38 | ||
39 | /* threshold to flush the translated code buffer */ | |
40 | #define CODE_GEN_BUFFER_MAX_SIZE (CODE_GEN_BUFFER_SIZE - CODE_GEN_MAX_SIZE) | |
41 | ||
42 | #define CODE_GEN_MAX_BLOCKS (CODE_GEN_BUFFER_SIZE / 64) | |
43 | ||
44 | TranslationBlock tbs[CODE_GEN_MAX_BLOCKS]; | |
45 | TranslationBlock *tb_hash[CODE_GEN_HASH_SIZE]; | |
46 | int nb_tbs; | |
eb51d102 FB |
47 | /* any access to the tbs or the page table must use this lock */ |
48 | spinlock_t tb_lock = SPIN_LOCK_UNLOCKED; | |
fd6ce8f6 FB |
49 | |
50 | uint8_t code_gen_buffer[CODE_GEN_BUFFER_SIZE]; | |
51 | uint8_t *code_gen_ptr; | |
52 | ||
54936004 FB |
53 | /* XXX: pack the flags in the low bits of the pointer ? */ |
54 | typedef struct PageDesc { | |
54936004 | 55 | unsigned long flags; |
fd6ce8f6 | 56 | TranslationBlock *first_tb; |
54936004 FB |
57 | } PageDesc; |
58 | ||
59 | #define L2_BITS 10 | |
60 | #define L1_BITS (32 - L2_BITS - TARGET_PAGE_BITS) | |
61 | ||
62 | #define L1_SIZE (1 << L1_BITS) | |
63 | #define L2_SIZE (1 << L2_BITS) | |
64 | ||
fd6ce8f6 | 65 | static void tb_invalidate_page(unsigned long address); |
33417e70 | 66 | static void io_mem_init(void); |
fd6ce8f6 | 67 | |
54936004 FB |
68 | unsigned long real_host_page_size; |
69 | unsigned long host_page_bits; | |
70 | unsigned long host_page_size; | |
71 | unsigned long host_page_mask; | |
72 | ||
73 | static PageDesc *l1_map[L1_SIZE]; | |
74 | ||
33417e70 FB |
75 | /* io memory support */ |
76 | static unsigned long *l1_physmap[L1_SIZE]; | |
77 | CPUWriteMemoryFunc *io_mem_write[IO_MEM_NB_ENTRIES][4]; | |
78 | CPUReadMemoryFunc *io_mem_read[IO_MEM_NB_ENTRIES][4]; | |
79 | static int io_mem_nb; | |
80 | ||
34865134 FB |
81 | /* log support */ |
82 | char *logfilename = "/tmp/qemu.log"; | |
83 | FILE *logfile; | |
84 | int loglevel; | |
85 | ||
b346ff46 | 86 | static void page_init(void) |
54936004 FB |
87 | { |
88 | /* NOTE: we can always suppose that host_page_size >= | |
89 | TARGET_PAGE_SIZE */ | |
90 | real_host_page_size = getpagesize(); | |
91 | if (host_page_size == 0) | |
92 | host_page_size = real_host_page_size; | |
93 | if (host_page_size < TARGET_PAGE_SIZE) | |
94 | host_page_size = TARGET_PAGE_SIZE; | |
95 | host_page_bits = 0; | |
96 | while ((1 << host_page_bits) < host_page_size) | |
97 | host_page_bits++; | |
98 | host_page_mask = ~(host_page_size - 1); | |
99 | } | |
100 | ||
101 | /* dump memory mappings */ | |
102 | void page_dump(FILE *f) | |
103 | { | |
104 | unsigned long start, end; | |
105 | int i, j, prot, prot1; | |
106 | PageDesc *p; | |
107 | ||
108 | fprintf(f, "%-8s %-8s %-8s %s\n", | |
109 | "start", "end", "size", "prot"); | |
110 | start = -1; | |
111 | end = -1; | |
112 | prot = 0; | |
113 | for(i = 0; i <= L1_SIZE; i++) { | |
114 | if (i < L1_SIZE) | |
115 | p = l1_map[i]; | |
116 | else | |
117 | p = NULL; | |
118 | for(j = 0;j < L2_SIZE; j++) { | |
119 | if (!p) | |
120 | prot1 = 0; | |
121 | else | |
122 | prot1 = p[j].flags; | |
123 | if (prot1 != prot) { | |
124 | end = (i << (32 - L1_BITS)) | (j << TARGET_PAGE_BITS); | |
125 | if (start != -1) { | |
126 | fprintf(f, "%08lx-%08lx %08lx %c%c%c\n", | |
127 | start, end, end - start, | |
128 | prot & PAGE_READ ? 'r' : '-', | |
129 | prot & PAGE_WRITE ? 'w' : '-', | |
130 | prot & PAGE_EXEC ? 'x' : '-'); | |
131 | } | |
132 | if (prot1 != 0) | |
133 | start = end; | |
134 | else | |
135 | start = -1; | |
136 | prot = prot1; | |
137 | } | |
138 | if (!p) | |
139 | break; | |
140 | } | |
141 | } | |
142 | } | |
143 | ||
fd6ce8f6 | 144 | static inline PageDesc *page_find_alloc(unsigned int index) |
54936004 | 145 | { |
54936004 FB |
146 | PageDesc **lp, *p; |
147 | ||
54936004 FB |
148 | lp = &l1_map[index >> L2_BITS]; |
149 | p = *lp; | |
150 | if (!p) { | |
151 | /* allocate if not found */ | |
152 | p = malloc(sizeof(PageDesc) * L2_SIZE); | |
fd6ce8f6 | 153 | memset(p, 0, sizeof(PageDesc) * L2_SIZE); |
54936004 FB |
154 | *lp = p; |
155 | } | |
156 | return p + (index & (L2_SIZE - 1)); | |
157 | } | |
158 | ||
fd6ce8f6 | 159 | static inline PageDesc *page_find(unsigned int index) |
54936004 | 160 | { |
54936004 FB |
161 | PageDesc *p; |
162 | ||
54936004 FB |
163 | p = l1_map[index >> L2_BITS]; |
164 | if (!p) | |
165 | return 0; | |
fd6ce8f6 FB |
166 | return p + (index & (L2_SIZE - 1)); |
167 | } | |
168 | ||
169 | int page_get_flags(unsigned long address) | |
170 | { | |
171 | PageDesc *p; | |
172 | ||
173 | p = page_find(address >> TARGET_PAGE_BITS); | |
174 | if (!p) | |
175 | return 0; | |
176 | return p->flags; | |
54936004 FB |
177 | } |
178 | ||
fd6ce8f6 FB |
179 | /* modify the flags of a page and invalidate the code if |
180 | necessary. The flag PAGE_WRITE_ORG is positionned automatically | |
181 | depending on PAGE_WRITE */ | |
54936004 FB |
182 | void page_set_flags(unsigned long start, unsigned long end, int flags) |
183 | { | |
184 | PageDesc *p; | |
185 | unsigned long addr; | |
186 | ||
187 | start = start & TARGET_PAGE_MASK; | |
188 | end = TARGET_PAGE_ALIGN(end); | |
fd6ce8f6 FB |
189 | if (flags & PAGE_WRITE) |
190 | flags |= PAGE_WRITE_ORG; | |
eb51d102 | 191 | spin_lock(&tb_lock); |
54936004 | 192 | for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) { |
fd6ce8f6 FB |
193 | p = page_find_alloc(addr >> TARGET_PAGE_BITS); |
194 | /* if the write protection is set, then we invalidate the code | |
195 | inside */ | |
196 | if (!(p->flags & PAGE_WRITE) && | |
197 | (flags & PAGE_WRITE) && | |
198 | p->first_tb) { | |
199 | tb_invalidate_page(addr); | |
200 | } | |
54936004 FB |
201 | p->flags = flags; |
202 | } | |
eb51d102 | 203 | spin_unlock(&tb_lock); |
54936004 | 204 | } |
fd6ce8f6 | 205 | |
b346ff46 | 206 | void cpu_exec_init(void) |
fd6ce8f6 FB |
207 | { |
208 | if (!code_gen_ptr) { | |
209 | code_gen_ptr = code_gen_buffer; | |
b346ff46 | 210 | page_init(); |
33417e70 | 211 | io_mem_init(); |
fd6ce8f6 FB |
212 | } |
213 | } | |
214 | ||
215 | /* set to NULL all the 'first_tb' fields in all PageDescs */ | |
216 | static void page_flush_tb(void) | |
217 | { | |
218 | int i, j; | |
219 | PageDesc *p; | |
220 | ||
221 | for(i = 0; i < L1_SIZE; i++) { | |
222 | p = l1_map[i]; | |
223 | if (p) { | |
224 | for(j = 0; j < L2_SIZE; j++) | |
225 | p[j].first_tb = NULL; | |
226 | } | |
227 | } | |
228 | } | |
229 | ||
230 | /* flush all the translation blocks */ | |
d4e8164f | 231 | /* XXX: tb_flush is currently not thread safe */ |
fd6ce8f6 FB |
232 | void tb_flush(void) |
233 | { | |
234 | int i; | |
235 | #ifdef DEBUG_FLUSH | |
236 | printf("qemu: flush code_size=%d nb_tbs=%d avg_tb_size=%d\n", | |
237 | code_gen_ptr - code_gen_buffer, | |
238 | nb_tbs, | |
239 | (code_gen_ptr - code_gen_buffer) / nb_tbs); | |
240 | #endif | |
241 | nb_tbs = 0; | |
242 | for(i = 0;i < CODE_GEN_HASH_SIZE; i++) | |
243 | tb_hash[i] = NULL; | |
244 | page_flush_tb(); | |
245 | code_gen_ptr = code_gen_buffer; | |
d4e8164f FB |
246 | /* XXX: flush processor icache at this point if cache flush is |
247 | expensive */ | |
fd6ce8f6 FB |
248 | } |
249 | ||
250 | #ifdef DEBUG_TB_CHECK | |
251 | ||
252 | static void tb_invalidate_check(unsigned long address) | |
253 | { | |
254 | TranslationBlock *tb; | |
255 | int i; | |
256 | address &= TARGET_PAGE_MASK; | |
257 | for(i = 0;i < CODE_GEN_HASH_SIZE; i++) { | |
258 | for(tb = tb_hash[i]; tb != NULL; tb = tb->hash_next) { | |
259 | if (!(address + TARGET_PAGE_SIZE <= tb->pc || | |
260 | address >= tb->pc + tb->size)) { | |
261 | printf("ERROR invalidate: address=%08lx PC=%08lx size=%04x\n", | |
262 | address, tb->pc, tb->size); | |
263 | } | |
264 | } | |
265 | } | |
266 | } | |
267 | ||
268 | /* verify that all the pages have correct rights for code */ | |
269 | static void tb_page_check(void) | |
270 | { | |
271 | TranslationBlock *tb; | |
272 | int i, flags1, flags2; | |
273 | ||
274 | for(i = 0;i < CODE_GEN_HASH_SIZE; i++) { | |
275 | for(tb = tb_hash[i]; tb != NULL; tb = tb->hash_next) { | |
276 | flags1 = page_get_flags(tb->pc); | |
277 | flags2 = page_get_flags(tb->pc + tb->size - 1); | |
278 | if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) { | |
279 | printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n", | |
280 | tb->pc, tb->size, flags1, flags2); | |
281 | } | |
282 | } | |
283 | } | |
284 | } | |
285 | ||
d4e8164f FB |
286 | void tb_jmp_check(TranslationBlock *tb) |
287 | { | |
288 | TranslationBlock *tb1; | |
289 | unsigned int n1; | |
290 | ||
291 | /* suppress any remaining jumps to this TB */ | |
292 | tb1 = tb->jmp_first; | |
293 | for(;;) { | |
294 | n1 = (long)tb1 & 3; | |
295 | tb1 = (TranslationBlock *)((long)tb1 & ~3); | |
296 | if (n1 == 2) | |
297 | break; | |
298 | tb1 = tb1->jmp_next[n1]; | |
299 | } | |
300 | /* check end of list */ | |
301 | if (tb1 != tb) { | |
302 | printf("ERROR: jmp_list from 0x%08lx\n", (long)tb); | |
303 | } | |
304 | } | |
305 | ||
fd6ce8f6 FB |
306 | #endif |
307 | ||
308 | /* invalidate one TB */ | |
309 | static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb, | |
310 | int next_offset) | |
311 | { | |
312 | TranslationBlock *tb1; | |
313 | for(;;) { | |
314 | tb1 = *ptb; | |
315 | if (tb1 == tb) { | |
316 | *ptb = *(TranslationBlock **)((char *)tb1 + next_offset); | |
317 | break; | |
318 | } | |
319 | ptb = (TranslationBlock **)((char *)tb1 + next_offset); | |
320 | } | |
321 | } | |
322 | ||
d4e8164f FB |
323 | static inline void tb_jmp_remove(TranslationBlock *tb, int n) |
324 | { | |
325 | TranslationBlock *tb1, **ptb; | |
326 | unsigned int n1; | |
327 | ||
328 | ptb = &tb->jmp_next[n]; | |
329 | tb1 = *ptb; | |
330 | if (tb1) { | |
331 | /* find tb(n) in circular list */ | |
332 | for(;;) { | |
333 | tb1 = *ptb; | |
334 | n1 = (long)tb1 & 3; | |
335 | tb1 = (TranslationBlock *)((long)tb1 & ~3); | |
336 | if (n1 == n && tb1 == tb) | |
337 | break; | |
338 | if (n1 == 2) { | |
339 | ptb = &tb1->jmp_first; | |
340 | } else { | |
341 | ptb = &tb1->jmp_next[n1]; | |
342 | } | |
343 | } | |
344 | /* now we can suppress tb(n) from the list */ | |
345 | *ptb = tb->jmp_next[n]; | |
346 | ||
347 | tb->jmp_next[n] = NULL; | |
348 | } | |
349 | } | |
350 | ||
351 | /* reset the jump entry 'n' of a TB so that it is not chained to | |
352 | another TB */ | |
353 | static inline void tb_reset_jump(TranslationBlock *tb, int n) | |
354 | { | |
355 | tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n])); | |
356 | } | |
357 | ||
fd6ce8f6 FB |
358 | static inline void tb_invalidate(TranslationBlock *tb, int parity) |
359 | { | |
360 | PageDesc *p; | |
361 | unsigned int page_index1, page_index2; | |
d4e8164f FB |
362 | unsigned int h, n1; |
363 | TranslationBlock *tb1, *tb2; | |
364 | ||
fd6ce8f6 FB |
365 | /* remove the TB from the hash list */ |
366 | h = tb_hash_func(tb->pc); | |
367 | tb_remove(&tb_hash[h], tb, | |
368 | offsetof(TranslationBlock, hash_next)); | |
369 | /* remove the TB from the page list */ | |
370 | page_index1 = tb->pc >> TARGET_PAGE_BITS; | |
371 | if ((page_index1 & 1) == parity) { | |
372 | p = page_find(page_index1); | |
373 | tb_remove(&p->first_tb, tb, | |
374 | offsetof(TranslationBlock, page_next[page_index1 & 1])); | |
375 | } | |
376 | page_index2 = (tb->pc + tb->size - 1) >> TARGET_PAGE_BITS; | |
377 | if ((page_index2 & 1) == parity) { | |
378 | p = page_find(page_index2); | |
379 | tb_remove(&p->first_tb, tb, | |
380 | offsetof(TranslationBlock, page_next[page_index2 & 1])); | |
381 | } | |
d4e8164f FB |
382 | |
383 | /* suppress this TB from the two jump lists */ | |
384 | tb_jmp_remove(tb, 0); | |
385 | tb_jmp_remove(tb, 1); | |
386 | ||
387 | /* suppress any remaining jumps to this TB */ | |
388 | tb1 = tb->jmp_first; | |
389 | for(;;) { | |
390 | n1 = (long)tb1 & 3; | |
391 | if (n1 == 2) | |
392 | break; | |
393 | tb1 = (TranslationBlock *)((long)tb1 & ~3); | |
394 | tb2 = tb1->jmp_next[n1]; | |
395 | tb_reset_jump(tb1, n1); | |
396 | tb1->jmp_next[n1] = NULL; | |
397 | tb1 = tb2; | |
398 | } | |
399 | tb->jmp_first = (TranslationBlock *)((long)tb | 2); /* fail safe */ | |
fd6ce8f6 FB |
400 | } |
401 | ||
402 | /* invalidate all TBs which intersect with the target page starting at addr */ | |
403 | static void tb_invalidate_page(unsigned long address) | |
404 | { | |
405 | TranslationBlock *tb_next, *tb; | |
406 | unsigned int page_index; | |
407 | int parity1, parity2; | |
408 | PageDesc *p; | |
409 | #ifdef DEBUG_TB_INVALIDATE | |
410 | printf("tb_invalidate_page: %lx\n", address); | |
411 | #endif | |
412 | ||
413 | page_index = address >> TARGET_PAGE_BITS; | |
414 | p = page_find(page_index); | |
415 | if (!p) | |
416 | return; | |
417 | tb = p->first_tb; | |
418 | parity1 = page_index & 1; | |
419 | parity2 = parity1 ^ 1; | |
420 | while (tb != NULL) { | |
421 | tb_next = tb->page_next[parity1]; | |
422 | tb_invalidate(tb, parity2); | |
423 | tb = tb_next; | |
424 | } | |
425 | p->first_tb = NULL; | |
426 | } | |
427 | ||
428 | /* add the tb in the target page and protect it if necessary */ | |
429 | static inline void tb_alloc_page(TranslationBlock *tb, unsigned int page_index) | |
430 | { | |
431 | PageDesc *p; | |
432 | unsigned long host_start, host_end, addr, page_addr; | |
433 | int prot; | |
434 | ||
435 | p = page_find_alloc(page_index); | |
436 | tb->page_next[page_index & 1] = p->first_tb; | |
437 | p->first_tb = tb; | |
438 | if (p->flags & PAGE_WRITE) { | |
439 | /* force the host page as non writable (writes will have a | |
440 | page fault + mprotect overhead) */ | |
441 | page_addr = (page_index << TARGET_PAGE_BITS); | |
442 | host_start = page_addr & host_page_mask; | |
443 | host_end = host_start + host_page_size; | |
444 | prot = 0; | |
445 | for(addr = host_start; addr < host_end; addr += TARGET_PAGE_SIZE) | |
446 | prot |= page_get_flags(addr); | |
447 | mprotect((void *)host_start, host_page_size, | |
448 | (prot & PAGE_BITS) & ~PAGE_WRITE); | |
449 | #ifdef DEBUG_TB_INVALIDATE | |
450 | printf("protecting code page: 0x%08lx\n", | |
451 | host_start); | |
452 | #endif | |
453 | p->flags &= ~PAGE_WRITE; | |
454 | #ifdef DEBUG_TB_CHECK | |
455 | tb_page_check(); | |
456 | #endif | |
457 | } | |
458 | } | |
459 | ||
460 | /* Allocate a new translation block. Flush the translation buffer if | |
461 | too many translation blocks or too much generated code. */ | |
d4e8164f | 462 | TranslationBlock *tb_alloc(unsigned long pc) |
fd6ce8f6 FB |
463 | { |
464 | TranslationBlock *tb; | |
fd6ce8f6 FB |
465 | |
466 | if (nb_tbs >= CODE_GEN_MAX_BLOCKS || | |
467 | (code_gen_ptr - code_gen_buffer) >= CODE_GEN_BUFFER_MAX_SIZE) | |
d4e8164f | 468 | return NULL; |
fd6ce8f6 FB |
469 | tb = &tbs[nb_tbs++]; |
470 | tb->pc = pc; | |
d4e8164f FB |
471 | return tb; |
472 | } | |
473 | ||
474 | /* link the tb with the other TBs */ | |
475 | void tb_link(TranslationBlock *tb) | |
476 | { | |
477 | unsigned int page_index1, page_index2; | |
fd6ce8f6 FB |
478 | |
479 | /* add in the page list */ | |
d4e8164f | 480 | page_index1 = tb->pc >> TARGET_PAGE_BITS; |
fd6ce8f6 | 481 | tb_alloc_page(tb, page_index1); |
d4e8164f | 482 | page_index2 = (tb->pc + tb->size - 1) >> TARGET_PAGE_BITS; |
fd6ce8f6 FB |
483 | if (page_index2 != page_index1) { |
484 | tb_alloc_page(tb, page_index2); | |
485 | } | |
d4e8164f FB |
486 | tb->jmp_first = (TranslationBlock *)((long)tb | 2); |
487 | tb->jmp_next[0] = NULL; | |
488 | tb->jmp_next[1] = NULL; | |
489 | ||
490 | /* init original jump addresses */ | |
491 | if (tb->tb_next_offset[0] != 0xffff) | |
492 | tb_reset_jump(tb, 0); | |
493 | if (tb->tb_next_offset[1] != 0xffff) | |
494 | tb_reset_jump(tb, 1); | |
fd6ce8f6 FB |
495 | } |
496 | ||
497 | /* called from signal handler: invalidate the code and unprotect the | |
498 | page. Return TRUE if the fault was succesfully handled. */ | |
499 | int page_unprotect(unsigned long address) | |
500 | { | |
1565b7bc FB |
501 | unsigned int page_index, prot, pindex; |
502 | PageDesc *p, *p1; | |
fd6ce8f6 FB |
503 | unsigned long host_start, host_end, addr; |
504 | ||
1565b7bc FB |
505 | host_start = address & host_page_mask; |
506 | page_index = host_start >> TARGET_PAGE_BITS; | |
507 | p1 = page_find(page_index); | |
508 | if (!p1) | |
fd6ce8f6 | 509 | return 0; |
1565b7bc FB |
510 | host_end = host_start + host_page_size; |
511 | p = p1; | |
512 | prot = 0; | |
513 | for(addr = host_start;addr < host_end; addr += TARGET_PAGE_SIZE) { | |
514 | prot |= p->flags; | |
515 | p++; | |
516 | } | |
517 | /* if the page was really writable, then we change its | |
518 | protection back to writable */ | |
519 | if (prot & PAGE_WRITE_ORG) { | |
fd6ce8f6 FB |
520 | mprotect((void *)host_start, host_page_size, |
521 | (prot & PAGE_BITS) | PAGE_WRITE); | |
1565b7bc FB |
522 | pindex = (address - host_start) >> TARGET_PAGE_BITS; |
523 | p1[pindex].flags |= PAGE_WRITE; | |
fd6ce8f6 FB |
524 | /* and since the content will be modified, we must invalidate |
525 | the corresponding translated code. */ | |
526 | tb_invalidate_page(address); | |
527 | #ifdef DEBUG_TB_CHECK | |
528 | tb_invalidate_check(address); | |
529 | #endif | |
530 | return 1; | |
531 | } else { | |
532 | return 0; | |
533 | } | |
534 | } | |
535 | ||
536 | /* call this function when system calls directly modify a memory area */ | |
537 | void page_unprotect_range(uint8_t *data, unsigned long data_size) | |
538 | { | |
539 | unsigned long start, end, addr; | |
540 | ||
541 | start = (unsigned long)data; | |
542 | end = start + data_size; | |
543 | start &= TARGET_PAGE_MASK; | |
544 | end = TARGET_PAGE_ALIGN(end); | |
545 | for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) { | |
546 | page_unprotect(addr); | |
547 | } | |
548 | } | |
a513fe19 FB |
549 | |
550 | /* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr < | |
551 | tb[1].tc_ptr. Return NULL if not found */ | |
552 | TranslationBlock *tb_find_pc(unsigned long tc_ptr) | |
553 | { | |
554 | int m_min, m_max, m; | |
555 | unsigned long v; | |
556 | TranslationBlock *tb; | |
557 | ||
558 | if (nb_tbs <= 0) | |
559 | return NULL; | |
560 | if (tc_ptr < (unsigned long)code_gen_buffer || | |
561 | tc_ptr >= (unsigned long)code_gen_ptr) | |
562 | return NULL; | |
563 | /* binary search (cf Knuth) */ | |
564 | m_min = 0; | |
565 | m_max = nb_tbs - 1; | |
566 | while (m_min <= m_max) { | |
567 | m = (m_min + m_max) >> 1; | |
568 | tb = &tbs[m]; | |
569 | v = (unsigned long)tb->tc_ptr; | |
570 | if (v == tc_ptr) | |
571 | return tb; | |
572 | else if (tc_ptr < v) { | |
573 | m_max = m - 1; | |
574 | } else { | |
575 | m_min = m + 1; | |
576 | } | |
577 | } | |
578 | return &tbs[m_max]; | |
579 | } | |
7501267e | 580 | |
ea041c0e FB |
581 | static void tb_reset_jump_recursive(TranslationBlock *tb); |
582 | ||
583 | static inline void tb_reset_jump_recursive2(TranslationBlock *tb, int n) | |
584 | { | |
585 | TranslationBlock *tb1, *tb_next, **ptb; | |
586 | unsigned int n1; | |
587 | ||
588 | tb1 = tb->jmp_next[n]; | |
589 | if (tb1 != NULL) { | |
590 | /* find head of list */ | |
591 | for(;;) { | |
592 | n1 = (long)tb1 & 3; | |
593 | tb1 = (TranslationBlock *)((long)tb1 & ~3); | |
594 | if (n1 == 2) | |
595 | break; | |
596 | tb1 = tb1->jmp_next[n1]; | |
597 | } | |
598 | /* we are now sure now that tb jumps to tb1 */ | |
599 | tb_next = tb1; | |
600 | ||
601 | /* remove tb from the jmp_first list */ | |
602 | ptb = &tb_next->jmp_first; | |
603 | for(;;) { | |
604 | tb1 = *ptb; | |
605 | n1 = (long)tb1 & 3; | |
606 | tb1 = (TranslationBlock *)((long)tb1 & ~3); | |
607 | if (n1 == n && tb1 == tb) | |
608 | break; | |
609 | ptb = &tb1->jmp_next[n1]; | |
610 | } | |
611 | *ptb = tb->jmp_next[n]; | |
612 | tb->jmp_next[n] = NULL; | |
613 | ||
614 | /* suppress the jump to next tb in generated code */ | |
615 | tb_reset_jump(tb, n); | |
616 | ||
617 | /* suppress jumps in the tb on which we could have jump */ | |
618 | tb_reset_jump_recursive(tb_next); | |
619 | } | |
620 | } | |
621 | ||
622 | static void tb_reset_jump_recursive(TranslationBlock *tb) | |
623 | { | |
624 | tb_reset_jump_recursive2(tb, 0); | |
625 | tb_reset_jump_recursive2(tb, 1); | |
626 | } | |
627 | ||
c33a346e FB |
628 | /* add a breakpoint. EXCP_DEBUG is returned by the CPU loop if a |
629 | breakpoint is reached */ | |
4c3a88a2 FB |
630 | int cpu_breakpoint_insert(CPUState *env, uint32_t pc) |
631 | { | |
632 | #if defined(TARGET_I386) | |
633 | int i; | |
634 | ||
635 | for(i = 0; i < env->nb_breakpoints; i++) { | |
636 | if (env->breakpoints[i] == pc) | |
637 | return 0; | |
638 | } | |
639 | ||
640 | if (env->nb_breakpoints >= MAX_BREAKPOINTS) | |
641 | return -1; | |
642 | env->breakpoints[env->nb_breakpoints++] = pc; | |
643 | tb_invalidate_page(pc); | |
644 | return 0; | |
645 | #else | |
646 | return -1; | |
647 | #endif | |
648 | } | |
649 | ||
650 | /* remove a breakpoint */ | |
651 | int cpu_breakpoint_remove(CPUState *env, uint32_t pc) | |
652 | { | |
653 | #if defined(TARGET_I386) | |
654 | int i; | |
655 | for(i = 0; i < env->nb_breakpoints; i++) { | |
656 | if (env->breakpoints[i] == pc) | |
657 | goto found; | |
658 | } | |
659 | return -1; | |
660 | found: | |
661 | memmove(&env->breakpoints[i], &env->breakpoints[i + 1], | |
662 | (env->nb_breakpoints - (i + 1)) * sizeof(env->breakpoints[0])); | |
663 | env->nb_breakpoints--; | |
664 | tb_invalidate_page(pc); | |
665 | return 0; | |
666 | #else | |
667 | return -1; | |
668 | #endif | |
669 | } | |
670 | ||
c33a346e FB |
671 | /* enable or disable single step mode. EXCP_DEBUG is returned by the |
672 | CPU loop after each instruction */ | |
673 | void cpu_single_step(CPUState *env, int enabled) | |
674 | { | |
675 | #if defined(TARGET_I386) | |
676 | if (env->singlestep_enabled != enabled) { | |
677 | env->singlestep_enabled = enabled; | |
678 | /* must flush all the translated code to avoid inconsistancies */ | |
679 | tb_flush(); | |
680 | } | |
681 | #endif | |
682 | } | |
683 | ||
34865134 FB |
684 | /* enable or disable low levels log */ |
685 | void cpu_set_log(int log_flags) | |
686 | { | |
687 | loglevel = log_flags; | |
688 | if (loglevel && !logfile) { | |
689 | logfile = fopen(logfilename, "w"); | |
690 | if (!logfile) { | |
691 | perror(logfilename); | |
692 | _exit(1); | |
693 | } | |
694 | setvbuf(logfile, NULL, _IOLBF, 0); | |
695 | } | |
696 | } | |
697 | ||
698 | void cpu_set_log_filename(const char *filename) | |
699 | { | |
700 | logfilename = strdup(filename); | |
701 | } | |
c33a346e | 702 | |
68a79315 FB |
703 | /* mask must never be zero */ |
704 | void cpu_interrupt(CPUState *env, int mask) | |
ea041c0e FB |
705 | { |
706 | TranslationBlock *tb; | |
68a79315 FB |
707 | |
708 | env->interrupt_request |= mask; | |
ea041c0e FB |
709 | /* if the cpu is currently executing code, we must unlink it and |
710 | all the potentially executing TB */ | |
711 | tb = env->current_tb; | |
712 | if (tb) { | |
713 | tb_reset_jump_recursive(tb); | |
714 | } | |
715 | } | |
716 | ||
717 | ||
7501267e FB |
718 | void cpu_abort(CPUState *env, const char *fmt, ...) |
719 | { | |
720 | va_list ap; | |
721 | ||
722 | va_start(ap, fmt); | |
723 | fprintf(stderr, "qemu: fatal: "); | |
724 | vfprintf(stderr, fmt, ap); | |
725 | fprintf(stderr, "\n"); | |
726 | #ifdef TARGET_I386 | |
727 | cpu_x86_dump_state(env, stderr, X86_DUMP_FPU | X86_DUMP_CCOP); | |
728 | #endif | |
729 | va_end(ap); | |
730 | abort(); | |
731 | } | |
732 | ||
66e85a21 FB |
733 | #ifdef TARGET_I386 |
734 | /* unmap all maped pages and flush all associated code */ | |
735 | void page_unmap(void) | |
736 | { | |
737 | PageDesc *p, *pmap; | |
738 | unsigned long addr; | |
7c2d6a78 | 739 | int i, j, ret, j1; |
66e85a21 FB |
740 | |
741 | for(i = 0; i < L1_SIZE; i++) { | |
742 | pmap = l1_map[i]; | |
743 | if (pmap) { | |
744 | p = pmap; | |
7c2d6a78 | 745 | for(j = 0;j < L2_SIZE;) { |
66e85a21 FB |
746 | if (p->flags & PAGE_VALID) { |
747 | addr = (i << (32 - L1_BITS)) | (j << TARGET_PAGE_BITS); | |
7c2d6a78 FB |
748 | /* we try to find a range to make less syscalls */ |
749 | j1 = j; | |
750 | p++; | |
751 | j++; | |
752 | while (j < L2_SIZE && (p->flags & PAGE_VALID)) { | |
753 | p++; | |
754 | j++; | |
755 | } | |
756 | ret = munmap((void *)addr, (j - j1) << TARGET_PAGE_BITS); | |
66e85a21 FB |
757 | if (ret != 0) { |
758 | fprintf(stderr, "Could not unmap page 0x%08lx\n", addr); | |
759 | exit(1); | |
760 | } | |
7c2d6a78 FB |
761 | } else { |
762 | p++; | |
763 | j++; | |
66e85a21 | 764 | } |
66e85a21 FB |
765 | } |
766 | free(pmap); | |
767 | l1_map[i] = NULL; | |
768 | } | |
769 | } | |
770 | tb_flush(); | |
771 | } | |
772 | #endif | |
33417e70 FB |
773 | |
774 | void tlb_flush(CPUState *env) | |
775 | { | |
776 | #if defined(TARGET_I386) | |
777 | int i; | |
778 | for(i = 0; i < CPU_TLB_SIZE; i++) { | |
779 | env->tlb_read[0][i].address = -1; | |
780 | env->tlb_write[0][i].address = -1; | |
781 | env->tlb_read[1][i].address = -1; | |
782 | env->tlb_write[1][i].address = -1; | |
783 | } | |
784 | #endif | |
785 | } | |
786 | ||
787 | void tlb_flush_page(CPUState *env, uint32_t addr) | |
788 | { | |
789 | #if defined(TARGET_I386) | |
790 | int i; | |
791 | ||
792 | i = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1); | |
793 | env->tlb_read[0][i].address = -1; | |
794 | env->tlb_write[0][i].address = -1; | |
795 | env->tlb_read[1][i].address = -1; | |
796 | env->tlb_write[1][i].address = -1; | |
797 | #endif | |
798 | } | |
799 | ||
800 | static inline unsigned long *physpage_find_alloc(unsigned int page) | |
801 | { | |
802 | unsigned long **lp, *p; | |
803 | unsigned int index, i; | |
804 | ||
805 | index = page >> TARGET_PAGE_BITS; | |
806 | lp = &l1_physmap[index >> L2_BITS]; | |
807 | p = *lp; | |
808 | if (!p) { | |
809 | /* allocate if not found */ | |
810 | p = malloc(sizeof(unsigned long) * L2_SIZE); | |
811 | for(i = 0; i < L2_SIZE; i++) | |
812 | p[i] = IO_MEM_UNASSIGNED; | |
813 | *lp = p; | |
814 | } | |
815 | return p + (index & (L2_SIZE - 1)); | |
816 | } | |
817 | ||
818 | /* return NULL if no page defined (unused memory) */ | |
819 | unsigned long physpage_find(unsigned long page) | |
820 | { | |
821 | unsigned long *p; | |
822 | unsigned int index; | |
823 | index = page >> TARGET_PAGE_BITS; | |
824 | p = l1_physmap[index >> L2_BITS]; | |
825 | if (!p) | |
826 | return IO_MEM_UNASSIGNED; | |
827 | return p[index & (L2_SIZE - 1)]; | |
828 | } | |
829 | ||
830 | /* register physical memory. 'size' must be a multiple of the target | |
831 | page size. If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an | |
832 | io memory page */ | |
833 | void cpu_register_physical_memory(unsigned long start_addr, unsigned long size, | |
834 | long phys_offset) | |
835 | { | |
836 | unsigned long addr, end_addr; | |
837 | unsigned long *p; | |
838 | ||
839 | end_addr = start_addr + size; | |
840 | for(addr = start_addr; addr < end_addr; addr += TARGET_PAGE_SIZE) { | |
841 | p = physpage_find_alloc(addr); | |
842 | *p = phys_offset; | |
843 | if ((phys_offset & ~TARGET_PAGE_MASK) == 0) | |
844 | phys_offset += TARGET_PAGE_SIZE; | |
845 | } | |
846 | } | |
847 | ||
848 | static uint32_t unassigned_mem_readb(uint32_t addr) | |
849 | { | |
850 | return 0; | |
851 | } | |
852 | ||
853 | static void unassigned_mem_writeb(uint32_t addr, uint32_t val) | |
854 | { | |
855 | } | |
856 | ||
857 | static CPUReadMemoryFunc *unassigned_mem_read[3] = { | |
858 | unassigned_mem_readb, | |
859 | unassigned_mem_readb, | |
860 | unassigned_mem_readb, | |
861 | }; | |
862 | ||
863 | static CPUWriteMemoryFunc *unassigned_mem_write[3] = { | |
864 | unassigned_mem_writeb, | |
865 | unassigned_mem_writeb, | |
866 | unassigned_mem_writeb, | |
867 | }; | |
868 | ||
869 | ||
870 | static void io_mem_init(void) | |
871 | { | |
872 | io_mem_nb = 1; | |
873 | cpu_register_io_memory(0, unassigned_mem_read, unassigned_mem_write); | |
874 | } | |
875 | ||
876 | /* mem_read and mem_write are arrays of functions containing the | |
877 | function to access byte (index 0), word (index 1) and dword (index | |
878 | 2). All functions must be supplied. If io_index is non zero, the | |
879 | corresponding io zone is modified. If it is zero, a new io zone is | |
880 | allocated. The return value can be used with | |
881 | cpu_register_physical_memory(). (-1) is returned if error. */ | |
882 | int cpu_register_io_memory(int io_index, | |
883 | CPUReadMemoryFunc **mem_read, | |
884 | CPUWriteMemoryFunc **mem_write) | |
885 | { | |
886 | int i; | |
887 | ||
888 | if (io_index <= 0) { | |
889 | if (io_index >= IO_MEM_NB_ENTRIES) | |
890 | return -1; | |
891 | io_index = io_mem_nb++; | |
892 | } else { | |
893 | if (io_index >= IO_MEM_NB_ENTRIES) | |
894 | return -1; | |
895 | } | |
896 | ||
897 | for(i = 0;i < 3; i++) { | |
898 | io_mem_read[io_index][i] = mem_read[i]; | |
899 | io_mem_write[io_index][i] = mem_write[i]; | |
900 | } | |
901 | return io_index << IO_MEM_SHIFT; | |
902 | } |