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
31 //#define DEBUG_TB_INVALIDATE
34 /* make various TB consistency checks */
35 //#define DEBUG_TB_CHECK
37 /* threshold to flush the translated code buffer */
38 #define CODE_GEN_BUFFER_MAX_SIZE (CODE_GEN_BUFFER_SIZE - CODE_GEN_MAX_SIZE)
40 #define CODE_GEN_MAX_BLOCKS (CODE_GEN_BUFFER_SIZE / 64)
42 TranslationBlock tbs[CODE_GEN_MAX_BLOCKS];
43 TranslationBlock *tb_hash[CODE_GEN_HASH_SIZE];
46 uint8_t code_gen_buffer[CODE_GEN_BUFFER_SIZE];
47 uint8_t *code_gen_ptr;
49 /* XXX: pack the flags in the low bits of the pointer ? */
50 typedef struct PageDesc {
52 TranslationBlock *first_tb;
56 #define L1_BITS (32 - L2_BITS - TARGET_PAGE_BITS)
58 #define L1_SIZE (1 << L1_BITS)
59 #define L2_SIZE (1 << L2_BITS)
61 static void tb_invalidate_page(unsigned long address);
63 unsigned long real_host_page_size;
64 unsigned long host_page_bits;
65 unsigned long host_page_size;
66 unsigned long host_page_mask;
68 static PageDesc *l1_map[L1_SIZE];
72 /* NOTE: we can always suppose that host_page_size >=
74 real_host_page_size = getpagesize();
75 if (host_page_size == 0)
76 host_page_size = real_host_page_size;
77 if (host_page_size < TARGET_PAGE_SIZE)
78 host_page_size = TARGET_PAGE_SIZE;
80 while ((1 << host_page_bits) < host_page_size)
82 host_page_mask = ~(host_page_size - 1);
85 /* dump memory mappings */
86 void page_dump(FILE *f)
88 unsigned long start, end;
89 int i, j, prot, prot1;
92 fprintf(f, "%-8s %-8s %-8s %s\n",
93 "start", "end", "size", "prot");
97 for(i = 0; i <= L1_SIZE; i++) {
102 for(j = 0;j < L2_SIZE; j++) {
108 end = (i << (32 - L1_BITS)) | (j << TARGET_PAGE_BITS);
110 fprintf(f, "%08lx-%08lx %08lx %c%c%c\n",
111 start, end, end - start,
112 prot & PAGE_READ ? 'r' : '-',
113 prot & PAGE_WRITE ? 'w' : '-',
114 prot & PAGE_EXEC ? 'x' : '-');
128 static inline PageDesc *page_find_alloc(unsigned int index)
132 lp = &l1_map[index >> L2_BITS];
135 /* allocate if not found */
136 p = malloc(sizeof(PageDesc) * L2_SIZE);
137 memset(p, 0, sizeof(PageDesc) * L2_SIZE);
140 return p + (index & (L2_SIZE - 1));
143 static inline PageDesc *page_find(unsigned int index)
147 p = l1_map[index >> L2_BITS];
150 return p + (index & (L2_SIZE - 1));
153 int page_get_flags(unsigned long address)
157 p = page_find(address >> TARGET_PAGE_BITS);
163 /* modify the flags of a page and invalidate the code if
164 necessary. The flag PAGE_WRITE_ORG is positionned automatically
165 depending on PAGE_WRITE */
166 void page_set_flags(unsigned long start, unsigned long end, int flags)
171 start = start & TARGET_PAGE_MASK;
172 end = TARGET_PAGE_ALIGN(end);
173 if (flags & PAGE_WRITE)
174 flags |= PAGE_WRITE_ORG;
175 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
176 p = page_find_alloc(addr >> TARGET_PAGE_BITS);
177 /* if the write protection is set, then we invalidate the code
179 if (!(p->flags & PAGE_WRITE) &&
180 (flags & PAGE_WRITE) &&
182 tb_invalidate_page(addr);
188 void cpu_x86_tblocks_init(void)
191 code_gen_ptr = code_gen_buffer;
195 /* set to NULL all the 'first_tb' fields in all PageDescs */
196 static void page_flush_tb(void)
201 for(i = 0; i < L1_SIZE; i++) {
204 for(j = 0; j < L2_SIZE; j++)
205 p[j].first_tb = NULL;
210 /* flush all the translation blocks */
215 printf("qemu: flush code_size=%d nb_tbs=%d avg_tb_size=%d\n",
216 code_gen_ptr - code_gen_buffer,
218 (code_gen_ptr - code_gen_buffer) / nb_tbs);
221 for(i = 0;i < CODE_GEN_HASH_SIZE; i++)
224 code_gen_ptr = code_gen_buffer;
225 /* XXX: flush processor icache at this point */
228 #ifdef DEBUG_TB_CHECK
230 static void tb_invalidate_check(unsigned long address)
232 TranslationBlock *tb;
234 address &= TARGET_PAGE_MASK;
235 for(i = 0;i < CODE_GEN_HASH_SIZE; i++) {
236 for(tb = tb_hash[i]; tb != NULL; tb = tb->hash_next) {
237 if (!(address + TARGET_PAGE_SIZE <= tb->pc ||
238 address >= tb->pc + tb->size)) {
239 printf("ERROR invalidate: address=%08lx PC=%08lx size=%04x\n",
240 address, tb->pc, tb->size);
246 /* verify that all the pages have correct rights for code */
247 static void tb_page_check(void)
249 TranslationBlock *tb;
250 int i, flags1, flags2;
252 for(i = 0;i < CODE_GEN_HASH_SIZE; i++) {
253 for(tb = tb_hash[i]; tb != NULL; tb = tb->hash_next) {
254 flags1 = page_get_flags(tb->pc);
255 flags2 = page_get_flags(tb->pc + tb->size - 1);
256 if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) {
257 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
258 tb->pc, tb->size, flags1, flags2);
266 /* invalidate one TB */
267 static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb,
270 TranslationBlock *tb1;
274 *ptb = *(TranslationBlock **)((char *)tb1 + next_offset);
277 ptb = (TranslationBlock **)((char *)tb1 + next_offset);
281 static inline void tb_invalidate(TranslationBlock *tb, int parity)
284 unsigned int page_index1, page_index2;
287 /* remove the TB from the hash list */
288 h = tb_hash_func(tb->pc);
289 tb_remove(&tb_hash[h], tb,
290 offsetof(TranslationBlock, hash_next));
291 /* remove the TB from the page list */
292 page_index1 = tb->pc >> TARGET_PAGE_BITS;
293 if ((page_index1 & 1) == parity) {
294 p = page_find(page_index1);
295 tb_remove(&p->first_tb, tb,
296 offsetof(TranslationBlock, page_next[page_index1 & 1]));
298 page_index2 = (tb->pc + tb->size - 1) >> TARGET_PAGE_BITS;
299 if ((page_index2 & 1) == parity) {
300 p = page_find(page_index2);
301 tb_remove(&p->first_tb, tb,
302 offsetof(TranslationBlock, page_next[page_index2 & 1]));
306 /* invalidate all TBs which intersect with the target page starting at addr */
307 static void tb_invalidate_page(unsigned long address)
309 TranslationBlock *tb_next, *tb;
310 unsigned int page_index;
311 int parity1, parity2;
313 #ifdef DEBUG_TB_INVALIDATE
314 printf("tb_invalidate_page: %lx\n", address);
317 page_index = address >> TARGET_PAGE_BITS;
318 p = page_find(page_index);
322 parity1 = page_index & 1;
323 parity2 = parity1 ^ 1;
325 tb_next = tb->page_next[parity1];
326 tb_invalidate(tb, parity2);
332 /* add the tb in the target page and protect it if necessary */
333 static inline void tb_alloc_page(TranslationBlock *tb, unsigned int page_index)
336 unsigned long host_start, host_end, addr, page_addr;
339 p = page_find_alloc(page_index);
340 tb->page_next[page_index & 1] = p->first_tb;
342 if (p->flags & PAGE_WRITE) {
343 /* force the host page as non writable (writes will have a
344 page fault + mprotect overhead) */
345 page_addr = (page_index << TARGET_PAGE_BITS);
346 host_start = page_addr & host_page_mask;
347 host_end = host_start + host_page_size;
349 for(addr = host_start; addr < host_end; addr += TARGET_PAGE_SIZE)
350 prot |= page_get_flags(addr);
351 mprotect((void *)host_start, host_page_size,
352 (prot & PAGE_BITS) & ~PAGE_WRITE);
353 #ifdef DEBUG_TB_INVALIDATE
354 printf("protecting code page: 0x%08lx\n",
357 p->flags &= ~PAGE_WRITE;
358 #ifdef DEBUG_TB_CHECK
364 /* Allocate a new translation block. Flush the translation buffer if
365 too many translation blocks or too much generated code. */
366 TranslationBlock *tb_alloc(unsigned long pc,
369 TranslationBlock *tb;
370 unsigned int page_index1, page_index2;
372 if (nb_tbs >= CODE_GEN_MAX_BLOCKS ||
373 (code_gen_ptr - code_gen_buffer) >= CODE_GEN_BUFFER_MAX_SIZE)
379 /* add in the page list */
380 page_index1 = pc >> TARGET_PAGE_BITS;
381 tb_alloc_page(tb, page_index1);
382 page_index2 = (pc + size - 1) >> TARGET_PAGE_BITS;
383 if (page_index2 != page_index1) {
384 tb_alloc_page(tb, page_index2);
389 /* called from signal handler: invalidate the code and unprotect the
390 page. Return TRUE if the fault was succesfully handled. */
391 int page_unprotect(unsigned long address)
393 unsigned int page_index, prot;
395 unsigned long host_start, host_end, addr;
397 page_index = address >> TARGET_PAGE_BITS;
398 p = page_find(page_index);
401 if ((p->flags & (PAGE_WRITE_ORG | PAGE_WRITE)) == PAGE_WRITE_ORG) {
402 /* if the page was really writable, then we change its
403 protection back to writable */
404 host_start = address & host_page_mask;
405 host_end = host_start + host_page_size;
407 for(addr = host_start; addr < host_end; addr += TARGET_PAGE_SIZE)
408 prot |= page_get_flags(addr);
409 mprotect((void *)host_start, host_page_size,
410 (prot & PAGE_BITS) | PAGE_WRITE);
411 p->flags |= PAGE_WRITE;
413 /* and since the content will be modified, we must invalidate
414 the corresponding translated code. */
415 tb_invalidate_page(address);
416 #ifdef DEBUG_TB_CHECK
417 tb_invalidate_check(address);
425 /* call this function when system calls directly modify a memory area */
426 void page_unprotect_range(uint8_t *data, unsigned long data_size)
428 unsigned long start, end, addr;
430 start = (unsigned long)data;
431 end = start + data_size;
432 start &= TARGET_PAGE_MASK;
433 end = TARGET_PAGE_ALIGN(end);
434 for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
435 page_unprotect(addr);