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