2 * virtual page mapping and translated block handling
4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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
38 //#define DEBUG_TB_INVALIDATE
41 /* make various TB consistency checks */
42 //#define DEBUG_TB_CHECK
44 /* threshold to flush the translated code buffer */
45 #define CODE_GEN_BUFFER_MAX_SIZE (CODE_GEN_BUFFER_SIZE - CODE_GEN_MAX_SIZE)
47 #define CODE_GEN_MAX_BLOCKS (CODE_GEN_BUFFER_SIZE / 64)
49 TranslationBlock tbs[CODE_GEN_MAX_BLOCKS];
50 TranslationBlock *tb_hash[CODE_GEN_HASH_SIZE];
52 /* any access to the tbs or the page table must use this lock */
53 spinlock_t tb_lock = SPIN_LOCK_UNLOCKED;
55 uint8_t code_gen_buffer[CODE_GEN_BUFFER_SIZE];
56 uint8_t *code_gen_ptr;
58 /* XXX: pack the flags in the low bits of the pointer ? */
59 typedef struct PageDesc {
61 TranslationBlock *first_tb;
65 #define L1_BITS (32 - L2_BITS - TARGET_PAGE_BITS)
67 #define L1_SIZE (1 << L1_BITS)
68 #define L2_SIZE (1 << L2_BITS)
70 static void tb_invalidate_page(unsigned long address);
72 unsigned long real_host_page_size;
73 unsigned long host_page_bits;
74 unsigned long host_page_size;
75 unsigned long host_page_mask;
77 static PageDesc *l1_map[L1_SIZE];
79 static void page_init(void)
81 /* NOTE: we can always suppose that host_page_size >=
83 real_host_page_size = getpagesize();
84 if (host_page_size == 0)
85 host_page_size = real_host_page_size;
86 if (host_page_size < TARGET_PAGE_SIZE)
87 host_page_size = TARGET_PAGE_SIZE;
89 while ((1 << host_page_bits) < host_page_size)
91 host_page_mask = ~(host_page_size - 1);
94 /* dump memory mappings */
95 void page_dump(FILE *f)
97 unsigned long start, end;
98 int i, j, prot, prot1;
101 fprintf(f, "%-8s %-8s %-8s %s\n",
102 "start", "end", "size", "prot");
106 for(i = 0; i <= L1_SIZE; i++) {
111 for(j = 0;j < L2_SIZE; j++) {
117 end = (i << (32 - L1_BITS)) | (j << TARGET_PAGE_BITS);
119 fprintf(f, "%08lx-%08lx %08lx %c%c%c\n",
120 start, end, end - start,
121 prot & PAGE_READ ? 'r' : '-',
122 prot & PAGE_WRITE ? 'w' : '-',
123 prot & PAGE_EXEC ? 'x' : '-');
137 static inline PageDesc *page_find_alloc(unsigned int index)
141 lp = &l1_map[index >> L2_BITS];
144 /* allocate if not found */
145 p = malloc(sizeof(PageDesc) * L2_SIZE);
146 memset(p, 0, sizeof(PageDesc) * L2_SIZE);
149 return p + (index & (L2_SIZE - 1));
152 static inline PageDesc *page_find(unsigned int index)
156 p = l1_map[index >> L2_BITS];
159 return p + (index & (L2_SIZE - 1));
162 int page_get_flags(unsigned long address)
166 p = page_find(address >> TARGET_PAGE_BITS);
172 /* modify the flags of a page and invalidate the code if
173 necessary. The flag PAGE_WRITE_ORG is positionned automatically
174 depending on PAGE_WRITE */
175 void page_set_flags(unsigned long start, unsigned long end, int flags)
180 start = start & TARGET_PAGE_MASK;
181 end = TARGET_PAGE_ALIGN(end);
182 if (flags & PAGE_WRITE)
183 flags |= PAGE_WRITE_ORG;
185 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
186 p = page_find_alloc(addr >> TARGET_PAGE_BITS);
187 /* if the write protection is set, then we invalidate the code
189 if (!(p->flags & PAGE_WRITE) &&
190 (flags & PAGE_WRITE) &&
192 tb_invalidate_page(addr);
196 spin_unlock(&tb_lock);
199 void cpu_exec_init(void)
202 code_gen_ptr = code_gen_buffer;
207 /* set to NULL all the 'first_tb' fields in all PageDescs */
208 static void page_flush_tb(void)
213 for(i = 0; i < L1_SIZE; i++) {
216 for(j = 0; j < L2_SIZE; j++)
217 p[j].first_tb = NULL;
222 /* flush all the translation blocks */
223 /* XXX: tb_flush is currently not thread safe */
228 printf("qemu: flush code_size=%d nb_tbs=%d avg_tb_size=%d\n",
229 code_gen_ptr - code_gen_buffer,
231 (code_gen_ptr - code_gen_buffer) / nb_tbs);
234 for(i = 0;i < CODE_GEN_HASH_SIZE; i++)
237 code_gen_ptr = code_gen_buffer;
238 /* XXX: flush processor icache at this point if cache flush is
242 #ifdef DEBUG_TB_CHECK
244 static void tb_invalidate_check(unsigned long address)
246 TranslationBlock *tb;
248 address &= TARGET_PAGE_MASK;
249 for(i = 0;i < CODE_GEN_HASH_SIZE; i++) {
250 for(tb = tb_hash[i]; tb != NULL; tb = tb->hash_next) {
251 if (!(address + TARGET_PAGE_SIZE <= tb->pc ||
252 address >= tb->pc + tb->size)) {
253 printf("ERROR invalidate: address=%08lx PC=%08lx size=%04x\n",
254 address, tb->pc, tb->size);
260 /* verify that all the pages have correct rights for code */
261 static void tb_page_check(void)
263 TranslationBlock *tb;
264 int i, flags1, flags2;
266 for(i = 0;i < CODE_GEN_HASH_SIZE; i++) {
267 for(tb = tb_hash[i]; tb != NULL; tb = tb->hash_next) {
268 flags1 = page_get_flags(tb->pc);
269 flags2 = page_get_flags(tb->pc + tb->size - 1);
270 if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) {
271 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
272 tb->pc, tb->size, flags1, flags2);
278 void tb_jmp_check(TranslationBlock *tb)
280 TranslationBlock *tb1;
283 /* suppress any remaining jumps to this TB */
287 tb1 = (TranslationBlock *)((long)tb1 & ~3);
290 tb1 = tb1->jmp_next[n1];
292 /* check end of list */
294 printf("ERROR: jmp_list from 0x%08lx\n", (long)tb);
300 /* invalidate one TB */
301 static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb,
304 TranslationBlock *tb1;
308 *ptb = *(TranslationBlock **)((char *)tb1 + next_offset);
311 ptb = (TranslationBlock **)((char *)tb1 + next_offset);
315 static inline void tb_jmp_remove(TranslationBlock *tb, int n)
317 TranslationBlock *tb1, **ptb;
320 ptb = &tb->jmp_next[n];
323 /* find tb(n) in circular list */
327 tb1 = (TranslationBlock *)((long)tb1 & ~3);
328 if (n1 == n && tb1 == tb)
331 ptb = &tb1->jmp_first;
333 ptb = &tb1->jmp_next[n1];
336 /* now we can suppress tb(n) from the list */
337 *ptb = tb->jmp_next[n];
339 tb->jmp_next[n] = NULL;
343 /* reset the jump entry 'n' of a TB so that it is not chained to
345 static inline void tb_reset_jump(TranslationBlock *tb, int n)
347 tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n]));
350 static inline void tb_invalidate(TranslationBlock *tb, int parity)
353 unsigned int page_index1, page_index2;
355 TranslationBlock *tb1, *tb2;
357 /* remove the TB from the hash list */
358 h = tb_hash_func(tb->pc);
359 tb_remove(&tb_hash[h], tb,
360 offsetof(TranslationBlock, hash_next));
361 /* remove the TB from the page list */
362 page_index1 = tb->pc >> TARGET_PAGE_BITS;
363 if ((page_index1 & 1) == parity) {
364 p = page_find(page_index1);
365 tb_remove(&p->first_tb, tb,
366 offsetof(TranslationBlock, page_next[page_index1 & 1]));
368 page_index2 = (tb->pc + tb->size - 1) >> TARGET_PAGE_BITS;
369 if ((page_index2 & 1) == parity) {
370 p = page_find(page_index2);
371 tb_remove(&p->first_tb, tb,
372 offsetof(TranslationBlock, page_next[page_index2 & 1]));
375 /* suppress this TB from the two jump lists */
376 tb_jmp_remove(tb, 0);
377 tb_jmp_remove(tb, 1);
379 /* suppress any remaining jumps to this TB */
385 tb1 = (TranslationBlock *)((long)tb1 & ~3);
386 tb2 = tb1->jmp_next[n1];
387 tb_reset_jump(tb1, n1);
388 tb1->jmp_next[n1] = NULL;
391 tb->jmp_first = (TranslationBlock *)((long)tb | 2); /* fail safe */
394 /* invalidate all TBs which intersect with the target page starting at addr */
395 static void tb_invalidate_page(unsigned long address)
397 TranslationBlock *tb_next, *tb;
398 unsigned int page_index;
399 int parity1, parity2;
401 #ifdef DEBUG_TB_INVALIDATE
402 printf("tb_invalidate_page: %lx\n", address);
405 page_index = address >> TARGET_PAGE_BITS;
406 p = page_find(page_index);
410 parity1 = page_index & 1;
411 parity2 = parity1 ^ 1;
413 tb_next = tb->page_next[parity1];
414 tb_invalidate(tb, parity2);
420 /* add the tb in the target page and protect it if necessary */
421 static inline void tb_alloc_page(TranslationBlock *tb, unsigned int page_index)
424 unsigned long host_start, host_end, addr, page_addr;
427 p = page_find_alloc(page_index);
428 tb->page_next[page_index & 1] = p->first_tb;
430 if (p->flags & PAGE_WRITE) {
431 /* force the host page as non writable (writes will have a
432 page fault + mprotect overhead) */
433 page_addr = (page_index << TARGET_PAGE_BITS);
434 host_start = page_addr & host_page_mask;
435 host_end = host_start + host_page_size;
437 for(addr = host_start; addr < host_end; addr += TARGET_PAGE_SIZE)
438 prot |= page_get_flags(addr);
439 mprotect((void *)host_start, host_page_size,
440 (prot & PAGE_BITS) & ~PAGE_WRITE);
441 #ifdef DEBUG_TB_INVALIDATE
442 printf("protecting code page: 0x%08lx\n",
445 p->flags &= ~PAGE_WRITE;
446 #ifdef DEBUG_TB_CHECK
452 /* Allocate a new translation block. Flush the translation buffer if
453 too many translation blocks or too much generated code. */
454 TranslationBlock *tb_alloc(unsigned long pc)
456 TranslationBlock *tb;
458 if (nb_tbs >= CODE_GEN_MAX_BLOCKS ||
459 (code_gen_ptr - code_gen_buffer) >= CODE_GEN_BUFFER_MAX_SIZE)
466 /* link the tb with the other TBs */
467 void tb_link(TranslationBlock *tb)
469 unsigned int page_index1, page_index2;
471 /* add in the page list */
472 page_index1 = tb->pc >> TARGET_PAGE_BITS;
473 tb_alloc_page(tb, page_index1);
474 page_index2 = (tb->pc + tb->size - 1) >> TARGET_PAGE_BITS;
475 if (page_index2 != page_index1) {
476 tb_alloc_page(tb, page_index2);
478 tb->jmp_first = (TranslationBlock *)((long)tb | 2);
479 tb->jmp_next[0] = NULL;
480 tb->jmp_next[1] = NULL;
482 /* init original jump addresses */
483 if (tb->tb_next_offset[0] != 0xffff)
484 tb_reset_jump(tb, 0);
485 if (tb->tb_next_offset[1] != 0xffff)
486 tb_reset_jump(tb, 1);
489 /* called from signal handler: invalidate the code and unprotect the
490 page. Return TRUE if the fault was succesfully handled. */
491 int page_unprotect(unsigned long address)
493 unsigned int page_index, prot, pindex;
495 unsigned long host_start, host_end, addr;
497 host_start = address & host_page_mask;
498 page_index = host_start >> TARGET_PAGE_BITS;
499 p1 = page_find(page_index);
502 host_end = host_start + host_page_size;
505 for(addr = host_start;addr < host_end; addr += TARGET_PAGE_SIZE) {
509 /* if the page was really writable, then we change its
510 protection back to writable */
511 if (prot & PAGE_WRITE_ORG) {
512 mprotect((void *)host_start, host_page_size,
513 (prot & PAGE_BITS) | PAGE_WRITE);
514 pindex = (address - host_start) >> TARGET_PAGE_BITS;
515 p1[pindex].flags |= PAGE_WRITE;
516 /* and since the content will be modified, we must invalidate
517 the corresponding translated code. */
518 tb_invalidate_page(address);
519 #ifdef DEBUG_TB_CHECK
520 tb_invalidate_check(address);
528 /* call this function when system calls directly modify a memory area */
529 void page_unprotect_range(uint8_t *data, unsigned long data_size)
531 unsigned long start, end, addr;
533 start = (unsigned long)data;
534 end = start + data_size;
535 start &= TARGET_PAGE_MASK;
536 end = TARGET_PAGE_ALIGN(end);
537 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
538 page_unprotect(addr);
542 /* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
543 tb[1].tc_ptr. Return NULL if not found */
544 TranslationBlock *tb_find_pc(unsigned long tc_ptr)
548 TranslationBlock *tb;
552 if (tc_ptr < (unsigned long)code_gen_buffer ||
553 tc_ptr >= (unsigned long)code_gen_ptr)
555 /* binary search (cf Knuth) */
558 while (m_min <= m_max) {
559 m = (m_min + m_max) >> 1;
561 v = (unsigned long)tb->tc_ptr;
564 else if (tc_ptr < v) {
573 static void tb_reset_jump_recursive(TranslationBlock *tb);
575 static inline void tb_reset_jump_recursive2(TranslationBlock *tb, int n)
577 TranslationBlock *tb1, *tb_next, **ptb;
580 tb1 = tb->jmp_next[n];
582 /* find head of list */
585 tb1 = (TranslationBlock *)((long)tb1 & ~3);
588 tb1 = tb1->jmp_next[n1];
590 /* we are now sure now that tb jumps to tb1 */
593 /* remove tb from the jmp_first list */
594 ptb = &tb_next->jmp_first;
598 tb1 = (TranslationBlock *)((long)tb1 & ~3);
599 if (n1 == n && tb1 == tb)
601 ptb = &tb1->jmp_next[n1];
603 *ptb = tb->jmp_next[n];
604 tb->jmp_next[n] = NULL;
606 /* suppress the jump to next tb in generated code */
607 tb_reset_jump(tb, n);
609 /* suppress jumps in the tb on which we could have jump */
610 tb_reset_jump_recursive(tb_next);
614 static void tb_reset_jump_recursive(TranslationBlock *tb)
616 tb_reset_jump_recursive2(tb, 0);
617 tb_reset_jump_recursive2(tb, 1);
620 /* mask must never be zero */
621 void cpu_interrupt(CPUState *env, int mask)
623 TranslationBlock *tb;
625 env->interrupt_request |= mask;
626 /* if the cpu is currently executing code, we must unlink it and
627 all the potentially executing TB */
628 tb = env->current_tb;
630 tb_reset_jump_recursive(tb);
635 void cpu_abort(CPUState *env, const char *fmt, ...)
640 fprintf(stderr, "qemu: fatal: ");
641 vfprintf(stderr, fmt, ap);
642 fprintf(stderr, "\n");
644 cpu_x86_dump_state(env, stderr, X86_DUMP_FPU | X86_DUMP_CCOP);
651 /* unmap all maped pages and flush all associated code */
652 void page_unmap(void)
658 for(i = 0; i < L1_SIZE; i++) {
662 for(j = 0;j < L2_SIZE;) {
663 if (p->flags & PAGE_VALID) {
664 addr = (i << (32 - L1_BITS)) | (j << TARGET_PAGE_BITS);
665 /* we try to find a range to make less syscalls */
669 while (j < L2_SIZE && (p->flags & PAGE_VALID)) {
673 ret = munmap((void *)addr, (j - j1) << TARGET_PAGE_BITS);
675 fprintf(stderr, "Could not unmap page 0x%08lx\n", addr);