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
33 //#define DEBUG_TB_INVALIDATE
36 /* make various TB consistency checks */
37 //#define DEBUG_TB_CHECK
39 /* threshold to flush the translated code buffer */
40 #define CODE_GEN_BUFFER_MAX_SIZE (CODE_GEN_BUFFER_SIZE - CODE_GEN_MAX_SIZE)
42 #define CODE_GEN_MAX_BLOCKS (CODE_GEN_BUFFER_SIZE / 64)
44 TranslationBlock tbs
[CODE_GEN_MAX_BLOCKS
];
45 TranslationBlock
*tb_hash
[CODE_GEN_HASH_SIZE
];
47 /* any access to the tbs or the page table must use this lock */
48 spinlock_t tb_lock
= SPIN_LOCK_UNLOCKED
;
50 uint8_t code_gen_buffer
[CODE_GEN_BUFFER_SIZE
];
51 uint8_t *code_gen_ptr
;
53 /* XXX: pack the flags in the low bits of the pointer ? */
54 typedef struct PageDesc
{
56 TranslationBlock
*first_tb
;
60 #define L1_BITS (32 - L2_BITS - TARGET_PAGE_BITS)
62 #define L1_SIZE (1 << L1_BITS)
63 #define L2_SIZE (1 << L2_BITS)
65 static void tb_invalidate_page(unsigned long address
);
66 static void io_mem_init(void);
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
;
73 static PageDesc
*l1_map
[L1_SIZE
];
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];
82 char *logfilename
= "/tmp/qemu.log";
86 static void page_init(void)
88 /* NOTE: we can always suppose that host_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
;
96 while ((1 << host_page_bits
) < host_page_size
)
98 host_page_mask
= ~(host_page_size
- 1);
101 /* dump memory mappings */
102 void page_dump(FILE *f
)
104 unsigned long start
, end
;
105 int i
, j
, prot
, prot1
;
108 fprintf(f
, "%-8s %-8s %-8s %s\n",
109 "start", "end", "size", "prot");
113 for(i
= 0; i
<= L1_SIZE
; i
++) {
118 for(j
= 0;j
< L2_SIZE
; j
++) {
124 end
= (i
<< (32 - L1_BITS
)) | (j
<< TARGET_PAGE_BITS
);
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' : '-');
144 static inline PageDesc
*page_find_alloc(unsigned int index
)
148 lp
= &l1_map
[index
>> L2_BITS
];
151 /* allocate if not found */
152 p
= malloc(sizeof(PageDesc
) * L2_SIZE
);
153 memset(p
, 0, sizeof(PageDesc
) * L2_SIZE
);
156 return p
+ (index
& (L2_SIZE
- 1));
159 static inline PageDesc
*page_find(unsigned int index
)
163 p
= l1_map
[index
>> L2_BITS
];
166 return p
+ (index
& (L2_SIZE
- 1));
169 int page_get_flags(unsigned long address
)
173 p
= page_find(address
>> TARGET_PAGE_BITS
);
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 */
182 void page_set_flags(unsigned long start
, unsigned long end
, int flags
)
187 start
= start
& TARGET_PAGE_MASK
;
188 end
= TARGET_PAGE_ALIGN(end
);
189 if (flags
& PAGE_WRITE
)
190 flags
|= PAGE_WRITE_ORG
;
192 for(addr
= start
; addr
< end
; addr
+= TARGET_PAGE_SIZE
) {
193 p
= page_find_alloc(addr
>> TARGET_PAGE_BITS
);
194 /* if the write protection is set, then we invalidate the code
196 if (!(p
->flags
& PAGE_WRITE
) &&
197 (flags
& PAGE_WRITE
) &&
199 tb_invalidate_page(addr
);
203 spin_unlock(&tb_lock
);
206 void cpu_exec_init(void)
209 code_gen_ptr
= code_gen_buffer
;
215 /* set to NULL all the 'first_tb' fields in all PageDescs */
216 static void page_flush_tb(void)
221 for(i
= 0; i
< L1_SIZE
; i
++) {
224 for(j
= 0; j
< L2_SIZE
; j
++)
225 p
[j
].first_tb
= NULL
;
230 /* flush all the translation blocks */
231 /* XXX: tb_flush is currently not thread safe */
236 printf("qemu: flush code_size=%d nb_tbs=%d avg_tb_size=%d\n",
237 code_gen_ptr
- code_gen_buffer
,
239 (code_gen_ptr
- code_gen_buffer
) / nb_tbs
);
242 for(i
= 0;i
< CODE_GEN_HASH_SIZE
; i
++)
245 code_gen_ptr
= code_gen_buffer
;
246 /* XXX: flush processor icache at this point if cache flush is
250 #ifdef DEBUG_TB_CHECK
252 static void tb_invalidate_check(unsigned long address
)
254 TranslationBlock
*tb
;
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
);
268 /* verify that all the pages have correct rights for code */
269 static void tb_page_check(void)
271 TranslationBlock
*tb
;
272 int i
, flags1
, flags2
;
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
);
286 void tb_jmp_check(TranslationBlock
*tb
)
288 TranslationBlock
*tb1
;
291 /* suppress any remaining jumps to this TB */
295 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
298 tb1
= tb1
->jmp_next
[n1
];
300 /* check end of list */
302 printf("ERROR: jmp_list from 0x%08lx\n", (long)tb
);
308 /* invalidate one TB */
309 static inline void tb_remove(TranslationBlock
**ptb
, TranslationBlock
*tb
,
312 TranslationBlock
*tb1
;
316 *ptb
= *(TranslationBlock
**)((char *)tb1
+ next_offset
);
319 ptb
= (TranslationBlock
**)((char *)tb1
+ next_offset
);
323 static inline void tb_jmp_remove(TranslationBlock
*tb
, int n
)
325 TranslationBlock
*tb1
, **ptb
;
328 ptb
= &tb
->jmp_next
[n
];
331 /* find tb(n) in circular list */
335 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
336 if (n1
== n
&& tb1
== tb
)
339 ptb
= &tb1
->jmp_first
;
341 ptb
= &tb1
->jmp_next
[n1
];
344 /* now we can suppress tb(n) from the list */
345 *ptb
= tb
->jmp_next
[n
];
347 tb
->jmp_next
[n
] = NULL
;
351 /* reset the jump entry 'n' of a TB so that it is not chained to
353 static inline void tb_reset_jump(TranslationBlock
*tb
, int n
)
355 tb_set_jmp_target(tb
, n
, (unsigned long)(tb
->tc_ptr
+ tb
->tb_next_offset
[n
]));
358 static inline void tb_invalidate(TranslationBlock
*tb
, int parity
)
361 unsigned int page_index1
, page_index2
;
363 TranslationBlock
*tb1
, *tb2
;
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]));
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]));
383 /* suppress this TB from the two jump lists */
384 tb_jmp_remove(tb
, 0);
385 tb_jmp_remove(tb
, 1);
387 /* suppress any remaining jumps to this TB */
393 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
394 tb2
= tb1
->jmp_next
[n1
];
395 tb_reset_jump(tb1
, n1
);
396 tb1
->jmp_next
[n1
] = NULL
;
399 tb
->jmp_first
= (TranslationBlock
*)((long)tb
| 2); /* fail safe */
402 /* invalidate all TBs which intersect with the target page starting at addr */
403 static void tb_invalidate_page(unsigned long address
)
405 TranslationBlock
*tb_next
, *tb
;
406 unsigned int page_index
;
407 int parity1
, parity2
;
409 #ifdef DEBUG_TB_INVALIDATE
410 printf("tb_invalidate_page: %lx\n", address
);
413 page_index
= address
>> TARGET_PAGE_BITS
;
414 p
= page_find(page_index
);
418 parity1
= page_index
& 1;
419 parity2
= parity1
^ 1;
421 tb_next
= tb
->page_next
[parity1
];
422 tb_invalidate(tb
, parity2
);
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
)
432 unsigned long host_start
, host_end
, addr
, page_addr
;
435 p
= page_find_alloc(page_index
);
436 tb
->page_next
[page_index
& 1] = p
->first_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
;
445 for(addr
= host_start
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
)
446 prot
|= page_get_flags(addr
);
447 #if !defined(CONFIG_SOFTMMU)
448 mprotect((void *)host_start
, host_page_size
,
449 (prot
& PAGE_BITS
) & ~PAGE_WRITE
);
451 #if !defined(CONFIG_USER_ONLY)
452 /* suppress soft TLB */
453 /* XXX: must flush on all processor with same address space */
454 tlb_flush_page_write(cpu_single_env
, host_start
);
456 #ifdef DEBUG_TB_INVALIDATE
457 printf("protecting code page: 0x%08lx\n",
460 p
->flags
&= ~PAGE_WRITE
;
464 /* Allocate a new translation block. Flush the translation buffer if
465 too many translation blocks or too much generated code. */
466 TranslationBlock
*tb_alloc(unsigned long pc
)
468 TranslationBlock
*tb
;
470 if (nb_tbs
>= CODE_GEN_MAX_BLOCKS
||
471 (code_gen_ptr
- code_gen_buffer
) >= CODE_GEN_BUFFER_MAX_SIZE
)
478 /* link the tb with the other TBs */
479 void tb_link(TranslationBlock
*tb
)
481 unsigned int page_index1
, page_index2
;
483 /* add in the page list */
484 page_index1
= tb
->pc
>> TARGET_PAGE_BITS
;
485 tb_alloc_page(tb
, page_index1
);
486 page_index2
= (tb
->pc
+ tb
->size
- 1) >> TARGET_PAGE_BITS
;
487 if (page_index2
!= page_index1
) {
488 tb_alloc_page(tb
, page_index2
);
490 #ifdef DEBUG_TB_CHECK
493 tb
->jmp_first
= (TranslationBlock
*)((long)tb
| 2);
494 tb
->jmp_next
[0] = NULL
;
495 tb
->jmp_next
[1] = NULL
;
497 /* init original jump addresses */
498 if (tb
->tb_next_offset
[0] != 0xffff)
499 tb_reset_jump(tb
, 0);
500 if (tb
->tb_next_offset
[1] != 0xffff)
501 tb_reset_jump(tb
, 1);
504 /* called from signal handler: invalidate the code and unprotect the
505 page. Return TRUE if the fault was succesfully handled. */
506 int page_unprotect(unsigned long address
)
508 unsigned int page_index
, prot
, pindex
;
510 unsigned long host_start
, host_end
, addr
;
512 host_start
= address
& host_page_mask
;
513 page_index
= host_start
>> TARGET_PAGE_BITS
;
514 p1
= page_find(page_index
);
517 host_end
= host_start
+ host_page_size
;
520 for(addr
= host_start
;addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
524 /* if the page was really writable, then we change its
525 protection back to writable */
526 if (prot
& PAGE_WRITE_ORG
) {
527 pindex
= (address
- host_start
) >> TARGET_PAGE_BITS
;
528 if (!(p1
[pindex
].flags
& PAGE_WRITE
)) {
529 #if !defined(CONFIG_SOFTMMU)
530 mprotect((void *)host_start
, host_page_size
,
531 (prot
& PAGE_BITS
) | PAGE_WRITE
);
533 p1
[pindex
].flags
|= PAGE_WRITE
;
534 /* and since the content will be modified, we must invalidate
535 the corresponding translated code. */
536 tb_invalidate_page(address
);
537 #ifdef DEBUG_TB_CHECK
538 tb_invalidate_check(address
);
546 /* call this function when system calls directly modify a memory area */
547 void page_unprotect_range(uint8_t *data
, unsigned long data_size
)
549 unsigned long start
, end
, addr
;
551 start
= (unsigned long)data
;
552 end
= start
+ data_size
;
553 start
&= TARGET_PAGE_MASK
;
554 end
= TARGET_PAGE_ALIGN(end
);
555 for(addr
= start
; addr
< end
; addr
+= TARGET_PAGE_SIZE
) {
556 page_unprotect(addr
);
560 /* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
561 tb[1].tc_ptr. Return NULL if not found */
562 TranslationBlock
*tb_find_pc(unsigned long tc_ptr
)
566 TranslationBlock
*tb
;
570 if (tc_ptr
< (unsigned long)code_gen_buffer
||
571 tc_ptr
>= (unsigned long)code_gen_ptr
)
573 /* binary search (cf Knuth) */
576 while (m_min
<= m_max
) {
577 m
= (m_min
+ m_max
) >> 1;
579 v
= (unsigned long)tb
->tc_ptr
;
582 else if (tc_ptr
< v
) {
591 static void tb_reset_jump_recursive(TranslationBlock
*tb
);
593 static inline void tb_reset_jump_recursive2(TranslationBlock
*tb
, int n
)
595 TranslationBlock
*tb1
, *tb_next
, **ptb
;
598 tb1
= tb
->jmp_next
[n
];
600 /* find head of list */
603 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
606 tb1
= tb1
->jmp_next
[n1
];
608 /* we are now sure now that tb jumps to tb1 */
611 /* remove tb from the jmp_first list */
612 ptb
= &tb_next
->jmp_first
;
616 tb1
= (TranslationBlock
*)((long)tb1
& ~3);
617 if (n1
== n
&& tb1
== tb
)
619 ptb
= &tb1
->jmp_next
[n1
];
621 *ptb
= tb
->jmp_next
[n
];
622 tb
->jmp_next
[n
] = NULL
;
624 /* suppress the jump to next tb in generated code */
625 tb_reset_jump(tb
, n
);
627 /* suppress jumps in the tb on which we could have jump */
628 tb_reset_jump_recursive(tb_next
);
632 static void tb_reset_jump_recursive(TranslationBlock
*tb
)
634 tb_reset_jump_recursive2(tb
, 0);
635 tb_reset_jump_recursive2(tb
, 1);
638 /* add a breakpoint. EXCP_DEBUG is returned by the CPU loop if a
639 breakpoint is reached */
640 int cpu_breakpoint_insert(CPUState
*env
, uint32_t pc
)
642 #if defined(TARGET_I386)
645 for(i
= 0; i
< env
->nb_breakpoints
; i
++) {
646 if (env
->breakpoints
[i
] == pc
)
650 if (env
->nb_breakpoints
>= MAX_BREAKPOINTS
)
652 env
->breakpoints
[env
->nb_breakpoints
++] = pc
;
653 tb_invalidate_page(pc
);
660 /* remove a breakpoint */
661 int cpu_breakpoint_remove(CPUState
*env
, uint32_t pc
)
663 #if defined(TARGET_I386)
665 for(i
= 0; i
< env
->nb_breakpoints
; i
++) {
666 if (env
->breakpoints
[i
] == pc
)
671 memmove(&env
->breakpoints
[i
], &env
->breakpoints
[i
+ 1],
672 (env
->nb_breakpoints
- (i
+ 1)) * sizeof(env
->breakpoints
[0]));
673 env
->nb_breakpoints
--;
674 tb_invalidate_page(pc
);
681 /* enable or disable single step mode. EXCP_DEBUG is returned by the
682 CPU loop after each instruction */
683 void cpu_single_step(CPUState
*env
, int enabled
)
685 #if defined(TARGET_I386)
686 if (env
->singlestep_enabled
!= enabled
) {
687 env
->singlestep_enabled
= enabled
;
688 /* must flush all the translated code to avoid inconsistancies */
694 /* enable or disable low levels log */
695 void cpu_set_log(int log_flags
)
697 loglevel
= log_flags
;
698 if (loglevel
&& !logfile
) {
699 logfile
= fopen(logfilename
, "w");
704 setvbuf(logfile
, NULL
, _IOLBF
, 0);
708 void cpu_set_log_filename(const char *filename
)
710 logfilename
= strdup(filename
);
713 /* mask must never be zero */
714 void cpu_interrupt(CPUState
*env
, int mask
)
716 TranslationBlock
*tb
;
718 env
->interrupt_request
|= mask
;
719 /* if the cpu is currently executing code, we must unlink it and
720 all the potentially executing TB */
721 tb
= env
->current_tb
;
723 tb_reset_jump_recursive(tb
);
728 void cpu_abort(CPUState
*env
, const char *fmt
, ...)
733 fprintf(stderr
, "qemu: fatal: ");
734 vfprintf(stderr
, fmt
, ap
);
735 fprintf(stderr
, "\n");
737 cpu_x86_dump_state(env
, stderr
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
744 /* unmap all maped pages and flush all associated code */
745 void page_unmap(void)
750 for(i
= 0; i
< L1_SIZE
; i
++) {
753 #if !defined(CONFIG_SOFTMMU)
759 for(j
= 0;j
< L2_SIZE
;) {
760 if (p
->flags
& PAGE_VALID
) {
761 addr
= (i
<< (32 - L1_BITS
)) | (j
<< TARGET_PAGE_BITS
);
762 /* we try to find a range to make less syscalls */
766 while (j
< L2_SIZE
&& (p
->flags
& PAGE_VALID
)) {
770 ret
= munmap((void *)addr
, (j
- j1
) << TARGET_PAGE_BITS
);
772 fprintf(stderr
, "Could not unmap page 0x%08lx\n", addr
);
789 void tlb_flush(CPUState
*env
)
791 #if !defined(CONFIG_USER_ONLY)
793 for(i
= 0; i
< CPU_TLB_SIZE
; i
++) {
794 env
->tlb_read
[0][i
].address
= -1;
795 env
->tlb_write
[0][i
].address
= -1;
796 env
->tlb_read
[1][i
].address
= -1;
797 env
->tlb_write
[1][i
].address
= -1;
802 static inline void tlb_flush_entry(CPUTLBEntry
*tlb_entry
, uint32_t addr
)
804 if (addr
== (tlb_entry
->address
&
805 (TARGET_PAGE_MASK
| TLB_INVALID_MASK
)))
806 tlb_entry
->address
= -1;
809 void tlb_flush_page(CPUState
*env
, uint32_t addr
)
811 #if !defined(CONFIG_USER_ONLY)
814 addr
&= TARGET_PAGE_MASK
;
815 i
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
816 tlb_flush_entry(&env
->tlb_read
[0][i
], addr
);
817 tlb_flush_entry(&env
->tlb_write
[0][i
], addr
);
818 tlb_flush_entry(&env
->tlb_read
[1][i
], addr
);
819 tlb_flush_entry(&env
->tlb_write
[1][i
], addr
);
823 /* make all write to page 'addr' trigger a TLB exception to detect
824 self modifying code */
825 void tlb_flush_page_write(CPUState
*env
, uint32_t addr
)
827 #if !defined(CONFIG_USER_ONLY)
830 addr
&= TARGET_PAGE_MASK
;
831 i
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
832 tlb_flush_entry(&env
->tlb_write
[0][i
], addr
);
833 tlb_flush_entry(&env
->tlb_write
[1][i
], addr
);
837 static inline unsigned long *physpage_find_alloc(unsigned int page
)
839 unsigned long **lp
, *p
;
840 unsigned int index
, i
;
842 index
= page
>> TARGET_PAGE_BITS
;
843 lp
= &l1_physmap
[index
>> L2_BITS
];
846 /* allocate if not found */
847 p
= malloc(sizeof(unsigned long) * L2_SIZE
);
848 for(i
= 0; i
< L2_SIZE
; i
++)
849 p
[i
] = IO_MEM_UNASSIGNED
;
852 return p
+ (index
& (L2_SIZE
- 1));
855 /* return NULL if no page defined (unused memory) */
856 unsigned long physpage_find(unsigned long page
)
860 index
= page
>> TARGET_PAGE_BITS
;
861 p
= l1_physmap
[index
>> L2_BITS
];
863 return IO_MEM_UNASSIGNED
;
864 return p
[index
& (L2_SIZE
- 1)];
867 /* register physical memory. 'size' must be a multiple of the target
868 page size. If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
870 void cpu_register_physical_memory(unsigned long start_addr
, unsigned long size
,
873 unsigned long addr
, end_addr
;
876 end_addr
= start_addr
+ size
;
877 for(addr
= start_addr
; addr
< end_addr
; addr
+= TARGET_PAGE_SIZE
) {
878 p
= physpage_find_alloc(addr
);
880 if ((phys_offset
& ~TARGET_PAGE_MASK
) == 0)
881 phys_offset
+= TARGET_PAGE_SIZE
;
885 static uint32_t unassigned_mem_readb(uint32_t addr
)
890 static void unassigned_mem_writeb(uint32_t addr
, uint32_t val
)
894 static CPUReadMemoryFunc
*unassigned_mem_read
[3] = {
895 unassigned_mem_readb
,
896 unassigned_mem_readb
,
897 unassigned_mem_readb
,
900 static CPUWriteMemoryFunc
*unassigned_mem_write
[3] = {
901 unassigned_mem_writeb
,
902 unassigned_mem_writeb
,
903 unassigned_mem_writeb
,
907 static void io_mem_init(void)
910 cpu_register_io_memory(0, unassigned_mem_read
, unassigned_mem_write
);
913 /* mem_read and mem_write are arrays of functions containing the
914 function to access byte (index 0), word (index 1) and dword (index
915 2). All functions must be supplied. If io_index is non zero, the
916 corresponding io zone is modified. If it is zero, a new io zone is
917 allocated. The return value can be used with
918 cpu_register_physical_memory(). (-1) is returned if error. */
919 int cpu_register_io_memory(int io_index
,
920 CPUReadMemoryFunc
**mem_read
,
921 CPUWriteMemoryFunc
**mem_write
)
926 if (io_index
>= IO_MEM_NB_ENTRIES
)
928 io_index
= io_mem_nb
++;
930 if (io_index
>= IO_MEM_NB_ENTRIES
)
934 for(i
= 0;i
< 3; i
++) {
935 io_mem_read
[io_index
][i
] = mem_read
[i
];
936 io_mem_write
[io_index
][i
] = mem_write
[i
];
938 return io_index
<< IO_MEM_SHIFT
;
941 #if !defined(CONFIG_USER_ONLY)
943 #define MMUSUFFIX _cmmu
945 #define env cpu_single_env
948 #include "softmmu_template.h"
951 #include "softmmu_template.h"
954 #include "softmmu_template.h"
957 #include "softmmu_template.h"