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
32 //#define DEBUG_TB_INVALIDATE
35 /* make various TB consistency checks */
36 //#define DEBUG_TB_CHECK
38 /* threshold to flush the translated code buffer */
39 #define CODE_GEN_BUFFER_MAX_SIZE (CODE_GEN_BUFFER_SIZE - CODE_GEN_MAX_SIZE)
41 #define CODE_GEN_MAX_BLOCKS (CODE_GEN_BUFFER_SIZE / 64)
43 TranslationBlock tbs[CODE_GEN_MAX_BLOCKS];
44 TranslationBlock *tb_hash[CODE_GEN_HASH_SIZE];
46 /* any access to the tbs or the page table must use this lock */
47 spinlock_t tb_lock = SPIN_LOCK_UNLOCKED;
49 uint8_t code_gen_buffer[CODE_GEN_BUFFER_SIZE];
50 uint8_t *code_gen_ptr;
52 /* XXX: pack the flags in the low bits of the pointer ? */
53 typedef struct PageDesc {
55 TranslationBlock *first_tb;
59 #define L1_BITS (32 - L2_BITS - TARGET_PAGE_BITS)
61 #define L1_SIZE (1 << L1_BITS)
62 #define L2_SIZE (1 << L2_BITS)
64 static void tb_invalidate_page(unsigned long address);
66 unsigned long real_host_page_size;
67 unsigned long host_page_bits;
68 unsigned long host_page_size;
69 unsigned long host_page_mask;
71 static PageDesc *l1_map[L1_SIZE];
73 static void page_init(void)
75 /* NOTE: we can always suppose that host_page_size >=
77 real_host_page_size = getpagesize();
78 if (host_page_size == 0)
79 host_page_size = real_host_page_size;
80 if (host_page_size < TARGET_PAGE_SIZE)
81 host_page_size = TARGET_PAGE_SIZE;
83 while ((1 << host_page_bits) < host_page_size)
85 host_page_mask = ~(host_page_size - 1);
88 /* dump memory mappings */
89 void page_dump(FILE *f)
91 unsigned long start, end;
92 int i, j, prot, prot1;
95 fprintf(f, "%-8s %-8s %-8s %s\n",
96 "start", "end", "size", "prot");
100 for(i = 0; i <= L1_SIZE; i++) {
105 for(j = 0;j < L2_SIZE; j++) {
111 end = (i << (32 - L1_BITS)) | (j << TARGET_PAGE_BITS);
113 fprintf(f, "%08lx-%08lx %08lx %c%c%c\n",
114 start, end, end - start,
115 prot & PAGE_READ ? 'r' : '-',
116 prot & PAGE_WRITE ? 'w' : '-',
117 prot & PAGE_EXEC ? 'x' : '-');
131 static inline PageDesc *page_find_alloc(unsigned int index)
135 lp = &l1_map[index >> L2_BITS];
138 /* allocate if not found */
139 p = malloc(sizeof(PageDesc) * L2_SIZE);
140 memset(p, 0, sizeof(PageDesc) * L2_SIZE);
143 return p + (index & (L2_SIZE - 1));
146 static inline PageDesc *page_find(unsigned int index)
150 p = l1_map[index >> L2_BITS];
153 return p + (index & (L2_SIZE - 1));
156 int page_get_flags(unsigned long address)
160 p = page_find(address >> TARGET_PAGE_BITS);
166 /* modify the flags of a page and invalidate the code if
167 necessary. The flag PAGE_WRITE_ORG is positionned automatically
168 depending on PAGE_WRITE */
169 void page_set_flags(unsigned long start, unsigned long end, int flags)
174 start = start & TARGET_PAGE_MASK;
175 end = TARGET_PAGE_ALIGN(end);
176 if (flags & PAGE_WRITE)
177 flags |= PAGE_WRITE_ORG;
179 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
180 p = page_find_alloc(addr >> TARGET_PAGE_BITS);
181 /* if the write protection is set, then we invalidate the code
183 if (!(p->flags & PAGE_WRITE) &&
184 (flags & PAGE_WRITE) &&
186 tb_invalidate_page(addr);
190 spin_unlock(&tb_lock);
193 void cpu_exec_init(void)
196 code_gen_ptr = code_gen_buffer;
201 /* set to NULL all the 'first_tb' fields in all PageDescs */
202 static void page_flush_tb(void)
207 for(i = 0; i < L1_SIZE; i++) {
210 for(j = 0; j < L2_SIZE; j++)
211 p[j].first_tb = NULL;
216 /* flush all the translation blocks */
217 /* XXX: tb_flush is currently not thread safe */
222 printf("qemu: flush code_size=%d nb_tbs=%d avg_tb_size=%d\n",
223 code_gen_ptr - code_gen_buffer,
225 (code_gen_ptr - code_gen_buffer) / nb_tbs);
228 for(i = 0;i < CODE_GEN_HASH_SIZE; i++)
231 code_gen_ptr = code_gen_buffer;
232 /* XXX: flush processor icache at this point if cache flush is
236 #ifdef DEBUG_TB_CHECK
238 static void tb_invalidate_check(unsigned long address)
240 TranslationBlock *tb;
242 address &= TARGET_PAGE_MASK;
243 for(i = 0;i < CODE_GEN_HASH_SIZE; i++) {
244 for(tb = tb_hash[i]; tb != NULL; tb = tb->hash_next) {
245 if (!(address + TARGET_PAGE_SIZE <= tb->pc ||
246 address >= tb->pc + tb->size)) {
247 printf("ERROR invalidate: address=%08lx PC=%08lx size=%04x\n",
248 address, tb->pc, tb->size);
254 /* verify that all the pages have correct rights for code */
255 static void tb_page_check(void)
257 TranslationBlock *tb;
258 int i, flags1, flags2;
260 for(i = 0;i < CODE_GEN_HASH_SIZE; i++) {
261 for(tb = tb_hash[i]; tb != NULL; tb = tb->hash_next) {
262 flags1 = page_get_flags(tb->pc);
263 flags2 = page_get_flags(tb->pc + tb->size - 1);
264 if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) {
265 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
266 tb->pc, tb->size, flags1, flags2);
272 void tb_jmp_check(TranslationBlock *tb)
274 TranslationBlock *tb1;
277 /* suppress any remaining jumps to this TB */
281 tb1 = (TranslationBlock *)((long)tb1 & ~3);
284 tb1 = tb1->jmp_next[n1];
286 /* check end of list */
288 printf("ERROR: jmp_list from 0x%08lx\n", (long)tb);
294 /* invalidate one TB */
295 static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb,
298 TranslationBlock *tb1;
302 *ptb = *(TranslationBlock **)((char *)tb1 + next_offset);
305 ptb = (TranslationBlock **)((char *)tb1 + next_offset);
309 static inline void tb_jmp_remove(TranslationBlock *tb, int n)
311 TranslationBlock *tb1, **ptb;
314 ptb = &tb->jmp_next[n];
317 /* find tb(n) in circular list */
321 tb1 = (TranslationBlock *)((long)tb1 & ~3);
322 if (n1 == n && tb1 == tb)
325 ptb = &tb1->jmp_first;
327 ptb = &tb1->jmp_next[n1];
330 /* now we can suppress tb(n) from the list */
331 *ptb = tb->jmp_next[n];
333 tb->jmp_next[n] = NULL;
337 /* reset the jump entry 'n' of a TB so that it is not chained to
339 static inline void tb_reset_jump(TranslationBlock *tb, int n)
341 tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n]));
344 static inline void tb_invalidate(TranslationBlock *tb, int parity)
347 unsigned int page_index1, page_index2;
349 TranslationBlock *tb1, *tb2;
351 /* remove the TB from the hash list */
352 h = tb_hash_func(tb->pc);
353 tb_remove(&tb_hash[h], tb,
354 offsetof(TranslationBlock, hash_next));
355 /* remove the TB from the page list */
356 page_index1 = tb->pc >> TARGET_PAGE_BITS;
357 if ((page_index1 & 1) == parity) {
358 p = page_find(page_index1);
359 tb_remove(&p->first_tb, tb,
360 offsetof(TranslationBlock, page_next[page_index1 & 1]));
362 page_index2 = (tb->pc + tb->size - 1) >> TARGET_PAGE_BITS;
363 if ((page_index2 & 1) == parity) {
364 p = page_find(page_index2);
365 tb_remove(&p->first_tb, tb,
366 offsetof(TranslationBlock, page_next[page_index2 & 1]));
369 /* suppress this TB from the two jump lists */
370 tb_jmp_remove(tb, 0);
371 tb_jmp_remove(tb, 1);
373 /* suppress any remaining jumps to this TB */
379 tb1 = (TranslationBlock *)((long)tb1 & ~3);
380 tb2 = tb1->jmp_next[n1];
381 tb_reset_jump(tb1, n1);
382 tb1->jmp_next[n1] = NULL;
385 tb->jmp_first = (TranslationBlock *)((long)tb | 2); /* fail safe */
388 /* invalidate all TBs which intersect with the target page starting at addr */
389 static void tb_invalidate_page(unsigned long address)
391 TranslationBlock *tb_next, *tb;
392 unsigned int page_index;
393 int parity1, parity2;
395 #ifdef DEBUG_TB_INVALIDATE
396 printf("tb_invalidate_page: %lx\n", address);
399 page_index = address >> TARGET_PAGE_BITS;
400 p = page_find(page_index);
404 parity1 = page_index & 1;
405 parity2 = parity1 ^ 1;
407 tb_next = tb->page_next[parity1];
408 tb_invalidate(tb, parity2);
414 /* add the tb in the target page and protect it if necessary */
415 static inline void tb_alloc_page(TranslationBlock *tb, unsigned int page_index)
418 unsigned long host_start, host_end, addr, page_addr;
421 p = page_find_alloc(page_index);
422 tb->page_next[page_index & 1] = p->first_tb;
424 if (p->flags & PAGE_WRITE) {
425 /* force the host page as non writable (writes will have a
426 page fault + mprotect overhead) */
427 page_addr = (page_index << TARGET_PAGE_BITS);
428 host_start = page_addr & host_page_mask;
429 host_end = host_start + host_page_size;
431 for(addr = host_start; addr < host_end; addr += TARGET_PAGE_SIZE)
432 prot |= page_get_flags(addr);
433 mprotect((void *)host_start, host_page_size,
434 (prot & PAGE_BITS) & ~PAGE_WRITE);
435 #ifdef DEBUG_TB_INVALIDATE
436 printf("protecting code page: 0x%08lx\n",
439 p->flags &= ~PAGE_WRITE;
440 #ifdef DEBUG_TB_CHECK
446 /* Allocate a new translation block. Flush the translation buffer if
447 too many translation blocks or too much generated code. */
448 TranslationBlock *tb_alloc(unsigned long pc)
450 TranslationBlock *tb;
452 if (nb_tbs >= CODE_GEN_MAX_BLOCKS ||
453 (code_gen_ptr - code_gen_buffer) >= CODE_GEN_BUFFER_MAX_SIZE)
460 /* link the tb with the other TBs */
461 void tb_link(TranslationBlock *tb)
463 unsigned int page_index1, page_index2;
465 /* add in the page list */
466 page_index1 = tb->pc >> TARGET_PAGE_BITS;
467 tb_alloc_page(tb, page_index1);
468 page_index2 = (tb->pc + tb->size - 1) >> TARGET_PAGE_BITS;
469 if (page_index2 != page_index1) {
470 tb_alloc_page(tb, page_index2);
472 tb->jmp_first = (TranslationBlock *)((long)tb | 2);
473 tb->jmp_next[0] = NULL;
474 tb->jmp_next[1] = NULL;
476 /* init original jump addresses */
477 if (tb->tb_next_offset[0] != 0xffff)
478 tb_reset_jump(tb, 0);
479 if (tb->tb_next_offset[1] != 0xffff)
480 tb_reset_jump(tb, 1);
483 /* called from signal handler: invalidate the code and unprotect the
484 page. Return TRUE if the fault was succesfully handled. */
485 int page_unprotect(unsigned long address)
487 unsigned int page_index, prot, pindex;
489 unsigned long host_start, host_end, addr;
491 host_start = address & host_page_mask;
492 page_index = host_start >> TARGET_PAGE_BITS;
493 p1 = page_find(page_index);
496 host_end = host_start + host_page_size;
499 for(addr = host_start;addr < host_end; addr += TARGET_PAGE_SIZE) {
503 /* if the page was really writable, then we change its
504 protection back to writable */
505 if (prot & PAGE_WRITE_ORG) {
506 mprotect((void *)host_start, host_page_size,
507 (prot & PAGE_BITS) | PAGE_WRITE);
508 pindex = (address - host_start) >> TARGET_PAGE_BITS;
509 p1[pindex].flags |= PAGE_WRITE;
510 /* and since the content will be modified, we must invalidate
511 the corresponding translated code. */
512 tb_invalidate_page(address);
513 #ifdef DEBUG_TB_CHECK
514 tb_invalidate_check(address);
522 /* call this function when system calls directly modify a memory area */
523 void page_unprotect_range(uint8_t *data, unsigned long data_size)
525 unsigned long start, end, addr;
527 start = (unsigned long)data;
528 end = start + data_size;
529 start &= TARGET_PAGE_MASK;
530 end = TARGET_PAGE_ALIGN(end);
531 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
532 page_unprotect(addr);
536 /* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
537 tb[1].tc_ptr. Return NULL if not found */
538 TranslationBlock *tb_find_pc(unsigned long tc_ptr)
542 TranslationBlock *tb;
546 if (tc_ptr < (unsigned long)code_gen_buffer ||
547 tc_ptr >= (unsigned long)code_gen_ptr)
549 /* binary search (cf Knuth) */
552 while (m_min <= m_max) {
553 m = (m_min + m_max) >> 1;
555 v = (unsigned long)tb->tc_ptr;
558 else if (tc_ptr < v) {
567 void cpu_abort(CPUState *env, const char *fmt, ...)
572 fprintf(stderr, "qemu: fatal: ");
573 vfprintf(stderr, fmt, ap);
574 fprintf(stderr, "\n");
576 cpu_x86_dump_state(env, stderr, X86_DUMP_FPU | X86_DUMP_CCOP);