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