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