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 | |
Stefan Weil | 055403b | 2010-10-22 23:03:32 +0200 | [diff] [blame] | 27 | #include "qemu-common.h" |
bellard | 6180a18 | 2003-09-30 21:04:53 +0000 | [diff] [blame] | 28 | #include "cpu.h" |
bellard | b67d9a5 | 2008-05-23 09:57:34 +0000 | [diff] [blame] | 29 | #include "tcg.h" |
pbrook | b3c7724 | 2008-06-30 16:31:04 +0000 | [diff] [blame] | 30 | #include "hw/hw.h" |
Alex Williamson | cc9e98c | 2010-06-25 11:09:43 -0600 | [diff] [blame] | 31 | #include "hw/qdev.h" |
aliguori | 7457619 | 2008-10-06 14:02:03 +0000 | [diff] [blame] | 32 | #include "osdep.h" |
aliguori | 7ba1e61 | 2008-11-05 16:04:33 +0000 | [diff] [blame] | 33 | #include "kvm.h" |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 34 | #include "hw/xen.h" |
Blue Swirl | 29e922b | 2010-03-29 19:24:00 +0000 | [diff] [blame] | 35 | #include "qemu-timer.h" |
Avi Kivity | 62152b8 | 2011-07-26 14:26:14 +0300 | [diff] [blame] | 36 | #include "memory.h" |
| 37 | #include "exec-memory.h" |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 38 | #if defined(CONFIG_USER_ONLY) |
| 39 | #include <qemu.h> |
Juergen Lock | f01576f | 2010-03-25 22:32:16 +0100 | [diff] [blame] | 40 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
| 41 | #include <sys/param.h> |
| 42 | #if __FreeBSD_version >= 700104 |
| 43 | #define HAVE_KINFO_GETVMMAP |
| 44 | #define sigqueue sigqueue_freebsd /* avoid redefinition */ |
| 45 | #include <sys/time.h> |
| 46 | #include <sys/proc.h> |
| 47 | #include <machine/profile.h> |
| 48 | #define _KERNEL |
| 49 | #include <sys/user.h> |
| 50 | #undef _KERNEL |
| 51 | #undef sigqueue |
| 52 | #include <libutil.h> |
| 53 | #endif |
| 54 | #endif |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 55 | #else /* !CONFIG_USER_ONLY */ |
| 56 | #include "xen-mapcache.h" |
Stefano Stabellini | 6506e4f | 2011-05-19 18:35:44 +0100 | [diff] [blame] | 57 | #include "trace.h" |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 58 | #endif |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 59 | |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 60 | //#define DEBUG_TB_INVALIDATE |
bellard | 66e85a2 | 2003-06-24 13:28:12 +0000 | [diff] [blame] | 61 | //#define DEBUG_FLUSH |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 62 | //#define DEBUG_TLB |
pbrook | 67d3b95 | 2006-12-18 05:03:52 +0000 | [diff] [blame] | 63 | //#define DEBUG_UNASSIGNED |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 64 | |
| 65 | /* make various TB consistency checks */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 66 | //#define DEBUG_TB_CHECK |
| 67 | //#define DEBUG_TLB_CHECK |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 68 | |
ths | 1196be3 | 2007-03-17 15:17:58 +0000 | [diff] [blame] | 69 | //#define DEBUG_IOPORT |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 70 | //#define DEBUG_SUBPAGE |
ths | 1196be3 | 2007-03-17 15:17:58 +0000 | [diff] [blame] | 71 | |
pbrook | 99773bd | 2006-04-16 15:14:59 +0000 | [diff] [blame] | 72 | #if !defined(CONFIG_USER_ONLY) |
| 73 | /* TB consistency checks only implemented for usermode emulation. */ |
| 74 | #undef DEBUG_TB_CHECK |
| 75 | #endif |
| 76 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 77 | #define SMC_BITMAP_USE_THRESHOLD 10 |
| 78 | |
blueswir1 | bdaf78e | 2008-10-04 07:24:27 +0000 | [diff] [blame] | 79 | static TranslationBlock *tbs; |
Stefan Weil | 24ab68a | 2010-07-19 18:23:17 +0200 | [diff] [blame] | 80 | static int code_gen_max_blocks; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 81 | TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE]; |
blueswir1 | bdaf78e | 2008-10-04 07:24:27 +0000 | [diff] [blame] | 82 | static int nb_tbs; |
bellard | eb51d10 | 2003-05-14 21:51:13 +0000 | [diff] [blame] | 83 | /* 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] | 84 | spinlock_t tb_lock = SPIN_LOCK_UNLOCKED; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 85 | |
blueswir1 | 141ac46 | 2008-07-26 15:05:57 +0000 | [diff] [blame] | 86 | #if defined(__arm__) || defined(__sparc_v9__) |
| 87 | /* The prologue must be reachable with a direct jump. ARM and Sparc64 |
| 88 | have limited branch ranges (possibly also PPC) so place it in a |
blueswir1 | d03d860 | 2008-07-10 17:21:31 +0000 | [diff] [blame] | 89 | section close to code segment. */ |
| 90 | #define code_gen_section \ |
| 91 | __attribute__((__section__(".gen_code"))) \ |
| 92 | __attribute__((aligned (32))) |
Stefan Weil | f8e2af1 | 2009-06-18 23:04:48 +0200 | [diff] [blame] | 93 | #elif defined(_WIN32) |
| 94 | /* Maximum alignment for Win32 is 16. */ |
| 95 | #define code_gen_section \ |
| 96 | __attribute__((aligned (16))) |
blueswir1 | d03d860 | 2008-07-10 17:21:31 +0000 | [diff] [blame] | 97 | #else |
| 98 | #define code_gen_section \ |
| 99 | __attribute__((aligned (32))) |
| 100 | #endif |
| 101 | |
| 102 | uint8_t code_gen_prologue[1024] code_gen_section; |
blueswir1 | bdaf78e | 2008-10-04 07:24:27 +0000 | [diff] [blame] | 103 | static uint8_t *code_gen_buffer; |
| 104 | static unsigned long code_gen_buffer_size; |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 105 | /* threshold to flush the translated code buffer */ |
blueswir1 | bdaf78e | 2008-10-04 07:24:27 +0000 | [diff] [blame] | 106 | static unsigned long code_gen_buffer_max_size; |
Stefan Weil | 24ab68a | 2010-07-19 18:23:17 +0200 | [diff] [blame] | 107 | static uint8_t *code_gen_ptr; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 108 | |
pbrook | e2eef17 | 2008-06-08 01:09:01 +0000 | [diff] [blame] | 109 | #if !defined(CONFIG_USER_ONLY) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 110 | int phys_ram_fd; |
aliguori | 7457619 | 2008-10-06 14:02:03 +0000 | [diff] [blame] | 111 | static int in_migration; |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 112 | |
Alex Williamson | f471a17 | 2010-06-11 11:11:42 -0600 | [diff] [blame] | 113 | RAMList ram_list = { .blocks = QLIST_HEAD_INITIALIZER(ram_list) }; |
Avi Kivity | 62152b8 | 2011-07-26 14:26:14 +0300 | [diff] [blame] | 114 | |
| 115 | static MemoryRegion *system_memory; |
| 116 | |
pbrook | e2eef17 | 2008-06-08 01:09:01 +0000 | [diff] [blame] | 117 | #endif |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 118 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 119 | CPUState *first_cpu; |
| 120 | /* current CPU in the current thread. It is only valid inside |
| 121 | cpu_exec() */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 122 | CPUState *cpu_single_env; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 123 | /* 0 = Do not count executed instructions. |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 124 | 1 = Precise instruction counting. |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 125 | 2 = Adaptive rate instruction counting. */ |
| 126 | int use_icount = 0; |
| 127 | /* Current instruction counter. While executing translated code this may |
| 128 | include some instructions that have not yet been executed. */ |
| 129 | int64_t qemu_icount; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 130 | |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 131 | typedef struct PageDesc { |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 132 | /* list of TBs intersecting this ram page */ |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 133 | TranslationBlock *first_tb; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 134 | /* in order to optimize self modifying code, we count the number |
| 135 | of lookups we do to a given page to use a bitmap */ |
| 136 | unsigned int code_write_count; |
| 137 | uint8_t *code_bitmap; |
| 138 | #if defined(CONFIG_USER_ONLY) |
| 139 | unsigned long flags; |
| 140 | #endif |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 141 | } PageDesc; |
| 142 | |
Paul Brook | 41c1b1c | 2010-03-12 16:54:58 +0000 | [diff] [blame] | 143 | /* In system mode we want L1_MAP to be based on ram offsets, |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 144 | while in user mode we want it to be based on virtual addresses. */ |
| 145 | #if !defined(CONFIG_USER_ONLY) |
Paul Brook | 41c1b1c | 2010-03-12 16:54:58 +0000 | [diff] [blame] | 146 | #if HOST_LONG_BITS < TARGET_PHYS_ADDR_SPACE_BITS |
| 147 | # define L1_MAP_ADDR_SPACE_BITS HOST_LONG_BITS |
| 148 | #else |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 149 | # define L1_MAP_ADDR_SPACE_BITS TARGET_PHYS_ADDR_SPACE_BITS |
Paul Brook | 41c1b1c | 2010-03-12 16:54:58 +0000 | [diff] [blame] | 150 | #endif |
j_mayer | bedb69e | 2007-04-05 20:08:21 +0000 | [diff] [blame] | 151 | #else |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 152 | # define L1_MAP_ADDR_SPACE_BITS TARGET_VIRT_ADDR_SPACE_BITS |
j_mayer | bedb69e | 2007-04-05 20:08:21 +0000 | [diff] [blame] | 153 | #endif |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 154 | |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 155 | /* Size of the L2 (and L3, etc) page tables. */ |
| 156 | #define L2_BITS 10 |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 157 | #define L2_SIZE (1 << L2_BITS) |
| 158 | |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 159 | /* The bits remaining after N lower levels of page tables. */ |
| 160 | #define P_L1_BITS_REM \ |
| 161 | ((TARGET_PHYS_ADDR_SPACE_BITS - TARGET_PAGE_BITS) % L2_BITS) |
| 162 | #define V_L1_BITS_REM \ |
| 163 | ((L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS) % L2_BITS) |
| 164 | |
| 165 | /* Size of the L1 page table. Avoid silly small sizes. */ |
| 166 | #if P_L1_BITS_REM < 4 |
| 167 | #define P_L1_BITS (P_L1_BITS_REM + L2_BITS) |
| 168 | #else |
| 169 | #define P_L1_BITS P_L1_BITS_REM |
| 170 | #endif |
| 171 | |
| 172 | #if V_L1_BITS_REM < 4 |
| 173 | #define V_L1_BITS (V_L1_BITS_REM + L2_BITS) |
| 174 | #else |
| 175 | #define V_L1_BITS V_L1_BITS_REM |
| 176 | #endif |
| 177 | |
| 178 | #define P_L1_SIZE ((target_phys_addr_t)1 << P_L1_BITS) |
| 179 | #define V_L1_SIZE ((target_ulong)1 << V_L1_BITS) |
| 180 | |
| 181 | #define P_L1_SHIFT (TARGET_PHYS_ADDR_SPACE_BITS - TARGET_PAGE_BITS - P_L1_BITS) |
| 182 | #define V_L1_SHIFT (L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS - V_L1_BITS) |
| 183 | |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 184 | unsigned long qemu_real_host_page_size; |
| 185 | unsigned long qemu_host_page_bits; |
| 186 | unsigned long qemu_host_page_size; |
| 187 | unsigned long qemu_host_page_mask; |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 188 | |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 189 | /* This is a multi-level map on the virtual address space. |
| 190 | The bottom level has pointers to PageDesc. */ |
| 191 | static void *l1_map[V_L1_SIZE]; |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 192 | |
pbrook | e2eef17 | 2008-06-08 01:09:01 +0000 | [diff] [blame] | 193 | #if !defined(CONFIG_USER_ONLY) |
Paul Brook | 41c1b1c | 2010-03-12 16:54:58 +0000 | [diff] [blame] | 194 | typedef struct PhysPageDesc { |
| 195 | /* offset in host memory of the page + io_index in the low bits */ |
| 196 | ram_addr_t phys_offset; |
| 197 | ram_addr_t region_offset; |
| 198 | } PhysPageDesc; |
| 199 | |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 200 | /* This is a multi-level map on the physical address space. |
| 201 | The bottom level has pointers to PhysPageDesc. */ |
| 202 | static void *l1_phys_map[P_L1_SIZE]; |
Paul Brook | 6d9a130 | 2010-02-28 23:55:53 +0000 | [diff] [blame] | 203 | |
pbrook | e2eef17 | 2008-06-08 01:09:01 +0000 | [diff] [blame] | 204 | static void io_mem_init(void); |
Avi Kivity | 62152b8 | 2011-07-26 14:26:14 +0300 | [diff] [blame] | 205 | static void memory_map_init(void); |
pbrook | e2eef17 | 2008-06-08 01:09:01 +0000 | [diff] [blame] | 206 | |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 207 | /* io memory support */ |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 208 | CPUWriteMemoryFunc *io_mem_write[IO_MEM_NB_ENTRIES][4]; |
| 209 | CPUReadMemoryFunc *io_mem_read[IO_MEM_NB_ENTRIES][4]; |
bellard | a4193c8 | 2004-06-03 14:01:43 +0000 | [diff] [blame] | 210 | void *io_mem_opaque[IO_MEM_NB_ENTRIES]; |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 211 | static char io_mem_used[IO_MEM_NB_ENTRIES]; |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 212 | static int io_mem_watch; |
| 213 | #endif |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 214 | |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 215 | /* log support */ |
Juha Riihimäki | 1e8b27c | 2009-12-03 15:56:02 +0200 | [diff] [blame] | 216 | #ifdef WIN32 |
| 217 | static const char *logfilename = "qemu.log"; |
| 218 | #else |
blueswir1 | d9b630f | 2008-10-05 09:57:08 +0000 | [diff] [blame] | 219 | static const char *logfilename = "/tmp/qemu.log"; |
Juha Riihimäki | 1e8b27c | 2009-12-03 15:56:02 +0200 | [diff] [blame] | 220 | #endif |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 221 | FILE *logfile; |
| 222 | int loglevel; |
pbrook | e735b91 | 2007-06-30 13:53:24 +0000 | [diff] [blame] | 223 | static int log_append = 0; |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 224 | |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 225 | /* statistics */ |
Paul Brook | b3755a9 | 2010-03-12 16:54:58 +0000 | [diff] [blame] | 226 | #if !defined(CONFIG_USER_ONLY) |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 227 | static int tlb_flush_count; |
Paul Brook | b3755a9 | 2010-03-12 16:54:58 +0000 | [diff] [blame] | 228 | #endif |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 229 | static int tb_flush_count; |
| 230 | static int tb_phys_invalidate_count; |
| 231 | |
bellard | 7cb69ca | 2008-05-10 10:55:51 +0000 | [diff] [blame] | 232 | #ifdef _WIN32 |
| 233 | static void map_exec(void *addr, long size) |
| 234 | { |
| 235 | DWORD old_protect; |
| 236 | VirtualProtect(addr, size, |
| 237 | PAGE_EXECUTE_READWRITE, &old_protect); |
| 238 | |
| 239 | } |
| 240 | #else |
| 241 | static void map_exec(void *addr, long size) |
| 242 | { |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 243 | unsigned long start, end, page_size; |
bellard | 7cb69ca | 2008-05-10 10:55:51 +0000 | [diff] [blame] | 244 | |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 245 | page_size = getpagesize(); |
bellard | 7cb69ca | 2008-05-10 10:55:51 +0000 | [diff] [blame] | 246 | start = (unsigned long)addr; |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 247 | start &= ~(page_size - 1); |
bellard | 7cb69ca | 2008-05-10 10:55:51 +0000 | [diff] [blame] | 248 | |
| 249 | end = (unsigned long)addr + size; |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 250 | end += page_size - 1; |
| 251 | end &= ~(page_size - 1); |
bellard | 7cb69ca | 2008-05-10 10:55:51 +0000 | [diff] [blame] | 252 | |
| 253 | mprotect((void *)start, end - start, |
| 254 | PROT_READ | PROT_WRITE | PROT_EXEC); |
| 255 | } |
| 256 | #endif |
| 257 | |
bellard | b346ff4 | 2003-06-15 20:05:50 +0000 | [diff] [blame] | 258 | static void page_init(void) |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 259 | { |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 260 | /* NOTE: we can always suppose that qemu_host_page_size >= |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 261 | TARGET_PAGE_SIZE */ |
aliguori | c2b48b6 | 2008-11-11 22:06:42 +0000 | [diff] [blame] | 262 | #ifdef _WIN32 |
| 263 | { |
| 264 | SYSTEM_INFO system_info; |
| 265 | |
| 266 | GetSystemInfo(&system_info); |
| 267 | qemu_real_host_page_size = system_info.dwPageSize; |
| 268 | } |
| 269 | #else |
| 270 | qemu_real_host_page_size = getpagesize(); |
| 271 | #endif |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 272 | if (qemu_host_page_size == 0) |
| 273 | qemu_host_page_size = qemu_real_host_page_size; |
| 274 | if (qemu_host_page_size < TARGET_PAGE_SIZE) |
| 275 | qemu_host_page_size = TARGET_PAGE_SIZE; |
| 276 | qemu_host_page_bits = 0; |
| 277 | while ((1 << qemu_host_page_bits) < qemu_host_page_size) |
| 278 | qemu_host_page_bits++; |
| 279 | qemu_host_page_mask = ~(qemu_host_page_size - 1); |
balrog | 50a9569 | 2007-12-12 01:16:23 +0000 | [diff] [blame] | 280 | |
Paul Brook | 2e9a571 | 2010-05-05 16:32:59 +0100 | [diff] [blame] | 281 | #if defined(CONFIG_BSD) && defined(CONFIG_USER_ONLY) |
balrog | 50a9569 | 2007-12-12 01:16:23 +0000 | [diff] [blame] | 282 | { |
Juergen Lock | f01576f | 2010-03-25 22:32:16 +0100 | [diff] [blame] | 283 | #ifdef HAVE_KINFO_GETVMMAP |
| 284 | struct kinfo_vmentry *freep; |
| 285 | int i, cnt; |
| 286 | |
| 287 | freep = kinfo_getvmmap(getpid(), &cnt); |
| 288 | if (freep) { |
| 289 | mmap_lock(); |
| 290 | for (i = 0; i < cnt; i++) { |
| 291 | unsigned long startaddr, endaddr; |
| 292 | |
| 293 | startaddr = freep[i].kve_start; |
| 294 | endaddr = freep[i].kve_end; |
| 295 | if (h2g_valid(startaddr)) { |
| 296 | startaddr = h2g(startaddr) & TARGET_PAGE_MASK; |
| 297 | |
| 298 | if (h2g_valid(endaddr)) { |
| 299 | endaddr = h2g(endaddr); |
Aurelien Jarno | fd43690 | 2010-04-10 17:20:36 +0200 | [diff] [blame] | 300 | page_set_flags(startaddr, endaddr, PAGE_RESERVED); |
Juergen Lock | f01576f | 2010-03-25 22:32:16 +0100 | [diff] [blame] | 301 | } else { |
| 302 | #if TARGET_ABI_BITS <= L1_MAP_ADDR_SPACE_BITS |
| 303 | endaddr = ~0ul; |
Aurelien Jarno | fd43690 | 2010-04-10 17:20:36 +0200 | [diff] [blame] | 304 | page_set_flags(startaddr, endaddr, PAGE_RESERVED); |
Juergen Lock | f01576f | 2010-03-25 22:32:16 +0100 | [diff] [blame] | 305 | #endif |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | free(freep); |
| 310 | mmap_unlock(); |
| 311 | } |
| 312 | #else |
balrog | 50a9569 | 2007-12-12 01:16:23 +0000 | [diff] [blame] | 313 | FILE *f; |
balrog | 50a9569 | 2007-12-12 01:16:23 +0000 | [diff] [blame] | 314 | |
pbrook | 0776590 | 2008-05-31 16:33:53 +0000 | [diff] [blame] | 315 | last_brk = (unsigned long)sbrk(0); |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 316 | |
Aurelien Jarno | fd43690 | 2010-04-10 17:20:36 +0200 | [diff] [blame] | 317 | f = fopen("/compat/linux/proc/self/maps", "r"); |
balrog | 50a9569 | 2007-12-12 01:16:23 +0000 | [diff] [blame] | 318 | if (f) { |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 319 | mmap_lock(); |
| 320 | |
balrog | 50a9569 | 2007-12-12 01:16:23 +0000 | [diff] [blame] | 321 | do { |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 322 | unsigned long startaddr, endaddr; |
| 323 | int n; |
| 324 | |
| 325 | n = fscanf (f, "%lx-%lx %*[^\n]\n", &startaddr, &endaddr); |
| 326 | |
| 327 | if (n == 2 && h2g_valid(startaddr)) { |
| 328 | startaddr = h2g(startaddr) & TARGET_PAGE_MASK; |
| 329 | |
| 330 | if (h2g_valid(endaddr)) { |
| 331 | endaddr = h2g(endaddr); |
| 332 | } else { |
| 333 | endaddr = ~0ul; |
| 334 | } |
| 335 | page_set_flags(startaddr, endaddr, PAGE_RESERVED); |
balrog | 50a9569 | 2007-12-12 01:16:23 +0000 | [diff] [blame] | 336 | } |
| 337 | } while (!feof(f)); |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 338 | |
balrog | 50a9569 | 2007-12-12 01:16:23 +0000 | [diff] [blame] | 339 | fclose(f); |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 340 | mmap_unlock(); |
balrog | 50a9569 | 2007-12-12 01:16:23 +0000 | [diff] [blame] | 341 | } |
Juergen Lock | f01576f | 2010-03-25 22:32:16 +0100 | [diff] [blame] | 342 | #endif |
balrog | 50a9569 | 2007-12-12 01:16:23 +0000 | [diff] [blame] | 343 | } |
| 344 | #endif |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Paul Brook | 41c1b1c | 2010-03-12 16:54:58 +0000 | [diff] [blame] | 347 | static PageDesc *page_find_alloc(tb_page_addr_t index, int alloc) |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 348 | { |
Paul Brook | 41c1b1c | 2010-03-12 16:54:58 +0000 | [diff] [blame] | 349 | PageDesc *pd; |
| 350 | void **lp; |
| 351 | int i; |
| 352 | |
pbrook | 17e2377 | 2008-06-09 13:47:45 +0000 | [diff] [blame] | 353 | #if defined(CONFIG_USER_ONLY) |
Paul Brook | 2e9a571 | 2010-05-05 16:32:59 +0100 | [diff] [blame] | 354 | /* We can't use qemu_malloc because it may recurse into a locked mutex. */ |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 355 | # define ALLOC(P, SIZE) \ |
| 356 | do { \ |
| 357 | P = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, \ |
| 358 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); \ |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 359 | } while (0) |
pbrook | 17e2377 | 2008-06-09 13:47:45 +0000 | [diff] [blame] | 360 | #else |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 361 | # define ALLOC(P, SIZE) \ |
| 362 | do { P = qemu_mallocz(SIZE); } while (0) |
pbrook | 17e2377 | 2008-06-09 13:47:45 +0000 | [diff] [blame] | 363 | #endif |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 364 | |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 365 | /* Level 1. Always allocated. */ |
| 366 | lp = l1_map + ((index >> V_L1_SHIFT) & (V_L1_SIZE - 1)); |
| 367 | |
| 368 | /* Level 2..N-1. */ |
| 369 | for (i = V_L1_SHIFT / L2_BITS - 1; i > 0; i--) { |
| 370 | void **p = *lp; |
| 371 | |
| 372 | if (p == NULL) { |
| 373 | if (!alloc) { |
| 374 | return NULL; |
| 375 | } |
| 376 | ALLOC(p, sizeof(void *) * L2_SIZE); |
| 377 | *lp = p; |
| 378 | } |
| 379 | |
| 380 | lp = p + ((index >> (i * L2_BITS)) & (L2_SIZE - 1)); |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 381 | } |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 382 | |
| 383 | pd = *lp; |
| 384 | if (pd == NULL) { |
| 385 | if (!alloc) { |
| 386 | return NULL; |
| 387 | } |
| 388 | ALLOC(pd, sizeof(PageDesc) * L2_SIZE); |
| 389 | *lp = pd; |
| 390 | } |
| 391 | |
| 392 | #undef ALLOC |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 393 | |
| 394 | return pd + (index & (L2_SIZE - 1)); |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Paul Brook | 41c1b1c | 2010-03-12 16:54:58 +0000 | [diff] [blame] | 397 | static inline PageDesc *page_find(tb_page_addr_t index) |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 398 | { |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 399 | return page_find_alloc(index, 0); |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Paul Brook | 6d9a130 | 2010-02-28 23:55:53 +0000 | [diff] [blame] | 402 | #if !defined(CONFIG_USER_ONLY) |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 403 | static PhysPageDesc *phys_page_find_alloc(target_phys_addr_t index, int alloc) |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 404 | { |
pbrook | e3f4e2a | 2006-04-08 20:02:06 +0000 | [diff] [blame] | 405 | PhysPageDesc *pd; |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 406 | void **lp; |
| 407 | int i; |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 408 | |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 409 | /* Level 1. Always allocated. */ |
| 410 | lp = l1_phys_map + ((index >> P_L1_SHIFT) & (P_L1_SIZE - 1)); |
bellard | 108c49b | 2005-07-24 12:55:09 +0000 | [diff] [blame] | 411 | |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 412 | /* Level 2..N-1. */ |
| 413 | for (i = P_L1_SHIFT / L2_BITS - 1; i > 0; i--) { |
| 414 | void **p = *lp; |
| 415 | if (p == NULL) { |
| 416 | if (!alloc) { |
| 417 | return NULL; |
| 418 | } |
| 419 | *lp = p = qemu_mallocz(sizeof(void *) * L2_SIZE); |
| 420 | } |
| 421 | lp = p + ((index >> (i * L2_BITS)) & (L2_SIZE - 1)); |
bellard | 108c49b | 2005-07-24 12:55:09 +0000 | [diff] [blame] | 422 | } |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 423 | |
pbrook | e3f4e2a | 2006-04-08 20:02:06 +0000 | [diff] [blame] | 424 | pd = *lp; |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 425 | if (pd == NULL) { |
pbrook | e3f4e2a | 2006-04-08 20:02:06 +0000 | [diff] [blame] | 426 | int i; |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 427 | |
| 428 | if (!alloc) { |
bellard | 108c49b | 2005-07-24 12:55:09 +0000 | [diff] [blame] | 429 | return NULL; |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | *lp = pd = qemu_malloc(sizeof(PhysPageDesc) * L2_SIZE); |
| 433 | |
pbrook | 67c4d23 | 2009-02-23 13:16:07 +0000 | [diff] [blame] | 434 | for (i = 0; i < L2_SIZE; i++) { |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 435 | pd[i].phys_offset = IO_MEM_UNASSIGNED; |
| 436 | pd[i].region_offset = (index + i) << TARGET_PAGE_BITS; |
pbrook | 67c4d23 | 2009-02-23 13:16:07 +0000 | [diff] [blame] | 437 | } |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 438 | } |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 439 | |
| 440 | return pd + (index & (L2_SIZE - 1)); |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 443 | static inline PhysPageDesc *phys_page_find(target_phys_addr_t index) |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 444 | { |
bellard | 108c49b | 2005-07-24 12:55:09 +0000 | [diff] [blame] | 445 | return phys_page_find_alloc(index, 0); |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 448 | static void tlb_protect_code(ram_addr_t ram_addr); |
| 449 | static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr, |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 450 | target_ulong vaddr); |
pbrook | c8a706f | 2008-06-02 16:16:42 +0000 | [diff] [blame] | 451 | #define mmap_lock() do { } while(0) |
| 452 | #define mmap_unlock() do { } while(0) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 453 | #endif |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 454 | |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 455 | #define DEFAULT_CODE_GEN_BUFFER_SIZE (32 * 1024 * 1024) |
| 456 | |
| 457 | #if defined(CONFIG_USER_ONLY) |
Stuart Brady | ccbb4d4 | 2009-05-03 12:15:06 +0100 | [diff] [blame] | 458 | /* Currently it is not recommended to allocate big chunks of data in |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 459 | user mode. It will change when a dedicated libc will be used */ |
| 460 | #define USE_STATIC_CODE_GEN_BUFFER |
| 461 | #endif |
| 462 | |
| 463 | #ifdef USE_STATIC_CODE_GEN_BUFFER |
Aurelien Jarno | ebf50fb | 2010-03-29 02:12:51 +0200 | [diff] [blame] | 464 | static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE] |
| 465 | __attribute__((aligned (CODE_GEN_ALIGN))); |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 466 | #endif |
| 467 | |
blueswir1 | 8fcd369 | 2008-08-17 20:26:25 +0000 | [diff] [blame] | 468 | static void code_gen_alloc(unsigned long tb_size) |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 469 | { |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 470 | #ifdef USE_STATIC_CODE_GEN_BUFFER |
| 471 | code_gen_buffer = static_code_gen_buffer; |
| 472 | code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE; |
| 473 | map_exec(code_gen_buffer, code_gen_buffer_size); |
| 474 | #else |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 475 | code_gen_buffer_size = tb_size; |
| 476 | if (code_gen_buffer_size == 0) { |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 477 | #if defined(CONFIG_USER_ONLY) |
| 478 | /* in user mode, phys_ram_size is not meaningful */ |
| 479 | code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE; |
| 480 | #else |
Stuart Brady | ccbb4d4 | 2009-05-03 12:15:06 +0100 | [diff] [blame] | 481 | /* XXX: needs adjustments */ |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 482 | code_gen_buffer_size = (unsigned long)(ram_size / 4); |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 483 | #endif |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 484 | } |
| 485 | if (code_gen_buffer_size < MIN_CODE_GEN_BUFFER_SIZE) |
| 486 | code_gen_buffer_size = MIN_CODE_GEN_BUFFER_SIZE; |
| 487 | /* The code gen buffer location may have constraints depending on |
| 488 | the host cpu and OS */ |
| 489 | #if defined(__linux__) |
| 490 | { |
| 491 | int flags; |
blueswir1 | 141ac46 | 2008-07-26 15:05:57 +0000 | [diff] [blame] | 492 | void *start = NULL; |
| 493 | |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 494 | flags = MAP_PRIVATE | MAP_ANONYMOUS; |
| 495 | #if defined(__x86_64__) |
| 496 | flags |= MAP_32BIT; |
| 497 | /* Cannot map more than that */ |
| 498 | if (code_gen_buffer_size > (800 * 1024 * 1024)) |
| 499 | code_gen_buffer_size = (800 * 1024 * 1024); |
blueswir1 | 141ac46 | 2008-07-26 15:05:57 +0000 | [diff] [blame] | 500 | #elif defined(__sparc_v9__) |
| 501 | // Map the buffer below 2G, so we can use direct calls and branches |
| 502 | flags |= MAP_FIXED; |
| 503 | start = (void *) 0x60000000UL; |
| 504 | if (code_gen_buffer_size > (512 * 1024 * 1024)) |
| 505 | code_gen_buffer_size = (512 * 1024 * 1024); |
balrog | 1cb0661 | 2008-12-01 02:10:17 +0000 | [diff] [blame] | 506 | #elif defined(__arm__) |
balrog | 63d4124 | 2008-12-01 02:19:41 +0000 | [diff] [blame] | 507 | /* Map the buffer below 32M, so we can use direct calls and branches */ |
balrog | 1cb0661 | 2008-12-01 02:10:17 +0000 | [diff] [blame] | 508 | flags |= MAP_FIXED; |
| 509 | start = (void *) 0x01000000UL; |
| 510 | if (code_gen_buffer_size > 16 * 1024 * 1024) |
| 511 | code_gen_buffer_size = 16 * 1024 * 1024; |
Richard Henderson | eba0b89 | 2010-06-04 12:14:14 -0700 | [diff] [blame] | 512 | #elif defined(__s390x__) |
| 513 | /* Map the buffer so that we can use direct calls and branches. */ |
| 514 | /* We have a +- 4GB range on the branches; leave some slop. */ |
| 515 | if (code_gen_buffer_size > (3ul * 1024 * 1024 * 1024)) { |
| 516 | code_gen_buffer_size = 3ul * 1024 * 1024 * 1024; |
| 517 | } |
| 518 | start = (void *)0x90000000UL; |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 519 | #endif |
blueswir1 | 141ac46 | 2008-07-26 15:05:57 +0000 | [diff] [blame] | 520 | code_gen_buffer = mmap(start, code_gen_buffer_size, |
| 521 | PROT_WRITE | PROT_READ | PROT_EXEC, |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 522 | flags, -1, 0); |
| 523 | if (code_gen_buffer == MAP_FAILED) { |
| 524 | fprintf(stderr, "Could not allocate dynamic translator buffer\n"); |
| 525 | exit(1); |
| 526 | } |
| 527 | } |
Brad | cbb608a | 2010-12-20 21:25:40 -0500 | [diff] [blame] | 528 | #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \ |
| 529 | || defined(__DragonFly__) || defined(__OpenBSD__) |
aliguori | 06e67a8 | 2008-09-27 15:32:41 +0000 | [diff] [blame] | 530 | { |
| 531 | int flags; |
| 532 | void *addr = NULL; |
| 533 | flags = MAP_PRIVATE | MAP_ANONYMOUS; |
| 534 | #if defined(__x86_64__) |
| 535 | /* FreeBSD doesn't have MAP_32BIT, use MAP_FIXED and assume |
| 536 | * 0x40000000 is free */ |
| 537 | flags |= MAP_FIXED; |
| 538 | addr = (void *)0x40000000; |
| 539 | /* Cannot map more than that */ |
| 540 | if (code_gen_buffer_size > (800 * 1024 * 1024)) |
| 541 | code_gen_buffer_size = (800 * 1024 * 1024); |
Blue Swirl | 4cd31ad | 2011-01-16 08:32:27 +0000 | [diff] [blame] | 542 | #elif defined(__sparc_v9__) |
| 543 | // Map the buffer below 2G, so we can use direct calls and branches |
| 544 | flags |= MAP_FIXED; |
| 545 | addr = (void *) 0x60000000UL; |
| 546 | if (code_gen_buffer_size > (512 * 1024 * 1024)) { |
| 547 | code_gen_buffer_size = (512 * 1024 * 1024); |
| 548 | } |
aliguori | 06e67a8 | 2008-09-27 15:32:41 +0000 | [diff] [blame] | 549 | #endif |
| 550 | code_gen_buffer = mmap(addr, code_gen_buffer_size, |
| 551 | PROT_WRITE | PROT_READ | PROT_EXEC, |
| 552 | flags, -1, 0); |
| 553 | if (code_gen_buffer == MAP_FAILED) { |
| 554 | fprintf(stderr, "Could not allocate dynamic translator buffer\n"); |
| 555 | exit(1); |
| 556 | } |
| 557 | } |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 558 | #else |
| 559 | code_gen_buffer = qemu_malloc(code_gen_buffer_size); |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 560 | map_exec(code_gen_buffer, code_gen_buffer_size); |
| 561 | #endif |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 562 | #endif /* !USE_STATIC_CODE_GEN_BUFFER */ |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 563 | map_exec(code_gen_prologue, sizeof(code_gen_prologue)); |
Peter Maydell | a884da8 | 2011-06-22 11:58:25 +0100 | [diff] [blame] | 564 | code_gen_buffer_max_size = code_gen_buffer_size - |
| 565 | (TCG_MAX_OP_SIZE * OPC_BUF_SIZE); |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 566 | code_gen_max_blocks = code_gen_buffer_size / CODE_GEN_AVG_BLOCK_SIZE; |
| 567 | tbs = qemu_malloc(code_gen_max_blocks * sizeof(TranslationBlock)); |
| 568 | } |
| 569 | |
| 570 | /* Must be called before using the QEMU cpus. 'tb_size' is the size |
| 571 | (in bytes) allocated to the translation buffer. Zero means default |
| 572 | size. */ |
Jan Kiszka | d5ab971 | 2011-08-02 16:10:21 +0200 | [diff] [blame^] | 573 | void tcg_exec_init(unsigned long tb_size) |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 574 | { |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 575 | cpu_gen_init(); |
| 576 | code_gen_alloc(tb_size); |
| 577 | code_gen_ptr = code_gen_buffer; |
bellard | 4369415 | 2008-05-29 09:35:57 +0000 | [diff] [blame] | 578 | page_init(); |
Richard Henderson | 9002ec7 | 2010-05-06 08:50:41 -0700 | [diff] [blame] | 579 | #if !defined(CONFIG_USER_ONLY) || !defined(CONFIG_USE_GUEST_BASE) |
| 580 | /* There's no guest base to take into account, so go ahead and |
| 581 | initialize the prologue now. */ |
| 582 | tcg_prologue_init(&tcg_ctx); |
| 583 | #endif |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Jan Kiszka | d5ab971 | 2011-08-02 16:10:21 +0200 | [diff] [blame^] | 586 | bool tcg_enabled(void) |
| 587 | { |
| 588 | return code_gen_buffer != NULL; |
| 589 | } |
| 590 | |
| 591 | void cpu_exec_init_all(void) |
| 592 | { |
| 593 | #if !defined(CONFIG_USER_ONLY) |
| 594 | memory_map_init(); |
| 595 | io_mem_init(); |
| 596 | #endif |
| 597 | } |
| 598 | |
pbrook | 9656f32 | 2008-07-01 20:01:19 +0000 | [diff] [blame] | 599 | #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY) |
| 600 | |
Juan Quintela | e59fb37 | 2009-09-29 22:48:21 +0200 | [diff] [blame] | 601 | static int cpu_common_post_load(void *opaque, int version_id) |
Juan Quintela | e7f4eff | 2009-09-10 03:04:33 +0200 | [diff] [blame] | 602 | { |
| 603 | CPUState *env = opaque; |
| 604 | |
aurel32 | 3098dba | 2009-03-07 21:28:24 +0000 | [diff] [blame] | 605 | /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the |
| 606 | version_id is increased. */ |
| 607 | env->interrupt_request &= ~0x01; |
pbrook | 9656f32 | 2008-07-01 20:01:19 +0000 | [diff] [blame] | 608 | tlb_flush(env, 1); |
| 609 | |
| 610 | return 0; |
| 611 | } |
Juan Quintela | e7f4eff | 2009-09-10 03:04:33 +0200 | [diff] [blame] | 612 | |
| 613 | static const VMStateDescription vmstate_cpu_common = { |
| 614 | .name = "cpu_common", |
| 615 | .version_id = 1, |
| 616 | .minimum_version_id = 1, |
| 617 | .minimum_version_id_old = 1, |
Juan Quintela | e7f4eff | 2009-09-10 03:04:33 +0200 | [diff] [blame] | 618 | .post_load = cpu_common_post_load, |
| 619 | .fields = (VMStateField []) { |
| 620 | VMSTATE_UINT32(halted, CPUState), |
| 621 | VMSTATE_UINT32(interrupt_request, CPUState), |
| 622 | VMSTATE_END_OF_LIST() |
| 623 | } |
| 624 | }; |
pbrook | 9656f32 | 2008-07-01 20:01:19 +0000 | [diff] [blame] | 625 | #endif |
| 626 | |
Glauber Costa | 950f147 | 2009-06-09 12:15:18 -0400 | [diff] [blame] | 627 | CPUState *qemu_get_cpu(int cpu) |
| 628 | { |
| 629 | CPUState *env = first_cpu; |
| 630 | |
| 631 | while (env) { |
| 632 | if (env->cpu_index == cpu) |
| 633 | break; |
| 634 | env = env->next_cpu; |
| 635 | } |
| 636 | |
| 637 | return env; |
| 638 | } |
| 639 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 640 | void cpu_exec_init(CPUState *env) |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 641 | { |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 642 | CPUState **penv; |
| 643 | int cpu_index; |
| 644 | |
pbrook | c276471 | 2009-03-07 15:24:59 +0000 | [diff] [blame] | 645 | #if defined(CONFIG_USER_ONLY) |
| 646 | cpu_list_lock(); |
| 647 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 648 | env->next_cpu = NULL; |
| 649 | penv = &first_cpu; |
| 650 | cpu_index = 0; |
| 651 | while (*penv != NULL) { |
Nathan Froyd | 1e9fa73 | 2009-06-03 11:33:08 -0700 | [diff] [blame] | 652 | penv = &(*penv)->next_cpu; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 653 | cpu_index++; |
| 654 | } |
| 655 | env->cpu_index = cpu_index; |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 656 | env->numa_node = 0; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 657 | QTAILQ_INIT(&env->breakpoints); |
| 658 | QTAILQ_INIT(&env->watchpoints); |
Jan Kiszka | dc7a09c | 2011-03-15 12:26:31 +0100 | [diff] [blame] | 659 | #ifndef CONFIG_USER_ONLY |
| 660 | env->thread_id = qemu_get_thread_id(); |
| 661 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 662 | *penv = env; |
pbrook | c276471 | 2009-03-07 15:24:59 +0000 | [diff] [blame] | 663 | #if defined(CONFIG_USER_ONLY) |
| 664 | cpu_list_unlock(); |
| 665 | #endif |
pbrook | b3c7724 | 2008-06-30 16:31:04 +0000 | [diff] [blame] | 666 | #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY) |
Alex Williamson | 0be71e3 | 2010-06-25 11:09:07 -0600 | [diff] [blame] | 667 | vmstate_register(NULL, cpu_index, &vmstate_cpu_common, env); |
| 668 | register_savevm(NULL, "cpu", cpu_index, CPU_SAVE_VERSION, |
pbrook | b3c7724 | 2008-06-30 16:31:04 +0000 | [diff] [blame] | 669 | cpu_save, cpu_load, env); |
| 670 | #endif |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Tristan Gingold | d1a1eb7 | 2011-02-10 10:04:57 +0100 | [diff] [blame] | 673 | /* Allocate a new translation block. Flush the translation buffer if |
| 674 | too many translation blocks or too much generated code. */ |
| 675 | static TranslationBlock *tb_alloc(target_ulong pc) |
| 676 | { |
| 677 | TranslationBlock *tb; |
| 678 | |
| 679 | if (nb_tbs >= code_gen_max_blocks || |
| 680 | (code_gen_ptr - code_gen_buffer) >= code_gen_buffer_max_size) |
| 681 | return NULL; |
| 682 | tb = &tbs[nb_tbs++]; |
| 683 | tb->pc = pc; |
| 684 | tb->cflags = 0; |
| 685 | return tb; |
| 686 | } |
| 687 | |
| 688 | void tb_free(TranslationBlock *tb) |
| 689 | { |
| 690 | /* In practice this is mostly used for single use temporary TB |
| 691 | Ignore the hard cases and just back up if this TB happens to |
| 692 | be the last one generated. */ |
| 693 | if (nb_tbs > 0 && tb == &tbs[nb_tbs - 1]) { |
| 694 | code_gen_ptr = tb->tc_ptr; |
| 695 | nb_tbs--; |
| 696 | } |
| 697 | } |
| 698 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 699 | static inline void invalidate_page_bitmap(PageDesc *p) |
| 700 | { |
| 701 | if (p->code_bitmap) { |
bellard | 59817cc | 2004-02-16 22:01:13 +0000 | [diff] [blame] | 702 | qemu_free(p->code_bitmap); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 703 | p->code_bitmap = NULL; |
| 704 | } |
| 705 | p->code_write_count = 0; |
| 706 | } |
| 707 | |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 708 | /* Set to NULL all the 'first_tb' fields in all PageDescs. */ |
| 709 | |
| 710 | static void page_flush_tb_1 (int level, void **lp) |
| 711 | { |
| 712 | int i; |
| 713 | |
| 714 | if (*lp == NULL) { |
| 715 | return; |
| 716 | } |
| 717 | if (level == 0) { |
| 718 | PageDesc *pd = *lp; |
Paul Brook | 7296aba | 2010-03-14 14:58:46 +0000 | [diff] [blame] | 719 | for (i = 0; i < L2_SIZE; ++i) { |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 720 | pd[i].first_tb = NULL; |
| 721 | invalidate_page_bitmap(pd + i); |
| 722 | } |
| 723 | } else { |
| 724 | void **pp = *lp; |
Paul Brook | 7296aba | 2010-03-14 14:58:46 +0000 | [diff] [blame] | 725 | for (i = 0; i < L2_SIZE; ++i) { |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 726 | page_flush_tb_1 (level - 1, pp + i); |
| 727 | } |
| 728 | } |
| 729 | } |
| 730 | |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 731 | static void page_flush_tb(void) |
| 732 | { |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 733 | int i; |
| 734 | for (i = 0; i < V_L1_SIZE; i++) { |
| 735 | page_flush_tb_1(V_L1_SHIFT / L2_BITS - 1, l1_map + i); |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 736 | } |
| 737 | } |
| 738 | |
| 739 | /* flush all the translation blocks */ |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 740 | /* XXX: tb_flush is currently not thread safe */ |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 741 | void tb_flush(CPUState *env1) |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 742 | { |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 743 | CPUState *env; |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 744 | #if defined(DEBUG_FLUSH) |
blueswir1 | ab3d172 | 2007-11-04 07:31:40 +0000 | [diff] [blame] | 745 | printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n", |
| 746 | (unsigned long)(code_gen_ptr - code_gen_buffer), |
| 747 | nb_tbs, nb_tbs > 0 ? |
| 748 | ((unsigned long)(code_gen_ptr - code_gen_buffer)) / nb_tbs : 0); |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 749 | #endif |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 750 | if ((unsigned long)(code_gen_ptr - code_gen_buffer) > code_gen_buffer_size) |
pbrook | a208e54 | 2008-03-31 17:07:36 +0000 | [diff] [blame] | 751 | cpu_abort(env1, "Internal error: code buffer overflow\n"); |
| 752 | |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 753 | nb_tbs = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 754 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 755 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
| 756 | memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *)); |
| 757 | } |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 758 | |
bellard | 8a8a608 | 2004-10-03 13:36:49 +0000 | [diff] [blame] | 759 | memset (tb_phys_hash, 0, CODE_GEN_PHYS_HASH_SIZE * sizeof (void *)); |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 760 | page_flush_tb(); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 761 | |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 762 | code_gen_ptr = code_gen_buffer; |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 763 | /* XXX: flush processor icache at this point if cache flush is |
| 764 | expensive */ |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 765 | tb_flush_count++; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | #ifdef DEBUG_TB_CHECK |
| 769 | |
j_mayer | bc98a7e | 2007-04-04 07:55:12 +0000 | [diff] [blame] | 770 | static void tb_invalidate_check(target_ulong address) |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 771 | { |
| 772 | TranslationBlock *tb; |
| 773 | int i; |
| 774 | address &= TARGET_PAGE_MASK; |
pbrook | 99773bd | 2006-04-16 15:14:59 +0000 | [diff] [blame] | 775 | for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) { |
| 776 | for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) { |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 777 | if (!(address + TARGET_PAGE_SIZE <= tb->pc || |
| 778 | address >= tb->pc + tb->size)) { |
Blue Swirl | 0bf9e31 | 2009-07-20 17:19:25 +0000 | [diff] [blame] | 779 | printf("ERROR invalidate: address=" TARGET_FMT_lx |
| 780 | " PC=%08lx size=%04x\n", |
pbrook | 99773bd | 2006-04-16 15:14:59 +0000 | [diff] [blame] | 781 | address, (long)tb->pc, tb->size); |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 782 | } |
| 783 | } |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | /* verify that all the pages have correct rights for code */ |
| 788 | static void tb_page_check(void) |
| 789 | { |
| 790 | TranslationBlock *tb; |
| 791 | int i, flags1, flags2; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 792 | |
pbrook | 99773bd | 2006-04-16 15:14:59 +0000 | [diff] [blame] | 793 | for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) { |
| 794 | for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) { |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 795 | flags1 = page_get_flags(tb->pc); |
| 796 | flags2 = page_get_flags(tb->pc + tb->size - 1); |
| 797 | if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) { |
| 798 | printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n", |
pbrook | 99773bd | 2006-04-16 15:14:59 +0000 | [diff] [blame] | 799 | (long)tb->pc, tb->size, flags1, flags2); |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | #endif |
| 806 | |
| 807 | /* invalidate one TB */ |
| 808 | static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb, |
| 809 | int next_offset) |
| 810 | { |
| 811 | TranslationBlock *tb1; |
| 812 | for(;;) { |
| 813 | tb1 = *ptb; |
| 814 | if (tb1 == tb) { |
| 815 | *ptb = *(TranslationBlock **)((char *)tb1 + next_offset); |
| 816 | break; |
| 817 | } |
| 818 | ptb = (TranslationBlock **)((char *)tb1 + next_offset); |
| 819 | } |
| 820 | } |
| 821 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 822 | static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb) |
| 823 | { |
| 824 | TranslationBlock *tb1; |
| 825 | unsigned int n1; |
| 826 | |
| 827 | for(;;) { |
| 828 | tb1 = *ptb; |
| 829 | n1 = (long)tb1 & 3; |
| 830 | tb1 = (TranslationBlock *)((long)tb1 & ~3); |
| 831 | if (tb1 == tb) { |
| 832 | *ptb = tb1->page_next[n1]; |
| 833 | break; |
| 834 | } |
| 835 | ptb = &tb1->page_next[n1]; |
| 836 | } |
| 837 | } |
| 838 | |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 839 | static inline void tb_jmp_remove(TranslationBlock *tb, int n) |
| 840 | { |
| 841 | TranslationBlock *tb1, **ptb; |
| 842 | unsigned int n1; |
| 843 | |
| 844 | ptb = &tb->jmp_next[n]; |
| 845 | tb1 = *ptb; |
| 846 | if (tb1) { |
| 847 | /* find tb(n) in circular list */ |
| 848 | for(;;) { |
| 849 | tb1 = *ptb; |
| 850 | n1 = (long)tb1 & 3; |
| 851 | tb1 = (TranslationBlock *)((long)tb1 & ~3); |
| 852 | if (n1 == n && tb1 == tb) |
| 853 | break; |
| 854 | if (n1 == 2) { |
| 855 | ptb = &tb1->jmp_first; |
| 856 | } else { |
| 857 | ptb = &tb1->jmp_next[n1]; |
| 858 | } |
| 859 | } |
| 860 | /* now we can suppress tb(n) from the list */ |
| 861 | *ptb = tb->jmp_next[n]; |
| 862 | |
| 863 | tb->jmp_next[n] = NULL; |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | /* reset the jump entry 'n' of a TB so that it is not chained to |
| 868 | another TB */ |
| 869 | static inline void tb_reset_jump(TranslationBlock *tb, int n) |
| 870 | { |
| 871 | tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n])); |
| 872 | } |
| 873 | |
Paul Brook | 41c1b1c | 2010-03-12 16:54:58 +0000 | [diff] [blame] | 874 | void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr) |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 875 | { |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 876 | CPUState *env; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 877 | PageDesc *p; |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 878 | unsigned int h, n1; |
Paul Brook | 41c1b1c | 2010-03-12 16:54:58 +0000 | [diff] [blame] | 879 | tb_page_addr_t phys_pc; |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 880 | TranslationBlock *tb1, *tb2; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 881 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 882 | /* remove the TB from the hash list */ |
| 883 | phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK); |
| 884 | h = tb_phys_hash_func(phys_pc); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 885 | tb_remove(&tb_phys_hash[h], tb, |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 886 | offsetof(TranslationBlock, phys_hash_next)); |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 887 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 888 | /* remove the TB from the page list */ |
| 889 | if (tb->page_addr[0] != page_addr) { |
| 890 | p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS); |
| 891 | tb_page_remove(&p->first_tb, tb); |
| 892 | invalidate_page_bitmap(p); |
| 893 | } |
| 894 | if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) { |
| 895 | p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS); |
| 896 | tb_page_remove(&p->first_tb, tb); |
| 897 | invalidate_page_bitmap(p); |
| 898 | } |
| 899 | |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 900 | tb_invalidated_flag = 1; |
| 901 | |
| 902 | /* remove the TB from the hash list */ |
| 903 | h = tb_jmp_cache_hash_func(tb->pc); |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 904 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
| 905 | if (env->tb_jmp_cache[h] == tb) |
| 906 | env->tb_jmp_cache[h] = NULL; |
| 907 | } |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 908 | |
| 909 | /* suppress this TB from the two jump lists */ |
| 910 | tb_jmp_remove(tb, 0); |
| 911 | tb_jmp_remove(tb, 1); |
| 912 | |
| 913 | /* suppress any remaining jumps to this TB */ |
| 914 | tb1 = tb->jmp_first; |
| 915 | for(;;) { |
| 916 | n1 = (long)tb1 & 3; |
| 917 | if (n1 == 2) |
| 918 | break; |
| 919 | tb1 = (TranslationBlock *)((long)tb1 & ~3); |
| 920 | tb2 = tb1->jmp_next[n1]; |
| 921 | tb_reset_jump(tb1, n1); |
| 922 | tb1->jmp_next[n1] = NULL; |
| 923 | tb1 = tb2; |
| 924 | } |
| 925 | tb->jmp_first = (TranslationBlock *)((long)tb | 2); /* fail safe */ |
| 926 | |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 927 | tb_phys_invalidate_count++; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | static inline void set_bits(uint8_t *tab, int start, int len) |
| 931 | { |
| 932 | int end, mask, end1; |
| 933 | |
| 934 | end = start + len; |
| 935 | tab += start >> 3; |
| 936 | mask = 0xff << (start & 7); |
| 937 | if ((start & ~7) == (end & ~7)) { |
| 938 | if (start < end) { |
| 939 | mask &= ~(0xff << (end & 7)); |
| 940 | *tab |= mask; |
| 941 | } |
| 942 | } else { |
| 943 | *tab++ |= mask; |
| 944 | start = (start + 8) & ~7; |
| 945 | end1 = end & ~7; |
| 946 | while (start < end1) { |
| 947 | *tab++ = 0xff; |
| 948 | start += 8; |
| 949 | } |
| 950 | if (start < end) { |
| 951 | mask = ~(0xff << (end & 7)); |
| 952 | *tab |= mask; |
| 953 | } |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | static void build_page_bitmap(PageDesc *p) |
| 958 | { |
| 959 | int n, tb_start, tb_end; |
| 960 | TranslationBlock *tb; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 961 | |
pbrook | b2a7081 | 2008-06-09 13:57:23 +0000 | [diff] [blame] | 962 | p->code_bitmap = qemu_mallocz(TARGET_PAGE_SIZE / 8); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 963 | |
| 964 | tb = p->first_tb; |
| 965 | while (tb != NULL) { |
| 966 | n = (long)tb & 3; |
| 967 | tb = (TranslationBlock *)((long)tb & ~3); |
| 968 | /* NOTE: this is subtle as a TB may span two physical pages */ |
| 969 | if (n == 0) { |
| 970 | /* NOTE: tb_end may be after the end of the page, but |
| 971 | it is not a problem */ |
| 972 | tb_start = tb->pc & ~TARGET_PAGE_MASK; |
| 973 | tb_end = tb_start + tb->size; |
| 974 | if (tb_end > TARGET_PAGE_SIZE) |
| 975 | tb_end = TARGET_PAGE_SIZE; |
| 976 | } else { |
| 977 | tb_start = 0; |
| 978 | tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK); |
| 979 | } |
| 980 | set_bits(p->code_bitmap, tb_start, tb_end - tb_start); |
| 981 | tb = tb->page_next[n]; |
| 982 | } |
| 983 | } |
| 984 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 985 | TranslationBlock *tb_gen_code(CPUState *env, |
| 986 | target_ulong pc, target_ulong cs_base, |
| 987 | int flags, int cflags) |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 988 | { |
| 989 | TranslationBlock *tb; |
| 990 | uint8_t *tc_ptr; |
Paul Brook | 41c1b1c | 2010-03-12 16:54:58 +0000 | [diff] [blame] | 991 | tb_page_addr_t phys_pc, phys_page2; |
| 992 | target_ulong virt_page2; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 993 | int code_gen_size; |
| 994 | |
Paul Brook | 41c1b1c | 2010-03-12 16:54:58 +0000 | [diff] [blame] | 995 | phys_pc = get_page_addr_code(env, pc); |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 996 | tb = tb_alloc(pc); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 997 | if (!tb) { |
| 998 | /* flush must be done */ |
| 999 | tb_flush(env); |
| 1000 | /* cannot fail at this point */ |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 1001 | tb = tb_alloc(pc); |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1002 | /* Don't forget to invalidate previous TB info. */ |
| 1003 | tb_invalidated_flag = 1; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1004 | } |
| 1005 | tc_ptr = code_gen_ptr; |
| 1006 | tb->tc_ptr = tc_ptr; |
| 1007 | tb->cs_base = cs_base; |
| 1008 | tb->flags = flags; |
| 1009 | tb->cflags = cflags; |
blueswir1 | d07bde8 | 2007-12-11 19:35:45 +0000 | [diff] [blame] | 1010 | cpu_gen_code(env, tb, &code_gen_size); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1011 | 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] | 1012 | |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1013 | /* check next page if needed */ |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 1014 | virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1015 | phys_page2 = -1; |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 1016 | if ((pc & TARGET_PAGE_MASK) != virt_page2) { |
Paul Brook | 41c1b1c | 2010-03-12 16:54:58 +0000 | [diff] [blame] | 1017 | phys_page2 = get_page_addr_code(env, virt_page2); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1018 | } |
Paul Brook | 41c1b1c | 2010-03-12 16:54:58 +0000 | [diff] [blame] | 1019 | tb_link_page(tb, phys_pc, phys_page2); |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1020 | return tb; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1021 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1022 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1023 | /* invalidate all TBs which intersect with the target physical page |
| 1024 | starting in range [start;end[. NOTE: start and end must refer to |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1025 | the same physical page. 'is_cpu_write_access' should be true if called |
| 1026 | from a real cpu write access: the virtual CPU will exit the current |
| 1027 | TB if code is modified inside this TB. */ |
Paul Brook | 41c1b1c | 2010-03-12 16:54:58 +0000 | [diff] [blame] | 1028 | void tb_invalidate_phys_page_range(tb_page_addr_t start, tb_page_addr_t end, |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1029 | int is_cpu_write_access) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1030 | { |
aliguori | 6b91754 | 2008-11-18 19:46:41 +0000 | [diff] [blame] | 1031 | TranslationBlock *tb, *tb_next, *saved_tb; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1032 | CPUState *env = cpu_single_env; |
Paul Brook | 41c1b1c | 2010-03-12 16:54:58 +0000 | [diff] [blame] | 1033 | tb_page_addr_t tb_start, tb_end; |
aliguori | 6b91754 | 2008-11-18 19:46:41 +0000 | [diff] [blame] | 1034 | PageDesc *p; |
| 1035 | int n; |
| 1036 | #ifdef TARGET_HAS_PRECISE_SMC |
| 1037 | int current_tb_not_found = is_cpu_write_access; |
| 1038 | TranslationBlock *current_tb = NULL; |
| 1039 | int current_tb_modified = 0; |
| 1040 | target_ulong current_pc = 0; |
| 1041 | target_ulong current_cs_base = 0; |
| 1042 | int current_flags = 0; |
| 1043 | #endif /* TARGET_HAS_PRECISE_SMC */ |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1044 | |
| 1045 | p = page_find(start >> TARGET_PAGE_BITS); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1046 | if (!p) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1047 | return; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1048 | if (!p->code_bitmap && |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1049 | ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD && |
| 1050 | is_cpu_write_access) { |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1051 | /* build code bitmap */ |
| 1052 | build_page_bitmap(p); |
| 1053 | } |
| 1054 | |
| 1055 | /* we remove all the TBs in the range [start, end[ */ |
| 1056 | /* XXX: see if in some cases it could be faster to invalidate all the code */ |
| 1057 | tb = p->first_tb; |
| 1058 | while (tb != NULL) { |
| 1059 | n = (long)tb & 3; |
| 1060 | tb = (TranslationBlock *)((long)tb & ~3); |
| 1061 | tb_next = tb->page_next[n]; |
| 1062 | /* NOTE: this is subtle as a TB may span two physical pages */ |
| 1063 | if (n == 0) { |
| 1064 | /* NOTE: tb_end may be after the end of the page, but |
| 1065 | it is not a problem */ |
| 1066 | tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK); |
| 1067 | tb_end = tb_start + tb->size; |
| 1068 | } else { |
| 1069 | tb_start = tb->page_addr[1]; |
| 1070 | tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK); |
| 1071 | } |
| 1072 | if (!(tb_end <= start || tb_start >= end)) { |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1073 | #ifdef TARGET_HAS_PRECISE_SMC |
| 1074 | if (current_tb_not_found) { |
| 1075 | current_tb_not_found = 0; |
| 1076 | current_tb = NULL; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1077 | if (env->mem_io_pc) { |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1078 | /* now we have a real cpu fault */ |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1079 | current_tb = tb_find_pc(env->mem_io_pc); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1080 | } |
| 1081 | } |
| 1082 | if (current_tb == tb && |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1083 | (current_tb->cflags & CF_COUNT_MASK) != 1) { |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1084 | /* If we are modifying the current TB, we must stop |
| 1085 | its execution. We could be more precise by checking |
| 1086 | that the modification is after the current PC, but it |
| 1087 | would require a specialized function to partially |
| 1088 | restore the CPU state */ |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1089 | |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1090 | current_tb_modified = 1; |
Stefan Weil | 618ba8e | 2011-04-18 06:39:53 +0000 | [diff] [blame] | 1091 | cpu_restore_state(current_tb, env, env->mem_io_pc); |
aliguori | 6b91754 | 2008-11-18 19:46:41 +0000 | [diff] [blame] | 1092 | cpu_get_tb_cpu_state(env, ¤t_pc, ¤t_cs_base, |
| 1093 | ¤t_flags); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1094 | } |
| 1095 | #endif /* TARGET_HAS_PRECISE_SMC */ |
bellard | 6f5a9f7 | 2005-11-26 20:12:28 +0000 | [diff] [blame] | 1096 | /* we need to do that to handle the case where a signal |
| 1097 | occurs while doing tb_phys_invalidate() */ |
| 1098 | saved_tb = NULL; |
| 1099 | if (env) { |
| 1100 | saved_tb = env->current_tb; |
| 1101 | env->current_tb = NULL; |
| 1102 | } |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1103 | tb_phys_invalidate(tb, -1); |
bellard | 6f5a9f7 | 2005-11-26 20:12:28 +0000 | [diff] [blame] | 1104 | if (env) { |
| 1105 | env->current_tb = saved_tb; |
| 1106 | if (env->interrupt_request && env->current_tb) |
| 1107 | cpu_interrupt(env, env->interrupt_request); |
| 1108 | } |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1109 | } |
| 1110 | tb = tb_next; |
| 1111 | } |
| 1112 | #if !defined(CONFIG_USER_ONLY) |
| 1113 | /* if no code remaining, no need to continue to use slow writes */ |
| 1114 | if (!p->first_tb) { |
| 1115 | invalidate_page_bitmap(p); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1116 | if (is_cpu_write_access) { |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1117 | tlb_unprotect_code_phys(env, start, env->mem_io_vaddr); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1118 | } |
| 1119 | } |
| 1120 | #endif |
| 1121 | #ifdef TARGET_HAS_PRECISE_SMC |
| 1122 | if (current_tb_modified) { |
| 1123 | /* we generate a block containing just the instruction |
| 1124 | modifying the memory. It will ensure that it cannot modify |
| 1125 | itself */ |
bellard | ea1c180 | 2004-06-14 18:56:36 +0000 | [diff] [blame] | 1126 | env->current_tb = NULL; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1127 | tb_gen_code(env, current_pc, current_cs_base, current_flags, 1); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1128 | cpu_resume_from_signal(env, NULL); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1129 | } |
| 1130 | #endif |
| 1131 | } |
| 1132 | |
| 1133 | /* len must be <= 8 and start must be a multiple of len */ |
Paul Brook | 41c1b1c | 2010-03-12 16:54:58 +0000 | [diff] [blame] | 1134 | static inline void tb_invalidate_phys_page_fast(tb_page_addr_t start, int len) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1135 | { |
| 1136 | PageDesc *p; |
| 1137 | int offset, b; |
bellard | 59817cc | 2004-02-16 22:01:13 +0000 | [diff] [blame] | 1138 | #if 0 |
bellard | a4193c8 | 2004-06-03 14:01:43 +0000 | [diff] [blame] | 1139 | if (1) { |
aliguori | 93fcfe3 | 2009-01-15 22:34:14 +0000 | [diff] [blame] | 1140 | qemu_log("modifying code at 0x%x size=%d EIP=%x PC=%08x\n", |
| 1141 | cpu_single_env->mem_io_vaddr, len, |
| 1142 | cpu_single_env->eip, |
| 1143 | cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base); |
bellard | 59817cc | 2004-02-16 22:01:13 +0000 | [diff] [blame] | 1144 | } |
| 1145 | #endif |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1146 | p = page_find(start >> TARGET_PAGE_BITS); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1147 | if (!p) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1148 | return; |
| 1149 | if (p->code_bitmap) { |
| 1150 | offset = start & ~TARGET_PAGE_MASK; |
| 1151 | b = p->code_bitmap[offset >> 3] >> (offset & 7); |
| 1152 | if (b & ((1 << len) - 1)) |
| 1153 | goto do_invalidate; |
| 1154 | } else { |
| 1155 | do_invalidate: |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1156 | tb_invalidate_phys_page_range(start, start + len, 1); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1157 | } |
| 1158 | } |
| 1159 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1160 | #if !defined(CONFIG_SOFTMMU) |
Paul Brook | 41c1b1c | 2010-03-12 16:54:58 +0000 | [diff] [blame] | 1161 | static void tb_invalidate_phys_page(tb_page_addr_t addr, |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1162 | unsigned long pc, void *puc) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1163 | { |
aliguori | 6b91754 | 2008-11-18 19:46:41 +0000 | [diff] [blame] | 1164 | TranslationBlock *tb; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1165 | PageDesc *p; |
aliguori | 6b91754 | 2008-11-18 19:46:41 +0000 | [diff] [blame] | 1166 | int n; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1167 | #ifdef TARGET_HAS_PRECISE_SMC |
aliguori | 6b91754 | 2008-11-18 19:46:41 +0000 | [diff] [blame] | 1168 | TranslationBlock *current_tb = NULL; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1169 | CPUState *env = cpu_single_env; |
aliguori | 6b91754 | 2008-11-18 19:46:41 +0000 | [diff] [blame] | 1170 | int current_tb_modified = 0; |
| 1171 | target_ulong current_pc = 0; |
| 1172 | target_ulong current_cs_base = 0; |
| 1173 | int current_flags = 0; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1174 | #endif |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1175 | |
| 1176 | addr &= TARGET_PAGE_MASK; |
| 1177 | p = page_find(addr >> TARGET_PAGE_BITS); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1178 | if (!p) |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1179 | return; |
| 1180 | tb = p->first_tb; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1181 | #ifdef TARGET_HAS_PRECISE_SMC |
| 1182 | if (tb && pc != 0) { |
| 1183 | current_tb = tb_find_pc(pc); |
| 1184 | } |
| 1185 | #endif |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1186 | while (tb != NULL) { |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1187 | n = (long)tb & 3; |
| 1188 | tb = (TranslationBlock *)((long)tb & ~3); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1189 | #ifdef TARGET_HAS_PRECISE_SMC |
| 1190 | if (current_tb == tb && |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1191 | (current_tb->cflags & CF_COUNT_MASK) != 1) { |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1192 | /* If we are modifying the current TB, we must stop |
| 1193 | its execution. We could be more precise by checking |
| 1194 | that the modification is after the current PC, but it |
| 1195 | would require a specialized function to partially |
| 1196 | restore the CPU state */ |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1197 | |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1198 | current_tb_modified = 1; |
Stefan Weil | 618ba8e | 2011-04-18 06:39:53 +0000 | [diff] [blame] | 1199 | cpu_restore_state(current_tb, env, pc); |
aliguori | 6b91754 | 2008-11-18 19:46:41 +0000 | [diff] [blame] | 1200 | cpu_get_tb_cpu_state(env, ¤t_pc, ¤t_cs_base, |
| 1201 | ¤t_flags); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1202 | } |
| 1203 | #endif /* TARGET_HAS_PRECISE_SMC */ |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1204 | tb_phys_invalidate(tb, addr); |
| 1205 | tb = tb->page_next[n]; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1206 | } |
| 1207 | p->first_tb = NULL; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1208 | #ifdef TARGET_HAS_PRECISE_SMC |
| 1209 | if (current_tb_modified) { |
| 1210 | /* we generate a block containing just the instruction |
| 1211 | modifying the memory. It will ensure that it cannot modify |
| 1212 | itself */ |
bellard | ea1c180 | 2004-06-14 18:56:36 +0000 | [diff] [blame] | 1213 | env->current_tb = NULL; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1214 | tb_gen_code(env, current_pc, current_cs_base, current_flags, 1); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1215 | cpu_resume_from_signal(env, puc); |
| 1216 | } |
| 1217 | #endif |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1218 | } |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1219 | #endif |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1220 | |
| 1221 | /* add the tb in the target page and protect it if necessary */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1222 | static inline void tb_alloc_page(TranslationBlock *tb, |
Paul Brook | 41c1b1c | 2010-03-12 16:54:58 +0000 | [diff] [blame] | 1223 | unsigned int n, tb_page_addr_t page_addr) |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1224 | { |
| 1225 | PageDesc *p; |
Juan Quintela | 4429ab4 | 2011-06-02 01:53:44 +0000 | [diff] [blame] | 1226 | #ifndef CONFIG_USER_ONLY |
| 1227 | bool page_already_protected; |
| 1228 | #endif |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1229 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1230 | tb->page_addr[n] = page_addr; |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 1231 | p = page_find_alloc(page_addr >> TARGET_PAGE_BITS, 1); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1232 | tb->page_next[n] = p->first_tb; |
Juan Quintela | 4429ab4 | 2011-06-02 01:53:44 +0000 | [diff] [blame] | 1233 | #ifndef CONFIG_USER_ONLY |
| 1234 | page_already_protected = p->first_tb != NULL; |
| 1235 | #endif |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1236 | p->first_tb = (TranslationBlock *)((long)tb | n); |
| 1237 | invalidate_page_bitmap(p); |
| 1238 | |
bellard | 107db44 | 2004-06-22 18:48:46 +0000 | [diff] [blame] | 1239 | #if defined(TARGET_HAS_SMC) || 1 |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1240 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1241 | #if defined(CONFIG_USER_ONLY) |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1242 | if (p->flags & PAGE_WRITE) { |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1243 | target_ulong addr; |
| 1244 | PageDesc *p2; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1245 | int prot; |
| 1246 | |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1247 | /* force the host page as non writable (writes will have a |
| 1248 | page fault + mprotect overhead) */ |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1249 | page_addr &= qemu_host_page_mask; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1250 | prot = 0; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1251 | for(addr = page_addr; addr < page_addr + qemu_host_page_size; |
| 1252 | addr += TARGET_PAGE_SIZE) { |
| 1253 | |
| 1254 | p2 = page_find (addr >> TARGET_PAGE_BITS); |
| 1255 | if (!p2) |
| 1256 | continue; |
| 1257 | prot |= p2->flags; |
| 1258 | p2->flags &= ~PAGE_WRITE; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1259 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1260 | mprotect(g2h(page_addr), qemu_host_page_size, |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1261 | (prot & PAGE_BITS) & ~PAGE_WRITE); |
| 1262 | #ifdef DEBUG_TB_INVALIDATE |
blueswir1 | ab3d172 | 2007-11-04 07:31:40 +0000 | [diff] [blame] | 1263 | printf("protecting code page: 0x" TARGET_FMT_lx "\n", |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 1264 | page_addr); |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1265 | #endif |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1266 | } |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1267 | #else |
| 1268 | /* if some code is already present, then the pages are already |
| 1269 | protected. So we handle the case where only the first TB is |
| 1270 | allocated in a physical page */ |
Juan Quintela | 4429ab4 | 2011-06-02 01:53:44 +0000 | [diff] [blame] | 1271 | if (!page_already_protected) { |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1272 | tlb_protect_code(page_addr); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1273 | } |
| 1274 | #endif |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1275 | |
| 1276 | #endif /* TARGET_HAS_SMC */ |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1279 | /* add a new TB and link it to the physical page tables. phys_page2 is |
| 1280 | (-1) to indicate that only one page contains the TB. */ |
Paul Brook | 41c1b1c | 2010-03-12 16:54:58 +0000 | [diff] [blame] | 1281 | void tb_link_page(TranslationBlock *tb, |
| 1282 | tb_page_addr_t phys_pc, tb_page_addr_t phys_page2) |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 1283 | { |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1284 | unsigned int h; |
| 1285 | TranslationBlock **ptb; |
| 1286 | |
pbrook | c8a706f | 2008-06-02 16:16:42 +0000 | [diff] [blame] | 1287 | /* Grab the mmap lock to stop another thread invalidating this TB |
| 1288 | before we are done. */ |
| 1289 | mmap_lock(); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1290 | /* add in the physical hash table */ |
| 1291 | h = tb_phys_hash_func(phys_pc); |
| 1292 | ptb = &tb_phys_hash[h]; |
| 1293 | tb->phys_hash_next = *ptb; |
| 1294 | *ptb = tb; |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1295 | |
| 1296 | /* add in the page list */ |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1297 | tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK); |
| 1298 | if (phys_page2 != -1) |
| 1299 | tb_alloc_page(tb, 1, phys_page2); |
| 1300 | else |
| 1301 | tb->page_addr[1] = -1; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1302 | |
bellard | d4e8164 | 2003-05-25 16:46:15 +0000 | [diff] [blame] | 1303 | tb->jmp_first = (TranslationBlock *)((long)tb | 2); |
| 1304 | tb->jmp_next[0] = NULL; |
| 1305 | tb->jmp_next[1] = NULL; |
| 1306 | |
| 1307 | /* init original jump addresses */ |
| 1308 | if (tb->tb_next_offset[0] != 0xffff) |
| 1309 | tb_reset_jump(tb, 0); |
| 1310 | if (tb->tb_next_offset[1] != 0xffff) |
| 1311 | tb_reset_jump(tb, 1); |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 1312 | |
| 1313 | #ifdef DEBUG_TB_CHECK |
| 1314 | tb_page_check(); |
| 1315 | #endif |
pbrook | c8a706f | 2008-06-02 16:16:42 +0000 | [diff] [blame] | 1316 | mmap_unlock(); |
bellard | fd6ce8f | 2003-05-14 19:00:11 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 1319 | /* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr < |
| 1320 | tb[1].tc_ptr. Return NULL if not found */ |
| 1321 | TranslationBlock *tb_find_pc(unsigned long tc_ptr) |
| 1322 | { |
| 1323 | int m_min, m_max, m; |
| 1324 | unsigned long v; |
| 1325 | TranslationBlock *tb; |
| 1326 | |
| 1327 | if (nb_tbs <= 0) |
| 1328 | return NULL; |
| 1329 | if (tc_ptr < (unsigned long)code_gen_buffer || |
| 1330 | tc_ptr >= (unsigned long)code_gen_ptr) |
| 1331 | return NULL; |
| 1332 | /* binary search (cf Knuth) */ |
| 1333 | m_min = 0; |
| 1334 | m_max = nb_tbs - 1; |
| 1335 | while (m_min <= m_max) { |
| 1336 | m = (m_min + m_max) >> 1; |
| 1337 | tb = &tbs[m]; |
| 1338 | v = (unsigned long)tb->tc_ptr; |
| 1339 | if (v == tc_ptr) |
| 1340 | return tb; |
| 1341 | else if (tc_ptr < v) { |
| 1342 | m_max = m - 1; |
| 1343 | } else { |
| 1344 | m_min = m + 1; |
| 1345 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1346 | } |
bellard | a513fe1 | 2003-05-27 23:29:48 +0000 | [diff] [blame] | 1347 | return &tbs[m_max]; |
| 1348 | } |
bellard | 7501267 | 2003-06-21 13:11:07 +0000 | [diff] [blame] | 1349 | |
bellard | ea041c0 | 2003-06-25 16:16:50 +0000 | [diff] [blame] | 1350 | static void tb_reset_jump_recursive(TranslationBlock *tb); |
| 1351 | |
| 1352 | static inline void tb_reset_jump_recursive2(TranslationBlock *tb, int n) |
| 1353 | { |
| 1354 | TranslationBlock *tb1, *tb_next, **ptb; |
| 1355 | unsigned int n1; |
| 1356 | |
| 1357 | tb1 = tb->jmp_next[n]; |
| 1358 | if (tb1 != NULL) { |
| 1359 | /* find head of list */ |
| 1360 | for(;;) { |
| 1361 | n1 = (long)tb1 & 3; |
| 1362 | tb1 = (TranslationBlock *)((long)tb1 & ~3); |
| 1363 | if (n1 == 2) |
| 1364 | break; |
| 1365 | tb1 = tb1->jmp_next[n1]; |
| 1366 | } |
| 1367 | /* we are now sure now that tb jumps to tb1 */ |
| 1368 | tb_next = tb1; |
| 1369 | |
| 1370 | /* remove tb from the jmp_first list */ |
| 1371 | ptb = &tb_next->jmp_first; |
| 1372 | for(;;) { |
| 1373 | tb1 = *ptb; |
| 1374 | n1 = (long)tb1 & 3; |
| 1375 | tb1 = (TranslationBlock *)((long)tb1 & ~3); |
| 1376 | if (n1 == n && tb1 == tb) |
| 1377 | break; |
| 1378 | ptb = &tb1->jmp_next[n1]; |
| 1379 | } |
| 1380 | *ptb = tb->jmp_next[n]; |
| 1381 | tb->jmp_next[n] = NULL; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1382 | |
bellard | ea041c0 | 2003-06-25 16:16:50 +0000 | [diff] [blame] | 1383 | /* suppress the jump to next tb in generated code */ |
| 1384 | tb_reset_jump(tb, n); |
| 1385 | |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 1386 | /* suppress jumps in the tb on which we could have jumped */ |
bellard | ea041c0 | 2003-06-25 16:16:50 +0000 | [diff] [blame] | 1387 | tb_reset_jump_recursive(tb_next); |
| 1388 | } |
| 1389 | } |
| 1390 | |
| 1391 | static void tb_reset_jump_recursive(TranslationBlock *tb) |
| 1392 | { |
| 1393 | tb_reset_jump_recursive2(tb, 0); |
| 1394 | tb_reset_jump_recursive2(tb, 1); |
| 1395 | } |
| 1396 | |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1397 | #if defined(TARGET_HAS_ICE) |
Paul Brook | 94df27f | 2010-02-28 23:47:45 +0000 | [diff] [blame] | 1398 | #if defined(CONFIG_USER_ONLY) |
| 1399 | static void breakpoint_invalidate(CPUState *env, target_ulong pc) |
| 1400 | { |
| 1401 | tb_invalidate_phys_page_range(pc, pc + 1, 0); |
| 1402 | } |
| 1403 | #else |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1404 | static void breakpoint_invalidate(CPUState *env, target_ulong pc) |
| 1405 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 1406 | target_phys_addr_t addr; |
j_mayer | 9b3c35e | 2007-04-07 11:21:28 +0000 | [diff] [blame] | 1407 | target_ulong pd; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 1408 | ram_addr_t ram_addr; |
pbrook | c2f07f8 | 2006-04-08 17:14:56 +0000 | [diff] [blame] | 1409 | PhysPageDesc *p; |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1410 | |
pbrook | c2f07f8 | 2006-04-08 17:14:56 +0000 | [diff] [blame] | 1411 | addr = cpu_get_phys_page_debug(env, pc); |
| 1412 | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
| 1413 | if (!p) { |
| 1414 | pd = IO_MEM_UNASSIGNED; |
| 1415 | } else { |
| 1416 | pd = p->phys_offset; |
| 1417 | } |
| 1418 | ram_addr = (pd & TARGET_PAGE_MASK) | (pc & ~TARGET_PAGE_MASK); |
pbrook | 706cd4b | 2006-04-08 17:36:21 +0000 | [diff] [blame] | 1419 | tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1420 | } |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 1421 | #endif |
Paul Brook | 94df27f | 2010-02-28 23:47:45 +0000 | [diff] [blame] | 1422 | #endif /* TARGET_HAS_ICE */ |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1423 | |
Paul Brook | c527ee8 | 2010-03-01 03:31:14 +0000 | [diff] [blame] | 1424 | #if defined(CONFIG_USER_ONLY) |
| 1425 | void cpu_watchpoint_remove_all(CPUState *env, int mask) |
| 1426 | |
| 1427 | { |
| 1428 | } |
| 1429 | |
| 1430 | int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len, |
| 1431 | int flags, CPUWatchpoint **watchpoint) |
| 1432 | { |
| 1433 | return -ENOSYS; |
| 1434 | } |
| 1435 | #else |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 1436 | /* Add a watchpoint. */ |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1437 | int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len, |
| 1438 | int flags, CPUWatchpoint **watchpoint) |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 1439 | { |
aliguori | b405133 | 2008-11-18 20:14:20 +0000 | [diff] [blame] | 1440 | target_ulong len_mask = ~(len - 1); |
aliguori | c0ce998 | 2008-11-25 22:13:57 +0000 | [diff] [blame] | 1441 | CPUWatchpoint *wp; |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 1442 | |
aliguori | b405133 | 2008-11-18 20:14:20 +0000 | [diff] [blame] | 1443 | /* sanity checks: allow power-of-2 lengths, deny unaligned watchpoints */ |
| 1444 | if ((len != 1 && len != 2 && len != 4 && len != 8) || (addr & ~len_mask)) { |
| 1445 | fprintf(stderr, "qemu: tried to set invalid watchpoint at " |
| 1446 | TARGET_FMT_lx ", len=" TARGET_FMT_lu "\n", addr, len); |
| 1447 | return -EINVAL; |
| 1448 | } |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1449 | wp = qemu_malloc(sizeof(*wp)); |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 1450 | |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1451 | wp->vaddr = addr; |
aliguori | b405133 | 2008-11-18 20:14:20 +0000 | [diff] [blame] | 1452 | wp->len_mask = len_mask; |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1453 | wp->flags = flags; |
| 1454 | |
aliguori | 2dc9f41 | 2008-11-18 20:56:59 +0000 | [diff] [blame] | 1455 | /* keep all GDB-injected watchpoints in front */ |
aliguori | c0ce998 | 2008-11-25 22:13:57 +0000 | [diff] [blame] | 1456 | if (flags & BP_GDB) |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1457 | QTAILQ_INSERT_HEAD(&env->watchpoints, wp, entry); |
aliguori | c0ce998 | 2008-11-25 22:13:57 +0000 | [diff] [blame] | 1458 | else |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1459 | QTAILQ_INSERT_TAIL(&env->watchpoints, wp, entry); |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1460 | |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 1461 | tlb_flush_page(env, addr); |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1462 | |
| 1463 | if (watchpoint) |
| 1464 | *watchpoint = wp; |
| 1465 | return 0; |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1468 | /* Remove a specific watchpoint. */ |
| 1469 | int cpu_watchpoint_remove(CPUState *env, target_ulong addr, target_ulong len, |
| 1470 | int flags) |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 1471 | { |
aliguori | b405133 | 2008-11-18 20:14:20 +0000 | [diff] [blame] | 1472 | target_ulong len_mask = ~(len - 1); |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1473 | CPUWatchpoint *wp; |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 1474 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1475 | QTAILQ_FOREACH(wp, &env->watchpoints, entry) { |
aliguori | b405133 | 2008-11-18 20:14:20 +0000 | [diff] [blame] | 1476 | if (addr == wp->vaddr && len_mask == wp->len_mask |
aliguori | 6e140f2 | 2008-11-18 20:37:55 +0000 | [diff] [blame] | 1477 | && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) { |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1478 | cpu_watchpoint_remove_by_ref(env, wp); |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 1479 | return 0; |
| 1480 | } |
| 1481 | } |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1482 | return -ENOENT; |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1485 | /* Remove a specific watchpoint by reference. */ |
| 1486 | void cpu_watchpoint_remove_by_ref(CPUState *env, CPUWatchpoint *watchpoint) |
| 1487 | { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1488 | QTAILQ_REMOVE(&env->watchpoints, watchpoint, entry); |
edgar_igl | 7d03f82 | 2008-05-17 18:58:29 +0000 | [diff] [blame] | 1489 | |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1490 | tlb_flush_page(env, watchpoint->vaddr); |
| 1491 | |
| 1492 | qemu_free(watchpoint); |
edgar_igl | 7d03f82 | 2008-05-17 18:58:29 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1495 | /* Remove all matching watchpoints. */ |
| 1496 | void cpu_watchpoint_remove_all(CPUState *env, int mask) |
| 1497 | { |
aliguori | c0ce998 | 2008-11-25 22:13:57 +0000 | [diff] [blame] | 1498 | CPUWatchpoint *wp, *next; |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1499 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1500 | QTAILQ_FOREACH_SAFE(wp, &env->watchpoints, entry, next) { |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1501 | if (wp->flags & mask) |
| 1502 | cpu_watchpoint_remove_by_ref(env, wp); |
aliguori | c0ce998 | 2008-11-25 22:13:57 +0000 | [diff] [blame] | 1503 | } |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1504 | } |
Paul Brook | c527ee8 | 2010-03-01 03:31:14 +0000 | [diff] [blame] | 1505 | #endif |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1506 | |
| 1507 | /* Add a breakpoint. */ |
| 1508 | int cpu_breakpoint_insert(CPUState *env, target_ulong pc, int flags, |
| 1509 | CPUBreakpoint **breakpoint) |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 1510 | { |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1511 | #if defined(TARGET_HAS_ICE) |
aliguori | c0ce998 | 2008-11-25 22:13:57 +0000 | [diff] [blame] | 1512 | CPUBreakpoint *bp; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1513 | |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1514 | bp = qemu_malloc(sizeof(*bp)); |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1515 | |
| 1516 | bp->pc = pc; |
| 1517 | bp->flags = flags; |
| 1518 | |
aliguori | 2dc9f41 | 2008-11-18 20:56:59 +0000 | [diff] [blame] | 1519 | /* keep all GDB-injected breakpoints in front */ |
aliguori | c0ce998 | 2008-11-25 22:13:57 +0000 | [diff] [blame] | 1520 | if (flags & BP_GDB) |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1521 | QTAILQ_INSERT_HEAD(&env->breakpoints, bp, entry); |
aliguori | c0ce998 | 2008-11-25 22:13:57 +0000 | [diff] [blame] | 1522 | else |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1523 | QTAILQ_INSERT_TAIL(&env->breakpoints, bp, entry); |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1524 | |
| 1525 | breakpoint_invalidate(env, pc); |
| 1526 | |
| 1527 | if (breakpoint) |
| 1528 | *breakpoint = bp; |
| 1529 | return 0; |
| 1530 | #else |
| 1531 | return -ENOSYS; |
| 1532 | #endif |
| 1533 | } |
| 1534 | |
| 1535 | /* Remove a specific breakpoint. */ |
| 1536 | int cpu_breakpoint_remove(CPUState *env, target_ulong pc, int flags) |
| 1537 | { |
| 1538 | #if defined(TARGET_HAS_ICE) |
| 1539 | CPUBreakpoint *bp; |
| 1540 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1541 | QTAILQ_FOREACH(bp, &env->breakpoints, entry) { |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1542 | if (bp->pc == pc && bp->flags == flags) { |
| 1543 | cpu_breakpoint_remove_by_ref(env, bp); |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 1544 | return 0; |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1545 | } |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 1546 | } |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1547 | return -ENOENT; |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 1548 | #else |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1549 | return -ENOSYS; |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 1550 | #endif |
| 1551 | } |
| 1552 | |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1553 | /* Remove a specific breakpoint by reference. */ |
| 1554 | void cpu_breakpoint_remove_by_ref(CPUState *env, CPUBreakpoint *breakpoint) |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 1555 | { |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1556 | #if defined(TARGET_HAS_ICE) |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1557 | QTAILQ_REMOVE(&env->breakpoints, breakpoint, entry); |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 1558 | |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1559 | breakpoint_invalidate(env, breakpoint->pc); |
| 1560 | |
| 1561 | qemu_free(breakpoint); |
| 1562 | #endif |
| 1563 | } |
| 1564 | |
| 1565 | /* Remove all matching breakpoints. */ |
| 1566 | void cpu_breakpoint_remove_all(CPUState *env, int mask) |
| 1567 | { |
| 1568 | #if defined(TARGET_HAS_ICE) |
aliguori | c0ce998 | 2008-11-25 22:13:57 +0000 | [diff] [blame] | 1569 | CPUBreakpoint *bp, *next; |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1570 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1571 | QTAILQ_FOREACH_SAFE(bp, &env->breakpoints, entry, next) { |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 1572 | if (bp->flags & mask) |
| 1573 | cpu_breakpoint_remove_by_ref(env, bp); |
aliguori | c0ce998 | 2008-11-25 22:13:57 +0000 | [diff] [blame] | 1574 | } |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 1575 | #endif |
| 1576 | } |
| 1577 | |
bellard | c33a346 | 2003-07-29 20:50:33 +0000 | [diff] [blame] | 1578 | /* enable or disable single step mode. EXCP_DEBUG is returned by the |
| 1579 | CPU loop after each instruction */ |
| 1580 | void cpu_single_step(CPUState *env, int enabled) |
| 1581 | { |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 1582 | #if defined(TARGET_HAS_ICE) |
bellard | c33a346 | 2003-07-29 20:50:33 +0000 | [diff] [blame] | 1583 | if (env->singlestep_enabled != enabled) { |
| 1584 | env->singlestep_enabled = enabled; |
aliguori | e22a25c | 2009-03-12 20:12:48 +0000 | [diff] [blame] | 1585 | if (kvm_enabled()) |
| 1586 | kvm_update_guest_debug(env, 0); |
| 1587 | else { |
Stuart Brady | ccbb4d4 | 2009-05-03 12:15:06 +0100 | [diff] [blame] | 1588 | /* must flush all the translated code to avoid inconsistencies */ |
aliguori | e22a25c | 2009-03-12 20:12:48 +0000 | [diff] [blame] | 1589 | /* XXX: only flush what is necessary */ |
| 1590 | tb_flush(env); |
| 1591 | } |
bellard | c33a346 | 2003-07-29 20:50:33 +0000 | [diff] [blame] | 1592 | } |
| 1593 | #endif |
| 1594 | } |
| 1595 | |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 1596 | /* enable or disable low levels log */ |
| 1597 | void cpu_set_log(int log_flags) |
| 1598 | { |
| 1599 | loglevel = log_flags; |
| 1600 | if (loglevel && !logfile) { |
pbrook | 11fcfab | 2007-07-01 18:21:11 +0000 | [diff] [blame] | 1601 | logfile = fopen(logfilename, log_append ? "a" : "w"); |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 1602 | if (!logfile) { |
| 1603 | perror(logfilename); |
| 1604 | _exit(1); |
| 1605 | } |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1606 | #if !defined(CONFIG_SOFTMMU) |
| 1607 | /* must avoid mmap() usage of glibc by setting a buffer "by hand" */ |
| 1608 | { |
blueswir1 | b55266b | 2008-09-20 08:07:15 +0000 | [diff] [blame] | 1609 | static char logfile_buf[4096]; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1610 | setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf)); |
| 1611 | } |
Filip Navara | bf65f53 | 2009-07-27 10:02:04 -0500 | [diff] [blame] | 1612 | #elif !defined(_WIN32) |
| 1613 | /* Win32 doesn't support line-buffering and requires size >= 2 */ |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 1614 | setvbuf(logfile, NULL, _IOLBF, 0); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1615 | #endif |
pbrook | e735b91 | 2007-06-30 13:53:24 +0000 | [diff] [blame] | 1616 | log_append = 1; |
| 1617 | } |
| 1618 | if (!loglevel && logfile) { |
| 1619 | fclose(logfile); |
| 1620 | logfile = NULL; |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 1621 | } |
| 1622 | } |
| 1623 | |
| 1624 | void cpu_set_log_filename(const char *filename) |
| 1625 | { |
| 1626 | logfilename = strdup(filename); |
pbrook | e735b91 | 2007-06-30 13:53:24 +0000 | [diff] [blame] | 1627 | if (logfile) { |
| 1628 | fclose(logfile); |
| 1629 | logfile = NULL; |
| 1630 | } |
| 1631 | cpu_set_log(loglevel); |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 1632 | } |
bellard | c33a346 | 2003-07-29 20:50:33 +0000 | [diff] [blame] | 1633 | |
aurel32 | 3098dba | 2009-03-07 21:28:24 +0000 | [diff] [blame] | 1634 | static void cpu_unlink_tb(CPUState *env) |
bellard | ea041c0 | 2003-06-25 16:16:50 +0000 | [diff] [blame] | 1635 | { |
pbrook | d597536 | 2008-06-07 20:50:51 +0000 | [diff] [blame] | 1636 | /* FIXME: TB unchaining isn't SMP safe. For now just ignore the |
| 1637 | problem and hope the cpu will stop of its own accord. For userspace |
| 1638 | emulation this often isn't actually as bad as it sounds. Often |
| 1639 | signals are used primarily to interrupt blocking syscalls. */ |
aurel32 | 3098dba | 2009-03-07 21:28:24 +0000 | [diff] [blame] | 1640 | TranslationBlock *tb; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 1641 | static spinlock_t interrupt_lock = SPIN_LOCK_UNLOCKED; |
aurel32 | 3098dba | 2009-03-07 21:28:24 +0000 | [diff] [blame] | 1642 | |
Riku Voipio | cab1b4b | 2010-01-20 12:56:27 +0200 | [diff] [blame] | 1643 | spin_lock(&interrupt_lock); |
aurel32 | 3098dba | 2009-03-07 21:28:24 +0000 | [diff] [blame] | 1644 | tb = env->current_tb; |
| 1645 | /* if the cpu is currently executing code, we must unlink it and |
| 1646 | all the potentially executing TB */ |
Riku Voipio | f76cfe5 | 2009-12-04 15:16:30 +0200 | [diff] [blame] | 1647 | if (tb) { |
aurel32 | 3098dba | 2009-03-07 21:28:24 +0000 | [diff] [blame] | 1648 | env->current_tb = NULL; |
| 1649 | tb_reset_jump_recursive(tb); |
aurel32 | 3098dba | 2009-03-07 21:28:24 +0000 | [diff] [blame] | 1650 | } |
Riku Voipio | cab1b4b | 2010-01-20 12:56:27 +0200 | [diff] [blame] | 1651 | spin_unlock(&interrupt_lock); |
aurel32 | 3098dba | 2009-03-07 21:28:24 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
Jan Kiszka | 97ffbd8 | 2011-04-13 01:32:56 +0200 | [diff] [blame] | 1654 | #ifndef CONFIG_USER_ONLY |
aurel32 | 3098dba | 2009-03-07 21:28:24 +0000 | [diff] [blame] | 1655 | /* mask must never be zero, except for A20 change call */ |
Jan Kiszka | ec6959d | 2011-04-13 01:32:56 +0200 | [diff] [blame] | 1656 | static void tcg_handle_interrupt(CPUState *env, int mask) |
aurel32 | 3098dba | 2009-03-07 21:28:24 +0000 | [diff] [blame] | 1657 | { |
| 1658 | int old_mask; |
| 1659 | |
| 1660 | old_mask = env->interrupt_request; |
| 1661 | env->interrupt_request |= mask; |
| 1662 | |
aliguori | 8edac96 | 2009-04-24 18:03:45 +0000 | [diff] [blame] | 1663 | /* |
| 1664 | * If called from iothread context, wake the target cpu in |
| 1665 | * case its halted. |
| 1666 | */ |
Jan Kiszka | b7680cb | 2011-03-12 17:43:51 +0100 | [diff] [blame] | 1667 | if (!qemu_cpu_is_self(env)) { |
aliguori | 8edac96 | 2009-04-24 18:03:45 +0000 | [diff] [blame] | 1668 | qemu_cpu_kick(env); |
| 1669 | return; |
| 1670 | } |
aliguori | 8edac96 | 2009-04-24 18:03:45 +0000 | [diff] [blame] | 1671 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1672 | if (use_icount) { |
pbrook | 266910c | 2008-07-09 15:31:50 +0000 | [diff] [blame] | 1673 | env->icount_decr.u16.high = 0xffff; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1674 | if (!can_do_io(env) |
aurel32 | be214e6 | 2009-03-06 21:48:00 +0000 | [diff] [blame] | 1675 | && (mask & ~old_mask) != 0) { |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1676 | cpu_abort(env, "Raised interrupt while not in I/O function"); |
| 1677 | } |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1678 | } else { |
aurel32 | 3098dba | 2009-03-07 21:28:24 +0000 | [diff] [blame] | 1679 | cpu_unlink_tb(env); |
bellard | ea041c0 | 2003-06-25 16:16:50 +0000 | [diff] [blame] | 1680 | } |
| 1681 | } |
| 1682 | |
Jan Kiszka | ec6959d | 2011-04-13 01:32:56 +0200 | [diff] [blame] | 1683 | CPUInterruptHandler cpu_interrupt_handler = tcg_handle_interrupt; |
| 1684 | |
Jan Kiszka | 97ffbd8 | 2011-04-13 01:32:56 +0200 | [diff] [blame] | 1685 | #else /* CONFIG_USER_ONLY */ |
| 1686 | |
| 1687 | void cpu_interrupt(CPUState *env, int mask) |
| 1688 | { |
| 1689 | env->interrupt_request |= mask; |
| 1690 | cpu_unlink_tb(env); |
| 1691 | } |
| 1692 | #endif /* CONFIG_USER_ONLY */ |
| 1693 | |
bellard | b54ad04 | 2004-05-20 13:42:52 +0000 | [diff] [blame] | 1694 | void cpu_reset_interrupt(CPUState *env, int mask) |
| 1695 | { |
| 1696 | env->interrupt_request &= ~mask; |
| 1697 | } |
| 1698 | |
aurel32 | 3098dba | 2009-03-07 21:28:24 +0000 | [diff] [blame] | 1699 | void cpu_exit(CPUState *env) |
| 1700 | { |
| 1701 | env->exit_request = 1; |
| 1702 | cpu_unlink_tb(env); |
| 1703 | } |
| 1704 | |
blueswir1 | c7cd6a3 | 2008-10-02 18:27:46 +0000 | [diff] [blame] | 1705 | const CPULogItem cpu_log_items[] = { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1706 | { CPU_LOG_TB_OUT_ASM, "out_asm", |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1707 | "show generated host assembly code for each compiled TB" }, |
| 1708 | { CPU_LOG_TB_IN_ASM, "in_asm", |
| 1709 | "show target assembly code for each compiled TB" }, |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1710 | { CPU_LOG_TB_OP, "op", |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 1711 | "show micro ops for each compiled TB" }, |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1712 | { CPU_LOG_TB_OP_OPT, "op_opt", |
blueswir1 | e01a115 | 2008-03-14 17:37:11 +0000 | [diff] [blame] | 1713 | "show micro ops " |
| 1714 | #ifdef TARGET_I386 |
| 1715 | "before eflags optimization and " |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1716 | #endif |
blueswir1 | e01a115 | 2008-03-14 17:37:11 +0000 | [diff] [blame] | 1717 | "after liveness analysis" }, |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1718 | { CPU_LOG_INT, "int", |
| 1719 | "show interrupts/exceptions in short format" }, |
| 1720 | { CPU_LOG_EXEC, "exec", |
| 1721 | "show trace before each executed TB (lots of logs)" }, |
bellard | 9fddaa0 | 2004-05-21 12:59:32 +0000 | [diff] [blame] | 1722 | { CPU_LOG_TB_CPU, "cpu", |
ths | e91c8a7 | 2007-06-03 13:35:16 +0000 | [diff] [blame] | 1723 | "show CPU state before block translation" }, |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1724 | #ifdef TARGET_I386 |
| 1725 | { CPU_LOG_PCALL, "pcall", |
| 1726 | "show protected mode far calls/returns/exceptions" }, |
aliguori | eca1bdf | 2009-01-26 19:54:31 +0000 | [diff] [blame] | 1727 | { CPU_LOG_RESET, "cpu_reset", |
| 1728 | "show CPU state before CPU resets" }, |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1729 | #endif |
bellard | 8e3a9fd | 2004-10-09 17:32:58 +0000 | [diff] [blame] | 1730 | #ifdef DEBUG_IOPORT |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1731 | { CPU_LOG_IOPORT, "ioport", |
| 1732 | "show all i/o ports accesses" }, |
bellard | 8e3a9fd | 2004-10-09 17:32:58 +0000 | [diff] [blame] | 1733 | #endif |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1734 | { 0, NULL, NULL }, |
| 1735 | }; |
| 1736 | |
Michael S. Tsirkin | f6f3fbc | 2010-01-27 22:06:57 +0200 | [diff] [blame] | 1737 | #ifndef CONFIG_USER_ONLY |
| 1738 | static QLIST_HEAD(memory_client_list, CPUPhysMemoryClient) memory_client_list |
| 1739 | = QLIST_HEAD_INITIALIZER(memory_client_list); |
| 1740 | |
| 1741 | static void cpu_notify_set_memory(target_phys_addr_t start_addr, |
Yoshiaki Tamura | 9742bf2 | 2010-08-18 13:30:13 +0900 | [diff] [blame] | 1742 | ram_addr_t size, |
Michael S. Tsirkin | 0fd542f | 2011-04-06 22:25:38 +0300 | [diff] [blame] | 1743 | ram_addr_t phys_offset, |
| 1744 | bool log_dirty) |
Michael S. Tsirkin | f6f3fbc | 2010-01-27 22:06:57 +0200 | [diff] [blame] | 1745 | { |
| 1746 | CPUPhysMemoryClient *client; |
| 1747 | QLIST_FOREACH(client, &memory_client_list, list) { |
Michael S. Tsirkin | 0fd542f | 2011-04-06 22:25:38 +0300 | [diff] [blame] | 1748 | client->set_memory(client, start_addr, size, phys_offset, log_dirty); |
Michael S. Tsirkin | f6f3fbc | 2010-01-27 22:06:57 +0200 | [diff] [blame] | 1749 | } |
| 1750 | } |
| 1751 | |
| 1752 | static int cpu_notify_sync_dirty_bitmap(target_phys_addr_t start, |
Yoshiaki Tamura | 9742bf2 | 2010-08-18 13:30:13 +0900 | [diff] [blame] | 1753 | target_phys_addr_t end) |
Michael S. Tsirkin | f6f3fbc | 2010-01-27 22:06:57 +0200 | [diff] [blame] | 1754 | { |
| 1755 | CPUPhysMemoryClient *client; |
| 1756 | QLIST_FOREACH(client, &memory_client_list, list) { |
| 1757 | int r = client->sync_dirty_bitmap(client, start, end); |
| 1758 | if (r < 0) |
| 1759 | return r; |
| 1760 | } |
| 1761 | return 0; |
| 1762 | } |
| 1763 | |
| 1764 | static int cpu_notify_migration_log(int enable) |
| 1765 | { |
| 1766 | CPUPhysMemoryClient *client; |
| 1767 | QLIST_FOREACH(client, &memory_client_list, list) { |
| 1768 | int r = client->migration_log(client, enable); |
| 1769 | if (r < 0) |
| 1770 | return r; |
| 1771 | } |
| 1772 | return 0; |
| 1773 | } |
| 1774 | |
Alex Williamson | 2173a75 | 2011-05-03 12:36:58 -0600 | [diff] [blame] | 1775 | struct last_map { |
| 1776 | target_phys_addr_t start_addr; |
| 1777 | ram_addr_t size; |
| 1778 | ram_addr_t phys_offset; |
| 1779 | }; |
| 1780 | |
Alex Williamson | 8d4c78e | 2011-05-03 12:36:46 -0600 | [diff] [blame] | 1781 | /* The l1_phys_map provides the upper P_L1_BITs of the guest physical |
| 1782 | * address. Each intermediate table provides the next L2_BITs of guest |
| 1783 | * physical address space. The number of levels vary based on host and |
| 1784 | * guest configuration, making it efficient to build the final guest |
| 1785 | * physical address by seeding the L1 offset and shifting and adding in |
| 1786 | * each L2 offset as we recurse through them. */ |
Alex Williamson | 2173a75 | 2011-05-03 12:36:58 -0600 | [diff] [blame] | 1787 | static void phys_page_for_each_1(CPUPhysMemoryClient *client, int level, |
| 1788 | void **lp, target_phys_addr_t addr, |
| 1789 | struct last_map *map) |
Michael S. Tsirkin | f6f3fbc | 2010-01-27 22:06:57 +0200 | [diff] [blame] | 1790 | { |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 1791 | int i; |
Michael S. Tsirkin | f6f3fbc | 2010-01-27 22:06:57 +0200 | [diff] [blame] | 1792 | |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 1793 | if (*lp == NULL) { |
| 1794 | return; |
| 1795 | } |
| 1796 | if (level == 0) { |
| 1797 | PhysPageDesc *pd = *lp; |
Alex Williamson | 8d4c78e | 2011-05-03 12:36:46 -0600 | [diff] [blame] | 1798 | addr <<= L2_BITS + TARGET_PAGE_BITS; |
Paul Brook | 7296aba | 2010-03-14 14:58:46 +0000 | [diff] [blame] | 1799 | for (i = 0; i < L2_SIZE; ++i) { |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 1800 | if (pd[i].phys_offset != IO_MEM_UNASSIGNED) { |
Alex Williamson | 2173a75 | 2011-05-03 12:36:58 -0600 | [diff] [blame] | 1801 | target_phys_addr_t start_addr = addr | i << TARGET_PAGE_BITS; |
| 1802 | |
| 1803 | if (map->size && |
| 1804 | start_addr == map->start_addr + map->size && |
| 1805 | pd[i].phys_offset == map->phys_offset + map->size) { |
| 1806 | |
| 1807 | map->size += TARGET_PAGE_SIZE; |
| 1808 | continue; |
| 1809 | } else if (map->size) { |
| 1810 | client->set_memory(client, map->start_addr, |
| 1811 | map->size, map->phys_offset, false); |
| 1812 | } |
| 1813 | |
| 1814 | map->start_addr = start_addr; |
| 1815 | map->size = TARGET_PAGE_SIZE; |
| 1816 | map->phys_offset = pd[i].phys_offset; |
Michael S. Tsirkin | f6f3fbc | 2010-01-27 22:06:57 +0200 | [diff] [blame] | 1817 | } |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 1818 | } |
| 1819 | } else { |
| 1820 | void **pp = *lp; |
Paul Brook | 7296aba | 2010-03-14 14:58:46 +0000 | [diff] [blame] | 1821 | for (i = 0; i < L2_SIZE; ++i) { |
Alex Williamson | 8d4c78e | 2011-05-03 12:36:46 -0600 | [diff] [blame] | 1822 | phys_page_for_each_1(client, level - 1, pp + i, |
Alex Williamson | 2173a75 | 2011-05-03 12:36:58 -0600 | [diff] [blame] | 1823 | (addr << L2_BITS) | i, map); |
Michael S. Tsirkin | f6f3fbc | 2010-01-27 22:06:57 +0200 | [diff] [blame] | 1824 | } |
| 1825 | } |
| 1826 | } |
| 1827 | |
| 1828 | static void phys_page_for_each(CPUPhysMemoryClient *client) |
| 1829 | { |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 1830 | int i; |
Alex Williamson | 2173a75 | 2011-05-03 12:36:58 -0600 | [diff] [blame] | 1831 | struct last_map map = { }; |
| 1832 | |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 1833 | for (i = 0; i < P_L1_SIZE; ++i) { |
| 1834 | phys_page_for_each_1(client, P_L1_SHIFT / L2_BITS - 1, |
Alex Williamson | 2173a75 | 2011-05-03 12:36:58 -0600 | [diff] [blame] | 1835 | l1_phys_map + i, i, &map); |
| 1836 | } |
| 1837 | if (map.size) { |
| 1838 | client->set_memory(client, map.start_addr, map.size, map.phys_offset, |
| 1839 | false); |
Michael S. Tsirkin | f6f3fbc | 2010-01-27 22:06:57 +0200 | [diff] [blame] | 1840 | } |
Michael S. Tsirkin | f6f3fbc | 2010-01-27 22:06:57 +0200 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | void cpu_register_phys_memory_client(CPUPhysMemoryClient *client) |
| 1844 | { |
| 1845 | QLIST_INSERT_HEAD(&memory_client_list, client, list); |
| 1846 | phys_page_for_each(client); |
| 1847 | } |
| 1848 | |
| 1849 | void cpu_unregister_phys_memory_client(CPUPhysMemoryClient *client) |
| 1850 | { |
| 1851 | QLIST_REMOVE(client, list); |
| 1852 | } |
| 1853 | #endif |
| 1854 | |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1855 | static int cmp1(const char *s1, int n, const char *s2) |
| 1856 | { |
| 1857 | if (strlen(s2) != n) |
| 1858 | return 0; |
| 1859 | return memcmp(s1, s2, n) == 0; |
| 1860 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1861 | |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1862 | /* takes a comma separated list of log masks. Return 0 if error. */ |
| 1863 | int cpu_str_to_log_mask(const char *str) |
| 1864 | { |
blueswir1 | c7cd6a3 | 2008-10-02 18:27:46 +0000 | [diff] [blame] | 1865 | const CPULogItem *item; |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1866 | int mask; |
| 1867 | const char *p, *p1; |
| 1868 | |
| 1869 | p = str; |
| 1870 | mask = 0; |
| 1871 | for(;;) { |
| 1872 | p1 = strchr(p, ','); |
| 1873 | if (!p1) |
| 1874 | p1 = p + strlen(p); |
Yoshiaki Tamura | 9742bf2 | 2010-08-18 13:30:13 +0900 | [diff] [blame] | 1875 | if(cmp1(p,p1-p,"all")) { |
| 1876 | for(item = cpu_log_items; item->mask != 0; item++) { |
| 1877 | mask |= item->mask; |
| 1878 | } |
| 1879 | } else { |
| 1880 | for(item = cpu_log_items; item->mask != 0; item++) { |
| 1881 | if (cmp1(p, p1 - p, item->name)) |
| 1882 | goto found; |
| 1883 | } |
| 1884 | return 0; |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1885 | } |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1886 | found: |
| 1887 | mask |= item->mask; |
| 1888 | if (*p1 != ',') |
| 1889 | break; |
| 1890 | p = p1 + 1; |
| 1891 | } |
| 1892 | return mask; |
| 1893 | } |
bellard | ea041c0 | 2003-06-25 16:16:50 +0000 | [diff] [blame] | 1894 | |
bellard | 7501267 | 2003-06-21 13:11:07 +0000 | [diff] [blame] | 1895 | void cpu_abort(CPUState *env, const char *fmt, ...) |
| 1896 | { |
| 1897 | va_list ap; |
pbrook | 493ae1f | 2007-11-23 16:53:59 +0000 | [diff] [blame] | 1898 | va_list ap2; |
bellard | 7501267 | 2003-06-21 13:11:07 +0000 | [diff] [blame] | 1899 | |
| 1900 | va_start(ap, fmt); |
pbrook | 493ae1f | 2007-11-23 16:53:59 +0000 | [diff] [blame] | 1901 | va_copy(ap2, ap); |
bellard | 7501267 | 2003-06-21 13:11:07 +0000 | [diff] [blame] | 1902 | fprintf(stderr, "qemu: fatal: "); |
| 1903 | vfprintf(stderr, fmt, ap); |
| 1904 | fprintf(stderr, "\n"); |
| 1905 | #ifdef TARGET_I386 |
bellard | 7fe4848 | 2004-10-09 18:08:01 +0000 | [diff] [blame] | 1906 | cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP); |
| 1907 | #else |
| 1908 | cpu_dump_state(env, stderr, fprintf, 0); |
bellard | 7501267 | 2003-06-21 13:11:07 +0000 | [diff] [blame] | 1909 | #endif |
aliguori | 93fcfe3 | 2009-01-15 22:34:14 +0000 | [diff] [blame] | 1910 | if (qemu_log_enabled()) { |
| 1911 | qemu_log("qemu: fatal: "); |
| 1912 | qemu_log_vprintf(fmt, ap2); |
| 1913 | qemu_log("\n"); |
j_mayer | f937329 | 2007-09-29 12:18:20 +0000 | [diff] [blame] | 1914 | #ifdef TARGET_I386 |
aliguori | 93fcfe3 | 2009-01-15 22:34:14 +0000 | [diff] [blame] | 1915 | log_cpu_state(env, X86_DUMP_FPU | X86_DUMP_CCOP); |
j_mayer | f937329 | 2007-09-29 12:18:20 +0000 | [diff] [blame] | 1916 | #else |
aliguori | 93fcfe3 | 2009-01-15 22:34:14 +0000 | [diff] [blame] | 1917 | log_cpu_state(env, 0); |
j_mayer | f937329 | 2007-09-29 12:18:20 +0000 | [diff] [blame] | 1918 | #endif |
aliguori | 31b1a7b | 2009-01-15 22:35:09 +0000 | [diff] [blame] | 1919 | qemu_log_flush(); |
aliguori | 93fcfe3 | 2009-01-15 22:34:14 +0000 | [diff] [blame] | 1920 | qemu_log_close(); |
balrog | 924edca | 2007-06-10 14:07:13 +0000 | [diff] [blame] | 1921 | } |
pbrook | 493ae1f | 2007-11-23 16:53:59 +0000 | [diff] [blame] | 1922 | va_end(ap2); |
j_mayer | f937329 | 2007-09-29 12:18:20 +0000 | [diff] [blame] | 1923 | va_end(ap); |
Riku Voipio | fd052bf | 2010-01-25 14:30:49 +0200 | [diff] [blame] | 1924 | #if defined(CONFIG_USER_ONLY) |
| 1925 | { |
| 1926 | struct sigaction act; |
| 1927 | sigfillset(&act.sa_mask); |
| 1928 | act.sa_handler = SIG_DFL; |
| 1929 | sigaction(SIGABRT, &act, NULL); |
| 1930 | } |
| 1931 | #endif |
bellard | 7501267 | 2003-06-21 13:11:07 +0000 | [diff] [blame] | 1932 | abort(); |
| 1933 | } |
| 1934 | |
ths | c5be9f0 | 2007-02-28 20:20:53 +0000 | [diff] [blame] | 1935 | CPUState *cpu_copy(CPUState *env) |
| 1936 | { |
ths | 01ba981 | 2007-12-09 02:22:57 +0000 | [diff] [blame] | 1937 | CPUState *new_env = cpu_init(env->cpu_model_str); |
ths | c5be9f0 | 2007-02-28 20:20:53 +0000 | [diff] [blame] | 1938 | CPUState *next_cpu = new_env->next_cpu; |
| 1939 | int cpu_index = new_env->cpu_index; |
aliguori | 5a38f08 | 2009-01-15 20:16:51 +0000 | [diff] [blame] | 1940 | #if defined(TARGET_HAS_ICE) |
| 1941 | CPUBreakpoint *bp; |
| 1942 | CPUWatchpoint *wp; |
| 1943 | #endif |
| 1944 | |
ths | c5be9f0 | 2007-02-28 20:20:53 +0000 | [diff] [blame] | 1945 | memcpy(new_env, env, sizeof(CPUState)); |
aliguori | 5a38f08 | 2009-01-15 20:16:51 +0000 | [diff] [blame] | 1946 | |
| 1947 | /* Preserve chaining and index. */ |
ths | c5be9f0 | 2007-02-28 20:20:53 +0000 | [diff] [blame] | 1948 | new_env->next_cpu = next_cpu; |
| 1949 | new_env->cpu_index = cpu_index; |
aliguori | 5a38f08 | 2009-01-15 20:16:51 +0000 | [diff] [blame] | 1950 | |
| 1951 | /* Clone all break/watchpoints. |
| 1952 | Note: Once we support ptrace with hw-debug register access, make sure |
| 1953 | BP_CPU break/watchpoints are handled correctly on clone. */ |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1954 | QTAILQ_INIT(&env->breakpoints); |
| 1955 | QTAILQ_INIT(&env->watchpoints); |
aliguori | 5a38f08 | 2009-01-15 20:16:51 +0000 | [diff] [blame] | 1956 | #if defined(TARGET_HAS_ICE) |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1957 | QTAILQ_FOREACH(bp, &env->breakpoints, entry) { |
aliguori | 5a38f08 | 2009-01-15 20:16:51 +0000 | [diff] [blame] | 1958 | cpu_breakpoint_insert(new_env, bp->pc, bp->flags, NULL); |
| 1959 | } |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1960 | QTAILQ_FOREACH(wp, &env->watchpoints, entry) { |
aliguori | 5a38f08 | 2009-01-15 20:16:51 +0000 | [diff] [blame] | 1961 | cpu_watchpoint_insert(new_env, wp->vaddr, (~wp->len_mask) + 1, |
| 1962 | wp->flags, NULL); |
| 1963 | } |
| 1964 | #endif |
| 1965 | |
ths | c5be9f0 | 2007-02-28 20:20:53 +0000 | [diff] [blame] | 1966 | return new_env; |
| 1967 | } |
| 1968 | |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 1969 | #if !defined(CONFIG_USER_ONLY) |
| 1970 | |
edgar_igl | 5c751e9 | 2008-05-06 08:44:21 +0000 | [diff] [blame] | 1971 | static inline void tlb_flush_jmp_cache(CPUState *env, target_ulong addr) |
| 1972 | { |
| 1973 | unsigned int i; |
| 1974 | |
| 1975 | /* Discard jump cache entries for any tb which might potentially |
| 1976 | overlap the flushed page. */ |
| 1977 | i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE); |
| 1978 | memset (&env->tb_jmp_cache[i], 0, |
Yoshiaki Tamura | 9742bf2 | 2010-08-18 13:30:13 +0900 | [diff] [blame] | 1979 | TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *)); |
edgar_igl | 5c751e9 | 2008-05-06 08:44:21 +0000 | [diff] [blame] | 1980 | |
| 1981 | i = tb_jmp_cache_hash_page(addr); |
| 1982 | memset (&env->tb_jmp_cache[i], 0, |
Yoshiaki Tamura | 9742bf2 | 2010-08-18 13:30:13 +0900 | [diff] [blame] | 1983 | TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *)); |
edgar_igl | 5c751e9 | 2008-05-06 08:44:21 +0000 | [diff] [blame] | 1984 | } |
| 1985 | |
Igor Kovalenko | 0873898 | 2009-07-12 02:15:40 +0400 | [diff] [blame] | 1986 | static CPUTLBEntry s_cputlb_empty_entry = { |
| 1987 | .addr_read = -1, |
| 1988 | .addr_write = -1, |
| 1989 | .addr_code = -1, |
| 1990 | .addend = -1, |
| 1991 | }; |
| 1992 | |
bellard | ee8b702 | 2004-02-03 23:35:10 +0000 | [diff] [blame] | 1993 | /* NOTE: if flush_global is true, also flush global entries (not |
| 1994 | implemented yet) */ |
| 1995 | void tlb_flush(CPUState *env, int flush_global) |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 1996 | { |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 1997 | int i; |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 1998 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 1999 | #if defined(DEBUG_TLB) |
| 2000 | printf("tlb_flush:\n"); |
| 2001 | #endif |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 2002 | /* must reset current TB so that interrupts cannot modify the |
| 2003 | links while we are modifying them */ |
| 2004 | env->current_tb = NULL; |
| 2005 | |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2006 | for(i = 0; i < CPU_TLB_SIZE; i++) { |
Isaku Yamahata | cfde4bd | 2009-05-20 11:31:43 +0900 | [diff] [blame] | 2007 | int mmu_idx; |
| 2008 | for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) { |
Igor Kovalenko | 0873898 | 2009-07-12 02:15:40 +0400 | [diff] [blame] | 2009 | env->tlb_table[mmu_idx][i] = s_cputlb_empty_entry; |
Isaku Yamahata | cfde4bd | 2009-05-20 11:31:43 +0900 | [diff] [blame] | 2010 | } |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2011 | } |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2012 | |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 2013 | memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *)); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2014 | |
Paul Brook | d4c430a | 2010-03-17 02:14:28 +0000 | [diff] [blame] | 2015 | env->tlb_flush_addr = -1; |
| 2016 | env->tlb_flush_mask = 0; |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 2017 | tlb_flush_count++; |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2018 | } |
| 2019 | |
bellard | 274da6b | 2004-05-20 21:56:27 +0000 | [diff] [blame] | 2020 | static inline void tlb_flush_entry(CPUTLBEntry *tlb_entry, target_ulong addr) |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 2021 | { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2022 | if (addr == (tlb_entry->addr_read & |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 2023 | (TARGET_PAGE_MASK | TLB_INVALID_MASK)) || |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2024 | addr == (tlb_entry->addr_write & |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 2025 | (TARGET_PAGE_MASK | TLB_INVALID_MASK)) || |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2026 | addr == (tlb_entry->addr_code & |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 2027 | (TARGET_PAGE_MASK | TLB_INVALID_MASK))) { |
Igor Kovalenko | 0873898 | 2009-07-12 02:15:40 +0400 | [diff] [blame] | 2028 | *tlb_entry = s_cputlb_empty_entry; |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 2029 | } |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
bellard | 2e12669 | 2004-04-25 21:28:44 +0000 | [diff] [blame] | 2032 | void tlb_flush_page(CPUState *env, target_ulong addr) |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2033 | { |
bellard | 8a40a18 | 2005-11-20 10:35:40 +0000 | [diff] [blame] | 2034 | int i; |
Isaku Yamahata | cfde4bd | 2009-05-20 11:31:43 +0900 | [diff] [blame] | 2035 | int mmu_idx; |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 2036 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2037 | #if defined(DEBUG_TLB) |
bellard | 108c49b | 2005-07-24 12:55:09 +0000 | [diff] [blame] | 2038 | printf("tlb_flush_page: " TARGET_FMT_lx "\n", addr); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2039 | #endif |
Paul Brook | d4c430a | 2010-03-17 02:14:28 +0000 | [diff] [blame] | 2040 | /* Check if we need to flush due to large pages. */ |
| 2041 | if ((addr & env->tlb_flush_mask) == env->tlb_flush_addr) { |
| 2042 | #if defined(DEBUG_TLB) |
| 2043 | printf("tlb_flush_page: forced full flush (" |
| 2044 | TARGET_FMT_lx "/" TARGET_FMT_lx ")\n", |
| 2045 | env->tlb_flush_addr, env->tlb_flush_mask); |
| 2046 | #endif |
| 2047 | tlb_flush(env, 1); |
| 2048 | return; |
| 2049 | } |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 2050 | /* must reset current TB so that interrupts cannot modify the |
| 2051 | links while we are modifying them */ |
| 2052 | env->current_tb = NULL; |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2053 | |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 2054 | addr &= TARGET_PAGE_MASK; |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2055 | i = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1); |
Isaku Yamahata | cfde4bd | 2009-05-20 11:31:43 +0900 | [diff] [blame] | 2056 | for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) |
| 2057 | tlb_flush_entry(&env->tlb_table[mmu_idx][i], addr); |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 2058 | |
edgar_igl | 5c751e9 | 2008-05-06 08:44:21 +0000 | [diff] [blame] | 2059 | tlb_flush_jmp_cache(env, addr); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2060 | } |
| 2061 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2062 | /* update the TLBs so that writes to code in the virtual page 'addr' |
| 2063 | can be detected */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2064 | static void tlb_protect_code(ram_addr_t ram_addr) |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 2065 | { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2066 | cpu_physical_memory_reset_dirty(ram_addr, |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 2067 | ram_addr + TARGET_PAGE_SIZE, |
| 2068 | CODE_DIRTY_FLAG); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2071 | /* 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] | 2072 | tested for self modifying code */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2073 | static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr, |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2074 | target_ulong vaddr) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2075 | { |
Yoshiaki Tamura | f7c11b5 | 2010-03-23 16:39:53 +0900 | [diff] [blame] | 2076 | cpu_physical_memory_set_dirty_flags(ram_addr, CODE_DIRTY_FLAG); |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2077 | } |
| 2078 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2079 | static inline void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry, |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2080 | unsigned long start, unsigned long length) |
| 2081 | { |
| 2082 | unsigned long addr; |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 2083 | if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) { |
| 2084 | addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend; |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2085 | if ((addr - start) < length) { |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2086 | tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | TLB_NOTDIRTY; |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2087 | } |
| 2088 | } |
| 2089 | } |
| 2090 | |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2091 | /* Note: start and end must be within the same ram block. */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2092 | 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] | 2093 | int dirty_flags) |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2094 | { |
| 2095 | CPUState *env; |
bellard | 4f2ac23 | 2004-04-26 19:44:02 +0000 | [diff] [blame] | 2096 | unsigned long length, start1; |
Yoshiaki Tamura | f7c11b5 | 2010-03-23 16:39:53 +0900 | [diff] [blame] | 2097 | int i; |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2098 | |
| 2099 | start &= TARGET_PAGE_MASK; |
| 2100 | end = TARGET_PAGE_ALIGN(end); |
| 2101 | |
| 2102 | length = end - start; |
| 2103 | if (length == 0) |
| 2104 | return; |
Yoshiaki Tamura | f7c11b5 | 2010-03-23 16:39:53 +0900 | [diff] [blame] | 2105 | cpu_physical_memory_mask_dirty_range(start, length, dirty_flags); |
bellard | f23db16 | 2005-08-21 19:12:28 +0000 | [diff] [blame] | 2106 | |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2107 | /* we modify the TLB cache so that the dirty bit will be set again |
| 2108 | when accessing the range */ |
Michael S. Tsirkin | b2e0a13 | 2010-11-22 19:52:34 +0200 | [diff] [blame] | 2109 | start1 = (unsigned long)qemu_safe_ram_ptr(start); |
Stefan Weil | a57d23e | 2011-04-30 22:49:26 +0200 | [diff] [blame] | 2110 | /* Check that we don't span multiple blocks - this breaks the |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2111 | address comparisons below. */ |
Michael S. Tsirkin | b2e0a13 | 2010-11-22 19:52:34 +0200 | [diff] [blame] | 2112 | if ((unsigned long)qemu_safe_ram_ptr(end - 1) - start1 |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2113 | != (end - 1) - start) { |
| 2114 | abort(); |
| 2115 | } |
| 2116 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 2117 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
Isaku Yamahata | cfde4bd | 2009-05-20 11:31:43 +0900 | [diff] [blame] | 2118 | int mmu_idx; |
| 2119 | for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) { |
| 2120 | for(i = 0; i < CPU_TLB_SIZE; i++) |
| 2121 | tlb_reset_dirty_range(&env->tlb_table[mmu_idx][i], |
| 2122 | start1, length); |
| 2123 | } |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 2124 | } |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
aliguori | 7457619 | 2008-10-06 14:02:03 +0000 | [diff] [blame] | 2127 | int cpu_physical_memory_set_dirty_tracking(int enable) |
| 2128 | { |
Michael S. Tsirkin | f6f3fbc | 2010-01-27 22:06:57 +0200 | [diff] [blame] | 2129 | int ret = 0; |
aliguori | 7457619 | 2008-10-06 14:02:03 +0000 | [diff] [blame] | 2130 | in_migration = enable; |
Michael S. Tsirkin | f6f3fbc | 2010-01-27 22:06:57 +0200 | [diff] [blame] | 2131 | ret = cpu_notify_migration_log(!!enable); |
| 2132 | return ret; |
aliguori | 7457619 | 2008-10-06 14:02:03 +0000 | [diff] [blame] | 2133 | } |
| 2134 | |
| 2135 | int cpu_physical_memory_get_dirty_tracking(void) |
| 2136 | { |
| 2137 | return in_migration; |
| 2138 | } |
| 2139 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2140 | int cpu_physical_sync_dirty_bitmap(target_phys_addr_t start_addr, |
| 2141 | target_phys_addr_t end_addr) |
aliguori | 2bec46d | 2008-11-24 20:21:41 +0000 | [diff] [blame] | 2142 | { |
Michael S. Tsirkin | 7b8f3b7 | 2010-01-27 22:07:21 +0200 | [diff] [blame] | 2143 | int ret; |
Jan Kiszka | 151f774 | 2009-05-01 20:52:47 +0200 | [diff] [blame] | 2144 | |
Michael S. Tsirkin | f6f3fbc | 2010-01-27 22:06:57 +0200 | [diff] [blame] | 2145 | ret = cpu_notify_sync_dirty_bitmap(start_addr, end_addr); |
Jan Kiszka | 151f774 | 2009-05-01 20:52:47 +0200 | [diff] [blame] | 2146 | return ret; |
aliguori | 2bec46d | 2008-11-24 20:21:41 +0000 | [diff] [blame] | 2147 | } |
| 2148 | |
Anthony PERARD | e5896b1 | 2011-02-07 12:19:23 +0100 | [diff] [blame] | 2149 | int cpu_physical_log_start(target_phys_addr_t start_addr, |
| 2150 | ram_addr_t size) |
| 2151 | { |
| 2152 | CPUPhysMemoryClient *client; |
| 2153 | QLIST_FOREACH(client, &memory_client_list, list) { |
| 2154 | if (client->log_start) { |
| 2155 | int r = client->log_start(client, start_addr, size); |
| 2156 | if (r < 0) { |
| 2157 | return r; |
| 2158 | } |
| 2159 | } |
| 2160 | } |
| 2161 | return 0; |
| 2162 | } |
| 2163 | |
| 2164 | int cpu_physical_log_stop(target_phys_addr_t start_addr, |
| 2165 | ram_addr_t size) |
| 2166 | { |
| 2167 | CPUPhysMemoryClient *client; |
| 2168 | QLIST_FOREACH(client, &memory_client_list, list) { |
| 2169 | if (client->log_stop) { |
| 2170 | int r = client->log_stop(client, start_addr, size); |
| 2171 | if (r < 0) { |
| 2172 | return r; |
| 2173 | } |
| 2174 | } |
| 2175 | } |
| 2176 | return 0; |
| 2177 | } |
| 2178 | |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2179 | static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry) |
| 2180 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2181 | ram_addr_t ram_addr; |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2182 | void *p; |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2183 | |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 2184 | if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) { |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2185 | p = (void *)(unsigned long)((tlb_entry->addr_write & TARGET_PAGE_MASK) |
| 2186 | + tlb_entry->addend); |
Marcelo Tosatti | e890261 | 2010-10-11 15:31:19 -0300 | [diff] [blame] | 2187 | ram_addr = qemu_ram_addr_from_host_nofail(p); |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2188 | if (!cpu_physical_memory_is_dirty(ram_addr)) { |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2189 | tlb_entry->addr_write |= TLB_NOTDIRTY; |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2190 | } |
| 2191 | } |
| 2192 | } |
| 2193 | |
| 2194 | /* update the TLB according to the current state of the dirty bits */ |
| 2195 | void cpu_tlb_update_dirty(CPUState *env) |
| 2196 | { |
| 2197 | int i; |
Isaku Yamahata | cfde4bd | 2009-05-20 11:31:43 +0900 | [diff] [blame] | 2198 | int mmu_idx; |
| 2199 | for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) { |
| 2200 | for(i = 0; i < CPU_TLB_SIZE; i++) |
| 2201 | tlb_update_dirty(&env->tlb_table[mmu_idx][i]); |
| 2202 | } |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 2203 | } |
| 2204 | |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2205 | static inline void tlb_set_dirty1(CPUTLBEntry *tlb_entry, target_ulong vaddr) |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2206 | { |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2207 | if (tlb_entry->addr_write == (vaddr | TLB_NOTDIRTY)) |
| 2208 | tlb_entry->addr_write = vaddr; |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2209 | } |
| 2210 | |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2211 | /* update the TLB corresponding to virtual page vaddr |
| 2212 | so that it is no longer dirty */ |
| 2213 | static inline void tlb_set_dirty(CPUState *env, target_ulong vaddr) |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2214 | { |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2215 | int i; |
Isaku Yamahata | cfde4bd | 2009-05-20 11:31:43 +0900 | [diff] [blame] | 2216 | int mmu_idx; |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2217 | |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2218 | vaddr &= TARGET_PAGE_MASK; |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2219 | i = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1); |
Isaku Yamahata | cfde4bd | 2009-05-20 11:31:43 +0900 | [diff] [blame] | 2220 | for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) |
| 2221 | tlb_set_dirty1(&env->tlb_table[mmu_idx][i], vaddr); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2222 | } |
| 2223 | |
Paul Brook | d4c430a | 2010-03-17 02:14:28 +0000 | [diff] [blame] | 2224 | /* Our TLB does not support large pages, so remember the area covered by |
| 2225 | large pages and trigger a full TLB flush if these are invalidated. */ |
| 2226 | static void tlb_add_large_page(CPUState *env, target_ulong vaddr, |
| 2227 | target_ulong size) |
| 2228 | { |
| 2229 | target_ulong mask = ~(size - 1); |
| 2230 | |
| 2231 | if (env->tlb_flush_addr == (target_ulong)-1) { |
| 2232 | env->tlb_flush_addr = vaddr & mask; |
| 2233 | env->tlb_flush_mask = mask; |
| 2234 | return; |
| 2235 | } |
| 2236 | /* Extend the existing region to include the new page. |
| 2237 | This is a compromise between unnecessary flushes and the cost |
| 2238 | of maintaining a full variable size TLB. */ |
| 2239 | mask &= env->tlb_flush_mask; |
| 2240 | while (((env->tlb_flush_addr ^ vaddr) & mask) != 0) { |
| 2241 | mask <<= 1; |
| 2242 | } |
| 2243 | env->tlb_flush_addr &= mask; |
| 2244 | env->tlb_flush_mask = mask; |
| 2245 | } |
| 2246 | |
| 2247 | /* Add a new TLB entry. At most one entry for a given virtual address |
| 2248 | is permitted. Only a single TARGET_PAGE_SIZE region is mapped, the |
| 2249 | supplied size is only used by tlb_flush_page. */ |
| 2250 | void tlb_set_page(CPUState *env, target_ulong vaddr, |
| 2251 | target_phys_addr_t paddr, int prot, |
| 2252 | int mmu_idx, target_ulong size) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2253 | { |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 2254 | PhysPageDesc *p; |
bellard | 4f2ac23 | 2004-04-26 19:44:02 +0000 | [diff] [blame] | 2255 | unsigned long pd; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2256 | unsigned int index; |
bellard | 4f2ac23 | 2004-04-26 19:44:02 +0000 | [diff] [blame] | 2257 | target_ulong address; |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2258 | target_ulong code_address; |
Paul Brook | 355b194 | 2010-04-05 00:28:53 +0100 | [diff] [blame] | 2259 | unsigned long addend; |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 2260 | CPUTLBEntry *te; |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 2261 | CPUWatchpoint *wp; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2262 | target_phys_addr_t iotlb; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2263 | |
Paul Brook | d4c430a | 2010-03-17 02:14:28 +0000 | [diff] [blame] | 2264 | assert(size >= TARGET_PAGE_SIZE); |
| 2265 | if (size != TARGET_PAGE_SIZE) { |
| 2266 | tlb_add_large_page(env, vaddr, size); |
| 2267 | } |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 2268 | p = phys_page_find(paddr >> TARGET_PAGE_BITS); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2269 | if (!p) { |
| 2270 | pd = IO_MEM_UNASSIGNED; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2271 | } else { |
| 2272 | pd = p->phys_offset; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2273 | } |
| 2274 | #if defined(DEBUG_TLB) |
Stefan Weil | 7fd3f49 | 2010-09-30 22:39:51 +0200 | [diff] [blame] | 2275 | printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x" TARGET_FMT_plx |
| 2276 | " prot=%x idx=%d pd=0x%08lx\n", |
| 2277 | vaddr, paddr, prot, mmu_idx, pd); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2278 | #endif |
| 2279 | |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2280 | address = vaddr; |
| 2281 | if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) { |
| 2282 | /* IO memory case (romd handled later) */ |
| 2283 | address |= TLB_MMIO; |
| 2284 | } |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2285 | addend = (unsigned long)qemu_get_ram_ptr(pd & TARGET_PAGE_MASK); |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2286 | if ((pd & ~TARGET_PAGE_MASK) <= IO_MEM_ROM) { |
| 2287 | /* Normal RAM. */ |
| 2288 | iotlb = pd & TARGET_PAGE_MASK; |
| 2289 | if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM) |
| 2290 | iotlb |= IO_MEM_NOTDIRTY; |
| 2291 | else |
| 2292 | iotlb |= IO_MEM_ROM; |
| 2293 | } else { |
Stuart Brady | ccbb4d4 | 2009-05-03 12:15:06 +0100 | [diff] [blame] | 2294 | /* IO handlers are currently passed a physical address. |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2295 | It would be nice to pass an offset from the base address |
| 2296 | of that region. This would avoid having to special case RAM, |
| 2297 | and avoid full address decoding in every device. |
| 2298 | We can't use the high bits of pd for this because |
| 2299 | IO_MEM_ROMD uses these as a ram address. */ |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2300 | iotlb = (pd & ~TARGET_PAGE_MASK); |
| 2301 | if (p) { |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2302 | iotlb += p->region_offset; |
| 2303 | } else { |
| 2304 | iotlb += paddr; |
| 2305 | } |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2306 | } |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2307 | |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2308 | code_address = address; |
| 2309 | /* Make accesses to pages with watchpoints go via the |
| 2310 | watchpoint trap routines. */ |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 2311 | QTAILQ_FOREACH(wp, &env->watchpoints, entry) { |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 2312 | if (vaddr == (wp->vaddr & TARGET_PAGE_MASK)) { |
Jun Koi | bf298f8 | 2010-05-06 14:36:59 +0900 | [diff] [blame] | 2313 | /* Avoid trapping reads of pages with a write breakpoint. */ |
| 2314 | if ((prot & PAGE_WRITE) || (wp->flags & BP_MEM_READ)) { |
| 2315 | iotlb = io_mem_watch + paddr; |
| 2316 | address |= TLB_MMIO; |
| 2317 | break; |
| 2318 | } |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 2319 | } |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2320 | } |
balrog | d79acba | 2007-06-26 20:01:13 +0000 | [diff] [blame] | 2321 | |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2322 | index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1); |
| 2323 | env->iotlb[mmu_idx][index] = iotlb - vaddr; |
| 2324 | te = &env->tlb_table[mmu_idx][index]; |
| 2325 | te->addend = addend - vaddr; |
| 2326 | if (prot & PAGE_READ) { |
| 2327 | te->addr_read = address; |
| 2328 | } else { |
| 2329 | te->addr_read = -1; |
| 2330 | } |
edgar_igl | 5c751e9 | 2008-05-06 08:44:21 +0000 | [diff] [blame] | 2331 | |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2332 | if (prot & PAGE_EXEC) { |
| 2333 | te->addr_code = code_address; |
| 2334 | } else { |
| 2335 | te->addr_code = -1; |
| 2336 | } |
| 2337 | if (prot & PAGE_WRITE) { |
| 2338 | if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM || |
| 2339 | (pd & IO_MEM_ROMD)) { |
| 2340 | /* Write access calls the I/O callback. */ |
| 2341 | te->addr_write = address | TLB_MMIO; |
| 2342 | } else if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM && |
| 2343 | !cpu_physical_memory_is_dirty(pd)) { |
| 2344 | te->addr_write = address | TLB_NOTDIRTY; |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 2345 | } else { |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2346 | te->addr_write = address; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2347 | } |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 2348 | } else { |
| 2349 | te->addr_write = -1; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2350 | } |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2351 | } |
| 2352 | |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 2353 | #else |
| 2354 | |
bellard | ee8b702 | 2004-02-03 23:35:10 +0000 | [diff] [blame] | 2355 | void tlb_flush(CPUState *env, int flush_global) |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 2356 | { |
| 2357 | } |
| 2358 | |
bellard | 2e12669 | 2004-04-25 21:28:44 +0000 | [diff] [blame] | 2359 | void tlb_flush_page(CPUState *env, target_ulong addr) |
bellard | 0124311 | 2004-01-04 15:48:17 +0000 | [diff] [blame] | 2360 | { |
| 2361 | } |
| 2362 | |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 2363 | /* |
| 2364 | * Walks guest process memory "regions" one by one |
| 2365 | * and calls callback function 'fn' for each region. |
| 2366 | */ |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 2367 | |
| 2368 | struct walk_memory_regions_data |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2369 | { |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 2370 | walk_memory_regions_fn fn; |
| 2371 | void *priv; |
| 2372 | unsigned long start; |
| 2373 | int prot; |
| 2374 | }; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2375 | |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 2376 | static int walk_memory_regions_end(struct walk_memory_regions_data *data, |
Paul Brook | b480d9b | 2010-03-12 23:23:29 +0000 | [diff] [blame] | 2377 | abi_ulong end, int new_prot) |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 2378 | { |
| 2379 | if (data->start != -1ul) { |
| 2380 | int rc = data->fn(data->priv, data->start, end, data->prot); |
| 2381 | if (rc != 0) { |
| 2382 | return rc; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2383 | } |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2384 | } |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 2385 | |
| 2386 | data->start = (new_prot ? end : -1ul); |
| 2387 | data->prot = new_prot; |
| 2388 | |
| 2389 | return 0; |
| 2390 | } |
| 2391 | |
| 2392 | static int walk_memory_regions_1(struct walk_memory_regions_data *data, |
Paul Brook | b480d9b | 2010-03-12 23:23:29 +0000 | [diff] [blame] | 2393 | abi_ulong base, int level, void **lp) |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 2394 | { |
Paul Brook | b480d9b | 2010-03-12 23:23:29 +0000 | [diff] [blame] | 2395 | abi_ulong pa; |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 2396 | int i, rc; |
| 2397 | |
| 2398 | if (*lp == NULL) { |
| 2399 | return walk_memory_regions_end(data, base, 0); |
| 2400 | } |
| 2401 | |
| 2402 | if (level == 0) { |
| 2403 | PageDesc *pd = *lp; |
Paul Brook | 7296aba | 2010-03-14 14:58:46 +0000 | [diff] [blame] | 2404 | for (i = 0; i < L2_SIZE; ++i) { |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 2405 | int prot = pd[i].flags; |
| 2406 | |
| 2407 | pa = base | (i << TARGET_PAGE_BITS); |
| 2408 | if (prot != data->prot) { |
| 2409 | rc = walk_memory_regions_end(data, pa, prot); |
| 2410 | if (rc != 0) { |
| 2411 | return rc; |
| 2412 | } |
| 2413 | } |
| 2414 | } |
| 2415 | } else { |
| 2416 | void **pp = *lp; |
Paul Brook | 7296aba | 2010-03-14 14:58:46 +0000 | [diff] [blame] | 2417 | for (i = 0; i < L2_SIZE; ++i) { |
Paul Brook | b480d9b | 2010-03-12 23:23:29 +0000 | [diff] [blame] | 2418 | pa = base | ((abi_ulong)i << |
| 2419 | (TARGET_PAGE_BITS + L2_BITS * level)); |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 2420 | rc = walk_memory_regions_1(data, pa, level - 1, pp + i); |
| 2421 | if (rc != 0) { |
| 2422 | return rc; |
| 2423 | } |
| 2424 | } |
| 2425 | } |
| 2426 | |
| 2427 | return 0; |
| 2428 | } |
| 2429 | |
| 2430 | int walk_memory_regions(void *priv, walk_memory_regions_fn fn) |
| 2431 | { |
| 2432 | struct walk_memory_regions_data data; |
| 2433 | unsigned long i; |
| 2434 | |
| 2435 | data.fn = fn; |
| 2436 | data.priv = priv; |
| 2437 | data.start = -1ul; |
| 2438 | data.prot = 0; |
| 2439 | |
| 2440 | for (i = 0; i < V_L1_SIZE; i++) { |
Paul Brook | b480d9b | 2010-03-12 23:23:29 +0000 | [diff] [blame] | 2441 | int rc = walk_memory_regions_1(&data, (abi_ulong)i << V_L1_SHIFT, |
Richard Henderson | 5cd2c5b | 2010-03-10 15:53:37 -0800 | [diff] [blame] | 2442 | V_L1_SHIFT / L2_BITS - 1, l1_map + i); |
| 2443 | if (rc != 0) { |
| 2444 | return rc; |
| 2445 | } |
| 2446 | } |
| 2447 | |
| 2448 | return walk_memory_regions_end(&data, 0, 0); |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 2449 | } |
| 2450 | |
Paul Brook | b480d9b | 2010-03-12 23:23:29 +0000 | [diff] [blame] | 2451 | static int dump_region(void *priv, abi_ulong start, |
| 2452 | abi_ulong end, unsigned long prot) |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 2453 | { |
| 2454 | FILE *f = (FILE *)priv; |
| 2455 | |
Paul Brook | b480d9b | 2010-03-12 23:23:29 +0000 | [diff] [blame] | 2456 | (void) fprintf(f, TARGET_ABI_FMT_lx"-"TARGET_ABI_FMT_lx |
| 2457 | " "TARGET_ABI_FMT_lx" %c%c%c\n", |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 2458 | start, end, end - start, |
| 2459 | ((prot & PAGE_READ) ? 'r' : '-'), |
| 2460 | ((prot & PAGE_WRITE) ? 'w' : '-'), |
| 2461 | ((prot & PAGE_EXEC) ? 'x' : '-')); |
| 2462 | |
| 2463 | return (0); |
| 2464 | } |
| 2465 | |
| 2466 | /* dump memory mappings */ |
| 2467 | void page_dump(FILE *f) |
| 2468 | { |
| 2469 | (void) fprintf(f, "%-8s %-8s %-8s %s\n", |
| 2470 | "start", "end", "size", "prot"); |
| 2471 | walk_memory_regions(f, dump_region); |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2472 | } |
| 2473 | |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2474 | int page_get_flags(target_ulong address) |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2475 | { |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2476 | PageDesc *p; |
| 2477 | |
| 2478 | p = page_find(address >> TARGET_PAGE_BITS); |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2479 | if (!p) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2480 | return 0; |
| 2481 | return p->flags; |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
Richard Henderson | 376a790 | 2010-03-10 15:57:04 -0800 | [diff] [blame] | 2484 | /* Modify the flags of a page and invalidate the code if necessary. |
| 2485 | The flag PAGE_WRITE_ORG is positioned automatically depending |
| 2486 | on PAGE_WRITE. The mmap_lock should already be held. */ |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2487 | void page_set_flags(target_ulong start, target_ulong end, int flags) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2488 | { |
Richard Henderson | 376a790 | 2010-03-10 15:57:04 -0800 | [diff] [blame] | 2489 | target_ulong addr, len; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2490 | |
Richard Henderson | 376a790 | 2010-03-10 15:57:04 -0800 | [diff] [blame] | 2491 | /* This function should never be called with addresses outside the |
| 2492 | guest address space. If this assert fires, it probably indicates |
| 2493 | a missing call to h2g_valid. */ |
Paul Brook | b480d9b | 2010-03-12 23:23:29 +0000 | [diff] [blame] | 2494 | #if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS |
| 2495 | assert(end < ((abi_ulong)1 << L1_MAP_ADDR_SPACE_BITS)); |
Richard Henderson | 376a790 | 2010-03-10 15:57:04 -0800 | [diff] [blame] | 2496 | #endif |
| 2497 | assert(start < end); |
| 2498 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2499 | start = start & TARGET_PAGE_MASK; |
| 2500 | end = TARGET_PAGE_ALIGN(end); |
Richard Henderson | 376a790 | 2010-03-10 15:57:04 -0800 | [diff] [blame] | 2501 | |
| 2502 | if (flags & PAGE_WRITE) { |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2503 | flags |= PAGE_WRITE_ORG; |
Richard Henderson | 376a790 | 2010-03-10 15:57:04 -0800 | [diff] [blame] | 2504 | } |
| 2505 | |
| 2506 | for (addr = start, len = end - start; |
| 2507 | len != 0; |
| 2508 | len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) { |
| 2509 | PageDesc *p = page_find_alloc(addr >> TARGET_PAGE_BITS, 1); |
| 2510 | |
| 2511 | /* If the write protection bit is set, then we invalidate |
| 2512 | the code inside. */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2513 | if (!(p->flags & PAGE_WRITE) && |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2514 | (flags & PAGE_WRITE) && |
| 2515 | p->first_tb) { |
bellard | d720b93 | 2004-04-25 17:57:43 +0000 | [diff] [blame] | 2516 | tb_invalidate_phys_page(addr, 0, NULL); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2517 | } |
| 2518 | p->flags = flags; |
| 2519 | } |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2520 | } |
| 2521 | |
ths | 3d97b40 | 2007-11-02 19:02:07 +0000 | [diff] [blame] | 2522 | int page_check_range(target_ulong start, target_ulong len, int flags) |
| 2523 | { |
| 2524 | PageDesc *p; |
| 2525 | target_ulong end; |
| 2526 | target_ulong addr; |
| 2527 | |
Richard Henderson | 376a790 | 2010-03-10 15:57:04 -0800 | [diff] [blame] | 2528 | /* This function should never be called with addresses outside the |
| 2529 | guest address space. If this assert fires, it probably indicates |
| 2530 | a missing call to h2g_valid. */ |
Blue Swirl | 338e9e6 | 2010-03-13 09:48:08 +0000 | [diff] [blame] | 2531 | #if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS |
| 2532 | assert(start < ((abi_ulong)1 << L1_MAP_ADDR_SPACE_BITS)); |
Richard Henderson | 376a790 | 2010-03-10 15:57:04 -0800 | [diff] [blame] | 2533 | #endif |
| 2534 | |
Richard Henderson | 3e0650a | 2010-03-29 10:54:42 -0700 | [diff] [blame] | 2535 | if (len == 0) { |
| 2536 | return 0; |
| 2537 | } |
Richard Henderson | 376a790 | 2010-03-10 15:57:04 -0800 | [diff] [blame] | 2538 | if (start + len - 1 < start) { |
| 2539 | /* We've wrapped around. */ |
balrog | 55f280c | 2008-10-28 10:24:11 +0000 | [diff] [blame] | 2540 | return -1; |
Richard Henderson | 376a790 | 2010-03-10 15:57:04 -0800 | [diff] [blame] | 2541 | } |
balrog | 55f280c | 2008-10-28 10:24:11 +0000 | [diff] [blame] | 2542 | |
ths | 3d97b40 | 2007-11-02 19:02:07 +0000 | [diff] [blame] | 2543 | end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */ |
| 2544 | start = start & TARGET_PAGE_MASK; |
| 2545 | |
Richard Henderson | 376a790 | 2010-03-10 15:57:04 -0800 | [diff] [blame] | 2546 | for (addr = start, len = end - start; |
| 2547 | len != 0; |
| 2548 | len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) { |
ths | 3d97b40 | 2007-11-02 19:02:07 +0000 | [diff] [blame] | 2549 | p = page_find(addr >> TARGET_PAGE_BITS); |
| 2550 | if( !p ) |
| 2551 | return -1; |
| 2552 | if( !(p->flags & PAGE_VALID) ) |
| 2553 | return -1; |
| 2554 | |
bellard | dae3270 | 2007-11-14 10:51:00 +0000 | [diff] [blame] | 2555 | if ((flags & PAGE_READ) && !(p->flags & PAGE_READ)) |
ths | 3d97b40 | 2007-11-02 19:02:07 +0000 | [diff] [blame] | 2556 | return -1; |
bellard | dae3270 | 2007-11-14 10:51:00 +0000 | [diff] [blame] | 2557 | if (flags & PAGE_WRITE) { |
| 2558 | if (!(p->flags & PAGE_WRITE_ORG)) |
| 2559 | return -1; |
| 2560 | /* unprotect the page if it was put read-only because it |
| 2561 | contains translated code */ |
| 2562 | if (!(p->flags & PAGE_WRITE)) { |
| 2563 | if (!page_unprotect(addr, 0, NULL)) |
| 2564 | return -1; |
| 2565 | } |
| 2566 | return 0; |
| 2567 | } |
ths | 3d97b40 | 2007-11-02 19:02:07 +0000 | [diff] [blame] | 2568 | } |
| 2569 | return 0; |
| 2570 | } |
| 2571 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2572 | /* called from signal handler: invalidate the code and unprotect the |
Stuart Brady | ccbb4d4 | 2009-05-03 12:15:06 +0100 | [diff] [blame] | 2573 | page. Return TRUE if the fault was successfully handled. */ |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2574 | int page_unprotect(target_ulong address, unsigned long pc, void *puc) |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2575 | { |
Aurelien Jarno | 45d679d | 2010-03-29 02:12:51 +0200 | [diff] [blame] | 2576 | unsigned int prot; |
| 2577 | PageDesc *p; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 2578 | target_ulong host_start, host_end, addr; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2579 | |
pbrook | c8a706f | 2008-06-02 16:16:42 +0000 | [diff] [blame] | 2580 | /* Technically this isn't safe inside a signal handler. However we |
| 2581 | know this only ever happens in a synchronous SEGV handler, so in |
| 2582 | practice it seems to be ok. */ |
| 2583 | mmap_lock(); |
| 2584 | |
Aurelien Jarno | 45d679d | 2010-03-29 02:12:51 +0200 | [diff] [blame] | 2585 | p = page_find(address >> TARGET_PAGE_BITS); |
| 2586 | if (!p) { |
pbrook | c8a706f | 2008-06-02 16:16:42 +0000 | [diff] [blame] | 2587 | mmap_unlock(); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2588 | return 0; |
pbrook | c8a706f | 2008-06-02 16:16:42 +0000 | [diff] [blame] | 2589 | } |
Aurelien Jarno | 45d679d | 2010-03-29 02:12:51 +0200 | [diff] [blame] | 2590 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2591 | /* if the page was really writable, then we change its |
| 2592 | protection back to writable */ |
Aurelien Jarno | 45d679d | 2010-03-29 02:12:51 +0200 | [diff] [blame] | 2593 | if ((p->flags & PAGE_WRITE_ORG) && !(p->flags & PAGE_WRITE)) { |
| 2594 | host_start = address & qemu_host_page_mask; |
| 2595 | host_end = host_start + qemu_host_page_size; |
| 2596 | |
| 2597 | prot = 0; |
| 2598 | for (addr = host_start ; addr < host_end ; addr += TARGET_PAGE_SIZE) { |
| 2599 | p = page_find(addr >> TARGET_PAGE_BITS); |
| 2600 | p->flags |= PAGE_WRITE; |
| 2601 | prot |= p->flags; |
| 2602 | |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2603 | /* and since the content will be modified, we must invalidate |
| 2604 | the corresponding translated code. */ |
Aurelien Jarno | 45d679d | 2010-03-29 02:12:51 +0200 | [diff] [blame] | 2605 | tb_invalidate_phys_page(addr, pc, puc); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2606 | #ifdef DEBUG_TB_CHECK |
Aurelien Jarno | 45d679d | 2010-03-29 02:12:51 +0200 | [diff] [blame] | 2607 | tb_invalidate_check(addr); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2608 | #endif |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2609 | } |
Aurelien Jarno | 45d679d | 2010-03-29 02:12:51 +0200 | [diff] [blame] | 2610 | mprotect((void *)g2h(host_start), qemu_host_page_size, |
| 2611 | prot & PAGE_BITS); |
| 2612 | |
| 2613 | mmap_unlock(); |
| 2614 | return 1; |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2615 | } |
pbrook | c8a706f | 2008-06-02 16:16:42 +0000 | [diff] [blame] | 2616 | mmap_unlock(); |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2617 | return 0; |
| 2618 | } |
| 2619 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 2620 | static inline void tlb_set_dirty(CPUState *env, |
| 2621 | unsigned long addr, target_ulong vaddr) |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 2622 | { |
| 2623 | } |
bellard | 9fa3e85 | 2004-01-04 18:06:42 +0000 | [diff] [blame] | 2624 | #endif /* defined(CONFIG_USER_ONLY) */ |
| 2625 | |
pbrook | e2eef17 | 2008-06-08 01:09:01 +0000 | [diff] [blame] | 2626 | #if !defined(CONFIG_USER_ONLY) |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2627 | |
Paul Brook | c04b2b7 | 2010-03-01 03:31:14 +0000 | [diff] [blame] | 2628 | #define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK) |
| 2629 | typedef struct subpage_t { |
| 2630 | target_phys_addr_t base; |
Richard Henderson | f640524 | 2010-04-22 16:47:31 -0700 | [diff] [blame] | 2631 | ram_addr_t sub_io_index[TARGET_PAGE_SIZE]; |
| 2632 | ram_addr_t region_offset[TARGET_PAGE_SIZE]; |
Paul Brook | c04b2b7 | 2010-03-01 03:31:14 +0000 | [diff] [blame] | 2633 | } subpage_t; |
| 2634 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2635 | static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end, |
| 2636 | ram_addr_t memory, ram_addr_t region_offset); |
Richard Henderson | f640524 | 2010-04-22 16:47:31 -0700 | [diff] [blame] | 2637 | static subpage_t *subpage_init (target_phys_addr_t base, ram_addr_t *phys, |
| 2638 | ram_addr_t orig_memory, |
| 2639 | ram_addr_t region_offset); |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2640 | #define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \ |
| 2641 | need_subpage) \ |
| 2642 | do { \ |
| 2643 | if (addr > start_addr) \ |
| 2644 | start_addr2 = 0; \ |
| 2645 | else { \ |
| 2646 | start_addr2 = start_addr & ~TARGET_PAGE_MASK; \ |
| 2647 | if (start_addr2 > 0) \ |
| 2648 | need_subpage = 1; \ |
| 2649 | } \ |
| 2650 | \ |
blueswir1 | 49e9fba | 2007-05-30 17:25:06 +0000 | [diff] [blame] | 2651 | if ((start_addr + orig_size) - addr >= TARGET_PAGE_SIZE) \ |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2652 | end_addr2 = TARGET_PAGE_SIZE - 1; \ |
| 2653 | else { \ |
| 2654 | end_addr2 = (start_addr + orig_size - 1) & ~TARGET_PAGE_MASK; \ |
| 2655 | if (end_addr2 < TARGET_PAGE_SIZE - 1) \ |
| 2656 | need_subpage = 1; \ |
| 2657 | } \ |
| 2658 | } while (0) |
| 2659 | |
Michael S. Tsirkin | 8f2498f | 2009-09-29 18:53:16 +0200 | [diff] [blame] | 2660 | /* register physical memory. |
| 2661 | For RAM, 'size' must be a multiple of the target page size. |
| 2662 | If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2663 | io memory page. The address used when calling the IO function is |
| 2664 | the offset from the start of the region, plus region_offset. Both |
Stuart Brady | ccbb4d4 | 2009-05-03 12:15:06 +0100 | [diff] [blame] | 2665 | start_addr and region_offset are rounded down to a page boundary |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2666 | before calculating this offset. This should not be a problem unless |
| 2667 | the low bits of start_addr and region_offset differ. */ |
Michael S. Tsirkin | 0fd542f | 2011-04-06 22:25:38 +0300 | [diff] [blame] | 2668 | void cpu_register_physical_memory_log(target_phys_addr_t start_addr, |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2669 | ram_addr_t size, |
| 2670 | ram_addr_t phys_offset, |
Michael S. Tsirkin | 0fd542f | 2011-04-06 22:25:38 +0300 | [diff] [blame] | 2671 | ram_addr_t region_offset, |
| 2672 | bool log_dirty) |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2673 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2674 | target_phys_addr_t addr, end_addr; |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 2675 | PhysPageDesc *p; |
bellard | 9d42037 | 2006-06-25 22:25:22 +0000 | [diff] [blame] | 2676 | CPUState *env; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2677 | ram_addr_t orig_size = size; |
Richard Henderson | f640524 | 2010-04-22 16:47:31 -0700 | [diff] [blame] | 2678 | subpage_t *subpage; |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2679 | |
Edgar E. Iglesias | 3b8e6a2 | 2011-04-05 13:00:36 +0200 | [diff] [blame] | 2680 | assert(size); |
Michael S. Tsirkin | 0fd542f | 2011-04-06 22:25:38 +0300 | [diff] [blame] | 2681 | cpu_notify_set_memory(start_addr, size, phys_offset, log_dirty); |
Michael S. Tsirkin | f6f3fbc | 2010-01-27 22:06:57 +0200 | [diff] [blame] | 2682 | |
pbrook | 67c4d23 | 2009-02-23 13:16:07 +0000 | [diff] [blame] | 2683 | if (phys_offset == IO_MEM_UNASSIGNED) { |
| 2684 | region_offset = start_addr; |
| 2685 | } |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2686 | region_offset &= TARGET_PAGE_MASK; |
bellard | 5fd386f | 2004-05-23 21:11:22 +0000 | [diff] [blame] | 2687 | size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2688 | end_addr = start_addr + (target_phys_addr_t)size; |
Edgar E. Iglesias | 3b8e6a2 | 2011-04-05 13:00:36 +0200 | [diff] [blame] | 2689 | |
| 2690 | addr = start_addr; |
| 2691 | do { |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2692 | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
| 2693 | if (p && p->phys_offset != IO_MEM_UNASSIGNED) { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2694 | ram_addr_t orig_memory = p->phys_offset; |
| 2695 | target_phys_addr_t start_addr2, end_addr2; |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2696 | int need_subpage = 0; |
| 2697 | |
| 2698 | CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, |
| 2699 | need_subpage); |
Richard Henderson | f640524 | 2010-04-22 16:47:31 -0700 | [diff] [blame] | 2700 | if (need_subpage) { |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2701 | if (!(orig_memory & IO_MEM_SUBPAGE)) { |
| 2702 | subpage = subpage_init((addr & TARGET_PAGE_MASK), |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2703 | &p->phys_offset, orig_memory, |
| 2704 | p->region_offset); |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2705 | } else { |
| 2706 | subpage = io_mem_opaque[(orig_memory & ~TARGET_PAGE_MASK) |
| 2707 | >> IO_MEM_SHIFT]; |
| 2708 | } |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2709 | subpage_register(subpage, start_addr2, end_addr2, phys_offset, |
| 2710 | region_offset); |
| 2711 | p->region_offset = 0; |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2712 | } else { |
| 2713 | p->phys_offset = phys_offset; |
| 2714 | if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM || |
| 2715 | (phys_offset & IO_MEM_ROMD)) |
| 2716 | phys_offset += TARGET_PAGE_SIZE; |
| 2717 | } |
| 2718 | } else { |
| 2719 | p = phys_page_find_alloc(addr >> TARGET_PAGE_BITS, 1); |
| 2720 | p->phys_offset = phys_offset; |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2721 | p->region_offset = region_offset; |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2722 | if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM || |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2723 | (phys_offset & IO_MEM_ROMD)) { |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2724 | phys_offset += TARGET_PAGE_SIZE; |
pbrook | 0e8f096 | 2008-12-02 09:02:15 +0000 | [diff] [blame] | 2725 | } else { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2726 | target_phys_addr_t start_addr2, end_addr2; |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2727 | int need_subpage = 0; |
| 2728 | |
| 2729 | CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, |
| 2730 | end_addr2, need_subpage); |
| 2731 | |
Richard Henderson | f640524 | 2010-04-22 16:47:31 -0700 | [diff] [blame] | 2732 | if (need_subpage) { |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2733 | subpage = subpage_init((addr & TARGET_PAGE_MASK), |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2734 | &p->phys_offset, IO_MEM_UNASSIGNED, |
pbrook | 67c4d23 | 2009-02-23 13:16:07 +0000 | [diff] [blame] | 2735 | addr & TARGET_PAGE_MASK); |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2736 | subpage_register(subpage, start_addr2, end_addr2, |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2737 | phys_offset, region_offset); |
| 2738 | p->region_offset = 0; |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 2739 | } |
| 2740 | } |
| 2741 | } |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 2742 | region_offset += TARGET_PAGE_SIZE; |
Edgar E. Iglesias | 3b8e6a2 | 2011-04-05 13:00:36 +0200 | [diff] [blame] | 2743 | addr += TARGET_PAGE_SIZE; |
| 2744 | } while (addr != end_addr); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2745 | |
bellard | 9d42037 | 2006-06-25 22:25:22 +0000 | [diff] [blame] | 2746 | /* since each CPU stores ram addresses in its TLB cache, we must |
| 2747 | reset the modified entries */ |
| 2748 | /* XXX: slow ! */ |
| 2749 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
| 2750 | tlb_flush(env, 1); |
| 2751 | } |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 2752 | } |
| 2753 | |
bellard | ba86345 | 2006-09-24 18:41:10 +0000 | [diff] [blame] | 2754 | /* XXX: temporary until new memory mapping API */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2755 | ram_addr_t cpu_get_physical_page_desc(target_phys_addr_t addr) |
bellard | ba86345 | 2006-09-24 18:41:10 +0000 | [diff] [blame] | 2756 | { |
| 2757 | PhysPageDesc *p; |
| 2758 | |
| 2759 | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
| 2760 | if (!p) |
| 2761 | return IO_MEM_UNASSIGNED; |
| 2762 | return p->phys_offset; |
| 2763 | } |
| 2764 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2765 | 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] | 2766 | { |
| 2767 | if (kvm_enabled()) |
| 2768 | kvm_coalesce_mmio_region(addr, size); |
| 2769 | } |
| 2770 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2771 | 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] | 2772 | { |
| 2773 | if (kvm_enabled()) |
| 2774 | kvm_uncoalesce_mmio_region(addr, size); |
| 2775 | } |
| 2776 | |
Sheng Yang | 62a2744 | 2010-01-26 19:21:16 +0800 | [diff] [blame] | 2777 | void qemu_flush_coalesced_mmio_buffer(void) |
| 2778 | { |
| 2779 | if (kvm_enabled()) |
| 2780 | kvm_flush_coalesced_mmio_buffer(); |
| 2781 | } |
| 2782 | |
Marcelo Tosatti | c902760 | 2010-03-01 20:25:08 -0300 | [diff] [blame] | 2783 | #if defined(__linux__) && !defined(TARGET_S390X) |
| 2784 | |
| 2785 | #include <sys/vfs.h> |
| 2786 | |
| 2787 | #define HUGETLBFS_MAGIC 0x958458f6 |
| 2788 | |
| 2789 | static long gethugepagesize(const char *path) |
| 2790 | { |
| 2791 | struct statfs fs; |
| 2792 | int ret; |
| 2793 | |
| 2794 | do { |
Yoshiaki Tamura | 9742bf2 | 2010-08-18 13:30:13 +0900 | [diff] [blame] | 2795 | ret = statfs(path, &fs); |
Marcelo Tosatti | c902760 | 2010-03-01 20:25:08 -0300 | [diff] [blame] | 2796 | } while (ret != 0 && errno == EINTR); |
| 2797 | |
| 2798 | if (ret != 0) { |
Yoshiaki Tamura | 9742bf2 | 2010-08-18 13:30:13 +0900 | [diff] [blame] | 2799 | perror(path); |
| 2800 | return 0; |
Marcelo Tosatti | c902760 | 2010-03-01 20:25:08 -0300 | [diff] [blame] | 2801 | } |
| 2802 | |
| 2803 | if (fs.f_type != HUGETLBFS_MAGIC) |
Yoshiaki Tamura | 9742bf2 | 2010-08-18 13:30:13 +0900 | [diff] [blame] | 2804 | fprintf(stderr, "Warning: path not on HugeTLBFS: %s\n", path); |
Marcelo Tosatti | c902760 | 2010-03-01 20:25:08 -0300 | [diff] [blame] | 2805 | |
| 2806 | return fs.f_bsize; |
| 2807 | } |
| 2808 | |
Alex Williamson | 04b1665 | 2010-07-02 11:13:17 -0600 | [diff] [blame] | 2809 | static void *file_ram_alloc(RAMBlock *block, |
| 2810 | ram_addr_t memory, |
| 2811 | const char *path) |
Marcelo Tosatti | c902760 | 2010-03-01 20:25:08 -0300 | [diff] [blame] | 2812 | { |
| 2813 | char *filename; |
| 2814 | void *area; |
| 2815 | int fd; |
| 2816 | #ifdef MAP_POPULATE |
| 2817 | int flags; |
| 2818 | #endif |
| 2819 | unsigned long hpagesize; |
| 2820 | |
| 2821 | hpagesize = gethugepagesize(path); |
| 2822 | if (!hpagesize) { |
Yoshiaki Tamura | 9742bf2 | 2010-08-18 13:30:13 +0900 | [diff] [blame] | 2823 | return NULL; |
Marcelo Tosatti | c902760 | 2010-03-01 20:25:08 -0300 | [diff] [blame] | 2824 | } |
| 2825 | |
| 2826 | if (memory < hpagesize) { |
| 2827 | return NULL; |
| 2828 | } |
| 2829 | |
| 2830 | if (kvm_enabled() && !kvm_has_sync_mmu()) { |
| 2831 | fprintf(stderr, "host lacks kvm mmu notifiers, -mem-path unsupported\n"); |
| 2832 | return NULL; |
| 2833 | } |
| 2834 | |
| 2835 | if (asprintf(&filename, "%s/qemu_back_mem.XXXXXX", path) == -1) { |
Yoshiaki Tamura | 9742bf2 | 2010-08-18 13:30:13 +0900 | [diff] [blame] | 2836 | return NULL; |
Marcelo Tosatti | c902760 | 2010-03-01 20:25:08 -0300 | [diff] [blame] | 2837 | } |
| 2838 | |
| 2839 | fd = mkstemp(filename); |
| 2840 | if (fd < 0) { |
Yoshiaki Tamura | 9742bf2 | 2010-08-18 13:30:13 +0900 | [diff] [blame] | 2841 | perror("unable to create backing store for hugepages"); |
| 2842 | free(filename); |
| 2843 | return NULL; |
Marcelo Tosatti | c902760 | 2010-03-01 20:25:08 -0300 | [diff] [blame] | 2844 | } |
| 2845 | unlink(filename); |
| 2846 | free(filename); |
| 2847 | |
| 2848 | memory = (memory+hpagesize-1) & ~(hpagesize-1); |
| 2849 | |
| 2850 | /* |
| 2851 | * ftruncate is not supported by hugetlbfs in older |
| 2852 | * hosts, so don't bother bailing out on errors. |
| 2853 | * If anything goes wrong with it under other filesystems, |
| 2854 | * mmap will fail. |
| 2855 | */ |
| 2856 | if (ftruncate(fd, memory)) |
Yoshiaki Tamura | 9742bf2 | 2010-08-18 13:30:13 +0900 | [diff] [blame] | 2857 | perror("ftruncate"); |
Marcelo Tosatti | c902760 | 2010-03-01 20:25:08 -0300 | [diff] [blame] | 2858 | |
| 2859 | #ifdef MAP_POPULATE |
| 2860 | /* NB: MAP_POPULATE won't exhaustively alloc all phys pages in the case |
| 2861 | * MAP_PRIVATE is requested. For mem_prealloc we mmap as MAP_SHARED |
| 2862 | * to sidestep this quirk. |
| 2863 | */ |
| 2864 | flags = mem_prealloc ? MAP_POPULATE | MAP_SHARED : MAP_PRIVATE; |
| 2865 | area = mmap(0, memory, PROT_READ | PROT_WRITE, flags, fd, 0); |
| 2866 | #else |
| 2867 | area = mmap(0, memory, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
| 2868 | #endif |
| 2869 | if (area == MAP_FAILED) { |
Yoshiaki Tamura | 9742bf2 | 2010-08-18 13:30:13 +0900 | [diff] [blame] | 2870 | perror("file_ram_alloc: can't mmap RAM pages"); |
| 2871 | close(fd); |
| 2872 | return (NULL); |
Marcelo Tosatti | c902760 | 2010-03-01 20:25:08 -0300 | [diff] [blame] | 2873 | } |
Alex Williamson | 04b1665 | 2010-07-02 11:13:17 -0600 | [diff] [blame] | 2874 | block->fd = fd; |
Marcelo Tosatti | c902760 | 2010-03-01 20:25:08 -0300 | [diff] [blame] | 2875 | return area; |
| 2876 | } |
| 2877 | #endif |
| 2878 | |
Alex Williamson | d17b528 | 2010-06-25 11:08:38 -0600 | [diff] [blame] | 2879 | static ram_addr_t find_ram_offset(ram_addr_t size) |
| 2880 | { |
Alex Williamson | 04b1665 | 2010-07-02 11:13:17 -0600 | [diff] [blame] | 2881 | RAMBlock *block, *next_block; |
Anthony PERARD | f15fbc4 | 2011-07-20 08:17:42 +0000 | [diff] [blame] | 2882 | ram_addr_t offset = 0, mingap = RAM_ADDR_MAX; |
Alex Williamson | 04b1665 | 2010-07-02 11:13:17 -0600 | [diff] [blame] | 2883 | |
| 2884 | if (QLIST_EMPTY(&ram_list.blocks)) |
| 2885 | return 0; |
| 2886 | |
| 2887 | QLIST_FOREACH(block, &ram_list.blocks, next) { |
Anthony PERARD | f15fbc4 | 2011-07-20 08:17:42 +0000 | [diff] [blame] | 2888 | ram_addr_t end, next = RAM_ADDR_MAX; |
Alex Williamson | 04b1665 | 2010-07-02 11:13:17 -0600 | [diff] [blame] | 2889 | |
| 2890 | end = block->offset + block->length; |
| 2891 | |
| 2892 | QLIST_FOREACH(next_block, &ram_list.blocks, next) { |
| 2893 | if (next_block->offset >= end) { |
| 2894 | next = MIN(next, next_block->offset); |
| 2895 | } |
| 2896 | } |
| 2897 | if (next - end >= size && next - end < mingap) { |
| 2898 | offset = end; |
| 2899 | mingap = next - end; |
| 2900 | } |
| 2901 | } |
| 2902 | return offset; |
| 2903 | } |
| 2904 | |
| 2905 | static ram_addr_t last_ram_offset(void) |
| 2906 | { |
Alex Williamson | d17b528 | 2010-06-25 11:08:38 -0600 | [diff] [blame] | 2907 | RAMBlock *block; |
| 2908 | ram_addr_t last = 0; |
| 2909 | |
| 2910 | QLIST_FOREACH(block, &ram_list.blocks, next) |
| 2911 | last = MAX(last, block->offset + block->length); |
| 2912 | |
| 2913 | return last; |
| 2914 | } |
| 2915 | |
Cam Macdonell | 84b89d7 | 2010-07-26 18:10:57 -0600 | [diff] [blame] | 2916 | ram_addr_t qemu_ram_alloc_from_ptr(DeviceState *dev, const char *name, |
Yoshiaki Tamura | 6977dfe | 2010-08-18 15:41:49 +0900 | [diff] [blame] | 2917 | ram_addr_t size, void *host) |
Cam Macdonell | 84b89d7 | 2010-07-26 18:10:57 -0600 | [diff] [blame] | 2918 | { |
| 2919 | RAMBlock *new_block, *block; |
| 2920 | |
| 2921 | size = TARGET_PAGE_ALIGN(size); |
| 2922 | new_block = qemu_mallocz(sizeof(*new_block)); |
| 2923 | |
| 2924 | if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) { |
| 2925 | char *id = dev->parent_bus->info->get_dev_path(dev); |
| 2926 | if (id) { |
| 2927 | snprintf(new_block->idstr, sizeof(new_block->idstr), "%s/", id); |
| 2928 | qemu_free(id); |
| 2929 | } |
| 2930 | } |
| 2931 | pstrcat(new_block->idstr, sizeof(new_block->idstr), name); |
| 2932 | |
| 2933 | QLIST_FOREACH(block, &ram_list.blocks, next) { |
| 2934 | if (!strcmp(block->idstr, new_block->idstr)) { |
| 2935 | fprintf(stderr, "RAMBlock \"%s\" already registered, abort!\n", |
| 2936 | new_block->idstr); |
| 2937 | abort(); |
| 2938 | } |
| 2939 | } |
| 2940 | |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 2941 | new_block->offset = find_ram_offset(size); |
Yoshiaki Tamura | 6977dfe | 2010-08-18 15:41:49 +0900 | [diff] [blame] | 2942 | if (host) { |
| 2943 | new_block->host = host; |
Huang Ying | cd19cfa | 2011-03-02 08:56:19 +0100 | [diff] [blame] | 2944 | new_block->flags |= RAM_PREALLOC_MASK; |
Yoshiaki Tamura | 6977dfe | 2010-08-18 15:41:49 +0900 | [diff] [blame] | 2945 | } else { |
| 2946 | if (mem_path) { |
| 2947 | #if defined (__linux__) && !defined(TARGET_S390X) |
| 2948 | new_block->host = file_ram_alloc(new_block, size, mem_path); |
| 2949 | if (!new_block->host) { |
| 2950 | new_block->host = qemu_vmalloc(size); |
Andreas Färber | e78815a | 2010-09-25 11:26:05 +0000 | [diff] [blame] | 2951 | qemu_madvise(new_block->host, size, QEMU_MADV_MERGEABLE); |
Yoshiaki Tamura | 6977dfe | 2010-08-18 15:41:49 +0900 | [diff] [blame] | 2952 | } |
| 2953 | #else |
| 2954 | fprintf(stderr, "-mem-path option unsupported\n"); |
| 2955 | exit(1); |
| 2956 | #endif |
| 2957 | } else { |
| 2958 | #if defined(TARGET_S390X) && defined(CONFIG_KVM) |
Christian Borntraeger | ff83678 | 2011-05-10 14:49:10 +0200 | [diff] [blame] | 2959 | /* S390 KVM requires the topmost vma of the RAM to be smaller than |
| 2960 | an system defined value, which is at least 256GB. Larger systems |
| 2961 | have larger values. We put the guest between the end of data |
| 2962 | segment (system break) and this value. We use 32GB as a base to |
| 2963 | have enough room for the system break to grow. */ |
| 2964 | new_block->host = mmap((void*)0x800000000, size, |
Yoshiaki Tamura | 6977dfe | 2010-08-18 15:41:49 +0900 | [diff] [blame] | 2965 | PROT_EXEC|PROT_READ|PROT_WRITE, |
Christian Borntraeger | ff83678 | 2011-05-10 14:49:10 +0200 | [diff] [blame] | 2966 | MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0); |
Alexander Graf | fb8b273 | 2011-05-20 17:33:28 +0200 | [diff] [blame] | 2967 | if (new_block->host == MAP_FAILED) { |
| 2968 | fprintf(stderr, "Allocating RAM failed\n"); |
| 2969 | abort(); |
| 2970 | } |
Yoshiaki Tamura | 6977dfe | 2010-08-18 15:41:49 +0900 | [diff] [blame] | 2971 | #else |
Jan Kiszka | 868bb33 | 2011-06-21 22:59:09 +0200 | [diff] [blame] | 2972 | if (xen_enabled()) { |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 2973 | xen_ram_alloc(new_block->offset, size); |
| 2974 | } else { |
| 2975 | new_block->host = qemu_vmalloc(size); |
| 2976 | } |
Yoshiaki Tamura | 6977dfe | 2010-08-18 15:41:49 +0900 | [diff] [blame] | 2977 | #endif |
Andreas Färber | e78815a | 2010-09-25 11:26:05 +0000 | [diff] [blame] | 2978 | qemu_madvise(new_block->host, size, QEMU_MADV_MERGEABLE); |
Yoshiaki Tamura | 6977dfe | 2010-08-18 15:41:49 +0900 | [diff] [blame] | 2979 | } |
| 2980 | } |
Cam Macdonell | 84b89d7 | 2010-07-26 18:10:57 -0600 | [diff] [blame] | 2981 | new_block->length = size; |
| 2982 | |
| 2983 | QLIST_INSERT_HEAD(&ram_list.blocks, new_block, next); |
| 2984 | |
| 2985 | ram_list.phys_dirty = qemu_realloc(ram_list.phys_dirty, |
| 2986 | last_ram_offset() >> TARGET_PAGE_BITS); |
| 2987 | memset(ram_list.phys_dirty + (new_block->offset >> TARGET_PAGE_BITS), |
| 2988 | 0xff, size >> TARGET_PAGE_BITS); |
| 2989 | |
| 2990 | if (kvm_enabled()) |
| 2991 | kvm_setup_guest_memory(new_block->host, size); |
| 2992 | |
| 2993 | return new_block->offset; |
| 2994 | } |
| 2995 | |
Alex Williamson | 1724f04 | 2010-06-25 11:09:35 -0600 | [diff] [blame] | 2996 | ram_addr_t qemu_ram_alloc(DeviceState *dev, const char *name, ram_addr_t size) |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2997 | { |
Yoshiaki Tamura | 6977dfe | 2010-08-18 15:41:49 +0900 | [diff] [blame] | 2998 | return qemu_ram_alloc_from_ptr(dev, name, size, NULL); |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2999 | } |
bellard | e9a1ab1 | 2007-02-08 23:08:38 +0000 | [diff] [blame] | 3000 | |
Alex Williamson | 1f2e98b | 2011-05-03 12:48:09 -0600 | [diff] [blame] | 3001 | void qemu_ram_free_from_ptr(ram_addr_t addr) |
| 3002 | { |
| 3003 | RAMBlock *block; |
| 3004 | |
| 3005 | QLIST_FOREACH(block, &ram_list.blocks, next) { |
| 3006 | if (addr == block->offset) { |
| 3007 | QLIST_REMOVE(block, next); |
| 3008 | qemu_free(block); |
| 3009 | return; |
| 3010 | } |
| 3011 | } |
| 3012 | } |
| 3013 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3014 | void qemu_ram_free(ram_addr_t addr) |
bellard | e9a1ab1 | 2007-02-08 23:08:38 +0000 | [diff] [blame] | 3015 | { |
Alex Williamson | 04b1665 | 2010-07-02 11:13:17 -0600 | [diff] [blame] | 3016 | RAMBlock *block; |
| 3017 | |
| 3018 | QLIST_FOREACH(block, &ram_list.blocks, next) { |
| 3019 | if (addr == block->offset) { |
| 3020 | QLIST_REMOVE(block, next); |
Huang Ying | cd19cfa | 2011-03-02 08:56:19 +0100 | [diff] [blame] | 3021 | if (block->flags & RAM_PREALLOC_MASK) { |
| 3022 | ; |
| 3023 | } else if (mem_path) { |
Alex Williamson | 04b1665 | 2010-07-02 11:13:17 -0600 | [diff] [blame] | 3024 | #if defined (__linux__) && !defined(TARGET_S390X) |
| 3025 | if (block->fd) { |
| 3026 | munmap(block->host, block->length); |
| 3027 | close(block->fd); |
| 3028 | } else { |
| 3029 | qemu_vfree(block->host); |
| 3030 | } |
Jan Kiszka | fd28aa1 | 2011-03-15 12:26:14 +0100 | [diff] [blame] | 3031 | #else |
| 3032 | abort(); |
Alex Williamson | 04b1665 | 2010-07-02 11:13:17 -0600 | [diff] [blame] | 3033 | #endif |
| 3034 | } else { |
| 3035 | #if defined(TARGET_S390X) && defined(CONFIG_KVM) |
| 3036 | munmap(block->host, block->length); |
| 3037 | #else |
Jan Kiszka | 868bb33 | 2011-06-21 22:59:09 +0200 | [diff] [blame] | 3038 | if (xen_enabled()) { |
Jan Kiszka | e41d7c6 | 2011-06-21 22:59:08 +0200 | [diff] [blame] | 3039 | xen_invalidate_map_cache_entry(block->host); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 3040 | } else { |
| 3041 | qemu_vfree(block->host); |
| 3042 | } |
Alex Williamson | 04b1665 | 2010-07-02 11:13:17 -0600 | [diff] [blame] | 3043 | #endif |
| 3044 | } |
| 3045 | qemu_free(block); |
| 3046 | return; |
| 3047 | } |
| 3048 | } |
| 3049 | |
bellard | e9a1ab1 | 2007-02-08 23:08:38 +0000 | [diff] [blame] | 3050 | } |
| 3051 | |
Huang Ying | cd19cfa | 2011-03-02 08:56:19 +0100 | [diff] [blame] | 3052 | #ifndef _WIN32 |
| 3053 | void qemu_ram_remap(ram_addr_t addr, ram_addr_t length) |
| 3054 | { |
| 3055 | RAMBlock *block; |
| 3056 | ram_addr_t offset; |
| 3057 | int flags; |
| 3058 | void *area, *vaddr; |
| 3059 | |
| 3060 | QLIST_FOREACH(block, &ram_list.blocks, next) { |
| 3061 | offset = addr - block->offset; |
| 3062 | if (offset < block->length) { |
| 3063 | vaddr = block->host + offset; |
| 3064 | if (block->flags & RAM_PREALLOC_MASK) { |
| 3065 | ; |
| 3066 | } else { |
| 3067 | flags = MAP_FIXED; |
| 3068 | munmap(vaddr, length); |
| 3069 | if (mem_path) { |
| 3070 | #if defined(__linux__) && !defined(TARGET_S390X) |
| 3071 | if (block->fd) { |
| 3072 | #ifdef MAP_POPULATE |
| 3073 | flags |= mem_prealloc ? MAP_POPULATE | MAP_SHARED : |
| 3074 | MAP_PRIVATE; |
| 3075 | #else |
| 3076 | flags |= MAP_PRIVATE; |
| 3077 | #endif |
| 3078 | area = mmap(vaddr, length, PROT_READ | PROT_WRITE, |
| 3079 | flags, block->fd, offset); |
| 3080 | } else { |
| 3081 | flags |= MAP_PRIVATE | MAP_ANONYMOUS; |
| 3082 | area = mmap(vaddr, length, PROT_READ | PROT_WRITE, |
| 3083 | flags, -1, 0); |
| 3084 | } |
Jan Kiszka | fd28aa1 | 2011-03-15 12:26:14 +0100 | [diff] [blame] | 3085 | #else |
| 3086 | abort(); |
Huang Ying | cd19cfa | 2011-03-02 08:56:19 +0100 | [diff] [blame] | 3087 | #endif |
| 3088 | } else { |
| 3089 | #if defined(TARGET_S390X) && defined(CONFIG_KVM) |
| 3090 | flags |= MAP_SHARED | MAP_ANONYMOUS; |
| 3091 | area = mmap(vaddr, length, PROT_EXEC|PROT_READ|PROT_WRITE, |
| 3092 | flags, -1, 0); |
| 3093 | #else |
| 3094 | flags |= MAP_PRIVATE | MAP_ANONYMOUS; |
| 3095 | area = mmap(vaddr, length, PROT_READ | PROT_WRITE, |
| 3096 | flags, -1, 0); |
| 3097 | #endif |
| 3098 | } |
| 3099 | if (area != vaddr) { |
Anthony PERARD | f15fbc4 | 2011-07-20 08:17:42 +0000 | [diff] [blame] | 3100 | fprintf(stderr, "Could not remap addr: " |
| 3101 | RAM_ADDR_FMT "@" RAM_ADDR_FMT "\n", |
Huang Ying | cd19cfa | 2011-03-02 08:56:19 +0100 | [diff] [blame] | 3102 | length, addr); |
| 3103 | exit(1); |
| 3104 | } |
| 3105 | qemu_madvise(vaddr, length, QEMU_MADV_MERGEABLE); |
| 3106 | } |
| 3107 | return; |
| 3108 | } |
| 3109 | } |
| 3110 | } |
| 3111 | #endif /* !_WIN32 */ |
| 3112 | |
pbrook | dc828ca | 2009-04-09 22:21:07 +0000 | [diff] [blame] | 3113 | /* Return a host pointer to ram allocated with qemu_ram_alloc. |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3114 | With the exception of the softmmu code in this file, this should |
| 3115 | only be used for local memory (e.g. video ram) that the device owns, |
| 3116 | and knows it isn't going to access beyond the end of the block. |
| 3117 | |
| 3118 | It should not be used for general purpose DMA. |
| 3119 | Use cpu_physical_memory_map/cpu_physical_memory_rw instead. |
| 3120 | */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3121 | void *qemu_get_ram_ptr(ram_addr_t addr) |
pbrook | dc828ca | 2009-04-09 22:21:07 +0000 | [diff] [blame] | 3122 | { |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 3123 | RAMBlock *block; |
| 3124 | |
Alex Williamson | f471a17 | 2010-06-11 11:11:42 -0600 | [diff] [blame] | 3125 | QLIST_FOREACH(block, &ram_list.blocks, next) { |
| 3126 | if (addr - block->offset < block->length) { |
Vincent Palatin | 7d82af3 | 2011-03-10 15:47:46 -0500 | [diff] [blame] | 3127 | /* Move this entry to to start of the list. */ |
| 3128 | if (block != QLIST_FIRST(&ram_list.blocks)) { |
| 3129 | QLIST_REMOVE(block, next); |
| 3130 | QLIST_INSERT_HEAD(&ram_list.blocks, block, next); |
| 3131 | } |
Jan Kiszka | 868bb33 | 2011-06-21 22:59:09 +0200 | [diff] [blame] | 3132 | if (xen_enabled()) { |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 3133 | /* We need to check if the requested address is in the RAM |
| 3134 | * because we don't want to map the entire memory in QEMU. |
Stefano Stabellini | 712c2b4 | 2011-05-19 18:35:46 +0100 | [diff] [blame] | 3135 | * In that case just map until the end of the page. |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 3136 | */ |
| 3137 | if (block->offset == 0) { |
Jan Kiszka | e41d7c6 | 2011-06-21 22:59:08 +0200 | [diff] [blame] | 3138 | return xen_map_cache(addr, 0, 0); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 3139 | } else if (block->host == NULL) { |
Jan Kiszka | e41d7c6 | 2011-06-21 22:59:08 +0200 | [diff] [blame] | 3140 | block->host = |
| 3141 | xen_map_cache(block->offset, block->length, 1); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 3142 | } |
| 3143 | } |
Alex Williamson | f471a17 | 2010-06-11 11:11:42 -0600 | [diff] [blame] | 3144 | return block->host + (addr - block->offset); |
| 3145 | } |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 3146 | } |
Alex Williamson | f471a17 | 2010-06-11 11:11:42 -0600 | [diff] [blame] | 3147 | |
| 3148 | fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr); |
| 3149 | abort(); |
| 3150 | |
| 3151 | return NULL; |
pbrook | dc828ca | 2009-04-09 22:21:07 +0000 | [diff] [blame] | 3152 | } |
| 3153 | |
Michael S. Tsirkin | b2e0a13 | 2010-11-22 19:52:34 +0200 | [diff] [blame] | 3154 | /* Return a host pointer to ram allocated with qemu_ram_alloc. |
| 3155 | * Same as qemu_get_ram_ptr but avoid reordering ramblocks. |
| 3156 | */ |
| 3157 | void *qemu_safe_ram_ptr(ram_addr_t addr) |
| 3158 | { |
| 3159 | RAMBlock *block; |
| 3160 | |
| 3161 | QLIST_FOREACH(block, &ram_list.blocks, next) { |
| 3162 | if (addr - block->offset < block->length) { |
Jan Kiszka | 868bb33 | 2011-06-21 22:59:09 +0200 | [diff] [blame] | 3163 | if (xen_enabled()) { |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 3164 | /* We need to check if the requested address is in the RAM |
| 3165 | * because we don't want to map the entire memory in QEMU. |
Stefano Stabellini | 712c2b4 | 2011-05-19 18:35:46 +0100 | [diff] [blame] | 3166 | * In that case just map until the end of the page. |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 3167 | */ |
| 3168 | if (block->offset == 0) { |
Jan Kiszka | e41d7c6 | 2011-06-21 22:59:08 +0200 | [diff] [blame] | 3169 | return xen_map_cache(addr, 0, 0); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 3170 | } else if (block->host == NULL) { |
Jan Kiszka | e41d7c6 | 2011-06-21 22:59:08 +0200 | [diff] [blame] | 3171 | block->host = |
| 3172 | xen_map_cache(block->offset, block->length, 1); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 3173 | } |
| 3174 | } |
Michael S. Tsirkin | b2e0a13 | 2010-11-22 19:52:34 +0200 | [diff] [blame] | 3175 | return block->host + (addr - block->offset); |
| 3176 | } |
| 3177 | } |
| 3178 | |
| 3179 | fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr); |
| 3180 | abort(); |
| 3181 | |
| 3182 | return NULL; |
| 3183 | } |
| 3184 | |
Stefano Stabellini | 38bee5d | 2011-05-19 18:35:45 +0100 | [diff] [blame] | 3185 | /* Return a host pointer to guest's ram. Similar to qemu_get_ram_ptr |
| 3186 | * but takes a size argument */ |
Stefano Stabellini | 8ab934f | 2011-06-27 18:26:06 +0100 | [diff] [blame] | 3187 | void *qemu_ram_ptr_length(ram_addr_t addr, ram_addr_t *size) |
Stefano Stabellini | 38bee5d | 2011-05-19 18:35:45 +0100 | [diff] [blame] | 3188 | { |
Stefano Stabellini | 8ab934f | 2011-06-27 18:26:06 +0100 | [diff] [blame] | 3189 | if (*size == 0) { |
| 3190 | return NULL; |
| 3191 | } |
Jan Kiszka | 868bb33 | 2011-06-21 22:59:09 +0200 | [diff] [blame] | 3192 | if (xen_enabled()) { |
Jan Kiszka | e41d7c6 | 2011-06-21 22:59:08 +0200 | [diff] [blame] | 3193 | return xen_map_cache(addr, *size, 1); |
Jan Kiszka | 868bb33 | 2011-06-21 22:59:09 +0200 | [diff] [blame] | 3194 | } else { |
Stefano Stabellini | 38bee5d | 2011-05-19 18:35:45 +0100 | [diff] [blame] | 3195 | RAMBlock *block; |
| 3196 | |
| 3197 | QLIST_FOREACH(block, &ram_list.blocks, next) { |
| 3198 | if (addr - block->offset < block->length) { |
| 3199 | if (addr - block->offset + *size > block->length) |
| 3200 | *size = block->length - addr + block->offset; |
| 3201 | return block->host + (addr - block->offset); |
| 3202 | } |
| 3203 | } |
| 3204 | |
| 3205 | fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr); |
| 3206 | abort(); |
Stefano Stabellini | 38bee5d | 2011-05-19 18:35:45 +0100 | [diff] [blame] | 3207 | } |
| 3208 | } |
| 3209 | |
Anthony PERARD | 050a0dd | 2010-09-16 13:57:49 +0100 | [diff] [blame] | 3210 | void qemu_put_ram_ptr(void *addr) |
| 3211 | { |
| 3212 | trace_qemu_put_ram_ptr(addr); |
Anthony PERARD | 050a0dd | 2010-09-16 13:57:49 +0100 | [diff] [blame] | 3213 | } |
| 3214 | |
Marcelo Tosatti | e890261 | 2010-10-11 15:31:19 -0300 | [diff] [blame] | 3215 | int qemu_ram_addr_from_host(void *ptr, ram_addr_t *ram_addr) |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3216 | { |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 3217 | RAMBlock *block; |
| 3218 | uint8_t *host = ptr; |
| 3219 | |
Jan Kiszka | 868bb33 | 2011-06-21 22:59:09 +0200 | [diff] [blame] | 3220 | if (xen_enabled()) { |
Jan Kiszka | e41d7c6 | 2011-06-21 22:59:08 +0200 | [diff] [blame] | 3221 | *ram_addr = xen_ram_addr_from_mapcache(ptr); |
Stefano Stabellini | 712c2b4 | 2011-05-19 18:35:46 +0100 | [diff] [blame] | 3222 | return 0; |
| 3223 | } |
| 3224 | |
Alex Williamson | f471a17 | 2010-06-11 11:11:42 -0600 | [diff] [blame] | 3225 | QLIST_FOREACH(block, &ram_list.blocks, next) { |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 3226 | /* This case append when the block is not mapped. */ |
| 3227 | if (block->host == NULL) { |
| 3228 | continue; |
| 3229 | } |
Alex Williamson | f471a17 | 2010-06-11 11:11:42 -0600 | [diff] [blame] | 3230 | if (host - block->host < block->length) { |
Marcelo Tosatti | e890261 | 2010-10-11 15:31:19 -0300 | [diff] [blame] | 3231 | *ram_addr = block->offset + (host - block->host); |
| 3232 | return 0; |
Alex Williamson | f471a17 | 2010-06-11 11:11:42 -0600 | [diff] [blame] | 3233 | } |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 3234 | } |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 3235 | |
Marcelo Tosatti | e890261 | 2010-10-11 15:31:19 -0300 | [diff] [blame] | 3236 | return -1; |
| 3237 | } |
Alex Williamson | f471a17 | 2010-06-11 11:11:42 -0600 | [diff] [blame] | 3238 | |
Marcelo Tosatti | e890261 | 2010-10-11 15:31:19 -0300 | [diff] [blame] | 3239 | /* Some of the softmmu routines need to translate from a host pointer |
| 3240 | (typically a TLB entry) back to a ram offset. */ |
| 3241 | ram_addr_t qemu_ram_addr_from_host_nofail(void *ptr) |
| 3242 | { |
| 3243 | ram_addr_t ram_addr; |
Alex Williamson | f471a17 | 2010-06-11 11:11:42 -0600 | [diff] [blame] | 3244 | |
Marcelo Tosatti | e890261 | 2010-10-11 15:31:19 -0300 | [diff] [blame] | 3245 | if (qemu_ram_addr_from_host(ptr, &ram_addr)) { |
| 3246 | fprintf(stderr, "Bad ram pointer %p\n", ptr); |
| 3247 | abort(); |
| 3248 | } |
| 3249 | return ram_addr; |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3250 | } |
| 3251 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3252 | static uint32_t unassigned_mem_readb(void *opaque, target_phys_addr_t addr) |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 3253 | { |
pbrook | 67d3b95 | 2006-12-18 05:03:52 +0000 | [diff] [blame] | 3254 | #ifdef DEBUG_UNASSIGNED |
blueswir1 | ab3d172 | 2007-11-04 07:31:40 +0000 | [diff] [blame] | 3255 | printf("Unassigned mem read " TARGET_FMT_plx "\n", addr); |
pbrook | 67d3b95 | 2006-12-18 05:03:52 +0000 | [diff] [blame] | 3256 | #endif |
Richard Henderson | 5b45040 | 2011-04-18 16:13:12 -0700 | [diff] [blame] | 3257 | #if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE) |
Blue Swirl | b14ef7c | 2011-07-03 08:53:46 +0000 | [diff] [blame] | 3258 | cpu_unassigned_access(cpu_single_env, addr, 0, 0, 0, 1); |
blueswir1 | e18231a | 2008-10-06 18:46:28 +0000 | [diff] [blame] | 3259 | #endif |
| 3260 | return 0; |
| 3261 | } |
| 3262 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3263 | static uint32_t unassigned_mem_readw(void *opaque, target_phys_addr_t addr) |
blueswir1 | e18231a | 2008-10-06 18:46:28 +0000 | [diff] [blame] | 3264 | { |
| 3265 | #ifdef DEBUG_UNASSIGNED |
| 3266 | printf("Unassigned mem read " TARGET_FMT_plx "\n", addr); |
| 3267 | #endif |
Richard Henderson | 5b45040 | 2011-04-18 16:13:12 -0700 | [diff] [blame] | 3268 | #if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE) |
Blue Swirl | b14ef7c | 2011-07-03 08:53:46 +0000 | [diff] [blame] | 3269 | cpu_unassigned_access(cpu_single_env, addr, 0, 0, 0, 2); |
blueswir1 | e18231a | 2008-10-06 18:46:28 +0000 | [diff] [blame] | 3270 | #endif |
| 3271 | return 0; |
| 3272 | } |
| 3273 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3274 | static uint32_t unassigned_mem_readl(void *opaque, target_phys_addr_t addr) |
blueswir1 | e18231a | 2008-10-06 18:46:28 +0000 | [diff] [blame] | 3275 | { |
| 3276 | #ifdef DEBUG_UNASSIGNED |
| 3277 | printf("Unassigned mem read " TARGET_FMT_plx "\n", addr); |
| 3278 | #endif |
Richard Henderson | 5b45040 | 2011-04-18 16:13:12 -0700 | [diff] [blame] | 3279 | #if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE) |
Blue Swirl | b14ef7c | 2011-07-03 08:53:46 +0000 | [diff] [blame] | 3280 | cpu_unassigned_access(cpu_single_env, addr, 0, 0, 0, 4); |
blueswir1 | b4f0a31 | 2007-05-06 17:59:24 +0000 | [diff] [blame] | 3281 | #endif |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 3282 | return 0; |
| 3283 | } |
| 3284 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3285 | 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] | 3286 | { |
pbrook | 67d3b95 | 2006-12-18 05:03:52 +0000 | [diff] [blame] | 3287 | #ifdef DEBUG_UNASSIGNED |
blueswir1 | ab3d172 | 2007-11-04 07:31:40 +0000 | [diff] [blame] | 3288 | printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val); |
pbrook | 67d3b95 | 2006-12-18 05:03:52 +0000 | [diff] [blame] | 3289 | #endif |
Richard Henderson | 5b45040 | 2011-04-18 16:13:12 -0700 | [diff] [blame] | 3290 | #if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE) |
Blue Swirl | b14ef7c | 2011-07-03 08:53:46 +0000 | [diff] [blame] | 3291 | cpu_unassigned_access(cpu_single_env, addr, 1, 0, 0, 1); |
blueswir1 | e18231a | 2008-10-06 18:46:28 +0000 | [diff] [blame] | 3292 | #endif |
| 3293 | } |
| 3294 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3295 | 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] | 3296 | { |
| 3297 | #ifdef DEBUG_UNASSIGNED |
| 3298 | printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val); |
| 3299 | #endif |
Richard Henderson | 5b45040 | 2011-04-18 16:13:12 -0700 | [diff] [blame] | 3300 | #if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE) |
Blue Swirl | b14ef7c | 2011-07-03 08:53:46 +0000 | [diff] [blame] | 3301 | cpu_unassigned_access(cpu_single_env, addr, 1, 0, 0, 2); |
blueswir1 | e18231a | 2008-10-06 18:46:28 +0000 | [diff] [blame] | 3302 | #endif |
| 3303 | } |
| 3304 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3305 | 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] | 3306 | { |
| 3307 | #ifdef DEBUG_UNASSIGNED |
| 3308 | printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val); |
| 3309 | #endif |
Richard Henderson | 5b45040 | 2011-04-18 16:13:12 -0700 | [diff] [blame] | 3310 | #if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE) |
Blue Swirl | b14ef7c | 2011-07-03 08:53:46 +0000 | [diff] [blame] | 3311 | cpu_unassigned_access(cpu_single_env, addr, 1, 0, 0, 4); |
blueswir1 | b4f0a31 | 2007-05-06 17:59:24 +0000 | [diff] [blame] | 3312 | #endif |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 3313 | } |
| 3314 | |
Blue Swirl | d60efc6 | 2009-08-25 18:29:31 +0000 | [diff] [blame] | 3315 | static CPUReadMemoryFunc * const unassigned_mem_read[3] = { |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 3316 | unassigned_mem_readb, |
blueswir1 | e18231a | 2008-10-06 18:46:28 +0000 | [diff] [blame] | 3317 | unassigned_mem_readw, |
| 3318 | unassigned_mem_readl, |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 3319 | }; |
| 3320 | |
Blue Swirl | d60efc6 | 2009-08-25 18:29:31 +0000 | [diff] [blame] | 3321 | static CPUWriteMemoryFunc * const unassigned_mem_write[3] = { |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 3322 | unassigned_mem_writeb, |
blueswir1 | e18231a | 2008-10-06 18:46:28 +0000 | [diff] [blame] | 3323 | unassigned_mem_writew, |
| 3324 | unassigned_mem_writel, |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 3325 | }; |
| 3326 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3327 | static void notdirty_mem_writeb(void *opaque, target_phys_addr_t ram_addr, |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 3328 | uint32_t val) |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 3329 | { |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 3330 | int dirty_flags; |
Yoshiaki Tamura | f7c11b5 | 2010-03-23 16:39:53 +0900 | [diff] [blame] | 3331 | dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr); |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 3332 | if (!(dirty_flags & CODE_DIRTY_FLAG)) { |
| 3333 | #if !defined(CONFIG_USER_ONLY) |
| 3334 | tb_invalidate_phys_page_fast(ram_addr, 1); |
Yoshiaki Tamura | f7c11b5 | 2010-03-23 16:39:53 +0900 | [diff] [blame] | 3335 | dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr); |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 3336 | #endif |
| 3337 | } |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3338 | stb_p(qemu_get_ram_ptr(ram_addr), val); |
bellard | f23db16 | 2005-08-21 19:12:28 +0000 | [diff] [blame] | 3339 | dirty_flags |= (0xff & ~CODE_DIRTY_FLAG); |
Yoshiaki Tamura | f7c11b5 | 2010-03-23 16:39:53 +0900 | [diff] [blame] | 3340 | cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags); |
bellard | f23db16 | 2005-08-21 19:12:28 +0000 | [diff] [blame] | 3341 | /* we remove the notdirty callback only if the code has been |
| 3342 | flushed */ |
| 3343 | if (dirty_flags == 0xff) |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 3344 | tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr); |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 3345 | } |
| 3346 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3347 | static void notdirty_mem_writew(void *opaque, target_phys_addr_t ram_addr, |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 3348 | uint32_t val) |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 3349 | { |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 3350 | int dirty_flags; |
Yoshiaki Tamura | f7c11b5 | 2010-03-23 16:39:53 +0900 | [diff] [blame] | 3351 | dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr); |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 3352 | if (!(dirty_flags & CODE_DIRTY_FLAG)) { |
| 3353 | #if !defined(CONFIG_USER_ONLY) |
| 3354 | tb_invalidate_phys_page_fast(ram_addr, 2); |
Yoshiaki Tamura | f7c11b5 | 2010-03-23 16:39:53 +0900 | [diff] [blame] | 3355 | dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr); |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 3356 | #endif |
| 3357 | } |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3358 | stw_p(qemu_get_ram_ptr(ram_addr), val); |
bellard | f23db16 | 2005-08-21 19:12:28 +0000 | [diff] [blame] | 3359 | dirty_flags |= (0xff & ~CODE_DIRTY_FLAG); |
Yoshiaki Tamura | f7c11b5 | 2010-03-23 16:39:53 +0900 | [diff] [blame] | 3360 | cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags); |
bellard | f23db16 | 2005-08-21 19:12:28 +0000 | [diff] [blame] | 3361 | /* we remove the notdirty callback only if the code has been |
| 3362 | flushed */ |
| 3363 | if (dirty_flags == 0xff) |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 3364 | tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr); |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 3365 | } |
| 3366 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3367 | static void notdirty_mem_writel(void *opaque, target_phys_addr_t ram_addr, |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 3368 | uint32_t val) |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 3369 | { |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 3370 | int dirty_flags; |
Yoshiaki Tamura | f7c11b5 | 2010-03-23 16:39:53 +0900 | [diff] [blame] | 3371 | dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr); |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 3372 | if (!(dirty_flags & CODE_DIRTY_FLAG)) { |
| 3373 | #if !defined(CONFIG_USER_ONLY) |
| 3374 | tb_invalidate_phys_page_fast(ram_addr, 4); |
Yoshiaki Tamura | f7c11b5 | 2010-03-23 16:39:53 +0900 | [diff] [blame] | 3375 | dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr); |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 3376 | #endif |
| 3377 | } |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3378 | stl_p(qemu_get_ram_ptr(ram_addr), val); |
bellard | f23db16 | 2005-08-21 19:12:28 +0000 | [diff] [blame] | 3379 | dirty_flags |= (0xff & ~CODE_DIRTY_FLAG); |
Yoshiaki Tamura | f7c11b5 | 2010-03-23 16:39:53 +0900 | [diff] [blame] | 3380 | cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags); |
bellard | f23db16 | 2005-08-21 19:12:28 +0000 | [diff] [blame] | 3381 | /* we remove the notdirty callback only if the code has been |
| 3382 | flushed */ |
| 3383 | if (dirty_flags == 0xff) |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 3384 | tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr); |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 3385 | } |
| 3386 | |
Blue Swirl | d60efc6 | 2009-08-25 18:29:31 +0000 | [diff] [blame] | 3387 | static CPUReadMemoryFunc * const error_mem_read[3] = { |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 3388 | NULL, /* never used */ |
| 3389 | NULL, /* never used */ |
| 3390 | NULL, /* never used */ |
| 3391 | }; |
| 3392 | |
Blue Swirl | d60efc6 | 2009-08-25 18:29:31 +0000 | [diff] [blame] | 3393 | static CPUWriteMemoryFunc * const notdirty_mem_write[3] = { |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 3394 | notdirty_mem_writeb, |
| 3395 | notdirty_mem_writew, |
| 3396 | notdirty_mem_writel, |
| 3397 | }; |
| 3398 | |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 3399 | /* Generate a debug exception if a watchpoint has been hit. */ |
aliguori | b405133 | 2008-11-18 20:14:20 +0000 | [diff] [blame] | 3400 | static void check_watchpoint(int offset, int len_mask, int flags) |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 3401 | { |
| 3402 | CPUState *env = cpu_single_env; |
aliguori | 06d55cc | 2008-11-18 20:24:06 +0000 | [diff] [blame] | 3403 | target_ulong pc, cs_base; |
| 3404 | TranslationBlock *tb; |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 3405 | target_ulong vaddr; |
aliguori | a1d1bb3 | 2008-11-18 20:07:32 +0000 | [diff] [blame] | 3406 | CPUWatchpoint *wp; |
aliguori | 06d55cc | 2008-11-18 20:24:06 +0000 | [diff] [blame] | 3407 | int cpu_flags; |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 3408 | |
aliguori | 06d55cc | 2008-11-18 20:24:06 +0000 | [diff] [blame] | 3409 | if (env->watchpoint_hit) { |
| 3410 | /* We re-entered the check after replacing the TB. Now raise |
| 3411 | * the debug interrupt so that is will trigger after the |
| 3412 | * current instruction. */ |
| 3413 | cpu_interrupt(env, CPU_INTERRUPT_DEBUG); |
| 3414 | return; |
| 3415 | } |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 3416 | vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3417 | QTAILQ_FOREACH(wp, &env->watchpoints, entry) { |
aliguori | b405133 | 2008-11-18 20:14:20 +0000 | [diff] [blame] | 3418 | if ((vaddr == (wp->vaddr & len_mask) || |
| 3419 | (vaddr & wp->len_mask) == wp->vaddr) && (wp->flags & flags)) { |
aliguori | 6e140f2 | 2008-11-18 20:37:55 +0000 | [diff] [blame] | 3420 | wp->flags |= BP_WATCHPOINT_HIT; |
| 3421 | if (!env->watchpoint_hit) { |
| 3422 | env->watchpoint_hit = wp; |
| 3423 | tb = tb_find_pc(env->mem_io_pc); |
| 3424 | if (!tb) { |
| 3425 | cpu_abort(env, "check_watchpoint: could not find TB for " |
| 3426 | "pc=%p", (void *)env->mem_io_pc); |
| 3427 | } |
Stefan Weil | 618ba8e | 2011-04-18 06:39:53 +0000 | [diff] [blame] | 3428 | cpu_restore_state(tb, env, env->mem_io_pc); |
aliguori | 6e140f2 | 2008-11-18 20:37:55 +0000 | [diff] [blame] | 3429 | tb_phys_invalidate(tb, -1); |
| 3430 | if (wp->flags & BP_STOP_BEFORE_ACCESS) { |
| 3431 | env->exception_index = EXCP_DEBUG; |
| 3432 | } else { |
| 3433 | cpu_get_tb_cpu_state(env, &pc, &cs_base, &cpu_flags); |
| 3434 | tb_gen_code(env, pc, cs_base, cpu_flags, 1); |
| 3435 | } |
| 3436 | cpu_resume_from_signal(env, NULL); |
aliguori | 06d55cc | 2008-11-18 20:24:06 +0000 | [diff] [blame] | 3437 | } |
aliguori | 6e140f2 | 2008-11-18 20:37:55 +0000 | [diff] [blame] | 3438 | } else { |
| 3439 | wp->flags &= ~BP_WATCHPOINT_HIT; |
pbrook | 0f459d1 | 2008-06-09 00:20:13 +0000 | [diff] [blame] | 3440 | } |
| 3441 | } |
| 3442 | } |
| 3443 | |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 3444 | /* Watchpoint access routines. Watchpoints are inserted using TLB tricks, |
| 3445 | so these check for a hit then pass through to the normal out-of-line |
| 3446 | phys routines. */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3447 | static uint32_t watch_mem_readb(void *opaque, target_phys_addr_t addr) |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 3448 | { |
aliguori | b405133 | 2008-11-18 20:14:20 +0000 | [diff] [blame] | 3449 | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_READ); |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 3450 | return ldub_phys(addr); |
| 3451 | } |
| 3452 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3453 | static uint32_t watch_mem_readw(void *opaque, target_phys_addr_t addr) |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 3454 | { |
aliguori | b405133 | 2008-11-18 20:14:20 +0000 | [diff] [blame] | 3455 | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_READ); |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 3456 | return lduw_phys(addr); |
| 3457 | } |
| 3458 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3459 | static uint32_t watch_mem_readl(void *opaque, target_phys_addr_t addr) |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 3460 | { |
aliguori | b405133 | 2008-11-18 20:14:20 +0000 | [diff] [blame] | 3461 | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_READ); |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 3462 | return ldl_phys(addr); |
| 3463 | } |
| 3464 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3465 | static void watch_mem_writeb(void *opaque, target_phys_addr_t addr, |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 3466 | uint32_t val) |
| 3467 | { |
aliguori | b405133 | 2008-11-18 20:14:20 +0000 | [diff] [blame] | 3468 | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_WRITE); |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 3469 | stb_phys(addr, val); |
| 3470 | } |
| 3471 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3472 | static void watch_mem_writew(void *opaque, target_phys_addr_t addr, |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 3473 | uint32_t val) |
| 3474 | { |
aliguori | b405133 | 2008-11-18 20:14:20 +0000 | [diff] [blame] | 3475 | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_WRITE); |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 3476 | stw_phys(addr, val); |
| 3477 | } |
| 3478 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3479 | static void watch_mem_writel(void *opaque, target_phys_addr_t addr, |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 3480 | uint32_t val) |
| 3481 | { |
aliguori | b405133 | 2008-11-18 20:14:20 +0000 | [diff] [blame] | 3482 | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_WRITE); |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 3483 | stl_phys(addr, val); |
| 3484 | } |
| 3485 | |
Blue Swirl | d60efc6 | 2009-08-25 18:29:31 +0000 | [diff] [blame] | 3486 | static CPUReadMemoryFunc * const watch_mem_read[3] = { |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 3487 | watch_mem_readb, |
| 3488 | watch_mem_readw, |
| 3489 | watch_mem_readl, |
| 3490 | }; |
| 3491 | |
Blue Swirl | d60efc6 | 2009-08-25 18:29:31 +0000 | [diff] [blame] | 3492 | static CPUWriteMemoryFunc * const watch_mem_write[3] = { |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 3493 | watch_mem_writeb, |
| 3494 | watch_mem_writew, |
| 3495 | watch_mem_writel, |
| 3496 | }; |
pbrook | 6658ffb | 2007-03-16 23:58:11 +0000 | [diff] [blame] | 3497 | |
Richard Henderson | f640524 | 2010-04-22 16:47:31 -0700 | [diff] [blame] | 3498 | static inline uint32_t subpage_readlen (subpage_t *mmio, |
| 3499 | target_phys_addr_t addr, |
| 3500 | unsigned int len) |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3501 | { |
Richard Henderson | f640524 | 2010-04-22 16:47:31 -0700 | [diff] [blame] | 3502 | unsigned int idx = SUBPAGE_IDX(addr); |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3503 | #if defined(DEBUG_SUBPAGE) |
| 3504 | printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__, |
| 3505 | mmio, len, addr, idx); |
| 3506 | #endif |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3507 | |
Richard Henderson | f640524 | 2010-04-22 16:47:31 -0700 | [diff] [blame] | 3508 | addr += mmio->region_offset[idx]; |
| 3509 | idx = mmio->sub_io_index[idx]; |
| 3510 | return io_mem_read[idx][len](io_mem_opaque[idx], addr); |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3511 | } |
| 3512 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3513 | static inline void subpage_writelen (subpage_t *mmio, target_phys_addr_t addr, |
Richard Henderson | f640524 | 2010-04-22 16:47:31 -0700 | [diff] [blame] | 3514 | uint32_t value, unsigned int len) |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3515 | { |
Richard Henderson | f640524 | 2010-04-22 16:47:31 -0700 | [diff] [blame] | 3516 | unsigned int idx = SUBPAGE_IDX(addr); |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3517 | #if defined(DEBUG_SUBPAGE) |
Richard Henderson | f640524 | 2010-04-22 16:47:31 -0700 | [diff] [blame] | 3518 | printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d value %08x\n", |
| 3519 | __func__, mmio, len, addr, idx, value); |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3520 | #endif |
Richard Henderson | f640524 | 2010-04-22 16:47:31 -0700 | [diff] [blame] | 3521 | |
| 3522 | addr += mmio->region_offset[idx]; |
| 3523 | idx = mmio->sub_io_index[idx]; |
| 3524 | io_mem_write[idx][len](io_mem_opaque[idx], addr, value); |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3525 | } |
| 3526 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3527 | static uint32_t subpage_readb (void *opaque, target_phys_addr_t addr) |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3528 | { |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3529 | return subpage_readlen(opaque, addr, 0); |
| 3530 | } |
| 3531 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3532 | static void subpage_writeb (void *opaque, target_phys_addr_t addr, |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3533 | uint32_t value) |
| 3534 | { |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3535 | subpage_writelen(opaque, addr, value, 0); |
| 3536 | } |
| 3537 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3538 | static uint32_t subpage_readw (void *opaque, target_phys_addr_t addr) |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3539 | { |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3540 | return subpage_readlen(opaque, addr, 1); |
| 3541 | } |
| 3542 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3543 | static void subpage_writew (void *opaque, target_phys_addr_t addr, |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3544 | uint32_t value) |
| 3545 | { |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3546 | subpage_writelen(opaque, addr, value, 1); |
| 3547 | } |
| 3548 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3549 | static uint32_t subpage_readl (void *opaque, target_phys_addr_t addr) |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3550 | { |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3551 | return subpage_readlen(opaque, addr, 2); |
| 3552 | } |
| 3553 | |
Richard Henderson | f640524 | 2010-04-22 16:47:31 -0700 | [diff] [blame] | 3554 | static void subpage_writel (void *opaque, target_phys_addr_t addr, |
| 3555 | uint32_t value) |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3556 | { |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3557 | subpage_writelen(opaque, addr, value, 2); |
| 3558 | } |
| 3559 | |
Blue Swirl | d60efc6 | 2009-08-25 18:29:31 +0000 | [diff] [blame] | 3560 | static CPUReadMemoryFunc * const subpage_read[] = { |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3561 | &subpage_readb, |
| 3562 | &subpage_readw, |
| 3563 | &subpage_readl, |
| 3564 | }; |
| 3565 | |
Blue Swirl | d60efc6 | 2009-08-25 18:29:31 +0000 | [diff] [blame] | 3566 | static CPUWriteMemoryFunc * const subpage_write[] = { |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3567 | &subpage_writeb, |
| 3568 | &subpage_writew, |
| 3569 | &subpage_writel, |
| 3570 | }; |
| 3571 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3572 | static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end, |
| 3573 | ram_addr_t memory, ram_addr_t region_offset) |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3574 | { |
| 3575 | int idx, eidx; |
| 3576 | |
| 3577 | if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE) |
| 3578 | return -1; |
| 3579 | idx = SUBPAGE_IDX(start); |
| 3580 | eidx = SUBPAGE_IDX(end); |
| 3581 | #if defined(DEBUG_SUBPAGE) |
Blue Swirl | 0bf9e31 | 2009-07-20 17:19:25 +0000 | [diff] [blame] | 3582 | 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] | 3583 | mmio, start, end, idx, eidx, memory); |
| 3584 | #endif |
Gleb Natapov | 95c318f | 2010-07-29 10:41:45 +0300 | [diff] [blame] | 3585 | if ((memory & ~TARGET_PAGE_MASK) == IO_MEM_RAM) |
| 3586 | memory = IO_MEM_UNASSIGNED; |
Richard Henderson | f640524 | 2010-04-22 16:47:31 -0700 | [diff] [blame] | 3587 | memory = (memory >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3588 | for (; idx <= eidx; idx++) { |
Richard Henderson | f640524 | 2010-04-22 16:47:31 -0700 | [diff] [blame] | 3589 | mmio->sub_io_index[idx] = memory; |
| 3590 | mmio->region_offset[idx] = region_offset; |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3591 | } |
| 3592 | |
| 3593 | return 0; |
| 3594 | } |
| 3595 | |
Richard Henderson | f640524 | 2010-04-22 16:47:31 -0700 | [diff] [blame] | 3596 | static subpage_t *subpage_init (target_phys_addr_t base, ram_addr_t *phys, |
| 3597 | ram_addr_t orig_memory, |
| 3598 | ram_addr_t region_offset) |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3599 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3600 | subpage_t *mmio; |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3601 | int subpage_memory; |
| 3602 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3603 | mmio = qemu_mallocz(sizeof(subpage_t)); |
aliguori | 1eec614 | 2009-02-05 22:06:18 +0000 | [diff] [blame] | 3604 | |
| 3605 | mmio->base = base; |
Alexander Graf | 2507c12 | 2010-12-08 12:05:37 +0100 | [diff] [blame] | 3606 | subpage_memory = cpu_register_io_memory(subpage_read, subpage_write, mmio, |
| 3607 | DEVICE_NATIVE_ENDIAN); |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3608 | #if defined(DEBUG_SUBPAGE) |
aliguori | 1eec614 | 2009-02-05 22:06:18 +0000 | [diff] [blame] | 3609 | printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__, |
| 3610 | mmio, base, TARGET_PAGE_SIZE, subpage_memory); |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3611 | #endif |
aliguori | 1eec614 | 2009-02-05 22:06:18 +0000 | [diff] [blame] | 3612 | *phys = subpage_memory | IO_MEM_SUBPAGE; |
Richard Henderson | f640524 | 2010-04-22 16:47:31 -0700 | [diff] [blame] | 3613 | subpage_register(mmio, 0, TARGET_PAGE_SIZE-1, orig_memory, region_offset); |
blueswir1 | db7b542 | 2007-05-26 17:36:03 +0000 | [diff] [blame] | 3614 | |
| 3615 | return mmio; |
| 3616 | } |
| 3617 | |
aliguori | 8871565 | 2009-02-11 15:20:58 +0000 | [diff] [blame] | 3618 | static int get_free_io_mem_idx(void) |
| 3619 | { |
| 3620 | int i; |
| 3621 | |
| 3622 | for (i = 0; i<IO_MEM_NB_ENTRIES; i++) |
| 3623 | if (!io_mem_used[i]) { |
| 3624 | io_mem_used[i] = 1; |
| 3625 | return i; |
| 3626 | } |
Riku Voipio | c6703b4 | 2009-12-03 15:56:05 +0200 | [diff] [blame] | 3627 | 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] | 3628 | return -1; |
| 3629 | } |
| 3630 | |
Alexander Graf | dd31053 | 2010-12-08 12:05:36 +0100 | [diff] [blame] | 3631 | /* |
| 3632 | * Usually, devices operate in little endian mode. There are devices out |
| 3633 | * there that operate in big endian too. Each device gets byte swapped |
| 3634 | * mmio if plugged onto a CPU that does the other endianness. |
| 3635 | * |
| 3636 | * CPU Device swap? |
| 3637 | * |
| 3638 | * little little no |
| 3639 | * little big yes |
| 3640 | * big little yes |
| 3641 | * big big no |
| 3642 | */ |
| 3643 | |
| 3644 | typedef struct SwapEndianContainer { |
| 3645 | CPUReadMemoryFunc *read[3]; |
| 3646 | CPUWriteMemoryFunc *write[3]; |
| 3647 | void *opaque; |
| 3648 | } SwapEndianContainer; |
| 3649 | |
| 3650 | static uint32_t swapendian_mem_readb (void *opaque, target_phys_addr_t addr) |
| 3651 | { |
| 3652 | uint32_t val; |
| 3653 | SwapEndianContainer *c = opaque; |
| 3654 | val = c->read[0](c->opaque, addr); |
| 3655 | return val; |
| 3656 | } |
| 3657 | |
| 3658 | static uint32_t swapendian_mem_readw(void *opaque, target_phys_addr_t addr) |
| 3659 | { |
| 3660 | uint32_t val; |
| 3661 | SwapEndianContainer *c = opaque; |
| 3662 | val = bswap16(c->read[1](c->opaque, addr)); |
| 3663 | return val; |
| 3664 | } |
| 3665 | |
| 3666 | static uint32_t swapendian_mem_readl(void *opaque, target_phys_addr_t addr) |
| 3667 | { |
| 3668 | uint32_t val; |
| 3669 | SwapEndianContainer *c = opaque; |
| 3670 | val = bswap32(c->read[2](c->opaque, addr)); |
| 3671 | return val; |
| 3672 | } |
| 3673 | |
| 3674 | static CPUReadMemoryFunc * const swapendian_readfn[3]={ |
| 3675 | swapendian_mem_readb, |
| 3676 | swapendian_mem_readw, |
| 3677 | swapendian_mem_readl |
| 3678 | }; |
| 3679 | |
| 3680 | static void swapendian_mem_writeb(void *opaque, target_phys_addr_t addr, |
| 3681 | uint32_t val) |
| 3682 | { |
| 3683 | SwapEndianContainer *c = opaque; |
| 3684 | c->write[0](c->opaque, addr, val); |
| 3685 | } |
| 3686 | |
| 3687 | static void swapendian_mem_writew(void *opaque, target_phys_addr_t addr, |
| 3688 | uint32_t val) |
| 3689 | { |
| 3690 | SwapEndianContainer *c = opaque; |
| 3691 | c->write[1](c->opaque, addr, bswap16(val)); |
| 3692 | } |
| 3693 | |
| 3694 | static void swapendian_mem_writel(void *opaque, target_phys_addr_t addr, |
| 3695 | uint32_t val) |
| 3696 | { |
| 3697 | SwapEndianContainer *c = opaque; |
| 3698 | c->write[2](c->opaque, addr, bswap32(val)); |
| 3699 | } |
| 3700 | |
| 3701 | static CPUWriteMemoryFunc * const swapendian_writefn[3]={ |
| 3702 | swapendian_mem_writeb, |
| 3703 | swapendian_mem_writew, |
| 3704 | swapendian_mem_writel |
| 3705 | }; |
| 3706 | |
| 3707 | static void swapendian_init(int io_index) |
| 3708 | { |
| 3709 | SwapEndianContainer *c = qemu_malloc(sizeof(SwapEndianContainer)); |
| 3710 | int i; |
| 3711 | |
| 3712 | /* Swap mmio for big endian targets */ |
| 3713 | c->opaque = io_mem_opaque[io_index]; |
| 3714 | for (i = 0; i < 3; i++) { |
| 3715 | c->read[i] = io_mem_read[io_index][i]; |
| 3716 | c->write[i] = io_mem_write[io_index][i]; |
| 3717 | |
| 3718 | io_mem_read[io_index][i] = swapendian_readfn[i]; |
| 3719 | io_mem_write[io_index][i] = swapendian_writefn[i]; |
| 3720 | } |
| 3721 | io_mem_opaque[io_index] = c; |
| 3722 | } |
| 3723 | |
| 3724 | static void swapendian_del(int io_index) |
| 3725 | { |
| 3726 | if (io_mem_read[io_index][0] == swapendian_readfn[0]) { |
| 3727 | qemu_free(io_mem_opaque[io_index]); |
| 3728 | } |
| 3729 | } |
| 3730 | |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 3731 | /* mem_read and mem_write are arrays of functions containing the |
| 3732 | function to access byte (index 0), word (index 1) and dword (index |
Paul Brook | 0b4e6e3 | 2009-04-30 18:37:55 +0100 | [diff] [blame] | 3733 | 2). Functions can be omitted with a NULL function pointer. |
blueswir1 | 3ee8992 | 2008-01-02 19:45:26 +0000 | [diff] [blame] | 3734 | If io_index is non zero, the corresponding io zone is |
blueswir1 | 4254fab | 2008-01-01 16:57:19 +0000 | [diff] [blame] | 3735 | modified. If it is zero, a new io zone is allocated. The return |
| 3736 | value can be used with cpu_register_physical_memory(). (-1) is |
| 3737 | returned if error. */ |
Avi Kivity | 1eed09c | 2009-06-14 11:38:51 +0300 | [diff] [blame] | 3738 | static int cpu_register_io_memory_fixed(int io_index, |
Blue Swirl | d60efc6 | 2009-08-25 18:29:31 +0000 | [diff] [blame] | 3739 | CPUReadMemoryFunc * const *mem_read, |
| 3740 | CPUWriteMemoryFunc * const *mem_write, |
Alexander Graf | dd31053 | 2010-12-08 12:05:36 +0100 | [diff] [blame] | 3741 | void *opaque, enum device_endian endian) |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 3742 | { |
Richard Henderson | 3cab721 | 2010-05-07 09:52:51 -0700 | [diff] [blame] | 3743 | int i; |
| 3744 | |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 3745 | if (io_index <= 0) { |
aliguori | 8871565 | 2009-02-11 15:20:58 +0000 | [diff] [blame] | 3746 | io_index = get_free_io_mem_idx(); |
| 3747 | if (io_index == -1) |
| 3748 | return io_index; |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 3749 | } else { |
Avi Kivity | 1eed09c | 2009-06-14 11:38:51 +0300 | [diff] [blame] | 3750 | io_index >>= IO_MEM_SHIFT; |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 3751 | if (io_index >= IO_MEM_NB_ENTRIES) |
| 3752 | return -1; |
| 3753 | } |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 3754 | |
Richard Henderson | 3cab721 | 2010-05-07 09:52:51 -0700 | [diff] [blame] | 3755 | for (i = 0; i < 3; ++i) { |
| 3756 | io_mem_read[io_index][i] |
| 3757 | = (mem_read[i] ? mem_read[i] : unassigned_mem_read[i]); |
| 3758 | } |
| 3759 | for (i = 0; i < 3; ++i) { |
| 3760 | io_mem_write[io_index][i] |
| 3761 | = (mem_write[i] ? mem_write[i] : unassigned_mem_write[i]); |
| 3762 | } |
bellard | a4193c8 | 2004-06-03 14:01:43 +0000 | [diff] [blame] | 3763 | io_mem_opaque[io_index] = opaque; |
Richard Henderson | f640524 | 2010-04-22 16:47:31 -0700 | [diff] [blame] | 3764 | |
Alexander Graf | dd31053 | 2010-12-08 12:05:36 +0100 | [diff] [blame] | 3765 | switch (endian) { |
| 3766 | case DEVICE_BIG_ENDIAN: |
| 3767 | #ifndef TARGET_WORDS_BIGENDIAN |
| 3768 | swapendian_init(io_index); |
| 3769 | #endif |
| 3770 | break; |
| 3771 | case DEVICE_LITTLE_ENDIAN: |
| 3772 | #ifdef TARGET_WORDS_BIGENDIAN |
| 3773 | swapendian_init(io_index); |
| 3774 | #endif |
| 3775 | break; |
| 3776 | case DEVICE_NATIVE_ENDIAN: |
| 3777 | default: |
| 3778 | break; |
| 3779 | } |
| 3780 | |
Richard Henderson | f640524 | 2010-04-22 16:47:31 -0700 | [diff] [blame] | 3781 | return (io_index << IO_MEM_SHIFT); |
bellard | 33417e7 | 2003-08-10 21:47:01 +0000 | [diff] [blame] | 3782 | } |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 3783 | |
Blue Swirl | d60efc6 | 2009-08-25 18:29:31 +0000 | [diff] [blame] | 3784 | int cpu_register_io_memory(CPUReadMemoryFunc * const *mem_read, |
| 3785 | CPUWriteMemoryFunc * const *mem_write, |
Alexander Graf | dd31053 | 2010-12-08 12:05:36 +0100 | [diff] [blame] | 3786 | void *opaque, enum device_endian endian) |
Avi Kivity | 1eed09c | 2009-06-14 11:38:51 +0300 | [diff] [blame] | 3787 | { |
Alexander Graf | 2507c12 | 2010-12-08 12:05:37 +0100 | [diff] [blame] | 3788 | return cpu_register_io_memory_fixed(0, mem_read, mem_write, opaque, endian); |
Avi Kivity | 1eed09c | 2009-06-14 11:38:51 +0300 | [diff] [blame] | 3789 | } |
| 3790 | |
aliguori | 8871565 | 2009-02-11 15:20:58 +0000 | [diff] [blame] | 3791 | void cpu_unregister_io_memory(int io_table_address) |
| 3792 | { |
| 3793 | int i; |
| 3794 | int io_index = io_table_address >> IO_MEM_SHIFT; |
| 3795 | |
Alexander Graf | dd31053 | 2010-12-08 12:05:36 +0100 | [diff] [blame] | 3796 | swapendian_del(io_index); |
| 3797 | |
aliguori | 8871565 | 2009-02-11 15:20:58 +0000 | [diff] [blame] | 3798 | for (i=0;i < 3; i++) { |
| 3799 | io_mem_read[io_index][i] = unassigned_mem_read[i]; |
| 3800 | io_mem_write[io_index][i] = unassigned_mem_write[i]; |
| 3801 | } |
| 3802 | io_mem_opaque[io_index] = NULL; |
| 3803 | io_mem_used[io_index] = 0; |
| 3804 | } |
| 3805 | |
Avi Kivity | e9179ce | 2009-06-14 11:38:52 +0300 | [diff] [blame] | 3806 | static void io_mem_init(void) |
| 3807 | { |
| 3808 | int i; |
| 3809 | |
Alexander Graf | 2507c12 | 2010-12-08 12:05:37 +0100 | [diff] [blame] | 3810 | cpu_register_io_memory_fixed(IO_MEM_ROM, error_mem_read, |
| 3811 | unassigned_mem_write, NULL, |
| 3812 | DEVICE_NATIVE_ENDIAN); |
| 3813 | cpu_register_io_memory_fixed(IO_MEM_UNASSIGNED, unassigned_mem_read, |
| 3814 | unassigned_mem_write, NULL, |
| 3815 | DEVICE_NATIVE_ENDIAN); |
| 3816 | cpu_register_io_memory_fixed(IO_MEM_NOTDIRTY, error_mem_read, |
| 3817 | notdirty_mem_write, NULL, |
| 3818 | DEVICE_NATIVE_ENDIAN); |
Avi Kivity | e9179ce | 2009-06-14 11:38:52 +0300 | [diff] [blame] | 3819 | for (i=0; i<5; i++) |
| 3820 | io_mem_used[i] = 1; |
| 3821 | |
| 3822 | io_mem_watch = cpu_register_io_memory(watch_mem_read, |
Alexander Graf | 2507c12 | 2010-12-08 12:05:37 +0100 | [diff] [blame] | 3823 | watch_mem_write, NULL, |
| 3824 | DEVICE_NATIVE_ENDIAN); |
Avi Kivity | e9179ce | 2009-06-14 11:38:52 +0300 | [diff] [blame] | 3825 | } |
| 3826 | |
Avi Kivity | 62152b8 | 2011-07-26 14:26:14 +0300 | [diff] [blame] | 3827 | static void memory_map_init(void) |
| 3828 | { |
| 3829 | system_memory = qemu_malloc(sizeof(*system_memory)); |
Avi Kivity | 8417ceb | 2011-08-03 11:56:14 +0300 | [diff] [blame] | 3830 | memory_region_init(system_memory, "system", INT64_MAX); |
Avi Kivity | 62152b8 | 2011-07-26 14:26:14 +0300 | [diff] [blame] | 3831 | set_system_memory_map(system_memory); |
| 3832 | } |
| 3833 | |
| 3834 | MemoryRegion *get_system_memory(void) |
| 3835 | { |
| 3836 | return system_memory; |
| 3837 | } |
| 3838 | |
pbrook | e2eef17 | 2008-06-08 01:09:01 +0000 | [diff] [blame] | 3839 | #endif /* !defined(CONFIG_USER_ONLY) */ |
| 3840 | |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3841 | /* physical memory access (slow version, mainly for debug) */ |
| 3842 | #if defined(CONFIG_USER_ONLY) |
Paul Brook | a68fe89 | 2010-03-01 00:08:59 +0000 | [diff] [blame] | 3843 | int cpu_memory_rw_debug(CPUState *env, target_ulong addr, |
| 3844 | uint8_t *buf, int len, int is_write) |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3845 | { |
| 3846 | int l, flags; |
| 3847 | target_ulong page; |
pbrook | 53a5960 | 2006-03-25 19:31:22 +0000 | [diff] [blame] | 3848 | void * p; |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3849 | |
| 3850 | while (len > 0) { |
| 3851 | page = addr & TARGET_PAGE_MASK; |
| 3852 | l = (page + TARGET_PAGE_SIZE) - addr; |
| 3853 | if (l > len) |
| 3854 | l = len; |
| 3855 | flags = page_get_flags(page); |
| 3856 | if (!(flags & PAGE_VALID)) |
Paul Brook | a68fe89 | 2010-03-01 00:08:59 +0000 | [diff] [blame] | 3857 | return -1; |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3858 | if (is_write) { |
| 3859 | if (!(flags & PAGE_WRITE)) |
Paul Brook | a68fe89 | 2010-03-01 00:08:59 +0000 | [diff] [blame] | 3860 | return -1; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3861 | /* XXX: this code should not depend on lock_user */ |
aurel32 | 72fb7da | 2008-04-27 23:53:45 +0000 | [diff] [blame] | 3862 | if (!(p = lock_user(VERIFY_WRITE, addr, l, 0))) |
Paul Brook | a68fe89 | 2010-03-01 00:08:59 +0000 | [diff] [blame] | 3863 | return -1; |
aurel32 | 72fb7da | 2008-04-27 23:53:45 +0000 | [diff] [blame] | 3864 | memcpy(p, buf, l); |
| 3865 | unlock_user(p, addr, l); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3866 | } else { |
| 3867 | if (!(flags & PAGE_READ)) |
Paul Brook | a68fe89 | 2010-03-01 00:08:59 +0000 | [diff] [blame] | 3868 | return -1; |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 3869 | /* XXX: this code should not depend on lock_user */ |
aurel32 | 72fb7da | 2008-04-27 23:53:45 +0000 | [diff] [blame] | 3870 | if (!(p = lock_user(VERIFY_READ, addr, l, 1))) |
Paul Brook | a68fe89 | 2010-03-01 00:08:59 +0000 | [diff] [blame] | 3871 | return -1; |
aurel32 | 72fb7da | 2008-04-27 23:53:45 +0000 | [diff] [blame] | 3872 | memcpy(buf, p, l); |
aurel32 | 5b25757 | 2008-04-28 08:54:59 +0000 | [diff] [blame] | 3873 | unlock_user(p, addr, 0); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3874 | } |
| 3875 | len -= l; |
| 3876 | buf += l; |
| 3877 | addr += l; |
| 3878 | } |
Paul Brook | a68fe89 | 2010-03-01 00:08:59 +0000 | [diff] [blame] | 3879 | return 0; |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3880 | } |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 3881 | |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3882 | #else |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3883 | void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf, |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3884 | int len, int is_write) |
| 3885 | { |
| 3886 | int l, io_index; |
| 3887 | uint8_t *ptr; |
| 3888 | uint32_t val; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3889 | target_phys_addr_t page; |
Anthony PERARD | 8ca5692 | 2011-07-15 04:32:53 +0000 | [diff] [blame] | 3890 | ram_addr_t pd; |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 3891 | PhysPageDesc *p; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3892 | |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3893 | while (len > 0) { |
| 3894 | page = addr & TARGET_PAGE_MASK; |
| 3895 | l = (page + TARGET_PAGE_SIZE) - addr; |
| 3896 | if (l > len) |
| 3897 | l = len; |
bellard | 92e873b | 2004-05-21 14:52:29 +0000 | [diff] [blame] | 3898 | p = phys_page_find(page >> TARGET_PAGE_BITS); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3899 | if (!p) { |
| 3900 | pd = IO_MEM_UNASSIGNED; |
| 3901 | } else { |
| 3902 | pd = p->phys_offset; |
| 3903 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3904 | |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3905 | if (is_write) { |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 3906 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3907 | target_phys_addr_t addr1 = addr; |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3908 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 3909 | if (p) |
aurel32 | 6c2934d | 2009-02-18 21:37:17 +0000 | [diff] [blame] | 3910 | addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 3911 | /* XXX: could force cpu_single_env to NULL to avoid |
| 3912 | potential bugs */ |
aurel32 | 6c2934d | 2009-02-18 21:37:17 +0000 | [diff] [blame] | 3913 | if (l >= 4 && ((addr1 & 3) == 0)) { |
bellard | 1c213d1 | 2005-09-03 10:49:04 +0000 | [diff] [blame] | 3914 | /* 32 bit write access */ |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 3915 | val = ldl_p(buf); |
aurel32 | 6c2934d | 2009-02-18 21:37:17 +0000 | [diff] [blame] | 3916 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr1, val); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3917 | l = 4; |
aurel32 | 6c2934d | 2009-02-18 21:37:17 +0000 | [diff] [blame] | 3918 | } else if (l >= 2 && ((addr1 & 1) == 0)) { |
bellard | 1c213d1 | 2005-09-03 10:49:04 +0000 | [diff] [blame] | 3919 | /* 16 bit write access */ |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 3920 | val = lduw_p(buf); |
aurel32 | 6c2934d | 2009-02-18 21:37:17 +0000 | [diff] [blame] | 3921 | io_mem_write[io_index][1](io_mem_opaque[io_index], addr1, val); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3922 | l = 2; |
| 3923 | } else { |
bellard | 1c213d1 | 2005-09-03 10:49:04 +0000 | [diff] [blame] | 3924 | /* 8 bit write access */ |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 3925 | val = ldub_p(buf); |
aurel32 | 6c2934d | 2009-02-18 21:37:17 +0000 | [diff] [blame] | 3926 | io_mem_write[io_index][0](io_mem_opaque[io_index], addr1, val); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3927 | l = 1; |
| 3928 | } |
| 3929 | } else { |
Anthony PERARD | 8ca5692 | 2011-07-15 04:32:53 +0000 | [diff] [blame] | 3930 | ram_addr_t addr1; |
bellard | b448f2f | 2004-02-25 23:24:04 +0000 | [diff] [blame] | 3931 | addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3932 | /* RAM case */ |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3933 | ptr = qemu_get_ram_ptr(addr1); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3934 | memcpy(ptr, buf, l); |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 3935 | if (!cpu_physical_memory_is_dirty(addr1)) { |
| 3936 | /* invalidate code */ |
| 3937 | tb_invalidate_phys_page_range(addr1, addr1 + l, 0); |
| 3938 | /* set dirty bit */ |
Yoshiaki Tamura | f7c11b5 | 2010-03-23 16:39:53 +0900 | [diff] [blame] | 3939 | cpu_physical_memory_set_dirty_flags( |
| 3940 | addr1, (0xff & ~CODE_DIRTY_FLAG)); |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 3941 | } |
Anthony PERARD | 050a0dd | 2010-09-16 13:57:49 +0100 | [diff] [blame] | 3942 | qemu_put_ram_ptr(ptr); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3943 | } |
| 3944 | } else { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3945 | if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && |
bellard | 2a4188a | 2006-06-25 21:54:59 +0000 | [diff] [blame] | 3946 | !(pd & IO_MEM_ROMD)) { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3947 | target_phys_addr_t addr1 = addr; |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3948 | /* I/O case */ |
| 3949 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 3950 | if (p) |
aurel32 | 6c2934d | 2009-02-18 21:37:17 +0000 | [diff] [blame] | 3951 | addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset; |
| 3952 | if (l >= 4 && ((addr1 & 3) == 0)) { |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3953 | /* 32 bit read access */ |
aurel32 | 6c2934d | 2009-02-18 21:37:17 +0000 | [diff] [blame] | 3954 | val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr1); |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 3955 | stl_p(buf, val); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3956 | l = 4; |
aurel32 | 6c2934d | 2009-02-18 21:37:17 +0000 | [diff] [blame] | 3957 | } else if (l >= 2 && ((addr1 & 1) == 0)) { |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3958 | /* 16 bit read access */ |
aurel32 | 6c2934d | 2009-02-18 21:37:17 +0000 | [diff] [blame] | 3959 | val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr1); |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 3960 | stw_p(buf, val); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3961 | l = 2; |
| 3962 | } else { |
bellard | 1c213d1 | 2005-09-03 10:49:04 +0000 | [diff] [blame] | 3963 | /* 8 bit read access */ |
aurel32 | 6c2934d | 2009-02-18 21:37:17 +0000 | [diff] [blame] | 3964 | val = io_mem_read[io_index][0](io_mem_opaque[io_index], addr1); |
bellard | c27004e | 2005-01-03 23:35:10 +0000 | [diff] [blame] | 3965 | stb_p(buf, val); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3966 | l = 1; |
| 3967 | } |
| 3968 | } else { |
| 3969 | /* RAM case */ |
Anthony PERARD | 050a0dd | 2010-09-16 13:57:49 +0100 | [diff] [blame] | 3970 | ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK); |
| 3971 | memcpy(buf, ptr + (addr & ~TARGET_PAGE_MASK), l); |
| 3972 | qemu_put_ram_ptr(ptr); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 3973 | } |
| 3974 | } |
| 3975 | len -= l; |
| 3976 | buf += l; |
| 3977 | addr += l; |
| 3978 | } |
| 3979 | } |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 3980 | |
bellard | d0ecd2a | 2006-04-23 17:14:48 +0000 | [diff] [blame] | 3981 | /* used for ROM loading : can write in RAM and ROM */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3982 | void cpu_physical_memory_write_rom(target_phys_addr_t addr, |
bellard | d0ecd2a | 2006-04-23 17:14:48 +0000 | [diff] [blame] | 3983 | const uint8_t *buf, int len) |
| 3984 | { |
| 3985 | int l; |
| 3986 | uint8_t *ptr; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3987 | target_phys_addr_t page; |
bellard | d0ecd2a | 2006-04-23 17:14:48 +0000 | [diff] [blame] | 3988 | unsigned long pd; |
| 3989 | PhysPageDesc *p; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3990 | |
bellard | d0ecd2a | 2006-04-23 17:14:48 +0000 | [diff] [blame] | 3991 | while (len > 0) { |
| 3992 | page = addr & TARGET_PAGE_MASK; |
| 3993 | l = (page + TARGET_PAGE_SIZE) - addr; |
| 3994 | if (l > len) |
| 3995 | l = len; |
| 3996 | p = phys_page_find(page >> TARGET_PAGE_BITS); |
| 3997 | if (!p) { |
| 3998 | pd = IO_MEM_UNASSIGNED; |
| 3999 | } else { |
| 4000 | pd = p->phys_offset; |
| 4001 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4002 | |
bellard | d0ecd2a | 2006-04-23 17:14:48 +0000 | [diff] [blame] | 4003 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM && |
bellard | 2a4188a | 2006-06-25 21:54:59 +0000 | [diff] [blame] | 4004 | (pd & ~TARGET_PAGE_MASK) != IO_MEM_ROM && |
| 4005 | !(pd & IO_MEM_ROMD)) { |
bellard | d0ecd2a | 2006-04-23 17:14:48 +0000 | [diff] [blame] | 4006 | /* do nothing */ |
| 4007 | } else { |
| 4008 | unsigned long addr1; |
| 4009 | addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); |
| 4010 | /* ROM/RAM case */ |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 4011 | ptr = qemu_get_ram_ptr(addr1); |
bellard | d0ecd2a | 2006-04-23 17:14:48 +0000 | [diff] [blame] | 4012 | memcpy(ptr, buf, l); |
Anthony PERARD | 050a0dd | 2010-09-16 13:57:49 +0100 | [diff] [blame] | 4013 | qemu_put_ram_ptr(ptr); |
bellard | d0ecd2a | 2006-04-23 17:14:48 +0000 | [diff] [blame] | 4014 | } |
| 4015 | len -= l; |
| 4016 | buf += l; |
| 4017 | addr += l; |
| 4018 | } |
| 4019 | } |
| 4020 | |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 4021 | typedef struct { |
| 4022 | void *buffer; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 4023 | target_phys_addr_t addr; |
| 4024 | target_phys_addr_t len; |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 4025 | } BounceBuffer; |
| 4026 | |
| 4027 | static BounceBuffer bounce; |
| 4028 | |
aliguori | ba223c2 | 2009-01-22 16:59:16 +0000 | [diff] [blame] | 4029 | typedef struct MapClient { |
| 4030 | void *opaque; |
| 4031 | void (*callback)(void *opaque); |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 4032 | QLIST_ENTRY(MapClient) link; |
aliguori | ba223c2 | 2009-01-22 16:59:16 +0000 | [diff] [blame] | 4033 | } MapClient; |
| 4034 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 4035 | static QLIST_HEAD(map_client_list, MapClient) map_client_list |
| 4036 | = QLIST_HEAD_INITIALIZER(map_client_list); |
aliguori | ba223c2 | 2009-01-22 16:59:16 +0000 | [diff] [blame] | 4037 | |
| 4038 | void *cpu_register_map_client(void *opaque, void (*callback)(void *opaque)) |
| 4039 | { |
| 4040 | MapClient *client = qemu_malloc(sizeof(*client)); |
| 4041 | |
| 4042 | client->opaque = opaque; |
| 4043 | client->callback = callback; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 4044 | QLIST_INSERT_HEAD(&map_client_list, client, link); |
aliguori | ba223c2 | 2009-01-22 16:59:16 +0000 | [diff] [blame] | 4045 | return client; |
| 4046 | } |
| 4047 | |
| 4048 | void cpu_unregister_map_client(void *_client) |
| 4049 | { |
| 4050 | MapClient *client = (MapClient *)_client; |
| 4051 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 4052 | QLIST_REMOVE(client, link); |
Isaku Yamahata | 34d5e94 | 2009-06-26 18:57:18 +0900 | [diff] [blame] | 4053 | qemu_free(client); |
aliguori | ba223c2 | 2009-01-22 16:59:16 +0000 | [diff] [blame] | 4054 | } |
| 4055 | |
| 4056 | static void cpu_notify_map_clients(void) |
| 4057 | { |
| 4058 | MapClient *client; |
| 4059 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 4060 | while (!QLIST_EMPTY(&map_client_list)) { |
| 4061 | client = QLIST_FIRST(&map_client_list); |
aliguori | ba223c2 | 2009-01-22 16:59:16 +0000 | [diff] [blame] | 4062 | client->callback(client->opaque); |
Isaku Yamahata | 34d5e94 | 2009-06-26 18:57:18 +0900 | [diff] [blame] | 4063 | cpu_unregister_map_client(client); |
aliguori | ba223c2 | 2009-01-22 16:59:16 +0000 | [diff] [blame] | 4064 | } |
| 4065 | } |
| 4066 | |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 4067 | /* Map a physical memory region into a host virtual address. |
| 4068 | * May map a subset of the requested range, given by and returned in *plen. |
| 4069 | * May return NULL if resources needed to perform the mapping are exhausted. |
| 4070 | * Use only for reads OR writes - not for read-modify-write operations. |
aliguori | ba223c2 | 2009-01-22 16:59:16 +0000 | [diff] [blame] | 4071 | * Use cpu_register_map_client() to know when retrying the map operation is |
| 4072 | * likely to succeed. |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 4073 | */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 4074 | void *cpu_physical_memory_map(target_phys_addr_t addr, |
| 4075 | target_phys_addr_t *plen, |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 4076 | int is_write) |
| 4077 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 4078 | target_phys_addr_t len = *plen; |
Stefano Stabellini | 38bee5d | 2011-05-19 18:35:45 +0100 | [diff] [blame] | 4079 | target_phys_addr_t todo = 0; |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 4080 | int l; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 4081 | target_phys_addr_t page; |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 4082 | unsigned long pd; |
| 4083 | PhysPageDesc *p; |
Anthony PERARD | f15fbc4 | 2011-07-20 08:17:42 +0000 | [diff] [blame] | 4084 | ram_addr_t raddr = RAM_ADDR_MAX; |
Stefano Stabellini | 8ab934f | 2011-06-27 18:26:06 +0100 | [diff] [blame] | 4085 | ram_addr_t rlen; |
| 4086 | void *ret; |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 4087 | |
| 4088 | while (len > 0) { |
| 4089 | page = addr & TARGET_PAGE_MASK; |
| 4090 | l = (page + TARGET_PAGE_SIZE) - addr; |
| 4091 | if (l > len) |
| 4092 | l = len; |
| 4093 | p = phys_page_find(page >> TARGET_PAGE_BITS); |
| 4094 | if (!p) { |
| 4095 | pd = IO_MEM_UNASSIGNED; |
| 4096 | } else { |
| 4097 | pd = p->phys_offset; |
| 4098 | } |
| 4099 | |
| 4100 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) { |
Stefano Stabellini | 38bee5d | 2011-05-19 18:35:45 +0100 | [diff] [blame] | 4101 | if (todo || bounce.buffer) { |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 4102 | break; |
| 4103 | } |
| 4104 | bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, TARGET_PAGE_SIZE); |
| 4105 | bounce.addr = addr; |
| 4106 | bounce.len = l; |
| 4107 | if (!is_write) { |
Stefan Weil | 54f7b4a | 2011-04-10 18:23:39 +0200 | [diff] [blame] | 4108 | cpu_physical_memory_read(addr, bounce.buffer, l); |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 4109 | } |
Stefano Stabellini | 38bee5d | 2011-05-19 18:35:45 +0100 | [diff] [blame] | 4110 | |
| 4111 | *plen = l; |
| 4112 | return bounce.buffer; |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 4113 | } |
Stefano Stabellini | 8ab934f | 2011-06-27 18:26:06 +0100 | [diff] [blame] | 4114 | if (!todo) { |
| 4115 | raddr = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); |
| 4116 | } |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 4117 | |
| 4118 | len -= l; |
| 4119 | addr += l; |
Stefano Stabellini | 38bee5d | 2011-05-19 18:35:45 +0100 | [diff] [blame] | 4120 | todo += l; |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 4121 | } |
Stefano Stabellini | 8ab934f | 2011-06-27 18:26:06 +0100 | [diff] [blame] | 4122 | rlen = todo; |
| 4123 | ret = qemu_ram_ptr_length(raddr, &rlen); |
| 4124 | *plen = rlen; |
| 4125 | return ret; |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 4126 | } |
| 4127 | |
| 4128 | /* Unmaps a memory region previously mapped by cpu_physical_memory_map(). |
| 4129 | * Will also mark the memory as dirty if is_write == 1. access_len gives |
| 4130 | * the amount of memory that was actually read or written by the caller. |
| 4131 | */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 4132 | void cpu_physical_memory_unmap(void *buffer, target_phys_addr_t len, |
| 4133 | int is_write, target_phys_addr_t access_len) |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 4134 | { |
| 4135 | if (buffer != bounce.buffer) { |
| 4136 | if (is_write) { |
Marcelo Tosatti | e890261 | 2010-10-11 15:31:19 -0300 | [diff] [blame] | 4137 | ram_addr_t addr1 = qemu_ram_addr_from_host_nofail(buffer); |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 4138 | while (access_len) { |
| 4139 | unsigned l; |
| 4140 | l = TARGET_PAGE_SIZE; |
| 4141 | if (l > access_len) |
| 4142 | l = access_len; |
| 4143 | if (!cpu_physical_memory_is_dirty(addr1)) { |
| 4144 | /* invalidate code */ |
| 4145 | tb_invalidate_phys_page_range(addr1, addr1 + l, 0); |
| 4146 | /* set dirty bit */ |
Yoshiaki Tamura | f7c11b5 | 2010-03-23 16:39:53 +0900 | [diff] [blame] | 4147 | cpu_physical_memory_set_dirty_flags( |
| 4148 | addr1, (0xff & ~CODE_DIRTY_FLAG)); |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 4149 | } |
| 4150 | addr1 += l; |
| 4151 | access_len -= l; |
| 4152 | } |
| 4153 | } |
Jan Kiszka | 868bb33 | 2011-06-21 22:59:09 +0200 | [diff] [blame] | 4154 | if (xen_enabled()) { |
Jan Kiszka | e41d7c6 | 2011-06-21 22:59:08 +0200 | [diff] [blame] | 4155 | xen_invalidate_map_cache_entry(buffer); |
Anthony PERARD | 050a0dd | 2010-09-16 13:57:49 +0100 | [diff] [blame] | 4156 | } |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 4157 | return; |
| 4158 | } |
| 4159 | if (is_write) { |
| 4160 | cpu_physical_memory_write(bounce.addr, bounce.buffer, access_len); |
| 4161 | } |
Herve Poussineau | f8a8324 | 2010-01-24 21:23:56 +0000 | [diff] [blame] | 4162 | qemu_vfree(bounce.buffer); |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 4163 | bounce.buffer = NULL; |
aliguori | ba223c2 | 2009-01-22 16:59:16 +0000 | [diff] [blame] | 4164 | cpu_notify_map_clients(); |
aliguori | 6d16c2f | 2009-01-22 16:59:11 +0000 | [diff] [blame] | 4165 | } |
bellard | d0ecd2a | 2006-04-23 17:14:48 +0000 | [diff] [blame] | 4166 | |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 4167 | /* warning: addr must be aligned */ |
Alexander Graf | 1e78bcc | 2011-07-06 09:09:23 +0200 | [diff] [blame] | 4168 | static inline uint32_t ldl_phys_internal(target_phys_addr_t addr, |
| 4169 | enum device_endian endian) |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 4170 | { |
| 4171 | int io_index; |
| 4172 | uint8_t *ptr; |
| 4173 | uint32_t val; |
| 4174 | unsigned long pd; |
| 4175 | PhysPageDesc *p; |
| 4176 | |
| 4177 | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
| 4178 | if (!p) { |
| 4179 | pd = IO_MEM_UNASSIGNED; |
| 4180 | } else { |
| 4181 | pd = p->phys_offset; |
| 4182 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4183 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4184 | if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && |
bellard | 2a4188a | 2006-06-25 21:54:59 +0000 | [diff] [blame] | 4185 | !(pd & IO_MEM_ROMD)) { |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 4186 | /* I/O case */ |
| 4187 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 4188 | if (p) |
| 4189 | addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset; |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 4190 | val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr); |
Alexander Graf | 1e78bcc | 2011-07-06 09:09:23 +0200 | [diff] [blame] | 4191 | #if defined(TARGET_WORDS_BIGENDIAN) |
| 4192 | if (endian == DEVICE_LITTLE_ENDIAN) { |
| 4193 | val = bswap32(val); |
| 4194 | } |
| 4195 | #else |
| 4196 | if (endian == DEVICE_BIG_ENDIAN) { |
| 4197 | val = bswap32(val); |
| 4198 | } |
| 4199 | #endif |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 4200 | } else { |
| 4201 | /* RAM case */ |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 4202 | ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) + |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 4203 | (addr & ~TARGET_PAGE_MASK); |
Alexander Graf | 1e78bcc | 2011-07-06 09:09:23 +0200 | [diff] [blame] | 4204 | switch (endian) { |
| 4205 | case DEVICE_LITTLE_ENDIAN: |
| 4206 | val = ldl_le_p(ptr); |
| 4207 | break; |
| 4208 | case DEVICE_BIG_ENDIAN: |
| 4209 | val = ldl_be_p(ptr); |
| 4210 | break; |
| 4211 | default: |
| 4212 | val = ldl_p(ptr); |
| 4213 | break; |
| 4214 | } |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 4215 | } |
| 4216 | return val; |
| 4217 | } |
| 4218 | |
Alexander Graf | 1e78bcc | 2011-07-06 09:09:23 +0200 | [diff] [blame] | 4219 | uint32_t ldl_phys(target_phys_addr_t addr) |
| 4220 | { |
| 4221 | return ldl_phys_internal(addr, DEVICE_NATIVE_ENDIAN); |
| 4222 | } |
| 4223 | |
| 4224 | uint32_t ldl_le_phys(target_phys_addr_t addr) |
| 4225 | { |
| 4226 | return ldl_phys_internal(addr, DEVICE_LITTLE_ENDIAN); |
| 4227 | } |
| 4228 | |
| 4229 | uint32_t ldl_be_phys(target_phys_addr_t addr) |
| 4230 | { |
| 4231 | return ldl_phys_internal(addr, DEVICE_BIG_ENDIAN); |
| 4232 | } |
| 4233 | |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 4234 | /* warning: addr must be aligned */ |
Alexander Graf | 1e78bcc | 2011-07-06 09:09:23 +0200 | [diff] [blame] | 4235 | static inline uint64_t ldq_phys_internal(target_phys_addr_t addr, |
| 4236 | enum device_endian endian) |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 4237 | { |
| 4238 | int io_index; |
| 4239 | uint8_t *ptr; |
| 4240 | uint64_t val; |
| 4241 | unsigned long pd; |
| 4242 | PhysPageDesc *p; |
| 4243 | |
| 4244 | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
| 4245 | if (!p) { |
| 4246 | pd = IO_MEM_UNASSIGNED; |
| 4247 | } else { |
| 4248 | pd = p->phys_offset; |
| 4249 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4250 | |
bellard | 2a4188a | 2006-06-25 21:54:59 +0000 | [diff] [blame] | 4251 | if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && |
| 4252 | !(pd & IO_MEM_ROMD)) { |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 4253 | /* I/O case */ |
| 4254 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 4255 | if (p) |
| 4256 | addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset; |
Alexander Graf | 1e78bcc | 2011-07-06 09:09:23 +0200 | [diff] [blame] | 4257 | |
| 4258 | /* XXX This is broken when device endian != cpu endian. |
| 4259 | Fix and add "endian" variable check */ |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 4260 | #ifdef TARGET_WORDS_BIGENDIAN |
| 4261 | val = (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr) << 32; |
| 4262 | val |= io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4); |
| 4263 | #else |
| 4264 | val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr); |
| 4265 | val |= (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4) << 32; |
| 4266 | #endif |
| 4267 | } else { |
| 4268 | /* RAM case */ |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 4269 | ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) + |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 4270 | (addr & ~TARGET_PAGE_MASK); |
Alexander Graf | 1e78bcc | 2011-07-06 09:09:23 +0200 | [diff] [blame] | 4271 | switch (endian) { |
| 4272 | case DEVICE_LITTLE_ENDIAN: |
| 4273 | val = ldq_le_p(ptr); |
| 4274 | break; |
| 4275 | case DEVICE_BIG_ENDIAN: |
| 4276 | val = ldq_be_p(ptr); |
| 4277 | break; |
| 4278 | default: |
| 4279 | val = ldq_p(ptr); |
| 4280 | break; |
| 4281 | } |
bellard | 84b7b8e | 2005-11-28 21:19:04 +0000 | [diff] [blame] | 4282 | } |
| 4283 | return val; |
| 4284 | } |
| 4285 | |
Alexander Graf | 1e78bcc | 2011-07-06 09:09:23 +0200 | [diff] [blame] | 4286 | uint64_t ldq_phys(target_phys_addr_t addr) |
| 4287 | { |
| 4288 | return ldq_phys_internal(addr, DEVICE_NATIVE_ENDIAN); |
| 4289 | } |
| 4290 | |
| 4291 | uint64_t ldq_le_phys(target_phys_addr_t addr) |
| 4292 | { |
| 4293 | return ldq_phys_internal(addr, DEVICE_LITTLE_ENDIAN); |
| 4294 | } |
| 4295 | |
| 4296 | uint64_t ldq_be_phys(target_phys_addr_t addr) |
| 4297 | { |
| 4298 | return ldq_phys_internal(addr, DEVICE_BIG_ENDIAN); |
| 4299 | } |
| 4300 | |
bellard | aab3309 | 2005-10-30 20:48:42 +0000 | [diff] [blame] | 4301 | /* XXX: optimize */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 4302 | uint32_t ldub_phys(target_phys_addr_t addr) |
bellard | aab3309 | 2005-10-30 20:48:42 +0000 | [diff] [blame] | 4303 | { |
| 4304 | uint8_t val; |
| 4305 | cpu_physical_memory_read(addr, &val, 1); |
| 4306 | return val; |
| 4307 | } |
| 4308 | |
Michael S. Tsirkin | 733f0b0 | 2010-04-06 14:18:19 +0300 | [diff] [blame] | 4309 | /* warning: addr must be aligned */ |
Alexander Graf | 1e78bcc | 2011-07-06 09:09:23 +0200 | [diff] [blame] | 4310 | static inline uint32_t lduw_phys_internal(target_phys_addr_t addr, |
| 4311 | enum device_endian endian) |
bellard | aab3309 | 2005-10-30 20:48:42 +0000 | [diff] [blame] | 4312 | { |
Michael S. Tsirkin | 733f0b0 | 2010-04-06 14:18:19 +0300 | [diff] [blame] | 4313 | int io_index; |
| 4314 | uint8_t *ptr; |
| 4315 | uint64_t val; |
| 4316 | unsigned long pd; |
| 4317 | PhysPageDesc *p; |
| 4318 | |
| 4319 | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
| 4320 | if (!p) { |
| 4321 | pd = IO_MEM_UNASSIGNED; |
| 4322 | } else { |
| 4323 | pd = p->phys_offset; |
| 4324 | } |
| 4325 | |
| 4326 | if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && |
| 4327 | !(pd & IO_MEM_ROMD)) { |
| 4328 | /* I/O case */ |
| 4329 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
| 4330 | if (p) |
| 4331 | addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset; |
| 4332 | val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr); |
Alexander Graf | 1e78bcc | 2011-07-06 09:09:23 +0200 | [diff] [blame] | 4333 | #if defined(TARGET_WORDS_BIGENDIAN) |
| 4334 | if (endian == DEVICE_LITTLE_ENDIAN) { |
| 4335 | val = bswap16(val); |
| 4336 | } |
| 4337 | #else |
| 4338 | if (endian == DEVICE_BIG_ENDIAN) { |
| 4339 | val = bswap16(val); |
| 4340 | } |
| 4341 | #endif |
Michael S. Tsirkin | 733f0b0 | 2010-04-06 14:18:19 +0300 | [diff] [blame] | 4342 | } else { |
| 4343 | /* RAM case */ |
| 4344 | ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) + |
| 4345 | (addr & ~TARGET_PAGE_MASK); |
Alexander Graf | 1e78bcc | 2011-07-06 09:09:23 +0200 | [diff] [blame] | 4346 | switch (endian) { |
| 4347 | case DEVICE_LITTLE_ENDIAN: |
| 4348 | val = lduw_le_p(ptr); |
| 4349 | break; |
| 4350 | case DEVICE_BIG_ENDIAN: |
| 4351 | val = lduw_be_p(ptr); |
| 4352 | break; |
| 4353 | default: |
| 4354 | val = lduw_p(ptr); |
| 4355 | break; |
| 4356 | } |
Michael S. Tsirkin | 733f0b0 | 2010-04-06 14:18:19 +0300 | [diff] [blame] | 4357 | } |
| 4358 | return val; |
bellard | aab3309 | 2005-10-30 20:48:42 +0000 | [diff] [blame] | 4359 | } |
| 4360 | |
Alexander Graf | 1e78bcc | 2011-07-06 09:09:23 +0200 | [diff] [blame] | 4361 | uint32_t lduw_phys(target_phys_addr_t addr) |
| 4362 | { |
| 4363 | return lduw_phys_internal(addr, DEVICE_NATIVE_ENDIAN); |
| 4364 | } |
| 4365 | |
| 4366 | uint32_t lduw_le_phys(target_phys_addr_t addr) |
| 4367 | { |
| 4368 | return lduw_phys_internal(addr, DEVICE_LITTLE_ENDIAN); |
| 4369 | } |
| 4370 | |
| 4371 | uint32_t lduw_be_phys(target_phys_addr_t addr) |
| 4372 | { |
| 4373 | return lduw_phys_internal(addr, DEVICE_BIG_ENDIAN); |
| 4374 | } |
| 4375 | |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 4376 | /* warning: addr must be aligned. The ram page is not masked as dirty |
| 4377 | and the code inside is not invalidated. It is useful if the dirty |
| 4378 | bits are used to track modified PTEs */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 4379 | void stl_phys_notdirty(target_phys_addr_t addr, uint32_t val) |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 4380 | { |
| 4381 | int io_index; |
| 4382 | uint8_t *ptr; |
| 4383 | unsigned long pd; |
| 4384 | PhysPageDesc *p; |
| 4385 | |
| 4386 | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
| 4387 | if (!p) { |
| 4388 | pd = IO_MEM_UNASSIGNED; |
| 4389 | } else { |
| 4390 | pd = p->phys_offset; |
| 4391 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4392 | |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 4393 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) { |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 4394 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 4395 | if (p) |
| 4396 | addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset; |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 4397 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val); |
| 4398 | } else { |
aliguori | 7457619 | 2008-10-06 14:02:03 +0000 | [diff] [blame] | 4399 | unsigned long addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 4400 | ptr = qemu_get_ram_ptr(addr1); |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 4401 | stl_p(ptr, val); |
aliguori | 7457619 | 2008-10-06 14:02:03 +0000 | [diff] [blame] | 4402 | |
| 4403 | if (unlikely(in_migration)) { |
| 4404 | if (!cpu_physical_memory_is_dirty(addr1)) { |
| 4405 | /* invalidate code */ |
| 4406 | tb_invalidate_phys_page_range(addr1, addr1 + 4, 0); |
| 4407 | /* set dirty bit */ |
Yoshiaki Tamura | f7c11b5 | 2010-03-23 16:39:53 +0900 | [diff] [blame] | 4408 | cpu_physical_memory_set_dirty_flags( |
| 4409 | addr1, (0xff & ~CODE_DIRTY_FLAG)); |
aliguori | 7457619 | 2008-10-06 14:02:03 +0000 | [diff] [blame] | 4410 | } |
| 4411 | } |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 4412 | } |
| 4413 | } |
| 4414 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 4415 | void stq_phys_notdirty(target_phys_addr_t addr, uint64_t val) |
j_mayer | bc98a7e | 2007-04-04 07:55:12 +0000 | [diff] [blame] | 4416 | { |
| 4417 | int io_index; |
| 4418 | uint8_t *ptr; |
| 4419 | unsigned long pd; |
| 4420 | PhysPageDesc *p; |
| 4421 | |
| 4422 | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
| 4423 | if (!p) { |
| 4424 | pd = IO_MEM_UNASSIGNED; |
| 4425 | } else { |
| 4426 | pd = p->phys_offset; |
| 4427 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4428 | |
j_mayer | bc98a7e | 2007-04-04 07:55:12 +0000 | [diff] [blame] | 4429 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) { |
| 4430 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 4431 | if (p) |
| 4432 | addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset; |
j_mayer | bc98a7e | 2007-04-04 07:55:12 +0000 | [diff] [blame] | 4433 | #ifdef TARGET_WORDS_BIGENDIAN |
| 4434 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val >> 32); |
| 4435 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val); |
| 4436 | #else |
| 4437 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val); |
| 4438 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val >> 32); |
| 4439 | #endif |
| 4440 | } else { |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 4441 | ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) + |
j_mayer | bc98a7e | 2007-04-04 07:55:12 +0000 | [diff] [blame] | 4442 | (addr & ~TARGET_PAGE_MASK); |
| 4443 | stq_p(ptr, val); |
| 4444 | } |
| 4445 | } |
| 4446 | |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 4447 | /* warning: addr must be aligned */ |
Alexander Graf | 1e78bcc | 2011-07-06 09:09:23 +0200 | [diff] [blame] | 4448 | static inline void stl_phys_internal(target_phys_addr_t addr, uint32_t val, |
| 4449 | enum device_endian endian) |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 4450 | { |
| 4451 | int io_index; |
| 4452 | uint8_t *ptr; |
| 4453 | unsigned long pd; |
| 4454 | PhysPageDesc *p; |
| 4455 | |
| 4456 | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
| 4457 | if (!p) { |
| 4458 | pd = IO_MEM_UNASSIGNED; |
| 4459 | } else { |
| 4460 | pd = p->phys_offset; |
| 4461 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4462 | |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 4463 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) { |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 4464 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
pbrook | 8da3ff1 | 2008-12-01 18:59:50 +0000 | [diff] [blame] | 4465 | if (p) |
| 4466 | addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset; |
Alexander Graf | 1e78bcc | 2011-07-06 09:09:23 +0200 | [diff] [blame] | 4467 | #if defined(TARGET_WORDS_BIGENDIAN) |
| 4468 | if (endian == DEVICE_LITTLE_ENDIAN) { |
| 4469 | val = bswap32(val); |
| 4470 | } |
| 4471 | #else |
| 4472 | if (endian == DEVICE_BIG_ENDIAN) { |
| 4473 | val = bswap32(val); |
| 4474 | } |
| 4475 | #endif |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 4476 | io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val); |
| 4477 | } else { |
| 4478 | unsigned long addr1; |
| 4479 | addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); |
| 4480 | /* RAM case */ |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 4481 | ptr = qemu_get_ram_ptr(addr1); |
Alexander Graf | 1e78bcc | 2011-07-06 09:09:23 +0200 | [diff] [blame] | 4482 | switch (endian) { |
| 4483 | case DEVICE_LITTLE_ENDIAN: |
| 4484 | stl_le_p(ptr, val); |
| 4485 | break; |
| 4486 | case DEVICE_BIG_ENDIAN: |
| 4487 | stl_be_p(ptr, val); |
| 4488 | break; |
| 4489 | default: |
| 4490 | stl_p(ptr, val); |
| 4491 | break; |
| 4492 | } |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 4493 | if (!cpu_physical_memory_is_dirty(addr1)) { |
| 4494 | /* invalidate code */ |
| 4495 | tb_invalidate_phys_page_range(addr1, addr1 + 4, 0); |
| 4496 | /* set dirty bit */ |
Yoshiaki Tamura | f7c11b5 | 2010-03-23 16:39:53 +0900 | [diff] [blame] | 4497 | cpu_physical_memory_set_dirty_flags(addr1, |
| 4498 | (0xff & ~CODE_DIRTY_FLAG)); |
bellard | 3a7d929 | 2005-08-21 09:26:42 +0000 | [diff] [blame] | 4499 | } |
bellard | 8df1cd0 | 2005-01-28 22:37:22 +0000 | [diff] [blame] | 4500 | } |
| 4501 | } |
| 4502 | |
Alexander Graf | 1e78bcc | 2011-07-06 09:09:23 +0200 | [diff] [blame] | 4503 | void stl_phys(target_phys_addr_t addr, uint32_t val) |
| 4504 | { |
| 4505 | stl_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN); |
| 4506 | } |
| 4507 | |
| 4508 | void stl_le_phys(target_phys_addr_t addr, uint32_t val) |
| 4509 | { |
| 4510 | stl_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN); |
| 4511 | } |
| 4512 | |
| 4513 | void stl_be_phys(target_phys_addr_t addr, uint32_t val) |
| 4514 | { |
| 4515 | stl_phys_internal(addr, val, DEVICE_BIG_ENDIAN); |
| 4516 | } |
| 4517 | |
bellard | aab3309 | 2005-10-30 20:48:42 +0000 | [diff] [blame] | 4518 | /* XXX: optimize */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 4519 | void stb_phys(target_phys_addr_t addr, uint32_t val) |
bellard | aab3309 | 2005-10-30 20:48:42 +0000 | [diff] [blame] | 4520 | { |
| 4521 | uint8_t v = val; |
| 4522 | cpu_physical_memory_write(addr, &v, 1); |
| 4523 | } |
| 4524 | |
Michael S. Tsirkin | 733f0b0 | 2010-04-06 14:18:19 +0300 | [diff] [blame] | 4525 | /* warning: addr must be aligned */ |
Alexander Graf | 1e78bcc | 2011-07-06 09:09:23 +0200 | [diff] [blame] | 4526 | static inline void stw_phys_internal(target_phys_addr_t addr, uint32_t val, |
| 4527 | enum device_endian endian) |
bellard | aab3309 | 2005-10-30 20:48:42 +0000 | [diff] [blame] | 4528 | { |
Michael S. Tsirkin | 733f0b0 | 2010-04-06 14:18:19 +0300 | [diff] [blame] | 4529 | int io_index; |
| 4530 | uint8_t *ptr; |
| 4531 | unsigned long pd; |
| 4532 | PhysPageDesc *p; |
| 4533 | |
| 4534 | p = phys_page_find(addr >> TARGET_PAGE_BITS); |
| 4535 | if (!p) { |
| 4536 | pd = IO_MEM_UNASSIGNED; |
| 4537 | } else { |
| 4538 | pd = p->phys_offset; |
| 4539 | } |
| 4540 | |
| 4541 | if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) { |
| 4542 | io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1); |
| 4543 | if (p) |
| 4544 | addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset; |
Alexander Graf | 1e78bcc | 2011-07-06 09:09:23 +0200 | [diff] [blame] | 4545 | #if defined(TARGET_WORDS_BIGENDIAN) |
| 4546 | if (endian == DEVICE_LITTLE_ENDIAN) { |
| 4547 | val = bswap16(val); |
| 4548 | } |
| 4549 | #else |
| 4550 | if (endian == DEVICE_BIG_ENDIAN) { |
| 4551 | val = bswap16(val); |
| 4552 | } |
| 4553 | #endif |
Michael S. Tsirkin | 733f0b0 | 2010-04-06 14:18:19 +0300 | [diff] [blame] | 4554 | io_mem_write[io_index][1](io_mem_opaque[io_index], addr, val); |
| 4555 | } else { |
| 4556 | unsigned long addr1; |
| 4557 | addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK); |
| 4558 | /* RAM case */ |
| 4559 | ptr = qemu_get_ram_ptr(addr1); |
Alexander Graf | 1e78bcc | 2011-07-06 09:09:23 +0200 | [diff] [blame] | 4560 | switch (endian) { |
| 4561 | case DEVICE_LITTLE_ENDIAN: |
| 4562 | stw_le_p(ptr, val); |
| 4563 | break; |
| 4564 | case DEVICE_BIG_ENDIAN: |
| 4565 | stw_be_p(ptr, val); |
| 4566 | break; |
| 4567 | default: |
| 4568 | stw_p(ptr, val); |
| 4569 | break; |
| 4570 | } |
Michael S. Tsirkin | 733f0b0 | 2010-04-06 14:18:19 +0300 | [diff] [blame] | 4571 | if (!cpu_physical_memory_is_dirty(addr1)) { |
| 4572 | /* invalidate code */ |
| 4573 | tb_invalidate_phys_page_range(addr1, addr1 + 2, 0); |
| 4574 | /* set dirty bit */ |
| 4575 | cpu_physical_memory_set_dirty_flags(addr1, |
| 4576 | (0xff & ~CODE_DIRTY_FLAG)); |
| 4577 | } |
| 4578 | } |
bellard | aab3309 | 2005-10-30 20:48:42 +0000 | [diff] [blame] | 4579 | } |
| 4580 | |
Alexander Graf | 1e78bcc | 2011-07-06 09:09:23 +0200 | [diff] [blame] | 4581 | void stw_phys(target_phys_addr_t addr, uint32_t val) |
| 4582 | { |
| 4583 | stw_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN); |
| 4584 | } |
| 4585 | |
| 4586 | void stw_le_phys(target_phys_addr_t addr, uint32_t val) |
| 4587 | { |
| 4588 | stw_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN); |
| 4589 | } |
| 4590 | |
| 4591 | void stw_be_phys(target_phys_addr_t addr, uint32_t val) |
| 4592 | { |
| 4593 | stw_phys_internal(addr, val, DEVICE_BIG_ENDIAN); |
| 4594 | } |
| 4595 | |
bellard | aab3309 | 2005-10-30 20:48:42 +0000 | [diff] [blame] | 4596 | /* XXX: optimize */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 4597 | void stq_phys(target_phys_addr_t addr, uint64_t val) |
bellard | aab3309 | 2005-10-30 20:48:42 +0000 | [diff] [blame] | 4598 | { |
| 4599 | val = tswap64(val); |
Stefan Weil | 71d2b72 | 2011-03-26 21:06:56 +0100 | [diff] [blame] | 4600 | cpu_physical_memory_write(addr, &val, 8); |
bellard | aab3309 | 2005-10-30 20:48:42 +0000 | [diff] [blame] | 4601 | } |
| 4602 | |
Alexander Graf | 1e78bcc | 2011-07-06 09:09:23 +0200 | [diff] [blame] | 4603 | void stq_le_phys(target_phys_addr_t addr, uint64_t val) |
| 4604 | { |
| 4605 | val = cpu_to_le64(val); |
| 4606 | cpu_physical_memory_write(addr, &val, 8); |
| 4607 | } |
| 4608 | |
| 4609 | void stq_be_phys(target_phys_addr_t addr, uint64_t val) |
| 4610 | { |
| 4611 | val = cpu_to_be64(val); |
| 4612 | cpu_physical_memory_write(addr, &val, 8); |
| 4613 | } |
| 4614 | |
aliguori | 5e2972f | 2009-03-28 17:51:36 +0000 | [diff] [blame] | 4615 | /* virtual memory access for debug (includes writing to ROM) */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4616 | int cpu_memory_rw_debug(CPUState *env, target_ulong addr, |
bellard | b448f2f | 2004-02-25 23:24:04 +0000 | [diff] [blame] | 4617 | uint8_t *buf, int len, int is_write) |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 4618 | { |
| 4619 | int l; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 4620 | target_phys_addr_t phys_addr; |
j_mayer | 9b3c35e | 2007-04-07 11:21:28 +0000 | [diff] [blame] | 4621 | target_ulong page; |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 4622 | |
| 4623 | while (len > 0) { |
| 4624 | page = addr & TARGET_PAGE_MASK; |
| 4625 | phys_addr = cpu_get_phys_page_debug(env, page); |
| 4626 | /* if no physical page mapped, return an error */ |
| 4627 | if (phys_addr == -1) |
| 4628 | return -1; |
| 4629 | l = (page + TARGET_PAGE_SIZE) - addr; |
| 4630 | if (l > len) |
| 4631 | l = len; |
aliguori | 5e2972f | 2009-03-28 17:51:36 +0000 | [diff] [blame] | 4632 | phys_addr += (addr & ~TARGET_PAGE_MASK); |
aliguori | 5e2972f | 2009-03-28 17:51:36 +0000 | [diff] [blame] | 4633 | if (is_write) |
| 4634 | cpu_physical_memory_write_rom(phys_addr, buf, l); |
| 4635 | else |
aliguori | 5e2972f | 2009-03-28 17:51:36 +0000 | [diff] [blame] | 4636 | cpu_physical_memory_rw(phys_addr, buf, l, is_write); |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 4637 | len -= l; |
| 4638 | buf += l; |
| 4639 | addr += l; |
| 4640 | } |
| 4641 | return 0; |
| 4642 | } |
Paul Brook | a68fe89 | 2010-03-01 00:08:59 +0000 | [diff] [blame] | 4643 | #endif |
bellard | 13eb76e | 2004-01-24 15:23:36 +0000 | [diff] [blame] | 4644 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 4645 | /* in deterministic execution mode, instructions doing device I/Os |
| 4646 | must be at the end of the TB */ |
| 4647 | void cpu_io_recompile(CPUState *env, void *retaddr) |
| 4648 | { |
| 4649 | TranslationBlock *tb; |
| 4650 | uint32_t n, cflags; |
| 4651 | target_ulong pc, cs_base; |
| 4652 | uint64_t flags; |
| 4653 | |
| 4654 | tb = tb_find_pc((unsigned long)retaddr); |
| 4655 | if (!tb) { |
| 4656 | cpu_abort(env, "cpu_io_recompile: could not find TB for pc=%p", |
| 4657 | retaddr); |
| 4658 | } |
| 4659 | n = env->icount_decr.u16.low + tb->icount; |
Stefan Weil | 618ba8e | 2011-04-18 06:39:53 +0000 | [diff] [blame] | 4660 | cpu_restore_state(tb, env, (unsigned long)retaddr); |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 4661 | /* Calculate how many instructions had been executed before the fault |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 4662 | occurred. */ |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 4663 | n = n - env->icount_decr.u16.low; |
| 4664 | /* Generate a new TB ending on the I/O insn. */ |
| 4665 | n++; |
| 4666 | /* On MIPS and SH, delay slot instructions can only be restarted if |
| 4667 | they were already the first instruction in the TB. If this is not |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 4668 | the first instruction in a TB then re-execute the preceding |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 4669 | branch. */ |
| 4670 | #if defined(TARGET_MIPS) |
| 4671 | if ((env->hflags & MIPS_HFLAG_BMASK) != 0 && n > 1) { |
| 4672 | env->active_tc.PC -= 4; |
| 4673 | env->icount_decr.u16.low++; |
| 4674 | env->hflags &= ~MIPS_HFLAG_BMASK; |
| 4675 | } |
| 4676 | #elif defined(TARGET_SH4) |
| 4677 | if ((env->flags & ((DELAY_SLOT | DELAY_SLOT_CONDITIONAL))) != 0 |
| 4678 | && n > 1) { |
| 4679 | env->pc -= 2; |
| 4680 | env->icount_decr.u16.low++; |
| 4681 | env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL); |
| 4682 | } |
| 4683 | #endif |
| 4684 | /* This should never happen. */ |
| 4685 | if (n > CF_COUNT_MASK) |
| 4686 | cpu_abort(env, "TB too big during recompile"); |
| 4687 | |
| 4688 | cflags = n | CF_LAST_IO; |
| 4689 | pc = tb->pc; |
| 4690 | cs_base = tb->cs_base; |
| 4691 | flags = tb->flags; |
| 4692 | tb_phys_invalidate(tb, -1); |
| 4693 | /* FIXME: In theory this could raise an exception. In practice |
| 4694 | we have already translated the block once so it's probably ok. */ |
| 4695 | tb_gen_code(env, pc, cs_base, flags, cflags); |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 4696 | /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 4697 | the first in the TB) then we end up generating a whole new TB and |
| 4698 | repeating the fault, which is horribly inefficient. |
| 4699 | Better would be to execute just this insn uncached, or generate a |
| 4700 | second new TB. */ |
| 4701 | cpu_resume_from_signal(env, NULL); |
| 4702 | } |
| 4703 | |
Paul Brook | b3755a9 | 2010-03-12 16:54:58 +0000 | [diff] [blame] | 4704 | #if !defined(CONFIG_USER_ONLY) |
| 4705 | |
Stefan Weil | 055403b | 2010-10-22 23:03:32 +0200 | [diff] [blame] | 4706 | void dump_exec_info(FILE *f, fprintf_function cpu_fprintf) |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 4707 | { |
| 4708 | int i, target_code_size, max_target_code_size; |
| 4709 | int direct_jmp_count, direct_jmp2_count, cross_page; |
| 4710 | TranslationBlock *tb; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4711 | |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 4712 | target_code_size = 0; |
| 4713 | max_target_code_size = 0; |
| 4714 | cross_page = 0; |
| 4715 | direct_jmp_count = 0; |
| 4716 | direct_jmp2_count = 0; |
| 4717 | for(i = 0; i < nb_tbs; i++) { |
| 4718 | tb = &tbs[i]; |
| 4719 | target_code_size += tb->size; |
| 4720 | if (tb->size > max_target_code_size) |
| 4721 | max_target_code_size = tb->size; |
| 4722 | if (tb->page_addr[1] != -1) |
| 4723 | cross_page++; |
| 4724 | if (tb->tb_next_offset[0] != 0xffff) { |
| 4725 | direct_jmp_count++; |
| 4726 | if (tb->tb_next_offset[1] != 0xffff) { |
| 4727 | direct_jmp2_count++; |
| 4728 | } |
| 4729 | } |
| 4730 | } |
| 4731 | /* XXX: avoid using doubles ? */ |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 4732 | cpu_fprintf(f, "Translation buffer state:\n"); |
Stefan Weil | 055403b | 2010-10-22 23:03:32 +0200 | [diff] [blame] | 4733 | cpu_fprintf(f, "gen code size %td/%ld\n", |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 4734 | code_gen_ptr - code_gen_buffer, code_gen_buffer_max_size); |
| 4735 | cpu_fprintf(f, "TB count %d/%d\n", |
| 4736 | nb_tbs, code_gen_max_blocks); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4737 | cpu_fprintf(f, "TB avg target size %d max=%d bytes\n", |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 4738 | nb_tbs ? target_code_size / nb_tbs : 0, |
| 4739 | max_target_code_size); |
Stefan Weil | 055403b | 2010-10-22 23:03:32 +0200 | [diff] [blame] | 4740 | cpu_fprintf(f, "TB avg host size %td bytes (expansion ratio: %0.1f)\n", |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 4741 | nb_tbs ? (code_gen_ptr - code_gen_buffer) / nb_tbs : 0, |
| 4742 | 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] | 4743 | cpu_fprintf(f, "cross page TB count %d (%d%%)\n", |
| 4744 | cross_page, |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 4745 | nb_tbs ? (cross_page * 100) / nb_tbs : 0); |
| 4746 | cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n", |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4747 | direct_jmp_count, |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 4748 | nb_tbs ? (direct_jmp_count * 100) / nb_tbs : 0, |
| 4749 | direct_jmp2_count, |
| 4750 | nb_tbs ? (direct_jmp2_count * 100) / nb_tbs : 0); |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 4751 | cpu_fprintf(f, "\nStatistics:\n"); |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 4752 | cpu_fprintf(f, "TB flush count %d\n", tb_flush_count); |
| 4753 | cpu_fprintf(f, "TB invalidate count %d\n", tb_phys_invalidate_count); |
| 4754 | cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count); |
bellard | b67d9a5 | 2008-05-23 09:57:34 +0000 | [diff] [blame] | 4755 | tcg_dump_info(f, cpu_fprintf); |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 4756 | } |
| 4757 | |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 4758 | #define MMUSUFFIX _cmmu |
| 4759 | #define GETPC() NULL |
| 4760 | #define env cpu_single_env |
bellard | b769d8f | 2004-10-03 15:07:13 +0000 | [diff] [blame] | 4761 | #define SOFTMMU_CODE_ACCESS |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 4762 | |
| 4763 | #define SHIFT 0 |
| 4764 | #include "softmmu_template.h" |
| 4765 | |
| 4766 | #define SHIFT 1 |
| 4767 | #include "softmmu_template.h" |
| 4768 | |
| 4769 | #define SHIFT 2 |
| 4770 | #include "softmmu_template.h" |
| 4771 | |
| 4772 | #define SHIFT 3 |
| 4773 | #include "softmmu_template.h" |
| 4774 | |
| 4775 | #undef env |
| 4776 | |
| 4777 | #endif |