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