bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 1 | /* |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 2 | * virtual page mapping and translated block handling |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 4 | * Copyright (c) 2003 Fabrice Bellard |
| 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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 |
| 19 | */ |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 20 | #include "config.h" |
bellard | d5a8f07 | 2004-09-29 21:15:28 +0000 | [diff] [blame] | 21 | #ifdef _WIN32 |
ths | 4fddf62 | 2007-12-17 04:42:29 +0000 | [diff] [blame] | 22 | #define WIN32_LEAN_AND_MEAN |
bellard | d5a8f07 | 2004-09-29 21:15:28 +0000 | [diff] [blame] | 23 | #include <windows.h> |
| 24 | #else |
bellard | a98d49b | 2004-11-14 16:22:05 +0000 | [diff] [blame] | 25 | #include <sys/types.h> |
bellard | d5a8f07 | 2004-09-29 21:15:28 +0000 | [diff] [blame] | 26 | #include <sys/mman.h> |
| 27 | #endif |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 28 | #include <stdlib.h> |
| 29 | #include <stdio.h> |
| 30 | #include <stdarg.h> |
| 31 | #include <string.h> |
| 32 | #include <errno.h> |
| 33 | #include <unistd.h> |
| 34 | #include <inttypes.h> |
| 35 | |
bellard | 6180a18 | 2003-09-30 21:04:53 +0000 | [diff] [blame] | 36 | #include "cpu.h" |
| 37 | #include "exec-all.h" |
aurel32 | ca10f86 | 2008-04-11 21:35:42 +0000 | [diff] [blame] | 38 | #include "qemu-common.h" |
bellard | b67d9a5 | 2008-05-23 09:57:34 +0000 | [diff] [blame] | 39 | #include "tcg.h" |
pbrook | b3c7724 | 2008-06-30 16:31:04 +0000 | [diff] [blame] | 40 | #include "hw/hw.h" |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 41 | #if defined(CONFIG_USER_ONLY) |
| 42 | #include <qemu.h> |
| 43 | #endif |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 44 | |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 45 | //#define DEBUG_TB_INVALIDATE |
bellard | 66e85a2 | 2003-06-24 13:28:12 +0000 | [diff] [blame] | 46 | //#define DEBUG_FLUSH |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 47 | //#define DEBUG_TLB |
pbrook | 67d3b95 | 2006-12-18 05:03:52 +0000 | [diff] [blame] | 48 | //#define DEBUG_UNASSIGNED |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 49 | |
| 50 | /* make various TB consistency checks */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 51 | //#define DEBUG_TB_CHECK |
| 52 | //#define DEBUG_TLB_CHECK |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 53 | |
ths | 1196be3 | 2007-03-17 15:17:58 +0000 | [diff] [blame] | 54 | //#define DEBUG_IOPORT |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 55 | //#define DEBUG_SUBPAGE |
ths | 1196be3 | 2007-03-17 15:17:58 +0000 | [diff] [blame] | 56 | |
pbrook | 99773bd | 2006-04-16 15:14:59 +0000 | [diff] [blame] | 57 | #if !defined(CONFIG_USER_ONLY) |
| 58 | /* TB consistency checks only implemented for usermode emulation. */ |
| 59 | #undef DEBUG_TB_CHECK |
| 60 | #endif |
| 61 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 62 | #define SMC_BITMAP_USE_THRESHOLD 10 |
| 63 | |
| 64 | #define MMAP_AREA_START 0x00000000 |
| 65 | #define MMAP_AREA_END 0xa8000000 |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 66 | |
bellard | 108c49b | 2005-07-24 12:55:09 +0000 | [diff] [blame] | 67 | #if defined(TARGET_SPARC64) |
| 68 | #define TARGET_PHYS_ADDR_SPACE_BITS 41 |
blueswir1 | 5dcb6b9 | 2007-05-19 12:58:30 +0000 | [diff] [blame] | 69 | #elif defined(TARGET_SPARC) |
| 70 | #define TARGET_PHYS_ADDR_SPACE_BITS 36 |
j_mayer | bedb69e | 2007-04-05 20:08:21 +0000 | [diff] [blame] | 71 | #elif defined(TARGET_ALPHA) |
| 72 | #define TARGET_PHYS_ADDR_SPACE_BITS 42 |
| 73 | #define TARGET_VIRT_ADDR_SPACE_BITS 42 |
bellard | 108c49b | 2005-07-24 12:55:09 +0000 | [diff] [blame] | 74 | #elif defined(TARGET_PPC64) |
| 75 | #define TARGET_PHYS_ADDR_SPACE_BITS 42 |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 76 | #elif defined(TARGET_X86_64) && !defined(USE_KQEMU) |
| 77 | #define TARGET_PHYS_ADDR_SPACE_BITS 42 |
| 78 | #elif defined(TARGET_I386) && !defined(USE_KQEMU) |
| 79 | #define TARGET_PHYS_ADDR_SPACE_BITS 36 |
bellard | 108c49b | 2005-07-24 12:55:09 +0000 | [diff] [blame] | 80 | #else |
| 81 | /* Note: for compatibility with kqemu, we use 32 bits for x86_64 */ |
| 82 | #define TARGET_PHYS_ADDR_SPACE_BITS 32 |
| 83 | #endif |
| 84 | |
pbrook | fab94c0 | 2008-05-24 13:56:15 +0000 | [diff] [blame] | 85 | TranslationBlock *tbs; |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 86 | int code_gen_max_blocks; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 87 | TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE]; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 88 | int nb_tbs; |
bellard | eb51d10 | 2003-05-14 21:51:13 +0000 | [diff] [blame] | 89 | /* any access to the tbs or the page table must use this lock */ |
| 90 | spinlock_t tb_lock = SPIN_LOCK_UNLOCKED; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 91 | |
bellard | 7cb69ca | 2008-05-10 10:55:51 +0000 | [diff] [blame] | 92 | uint8_t code_gen_prologue[1024] __attribute__((aligned (32))); |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 93 | uint8_t *code_gen_buffer; |
| 94 | unsigned long code_gen_buffer_size; |
| 95 | /* threshold to flush the translated code buffer */ |
| 96 | unsigned long code_gen_buffer_max_size; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 97 | uint8_t *code_gen_ptr; |
| 98 | |
pbrook | e2eef17 | 2008-06-08 01:09:01 +0000 | [diff] [blame] | 99 | #if !defined(CONFIG_USER_ONLY) |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 100 | ram_addr_t phys_ram_size; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 101 | int phys_ram_fd; |
| 102 | uint8_t *phys_ram_base; |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 103 | uint8_t *phys_ram_dirty; |
bellard | e9a1ab1 | 2007-02-08 23:08:38 +0000 | [diff] [blame] | 104 | static ram_addr_t phys_ram_alloc_offset = 0; |
pbrook | e2eef17 | 2008-06-08 01:09:01 +0000 | [diff] [blame] | 105 | #endif |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 106 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 107 | CPUState *first_cpu; |
| 108 | /* current CPU in the current thread. It is only valid inside |
| 109 | cpu_exec() */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 110 | CPUState *cpu_single_env; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 111 | /* 0 = Do not count executed instructions. |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 112 | 1 = Precise instruction counting. |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 113 | 2 = Adaptive rate instruction counting. */ |
| 114 | int use_icount = 0; |
| 115 | /* Current instruction counter. While executing translated code this may |
| 116 | include some instructions that have not yet been executed. */ |
| 117 | int64_t qemu_icount; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 118 | |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 119 | typedef struct PageDesc { |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 120 | /* list of TBs intersecting this ram page */ |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 121 | TranslationBlock *first_tb; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 122 | /* in order to optimize self modifying code, we count the number |
| 123 | of lookups we do to a given page to use a bitmap */ |
| 124 | unsigned int code_write_count; |
| 125 | uint8_t *code_bitmap; |
| 126 | #if defined(CONFIG_USER_ONLY) |
| 127 | unsigned long flags; |
| 128 | #endif |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 129 | } PageDesc; |
| 130 | |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 131 | typedef struct PhysPageDesc { |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 132 | /* offset in host memory of the page + io_index in the low bits */ |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 133 | ram_addr_t phys_offset; |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 134 | } PhysPageDesc; |
| 135 | |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 136 | #define L2_BITS 10 |
j_mayer | bedb69e | 2007-04-05 20:08:21 +0000 | [diff] [blame] | 137 | #if defined(CONFIG_USER_ONLY) && defined(TARGET_VIRT_ADDR_SPACE_BITS) |
| 138 | /* XXX: this is a temporary hack for alpha target. |
| 139 | * In the future, this is to be replaced by a multi-level table |
| 140 | * to actually be able to handle the complete 64 bits address space. |
| 141 | */ |
| 142 | #define L1_BITS (TARGET_VIRT_ADDR_SPACE_BITS - L2_BITS - TARGET_PAGE_BITS) |
| 143 | #else |
aurel32 | 0387544 | 2008-04-22 20:45:18 +0000 | [diff] [blame] | 144 | #define L1_BITS (32 - L2_BITS - TARGET_PAGE_BITS) |
j_mayer | bedb69e | 2007-04-05 20:08:21 +0000 | [diff] [blame] | 145 | #endif |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 146 | |
| 147 | #define L1_SIZE (1 << L1_BITS) |
| 148 | #define L2_SIZE (1 << L2_BITS) |
| 149 | |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 150 | unsigned long qemu_real_host_page_size; |
| 151 | unsigned long qemu_host_page_bits; |
| 152 | unsigned long qemu_host_page_size; |
| 153 | unsigned long qemu_host_page_mask; |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 154 | |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 155 | /* XXX: for system emulation, it could just be an array */ |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 156 | static PageDesc *l1_map[L1_SIZE]; |
bellard | 0a962c0 | 2005-02-10 22:00:27 +0000 | [diff] [blame] | 157 | PhysPageDesc **l1_phys_map; |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 158 | |
pbrook | e2eef17 | 2008-06-08 01:09:01 +0000 | [diff] [blame] | 159 | #if !defined(CONFIG_USER_ONLY) |
| 160 | static void io_mem_init(void); |
| 161 | |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 162 | /* io memory support */ |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 163 | CPUWriteMemoryFunc *io_mem_write[IO_MEM_NB_ENTRIES][4]; |
| 164 | CPUReadMemoryFunc *io_mem_read[IO_MEM_NB_ENTRIES][4]; |
bellard | a4193c8 | 2004-06-03 14:01:43 +0000 | [diff] [blame] | 165 | void *io_mem_opaque[IO_MEM_NB_ENTRIES]; |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 166 | static int io_mem_nb; |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 167 | static int io_mem_watch; |
| 168 | #endif |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 169 | |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 170 | /* log support */ |
| 171 | char *logfilename = "/tmp/qemu.log"; |
| 172 | FILE *logfile; |
| 173 | int loglevel; |
pbrook | e735b91 | 2007-06-30 13:53:24 +0000 | [diff] [blame] | 174 | static int log_append = 0; |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 175 | |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 176 | /* statistics */ |
| 177 | static int tlb_flush_count; |
| 178 | static int tb_flush_count; |
| 179 | static int tb_phys_invalidate_count; |
| 180 | |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 181 | #define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK) |
| 182 | typedef struct subpage_t { |
| 183 | target_phys_addr_t base; |
blueswir1 | 3ee8992 | 2008-01-02 19:45:26 +0000 | [diff] [blame] | 184 | CPUReadMemoryFunc **mem_read[TARGET_PAGE_SIZE][4]; |
| 185 | CPUWriteMemoryFunc **mem_write[TARGET_PAGE_SIZE][4]; |
| 186 | void *opaque[TARGET_PAGE_SIZE][2][4]; |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 187 | } subpage_t; |
| 188 | |
bellard | 7cb69ca | 2008-05-10 10:55:51 +0000 | [diff] [blame] | 189 | #ifdef _WIN32 |
| 190 | static void map_exec(void *addr, long size) |
| 191 | { |
| 192 | DWORD old_protect; |
| 193 | VirtualProtect(addr, size, |
| 194 | PAGE_EXECUTE_READWRITE, &old_protect); |
| 195 | |
| 196 | } |
| 197 | #else |
| 198 | static void map_exec(void *addr, long size) |
| 199 | { |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 200 | unsigned long start, end, page_size; |
bellard | 7cb69ca | 2008-05-10 10:55:51 +0000 | [diff] [blame] | 201 | |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 202 | page_size = getpagesize(); |
bellard | 7cb69ca | 2008-05-10 10:55:51 +0000 | [diff] [blame] | 203 | start = (unsigned long)addr; |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 204 | start &= ~(page_size - 1); |
bellard | 7cb69ca | 2008-05-10 10:55:51 +0000 | [diff] [blame] | 205 | |
| 206 | end = (unsigned long)addr + size; |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 207 | end += page_size - 1; |
| 208 | end &= ~(page_size - 1); |
bellard | 7cb69ca | 2008-05-10 10:55:51 +0000 | [diff] [blame] | 209 | |
| 210 | mprotect((void *)start, end - start, |
| 211 | PROT_READ | PROT_WRITE | PROT_EXEC); |
| 212 | } |
| 213 | #endif |
| 214 | |
bellard | b346ff4 | 2003-06-15 20:05:50 +0000 | [diff] [blame] | 215 | static void page_init(void) |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 216 | { |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 217 | /* NOTE: we can always suppose that qemu_host_page_size >= |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 218 | TARGET_PAGE_SIZE */ |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 219 | #ifdef _WIN32 |
bellard | d5a8f07 | 2004-09-29 21:15:28 +0000 | [diff] [blame] | 220 | { |
| 221 | SYSTEM_INFO system_info; |
| 222 | DWORD old_protect; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 223 | |
bellard | d5a8f07 | 2004-09-29 21:15:28 +0000 | [diff] [blame] | 224 | GetSystemInfo(&system_info); |
| 225 | qemu_real_host_page_size = system_info.dwPageSize; |
bellard | d5a8f07 | 2004-09-29 21:15:28 +0000 | [diff] [blame] | 226 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 227 | #else |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 228 | qemu_real_host_page_size = getpagesize(); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 229 | #endif |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 230 | if (qemu_host_page_size == 0) |
| 231 | qemu_host_page_size = qemu_real_host_page_size; |
| 232 | if (qemu_host_page_size < TARGET_PAGE_SIZE) |
| 233 | qemu_host_page_size = TARGET_PAGE_SIZE; |
| 234 | qemu_host_page_bits = 0; |
| 235 | while ((1 << qemu_host_page_bits) < qemu_host_page_size) |
| 236 | qemu_host_page_bits++; |
| 237 | qemu_host_page_mask = ~(qemu_host_page_size - 1); |
bellard | 108c49b | 2005-07-24 12:55:09 +0000 | [diff] [blame] | 238 | l1_phys_map = qemu_vmalloc(L1_SIZE * sizeof(void *)); |
| 239 | memset(l1_phys_map, 0, L1_SIZE * sizeof(void *)); |
balrog | 50a9569 | 2007-12-12 01:16:23 +0000 | [diff] [blame] | 240 | |
| 241 | #if !defined(_WIN32) && defined(CONFIG_USER_ONLY) |
| 242 | { |
| 243 | long long startaddr, endaddr; |
| 244 | FILE *f; |
| 245 | int n; |
| 246 | |
pbrook | c8a706f | 2008-06-02 16:16:42 +0000 | [diff] [blame] | 247 | mmap_lock(); |
pbrook | 0776590 | 2008-05-31 16:33:53 +0000 | [diff] [blame] | 248 | last_brk = (unsigned long)sbrk(0); |
balrog | 50a9569 | 2007-12-12 01:16:23 +0000 | [diff] [blame] | 249 | f = fopen("/proc/self/maps", "r"); |
| 250 | if (f) { |
| 251 | do { |
| 252 | n = fscanf (f, "%llx-%llx %*[^\n]\n", &startaddr, &endaddr); |
| 253 | if (n == 2) { |
blueswir1 | e0b8d65 | 2008-05-03 17:51:24 +0000 | [diff] [blame] | 254 | startaddr = MIN(startaddr, |
| 255 | (1ULL << TARGET_PHYS_ADDR_SPACE_BITS) - 1); |
| 256 | endaddr = MIN(endaddr, |
| 257 | (1ULL << TARGET_PHYS_ADDR_SPACE_BITS) - 1); |
pbrook | b5fc909 | 2008-05-29 13:56:10 +0000 | [diff] [blame] | 258 | page_set_flags(startaddr & TARGET_PAGE_MASK, |
balrog | 50a9569 | 2007-12-12 01:16:23 +0000 | [diff] [blame] | 259 | TARGET_PAGE_ALIGN(endaddr), |
| 260 | PAGE_RESERVED); |
| 261 | } |
| 262 | } while (!feof(f)); |
| 263 | fclose(f); |
| 264 | } |
pbrook | c8a706f | 2008-06-02 16:16:42 +0000 | [diff] [blame] | 265 | mmap_unlock(); |
balrog | 50a9569 | 2007-12-12 01:16:23 +0000 | [diff] [blame] | 266 | } |
| 267 | #endif |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 268 | } |
| 269 | |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 270 | static inline PageDesc *page_find_alloc(target_ulong index) |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 271 | { |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 272 | PageDesc **lp, *p; |
| 273 | |
pbrook | 17e2377 | 2008-06-09 13:47:45 +0000 | [diff] [blame] | 274 | #if TARGET_LONG_BITS > 32 |
| 275 | /* Host memory outside guest VM. For 32-bit targets we have already |
| 276 | excluded high addresses. */ |
| 277 | if (index > ((target_ulong)L2_SIZE * L1_SIZE * TARGET_PAGE_SIZE)) |
| 278 | return NULL; |
| 279 | #endif |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 280 | lp = &l1_map[index >> L2_BITS]; |
| 281 | p = *lp; |
| 282 | if (!p) { |
| 283 | /* allocate if not found */ |
pbrook | 17e2377 | 2008-06-09 13:47:45 +0000 | [diff] [blame] | 284 | #if defined(CONFIG_USER_ONLY) |
| 285 | unsigned long addr; |
| 286 | size_t len = sizeof(PageDesc) * L2_SIZE; |
| 287 | /* Don't use qemu_malloc because it may recurse. */ |
| 288 | p = mmap(0, len, PROT_READ | PROT_WRITE, |
| 289 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 290 | *lp = p; |
pbrook | 17e2377 | 2008-06-09 13:47:45 +0000 | [diff] [blame] | 291 | addr = h2g(p); |
| 292 | if (addr == (target_ulong)addr) { |
| 293 | page_set_flags(addr & TARGET_PAGE_MASK, |
| 294 | TARGET_PAGE_ALIGN(addr + len), |
| 295 | PAGE_RESERVED); |
| 296 | } |
| 297 | #else |
| 298 | p = qemu_mallocz(sizeof(PageDesc) * L2_SIZE); |
| 299 | *lp = p; |
| 300 | #endif |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 301 | } |
| 302 | return p + (index & (L2_SIZE - 1)); |
| 303 | } |
| 304 | |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 305 | static inline PageDesc *page_find(target_ulong index) |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 306 | { |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 307 | PageDesc *p; |
| 308 | |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 309 | p = l1_map[index >> L2_BITS]; |
| 310 | if (!p) |
| 311 | return 0; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 312 | return p + (index & (L2_SIZE - 1)); |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 313 | } |
| 314 | |
bellard | 108c49b | 2005-07-24 12:55:09 +0000 | [diff] [blame] | 315 | static PhysPageDesc *phys_page_find_alloc(target_phys_addr_t index, int alloc) |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 316 | { |
bellard | 108c49b | 2005-07-24 12:55:09 +0000 | [diff] [blame] | 317 | void **lp, **p; |
pbrook | e3f4e2a | 2006-04-08 20:02:06 +0000 | [diff] [blame] | 318 | PhysPageDesc *pd; |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 319 | |
bellard | 108c49b | 2005-07-24 12:55:09 +0000 | [diff] [blame] | 320 | p = (void **)l1_phys_map; |
| 321 | #if TARGET_PHYS_ADDR_SPACE_BITS > 32 |
| 322 | |
| 323 | #if TARGET_PHYS_ADDR_SPACE_BITS > (32 + L1_BITS) |
| 324 | #error unsupported TARGET_PHYS_ADDR_SPACE_BITS |
| 325 | #endif |
| 326 | lp = p + ((index >> (L1_BITS + L2_BITS)) & (L1_SIZE - 1)); |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 327 | p = *lp; |
| 328 | if (!p) { |
| 329 | /* allocate if not found */ |
bellard | 108c49b | 2005-07-24 12:55:09 +0000 | [diff] [blame] | 330 | if (!alloc) |
| 331 | return NULL; |
| 332 | p = qemu_vmalloc(sizeof(void *) * L1_SIZE); |
| 333 | memset(p, 0, sizeof(void *) * L1_SIZE); |
| 334 | *lp = p; |
| 335 | } |
| 336 | #endif |
| 337 | lp = p + ((index >> L2_BITS) & (L1_SIZE - 1)); |
pbrook | e3f4e2a | 2006-04-08 20:02:06 +0000 | [diff] [blame] | 338 | pd = *lp; |
| 339 | if (!pd) { |
| 340 | int i; |
bellard | 108c49b | 2005-07-24 12:55:09 +0000 | [diff] [blame] | 341 | /* allocate if not found */ |
| 342 | if (!alloc) |
| 343 | return NULL; |
pbrook | e3f4e2a | 2006-04-08 20:02:06 +0000 | [diff] [blame] | 344 | pd = qemu_vmalloc(sizeof(PhysPageDesc) * L2_SIZE); |
| 345 | *lp = pd; |
| 346 | for (i = 0; i < L2_SIZE; i++) |
| 347 | pd[i].phys_offset = IO_MEM_UNASSIGNED; |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 348 | } |
pbrook | e3f4e2a | 2006-04-08 20:02:06 +0000 | [diff] [blame] | 349 | return ((PhysPageDesc *)pd) + (index & (L2_SIZE - 1)); |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 350 | } |
| 351 | |
bellard | 108c49b | 2005-07-24 12:55:09 +0000 | [diff] [blame] | 352 | static inline PhysPageDesc *phys_page_find(target_phys_addr_t index) |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 353 | { |
bellard | 108c49b | 2005-07-24 12:55:09 +0000 | [diff] [blame] | 354 | return phys_page_find_alloc(index, 0); |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 355 | } |
| 356 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 357 | #if !defined(CONFIG_USER_ONLY) |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 358 | static void tlb_protect_code(ram_addr_t ram_addr); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 359 | static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr, |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 360 | target_ulong vaddr); |
pbrook | c8a706f | 2008-06-02 16:16:42 +0000 | [diff] [blame] | 361 | #define mmap_lock() do { } while(0) |
| 362 | #define mmap_unlock() do { } while(0) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 363 | #endif |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 364 | |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 365 | #define DEFAULT_CODE_GEN_BUFFER_SIZE (32 * 1024 * 1024) |
| 366 | |
| 367 | #if defined(CONFIG_USER_ONLY) |
| 368 | /* Currently it is not recommanded to allocate big chunks of data in |
| 369 | user mode. It will change when a dedicated libc will be used */ |
| 370 | #define USE_STATIC_CODE_GEN_BUFFER |
| 371 | #endif |
| 372 | |
| 373 | #ifdef USE_STATIC_CODE_GEN_BUFFER |
| 374 | static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE]; |
| 375 | #endif |
| 376 | |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 377 | void code_gen_alloc(unsigned long tb_size) |
| 378 | { |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 379 | #ifdef USE_STATIC_CODE_GEN_BUFFER |
| 380 | code_gen_buffer = static_code_gen_buffer; |
| 381 | code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE; |
| 382 | map_exec(code_gen_buffer, code_gen_buffer_size); |
| 383 | #else |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 384 | code_gen_buffer_size = tb_size; |
| 385 | if (code_gen_buffer_size == 0) { |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 386 | #if defined(CONFIG_USER_ONLY) |
| 387 | /* in user mode, phys_ram_size is not meaningful */ |
| 388 | code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE; |
| 389 | #else |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 390 | /* XXX: needs ajustments */ |
| 391 | code_gen_buffer_size = (int)(phys_ram_size / 4); |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 392 | #endif |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 393 | } |
| 394 | if (code_gen_buffer_size < MIN_CODE_GEN_BUFFER_SIZE) |
| 395 | code_gen_buffer_size = MIN_CODE_GEN_BUFFER_SIZE; |
| 396 | /* The code gen buffer location may have constraints depending on |
| 397 | the host cpu and OS */ |
| 398 | #if defined(__linux__) |
| 399 | { |
| 400 | int flags; |
| 401 | flags = MAP_PRIVATE | MAP_ANONYMOUS; |
| 402 | #if defined(__x86_64__) |
| 403 | flags |= MAP_32BIT; |
| 404 | /* Cannot map more than that */ |
| 405 | if (code_gen_buffer_size > (800 * 1024 * 1024)) |
| 406 | code_gen_buffer_size = (800 * 1024 * 1024); |
| 407 | #endif |
| 408 | code_gen_buffer = mmap(NULL, code_gen_buffer_size, |
| 409 | PROT_WRITE | PROT_READ | PROT_EXEC, |
| 410 | flags, -1, 0); |
| 411 | if (code_gen_buffer == MAP_FAILED) { |
| 412 | fprintf(stderr, "Could not allocate dynamic translator buffer\n"); |
| 413 | exit(1); |
| 414 | } |
| 415 | } |
| 416 | #else |
| 417 | code_gen_buffer = qemu_malloc(code_gen_buffer_size); |
| 418 | if (!code_gen_buffer) { |
| 419 | fprintf(stderr, "Could not allocate dynamic translator buffer\n"); |
| 420 | exit(1); |
| 421 | } |
| 422 | map_exec(code_gen_buffer, code_gen_buffer_size); |
| 423 | #endif |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 424 | #endif /* !USE_STATIC_CODE_GEN_BUFFER */ |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 425 | map_exec(code_gen_prologue, sizeof(code_gen_prologue)); |
| 426 | code_gen_buffer_max_size = code_gen_buffer_size - |
| 427 | code_gen_max_block_size(); |
| 428 | code_gen_max_blocks = code_gen_buffer_size / CODE_GEN_AVG_BLOCK_SIZE; |
| 429 | tbs = qemu_malloc(code_gen_max_blocks * sizeof(TranslationBlock)); |
| 430 | } |
| 431 | |
| 432 | /* Must be called before using the QEMU cpus. 'tb_size' is the size |
| 433 | (in bytes) allocated to the translation buffer. Zero means default |
| 434 | size. */ |
| 435 | void cpu_exec_init_all(unsigned long tb_size) |
| 436 | { |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 437 | cpu_gen_init(); |
| 438 | code_gen_alloc(tb_size); |
| 439 | code_gen_ptr = code_gen_buffer; |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 440 | page_init(); |
pbrook | e2eef17 | 2008-06-08 01:09:01 +0000 | [diff] [blame] | 441 | #if !defined(CONFIG_USER_ONLY) |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 442 | io_mem_init(); |
pbrook | e2eef17 | 2008-06-08 01:09:01 +0000 | [diff] [blame] | 443 | #endif |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 444 | } |
| 445 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 446 | void cpu_exec_init(CPUState *env) |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 447 | { |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 448 | CPUState **penv; |
| 449 | int cpu_index; |
| 450 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 451 | env->next_cpu = NULL; |
| 452 | penv = &first_cpu; |
| 453 | cpu_index = 0; |
| 454 | while (*penv != NULL) { |
| 455 | penv = (CPUState **)&(*penv)->next_cpu; |
| 456 | cpu_index++; |
| 457 | } |
| 458 | env->cpu_index = cpu_index; |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 459 | env->nb_watchpoints = 0; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 460 | *penv = env; |
pbrook | b3c7724 | 2008-06-30 16:31:04 +0000 | [diff] [blame] | 461 | #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY) |
| 462 | register_savevm("cpu", cpu_index, CPU_SAVE_VERSION, |
| 463 | cpu_save, cpu_load, env); |
| 464 | #endif |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 465 | } |
| 466 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 467 | static inline void invalidate_page_bitmap(PageDesc *p) |
| 468 | { |
| 469 | if (p->code_bitmap) { |
bellard | 59817cc | 2004-02-16 22:01:13 +0000 | [diff] [blame] | 470 | qemu_free(p->code_bitmap); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 471 | p->code_bitmap = NULL; |
| 472 | } |
| 473 | p->code_write_count = 0; |
| 474 | } |
| 475 | |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 476 | /* set to NULL all the 'first_tb' fields in all PageDescs */ |
| 477 | static void page_flush_tb(void) |
| 478 | { |
| 479 | int i, j; |
| 480 | PageDesc *p; |
| 481 | |
| 482 | for(i = 0; i < L1_SIZE; i++) { |
| 483 | p = l1_map[i]; |
| 484 | if (p) { |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 485 | for(j = 0; j < L2_SIZE; j++) { |
| 486 | p->first_tb = NULL; |
| 487 | invalidate_page_bitmap(p); |
| 488 | p++; |
| 489 | } |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | /* flush all the translation blocks */ |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 495 | /* XXX: tb_flush is currently not thread safe */ |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 496 | void tb_flush(CPUState *env1) |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 497 | { |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 498 | CPUState *env; |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 499 | #if defined(DEBUG_FLUSH) |
blueswir1 | ab3d172 | 2007-11-04 07:31:40 +0000 | [diff] [blame] | 500 | printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n", |
| 501 | (unsigned long)(code_gen_ptr - code_gen_buffer), |
| 502 | nb_tbs, nb_tbs > 0 ? |
| 503 | ((unsigned long)(code_gen_ptr - code_gen_buffer)) / nb_tbs : 0); |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 504 | #endif |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 505 | if ((unsigned long)(code_gen_ptr - code_gen_buffer) > code_gen_buffer_size) |
pbrook | a208e54 | 2008-03-31 17:07:36 +0000 | [diff] [blame] | 506 | cpu_abort(env1, "Internal error: code buffer overflow\n"); |
| 507 | |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 508 | nb_tbs = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 509 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 510 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
| 511 | memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *)); |
| 512 | } |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 513 | |
bellard | 8a8a608 | 2004-10-03 13:36:49 +0000 | [diff] [blame] | 514 | memset (tb_phys_hash, 0, CODE_GEN_PHYS_HASH_SIZE * sizeof (void *)); |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 515 | page_flush_tb(); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 516 | |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 517 | code_gen_ptr = code_gen_buffer; |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 518 | /* XXX: flush processor icache at this point if cache flush is |
| 519 | expensive */ |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 520 | tb_flush_count++; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | #ifdef DEBUG_TB_CHECK |
| 524 | |
j_mayer | bc98a7e | 2007-04-04 07:55:12 +0000 | [diff] [blame] | 525 | static void tb_invalidate_check(target_ulong address) |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 526 | { |
| 527 | TranslationBlock *tb; |
| 528 | int i; |
| 529 | address &= TARGET_PAGE_MASK; |
pbrook | 99773bd | 2006-04-16 15:14:59 +0000 | [diff] [blame] | 530 | for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) { |
| 531 | for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) { |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 532 | if (!(address + TARGET_PAGE_SIZE <= tb->pc || |
| 533 | address >= tb->pc + tb->size)) { |
| 534 | printf("ERROR invalidate: address=%08lx PC=%08lx size=%04x\n", |
pbrook | 99773bd | 2006-04-16 15:14:59 +0000 | [diff] [blame] | 535 | address, (long)tb->pc, tb->size); |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | /* verify that all the pages have correct rights for code */ |
| 542 | static void tb_page_check(void) |
| 543 | { |
| 544 | TranslationBlock *tb; |
| 545 | int i, flags1, flags2; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 546 | |
pbrook | 99773bd | 2006-04-16 15:14:59 +0000 | [diff] [blame] | 547 | for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) { |
| 548 | for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) { |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 549 | flags1 = page_get_flags(tb->pc); |
| 550 | flags2 = page_get_flags(tb->pc + tb->size - 1); |
| 551 | if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) { |
| 552 | printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n", |
pbrook | 99773bd | 2006-04-16 15:14:59 +0000 | [diff] [blame] | 553 | (long)tb->pc, tb->size, flags1, flags2); |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 559 | void tb_jmp_check(TranslationBlock *tb) |
| 560 | { |
| 561 | TranslationBlock *tb1; |
| 562 | unsigned int n1; |
| 563 | |
| 564 | /* suppress any remaining jumps to this TB */ |
| 565 | tb1 = tb->jmp_first; |
| 566 | for(;;) { |
| 567 | n1 = (long)tb1 & 3; |
| 568 | tb1 = (TranslationBlock *)((long)tb1 & ~3); |
| 569 | if (n1 == 2) |
| 570 | break; |
| 571 | tb1 = tb1->jmp_next[n1]; |
| 572 | } |
| 573 | /* check end of list */ |
| 574 | if (tb1 != tb) { |
| 575 | printf("ERROR: jmp_list from 0x%08lx\n", (long)tb); |
| 576 | } |
| 577 | } |
| 578 | |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 579 | #endif |
| 580 | |
| 581 | /* invalidate one TB */ |
| 582 | static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb, |
| 583 | int next_offset) |
| 584 | { |
| 585 | TranslationBlock *tb1; |
| 586 | for(;;) { |
| 587 | tb1 = *ptb; |
| 588 | if (tb1 == tb) { |
| 589 | *ptb = *(TranslationBlock **)((char *)tb1 + next_offset); |
| 590 | break; |
| 591 | } |
| 592 | ptb = (TranslationBlock **)((char *)tb1 + next_offset); |
| 593 | } |
| 594 | } |
| 595 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 596 | static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb) |
| 597 | { |
| 598 | TranslationBlock *tb1; |
| 599 | unsigned int n1; |
| 600 | |
| 601 | for(;;) { |
| 602 | tb1 = *ptb; |
| 603 | n1 = (long)tb1 & 3; |
| 604 | tb1 = (TranslationBlock *)((long)tb1 & ~3); |
| 605 | if (tb1 == tb) { |
| 606 | *ptb = tb1->page_next[n1]; |
| 607 | break; |
| 608 | } |
| 609 | ptb = &tb1->page_next[n1]; |
| 610 | } |
| 611 | } |
| 612 | |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 613 | static inline void tb_jmp_remove(TranslationBlock *tb, int n) |
| 614 | { |
| 615 | TranslationBlock *tb1, **ptb; |
| 616 | unsigned int n1; |
| 617 | |
| 618 | ptb = &tb->jmp_next[n]; |
| 619 | tb1 = *ptb; |
| 620 | if (tb1) { |
| 621 | /* find tb(n) in circular list */ |
| 622 | for(;;) { |
| 623 | tb1 = *ptb; |
| 624 | n1 = (long)tb1 & 3; |
| 625 | tb1 = (TranslationBlock *)((long)tb1 & ~3); |
| 626 | if (n1 == n && tb1 == tb) |
| 627 | break; |
| 628 | if (n1 == 2) { |
| 629 | ptb = &tb1->jmp_first; |
| 630 | } else { |
| 631 | ptb = &tb1->jmp_next[n1]; |
| 632 | } |
| 633 | } |
| 634 | /* now we can suppress tb(n) from the list */ |
| 635 | *ptb = tb->jmp_next[n]; |
| 636 | |
| 637 | tb->jmp_next[n] = NULL; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | /* reset the jump entry 'n' of a TB so that it is not chained to |
| 642 | another TB */ |
| 643 | static inline void tb_reset_jump(TranslationBlock *tb, int n) |
| 644 | { |
| 645 | tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n])); |
| 646 | } |
| 647 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 648 | void tb_phys_invalidate(TranslationBlock *tb, target_ulong page_addr) |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 649 | { |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 650 | CPUState *env; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 651 | PageDesc *p; |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 652 | unsigned int h, n1; |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 653 | target_phys_addr_t phys_pc; |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 654 | TranslationBlock *tb1, *tb2; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 655 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 656 | /* remove the TB from the hash list */ |
| 657 | phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK); |
| 658 | h = tb_phys_hash_func(phys_pc); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 659 | tb_remove(&tb_phys_hash[h], tb, |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 660 | offsetof(TranslationBlock, phys_hash_next)); |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 661 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 662 | /* remove the TB from the page list */ |
| 663 | if (tb->page_addr[0] != page_addr) { |
| 664 | p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS); |
| 665 | tb_page_remove(&p->first_tb, tb); |
| 666 | invalidate_page_bitmap(p); |
| 667 | } |
| 668 | if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) { |
| 669 | p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS); |
| 670 | tb_page_remove(&p->first_tb, tb); |
| 671 | invalidate_page_bitmap(p); |
| 672 | } |
| 673 | |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 674 | tb_invalidated_flag = 1; |
| 675 | |
| 676 | /* remove the TB from the hash list */ |
| 677 | h = tb_jmp_cache_hash_func(tb->pc); |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 678 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
| 679 | if (env->tb_jmp_cache[h] == tb) |
| 680 | env->tb_jmp_cache[h] = NULL; |
| 681 | } |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 682 | |
| 683 | /* suppress this TB from the two jump lists */ |
| 684 | tb_jmp_remove(tb, 0); |
| 685 | tb_jmp_remove(tb, 1); |
| 686 | |
| 687 | /* suppress any remaining jumps to this TB */ |
| 688 | tb1 = tb->jmp_first; |
| 689 | for(;;) { |
| 690 | n1 = (long)tb1 & 3; |
| 691 | if (n1 == 2) |
| 692 | break; |
| 693 | tb1 = (TranslationBlock *)((long)tb1 & ~3); |
| 694 | tb2 = tb1->jmp_next[n1]; |
| 695 | tb_reset_jump(tb1, n1); |
| 696 | tb1->jmp_next[n1] = NULL; |
| 697 | tb1 = tb2; |
| 698 | } |
| 699 | tb->jmp_first = (TranslationBlock *)((long)tb | 2); /* fail safe */ |
| 700 | |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 701 | tb_phys_invalidate_count++; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | static inline void set_bits(uint8_t *tab, int start, int len) |
| 705 | { |
| 706 | int end, mask, end1; |
| 707 | |
| 708 | end = start + len; |
| 709 | tab += start >> 3; |
| 710 | mask = 0xff << (start & 7); |
| 711 | if ((start & ~7) == (end & ~7)) { |
| 712 | if (start < end) { |
| 713 | mask &= ~(0xff << (end & 7)); |
| 714 | *tab |= mask; |
| 715 | } |
| 716 | } else { |
| 717 | *tab++ |= mask; |
| 718 | start = (start + 8) & ~7; |
| 719 | end1 = end & ~7; |
| 720 | while (start < end1) { |
| 721 | *tab++ = 0xff; |
| 722 | start += 8; |
| 723 | } |
| 724 | if (start < end) { |
| 725 | mask = ~(0xff << (end & 7)); |
| 726 | *tab |= mask; |
| 727 | } |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | static void build_page_bitmap(PageDesc *p) |
| 732 | { |
| 733 | int n, tb_start, tb_end; |
| 734 | TranslationBlock *tb; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 735 | |
pbrook | b2a7081 | 2008-06-09 13:57:23 +0000 | [diff] [blame] | 736 | p->code_bitmap = qemu_mallocz(TARGET_PAGE_SIZE / 8); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 737 | if (!p->code_bitmap) |
| 738 | return; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 739 | |
| 740 | tb = p->first_tb; |
| 741 | while (tb != NULL) { |
| 742 | n = (long)tb & 3; |
| 743 | tb = (TranslationBlock *)((long)tb & ~3); |
| 744 | /* NOTE: this is subtle as a TB may span two physical pages */ |
| 745 | if (n == 0) { |
| 746 | /* NOTE: tb_end may be after the end of the page, but |
| 747 | it is not a problem */ |
| 748 | tb_start = tb->pc & ~TARGET_PAGE_MASK; |
| 749 | tb_end = tb_start + tb->size; |
| 750 | if (tb_end > TARGET_PAGE_SIZE) |
| 751 | tb_end = TARGET_PAGE_SIZE; |
| 752 | } else { |
| 753 | tb_start = 0; |
| 754 | tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK); |
| 755 | } |
| 756 | set_bits(p->code_bitmap, tb_start, tb_end - tb_start); |
| 757 | tb = tb->page_next[n]; |
| 758 | } |
| 759 | } |
| 760 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 761 | TranslationBlock *tb_gen_code(CPUState *env, |
| 762 | target_ulong pc, target_ulong cs_base, |
| 763 | int flags, int cflags) |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 764 | { |
| 765 | TranslationBlock *tb; |
| 766 | uint8_t *tc_ptr; |
| 767 | target_ulong phys_pc, phys_page2, virt_page2; |
| 768 | int code_gen_size; |
| 769 | |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 770 | phys_pc = get_phys_addr_code(env, pc); |
| 771 | tb = tb_alloc(pc); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 772 | if (!tb) { |
| 773 | /* flush must be done */ |
| 774 | tb_flush(env); |
| 775 | /* cannot fail at this point */ |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 776 | tb = tb_alloc(pc); |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 777 | /* Don't forget to invalidate previous TB info. */ |
| 778 | tb_invalidated_flag = 1; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 779 | } |
| 780 | tc_ptr = code_gen_ptr; |
| 781 | tb->tc_ptr = tc_ptr; |
| 782 | tb->cs_base = cs_base; |
| 783 | tb->flags = flags; |
| 784 | tb->cflags = cflags; |
blueswir1 | d07bde8 | 2007-12-11 19:35:45 +0000 | [diff] [blame] | 785 | cpu_gen_code(env, tb, &code_gen_size); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 786 | code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1)); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 787 | |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 788 | /* check next page if needed */ |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 789 | virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 790 | phys_page2 = -1; |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 791 | if ((pc & TARGET_PAGE_MASK) != virt_page2) { |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 792 | phys_page2 = get_phys_addr_code(env, virt_page2); |
| 793 | } |
| 794 | tb_link_phys(tb, phys_pc, phys_page2); |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 795 | return tb; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 796 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 797 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 798 | /* invalidate all TBs which intersect with the target physical page |
| 799 | starting in range [start;end[. NOTE: start and end must refer to |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 800 | the same physical page. 'is_cpu_write_access' should be true if called |
| 801 | from a real cpu write access: the virtual CPU will exit the current |
| 802 | TB if code is modified inside this TB. */ |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 803 | void tb_invalidate_phys_page_range(target_phys_addr_t start, target_phys_addr_t end, |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 804 | int is_cpu_write_access) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 805 | { |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 806 | int n, current_tb_modified, current_tb_not_found, current_flags; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 807 | CPUState *env = cpu_single_env; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 808 | PageDesc *p; |
bellard | ea1c180 | 2004-06-14 18:56:36 +0000 | [diff] [blame] | 809 | TranslationBlock *tb, *tb_next, *current_tb, *saved_tb; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 810 | target_ulong tb_start, tb_end; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 811 | target_ulong current_pc, current_cs_base; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 812 | |
| 813 | p = page_find(start >> TARGET_PAGE_BITS); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 814 | if (!p) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 815 | return; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 816 | if (!p->code_bitmap && |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 817 | ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD && |
| 818 | is_cpu_write_access) { |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 819 | /* build code bitmap */ |
| 820 | build_page_bitmap(p); |
| 821 | } |
| 822 | |
| 823 | /* we remove all the TBs in the range [start, end[ */ |
| 824 | /* XXX: see if in some cases it could be faster to invalidate all the code */ |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 825 | current_tb_not_found = is_cpu_write_access; |
| 826 | current_tb_modified = 0; |
| 827 | current_tb = NULL; /* avoid warning */ |
| 828 | current_pc = 0; /* avoid warning */ |
| 829 | current_cs_base = 0; /* avoid warning */ |
| 830 | current_flags = 0; /* avoid warning */ |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 831 | tb = p->first_tb; |
| 832 | while (tb != NULL) { |
| 833 | n = (long)tb & 3; |
| 834 | tb = (TranslationBlock *)((long)tb & ~3); |
| 835 | tb_next = tb->page_next[n]; |
| 836 | /* NOTE: this is subtle as a TB may span two physical pages */ |
| 837 | if (n == 0) { |
| 838 | /* NOTE: tb_end may be after the end of the page, but |
| 839 | it is not a problem */ |
| 840 | tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK); |
| 841 | tb_end = tb_start + tb->size; |
| 842 | } else { |
| 843 | tb_start = tb->page_addr[1]; |
| 844 | tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK); |
| 845 | } |
| 846 | if (!(tb_end <= start || tb_start >= end)) { |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 847 | #ifdef TARGET_HAS_PRECISE_SMC |
| 848 | if (current_tb_not_found) { |
| 849 | current_tb_not_found = 0; |
| 850 | current_tb = NULL; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 851 | if (env->mem_io_pc) { |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 852 | /* now we have a real cpu fault */ |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 853 | current_tb = tb_find_pc(env->mem_io_pc); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 854 | } |
| 855 | } |
| 856 | if (current_tb == tb && |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 857 | (current_tb->cflags & CF_COUNT_MASK) != 1) { |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 858 | /* If we are modifying the current TB, we must stop |
| 859 | its execution. We could be more precise by checking |
| 860 | that the modification is after the current PC, but it |
| 861 | would require a specialized function to partially |
| 862 | restore the CPU state */ |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 863 | |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 864 | current_tb_modified = 1; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 865 | cpu_restore_state(current_tb, env, |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 866 | env->mem_io_pc, NULL); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 867 | #if defined(TARGET_I386) |
| 868 | current_flags = env->hflags; |
| 869 | current_flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK)); |
| 870 | current_cs_base = (target_ulong)env->segs[R_CS].base; |
| 871 | current_pc = current_cs_base + env->eip; |
| 872 | #else |
| 873 | #error unsupported CPU |
| 874 | #endif |
| 875 | } |
| 876 | #endif /* TARGET_HAS_PRECISE_SMC */ |
bellard | 6f5a9f7 | 2005-11-26 20:12:28 +0000 | [diff] [blame] | 877 | /* we need to do that to handle the case where a signal |
| 878 | occurs while doing tb_phys_invalidate() */ |
| 879 | saved_tb = NULL; |
| 880 | if (env) { |
| 881 | saved_tb = env->current_tb; |
| 882 | env->current_tb = NULL; |
| 883 | } |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 884 | tb_phys_invalidate(tb, -1); |
bellard | 6f5a9f7 | 2005-11-26 20:12:28 +0000 | [diff] [blame] | 885 | if (env) { |
| 886 | env->current_tb = saved_tb; |
| 887 | if (env->interrupt_request && env->current_tb) |
| 888 | cpu_interrupt(env, env->interrupt_request); |
| 889 | } |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 890 | } |
| 891 | tb = tb_next; |
| 892 | } |
| 893 | #if !defined(CONFIG_USER_ONLY) |
| 894 | /* if no code remaining, no need to continue to use slow writes */ |
| 895 | if (!p->first_tb) { |
| 896 | invalidate_page_bitmap(p); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 897 | if (is_cpu_write_access) { |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 898 | tlb_unprotect_code_phys(env, start, env->mem_io_vaddr); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | #endif |
| 902 | #ifdef TARGET_HAS_PRECISE_SMC |
| 903 | if (current_tb_modified) { |
| 904 | /* we generate a block containing just the instruction |
| 905 | modifying the memory. It will ensure that it cannot modify |
| 906 | itself */ |
bellard | ea1c180 | 2004-06-14 18:56:36 +0000 | [diff] [blame] | 907 | env->current_tb = NULL; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 908 | tb_gen_code(env, current_pc, current_cs_base, current_flags, 1); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 909 | cpu_resume_from_signal(env, NULL); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 910 | } |
| 911 | #endif |
| 912 | } |
| 913 | |
| 914 | /* len must be <= 8 and start must be a multiple of len */ |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 915 | static inline void tb_invalidate_phys_page_fast(target_phys_addr_t start, int len) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 916 | { |
| 917 | PageDesc *p; |
| 918 | int offset, b; |
bellard | 59817cc | 2004-02-16 22:01:13 +0000 | [diff] [blame] | 919 | #if 0 |
bellard | a4193c8 | 2004-06-03 14:01:43 +0000 | [diff] [blame] | 920 | if (1) { |
| 921 | if (loglevel) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 922 | fprintf(logfile, "modifying code at 0x%x size=%d EIP=%x PC=%08x\n", |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 923 | cpu_single_env->mem_io_vaddr, len, |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 924 | cpu_single_env->eip, |
bellard | a4193c8 | 2004-06-03 14:01:43 +0000 | [diff] [blame] | 925 | cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base); |
| 926 | } |
bellard | 59817cc | 2004-02-16 22:01:13 +0000 | [diff] [blame] | 927 | } |
| 928 | #endif |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 929 | p = page_find(start >> TARGET_PAGE_BITS); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 930 | if (!p) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 931 | return; |
| 932 | if (p->code_bitmap) { |
| 933 | offset = start & ~TARGET_PAGE_MASK; |
| 934 | b = p->code_bitmap[offset >> 3] >> (offset & 7); |
| 935 | if (b & ((1 << len) - 1)) |
| 936 | goto do_invalidate; |
| 937 | } else { |
| 938 | do_invalidate: |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 939 | tb_invalidate_phys_page_range(start, start + len, 1); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 940 | } |
| 941 | } |
| 942 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 943 | #if !defined(CONFIG_SOFTMMU) |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 944 | static void tb_invalidate_phys_page(target_phys_addr_t addr, |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 945 | unsigned long pc, void *puc) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 946 | { |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 947 | int n, current_flags, current_tb_modified; |
| 948 | target_ulong current_pc, current_cs_base; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 949 | PageDesc *p; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 950 | TranslationBlock *tb, *current_tb; |
| 951 | #ifdef TARGET_HAS_PRECISE_SMC |
| 952 | CPUState *env = cpu_single_env; |
| 953 | #endif |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 954 | |
| 955 | addr &= TARGET_PAGE_MASK; |
| 956 | p = page_find(addr >> TARGET_PAGE_BITS); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 957 | if (!p) |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 958 | return; |
| 959 | tb = p->first_tb; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 960 | current_tb_modified = 0; |
| 961 | current_tb = NULL; |
| 962 | current_pc = 0; /* avoid warning */ |
| 963 | current_cs_base = 0; /* avoid warning */ |
| 964 | current_flags = 0; /* avoid warning */ |
| 965 | #ifdef TARGET_HAS_PRECISE_SMC |
| 966 | if (tb && pc != 0) { |
| 967 | current_tb = tb_find_pc(pc); |
| 968 | } |
| 969 | #endif |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 970 | while (tb != NULL) { |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 971 | n = (long)tb & 3; |
| 972 | tb = (TranslationBlock *)((long)tb & ~3); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 973 | #ifdef TARGET_HAS_PRECISE_SMC |
| 974 | if (current_tb == tb && |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 975 | (current_tb->cflags & CF_COUNT_MASK) != 1) { |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 976 | /* If we are modifying the current TB, we must stop |
| 977 | its execution. We could be more precise by checking |
| 978 | that the modification is after the current PC, but it |
| 979 | would require a specialized function to partially |
| 980 | restore the CPU state */ |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 981 | |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 982 | current_tb_modified = 1; |
| 983 | cpu_restore_state(current_tb, env, pc, puc); |
| 984 | #if defined(TARGET_I386) |
| 985 | current_flags = env->hflags; |
| 986 | current_flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK)); |
| 987 | current_cs_base = (target_ulong)env->segs[R_CS].base; |
| 988 | current_pc = current_cs_base + env->eip; |
| 989 | #else |
| 990 | #error unsupported CPU |
| 991 | #endif |
| 992 | } |
| 993 | #endif /* TARGET_HAS_PRECISE_SMC */ |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 994 | tb_phys_invalidate(tb, addr); |
| 995 | tb = tb->page_next[n]; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 996 | } |
| 997 | p->first_tb = NULL; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 998 | #ifdef TARGET_HAS_PRECISE_SMC |
| 999 | if (current_tb_modified) { |
| 1000 | /* we generate a block containing just the instruction |
| 1001 | modifying the memory. It will ensure that it cannot modify |
| 1002 | itself */ |
bellard | ea1c180 | 2004-06-14 18:56:36 +0000 | [diff] [blame] | 1003 | env->current_tb = NULL; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1004 | tb_gen_code(env, current_pc, current_cs_base, current_flags, 1); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1005 | cpu_resume_from_signal(env, puc); |
| 1006 | } |
| 1007 | #endif |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1008 | } |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1009 | #endif |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1010 | |
| 1011 | /* add the tb in the target page and protect it if necessary */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1012 | static inline void tb_alloc_page(TranslationBlock *tb, |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1013 | unsigned int n, target_ulong page_addr) |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1014 | { |
| 1015 | PageDesc *p; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1016 | TranslationBlock *last_first_tb; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1017 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1018 | tb->page_addr[n] = page_addr; |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 1019 | p = page_find_alloc(page_addr >> TARGET_PAGE_BITS); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1020 | tb->page_next[n] = p->first_tb; |
| 1021 | last_first_tb = p->first_tb; |
| 1022 | p->first_tb = (TranslationBlock *)((long)tb | n); |
| 1023 | invalidate_page_bitmap(p); |
| 1024 | |
bellard | 107db44 | 2004-06-22 18:48:46 +0000 | [diff] [blame] | 1025 | #if defined(TARGET_HAS_SMC) || 1 |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1026 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1027 | #if defined(CONFIG_USER_ONLY) |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1028 | if (p->flags & PAGE_WRITE) { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1029 | target_ulong addr; |
| 1030 | PageDesc *p2; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1031 | int prot; |
| 1032 | |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1033 | /* force the host page as non writable (writes will have a |
| 1034 | page fault + mprotect overhead) */ |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1035 | page_addr &= qemu_host_page_mask; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1036 | prot = 0; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1037 | for(addr = page_addr; addr < page_addr + qemu_host_page_size; |
| 1038 | addr += TARGET_PAGE_SIZE) { |
| 1039 | |
| 1040 | p2 = page_find (addr >> TARGET_PAGE_BITS); |
| 1041 | if (!p2) |
| 1042 | continue; |
| 1043 | prot |= p2->flags; |
| 1044 | p2->flags &= ~PAGE_WRITE; |
| 1045 | page_get_flags(addr); |
| 1046 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1047 | mprotect(g2h(page_addr), qemu_host_page_size, |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1048 | (prot & PAGE_BITS) & ~PAGE_WRITE); |
| 1049 | #ifdef DEBUG_TB_INVALIDATE |
blueswir1 | ab3d172 | 2007-11-04 07:31:40 +0000 | [diff] [blame] | 1050 | printf("protecting code page: 0x" TARGET_FMT_lx "\n", |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1051 | page_addr); |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1052 | #endif |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1053 | } |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1054 | #else |
| 1055 | /* if some code is already present, then the pages are already |
| 1056 | protected. So we handle the case where only the first TB is |
| 1057 | allocated in a physical page */ |
| 1058 | if (!last_first_tb) { |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1059 | tlb_protect_code(page_addr); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1060 | } |
| 1061 | #endif |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1062 | |
| 1063 | #endif /* TARGET_HAS_SMC */ |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | /* Allocate a new translation block. Flush the translation buffer if |
| 1067 | too many translation blocks or too much generated code. */ |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 1068 | TranslationBlock *tb_alloc(target_ulong pc) |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1069 | { |
| 1070 | TranslationBlock *tb; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1071 | |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 1072 | if (nb_tbs >= code_gen_max_blocks || |
| 1073 | (code_gen_ptr - code_gen_buffer) >= code_gen_buffer_max_size) |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 1074 | return NULL; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1075 | tb = &tbs[nb_tbs++]; |
| 1076 | tb->pc = pc; |
bellard | b448f2f | 2004-02-25 23:24:04 +0000 | [diff] [blame] | 1077 | tb->cflags = 0; |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 1078 | return tb; |
| 1079 | } |
| 1080 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1081 | void tb_free(TranslationBlock *tb) |
| 1082 | { |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 1083 | /* In practice this is mostly used for single use temporary TB |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1084 | Ignore the hard cases and just back up if this TB happens to |
| 1085 | be the last one generated. */ |
| 1086 | if (nb_tbs > 0 && tb == &tbs[nb_tbs - 1]) { |
| 1087 | code_gen_ptr = tb->tc_ptr; |
| 1088 | nb_tbs--; |
| 1089 | } |
| 1090 | } |
| 1091 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1092 | /* add a new TB and link it to the physical page tables. phys_page2 is |
| 1093 | (-1) to indicate that only one page contains the TB. */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1094 | void tb_link_phys(TranslationBlock *tb, |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1095 | target_ulong phys_pc, target_ulong phys_page2) |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 1096 | { |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1097 | unsigned int h; |
| 1098 | TranslationBlock **ptb; |
| 1099 | |
pbrook | c8a706f | 2008-06-02 16:16:42 +0000 | [diff] [blame] | 1100 | /* Grab the mmap lock to stop another thread invalidating this TB |
| 1101 | before we are done. */ |
| 1102 | mmap_lock(); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1103 | /* add in the physical hash table */ |
| 1104 | h = tb_phys_hash_func(phys_pc); |
| 1105 | ptb = &tb_phys_hash[h]; |
| 1106 | tb->phys_hash_next = *ptb; |
| 1107 | *ptb = tb; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1108 | |
| 1109 | /* add in the page list */ |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1110 | tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK); |
| 1111 | if (phys_page2 != -1) |
| 1112 | tb_alloc_page(tb, 1, phys_page2); |
| 1113 | else |
| 1114 | tb->page_addr[1] = -1; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1115 | |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 1116 | tb->jmp_first = (TranslationBlock *)((long)tb | 2); |
| 1117 | tb->jmp_next[0] = NULL; |
| 1118 | tb->jmp_next[1] = NULL; |
| 1119 | |
| 1120 | /* init original jump addresses */ |
| 1121 | if (tb->tb_next_offset[0] != 0xffff) |
| 1122 | tb_reset_jump(tb, 0); |
| 1123 | if (tb->tb_next_offset[1] != 0xffff) |
| 1124 | tb_reset_jump(tb, 1); |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 1125 | |
| 1126 | #ifdef DEBUG_TB_CHECK |
| 1127 | tb_page_check(); |
| 1128 | #endif |
pbrook | c8a706f | 2008-06-02 16:16:42 +0000 | [diff] [blame] | 1129 | mmap_unlock(); |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 1132 | /* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr < |
| 1133 | tb[1].tc_ptr. Return NULL if not found */ |
| 1134 | TranslationBlock *tb_find_pc(unsigned long tc_ptr) |
| 1135 | { |
| 1136 | int m_min, m_max, m; |
| 1137 | unsigned long v; |
| 1138 | TranslationBlock *tb; |
| 1139 | |
| 1140 | if (nb_tbs <= 0) |
| 1141 | return NULL; |
| 1142 | if (tc_ptr < (unsigned long)code_gen_buffer || |
| 1143 | tc_ptr >= (unsigned long)code_gen_ptr) |
| 1144 | return NULL; |
| 1145 | /* binary search (cf Knuth) */ |
| 1146 | m_min = 0; |
| 1147 | m_max = nb_tbs - 1; |
| 1148 | while (m_min <= m_max) { |
| 1149 | m = (m_min + m_max) >> 1; |
| 1150 | tb = &tbs[m]; |
| 1151 | v = (unsigned long)tb->tc_ptr; |
| 1152 | if (v == tc_ptr) |
| 1153 | return tb; |
| 1154 | else if (tc_ptr < v) { |
| 1155 | m_max = m - 1; |
| 1156 | } else { |
| 1157 | m_min = m + 1; |
| 1158 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1159 | } |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 1160 | return &tbs[m_max]; |
| 1161 | } |
bellard | 7501267 | 2003-06-21 13:11:07 +0000 | [diff] [blame] | 1162 | |
bellard | ea041c0 | 2003-06-25 16:16:50 +0000 | [diff] [blame] | 1163 | static void tb_reset_jump_recursive(TranslationBlock *tb); |
| 1164 | |
| 1165 | static inline void tb_reset_jump_recursive2(TranslationBlock *tb, int n) |
| 1166 | { |
| 1167 | TranslationBlock *tb1, *tb_next, **ptb; |
| 1168 | unsigned int n1; |
| 1169 | |
| 1170 | tb1 = tb->jmp_next[n]; |
| 1171 | if (tb1 != NULL) { |
| 1172 | /* find head of list */ |
| 1173 | for(;;) { |
| 1174 | n1 = (long)tb1 & 3; |
| 1175 | tb1 = (TranslationBlock *)((long)tb1 & ~3); |
| 1176 | if (n1 == 2) |
| 1177 | break; |
| 1178 | tb1 = tb1->jmp_next[n1]; |
| 1179 | } |
| 1180 | /* we are now sure now that tb jumps to tb1 */ |
| 1181 | tb_next = tb1; |
| 1182 | |
| 1183 | /* remove tb from the jmp_first list */ |
| 1184 | ptb = &tb_next->jmp_first; |
| 1185 | for(;;) { |
| 1186 | tb1 = *ptb; |
| 1187 | n1 = (long)tb1 & 3; |
| 1188 | tb1 = (TranslationBlock *)((long)tb1 & ~3); |
| 1189 | if (n1 == n && tb1 == tb) |
| 1190 | break; |
| 1191 | ptb = &tb1->jmp_next[n1]; |
| 1192 | } |
| 1193 | *ptb = tb->jmp_next[n]; |
| 1194 | tb->jmp_next[n] = NULL; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1195 | |
bellard | ea041c0 | 2003-06-25 16:16:50 +0000 | [diff] [blame] | 1196 | /* suppress the jump to next tb in generated code */ |
| 1197 | tb_reset_jump(tb, n); |
| 1198 | |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 1199 | /* suppress jumps in the tb on which we could have jumped */ |
bellard | ea041c0 | 2003-06-25 16:16:50 +0000 | [diff] [blame] | 1200 | tb_reset_jump_recursive(tb_next); |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | static void tb_reset_jump_recursive(TranslationBlock *tb) |
| 1205 | { |
| 1206 | tb_reset_jump_recursive2(tb, 0); |
| 1207 | tb_reset_jump_recursive2(tb, 1); |
| 1208 | } |
| 1209 | |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1210 | #if defined(TARGET_HAS_ICE) |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1211 | static void breakpoint_invalidate(CPUState *env, target_ulong pc) |
| 1212 | { |
j_mayer | 9b3c35e | 2007-04-07 11:21:28 +0000 | [diff] [blame] | 1213 | target_phys_addr_t addr; |
| 1214 | target_ulong pd; |
pbrook | c2f07f8 | 2006-04-08 17:14:56 +0000 | [diff] [blame] | 1215 | ram_addr_t ram_addr; |
| 1216 | PhysPageDesc *p; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1217 | |
pbrook | c2f07f8 | 2006-04-08 17:14:56 +0000 | [diff] [blame] | 1218 | addr = cpu_get_phys_page_debug(env, pc); |
| 1219 | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
| 1220 | if (!p) { |
| 1221 | pd = IO_MEM_UNASSIGNED; |
| 1222 | } else { |
| 1223 | pd = p->phys_offset; |
| 1224 | } |
| 1225 | ram_addr = (pd & TARGET_PAGE_MASK) | (pc & ~TARGET_PAGE_MASK); |
pbrook | 706cd4b | 2006-04-08 17:36:21 +0000 | [diff] [blame] | 1226 | tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1227 | } |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 1228 | #endif |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1229 | |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 1230 | /* Add a watchpoint. */ |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 1231 | int cpu_watchpoint_insert(CPUState *env, target_ulong addr, int type) |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 1232 | { |
| 1233 | int i; |
| 1234 | |
| 1235 | for (i = 0; i < env->nb_watchpoints; i++) { |
| 1236 | if (addr == env->watchpoint[i].vaddr) |
| 1237 | return 0; |
| 1238 | } |
| 1239 | if (env->nb_watchpoints >= MAX_WATCHPOINTS) |
| 1240 | return -1; |
| 1241 | |
| 1242 | i = env->nb_watchpoints++; |
| 1243 | env->watchpoint[i].vaddr = addr; |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 1244 | env->watchpoint[i].type = type; |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 1245 | tlb_flush_page(env, addr); |
| 1246 | /* FIXME: This flush is needed because of the hack to make memory ops |
| 1247 | terminate the TB. It can be removed once the proper IO trap and |
| 1248 | re-execute bits are in. */ |
| 1249 | tb_flush(env); |
| 1250 | return i; |
| 1251 | } |
| 1252 | |
| 1253 | /* Remove a watchpoint. */ |
| 1254 | int cpu_watchpoint_remove(CPUState *env, target_ulong addr) |
| 1255 | { |
| 1256 | int i; |
| 1257 | |
| 1258 | for (i = 0; i < env->nb_watchpoints; i++) { |
| 1259 | if (addr == env->watchpoint[i].vaddr) { |
| 1260 | env->nb_watchpoints--; |
| 1261 | env->watchpoint[i] = env->watchpoint[env->nb_watchpoints]; |
| 1262 | tlb_flush_page(env, addr); |
| 1263 | return 0; |
| 1264 | } |
| 1265 | } |
| 1266 | return -1; |
| 1267 | } |
| 1268 | |
edgar_igl | 7d03f82 | 2008-05-17 18:58:29 +0000 | [diff] [blame] | 1269 | /* Remove all watchpoints. */ |
| 1270 | void cpu_watchpoint_remove_all(CPUState *env) { |
| 1271 | int i; |
| 1272 | |
| 1273 | for (i = 0; i < env->nb_watchpoints; i++) { |
| 1274 | tlb_flush_page(env, env->watchpoint[i].vaddr); |
| 1275 | } |
| 1276 | env->nb_watchpoints = 0; |
| 1277 | } |
| 1278 | |
bellard | c33a346 | 2003-07-29 20:50:33 +0000 | [diff] [blame] | 1279 | /* add a breakpoint. EXCP_DEBUG is returned by the CPU loop if a |
| 1280 | breakpoint is reached */ |
bellard | 2e12669 | 2004-04-25 21:28:44 +0000 | [diff] [blame] | 1281 | int cpu_breakpoint_insert(CPUState *env, target_ulong pc) |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 1282 | { |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1283 | #if defined(TARGET_HAS_ICE) |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 1284 | int i; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1285 | |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 1286 | for(i = 0; i < env->nb_breakpoints; i++) { |
| 1287 | if (env->breakpoints[i] == pc) |
| 1288 | return 0; |
| 1289 | } |
| 1290 | |
| 1291 | if (env->nb_breakpoints >= MAX_BREAKPOINTS) |
| 1292 | return -1; |
| 1293 | env->breakpoints[env->nb_breakpoints++] = pc; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1294 | |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1295 | breakpoint_invalidate(env, pc); |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 1296 | return 0; |
| 1297 | #else |
| 1298 | return -1; |
| 1299 | #endif |
| 1300 | } |
| 1301 | |
edgar_igl | 7d03f82 | 2008-05-17 18:58:29 +0000 | [diff] [blame] | 1302 | /* remove all breakpoints */ |
| 1303 | void cpu_breakpoint_remove_all(CPUState *env) { |
| 1304 | #if defined(TARGET_HAS_ICE) |
| 1305 | int i; |
| 1306 | for(i = 0; i < env->nb_breakpoints; i++) { |
| 1307 | breakpoint_invalidate(env, env->breakpoints[i]); |
| 1308 | } |
| 1309 | env->nb_breakpoints = 0; |
| 1310 | #endif |
| 1311 | } |
| 1312 | |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 1313 | /* remove a breakpoint */ |
bellard | 2e12669 | 2004-04-25 21:28:44 +0000 | [diff] [blame] | 1314 | int cpu_breakpoint_remove(CPUState *env, target_ulong pc) |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 1315 | { |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1316 | #if defined(TARGET_HAS_ICE) |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 1317 | int i; |
| 1318 | for(i = 0; i < env->nb_breakpoints; i++) { |
| 1319 | if (env->breakpoints[i] == pc) |
| 1320 | goto found; |
| 1321 | } |
| 1322 | return -1; |
| 1323 | found: |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 1324 | env->nb_breakpoints--; |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1325 | if (i < env->nb_breakpoints) |
| 1326 | env->breakpoints[i] = env->breakpoints[env->nb_breakpoints]; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1327 | |
| 1328 | breakpoint_invalidate(env, pc); |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 1329 | return 0; |
| 1330 | #else |
| 1331 | return -1; |
| 1332 | #endif |
| 1333 | } |
| 1334 | |
bellard | c33a346 | 2003-07-29 20:50:33 +0000 | [diff] [blame] | 1335 | /* enable or disable single step mode. EXCP_DEBUG is returned by the |
| 1336 | CPU loop after each instruction */ |
| 1337 | void cpu_single_step(CPUState *env, int enabled) |
| 1338 | { |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1339 | #if defined(TARGET_HAS_ICE) |
bellard | c33a346 | 2003-07-29 20:50:33 +0000 | [diff] [blame] | 1340 | if (env->singlestep_enabled != enabled) { |
| 1341 | env->singlestep_enabled = enabled; |
| 1342 | /* must flush all the translated code to avoid inconsistancies */ |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1343 | /* XXX: only flush what is necessary */ |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 1344 | tb_flush(env); |
bellard | c33a346 | 2003-07-29 20:50:33 +0000 | [diff] [blame] | 1345 | } |
| 1346 | #endif |
| 1347 | } |
| 1348 | |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 1349 | /* enable or disable low levels log */ |
| 1350 | void cpu_set_log(int log_flags) |
| 1351 | { |
| 1352 | loglevel = log_flags; |
| 1353 | if (loglevel && !logfile) { |
pbrook | 11fcfab | 2007-07-01 18:21:11 +0000 | [diff] [blame] | 1354 | logfile = fopen(logfilename, log_append ? "a" : "w"); |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 1355 | if (!logfile) { |
| 1356 | perror(logfilename); |
| 1357 | _exit(1); |
| 1358 | } |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1359 | #if !defined(CONFIG_SOFTMMU) |
| 1360 | /* must avoid mmap() usage of glibc by setting a buffer "by hand" */ |
| 1361 | { |
| 1362 | static uint8_t logfile_buf[4096]; |
| 1363 | setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf)); |
| 1364 | } |
| 1365 | #else |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 1366 | setvbuf(logfile, NULL, _IOLBF, 0); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1367 | #endif |
pbrook | e735b91 | 2007-06-30 13:53:24 +0000 | [diff] [blame] | 1368 | log_append = 1; |
| 1369 | } |
| 1370 | if (!loglevel && logfile) { |
| 1371 | fclose(logfile); |
| 1372 | logfile = NULL; |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | void cpu_set_log_filename(const char *filename) |
| 1377 | { |
| 1378 | logfilename = strdup(filename); |
pbrook | e735b91 | 2007-06-30 13:53:24 +0000 | [diff] [blame] | 1379 | if (logfile) { |
| 1380 | fclose(logfile); |
| 1381 | logfile = NULL; |
| 1382 | } |
| 1383 | cpu_set_log(loglevel); |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 1384 | } |
bellard | c33a346 | 2003-07-29 20:50:33 +0000 | [diff] [blame] | 1385 | |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 1386 | /* mask must never be zero, except for A20 change call */ |
bellard | 68a7931 | 2003-06-30 13:12:32 +0000 | [diff] [blame] | 1387 | void cpu_interrupt(CPUState *env, int mask) |
bellard | ea041c0 | 2003-06-25 16:16:50 +0000 | [diff] [blame] | 1388 | { |
pbrook | d597536 | 2008-06-07 20:50:51 +0000 | [diff] [blame] | 1389 | #if !defined(USE_NPTL) |
bellard | ea041c0 | 2003-06-25 16:16:50 +0000 | [diff] [blame] | 1390 | TranslationBlock *tb; |
aurel32 | 15a5115 | 2008-03-28 22:29:15 +0000 | [diff] [blame] | 1391 | static spinlock_t interrupt_lock = SPIN_LOCK_UNLOCKED; |
pbrook | d597536 | 2008-06-07 20:50:51 +0000 | [diff] [blame] | 1392 | #endif |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1393 | int old_mask; |
bellard | 59817cc | 2004-02-16 22:01:13 +0000 | [diff] [blame] | 1394 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1395 | old_mask = env->interrupt_request; |
pbrook | d597536 | 2008-06-07 20:50:51 +0000 | [diff] [blame] | 1396 | /* FIXME: This is probably not threadsafe. A different thread could |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 1397 | be in the middle of a read-modify-write operation. */ |
bellard | 68a7931 | 2003-06-30 13:12:32 +0000 | [diff] [blame] | 1398 | env->interrupt_request |= mask; |
pbrook | d597536 | 2008-06-07 20:50:51 +0000 | [diff] [blame] | 1399 | #if defined(USE_NPTL) |
| 1400 | /* FIXME: TB unchaining isn't SMP safe. For now just ignore the |
| 1401 | problem and hope the cpu will stop of its own accord. For userspace |
| 1402 | emulation this often isn't actually as bad as it sounds. Often |
| 1403 | signals are used primarily to interrupt blocking syscalls. */ |
| 1404 | #else |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1405 | if (use_icount) { |
| 1406 | env->icount_decr.u16.high = 0x8000; |
| 1407 | #ifndef CONFIG_USER_ONLY |
| 1408 | /* CPU_INTERRUPT_EXIT isn't a real interrupt. It just means |
| 1409 | an async event happened and we need to process it. */ |
| 1410 | if (!can_do_io(env) |
| 1411 | && (mask & ~(old_mask | CPU_INTERRUPT_EXIT)) != 0) { |
| 1412 | cpu_abort(env, "Raised interrupt while not in I/O function"); |
| 1413 | } |
| 1414 | #endif |
| 1415 | } else { |
| 1416 | tb = env->current_tb; |
| 1417 | /* if the cpu is currently executing code, we must unlink it and |
| 1418 | all the potentially executing TB */ |
| 1419 | if (tb && !testandset(&interrupt_lock)) { |
| 1420 | env->current_tb = NULL; |
| 1421 | tb_reset_jump_recursive(tb); |
| 1422 | resetlock(&interrupt_lock); |
| 1423 | } |
bellard | ea041c0 | 2003-06-25 16:16:50 +0000 | [diff] [blame] | 1424 | } |
pbrook | d597536 | 2008-06-07 20:50:51 +0000 | [diff] [blame] | 1425 | #endif |
bellard | ea041c0 | 2003-06-25 16:16:50 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
bellard | b54ad04 | 2004-05-20 13:42:52 +0000 | [diff] [blame] | 1428 | void cpu_reset_interrupt(CPUState *env, int mask) |
| 1429 | { |
| 1430 | env->interrupt_request &= ~mask; |
| 1431 | } |
| 1432 | |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1433 | CPULogItem cpu_log_items[] = { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1434 | { CPU_LOG_TB_OUT_ASM, "out_asm", |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1435 | "show generated host assembly code for each compiled TB" }, |
| 1436 | { CPU_LOG_TB_IN_ASM, "in_asm", |
| 1437 | "show target assembly code for each compiled TB" }, |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1438 | { CPU_LOG_TB_OP, "op", |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 1439 | "show micro ops for each compiled TB" }, |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1440 | { CPU_LOG_TB_OP_OPT, "op_opt", |
blueswir1 | e01a115 | 2008-03-14 17:37:11 +0000 | [diff] [blame] | 1441 | "show micro ops " |
| 1442 | #ifdef TARGET_I386 |
| 1443 | "before eflags optimization and " |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1444 | #endif |
blueswir1 | e01a115 | 2008-03-14 17:37:11 +0000 | [diff] [blame] | 1445 | "after liveness analysis" }, |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1446 | { CPU_LOG_INT, "int", |
| 1447 | "show interrupts/exceptions in short format" }, |
| 1448 | { CPU_LOG_EXEC, "exec", |
| 1449 | "show trace before each executed TB (lots of logs)" }, |
bellard | 9fddaa0 | 2004-05-21 12:59:32 +0000 | [diff] [blame] | 1450 | { CPU_LOG_TB_CPU, "cpu", |
ths | e91c8a7 | 2007-06-03 13:35:16 +0000 | [diff] [blame] | 1451 | "show CPU state before block translation" }, |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1452 | #ifdef TARGET_I386 |
| 1453 | { CPU_LOG_PCALL, "pcall", |
| 1454 | "show protected mode far calls/returns/exceptions" }, |
| 1455 | #endif |
bellard | 8e3a9fd | 2004-10-09 17:32:58 +0000 | [diff] [blame] | 1456 | #ifdef DEBUG_IOPORT |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1457 | { CPU_LOG_IOPORT, "ioport", |
| 1458 | "show all i/o ports accesses" }, |
bellard | 8e3a9fd | 2004-10-09 17:32:58 +0000 | [diff] [blame] | 1459 | #endif |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1460 | { 0, NULL, NULL }, |
| 1461 | }; |
| 1462 | |
| 1463 | static int cmp1(const char *s1, int n, const char *s2) |
| 1464 | { |
| 1465 | if (strlen(s2) != n) |
| 1466 | return 0; |
| 1467 | return memcmp(s1, s2, n) == 0; |
| 1468 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1469 | |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1470 | /* takes a comma separated list of log masks. Return 0 if error. */ |
| 1471 | int cpu_str_to_log_mask(const char *str) |
| 1472 | { |
| 1473 | CPULogItem *item; |
| 1474 | int mask; |
| 1475 | const char *p, *p1; |
| 1476 | |
| 1477 | p = str; |
| 1478 | mask = 0; |
| 1479 | for(;;) { |
| 1480 | p1 = strchr(p, ','); |
| 1481 | if (!p1) |
| 1482 | p1 = p + strlen(p); |
bellard | 8e3a9fd | 2004-10-09 17:32:58 +0000 | [diff] [blame] | 1483 | if(cmp1(p,p1-p,"all")) { |
| 1484 | for(item = cpu_log_items; item->mask != 0; item++) { |
| 1485 | mask |= item->mask; |
| 1486 | } |
| 1487 | } else { |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1488 | for(item = cpu_log_items; item->mask != 0; item++) { |
| 1489 | if (cmp1(p, p1 - p, item->name)) |
| 1490 | goto found; |
| 1491 | } |
| 1492 | return 0; |
bellard | 8e3a9fd | 2004-10-09 17:32:58 +0000 | [diff] [blame] | 1493 | } |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1494 | found: |
| 1495 | mask |= item->mask; |
| 1496 | if (*p1 != ',') |
| 1497 | break; |
| 1498 | p = p1 + 1; |
| 1499 | } |
| 1500 | return mask; |
| 1501 | } |
bellard | ea041c0 | 2003-06-25 16:16:50 +0000 | [diff] [blame] | 1502 | |
bellard | 7501267 | 2003-06-21 13:11:07 +0000 | [diff] [blame] | 1503 | void cpu_abort(CPUState *env, const char *fmt, ...) |
| 1504 | { |
| 1505 | va_list ap; |
pbrook | 493ae1f | 2007-11-23 16:53:59 +0000 | [diff] [blame] | 1506 | va_list ap2; |
bellard | 7501267 | 2003-06-21 13:11:07 +0000 | [diff] [blame] | 1507 | |
| 1508 | va_start(ap, fmt); |
pbrook | 493ae1f | 2007-11-23 16:53:59 +0000 | [diff] [blame] | 1509 | va_copy(ap2, ap); |
bellard | 7501267 | 2003-06-21 13:11:07 +0000 | [diff] [blame] | 1510 | fprintf(stderr, "qemu: fatal: "); |
| 1511 | vfprintf(stderr, fmt, ap); |
| 1512 | fprintf(stderr, "\n"); |
| 1513 | #ifdef TARGET_I386 |
bellard | 7fe4848 | 2004-10-09 18:08:01 +0000 | [diff] [blame] | 1514 | cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP); |
| 1515 | #else |
| 1516 | cpu_dump_state(env, stderr, fprintf, 0); |
bellard | 7501267 | 2003-06-21 13:11:07 +0000 | [diff] [blame] | 1517 | #endif |
balrog | 924edca | 2007-06-10 14:07:13 +0000 | [diff] [blame] | 1518 | if (logfile) { |
j_mayer | f937329 | 2007-09-29 12:18:20 +0000 | [diff] [blame] | 1519 | fprintf(logfile, "qemu: fatal: "); |
pbrook | 493ae1f | 2007-11-23 16:53:59 +0000 | [diff] [blame] | 1520 | vfprintf(logfile, fmt, ap2); |
j_mayer | f937329 | 2007-09-29 12:18:20 +0000 | [diff] [blame] | 1521 | fprintf(logfile, "\n"); |
| 1522 | #ifdef TARGET_I386 |
| 1523 | cpu_dump_state(env, logfile, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP); |
| 1524 | #else |
| 1525 | cpu_dump_state(env, logfile, fprintf, 0); |
| 1526 | #endif |
balrog | 924edca | 2007-06-10 14:07:13 +0000 | [diff] [blame] | 1527 | fflush(logfile); |
| 1528 | fclose(logfile); |
| 1529 | } |
pbrook | 493ae1f | 2007-11-23 16:53:59 +0000 | [diff] [blame] | 1530 | va_end(ap2); |
j_mayer | f937329 | 2007-09-29 12:18:20 +0000 | [diff] [blame] | 1531 | va_end(ap); |
bellard | 7501267 | 2003-06-21 13:11:07 +0000 | [diff] [blame] | 1532 | abort(); |
| 1533 | } |
| 1534 | |
ths | c5be9f0 | 2007-02-28 20:20:53 +0000 | [diff] [blame] | 1535 | CPUState *cpu_copy(CPUState *env) |
| 1536 | { |
ths | 01ba981 | 2007-12-09 02:22:57 +0000 | [diff] [blame] | 1537 | CPUState *new_env = cpu_init(env->cpu_model_str); |
ths | c5be9f0 | 2007-02-28 20:20:53 +0000 | [diff] [blame] | 1538 | /* preserve chaining and index */ |
| 1539 | CPUState *next_cpu = new_env->next_cpu; |
| 1540 | int cpu_index = new_env->cpu_index; |
| 1541 | memcpy(new_env, env, sizeof(CPUState)); |
| 1542 | new_env->next_cpu = next_cpu; |
| 1543 | new_env->cpu_index = cpu_index; |
| 1544 | return new_env; |
| 1545 | } |
| 1546 | |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 1547 | #if !defined(CONFIG_USER_ONLY) |
| 1548 | |
edgar_igl | 5c751e9 | 2008-05-06 08:44:21 +0000 | [diff] [blame] | 1549 | static inline void tlb_flush_jmp_cache(CPUState *env, target_ulong addr) |
| 1550 | { |
| 1551 | unsigned int i; |
| 1552 | |
| 1553 | /* Discard jump cache entries for any tb which might potentially |
| 1554 | overlap the flushed page. */ |
| 1555 | i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE); |
| 1556 | memset (&env->tb_jmp_cache[i], 0, |
| 1557 | TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *)); |
| 1558 | |
| 1559 | i = tb_jmp_cache_hash_page(addr); |
| 1560 | memset (&env->tb_jmp_cache[i], 0, |
| 1561 | TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *)); |
| 1562 | } |
| 1563 | |
bellard | ee8b702 | 2004-02-03 23:35:10 +0000 | [diff] [blame] | 1564 | /* NOTE: if flush_global is true, also flush global entries (not |
| 1565 | implemented yet) */ |
| 1566 | void tlb_flush(CPUState *env, int flush_global) |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 1567 | { |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 1568 | int i; |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 1569 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1570 | #if defined(DEBUG_TLB) |
| 1571 | printf("tlb_flush:\n"); |
| 1572 | #endif |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 1573 | /* must reset current TB so that interrupts cannot modify the |
| 1574 | links while we are modifying them */ |
| 1575 | env->current_tb = NULL; |
| 1576 | |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 1577 | for(i = 0; i < CPU_TLB_SIZE; i++) { |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 1578 | env->tlb_table[0][i].addr_read = -1; |
| 1579 | env->tlb_table[0][i].addr_write = -1; |
| 1580 | env->tlb_table[0][i].addr_code = -1; |
| 1581 | env->tlb_table[1][i].addr_read = -1; |
| 1582 | env->tlb_table[1][i].addr_write = -1; |
| 1583 | env->tlb_table[1][i].addr_code = -1; |
j_mayer | 6fa4cea | 2007-04-05 06:43:27 +0000 | [diff] [blame] | 1584 | #if (NB_MMU_MODES >= 3) |
| 1585 | env->tlb_table[2][i].addr_read = -1; |
| 1586 | env->tlb_table[2][i].addr_write = -1; |
| 1587 | env->tlb_table[2][i].addr_code = -1; |
| 1588 | #if (NB_MMU_MODES == 4) |
| 1589 | env->tlb_table[3][i].addr_read = -1; |
| 1590 | env->tlb_table[3][i].addr_write = -1; |
| 1591 | env->tlb_table[3][i].addr_code = -1; |
| 1592 | #endif |
| 1593 | #endif |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 1594 | } |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1595 | |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 1596 | memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *)); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1597 | |
bellard | 0a962c0 | 2005-02-10 22:00:27 +0000 | [diff] [blame] | 1598 | #ifdef USE_KQEMU |
| 1599 | if (env->kqemu_enabled) { |
| 1600 | kqemu_flush(env, flush_global); |
| 1601 | } |
| 1602 | #endif |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 1603 | tlb_flush_count++; |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
bellard | 274da6b | 2004-05-20 21:56:27 +0000 | [diff] [blame] | 1606 | static inline void tlb_flush_entry(CPUTLBEntry *tlb_entry, target_ulong addr) |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 1607 | { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1608 | if (addr == (tlb_entry->addr_read & |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 1609 | (TARGET_PAGE_MASK | TLB_INVALID_MASK)) || |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1610 | addr == (tlb_entry->addr_write & |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 1611 | (TARGET_PAGE_MASK | TLB_INVALID_MASK)) || |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1612 | addr == (tlb_entry->addr_code & |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 1613 | (TARGET_PAGE_MASK | TLB_INVALID_MASK))) { |
| 1614 | tlb_entry->addr_read = -1; |
| 1615 | tlb_entry->addr_write = -1; |
| 1616 | tlb_entry->addr_code = -1; |
| 1617 | } |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
bellard | 2e12669 | 2004-04-25 21:28:44 +0000 | [diff] [blame] | 1620 | void tlb_flush_page(CPUState *env, target_ulong addr) |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 1621 | { |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 1622 | int i; |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 1623 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1624 | #if defined(DEBUG_TLB) |
bellard | 108c49b | 2005-07-24 12:55:09 +0000 | [diff] [blame] | 1625 | printf("tlb_flush_page: " TARGET_FMT_lx "\n", addr); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1626 | #endif |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 1627 | /* must reset current TB so that interrupts cannot modify the |
| 1628 | links while we are modifying them */ |
| 1629 | env->current_tb = NULL; |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 1630 | |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 1631 | addr &= TARGET_PAGE_MASK; |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 1632 | i = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1); |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 1633 | tlb_flush_entry(&env->tlb_table[0][i], addr); |
| 1634 | tlb_flush_entry(&env->tlb_table[1][i], addr); |
j_mayer | 6fa4cea | 2007-04-05 06:43:27 +0000 | [diff] [blame] | 1635 | #if (NB_MMU_MODES >= 3) |
| 1636 | tlb_flush_entry(&env->tlb_table[2][i], addr); |
| 1637 | #if (NB_MMU_MODES == 4) |
| 1638 | tlb_flush_entry(&env->tlb_table[3][i], addr); |
| 1639 | #endif |
| 1640 | #endif |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 1641 | |
edgar_igl | 5c751e9 | 2008-05-06 08:44:21 +0000 | [diff] [blame] | 1642 | tlb_flush_jmp_cache(env, addr); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1643 | |
bellard | 0a962c0 | 2005-02-10 22:00:27 +0000 | [diff] [blame] | 1644 | #ifdef USE_KQEMU |
| 1645 | if (env->kqemu_enabled) { |
| 1646 | kqemu_flush_page(env, addr); |
| 1647 | } |
| 1648 | #endif |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1651 | /* update the TLBs so that writes to code in the virtual page 'addr' |
| 1652 | can be detected */ |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1653 | static void tlb_protect_code(ram_addr_t ram_addr) |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 1654 | { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1655 | cpu_physical_memory_reset_dirty(ram_addr, |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1656 | ram_addr + TARGET_PAGE_SIZE, |
| 1657 | CODE_DIRTY_FLAG); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1660 | /* update the TLB so that writes in physical page 'phys_addr' are no longer |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 1661 | tested for self modifying code */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1662 | static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr, |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 1663 | target_ulong vaddr) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1664 | { |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 1665 | phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] |= CODE_DIRTY_FLAG; |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 1666 | } |
| 1667 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1668 | static inline void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry, |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 1669 | unsigned long start, unsigned long length) |
| 1670 | { |
| 1671 | unsigned long addr; |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 1672 | if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) { |
| 1673 | addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend; |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 1674 | if ((addr - start) < length) { |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 1675 | tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | TLB_NOTDIRTY; |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 1676 | } |
| 1677 | } |
| 1678 | } |
| 1679 | |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 1680 | void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end, |
bellard | 0a962c0 | 2005-02-10 22:00:27 +0000 | [diff] [blame] | 1681 | int dirty_flags) |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 1682 | { |
| 1683 | CPUState *env; |
bellard | 4f2ac23 | 2004-04-26 19:44:02 +0000 | [diff] [blame] | 1684 | unsigned long length, start1; |
bellard | 0a962c0 | 2005-02-10 22:00:27 +0000 | [diff] [blame] | 1685 | int i, mask, len; |
| 1686 | uint8_t *p; |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 1687 | |
| 1688 | start &= TARGET_PAGE_MASK; |
| 1689 | end = TARGET_PAGE_ALIGN(end); |
| 1690 | |
| 1691 | length = end - start; |
| 1692 | if (length == 0) |
| 1693 | return; |
bellard | 0a962c0 | 2005-02-10 22:00:27 +0000 | [diff] [blame] | 1694 | len = length >> TARGET_PAGE_BITS; |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 1695 | #ifdef USE_KQEMU |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1696 | /* XXX: should not depend on cpu context */ |
| 1697 | env = first_cpu; |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 1698 | if (env->kqemu_enabled) { |
bellard | f23db16 | 2005-08-21 19:12:28 +0000 | [diff] [blame] | 1699 | ram_addr_t addr; |
| 1700 | addr = start; |
| 1701 | for(i = 0; i < len; i++) { |
| 1702 | kqemu_set_notdirty(env, addr); |
| 1703 | addr += TARGET_PAGE_SIZE; |
| 1704 | } |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 1705 | } |
| 1706 | #endif |
bellard | f23db16 | 2005-08-21 19:12:28 +0000 | [diff] [blame] | 1707 | mask = ~dirty_flags; |
| 1708 | p = phys_ram_dirty + (start >> TARGET_PAGE_BITS); |
| 1709 | for(i = 0; i < len; i++) |
| 1710 | p[i] &= mask; |
| 1711 | |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 1712 | /* we modify the TLB cache so that the dirty bit will be set again |
| 1713 | when accessing the range */ |
bellard | 59817cc | 2004-02-16 22:01:13 +0000 | [diff] [blame] | 1714 | start1 = start + (unsigned long)phys_ram_base; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1715 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
| 1716 | for(i = 0; i < CPU_TLB_SIZE; i++) |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 1717 | tlb_reset_dirty_range(&env->tlb_table[0][i], start1, length); |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1718 | for(i = 0; i < CPU_TLB_SIZE; i++) |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 1719 | tlb_reset_dirty_range(&env->tlb_table[1][i], start1, length); |
j_mayer | 6fa4cea | 2007-04-05 06:43:27 +0000 | [diff] [blame] | 1720 | #if (NB_MMU_MODES >= 3) |
| 1721 | for(i = 0; i < CPU_TLB_SIZE; i++) |
| 1722 | tlb_reset_dirty_range(&env->tlb_table[2][i], start1, length); |
| 1723 | #if (NB_MMU_MODES == 4) |
| 1724 | for(i = 0; i < CPU_TLB_SIZE; i++) |
| 1725 | tlb_reset_dirty_range(&env->tlb_table[3][i], start1, length); |
| 1726 | #endif |
| 1727 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1728 | } |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 1731 | static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry) |
| 1732 | { |
| 1733 | ram_addr_t ram_addr; |
| 1734 | |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 1735 | if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1736 | ram_addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 1737 | tlb_entry->addend - (unsigned long)phys_ram_base; |
| 1738 | if (!cpu_physical_memory_is_dirty(ram_addr)) { |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 1739 | tlb_entry->addr_write |= TLB_NOTDIRTY; |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 1740 | } |
| 1741 | } |
| 1742 | } |
| 1743 | |
| 1744 | /* update the TLB according to the current state of the dirty bits */ |
| 1745 | void cpu_tlb_update_dirty(CPUState *env) |
| 1746 | { |
| 1747 | int i; |
| 1748 | for(i = 0; i < CPU_TLB_SIZE; i++) |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 1749 | tlb_update_dirty(&env->tlb_table[0][i]); |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 1750 | for(i = 0; i < CPU_TLB_SIZE; i++) |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 1751 | tlb_update_dirty(&env->tlb_table[1][i]); |
j_mayer | 6fa4cea | 2007-04-05 06:43:27 +0000 | [diff] [blame] | 1752 | #if (NB_MMU_MODES >= 3) |
| 1753 | for(i = 0; i < CPU_TLB_SIZE; i++) |
| 1754 | tlb_update_dirty(&env->tlb_table[2][i]); |
| 1755 | #if (NB_MMU_MODES == 4) |
| 1756 | for(i = 0; i < CPU_TLB_SIZE; i++) |
| 1757 | tlb_update_dirty(&env->tlb_table[3][i]); |
| 1758 | #endif |
| 1759 | #endif |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 1762 | static inline void tlb_set_dirty1(CPUTLBEntry *tlb_entry, target_ulong vaddr) |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 1763 | { |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 1764 | if (tlb_entry->addr_write == (vaddr | TLB_NOTDIRTY)) |
| 1765 | tlb_entry->addr_write = vaddr; |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 1768 | /* update the TLB corresponding to virtual page vaddr |
| 1769 | so that it is no longer dirty */ |
| 1770 | static inline void tlb_set_dirty(CPUState *env, target_ulong vaddr) |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 1771 | { |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 1772 | int i; |
| 1773 | |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 1774 | vaddr &= TARGET_PAGE_MASK; |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 1775 | i = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1); |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 1776 | tlb_set_dirty1(&env->tlb_table[0][i], vaddr); |
| 1777 | tlb_set_dirty1(&env->tlb_table[1][i], vaddr); |
j_mayer | 6fa4cea | 2007-04-05 06:43:27 +0000 | [diff] [blame] | 1778 | #if (NB_MMU_MODES >= 3) |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 1779 | tlb_set_dirty1(&env->tlb_table[2][i], vaddr); |
j_mayer | 6fa4cea | 2007-04-05 06:43:27 +0000 | [diff] [blame] | 1780 | #if (NB_MMU_MODES == 4) |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 1781 | tlb_set_dirty1(&env->tlb_table[3][i], vaddr); |
j_mayer | 6fa4cea | 2007-04-05 06:43:27 +0000 | [diff] [blame] | 1782 | #endif |
| 1783 | #endif |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
bellard | 59817cc | 2004-02-16 22:01:13 +0000 | [diff] [blame] | 1786 | /* add a new TLB entry. At most one entry for a given virtual address |
| 1787 | is permitted. Return 0 if OK or 2 if the page could not be mapped |
| 1788 | (can only happen in non SOFTMMU mode for I/O pages or pages |
| 1789 | conflicting with the host address space). */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1790 | int tlb_set_page_exec(CPUState *env, target_ulong vaddr, |
| 1791 | target_phys_addr_t paddr, int prot, |
j_mayer | 6ebbf39 | 2007-10-14 07:07:08 +0000 | [diff] [blame] | 1792 | int mmu_idx, int is_softmmu) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1793 | { |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 1794 | PhysPageDesc *p; |
bellard | 4f2ac23 | 2004-04-26 19:44:02 +0000 | [diff] [blame] | 1795 | unsigned long pd; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1796 | unsigned int index; |
bellard | 4f2ac23 | 2004-04-26 19:44:02 +0000 | [diff] [blame] | 1797 | target_ulong address; |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 1798 | target_ulong code_address; |
bellard | 108c49b | 2005-07-24 12:55:09 +0000 | [diff] [blame] | 1799 | target_phys_addr_t addend; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1800 | int ret; |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 1801 | CPUTLBEntry *te; |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 1802 | int i; |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 1803 | target_phys_addr_t iotlb; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1804 | |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 1805 | p = phys_page_find(paddr >> TARGET_PAGE_BITS); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1806 | if (!p) { |
| 1807 | pd = IO_MEM_UNASSIGNED; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1808 | } else { |
| 1809 | pd = p->phys_offset; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1810 | } |
| 1811 | #if defined(DEBUG_TLB) |
j_mayer | 6ebbf39 | 2007-10-14 07:07:08 +0000 | [diff] [blame] | 1812 | printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x%08x prot=%x idx=%d smmu=%d pd=0x%08lx\n", |
| 1813 | vaddr, (int)paddr, prot, mmu_idx, is_softmmu, pd); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1814 | #endif |
| 1815 | |
| 1816 | ret = 0; |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 1817 | address = vaddr; |
| 1818 | if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) { |
| 1819 | /* IO memory case (romd handled later) */ |
| 1820 | address |= TLB_MMIO; |
| 1821 | } |
| 1822 | addend = (unsigned long)phys_ram_base + (pd & TARGET_PAGE_MASK); |
| 1823 | if ((pd & ~TARGET_PAGE_MASK) <= IO_MEM_ROM) { |
| 1824 | /* Normal RAM. */ |
| 1825 | iotlb = pd & TARGET_PAGE_MASK; |
| 1826 | if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM) |
| 1827 | iotlb |= IO_MEM_NOTDIRTY; |
| 1828 | else |
| 1829 | iotlb |= IO_MEM_ROM; |
| 1830 | } else { |
| 1831 | /* IO handlers are currently passed a phsical address. |
| 1832 | It would be nice to pass an offset from the base address |
| 1833 | of that region. This would avoid having to special case RAM, |
| 1834 | and avoid full address decoding in every device. |
| 1835 | We can't use the high bits of pd for this because |
| 1836 | IO_MEM_ROMD uses these as a ram address. */ |
| 1837 | iotlb = (pd & ~TARGET_PAGE_MASK) + paddr; |
| 1838 | } |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 1839 | |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 1840 | code_address = address; |
| 1841 | /* Make accesses to pages with watchpoints go via the |
| 1842 | watchpoint trap routines. */ |
| 1843 | for (i = 0; i < env->nb_watchpoints; i++) { |
| 1844 | if (vaddr == (env->watchpoint[i].vaddr & TARGET_PAGE_MASK)) { |
| 1845 | iotlb = io_mem_watch + paddr; |
| 1846 | /* TODO: The memory case can be optimized by not trapping |
| 1847 | reads of pages with a write breakpoint. */ |
| 1848 | address |= TLB_MMIO; |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 1849 | } |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 1850 | } |
balrog | d79acba | 2007-06-26 20:01:13 +0000 | [diff] [blame] | 1851 | |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 1852 | index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1); |
| 1853 | env->iotlb[mmu_idx][index] = iotlb - vaddr; |
| 1854 | te = &env->tlb_table[mmu_idx][index]; |
| 1855 | te->addend = addend - vaddr; |
| 1856 | if (prot & PAGE_READ) { |
| 1857 | te->addr_read = address; |
| 1858 | } else { |
| 1859 | te->addr_read = -1; |
| 1860 | } |
edgar_igl | 5c751e9 | 2008-05-06 08:44:21 +0000 | [diff] [blame] | 1861 | |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 1862 | if (prot & PAGE_EXEC) { |
| 1863 | te->addr_code = code_address; |
| 1864 | } else { |
| 1865 | te->addr_code = -1; |
| 1866 | } |
| 1867 | if (prot & PAGE_WRITE) { |
| 1868 | if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM || |
| 1869 | (pd & IO_MEM_ROMD)) { |
| 1870 | /* Write access calls the I/O callback. */ |
| 1871 | te->addr_write = address | TLB_MMIO; |
| 1872 | } else if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM && |
| 1873 | !cpu_physical_memory_is_dirty(pd)) { |
| 1874 | te->addr_write = address | TLB_NOTDIRTY; |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 1875 | } else { |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 1876 | te->addr_write = address; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1877 | } |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 1878 | } else { |
| 1879 | te->addr_write = -1; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1880 | } |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1881 | return ret; |
| 1882 | } |
| 1883 | |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 1884 | #else |
| 1885 | |
bellard | ee8b702 | 2004-02-03 23:35:10 +0000 | [diff] [blame] | 1886 | void tlb_flush(CPUState *env, int flush_global) |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 1887 | { |
| 1888 | } |
| 1889 | |
bellard | 2e12669 | 2004-04-25 21:28:44 +0000 | [diff] [blame] | 1890 | void tlb_flush_page(CPUState *env, target_ulong addr) |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 1891 | { |
| 1892 | } |
| 1893 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1894 | int tlb_set_page_exec(CPUState *env, target_ulong vaddr, |
| 1895 | target_phys_addr_t paddr, int prot, |
j_mayer | 6ebbf39 | 2007-10-14 07:07:08 +0000 | [diff] [blame] | 1896 | int mmu_idx, int is_softmmu) |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 1897 | { |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1898 | return 0; |
| 1899 | } |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 1900 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1901 | /* dump memory mappings */ |
| 1902 | void page_dump(FILE *f) |
| 1903 | { |
| 1904 | unsigned long start, end; |
| 1905 | int i, j, prot, prot1; |
| 1906 | PageDesc *p; |
| 1907 | |
| 1908 | fprintf(f, "%-8s %-8s %-8s %s\n", |
| 1909 | "start", "end", "size", "prot"); |
| 1910 | start = -1; |
| 1911 | end = -1; |
| 1912 | prot = 0; |
| 1913 | for(i = 0; i <= L1_SIZE; i++) { |
| 1914 | if (i < L1_SIZE) |
| 1915 | p = l1_map[i]; |
| 1916 | else |
| 1917 | p = NULL; |
| 1918 | for(j = 0;j < L2_SIZE; j++) { |
| 1919 | if (!p) |
| 1920 | prot1 = 0; |
| 1921 | else |
| 1922 | prot1 = p[j].flags; |
| 1923 | if (prot1 != prot) { |
| 1924 | end = (i << (32 - L1_BITS)) | (j << TARGET_PAGE_BITS); |
| 1925 | if (start != -1) { |
| 1926 | fprintf(f, "%08lx-%08lx %08lx %c%c%c\n", |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1927 | start, end, end - start, |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1928 | prot & PAGE_READ ? 'r' : '-', |
| 1929 | prot & PAGE_WRITE ? 'w' : '-', |
| 1930 | prot & PAGE_EXEC ? 'x' : '-'); |
| 1931 | } |
| 1932 | if (prot1 != 0) |
| 1933 | start = end; |
| 1934 | else |
| 1935 | start = -1; |
| 1936 | prot = prot1; |
| 1937 | } |
| 1938 | if (!p) |
| 1939 | break; |
| 1940 | } |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 1941 | } |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 1942 | } |
| 1943 | |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1944 | int page_get_flags(target_ulong address) |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 1945 | { |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1946 | PageDesc *p; |
| 1947 | |
| 1948 | p = page_find(address >> TARGET_PAGE_BITS); |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 1949 | if (!p) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1950 | return 0; |
| 1951 | return p->flags; |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1954 | /* modify the flags of a page and invalidate the code if |
| 1955 | necessary. The flag PAGE_WRITE_ORG is positionned automatically |
| 1956 | depending on PAGE_WRITE */ |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1957 | void page_set_flags(target_ulong start, target_ulong end, int flags) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1958 | { |
| 1959 | PageDesc *p; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1960 | target_ulong addr; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1961 | |
pbrook | c8a706f | 2008-06-02 16:16:42 +0000 | [diff] [blame] | 1962 | /* mmap_lock should already be held. */ |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1963 | start = start & TARGET_PAGE_MASK; |
| 1964 | end = TARGET_PAGE_ALIGN(end); |
| 1965 | if (flags & PAGE_WRITE) |
| 1966 | flags |= PAGE_WRITE_ORG; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1967 | for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) { |
| 1968 | p = page_find_alloc(addr >> TARGET_PAGE_BITS); |
pbrook | 17e2377 | 2008-06-09 13:47:45 +0000 | [diff] [blame] | 1969 | /* We may be called for host regions that are outside guest |
| 1970 | address space. */ |
| 1971 | if (!p) |
| 1972 | return; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1973 | /* if the write protection is set, then we invalidate the code |
| 1974 | inside */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1975 | if (!(p->flags & PAGE_WRITE) && |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1976 | (flags & PAGE_WRITE) && |
| 1977 | p->first_tb) { |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1978 | tb_invalidate_phys_page(addr, 0, NULL); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1979 | } |
| 1980 | p->flags = flags; |
| 1981 | } |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
ths | 3d97b40 | 2007-11-02 19:02:07 +0000 | [diff] [blame] | 1984 | int page_check_range(target_ulong start, target_ulong len, int flags) |
| 1985 | { |
| 1986 | PageDesc *p; |
| 1987 | target_ulong end; |
| 1988 | target_ulong addr; |
| 1989 | |
| 1990 | end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */ |
| 1991 | start = start & TARGET_PAGE_MASK; |
| 1992 | |
| 1993 | if( end < start ) |
| 1994 | /* we've wrapped around */ |
| 1995 | return -1; |
| 1996 | for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) { |
| 1997 | p = page_find(addr >> TARGET_PAGE_BITS); |
| 1998 | if( !p ) |
| 1999 | return -1; |
| 2000 | if( !(p->flags & PAGE_VALID) ) |
| 2001 | return -1; |
| 2002 | |
bellard | dae3270 | 2007-11-14 10:51:00 +0000 | [diff] [blame] | 2003 | if ((flags & PAGE_READ) && !(p->flags & PAGE_READ)) |
ths | 3d97b40 | 2007-11-02 19:02:07 +0000 | [diff] [blame] | 2004 | return -1; |
bellard | dae3270 | 2007-11-14 10:51:00 +0000 | [diff] [blame] | 2005 | if (flags & PAGE_WRITE) { |
| 2006 | if (!(p->flags & PAGE_WRITE_ORG)) |
| 2007 | return -1; |
| 2008 | /* unprotect the page if it was put read-only because it |
| 2009 | contains translated code */ |
| 2010 | if (!(p->flags & PAGE_WRITE)) { |
| 2011 | if (!page_unprotect(addr, 0, NULL)) |
| 2012 | return -1; |
| 2013 | } |
| 2014 | return 0; |
| 2015 | } |
ths | 3d97b40 | 2007-11-02 19:02:07 +0000 | [diff] [blame] | 2016 | } |
| 2017 | return 0; |
| 2018 | } |
| 2019 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2020 | /* called from signal handler: invalidate the code and unprotect the |
| 2021 | page. Return TRUE if the fault was succesfully handled. */ |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2022 | int page_unprotect(target_ulong address, unsigned long pc, void *puc) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2023 | { |
| 2024 | unsigned int page_index, prot, pindex; |
| 2025 | PageDesc *p, *p1; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2026 | target_ulong host_start, host_end, addr; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2027 | |
pbrook | c8a706f | 2008-06-02 16:16:42 +0000 | [diff] [blame] | 2028 | /* Technically this isn't safe inside a signal handler. However we |
| 2029 | know this only ever happens in a synchronous SEGV handler, so in |
| 2030 | practice it seems to be ok. */ |
| 2031 | mmap_lock(); |
| 2032 | |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 2033 | host_start = address & qemu_host_page_mask; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2034 | page_index = host_start >> TARGET_PAGE_BITS; |
| 2035 | p1 = page_find(page_index); |
pbrook | c8a706f | 2008-06-02 16:16:42 +0000 | [diff] [blame] | 2036 | if (!p1) { |
| 2037 | mmap_unlock(); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2038 | return 0; |
pbrook | c8a706f | 2008-06-02 16:16:42 +0000 | [diff] [blame] | 2039 | } |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 2040 | host_end = host_start + qemu_host_page_size; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2041 | p = p1; |
| 2042 | prot = 0; |
| 2043 | for(addr = host_start;addr < host_end; addr += TARGET_PAGE_SIZE) { |
| 2044 | prot |= p->flags; |
| 2045 | p++; |
| 2046 | } |
| 2047 | /* if the page was really writable, then we change its |
| 2048 | protection back to writable */ |
| 2049 | if (prot & PAGE_WRITE_ORG) { |
| 2050 | pindex = (address - host_start) >> TARGET_PAGE_BITS; |
| 2051 | if (!(p1[pindex].flags & PAGE_WRITE)) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2052 | mprotect((void *)g2h(host_start), qemu_host_page_size, |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2053 | (prot & PAGE_BITS) | PAGE_WRITE); |
| 2054 | p1[pindex].flags |= PAGE_WRITE; |
| 2055 | /* and since the content will be modified, we must invalidate |
| 2056 | the corresponding translated code. */ |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 2057 | tb_invalidate_phys_page(address, pc, puc); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2058 | #ifdef DEBUG_TB_CHECK |
| 2059 | tb_invalidate_check(address); |
| 2060 | #endif |
pbrook | c8a706f | 2008-06-02 16:16:42 +0000 | [diff] [blame] | 2061 | mmap_unlock(); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2062 | return 1; |
| 2063 | } |
| 2064 | } |
pbrook | c8a706f | 2008-06-02 16:16:42 +0000 | [diff] [blame] | 2065 | mmap_unlock(); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2066 | return 0; |
| 2067 | } |
| 2068 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 2069 | static inline void tlb_set_dirty(CPUState *env, |
| 2070 | unsigned long addr, target_ulong vaddr) |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2071 | { |
| 2072 | } |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2073 | #endif /* defined(CONFIG_USER_ONLY) */ |
| 2074 | |
pbrook | e2eef17 | 2008-06-08 01:09:01 +0000 | [diff] [blame] | 2075 | #if !defined(CONFIG_USER_ONLY) |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2076 | static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end, |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 2077 | ram_addr_t memory); |
| 2078 | static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys, |
| 2079 | ram_addr_t orig_memory); |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2080 | #define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \ |
| 2081 | need_subpage) \ |
| 2082 | do { \ |
| 2083 | if (addr > start_addr) \ |
| 2084 | start_addr2 = 0; \ |
| 2085 | else { \ |
| 2086 | start_addr2 = start_addr & ~TARGET_PAGE_MASK; \ |
| 2087 | if (start_addr2 > 0) \ |
| 2088 | need_subpage = 1; \ |
| 2089 | } \ |
| 2090 | \ |
blueswir1 | 49e9fba | 2007-05-30 17:25:06 +0000 | [diff] [blame] | 2091 | if ((start_addr + orig_size) - addr >= TARGET_PAGE_SIZE) \ |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2092 | end_addr2 = TARGET_PAGE_SIZE - 1; \ |
| 2093 | else { \ |
| 2094 | end_addr2 = (start_addr + orig_size - 1) & ~TARGET_PAGE_MASK; \ |
| 2095 | if (end_addr2 < TARGET_PAGE_SIZE - 1) \ |
| 2096 | need_subpage = 1; \ |
| 2097 | } \ |
| 2098 | } while (0) |
| 2099 | |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2100 | /* register physical memory. 'size' must be a multiple of the target |
| 2101 | page size. If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an |
| 2102 | io memory page */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2103 | void cpu_register_physical_memory(target_phys_addr_t start_addr, |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 2104 | ram_addr_t size, |
| 2105 | ram_addr_t phys_offset) |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2106 | { |
bellard | 108c49b | 2005-07-24 12:55:09 +0000 | [diff] [blame] | 2107 | target_phys_addr_t addr, end_addr; |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 2108 | PhysPageDesc *p; |
bellard | 9d42037 | 2006-06-25 22:25:22 +0000 | [diff] [blame] | 2109 | CPUState *env; |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 2110 | ram_addr_t orig_size = size; |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2111 | void *subpage; |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2112 | |
bellard | da26024 | 2008-05-30 20:48:25 +0000 | [diff] [blame] | 2113 | #ifdef USE_KQEMU |
| 2114 | /* XXX: should not depend on cpu context */ |
| 2115 | env = first_cpu; |
| 2116 | if (env->kqemu_enabled) { |
| 2117 | kqemu_set_phys_mem(start_addr, size, phys_offset); |
| 2118 | } |
| 2119 | #endif |
bellard | 5fd386f | 2004-05-23 21:11:22 +0000 | [diff] [blame] | 2120 | size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK; |
blueswir1 | 49e9fba | 2007-05-30 17:25:06 +0000 | [diff] [blame] | 2121 | end_addr = start_addr + (target_phys_addr_t)size; |
| 2122 | for(addr = start_addr; addr != end_addr; addr += TARGET_PAGE_SIZE) { |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2123 | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
| 2124 | if (p && p->phys_offset != IO_MEM_UNASSIGNED) { |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 2125 | ram_addr_t orig_memory = p->phys_offset; |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2126 | target_phys_addr_t start_addr2, end_addr2; |
| 2127 | int need_subpage = 0; |
| 2128 | |
| 2129 | CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, |
| 2130 | need_subpage); |
blueswir1 | 4254fab | 2008-01-01 16:57:19 +0000 | [diff] [blame] | 2131 | if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) { |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2132 | if (!(orig_memory & IO_MEM_SUBPAGE)) { |
| 2133 | subpage = subpage_init((addr & TARGET_PAGE_MASK), |
| 2134 | &p->phys_offset, orig_memory); |
| 2135 | } else { |
| 2136 | subpage = io_mem_opaque[(orig_memory & ~TARGET_PAGE_MASK) |
| 2137 | >> IO_MEM_SHIFT]; |
| 2138 | } |
| 2139 | subpage_register(subpage, start_addr2, end_addr2, phys_offset); |
| 2140 | } else { |
| 2141 | p->phys_offset = phys_offset; |
| 2142 | if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM || |
| 2143 | (phys_offset & IO_MEM_ROMD)) |
| 2144 | phys_offset += TARGET_PAGE_SIZE; |
| 2145 | } |
| 2146 | } else { |
| 2147 | p = phys_page_find_alloc(addr >> TARGET_PAGE_BITS, 1); |
| 2148 | p->phys_offset = phys_offset; |
| 2149 | if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM || |
| 2150 | (phys_offset & IO_MEM_ROMD)) |
| 2151 | phys_offset += TARGET_PAGE_SIZE; |
| 2152 | else { |
| 2153 | target_phys_addr_t start_addr2, end_addr2; |
| 2154 | int need_subpage = 0; |
| 2155 | |
| 2156 | CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, |
| 2157 | end_addr2, need_subpage); |
| 2158 | |
blueswir1 | 4254fab | 2008-01-01 16:57:19 +0000 | [diff] [blame] | 2159 | if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) { |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2160 | subpage = subpage_init((addr & TARGET_PAGE_MASK), |
| 2161 | &p->phys_offset, IO_MEM_UNASSIGNED); |
| 2162 | subpage_register(subpage, start_addr2, end_addr2, |
| 2163 | phys_offset); |
| 2164 | } |
| 2165 | } |
| 2166 | } |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2167 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2168 | |
bellard | 9d42037 | 2006-06-25 22:25:22 +0000 | [diff] [blame] | 2169 | /* since each CPU stores ram addresses in its TLB cache, we must |
| 2170 | reset the modified entries */ |
| 2171 | /* XXX: slow ! */ |
| 2172 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
| 2173 | tlb_flush(env, 1); |
| 2174 | } |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2175 | } |
| 2176 | |
bellard | ba86345 | 2006-09-24 18:41:10 +0000 | [diff] [blame] | 2177 | /* XXX: temporary until new memory mapping API */ |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 2178 | ram_addr_t cpu_get_physical_page_desc(target_phys_addr_t addr) |
bellard | ba86345 | 2006-09-24 18:41:10 +0000 | [diff] [blame] | 2179 | { |
| 2180 | PhysPageDesc *p; |
| 2181 | |
| 2182 | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
| 2183 | if (!p) |
| 2184 | return IO_MEM_UNASSIGNED; |
| 2185 | return p->phys_offset; |
| 2186 | } |
| 2187 | |
bellard | e9a1ab1 | 2007-02-08 23:08:38 +0000 | [diff] [blame] | 2188 | /* XXX: better than nothing */ |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 2189 | ram_addr_t qemu_ram_alloc(ram_addr_t size) |
bellard | e9a1ab1 | 2007-02-08 23:08:38 +0000 | [diff] [blame] | 2190 | { |
| 2191 | ram_addr_t addr; |
balrog | 7fb4fdc | 2008-04-24 17:59:27 +0000 | [diff] [blame] | 2192 | if ((phys_ram_alloc_offset + size) > phys_ram_size) { |
bellard | ed44146 | 2008-05-23 11:56:45 +0000 | [diff] [blame] | 2193 | fprintf(stderr, "Not enough memory (requested_size = %" PRIu64 ", max memory = %" PRIu64 "\n", |
| 2194 | (uint64_t)size, (uint64_t)phys_ram_size); |
bellard | e9a1ab1 | 2007-02-08 23:08:38 +0000 | [diff] [blame] | 2195 | abort(); |
| 2196 | } |
| 2197 | addr = phys_ram_alloc_offset; |
| 2198 | phys_ram_alloc_offset = TARGET_PAGE_ALIGN(phys_ram_alloc_offset + size); |
| 2199 | return addr; |
| 2200 | } |
| 2201 | |
| 2202 | void qemu_ram_free(ram_addr_t addr) |
| 2203 | { |
| 2204 | } |
| 2205 | |
bellard | a4193c8 | 2004-06-03 14:01:43 +0000 | [diff] [blame] | 2206 | static uint32_t unassigned_mem_readb(void *opaque, target_phys_addr_t addr) |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2207 | { |
pbrook | 67d3b95 | 2006-12-18 05:03:52 +0000 | [diff] [blame] | 2208 | #ifdef DEBUG_UNASSIGNED |
blueswir1 | ab3d172 | 2007-11-04 07:31:40 +0000 | [diff] [blame] | 2209 | printf("Unassigned mem read " TARGET_FMT_plx "\n", addr); |
pbrook | 67d3b95 | 2006-12-18 05:03:52 +0000 | [diff] [blame] | 2210 | #endif |
blueswir1 | b4f0a31 | 2007-05-06 17:59:24 +0000 | [diff] [blame] | 2211 | #ifdef TARGET_SPARC |
blueswir1 | 6c36d3f | 2007-05-17 19:30:10 +0000 | [diff] [blame] | 2212 | do_unassigned_access(addr, 0, 0, 0); |
ths | f1ccf90 | 2007-10-08 13:16:14 +0000 | [diff] [blame] | 2213 | #elif TARGET_CRIS |
| 2214 | do_unassigned_access(addr, 0, 0, 0); |
blueswir1 | b4f0a31 | 2007-05-06 17:59:24 +0000 | [diff] [blame] | 2215 | #endif |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2216 | return 0; |
| 2217 | } |
| 2218 | |
bellard | a4193c8 | 2004-06-03 14:01:43 +0000 | [diff] [blame] | 2219 | static void unassigned_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val) |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2220 | { |
pbrook | 67d3b95 | 2006-12-18 05:03:52 +0000 | [diff] [blame] | 2221 | #ifdef DEBUG_UNASSIGNED |
blueswir1 | ab3d172 | 2007-11-04 07:31:40 +0000 | [diff] [blame] | 2222 | printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val); |
pbrook | 67d3b95 | 2006-12-18 05:03:52 +0000 | [diff] [blame] | 2223 | #endif |
blueswir1 | b4f0a31 | 2007-05-06 17:59:24 +0000 | [diff] [blame] | 2224 | #ifdef TARGET_SPARC |
blueswir1 | 6c36d3f | 2007-05-17 19:30:10 +0000 | [diff] [blame] | 2225 | do_unassigned_access(addr, 1, 0, 0); |
ths | f1ccf90 | 2007-10-08 13:16:14 +0000 | [diff] [blame] | 2226 | #elif TARGET_CRIS |
| 2227 | do_unassigned_access(addr, 1, 0, 0); |
blueswir1 | b4f0a31 | 2007-05-06 17:59:24 +0000 | [diff] [blame] | 2228 | #endif |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
| 2231 | static CPUReadMemoryFunc *unassigned_mem_read[3] = { |
| 2232 | unassigned_mem_readb, |
| 2233 | unassigned_mem_readb, |
| 2234 | unassigned_mem_readb, |
| 2235 | }; |
| 2236 | |
| 2237 | static CPUWriteMemoryFunc *unassigned_mem_write[3] = { |
| 2238 | unassigned_mem_writeb, |
| 2239 | unassigned_mem_writeb, |
| 2240 | unassigned_mem_writeb, |
| 2241 | }; |
| 2242 | |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2243 | static void notdirty_mem_writeb(void *opaque, target_phys_addr_t ram_addr, |
| 2244 | uint32_t val) |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2245 | { |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2246 | int dirty_flags; |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2247 | dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS]; |
| 2248 | if (!(dirty_flags & CODE_DIRTY_FLAG)) { |
| 2249 | #if !defined(CONFIG_USER_ONLY) |
| 2250 | tb_invalidate_phys_page_fast(ram_addr, 1); |
| 2251 | dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS]; |
| 2252 | #endif |
| 2253 | } |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2254 | stb_p(phys_ram_base + ram_addr, val); |
bellard | f32fc64 | 2006-02-08 22:43:39 +0000 | [diff] [blame] | 2255 | #ifdef USE_KQEMU |
| 2256 | if (cpu_single_env->kqemu_enabled && |
| 2257 | (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK) |
| 2258 | kqemu_modify_page(cpu_single_env, ram_addr); |
| 2259 | #endif |
bellard | f23db16 | 2005-08-21 19:12:28 +0000 | [diff] [blame] | 2260 | dirty_flags |= (0xff & ~CODE_DIRTY_FLAG); |
| 2261 | phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags; |
| 2262 | /* we remove the notdirty callback only if the code has been |
| 2263 | flushed */ |
| 2264 | if (dirty_flags == 0xff) |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 2265 | tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr); |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2268 | static void notdirty_mem_writew(void *opaque, target_phys_addr_t ram_addr, |
| 2269 | uint32_t val) |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2270 | { |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2271 | int dirty_flags; |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2272 | dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS]; |
| 2273 | if (!(dirty_flags & CODE_DIRTY_FLAG)) { |
| 2274 | #if !defined(CONFIG_USER_ONLY) |
| 2275 | tb_invalidate_phys_page_fast(ram_addr, 2); |
| 2276 | dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS]; |
| 2277 | #endif |
| 2278 | } |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2279 | stw_p(phys_ram_base + ram_addr, val); |
bellard | f32fc64 | 2006-02-08 22:43:39 +0000 | [diff] [blame] | 2280 | #ifdef USE_KQEMU |
| 2281 | if (cpu_single_env->kqemu_enabled && |
| 2282 | (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK) |
| 2283 | kqemu_modify_page(cpu_single_env, ram_addr); |
| 2284 | #endif |
bellard | f23db16 | 2005-08-21 19:12:28 +0000 | [diff] [blame] | 2285 | dirty_flags |= (0xff & ~CODE_DIRTY_FLAG); |
| 2286 | phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags; |
| 2287 | /* we remove the notdirty callback only if the code has been |
| 2288 | flushed */ |
| 2289 | if (dirty_flags == 0xff) |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 2290 | tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr); |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2291 | } |
| 2292 | |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2293 | static void notdirty_mem_writel(void *opaque, target_phys_addr_t ram_addr, |
| 2294 | uint32_t val) |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2295 | { |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2296 | int dirty_flags; |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2297 | dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS]; |
| 2298 | if (!(dirty_flags & CODE_DIRTY_FLAG)) { |
| 2299 | #if !defined(CONFIG_USER_ONLY) |
| 2300 | tb_invalidate_phys_page_fast(ram_addr, 4); |
| 2301 | dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS]; |
| 2302 | #endif |
| 2303 | } |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2304 | stl_p(phys_ram_base + ram_addr, val); |
bellard | f32fc64 | 2006-02-08 22:43:39 +0000 | [diff] [blame] | 2305 | #ifdef USE_KQEMU |
| 2306 | if (cpu_single_env->kqemu_enabled && |
| 2307 | (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK) |
| 2308 | kqemu_modify_page(cpu_single_env, ram_addr); |
| 2309 | #endif |
bellard | f23db16 | 2005-08-21 19:12:28 +0000 | [diff] [blame] | 2310 | dirty_flags |= (0xff & ~CODE_DIRTY_FLAG); |
| 2311 | phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags; |
| 2312 | /* we remove the notdirty callback only if the code has been |
| 2313 | flushed */ |
| 2314 | if (dirty_flags == 0xff) |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 2315 | tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr); |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2318 | static CPUReadMemoryFunc *error_mem_read[3] = { |
| 2319 | NULL, /* never used */ |
| 2320 | NULL, /* never used */ |
| 2321 | NULL, /* never used */ |
| 2322 | }; |
| 2323 | |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2324 | static CPUWriteMemoryFunc *notdirty_mem_write[3] = { |
| 2325 | notdirty_mem_writeb, |
| 2326 | notdirty_mem_writew, |
| 2327 | notdirty_mem_writel, |
| 2328 | }; |
| 2329 | |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2330 | /* Generate a debug exception if a watchpoint has been hit. */ |
| 2331 | static void check_watchpoint(int offset, int flags) |
| 2332 | { |
| 2333 | CPUState *env = cpu_single_env; |
| 2334 | target_ulong vaddr; |
| 2335 | int i; |
| 2336 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 2337 | vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset; |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2338 | for (i = 0; i < env->nb_watchpoints; i++) { |
| 2339 | if (vaddr == env->watchpoint[i].vaddr |
| 2340 | && (env->watchpoint[i].type & flags)) { |
| 2341 | env->watchpoint_hit = i + 1; |
| 2342 | cpu_interrupt(env, CPU_INTERRUPT_DEBUG); |
| 2343 | break; |
| 2344 | } |
| 2345 | } |
| 2346 | } |
| 2347 | |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2348 | /* Watchpoint access routines. Watchpoints are inserted using TLB tricks, |
| 2349 | so these check for a hit then pass through to the normal out-of-line |
| 2350 | phys routines. */ |
| 2351 | static uint32_t watch_mem_readb(void *opaque, target_phys_addr_t addr) |
| 2352 | { |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2353 | check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_READ); |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2354 | return ldub_phys(addr); |
| 2355 | } |
| 2356 | |
| 2357 | static uint32_t watch_mem_readw(void *opaque, target_phys_addr_t addr) |
| 2358 | { |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2359 | check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_READ); |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2360 | return lduw_phys(addr); |
| 2361 | } |
| 2362 | |
| 2363 | static uint32_t watch_mem_readl(void *opaque, target_phys_addr_t addr) |
| 2364 | { |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2365 | check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_READ); |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2366 | return ldl_phys(addr); |
| 2367 | } |
| 2368 | |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2369 | static void watch_mem_writeb(void *opaque, target_phys_addr_t addr, |
| 2370 | uint32_t val) |
| 2371 | { |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2372 | check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_WRITE); |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2373 | stb_phys(addr, val); |
| 2374 | } |
| 2375 | |
| 2376 | static void watch_mem_writew(void *opaque, target_phys_addr_t addr, |
| 2377 | uint32_t val) |
| 2378 | { |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2379 | check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_WRITE); |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2380 | stw_phys(addr, val); |
| 2381 | } |
| 2382 | |
| 2383 | static void watch_mem_writel(void *opaque, target_phys_addr_t addr, |
| 2384 | uint32_t val) |
| 2385 | { |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2386 | check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_WRITE); |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2387 | stl_phys(addr, val); |
| 2388 | } |
| 2389 | |
| 2390 | static CPUReadMemoryFunc *watch_mem_read[3] = { |
| 2391 | watch_mem_readb, |
| 2392 | watch_mem_readw, |
| 2393 | watch_mem_readl, |
| 2394 | }; |
| 2395 | |
| 2396 | static CPUWriteMemoryFunc *watch_mem_write[3] = { |
| 2397 | watch_mem_writeb, |
| 2398 | watch_mem_writew, |
| 2399 | watch_mem_writel, |
| 2400 | }; |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2401 | |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2402 | static inline uint32_t subpage_readlen (subpage_t *mmio, target_phys_addr_t addr, |
| 2403 | unsigned int len) |
| 2404 | { |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2405 | uint32_t ret; |
| 2406 | unsigned int idx; |
| 2407 | |
| 2408 | idx = SUBPAGE_IDX(addr - mmio->base); |
| 2409 | #if defined(DEBUG_SUBPAGE) |
| 2410 | printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__, |
| 2411 | mmio, len, addr, idx); |
| 2412 | #endif |
blueswir1 | 3ee8992 | 2008-01-02 19:45:26 +0000 | [diff] [blame] | 2413 | ret = (**mmio->mem_read[idx][len])(mmio->opaque[idx][0][len], addr); |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2414 | |
| 2415 | return ret; |
| 2416 | } |
| 2417 | |
| 2418 | static inline void subpage_writelen (subpage_t *mmio, target_phys_addr_t addr, |
| 2419 | uint32_t value, unsigned int len) |
| 2420 | { |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2421 | unsigned int idx; |
| 2422 | |
| 2423 | idx = SUBPAGE_IDX(addr - mmio->base); |
| 2424 | #if defined(DEBUG_SUBPAGE) |
| 2425 | printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d value %08x\n", __func__, |
| 2426 | mmio, len, addr, idx, value); |
| 2427 | #endif |
blueswir1 | 3ee8992 | 2008-01-02 19:45:26 +0000 | [diff] [blame] | 2428 | (**mmio->mem_write[idx][len])(mmio->opaque[idx][1][len], addr, value); |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2429 | } |
| 2430 | |
| 2431 | static uint32_t subpage_readb (void *opaque, target_phys_addr_t addr) |
| 2432 | { |
| 2433 | #if defined(DEBUG_SUBPAGE) |
| 2434 | printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr); |
| 2435 | #endif |
| 2436 | |
| 2437 | return subpage_readlen(opaque, addr, 0); |
| 2438 | } |
| 2439 | |
| 2440 | static void subpage_writeb (void *opaque, target_phys_addr_t addr, |
| 2441 | uint32_t value) |
| 2442 | { |
| 2443 | #if defined(DEBUG_SUBPAGE) |
| 2444 | printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value); |
| 2445 | #endif |
| 2446 | subpage_writelen(opaque, addr, value, 0); |
| 2447 | } |
| 2448 | |
| 2449 | static uint32_t subpage_readw (void *opaque, target_phys_addr_t addr) |
| 2450 | { |
| 2451 | #if defined(DEBUG_SUBPAGE) |
| 2452 | printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr); |
| 2453 | #endif |
| 2454 | |
| 2455 | return subpage_readlen(opaque, addr, 1); |
| 2456 | } |
| 2457 | |
| 2458 | static void subpage_writew (void *opaque, target_phys_addr_t addr, |
| 2459 | uint32_t value) |
| 2460 | { |
| 2461 | #if defined(DEBUG_SUBPAGE) |
| 2462 | printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value); |
| 2463 | #endif |
| 2464 | subpage_writelen(opaque, addr, value, 1); |
| 2465 | } |
| 2466 | |
| 2467 | static uint32_t subpage_readl (void *opaque, target_phys_addr_t addr) |
| 2468 | { |
| 2469 | #if defined(DEBUG_SUBPAGE) |
| 2470 | printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr); |
| 2471 | #endif |
| 2472 | |
| 2473 | return subpage_readlen(opaque, addr, 2); |
| 2474 | } |
| 2475 | |
| 2476 | static void subpage_writel (void *opaque, |
| 2477 | target_phys_addr_t addr, uint32_t value) |
| 2478 | { |
| 2479 | #if defined(DEBUG_SUBPAGE) |
| 2480 | printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value); |
| 2481 | #endif |
| 2482 | subpage_writelen(opaque, addr, value, 2); |
| 2483 | } |
| 2484 | |
| 2485 | static CPUReadMemoryFunc *subpage_read[] = { |
| 2486 | &subpage_readb, |
| 2487 | &subpage_readw, |
| 2488 | &subpage_readl, |
| 2489 | }; |
| 2490 | |
| 2491 | static CPUWriteMemoryFunc *subpage_write[] = { |
| 2492 | &subpage_writeb, |
| 2493 | &subpage_writew, |
| 2494 | &subpage_writel, |
| 2495 | }; |
| 2496 | |
| 2497 | static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end, |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 2498 | ram_addr_t memory) |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2499 | { |
| 2500 | int idx, eidx; |
blueswir1 | 4254fab | 2008-01-01 16:57:19 +0000 | [diff] [blame] | 2501 | unsigned int i; |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2502 | |
| 2503 | if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE) |
| 2504 | return -1; |
| 2505 | idx = SUBPAGE_IDX(start); |
| 2506 | eidx = SUBPAGE_IDX(end); |
| 2507 | #if defined(DEBUG_SUBPAGE) |
| 2508 | printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %d\n", __func__, |
| 2509 | mmio, start, end, idx, eidx, memory); |
| 2510 | #endif |
| 2511 | memory >>= IO_MEM_SHIFT; |
| 2512 | for (; idx <= eidx; idx++) { |
blueswir1 | 4254fab | 2008-01-01 16:57:19 +0000 | [diff] [blame] | 2513 | for (i = 0; i < 4; i++) { |
blueswir1 | 3ee8992 | 2008-01-02 19:45:26 +0000 | [diff] [blame] | 2514 | if (io_mem_read[memory][i]) { |
| 2515 | mmio->mem_read[idx][i] = &io_mem_read[memory][i]; |
| 2516 | mmio->opaque[idx][0][i] = io_mem_opaque[memory]; |
| 2517 | } |
| 2518 | if (io_mem_write[memory][i]) { |
| 2519 | mmio->mem_write[idx][i] = &io_mem_write[memory][i]; |
| 2520 | mmio->opaque[idx][1][i] = io_mem_opaque[memory]; |
| 2521 | } |
blueswir1 | 4254fab | 2008-01-01 16:57:19 +0000 | [diff] [blame] | 2522 | } |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2523 | } |
| 2524 | |
| 2525 | return 0; |
| 2526 | } |
| 2527 | |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 2528 | static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys, |
| 2529 | ram_addr_t orig_memory) |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2530 | { |
| 2531 | subpage_t *mmio; |
| 2532 | int subpage_memory; |
| 2533 | |
| 2534 | mmio = qemu_mallocz(sizeof(subpage_t)); |
| 2535 | if (mmio != NULL) { |
| 2536 | mmio->base = base; |
| 2537 | subpage_memory = cpu_register_io_memory(0, subpage_read, subpage_write, mmio); |
| 2538 | #if defined(DEBUG_SUBPAGE) |
| 2539 | printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__, |
| 2540 | mmio, base, TARGET_PAGE_SIZE, subpage_memory); |
| 2541 | #endif |
| 2542 | *phys = subpage_memory | IO_MEM_SUBPAGE; |
| 2543 | subpage_register(mmio, 0, TARGET_PAGE_SIZE - 1, orig_memory); |
| 2544 | } |
| 2545 | |
| 2546 | return mmio; |
| 2547 | } |
| 2548 | |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2549 | static void io_mem_init(void) |
| 2550 | { |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2551 | cpu_register_io_memory(IO_MEM_ROM >> IO_MEM_SHIFT, error_mem_read, unassigned_mem_write, NULL); |
bellard | a4193c8 | 2004-06-03 14:01:43 +0000 | [diff] [blame] | 2552 | cpu_register_io_memory(IO_MEM_UNASSIGNED >> IO_MEM_SHIFT, unassigned_mem_read, unassigned_mem_write, NULL); |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2553 | cpu_register_io_memory(IO_MEM_NOTDIRTY >> IO_MEM_SHIFT, error_mem_read, notdirty_mem_write, NULL); |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2554 | io_mem_nb = 5; |
| 2555 | |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2556 | io_mem_watch = cpu_register_io_memory(0, watch_mem_read, |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2557 | watch_mem_write, NULL); |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2558 | /* alloc dirty bits array */ |
bellard | 0a962c0 | 2005-02-10 22:00:27 +0000 | [diff] [blame] | 2559 | phys_ram_dirty = qemu_vmalloc(phys_ram_size >> TARGET_PAGE_BITS); |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2560 | memset(phys_ram_dirty, 0xff, phys_ram_size >> TARGET_PAGE_BITS); |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2561 | } |
| 2562 | |
| 2563 | /* mem_read and mem_write are arrays of functions containing the |
| 2564 | function to access byte (index 0), word (index 1) and dword (index |
blueswir1 | 3ee8992 | 2008-01-02 19:45:26 +0000 | [diff] [blame] | 2565 | 2). Functions can be omitted with a NULL function pointer. The |
| 2566 | registered functions may be modified dynamically later. |
| 2567 | If io_index is non zero, the corresponding io zone is |
blueswir1 | 4254fab | 2008-01-01 16:57:19 +0000 | [diff] [blame] | 2568 | modified. If it is zero, a new io zone is allocated. The return |
| 2569 | value can be used with cpu_register_physical_memory(). (-1) is |
| 2570 | returned if error. */ |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2571 | int cpu_register_io_memory(int io_index, |
| 2572 | CPUReadMemoryFunc **mem_read, |
bellard | a4193c8 | 2004-06-03 14:01:43 +0000 | [diff] [blame] | 2573 | CPUWriteMemoryFunc **mem_write, |
| 2574 | void *opaque) |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2575 | { |
blueswir1 | 4254fab | 2008-01-01 16:57:19 +0000 | [diff] [blame] | 2576 | int i, subwidth = 0; |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2577 | |
| 2578 | if (io_index <= 0) { |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2579 | if (io_mem_nb >= IO_MEM_NB_ENTRIES) |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2580 | return -1; |
| 2581 | io_index = io_mem_nb++; |
| 2582 | } else { |
| 2583 | if (io_index >= IO_MEM_NB_ENTRIES) |
| 2584 | return -1; |
| 2585 | } |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2586 | |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2587 | for(i = 0;i < 3; i++) { |
blueswir1 | 4254fab | 2008-01-01 16:57:19 +0000 | [diff] [blame] | 2588 | if (!mem_read[i] || !mem_write[i]) |
| 2589 | subwidth = IO_MEM_SUBWIDTH; |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2590 | io_mem_read[io_index][i] = mem_read[i]; |
| 2591 | io_mem_write[io_index][i] = mem_write[i]; |
| 2592 | } |
bellard | a4193c8 | 2004-06-03 14:01:43 +0000 | [diff] [blame] | 2593 | io_mem_opaque[io_index] = opaque; |
blueswir1 | 4254fab | 2008-01-01 16:57:19 +0000 | [diff] [blame] | 2594 | return (io_index << IO_MEM_SHIFT) | subwidth; |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2595 | } |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 2596 | |
bellard | 8926b51 | 2004-10-10 15:14:20 +0000 | [diff] [blame] | 2597 | CPUWriteMemoryFunc **cpu_get_io_memory_write(int io_index) |
| 2598 | { |
| 2599 | return io_mem_write[io_index >> IO_MEM_SHIFT]; |
| 2600 | } |
| 2601 | |
| 2602 | CPUReadMemoryFunc **cpu_get_io_memory_read(int io_index) |
| 2603 | { |
| 2604 | return io_mem_read[io_index >> IO_MEM_SHIFT]; |
| 2605 | } |
| 2606 | |
pbrook | e2eef17 | 2008-06-08 01:09:01 +0000 | [diff] [blame] | 2607 | #endif /* !defined(CONFIG_USER_ONLY) */ |
| 2608 | |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 2609 | /* physical memory access (slow version, mainly for debug) */ |
| 2610 | #if defined(CONFIG_USER_ONLY) |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2611 | void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf, |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 2612 | int len, int is_write) |
| 2613 | { |
| 2614 | int l, flags; |
| 2615 | target_ulong page; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2616 | void * p; |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 2617 | |
| 2618 | while (len > 0) { |
| 2619 | page = addr & TARGET_PAGE_MASK; |
| 2620 | l = (page + TARGET_PAGE_SIZE) - addr; |
| 2621 | if (l > len) |
| 2622 | l = len; |
| 2623 | flags = page_get_flags(page); |
| 2624 | if (!(flags & PAGE_VALID)) |
| 2625 | return; |
| 2626 | if (is_write) { |
| 2627 | if (!(flags & PAGE_WRITE)) |
| 2628 | return; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 2629 | /* XXX: this code should not depend on lock_user */ |
aurel32 | 72fb7da | 2008-04-27 23:53:45 +0000 | [diff] [blame] | 2630 | if (!(p = lock_user(VERIFY_WRITE, addr, l, 0))) |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 2631 | /* FIXME - should this return an error rather than just fail? */ |
| 2632 | return; |
aurel32 | 72fb7da | 2008-04-27 23:53:45 +0000 | [diff] [blame] | 2633 | memcpy(p, buf, l); |
| 2634 | unlock_user(p, addr, l); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 2635 | } else { |
| 2636 | if (!(flags & PAGE_READ)) |
| 2637 | return; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 2638 | /* XXX: this code should not depend on lock_user */ |
aurel32 | 72fb7da | 2008-04-27 23:53:45 +0000 | [diff] [blame] | 2639 | if (!(p = lock_user(VERIFY_READ, addr, l, 1))) |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 2640 | /* FIXME - should this return an error rather than just fail? */ |
| 2641 | return; |
aurel32 | 72fb7da | 2008-04-27 23:53:45 +0000 | [diff] [blame] | 2642 | memcpy(buf, p, l); |
aurel32 | 5b25757 | 2008-04-28 08:54:59 +0000 | [diff] [blame] | 2643 | unlock_user(p, addr, 0); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 2644 | } |
| 2645 | len -= l; |
| 2646 | buf += l; |
| 2647 | addr += l; |
| 2648 | } |
| 2649 | } |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 2650 | |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 2651 | #else |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2652 | void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf, |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 2653 | int len, int is_write) |
| 2654 | { |
| 2655 | int l, io_index; |
| 2656 | uint8_t *ptr; |
| 2657 | uint32_t val; |
bellard | 2e12669 | 2004-04-25 21:28:44 +0000 | [diff] [blame] | 2658 | target_phys_addr_t page; |
| 2659 | unsigned long pd; |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 2660 | PhysPageDesc *p; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2661 | |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 2662 | while (len > 0) { |
| 2663 | page = addr & TARGET_PAGE_MASK; |
| 2664 | l = (page + TARGET_PAGE_SIZE) - addr; |
| 2665 | if (l > len) |
| 2666 | l = len; |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 2667 | p = phys_page_find(page >> TARGET_PAGE_BITS); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 2668 | if (!p) { |
| 2669 | pd = IO_MEM_UNASSIGNED; |
| 2670 | } else { |
| 2671 | pd = p->phys_offset; |
| 2672 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2673 | |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 2674 | if (is_write) { |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2675 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) { |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 2676 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 2677 | /* XXX: could force cpu_single_env to NULL to avoid |
| 2678 | potential bugs */ |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 2679 | if (l >= 4 && ((addr & 3) == 0)) { |
bellard | 1c213d1 | 2005-09-03 10:49:04 +0000 | [diff] [blame] | 2680 | /* 32 bit write access */ |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 2681 | val = ldl_p(buf); |
bellard | a4193c8 | 2004-06-03 14:01:43 +0000 | [diff] [blame] | 2682 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 2683 | l = 4; |
| 2684 | } else if (l >= 2 && ((addr & 1) == 0)) { |
bellard | 1c213d1 | 2005-09-03 10:49:04 +0000 | [diff] [blame] | 2685 | /* 16 bit write access */ |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 2686 | val = lduw_p(buf); |
bellard | a4193c8 | 2004-06-03 14:01:43 +0000 | [diff] [blame] | 2687 | io_mem_write[io_index][1](io_mem_opaque[io_index], addr, val); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 2688 | l = 2; |
| 2689 | } else { |
bellard | 1c213d1 | 2005-09-03 10:49:04 +0000 | [diff] [blame] | 2690 | /* 8 bit write access */ |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 2691 | val = ldub_p(buf); |
bellard | a4193c8 | 2004-06-03 14:01:43 +0000 | [diff] [blame] | 2692 | io_mem_write[io_index][0](io_mem_opaque[io_index], addr, val); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 2693 | l = 1; |
| 2694 | } |
| 2695 | } else { |
bellard | b448f2f | 2004-02-25 23:24:04 +0000 | [diff] [blame] | 2696 | unsigned long addr1; |
| 2697 | addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 2698 | /* RAM case */ |
bellard | b448f2f | 2004-02-25 23:24:04 +0000 | [diff] [blame] | 2699 | ptr = phys_ram_base + addr1; |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 2700 | memcpy(ptr, buf, l); |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2701 | if (!cpu_physical_memory_is_dirty(addr1)) { |
| 2702 | /* invalidate code */ |
| 2703 | tb_invalidate_phys_page_range(addr1, addr1 + l, 0); |
| 2704 | /* set dirty bit */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2705 | phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |= |
bellard | f23db16 | 2005-08-21 19:12:28 +0000 | [diff] [blame] | 2706 | (0xff & ~CODE_DIRTY_FLAG); |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2707 | } |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 2708 | } |
| 2709 | } else { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2710 | if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && |
bellard | 2a4188a | 2006-06-25 21:54:59 +0000 | [diff] [blame] | 2711 | !(pd & IO_MEM_ROMD)) { |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 2712 | /* I/O case */ |
| 2713 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
| 2714 | if (l >= 4 && ((addr & 3) == 0)) { |
| 2715 | /* 32 bit read access */ |
bellard | a4193c8 | 2004-06-03 14:01:43 +0000 | [diff] [blame] | 2716 | val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr); |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 2717 | stl_p(buf, val); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 2718 | l = 4; |
| 2719 | } else if (l >= 2 && ((addr & 1) == 0)) { |
| 2720 | /* 16 bit read access */ |
bellard | a4193c8 | 2004-06-03 14:01:43 +0000 | [diff] [blame] | 2721 | val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr); |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 2722 | stw_p(buf, val); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 2723 | l = 2; |
| 2724 | } else { |
bellard | 1c213d1 | 2005-09-03 10:49:04 +0000 | [diff] [blame] | 2725 | /* 8 bit read access */ |
bellard | a4193c8 | 2004-06-03 14:01:43 +0000 | [diff] [blame] | 2726 | val = io_mem_read[io_index][0](io_mem_opaque[io_index], addr); |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 2727 | stb_p(buf, val); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 2728 | l = 1; |
| 2729 | } |
| 2730 | } else { |
| 2731 | /* RAM case */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2732 | ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) + |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 2733 | (addr & ~TARGET_PAGE_MASK); |
| 2734 | memcpy(buf, ptr, l); |
| 2735 | } |
| 2736 | } |
| 2737 | len -= l; |
| 2738 | buf += l; |
| 2739 | addr += l; |
| 2740 | } |
| 2741 | } |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 2742 | |
bellard | d0ecd2a | 2006-04-23 17:14:48 +0000 | [diff] [blame] | 2743 | /* used for ROM loading : can write in RAM and ROM */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2744 | void cpu_physical_memory_write_rom(target_phys_addr_t addr, |
bellard | d0ecd2a | 2006-04-23 17:14:48 +0000 | [diff] [blame] | 2745 | const uint8_t *buf, int len) |
| 2746 | { |
| 2747 | int l; |
| 2748 | uint8_t *ptr; |
| 2749 | target_phys_addr_t page; |
| 2750 | unsigned long pd; |
| 2751 | PhysPageDesc *p; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2752 | |
bellard | d0ecd2a | 2006-04-23 17:14:48 +0000 | [diff] [blame] | 2753 | while (len > 0) { |
| 2754 | page = addr & TARGET_PAGE_MASK; |
| 2755 | l = (page + TARGET_PAGE_SIZE) - addr; |
| 2756 | if (l > len) |
| 2757 | l = len; |
| 2758 | p = phys_page_find(page >> TARGET_PAGE_BITS); |
| 2759 | if (!p) { |
| 2760 | pd = IO_MEM_UNASSIGNED; |
| 2761 | } else { |
| 2762 | pd = p->phys_offset; |
| 2763 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2764 | |
bellard | d0ecd2a | 2006-04-23 17:14:48 +0000 | [diff] [blame] | 2765 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM && |
bellard | 2a4188a | 2006-06-25 21:54:59 +0000 | [diff] [blame] | 2766 | (pd & ~TARGET_PAGE_MASK) != IO_MEM_ROM && |
| 2767 | !(pd & IO_MEM_ROMD)) { |
bellard | d0ecd2a | 2006-04-23 17:14:48 +0000 | [diff] [blame] | 2768 | /* do nothing */ |
| 2769 | } else { |
| 2770 | unsigned long addr1; |
| 2771 | addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); |
| 2772 | /* ROM/RAM case */ |
| 2773 | ptr = phys_ram_base + addr1; |
| 2774 | memcpy(ptr, buf, l); |
| 2775 | } |
| 2776 | len -= l; |
| 2777 | buf += l; |
| 2778 | addr += l; |
| 2779 | } |
| 2780 | } |
| 2781 | |
| 2782 | |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 2783 | /* warning: addr must be aligned */ |
| 2784 | uint32_t ldl_phys(target_phys_addr_t addr) |
| 2785 | { |
| 2786 | int io_index; |
| 2787 | uint8_t *ptr; |
| 2788 | uint32_t val; |
| 2789 | unsigned long pd; |
| 2790 | PhysPageDesc *p; |
| 2791 | |
| 2792 | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
| 2793 | if (!p) { |
| 2794 | pd = IO_MEM_UNASSIGNED; |
| 2795 | } else { |
| 2796 | pd = p->phys_offset; |
| 2797 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2798 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2799 | if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && |
bellard | 2a4188a | 2006-06-25 21:54:59 +0000 | [diff] [blame] | 2800 | !(pd & IO_MEM_ROMD)) { |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 2801 | /* I/O case */ |
| 2802 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
| 2803 | val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr); |
| 2804 | } else { |
| 2805 | /* RAM case */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2806 | ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) + |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 2807 | (addr & ~TARGET_PAGE_MASK); |
| 2808 | val = ldl_p(ptr); |
| 2809 | } |
| 2810 | return val; |
| 2811 | } |
| 2812 | |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 2813 | /* warning: addr must be aligned */ |
| 2814 | uint64_t ldq_phys(target_phys_addr_t addr) |
| 2815 | { |
| 2816 | int io_index; |
| 2817 | uint8_t *ptr; |
| 2818 | uint64_t val; |
| 2819 | unsigned long pd; |
| 2820 | PhysPageDesc *p; |
| 2821 | |
| 2822 | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
| 2823 | if (!p) { |
| 2824 | pd = IO_MEM_UNASSIGNED; |
| 2825 | } else { |
| 2826 | pd = p->phys_offset; |
| 2827 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2828 | |
bellard | 2a4188a | 2006-06-25 21:54:59 +0000 | [diff] [blame] | 2829 | if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && |
| 2830 | !(pd & IO_MEM_ROMD)) { |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 2831 | /* I/O case */ |
| 2832 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
| 2833 | #ifdef TARGET_WORDS_BIGENDIAN |
| 2834 | val = (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr) << 32; |
| 2835 | val |= io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4); |
| 2836 | #else |
| 2837 | val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr); |
| 2838 | val |= (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4) << 32; |
| 2839 | #endif |
| 2840 | } else { |
| 2841 | /* RAM case */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2842 | ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) + |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 2843 | (addr & ~TARGET_PAGE_MASK); |
| 2844 | val = ldq_p(ptr); |
| 2845 | } |
| 2846 | return val; |
| 2847 | } |
| 2848 | |
bellard | aab3309 | 2005-10-30 20:48:42 +0000 | [diff] [blame] | 2849 | /* XXX: optimize */ |
| 2850 | uint32_t ldub_phys(target_phys_addr_t addr) |
| 2851 | { |
| 2852 | uint8_t val; |
| 2853 | cpu_physical_memory_read(addr, &val, 1); |
| 2854 | return val; |
| 2855 | } |
| 2856 | |
| 2857 | /* XXX: optimize */ |
| 2858 | uint32_t lduw_phys(target_phys_addr_t addr) |
| 2859 | { |
| 2860 | uint16_t val; |
| 2861 | cpu_physical_memory_read(addr, (uint8_t *)&val, 2); |
| 2862 | return tswap16(val); |
| 2863 | } |
| 2864 | |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 2865 | /* warning: addr must be aligned. The ram page is not masked as dirty |
| 2866 | and the code inside is not invalidated. It is useful if the dirty |
| 2867 | bits are used to track modified PTEs */ |
| 2868 | void stl_phys_notdirty(target_phys_addr_t addr, uint32_t val) |
| 2869 | { |
| 2870 | int io_index; |
| 2871 | uint8_t *ptr; |
| 2872 | unsigned long pd; |
| 2873 | PhysPageDesc *p; |
| 2874 | |
| 2875 | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
| 2876 | if (!p) { |
| 2877 | pd = IO_MEM_UNASSIGNED; |
| 2878 | } else { |
| 2879 | pd = p->phys_offset; |
| 2880 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2881 | |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2882 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) { |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 2883 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
| 2884 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val); |
| 2885 | } else { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2886 | ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) + |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 2887 | (addr & ~TARGET_PAGE_MASK); |
| 2888 | stl_p(ptr, val); |
| 2889 | } |
| 2890 | } |
| 2891 | |
j_mayer | bc98a7e | 2007-04-04 07:55:12 +0000 | [diff] [blame] | 2892 | void stq_phys_notdirty(target_phys_addr_t addr, uint64_t val) |
| 2893 | { |
| 2894 | int io_index; |
| 2895 | uint8_t *ptr; |
| 2896 | unsigned long pd; |
| 2897 | PhysPageDesc *p; |
| 2898 | |
| 2899 | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
| 2900 | if (!p) { |
| 2901 | pd = IO_MEM_UNASSIGNED; |
| 2902 | } else { |
| 2903 | pd = p->phys_offset; |
| 2904 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2905 | |
j_mayer | bc98a7e | 2007-04-04 07:55:12 +0000 | [diff] [blame] | 2906 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) { |
| 2907 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
| 2908 | #ifdef TARGET_WORDS_BIGENDIAN |
| 2909 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val >> 32); |
| 2910 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val); |
| 2911 | #else |
| 2912 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val); |
| 2913 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val >> 32); |
| 2914 | #endif |
| 2915 | } else { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2916 | ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) + |
j_mayer | bc98a7e | 2007-04-04 07:55:12 +0000 | [diff] [blame] | 2917 | (addr & ~TARGET_PAGE_MASK); |
| 2918 | stq_p(ptr, val); |
| 2919 | } |
| 2920 | } |
| 2921 | |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 2922 | /* warning: addr must be aligned */ |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 2923 | void stl_phys(target_phys_addr_t addr, uint32_t val) |
| 2924 | { |
| 2925 | int io_index; |
| 2926 | uint8_t *ptr; |
| 2927 | unsigned long pd; |
| 2928 | PhysPageDesc *p; |
| 2929 | |
| 2930 | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
| 2931 | if (!p) { |
| 2932 | pd = IO_MEM_UNASSIGNED; |
| 2933 | } else { |
| 2934 | pd = p->phys_offset; |
| 2935 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2936 | |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2937 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) { |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 2938 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
| 2939 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val); |
| 2940 | } else { |
| 2941 | unsigned long addr1; |
| 2942 | addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); |
| 2943 | /* RAM case */ |
| 2944 | ptr = phys_ram_base + addr1; |
| 2945 | stl_p(ptr, val); |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2946 | if (!cpu_physical_memory_is_dirty(addr1)) { |
| 2947 | /* invalidate code */ |
| 2948 | tb_invalidate_phys_page_range(addr1, addr1 + 4, 0); |
| 2949 | /* set dirty bit */ |
bellard | f23db16 | 2005-08-21 19:12:28 +0000 | [diff] [blame] | 2950 | phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |= |
| 2951 | (0xff & ~CODE_DIRTY_FLAG); |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2952 | } |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 2953 | } |
| 2954 | } |
| 2955 | |
bellard | aab3309 | 2005-10-30 20:48:42 +0000 | [diff] [blame] | 2956 | /* XXX: optimize */ |
| 2957 | void stb_phys(target_phys_addr_t addr, uint32_t val) |
| 2958 | { |
| 2959 | uint8_t v = val; |
| 2960 | cpu_physical_memory_write(addr, &v, 1); |
| 2961 | } |
| 2962 | |
| 2963 | /* XXX: optimize */ |
| 2964 | void stw_phys(target_phys_addr_t addr, uint32_t val) |
| 2965 | { |
| 2966 | uint16_t v = tswap16(val); |
| 2967 | cpu_physical_memory_write(addr, (const uint8_t *)&v, 2); |
| 2968 | } |
| 2969 | |
| 2970 | /* XXX: optimize */ |
| 2971 | void stq_phys(target_phys_addr_t addr, uint64_t val) |
| 2972 | { |
| 2973 | val = tswap64(val); |
| 2974 | cpu_physical_memory_write(addr, (const uint8_t *)&val, 8); |
| 2975 | } |
| 2976 | |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 2977 | #endif |
| 2978 | |
| 2979 | /* virtual memory access for debug */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2980 | int cpu_memory_rw_debug(CPUState *env, target_ulong addr, |
bellard | b448f2f | 2004-02-25 23:24:04 +0000 | [diff] [blame] | 2981 | uint8_t *buf, int len, int is_write) |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 2982 | { |
| 2983 | int l; |
j_mayer | 9b3c35e | 2007-04-07 11:21:28 +0000 | [diff] [blame] | 2984 | target_phys_addr_t phys_addr; |
| 2985 | target_ulong page; |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 2986 | |
| 2987 | while (len > 0) { |
| 2988 | page = addr & TARGET_PAGE_MASK; |
| 2989 | phys_addr = cpu_get_phys_page_debug(env, page); |
| 2990 | /* if no physical page mapped, return an error */ |
| 2991 | if (phys_addr == -1) |
| 2992 | return -1; |
| 2993 | l = (page + TARGET_PAGE_SIZE) - addr; |
| 2994 | if (l > len) |
| 2995 | l = len; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2996 | cpu_physical_memory_rw(phys_addr + (addr & ~TARGET_PAGE_MASK), |
bellard | b448f2f | 2004-02-25 23:24:04 +0000 | [diff] [blame] | 2997 | buf, l, is_write); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 2998 | len -= l; |
| 2999 | buf += l; |
| 3000 | addr += l; |
| 3001 | } |
| 3002 | return 0; |
| 3003 | } |
| 3004 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 3005 | /* in deterministic execution mode, instructions doing device I/Os |
| 3006 | must be at the end of the TB */ |
| 3007 | void cpu_io_recompile(CPUState *env, void *retaddr) |
| 3008 | { |
| 3009 | TranslationBlock *tb; |
| 3010 | uint32_t n, cflags; |
| 3011 | target_ulong pc, cs_base; |
| 3012 | uint64_t flags; |
| 3013 | |
| 3014 | tb = tb_find_pc((unsigned long)retaddr); |
| 3015 | if (!tb) { |
| 3016 | cpu_abort(env, "cpu_io_recompile: could not find TB for pc=%p", |
| 3017 | retaddr); |
| 3018 | } |
| 3019 | n = env->icount_decr.u16.low + tb->icount; |
| 3020 | cpu_restore_state(tb, env, (unsigned long)retaddr, NULL); |
| 3021 | /* Calculate how many instructions had been executed before the fault |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 3022 | occurred. */ |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 3023 | n = n - env->icount_decr.u16.low; |
| 3024 | /* Generate a new TB ending on the I/O insn. */ |
| 3025 | n++; |
| 3026 | /* On MIPS and SH, delay slot instructions can only be restarted if |
| 3027 | they were already the first instruction in the TB. If this is not |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 3028 | the first instruction in a TB then re-execute the preceding |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 3029 | branch. */ |
| 3030 | #if defined(TARGET_MIPS) |
| 3031 | if ((env->hflags & MIPS_HFLAG_BMASK) != 0 && n > 1) { |
| 3032 | env->active_tc.PC -= 4; |
| 3033 | env->icount_decr.u16.low++; |
| 3034 | env->hflags &= ~MIPS_HFLAG_BMASK; |
| 3035 | } |
| 3036 | #elif defined(TARGET_SH4) |
| 3037 | if ((env->flags & ((DELAY_SLOT | DELAY_SLOT_CONDITIONAL))) != 0 |
| 3038 | && n > 1) { |
| 3039 | env->pc -= 2; |
| 3040 | env->icount_decr.u16.low++; |
| 3041 | env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL); |
| 3042 | } |
| 3043 | #endif |
| 3044 | /* This should never happen. */ |
| 3045 | if (n > CF_COUNT_MASK) |
| 3046 | cpu_abort(env, "TB too big during recompile"); |
| 3047 | |
| 3048 | cflags = n | CF_LAST_IO; |
| 3049 | pc = tb->pc; |
| 3050 | cs_base = tb->cs_base; |
| 3051 | flags = tb->flags; |
| 3052 | tb_phys_invalidate(tb, -1); |
| 3053 | /* FIXME: In theory this could raise an exception. In practice |
| 3054 | we have already translated the block once so it's probably ok. */ |
| 3055 | tb_gen_code(env, pc, cs_base, flags, cflags); |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 3056 | /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 3057 | the first in the TB) then we end up generating a whole new TB and |
| 3058 | repeating the fault, which is horribly inefficient. |
| 3059 | Better would be to execute just this insn uncached, or generate a |
| 3060 | second new TB. */ |
| 3061 | cpu_resume_from_signal(env, NULL); |
| 3062 | } |
| 3063 | |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 3064 | void dump_exec_info(FILE *f, |
| 3065 | int (*cpu_fprintf)(FILE *f, const char *fmt, ...)) |
| 3066 | { |
| 3067 | int i, target_code_size, max_target_code_size; |
| 3068 | int direct_jmp_count, direct_jmp2_count, cross_page; |
| 3069 | TranslationBlock *tb; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3070 | |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 3071 | target_code_size = 0; |
| 3072 | max_target_code_size = 0; |
| 3073 | cross_page = 0; |
| 3074 | direct_jmp_count = 0; |
| 3075 | direct_jmp2_count = 0; |
| 3076 | for(i = 0; i < nb_tbs; i++) { |
| 3077 | tb = &tbs[i]; |
| 3078 | target_code_size += tb->size; |
| 3079 | if (tb->size > max_target_code_size) |
| 3080 | max_target_code_size = tb->size; |
| 3081 | if (tb->page_addr[1] != -1) |
| 3082 | cross_page++; |
| 3083 | if (tb->tb_next_offset[0] != 0xffff) { |
| 3084 | direct_jmp_count++; |
| 3085 | if (tb->tb_next_offset[1] != 0xffff) { |
| 3086 | direct_jmp2_count++; |
| 3087 | } |
| 3088 | } |
| 3089 | } |
| 3090 | /* XXX: avoid using doubles ? */ |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 3091 | cpu_fprintf(f, "Translation buffer state:\n"); |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 3092 | cpu_fprintf(f, "gen code size %ld/%ld\n", |
| 3093 | code_gen_ptr - code_gen_buffer, code_gen_buffer_max_size); |
| 3094 | cpu_fprintf(f, "TB count %d/%d\n", |
| 3095 | nb_tbs, code_gen_max_blocks); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3096 | cpu_fprintf(f, "TB avg target size %d max=%d bytes\n", |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 3097 | nb_tbs ? target_code_size / nb_tbs : 0, |
| 3098 | max_target_code_size); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3099 | cpu_fprintf(f, "TB avg host size %d bytes (expansion ratio: %0.1f)\n", |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 3100 | nb_tbs ? (code_gen_ptr - code_gen_buffer) / nb_tbs : 0, |
| 3101 | target_code_size ? (double) (code_gen_ptr - code_gen_buffer) / target_code_size : 0); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3102 | cpu_fprintf(f, "cross page TB count %d (%d%%)\n", |
| 3103 | cross_page, |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 3104 | nb_tbs ? (cross_page * 100) / nb_tbs : 0); |
| 3105 | cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n", |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3106 | direct_jmp_count, |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 3107 | nb_tbs ? (direct_jmp_count * 100) / nb_tbs : 0, |
| 3108 | direct_jmp2_count, |
| 3109 | nb_tbs ? (direct_jmp2_count * 100) / nb_tbs : 0); |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 3110 | cpu_fprintf(f, "\nStatistics:\n"); |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 3111 | cpu_fprintf(f, "TB flush count %d\n", tb_flush_count); |
| 3112 | cpu_fprintf(f, "TB invalidate count %d\n", tb_phys_invalidate_count); |
| 3113 | cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count); |
bellard | b67d9a5 | 2008-05-23 09:57:34 +0000 | [diff] [blame] | 3114 | tcg_dump_info(f, cpu_fprintf); |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 3115 | } |
| 3116 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3117 | #if !defined(CONFIG_USER_ONLY) |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 3118 | |
| 3119 | #define MMUSUFFIX _cmmu |
| 3120 | #define GETPC() NULL |
| 3121 | #define env cpu_single_env |
bellard | b769d8f | 2004-10-03 15:07:13 +0000 | [diff] [blame] | 3122 | #define SOFTMMU_CODE_ACCESS |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 3123 | |
| 3124 | #define SHIFT 0 |
| 3125 | #include "softmmu_template.h" |
| 3126 | |
| 3127 | #define SHIFT 1 |
| 3128 | #include "softmmu_template.h" |
| 3129 | |
| 3130 | #define SHIFT 2 |
| 3131 | #include "softmmu_template.h" |
| 3132 | |
| 3133 | #define SHIFT 3 |
| 3134 | #include "softmmu_template.h" |
| 3135 | |
| 3136 | #undef env |
| 3137 | |
| 3138 | #endif |