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