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