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