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 |
Blue Swirl | 8167ee8 | 2009-07-16 20:47:01 +0000 | [diff] [blame] | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 18 | */ |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 19 | #include "config.h" |
bellard | d5a8f07 | 2004-09-29 21:15:28 +0000 | [diff] [blame] | 20 | #ifdef _WIN32 |
| 21 | #include <windows.h> |
| 22 | #else |
bellard | a98d49b | 2004-11-14 16:22:05 +0000 | [diff] [blame] | 23 | #include <sys/types.h> |
bellard | d5a8f07 | 2004-09-29 21:15:28 +0000 | [diff] [blame] | 24 | #include <sys/mman.h> |
| 25 | #endif |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 26 | #include <stdlib.h> |
| 27 | #include <stdio.h> |
| 28 | #include <stdarg.h> |
| 29 | #include <string.h> |
| 30 | #include <errno.h> |
| 31 | #include <unistd.h> |
| 32 | #include <inttypes.h> |
| 33 | |
bellard | 6180a18 | 2003-09-30 21:04:53 +0000 | [diff] [blame] | 34 | #include "cpu.h" |
| 35 | #include "exec-all.h" |
aurel32 | ca10f86 | 2008-04-11 21:35:42 +0000 | [diff] [blame] | 36 | #include "qemu-common.h" |
bellard | b67d9a5 | 2008-05-23 09:57:34 +0000 | [diff] [blame] | 37 | #include "tcg.h" |
pbrook | b3c7724 | 2008-06-30 16:31:04 +0000 | [diff] [blame] | 38 | #include "hw/hw.h" |
aliguori | 7457619 | 2008-10-06 14:02:03 +0000 | [diff] [blame] | 39 | #include "osdep.h" |
aliguori | 7ba1e61 | 2008-11-05 16:04:33 +0000 | [diff] [blame] | 40 | #include "kvm.h" |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 41 | #if defined(CONFIG_USER_ONLY) |
| 42 | #include <qemu.h> |
Riku Voipio | fd052bf | 2010-01-25 14:30:49 +0200 | [diff] [blame] | 43 | #include <signal.h> |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 44 | #endif |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 45 | |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 46 | //#define DEBUG_TB_INVALIDATE |
bellard | 66e85a2 | 2003-06-24 13:28:12 +0000 | [diff] [blame] | 47 | //#define DEBUG_FLUSH |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 48 | //#define DEBUG_TLB |
pbrook | 67d3b95 | 2006-12-18 05:03:52 +0000 | [diff] [blame] | 49 | //#define DEBUG_UNASSIGNED |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 50 | |
| 51 | /* make various TB consistency checks */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 52 | //#define DEBUG_TB_CHECK |
| 53 | //#define DEBUG_TLB_CHECK |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 54 | |
ths | 1196be3 | 2007-03-17 15:17:58 +0000 | [diff] [blame] | 55 | //#define DEBUG_IOPORT |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 56 | //#define DEBUG_SUBPAGE |
ths | 1196be3 | 2007-03-17 15:17:58 +0000 | [diff] [blame] | 57 | |
pbrook | 99773bd | 2006-04-16 15:14:59 +0000 | [diff] [blame] | 58 | #if !defined(CONFIG_USER_ONLY) |
| 59 | /* TB consistency checks only implemented for usermode emulation. */ |
| 60 | #undef DEBUG_TB_CHECK |
| 61 | #endif |
| 62 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 63 | #define SMC_BITMAP_USE_THRESHOLD 10 |
| 64 | |
blueswir1 | bdaf78e | 2008-10-04 07:24:27 +0000 | [diff] [blame] | 65 | static TranslationBlock *tbs; |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 66 | int code_gen_max_blocks; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 67 | TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE]; |
blueswir1 | bdaf78e | 2008-10-04 07:24:27 +0000 | [diff] [blame] | 68 | static int nb_tbs; |
bellard | eb51d10 | 2003-05-14 21:51:13 +0000 | [diff] [blame] | 69 | /* any access to the tbs or the page table must use this lock */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 70 | spinlock_t tb_lock = SPIN_LOCK_UNLOCKED; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 71 | |
blueswir1 | 141ac46 | 2008-07-26 15:05:57 +0000 | [diff] [blame] | 72 | #if defined(__arm__) || defined(__sparc_v9__) |
| 73 | /* The prologue must be reachable with a direct jump. ARM and Sparc64 |
| 74 | have limited branch ranges (possibly also PPC) so place it in a |
blueswir1 | d03d860 | 2008-07-10 17:21:31 +0000 | [diff] [blame] | 75 | section close to code segment. */ |
| 76 | #define code_gen_section \ |
| 77 | __attribute__((__section__(".gen_code"))) \ |
| 78 | __attribute__((aligned (32))) |
Stefan Weil | f8e2af1 | 2009-06-18 23:04:48 +0200 | [diff] [blame] | 79 | #elif defined(_WIN32) |
| 80 | /* Maximum alignment for Win32 is 16. */ |
| 81 | #define code_gen_section \ |
| 82 | __attribute__((aligned (16))) |
blueswir1 | d03d860 | 2008-07-10 17:21:31 +0000 | [diff] [blame] | 83 | #else |
| 84 | #define code_gen_section \ |
| 85 | __attribute__((aligned (32))) |
| 86 | #endif |
| 87 | |
| 88 | uint8_t code_gen_prologue[1024] code_gen_section; |
blueswir1 | bdaf78e | 2008-10-04 07:24:27 +0000 | [diff] [blame] | 89 | static uint8_t *code_gen_buffer; |
| 90 | static unsigned long code_gen_buffer_size; |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 91 | /* threshold to flush the translated code buffer */ |
blueswir1 | bdaf78e | 2008-10-04 07:24:27 +0000 | [diff] [blame] | 92 | static unsigned long code_gen_buffer_max_size; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 93 | uint8_t *code_gen_ptr; |
| 94 | |
pbrook | e2eef17 | 2008-06-08 01:09:01 +0000 | [diff] [blame] | 95 | #if !defined(CONFIG_USER_ONLY) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 96 | int phys_ram_fd; |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 97 | uint8_t *phys_ram_dirty; |
aliguori | 7457619 | 2008-10-06 14:02:03 +0000 | [diff] [blame] | 98 | static int in_migration; |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 99 | |
| 100 | typedef struct RAMBlock { |
| 101 | uint8_t *host; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 102 | ram_addr_t offset; |
| 103 | ram_addr_t length; |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 104 | struct RAMBlock *next; |
| 105 | } RAMBlock; |
| 106 | |
| 107 | static RAMBlock *ram_blocks; |
| 108 | /* TODO: When we implement (and use) ram deallocation (e.g. for hotplug) |
Stuart Brady | ccbb4d4 | 2009-05-03 12:15:06 +0100 | [diff] [blame] | 109 | then we can no longer assume contiguous ram offsets, and external uses |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 110 | of this variable will break. */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 111 | ram_addr_t last_ram_offset; |
pbrook | e2eef17 | 2008-06-08 01:09:01 +0000 | [diff] [blame] | 112 | #endif |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 113 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 114 | CPUState *first_cpu; |
| 115 | /* current CPU in the current thread. It is only valid inside |
| 116 | cpu_exec() */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 117 | CPUState *cpu_single_env; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 118 | /* 0 = Do not count executed instructions. |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 119 | 1 = Precise instruction counting. |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 120 | 2 = Adaptive rate instruction counting. */ |
| 121 | int use_icount = 0; |
| 122 | /* Current instruction counter. While executing translated code this may |
| 123 | include some instructions that have not yet been executed. */ |
| 124 | int64_t qemu_icount; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 125 | |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 126 | typedef struct PageDesc { |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 127 | /* list of TBs intersecting this ram page */ |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 128 | TranslationBlock *first_tb; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 129 | /* in order to optimize self modifying code, we count the number |
| 130 | of lookups we do to a given page to use a bitmap */ |
| 131 | unsigned int code_write_count; |
| 132 | uint8_t *code_bitmap; |
| 133 | #if defined(CONFIG_USER_ONLY) |
| 134 | unsigned long flags; |
| 135 | #endif |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 136 | } PageDesc; |
| 137 | |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 138 | typedef struct PhysPageDesc { |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 139 | /* offset in host memory of the page + io_index in the low bits */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 140 | ram_addr_t phys_offset; |
| 141 | ram_addr_t region_offset; |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 142 | } PhysPageDesc; |
| 143 | |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 144 | #define L2_BITS 10 |
j_mayer | bedb69e | 2007-04-05 20:08:21 +0000 | [diff] [blame] | 145 | #if defined(CONFIG_USER_ONLY) && defined(TARGET_VIRT_ADDR_SPACE_BITS) |
| 146 | /* XXX: this is a temporary hack for alpha target. |
| 147 | * In the future, this is to be replaced by a multi-level table |
| 148 | * to actually be able to handle the complete 64 bits address space. |
| 149 | */ |
| 150 | #define L1_BITS (TARGET_VIRT_ADDR_SPACE_BITS - L2_BITS - TARGET_PAGE_BITS) |
| 151 | #else |
aurel32 | 0387544 | 2008-04-22 20:45:18 +0000 | [diff] [blame] | 152 | #define L1_BITS (32 - L2_BITS - TARGET_PAGE_BITS) |
j_mayer | bedb69e | 2007-04-05 20:08:21 +0000 | [diff] [blame] | 153 | #endif |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 154 | |
| 155 | #define L1_SIZE (1 << L1_BITS) |
| 156 | #define L2_SIZE (1 << L2_BITS) |
| 157 | |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 158 | unsigned long qemu_real_host_page_size; |
| 159 | unsigned long qemu_host_page_bits; |
| 160 | unsigned long qemu_host_page_size; |
| 161 | unsigned long qemu_host_page_mask; |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 162 | |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 163 | /* XXX: for system emulation, it could just be an array */ |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 164 | static PageDesc *l1_map[L1_SIZE]; |
| 165 | |
pbrook | e2eef17 | 2008-06-08 01:09:01 +0000 | [diff] [blame] | 166 | #if !defined(CONFIG_USER_ONLY) |
Paul Brook | 6d9a130 | 2010-02-28 23:55:53 +0000 | [diff] [blame] | 167 | static PhysPageDesc **l1_phys_map; |
| 168 | |
pbrook | e2eef17 | 2008-06-08 01:09:01 +0000 | [diff] [blame] | 169 | static void io_mem_init(void); |
| 170 | |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 171 | /* io memory support */ |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 172 | CPUWriteMemoryFunc *io_mem_write[IO_MEM_NB_ENTRIES][4]; |
| 173 | CPUReadMemoryFunc *io_mem_read[IO_MEM_NB_ENTRIES][4]; |
bellard | a4193c8 | 2004-06-03 14:01:43 +0000 | [diff] [blame] | 174 | void *io_mem_opaque[IO_MEM_NB_ENTRIES]; |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 175 | static char io_mem_used[IO_MEM_NB_ENTRIES]; |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 176 | static int io_mem_watch; |
| 177 | #endif |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 178 | |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 179 | /* log support */ |
Juha Riihimäki | 1e8b27c | 2009-12-03 15:56:02 +0200 | [diff] [blame] | 180 | #ifdef WIN32 |
| 181 | static const char *logfilename = "qemu.log"; |
| 182 | #else |
blueswir1 | d9b630f | 2008-10-05 09:57:08 +0000 | [diff] [blame] | 183 | static const char *logfilename = "/tmp/qemu.log"; |
Juha Riihimäki | 1e8b27c | 2009-12-03 15:56:02 +0200 | [diff] [blame] | 184 | #endif |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 185 | FILE *logfile; |
| 186 | int loglevel; |
pbrook | e735b91 | 2007-06-30 13:53:24 +0000 | [diff] [blame] | 187 | static int log_append = 0; |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 188 | |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 189 | /* statistics */ |
| 190 | static int tlb_flush_count; |
| 191 | static int tb_flush_count; |
| 192 | static int tb_phys_invalidate_count; |
| 193 | |
bellard | 7cb69ca | 2008-05-10 10:55:51 +0000 | [diff] [blame] | 194 | #ifdef _WIN32 |
| 195 | static void map_exec(void *addr, long size) |
| 196 | { |
| 197 | DWORD old_protect; |
| 198 | VirtualProtect(addr, size, |
| 199 | PAGE_EXECUTE_READWRITE, &old_protect); |
| 200 | |
| 201 | } |
| 202 | #else |
| 203 | static void map_exec(void *addr, long size) |
| 204 | { |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 205 | unsigned long start, end, page_size; |
bellard | 7cb69ca | 2008-05-10 10:55:51 +0000 | [diff] [blame] | 206 | |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 207 | page_size = getpagesize(); |
bellard | 7cb69ca | 2008-05-10 10:55:51 +0000 | [diff] [blame] | 208 | start = (unsigned long)addr; |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 209 | start &= ~(page_size - 1); |
bellard | 7cb69ca | 2008-05-10 10:55:51 +0000 | [diff] [blame] | 210 | |
| 211 | end = (unsigned long)addr + size; |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 212 | end += page_size - 1; |
| 213 | end &= ~(page_size - 1); |
bellard | 7cb69ca | 2008-05-10 10:55:51 +0000 | [diff] [blame] | 214 | |
| 215 | mprotect((void *)start, end - start, |
| 216 | PROT_READ | PROT_WRITE | PROT_EXEC); |
| 217 | } |
| 218 | #endif |
| 219 | |
bellard | b346ff4 | 2003-06-15 20:05:50 +0000 | [diff] [blame] | 220 | static void page_init(void) |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 221 | { |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 222 | /* NOTE: we can always suppose that qemu_host_page_size >= |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 223 | TARGET_PAGE_SIZE */ |
aliguori | c2b48b6 | 2008-11-11 22:06:42 +0000 | [diff] [blame] | 224 | #ifdef _WIN32 |
| 225 | { |
| 226 | SYSTEM_INFO system_info; |
| 227 | |
| 228 | GetSystemInfo(&system_info); |
| 229 | qemu_real_host_page_size = system_info.dwPageSize; |
| 230 | } |
| 231 | #else |
| 232 | qemu_real_host_page_size = getpagesize(); |
| 233 | #endif |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 234 | if (qemu_host_page_size == 0) |
| 235 | qemu_host_page_size = qemu_real_host_page_size; |
| 236 | if (qemu_host_page_size < TARGET_PAGE_SIZE) |
| 237 | qemu_host_page_size = TARGET_PAGE_SIZE; |
| 238 | qemu_host_page_bits = 0; |
| 239 | while ((1 << qemu_host_page_bits) < qemu_host_page_size) |
| 240 | qemu_host_page_bits++; |
| 241 | qemu_host_page_mask = ~(qemu_host_page_size - 1); |
Paul Brook | 6d9a130 | 2010-02-28 23:55:53 +0000 | [diff] [blame] | 242 | #if !defined(CONFIG_USER_ONLY) |
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 *)); |
Paul Brook | 6d9a130 | 2010-02-28 23:55:53 +0000 | [diff] [blame] | 245 | #endif |
balrog | 50a9569 | 2007-12-12 01:16:23 +0000 | [diff] [blame] | 246 | |
| 247 | #if !defined(_WIN32) && defined(CONFIG_USER_ONLY) |
| 248 | { |
| 249 | long long startaddr, endaddr; |
| 250 | FILE *f; |
| 251 | int n; |
| 252 | |
pbrook | c8a706f | 2008-06-02 16:16:42 +0000 | [diff] [blame] | 253 | mmap_lock(); |
pbrook | 0776590 | 2008-05-31 16:33:53 +0000 | [diff] [blame] | 254 | last_brk = (unsigned long)sbrk(0); |
balrog | 50a9569 | 2007-12-12 01:16:23 +0000 | [diff] [blame] | 255 | f = fopen("/proc/self/maps", "r"); |
| 256 | if (f) { |
| 257 | do { |
| 258 | n = fscanf (f, "%llx-%llx %*[^\n]\n", &startaddr, &endaddr); |
| 259 | if (n == 2) { |
blueswir1 | e0b8d65 | 2008-05-03 17:51:24 +0000 | [diff] [blame] | 260 | startaddr = MIN(startaddr, |
| 261 | (1ULL << TARGET_PHYS_ADDR_SPACE_BITS) - 1); |
| 262 | endaddr = MIN(endaddr, |
| 263 | (1ULL << TARGET_PHYS_ADDR_SPACE_BITS) - 1); |
pbrook | b5fc909 | 2008-05-29 13:56:10 +0000 | [diff] [blame] | 264 | page_set_flags(startaddr & TARGET_PAGE_MASK, |
balrog | 50a9569 | 2007-12-12 01:16:23 +0000 | [diff] [blame] | 265 | TARGET_PAGE_ALIGN(endaddr), |
| 266 | PAGE_RESERVED); |
| 267 | } |
| 268 | } while (!feof(f)); |
| 269 | fclose(f); |
| 270 | } |
pbrook | c8a706f | 2008-06-02 16:16:42 +0000 | [diff] [blame] | 271 | mmap_unlock(); |
balrog | 50a9569 | 2007-12-12 01:16:23 +0000 | [diff] [blame] | 272 | } |
| 273 | #endif |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 274 | } |
| 275 | |
aliguori | 434929b | 2008-09-15 15:56:30 +0000 | [diff] [blame] | 276 | static inline PageDesc **page_l1_map(target_ulong index) |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 277 | { |
pbrook | 17e2377 | 2008-06-09 13:47:45 +0000 | [diff] [blame] | 278 | #if TARGET_LONG_BITS > 32 |
| 279 | /* Host memory outside guest VM. For 32-bit targets we have already |
| 280 | excluded high addresses. */ |
ths | d8173e0 | 2008-08-29 13:10:00 +0000 | [diff] [blame] | 281 | if (index > ((target_ulong)L2_SIZE * L1_SIZE)) |
pbrook | 17e2377 | 2008-06-09 13:47:45 +0000 | [diff] [blame] | 282 | return NULL; |
| 283 | #endif |
aliguori | 434929b | 2008-09-15 15:56:30 +0000 | [diff] [blame] | 284 | return &l1_map[index >> L2_BITS]; |
| 285 | } |
| 286 | |
| 287 | static inline PageDesc *page_find_alloc(target_ulong index) |
| 288 | { |
| 289 | PageDesc **lp, *p; |
| 290 | lp = page_l1_map(index); |
| 291 | if (!lp) |
| 292 | return NULL; |
| 293 | |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 294 | p = *lp; |
| 295 | if (!p) { |
| 296 | /* allocate if not found */ |
pbrook | 17e2377 | 2008-06-09 13:47:45 +0000 | [diff] [blame] | 297 | #if defined(CONFIG_USER_ONLY) |
pbrook | 17e2377 | 2008-06-09 13:47:45 +0000 | [diff] [blame] | 298 | size_t len = sizeof(PageDesc) * L2_SIZE; |
| 299 | /* Don't use qemu_malloc because it may recurse. */ |
Blue Swirl | 660f11b | 2009-07-31 21:16:51 +0000 | [diff] [blame] | 300 | p = mmap(NULL, len, PROT_READ | PROT_WRITE, |
pbrook | 17e2377 | 2008-06-09 13:47:45 +0000 | [diff] [blame] | 301 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 302 | *lp = p; |
aurel32 | fb1c2cd | 2008-12-08 18:12:26 +0000 | [diff] [blame] | 303 | if (h2g_valid(p)) { |
| 304 | unsigned long addr = h2g(p); |
pbrook | 17e2377 | 2008-06-09 13:47:45 +0000 | [diff] [blame] | 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; |
Blue Swirl | 660f11b | 2009-07-31 21:16:51 +0000 | [diff] [blame] | 325 | if (!p) { |
| 326 | return NULL; |
| 327 | } |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 328 | return p + (index & (L2_SIZE - 1)); |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Paul Brook | 6d9a130 | 2010-02-28 23:55:53 +0000 | [diff] [blame] | 331 | #if !defined(CONFIG_USER_ONLY) |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 332 | static PhysPageDesc *phys_page_find_alloc(target_phys_addr_t index, int alloc) |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 333 | { |
bellard | 108c49b | 2005-07-24 12:55:09 +0000 | [diff] [blame] | 334 | void **lp, **p; |
pbrook | e3f4e2a | 2006-04-08 20:02:06 +0000 | [diff] [blame] | 335 | PhysPageDesc *pd; |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 336 | |
bellard | 108c49b | 2005-07-24 12:55:09 +0000 | [diff] [blame] | 337 | p = (void **)l1_phys_map; |
| 338 | #if TARGET_PHYS_ADDR_SPACE_BITS > 32 |
| 339 | |
| 340 | #if TARGET_PHYS_ADDR_SPACE_BITS > (32 + L1_BITS) |
| 341 | #error unsupported TARGET_PHYS_ADDR_SPACE_BITS |
| 342 | #endif |
| 343 | lp = p + ((index >> (L1_BITS + L2_BITS)) & (L1_SIZE - 1)); |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 344 | p = *lp; |
| 345 | if (!p) { |
| 346 | /* allocate if not found */ |
bellard | 108c49b | 2005-07-24 12:55:09 +0000 | [diff] [blame] | 347 | if (!alloc) |
| 348 | return NULL; |
| 349 | p = qemu_vmalloc(sizeof(void *) * L1_SIZE); |
| 350 | memset(p, 0, sizeof(void *) * L1_SIZE); |
| 351 | *lp = p; |
| 352 | } |
| 353 | #endif |
| 354 | lp = p + ((index >> L2_BITS) & (L1_SIZE - 1)); |
pbrook | e3f4e2a | 2006-04-08 20:02:06 +0000 | [diff] [blame] | 355 | pd = *lp; |
| 356 | if (!pd) { |
| 357 | int i; |
bellard | 108c49b | 2005-07-24 12:55:09 +0000 | [diff] [blame] | 358 | /* allocate if not found */ |
| 359 | if (!alloc) |
| 360 | return NULL; |
pbrook | e3f4e2a | 2006-04-08 20:02:06 +0000 | [diff] [blame] | 361 | pd = qemu_vmalloc(sizeof(PhysPageDesc) * L2_SIZE); |
| 362 | *lp = pd; |
pbrook | 67c4d23 | 2009-02-23 13:16:07 +0000 | [diff] [blame] | 363 | for (i = 0; i < L2_SIZE; i++) { |
pbrook | e3f4e2a | 2006-04-08 20:02:06 +0000 | [diff] [blame] | 364 | pd[i].phys_offset = IO_MEM_UNASSIGNED; |
pbrook | 67c4d23 | 2009-02-23 13:16:07 +0000 | [diff] [blame] | 365 | pd[i].region_offset = (index + i) << TARGET_PAGE_BITS; |
| 366 | } |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 367 | } |
pbrook | e3f4e2a | 2006-04-08 20:02:06 +0000 | [diff] [blame] | 368 | return ((PhysPageDesc *)pd) + (index & (L2_SIZE - 1)); |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 371 | static inline PhysPageDesc *phys_page_find(target_phys_addr_t index) |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 372 | { |
bellard | 108c49b | 2005-07-24 12:55:09 +0000 | [diff] [blame] | 373 | return phys_page_find_alloc(index, 0); |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 376 | static void tlb_protect_code(ram_addr_t ram_addr); |
| 377 | static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr, |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 378 | target_ulong vaddr); |
pbrook | c8a706f | 2008-06-02 16:16:42 +0000 | [diff] [blame] | 379 | #define mmap_lock() do { } while(0) |
| 380 | #define mmap_unlock() do { } while(0) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 381 | #endif |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 382 | |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 383 | #define DEFAULT_CODE_GEN_BUFFER_SIZE (32 * 1024 * 1024) |
| 384 | |
| 385 | #if defined(CONFIG_USER_ONLY) |
Stuart Brady | ccbb4d4 | 2009-05-03 12:15:06 +0100 | [diff] [blame] | 386 | /* Currently it is not recommended to allocate big chunks of data in |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 387 | user mode. It will change when a dedicated libc will be used */ |
| 388 | #define USE_STATIC_CODE_GEN_BUFFER |
| 389 | #endif |
| 390 | |
| 391 | #ifdef USE_STATIC_CODE_GEN_BUFFER |
| 392 | static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE]; |
| 393 | #endif |
| 394 | |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 395 | static void code_gen_alloc(unsigned long tb_size) |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 396 | { |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 397 | #ifdef USE_STATIC_CODE_GEN_BUFFER |
| 398 | code_gen_buffer = static_code_gen_buffer; |
| 399 | code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE; |
| 400 | map_exec(code_gen_buffer, code_gen_buffer_size); |
| 401 | #else |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 402 | code_gen_buffer_size = tb_size; |
| 403 | if (code_gen_buffer_size == 0) { |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 404 | #if defined(CONFIG_USER_ONLY) |
| 405 | /* in user mode, phys_ram_size is not meaningful */ |
| 406 | code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE; |
| 407 | #else |
Stuart Brady | ccbb4d4 | 2009-05-03 12:15:06 +0100 | [diff] [blame] | 408 | /* XXX: needs adjustments */ |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 409 | code_gen_buffer_size = (unsigned long)(ram_size / 4); |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 410 | #endif |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 411 | } |
| 412 | if (code_gen_buffer_size < MIN_CODE_GEN_BUFFER_SIZE) |
| 413 | code_gen_buffer_size = MIN_CODE_GEN_BUFFER_SIZE; |
| 414 | /* The code gen buffer location may have constraints depending on |
| 415 | the host cpu and OS */ |
| 416 | #if defined(__linux__) |
| 417 | { |
| 418 | int flags; |
blueswir1 | 141ac46 | 2008-07-26 15:05:57 +0000 | [diff] [blame] | 419 | void *start = NULL; |
| 420 | |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 421 | flags = MAP_PRIVATE | MAP_ANONYMOUS; |
| 422 | #if defined(__x86_64__) |
| 423 | flags |= MAP_32BIT; |
| 424 | /* Cannot map more than that */ |
| 425 | if (code_gen_buffer_size > (800 * 1024 * 1024)) |
| 426 | code_gen_buffer_size = (800 * 1024 * 1024); |
blueswir1 | 141ac46 | 2008-07-26 15:05:57 +0000 | [diff] [blame] | 427 | #elif defined(__sparc_v9__) |
| 428 | // Map the buffer below 2G, so we can use direct calls and branches |
| 429 | flags |= MAP_FIXED; |
| 430 | start = (void *) 0x60000000UL; |
| 431 | if (code_gen_buffer_size > (512 * 1024 * 1024)) |
| 432 | code_gen_buffer_size = (512 * 1024 * 1024); |
balrog | 1cb0661 | 2008-12-01 02:10:17 +0000 | [diff] [blame] | 433 | #elif defined(__arm__) |
balrog | 63d4124 | 2008-12-01 02:19:41 +0000 | [diff] [blame] | 434 | /* Map the buffer below 32M, so we can use direct calls and branches */ |
balrog | 1cb0661 | 2008-12-01 02:10:17 +0000 | [diff] [blame] | 435 | flags |= MAP_FIXED; |
| 436 | start = (void *) 0x01000000UL; |
| 437 | if (code_gen_buffer_size > 16 * 1024 * 1024) |
| 438 | code_gen_buffer_size = 16 * 1024 * 1024; |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 439 | #endif |
blueswir1 | 141ac46 | 2008-07-26 15:05:57 +0000 | [diff] [blame] | 440 | code_gen_buffer = mmap(start, code_gen_buffer_size, |
| 441 | PROT_WRITE | PROT_READ | PROT_EXEC, |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 442 | flags, -1, 0); |
| 443 | if (code_gen_buffer == MAP_FAILED) { |
| 444 | fprintf(stderr, "Could not allocate dynamic translator buffer\n"); |
| 445 | exit(1); |
| 446 | } |
| 447 | } |
Aurelien Jarno | a167ba5 | 2009-11-29 18:00:41 +0100 | [diff] [blame] | 448 | #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) |
aliguori | 06e67a8 | 2008-09-27 15:32:41 +0000 | [diff] [blame] | 449 | { |
| 450 | int flags; |
| 451 | void *addr = NULL; |
| 452 | flags = MAP_PRIVATE | MAP_ANONYMOUS; |
| 453 | #if defined(__x86_64__) |
| 454 | /* FreeBSD doesn't have MAP_32BIT, use MAP_FIXED and assume |
| 455 | * 0x40000000 is free */ |
| 456 | flags |= MAP_FIXED; |
| 457 | addr = (void *)0x40000000; |
| 458 | /* Cannot map more than that */ |
| 459 | if (code_gen_buffer_size > (800 * 1024 * 1024)) |
| 460 | code_gen_buffer_size = (800 * 1024 * 1024); |
| 461 | #endif |
| 462 | code_gen_buffer = mmap(addr, code_gen_buffer_size, |
| 463 | PROT_WRITE | PROT_READ | PROT_EXEC, |
| 464 | flags, -1, 0); |
| 465 | if (code_gen_buffer == MAP_FAILED) { |
| 466 | fprintf(stderr, "Could not allocate dynamic translator buffer\n"); |
| 467 | exit(1); |
| 468 | } |
| 469 | } |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 470 | #else |
| 471 | code_gen_buffer = qemu_malloc(code_gen_buffer_size); |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 472 | map_exec(code_gen_buffer, code_gen_buffer_size); |
| 473 | #endif |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 474 | #endif /* !USE_STATIC_CODE_GEN_BUFFER */ |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 475 | map_exec(code_gen_prologue, sizeof(code_gen_prologue)); |
| 476 | code_gen_buffer_max_size = code_gen_buffer_size - |
| 477 | code_gen_max_block_size(); |
| 478 | code_gen_max_blocks = code_gen_buffer_size / CODE_GEN_AVG_BLOCK_SIZE; |
| 479 | tbs = qemu_malloc(code_gen_max_blocks * sizeof(TranslationBlock)); |
| 480 | } |
| 481 | |
| 482 | /* Must be called before using the QEMU cpus. 'tb_size' is the size |
| 483 | (in bytes) allocated to the translation buffer. Zero means default |
| 484 | size. */ |
| 485 | void cpu_exec_init_all(unsigned long tb_size) |
| 486 | { |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 487 | cpu_gen_init(); |
| 488 | code_gen_alloc(tb_size); |
| 489 | code_gen_ptr = code_gen_buffer; |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 490 | page_init(); |
pbrook | e2eef17 | 2008-06-08 01:09:01 +0000 | [diff] [blame] | 491 | #if !defined(CONFIG_USER_ONLY) |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 492 | io_mem_init(); |
pbrook | e2eef17 | 2008-06-08 01:09:01 +0000 | [diff] [blame] | 493 | #endif |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 494 | } |
| 495 | |
pbrook | 9656f32 | 2008-07-01 20:01:19 +0000 | [diff] [blame] | 496 | #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY) |
| 497 | |
Juan Quintela | e59fb37 | 2009-09-29 22:48:21 +0200 | [diff] [blame] | 498 | static int cpu_common_post_load(void *opaque, int version_id) |
Juan Quintela | e7f4eff | 2009-09-10 03:04:33 +0200 | [diff] [blame] | 499 | { |
| 500 | CPUState *env = opaque; |
| 501 | |
aurel32 | 3098dba | 2009-03-07 21:28:24 +0000 | [diff] [blame] | 502 | /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the |
| 503 | version_id is increased. */ |
| 504 | env->interrupt_request &= ~0x01; |
pbrook | 9656f32 | 2008-07-01 20:01:19 +0000 | [diff] [blame] | 505 | tlb_flush(env, 1); |
| 506 | |
| 507 | return 0; |
| 508 | } |
Juan Quintela | e7f4eff | 2009-09-10 03:04:33 +0200 | [diff] [blame] | 509 | |
| 510 | static const VMStateDescription vmstate_cpu_common = { |
| 511 | .name = "cpu_common", |
| 512 | .version_id = 1, |
| 513 | .minimum_version_id = 1, |
| 514 | .minimum_version_id_old = 1, |
Juan Quintela | e7f4eff | 2009-09-10 03:04:33 +0200 | [diff] [blame] | 515 | .post_load = cpu_common_post_load, |
| 516 | .fields = (VMStateField []) { |
| 517 | VMSTATE_UINT32(halted, CPUState), |
| 518 | VMSTATE_UINT32(interrupt_request, CPUState), |
| 519 | VMSTATE_END_OF_LIST() |
| 520 | } |
| 521 | }; |
pbrook | 9656f32 | 2008-07-01 20:01:19 +0000 | [diff] [blame] | 522 | #endif |
| 523 | |
Glauber Costa | 950f147 | 2009-06-09 12:15:18 -0400 | [diff] [blame] | 524 | CPUState *qemu_get_cpu(int cpu) |
| 525 | { |
| 526 | CPUState *env = first_cpu; |
| 527 | |
| 528 | while (env) { |
| 529 | if (env->cpu_index == cpu) |
| 530 | break; |
| 531 | env = env->next_cpu; |
| 532 | } |
| 533 | |
| 534 | return env; |
| 535 | } |
| 536 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 537 | void cpu_exec_init(CPUState *env) |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 538 | { |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 539 | CPUState **penv; |
| 540 | int cpu_index; |
| 541 | |
pbrook | c276471 | 2009-03-07 15:24:59 +0000 | [diff] [blame] | 542 | #if defined(CONFIG_USER_ONLY) |
| 543 | cpu_list_lock(); |
| 544 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 545 | env->next_cpu = NULL; |
| 546 | penv = &first_cpu; |
| 547 | cpu_index = 0; |
| 548 | while (*penv != NULL) { |
Nathan Froyd | 1e9fa73 | 2009-06-03 11:33:08 -0700 | [diff] [blame] | 549 | penv = &(*penv)->next_cpu; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 550 | cpu_index++; |
| 551 | } |
| 552 | env->cpu_index = cpu_index; |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 553 | env->numa_node = 0; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 554 | QTAILQ_INIT(&env->breakpoints); |
| 555 | QTAILQ_INIT(&env->watchpoints); |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 556 | *penv = env; |
pbrook | c276471 | 2009-03-07 15:24:59 +0000 | [diff] [blame] | 557 | #if defined(CONFIG_USER_ONLY) |
| 558 | cpu_list_unlock(); |
| 559 | #endif |
pbrook | b3c7724 | 2008-06-30 16:31:04 +0000 | [diff] [blame] | 560 | #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY) |
Juan Quintela | e7f4eff | 2009-09-10 03:04:33 +0200 | [diff] [blame] | 561 | vmstate_register(cpu_index, &vmstate_cpu_common, env); |
pbrook | b3c7724 | 2008-06-30 16:31:04 +0000 | [diff] [blame] | 562 | register_savevm("cpu", cpu_index, CPU_SAVE_VERSION, |
| 563 | cpu_save, cpu_load, env); |
| 564 | #endif |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 565 | } |
| 566 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 567 | static inline void invalidate_page_bitmap(PageDesc *p) |
| 568 | { |
| 569 | if (p->code_bitmap) { |
bellard | 59817cc | 2004-02-16 22:01:13 +0000 | [diff] [blame] | 570 | qemu_free(p->code_bitmap); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 571 | p->code_bitmap = NULL; |
| 572 | } |
| 573 | p->code_write_count = 0; |
| 574 | } |
| 575 | |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 576 | /* set to NULL all the 'first_tb' fields in all PageDescs */ |
| 577 | static void page_flush_tb(void) |
| 578 | { |
| 579 | int i, j; |
| 580 | PageDesc *p; |
| 581 | |
| 582 | for(i = 0; i < L1_SIZE; i++) { |
| 583 | p = l1_map[i]; |
| 584 | if (p) { |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 585 | for(j = 0; j < L2_SIZE; j++) { |
| 586 | p->first_tb = NULL; |
| 587 | invalidate_page_bitmap(p); |
| 588 | p++; |
| 589 | } |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | /* flush all the translation blocks */ |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 595 | /* XXX: tb_flush is currently not thread safe */ |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 596 | void tb_flush(CPUState *env1) |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 597 | { |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 598 | CPUState *env; |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 599 | #if defined(DEBUG_FLUSH) |
blueswir1 | ab3d172 | 2007-11-04 07:31:40 +0000 | [diff] [blame] | 600 | printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n", |
| 601 | (unsigned long)(code_gen_ptr - code_gen_buffer), |
| 602 | nb_tbs, nb_tbs > 0 ? |
| 603 | ((unsigned long)(code_gen_ptr - code_gen_buffer)) / nb_tbs : 0); |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 604 | #endif |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 605 | if ((unsigned long)(code_gen_ptr - code_gen_buffer) > code_gen_buffer_size) |
pbrook | a208e54 | 2008-03-31 17:07:36 +0000 | [diff] [blame] | 606 | cpu_abort(env1, "Internal error: code buffer overflow\n"); |
| 607 | |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 608 | nb_tbs = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 609 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 610 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
| 611 | memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *)); |
| 612 | } |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 613 | |
bellard | 8a8a608 | 2004-10-03 13:36:49 +0000 | [diff] [blame] | 614 | memset (tb_phys_hash, 0, CODE_GEN_PHYS_HASH_SIZE * sizeof (void *)); |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 615 | page_flush_tb(); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 616 | |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 617 | code_gen_ptr = code_gen_buffer; |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 618 | /* XXX: flush processor icache at this point if cache flush is |
| 619 | expensive */ |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 620 | tb_flush_count++; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | #ifdef DEBUG_TB_CHECK |
| 624 | |
j_mayer | bc98a7e | 2007-04-04 07:55:12 +0000 | [diff] [blame] | 625 | static void tb_invalidate_check(target_ulong address) |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 626 | { |
| 627 | TranslationBlock *tb; |
| 628 | int i; |
| 629 | address &= TARGET_PAGE_MASK; |
pbrook | 99773bd | 2006-04-16 15:14:59 +0000 | [diff] [blame] | 630 | for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) { |
| 631 | for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) { |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 632 | if (!(address + TARGET_PAGE_SIZE <= tb->pc || |
| 633 | address >= tb->pc + tb->size)) { |
Blue Swirl | 0bf9e31 | 2009-07-20 17:19:25 +0000 | [diff] [blame] | 634 | printf("ERROR invalidate: address=" TARGET_FMT_lx |
| 635 | " PC=%08lx size=%04x\n", |
pbrook | 99773bd | 2006-04-16 15:14:59 +0000 | [diff] [blame] | 636 | address, (long)tb->pc, tb->size); |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | /* verify that all the pages have correct rights for code */ |
| 643 | static void tb_page_check(void) |
| 644 | { |
| 645 | TranslationBlock *tb; |
| 646 | int i, flags1, flags2; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 647 | |
pbrook | 99773bd | 2006-04-16 15:14:59 +0000 | [diff] [blame] | 648 | for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) { |
| 649 | for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) { |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 650 | flags1 = page_get_flags(tb->pc); |
| 651 | flags2 = page_get_flags(tb->pc + tb->size - 1); |
| 652 | if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) { |
| 653 | printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n", |
pbrook | 99773bd | 2006-04-16 15:14:59 +0000 | [diff] [blame] | 654 | (long)tb->pc, tb->size, flags1, flags2); |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | #endif |
| 661 | |
| 662 | /* invalidate one TB */ |
| 663 | static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb, |
| 664 | int next_offset) |
| 665 | { |
| 666 | TranslationBlock *tb1; |
| 667 | for(;;) { |
| 668 | tb1 = *ptb; |
| 669 | if (tb1 == tb) { |
| 670 | *ptb = *(TranslationBlock **)((char *)tb1 + next_offset); |
| 671 | break; |
| 672 | } |
| 673 | ptb = (TranslationBlock **)((char *)tb1 + next_offset); |
| 674 | } |
| 675 | } |
| 676 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 677 | static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb) |
| 678 | { |
| 679 | TranslationBlock *tb1; |
| 680 | unsigned int n1; |
| 681 | |
| 682 | for(;;) { |
| 683 | tb1 = *ptb; |
| 684 | n1 = (long)tb1 & 3; |
| 685 | tb1 = (TranslationBlock *)((long)tb1 & ~3); |
| 686 | if (tb1 == tb) { |
| 687 | *ptb = tb1->page_next[n1]; |
| 688 | break; |
| 689 | } |
| 690 | ptb = &tb1->page_next[n1]; |
| 691 | } |
| 692 | } |
| 693 | |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 694 | static inline void tb_jmp_remove(TranslationBlock *tb, int n) |
| 695 | { |
| 696 | TranslationBlock *tb1, **ptb; |
| 697 | unsigned int n1; |
| 698 | |
| 699 | ptb = &tb->jmp_next[n]; |
| 700 | tb1 = *ptb; |
| 701 | if (tb1) { |
| 702 | /* find tb(n) in circular list */ |
| 703 | for(;;) { |
| 704 | tb1 = *ptb; |
| 705 | n1 = (long)tb1 & 3; |
| 706 | tb1 = (TranslationBlock *)((long)tb1 & ~3); |
| 707 | if (n1 == n && tb1 == tb) |
| 708 | break; |
| 709 | if (n1 == 2) { |
| 710 | ptb = &tb1->jmp_first; |
| 711 | } else { |
| 712 | ptb = &tb1->jmp_next[n1]; |
| 713 | } |
| 714 | } |
| 715 | /* now we can suppress tb(n) from the list */ |
| 716 | *ptb = tb->jmp_next[n]; |
| 717 | |
| 718 | tb->jmp_next[n] = NULL; |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | /* reset the jump entry 'n' of a TB so that it is not chained to |
| 723 | another TB */ |
| 724 | static inline void tb_reset_jump(TranslationBlock *tb, int n) |
| 725 | { |
| 726 | tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n])); |
| 727 | } |
| 728 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 729 | void tb_phys_invalidate(TranslationBlock *tb, target_ulong page_addr) |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 730 | { |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 731 | CPUState *env; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 732 | PageDesc *p; |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 733 | unsigned int h, n1; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 734 | target_phys_addr_t phys_pc; |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 735 | TranslationBlock *tb1, *tb2; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 736 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 737 | /* remove the TB from the hash list */ |
| 738 | phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK); |
| 739 | h = tb_phys_hash_func(phys_pc); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 740 | tb_remove(&tb_phys_hash[h], tb, |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 741 | offsetof(TranslationBlock, phys_hash_next)); |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 742 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 743 | /* remove the TB from the page list */ |
| 744 | if (tb->page_addr[0] != page_addr) { |
| 745 | p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS); |
| 746 | tb_page_remove(&p->first_tb, tb); |
| 747 | invalidate_page_bitmap(p); |
| 748 | } |
| 749 | if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) { |
| 750 | p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS); |
| 751 | tb_page_remove(&p->first_tb, tb); |
| 752 | invalidate_page_bitmap(p); |
| 753 | } |
| 754 | |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 755 | tb_invalidated_flag = 1; |
| 756 | |
| 757 | /* remove the TB from the hash list */ |
| 758 | h = tb_jmp_cache_hash_func(tb->pc); |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 759 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
| 760 | if (env->tb_jmp_cache[h] == tb) |
| 761 | env->tb_jmp_cache[h] = NULL; |
| 762 | } |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 763 | |
| 764 | /* suppress this TB from the two jump lists */ |
| 765 | tb_jmp_remove(tb, 0); |
| 766 | tb_jmp_remove(tb, 1); |
| 767 | |
| 768 | /* suppress any remaining jumps to this TB */ |
| 769 | tb1 = tb->jmp_first; |
| 770 | for(;;) { |
| 771 | n1 = (long)tb1 & 3; |
| 772 | if (n1 == 2) |
| 773 | break; |
| 774 | tb1 = (TranslationBlock *)((long)tb1 & ~3); |
| 775 | tb2 = tb1->jmp_next[n1]; |
| 776 | tb_reset_jump(tb1, n1); |
| 777 | tb1->jmp_next[n1] = NULL; |
| 778 | tb1 = tb2; |
| 779 | } |
| 780 | tb->jmp_first = (TranslationBlock *)((long)tb | 2); /* fail safe */ |
| 781 | |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 782 | tb_phys_invalidate_count++; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | static inline void set_bits(uint8_t *tab, int start, int len) |
| 786 | { |
| 787 | int end, mask, end1; |
| 788 | |
| 789 | end = start + len; |
| 790 | tab += start >> 3; |
| 791 | mask = 0xff << (start & 7); |
| 792 | if ((start & ~7) == (end & ~7)) { |
| 793 | if (start < end) { |
| 794 | mask &= ~(0xff << (end & 7)); |
| 795 | *tab |= mask; |
| 796 | } |
| 797 | } else { |
| 798 | *tab++ |= mask; |
| 799 | start = (start + 8) & ~7; |
| 800 | end1 = end & ~7; |
| 801 | while (start < end1) { |
| 802 | *tab++ = 0xff; |
| 803 | start += 8; |
| 804 | } |
| 805 | if (start < end) { |
| 806 | mask = ~(0xff << (end & 7)); |
| 807 | *tab |= mask; |
| 808 | } |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | static void build_page_bitmap(PageDesc *p) |
| 813 | { |
| 814 | int n, tb_start, tb_end; |
| 815 | TranslationBlock *tb; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 816 | |
pbrook | b2a7081 | 2008-06-09 13:57:23 +0000 | [diff] [blame] | 817 | p->code_bitmap = qemu_mallocz(TARGET_PAGE_SIZE / 8); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 818 | |
| 819 | tb = p->first_tb; |
| 820 | while (tb != NULL) { |
| 821 | n = (long)tb & 3; |
| 822 | tb = (TranslationBlock *)((long)tb & ~3); |
| 823 | /* NOTE: this is subtle as a TB may span two physical pages */ |
| 824 | if (n == 0) { |
| 825 | /* NOTE: tb_end may be after the end of the page, but |
| 826 | it is not a problem */ |
| 827 | tb_start = tb->pc & ~TARGET_PAGE_MASK; |
| 828 | tb_end = tb_start + tb->size; |
| 829 | if (tb_end > TARGET_PAGE_SIZE) |
| 830 | tb_end = TARGET_PAGE_SIZE; |
| 831 | } else { |
| 832 | tb_start = 0; |
| 833 | tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK); |
| 834 | } |
| 835 | set_bits(p->code_bitmap, tb_start, tb_end - tb_start); |
| 836 | tb = tb->page_next[n]; |
| 837 | } |
| 838 | } |
| 839 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 840 | TranslationBlock *tb_gen_code(CPUState *env, |
| 841 | target_ulong pc, target_ulong cs_base, |
| 842 | int flags, int cflags) |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 843 | { |
| 844 | TranslationBlock *tb; |
| 845 | uint8_t *tc_ptr; |
| 846 | target_ulong phys_pc, phys_page2, virt_page2; |
| 847 | int code_gen_size; |
| 848 | |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 849 | phys_pc = get_phys_addr_code(env, pc); |
| 850 | tb = tb_alloc(pc); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 851 | if (!tb) { |
| 852 | /* flush must be done */ |
| 853 | tb_flush(env); |
| 854 | /* cannot fail at this point */ |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 855 | tb = tb_alloc(pc); |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 856 | /* Don't forget to invalidate previous TB info. */ |
| 857 | tb_invalidated_flag = 1; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 858 | } |
| 859 | tc_ptr = code_gen_ptr; |
| 860 | tb->tc_ptr = tc_ptr; |
| 861 | tb->cs_base = cs_base; |
| 862 | tb->flags = flags; |
| 863 | tb->cflags = cflags; |
blueswir1 | d07bde8 | 2007-12-11 19:35:45 +0000 | [diff] [blame] | 864 | cpu_gen_code(env, tb, &code_gen_size); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 865 | 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] | 866 | |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 867 | /* check next page if needed */ |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 868 | virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 869 | phys_page2 = -1; |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 870 | if ((pc & TARGET_PAGE_MASK) != virt_page2) { |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 871 | phys_page2 = get_phys_addr_code(env, virt_page2); |
| 872 | } |
| 873 | tb_link_phys(tb, phys_pc, phys_page2); |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 874 | return tb; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 875 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 876 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 877 | /* invalidate all TBs which intersect with the target physical page |
| 878 | starting in range [start;end[. NOTE: start and end must refer to |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 879 | the same physical page. 'is_cpu_write_access' should be true if called |
| 880 | from a real cpu write access: the virtual CPU will exit the current |
| 881 | TB if code is modified inside this TB. */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 882 | 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] | 883 | int is_cpu_write_access) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 884 | { |
aliguori | 6b91754 | 2008-11-18 19:46:41 +0000 | [diff] [blame] | 885 | TranslationBlock *tb, *tb_next, *saved_tb; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 886 | CPUState *env = cpu_single_env; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 887 | target_ulong tb_start, tb_end; |
aliguori | 6b91754 | 2008-11-18 19:46:41 +0000 | [diff] [blame] | 888 | PageDesc *p; |
| 889 | int n; |
| 890 | #ifdef TARGET_HAS_PRECISE_SMC |
| 891 | int current_tb_not_found = is_cpu_write_access; |
| 892 | TranslationBlock *current_tb = NULL; |
| 893 | int current_tb_modified = 0; |
| 894 | target_ulong current_pc = 0; |
| 895 | target_ulong current_cs_base = 0; |
| 896 | int current_flags = 0; |
| 897 | #endif /* TARGET_HAS_PRECISE_SMC */ |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 898 | |
| 899 | p = page_find(start >> TARGET_PAGE_BITS); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 900 | if (!p) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 901 | return; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 902 | if (!p->code_bitmap && |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 903 | ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD && |
| 904 | is_cpu_write_access) { |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 905 | /* build code bitmap */ |
| 906 | build_page_bitmap(p); |
| 907 | } |
| 908 | |
| 909 | /* we remove all the TBs in the range [start, end[ */ |
| 910 | /* XXX: see if in some cases it could be faster to invalidate all the code */ |
| 911 | tb = p->first_tb; |
| 912 | while (tb != NULL) { |
| 913 | n = (long)tb & 3; |
| 914 | tb = (TranslationBlock *)((long)tb & ~3); |
| 915 | tb_next = tb->page_next[n]; |
| 916 | /* NOTE: this is subtle as a TB may span two physical pages */ |
| 917 | if (n == 0) { |
| 918 | /* NOTE: tb_end may be after the end of the page, but |
| 919 | it is not a problem */ |
| 920 | tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK); |
| 921 | tb_end = tb_start + tb->size; |
| 922 | } else { |
| 923 | tb_start = tb->page_addr[1]; |
| 924 | tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK); |
| 925 | } |
| 926 | if (!(tb_end <= start || tb_start >= end)) { |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 927 | #ifdef TARGET_HAS_PRECISE_SMC |
| 928 | if (current_tb_not_found) { |
| 929 | current_tb_not_found = 0; |
| 930 | current_tb = NULL; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 931 | if (env->mem_io_pc) { |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 932 | /* now we have a real cpu fault */ |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 933 | current_tb = tb_find_pc(env->mem_io_pc); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | if (current_tb == tb && |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 937 | (current_tb->cflags & CF_COUNT_MASK) != 1) { |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 938 | /* If we are modifying the current TB, we must stop |
| 939 | its execution. We could be more precise by checking |
| 940 | that the modification is after the current PC, but it |
| 941 | would require a specialized function to partially |
| 942 | restore the CPU state */ |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 943 | |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 944 | current_tb_modified = 1; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 945 | cpu_restore_state(current_tb, env, |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 946 | env->mem_io_pc, NULL); |
aliguori | 6b91754 | 2008-11-18 19:46:41 +0000 | [diff] [blame] | 947 | cpu_get_tb_cpu_state(env, ¤t_pc, ¤t_cs_base, |
| 948 | ¤t_flags); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 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 */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [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) { |
aliguori | 93fcfe3 | 2009-01-15 22:34:14 +0000 | [diff] [blame] | 995 | qemu_log("modifying code at 0x%x size=%d EIP=%x PC=%08x\n", |
| 996 | cpu_single_env->mem_io_vaddr, len, |
| 997 | cpu_single_env->eip, |
| 998 | cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base); |
bellard | 59817cc | 2004-02-16 22:01:13 +0000 | [diff] [blame] | 999 | } |
| 1000 | #endif |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1001 | p = page_find(start >> TARGET_PAGE_BITS); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1002 | if (!p) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1003 | return; |
| 1004 | if (p->code_bitmap) { |
| 1005 | offset = start & ~TARGET_PAGE_MASK; |
| 1006 | b = p->code_bitmap[offset >> 3] >> (offset & 7); |
| 1007 | if (b & ((1 << len) - 1)) |
| 1008 | goto do_invalidate; |
| 1009 | } else { |
| 1010 | do_invalidate: |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1011 | tb_invalidate_phys_page_range(start, start + len, 1); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1012 | } |
| 1013 | } |
| 1014 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1015 | #if !defined(CONFIG_SOFTMMU) |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 1016 | static void tb_invalidate_phys_page(target_phys_addr_t addr, |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1017 | unsigned long pc, void *puc) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1018 | { |
aliguori | 6b91754 | 2008-11-18 19:46:41 +0000 | [diff] [blame] | 1019 | TranslationBlock *tb; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1020 | PageDesc *p; |
aliguori | 6b91754 | 2008-11-18 19:46:41 +0000 | [diff] [blame] | 1021 | int n; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1022 | #ifdef TARGET_HAS_PRECISE_SMC |
aliguori | 6b91754 | 2008-11-18 19:46:41 +0000 | [diff] [blame] | 1023 | TranslationBlock *current_tb = NULL; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1024 | CPUState *env = cpu_single_env; |
aliguori | 6b91754 | 2008-11-18 19:46:41 +0000 | [diff] [blame] | 1025 | int current_tb_modified = 0; |
| 1026 | target_ulong current_pc = 0; |
| 1027 | target_ulong current_cs_base = 0; |
| 1028 | int current_flags = 0; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1029 | #endif |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1030 | |
| 1031 | addr &= TARGET_PAGE_MASK; |
| 1032 | p = page_find(addr >> TARGET_PAGE_BITS); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1033 | if (!p) |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1034 | return; |
| 1035 | tb = p->first_tb; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1036 | #ifdef TARGET_HAS_PRECISE_SMC |
| 1037 | if (tb && pc != 0) { |
| 1038 | current_tb = tb_find_pc(pc); |
| 1039 | } |
| 1040 | #endif |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1041 | while (tb != NULL) { |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1042 | n = (long)tb & 3; |
| 1043 | tb = (TranslationBlock *)((long)tb & ~3); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1044 | #ifdef TARGET_HAS_PRECISE_SMC |
| 1045 | if (current_tb == tb && |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1046 | (current_tb->cflags & CF_COUNT_MASK) != 1) { |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1047 | /* If we are modifying the current TB, we must stop |
| 1048 | its execution. We could be more precise by checking |
| 1049 | that the modification is after the current PC, but it |
| 1050 | would require a specialized function to partially |
| 1051 | restore the CPU state */ |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1052 | |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1053 | current_tb_modified = 1; |
| 1054 | cpu_restore_state(current_tb, env, pc, puc); |
aliguori | 6b91754 | 2008-11-18 19:46:41 +0000 | [diff] [blame] | 1055 | cpu_get_tb_cpu_state(env, ¤t_pc, ¤t_cs_base, |
| 1056 | ¤t_flags); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1057 | } |
| 1058 | #endif /* TARGET_HAS_PRECISE_SMC */ |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1059 | tb_phys_invalidate(tb, addr); |
| 1060 | tb = tb->page_next[n]; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1061 | } |
| 1062 | p->first_tb = NULL; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1063 | #ifdef TARGET_HAS_PRECISE_SMC |
| 1064 | if (current_tb_modified) { |
| 1065 | /* we generate a block containing just the instruction |
| 1066 | modifying the memory. It will ensure that it cannot modify |
| 1067 | itself */ |
bellard | ea1c180 | 2004-06-14 18:56:36 +0000 | [diff] [blame] | 1068 | env->current_tb = NULL; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1069 | tb_gen_code(env, current_pc, current_cs_base, current_flags, 1); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1070 | cpu_resume_from_signal(env, puc); |
| 1071 | } |
| 1072 | #endif |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1073 | } |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1074 | #endif |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1075 | |
| 1076 | /* add the tb in the target page and protect it if necessary */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1077 | static inline void tb_alloc_page(TranslationBlock *tb, |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1078 | unsigned int n, target_ulong page_addr) |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1079 | { |
| 1080 | PageDesc *p; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1081 | TranslationBlock *last_first_tb; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1082 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1083 | tb->page_addr[n] = page_addr; |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 1084 | p = page_find_alloc(page_addr >> TARGET_PAGE_BITS); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1085 | tb->page_next[n] = p->first_tb; |
| 1086 | last_first_tb = p->first_tb; |
| 1087 | p->first_tb = (TranslationBlock *)((long)tb | n); |
| 1088 | invalidate_page_bitmap(p); |
| 1089 | |
bellard | 107db44 | 2004-06-22 18:48:46 +0000 | [diff] [blame] | 1090 | #if defined(TARGET_HAS_SMC) || 1 |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1091 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1092 | #if defined(CONFIG_USER_ONLY) |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1093 | if (p->flags & PAGE_WRITE) { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1094 | target_ulong addr; |
| 1095 | PageDesc *p2; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1096 | int prot; |
| 1097 | |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1098 | /* force the host page as non writable (writes will have a |
| 1099 | page fault + mprotect overhead) */ |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1100 | page_addr &= qemu_host_page_mask; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1101 | prot = 0; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1102 | for(addr = page_addr; addr < page_addr + qemu_host_page_size; |
| 1103 | addr += TARGET_PAGE_SIZE) { |
| 1104 | |
| 1105 | p2 = page_find (addr >> TARGET_PAGE_BITS); |
| 1106 | if (!p2) |
| 1107 | continue; |
| 1108 | prot |= p2->flags; |
| 1109 | p2->flags &= ~PAGE_WRITE; |
| 1110 | page_get_flags(addr); |
| 1111 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1112 | mprotect(g2h(page_addr), qemu_host_page_size, |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1113 | (prot & PAGE_BITS) & ~PAGE_WRITE); |
| 1114 | #ifdef DEBUG_TB_INVALIDATE |
blueswir1 | ab3d172 | 2007-11-04 07:31:40 +0000 | [diff] [blame] | 1115 | printf("protecting code page: 0x" TARGET_FMT_lx "\n", |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1116 | page_addr); |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1117 | #endif |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1118 | } |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1119 | #else |
| 1120 | /* if some code is already present, then the pages are already |
| 1121 | protected. So we handle the case where only the first TB is |
| 1122 | allocated in a physical page */ |
| 1123 | if (!last_first_tb) { |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1124 | tlb_protect_code(page_addr); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1125 | } |
| 1126 | #endif |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1127 | |
| 1128 | #endif /* TARGET_HAS_SMC */ |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | /* Allocate a new translation block. Flush the translation buffer if |
| 1132 | too many translation blocks or too much generated code. */ |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 1133 | TranslationBlock *tb_alloc(target_ulong pc) |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1134 | { |
| 1135 | TranslationBlock *tb; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1136 | |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 1137 | if (nb_tbs >= code_gen_max_blocks || |
| 1138 | (code_gen_ptr - code_gen_buffer) >= code_gen_buffer_max_size) |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 1139 | return NULL; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1140 | tb = &tbs[nb_tbs++]; |
| 1141 | tb->pc = pc; |
bellard | b448f2f | 2004-02-25 23:24:04 +0000 | [diff] [blame] | 1142 | tb->cflags = 0; |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 1143 | return tb; |
| 1144 | } |
| 1145 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1146 | void tb_free(TranslationBlock *tb) |
| 1147 | { |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 1148 | /* In practice this is mostly used for single use temporary TB |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1149 | Ignore the hard cases and just back up if this TB happens to |
| 1150 | be the last one generated. */ |
| 1151 | if (nb_tbs > 0 && tb == &tbs[nb_tbs - 1]) { |
| 1152 | code_gen_ptr = tb->tc_ptr; |
| 1153 | nb_tbs--; |
| 1154 | } |
| 1155 | } |
| 1156 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1157 | /* add a new TB and link it to the physical page tables. phys_page2 is |
| 1158 | (-1) to indicate that only one page contains the TB. */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1159 | void tb_link_phys(TranslationBlock *tb, |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1160 | target_ulong phys_pc, target_ulong phys_page2) |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 1161 | { |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1162 | unsigned int h; |
| 1163 | TranslationBlock **ptb; |
| 1164 | |
pbrook | c8a706f | 2008-06-02 16:16:42 +0000 | [diff] [blame] | 1165 | /* Grab the mmap lock to stop another thread invalidating this TB |
| 1166 | before we are done. */ |
| 1167 | mmap_lock(); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1168 | /* add in the physical hash table */ |
| 1169 | h = tb_phys_hash_func(phys_pc); |
| 1170 | ptb = &tb_phys_hash[h]; |
| 1171 | tb->phys_hash_next = *ptb; |
| 1172 | *ptb = tb; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1173 | |
| 1174 | /* add in the page list */ |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1175 | tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK); |
| 1176 | if (phys_page2 != -1) |
| 1177 | tb_alloc_page(tb, 1, phys_page2); |
| 1178 | else |
| 1179 | tb->page_addr[1] = -1; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1180 | |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 1181 | tb->jmp_first = (TranslationBlock *)((long)tb | 2); |
| 1182 | tb->jmp_next[0] = NULL; |
| 1183 | tb->jmp_next[1] = NULL; |
| 1184 | |
| 1185 | /* init original jump addresses */ |
| 1186 | if (tb->tb_next_offset[0] != 0xffff) |
| 1187 | tb_reset_jump(tb, 0); |
| 1188 | if (tb->tb_next_offset[1] != 0xffff) |
| 1189 | tb_reset_jump(tb, 1); |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 1190 | |
| 1191 | #ifdef DEBUG_TB_CHECK |
| 1192 | tb_page_check(); |
| 1193 | #endif |
pbrook | c8a706f | 2008-06-02 16:16:42 +0000 | [diff] [blame] | 1194 | mmap_unlock(); |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 1197 | /* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr < |
| 1198 | tb[1].tc_ptr. Return NULL if not found */ |
| 1199 | TranslationBlock *tb_find_pc(unsigned long tc_ptr) |
| 1200 | { |
| 1201 | int m_min, m_max, m; |
| 1202 | unsigned long v; |
| 1203 | TranslationBlock *tb; |
| 1204 | |
| 1205 | if (nb_tbs <= 0) |
| 1206 | return NULL; |
| 1207 | if (tc_ptr < (unsigned long)code_gen_buffer || |
| 1208 | tc_ptr >= (unsigned long)code_gen_ptr) |
| 1209 | return NULL; |
| 1210 | /* binary search (cf Knuth) */ |
| 1211 | m_min = 0; |
| 1212 | m_max = nb_tbs - 1; |
| 1213 | while (m_min <= m_max) { |
| 1214 | m = (m_min + m_max) >> 1; |
| 1215 | tb = &tbs[m]; |
| 1216 | v = (unsigned long)tb->tc_ptr; |
| 1217 | if (v == tc_ptr) |
| 1218 | return tb; |
| 1219 | else if (tc_ptr < v) { |
| 1220 | m_max = m - 1; |
| 1221 | } else { |
| 1222 | m_min = m + 1; |
| 1223 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1224 | } |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 1225 | return &tbs[m_max]; |
| 1226 | } |
bellard | 7501267 | 2003-06-21 13:11:07 +0000 | [diff] [blame] | 1227 | |
bellard | ea041c0 | 2003-06-25 16:16:50 +0000 | [diff] [blame] | 1228 | static void tb_reset_jump_recursive(TranslationBlock *tb); |
| 1229 | |
| 1230 | static inline void tb_reset_jump_recursive2(TranslationBlock *tb, int n) |
| 1231 | { |
| 1232 | TranslationBlock *tb1, *tb_next, **ptb; |
| 1233 | unsigned int n1; |
| 1234 | |
| 1235 | tb1 = tb->jmp_next[n]; |
| 1236 | if (tb1 != NULL) { |
| 1237 | /* find head of list */ |
| 1238 | for(;;) { |
| 1239 | n1 = (long)tb1 & 3; |
| 1240 | tb1 = (TranslationBlock *)((long)tb1 & ~3); |
| 1241 | if (n1 == 2) |
| 1242 | break; |
| 1243 | tb1 = tb1->jmp_next[n1]; |
| 1244 | } |
| 1245 | /* we are now sure now that tb jumps to tb1 */ |
| 1246 | tb_next = tb1; |
| 1247 | |
| 1248 | /* remove tb from the jmp_first list */ |
| 1249 | ptb = &tb_next->jmp_first; |
| 1250 | for(;;) { |
| 1251 | tb1 = *ptb; |
| 1252 | n1 = (long)tb1 & 3; |
| 1253 | tb1 = (TranslationBlock *)((long)tb1 & ~3); |
| 1254 | if (n1 == n && tb1 == tb) |
| 1255 | break; |
| 1256 | ptb = &tb1->jmp_next[n1]; |
| 1257 | } |
| 1258 | *ptb = tb->jmp_next[n]; |
| 1259 | tb->jmp_next[n] = NULL; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1260 | |
bellard | ea041c0 | 2003-06-25 16:16:50 +0000 | [diff] [blame] | 1261 | /* suppress the jump to next tb in generated code */ |
| 1262 | tb_reset_jump(tb, n); |
| 1263 | |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 1264 | /* suppress jumps in the tb on which we could have jumped */ |
bellard | ea041c0 | 2003-06-25 16:16:50 +0000 | [diff] [blame] | 1265 | tb_reset_jump_recursive(tb_next); |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | static void tb_reset_jump_recursive(TranslationBlock *tb) |
| 1270 | { |
| 1271 | tb_reset_jump_recursive2(tb, 0); |
| 1272 | tb_reset_jump_recursive2(tb, 1); |
| 1273 | } |
| 1274 | |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1275 | #if defined(TARGET_HAS_ICE) |
Paul Brook | 94df27f | 2010-02-28 23:47:45 +0000 | [diff] [blame] | 1276 | #if defined(CONFIG_USER_ONLY) |
| 1277 | static void breakpoint_invalidate(CPUState *env, target_ulong pc) |
| 1278 | { |
| 1279 | tb_invalidate_phys_page_range(pc, pc + 1, 0); |
| 1280 | } |
| 1281 | #else |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1282 | static void breakpoint_invalidate(CPUState *env, target_ulong pc) |
| 1283 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 1284 | target_phys_addr_t addr; |
j_mayer | 9b3c35e | 2007-04-07 11:21:28 +0000 | [diff] [blame] | 1285 | target_ulong pd; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 1286 | ram_addr_t ram_addr; |
pbrook | c2f07f8 | 2006-04-08 17:14:56 +0000 | [diff] [blame] | 1287 | PhysPageDesc *p; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1288 | |
pbrook | c2f07f8 | 2006-04-08 17:14:56 +0000 | [diff] [blame] | 1289 | addr = cpu_get_phys_page_debug(env, pc); |
| 1290 | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
| 1291 | if (!p) { |
| 1292 | pd = IO_MEM_UNASSIGNED; |
| 1293 | } else { |
| 1294 | pd = p->phys_offset; |
| 1295 | } |
| 1296 | ram_addr = (pd & TARGET_PAGE_MASK) | (pc & ~TARGET_PAGE_MASK); |
pbrook | 706cd4b | 2006-04-08 17:36:21 +0000 | [diff] [blame] | 1297 | tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1298 | } |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 1299 | #endif |
Paul Brook | 94df27f | 2010-02-28 23:47:45 +0000 | [diff] [blame] | 1300 | #endif /* TARGET_HAS_ICE */ |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1301 | |
Paul Brook | c527ee8 | 2010-03-01 03:31:14 +0000 | [diff] [blame] | 1302 | #if defined(CONFIG_USER_ONLY) |
| 1303 | void cpu_watchpoint_remove_all(CPUState *env, int mask) |
| 1304 | |
| 1305 | { |
| 1306 | } |
| 1307 | |
| 1308 | int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len, |
| 1309 | int flags, CPUWatchpoint **watchpoint) |
| 1310 | { |
| 1311 | return -ENOSYS; |
| 1312 | } |
| 1313 | #else |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 1314 | /* Add a watchpoint. */ |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1315 | int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len, |
| 1316 | int flags, CPUWatchpoint **watchpoint) |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 1317 | { |
aliguori | b405133 | 2008-11-18 20:14:20 +0000 | [diff] [blame] | 1318 | target_ulong len_mask = ~(len - 1); |
aliguori | c0ce998 | 2008-11-25 22:13:57 +0000 | [diff] [blame] | 1319 | CPUWatchpoint *wp; |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 1320 | |
aliguori | b405133 | 2008-11-18 20:14:20 +0000 | [diff] [blame] | 1321 | /* sanity checks: allow power-of-2 lengths, deny unaligned watchpoints */ |
| 1322 | if ((len != 1 && len != 2 && len != 4 && len != 8) || (addr & ~len_mask)) { |
| 1323 | fprintf(stderr, "qemu: tried to set invalid watchpoint at " |
| 1324 | TARGET_FMT_lx ", len=" TARGET_FMT_lu "\n", addr, len); |
| 1325 | return -EINVAL; |
| 1326 | } |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1327 | wp = qemu_malloc(sizeof(*wp)); |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 1328 | |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1329 | wp->vaddr = addr; |
aliguori | b405133 | 2008-11-18 20:14:20 +0000 | [diff] [blame] | 1330 | wp->len_mask = len_mask; |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1331 | wp->flags = flags; |
| 1332 | |
aliguori | 2dc9f41 | 2008-11-18 20:56:59 +0000 | [diff] [blame] | 1333 | /* keep all GDB-injected watchpoints in front */ |
aliguori | c0ce998 | 2008-11-25 22:13:57 +0000 | [diff] [blame] | 1334 | if (flags & BP_GDB) |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1335 | QTAILQ_INSERT_HEAD(&env->watchpoints, wp, entry); |
aliguori | c0ce998 | 2008-11-25 22:13:57 +0000 | [diff] [blame] | 1336 | else |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1337 | QTAILQ_INSERT_TAIL(&env->watchpoints, wp, entry); |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1338 | |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 1339 | tlb_flush_page(env, addr); |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1340 | |
| 1341 | if (watchpoint) |
| 1342 | *watchpoint = wp; |
| 1343 | return 0; |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1346 | /* Remove a specific watchpoint. */ |
| 1347 | int cpu_watchpoint_remove(CPUState *env, target_ulong addr, target_ulong len, |
| 1348 | int flags) |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 1349 | { |
aliguori | b405133 | 2008-11-18 20:14:20 +0000 | [diff] [blame] | 1350 | target_ulong len_mask = ~(len - 1); |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1351 | CPUWatchpoint *wp; |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 1352 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1353 | QTAILQ_FOREACH(wp, &env->watchpoints, entry) { |
aliguori | b405133 | 2008-11-18 20:14:20 +0000 | [diff] [blame] | 1354 | if (addr == wp->vaddr && len_mask == wp->len_mask |
aliguori | 6e140f2 | 2008-11-18 20:37:55 +0000 | [diff] [blame] | 1355 | && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) { |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1356 | cpu_watchpoint_remove_by_ref(env, wp); |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 1357 | return 0; |
| 1358 | } |
| 1359 | } |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1360 | return -ENOENT; |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1363 | /* Remove a specific watchpoint by reference. */ |
| 1364 | void cpu_watchpoint_remove_by_ref(CPUState *env, CPUWatchpoint *watchpoint) |
| 1365 | { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1366 | QTAILQ_REMOVE(&env->watchpoints, watchpoint, entry); |
edgar_igl | 7d03f82 | 2008-05-17 18:58:29 +0000 | [diff] [blame] | 1367 | |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1368 | tlb_flush_page(env, watchpoint->vaddr); |
| 1369 | |
| 1370 | qemu_free(watchpoint); |
edgar_igl | 7d03f82 | 2008-05-17 18:58:29 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1373 | /* Remove all matching watchpoints. */ |
| 1374 | void cpu_watchpoint_remove_all(CPUState *env, int mask) |
| 1375 | { |
aliguori | c0ce998 | 2008-11-25 22:13:57 +0000 | [diff] [blame] | 1376 | CPUWatchpoint *wp, *next; |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1377 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1378 | QTAILQ_FOREACH_SAFE(wp, &env->watchpoints, entry, next) { |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1379 | if (wp->flags & mask) |
| 1380 | cpu_watchpoint_remove_by_ref(env, wp); |
aliguori | c0ce998 | 2008-11-25 22:13:57 +0000 | [diff] [blame] | 1381 | } |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1382 | } |
Paul Brook | c527ee8 | 2010-03-01 03:31:14 +0000 | [diff] [blame] | 1383 | #endif |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1384 | |
| 1385 | /* Add a breakpoint. */ |
| 1386 | int cpu_breakpoint_insert(CPUState *env, target_ulong pc, int flags, |
| 1387 | CPUBreakpoint **breakpoint) |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 1388 | { |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1389 | #if defined(TARGET_HAS_ICE) |
aliguori | c0ce998 | 2008-11-25 22:13:57 +0000 | [diff] [blame] | 1390 | CPUBreakpoint *bp; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1391 | |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1392 | bp = qemu_malloc(sizeof(*bp)); |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1393 | |
| 1394 | bp->pc = pc; |
| 1395 | bp->flags = flags; |
| 1396 | |
aliguori | 2dc9f41 | 2008-11-18 20:56:59 +0000 | [diff] [blame] | 1397 | /* keep all GDB-injected breakpoints in front */ |
aliguori | c0ce998 | 2008-11-25 22:13:57 +0000 | [diff] [blame] | 1398 | if (flags & BP_GDB) |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1399 | QTAILQ_INSERT_HEAD(&env->breakpoints, bp, entry); |
aliguori | c0ce998 | 2008-11-25 22:13:57 +0000 | [diff] [blame] | 1400 | else |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1401 | QTAILQ_INSERT_TAIL(&env->breakpoints, bp, entry); |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1402 | |
| 1403 | breakpoint_invalidate(env, pc); |
| 1404 | |
| 1405 | if (breakpoint) |
| 1406 | *breakpoint = bp; |
| 1407 | return 0; |
| 1408 | #else |
| 1409 | return -ENOSYS; |
| 1410 | #endif |
| 1411 | } |
| 1412 | |
| 1413 | /* Remove a specific breakpoint. */ |
| 1414 | int cpu_breakpoint_remove(CPUState *env, target_ulong pc, int flags) |
| 1415 | { |
| 1416 | #if defined(TARGET_HAS_ICE) |
| 1417 | CPUBreakpoint *bp; |
| 1418 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1419 | QTAILQ_FOREACH(bp, &env->breakpoints, entry) { |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1420 | if (bp->pc == pc && bp->flags == flags) { |
| 1421 | cpu_breakpoint_remove_by_ref(env, bp); |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 1422 | return 0; |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1423 | } |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 1424 | } |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1425 | return -ENOENT; |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 1426 | #else |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1427 | return -ENOSYS; |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 1428 | #endif |
| 1429 | } |
| 1430 | |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1431 | /* Remove a specific breakpoint by reference. */ |
| 1432 | void cpu_breakpoint_remove_by_ref(CPUState *env, CPUBreakpoint *breakpoint) |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 1433 | { |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1434 | #if defined(TARGET_HAS_ICE) |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1435 | QTAILQ_REMOVE(&env->breakpoints, breakpoint, entry); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1436 | |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1437 | breakpoint_invalidate(env, breakpoint->pc); |
| 1438 | |
| 1439 | qemu_free(breakpoint); |
| 1440 | #endif |
| 1441 | } |
| 1442 | |
| 1443 | /* Remove all matching breakpoints. */ |
| 1444 | void cpu_breakpoint_remove_all(CPUState *env, int mask) |
| 1445 | { |
| 1446 | #if defined(TARGET_HAS_ICE) |
aliguori | c0ce998 | 2008-11-25 22:13:57 +0000 | [diff] [blame] | 1447 | CPUBreakpoint *bp, *next; |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1448 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1449 | QTAILQ_FOREACH_SAFE(bp, &env->breakpoints, entry, next) { |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1450 | if (bp->flags & mask) |
| 1451 | cpu_breakpoint_remove_by_ref(env, bp); |
aliguori | c0ce998 | 2008-11-25 22:13:57 +0000 | [diff] [blame] | 1452 | } |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 1453 | #endif |
| 1454 | } |
| 1455 | |
bellard | c33a346 | 2003-07-29 20:50:33 +0000 | [diff] [blame] | 1456 | /* enable or disable single step mode. EXCP_DEBUG is returned by the |
| 1457 | CPU loop after each instruction */ |
| 1458 | void cpu_single_step(CPUState *env, int enabled) |
| 1459 | { |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1460 | #if defined(TARGET_HAS_ICE) |
bellard | c33a346 | 2003-07-29 20:50:33 +0000 | [diff] [blame] | 1461 | if (env->singlestep_enabled != enabled) { |
| 1462 | env->singlestep_enabled = enabled; |
aliguori | e22a25c | 2009-03-12 20:12:48 +0000 | [diff] [blame] | 1463 | if (kvm_enabled()) |
| 1464 | kvm_update_guest_debug(env, 0); |
| 1465 | else { |
Stuart Brady | ccbb4d4 | 2009-05-03 12:15:06 +0100 | [diff] [blame] | 1466 | /* must flush all the translated code to avoid inconsistencies */ |
aliguori | e22a25c | 2009-03-12 20:12:48 +0000 | [diff] [blame] | 1467 | /* XXX: only flush what is necessary */ |
| 1468 | tb_flush(env); |
| 1469 | } |
bellard | c33a346 | 2003-07-29 20:50:33 +0000 | [diff] [blame] | 1470 | } |
| 1471 | #endif |
| 1472 | } |
| 1473 | |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 1474 | /* enable or disable low levels log */ |
| 1475 | void cpu_set_log(int log_flags) |
| 1476 | { |
| 1477 | loglevel = log_flags; |
| 1478 | if (loglevel && !logfile) { |
pbrook | 11fcfab | 2007-07-01 18:21:11 +0000 | [diff] [blame] | 1479 | logfile = fopen(logfilename, log_append ? "a" : "w"); |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 1480 | if (!logfile) { |
| 1481 | perror(logfilename); |
| 1482 | _exit(1); |
| 1483 | } |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1484 | #if !defined(CONFIG_SOFTMMU) |
| 1485 | /* must avoid mmap() usage of glibc by setting a buffer "by hand" */ |
| 1486 | { |
blueswir1 | b55266b | 2008-09-20 08:07:15 +0000 | [diff] [blame] | 1487 | static char logfile_buf[4096]; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1488 | setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf)); |
| 1489 | } |
Filip Navara | bf65f53 | 2009-07-27 10:02:04 -0500 | [diff] [blame] | 1490 | #elif !defined(_WIN32) |
| 1491 | /* Win32 doesn't support line-buffering and requires size >= 2 */ |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 1492 | setvbuf(logfile, NULL, _IOLBF, 0); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1493 | #endif |
pbrook | e735b91 | 2007-06-30 13:53:24 +0000 | [diff] [blame] | 1494 | log_append = 1; |
| 1495 | } |
| 1496 | if (!loglevel && logfile) { |
| 1497 | fclose(logfile); |
| 1498 | logfile = NULL; |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 1499 | } |
| 1500 | } |
| 1501 | |
| 1502 | void cpu_set_log_filename(const char *filename) |
| 1503 | { |
| 1504 | logfilename = strdup(filename); |
pbrook | e735b91 | 2007-06-30 13:53:24 +0000 | [diff] [blame] | 1505 | if (logfile) { |
| 1506 | fclose(logfile); |
| 1507 | logfile = NULL; |
| 1508 | } |
| 1509 | cpu_set_log(loglevel); |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 1510 | } |
bellard | c33a346 | 2003-07-29 20:50:33 +0000 | [diff] [blame] | 1511 | |
aurel32 | 3098dba | 2009-03-07 21:28:24 +0000 | [diff] [blame] | 1512 | static void cpu_unlink_tb(CPUState *env) |
bellard | ea041c0 | 2003-06-25 16:16:50 +0000 | [diff] [blame] | 1513 | { |
pbrook | d597536 | 2008-06-07 20:50:51 +0000 | [diff] [blame] | 1514 | /* FIXME: TB unchaining isn't SMP safe. For now just ignore the |
| 1515 | problem and hope the cpu will stop of its own accord. For userspace |
| 1516 | emulation this often isn't actually as bad as it sounds. Often |
| 1517 | signals are used primarily to interrupt blocking syscalls. */ |
aurel32 | 3098dba | 2009-03-07 21:28:24 +0000 | [diff] [blame] | 1518 | TranslationBlock *tb; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 1519 | static spinlock_t interrupt_lock = SPIN_LOCK_UNLOCKED; |
aurel32 | 3098dba | 2009-03-07 21:28:24 +0000 | [diff] [blame] | 1520 | |
Riku Voipio | cab1b4b | 2010-01-20 12:56:27 +0200 | [diff] [blame] | 1521 | spin_lock(&interrupt_lock); |
aurel32 | 3098dba | 2009-03-07 21:28:24 +0000 | [diff] [blame] | 1522 | tb = env->current_tb; |
| 1523 | /* if the cpu is currently executing code, we must unlink it and |
| 1524 | all the potentially executing TB */ |
Riku Voipio | f76cfe5 | 2009-12-04 15:16:30 +0200 | [diff] [blame] | 1525 | if (tb) { |
aurel32 | 3098dba | 2009-03-07 21:28:24 +0000 | [diff] [blame] | 1526 | env->current_tb = NULL; |
| 1527 | tb_reset_jump_recursive(tb); |
aurel32 | 3098dba | 2009-03-07 21:28:24 +0000 | [diff] [blame] | 1528 | } |
Riku Voipio | cab1b4b | 2010-01-20 12:56:27 +0200 | [diff] [blame] | 1529 | spin_unlock(&interrupt_lock); |
aurel32 | 3098dba | 2009-03-07 21:28:24 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
| 1532 | /* mask must never be zero, except for A20 change call */ |
| 1533 | void cpu_interrupt(CPUState *env, int mask) |
| 1534 | { |
| 1535 | int old_mask; |
| 1536 | |
| 1537 | old_mask = env->interrupt_request; |
| 1538 | env->interrupt_request |= mask; |
| 1539 | |
aliguori | 8edac96 | 2009-04-24 18:03:45 +0000 | [diff] [blame] | 1540 | #ifndef CONFIG_USER_ONLY |
| 1541 | /* |
| 1542 | * If called from iothread context, wake the target cpu in |
| 1543 | * case its halted. |
| 1544 | */ |
| 1545 | if (!qemu_cpu_self(env)) { |
| 1546 | qemu_cpu_kick(env); |
| 1547 | return; |
| 1548 | } |
| 1549 | #endif |
| 1550 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1551 | if (use_icount) { |
pbrook | 266910c | 2008-07-09 15:31:50 +0000 | [diff] [blame] | 1552 | env->icount_decr.u16.high = 0xffff; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1553 | #ifndef CONFIG_USER_ONLY |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1554 | if (!can_do_io(env) |
aurel32 | be214e6 | 2009-03-06 21:48:00 +0000 | [diff] [blame] | 1555 | && (mask & ~old_mask) != 0) { |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1556 | cpu_abort(env, "Raised interrupt while not in I/O function"); |
| 1557 | } |
| 1558 | #endif |
| 1559 | } else { |
aurel32 | 3098dba | 2009-03-07 21:28:24 +0000 | [diff] [blame] | 1560 | cpu_unlink_tb(env); |
bellard | ea041c0 | 2003-06-25 16:16:50 +0000 | [diff] [blame] | 1561 | } |
| 1562 | } |
| 1563 | |
bellard | b54ad04 | 2004-05-20 13:42:52 +0000 | [diff] [blame] | 1564 | void cpu_reset_interrupt(CPUState *env, int mask) |
| 1565 | { |
| 1566 | env->interrupt_request &= ~mask; |
| 1567 | } |
| 1568 | |
aurel32 | 3098dba | 2009-03-07 21:28:24 +0000 | [diff] [blame] | 1569 | void cpu_exit(CPUState *env) |
| 1570 | { |
| 1571 | env->exit_request = 1; |
| 1572 | cpu_unlink_tb(env); |
| 1573 | } |
| 1574 | |
blueswir1 | c7cd6a3 | 2008-10-02 18:27:46 +0000 | [diff] [blame] | 1575 | const CPULogItem cpu_log_items[] = { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1576 | { CPU_LOG_TB_OUT_ASM, "out_asm", |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1577 | "show generated host assembly code for each compiled TB" }, |
| 1578 | { CPU_LOG_TB_IN_ASM, "in_asm", |
| 1579 | "show target assembly code for each compiled TB" }, |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1580 | { CPU_LOG_TB_OP, "op", |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 1581 | "show micro ops for each compiled TB" }, |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1582 | { CPU_LOG_TB_OP_OPT, "op_opt", |
blueswir1 | e01a115 | 2008-03-14 17:37:11 +0000 | [diff] [blame] | 1583 | "show micro ops " |
| 1584 | #ifdef TARGET_I386 |
| 1585 | "before eflags optimization and " |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1586 | #endif |
blueswir1 | e01a115 | 2008-03-14 17:37:11 +0000 | [diff] [blame] | 1587 | "after liveness analysis" }, |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1588 | { CPU_LOG_INT, "int", |
| 1589 | "show interrupts/exceptions in short format" }, |
| 1590 | { CPU_LOG_EXEC, "exec", |
| 1591 | "show trace before each executed TB (lots of logs)" }, |
bellard | 9fddaa0 | 2004-05-21 12:59:32 +0000 | [diff] [blame] | 1592 | { CPU_LOG_TB_CPU, "cpu", |
ths | e91c8a7 | 2007-06-03 13:35:16 +0000 | [diff] [blame] | 1593 | "show CPU state before block translation" }, |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1594 | #ifdef TARGET_I386 |
| 1595 | { CPU_LOG_PCALL, "pcall", |
| 1596 | "show protected mode far calls/returns/exceptions" }, |
aliguori | eca1bdf | 2009-01-26 19:54:31 +0000 | [diff] [blame] | 1597 | { CPU_LOG_RESET, "cpu_reset", |
| 1598 | "show CPU state before CPU resets" }, |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1599 | #endif |
bellard | 8e3a9fd | 2004-10-09 17:32:58 +0000 | [diff] [blame] | 1600 | #ifdef DEBUG_IOPORT |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1601 | { CPU_LOG_IOPORT, "ioport", |
| 1602 | "show all i/o ports accesses" }, |
bellard | 8e3a9fd | 2004-10-09 17:32:58 +0000 | [diff] [blame] | 1603 | #endif |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1604 | { 0, NULL, NULL }, |
| 1605 | }; |
| 1606 | |
Michael S. Tsirkin | f6f3fbc | 2010-01-27 22:06:57 +0200 | [diff] [blame] | 1607 | #ifndef CONFIG_USER_ONLY |
| 1608 | static QLIST_HEAD(memory_client_list, CPUPhysMemoryClient) memory_client_list |
| 1609 | = QLIST_HEAD_INITIALIZER(memory_client_list); |
| 1610 | |
| 1611 | static void cpu_notify_set_memory(target_phys_addr_t start_addr, |
| 1612 | ram_addr_t size, |
| 1613 | ram_addr_t phys_offset) |
| 1614 | { |
| 1615 | CPUPhysMemoryClient *client; |
| 1616 | QLIST_FOREACH(client, &memory_client_list, list) { |
| 1617 | client->set_memory(client, start_addr, size, phys_offset); |
| 1618 | } |
| 1619 | } |
| 1620 | |
| 1621 | static int cpu_notify_sync_dirty_bitmap(target_phys_addr_t start, |
| 1622 | target_phys_addr_t end) |
| 1623 | { |
| 1624 | CPUPhysMemoryClient *client; |
| 1625 | QLIST_FOREACH(client, &memory_client_list, list) { |
| 1626 | int r = client->sync_dirty_bitmap(client, start, end); |
| 1627 | if (r < 0) |
| 1628 | return r; |
| 1629 | } |
| 1630 | return 0; |
| 1631 | } |
| 1632 | |
| 1633 | static int cpu_notify_migration_log(int enable) |
| 1634 | { |
| 1635 | CPUPhysMemoryClient *client; |
| 1636 | QLIST_FOREACH(client, &memory_client_list, list) { |
| 1637 | int r = client->migration_log(client, enable); |
| 1638 | if (r < 0) |
| 1639 | return r; |
| 1640 | } |
| 1641 | return 0; |
| 1642 | } |
| 1643 | |
| 1644 | static void phys_page_for_each_in_l1_map(PhysPageDesc **phys_map, |
| 1645 | CPUPhysMemoryClient *client) |
| 1646 | { |
| 1647 | PhysPageDesc *pd; |
| 1648 | int l1, l2; |
| 1649 | |
| 1650 | for (l1 = 0; l1 < L1_SIZE; ++l1) { |
| 1651 | pd = phys_map[l1]; |
| 1652 | if (!pd) { |
| 1653 | continue; |
| 1654 | } |
| 1655 | for (l2 = 0; l2 < L2_SIZE; ++l2) { |
| 1656 | if (pd[l2].phys_offset == IO_MEM_UNASSIGNED) { |
| 1657 | continue; |
| 1658 | } |
| 1659 | client->set_memory(client, pd[l2].region_offset, |
| 1660 | TARGET_PAGE_SIZE, pd[l2].phys_offset); |
| 1661 | } |
| 1662 | } |
| 1663 | } |
| 1664 | |
| 1665 | static void phys_page_for_each(CPUPhysMemoryClient *client) |
| 1666 | { |
| 1667 | #if TARGET_PHYS_ADDR_SPACE_BITS > 32 |
| 1668 | |
| 1669 | #if TARGET_PHYS_ADDR_SPACE_BITS > (32 + L1_BITS) |
| 1670 | #error unsupported TARGET_PHYS_ADDR_SPACE_BITS |
| 1671 | #endif |
| 1672 | void **phys_map = (void **)l1_phys_map; |
| 1673 | int l1; |
| 1674 | if (!l1_phys_map) { |
| 1675 | return; |
| 1676 | } |
| 1677 | for (l1 = 0; l1 < L1_SIZE; ++l1) { |
| 1678 | if (phys_map[l1]) { |
| 1679 | phys_page_for_each_in_l1_map(phys_map[l1], client); |
| 1680 | } |
| 1681 | } |
| 1682 | #else |
| 1683 | if (!l1_phys_map) { |
| 1684 | return; |
| 1685 | } |
| 1686 | phys_page_for_each_in_l1_map(l1_phys_map, client); |
| 1687 | #endif |
| 1688 | } |
| 1689 | |
| 1690 | void cpu_register_phys_memory_client(CPUPhysMemoryClient *client) |
| 1691 | { |
| 1692 | QLIST_INSERT_HEAD(&memory_client_list, client, list); |
| 1693 | phys_page_for_each(client); |
| 1694 | } |
| 1695 | |
| 1696 | void cpu_unregister_phys_memory_client(CPUPhysMemoryClient *client) |
| 1697 | { |
| 1698 | QLIST_REMOVE(client, list); |
| 1699 | } |
| 1700 | #endif |
| 1701 | |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1702 | static int cmp1(const char *s1, int n, const char *s2) |
| 1703 | { |
| 1704 | if (strlen(s2) != n) |
| 1705 | return 0; |
| 1706 | return memcmp(s1, s2, n) == 0; |
| 1707 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1708 | |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1709 | /* takes a comma separated list of log masks. Return 0 if error. */ |
| 1710 | int cpu_str_to_log_mask(const char *str) |
| 1711 | { |
blueswir1 | c7cd6a3 | 2008-10-02 18:27:46 +0000 | [diff] [blame] | 1712 | const CPULogItem *item; |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1713 | int mask; |
| 1714 | const char *p, *p1; |
| 1715 | |
| 1716 | p = str; |
| 1717 | mask = 0; |
| 1718 | for(;;) { |
| 1719 | p1 = strchr(p, ','); |
| 1720 | if (!p1) |
| 1721 | p1 = p + strlen(p); |
bellard | 8e3a9fd | 2004-10-09 17:32:58 +0000 | [diff] [blame] | 1722 | if(cmp1(p,p1-p,"all")) { |
| 1723 | for(item = cpu_log_items; item->mask != 0; item++) { |
| 1724 | mask |= item->mask; |
| 1725 | } |
| 1726 | } else { |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1727 | for(item = cpu_log_items; item->mask != 0; item++) { |
| 1728 | if (cmp1(p, p1 - p, item->name)) |
| 1729 | goto found; |
| 1730 | } |
| 1731 | return 0; |
bellard | 8e3a9fd | 2004-10-09 17:32:58 +0000 | [diff] [blame] | 1732 | } |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1733 | found: |
| 1734 | mask |= item->mask; |
| 1735 | if (*p1 != ',') |
| 1736 | break; |
| 1737 | p = p1 + 1; |
| 1738 | } |
| 1739 | return mask; |
| 1740 | } |
bellard | ea041c0 | 2003-06-25 16:16:50 +0000 | [diff] [blame] | 1741 | |
bellard | 7501267 | 2003-06-21 13:11:07 +0000 | [diff] [blame] | 1742 | void cpu_abort(CPUState *env, const char *fmt, ...) |
| 1743 | { |
| 1744 | va_list ap; |
pbrook | 493ae1f | 2007-11-23 16:53:59 +0000 | [diff] [blame] | 1745 | va_list ap2; |
bellard | 7501267 | 2003-06-21 13:11:07 +0000 | [diff] [blame] | 1746 | |
| 1747 | va_start(ap, fmt); |
pbrook | 493ae1f | 2007-11-23 16:53:59 +0000 | [diff] [blame] | 1748 | va_copy(ap2, ap); |
bellard | 7501267 | 2003-06-21 13:11:07 +0000 | [diff] [blame] | 1749 | fprintf(stderr, "qemu: fatal: "); |
| 1750 | vfprintf(stderr, fmt, ap); |
| 1751 | fprintf(stderr, "\n"); |
| 1752 | #ifdef TARGET_I386 |
bellard | 7fe4848 | 2004-10-09 18:08:01 +0000 | [diff] [blame] | 1753 | cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP); |
| 1754 | #else |
| 1755 | cpu_dump_state(env, stderr, fprintf, 0); |
bellard | 7501267 | 2003-06-21 13:11:07 +0000 | [diff] [blame] | 1756 | #endif |
aliguori | 93fcfe3 | 2009-01-15 22:34:14 +0000 | [diff] [blame] | 1757 | if (qemu_log_enabled()) { |
| 1758 | qemu_log("qemu: fatal: "); |
| 1759 | qemu_log_vprintf(fmt, ap2); |
| 1760 | qemu_log("\n"); |
j_mayer | f937329 | 2007-09-29 12:18:20 +0000 | [diff] [blame] | 1761 | #ifdef TARGET_I386 |
aliguori | 93fcfe3 | 2009-01-15 22:34:14 +0000 | [diff] [blame] | 1762 | log_cpu_state(env, X86_DUMP_FPU | X86_DUMP_CCOP); |
j_mayer | f937329 | 2007-09-29 12:18:20 +0000 | [diff] [blame] | 1763 | #else |
aliguori | 93fcfe3 | 2009-01-15 22:34:14 +0000 | [diff] [blame] | 1764 | log_cpu_state(env, 0); |
j_mayer | f937329 | 2007-09-29 12:18:20 +0000 | [diff] [blame] | 1765 | #endif |
aliguori | 31b1a7b | 2009-01-15 22:35:09 +0000 | [diff] [blame] | 1766 | qemu_log_flush(); |
aliguori | 93fcfe3 | 2009-01-15 22:34:14 +0000 | [diff] [blame] | 1767 | qemu_log_close(); |
balrog | 924edca | 2007-06-10 14:07:13 +0000 | [diff] [blame] | 1768 | } |
pbrook | 493ae1f | 2007-11-23 16:53:59 +0000 | [diff] [blame] | 1769 | va_end(ap2); |
j_mayer | f937329 | 2007-09-29 12:18:20 +0000 | [diff] [blame] | 1770 | va_end(ap); |
Riku Voipio | fd052bf | 2010-01-25 14:30:49 +0200 | [diff] [blame] | 1771 | #if defined(CONFIG_USER_ONLY) |
| 1772 | { |
| 1773 | struct sigaction act; |
| 1774 | sigfillset(&act.sa_mask); |
| 1775 | act.sa_handler = SIG_DFL; |
| 1776 | sigaction(SIGABRT, &act, NULL); |
| 1777 | } |
| 1778 | #endif |
bellard | 7501267 | 2003-06-21 13:11:07 +0000 | [diff] [blame] | 1779 | abort(); |
| 1780 | } |
| 1781 | |
ths | c5be9f0 | 2007-02-28 20:20:53 +0000 | [diff] [blame] | 1782 | CPUState *cpu_copy(CPUState *env) |
| 1783 | { |
ths | 01ba981 | 2007-12-09 02:22:57 +0000 | [diff] [blame] | 1784 | CPUState *new_env = cpu_init(env->cpu_model_str); |
ths | c5be9f0 | 2007-02-28 20:20:53 +0000 | [diff] [blame] | 1785 | CPUState *next_cpu = new_env->next_cpu; |
| 1786 | int cpu_index = new_env->cpu_index; |
aliguori | 5a38f08 | 2009-01-15 20:16:51 +0000 | [diff] [blame] | 1787 | #if defined(TARGET_HAS_ICE) |
| 1788 | CPUBreakpoint *bp; |
| 1789 | CPUWatchpoint *wp; |
| 1790 | #endif |
| 1791 | |
ths | c5be9f0 | 2007-02-28 20:20:53 +0000 | [diff] [blame] | 1792 | memcpy(new_env, env, sizeof(CPUState)); |
aliguori | 5a38f08 | 2009-01-15 20:16:51 +0000 | [diff] [blame] | 1793 | |
| 1794 | /* Preserve chaining and index. */ |
ths | c5be9f0 | 2007-02-28 20:20:53 +0000 | [diff] [blame] | 1795 | new_env->next_cpu = next_cpu; |
| 1796 | new_env->cpu_index = cpu_index; |
aliguori | 5a38f08 | 2009-01-15 20:16:51 +0000 | [diff] [blame] | 1797 | |
| 1798 | /* Clone all break/watchpoints. |
| 1799 | Note: Once we support ptrace with hw-debug register access, make sure |
| 1800 | BP_CPU break/watchpoints are handled correctly on clone. */ |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1801 | QTAILQ_INIT(&env->breakpoints); |
| 1802 | QTAILQ_INIT(&env->watchpoints); |
aliguori | 5a38f08 | 2009-01-15 20:16:51 +0000 | [diff] [blame] | 1803 | #if defined(TARGET_HAS_ICE) |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1804 | QTAILQ_FOREACH(bp, &env->breakpoints, entry) { |
aliguori | 5a38f08 | 2009-01-15 20:16:51 +0000 | [diff] [blame] | 1805 | cpu_breakpoint_insert(new_env, bp->pc, bp->flags, NULL); |
| 1806 | } |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1807 | QTAILQ_FOREACH(wp, &env->watchpoints, entry) { |
aliguori | 5a38f08 | 2009-01-15 20:16:51 +0000 | [diff] [blame] | 1808 | cpu_watchpoint_insert(new_env, wp->vaddr, (~wp->len_mask) + 1, |
| 1809 | wp->flags, NULL); |
| 1810 | } |
| 1811 | #endif |
| 1812 | |
ths | c5be9f0 | 2007-02-28 20:20:53 +0000 | [diff] [blame] | 1813 | return new_env; |
| 1814 | } |
| 1815 | |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 1816 | #if !defined(CONFIG_USER_ONLY) |
| 1817 | |
edgar_igl | 5c751e9 | 2008-05-06 08:44:21 +0000 | [diff] [blame] | 1818 | static inline void tlb_flush_jmp_cache(CPUState *env, target_ulong addr) |
| 1819 | { |
| 1820 | unsigned int i; |
| 1821 | |
| 1822 | /* Discard jump cache entries for any tb which might potentially |
| 1823 | overlap the flushed page. */ |
| 1824 | i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE); |
| 1825 | memset (&env->tb_jmp_cache[i], 0, |
| 1826 | TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *)); |
| 1827 | |
| 1828 | i = tb_jmp_cache_hash_page(addr); |
| 1829 | memset (&env->tb_jmp_cache[i], 0, |
| 1830 | TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *)); |
| 1831 | } |
| 1832 | |
Igor Kovalenko | 0873898 | 2009-07-12 02:15:40 +0400 | [diff] [blame] | 1833 | static CPUTLBEntry s_cputlb_empty_entry = { |
| 1834 | .addr_read = -1, |
| 1835 | .addr_write = -1, |
| 1836 | .addr_code = -1, |
| 1837 | .addend = -1, |
| 1838 | }; |
| 1839 | |
bellard | ee8b702 | 2004-02-03 23:35:10 +0000 | [diff] [blame] | 1840 | /* NOTE: if flush_global is true, also flush global entries (not |
| 1841 | implemented yet) */ |
| 1842 | void tlb_flush(CPUState *env, int flush_global) |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 1843 | { |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 1844 | int i; |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 1845 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1846 | #if defined(DEBUG_TLB) |
| 1847 | printf("tlb_flush:\n"); |
| 1848 | #endif |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 1849 | /* must reset current TB so that interrupts cannot modify the |
| 1850 | links while we are modifying them */ |
| 1851 | env->current_tb = NULL; |
| 1852 | |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 1853 | for(i = 0; i < CPU_TLB_SIZE; i++) { |
Isaku Yamahata | cfde4bd | 2009-05-20 11:31:43 +0900 | [diff] [blame] | 1854 | int mmu_idx; |
| 1855 | for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) { |
Igor Kovalenko | 0873898 | 2009-07-12 02:15:40 +0400 | [diff] [blame] | 1856 | env->tlb_table[mmu_idx][i] = s_cputlb_empty_entry; |
Isaku Yamahata | cfde4bd | 2009-05-20 11:31:43 +0900 | [diff] [blame] | 1857 | } |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 1858 | } |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1859 | |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 1860 | memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *)); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1861 | |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 1862 | tlb_flush_count++; |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
bellard | 274da6b | 2004-05-20 21:56:27 +0000 | [diff] [blame] | 1865 | static inline void tlb_flush_entry(CPUTLBEntry *tlb_entry, target_ulong addr) |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 1866 | { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1867 | if (addr == (tlb_entry->addr_read & |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 1868 | (TARGET_PAGE_MASK | TLB_INVALID_MASK)) || |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1869 | addr == (tlb_entry->addr_write & |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 1870 | (TARGET_PAGE_MASK | TLB_INVALID_MASK)) || |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1871 | addr == (tlb_entry->addr_code & |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 1872 | (TARGET_PAGE_MASK | TLB_INVALID_MASK))) { |
Igor Kovalenko | 0873898 | 2009-07-12 02:15:40 +0400 | [diff] [blame] | 1873 | *tlb_entry = s_cputlb_empty_entry; |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 1874 | } |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
bellard | 2e12669 | 2004-04-25 21:28:44 +0000 | [diff] [blame] | 1877 | void tlb_flush_page(CPUState *env, target_ulong addr) |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 1878 | { |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 1879 | int i; |
Isaku Yamahata | cfde4bd | 2009-05-20 11:31:43 +0900 | [diff] [blame] | 1880 | int mmu_idx; |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 1881 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1882 | #if defined(DEBUG_TLB) |
bellard | 108c49b | 2005-07-24 12:55:09 +0000 | [diff] [blame] | 1883 | printf("tlb_flush_page: " TARGET_FMT_lx "\n", addr); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1884 | #endif |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 1885 | /* must reset current TB so that interrupts cannot modify the |
| 1886 | links while we are modifying them */ |
| 1887 | env->current_tb = NULL; |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 1888 | |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 1889 | addr &= TARGET_PAGE_MASK; |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 1890 | i = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1); |
Isaku Yamahata | cfde4bd | 2009-05-20 11:31:43 +0900 | [diff] [blame] | 1891 | for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) |
| 1892 | tlb_flush_entry(&env->tlb_table[mmu_idx][i], addr); |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 1893 | |
edgar_igl | 5c751e9 | 2008-05-06 08:44:21 +0000 | [diff] [blame] | 1894 | tlb_flush_jmp_cache(env, addr); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1897 | /* update the TLBs so that writes to code in the virtual page 'addr' |
| 1898 | can be detected */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 1899 | static void tlb_protect_code(ram_addr_t ram_addr) |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 1900 | { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1901 | cpu_physical_memory_reset_dirty(ram_addr, |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1902 | ram_addr + TARGET_PAGE_SIZE, |
| 1903 | CODE_DIRTY_FLAG); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1904 | } |
| 1905 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1906 | /* 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] | 1907 | tested for self modifying code */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 1908 | static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr, |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 1909 | target_ulong vaddr) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1910 | { |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 1911 | phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] |= CODE_DIRTY_FLAG; |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1914 | static inline void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry, |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 1915 | unsigned long start, unsigned long length) |
| 1916 | { |
| 1917 | unsigned long addr; |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 1918 | if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) { |
| 1919 | addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend; |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 1920 | if ((addr - start) < length) { |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 1921 | tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | TLB_NOTDIRTY; |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 1922 | } |
| 1923 | } |
| 1924 | } |
| 1925 | |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 1926 | /* Note: start and end must be within the same ram block. */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 1927 | 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] | 1928 | int dirty_flags) |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 1929 | { |
| 1930 | CPUState *env; |
bellard | 4f2ac23 | 2004-04-26 19:44:02 +0000 | [diff] [blame] | 1931 | unsigned long length, start1; |
bellard | 0a962c0 | 2005-02-10 22:00:27 +0000 | [diff] [blame] | 1932 | int i, mask, len; |
| 1933 | uint8_t *p; |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 1934 | |
| 1935 | start &= TARGET_PAGE_MASK; |
| 1936 | end = TARGET_PAGE_ALIGN(end); |
| 1937 | |
| 1938 | length = end - start; |
| 1939 | if (length == 0) |
| 1940 | return; |
bellard | 0a962c0 | 2005-02-10 22:00:27 +0000 | [diff] [blame] | 1941 | len = length >> TARGET_PAGE_BITS; |
bellard | f23db16 | 2005-08-21 19:12:28 +0000 | [diff] [blame] | 1942 | mask = ~dirty_flags; |
| 1943 | p = phys_ram_dirty + (start >> TARGET_PAGE_BITS); |
| 1944 | for(i = 0; i < len; i++) |
| 1945 | p[i] &= mask; |
| 1946 | |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 1947 | /* we modify the TLB cache so that the dirty bit will be set again |
| 1948 | when accessing the range */ |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 1949 | start1 = (unsigned long)qemu_get_ram_ptr(start); |
| 1950 | /* Chek that we don't span multiple blocks - this breaks the |
| 1951 | address comparisons below. */ |
| 1952 | if ((unsigned long)qemu_get_ram_ptr(end - 1) - start1 |
| 1953 | != (end - 1) - start) { |
| 1954 | abort(); |
| 1955 | } |
| 1956 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1957 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
Isaku Yamahata | cfde4bd | 2009-05-20 11:31:43 +0900 | [diff] [blame] | 1958 | int mmu_idx; |
| 1959 | for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) { |
| 1960 | for(i = 0; i < CPU_TLB_SIZE; i++) |
| 1961 | tlb_reset_dirty_range(&env->tlb_table[mmu_idx][i], |
| 1962 | start1, length); |
| 1963 | } |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1964 | } |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
aliguori | 7457619 | 2008-10-06 14:02:03 +0000 | [diff] [blame] | 1967 | int cpu_physical_memory_set_dirty_tracking(int enable) |
| 1968 | { |
Michael S. Tsirkin | f6f3fbc | 2010-01-27 22:06:57 +0200 | [diff] [blame] | 1969 | int ret = 0; |
aliguori | 7457619 | 2008-10-06 14:02:03 +0000 | [diff] [blame] | 1970 | in_migration = enable; |
Michael S. Tsirkin | f6f3fbc | 2010-01-27 22:06:57 +0200 | [diff] [blame] | 1971 | ret = cpu_notify_migration_log(!!enable); |
| 1972 | return ret; |
aliguori | 7457619 | 2008-10-06 14:02:03 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
| 1975 | int cpu_physical_memory_get_dirty_tracking(void) |
| 1976 | { |
| 1977 | return in_migration; |
| 1978 | } |
| 1979 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 1980 | int cpu_physical_sync_dirty_bitmap(target_phys_addr_t start_addr, |
| 1981 | target_phys_addr_t end_addr) |
aliguori | 2bec46d | 2008-11-24 20:21:41 +0000 | [diff] [blame] | 1982 | { |
Michael S. Tsirkin | 7b8f3b7 | 2010-01-27 22:07:21 +0200 | [diff] [blame] | 1983 | int ret; |
Jan Kiszka | 151f774 | 2009-05-01 20:52:47 +0200 | [diff] [blame] | 1984 | |
Michael S. Tsirkin | f6f3fbc | 2010-01-27 22:06:57 +0200 | [diff] [blame] | 1985 | ret = cpu_notify_sync_dirty_bitmap(start_addr, end_addr); |
Jan Kiszka | 151f774 | 2009-05-01 20:52:47 +0200 | [diff] [blame] | 1986 | return ret; |
aliguori | 2bec46d | 2008-11-24 20:21:41 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 1989 | static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry) |
| 1990 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 1991 | ram_addr_t ram_addr; |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 1992 | void *p; |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 1993 | |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 1994 | if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) { |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 1995 | p = (void *)(unsigned long)((tlb_entry->addr_write & TARGET_PAGE_MASK) |
| 1996 | + tlb_entry->addend); |
| 1997 | ram_addr = qemu_ram_addr_from_host(p); |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 1998 | if (!cpu_physical_memory_is_dirty(ram_addr)) { |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 1999 | tlb_entry->addr_write |= TLB_NOTDIRTY; |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2000 | } |
| 2001 | } |
| 2002 | } |
| 2003 | |
| 2004 | /* update the TLB according to the current state of the dirty bits */ |
| 2005 | void cpu_tlb_update_dirty(CPUState *env) |
| 2006 | { |
| 2007 | int i; |
Isaku Yamahata | cfde4bd | 2009-05-20 11:31:43 +0900 | [diff] [blame] | 2008 | int mmu_idx; |
| 2009 | for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) { |
| 2010 | for(i = 0; i < CPU_TLB_SIZE; i++) |
| 2011 | tlb_update_dirty(&env->tlb_table[mmu_idx][i]); |
| 2012 | } |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2015 | static inline void tlb_set_dirty1(CPUTLBEntry *tlb_entry, target_ulong vaddr) |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2016 | { |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2017 | if (tlb_entry->addr_write == (vaddr | TLB_NOTDIRTY)) |
| 2018 | tlb_entry->addr_write = vaddr; |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2021 | /* update the TLB corresponding to virtual page vaddr |
| 2022 | so that it is no longer dirty */ |
| 2023 | static inline void tlb_set_dirty(CPUState *env, target_ulong vaddr) |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2024 | { |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2025 | int i; |
Isaku Yamahata | cfde4bd | 2009-05-20 11:31:43 +0900 | [diff] [blame] | 2026 | int mmu_idx; |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2027 | |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2028 | vaddr &= TARGET_PAGE_MASK; |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2029 | i = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1); |
Isaku Yamahata | cfde4bd | 2009-05-20 11:31:43 +0900 | [diff] [blame] | 2030 | for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) |
| 2031 | tlb_set_dirty1(&env->tlb_table[mmu_idx][i], vaddr); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
bellard | 59817cc | 2004-02-16 22:01:13 +0000 | [diff] [blame] | 2034 | /* add a new TLB entry. At most one entry for a given virtual address |
| 2035 | is permitted. Return 0 if OK or 2 if the page could not be mapped |
| 2036 | (can only happen in non SOFTMMU mode for I/O pages or pages |
| 2037 | conflicting with the host address space). */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2038 | int tlb_set_page_exec(CPUState *env, target_ulong vaddr, |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2039 | target_phys_addr_t paddr, int prot, |
j_mayer | 6ebbf39 | 2007-10-14 07:07:08 +0000 | [diff] [blame] | 2040 | int mmu_idx, int is_softmmu) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2041 | { |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 2042 | PhysPageDesc *p; |
bellard | 4f2ac23 | 2004-04-26 19:44:02 +0000 | [diff] [blame] | 2043 | unsigned long pd; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2044 | unsigned int index; |
bellard | 4f2ac23 | 2004-04-26 19:44:02 +0000 | [diff] [blame] | 2045 | target_ulong address; |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2046 | target_ulong code_address; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2047 | target_phys_addr_t addend; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2048 | int ret; |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 2049 | CPUTLBEntry *te; |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 2050 | CPUWatchpoint *wp; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2051 | target_phys_addr_t iotlb; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2052 | |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 2053 | p = phys_page_find(paddr >> TARGET_PAGE_BITS); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2054 | if (!p) { |
| 2055 | pd = IO_MEM_UNASSIGNED; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2056 | } else { |
| 2057 | pd = p->phys_offset; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2058 | } |
| 2059 | #if defined(DEBUG_TLB) |
j_mayer | 6ebbf39 | 2007-10-14 07:07:08 +0000 | [diff] [blame] | 2060 | printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x%08x prot=%x idx=%d smmu=%d pd=0x%08lx\n", |
| 2061 | vaddr, (int)paddr, prot, mmu_idx, is_softmmu, pd); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2062 | #endif |
| 2063 | |
| 2064 | ret = 0; |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2065 | address = vaddr; |
| 2066 | if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) { |
| 2067 | /* IO memory case (romd handled later) */ |
| 2068 | address |= TLB_MMIO; |
| 2069 | } |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2070 | addend = (unsigned long)qemu_get_ram_ptr(pd & TARGET_PAGE_MASK); |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2071 | if ((pd & ~TARGET_PAGE_MASK) <= IO_MEM_ROM) { |
| 2072 | /* Normal RAM. */ |
| 2073 | iotlb = pd & TARGET_PAGE_MASK; |
| 2074 | if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM) |
| 2075 | iotlb |= IO_MEM_NOTDIRTY; |
| 2076 | else |
| 2077 | iotlb |= IO_MEM_ROM; |
| 2078 | } else { |
Stuart Brady | ccbb4d4 | 2009-05-03 12:15:06 +0100 | [diff] [blame] | 2079 | /* IO handlers are currently passed a physical address. |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2080 | It would be nice to pass an offset from the base address |
| 2081 | of that region. This would avoid having to special case RAM, |
| 2082 | and avoid full address decoding in every device. |
| 2083 | We can't use the high bits of pd for this because |
| 2084 | IO_MEM_ROMD uses these as a ram address. */ |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2085 | iotlb = (pd & ~TARGET_PAGE_MASK); |
| 2086 | if (p) { |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2087 | iotlb += p->region_offset; |
| 2088 | } else { |
| 2089 | iotlb += paddr; |
| 2090 | } |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2091 | } |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2092 | |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2093 | code_address = address; |
| 2094 | /* Make accesses to pages with watchpoints go via the |
| 2095 | watchpoint trap routines. */ |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 2096 | QTAILQ_FOREACH(wp, &env->watchpoints, entry) { |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 2097 | if (vaddr == (wp->vaddr & TARGET_PAGE_MASK)) { |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2098 | iotlb = io_mem_watch + paddr; |
| 2099 | /* TODO: The memory case can be optimized by not trapping |
| 2100 | reads of pages with a write breakpoint. */ |
| 2101 | address |= TLB_MMIO; |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2102 | } |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2103 | } |
balrog | d79acba | 2007-06-26 20:01:13 +0000 | [diff] [blame] | 2104 | |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2105 | index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1); |
| 2106 | env->iotlb[mmu_idx][index] = iotlb - vaddr; |
| 2107 | te = &env->tlb_table[mmu_idx][index]; |
| 2108 | te->addend = addend - vaddr; |
| 2109 | if (prot & PAGE_READ) { |
| 2110 | te->addr_read = address; |
| 2111 | } else { |
| 2112 | te->addr_read = -1; |
| 2113 | } |
edgar_igl | 5c751e9 | 2008-05-06 08:44:21 +0000 | [diff] [blame] | 2114 | |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2115 | if (prot & PAGE_EXEC) { |
| 2116 | te->addr_code = code_address; |
| 2117 | } else { |
| 2118 | te->addr_code = -1; |
| 2119 | } |
| 2120 | if (prot & PAGE_WRITE) { |
| 2121 | if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM || |
| 2122 | (pd & IO_MEM_ROMD)) { |
| 2123 | /* Write access calls the I/O callback. */ |
| 2124 | te->addr_write = address | TLB_MMIO; |
| 2125 | } else if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM && |
| 2126 | !cpu_physical_memory_is_dirty(pd)) { |
| 2127 | te->addr_write = address | TLB_NOTDIRTY; |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 2128 | } else { |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2129 | te->addr_write = address; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2130 | } |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2131 | } else { |
| 2132 | te->addr_write = -1; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2133 | } |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2134 | return ret; |
| 2135 | } |
| 2136 | |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 2137 | #else |
| 2138 | |
bellard | ee8b702 | 2004-02-03 23:35:10 +0000 | [diff] [blame] | 2139 | void tlb_flush(CPUState *env, int flush_global) |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 2140 | { |
| 2141 | } |
| 2142 | |
bellard | 2e12669 | 2004-04-25 21:28:44 +0000 | [diff] [blame] | 2143 | void tlb_flush_page(CPUState *env, target_ulong addr) |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 2144 | { |
| 2145 | } |
| 2146 | |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 2147 | /* |
| 2148 | * Walks guest process memory "regions" one by one |
| 2149 | * and calls callback function 'fn' for each region. |
| 2150 | */ |
| 2151 | int walk_memory_regions(void *priv, |
| 2152 | int (*fn)(void *, unsigned long, unsigned long, unsigned long)) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2153 | { |
| 2154 | unsigned long start, end; |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 2155 | PageDesc *p = NULL; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2156 | int i, j, prot, prot1; |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 2157 | int rc = 0; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2158 | |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 2159 | start = end = -1; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2160 | prot = 0; |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 2161 | |
| 2162 | for (i = 0; i <= L1_SIZE; i++) { |
| 2163 | p = (i < L1_SIZE) ? l1_map[i] : NULL; |
| 2164 | for (j = 0; j < L2_SIZE; j++) { |
| 2165 | prot1 = (p == NULL) ? 0 : p[j].flags; |
| 2166 | /* |
| 2167 | * "region" is one continuous chunk of memory |
| 2168 | * that has same protection flags set. |
| 2169 | */ |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2170 | if (prot1 != prot) { |
| 2171 | end = (i << (32 - L1_BITS)) | (j << TARGET_PAGE_BITS); |
| 2172 | if (start != -1) { |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 2173 | rc = (*fn)(priv, start, end, prot); |
| 2174 | /* callback can stop iteration by returning != 0 */ |
| 2175 | if (rc != 0) |
| 2176 | return (rc); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2177 | } |
| 2178 | if (prot1 != 0) |
| 2179 | start = end; |
| 2180 | else |
| 2181 | start = -1; |
| 2182 | prot = prot1; |
| 2183 | } |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 2184 | if (p == NULL) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2185 | break; |
| 2186 | } |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2187 | } |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 2188 | return (rc); |
| 2189 | } |
| 2190 | |
| 2191 | static int dump_region(void *priv, unsigned long start, |
| 2192 | unsigned long end, unsigned long prot) |
| 2193 | { |
| 2194 | FILE *f = (FILE *)priv; |
| 2195 | |
| 2196 | (void) fprintf(f, "%08lx-%08lx %08lx %c%c%c\n", |
| 2197 | start, end, end - start, |
| 2198 | ((prot & PAGE_READ) ? 'r' : '-'), |
| 2199 | ((prot & PAGE_WRITE) ? 'w' : '-'), |
| 2200 | ((prot & PAGE_EXEC) ? 'x' : '-')); |
| 2201 | |
| 2202 | return (0); |
| 2203 | } |
| 2204 | |
| 2205 | /* dump memory mappings */ |
| 2206 | void page_dump(FILE *f) |
| 2207 | { |
| 2208 | (void) fprintf(f, "%-8s %-8s %-8s %s\n", |
| 2209 | "start", "end", "size", "prot"); |
| 2210 | walk_memory_regions(f, dump_region); |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2211 | } |
| 2212 | |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2213 | int page_get_flags(target_ulong address) |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2214 | { |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2215 | PageDesc *p; |
| 2216 | |
| 2217 | p = page_find(address >> TARGET_PAGE_BITS); |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2218 | if (!p) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2219 | return 0; |
| 2220 | return p->flags; |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2223 | /* modify the flags of a page and invalidate the code if |
Stuart Brady | ccbb4d4 | 2009-05-03 12:15:06 +0100 | [diff] [blame] | 2224 | necessary. The flag PAGE_WRITE_ORG is positioned automatically |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2225 | depending on PAGE_WRITE */ |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2226 | void page_set_flags(target_ulong start, target_ulong end, int flags) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2227 | { |
| 2228 | PageDesc *p; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2229 | target_ulong addr; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2230 | |
pbrook | c8a706f | 2008-06-02 16:16:42 +0000 | [diff] [blame] | 2231 | /* mmap_lock should already be held. */ |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2232 | start = start & TARGET_PAGE_MASK; |
| 2233 | end = TARGET_PAGE_ALIGN(end); |
| 2234 | if (flags & PAGE_WRITE) |
| 2235 | flags |= PAGE_WRITE_ORG; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2236 | for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) { |
| 2237 | p = page_find_alloc(addr >> TARGET_PAGE_BITS); |
pbrook | 17e2377 | 2008-06-09 13:47:45 +0000 | [diff] [blame] | 2238 | /* We may be called for host regions that are outside guest |
| 2239 | address space. */ |
| 2240 | if (!p) |
| 2241 | return; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2242 | /* if the write protection is set, then we invalidate the code |
| 2243 | inside */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2244 | if (!(p->flags & PAGE_WRITE) && |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2245 | (flags & PAGE_WRITE) && |
| 2246 | p->first_tb) { |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 2247 | tb_invalidate_phys_page(addr, 0, NULL); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2248 | } |
| 2249 | p->flags = flags; |
| 2250 | } |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2251 | } |
| 2252 | |
ths | 3d97b40 | 2007-11-02 19:02:07 +0000 | [diff] [blame] | 2253 | int page_check_range(target_ulong start, target_ulong len, int flags) |
| 2254 | { |
| 2255 | PageDesc *p; |
| 2256 | target_ulong end; |
| 2257 | target_ulong addr; |
| 2258 | |
balrog | 55f280c | 2008-10-28 10:24:11 +0000 | [diff] [blame] | 2259 | if (start + len < start) |
| 2260 | /* we've wrapped around */ |
| 2261 | return -1; |
| 2262 | |
ths | 3d97b40 | 2007-11-02 19:02:07 +0000 | [diff] [blame] | 2263 | end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */ |
| 2264 | start = start & TARGET_PAGE_MASK; |
| 2265 | |
ths | 3d97b40 | 2007-11-02 19:02:07 +0000 | [diff] [blame] | 2266 | for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) { |
| 2267 | p = page_find(addr >> TARGET_PAGE_BITS); |
| 2268 | if( !p ) |
| 2269 | return -1; |
| 2270 | if( !(p->flags & PAGE_VALID) ) |
| 2271 | return -1; |
| 2272 | |
bellard | dae3270 | 2007-11-14 10:51:00 +0000 | [diff] [blame] | 2273 | if ((flags & PAGE_READ) && !(p->flags & PAGE_READ)) |
ths | 3d97b40 | 2007-11-02 19:02:07 +0000 | [diff] [blame] | 2274 | return -1; |
bellard | dae3270 | 2007-11-14 10:51:00 +0000 | [diff] [blame] | 2275 | if (flags & PAGE_WRITE) { |
| 2276 | if (!(p->flags & PAGE_WRITE_ORG)) |
| 2277 | return -1; |
| 2278 | /* unprotect the page if it was put read-only because it |
| 2279 | contains translated code */ |
| 2280 | if (!(p->flags & PAGE_WRITE)) { |
| 2281 | if (!page_unprotect(addr, 0, NULL)) |
| 2282 | return -1; |
| 2283 | } |
| 2284 | return 0; |
| 2285 | } |
ths | 3d97b40 | 2007-11-02 19:02:07 +0000 | [diff] [blame] | 2286 | } |
| 2287 | return 0; |
| 2288 | } |
| 2289 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2290 | /* called from signal handler: invalidate the code and unprotect the |
Stuart Brady | ccbb4d4 | 2009-05-03 12:15:06 +0100 | [diff] [blame] | 2291 | page. Return TRUE if the fault was successfully handled. */ |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2292 | int page_unprotect(target_ulong address, unsigned long pc, void *puc) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2293 | { |
| 2294 | unsigned int page_index, prot, pindex; |
| 2295 | PageDesc *p, *p1; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2296 | target_ulong host_start, host_end, addr; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2297 | |
pbrook | c8a706f | 2008-06-02 16:16:42 +0000 | [diff] [blame] | 2298 | /* Technically this isn't safe inside a signal handler. However we |
| 2299 | know this only ever happens in a synchronous SEGV handler, so in |
| 2300 | practice it seems to be ok. */ |
| 2301 | mmap_lock(); |
| 2302 | |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 2303 | host_start = address & qemu_host_page_mask; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2304 | page_index = host_start >> TARGET_PAGE_BITS; |
| 2305 | p1 = page_find(page_index); |
pbrook | c8a706f | 2008-06-02 16:16:42 +0000 | [diff] [blame] | 2306 | if (!p1) { |
| 2307 | mmap_unlock(); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2308 | return 0; |
pbrook | c8a706f | 2008-06-02 16:16:42 +0000 | [diff] [blame] | 2309 | } |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 2310 | host_end = host_start + qemu_host_page_size; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2311 | p = p1; |
| 2312 | prot = 0; |
| 2313 | for(addr = host_start;addr < host_end; addr += TARGET_PAGE_SIZE) { |
| 2314 | prot |= p->flags; |
| 2315 | p++; |
| 2316 | } |
| 2317 | /* if the page was really writable, then we change its |
| 2318 | protection back to writable */ |
| 2319 | if (prot & PAGE_WRITE_ORG) { |
| 2320 | pindex = (address - host_start) >> TARGET_PAGE_BITS; |
| 2321 | if (!(p1[pindex].flags & PAGE_WRITE)) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2322 | mprotect((void *)g2h(host_start), qemu_host_page_size, |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2323 | (prot & PAGE_BITS) | PAGE_WRITE); |
| 2324 | p1[pindex].flags |= PAGE_WRITE; |
| 2325 | /* and since the content will be modified, we must invalidate |
| 2326 | the corresponding translated code. */ |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 2327 | tb_invalidate_phys_page(address, pc, puc); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2328 | #ifdef DEBUG_TB_CHECK |
| 2329 | tb_invalidate_check(address); |
| 2330 | #endif |
pbrook | c8a706f | 2008-06-02 16:16:42 +0000 | [diff] [blame] | 2331 | mmap_unlock(); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2332 | return 1; |
| 2333 | } |
| 2334 | } |
pbrook | c8a706f | 2008-06-02 16:16:42 +0000 | [diff] [blame] | 2335 | mmap_unlock(); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2336 | return 0; |
| 2337 | } |
| 2338 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 2339 | static inline void tlb_set_dirty(CPUState *env, |
| 2340 | unsigned long addr, target_ulong vaddr) |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2341 | { |
| 2342 | } |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2343 | #endif /* defined(CONFIG_USER_ONLY) */ |
| 2344 | |
pbrook | e2eef17 | 2008-06-08 01:09:01 +0000 | [diff] [blame] | 2345 | #if !defined(CONFIG_USER_ONLY) |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2346 | |
Paul Brook | c04b2b7 | 2010-03-01 03:31:14 +0000 | [diff] [blame] | 2347 | #define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK) |
| 2348 | typedef struct subpage_t { |
| 2349 | target_phys_addr_t base; |
| 2350 | CPUReadMemoryFunc * const *mem_read[TARGET_PAGE_SIZE][4]; |
| 2351 | CPUWriteMemoryFunc * const *mem_write[TARGET_PAGE_SIZE][4]; |
| 2352 | void *opaque[TARGET_PAGE_SIZE][2][4]; |
| 2353 | ram_addr_t region_offset[TARGET_PAGE_SIZE][2][4]; |
| 2354 | } subpage_t; |
| 2355 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2356 | static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end, |
| 2357 | ram_addr_t memory, ram_addr_t region_offset); |
| 2358 | static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys, |
| 2359 | ram_addr_t orig_memory, ram_addr_t region_offset); |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2360 | #define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \ |
| 2361 | need_subpage) \ |
| 2362 | do { \ |
| 2363 | if (addr > start_addr) \ |
| 2364 | start_addr2 = 0; \ |
| 2365 | else { \ |
| 2366 | start_addr2 = start_addr & ~TARGET_PAGE_MASK; \ |
| 2367 | if (start_addr2 > 0) \ |
| 2368 | need_subpage = 1; \ |
| 2369 | } \ |
| 2370 | \ |
blueswir1 | 49e9fba | 2007-05-30 17:25:06 +0000 | [diff] [blame] | 2371 | if ((start_addr + orig_size) - addr >= TARGET_PAGE_SIZE) \ |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2372 | end_addr2 = TARGET_PAGE_SIZE - 1; \ |
| 2373 | else { \ |
| 2374 | end_addr2 = (start_addr + orig_size - 1) & ~TARGET_PAGE_MASK; \ |
| 2375 | if (end_addr2 < TARGET_PAGE_SIZE - 1) \ |
| 2376 | need_subpage = 1; \ |
| 2377 | } \ |
| 2378 | } while (0) |
| 2379 | |
Michael S. Tsirkin | 8f2498f | 2009-09-29 18:53:16 +0200 | [diff] [blame] | 2380 | /* register physical memory. |
| 2381 | For RAM, 'size' must be a multiple of the target page size. |
| 2382 | If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2383 | io memory page. The address used when calling the IO function is |
| 2384 | the offset from the start of the region, plus region_offset. Both |
Stuart Brady | ccbb4d4 | 2009-05-03 12:15:06 +0100 | [diff] [blame] | 2385 | start_addr and region_offset are rounded down to a page boundary |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2386 | before calculating this offset. This should not be a problem unless |
| 2387 | the low bits of start_addr and region_offset differ. */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2388 | void cpu_register_physical_memory_offset(target_phys_addr_t start_addr, |
| 2389 | ram_addr_t size, |
| 2390 | ram_addr_t phys_offset, |
| 2391 | ram_addr_t region_offset) |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2392 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2393 | target_phys_addr_t addr, end_addr; |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 2394 | PhysPageDesc *p; |
bellard | 9d42037 | 2006-06-25 22:25:22 +0000 | [diff] [blame] | 2395 | CPUState *env; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2396 | ram_addr_t orig_size = size; |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2397 | void *subpage; |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2398 | |
Michael S. Tsirkin | f6f3fbc | 2010-01-27 22:06:57 +0200 | [diff] [blame] | 2399 | cpu_notify_set_memory(start_addr, size, phys_offset); |
| 2400 | |
pbrook | 67c4d23 | 2009-02-23 13:16:07 +0000 | [diff] [blame] | 2401 | if (phys_offset == IO_MEM_UNASSIGNED) { |
| 2402 | region_offset = start_addr; |
| 2403 | } |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2404 | region_offset &= TARGET_PAGE_MASK; |
bellard | 5fd386f | 2004-05-23 21:11:22 +0000 | [diff] [blame] | 2405 | size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2406 | end_addr = start_addr + (target_phys_addr_t)size; |
blueswir1 | 49e9fba | 2007-05-30 17:25:06 +0000 | [diff] [blame] | 2407 | for(addr = start_addr; addr != end_addr; addr += TARGET_PAGE_SIZE) { |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2408 | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
| 2409 | if (p && p->phys_offset != IO_MEM_UNASSIGNED) { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2410 | ram_addr_t orig_memory = p->phys_offset; |
| 2411 | target_phys_addr_t start_addr2, end_addr2; |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2412 | int need_subpage = 0; |
| 2413 | |
| 2414 | CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, |
| 2415 | need_subpage); |
blueswir1 | 4254fab | 2008-01-01 16:57:19 +0000 | [diff] [blame] | 2416 | if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) { |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2417 | if (!(orig_memory & IO_MEM_SUBPAGE)) { |
| 2418 | subpage = subpage_init((addr & TARGET_PAGE_MASK), |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2419 | &p->phys_offset, orig_memory, |
| 2420 | p->region_offset); |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2421 | } else { |
| 2422 | subpage = io_mem_opaque[(orig_memory & ~TARGET_PAGE_MASK) |
| 2423 | >> IO_MEM_SHIFT]; |
| 2424 | } |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2425 | subpage_register(subpage, start_addr2, end_addr2, phys_offset, |
| 2426 | region_offset); |
| 2427 | p->region_offset = 0; |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2428 | } else { |
| 2429 | p->phys_offset = phys_offset; |
| 2430 | if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM || |
| 2431 | (phys_offset & IO_MEM_ROMD)) |
| 2432 | phys_offset += TARGET_PAGE_SIZE; |
| 2433 | } |
| 2434 | } else { |
| 2435 | p = phys_page_find_alloc(addr >> TARGET_PAGE_BITS, 1); |
| 2436 | p->phys_offset = phys_offset; |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2437 | p->region_offset = region_offset; |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2438 | if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM || |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2439 | (phys_offset & IO_MEM_ROMD)) { |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2440 | phys_offset += TARGET_PAGE_SIZE; |
pbrook | 0e8f096 | 2008-12-02 09:02:15 +0000 | [diff] [blame] | 2441 | } else { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2442 | target_phys_addr_t start_addr2, end_addr2; |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2443 | int need_subpage = 0; |
| 2444 | |
| 2445 | CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, |
| 2446 | end_addr2, need_subpage); |
| 2447 | |
blueswir1 | 4254fab | 2008-01-01 16:57:19 +0000 | [diff] [blame] | 2448 | if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) { |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2449 | subpage = subpage_init((addr & TARGET_PAGE_MASK), |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2450 | &p->phys_offset, IO_MEM_UNASSIGNED, |
pbrook | 67c4d23 | 2009-02-23 13:16:07 +0000 | [diff] [blame] | 2451 | addr & TARGET_PAGE_MASK); |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2452 | subpage_register(subpage, start_addr2, end_addr2, |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2453 | phys_offset, region_offset); |
| 2454 | p->region_offset = 0; |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2455 | } |
| 2456 | } |
| 2457 | } |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2458 | region_offset += TARGET_PAGE_SIZE; |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2459 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2460 | |
bellard | 9d42037 | 2006-06-25 22:25:22 +0000 | [diff] [blame] | 2461 | /* since each CPU stores ram addresses in its TLB cache, we must |
| 2462 | reset the modified entries */ |
| 2463 | /* XXX: slow ! */ |
| 2464 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
| 2465 | tlb_flush(env, 1); |
| 2466 | } |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2467 | } |
| 2468 | |
bellard | ba86345 | 2006-09-24 18:41:10 +0000 | [diff] [blame] | 2469 | /* XXX: temporary until new memory mapping API */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2470 | ram_addr_t cpu_get_physical_page_desc(target_phys_addr_t addr) |
bellard | ba86345 | 2006-09-24 18:41:10 +0000 | [diff] [blame] | 2471 | { |
| 2472 | PhysPageDesc *p; |
| 2473 | |
| 2474 | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
| 2475 | if (!p) |
| 2476 | return IO_MEM_UNASSIGNED; |
| 2477 | return p->phys_offset; |
| 2478 | } |
| 2479 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2480 | void qemu_register_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size) |
aliguori | f65ed4c | 2008-12-09 20:09:57 +0000 | [diff] [blame] | 2481 | { |
| 2482 | if (kvm_enabled()) |
| 2483 | kvm_coalesce_mmio_region(addr, size); |
| 2484 | } |
| 2485 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2486 | void qemu_unregister_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size) |
aliguori | f65ed4c | 2008-12-09 20:09:57 +0000 | [diff] [blame] | 2487 | { |
| 2488 | if (kvm_enabled()) |
| 2489 | kvm_uncoalesce_mmio_region(addr, size); |
| 2490 | } |
| 2491 | |
Sheng Yang | 62a2744 | 2010-01-26 19:21:16 +0800 | [diff] [blame] | 2492 | void qemu_flush_coalesced_mmio_buffer(void) |
| 2493 | { |
| 2494 | if (kvm_enabled()) |
| 2495 | kvm_flush_coalesced_mmio_buffer(); |
| 2496 | } |
| 2497 | |
Marcelo Tosatti | c902760 | 2010-03-01 20:25:08 -0300 | [diff] [blame] | 2498 | #if defined(__linux__) && !defined(TARGET_S390X) |
| 2499 | |
| 2500 | #include <sys/vfs.h> |
| 2501 | |
| 2502 | #define HUGETLBFS_MAGIC 0x958458f6 |
| 2503 | |
| 2504 | static long gethugepagesize(const char *path) |
| 2505 | { |
| 2506 | struct statfs fs; |
| 2507 | int ret; |
| 2508 | |
| 2509 | do { |
| 2510 | ret = statfs(path, &fs); |
| 2511 | } while (ret != 0 && errno == EINTR); |
| 2512 | |
| 2513 | if (ret != 0) { |
| 2514 | perror("statfs"); |
| 2515 | return 0; |
| 2516 | } |
| 2517 | |
| 2518 | if (fs.f_type != HUGETLBFS_MAGIC) |
| 2519 | fprintf(stderr, "Warning: path not on HugeTLBFS: %s\n", path); |
| 2520 | |
| 2521 | return fs.f_bsize; |
| 2522 | } |
| 2523 | |
| 2524 | static void *file_ram_alloc(ram_addr_t memory, const char *path) |
| 2525 | { |
| 2526 | char *filename; |
| 2527 | void *area; |
| 2528 | int fd; |
| 2529 | #ifdef MAP_POPULATE |
| 2530 | int flags; |
| 2531 | #endif |
| 2532 | unsigned long hpagesize; |
| 2533 | |
| 2534 | hpagesize = gethugepagesize(path); |
| 2535 | if (!hpagesize) { |
| 2536 | return NULL; |
| 2537 | } |
| 2538 | |
| 2539 | if (memory < hpagesize) { |
| 2540 | return NULL; |
| 2541 | } |
| 2542 | |
| 2543 | if (kvm_enabled() && !kvm_has_sync_mmu()) { |
| 2544 | fprintf(stderr, "host lacks kvm mmu notifiers, -mem-path unsupported\n"); |
| 2545 | return NULL; |
| 2546 | } |
| 2547 | |
| 2548 | if (asprintf(&filename, "%s/qemu_back_mem.XXXXXX", path) == -1) { |
| 2549 | return NULL; |
| 2550 | } |
| 2551 | |
| 2552 | fd = mkstemp(filename); |
| 2553 | if (fd < 0) { |
| 2554 | perror("mkstemp"); |
| 2555 | free(filename); |
| 2556 | return NULL; |
| 2557 | } |
| 2558 | unlink(filename); |
| 2559 | free(filename); |
| 2560 | |
| 2561 | memory = (memory+hpagesize-1) & ~(hpagesize-1); |
| 2562 | |
| 2563 | /* |
| 2564 | * ftruncate is not supported by hugetlbfs in older |
| 2565 | * hosts, so don't bother bailing out on errors. |
| 2566 | * If anything goes wrong with it under other filesystems, |
| 2567 | * mmap will fail. |
| 2568 | */ |
| 2569 | if (ftruncate(fd, memory)) |
| 2570 | perror("ftruncate"); |
| 2571 | |
| 2572 | #ifdef MAP_POPULATE |
| 2573 | /* NB: MAP_POPULATE won't exhaustively alloc all phys pages in the case |
| 2574 | * MAP_PRIVATE is requested. For mem_prealloc we mmap as MAP_SHARED |
| 2575 | * to sidestep this quirk. |
| 2576 | */ |
| 2577 | flags = mem_prealloc ? MAP_POPULATE | MAP_SHARED : MAP_PRIVATE; |
| 2578 | area = mmap(0, memory, PROT_READ | PROT_WRITE, flags, fd, 0); |
| 2579 | #else |
| 2580 | area = mmap(0, memory, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
| 2581 | #endif |
| 2582 | if (area == MAP_FAILED) { |
| 2583 | perror("file_ram_alloc: can't mmap RAM pages"); |
| 2584 | close(fd); |
| 2585 | return (NULL); |
| 2586 | } |
| 2587 | return area; |
| 2588 | } |
| 2589 | #endif |
| 2590 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2591 | ram_addr_t qemu_ram_alloc(ram_addr_t size) |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2592 | { |
| 2593 | RAMBlock *new_block; |
| 2594 | |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2595 | size = TARGET_PAGE_ALIGN(size); |
| 2596 | new_block = qemu_malloc(sizeof(*new_block)); |
| 2597 | |
Marcelo Tosatti | c902760 | 2010-03-01 20:25:08 -0300 | [diff] [blame] | 2598 | if (mem_path) { |
| 2599 | #if defined (__linux__) && !defined(TARGET_S390X) |
| 2600 | new_block->host = file_ram_alloc(size, mem_path); |
| 2601 | if (!new_block->host) |
| 2602 | exit(1); |
Alexander Graf | 6b02494 | 2009-12-05 12:44:25 +0100 | [diff] [blame] | 2603 | #else |
Marcelo Tosatti | c902760 | 2010-03-01 20:25:08 -0300 | [diff] [blame] | 2604 | fprintf(stderr, "-mem-path option unsupported\n"); |
| 2605 | exit(1); |
| 2606 | #endif |
| 2607 | } else { |
| 2608 | #if defined(TARGET_S390X) && defined(CONFIG_KVM) |
| 2609 | /* XXX S390 KVM requires the topmost vma of the RAM to be < 256GB */ |
| 2610 | new_block->host = mmap((void*)0x1000000, size, |
| 2611 | PROT_EXEC|PROT_READ|PROT_WRITE, |
| 2612 | MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
| 2613 | #else |
| 2614 | new_block->host = qemu_vmalloc(size); |
Alexander Graf | 6b02494 | 2009-12-05 12:44:25 +0100 | [diff] [blame] | 2615 | #endif |
Izik Eidus | ccb167e | 2009-10-08 16:39:39 +0200 | [diff] [blame] | 2616 | #ifdef MADV_MERGEABLE |
Marcelo Tosatti | c902760 | 2010-03-01 20:25:08 -0300 | [diff] [blame] | 2617 | madvise(new_block->host, size, MADV_MERGEABLE); |
Izik Eidus | ccb167e | 2009-10-08 16:39:39 +0200 | [diff] [blame] | 2618 | #endif |
Marcelo Tosatti | c902760 | 2010-03-01 20:25:08 -0300 | [diff] [blame] | 2619 | } |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2620 | new_block->offset = last_ram_offset; |
| 2621 | new_block->length = size; |
| 2622 | |
| 2623 | new_block->next = ram_blocks; |
| 2624 | ram_blocks = new_block; |
| 2625 | |
| 2626 | phys_ram_dirty = qemu_realloc(phys_ram_dirty, |
| 2627 | (last_ram_offset + size) >> TARGET_PAGE_BITS); |
| 2628 | memset(phys_ram_dirty + (last_ram_offset >> TARGET_PAGE_BITS), |
| 2629 | 0xff, size >> TARGET_PAGE_BITS); |
| 2630 | |
| 2631 | last_ram_offset += size; |
| 2632 | |
Jan Kiszka | 6f0437e | 2009-04-26 18:03:40 +0200 | [diff] [blame] | 2633 | if (kvm_enabled()) |
| 2634 | kvm_setup_guest_memory(new_block->host, size); |
| 2635 | |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2636 | return new_block->offset; |
| 2637 | } |
bellard | e9a1ab1 | 2007-02-08 23:08:38 +0000 | [diff] [blame] | 2638 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2639 | void qemu_ram_free(ram_addr_t addr) |
bellard | e9a1ab1 | 2007-02-08 23:08:38 +0000 | [diff] [blame] | 2640 | { |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2641 | /* TODO: implement this. */ |
bellard | e9a1ab1 | 2007-02-08 23:08:38 +0000 | [diff] [blame] | 2642 | } |
| 2643 | |
pbrook | dc828ca | 2009-04-09 22:21:07 +0000 | [diff] [blame] | 2644 | /* Return a host pointer to ram allocated with qemu_ram_alloc. |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2645 | With the exception of the softmmu code in this file, this should |
| 2646 | only be used for local memory (e.g. video ram) that the device owns, |
| 2647 | and knows it isn't going to access beyond the end of the block. |
| 2648 | |
| 2649 | It should not be used for general purpose DMA. |
| 2650 | Use cpu_physical_memory_map/cpu_physical_memory_rw instead. |
| 2651 | */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2652 | void *qemu_get_ram_ptr(ram_addr_t addr) |
pbrook | dc828ca | 2009-04-09 22:21:07 +0000 | [diff] [blame] | 2653 | { |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2654 | RAMBlock *prev; |
| 2655 | RAMBlock **prevp; |
| 2656 | RAMBlock *block; |
| 2657 | |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2658 | prev = NULL; |
| 2659 | prevp = &ram_blocks; |
| 2660 | block = ram_blocks; |
| 2661 | while (block && (block->offset > addr |
| 2662 | || block->offset + block->length <= addr)) { |
| 2663 | if (prev) |
| 2664 | prevp = &prev->next; |
| 2665 | prev = block; |
| 2666 | block = block->next; |
| 2667 | } |
| 2668 | if (!block) { |
| 2669 | fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr); |
| 2670 | abort(); |
| 2671 | } |
| 2672 | /* Move this entry to to start of the list. */ |
| 2673 | if (prev) { |
| 2674 | prev->next = block->next; |
| 2675 | block->next = *prevp; |
| 2676 | *prevp = block; |
| 2677 | } |
| 2678 | return block->host + (addr - block->offset); |
pbrook | dc828ca | 2009-04-09 22:21:07 +0000 | [diff] [blame] | 2679 | } |
| 2680 | |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2681 | /* Some of the softmmu routines need to translate from a host pointer |
| 2682 | (typically a TLB entry) back to a ram offset. */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2683 | ram_addr_t qemu_ram_addr_from_host(void *ptr) |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2684 | { |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2685 | RAMBlock *prev; |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2686 | RAMBlock *block; |
| 2687 | uint8_t *host = ptr; |
| 2688 | |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2689 | prev = NULL; |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2690 | block = ram_blocks; |
| 2691 | while (block && (block->host > host |
| 2692 | || block->host + block->length <= host)) { |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2693 | prev = block; |
| 2694 | block = block->next; |
| 2695 | } |
| 2696 | if (!block) { |
| 2697 | fprintf(stderr, "Bad ram pointer %p\n", ptr); |
| 2698 | abort(); |
| 2699 | } |
| 2700 | return block->offset + (host - block->host); |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2701 | } |
| 2702 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2703 | static uint32_t unassigned_mem_readb(void *opaque, target_phys_addr_t addr) |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2704 | { |
pbrook | 67d3b95 | 2006-12-18 05:03:52 +0000 | [diff] [blame] | 2705 | #ifdef DEBUG_UNASSIGNED |
blueswir1 | ab3d172 | 2007-11-04 07:31:40 +0000 | [diff] [blame] | 2706 | printf("Unassigned mem read " TARGET_FMT_plx "\n", addr); |
pbrook | 67d3b95 | 2006-12-18 05:03:52 +0000 | [diff] [blame] | 2707 | #endif |
Edgar E. Iglesias | faed1c2 | 2009-09-03 13:25:09 +0200 | [diff] [blame] | 2708 | #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE) |
blueswir1 | e18231a | 2008-10-06 18:46:28 +0000 | [diff] [blame] | 2709 | do_unassigned_access(addr, 0, 0, 0, 1); |
| 2710 | #endif |
| 2711 | return 0; |
| 2712 | } |
| 2713 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2714 | static uint32_t unassigned_mem_readw(void *opaque, target_phys_addr_t addr) |
blueswir1 | e18231a | 2008-10-06 18:46:28 +0000 | [diff] [blame] | 2715 | { |
| 2716 | #ifdef DEBUG_UNASSIGNED |
| 2717 | printf("Unassigned mem read " TARGET_FMT_plx "\n", addr); |
| 2718 | #endif |
Edgar E. Iglesias | faed1c2 | 2009-09-03 13:25:09 +0200 | [diff] [blame] | 2719 | #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE) |
blueswir1 | e18231a | 2008-10-06 18:46:28 +0000 | [diff] [blame] | 2720 | do_unassigned_access(addr, 0, 0, 0, 2); |
| 2721 | #endif |
| 2722 | return 0; |
| 2723 | } |
| 2724 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2725 | static uint32_t unassigned_mem_readl(void *opaque, target_phys_addr_t addr) |
blueswir1 | e18231a | 2008-10-06 18:46:28 +0000 | [diff] [blame] | 2726 | { |
| 2727 | #ifdef DEBUG_UNASSIGNED |
| 2728 | printf("Unassigned mem read " TARGET_FMT_plx "\n", addr); |
| 2729 | #endif |
Edgar E. Iglesias | faed1c2 | 2009-09-03 13:25:09 +0200 | [diff] [blame] | 2730 | #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE) |
blueswir1 | e18231a | 2008-10-06 18:46:28 +0000 | [diff] [blame] | 2731 | do_unassigned_access(addr, 0, 0, 0, 4); |
blueswir1 | b4f0a31 | 2007-05-06 17:59:24 +0000 | [diff] [blame] | 2732 | #endif |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2733 | return 0; |
| 2734 | } |
| 2735 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2736 | 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] | 2737 | { |
pbrook | 67d3b95 | 2006-12-18 05:03:52 +0000 | [diff] [blame] | 2738 | #ifdef DEBUG_UNASSIGNED |
blueswir1 | ab3d172 | 2007-11-04 07:31:40 +0000 | [diff] [blame] | 2739 | printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val); |
pbrook | 67d3b95 | 2006-12-18 05:03:52 +0000 | [diff] [blame] | 2740 | #endif |
Edgar E. Iglesias | faed1c2 | 2009-09-03 13:25:09 +0200 | [diff] [blame] | 2741 | #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE) |
blueswir1 | e18231a | 2008-10-06 18:46:28 +0000 | [diff] [blame] | 2742 | do_unassigned_access(addr, 1, 0, 0, 1); |
| 2743 | #endif |
| 2744 | } |
| 2745 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2746 | static void unassigned_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val) |
blueswir1 | e18231a | 2008-10-06 18:46:28 +0000 | [diff] [blame] | 2747 | { |
| 2748 | #ifdef DEBUG_UNASSIGNED |
| 2749 | printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val); |
| 2750 | #endif |
Edgar E. Iglesias | faed1c2 | 2009-09-03 13:25:09 +0200 | [diff] [blame] | 2751 | #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE) |
blueswir1 | e18231a | 2008-10-06 18:46:28 +0000 | [diff] [blame] | 2752 | do_unassigned_access(addr, 1, 0, 0, 2); |
| 2753 | #endif |
| 2754 | } |
| 2755 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2756 | static void unassigned_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val) |
blueswir1 | e18231a | 2008-10-06 18:46:28 +0000 | [diff] [blame] | 2757 | { |
| 2758 | #ifdef DEBUG_UNASSIGNED |
| 2759 | printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val); |
| 2760 | #endif |
Edgar E. Iglesias | faed1c2 | 2009-09-03 13:25:09 +0200 | [diff] [blame] | 2761 | #if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE) |
blueswir1 | e18231a | 2008-10-06 18:46:28 +0000 | [diff] [blame] | 2762 | do_unassigned_access(addr, 1, 0, 0, 4); |
blueswir1 | b4f0a31 | 2007-05-06 17:59:24 +0000 | [diff] [blame] | 2763 | #endif |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2764 | } |
| 2765 | |
Blue Swirl | d60efc6 | 2009-08-25 18:29:31 +0000 | [diff] [blame] | 2766 | static CPUReadMemoryFunc * const unassigned_mem_read[3] = { |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2767 | unassigned_mem_readb, |
blueswir1 | e18231a | 2008-10-06 18:46:28 +0000 | [diff] [blame] | 2768 | unassigned_mem_readw, |
| 2769 | unassigned_mem_readl, |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2770 | }; |
| 2771 | |
Blue Swirl | d60efc6 | 2009-08-25 18:29:31 +0000 | [diff] [blame] | 2772 | static CPUWriteMemoryFunc * const unassigned_mem_write[3] = { |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2773 | unassigned_mem_writeb, |
blueswir1 | e18231a | 2008-10-06 18:46:28 +0000 | [diff] [blame] | 2774 | unassigned_mem_writew, |
| 2775 | unassigned_mem_writel, |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2776 | }; |
| 2777 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2778 | static void notdirty_mem_writeb(void *opaque, target_phys_addr_t ram_addr, |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2779 | uint32_t val) |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2780 | { |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2781 | int dirty_flags; |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2782 | dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS]; |
| 2783 | if (!(dirty_flags & CODE_DIRTY_FLAG)) { |
| 2784 | #if !defined(CONFIG_USER_ONLY) |
| 2785 | tb_invalidate_phys_page_fast(ram_addr, 1); |
| 2786 | dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS]; |
| 2787 | #endif |
| 2788 | } |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2789 | stb_p(qemu_get_ram_ptr(ram_addr), val); |
bellard | f23db16 | 2005-08-21 19:12:28 +0000 | [diff] [blame] | 2790 | dirty_flags |= (0xff & ~CODE_DIRTY_FLAG); |
| 2791 | phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags; |
| 2792 | /* we remove the notdirty callback only if the code has been |
| 2793 | flushed */ |
| 2794 | if (dirty_flags == 0xff) |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 2795 | tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr); |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2796 | } |
| 2797 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2798 | static void notdirty_mem_writew(void *opaque, target_phys_addr_t ram_addr, |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2799 | uint32_t val) |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2800 | { |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2801 | int dirty_flags; |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2802 | dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS]; |
| 2803 | if (!(dirty_flags & CODE_DIRTY_FLAG)) { |
| 2804 | #if !defined(CONFIG_USER_ONLY) |
| 2805 | tb_invalidate_phys_page_fast(ram_addr, 2); |
| 2806 | dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS]; |
| 2807 | #endif |
| 2808 | } |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2809 | stw_p(qemu_get_ram_ptr(ram_addr), val); |
bellard | f23db16 | 2005-08-21 19:12:28 +0000 | [diff] [blame] | 2810 | dirty_flags |= (0xff & ~CODE_DIRTY_FLAG); |
| 2811 | phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags; |
| 2812 | /* we remove the notdirty callback only if the code has been |
| 2813 | flushed */ |
| 2814 | if (dirty_flags == 0xff) |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 2815 | tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr); |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2816 | } |
| 2817 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2818 | static void notdirty_mem_writel(void *opaque, target_phys_addr_t ram_addr, |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2819 | uint32_t val) |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2820 | { |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2821 | int dirty_flags; |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2822 | dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS]; |
| 2823 | if (!(dirty_flags & CODE_DIRTY_FLAG)) { |
| 2824 | #if !defined(CONFIG_USER_ONLY) |
| 2825 | tb_invalidate_phys_page_fast(ram_addr, 4); |
| 2826 | dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS]; |
| 2827 | #endif |
| 2828 | } |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2829 | stl_p(qemu_get_ram_ptr(ram_addr), val); |
bellard | f23db16 | 2005-08-21 19:12:28 +0000 | [diff] [blame] | 2830 | dirty_flags |= (0xff & ~CODE_DIRTY_FLAG); |
| 2831 | phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags; |
| 2832 | /* we remove the notdirty callback only if the code has been |
| 2833 | flushed */ |
| 2834 | if (dirty_flags == 0xff) |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 2835 | tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr); |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2836 | } |
| 2837 | |
Blue Swirl | d60efc6 | 2009-08-25 18:29:31 +0000 | [diff] [blame] | 2838 | static CPUReadMemoryFunc * const error_mem_read[3] = { |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2839 | NULL, /* never used */ |
| 2840 | NULL, /* never used */ |
| 2841 | NULL, /* never used */ |
| 2842 | }; |
| 2843 | |
Blue Swirl | d60efc6 | 2009-08-25 18:29:31 +0000 | [diff] [blame] | 2844 | static CPUWriteMemoryFunc * const notdirty_mem_write[3] = { |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2845 | notdirty_mem_writeb, |
| 2846 | notdirty_mem_writew, |
| 2847 | notdirty_mem_writel, |
| 2848 | }; |
| 2849 | |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2850 | /* Generate a debug exception if a watchpoint has been hit. */ |
aliguori | b405133 | 2008-11-18 20:14:20 +0000 | [diff] [blame] | 2851 | static void check_watchpoint(int offset, int len_mask, int flags) |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2852 | { |
| 2853 | CPUState *env = cpu_single_env; |
aliguori | 06d55cc | 2008-11-18 20:24:06 +0000 | [diff] [blame] | 2854 | target_ulong pc, cs_base; |
| 2855 | TranslationBlock *tb; |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2856 | target_ulong vaddr; |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 2857 | CPUWatchpoint *wp; |
aliguori | 06d55cc | 2008-11-18 20:24:06 +0000 | [diff] [blame] | 2858 | int cpu_flags; |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2859 | |
aliguori | 06d55cc | 2008-11-18 20:24:06 +0000 | [diff] [blame] | 2860 | if (env->watchpoint_hit) { |
| 2861 | /* We re-entered the check after replacing the TB. Now raise |
| 2862 | * the debug interrupt so that is will trigger after the |
| 2863 | * current instruction. */ |
| 2864 | cpu_interrupt(env, CPU_INTERRUPT_DEBUG); |
| 2865 | return; |
| 2866 | } |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 2867 | vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 2868 | QTAILQ_FOREACH(wp, &env->watchpoints, entry) { |
aliguori | b405133 | 2008-11-18 20:14:20 +0000 | [diff] [blame] | 2869 | if ((vaddr == (wp->vaddr & len_mask) || |
| 2870 | (vaddr & wp->len_mask) == wp->vaddr) && (wp->flags & flags)) { |
aliguori | 6e140f2 | 2008-11-18 20:37:55 +0000 | [diff] [blame] | 2871 | wp->flags |= BP_WATCHPOINT_HIT; |
| 2872 | if (!env->watchpoint_hit) { |
| 2873 | env->watchpoint_hit = wp; |
| 2874 | tb = tb_find_pc(env->mem_io_pc); |
| 2875 | if (!tb) { |
| 2876 | cpu_abort(env, "check_watchpoint: could not find TB for " |
| 2877 | "pc=%p", (void *)env->mem_io_pc); |
| 2878 | } |
| 2879 | cpu_restore_state(tb, env, env->mem_io_pc, NULL); |
| 2880 | tb_phys_invalidate(tb, -1); |
| 2881 | if (wp->flags & BP_STOP_BEFORE_ACCESS) { |
| 2882 | env->exception_index = EXCP_DEBUG; |
| 2883 | } else { |
| 2884 | cpu_get_tb_cpu_state(env, &pc, &cs_base, &cpu_flags); |
| 2885 | tb_gen_code(env, pc, cs_base, cpu_flags, 1); |
| 2886 | } |
| 2887 | cpu_resume_from_signal(env, NULL); |
aliguori | 06d55cc | 2008-11-18 20:24:06 +0000 | [diff] [blame] | 2888 | } |
aliguori | 6e140f2 | 2008-11-18 20:37:55 +0000 | [diff] [blame] | 2889 | } else { |
| 2890 | wp->flags &= ~BP_WATCHPOINT_HIT; |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2891 | } |
| 2892 | } |
| 2893 | } |
| 2894 | |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2895 | /* Watchpoint access routines. Watchpoints are inserted using TLB tricks, |
| 2896 | so these check for a hit then pass through to the normal out-of-line |
| 2897 | phys routines. */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2898 | static uint32_t watch_mem_readb(void *opaque, target_phys_addr_t addr) |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2899 | { |
aliguori | b405133 | 2008-11-18 20:14:20 +0000 | [diff] [blame] | 2900 | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_READ); |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2901 | return ldub_phys(addr); |
| 2902 | } |
| 2903 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2904 | static uint32_t watch_mem_readw(void *opaque, target_phys_addr_t addr) |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2905 | { |
aliguori | b405133 | 2008-11-18 20:14:20 +0000 | [diff] [blame] | 2906 | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_READ); |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2907 | return lduw_phys(addr); |
| 2908 | } |
| 2909 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2910 | static uint32_t watch_mem_readl(void *opaque, target_phys_addr_t addr) |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2911 | { |
aliguori | b405133 | 2008-11-18 20:14:20 +0000 | [diff] [blame] | 2912 | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_READ); |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2913 | return ldl_phys(addr); |
| 2914 | } |
| 2915 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2916 | static void watch_mem_writeb(void *opaque, target_phys_addr_t addr, |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2917 | uint32_t val) |
| 2918 | { |
aliguori | b405133 | 2008-11-18 20:14:20 +0000 | [diff] [blame] | 2919 | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_WRITE); |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2920 | stb_phys(addr, val); |
| 2921 | } |
| 2922 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2923 | static void watch_mem_writew(void *opaque, target_phys_addr_t addr, |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2924 | uint32_t val) |
| 2925 | { |
aliguori | b405133 | 2008-11-18 20:14:20 +0000 | [diff] [blame] | 2926 | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_WRITE); |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2927 | stw_phys(addr, val); |
| 2928 | } |
| 2929 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2930 | static void watch_mem_writel(void *opaque, target_phys_addr_t addr, |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2931 | uint32_t val) |
| 2932 | { |
aliguori | b405133 | 2008-11-18 20:14:20 +0000 | [diff] [blame] | 2933 | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_WRITE); |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2934 | stl_phys(addr, val); |
| 2935 | } |
| 2936 | |
Blue Swirl | d60efc6 | 2009-08-25 18:29:31 +0000 | [diff] [blame] | 2937 | static CPUReadMemoryFunc * const watch_mem_read[3] = { |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2938 | watch_mem_readb, |
| 2939 | watch_mem_readw, |
| 2940 | watch_mem_readl, |
| 2941 | }; |
| 2942 | |
Blue Swirl | d60efc6 | 2009-08-25 18:29:31 +0000 | [diff] [blame] | 2943 | static CPUWriteMemoryFunc * const watch_mem_write[3] = { |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2944 | watch_mem_writeb, |
| 2945 | watch_mem_writew, |
| 2946 | watch_mem_writel, |
| 2947 | }; |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2948 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2949 | static inline uint32_t subpage_readlen (subpage_t *mmio, target_phys_addr_t addr, |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2950 | unsigned int len) |
| 2951 | { |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2952 | uint32_t ret; |
| 2953 | unsigned int idx; |
| 2954 | |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2955 | idx = SUBPAGE_IDX(addr); |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2956 | #if defined(DEBUG_SUBPAGE) |
| 2957 | printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__, |
| 2958 | mmio, len, addr, idx); |
| 2959 | #endif |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2960 | ret = (**mmio->mem_read[idx][len])(mmio->opaque[idx][0][len], |
| 2961 | addr + mmio->region_offset[idx][0][len]); |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2962 | |
| 2963 | return ret; |
| 2964 | } |
| 2965 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2966 | static inline void subpage_writelen (subpage_t *mmio, target_phys_addr_t addr, |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2967 | uint32_t value, unsigned int len) |
| 2968 | { |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2969 | unsigned int idx; |
| 2970 | |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2971 | idx = SUBPAGE_IDX(addr); |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2972 | #if defined(DEBUG_SUBPAGE) |
| 2973 | printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d value %08x\n", __func__, |
| 2974 | mmio, len, addr, idx, value); |
| 2975 | #endif |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2976 | (**mmio->mem_write[idx][len])(mmio->opaque[idx][1][len], |
| 2977 | addr + mmio->region_offset[idx][1][len], |
| 2978 | value); |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2979 | } |
| 2980 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2981 | static uint32_t subpage_readb (void *opaque, target_phys_addr_t addr) |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2982 | { |
| 2983 | #if defined(DEBUG_SUBPAGE) |
| 2984 | printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr); |
| 2985 | #endif |
| 2986 | |
| 2987 | return subpage_readlen(opaque, addr, 0); |
| 2988 | } |
| 2989 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2990 | static void subpage_writeb (void *opaque, target_phys_addr_t addr, |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2991 | uint32_t value) |
| 2992 | { |
| 2993 | #if defined(DEBUG_SUBPAGE) |
| 2994 | printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value); |
| 2995 | #endif |
| 2996 | subpage_writelen(opaque, addr, value, 0); |
| 2997 | } |
| 2998 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2999 | static uint32_t subpage_readw (void *opaque, target_phys_addr_t addr) |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3000 | { |
| 3001 | #if defined(DEBUG_SUBPAGE) |
| 3002 | printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr); |
| 3003 | #endif |
| 3004 | |
| 3005 | return subpage_readlen(opaque, addr, 1); |
| 3006 | } |
| 3007 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3008 | static void subpage_writew (void *opaque, target_phys_addr_t addr, |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3009 | uint32_t value) |
| 3010 | { |
| 3011 | #if defined(DEBUG_SUBPAGE) |
| 3012 | printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value); |
| 3013 | #endif |
| 3014 | subpage_writelen(opaque, addr, value, 1); |
| 3015 | } |
| 3016 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3017 | static uint32_t subpage_readl (void *opaque, target_phys_addr_t addr) |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3018 | { |
| 3019 | #if defined(DEBUG_SUBPAGE) |
| 3020 | printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr); |
| 3021 | #endif |
| 3022 | |
| 3023 | return subpage_readlen(opaque, addr, 2); |
| 3024 | } |
| 3025 | |
| 3026 | static void subpage_writel (void *opaque, |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3027 | target_phys_addr_t addr, uint32_t value) |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3028 | { |
| 3029 | #if defined(DEBUG_SUBPAGE) |
| 3030 | printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value); |
| 3031 | #endif |
| 3032 | subpage_writelen(opaque, addr, value, 2); |
| 3033 | } |
| 3034 | |
Blue Swirl | d60efc6 | 2009-08-25 18:29:31 +0000 | [diff] [blame] | 3035 | static CPUReadMemoryFunc * const subpage_read[] = { |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3036 | &subpage_readb, |
| 3037 | &subpage_readw, |
| 3038 | &subpage_readl, |
| 3039 | }; |
| 3040 | |
Blue Swirl | d60efc6 | 2009-08-25 18:29:31 +0000 | [diff] [blame] | 3041 | static CPUWriteMemoryFunc * const subpage_write[] = { |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3042 | &subpage_writeb, |
| 3043 | &subpage_writew, |
| 3044 | &subpage_writel, |
| 3045 | }; |
| 3046 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3047 | static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end, |
| 3048 | ram_addr_t memory, ram_addr_t region_offset) |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3049 | { |
| 3050 | int idx, eidx; |
blueswir1 | 4254fab | 2008-01-01 16:57:19 +0000 | [diff] [blame] | 3051 | unsigned int i; |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3052 | |
| 3053 | if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE) |
| 3054 | return -1; |
| 3055 | idx = SUBPAGE_IDX(start); |
| 3056 | eidx = SUBPAGE_IDX(end); |
| 3057 | #if defined(DEBUG_SUBPAGE) |
Blue Swirl | 0bf9e31 | 2009-07-20 17:19:25 +0000 | [diff] [blame] | 3058 | printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %ld\n", __func__, |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3059 | mmio, start, end, idx, eidx, memory); |
| 3060 | #endif |
| 3061 | memory >>= IO_MEM_SHIFT; |
| 3062 | for (; idx <= eidx; idx++) { |
blueswir1 | 4254fab | 2008-01-01 16:57:19 +0000 | [diff] [blame] | 3063 | for (i = 0; i < 4; i++) { |
blueswir1 | 3ee8992 | 2008-01-02 19:45:26 +0000 | [diff] [blame] | 3064 | if (io_mem_read[memory][i]) { |
| 3065 | mmio->mem_read[idx][i] = &io_mem_read[memory][i]; |
| 3066 | mmio->opaque[idx][0][i] = io_mem_opaque[memory]; |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 3067 | mmio->region_offset[idx][0][i] = region_offset; |
blueswir1 | 3ee8992 | 2008-01-02 19:45:26 +0000 | [diff] [blame] | 3068 | } |
| 3069 | if (io_mem_write[memory][i]) { |
| 3070 | mmio->mem_write[idx][i] = &io_mem_write[memory][i]; |
| 3071 | mmio->opaque[idx][1][i] = io_mem_opaque[memory]; |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 3072 | mmio->region_offset[idx][1][i] = region_offset; |
blueswir1 | 3ee8992 | 2008-01-02 19:45:26 +0000 | [diff] [blame] | 3073 | } |
blueswir1 | 4254fab | 2008-01-01 16:57:19 +0000 | [diff] [blame] | 3074 | } |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3075 | } |
| 3076 | |
| 3077 | return 0; |
| 3078 | } |
| 3079 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3080 | static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys, |
| 3081 | ram_addr_t orig_memory, ram_addr_t region_offset) |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3082 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3083 | subpage_t *mmio; |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3084 | int subpage_memory; |
| 3085 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3086 | mmio = qemu_mallocz(sizeof(subpage_t)); |
aliguori | 1eec614 | 2009-02-05 22:06:18 +0000 | [diff] [blame] | 3087 | |
| 3088 | mmio->base = base; |
Avi Kivity | 1eed09c | 2009-06-14 11:38:51 +0300 | [diff] [blame] | 3089 | subpage_memory = cpu_register_io_memory(subpage_read, subpage_write, mmio); |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3090 | #if defined(DEBUG_SUBPAGE) |
aliguori | 1eec614 | 2009-02-05 22:06:18 +0000 | [diff] [blame] | 3091 | printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__, |
| 3092 | mmio, base, TARGET_PAGE_SIZE, subpage_memory); |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3093 | #endif |
aliguori | 1eec614 | 2009-02-05 22:06:18 +0000 | [diff] [blame] | 3094 | *phys = subpage_memory | IO_MEM_SUBPAGE; |
| 3095 | subpage_register(mmio, 0, TARGET_PAGE_SIZE - 1, orig_memory, |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 3096 | region_offset); |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3097 | |
| 3098 | return mmio; |
| 3099 | } |
| 3100 | |
aliguori | 8871565 | 2009-02-11 15:20:58 +0000 | [diff] [blame] | 3101 | static int get_free_io_mem_idx(void) |
| 3102 | { |
| 3103 | int i; |
| 3104 | |
| 3105 | for (i = 0; i<IO_MEM_NB_ENTRIES; i++) |
| 3106 | if (!io_mem_used[i]) { |
| 3107 | io_mem_used[i] = 1; |
| 3108 | return i; |
| 3109 | } |
Riku Voipio | c6703b4 | 2009-12-03 15:56:05 +0200 | [diff] [blame] | 3110 | fprintf(stderr, "RAN out out io_mem_idx, max %d !\n", IO_MEM_NB_ENTRIES); |
aliguori | 8871565 | 2009-02-11 15:20:58 +0000 | [diff] [blame] | 3111 | return -1; |
| 3112 | } |
| 3113 | |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 3114 | /* mem_read and mem_write are arrays of functions containing the |
| 3115 | function to access byte (index 0), word (index 1) and dword (index |
Paul Brook | 0b4e6e3 | 2009-04-30 18:37:55 +0100 | [diff] [blame] | 3116 | 2). Functions can be omitted with a NULL function pointer. |
blueswir1 | 3ee8992 | 2008-01-02 19:45:26 +0000 | [diff] [blame] | 3117 | If io_index is non zero, the corresponding io zone is |
blueswir1 | 4254fab | 2008-01-01 16:57:19 +0000 | [diff] [blame] | 3118 | modified. If it is zero, a new io zone is allocated. The return |
| 3119 | value can be used with cpu_register_physical_memory(). (-1) is |
| 3120 | returned if error. */ |
Avi Kivity | 1eed09c | 2009-06-14 11:38:51 +0300 | [diff] [blame] | 3121 | static int cpu_register_io_memory_fixed(int io_index, |
Blue Swirl | d60efc6 | 2009-08-25 18:29:31 +0000 | [diff] [blame] | 3122 | CPUReadMemoryFunc * const *mem_read, |
| 3123 | CPUWriteMemoryFunc * const *mem_write, |
Avi Kivity | 1eed09c | 2009-06-14 11:38:51 +0300 | [diff] [blame] | 3124 | void *opaque) |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 3125 | { |
blueswir1 | 4254fab | 2008-01-01 16:57:19 +0000 | [diff] [blame] | 3126 | int i, subwidth = 0; |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 3127 | |
| 3128 | if (io_index <= 0) { |
aliguori | 8871565 | 2009-02-11 15:20:58 +0000 | [diff] [blame] | 3129 | io_index = get_free_io_mem_idx(); |
| 3130 | if (io_index == -1) |
| 3131 | return io_index; |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 3132 | } else { |
Avi Kivity | 1eed09c | 2009-06-14 11:38:51 +0300 | [diff] [blame] | 3133 | io_index >>= IO_MEM_SHIFT; |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 3134 | if (io_index >= IO_MEM_NB_ENTRIES) |
| 3135 | return -1; |
| 3136 | } |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 3137 | |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 3138 | for(i = 0;i < 3; i++) { |
blueswir1 | 4254fab | 2008-01-01 16:57:19 +0000 | [diff] [blame] | 3139 | if (!mem_read[i] || !mem_write[i]) |
| 3140 | subwidth = IO_MEM_SUBWIDTH; |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 3141 | io_mem_read[io_index][i] = mem_read[i]; |
| 3142 | io_mem_write[io_index][i] = mem_write[i]; |
| 3143 | } |
bellard | a4193c8 | 2004-06-03 14:01:43 +0000 | [diff] [blame] | 3144 | io_mem_opaque[io_index] = opaque; |
blueswir1 | 4254fab | 2008-01-01 16:57:19 +0000 | [diff] [blame] | 3145 | return (io_index << IO_MEM_SHIFT) | subwidth; |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 3146 | } |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 3147 | |
Blue Swirl | d60efc6 | 2009-08-25 18:29:31 +0000 | [diff] [blame] | 3148 | int cpu_register_io_memory(CPUReadMemoryFunc * const *mem_read, |
| 3149 | CPUWriteMemoryFunc * const *mem_write, |
Avi Kivity | 1eed09c | 2009-06-14 11:38:51 +0300 | [diff] [blame] | 3150 | void *opaque) |
| 3151 | { |
| 3152 | return cpu_register_io_memory_fixed(0, mem_read, mem_write, opaque); |
| 3153 | } |
| 3154 | |
aliguori | 8871565 | 2009-02-11 15:20:58 +0000 | [diff] [blame] | 3155 | void cpu_unregister_io_memory(int io_table_address) |
| 3156 | { |
| 3157 | int i; |
| 3158 | int io_index = io_table_address >> IO_MEM_SHIFT; |
| 3159 | |
| 3160 | for (i=0;i < 3; i++) { |
| 3161 | io_mem_read[io_index][i] = unassigned_mem_read[i]; |
| 3162 | io_mem_write[io_index][i] = unassigned_mem_write[i]; |
| 3163 | } |
| 3164 | io_mem_opaque[io_index] = NULL; |
| 3165 | io_mem_used[io_index] = 0; |
| 3166 | } |
| 3167 | |
Avi Kivity | e9179ce | 2009-06-14 11:38:52 +0300 | [diff] [blame] | 3168 | static void io_mem_init(void) |
| 3169 | { |
| 3170 | int i; |
| 3171 | |
| 3172 | cpu_register_io_memory_fixed(IO_MEM_ROM, error_mem_read, unassigned_mem_write, NULL); |
| 3173 | cpu_register_io_memory_fixed(IO_MEM_UNASSIGNED, unassigned_mem_read, unassigned_mem_write, NULL); |
| 3174 | cpu_register_io_memory_fixed(IO_MEM_NOTDIRTY, error_mem_read, notdirty_mem_write, NULL); |
| 3175 | for (i=0; i<5; i++) |
| 3176 | io_mem_used[i] = 1; |
| 3177 | |
| 3178 | io_mem_watch = cpu_register_io_memory(watch_mem_read, |
| 3179 | watch_mem_write, NULL); |
Avi Kivity | e9179ce | 2009-06-14 11:38:52 +0300 | [diff] [blame] | 3180 | } |
| 3181 | |
pbrook | e2eef17 | 2008-06-08 01:09:01 +0000 | [diff] [blame] | 3182 | #endif /* !defined(CONFIG_USER_ONLY) */ |
| 3183 | |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3184 | /* physical memory access (slow version, mainly for debug) */ |
| 3185 | #if defined(CONFIG_USER_ONLY) |
Paul Brook | a68fe89 | 2010-03-01 00:08:59 +0000 | [diff] [blame] | 3186 | int cpu_memory_rw_debug(CPUState *env, target_ulong addr, |
| 3187 | uint8_t *buf, int len, int is_write) |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3188 | { |
| 3189 | int l, flags; |
| 3190 | target_ulong page; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 3191 | void * p; |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3192 | |
| 3193 | while (len > 0) { |
| 3194 | page = addr & TARGET_PAGE_MASK; |
| 3195 | l = (page + TARGET_PAGE_SIZE) - addr; |
| 3196 | if (l > len) |
| 3197 | l = len; |
| 3198 | flags = page_get_flags(page); |
| 3199 | if (!(flags & PAGE_VALID)) |
Paul Brook | a68fe89 | 2010-03-01 00:08:59 +0000 | [diff] [blame] | 3200 | return -1; |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3201 | if (is_write) { |
| 3202 | if (!(flags & PAGE_WRITE)) |
Paul Brook | a68fe89 | 2010-03-01 00:08:59 +0000 | [diff] [blame] | 3203 | return -1; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3204 | /* XXX: this code should not depend on lock_user */ |
aurel32 | 72fb7da | 2008-04-27 23:53:45 +0000 | [diff] [blame] | 3205 | if (!(p = lock_user(VERIFY_WRITE, addr, l, 0))) |
Paul Brook | a68fe89 | 2010-03-01 00:08:59 +0000 | [diff] [blame] | 3206 | return -1; |
aurel32 | 72fb7da | 2008-04-27 23:53:45 +0000 | [diff] [blame] | 3207 | memcpy(p, buf, l); |
| 3208 | unlock_user(p, addr, l); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3209 | } else { |
| 3210 | if (!(flags & PAGE_READ)) |
Paul Brook | a68fe89 | 2010-03-01 00:08:59 +0000 | [diff] [blame] | 3211 | return -1; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3212 | /* XXX: this code should not depend on lock_user */ |
aurel32 | 72fb7da | 2008-04-27 23:53:45 +0000 | [diff] [blame] | 3213 | if (!(p = lock_user(VERIFY_READ, addr, l, 1))) |
Paul Brook | a68fe89 | 2010-03-01 00:08:59 +0000 | [diff] [blame] | 3214 | return -1; |
aurel32 | 72fb7da | 2008-04-27 23:53:45 +0000 | [diff] [blame] | 3215 | memcpy(buf, p, l); |
aurel32 | 5b25757 | 2008-04-28 08:54:59 +0000 | [diff] [blame] | 3216 | unlock_user(p, addr, 0); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3217 | } |
| 3218 | len -= l; |
| 3219 | buf += l; |
| 3220 | addr += l; |
| 3221 | } |
Paul Brook | a68fe89 | 2010-03-01 00:08:59 +0000 | [diff] [blame] | 3222 | return 0; |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3223 | } |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 3224 | |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3225 | #else |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3226 | void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf, |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3227 | int len, int is_write) |
| 3228 | { |
| 3229 | int l, io_index; |
| 3230 | uint8_t *ptr; |
| 3231 | uint32_t val; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3232 | target_phys_addr_t page; |
bellard | 2e12669 | 2004-04-25 21:28:44 +0000 | [diff] [blame] | 3233 | unsigned long pd; |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 3234 | PhysPageDesc *p; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3235 | |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3236 | while (len > 0) { |
| 3237 | page = addr & TARGET_PAGE_MASK; |
| 3238 | l = (page + TARGET_PAGE_SIZE) - addr; |
| 3239 | if (l > len) |
| 3240 | l = len; |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 3241 | p = phys_page_find(page >> TARGET_PAGE_BITS); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3242 | if (!p) { |
| 3243 | pd = IO_MEM_UNASSIGNED; |
| 3244 | } else { |
| 3245 | pd = p->phys_offset; |
| 3246 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3247 | |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3248 | if (is_write) { |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 3249 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3250 | target_phys_addr_t addr1 = addr; |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3251 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 3252 | if (p) |
aurel32 | 6c2934d | 2009-02-18 21:37:17 +0000 | [diff] [blame] | 3253 | addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 3254 | /* XXX: could force cpu_single_env to NULL to avoid |
| 3255 | potential bugs */ |
aurel32 | 6c2934d | 2009-02-18 21:37:17 +0000 | [diff] [blame] | 3256 | if (l >= 4 && ((addr1 & 3) == 0)) { |
bellard | 1c213d1 | 2005-09-03 10:49:04 +0000 | [diff] [blame] | 3257 | /* 32 bit write access */ |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 3258 | val = ldl_p(buf); |
aurel32 | 6c2934d | 2009-02-18 21:37:17 +0000 | [diff] [blame] | 3259 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr1, val); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3260 | l = 4; |
aurel32 | 6c2934d | 2009-02-18 21:37:17 +0000 | [diff] [blame] | 3261 | } else if (l >= 2 && ((addr1 & 1) == 0)) { |
bellard | 1c213d1 | 2005-09-03 10:49:04 +0000 | [diff] [blame] | 3262 | /* 16 bit write access */ |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 3263 | val = lduw_p(buf); |
aurel32 | 6c2934d | 2009-02-18 21:37:17 +0000 | [diff] [blame] | 3264 | io_mem_write[io_index][1](io_mem_opaque[io_index], addr1, val); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3265 | l = 2; |
| 3266 | } else { |
bellard | 1c213d1 | 2005-09-03 10:49:04 +0000 | [diff] [blame] | 3267 | /* 8 bit write access */ |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 3268 | val = ldub_p(buf); |
aurel32 | 6c2934d | 2009-02-18 21:37:17 +0000 | [diff] [blame] | 3269 | io_mem_write[io_index][0](io_mem_opaque[io_index], addr1, val); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3270 | l = 1; |
| 3271 | } |
| 3272 | } else { |
bellard | b448f2f | 2004-02-25 23:24:04 +0000 | [diff] [blame] | 3273 | unsigned long addr1; |
| 3274 | addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3275 | /* RAM case */ |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3276 | ptr = qemu_get_ram_ptr(addr1); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3277 | memcpy(ptr, buf, l); |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 3278 | if (!cpu_physical_memory_is_dirty(addr1)) { |
| 3279 | /* invalidate code */ |
| 3280 | tb_invalidate_phys_page_range(addr1, addr1 + l, 0); |
| 3281 | /* set dirty bit */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3282 | phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |= |
bellard | f23db16 | 2005-08-21 19:12:28 +0000 | [diff] [blame] | 3283 | (0xff & ~CODE_DIRTY_FLAG); |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 3284 | } |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3285 | } |
| 3286 | } else { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3287 | if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && |
bellard | 2a4188a | 2006-06-25 21:54:59 +0000 | [diff] [blame] | 3288 | !(pd & IO_MEM_ROMD)) { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3289 | target_phys_addr_t addr1 = addr; |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3290 | /* I/O case */ |
| 3291 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 3292 | if (p) |
aurel32 | 6c2934d | 2009-02-18 21:37:17 +0000 | [diff] [blame] | 3293 | addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset; |
| 3294 | if (l >= 4 && ((addr1 & 3) == 0)) { |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3295 | /* 32 bit read access */ |
aurel32 | 6c2934d | 2009-02-18 21:37:17 +0000 | [diff] [blame] | 3296 | val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr1); |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 3297 | stl_p(buf, val); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3298 | l = 4; |
aurel32 | 6c2934d | 2009-02-18 21:37:17 +0000 | [diff] [blame] | 3299 | } else if (l >= 2 && ((addr1 & 1) == 0)) { |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3300 | /* 16 bit read access */ |
aurel32 | 6c2934d | 2009-02-18 21:37:17 +0000 | [diff] [blame] | 3301 | val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr1); |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 3302 | stw_p(buf, val); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3303 | l = 2; |
| 3304 | } else { |
bellard | 1c213d1 | 2005-09-03 10:49:04 +0000 | [diff] [blame] | 3305 | /* 8 bit read access */ |
aurel32 | 6c2934d | 2009-02-18 21:37:17 +0000 | [diff] [blame] | 3306 | val = io_mem_read[io_index][0](io_mem_opaque[io_index], addr1); |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 3307 | stb_p(buf, val); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3308 | l = 1; |
| 3309 | } |
| 3310 | } else { |
| 3311 | /* RAM case */ |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3312 | ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) + |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3313 | (addr & ~TARGET_PAGE_MASK); |
| 3314 | memcpy(buf, ptr, l); |
| 3315 | } |
| 3316 | } |
| 3317 | len -= l; |
| 3318 | buf += l; |
| 3319 | addr += l; |
| 3320 | } |
| 3321 | } |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 3322 | |
bellard | d0ecd2a | 2006-04-23 17:14:48 +0000 | [diff] [blame] | 3323 | /* used for ROM loading : can write in RAM and ROM */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3324 | void cpu_physical_memory_write_rom(target_phys_addr_t addr, |
bellard | d0ecd2a | 2006-04-23 17:14:48 +0000 | [diff] [blame] | 3325 | const uint8_t *buf, int len) |
| 3326 | { |
| 3327 | int l; |
| 3328 | uint8_t *ptr; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3329 | target_phys_addr_t page; |
bellard | d0ecd2a | 2006-04-23 17:14:48 +0000 | [diff] [blame] | 3330 | unsigned long pd; |
| 3331 | PhysPageDesc *p; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3332 | |
bellard | d0ecd2a | 2006-04-23 17:14:48 +0000 | [diff] [blame] | 3333 | while (len > 0) { |
| 3334 | page = addr & TARGET_PAGE_MASK; |
| 3335 | l = (page + TARGET_PAGE_SIZE) - addr; |
| 3336 | if (l > len) |
| 3337 | l = len; |
| 3338 | p = phys_page_find(page >> TARGET_PAGE_BITS); |
| 3339 | if (!p) { |
| 3340 | pd = IO_MEM_UNASSIGNED; |
| 3341 | } else { |
| 3342 | pd = p->phys_offset; |
| 3343 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3344 | |
bellard | d0ecd2a | 2006-04-23 17:14:48 +0000 | [diff] [blame] | 3345 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM && |
bellard | 2a4188a | 2006-06-25 21:54:59 +0000 | [diff] [blame] | 3346 | (pd & ~TARGET_PAGE_MASK) != IO_MEM_ROM && |
| 3347 | !(pd & IO_MEM_ROMD)) { |
bellard | d0ecd2a | 2006-04-23 17:14:48 +0000 | [diff] [blame] | 3348 | /* do nothing */ |
| 3349 | } else { |
| 3350 | unsigned long addr1; |
| 3351 | addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); |
| 3352 | /* ROM/RAM case */ |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3353 | ptr = qemu_get_ram_ptr(addr1); |
bellard | d0ecd2a | 2006-04-23 17:14:48 +0000 | [diff] [blame] | 3354 | memcpy(ptr, buf, l); |
| 3355 | } |
| 3356 | len -= l; |
| 3357 | buf += l; |
| 3358 | addr += l; |
| 3359 | } |
| 3360 | } |
| 3361 | |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 3362 | typedef struct { |
| 3363 | void *buffer; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3364 | target_phys_addr_t addr; |
| 3365 | target_phys_addr_t len; |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 3366 | } BounceBuffer; |
| 3367 | |
| 3368 | static BounceBuffer bounce; |
| 3369 | |
aliguori | ba223c2 | 2009-01-22 16:59:16 +0000 | [diff] [blame] | 3370 | typedef struct MapClient { |
| 3371 | void *opaque; |
| 3372 | void (*callback)(void *opaque); |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3373 | QLIST_ENTRY(MapClient) link; |
aliguori | ba223c2 | 2009-01-22 16:59:16 +0000 | [diff] [blame] | 3374 | } MapClient; |
| 3375 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3376 | static QLIST_HEAD(map_client_list, MapClient) map_client_list |
| 3377 | = QLIST_HEAD_INITIALIZER(map_client_list); |
aliguori | ba223c2 | 2009-01-22 16:59:16 +0000 | [diff] [blame] | 3378 | |
| 3379 | void *cpu_register_map_client(void *opaque, void (*callback)(void *opaque)) |
| 3380 | { |
| 3381 | MapClient *client = qemu_malloc(sizeof(*client)); |
| 3382 | |
| 3383 | client->opaque = opaque; |
| 3384 | client->callback = callback; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3385 | QLIST_INSERT_HEAD(&map_client_list, client, link); |
aliguori | ba223c2 | 2009-01-22 16:59:16 +0000 | [diff] [blame] | 3386 | return client; |
| 3387 | } |
| 3388 | |
| 3389 | void cpu_unregister_map_client(void *_client) |
| 3390 | { |
| 3391 | MapClient *client = (MapClient *)_client; |
| 3392 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3393 | QLIST_REMOVE(client, link); |
Isaku Yamahata | 34d5e94 | 2009-06-26 18:57:18 +0900 | [diff] [blame] | 3394 | qemu_free(client); |
aliguori | ba223c2 | 2009-01-22 16:59:16 +0000 | [diff] [blame] | 3395 | } |
| 3396 | |
| 3397 | static void cpu_notify_map_clients(void) |
| 3398 | { |
| 3399 | MapClient *client; |
| 3400 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3401 | while (!QLIST_EMPTY(&map_client_list)) { |
| 3402 | client = QLIST_FIRST(&map_client_list); |
aliguori | ba223c2 | 2009-01-22 16:59:16 +0000 | [diff] [blame] | 3403 | client->callback(client->opaque); |
Isaku Yamahata | 34d5e94 | 2009-06-26 18:57:18 +0900 | [diff] [blame] | 3404 | cpu_unregister_map_client(client); |
aliguori | ba223c2 | 2009-01-22 16:59:16 +0000 | [diff] [blame] | 3405 | } |
| 3406 | } |
| 3407 | |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 3408 | /* Map a physical memory region into a host virtual address. |
| 3409 | * May map a subset of the requested range, given by and returned in *plen. |
| 3410 | * May return NULL if resources needed to perform the mapping are exhausted. |
| 3411 | * Use only for reads OR writes - not for read-modify-write operations. |
aliguori | ba223c2 | 2009-01-22 16:59:16 +0000 | [diff] [blame] | 3412 | * Use cpu_register_map_client() to know when retrying the map operation is |
| 3413 | * likely to succeed. |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 3414 | */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3415 | void *cpu_physical_memory_map(target_phys_addr_t addr, |
| 3416 | target_phys_addr_t *plen, |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 3417 | int is_write) |
| 3418 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3419 | target_phys_addr_t len = *plen; |
| 3420 | target_phys_addr_t done = 0; |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 3421 | int l; |
| 3422 | uint8_t *ret = NULL; |
| 3423 | uint8_t *ptr; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3424 | target_phys_addr_t page; |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 3425 | unsigned long pd; |
| 3426 | PhysPageDesc *p; |
| 3427 | unsigned long addr1; |
| 3428 | |
| 3429 | while (len > 0) { |
| 3430 | page = addr & TARGET_PAGE_MASK; |
| 3431 | l = (page + TARGET_PAGE_SIZE) - addr; |
| 3432 | if (l > len) |
| 3433 | l = len; |
| 3434 | p = phys_page_find(page >> TARGET_PAGE_BITS); |
| 3435 | if (!p) { |
| 3436 | pd = IO_MEM_UNASSIGNED; |
| 3437 | } else { |
| 3438 | pd = p->phys_offset; |
| 3439 | } |
| 3440 | |
| 3441 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) { |
| 3442 | if (done || bounce.buffer) { |
| 3443 | break; |
| 3444 | } |
| 3445 | bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, TARGET_PAGE_SIZE); |
| 3446 | bounce.addr = addr; |
| 3447 | bounce.len = l; |
| 3448 | if (!is_write) { |
| 3449 | cpu_physical_memory_rw(addr, bounce.buffer, l, 0); |
| 3450 | } |
| 3451 | ptr = bounce.buffer; |
| 3452 | } else { |
| 3453 | addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3454 | ptr = qemu_get_ram_ptr(addr1); |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 3455 | } |
| 3456 | if (!done) { |
| 3457 | ret = ptr; |
| 3458 | } else if (ret + done != ptr) { |
| 3459 | break; |
| 3460 | } |
| 3461 | |
| 3462 | len -= l; |
| 3463 | addr += l; |
| 3464 | done += l; |
| 3465 | } |
| 3466 | *plen = done; |
| 3467 | return ret; |
| 3468 | } |
| 3469 | |
| 3470 | /* Unmaps a memory region previously mapped by cpu_physical_memory_map(). |
| 3471 | * Will also mark the memory as dirty if is_write == 1. access_len gives |
| 3472 | * the amount of memory that was actually read or written by the caller. |
| 3473 | */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3474 | void cpu_physical_memory_unmap(void *buffer, target_phys_addr_t len, |
| 3475 | int is_write, target_phys_addr_t access_len) |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 3476 | { |
| 3477 | if (buffer != bounce.buffer) { |
| 3478 | if (is_write) { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3479 | ram_addr_t addr1 = qemu_ram_addr_from_host(buffer); |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 3480 | while (access_len) { |
| 3481 | unsigned l; |
| 3482 | l = TARGET_PAGE_SIZE; |
| 3483 | if (l > access_len) |
| 3484 | l = access_len; |
| 3485 | if (!cpu_physical_memory_is_dirty(addr1)) { |
| 3486 | /* invalidate code */ |
| 3487 | tb_invalidate_phys_page_range(addr1, addr1 + l, 0); |
| 3488 | /* set dirty bit */ |
| 3489 | phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |= |
| 3490 | (0xff & ~CODE_DIRTY_FLAG); |
| 3491 | } |
| 3492 | addr1 += l; |
| 3493 | access_len -= l; |
| 3494 | } |
| 3495 | } |
| 3496 | return; |
| 3497 | } |
| 3498 | if (is_write) { |
| 3499 | cpu_physical_memory_write(bounce.addr, bounce.buffer, access_len); |
| 3500 | } |
Herve Poussineau | f8a8324 | 2010-01-24 21:23:56 +0000 | [diff] [blame] | 3501 | qemu_vfree(bounce.buffer); |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 3502 | bounce.buffer = NULL; |
aliguori | ba223c2 | 2009-01-22 16:59:16 +0000 | [diff] [blame] | 3503 | cpu_notify_map_clients(); |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 3504 | } |
bellard | d0ecd2a | 2006-04-23 17:14:48 +0000 | [diff] [blame] | 3505 | |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 3506 | /* warning: addr must be aligned */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3507 | uint32_t ldl_phys(target_phys_addr_t addr) |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 3508 | { |
| 3509 | int io_index; |
| 3510 | uint8_t *ptr; |
| 3511 | uint32_t val; |
| 3512 | unsigned long pd; |
| 3513 | PhysPageDesc *p; |
| 3514 | |
| 3515 | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
| 3516 | if (!p) { |
| 3517 | pd = IO_MEM_UNASSIGNED; |
| 3518 | } else { |
| 3519 | pd = p->phys_offset; |
| 3520 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3521 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3522 | if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && |
bellard | 2a4188a | 2006-06-25 21:54:59 +0000 | [diff] [blame] | 3523 | !(pd & IO_MEM_ROMD)) { |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 3524 | /* I/O case */ |
| 3525 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 3526 | if (p) |
| 3527 | addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset; |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 3528 | val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr); |
| 3529 | } else { |
| 3530 | /* RAM case */ |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3531 | ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) + |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 3532 | (addr & ~TARGET_PAGE_MASK); |
| 3533 | val = ldl_p(ptr); |
| 3534 | } |
| 3535 | return val; |
| 3536 | } |
| 3537 | |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 3538 | /* warning: addr must be aligned */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3539 | uint64_t ldq_phys(target_phys_addr_t addr) |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 3540 | { |
| 3541 | int io_index; |
| 3542 | uint8_t *ptr; |
| 3543 | uint64_t val; |
| 3544 | unsigned long pd; |
| 3545 | PhysPageDesc *p; |
| 3546 | |
| 3547 | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
| 3548 | if (!p) { |
| 3549 | pd = IO_MEM_UNASSIGNED; |
| 3550 | } else { |
| 3551 | pd = p->phys_offset; |
| 3552 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3553 | |
bellard | 2a4188a | 2006-06-25 21:54:59 +0000 | [diff] [blame] | 3554 | if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && |
| 3555 | !(pd & IO_MEM_ROMD)) { |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 3556 | /* I/O case */ |
| 3557 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 3558 | if (p) |
| 3559 | addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset; |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 3560 | #ifdef TARGET_WORDS_BIGENDIAN |
| 3561 | val = (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr) << 32; |
| 3562 | val |= io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4); |
| 3563 | #else |
| 3564 | val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr); |
| 3565 | val |= (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4) << 32; |
| 3566 | #endif |
| 3567 | } else { |
| 3568 | /* RAM case */ |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3569 | ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) + |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 3570 | (addr & ~TARGET_PAGE_MASK); |
| 3571 | val = ldq_p(ptr); |
| 3572 | } |
| 3573 | return val; |
| 3574 | } |
| 3575 | |
bellard | aab3309 | 2005-10-30 20:48:42 +0000 | [diff] [blame] | 3576 | /* XXX: optimize */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3577 | uint32_t ldub_phys(target_phys_addr_t addr) |
bellard | aab3309 | 2005-10-30 20:48:42 +0000 | [diff] [blame] | 3578 | { |
| 3579 | uint8_t val; |
| 3580 | cpu_physical_memory_read(addr, &val, 1); |
| 3581 | return val; |
| 3582 | } |
| 3583 | |
| 3584 | /* XXX: optimize */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3585 | uint32_t lduw_phys(target_phys_addr_t addr) |
bellard | aab3309 | 2005-10-30 20:48:42 +0000 | [diff] [blame] | 3586 | { |
| 3587 | uint16_t val; |
| 3588 | cpu_physical_memory_read(addr, (uint8_t *)&val, 2); |
| 3589 | return tswap16(val); |
| 3590 | } |
| 3591 | |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 3592 | /* warning: addr must be aligned. The ram page is not masked as dirty |
| 3593 | and the code inside is not invalidated. It is useful if the dirty |
| 3594 | bits are used to track modified PTEs */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3595 | void stl_phys_notdirty(target_phys_addr_t addr, uint32_t val) |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 3596 | { |
| 3597 | int io_index; |
| 3598 | uint8_t *ptr; |
| 3599 | unsigned long pd; |
| 3600 | PhysPageDesc *p; |
| 3601 | |
| 3602 | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
| 3603 | if (!p) { |
| 3604 | pd = IO_MEM_UNASSIGNED; |
| 3605 | } else { |
| 3606 | pd = p->phys_offset; |
| 3607 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3608 | |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 3609 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) { |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 3610 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 3611 | if (p) |
| 3612 | addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset; |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 3613 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val); |
| 3614 | } else { |
aliguori | 7457619 | 2008-10-06 14:02:03 +0000 | [diff] [blame] | 3615 | unsigned long addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3616 | ptr = qemu_get_ram_ptr(addr1); |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 3617 | stl_p(ptr, val); |
aliguori | 7457619 | 2008-10-06 14:02:03 +0000 | [diff] [blame] | 3618 | |
| 3619 | if (unlikely(in_migration)) { |
| 3620 | if (!cpu_physical_memory_is_dirty(addr1)) { |
| 3621 | /* invalidate code */ |
| 3622 | tb_invalidate_phys_page_range(addr1, addr1 + 4, 0); |
| 3623 | /* set dirty bit */ |
| 3624 | phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |= |
| 3625 | (0xff & ~CODE_DIRTY_FLAG); |
| 3626 | } |
| 3627 | } |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 3628 | } |
| 3629 | } |
| 3630 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3631 | void stq_phys_notdirty(target_phys_addr_t addr, uint64_t val) |
j_mayer | bc98a7e | 2007-04-04 07:55:12 +0000 | [diff] [blame] | 3632 | { |
| 3633 | int io_index; |
| 3634 | uint8_t *ptr; |
| 3635 | unsigned long pd; |
| 3636 | PhysPageDesc *p; |
| 3637 | |
| 3638 | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
| 3639 | if (!p) { |
| 3640 | pd = IO_MEM_UNASSIGNED; |
| 3641 | } else { |
| 3642 | pd = p->phys_offset; |
| 3643 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3644 | |
j_mayer | bc98a7e | 2007-04-04 07:55:12 +0000 | [diff] [blame] | 3645 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) { |
| 3646 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 3647 | if (p) |
| 3648 | addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset; |
j_mayer | bc98a7e | 2007-04-04 07:55:12 +0000 | [diff] [blame] | 3649 | #ifdef TARGET_WORDS_BIGENDIAN |
| 3650 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val >> 32); |
| 3651 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val); |
| 3652 | #else |
| 3653 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val); |
| 3654 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val >> 32); |
| 3655 | #endif |
| 3656 | } else { |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3657 | ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) + |
j_mayer | bc98a7e | 2007-04-04 07:55:12 +0000 | [diff] [blame] | 3658 | (addr & ~TARGET_PAGE_MASK); |
| 3659 | stq_p(ptr, val); |
| 3660 | } |
| 3661 | } |
| 3662 | |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 3663 | /* warning: addr must be aligned */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3664 | void stl_phys(target_phys_addr_t addr, uint32_t val) |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 3665 | { |
| 3666 | int io_index; |
| 3667 | uint8_t *ptr; |
| 3668 | unsigned long pd; |
| 3669 | PhysPageDesc *p; |
| 3670 | |
| 3671 | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
| 3672 | if (!p) { |
| 3673 | pd = IO_MEM_UNASSIGNED; |
| 3674 | } else { |
| 3675 | pd = p->phys_offset; |
| 3676 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3677 | |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 3678 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) { |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 3679 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 3680 | if (p) |
| 3681 | addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset; |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 3682 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val); |
| 3683 | } else { |
| 3684 | unsigned long addr1; |
| 3685 | addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); |
| 3686 | /* RAM case */ |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3687 | ptr = qemu_get_ram_ptr(addr1); |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 3688 | stl_p(ptr, val); |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 3689 | if (!cpu_physical_memory_is_dirty(addr1)) { |
| 3690 | /* invalidate code */ |
| 3691 | tb_invalidate_phys_page_range(addr1, addr1 + 4, 0); |
| 3692 | /* set dirty bit */ |
bellard | f23db16 | 2005-08-21 19:12:28 +0000 | [diff] [blame] | 3693 | phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |= |
| 3694 | (0xff & ~CODE_DIRTY_FLAG); |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 3695 | } |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 3696 | } |
| 3697 | } |
| 3698 | |
bellard | aab3309 | 2005-10-30 20:48:42 +0000 | [diff] [blame] | 3699 | /* XXX: optimize */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3700 | void stb_phys(target_phys_addr_t addr, uint32_t val) |
bellard | aab3309 | 2005-10-30 20:48:42 +0000 | [diff] [blame] | 3701 | { |
| 3702 | uint8_t v = val; |
| 3703 | cpu_physical_memory_write(addr, &v, 1); |
| 3704 | } |
| 3705 | |
| 3706 | /* XXX: optimize */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3707 | void stw_phys(target_phys_addr_t addr, uint32_t val) |
bellard | aab3309 | 2005-10-30 20:48:42 +0000 | [diff] [blame] | 3708 | { |
| 3709 | uint16_t v = tswap16(val); |
| 3710 | cpu_physical_memory_write(addr, (const uint8_t *)&v, 2); |
| 3711 | } |
| 3712 | |
| 3713 | /* XXX: optimize */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3714 | void stq_phys(target_phys_addr_t addr, uint64_t val) |
bellard | aab3309 | 2005-10-30 20:48:42 +0000 | [diff] [blame] | 3715 | { |
| 3716 | val = tswap64(val); |
| 3717 | cpu_physical_memory_write(addr, (const uint8_t *)&val, 8); |
| 3718 | } |
| 3719 | |
aliguori | 5e2972f | 2009-03-28 17:51:36 +0000 | [diff] [blame] | 3720 | /* virtual memory access for debug (includes writing to ROM) */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3721 | int cpu_memory_rw_debug(CPUState *env, target_ulong addr, |
bellard | b448f2f | 2004-02-25 23:24:04 +0000 | [diff] [blame] | 3722 | uint8_t *buf, int len, int is_write) |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3723 | { |
| 3724 | int l; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3725 | target_phys_addr_t phys_addr; |
j_mayer | 9b3c35e | 2007-04-07 11:21:28 +0000 | [diff] [blame] | 3726 | target_ulong page; |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3727 | |
| 3728 | while (len > 0) { |
| 3729 | page = addr & TARGET_PAGE_MASK; |
| 3730 | phys_addr = cpu_get_phys_page_debug(env, page); |
| 3731 | /* if no physical page mapped, return an error */ |
| 3732 | if (phys_addr == -1) |
| 3733 | return -1; |
| 3734 | l = (page + TARGET_PAGE_SIZE) - addr; |
| 3735 | if (l > len) |
| 3736 | l = len; |
aliguori | 5e2972f | 2009-03-28 17:51:36 +0000 | [diff] [blame] | 3737 | phys_addr += (addr & ~TARGET_PAGE_MASK); |
aliguori | 5e2972f | 2009-03-28 17:51:36 +0000 | [diff] [blame] | 3738 | if (is_write) |
| 3739 | cpu_physical_memory_write_rom(phys_addr, buf, l); |
| 3740 | else |
aliguori | 5e2972f | 2009-03-28 17:51:36 +0000 | [diff] [blame] | 3741 | cpu_physical_memory_rw(phys_addr, buf, l, is_write); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3742 | len -= l; |
| 3743 | buf += l; |
| 3744 | addr += l; |
| 3745 | } |
| 3746 | return 0; |
| 3747 | } |
Paul Brook | a68fe89 | 2010-03-01 00:08:59 +0000 | [diff] [blame] | 3748 | #endif |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3749 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 3750 | /* in deterministic execution mode, instructions doing device I/Os |
| 3751 | must be at the end of the TB */ |
| 3752 | void cpu_io_recompile(CPUState *env, void *retaddr) |
| 3753 | { |
| 3754 | TranslationBlock *tb; |
| 3755 | uint32_t n, cflags; |
| 3756 | target_ulong pc, cs_base; |
| 3757 | uint64_t flags; |
| 3758 | |
| 3759 | tb = tb_find_pc((unsigned long)retaddr); |
| 3760 | if (!tb) { |
| 3761 | cpu_abort(env, "cpu_io_recompile: could not find TB for pc=%p", |
| 3762 | retaddr); |
| 3763 | } |
| 3764 | n = env->icount_decr.u16.low + tb->icount; |
| 3765 | cpu_restore_state(tb, env, (unsigned long)retaddr, NULL); |
| 3766 | /* Calculate how many instructions had been executed before the fault |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 3767 | occurred. */ |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 3768 | n = n - env->icount_decr.u16.low; |
| 3769 | /* Generate a new TB ending on the I/O insn. */ |
| 3770 | n++; |
| 3771 | /* On MIPS and SH, delay slot instructions can only be restarted if |
| 3772 | they were already the first instruction in the TB. If this is not |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 3773 | the first instruction in a TB then re-execute the preceding |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 3774 | branch. */ |
| 3775 | #if defined(TARGET_MIPS) |
| 3776 | if ((env->hflags & MIPS_HFLAG_BMASK) != 0 && n > 1) { |
| 3777 | env->active_tc.PC -= 4; |
| 3778 | env->icount_decr.u16.low++; |
| 3779 | env->hflags &= ~MIPS_HFLAG_BMASK; |
| 3780 | } |
| 3781 | #elif defined(TARGET_SH4) |
| 3782 | if ((env->flags & ((DELAY_SLOT | DELAY_SLOT_CONDITIONAL))) != 0 |
| 3783 | && n > 1) { |
| 3784 | env->pc -= 2; |
| 3785 | env->icount_decr.u16.low++; |
| 3786 | env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL); |
| 3787 | } |
| 3788 | #endif |
| 3789 | /* This should never happen. */ |
| 3790 | if (n > CF_COUNT_MASK) |
| 3791 | cpu_abort(env, "TB too big during recompile"); |
| 3792 | |
| 3793 | cflags = n | CF_LAST_IO; |
| 3794 | pc = tb->pc; |
| 3795 | cs_base = tb->cs_base; |
| 3796 | flags = tb->flags; |
| 3797 | tb_phys_invalidate(tb, -1); |
| 3798 | /* FIXME: In theory this could raise an exception. In practice |
| 3799 | we have already translated the block once so it's probably ok. */ |
| 3800 | tb_gen_code(env, pc, cs_base, flags, cflags); |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 3801 | /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 3802 | the first in the TB) then we end up generating a whole new TB and |
| 3803 | repeating the fault, which is horribly inefficient. |
| 3804 | Better would be to execute just this insn uncached, or generate a |
| 3805 | second new TB. */ |
| 3806 | cpu_resume_from_signal(env, NULL); |
| 3807 | } |
| 3808 | |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 3809 | void dump_exec_info(FILE *f, |
| 3810 | int (*cpu_fprintf)(FILE *f, const char *fmt, ...)) |
| 3811 | { |
| 3812 | int i, target_code_size, max_target_code_size; |
| 3813 | int direct_jmp_count, direct_jmp2_count, cross_page; |
| 3814 | TranslationBlock *tb; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3815 | |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 3816 | target_code_size = 0; |
| 3817 | max_target_code_size = 0; |
| 3818 | cross_page = 0; |
| 3819 | direct_jmp_count = 0; |
| 3820 | direct_jmp2_count = 0; |
| 3821 | for(i = 0; i < nb_tbs; i++) { |
| 3822 | tb = &tbs[i]; |
| 3823 | target_code_size += tb->size; |
| 3824 | if (tb->size > max_target_code_size) |
| 3825 | max_target_code_size = tb->size; |
| 3826 | if (tb->page_addr[1] != -1) |
| 3827 | cross_page++; |
| 3828 | if (tb->tb_next_offset[0] != 0xffff) { |
| 3829 | direct_jmp_count++; |
| 3830 | if (tb->tb_next_offset[1] != 0xffff) { |
| 3831 | direct_jmp2_count++; |
| 3832 | } |
| 3833 | } |
| 3834 | } |
| 3835 | /* XXX: avoid using doubles ? */ |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 3836 | cpu_fprintf(f, "Translation buffer state:\n"); |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 3837 | cpu_fprintf(f, "gen code size %ld/%ld\n", |
| 3838 | code_gen_ptr - code_gen_buffer, code_gen_buffer_max_size); |
| 3839 | cpu_fprintf(f, "TB count %d/%d\n", |
| 3840 | nb_tbs, code_gen_max_blocks); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3841 | cpu_fprintf(f, "TB avg target size %d max=%d bytes\n", |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 3842 | nb_tbs ? target_code_size / nb_tbs : 0, |
| 3843 | max_target_code_size); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3844 | 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] | 3845 | nb_tbs ? (code_gen_ptr - code_gen_buffer) / nb_tbs : 0, |
| 3846 | 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] | 3847 | cpu_fprintf(f, "cross page TB count %d (%d%%)\n", |
| 3848 | cross_page, |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 3849 | nb_tbs ? (cross_page * 100) / nb_tbs : 0); |
| 3850 | cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n", |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3851 | direct_jmp_count, |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 3852 | nb_tbs ? (direct_jmp_count * 100) / nb_tbs : 0, |
| 3853 | direct_jmp2_count, |
| 3854 | nb_tbs ? (direct_jmp2_count * 100) / nb_tbs : 0); |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 3855 | cpu_fprintf(f, "\nStatistics:\n"); |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 3856 | cpu_fprintf(f, "TB flush count %d\n", tb_flush_count); |
| 3857 | cpu_fprintf(f, "TB invalidate count %d\n", tb_phys_invalidate_count); |
| 3858 | cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count); |
bellard | b67d9a5 | 2008-05-23 09:57:34 +0000 | [diff] [blame] | 3859 | tcg_dump_info(f, cpu_fprintf); |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 3860 | } |
| 3861 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3862 | #if !defined(CONFIG_USER_ONLY) |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 3863 | |
| 3864 | #define MMUSUFFIX _cmmu |
| 3865 | #define GETPC() NULL |
| 3866 | #define env cpu_single_env |
bellard | b769d8f | 2004-10-03 15:07:13 +0000 | [diff] [blame] | 3867 | #define SOFTMMU_CODE_ACCESS |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 3868 | |
| 3869 | #define SHIFT 0 |
| 3870 | #include "softmmu_template.h" |
| 3871 | |
| 3872 | #define SHIFT 1 |
| 3873 | #include "softmmu_template.h" |
| 3874 | |
| 3875 | #define SHIFT 2 |
| 3876 | #include "softmmu_template.h" |
| 3877 | |
| 3878 | #define SHIFT 3 |
| 3879 | #include "softmmu_template.h" |
| 3880 | |
| 3881 | #undef env |
| 3882 | |
| 3883 | #endif |