blob: ee0a6e3abf98b7279ecf137773e9947d6fba70c4 [file] [log] [blame]
bellard5a9fdfe2003-06-15 20:02:25 +00001/*
2 * defines common to all virtual CPUs
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard5a9fdfe2003-06-15 20:02:25 +00004 * 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
17 * License along with this library; if not, write to the Free Software
aurel32fad6cb12009-01-04 22:05:52 +000018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
bellard5a9fdfe2003-06-15 20:02:25 +000019 */
20#ifndef CPU_ALL_H
21#define CPU_ALL_H
22
blueswir17d99a002009-01-14 19:00:36 +000023#include "qemu-common.h"
24
aurel32f54b3f92008-04-12 20:14:54 +000025#if defined(__arm__) || defined(__sparc__) || defined(__mips__) || defined(__hppa__)
bellard0ac4bd52004-01-04 15:44:17 +000026#define WORDS_ALIGNED
27#endif
28
ths5fafdf22007-09-16 21:08:06 +000029/* some important defines:
30 *
bellard0ac4bd52004-01-04 15:44:17 +000031 * WORDS_ALIGNED : if defined, the host cpu can only make word aligned
32 * memory accesses.
ths5fafdf22007-09-16 21:08:06 +000033 *
bellard0ac4bd52004-01-04 15:44:17 +000034 * WORDS_BIGENDIAN : if defined, the host cpu is big endian and
35 * otherwise little endian.
ths5fafdf22007-09-16 21:08:06 +000036 *
bellard0ac4bd52004-01-04 15:44:17 +000037 * (TARGET_WORDS_ALIGNED : same for target cpu (not supported yet))
ths5fafdf22007-09-16 21:08:06 +000038 *
bellard0ac4bd52004-01-04 15:44:17 +000039 * TARGET_WORDS_BIGENDIAN : same for target cpu
40 */
41
bellardf193c792004-03-21 17:06:25 +000042#include "bswap.h"
aurel32939ef592008-05-09 18:45:47 +000043#include "softfloat.h"
bellardf193c792004-03-21 17:06:25 +000044
45#if defined(WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
46#define BSWAP_NEEDED
47#endif
48
49#ifdef BSWAP_NEEDED
50
51static inline uint16_t tswap16(uint16_t s)
52{
53 return bswap16(s);
54}
55
56static inline uint32_t tswap32(uint32_t s)
57{
58 return bswap32(s);
59}
60
61static inline uint64_t tswap64(uint64_t s)
62{
63 return bswap64(s);
64}
65
66static inline void tswap16s(uint16_t *s)
67{
68 *s = bswap16(*s);
69}
70
71static inline void tswap32s(uint32_t *s)
72{
73 *s = bswap32(*s);
74}
75
76static inline void tswap64s(uint64_t *s)
77{
78 *s = bswap64(*s);
79}
80
81#else
82
83static inline uint16_t tswap16(uint16_t s)
84{
85 return s;
86}
87
88static inline uint32_t tswap32(uint32_t s)
89{
90 return s;
91}
92
93static inline uint64_t tswap64(uint64_t s)
94{
95 return s;
96}
97
98static inline void tswap16s(uint16_t *s)
99{
100}
101
102static inline void tswap32s(uint32_t *s)
103{
104}
105
106static inline void tswap64s(uint64_t *s)
107{
108}
109
110#endif
111
112#if TARGET_LONG_SIZE == 4
113#define tswapl(s) tswap32(s)
114#define tswapls(s) tswap32s((uint32_t *)(s))
bellard0a962c02005-02-10 22:00:27 +0000115#define bswaptls(s) bswap32s(s)
bellardf193c792004-03-21 17:06:25 +0000116#else
117#define tswapl(s) tswap64(s)
118#define tswapls(s) tswap64s((uint64_t *)(s))
bellard0a962c02005-02-10 22:00:27 +0000119#define bswaptls(s) bswap64s(s)
bellardf193c792004-03-21 17:06:25 +0000120#endif
121
aurel320ca9d382008-03-13 19:19:16 +0000122typedef union {
123 float32 f;
124 uint32_t l;
125} CPU_FloatU;
126
bellard832ed0f2005-02-07 12:35:16 +0000127/* NOTE: arm FPA is horrible as double 32 bit words are stored in big
128 endian ! */
bellard0ac4bd52004-01-04 15:44:17 +0000129typedef union {
bellard53cd6632005-03-13 18:50:23 +0000130 float64 d;
bellard9d60cac2005-04-07 19:55:52 +0000131#if defined(WORDS_BIGENDIAN) \
132 || (defined(__arm__) && !defined(__VFP_FP__) && !defined(CONFIG_SOFTFLOAT))
bellard0ac4bd52004-01-04 15:44:17 +0000133 struct {
bellard0ac4bd52004-01-04 15:44:17 +0000134 uint32_t upper;
bellard832ed0f2005-02-07 12:35:16 +0000135 uint32_t lower;
bellard0ac4bd52004-01-04 15:44:17 +0000136 } l;
137#else
138 struct {
bellard0ac4bd52004-01-04 15:44:17 +0000139 uint32_t lower;
bellard832ed0f2005-02-07 12:35:16 +0000140 uint32_t upper;
bellard0ac4bd52004-01-04 15:44:17 +0000141 } l;
142#endif
143 uint64_t ll;
144} CPU_DoubleU;
145
blueswir11f587322007-11-25 18:40:20 +0000146#ifdef TARGET_SPARC
147typedef union {
148 float128 q;
149#if defined(WORDS_BIGENDIAN) \
150 || (defined(__arm__) && !defined(__VFP_FP__) && !defined(CONFIG_SOFTFLOAT))
151 struct {
152 uint32_t upmost;
153 uint32_t upper;
154 uint32_t lower;
155 uint32_t lowest;
156 } l;
157 struct {
158 uint64_t upper;
159 uint64_t lower;
160 } ll;
161#else
162 struct {
163 uint32_t lowest;
164 uint32_t lower;
165 uint32_t upper;
166 uint32_t upmost;
167 } l;
168 struct {
169 uint64_t lower;
170 uint64_t upper;
171 } ll;
172#endif
173} CPU_QuadU;
174#endif
175
bellard61382a52003-10-27 21:22:23 +0000176/* CPU memory access without any memory or io remapping */
177
bellard83d73962004-02-22 11:53:50 +0000178/*
179 * the generic syntax for the memory accesses is:
180 *
181 * load: ld{type}{sign}{size}{endian}_{access_type}(ptr)
182 *
183 * store: st{type}{size}{endian}_{access_type}(ptr, val)
184 *
185 * type is:
186 * (empty): integer access
187 * f : float access
ths5fafdf22007-09-16 21:08:06 +0000188 *
bellard83d73962004-02-22 11:53:50 +0000189 * sign is:
190 * (empty): for floats or 32 bit size
191 * u : unsigned
192 * s : signed
193 *
194 * size is:
195 * b: 8 bits
196 * w: 16 bits
197 * l: 32 bits
198 * q: 64 bits
ths5fafdf22007-09-16 21:08:06 +0000199 *
bellard83d73962004-02-22 11:53:50 +0000200 * endian is:
201 * (empty): target cpu endianness or 8 bit access
202 * r : reversed target cpu endianness (not implemented yet)
203 * be : big endian (not implemented yet)
204 * le : little endian (not implemented yet)
205 *
206 * access_type is:
207 * raw : host memory access
208 * user : user mode access using soft MMU
209 * kernel : kernel mode access using soft MMU
210 */
balrog8bba3ea2008-12-07 23:44:44 +0000211static inline int ldub_p(const void *ptr)
bellard5a9fdfe2003-06-15 20:02:25 +0000212{
213 return *(uint8_t *)ptr;
214}
215
balrog8bba3ea2008-12-07 23:44:44 +0000216static inline int ldsb_p(const void *ptr)
bellard5a9fdfe2003-06-15 20:02:25 +0000217{
218 return *(int8_t *)ptr;
219}
220
bellardc27004e2005-01-03 23:35:10 +0000221static inline void stb_p(void *ptr, int v)
bellard5a9fdfe2003-06-15 20:02:25 +0000222{
223 *(uint8_t *)ptr = v;
224}
225
226/* NOTE: on arm, putting 2 in /proc/sys/debug/alignment so that the
227 kernel handles unaligned load/stores may give better results, but
228 it is a system wide setting : bad */
bellard2df3b952005-11-19 17:47:39 +0000229#if defined(WORDS_BIGENDIAN) || defined(WORDS_ALIGNED)
bellard5a9fdfe2003-06-15 20:02:25 +0000230
231/* conservative code for little endian unaligned accesses */
balrog8bba3ea2008-12-07 23:44:44 +0000232static inline int lduw_le_p(const void *ptr)
bellard5a9fdfe2003-06-15 20:02:25 +0000233{
malce58ffeb2009-01-14 18:39:49 +0000234#ifdef _ARCH_PPC
bellard5a9fdfe2003-06-15 20:02:25 +0000235 int val;
236 __asm__ __volatile__ ("lhbrx %0,0,%1" : "=r" (val) : "r" (ptr));
237 return val;
238#else
malce01fe6d2008-12-11 00:14:30 +0000239 const uint8_t *p = ptr;
bellard5a9fdfe2003-06-15 20:02:25 +0000240 return p[0] | (p[1] << 8);
241#endif
242}
243
balrog8bba3ea2008-12-07 23:44:44 +0000244static inline int ldsw_le_p(const void *ptr)
bellard5a9fdfe2003-06-15 20:02:25 +0000245{
malce58ffeb2009-01-14 18:39:49 +0000246#ifdef _ARCH_PPC
bellard5a9fdfe2003-06-15 20:02:25 +0000247 int val;
248 __asm__ __volatile__ ("lhbrx %0,0,%1" : "=r" (val) : "r" (ptr));
249 return (int16_t)val;
250#else
malce01fe6d2008-12-11 00:14:30 +0000251 const uint8_t *p = ptr;
bellard5a9fdfe2003-06-15 20:02:25 +0000252 return (int16_t)(p[0] | (p[1] << 8));
253#endif
254}
255
balrog8bba3ea2008-12-07 23:44:44 +0000256static inline int ldl_le_p(const void *ptr)
bellard5a9fdfe2003-06-15 20:02:25 +0000257{
malce58ffeb2009-01-14 18:39:49 +0000258#ifdef _ARCH_PPC
bellard5a9fdfe2003-06-15 20:02:25 +0000259 int val;
260 __asm__ __volatile__ ("lwbrx %0,0,%1" : "=r" (val) : "r" (ptr));
261 return val;
262#else
malce01fe6d2008-12-11 00:14:30 +0000263 const uint8_t *p = ptr;
bellard5a9fdfe2003-06-15 20:02:25 +0000264 return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
265#endif
266}
267
balrog8bba3ea2008-12-07 23:44:44 +0000268static inline uint64_t ldq_le_p(const void *ptr)
bellard5a9fdfe2003-06-15 20:02:25 +0000269{
malce01fe6d2008-12-11 00:14:30 +0000270 const uint8_t *p = ptr;
bellard5a9fdfe2003-06-15 20:02:25 +0000271 uint32_t v1, v2;
bellardf0aca822005-11-21 23:22:06 +0000272 v1 = ldl_le_p(p);
273 v2 = ldl_le_p(p + 4);
bellard5a9fdfe2003-06-15 20:02:25 +0000274 return v1 | ((uint64_t)v2 << 32);
275}
276
bellard2df3b952005-11-19 17:47:39 +0000277static inline void stw_le_p(void *ptr, int v)
bellard5a9fdfe2003-06-15 20:02:25 +0000278{
malce58ffeb2009-01-14 18:39:49 +0000279#ifdef _ARCH_PPC
bellard5a9fdfe2003-06-15 20:02:25 +0000280 __asm__ __volatile__ ("sthbrx %1,0,%2" : "=m" (*(uint16_t *)ptr) : "r" (v), "r" (ptr));
281#else
282 uint8_t *p = ptr;
283 p[0] = v;
284 p[1] = v >> 8;
285#endif
286}
287
bellard2df3b952005-11-19 17:47:39 +0000288static inline void stl_le_p(void *ptr, int v)
bellard5a9fdfe2003-06-15 20:02:25 +0000289{
malce58ffeb2009-01-14 18:39:49 +0000290#ifdef _ARCH_PPC
bellard5a9fdfe2003-06-15 20:02:25 +0000291 __asm__ __volatile__ ("stwbrx %1,0,%2" : "=m" (*(uint32_t *)ptr) : "r" (v), "r" (ptr));
292#else
293 uint8_t *p = ptr;
294 p[0] = v;
295 p[1] = v >> 8;
296 p[2] = v >> 16;
297 p[3] = v >> 24;
298#endif
299}
300
bellard2df3b952005-11-19 17:47:39 +0000301static inline void stq_le_p(void *ptr, uint64_t v)
bellard5a9fdfe2003-06-15 20:02:25 +0000302{
303 uint8_t *p = ptr;
bellardf0aca822005-11-21 23:22:06 +0000304 stl_le_p(p, (uint32_t)v);
305 stl_le_p(p + 4, v >> 32);
bellard5a9fdfe2003-06-15 20:02:25 +0000306}
307
308/* float access */
309
balrog8bba3ea2008-12-07 23:44:44 +0000310static inline float32 ldfl_le_p(const void *ptr)
bellard5a9fdfe2003-06-15 20:02:25 +0000311{
312 union {
bellard53cd6632005-03-13 18:50:23 +0000313 float32 f;
bellard5a9fdfe2003-06-15 20:02:25 +0000314 uint32_t i;
315 } u;
bellard2df3b952005-11-19 17:47:39 +0000316 u.i = ldl_le_p(ptr);
bellard5a9fdfe2003-06-15 20:02:25 +0000317 return u.f;
318}
319
bellard2df3b952005-11-19 17:47:39 +0000320static inline void stfl_le_p(void *ptr, float32 v)
bellard5a9fdfe2003-06-15 20:02:25 +0000321{
322 union {
bellard53cd6632005-03-13 18:50:23 +0000323 float32 f;
bellard5a9fdfe2003-06-15 20:02:25 +0000324 uint32_t i;
325 } u;
326 u.f = v;
bellard2df3b952005-11-19 17:47:39 +0000327 stl_le_p(ptr, u.i);
bellard5a9fdfe2003-06-15 20:02:25 +0000328}
329
balrog8bba3ea2008-12-07 23:44:44 +0000330static inline float64 ldfq_le_p(const void *ptr)
bellard5a9fdfe2003-06-15 20:02:25 +0000331{
bellard0ac4bd52004-01-04 15:44:17 +0000332 CPU_DoubleU u;
bellard2df3b952005-11-19 17:47:39 +0000333 u.l.lower = ldl_le_p(ptr);
334 u.l.upper = ldl_le_p(ptr + 4);
bellard5a9fdfe2003-06-15 20:02:25 +0000335 return u.d;
336}
337
bellard2df3b952005-11-19 17:47:39 +0000338static inline void stfq_le_p(void *ptr, float64 v)
bellard5a9fdfe2003-06-15 20:02:25 +0000339{
bellard0ac4bd52004-01-04 15:44:17 +0000340 CPU_DoubleU u;
bellard5a9fdfe2003-06-15 20:02:25 +0000341 u.d = v;
bellard2df3b952005-11-19 17:47:39 +0000342 stl_le_p(ptr, u.l.lower);
343 stl_le_p(ptr + 4, u.l.upper);
bellard5a9fdfe2003-06-15 20:02:25 +0000344}
345
bellard2df3b952005-11-19 17:47:39 +0000346#else
bellard93ac68b2003-09-30 20:57:29 +0000347
balrog8bba3ea2008-12-07 23:44:44 +0000348static inline int lduw_le_p(const void *ptr)
bellard2df3b952005-11-19 17:47:39 +0000349{
350 return *(uint16_t *)ptr;
351}
352
balrog8bba3ea2008-12-07 23:44:44 +0000353static inline int ldsw_le_p(const void *ptr)
bellard2df3b952005-11-19 17:47:39 +0000354{
355 return *(int16_t *)ptr;
356}
357
balrog8bba3ea2008-12-07 23:44:44 +0000358static inline int ldl_le_p(const void *ptr)
bellard2df3b952005-11-19 17:47:39 +0000359{
360 return *(uint32_t *)ptr;
361}
362
balrog8bba3ea2008-12-07 23:44:44 +0000363static inline uint64_t ldq_le_p(const void *ptr)
bellard2df3b952005-11-19 17:47:39 +0000364{
365 return *(uint64_t *)ptr;
366}
367
368static inline void stw_le_p(void *ptr, int v)
369{
370 *(uint16_t *)ptr = v;
371}
372
373static inline void stl_le_p(void *ptr, int v)
374{
375 *(uint32_t *)ptr = v;
376}
377
378static inline void stq_le_p(void *ptr, uint64_t v)
379{
380 *(uint64_t *)ptr = v;
381}
382
383/* float access */
384
balrog8bba3ea2008-12-07 23:44:44 +0000385static inline float32 ldfl_le_p(const void *ptr)
bellard2df3b952005-11-19 17:47:39 +0000386{
387 return *(float32 *)ptr;
388}
389
balrog8bba3ea2008-12-07 23:44:44 +0000390static inline float64 ldfq_le_p(const void *ptr)
bellard2df3b952005-11-19 17:47:39 +0000391{
392 return *(float64 *)ptr;
393}
394
395static inline void stfl_le_p(void *ptr, float32 v)
396{
397 *(float32 *)ptr = v;
398}
399
400static inline void stfq_le_p(void *ptr, float64 v)
401{
402 *(float64 *)ptr = v;
403}
404#endif
405
406#if !defined(WORDS_BIGENDIAN) || defined(WORDS_ALIGNED)
407
balrog8bba3ea2008-12-07 23:44:44 +0000408static inline int lduw_be_p(const void *ptr)
bellard93ac68b2003-09-30 20:57:29 +0000409{
bellard83d73962004-02-22 11:53:50 +0000410#if defined(__i386__)
411 int val;
412 asm volatile ("movzwl %1, %0\n"
413 "xchgb %b0, %h0\n"
414 : "=q" (val)
415 : "m" (*(uint16_t *)ptr));
416 return val;
417#else
malce01fe6d2008-12-11 00:14:30 +0000418 const uint8_t *b = ptr;
bellard83d73962004-02-22 11:53:50 +0000419 return ((b[0] << 8) | b[1]);
420#endif
bellard93ac68b2003-09-30 20:57:29 +0000421}
422
balrog8bba3ea2008-12-07 23:44:44 +0000423static inline int ldsw_be_p(const void *ptr)
bellard93ac68b2003-09-30 20:57:29 +0000424{
bellard83d73962004-02-22 11:53:50 +0000425#if defined(__i386__)
426 int val;
427 asm volatile ("movzwl %1, %0\n"
428 "xchgb %b0, %h0\n"
429 : "=q" (val)
430 : "m" (*(uint16_t *)ptr));
431 return (int16_t)val;
432#else
malce01fe6d2008-12-11 00:14:30 +0000433 const uint8_t *b = ptr;
bellard83d73962004-02-22 11:53:50 +0000434 return (int16_t)((b[0] << 8) | b[1]);
435#endif
bellard93ac68b2003-09-30 20:57:29 +0000436}
437
balrog8bba3ea2008-12-07 23:44:44 +0000438static inline int ldl_be_p(const void *ptr)
bellard93ac68b2003-09-30 20:57:29 +0000439{
bellard4f2ac232004-04-26 19:44:02 +0000440#if defined(__i386__) || defined(__x86_64__)
bellard83d73962004-02-22 11:53:50 +0000441 int val;
442 asm volatile ("movl %1, %0\n"
443 "bswap %0\n"
444 : "=r" (val)
445 : "m" (*(uint32_t *)ptr));
446 return val;
447#else
malce01fe6d2008-12-11 00:14:30 +0000448 const uint8_t *b = ptr;
bellard83d73962004-02-22 11:53:50 +0000449 return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3];
450#endif
bellard93ac68b2003-09-30 20:57:29 +0000451}
452
balrog8bba3ea2008-12-07 23:44:44 +0000453static inline uint64_t ldq_be_p(const void *ptr)
bellard93ac68b2003-09-30 20:57:29 +0000454{
455 uint32_t a,b;
bellard2df3b952005-11-19 17:47:39 +0000456 a = ldl_be_p(ptr);
blueswir14d7a0882008-05-10 10:14:22 +0000457 b = ldl_be_p((uint8_t *)ptr + 4);
bellard93ac68b2003-09-30 20:57:29 +0000458 return (((uint64_t)a<<32)|b);
459}
460
bellard2df3b952005-11-19 17:47:39 +0000461static inline void stw_be_p(void *ptr, int v)
bellard93ac68b2003-09-30 20:57:29 +0000462{
bellard83d73962004-02-22 11:53:50 +0000463#if defined(__i386__)
464 asm volatile ("xchgb %b0, %h0\n"
465 "movw %w0, %1\n"
466 : "=q" (v)
467 : "m" (*(uint16_t *)ptr), "0" (v));
468#else
bellard93ac68b2003-09-30 20:57:29 +0000469 uint8_t *d = (uint8_t *) ptr;
470 d[0] = v >> 8;
471 d[1] = v;
bellard83d73962004-02-22 11:53:50 +0000472#endif
bellard93ac68b2003-09-30 20:57:29 +0000473}
474
bellard2df3b952005-11-19 17:47:39 +0000475static inline void stl_be_p(void *ptr, int v)
bellard93ac68b2003-09-30 20:57:29 +0000476{
bellard4f2ac232004-04-26 19:44:02 +0000477#if defined(__i386__) || defined(__x86_64__)
bellard83d73962004-02-22 11:53:50 +0000478 asm volatile ("bswap %0\n"
479 "movl %0, %1\n"
480 : "=r" (v)
481 : "m" (*(uint32_t *)ptr), "0" (v));
482#else
bellard93ac68b2003-09-30 20:57:29 +0000483 uint8_t *d = (uint8_t *) ptr;
484 d[0] = v >> 24;
485 d[1] = v >> 16;
486 d[2] = v >> 8;
487 d[3] = v;
bellard83d73962004-02-22 11:53:50 +0000488#endif
bellard93ac68b2003-09-30 20:57:29 +0000489}
490
bellard2df3b952005-11-19 17:47:39 +0000491static inline void stq_be_p(void *ptr, uint64_t v)
bellard93ac68b2003-09-30 20:57:29 +0000492{
bellard2df3b952005-11-19 17:47:39 +0000493 stl_be_p(ptr, v >> 32);
blueswir14d7a0882008-05-10 10:14:22 +0000494 stl_be_p((uint8_t *)ptr + 4, v);
bellard0ac4bd52004-01-04 15:44:17 +0000495}
496
497/* float access */
498
balrog8bba3ea2008-12-07 23:44:44 +0000499static inline float32 ldfl_be_p(const void *ptr)
bellard0ac4bd52004-01-04 15:44:17 +0000500{
501 union {
bellard53cd6632005-03-13 18:50:23 +0000502 float32 f;
bellard0ac4bd52004-01-04 15:44:17 +0000503 uint32_t i;
504 } u;
bellard2df3b952005-11-19 17:47:39 +0000505 u.i = ldl_be_p(ptr);
bellard0ac4bd52004-01-04 15:44:17 +0000506 return u.f;
507}
508
bellard2df3b952005-11-19 17:47:39 +0000509static inline void stfl_be_p(void *ptr, float32 v)
bellard0ac4bd52004-01-04 15:44:17 +0000510{
511 union {
bellard53cd6632005-03-13 18:50:23 +0000512 float32 f;
bellard0ac4bd52004-01-04 15:44:17 +0000513 uint32_t i;
514 } u;
515 u.f = v;
bellard2df3b952005-11-19 17:47:39 +0000516 stl_be_p(ptr, u.i);
bellard0ac4bd52004-01-04 15:44:17 +0000517}
518
balrog8bba3ea2008-12-07 23:44:44 +0000519static inline float64 ldfq_be_p(const void *ptr)
bellard0ac4bd52004-01-04 15:44:17 +0000520{
521 CPU_DoubleU u;
bellard2df3b952005-11-19 17:47:39 +0000522 u.l.upper = ldl_be_p(ptr);
blueswir14d7a0882008-05-10 10:14:22 +0000523 u.l.lower = ldl_be_p((uint8_t *)ptr + 4);
bellard0ac4bd52004-01-04 15:44:17 +0000524 return u.d;
525}
526
bellard2df3b952005-11-19 17:47:39 +0000527static inline void stfq_be_p(void *ptr, float64 v)
bellard0ac4bd52004-01-04 15:44:17 +0000528{
529 CPU_DoubleU u;
530 u.d = v;
bellard2df3b952005-11-19 17:47:39 +0000531 stl_be_p(ptr, u.l.upper);
blueswir14d7a0882008-05-10 10:14:22 +0000532 stl_be_p((uint8_t *)ptr + 4, u.l.lower);
bellard93ac68b2003-09-30 20:57:29 +0000533}
534
bellard5a9fdfe2003-06-15 20:02:25 +0000535#else
536
balrog8bba3ea2008-12-07 23:44:44 +0000537static inline int lduw_be_p(const void *ptr)
bellard5a9fdfe2003-06-15 20:02:25 +0000538{
539 return *(uint16_t *)ptr;
540}
541
balrog8bba3ea2008-12-07 23:44:44 +0000542static inline int ldsw_be_p(const void *ptr)
bellard5a9fdfe2003-06-15 20:02:25 +0000543{
544 return *(int16_t *)ptr;
545}
546
balrog8bba3ea2008-12-07 23:44:44 +0000547static inline int ldl_be_p(const void *ptr)
bellard5a9fdfe2003-06-15 20:02:25 +0000548{
549 return *(uint32_t *)ptr;
550}
551
balrog8bba3ea2008-12-07 23:44:44 +0000552static inline uint64_t ldq_be_p(const void *ptr)
bellard5a9fdfe2003-06-15 20:02:25 +0000553{
554 return *(uint64_t *)ptr;
555}
556
bellard2df3b952005-11-19 17:47:39 +0000557static inline void stw_be_p(void *ptr, int v)
bellard5a9fdfe2003-06-15 20:02:25 +0000558{
559 *(uint16_t *)ptr = v;
560}
561
bellard2df3b952005-11-19 17:47:39 +0000562static inline void stl_be_p(void *ptr, int v)
bellard5a9fdfe2003-06-15 20:02:25 +0000563{
564 *(uint32_t *)ptr = v;
565}
566
bellard2df3b952005-11-19 17:47:39 +0000567static inline void stq_be_p(void *ptr, uint64_t v)
bellard5a9fdfe2003-06-15 20:02:25 +0000568{
569 *(uint64_t *)ptr = v;
570}
571
572/* float access */
573
balrog8bba3ea2008-12-07 23:44:44 +0000574static inline float32 ldfl_be_p(const void *ptr)
bellard5a9fdfe2003-06-15 20:02:25 +0000575{
bellard53cd6632005-03-13 18:50:23 +0000576 return *(float32 *)ptr;
bellard5a9fdfe2003-06-15 20:02:25 +0000577}
578
balrog8bba3ea2008-12-07 23:44:44 +0000579static inline float64 ldfq_be_p(const void *ptr)
bellard5a9fdfe2003-06-15 20:02:25 +0000580{
bellard53cd6632005-03-13 18:50:23 +0000581 return *(float64 *)ptr;
bellard5a9fdfe2003-06-15 20:02:25 +0000582}
583
bellard2df3b952005-11-19 17:47:39 +0000584static inline void stfl_be_p(void *ptr, float32 v)
bellard5a9fdfe2003-06-15 20:02:25 +0000585{
bellard53cd6632005-03-13 18:50:23 +0000586 *(float32 *)ptr = v;
bellard5a9fdfe2003-06-15 20:02:25 +0000587}
588
bellard2df3b952005-11-19 17:47:39 +0000589static inline void stfq_be_p(void *ptr, float64 v)
bellard5a9fdfe2003-06-15 20:02:25 +0000590{
bellard53cd6632005-03-13 18:50:23 +0000591 *(float64 *)ptr = v;
bellard5a9fdfe2003-06-15 20:02:25 +0000592}
bellard2df3b952005-11-19 17:47:39 +0000593
594#endif
595
596/* target CPU memory access functions */
597#if defined(TARGET_WORDS_BIGENDIAN)
598#define lduw_p(p) lduw_be_p(p)
599#define ldsw_p(p) ldsw_be_p(p)
600#define ldl_p(p) ldl_be_p(p)
601#define ldq_p(p) ldq_be_p(p)
602#define ldfl_p(p) ldfl_be_p(p)
603#define ldfq_p(p) ldfq_be_p(p)
604#define stw_p(p, v) stw_be_p(p, v)
605#define stl_p(p, v) stl_be_p(p, v)
606#define stq_p(p, v) stq_be_p(p, v)
607#define stfl_p(p, v) stfl_be_p(p, v)
608#define stfq_p(p, v) stfq_be_p(p, v)
609#else
610#define lduw_p(p) lduw_le_p(p)
611#define ldsw_p(p) ldsw_le_p(p)
612#define ldl_p(p) ldl_le_p(p)
613#define ldq_p(p) ldq_le_p(p)
614#define ldfl_p(p) ldfl_le_p(p)
615#define ldfq_p(p) ldfq_le_p(p)
616#define stw_p(p, v) stw_le_p(p, v)
617#define stl_p(p, v) stl_le_p(p, v)
618#define stq_p(p, v) stq_le_p(p, v)
619#define stfl_p(p, v) stfl_le_p(p, v)
620#define stfq_p(p, v) stfq_le_p(p, v)
bellard5a9fdfe2003-06-15 20:02:25 +0000621#endif
622
bellard61382a52003-10-27 21:22:23 +0000623/* MMU memory access macros */
624
pbrook53a59602006-03-25 19:31:22 +0000625#if defined(CONFIG_USER_ONLY)
aurel320e62fd72008-12-08 18:12:11 +0000626#include <assert.h>
627#include "qemu-types.h"
628
pbrook53a59602006-03-25 19:31:22 +0000629/* On some host systems the guest address space is reserved on the host.
630 * This allows the guest address space to be offset to a convenient location.
631 */
632//#define GUEST_BASE 0x20000000
633#define GUEST_BASE 0
634
635/* All direct uses of g2h and h2g need to go away for usermode softmmu. */
636#define g2h(x) ((void *)((unsigned long)(x) + GUEST_BASE))
aurel320e62fd72008-12-08 18:12:11 +0000637#define h2g(x) ({ \
638 unsigned long __ret = (unsigned long)(x) - GUEST_BASE; \
639 /* Check if given address fits target address space */ \
640 assert(__ret == (abi_ulong)__ret); \
641 (abi_ulong)__ret; \
642})
aurel3214cc46b2008-12-08 18:12:18 +0000643#define h2g_valid(x) ({ \
644 unsigned long __guest = (unsigned long)(x) - GUEST_BASE; \
645 (__guest == (abi_ulong)__guest); \
646})
pbrook53a59602006-03-25 19:31:22 +0000647
648#define saddr(x) g2h(x)
649#define laddr(x) g2h(x)
650
651#else /* !CONFIG_USER_ONLY */
bellardc27004e2005-01-03 23:35:10 +0000652/* NOTE: we use double casts if pointers and target_ulong have
653 different sizes */
pbrook53a59602006-03-25 19:31:22 +0000654#define saddr(x) (uint8_t *)(long)(x)
655#define laddr(x) (uint8_t *)(long)(x)
656#endif
657
658#define ldub_raw(p) ldub_p(laddr((p)))
659#define ldsb_raw(p) ldsb_p(laddr((p)))
660#define lduw_raw(p) lduw_p(laddr((p)))
661#define ldsw_raw(p) ldsw_p(laddr((p)))
662#define ldl_raw(p) ldl_p(laddr((p)))
663#define ldq_raw(p) ldq_p(laddr((p)))
664#define ldfl_raw(p) ldfl_p(laddr((p)))
665#define ldfq_raw(p) ldfq_p(laddr((p)))
666#define stb_raw(p, v) stb_p(saddr((p)), v)
667#define stw_raw(p, v) stw_p(saddr((p)), v)
668#define stl_raw(p, v) stl_p(saddr((p)), v)
669#define stq_raw(p, v) stq_p(saddr((p)), v)
670#define stfl_raw(p, v) stfl_p(saddr((p)), v)
671#define stfq_raw(p, v) stfq_p(saddr((p)), v)
bellardc27004e2005-01-03 23:35:10 +0000672
673
ths5fafdf22007-09-16 21:08:06 +0000674#if defined(CONFIG_USER_ONLY)
bellard61382a52003-10-27 21:22:23 +0000675
676/* if user mode, no other memory access functions */
677#define ldub(p) ldub_raw(p)
678#define ldsb(p) ldsb_raw(p)
679#define lduw(p) lduw_raw(p)
680#define ldsw(p) ldsw_raw(p)
681#define ldl(p) ldl_raw(p)
682#define ldq(p) ldq_raw(p)
683#define ldfl(p) ldfl_raw(p)
684#define ldfq(p) ldfq_raw(p)
685#define stb(p, v) stb_raw(p, v)
686#define stw(p, v) stw_raw(p, v)
687#define stl(p, v) stl_raw(p, v)
688#define stq(p, v) stq_raw(p, v)
689#define stfl(p, v) stfl_raw(p, v)
690#define stfq(p, v) stfq_raw(p, v)
691
692#define ldub_code(p) ldub_raw(p)
693#define ldsb_code(p) ldsb_raw(p)
694#define lduw_code(p) lduw_raw(p)
695#define ldsw_code(p) ldsw_raw(p)
696#define ldl_code(p) ldl_raw(p)
j_mayerbc98a7e2007-04-04 07:55:12 +0000697#define ldq_code(p) ldq_raw(p)
bellard61382a52003-10-27 21:22:23 +0000698
699#define ldub_kernel(p) ldub_raw(p)
700#define ldsb_kernel(p) ldsb_raw(p)
701#define lduw_kernel(p) lduw_raw(p)
702#define ldsw_kernel(p) ldsw_raw(p)
703#define ldl_kernel(p) ldl_raw(p)
j_mayerbc98a7e2007-04-04 07:55:12 +0000704#define ldq_kernel(p) ldq_raw(p)
bellard0ac4bd52004-01-04 15:44:17 +0000705#define ldfl_kernel(p) ldfl_raw(p)
706#define ldfq_kernel(p) ldfq_raw(p)
bellard61382a52003-10-27 21:22:23 +0000707#define stb_kernel(p, v) stb_raw(p, v)
708#define stw_kernel(p, v) stw_raw(p, v)
709#define stl_kernel(p, v) stl_raw(p, v)
710#define stq_kernel(p, v) stq_raw(p, v)
bellard0ac4bd52004-01-04 15:44:17 +0000711#define stfl_kernel(p, v) stfl_raw(p, v)
712#define stfq_kernel(p, vt) stfq_raw(p, v)
bellard61382a52003-10-27 21:22:23 +0000713
714#endif /* defined(CONFIG_USER_ONLY) */
715
bellard5a9fdfe2003-06-15 20:02:25 +0000716/* page related stuff */
717
aurel3203875442008-04-22 20:45:18 +0000718#define TARGET_PAGE_SIZE (1 << TARGET_PAGE_BITS)
bellard5a9fdfe2003-06-15 20:02:25 +0000719#define TARGET_PAGE_MASK ~(TARGET_PAGE_SIZE - 1)
720#define TARGET_PAGE_ALIGN(addr) (((addr) + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK)
721
pbrook53a59602006-03-25 19:31:22 +0000722/* ??? These should be the larger of unsigned long and target_ulong. */
bellard83fb7ad2004-07-05 21:25:26 +0000723extern unsigned long qemu_real_host_page_size;
724extern unsigned long qemu_host_page_bits;
725extern unsigned long qemu_host_page_size;
726extern unsigned long qemu_host_page_mask;
bellard5a9fdfe2003-06-15 20:02:25 +0000727
bellard83fb7ad2004-07-05 21:25:26 +0000728#define HOST_PAGE_ALIGN(addr) (((addr) + qemu_host_page_size - 1) & qemu_host_page_mask)
bellard5a9fdfe2003-06-15 20:02:25 +0000729
730/* same as PROT_xxx */
731#define PAGE_READ 0x0001
732#define PAGE_WRITE 0x0002
733#define PAGE_EXEC 0x0004
734#define PAGE_BITS (PAGE_READ | PAGE_WRITE | PAGE_EXEC)
735#define PAGE_VALID 0x0008
736/* original state of the write flag (used when tracking self-modifying
737 code */
ths5fafdf22007-09-16 21:08:06 +0000738#define PAGE_WRITE_ORG 0x0010
balrog50a95692007-12-12 01:16:23 +0000739#define PAGE_RESERVED 0x0020
bellard5a9fdfe2003-06-15 20:02:25 +0000740
741void page_dump(FILE *f);
pbrook53a59602006-03-25 19:31:22 +0000742int page_get_flags(target_ulong address);
743void page_set_flags(target_ulong start, target_ulong end, int flags);
ths3d97b402007-11-02 19:02:07 +0000744int page_check_range(target_ulong start, target_ulong len, int flags);
bellard5a9fdfe2003-06-15 20:02:25 +0000745
bellard26a5f132008-05-28 12:30:31 +0000746void cpu_exec_init_all(unsigned long tb_size);
thsc5be9f02007-02-28 20:20:53 +0000747CPUState *cpu_copy(CPUState *env);
748
ths5fafdf22007-09-16 21:08:06 +0000749void cpu_dump_state(CPUState *env, FILE *f,
bellard7fe48482004-10-09 18:08:01 +0000750 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
751 int flags);
j_mayer76a66252007-03-07 08:32:30 +0000752void cpu_dump_statistics (CPUState *env, FILE *f,
753 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
754 int flags);
bellard7fe48482004-10-09 18:08:01 +0000755
blueswir17d99a002009-01-14 19:00:36 +0000756void noreturn cpu_abort(CPUState *env, const char *fmt, ...)
757 __attribute__ ((__format__ (__printf__, 2, 3)));
bellardf0aca822005-11-21 23:22:06 +0000758extern CPUState *first_cpu;
bellarde2f22892003-06-25 16:09:48 +0000759extern CPUState *cpu_single_env;
pbrook2e70f6e2008-06-29 01:03:05 +0000760extern int64_t qemu_icount;
761extern int use_icount;
bellard5a9fdfe2003-06-15 20:02:25 +0000762
bellard9acbed02004-02-16 21:57:02 +0000763#define CPU_INTERRUPT_EXIT 0x01 /* wants exit from main loop */
764#define CPU_INTERRUPT_HARD 0x02 /* hardware interrupt pending */
765#define CPU_INTERRUPT_EXITTB 0x04 /* exit the current TB (use for x86 a20 case) */
bellardef792f92004-05-17 20:19:32 +0000766#define CPU_INTERRUPT_TIMER 0x08 /* internal timer exception pending */
bellard98699962005-11-26 10:29:22 +0000767#define CPU_INTERRUPT_FIQ 0x10 /* Fast interrupt pending. */
bellardba3c64f2005-12-05 20:31:52 +0000768#define CPU_INTERRUPT_HALT 0x20 /* CPU halt wanted */
bellard3b21e032006-09-24 18:41:56 +0000769#define CPU_INTERRUPT_SMI 0x40 /* (x86 only) SMI interrupt pending */
pbrook6658ffb2007-03-16 23:58:11 +0000770#define CPU_INTERRUPT_DEBUG 0x80 /* Debug event occured. */
ths0573fbf2007-09-23 15:28:04 +0000771#define CPU_INTERRUPT_VIRQ 0x100 /* virtual interrupt pending. */
aurel32474ea842008-04-13 16:08:15 +0000772#define CPU_INTERRUPT_NMI 0x200 /* NMI pending. */
bellard98699962005-11-26 10:29:22 +0000773
bellard46907642003-07-07 12:17:46 +0000774void cpu_interrupt(CPUState *s, int mask);
bellardb54ad042004-05-20 13:42:52 +0000775void cpu_reset_interrupt(CPUState *env, int mask);
bellard68a79312003-06-30 13:12:32 +0000776
aliguoria1d1bb32008-11-18 20:07:32 +0000777/* Breakpoint/watchpoint flags */
778#define BP_MEM_READ 0x01
779#define BP_MEM_WRITE 0x02
780#define BP_MEM_ACCESS (BP_MEM_READ | BP_MEM_WRITE)
aliguori06d55cc2008-11-18 20:24:06 +0000781#define BP_STOP_BEFORE_ACCESS 0x04
aliguori6e140f22008-11-18 20:37:55 +0000782#define BP_WATCHPOINT_HIT 0x08
aliguoria1d1bb32008-11-18 20:07:32 +0000783#define BP_GDB 0x10
aliguori2dc9f412008-11-18 20:56:59 +0000784#define BP_CPU 0x20
aliguoria1d1bb32008-11-18 20:07:32 +0000785
786int cpu_breakpoint_insert(CPUState *env, target_ulong pc, int flags,
787 CPUBreakpoint **breakpoint);
788int cpu_breakpoint_remove(CPUState *env, target_ulong pc, int flags);
789void cpu_breakpoint_remove_by_ref(CPUState *env, CPUBreakpoint *breakpoint);
790void cpu_breakpoint_remove_all(CPUState *env, int mask);
791int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len,
792 int flags, CPUWatchpoint **watchpoint);
793int cpu_watchpoint_remove(CPUState *env, target_ulong addr,
794 target_ulong len, int flags);
795void cpu_watchpoint_remove_by_ref(CPUState *env, CPUWatchpoint *watchpoint);
796void cpu_watchpoint_remove_all(CPUState *env, int mask);
edgar_igl60897d32008-05-09 08:25:14 +0000797
798#define SSTEP_ENABLE 0x1 /* Enable simulated HW single stepping */
799#define SSTEP_NOIRQ 0x2 /* Do not use IRQ while single stepping */
800#define SSTEP_NOTIMER 0x4 /* Do not Timers while single stepping */
801
bellardc33a3462003-07-29 20:50:33 +0000802void cpu_single_step(CPUState *env, int enabled);
bellardd95dc322004-06-20 12:35:26 +0000803void cpu_reset(CPUState *s);
bellard4c3a88a2003-07-26 12:06:08 +0000804
bellard13eb76e2004-01-24 15:23:36 +0000805/* Return the physical page corresponding to a virtual one. Use it
806 only for debugging because no protection checks are done. Return -1
807 if no page found. */
j_mayer9b3c35e2007-04-07 11:21:28 +0000808target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr);
bellard13eb76e2004-01-24 15:23:36 +0000809
ths5fafdf22007-09-16 21:08:06 +0000810#define CPU_LOG_TB_OUT_ASM (1 << 0)
bellard9fddaa02004-05-21 12:59:32 +0000811#define CPU_LOG_TB_IN_ASM (1 << 1)
bellardf193c792004-03-21 17:06:25 +0000812#define CPU_LOG_TB_OP (1 << 2)
813#define CPU_LOG_TB_OP_OPT (1 << 3)
814#define CPU_LOG_INT (1 << 4)
815#define CPU_LOG_EXEC (1 << 5)
816#define CPU_LOG_PCALL (1 << 6)
bellardfd872592004-05-12 19:11:15 +0000817#define CPU_LOG_IOPORT (1 << 7)
bellard9fddaa02004-05-21 12:59:32 +0000818#define CPU_LOG_TB_CPU (1 << 8)
bellardf193c792004-03-21 17:06:25 +0000819
820/* define log items */
821typedef struct CPULogItem {
822 int mask;
823 const char *name;
824 const char *help;
825} CPULogItem;
826
blueswir1c7cd6a32008-10-02 18:27:46 +0000827extern const CPULogItem cpu_log_items[];
bellardf193c792004-03-21 17:06:25 +0000828
bellard34865132003-10-05 14:28:56 +0000829void cpu_set_log(int log_flags);
830void cpu_set_log_filename(const char *filename);
bellardf193c792004-03-21 17:06:25 +0000831int cpu_str_to_log_mask(const char *str);
bellard34865132003-10-05 14:28:56 +0000832
bellard09683d32004-01-04 23:49:41 +0000833/* IO ports API */
834
835/* NOTE: as these functions may be even used when there is an isa
836 brige on non x86 targets, we always defined them */
837#ifndef NO_CPU_IO_DEFS
838void cpu_outb(CPUState *env, int addr, int val);
839void cpu_outw(CPUState *env, int addr, int val);
840void cpu_outl(CPUState *env, int addr, int val);
841int cpu_inb(CPUState *env, int addr);
842int cpu_inw(CPUState *env, int addr);
843int cpu_inl(CPUState *env, int addr);
844#endif
845
aurel3200f82b82008-04-27 21:12:55 +0000846/* address in the RAM (different from a physical address) */
847#ifdef USE_KQEMU
848typedef uint32_t ram_addr_t;
849#else
850typedef unsigned long ram_addr_t;
851#endif
852
bellard33417e72003-08-10 21:47:01 +0000853/* memory API */
854
aurel3200f82b82008-04-27 21:12:55 +0000855extern ram_addr_t phys_ram_size;
bellardedf75d52004-01-04 17:43:30 +0000856extern int phys_ram_fd;
857extern uint8_t *phys_ram_base;
bellard1ccde1c2004-02-06 19:46:14 +0000858extern uint8_t *phys_ram_dirty;
aurel3200f82b82008-04-27 21:12:55 +0000859extern ram_addr_t ram_size;
bellardedf75d52004-01-04 17:43:30 +0000860
861/* physical memory access */
pbrook0f459d12008-06-09 00:20:13 +0000862
863/* MMIO pages are identified by a combination of an IO device index and
864 3 flags. The ROMD code stores the page ram offset in iotlb entry,
865 so only a limited number of ids are avaiable. */
866
867#define IO_MEM_SHIFT 3
bellard98699962005-11-26 10:29:22 +0000868#define IO_MEM_NB_ENTRIES (1 << (TARGET_PAGE_BITS - IO_MEM_SHIFT))
bellardedf75d52004-01-04 17:43:30 +0000869
870#define IO_MEM_RAM (0 << IO_MEM_SHIFT) /* hardcoded offset */
871#define IO_MEM_ROM (1 << IO_MEM_SHIFT) /* hardcoded offset */
872#define IO_MEM_UNASSIGNED (2 << IO_MEM_SHIFT)
pbrook0f459d12008-06-09 00:20:13 +0000873#define IO_MEM_NOTDIRTY (3 << IO_MEM_SHIFT)
874
875/* Acts like a ROM when read and like a device when written. */
bellard2a4188a2006-06-25 21:54:59 +0000876#define IO_MEM_ROMD (1)
blueswir1db7b5422007-05-26 17:36:03 +0000877#define IO_MEM_SUBPAGE (2)
blueswir14254fab2008-01-01 16:57:19 +0000878#define IO_MEM_SUBWIDTH (4)
bellardedf75d52004-01-04 17:43:30 +0000879
pbrook0f459d12008-06-09 00:20:13 +0000880/* Flags stored in the low bits of the TLB virtual address. These are
881 defined so that fast path ram access is all zeros. */
882/* Zero if TLB entry is valid. */
883#define TLB_INVALID_MASK (1 << 3)
884/* Set if TLB entry references a clean RAM page. The iotlb entry will
885 contain the page physical address. */
886#define TLB_NOTDIRTY (1 << 4)
887/* Set if TLB entry is an IO callback. */
888#define TLB_MMIO (1 << 5)
889
bellard77279942004-06-03 14:08:36 +0000890typedef void CPUWriteMemoryFunc(void *opaque, target_phys_addr_t addr, uint32_t value);
891typedef uint32_t CPUReadMemoryFunc(void *opaque, target_phys_addr_t addr);
bellard33417e72003-08-10 21:47:01 +0000892
pbrook8da3ff12008-12-01 18:59:50 +0000893void cpu_register_physical_memory_offset(target_phys_addr_t start_addr,
894 ram_addr_t size,
895 ram_addr_t phys_offset,
896 ram_addr_t region_offset);
897static inline void cpu_register_physical_memory(target_phys_addr_t start_addr,
898 ram_addr_t size,
899 ram_addr_t phys_offset)
900{
901 cpu_register_physical_memory_offset(start_addr, size, phys_offset, 0);
902}
903
aurel3200f82b82008-04-27 21:12:55 +0000904ram_addr_t cpu_get_physical_page_desc(target_phys_addr_t addr);
905ram_addr_t qemu_ram_alloc(ram_addr_t);
bellarde9a1ab12007-02-08 23:08:38 +0000906void qemu_ram_free(ram_addr_t addr);
bellard33417e72003-08-10 21:47:01 +0000907int cpu_register_io_memory(int io_index,
908 CPUReadMemoryFunc **mem_read,
bellard77279942004-06-03 14:08:36 +0000909 CPUWriteMemoryFunc **mem_write,
910 void *opaque);
bellard8926b512004-10-10 15:14:20 +0000911CPUWriteMemoryFunc **cpu_get_io_memory_write(int io_index);
912CPUReadMemoryFunc **cpu_get_io_memory_read(int io_index);
bellard33417e72003-08-10 21:47:01 +0000913
bellard2e126692004-04-25 21:28:44 +0000914void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
bellard13eb76e2004-01-24 15:23:36 +0000915 int len, int is_write);
ths5fafdf22007-09-16 21:08:06 +0000916static inline void cpu_physical_memory_read(target_phys_addr_t addr,
bellard2e126692004-04-25 21:28:44 +0000917 uint8_t *buf, int len)
bellard8b1f24b2004-02-25 23:24:38 +0000918{
919 cpu_physical_memory_rw(addr, buf, len, 0);
920}
ths5fafdf22007-09-16 21:08:06 +0000921static inline void cpu_physical_memory_write(target_phys_addr_t addr,
bellard2e126692004-04-25 21:28:44 +0000922 const uint8_t *buf, int len)
bellard8b1f24b2004-02-25 23:24:38 +0000923{
924 cpu_physical_memory_rw(addr, (uint8_t *)buf, len, 1);
925}
bellardaab33092005-10-30 20:48:42 +0000926uint32_t ldub_phys(target_phys_addr_t addr);
927uint32_t lduw_phys(target_phys_addr_t addr);
bellard8df1cd02005-01-28 22:37:22 +0000928uint32_t ldl_phys(target_phys_addr_t addr);
bellardaab33092005-10-30 20:48:42 +0000929uint64_t ldq_phys(target_phys_addr_t addr);
bellard8df1cd02005-01-28 22:37:22 +0000930void stl_phys_notdirty(target_phys_addr_t addr, uint32_t val);
j_mayerbc98a7e2007-04-04 07:55:12 +0000931void stq_phys_notdirty(target_phys_addr_t addr, uint64_t val);
bellardaab33092005-10-30 20:48:42 +0000932void stb_phys(target_phys_addr_t addr, uint32_t val);
933void stw_phys(target_phys_addr_t addr, uint32_t val);
bellard8df1cd02005-01-28 22:37:22 +0000934void stl_phys(target_phys_addr_t addr, uint32_t val);
bellardaab33092005-10-30 20:48:42 +0000935void stq_phys(target_phys_addr_t addr, uint64_t val);
bellard8b1f24b2004-02-25 23:24:38 +0000936
ths5fafdf22007-09-16 21:08:06 +0000937void cpu_physical_memory_write_rom(target_phys_addr_t addr,
bellardd0ecd2a2006-04-23 17:14:48 +0000938 const uint8_t *buf, int len);
ths5fafdf22007-09-16 21:08:06 +0000939int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
bellard8b1f24b2004-02-25 23:24:38 +0000940 uint8_t *buf, int len, int is_write);
bellard13eb76e2004-01-24 15:23:36 +0000941
aliguori74576192008-10-06 14:02:03 +0000942#define VGA_DIRTY_FLAG 0x01
943#define CODE_DIRTY_FLAG 0x02
944#define KQEMU_DIRTY_FLAG 0x04
945#define MIGRATION_DIRTY_FLAG 0x08
bellard0a962c02005-02-10 22:00:27 +0000946
bellard1ccde1c2004-02-06 19:46:14 +0000947/* read dirty bit (return 0 or 1) */
bellard04c504c2005-08-21 09:24:50 +0000948static inline int cpu_physical_memory_is_dirty(ram_addr_t addr)
bellard1ccde1c2004-02-06 19:46:14 +0000949{
bellard0a962c02005-02-10 22:00:27 +0000950 return phys_ram_dirty[addr >> TARGET_PAGE_BITS] == 0xff;
951}
952
ths5fafdf22007-09-16 21:08:06 +0000953static inline int cpu_physical_memory_get_dirty(ram_addr_t addr,
bellard0a962c02005-02-10 22:00:27 +0000954 int dirty_flags)
955{
956 return phys_ram_dirty[addr >> TARGET_PAGE_BITS] & dirty_flags;
bellard1ccde1c2004-02-06 19:46:14 +0000957}
958
bellard04c504c2005-08-21 09:24:50 +0000959static inline void cpu_physical_memory_set_dirty(ram_addr_t addr)
bellard1ccde1c2004-02-06 19:46:14 +0000960{
bellard0a962c02005-02-10 22:00:27 +0000961 phys_ram_dirty[addr >> TARGET_PAGE_BITS] = 0xff;
bellard1ccde1c2004-02-06 19:46:14 +0000962}
963
bellard04c504c2005-08-21 09:24:50 +0000964void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
bellard0a962c02005-02-10 22:00:27 +0000965 int dirty_flags);
bellard04c504c2005-08-21 09:24:50 +0000966void cpu_tlb_update_dirty(CPUState *env);
bellard1ccde1c2004-02-06 19:46:14 +0000967
aliguori74576192008-10-06 14:02:03 +0000968int cpu_physical_memory_set_dirty_tracking(int enable);
969
970int cpu_physical_memory_get_dirty_tracking(void);
971
aliguori2bec46d2008-11-24 20:21:41 +0000972void cpu_physical_sync_dirty_bitmap(target_phys_addr_t start_addr, target_phys_addr_t end_addr);
973
bellarde3db7222005-01-26 22:00:47 +0000974void dump_exec_info(FILE *f,
975 int (*cpu_fprintf)(FILE *f, const char *fmt, ...));
976
aliguorif65ed4c2008-12-09 20:09:57 +0000977/* Coalesced MMIO regions are areas where write operations can be reordered.
978 * This usually implies that write operations are side-effect free. This allows
979 * batching which can make a major impact on performance when using
980 * virtualization.
981 */
982void qemu_register_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size);
983
984void qemu_unregister_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size);
985
bellardeffedbc2006-07-13 23:00:40 +0000986/*******************************************/
987/* host CPU ticks (if available) */
988
malce58ffeb2009-01-14 18:39:49 +0000989#if defined(_ARCH_PPC)
bellardeffedbc2006-07-13 23:00:40 +0000990
ths5fafdf22007-09-16 21:08:06 +0000991static inline uint32_t get_tbl(void)
bellardeffedbc2006-07-13 23:00:40 +0000992{
993 uint32_t tbl;
994 asm volatile("mftb %0" : "=r" (tbl));
995 return tbl;
996}
997
ths5fafdf22007-09-16 21:08:06 +0000998static inline uint32_t get_tbu(void)
bellardeffedbc2006-07-13 23:00:40 +0000999{
1000 uint32_t tbl;
1001 asm volatile("mftbu %0" : "=r" (tbl));
1002 return tbl;
1003}
1004
1005static inline int64_t cpu_get_real_ticks(void)
1006{
1007 uint32_t l, h, h1;
1008 /* NOTE: we test if wrapping has occurred */
1009 do {
1010 h = get_tbu();
1011 l = get_tbl();
1012 h1 = get_tbu();
1013 } while (h != h1);
1014 return ((int64_t)h << 32) | l;
1015}
1016
1017#elif defined(__i386__)
1018
1019static inline int64_t cpu_get_real_ticks(void)
bellard5f1ce942006-02-08 22:40:15 +00001020{
1021 int64_t val;
1022 asm volatile ("rdtsc" : "=A" (val));
1023 return val;
1024}
1025
bellardeffedbc2006-07-13 23:00:40 +00001026#elif defined(__x86_64__)
1027
1028static inline int64_t cpu_get_real_ticks(void)
1029{
1030 uint32_t low,high;
1031 int64_t val;
1032 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1033 val = high;
1034 val <<= 32;
1035 val |= low;
1036 return val;
1037}
1038
aurel32f54b3f92008-04-12 20:14:54 +00001039#elif defined(__hppa__)
1040
1041static inline int64_t cpu_get_real_ticks(void)
1042{
1043 int val;
1044 asm volatile ("mfctl %%cr16, %0" : "=r"(val));
1045 return val;
1046}
1047
bellardeffedbc2006-07-13 23:00:40 +00001048#elif defined(__ia64)
1049
1050static inline int64_t cpu_get_real_ticks(void)
1051{
1052 int64_t val;
1053 asm volatile ("mov %0 = ar.itc" : "=r"(val) :: "memory");
1054 return val;
1055}
1056
1057#elif defined(__s390__)
1058
1059static inline int64_t cpu_get_real_ticks(void)
1060{
1061 int64_t val;
1062 asm volatile("stck 0(%1)" : "=m" (val) : "a" (&val) : "cc");
1063 return val;
1064}
1065
blueswir131422552007-04-16 18:27:06 +00001066#elif defined(__sparc_v8plus__) || defined(__sparc_v8plusa__) || defined(__sparc_v9__)
bellardeffedbc2006-07-13 23:00:40 +00001067
1068static inline int64_t cpu_get_real_ticks (void)
1069{
1070#if defined(_LP64)
1071 uint64_t rval;
1072 asm volatile("rd %%tick,%0" : "=r"(rval));
1073 return rval;
1074#else
1075 union {
1076 uint64_t i64;
1077 struct {
1078 uint32_t high;
1079 uint32_t low;
1080 } i32;
1081 } rval;
1082 asm volatile("rd %%tick,%1; srlx %1,32,%0"
1083 : "=r"(rval.i32.high), "=r"(rval.i32.low));
1084 return rval.i64;
1085#endif
1086}
thsc4b89d12007-05-05 19:23:11 +00001087
1088#elif defined(__mips__)
1089
1090static inline int64_t cpu_get_real_ticks(void)
1091{
1092#if __mips_isa_rev >= 2
1093 uint32_t count;
1094 static uint32_t cyc_per_count = 0;
1095
1096 if (!cyc_per_count)
1097 __asm__ __volatile__("rdhwr %0, $3" : "=r" (cyc_per_count));
1098
1099 __asm__ __volatile__("rdhwr %1, $2" : "=r" (count));
1100 return (int64_t)(count * cyc_per_count);
1101#else
1102 /* FIXME */
1103 static int64_t ticks = 0;
1104 return ticks++;
1105#endif
1106}
1107
pbrook46152182006-07-30 19:16:29 +00001108#else
1109/* The host CPU doesn't have an easily accessible cycle counter.
ths85028e42007-05-08 22:51:41 +00001110 Just return a monotonically increasing value. This will be
1111 totally wrong, but hopefully better than nothing. */
pbrook46152182006-07-30 19:16:29 +00001112static inline int64_t cpu_get_real_ticks (void)
1113{
1114 static int64_t ticks = 0;
1115 return ticks++;
1116}
bellardeffedbc2006-07-13 23:00:40 +00001117#endif
1118
1119/* profiling */
1120#ifdef CONFIG_PROFILER
1121static inline int64_t profile_getclock(void)
1122{
1123 return cpu_get_real_ticks();
1124}
1125
bellard5f1ce942006-02-08 22:40:15 +00001126extern int64_t kqemu_time, kqemu_time_start;
1127extern int64_t qemu_time, qemu_time_start;
1128extern int64_t tlb_flush_time;
1129extern int64_t kqemu_exec_count;
1130extern int64_t dev_time;
1131extern int64_t kqemu_ret_int_count;
1132extern int64_t kqemu_ret_excp_count;
1133extern int64_t kqemu_ret_intr_count;
bellard5f1ce942006-02-08 22:40:15 +00001134#endif
1135
bellard5a9fdfe2003-06-15 20:02:25 +00001136#endif /* CPU_ALL_H */