]>
git.proxmox.com Git - qemu.git/blob - exec.c
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
];
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_x86_tblocks_init(void)
196 code_gen_ptr
= code_gen_buffer
;
200 /* set to NULL all the 'first_tb' fields in all PageDescs */
201 static void page_flush_tb(void)
206 for(i
= 0; i
< L1_SIZE
; i
++) {
209 for(j
= 0; j
< L2_SIZE
; j
++)
210 p
[j
].first_tb
= NULL
;
215 /* flush all the translation blocks */
216 /* XXX: tb_flush is currently not thread safe */
221 printf("qemu: flush code_size=%d nb_tbs=%d avg_tb_size=%d\n",
222 code_gen_ptr
- code_gen_buffer
,
224 (code_gen_ptr
- code_gen_buffer
) / nb_tbs
);
227 for(i
= 0;i
< CODE_GEN_HASH_SIZE
; i
++)
230 code_gen_ptr
= code_gen_buffer
;
231 /* XXX: flush processor icache at this point if cache flush is
235 #ifdef DEBUG_TB_CHECK
237 static void tb_invalidate_check(unsigned long address
)
239 TranslationBlock
*tb
;
241 address
&= TARGET_PAGE_MASK
;
242 for(i
= 0;i
< CODE_GEN_HASH_SIZE
; i
++) {
243 for(tb
= tb_hash
[i
]; tb
!= NULL
; tb
= tb
->hash_next
) {
244 if (!(address
+ TARGET_PAGE_SIZE
<= tb
->pc
||
245 address
>= tb
->pc
+ tb
->size
)) {
246 printf("ERROR invalidate: address=%08lx PC=%08lx size=%04x\n",
247 address
, tb
->pc
, tb
->size
);
253 /* verify that all the pages have correct rights for code */
254 static void tb_page_check(void)
256 TranslationBlock
*tb
;
257 int i
, flags1
, flags2
;
259 for(i
= 0;i
< CODE_GEN_HASH_SIZE
; i
++) {
260 for(tb
= tb_hash
[i
]; tb
!= NULL
; tb
= tb
->hash_next
) {
261 flags1
= page_get_flags(tb
->pc
);
262 flags2
= page_get_flags(tb
->pc
+ tb
->size
- 1);
263 if ((flags1
& PAGE_WRITE
) || (flags2
& PAGE_WRITE
)) {
264 printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
265 tb
->pc
, tb
->size
, flags1
, flags2
);
271 void tb_jmp_check(TranslationBlock
*tb
)
273 TranslationBlock
*tb1
;
276 /* suppress any remaining jumps to this TB */
280 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
283 tb1
= tb1
->jmp_next
[n1
];
285 /* check end of list */
287 printf("ERROR: jmp_list from 0x%08lx\n", (long)tb
);
293 /* invalidate one TB */
294 static inline void tb_remove(TranslationBlock
**ptb
, TranslationBlock
*tb
,
297 TranslationBlock
*tb1
;
301 *ptb
= *(TranslationBlock
**)((char *)tb1
+ next_offset
);
304 ptb
= (TranslationBlock
**)((char *)tb1
+ next_offset
);
308 static inline void tb_jmp_remove(TranslationBlock
*tb
, int n
)
310 TranslationBlock
*tb1
, **ptb
;
313 ptb
= &tb
->jmp_next
[n
];
316 /* find tb(n) in circular list */
320 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
321 if (n1
== n
&& tb1
== tb
)
324 ptb
= &tb1
->jmp_first
;
326 ptb
= &tb1
->jmp_next
[n1
];
329 /* now we can suppress tb(n) from the list */
330 *ptb
= tb
->jmp_next
[n
];
332 tb
->jmp_next
[n
] = NULL
;
336 /* reset the jump entry 'n' of a TB so that it is not chained to
338 static inline void tb_reset_jump(TranslationBlock
*tb
, int n
)
340 tb_set_jmp_target(tb
, n
, (unsigned long)(tb
->tc_ptr
+ tb
->tb_next_offset
[n
]));
343 static inline void tb_invalidate(TranslationBlock
*tb
, int parity
)
346 unsigned int page_index1
, page_index2
;
348 TranslationBlock
*tb1
, *tb2
;
350 /* remove the TB from the hash list */
351 h
= tb_hash_func(tb
->pc
);
352 tb_remove(&tb_hash
[h
], tb
,
353 offsetof(TranslationBlock
, hash_next
));
354 /* remove the TB from the page list */
355 page_index1
= tb
->pc
>> TARGET_PAGE_BITS
;
356 if ((page_index1
& 1) == parity
) {
357 p
= page_find(page_index1
);
358 tb_remove(&p
->first_tb
, tb
,
359 offsetof(TranslationBlock
, page_next
[page_index1
& 1]));
361 page_index2
= (tb
->pc
+ tb
->size
- 1) >> TARGET_PAGE_BITS
;
362 if ((page_index2
& 1) == parity
) {
363 p
= page_find(page_index2
);
364 tb_remove(&p
->first_tb
, tb
,
365 offsetof(TranslationBlock
, page_next
[page_index2
& 1]));
368 /* suppress this TB from the two jump lists */
369 tb_jmp_remove(tb
, 0);
370 tb_jmp_remove(tb
, 1);
372 /* suppress any remaining jumps to this TB */
378 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
379 tb2
= tb1
->jmp_next
[n1
];
380 tb_reset_jump(tb1
, n1
);
381 tb1
->jmp_next
[n1
] = NULL
;
384 tb
->jmp_first
= (TranslationBlock
*)((long)tb
| 2); /* fail safe */
387 /* invalidate all TBs which intersect with the target page starting at addr */
388 static void tb_invalidate_page(unsigned long address
)
390 TranslationBlock
*tb_next
, *tb
;
391 unsigned int page_index
;
392 int parity1
, parity2
;
394 #ifdef DEBUG_TB_INVALIDATE
395 printf("tb_invalidate_page: %lx\n", address
);
398 page_index
= address
>> TARGET_PAGE_BITS
;
399 p
= page_find(page_index
);
403 parity1
= page_index
& 1;
404 parity2
= parity1
^ 1;
406 tb_next
= tb
->page_next
[parity1
];
407 tb_invalidate(tb
, parity2
);
413 /* add the tb in the target page and protect it if necessary */
414 static inline void tb_alloc_page(TranslationBlock
*tb
, unsigned int page_index
)
417 unsigned long host_start
, host_end
, addr
, page_addr
;
420 p
= page_find_alloc(page_index
);
421 tb
->page_next
[page_index
& 1] = p
->first_tb
;
423 if (p
->flags
& PAGE_WRITE
) {
424 /* force the host page as non writable (writes will have a
425 page fault + mprotect overhead) */
426 page_addr
= (page_index
<< TARGET_PAGE_BITS
);
427 host_start
= page_addr
& host_page_mask
;
428 host_end
= host_start
+ host_page_size
;
430 for(addr
= host_start
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
)
431 prot
|= page_get_flags(addr
);
432 mprotect((void *)host_start
, host_page_size
,
433 (prot
& PAGE_BITS
) & ~PAGE_WRITE
);
434 #ifdef DEBUG_TB_INVALIDATE
435 printf("protecting code page: 0x%08lx\n",
438 p
->flags
&= ~PAGE_WRITE
;
439 #ifdef DEBUG_TB_CHECK
445 /* Allocate a new translation block. Flush the translation buffer if
446 too many translation blocks or too much generated code. */
447 TranslationBlock
*tb_alloc(unsigned long pc
)
449 TranslationBlock
*tb
;
451 if (nb_tbs
>= CODE_GEN_MAX_BLOCKS
||
452 (code_gen_ptr
- code_gen_buffer
) >= CODE_GEN_BUFFER_MAX_SIZE
)
459 /* link the tb with the other TBs */
460 void tb_link(TranslationBlock
*tb
)
462 unsigned int page_index1
, page_index2
;
464 /* add in the page list */
465 page_index1
= tb
->pc
>> TARGET_PAGE_BITS
;
466 tb_alloc_page(tb
, page_index1
);
467 page_index2
= (tb
->pc
+ tb
->size
- 1) >> TARGET_PAGE_BITS
;
468 if (page_index2
!= page_index1
) {
469 tb_alloc_page(tb
, page_index2
);
471 tb
->jmp_first
= (TranslationBlock
*)((long)tb
| 2);
472 tb
->jmp_next
[0] = NULL
;
473 tb
->jmp_next
[1] = NULL
;
475 /* init original jump addresses */
476 if (tb
->tb_next_offset
[0] != 0xffff)
477 tb_reset_jump(tb
, 0);
478 if (tb
->tb_next_offset
[1] != 0xffff)
479 tb_reset_jump(tb
, 1);
482 /* called from signal handler: invalidate the code and unprotect the
483 page. Return TRUE if the fault was succesfully handled. */
484 int page_unprotect(unsigned long address
)
486 unsigned int page_index
, prot
, pindex
;
488 unsigned long host_start
, host_end
, addr
;
490 host_start
= address
& host_page_mask
;
491 page_index
= host_start
>> TARGET_PAGE_BITS
;
492 p1
= page_find(page_index
);
495 host_end
= host_start
+ host_page_size
;
498 for(addr
= host_start
;addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
502 /* if the page was really writable, then we change its
503 protection back to writable */
504 if (prot
& PAGE_WRITE_ORG
) {
505 mprotect((void *)host_start
, host_page_size
,
506 (prot
& PAGE_BITS
) | PAGE_WRITE
);
507 pindex
= (address
- host_start
) >> TARGET_PAGE_BITS
;
508 p1
[pindex
].flags
|= PAGE_WRITE
;
509 /* and since the content will be modified, we must invalidate
510 the corresponding translated code. */
511 tb_invalidate_page(address
);
512 #ifdef DEBUG_TB_CHECK
513 tb_invalidate_check(address
);
521 /* call this function when system calls directly modify a memory area */
522 void page_unprotect_range(uint8_t *data
, unsigned long data_size
)
524 unsigned long start
, end
, addr
;
526 start
= (unsigned long)data
;
527 end
= start
+ data_size
;
528 start
&= TARGET_PAGE_MASK
;
529 end
= TARGET_PAGE_ALIGN(end
);
530 for(addr
= start
; addr
< end
; addr
+= TARGET_PAGE_SIZE
) {
531 page_unprotect(addr
);